1 package SQL::Translator::Schema::Object;
3 # ----------------------------------------------------------------------
5 # ----------------------------------------------------------------------
6 # Copyright (C) 2002-2009 SQLFairy Authors
8 # This program is free software; you can redistribute it and/or
9 # modify it under the terms of the GNU General Public License as
10 # published by the Free Software Foundation; version 2.
12 # This program is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 # General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with this program; if not, write to the Free Software
19 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
21 # -------------------------------------------------------------------
27 SQL::Translator::Schema::Object - Base class SQL::Translator Schema objects.
33 Base class for Schema objects. Sub classes L<Class::Base> and adds the following
40 use base 'Class::Data::Inheritable';
41 use base 'Class::Base';
42 use Class::MakeMethods::Utility::Ref qw( ref_compare );
46 Derived classes should declare their attributes using the C<_attributes>
47 method. They can then inherit the C<init> method from here which will call
48 accessors of the same name for any values given in the hash passed to C<new>.
49 Note that you will have to impliment the accessors your self and we expect perl
50 style methods; call with no args to get and with arg to set.
52 e.g. If we setup our class as follows;
54 package SQL::Translator::Schema::Table;
55 use base qw/SQL::Translator::Schema::Object/;
57 __PACKAGE__->_attributes( qw/schema name/ );
62 Then we can construct it with
64 my $table = SQL::Translator::Schema::Table->new(
69 and init will call C<< $table->name("foo") >> and C<< $table->schema($schema) >>
70 to set it up. Any undefined args will be ignored.
72 Multiple calls to C<_attributes> are cumulative and sub classes will inherit
73 their parents attribute names.
75 This is currently experimental, but will hopefull go on to form an introspection
76 API for the Schema objects.
81 __PACKAGE__->mk_classdata("__attributes");
83 # Define any global attributes here
84 __PACKAGE__->__attributes([qw/extra/]);
86 # Set the classes attribute names. Multiple calls are cumulative.
87 # We need to be careful to create a new ref so that all classes don't end up
88 # with the same ref and hence the same attributes!
91 if (@_) { $class->__attributes( [ @{$class->__attributes}, @_ ] ); }
92 return @{$class->__attributes};
95 # Call accessors for any args in hashref passed
97 my ( $self, $config ) = @_;
99 for my $arg ( $self->_attributes ) {
100 next unless defined $config->{$arg};
101 defined $self->$arg( $config->{$arg} ) or return;
107 # ----------------------------------------------------------------------
112 =head1 Global Attributes
114 The following attributes are defined here, therefore all schema objects will
119 Get or set the objects "extra" attibutes (e.g., "ZEROFILL" for MySQL fields).
120 Call with no args to get all the extra data.
121 Call with a single name arg to get the value of the named extra attribute,
122 returned as a scalar. Call with a hash or hashref to set extra attributes.
123 Returns a hash or a hashref.
125 $field->extra( qualifier => 'ZEROFILL' );
127 $qualifier = $field->extra('qualifier');
129 %extra = $field->extra;
130 $extra = $field->extra;
135 @_ = %{$_[0]} if ref $_[0] eq "HASH";
136 my $extra = $self->{'extra'} ||= {};
139 return exists($extra->{$_[0]}) ? $extra->{$_[0]} : undef ;
143 while ( my ( $key, $value ) = each %args ) {
144 $extra->{$key} = $value;
148 return wantarray ? %$extra : $extra;
152 # ----------------------------------------------------------------------
159 Determines if this object is the same as another.
161 my $isIdentical = $object1->equals( $object2 );
168 return 0 unless $other;
169 return 1 if overload::StrVal($self) eq overload::StrVal($other);
170 return 0 unless $other->isa( __PACKAGE__ );
174 # ----------------------------------------------------------------------
175 sub _compare_objects {
179 my $result = (ref_compare($obj1, $obj2) == 0);
181 # use Carp qw(cluck);
182 # cluck("How did I get here?");
184 # $Data::Dumper::Maxdepth = 1;
185 # print "obj1: ", Dumper($obj1), "\n";
186 # print "obj2: ", Dumper($obj2), "\n";
191 #=============================================================================
205 Ken Y. Clark E<lt>kclark@cpan.orgE<gt>, Mark Addison E<lt>mark.addison@itn.co.ukE<gt>