Reduce $Id to its normal form
[dbsrgits/SQL-Translator.git] / lib / SQL / Translator / Schema / Object.pm
CommitLineData
b6a880d1 1package SQL::Translator::Schema::Object;
2
3# ----------------------------------------------------------------------
782b5a43 4# $Id$
b6a880d1 5# ----------------------------------------------------------------------
478f608d 6# Copyright (C) 2002-2009 SQLFairy Authors
b6a880d1 7#
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.
11#
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.
16#
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
20# 02111-1307 USA
21# -------------------------------------------------------------------
22
23=pod
24
25=head1 NAME
26
27SQL::Translator::Schema::Object - Base class SQL::Translator Schema objects.
28
29=head1 SYNOPSIS
30
31=head1 DESCSIPTION
32
9371be50 33Base class for Schema objects. Sub classes L<Class::Base> and adds the following
34extra functionality.
b6a880d1 35
36=cut
37
38use strict;
39use Class::Base;
9371be50 40use base 'Class::Data::Inheritable';
b6a880d1 41use base 'Class::Base';
7db5f121 42use Class::MakeMethods::Utility::Ref qw( ref_compare );
b6a880d1 43
da06ac74 44use vars qw[ $VERSION ];
45
46$VERSION = '1.99';
47
48
9371be50 49=head1 Construction
b6a880d1 50
4faaaac6 51Derived classes should declare their attributes using the C<_attributes>
9371be50 52method. They can then inherit the C<init> method from here which will call
53accessors of the same name for any values given in the hash passed to C<new>.
54Note that you will have to impliment the accessors your self and we expect perl
55style methods; call with no args to get and with arg to set.
56
57e.g. If we setup our class as follows;
58
59 package SQL::Translator::Schema::Table;
60 use base qw/SQL::Translator::Schema::Object/;
61
62 __PACKAGE__->_attributes( qw/schema name/ );
63
64 sub name { ... }
65 sub schema { ... }
66
67Then we can construct it with
68
69 my $table = SQL::Translator::Schema::Table->new(
70 schema => $schema,
71 name => 'foo',
72 );
73
74and init will call C<< $table->name("foo") >> and C<< $table->schema($schema) >>
75to set it up. Any undefined args will be ignored.
76
77Multiple calls to C<_attributes> are cumulative and sub classes will inherit
78their parents attribute names.
79
80This is currently experimental, but will hopefull go on to form an introspection
81API for the Schema objects.
82
83=cut
84
85
86__PACKAGE__->mk_classdata("__attributes");
b1789409 87
88# Define any global attributes here
89__PACKAGE__->__attributes([qw/extra/]);
9371be50 90
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!
94sub _attributes {
95 my $class = shift;
96 if (@_) { $class->__attributes( [ @{$class->__attributes}, @_ ] ); }
97 return @{$class->__attributes};
98}
99
100# Call accessors for any args in hashref passed
101sub init {
102 my ( $self, $config ) = @_;
103
104 for my $arg ( $self->_attributes ) {
105 next unless defined $config->{$arg};
106 defined $self->$arg( $config->{$arg} ) or return;
107 }
108
109 return $self;
110}
111
b1789409 112# ----------------------------------------------------------------------
113sub extra {
114
115=pod
116
117=head1 Global Attributes
118
119The following attributes are defined here, therefore all schema objects will
120have them.
121
122=head2 extra
123
124Get or set the objects "extra" attibutes (e.g., "ZEROFILL" for MySQL fields).
24324d76 125Call with no args to get all the extra data.
126Call with a single name arg to get the value of the named extra attribute,
127returned as a scalar. Call with a hash or hashref to set extra attributes.
128Returns a hash or a hashref.
b1789409 129
130 $field->extra( qualifier => 'ZEROFILL' );
24324d76 131
132 $qualifier = $field->extra('qualifier');
133
134 %extra = $field->extra;
135 $extra = $field->extra;
136
b1789409 137=cut
138
139 my $self = shift;
24324d76 140 @_ = %{$_[0]} if ref $_[0] eq "HASH";
141 my $extra = $self->{'extra'} ||= {};
b1789409 142
24324d76 143 if (@_==1) {
144 return exists($extra->{$_[0]}) ? $extra->{$_[0]} : undef ;
b1789409 145 }
24324d76 146 elsif (@_) {
147 my %args = @_;
148 while ( my ( $key, $value ) = each %args ) {
149 $extra->{$key} = $value;
150 }
151 }
152
153 return wantarray ? %$extra : $extra;
b1789409 154}
9371be50 155
a9a72385 156
157# ----------------------------------------------------------------------
158sub equals {
159
160=pod
161
162=head2 equals
163
164Determines if this object is the same as another.
165
166 my $isIdentical = $object1->equals( $object2 );
167
168=cut
169
170 my $self = shift;
171 my $other = shift;
172
173 return 0 unless $other;
57059659 174 return 1 if overload::StrVal($self) eq overload::StrVal($other);
a9a72385 175 return 0 unless $other->isa( __PACKAGE__ );
176 return 1;
177}
178
179# ----------------------------------------------------------------------
7db5f121 180sub _compare_objects {
a9a72385 181 my $self = shift;
7db5f121 182 my $obj1 = shift;
183 my $obj2 = shift;
184 my $result = (ref_compare($obj1, $obj2) == 0);
185# if ( !$result ) {
186# use Carp qw(cluck);
187# cluck("How did I get here?");
188# use Data::Dumper;
189# $Data::Dumper::Maxdepth = 1;
190# print "obj1: ", Dumper($obj1), "\n";
191# print "obj2: ", Dumper($obj2), "\n";
192# }
193 return $result;
a9a72385 194}
195
9371be50 196#=============================================================================
197
1981;
b6a880d1 199
200=pod
201
202=head1 SEE ALSO
203
204=head1 TODO
205
206=head1 BUGS
207
208=head1 AUTHOR
209
210Ken Y. Clark E<lt>kclark@cpan.orgE<gt>, Mark Addison E<lt>mark.addison@itn.co.ukE<gt>
211
212=cut