1 package SQL::Translator::Schema::Field;
3 # ----------------------------------------------------------------------
4 # $Id: Field.pm,v 1.16 2004-03-23 21:42:42 grommit 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::Field - SQL::Translator field object
31 use SQL::Translator::Schema::Field;
32 my $field = SQL::Translator::Schema::Field->new(
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);
53 $VERSION = sprintf "%d.%02d", q$Revision: 1.16 $ =~ /(\d+)\.(\d+)/;
55 # Stringify to our name, being careful not to pass any args through so we don't
56 # accidentally set it to undef. We also have to tweak bool so the object is
57 # still true when it doesn't have a name (which shouldn't happen!).
59 '""' => sub { shift->name },
60 'bool' => sub { $_[0]->name || $_[0] },
64 # ----------------------------------------------------------------------
73 my $schema = SQL::Translator::Schema::Field->new(
78 my ( $self, $config ) = @_;
82 table name data_type size is_primary_key is_nullable
83 is_auto_increment default_value comments
86 next unless defined $config->{ $arg };
87 defined $self->$arg( $config->{ $arg } ) or return;
93 # ----------------------------------------------------------------------
100 Get or set the comments on a field. May be called several times to
101 set and it will accumulate the comments. Called in an array context,
102 returns each comment individually; called in a scalar context, returns
103 all the comments joined on newlines.
105 $field->comments('foo');
106 $field->comments('bar');
107 print join( ', ', $field->comments ); # prints "foo, bar"
114 $arg = $arg->[0] if ref $arg;
115 push @{ $self->{'comments'} }, $arg if $arg;
118 if ( @{ $self->{'comments'} || [] } ) {
120 ? @{ $self->{'comments'} || [] }
121 : join( "\n", @{ $self->{'comments'} || [] } );
124 return wantarray ? () : '';
129 # ----------------------------------------------------------------------
136 Get or set the field's data type.
138 my $data_type = $field->data_type('integer');
143 $self->{'data_type'} = shift if @_;
144 return $self->{'data_type'} || '';
147 # ----------------------------------------------------------------------
154 Get or set the field's default value. Will return undef if not defined
155 and could return the empty string (it's a valid default value), so don't
156 assume an error like other methods.
158 my $default = $field->default_value('foo');
162 my ( $self, $arg ) = @_;
163 $self->{'default_value'} = $arg if defined $arg;
164 return $self->{'default_value'};
167 # ----------------------------------------------------------------------
174 Get or set the field's "extra" attibutes (e.g., "ZEROFILL" for MySQL).
175 Accepts a hash(ref) of name/value pairs to store; returns a hash.
177 $field->extra( qualifier => 'ZEROFILL' );
178 my %extra = $field->extra;
183 my $args = ref $_[0] eq 'HASH' ? shift : { @_ };
185 while ( my ( $key, $value ) = each %$args ) {
186 $self->{'extra'}{ $key } = $value;
189 return %{ $self->{'extra'} || {} };
192 # ----------------------------------------------------------------------
193 sub foreign_key_reference {
197 =head2 foreign_key_reference
199 Get or set the field's foreign key reference;
201 my $constraint = $field->foreign_key_reference( $constraint );
207 if ( my $arg = shift ) {
208 my $class = 'SQL::Translator::Schema::Constraint';
209 if ( UNIVERSAL::isa( $arg, $class ) ) {
211 'Foreign key reference for ', $self->name, 'already defined'
212 ) if $self->{'foreign_key_reference'};
214 $self->{'foreign_key_reference'} = $arg;
218 "Argument to foreign_key_reference is not an $class object"
223 return $self->{'foreign_key_reference'};
226 # ----------------------------------------------------------------------
227 sub is_auto_increment {
231 =head2 is_auto_increment
233 Get or set the field's C<is_auto_increment> attribute.
235 my $is_auto = $field->is_auto_increment(1);
239 my ( $self, $arg ) = @_;
241 if ( defined $arg ) {
242 $self->{'is_auto_increment'} = $arg ? 1 : 0;
245 unless ( defined $self->{'is_auto_increment'} ) {
246 if ( my $table = $self->table ) {
247 if ( my $schema = $table->schema ) {
249 $schema->database eq 'PostgreSQL' &&
250 $self->data_type eq 'serial'
252 $self->{'is_auto_increment'} = 1;
258 return $self->{'is_auto_increment'} || 0;
261 # ----------------------------------------------------------------------
266 =head2 is_foreign_key
268 Returns whether or not the field is a foreign key.
270 my $is_fk = $field->is_foreign_key;
274 my ( $self, $arg ) = @_;
276 unless ( defined $self->{'is_foreign_key'} ) {
277 if ( my $table = $self->table ) {
278 for my $c ( $table->get_constraints ) {
279 if ( $c->type eq FOREIGN_KEY ) {
280 my %fields = map { $_, 1 } $c->fields;
281 if ( $fields{ $self->name } ) {
282 $self->{'is_foreign_key'} = 1;
283 $self->foreign_key_reference( $c );
291 return $self->{'is_foreign_key'} || 0;
294 # ----------------------------------------------------------------------
301 Get or set whether the field can be null. If not defined, then
302 returns "1" (assumes the field can be null). The argument is evaluated
303 by Perl for True or False, so the following are eqivalent:
305 $is_nullable = $field->is_nullable(0);
306 $is_nullable = $field->is_nullable('');
307 $is_nullable = $field->is_nullable('0');
309 While this is technically a field constraint, it's probably easier to
310 represent this as an attribute of the field. In order keep things
311 consistent, any other constraint on the field (unique, primary, and
312 foreign keys; checks) are represented as table constraints.
316 my ( $self, $arg ) = @_;
318 if ( defined $arg ) {
319 $self->{'is_nullable'} = $arg ? 1 : 0;
323 defined $self->{'is_nullable'} &&
324 $self->{'is_nullable'} == 1 &&
325 $self->is_primary_key
327 $self->{'is_nullable'} = 0;
330 return defined $self->{'is_nullable'} ? $self->{'is_nullable'} : 1;
333 # ----------------------------------------------------------------------
338 =head2 is_primary_key
340 Get or set the field's C<is_primary_key> attribute. Does not create
341 a table constraint (should it?).
343 my $is_pk = $field->is_primary_key(1);
347 my ( $self, $arg ) = @_;
349 if ( defined $arg ) {
350 $self->{'is_primary_key'} = $arg ? 1 : 0;
353 unless ( defined $self->{'is_primary_key'} ) {
354 if ( my $table = $self->table ) {
355 if ( my $pk = $table->primary_key ) {
356 my %fields = map { $_, 1 } $pk->fields;
357 $self->{'is_primary_key'} = $fields{ $self->name } || 0;
360 $self->{'is_primary_key'} = 0;
365 return $self->{'is_primary_key'} || 0;
368 # ----------------------------------------------------------------------
375 Determine whether the field has a UNIQUE constraint or not.
377 my $is_unique = $field->is_unique;
383 unless ( defined $self->{'is_unique'} ) {
384 if ( my $table = $self->table ) {
385 for my $c ( $table->get_constraints ) {
386 if ( $c->type eq UNIQUE ) {
387 my %fields = map { $_, 1 } $c->fields;
388 if ( $fields{ $self->name } ) {
389 $self->{'is_unique'} = 1;
397 return $self->{'is_unique'} || 0;
400 # ----------------------------------------------------------------------
407 Determine whether the field is valid or not.
409 my $ok = $field->is_valid;
414 return $self->error('No name') unless $self->name;
415 return $self->error('No data type') unless $self->data_type;
416 return $self->error('No table object') unless $self->table;
420 # ----------------------------------------------------------------------
427 Get or set the field's name.
429 my $name = $field->name('foo');
431 The field object will also stringify to its name.
433 my $setter_name = "set_$field";
435 Errors ("No field name") if you try to set a blank name.
442 my $arg = shift || return $self->error( "No field name" );
443 if ( my $table = $self->table ) {
444 return $self->error( qq[Can't use field name "$arg": field exists] )
445 if $table->get_field( $arg );
448 $self->{'name'} = $arg;
451 return $self->{'name'} || '';
458 Read only method to return the fields name with its table name pre-pended.
464 return $self->table.".".$self->name;
467 # ----------------------------------------------------------------------
474 Get or set the field's order.
476 my $order = $field->order(3);
480 my ( $self, $arg ) = @_;
482 if ( defined $arg && $arg =~ /^\d+$/ ) {
483 $self->{'order'} = $arg;
486 return $self->{'order'} || 0;
489 # ----------------------------------------------------------------------
496 Get or set the field's size. Accepts a string, array or arrayref of
497 numbers and returns a string.
500 $field->size( [ 255 ] );
501 $size = $field->size( 10, 2 );
502 print $size; # prints "10,2"
504 $size = $field->size( '10, 2' );
505 print $size; # prints "10,2"
510 my $numbers = parse_list_arg( @_ );
514 for my $num ( @$numbers ) {
515 if ( defined $num && $num =~ m/^\d+(?:\.\d+)?$/ ) {
519 $self->{'size'} = \@new if @new; # only set if all OK
523 ? @{ $self->{'size'} || [0] }
524 : join( ',', @{ $self->{'size'} || [0] } )
528 # ----------------------------------------------------------------------
535 Get or set the field's table object. As the table object stringifies this can
536 also be used to get the table name.
538 my $table = $field->table;
539 print "Table name: $table";
544 if ( my $arg = shift ) {
545 return $self->error('Not a table object') unless
546 UNIVERSAL::isa( $arg, 'SQL::Translator::Schema::Table' );
547 $self->{'table'} = $arg;
550 return $self->{'table'};
553 # ----------------------------------------------------------------------
556 # Destroy cyclical references.
559 undef $self->{'table'};
560 undef $self->{'foreign_key_reference'};
565 # ----------------------------------------------------------------------
571 Ken Y. Clark E<lt>kclark@cpan.orgE<gt>.