1 package SQL::Translator::Schema::Field;
3 # ----------------------------------------------------------------------
4 # $Id: Field.pm,v 1.5 2003-06-03 22:37:42 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 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 # ----------------------------------------------------------------------
122 sub is_auto_increment {
126 =head2 is_auto_increment
128 Get or set the field's C<is_auto_increment> attribute.
130 my $is_pk = $field->is_auto_increment(1);
134 my ( $self, $arg ) = @_;
136 if ( defined $arg ) {
137 $self->{'is_auto_increment'} = $arg ? 1 : 0;
140 unless ( defined $self->{'is_auto_increment'} ) {
141 if ( my $table = $self->table ) {
142 if ( my $schema = $table->schema ) {
144 $schema->database eq 'PostgreSQL' &&
145 $self->data_type eq 'serial'
147 $self->{'is_auto_increment'} = 1;
153 return $self->{'is_auto_increment'} || 0;
156 # ----------------------------------------------------------------------
163 Get or set the whether the field can be null. If not defined, then
164 returns "1" (assumes the field can be null). The argument is evaluated
165 by Perl for True or False, so the following are eqivalent:
167 $is_nullable = $field->is_nullable(0);
168 $is_nullable = $field->is_nullable('');
169 $is_nullable = $field->is_nullable('0');
171 While this is technically a field constraint, it's probably easier to
172 represent this as an attribute of the field. In order keep things
173 consistent, any other constraint on the field (unique, primary, and
174 foreign keys; checks) are represented as table constraints.
178 my ( $self, $arg ) = @_;
180 if ( defined $arg ) {
181 $self->{'is_nullable'} = $arg ? 1 : 0;
184 return defined $self->{'is_nullable'} ? $self->{'is_nullable'} : 1;
187 # ----------------------------------------------------------------------
192 =head2 is_primary_key
194 Get or set the field's C<is_primary_key> attribute. Does not create
195 a table constraint (should it?).
197 my $is_pk = $field->is_primary_key(1);
201 my ( $self, $arg ) = @_;
203 if ( defined $arg ) {
204 $self->{'is_primary_key'} = $arg ? 1 : 0;
207 unless ( defined $self->{'is_primary_key'} ) {
208 if ( my $table = $self->table ) {
209 if ( my $pk = $table->primary_key ) {
210 my %fields = map { $_, 1 } $pk->fields;
211 $self->{'is_primary_key'} = $fields{ $self->name } || 0;
214 $self->{'is_primary_key'} = 0;
219 return $self->{'is_primary_key'} || 0;
222 # ----------------------------------------------------------------------
229 Determine whether the field is valid or not.
231 my $ok = $field->is_valid;
236 return $self->error('No name') unless $self->name;
237 return $self->error('No data type') unless $self->data_type;
238 return $self->error('No table object') unless $self->table;
242 # ----------------------------------------------------------------------
249 Get or set the field's name.
251 my $name = $field->name('foo');
257 if ( my $arg = shift ) {
258 if ( my $table = $self->table ) {
259 return $self->error( qq[Can't use field name "$arg": table exists] )
260 if $table->get_field( $arg );
263 $self->{'name'} = $arg;
266 return $self->{'name'} || '';
269 # ----------------------------------------------------------------------
276 Get or set the field's order.
278 my $order = $field->order(3);
282 my ( $self, $arg ) = @_;
284 if ( defined $arg && $arg =~ /^\d+$/ ) {
285 $self->{'order'} = $arg;
288 return $self->{'order'} || 0;
291 # ----------------------------------------------------------------------
298 Get or set the field's size. Accepts a string, array or arrayref of
299 numbers and returns a string.
302 $field->size( [ 255 ] );
303 $size = $field->size( 10, 2 );
304 print $size; # prints "10,2"
306 $size = $field->size( '10, 2' );
307 print $size; # prints "10,2"
312 my $numbers = parse_list_arg( @_ );
316 for my $num ( @$numbers ) {
317 if ( defined $num && $num =~ m/^\d+(?:\.\d+)?$/ ) {
321 $self->{'size'} = \@new if @new; # only set if all OK
325 ? @{ $self->{'size'} }
326 : join( ',', @{ $self->{'size'} || [0] } )
330 # ----------------------------------------------------------------------
337 Get or set the field's table object.
339 my $table = $field->table;
344 if ( my $arg = shift ) {
345 return $self->error('Not a table object') unless
346 UNIVERSAL::isa( $arg, 'SQL::Translator::Schema::Table' );
347 $self->{'table'} = $arg;
350 return $self->{'table'};
353 # ----------------------------------------------------------------------
356 undef $self->{'table'}; # destroy cyclical reference
361 # ----------------------------------------------------------------------
367 Ken Y. Clark E<lt>kclark@cpan.orgE<gt>