1 package SQL::Translator::Schema::Field;
3 # ----------------------------------------------------------------------
4 # $Id: Field.pm,v 1.6 2003-06-06 00:09:25 kycl4rk Exp $
5 # ----------------------------------------------------------------------
6 # Copyright (C) 2003 Ken Y. Clark <kclark@cpan.org>
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::Field - SQL::Translator field object
31 use SQL::Translator::Schema::Field;
32 my $field = SQL::Translator::Schema::Field->new(
34 sql => 'select * from foo',
39 C<SQL::Translator::Schema::Field> is the field object.
47 use SQL::Translator::Schema::Constants;
48 use SQL::Translator::Utils 'parse_list_arg';
50 use base 'Class::Base';
51 use vars qw($VERSION $TABLE_COUNT $VIEW_COUNT);
55 # ----------------------------------------------------------------------
64 my $schema = SQL::Translator::Schema::Field->new;
68 my ( $self, $config ) = @_;
72 table name data_type size is_primary_key is_nullable
73 is_auto_increment default_value
76 next unless defined $config->{ $arg };
77 defined $self->$arg( $config->{ $arg } ) or return;
83 # ----------------------------------------------------------------------
90 Get or set the field's data type.
92 my $data_type = $field->data_type('integer');
97 $self->{'data_type'} = shift if @_;
98 return $self->{'data_type'} || '';
101 # ----------------------------------------------------------------------
108 Get or set the field's default value. Will return undef if not defined
109 and could return the empty string (it's a valid default value), so don't
110 assume an error like other methods.
112 my $default = $field->default_value('foo');
116 my ( $self, $arg ) = @_;
117 $self->{'default_value'} = $arg if defined $arg;
118 return $self->{'default_value'};
121 # ----------------------------------------------------------------------
128 Get or set the field's "extra" attibutes (e.g., "ZEROFILL" for MySQL).
129 Accepts a hash(ref) of name/value pairs to store; returns a hash.
131 $field->extra( qualifier => 'ZEROFILL' );
132 my %extra = $field->extra;
137 my $args = ref $_[0] eq 'HASH' ? shift : { @_ };
139 while ( my ( $key, $value ) = each %$args ) {
140 $self->{'extra'}{ $key } = $value;
143 return %{ $self->{'extra'} || {} };
146 # ----------------------------------------------------------------------
147 sub foreign_key_reference {
151 =head2 foreign_key_reference
153 Get or set the field's foreign key reference;
155 my $constraint = $field->foreign_key_reference( $constraint );
161 if ( my $arg = shift ) {
162 my $class = 'SQL::Translator::Schema::Constraint';
163 if ( UNIVERSAL::isa( $arg, $class ) ) {
165 'Foreign key reference for ', $self->name, 'already defined'
166 ) if $self->{'foreign_key_reference'};
168 $self->{'foreign_key_reference'} = $arg;
172 "Argument to foreign_key_reference is not an $class object"
177 return $self->{'foreign_key_reference'};
180 # ----------------------------------------------------------------------
181 sub is_auto_increment {
185 =head2 is_auto_increment
187 Get or set the field's C<is_auto_increment> attribute.
189 my $is_pk = $field->is_auto_increment(1);
193 my ( $self, $arg ) = @_;
195 if ( defined $arg ) {
196 $self->{'is_auto_increment'} = $arg ? 1 : 0;
199 unless ( defined $self->{'is_auto_increment'} ) {
200 if ( my $table = $self->table ) {
201 if ( my $schema = $table->schema ) {
203 $schema->database eq 'PostgreSQL' &&
204 $self->data_type eq 'serial'
206 $self->{'is_auto_increment'} = 1;
212 return $self->{'is_auto_increment'} || 0;
215 # ----------------------------------------------------------------------
220 =head2 is_foreign_key
222 Returns whether or not the field is a foreign key.
224 my $is_fk = $field->is_foreign_key;
228 my ( $self, $arg ) = @_;
230 unless ( defined $self->{'is_foreign_key'} ) {
231 if ( my $table = $self->table ) {
232 for my $c ( $table->get_constraints ) {
233 if ( $c->type eq FOREIGN_KEY ) {
234 my %fields = map { $_, 1 } $c->fields;
235 if ( $fields{ $self->name } ) {
236 $self->{'is_foreign_key'} = 1;
237 $self->foreign_key_reference( $c );
245 return $self->{'is_foreign_key'} || 0;
249 # ----------------------------------------------------------------------
256 Get or set the whether the field can be null. If not defined, then
257 returns "1" (assumes the field can be null). The argument is evaluated
258 by Perl for True or False, so the following are eqivalent:
260 $is_nullable = $field->is_nullable(0);
261 $is_nullable = $field->is_nullable('');
262 $is_nullable = $field->is_nullable('0');
264 While this is technically a field constraint, it's probably easier to
265 represent this as an attribute of the field. In order keep things
266 consistent, any other constraint on the field (unique, primary, and
267 foreign keys; checks) are represented as table constraints.
271 my ( $self, $arg ) = @_;
273 if ( defined $arg ) {
274 $self->{'is_nullable'} = $arg ? 1 : 0;
277 return defined $self->{'is_nullable'} ? $self->{'is_nullable'} : 1;
280 # ----------------------------------------------------------------------
285 =head2 is_primary_key
287 Get or set the field's C<is_primary_key> attribute. Does not create
288 a table constraint (should it?).
290 my $is_pk = $field->is_primary_key(1);
294 my ( $self, $arg ) = @_;
296 if ( defined $arg ) {
297 $self->{'is_primary_key'} = $arg ? 1 : 0;
300 unless ( defined $self->{'is_primary_key'} ) {
301 if ( my $table = $self->table ) {
302 if ( my $pk = $table->primary_key ) {
303 my %fields = map { $_, 1 } $pk->fields;
304 $self->{'is_primary_key'} = $fields{ $self->name } || 0;
307 $self->{'is_primary_key'} = 0;
312 return $self->{'is_primary_key'} || 0;
315 # ----------------------------------------------------------------------
322 Determine whether the field is valid or not.
324 my $ok = $field->is_valid;
329 return $self->error('No name') unless $self->name;
330 return $self->error('No data type') unless $self->data_type;
331 return $self->error('No table object') unless $self->table;
335 # ----------------------------------------------------------------------
342 Get or set the field's name.
344 my $name = $field->name('foo');
350 if ( my $arg = shift ) {
351 if ( my $table = $self->table ) {
352 return $self->error( qq[Can't use field name "$arg": table exists] )
353 if $table->get_field( $arg );
356 $self->{'name'} = $arg;
359 return $self->{'name'} || '';
362 # ----------------------------------------------------------------------
369 Get or set the field's order.
371 my $order = $field->order(3);
375 my ( $self, $arg ) = @_;
377 if ( defined $arg && $arg =~ /^\d+$/ ) {
378 $self->{'order'} = $arg;
381 return $self->{'order'} || 0;
384 # ----------------------------------------------------------------------
391 Get or set the field's size. Accepts a string, array or arrayref of
392 numbers and returns a string.
395 $field->size( [ 255 ] );
396 $size = $field->size( 10, 2 );
397 print $size; # prints "10,2"
399 $size = $field->size( '10, 2' );
400 print $size; # prints "10,2"
405 my $numbers = parse_list_arg( @_ );
409 for my $num ( @$numbers ) {
410 if ( defined $num && $num =~ m/^\d+(?:\.\d+)?$/ ) {
414 $self->{'size'} = \@new if @new; # only set if all OK
418 ? @{ $self->{'size'} }
419 : join( ',', @{ $self->{'size'} || [0] } )
423 # ----------------------------------------------------------------------
430 Get or set the field's table object.
432 my $table = $field->table;
437 if ( my $arg = shift ) {
438 return $self->error('Not a table object') unless
439 UNIVERSAL::isa( $arg, 'SQL::Translator::Schema::Table' );
440 $self->{'table'} = $arg;
443 return $self->{'table'};
446 # ----------------------------------------------------------------------
449 # Destroy cyclical references.
452 undef $self->{'table'};
453 undef $self->{'foreign_key_reference'};
458 # ----------------------------------------------------------------------
464 Ken Y. Clark E<lt>kclark@cpan.orgE<gt>