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