1 package SQL::Translator::Schema::Object;
3 # ----------------------------------------------------------------------
4 # $Id: Object.pm,v 1.7 2005-08-10 16:34:47 duality72 Exp $
5 # ----------------------------------------------------------------------
6 # Copyright (C) 2002-4 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 );
44 use vars qw[ $VERSION ];
46 $VERSION = sprintf "%d.%02d", q$Revision: 1.7 $ =~ /(\d+)\.(\d+)/;
51 Derived classes should decalare their attributes using the C<_attributes>
52 method. They can then inherit the C<init> method from here which will call
53 accessors of the same name for any values given in the hash passed to C<new>.
54 Note that you will have to impliment the accessors your self and we expect perl
55 style methods; call with no args to get and with arg to set.
57 e.g. If we setup our class as follows;
59 package SQL::Translator::Schema::Table;
60 use base qw/SQL::Translator::Schema::Object/;
62 __PACKAGE__->_attributes( qw/schema name/ );
67 Then we can construct it with
69 my $table = SQL::Translator::Schema::Table->new(
74 and init will call C<< $table->name("foo") >> and C<< $table->schema($schema) >>
75 to set it up. Any undefined args will be ignored.
77 Multiple calls to C<_attributes> are cumulative and sub classes will inherit
78 their parents attribute names.
80 This is currently experimental, but will hopefull go on to form an introspection
81 API for the Schema objects.
86 __PACKAGE__->mk_classdata("__attributes");
88 # Define any global attributes here
89 __PACKAGE__->__attributes([qw/extra/]);
91 # Set the classes attribute names. Multiple calls are cumulative.
92 # We need to be careful to create a new ref so that all classes don't end up
93 # with the same ref and hence the same attributes!
96 if (@_) { $class->__attributes( [ @{$class->__attributes}, @_ ] ); }
97 return @{$class->__attributes};
100 # Call accessors for any args in hashref passed
102 my ( $self, $config ) = @_;
104 for my $arg ( $self->_attributes ) {
105 next unless defined $config->{$arg};
106 defined $self->$arg( $config->{$arg} ) or return;
112 # ----------------------------------------------------------------------
117 =head1 Global Attributes
119 The following attributes are defined here, therefore all schema objects will
124 Get or set the objects "extra" attibutes (e.g., "ZEROFILL" for MySQL fields).
125 Call with no args to get all the extra data.
126 Call with a single name arg to get the value of the named extra attribute,
127 returned as a scalar. Call with a hash or hashref to set extra attributes.
128 Returns a hash or a hashref.
130 $field->extra( qualifier => 'ZEROFILL' );
132 $qualifier = $field->extra('qualifier');
134 %extra = $field->extra;
135 $extra = $field->extra;
140 @_ = %{$_[0]} if ref $_[0] eq "HASH";
141 my $extra = $self->{'extra'} ||= {};
144 return exists($extra->{$_[0]}) ? $extra->{$_[0]} : undef ;
148 while ( my ( $key, $value ) = each %args ) {
149 $extra->{$key} = $value;
153 return wantarray ? %$extra : $extra;
157 # ----------------------------------------------------------------------
164 Determines if this object is the same as another.
166 my $isIdentical = $object1->equals( $object2 );
173 return 0 unless $other;
174 return 1 if overload::StrVal($self) eq overload::StrVal($other);
175 return 0 unless $other->isa( __PACKAGE__ );
179 # ----------------------------------------------------------------------
180 sub _compare_objects {
184 my $result = (ref_compare($obj1, $obj2) == 0);
186 # use Carp qw(cluck);
187 # cluck("How did I get here?");
189 # $Data::Dumper::Maxdepth = 1;
190 # print "obj1: ", Dumper($obj1), "\n";
191 # print "obj2: ", Dumper($obj2), "\n";
196 #=============================================================================
210 Ken Y. Clark E<lt>kclark@cpan.orgE<gt>, Mark Addison E<lt>mark.addison@itn.co.ukE<gt>