1 package SQL::Translator::Schema::Field;
3 # ----------------------------------------------------------------------
4 # $Id: Field.pm,v 1.4 2003-05-09 17:08:14 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
76 next unless defined $config->{ $arg };
77 $self->$arg( $config->{ $arg } ) or return;
82 # ----------------------------------------------------------------------
89 Get or set the field's data type.
91 my $data_type = $field->data_type('integer');
96 $self->{'data_type'} = shift if @_;
97 return $self->{'data_type'} || '';
100 # ----------------------------------------------------------------------
107 Get or set the field's default value. Will return undef if not defined
108 and could return the empty string (it's a valid default value), so don't
109 assume an error like other methods.
111 my $default = $field->default_value('foo');
115 my ( $self, $arg ) = @_;
116 $self->{'default_value'} = $arg if defined $arg;
117 return $self->{'default_value'};
120 # ----------------------------------------------------------------------
121 sub is_auto_increment {
125 =head2 is_auto_increment
127 Get or set the field's C<is_auto_increment> attribute.
129 my $is_pk = $field->is_auto_increment(1);
133 my ( $self, $arg ) = @_;
135 if ( defined $arg ) {
136 $self->{'is_auto_increment'} = $arg ? 1 : 0;
139 unless ( defined $self->{'is_auto_increment'} ) {
140 if ( my $table = $self->table ) {
141 if ( my $schema = $table->schema ) {
143 $schema->database eq 'PostgreSQL' &&
144 $self->data_type eq 'serial'
146 $self->{'is_auto_increment'} = 1;
152 return $self->{'is_auto_increment'} || 0;
155 # ----------------------------------------------------------------------
162 Get or set the whether the field can be null. If not defined, then
163 returns "1" (assumes the field can be null). The argument is evaluated
164 by Perl for True or False, so the following are eqivalent:
166 $is_nullable = $field->is_nullable(0);
167 $is_nullable = $field->is_nullable('');
168 $is_nullable = $field->is_nullable('0');
170 While this is technically a field constraint, it's probably easier to
171 represent this as an attribute of the field. In order keep things
172 consistent, any other constraint on the field (unique, primary, and
173 foreign keys; checks) are represented as table constraints.
177 my ( $self, $arg ) = @_;
179 if ( defined $arg ) {
180 $self->{'is_nullable'} = $arg ? 1 : 0;
183 return defined $self->{'is_nullable'} ? $self->{'is_nullable'} : 1;
186 # ----------------------------------------------------------------------
191 =head2 is_primary_key
193 Get or set the field's C<is_primary_key> attribute. Does not create
194 a table constraint (should it?).
196 my $is_pk = $field->is_primary_key(1);
200 my ( $self, $arg ) = @_;
202 if ( defined $arg ) {
203 $self->{'is_primary_key'} = $arg ? 1 : 0;
206 unless ( defined $self->{'is_primary_key'} ) {
207 if ( my $table = $self->table ) {
208 if ( my $pk = $table->primary_key ) {
209 my %fields = map { $_, 1 } $pk->fields;
210 $self->{'is_primary_key'} = $fields{ $self->name } || 0;
213 $self->{'is_primary_key'} = 0;
218 return $self->{'is_primary_key'} || 0;
221 # ----------------------------------------------------------------------
228 Determine whether the field is valid or not.
230 my $ok = $field->is_valid;
235 return $self->error('No name') unless $self->name;
236 return $self->error('No data type') unless $self->data_type;
237 return $self->error('No table object') unless $self->table;
241 # ----------------------------------------------------------------------
248 Get or set the field's name.
250 my $name = $field->name('foo');
256 if ( my $arg = shift ) {
257 if ( my $table = $self->table ) {
258 return $self->error( qq[Can't use field name "$arg": table exists] )
259 if $table->get_field( $arg );
262 $self->{'name'} = $arg;
265 return $self->{'name'} || '';
268 # ----------------------------------------------------------------------
275 Get or set the field's order.
277 my $order = $field->order(3);
281 my ( $self, $arg ) = @_;
283 if ( defined $arg && $arg =~ /^\d+$/ ) {
284 $self->{'order'} = $arg;
287 return $self->{'order'} || 0;
290 # ----------------------------------------------------------------------
297 Get or set the field's size. Accepts a string, array or arrayref of
298 numbers and returns a string.
301 $field->size( [ 255 ] );
302 $size = $field->size( 10, 2 );
303 print $size; # prints "10,2"
305 $size = $field->size( '10, 2' );
306 print $size; # prints "10,2"
311 my $numbers = parse_list_arg( @_ );
315 for my $num ( @$numbers ) {
316 if ( defined $num && $num =~ m/^\d+(?:\.\d+)?$/ ) {
320 $self->{'size'} = \@new if @new; # only set if all OK
324 ? @{ $self->{'size'} }
325 : join( ',', @{ $self->{'size'} || [0] } )
329 # ----------------------------------------------------------------------
336 Get or set the field's table object.
338 my $table = $field->table;
343 if ( my $arg = shift ) {
344 return $self->error('Not a table object') unless
345 UNIVERSAL::isa( $arg, 'SQL::Translator::Schema::Table' );
346 $self->{'table'} = $arg;
349 return $self->{'table'};
352 # ----------------------------------------------------------------------
355 undef $self->{'table'}; # destroy cyclical reference
360 # ----------------------------------------------------------------------
366 Ken Y. Clark E<lt>kclark@cpan.orgE<gt>