1 package SQL::Translator::Schema::Field;
3 # ----------------------------------------------------------------------
4 # Copyright (C) 2002-2009 SQLFairy Authors
6 # This program is free software; you can redistribute it and/or
7 # modify it under the terms of the GNU General Public License as
8 # published by the Free Software Foundation; version 2.
10 # This program is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 # General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with this program; if not, write to the Free Software
17 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19 # -------------------------------------------------------------------
25 SQL::Translator::Schema::Field - SQL::Translator field object
29 use SQL::Translator::Schema::Field;
30 my $field = SQL::Translator::Schema::Field->new(
37 C<SQL::Translator::Schema::Field> is the field object.
44 use SQL::Translator::Schema::Constants;
45 use SQL::Translator::Utils 'parse_list_arg';
47 use base 'SQL::Translator::Schema::Object';
49 use vars qw($VERSION $TABLE_COUNT $VIEW_COUNT);
53 # Stringify to our name, being careful not to pass any args through so we don't
54 # accidentally set it to undef. We also have to tweak bool so the object is
55 # still true when it doesn't have a name (which shouldn't happen!).
57 '""' => sub { shift->name },
58 'bool' => sub { $_[0]->name || $_[0] },
62 use DBI qw(:sql_types);
64 # Mapping from string to sql contstant
66 integer => SQL_INTEGER,
69 smallint => SQL_SMALLINT,
70 bigint => 9999, # DBI doesn't export a constatn for this. Le suck
74 decimal => SQL_DECIMAL,
75 numeric => SQL_NUMERIC,
81 datetime => SQL_DATETIME,
82 timestamp => SQL_TIMESTAMP,
86 varchar => SQL_VARCHAR,
88 varbinary => SQL_VARBINARY,
91 text => SQL_LONGVARCHAR
95 __PACKAGE__->_attributes( qw/
96 table name data_type size is_primary_key is_nullable
97 is_auto_increment default_value comments is_foreign_key
98 is_unique order sql_data_type
107 my $field = SQL::Translator::Schema::Field->new(
120 Get or set the comments on a field. May be called several times to
121 set and it will accumulate the comments. Called in an array context,
122 returns each comment individually; called in a scalar context, returns
123 all the comments joined on newlines.
125 $field->comments('foo');
126 $field->comments('bar');
127 print join( ', ', $field->comments ); # prints "foo, bar"
134 $arg = $arg->[0] if ref $arg;
135 push @{ $self->{'comments'} }, $arg if $arg;
138 if ( @{ $self->{'comments'} || [] } ) {
140 ? @{ $self->{'comments'} || [] }
141 : join( "\n", @{ $self->{'comments'} || [] } );
144 return wantarray ? () : '';
155 Get or set the field's data type.
157 my $data_type = $field->data_type('integer');
163 $self->{'data_type'} = $_[0];
164 $self->{'sql_data_type'} = $type_mapping{lc $_[0]} || SQL_UNKNOWN_TYPE unless exists $self->{sql_data_type};
166 return $self->{'data_type'} || '';
173 Constant from DBI package representing this data type. See L<DBI/DBI Constants>
179 $self->{sql_data_type} = shift if @_;
180 return $self->{sql_data_type} || 0;
190 Get or set the field's default value. Will return undef if not defined
191 and could return the empty string (it's a valid default value), so don't
192 assume an error like other methods.
194 my $default = $field->default_value('foo');
199 $self->{'default_value'} = shift if @_;
200 return $self->{'default_value'};
207 Get or set the field's "extra" attibutes (e.g., "ZEROFILL" for MySQL).
208 Accepts a hash(ref) of name/value pairs to store; returns a hash.
210 $field->extra( qualifier => 'ZEROFILL' );
211 my %extra = $field->extra;
215 sub foreign_key_reference {
219 =head2 foreign_key_reference
221 Get or set the field's foreign key reference;
223 my $constraint = $field->foreign_key_reference( $constraint );
229 if ( my $arg = shift ) {
230 my $class = 'SQL::Translator::Schema::Constraint';
231 if ( UNIVERSAL::isa( $arg, $class ) ) {
233 'Foreign key reference for ', $self->name, 'already defined'
234 ) if $self->{'foreign_key_reference'};
236 $self->{'foreign_key_reference'} = $arg;
240 "Argument to foreign_key_reference is not an $class object"
245 return $self->{'foreign_key_reference'};
248 sub is_auto_increment {
252 =head2 is_auto_increment
254 Get or set the field's C<is_auto_increment> attribute.
256 my $is_auto = $field->is_auto_increment(1);
260 my ( $self, $arg ) = @_;
262 if ( defined $arg ) {
263 $self->{'is_auto_increment'} = $arg ? 1 : 0;
266 unless ( defined $self->{'is_auto_increment'} ) {
267 if ( my $table = $self->table ) {
268 if ( my $schema = $table->schema ) {
270 $schema->database eq 'PostgreSQL' &&
271 $self->data_type eq 'serial'
273 $self->{'is_auto_increment'} = 1;
279 return $self->{'is_auto_increment'} || 0;
286 =head2 is_foreign_key
288 Returns whether or not the field is a foreign key.
290 my $is_fk = $field->is_foreign_key;
294 my ( $self, $arg ) = @_;
296 unless ( defined $self->{'is_foreign_key'} ) {
297 if ( my $table = $self->table ) {
298 for my $c ( $table->get_constraints ) {
299 if ( $c->type eq FOREIGN_KEY ) {
300 my %fields = map { $_, 1 } $c->fields;
301 if ( $fields{ $self->name } ) {
302 $self->{'is_foreign_key'} = 1;
303 $self->foreign_key_reference( $c );
311 return $self->{'is_foreign_key'} || 0;
320 Get or set whether the field can be null. If not defined, then
321 returns "1" (assumes the field can be null). The argument is evaluated
322 by Perl for True or False, so the following are eqivalent:
324 $is_nullable = $field->is_nullable(0);
325 $is_nullable = $field->is_nullable('');
326 $is_nullable = $field->is_nullable('0');
328 While this is technically a field constraint, it's probably easier to
329 represent this as an attribute of the field. In order keep things
330 consistent, any other constraint on the field (unique, primary, and
331 foreign keys; checks) are represented as table constraints.
335 my ( $self, $arg ) = @_;
337 if ( defined $arg ) {
338 $self->{'is_nullable'} = $arg ? 1 : 0;
342 defined $self->{'is_nullable'} &&
343 $self->{'is_nullable'} == 1 &&
344 $self->is_primary_key
346 $self->{'is_nullable'} = 0;
349 return defined $self->{'is_nullable'} ? $self->{'is_nullable'} : 1;
356 =head2 is_primary_key
358 Get or set the field's C<is_primary_key> attribute. Does not create
359 a table constraint (should it?).
361 my $is_pk = $field->is_primary_key(1);
365 my ( $self, $arg ) = @_;
367 if ( defined $arg ) {
368 $self->{'is_primary_key'} = $arg ? 1 : 0;
371 unless ( defined $self->{'is_primary_key'} ) {
372 if ( my $table = $self->table ) {
373 if ( my $pk = $table->primary_key ) {
374 my %fields = map { $_, 1 } $pk->fields;
375 $self->{'is_primary_key'} = $fields{ $self->name } || 0;
378 $self->{'is_primary_key'} = 0;
383 return $self->{'is_primary_key'} || 0;
392 Determine whether the field has a UNIQUE constraint or not.
394 my $is_unique = $field->is_unique;
400 unless ( defined $self->{'is_unique'} ) {
401 if ( my $table = $self->table ) {
402 for my $c ( $table->get_constraints ) {
403 if ( $c->type eq UNIQUE ) {
404 my %fields = map { $_, 1 } $c->fields;
405 if ( $fields{ $self->name } ) {
406 $self->{'is_unique'} = 1;
414 return $self->{'is_unique'} || 0;
423 Determine whether the field is valid or not.
425 my $ok = $field->is_valid;
430 return $self->error('No name') unless $self->name;
431 return $self->error('No data type') unless $self->data_type;
432 return $self->error('No table object') unless $self->table;
442 Get or set the field's name.
444 my $name = $field->name('foo');
446 The field object will also stringify to its name.
448 my $setter_name = "set_$field";
450 Errors ("No field name") if you try to set a blank name.
457 my $arg = shift || return $self->error( "No field name" );
458 if ( my $table = $self->table ) {
459 return $self->error( qq[Can't use field name "$arg": field exists] )
460 if $table->get_field( $arg );
463 $self->{'name'} = $arg;
466 return $self->{'name'} || '';
473 Read only method to return the fields name with its table name pre-pended.
479 return $self->table.".".$self->name;
488 Get or set the field's order.
490 my $order = $field->order(3);
494 my ( $self, $arg ) = @_;
496 if ( defined $arg && $arg =~ /^\d+$/ ) {
497 $self->{'order'} = $arg;
500 return $self->{'order'} || 0;
507 Shortcut to get the fields schema ($field->table->schema) or undef if it
510 my $schema = $field->schema;
515 if ( my $table = $self->table ) { return $table->schema || undef; }
525 Get or set the field's size. Accepts a string, array or arrayref of
526 numbers and returns a string.
529 $field->size( [ 255 ] );
530 $size = $field->size( 10, 2 );
531 print $size; # prints "10,2"
533 $size = $field->size( '10, 2' );
534 print $size; # prints "10,2"
539 my $numbers = parse_list_arg( @_ );
543 for my $num ( @$numbers ) {
544 if ( defined $num && $num =~ m/^\d+(?:\.\d+)?$/ ) {
548 $self->{'size'} = \@new if @new; # only set if all OK
552 ? @{ $self->{'size'} || [0] }
553 : join( ',', @{ $self->{'size'} || [0] } )
563 Get or set the field's table object. As the table object stringifies this can
564 also be used to get the table name.
566 my $table = $field->table;
567 print "Table name: $table";
572 if ( my $arg = shift ) {
573 return $self->error('Not a table object') unless
574 UNIVERSAL::isa( $arg, 'SQL::Translator::Schema::Table' );
575 $self->{'table'} = $arg;
578 return $self->{'table'};
585 Returns the field exactly as the parser found it
593 $self->{parsed_field} = $value;
594 return $value || $self;
596 return $self->{parsed_field} || $self;
605 Determines if this field is the same as another
607 my $isIdentical = $field1->equals( $field2 );
613 my $case_insensitive = shift;
615 return 0 unless $self->SUPER::equals($other);
616 return 0 unless $case_insensitive ? uc($self->name) eq uc($other->name) : $self->name eq $other->name;
618 # Comparing types: use sql_data_type if both are not 0. Else use string data_type
619 if ($self->sql_data_type && $other->sql_data_type) {
620 return 0 unless $self->sql_data_type == $other->sql_data_type
622 return 0 unless lc($self->data_type) eq lc($other->data_type)
625 return 0 unless $self->size eq $other->size;
628 my $lhs = $self->default_value;
629 $lhs = \'NULL' unless defined $lhs;
630 my $lhs_is_ref = ! ! ref $lhs;
632 my $rhs = $other->default_value;
633 $rhs = \'NULL' unless defined $rhs;
634 my $rhs_is_ref = ! ! ref $rhs;
636 # If only one is a ref, fail. -- rjbs, 2008-12-02
637 return 0 if $lhs_is_ref xor $rhs_is_ref;
639 my $effective_lhs = $lhs_is_ref ? $$lhs : $lhs;
640 my $effective_rhs = $rhs_is_ref ? $$rhs : $rhs;
642 return 0 if $effective_lhs ne $effective_rhs;
645 return 0 unless $self->is_nullable eq $other->is_nullable;
646 # return 0 unless $self->is_unique eq $other->is_unique;
647 return 0 unless $self->is_primary_key eq $other->is_primary_key;
648 # return 0 unless $self->is_foreign_key eq $other->is_foreign_key;
649 return 0 unless $self->is_auto_increment eq $other->is_auto_increment;
650 # return 0 unless $self->comments eq $other->comments;
651 return 0 unless $self->_compare_objects(scalar $self->extra, scalar $other->extra);
657 # Destroy cyclical references.
660 undef $self->{'table'};
661 undef $self->{'foreign_key_reference'};
670 Ken Youens-Clark E<lt>kclark@cpan.orgE<gt>.