1 package SQL::Translator::Schema::Object;
3 # ----------------------------------------------------------------------
4 # Copyright (C) 2002-2009 SQLFairy Authors
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.
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.
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
19 # -------------------------------------------------------------------
25 SQL::Translator::Schema::Object - Base class SQL::Translator Schema objects.
31 Base class for Schema objects. Sub classes L<Class::Base> and adds the following
38 use base 'Class::Data::Inheritable';
39 use base 'Class::Base';
40 use Class::MakeMethods::Utility::Ref qw( ref_compare );
42 use vars qw[ $VERSION ];
48 Derived classes should declare their attributes using the C<_attributes>
49 method. They can then inherit the C<init> method from here which will call
50 accessors of the same name for any values given in the hash passed to C<new>.
51 Note that you will have to impliment the accessors your self and we expect perl
52 style methods; call with no args to get and with arg to set.
54 e.g. If we setup our class as follows;
56 package SQL::Translator::Schema::Table;
57 use base qw/SQL::Translator::Schema::Object/;
59 __PACKAGE__->_attributes( qw/schema name/ );
64 Then we can construct it with
66 my $table = SQL::Translator::Schema::Table->new(
71 and init will call C<< $table->name("foo") >> and C<< $table->schema($schema) >>
72 to set it up. Any undefined args will be ignored.
74 Multiple calls to C<_attributes> are cumulative and sub classes will inherit
75 their parents attribute names.
77 This is currently experimental, but will hopefull go on to form an introspection
78 API for the Schema objects.
83 __PACKAGE__->mk_classdata("__attributes");
85 # Define any global attributes here
86 __PACKAGE__->__attributes([qw/extra/]);
88 # Set the classes attribute names. Multiple calls are cumulative.
89 # We need to be careful to create a new ref so that all classes don't end up
90 # with the same ref and hence the same attributes!
93 if (@_) { $class->__attributes( [ @{$class->__attributes}, @_ ] ); }
94 return @{$class->__attributes};
97 # Call accessors for any args in hashref passed
99 my ( $self, $config ) = @_;
101 for my $arg ( $self->_attributes ) {
102 next unless defined $config->{$arg};
103 defined $self->$arg( $config->{$arg} ) or return;
109 # ----------------------------------------------------------------------
114 =head1 Global Attributes
116 The following attributes are defined here, therefore all schema objects will
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.
127 $field->extra( qualifier => 'ZEROFILL' );
129 $qualifier = $field->extra('qualifier');
131 %extra = $field->extra;
132 $extra = $field->extra;
137 @_ = %{$_[0]} if ref $_[0] eq "HASH";
138 my $extra = $self->{'extra'} ||= {};
141 return exists($extra->{$_[0]}) ? $extra->{$_[0]} : undef ;
145 while ( my ( $key, $value ) = each %args ) {
146 $extra->{$key} = $value;
150 return wantarray ? %$extra : $extra;
153 # ----------------------------------------------------------------------
158 L</extra> can only be used to get or set "extra" attributes but not to
159 remove some. Call with no args to remove all extra attributes that
160 have been set before. Call with a list of key names to remove
161 certain extra attributes only.
163 # remove all extra attributes
164 $field->remove_extra();
166 # remove timezone and locale attributes only
167 $field->remove_extra(qw/timezone locale/);
171 my ( $self, @keys ) = @_;
173 $self->{'extra'} = {};
176 delete $self->{'extra'}{$_} for @keys;
180 # ----------------------------------------------------------------------
187 Determines if this object is the same as another.
189 my $isIdentical = $object1->equals( $object2 );
196 return 0 unless $other;
197 return 1 if overload::StrVal($self) eq overload::StrVal($other);
198 return 0 unless $other->isa( __PACKAGE__ );
202 # ----------------------------------------------------------------------
203 sub _compare_objects {
207 my $result = (ref_compare($obj1, $obj2) == 0);
212 #=============================================================================
220 Ken Youens-Clark E<lt>kclark@cpan.orgE<gt>,
221 Mark Addison E<lt>mark.addison@itn.co.ukE<gt>.