Remove copyright headers from individual scripts
[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
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/;
41
42 __PACKAGE__->_attributes( qw/schema name/ );
43
44 sub name { ... }
45 sub schema { ... }
46
47Then we can construct it with
48
49 my $table = SQL::Translator::Schema::Table->new(
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
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 ) = @_;
83
84 for my $arg ( $self->_attributes ) {
85 next unless defined $config->{$arg};
86 defined $self->$arg( $config->{$arg} ) or return;
87 }
88
89 return $self;
90}
91
b1789409 92# ----------------------------------------------------------------------
93sub extra {
94
95=pod
96
97=head1 Global Attributes
98
99The following attributes are defined here, therefore all schema objects will
100have them.
101
102=head2 extra
103
104Get or set the objects "extra" attibutes (e.g., "ZEROFILL" for MySQL fields).
24324d76 105Call with no args to get all the extra data.
106Call with a single name arg to get the value of the named extra attribute,
107returned as a scalar. Call with a hash or hashref to set extra attributes.
108Returns a hash or a hashref.
b1789409 109
110 $field->extra( qualifier => 'ZEROFILL' );
24324d76 111
112 $qualifier = $field->extra('qualifier');
113
114 %extra = $field->extra;
115 $extra = $field->extra;
116
b1789409 117=cut
118
119 my $self = shift;
24324d76 120 @_ = %{$_[0]} if ref $_[0] eq "HASH";
121 my $extra = $self->{'extra'} ||= {};
b1789409 122
24324d76 123 if (@_==1) {
124 return exists($extra->{$_[0]}) ? $extra->{$_[0]} : undef ;
b1789409 125 }
24324d76 126 elsif (@_) {
127 my %args = @_;
128 while ( my ( $key, $value ) = each %args ) {
129 $extra->{$key} = $value;
130 }
131 }
132
133 return wantarray ? %$extra : $extra;
b1789409 134}
9371be50 135
12018c09 136# ----------------------------------------------------------------------
137sub remove_extra {
138
139=head2 remove_extra
140
141L</extra> can only be used to get or set "extra" attributes but not to
142remove some. Call with no args to remove all extra attributes that
143have been set before. Call with a list of key names to remove
144certain extra attributes only.
145
146 # remove all extra attributes
147 $field->remove_extra();
148
149 # remove timezone and locale attributes only
150 $field->remove_extra(qw/timezone locale/);
151
152=cut
153
154 my ( $self, @keys ) = @_;
155 unless (@keys) {
156 $self->{'extra'} = {};
157 }
158 else {
159 delete $self->{'extra'}{$_} for @keys;
160 }
161}
a9a72385 162
163# ----------------------------------------------------------------------
164sub equals {
165
166=pod
167
168=head2 equals
169
170Determines if this object is the same as another.
171
172 my $isIdentical = $object1->equals( $object2 );
173
174=cut
175
176 my $self = shift;
177 my $other = shift;
178
179 return 0 unless $other;
57059659 180 return 1 if overload::StrVal($self) eq overload::StrVal($other);
a9a72385 181 return 0 unless $other->isa( __PACKAGE__ );
182 return 1;
183}
184
185# ----------------------------------------------------------------------
7db5f121 186sub _compare_objects {
11ad2df9 187 my $self = shift;
188 my $obj1 = shift;
189 my $obj2 = shift;
7db5f121 190 my $result = (ref_compare($obj1, $obj2) == 0);
11ad2df9 191# if ( !$result ) {
192# use Carp qw(cluck);
193# cluck("How did I get here?");
194# use Data::Dumper;
195# $Data::Dumper::Maxdepth = 1;
196# print "obj1: ", Dumper($obj1), "\n";
197# print "obj2: ", Dumper($obj2), "\n";
198# }
7db5f121 199 return $result;
a9a72385 200}
201
9371be50 202#=============================================================================
203
2041;
b6a880d1 205
206=pod
207
11ad2df9 208=head1 SEE ALSO
209
210=head1 TODO
211
212=head1 BUGS
213
b6a880d1 214=head1 AUTHOR
215
c3b0b535 216Ken Youens-Clark E<lt>kclark@cpan.orgE<gt>,
217Mark Addison E<lt>mark.addison@itn.co.ukE<gt>.
b6a880d1 218
219=cut