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 our $VERSION = '1.59';
30 Derived classes should declare their attributes using the C<_attributes>
31 method. They can then inherit the C<init> method from here which will call
32 accessors of the same name for any values given in the hash passed to C<new>.
33 Note that you will have to impliment the accessors your self and we expect perl
34 style methods; call with no args to get and with arg to set.
36 e.g. If we setup our class as follows;
38 package SQL::Translator::Schema::Table;
39 use base qw/SQL::Translator::Schema::Object/;
41 __PACKAGE__->_attributes( qw/schema name/ );
46 Then we can construct it with
48 my $table = SQL::Translator::Schema::Table->new(
53 and init will call C<< $table->name("foo") >> and C<< $table->schema($schema) >>
54 to set it up. Any undefined args will be ignored.
56 Multiple calls to C<_attributes> are cumulative and sub classes will inherit
57 their parents attribute names.
59 This is currently experimental, but will hopefull go on to form an introspection
60 API for the Schema objects.
65 __PACKAGE__->mk_classdata("__attributes");
67 # Define any global attributes here
68 __PACKAGE__->__attributes([qw/extra/]);
70 # Set the classes attribute names. Multiple calls are cumulative.
71 # We need to be careful to create a new ref so that all classes don't end up
72 # with the same ref and hence the same attributes!
75 if (@_) { $class->__attributes( [ @{$class->__attributes}, @_ ] ); }
76 return @{$class->__attributes};
79 # Call accessors for any args in hashref passed
81 my ( $self, $config ) = @_;
83 for my $arg ( $self->_attributes ) {
84 next unless defined $config->{$arg};
85 defined $self->$arg( $config->{$arg} ) or return;
95 =head1 Global Attributes
97 The following attributes are defined here, therefore all schema objects will
102 Get or set the objects "extra" attibutes (e.g., "ZEROFILL" for MySQL fields).
103 Call with no args to get all the extra data.
104 Call with a single name arg to get the value of the named extra attribute,
105 returned as a scalar. Call with a hash or hashref to set extra attributes.
106 Returns a hash or a hashref.
108 $field->extra( qualifier => 'ZEROFILL' );
110 $qualifier = $field->extra('qualifier');
112 %extra = $field->extra;
113 $extra = $field->extra;
118 @_ = %{$_[0]} if ref $_[0] eq "HASH";
119 my $extra = $self->{'extra'} ||= {};
122 return exists($extra->{$_[0]}) ? $extra->{$_[0]} : undef ;
126 while ( my ( $key, $value ) = each %args ) {
127 $extra->{$key} = $value;
131 return wantarray ? %$extra : $extra;
138 L</extra> can only be used to get or set "extra" attributes but not to
139 remove some. Call with no args to remove all extra attributes that
140 have been set before. Call with a list of key names to remove
141 certain extra attributes only.
143 # remove all extra attributes
144 $field->remove_extra();
146 # remove timezone and locale attributes only
147 $field->remove_extra(qw/timezone locale/);
151 my ( $self, @keys ) = @_;
153 $self->{'extra'} = {};
156 delete $self->{'extra'}{$_} for @keys;
166 Determines if this object is the same as another.
168 my $isIdentical = $object1->equals( $object2 );
175 return 0 unless $other;
176 return 1 if overload::StrVal($self) eq overload::StrVal($other);
177 return 0 unless $other->isa( __PACKAGE__ );
181 sub _compare_objects {
185 my $result = (ref_compare($obj1, $obj2) == 0);
187 # use Carp qw(cluck);
188 # cluck("How did I get here?");
190 # $Data::Dumper::Maxdepth = 1;
191 # print "obj1: ", Dumper($obj1), "\n";
192 # print "obj2: ", Dumper($obj2), "\n";
209 Ken Youens-Clark E<lt>kclark@cpan.orgE<gt>,
210 Mark Addison E<lt>mark.addison@itn.co.ukE<gt>.