1 package SQL::Translator::Schema::Field;
3 # ----------------------------------------------------------------------
4 # $Id: Field.pm,v 1.3 2003-05-05 04:32:39 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;
49 use base 'Class::Base';
50 use vars qw($VERSION $TABLE_COUNT $VIEW_COUNT);
54 # ----------------------------------------------------------------------
63 my $schema = SQL::Translator::Schema::Field->new;
67 my ( $self, $config ) = @_;
69 for my $arg ( qw[ name data_type size is_primary_key nullable table ] ) {
70 next unless defined $config->{ $arg };
71 $self->$arg( $config->{ $arg } ) or return;
76 # ----------------------------------------------------------------------
83 Get or set the field's data type.
85 my $data_type = $field->data_type('integer');
90 $self->{'data_type'} = shift if @_;
91 return $self->{'data_type'} || '';
94 # ----------------------------------------------------------------------
101 Get or set the field's default value. Will return undef if not defined
102 and could return the empty string (it's a valid default value), so don't
103 assume an error like other methods.
105 my $default = $field->default_value('foo');
109 my ( $self, $arg ) = @_;
110 $self->{'default_value'} = $arg if defined $arg;
111 return $self->{'default_value'};
114 # ----------------------------------------------------------------------
115 sub is_auto_increment {
119 =head2 is_auto_increment
121 Get or set the field's C<is_auto_increment> attribute.
123 my $is_pk = $field->is_auto_increment(1);
127 my ( $self, $arg ) = @_;
129 if ( defined $arg ) {
130 $self->{'is_auto_increment'} = $arg ? 1 : 0;
133 unless ( defined $self->{'is_auto_increment'} ) {
134 if ( my $table = $self->table ) {
135 if ( my $schema = $table->schema ) {
137 $schema->database eq 'PostgreSQL' &&
138 $self->data_type eq 'serial'
140 $self->{'is_auto_increment'} = 1;
146 return $self->{'is_auto_increment'} || 0;
149 # ----------------------------------------------------------------------
154 =head2 is_primary_key
156 Get or set the field's C<is_primary_key> attribute.
158 my $is_pk = $field->is_primary_key(1);
162 my ( $self, $arg ) = @_;
164 if ( defined $arg ) {
165 $self->{'is_primary_key'} = $arg ? 1 : 0;
168 unless ( defined $self->{'is_primary_key'} ) {
169 if ( my $table = $self->table ) {
170 if ( my $pk = $table->primary_key ) {
171 my %fields = map { $_, 1 } $pk->fields;
172 $self->{'is_primary_key'} = $fields{ $self->name } || 0;
175 $self->{'is_primary_key'} = 0;
180 return $self->{'is_primary_key'} || 0;
183 # ----------------------------------------------------------------------
190 Get or set the field's name.
192 my $name = $field->name('foo');
198 if ( my $arg = shift ) {
199 if ( my $table = $self->table ) {
200 return $self->error( qq[Can't use field name "$arg": table exists] )
201 if $table->get_field( $arg );
204 $self->{'name'} = $arg;
207 return $self->{'name'} || '';
210 # ----------------------------------------------------------------------
217 Get or set the whether the field can be null. If not defined, then
218 returns "1" (assumes the field can be null). The argument is evaluated
219 by Perl for True or False, so the following are eqivalent:
221 $nullable = $field->nullable(0);
222 $nullable = $field->nullable('');
223 $nullable = $field->nullable('0');
227 my ( $self, $arg ) = @_;
229 if ( defined $arg ) {
230 $self->{'nullable'} = $arg ? 1 : 0;
233 return defined $self->{'nullable'} ? $self->{'nullable'} : 1;
236 # ----------------------------------------------------------------------
243 Get or set the field's size. Accepts a string, array or arrayref of
244 numbers and returns a string.
247 $field->size( [ 255 ] );
248 $size = $field->size( 10, 2 );
249 print $size; # prints "10,2"
251 $size = $field->size( '10, 2' );
252 print $size; # prints "10,2"
257 my $numbers = UNIVERSAL::isa( $_[0], 'ARRAY' )
258 ? shift : [ map { split /,/ } @_ ];
262 for my $num ( @$numbers ) {
263 if ( defined $num && $num =~ m/^\d+(?:\.\d+)?$/ ) {
267 $self->{'size'} = \@new if @new; # only set if all OK
270 return join( ',', @{ $self->{'size'} || [0] } );
273 # ----------------------------------------------------------------------
280 Determine whether the field is valid or not.
282 my $ok = $field->is_valid;
287 return $self->error('No name') unless $self->name;
288 return $self->error('No data type') unless $self->data_type;
289 return $self->error('No table object') unless $self->table;
293 # ----------------------------------------------------------------------
300 Get or set the field's table object.
302 my $table = $field->table;
307 if ( my $arg = shift ) {
308 return $self->error('Not a table object') unless
309 UNIVERSAL::isa( $arg, 'SQL::Translator::Schema::Table' );
310 $self->{'table'} = $arg;
313 return $self->{'table'};
318 # ----------------------------------------------------------------------
324 Ken Y. Clark E<lt>kclark@cpan.orgE<gt>