1 package SQL::Translator::Schema::Object;
7 SQL::Translator::Schema::Object - Base class SQL::Translator Schema objects.
13 Base class for Schema objects. Sub classes L<Class::Base> and adds the following
21 use base 'Class::Data::Inheritable';
22 use base 'Class::Base';
23 use Class::MakeMethods::Utility::Ref qw( ref_compare );
25 use vars qw[ $VERSION ];
32 Derived classes should declare their attributes using the C<_attributes>
33 method. They can then inherit the C<init> method from here which will call
34 accessors of the same name for any values given in the hash passed to C<new>.
35 Note that you will have to impliment the accessors your self and we expect perl
36 style methods; call with no args to get and with arg to set.
38 e.g. If we setup our class as follows;
40 package SQL::Translator::Schema::Table;
41 use base qw/SQL::Translator::Schema::Object/;
43 __PACKAGE__->_attributes( qw/schema name/ );
48 Then we can construct it with
50 my $table = SQL::Translator::Schema::Table->new(
55 and init will call C<< $table->name("foo") >> and C<< $table->schema($schema) >>
56 to set it up. Any undefined args will be ignored.
58 Multiple calls to C<_attributes> are cumulative and sub classes will inherit
59 their parents attribute names.
61 This is currently experimental, but will hopefull go on to form an introspection
62 API for the Schema objects.
67 __PACKAGE__->mk_classdata("__attributes");
69 # Define any global attributes here
70 __PACKAGE__->__attributes([qw/extra/]);
72 # Set the classes attribute names. Multiple calls are cumulative.
73 # We need to be careful to create a new ref so that all classes don't end up
74 # with the same ref and hence the same attributes!
77 if (@_) { $class->__attributes( [ @{$class->__attributes}, @_ ] ); }
78 return @{$class->__attributes};
81 # Call accessors for any args in hashref passed
83 my ( $self, $config ) = @_;
85 for my $arg ( $self->_attributes ) {
86 next unless defined $config->{$arg};
87 defined $self->$arg( $config->{$arg} ) or return;
97 =head1 Global Attributes
99 The following attributes are defined here, therefore all schema objects will
104 Get or set the objects "extra" attibutes (e.g., "ZEROFILL" for MySQL fields).
105 Call with no args to get all the extra data.
106 Call with a single name arg to get the value of the named extra attribute,
107 returned as a scalar. Call with a hash or hashref to set extra attributes.
108 Returns a hash or a hashref.
110 $field->extra( qualifier => 'ZEROFILL' );
112 $qualifier = $field->extra('qualifier');
114 %extra = $field->extra;
115 $extra = $field->extra;
120 @_ = %{$_[0]} if ref $_[0] eq "HASH";
121 my $extra = $self->{'extra'} ||= {};
124 return exists($extra->{$_[0]}) ? $extra->{$_[0]} : undef ;
128 while ( my ( $key, $value ) = each %args ) {
129 $extra->{$key} = $value;
133 return wantarray ? %$extra : $extra;
140 L</extra> can only be used to get or set "extra" attributes but not to
141 remove some. Call with no args to remove all extra attributes that
142 have been set before. Call with a list of key names to remove
143 certain extra attributes only.
145 # remove all extra attributes
146 $field->remove_extra();
148 # remove timezone and locale attributes only
149 $field->remove_extra(qw/timezone locale/);
153 my ( $self, @keys ) = @_;
155 $self->{'extra'} = {};
158 delete $self->{'extra'}{$_} for @keys;
168 Determines if this object is the same as another.
170 my $isIdentical = $object1->equals( $object2 );
177 return 0 unless $other;
178 return 1 if overload::StrVal($self) eq overload::StrVal($other);
179 return 0 unless $other->isa( __PACKAGE__ );
183 sub _compare_objects {
187 my $result = (ref_compare($obj1, $obj2) == 0);
189 # use Carp qw(cluck);
190 # cluck("How did I get here?");
192 # $Data::Dumper::Maxdepth = 1;
193 # print "obj1: ", Dumper($obj1), "\n";
194 # print "obj2: ", Dumper($obj2), "\n";
211 Ken Youens-Clark E<lt>kclark@cpan.orgE<gt>,
212 Mark Addison E<lt>mark.addison@itn.co.ukE<gt>.