take out duplicate docs
[dbsrgits/SQL-Translator.git] / lib / SQL / Translator / Schema / Object.pm
1 package SQL::Translator::Schema::Object;
2
3 # ----------------------------------------------------------------------
4 # Copyright (C) 2002-2009 SQLFairy Authors
5 #
6 # This program is free software; you can redistribute it and/or
7 # modify it under the terms of the GNU General Public License as
8 # published by the Free Software Foundation; version 2.
9 #
10 # This program is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13 # General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with this program; if not, write to the Free Software
17 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
18 # 02111-1307  USA
19 # -------------------------------------------------------------------
20
21 =pod
22
23 =head1 NAME
24
25 SQL::Translator::Schema::Object - Base class SQL::Translator Schema objects.
26
27 =head1 SYNOPSIS
28
29 =head1 DESCSIPTION
30
31 Base class for Schema objects. Sub classes L<Class::Base> and adds the following
32 extra functionality.
33
34 =cut
35
36 use strict;
37 use Class::Base;
38 use base 'Class::Data::Inheritable';
39 use base 'Class::Base';
40 use Class::MakeMethods::Utility::Ref qw( ref_compare );
41
42 use vars qw[ $VERSION ];
43
44 $VERSION = '1.59';
45
46
47 =head1 Construction
48
49 Derived classes should declare their attributes using the C<_attributes>
50 method. They can then inherit the C<init> method from here which will call
51 accessors of the same name for any values given in the hash passed to C<new>.
52 Note that you will have to impliment the accessors your self and we expect perl
53 style methods; call with no args to get and with arg to set.
54
55 e.g. If we setup our class as follows;
56
57  package SQL::Translator::Schema::Table;
58  use base qw/SQL::Translator::Schema::Object/;
59
60  __PACKAGE__->_attributes( qw/schema name/ );
61
62  sub name   { ... }
63  sub schema { ... }
64
65 Then we can construct it with
66
67  my $table  =  SQL::Translator::Schema::Table->new(
68      schema => $schema,
69      name   => 'foo',
70  );
71
72 and init will call C<< $table->name("foo") >> and C<< $table->schema($schema) >>
73 to set it up. Any undefined args will be ignored.
74
75 Multiple calls to C<_attributes> are cumulative and sub classes will inherit
76 their parents attribute names.
77
78 This is currently experimental, but will hopefull go on to form an introspection
79 API for the Schema objects.
80
81 =cut
82
83
84 __PACKAGE__->mk_classdata("__attributes");
85
86 # Define any global attributes here
87 __PACKAGE__->__attributes([qw/extra/]);
88
89 # Set the classes attribute names. Multiple calls are cumulative.
90 # We need to be careful to create a new ref so that all classes don't end up
91 # with the same ref and hence the same attributes!
92 sub _attributes {
93     my $class = shift;
94     if (@_) { $class->__attributes( [ @{$class->__attributes}, @_ ] ); }
95     return @{$class->__attributes};
96 }
97
98 # Call accessors for any args in hashref passed
99 sub init {
100     my ( $self, $config ) = @_;
101
102     for my $arg ( $self->_attributes ) {
103         next unless defined $config->{$arg};
104         defined $self->$arg( $config->{$arg} ) or return;
105     }
106
107     return $self;
108 }
109
110 sub extra {
111
112 =pod
113
114 =head1 Global Attributes
115
116 The following attributes are defined here, therefore all schema objects will
117 have them.
118
119 =head2 extra
120
121 Get or set the objects "extra" attibutes (e.g., "ZEROFILL" for MySQL fields).
122 Call with no args to get all the extra data.
123 Call with a single name arg to get the value of the named extra attribute,
124 returned as a scalar. Call with a hash or hashref to set extra attributes.
125 Returns a hash or a hashref.
126
127   $field->extra( qualifier => 'ZEROFILL' );
128
129   $qualifier = $field->extra('qualifier');
130
131   %extra = $field->extra;
132   $extra = $field->extra;
133
134 =cut
135
136     my $self = shift;
137     @_ = %{$_[0]} if ref $_[0] eq "HASH";
138     my $extra = $self->{'extra'} ||= {};
139
140     if (@_==1) {
141         return exists($extra->{$_[0]}) ? $extra->{$_[0]} : undef ;
142     }
143     elsif (@_) {
144         my %args = @_;
145         while ( my ( $key, $value ) = each %args ) {
146             $extra->{$key} = $value;
147         }
148     }
149
150     return wantarray ? %$extra : $extra;
151 }
152
153 sub remove_extra {
154
155 =head2 remove_extra
156
157 L</extra> can only be used to get or set "extra" attributes but not to
158 remove some. Call with no args to remove all extra attributes that
159 have been set before. Call with a list of key names to remove
160 certain extra attributes only.
161
162   # remove all extra attributes
163   $field->remove_extra();
164
165   # remove timezone and locale attributes only
166   $field->remove_extra(qw/timezone locale/);
167
168 =cut
169
170     my ( $self, @keys ) = @_;
171     unless (@keys) {
172         $self->{'extra'} = {};
173     }
174     else {
175         delete $self->{'extra'}{$_} for @keys;
176     }
177 }
178
179 sub equals {
180
181 =pod
182
183 =head2 equals
184
185 Determines if this object is the same as another.
186
187   my $isIdentical = $object1->equals( $object2 );
188
189 =cut
190
191     my $self = shift;
192     my $other = shift;
193
194     return 0 unless $other;
195     return 1 if overload::StrVal($self) eq overload::StrVal($other);
196     return 0 unless $other->isa( __PACKAGE__ );
197     return 1;
198 }
199
200 sub _compare_objects {
201    my $self = shift;
202    my $obj1 = shift;
203    my $obj2 = shift;
204    my $result = (ref_compare($obj1, $obj2) == 0);
205 #  if ( !$result ) {
206 #     use Carp qw(cluck);
207 #     cluck("How did I get here?");
208 #     use Data::Dumper;
209 #     $Data::Dumper::Maxdepth = 1;
210 #     print "obj1: ", Dumper($obj1), "\n";
211 #     print "obj2: ", Dumper($obj2), "\n";
212 #  }
213    return $result;
214 }
215
216 1;
217
218 =pod
219
220 =head1 SEE ALSO
221
222 =head1 TODO
223
224 =head1 BUGS
225
226 =head1 AUTHOR
227
228 Ken Youens-Clark E<lt>kclark@cpan.orgE<gt>,
229 Mark Addison E<lt>mark.addison@itn.co.ukE<gt>.
230
231 =cut