Added parsing of default value on init, added "extra" method for misc field
[dbsrgits/SQL-Translator.git] / lib / SQL / Translator / Schema / Table.pm
CommitLineData
3c5de62a 1package SQL::Translator::Schema::Table;
2
3# ----------------------------------------------------------------------
30f4ec44 4# $Id: Table.pm,v 1.5 2003-05-09 17:11:00 kycl4rk Exp $
3c5de62a 5# ----------------------------------------------------------------------
6# Copyright (C) 2003 Ken Y. Clark <kclark@cpan.org>
7#
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.
11#
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.
16#
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
20# 02111-1307 USA
21# -------------------------------------------------------------------
22
23=pod
24
25=head1 NAME
26
27SQL::Translator::Schema::Table - SQL::Translator table object
28
29=head1 SYNOPSIS
30
31 use SQL::Translator::Schema::Table;
0f3cc5c0 32 my $table = SQL::Translator::Schema::Table->new( name => 'foo' );
3c5de62a 33
34=head1 DESCSIPTION
35
36C<SQL::Translator::Schema::Table> is the table object.
37
38=head1 METHODS
39
40=cut
41
42use strict;
43use Class::Base;
30f4ec44 44use SQL::Translator::Utils 'parse_list_arg';
0f3cc5c0 45use SQL::Translator::Schema::Constants;
3c5de62a 46use SQL::Translator::Schema::Constraint;
47use SQL::Translator::Schema::Field;
48use SQL::Translator::Schema::Index;
49
50use base 'Class::Base';
0f3cc5c0 51use vars qw( $VERSION $FIELD_ORDER );
3c5de62a 52
53$VERSION = 1.00;
54
55# ----------------------------------------------------------------------
56sub init {
57
58=pod
59
60=head2 new
61
62Object constructor.
63
43b9dc7a 64 my $table = SQL::Translator::Schema::Table->new(
65 schema => $schema,
66 name => 'foo',
67 );
3c5de62a 68
69=cut
70
71 my ( $self, $config ) = @_;
43b9dc7a 72
73 for my $arg ( qw[ schema name ] ) {
74 next unless defined $config->{ $arg };
75 $self->$arg( $config->{ $arg } ) or return;
76 }
77
3c5de62a 78 return $self;
79}
80
81# ----------------------------------------------------------------------
82sub name {
83
84=pod
85
86=head2 name
87
88Get or set the table's name.
89
43b9dc7a 90If provided an argument, checks the schema object for a table of
91that name and disallows the change if one exists.
92
3c5de62a 93 my $table_name = $table->name('foo');
94
95=cut
96
97 my $self = shift;
43b9dc7a 98
99 if ( my $arg = shift ) {
100 if ( my $schema = $self->schema ) {
101 return $self->error( qq[Can't use table name "$arg": table exists] )
102 if $schema->get_table( $arg );
103 }
104 $self->{'name'} = $arg;
105 }
106
3c5de62a 107 return $self->{'name'} || '';
108}
109
110# ----------------------------------------------------------------------
111sub add_constraint {
112
113=pod
114
115=head2 add_constraint
116
0f3cc5c0 117Add a constraint to the table. Returns the newly created
118C<SQL::Translator::Schema::Constraint> object.
3c5de62a 119
43b9dc7a 120 my $constraint1 = $table->add_constraint(
121 name => 'pk',
122 type => PRIMARY_KEY,
123 fields => [ 'foo_id' ],
3c5de62a 124 );
125
43b9dc7a 126 my $constraint2 = SQL::Translator::Schema::Constraint->new( name => 'uniq' );
127 $constraint2 = $table->add_constraint( $constraint );
128
3c5de62a 129=cut
130
43b9dc7a 131 my $self = shift;
132 my $constraint_class = 'SQL::Translator::Schema::Constraint';
133 my $constraint;
134
135 if ( UNIVERSAL::isa( $_[0], $constraint_class ) ) {
136 $constraint = shift;
137 $constraint->table( $self );
138 }
139 else {
140 my %args = @_;
141 $args{'table'} = $self;
142 $constraint = $constraint_class->new( \%args ) or
143 return $self->error( $constraint_class->error );
144 }
145
3c5de62a 146 push @{ $self->{'constraints'} }, $constraint;
147 return $constraint;
148}
149
150# ----------------------------------------------------------------------
151sub add_index {
152
153=pod
154
155=head2 add_index
156
0f3cc5c0 157Add an index to the table. Returns the newly created
158C<SQL::Translator::Schema::Index> object.
3c5de62a 159
43b9dc7a 160 my $index1 = $table->add_index(
3c5de62a 161 name => 'name',
162 fields => [ 'name' ],
163 type => 'normal',
164 );
165
43b9dc7a 166 my $index2 = SQL::Translator::Schema::Index->new( name => 'id' );
167 $index2 = $table->add_index( $index );
168
3c5de62a 169=cut
170
43b9dc7a 171 my $self = shift;
172 my $index_class = 'SQL::Translator::Schema::Index';
173 my $index;
174
175 if ( UNIVERSAL::isa( $_[0], $index_class ) ) {
176 $index = shift;
177 $index->table( $self );
178 }
179 else {
180 my %args = @_;
181 $args{'table'} = $self;
182 $index = $index_class->new( \%args ) or return
183 $self->error( $index_class->error );
184 }
185
3c5de62a 186 push @{ $self->{'indices'} }, $index;
187 return $index;
188}
189
190# ----------------------------------------------------------------------
191sub add_field {
192
193=pod
194
195=head2 add_field
196
43b9dc7a 197Add an field to the table. Returns the newly created
198C<SQL::Translator::Schema::Field> object. The "name" parameter is
199required. If you try to create a field with the same name as an
200existing field, you will get an error and the field will not be created.
3c5de62a 201
43b9dc7a 202 my $field1 = $table->add_field(
0f3cc5c0 203 name => 'foo_id',
204 data_type => 'integer',
205 size => 11,
3c5de62a 206 );
207
43b9dc7a 208 my $field2 = SQL::Translator::Schema::Field->new(
209 name => 'name',
210 table => $table,
211 );
212 $field2 = $table->add_field( $field2 ) or die $table->error;
213
3c5de62a 214=cut
215
216 my $self = shift;
43b9dc7a 217 my $field_class = 'SQL::Translator::Schema::Field';
218 my $field;
219
220 if ( UNIVERSAL::isa( $_[0], $field_class ) ) {
221 $field = shift;
222 $field->table( $self );
223 }
224 else {
225 my %args = @_;
226 $args{'table'} = $self;
227 $field = $field_class->new( \%args ) or return
228 $self->error( $field_class->error );
229 }
230
30f4ec44 231 $field->order( ++$FIELD_ORDER );
43b9dc7a 232 my $field_name = $field->name or return $self->error('No name');
233
234 if ( exists $self->{'fields'}{ $field_name } ) {
235 return $self->error(qq[Can't create field: "$field_name" exists]);
236 }
237 else {
238 $self->{'fields'}{ $field_name } = $field;
43b9dc7a 239 }
240
3c5de62a 241 return $field;
242}
243
244# ----------------------------------------------------------------------
0f3cc5c0 245sub get_constraints {
246
247=pod
248
249=head2 get_constraints
250
251Returns all the constraint objects as an array or array reference.
252
253 my @constraints = $table->get_constraints;
254
255=cut
256
257 my $self = shift;
258
259 if ( ref $self->{'constraints'} ) {
260 return wantarray
261 ? @{ $self->{'constraints'} } : $self->{'constraints'};
262 }
263 else {
264 $self->error('No constraints');
265 return wantarray ? () : undef;
266 }
267}
268
269# ----------------------------------------------------------------------
270sub get_indices {
3c5de62a 271
272=pod
273
0f3cc5c0 274=head2 get_indices
3c5de62a 275
0f3cc5c0 276Returns all the index objects as an array or array reference.
3c5de62a 277
0f3cc5c0 278 my @indices = $table->get_indices;
3c5de62a 279
280=cut
281
282 my $self = shift;
0f3cc5c0 283
284 if ( ref $self->{'indices'} ) {
285 return wantarray
286 ? @{ $self->{'indices'} }
287 : $self->{'indices'};
288 }
289 else {
290 $self->error('No indices');
291 return wantarray ? () : undef;
292 }
293}
294
295# ----------------------------------------------------------------------
43b9dc7a 296sub get_field {
297
298=pod
299
300=head2 get_field
301
302Returns a field by the name provided.
303
304 my $field = $table->get_field('foo');
305
306=cut
307
308 my $self = shift;
309 my $field_name = shift or return $self->error('No field name');
310 return $self->error( qq[Field "$field_name" does not exist] ) unless
311 exists $self->{'fields'}{ $field_name };
312 return $self->{'fields'}{ $field_name };
313}
314
315# ----------------------------------------------------------------------
0f3cc5c0 316sub get_fields {
317
318=pod
319
320=head2 get_fields
321
322Returns all the field objects as an array or array reference.
323
324 my @fields = $table->get_fields;
325
326=cut
327
328 my $self = shift;
329 my @fields =
30f4ec44 330 map { $_->[1] }
331 sort { $a->[0] <=> $b->[0] }
332 map { [ $_->order, $_ ] }
0f3cc5c0 333 values %{ $self->{'fields'} || {} };
334
335 if ( @fields ) {
336 return wantarray ? @fields : \@fields;
337 }
338 else {
339 $self->error('No fields');
340 return wantarray ? () : undef;
341 }
3c5de62a 342}
343
344# ----------------------------------------------------------------------
345sub is_valid {
346
347=pod
348
349=head2 is_valid
350
351Determine whether the view is valid or not.
352
353 my $ok = $view->is_valid;
354
355=cut
356
357 my $self = shift;
43b9dc7a 358 return $self->error('No name') unless $self->name;
0f3cc5c0 359 return $self->error('No fields') unless $self->get_fields;
360
361 for my $object (
362 $self->get_fields, $self->get_indices, $self->get_constraints
363 ) {
364 return $object->error unless $object->is_valid;
365 }
366
367 return 1;
3c5de62a 368}
369
43b9dc7a 370# ----------------------------------------------------------------------
371sub schema {
372
373=pod
374
375=head2 schema
376
377Get or set the table's schema object.
378
379 my $schema = $table->schema;
380
381=cut
382
383 my $self = shift;
384 if ( my $arg = shift ) {
385 return $self->error('Not a schema object') unless
386 UNIVERSAL::isa( $arg, 'SQL::Translator::Schema' );
387 $self->{'schema'} = $arg;
388 }
389
390 return $self->{'schema'};
391}
392
393# ----------------------------------------------------------------------
394sub primary_key {
395
396=pod
397
398=head2 options
399
5e84ac85 400Gets or sets the table's primary key(s). Takes one or more field
401names (as a string, list or array[ref]) as an argument. If the field
402names are present, it will create a new PK if none exists, or it will
403add to the fields of an existing PK (and will unique the field names).
404Returns the C<SQL::Translator::Schema::Constraint> object representing
405the primary key.
406
407These are eqivalent:
43b9dc7a 408
409 $table->primary_key('id');
5e84ac85 410 $table->primary_key(['name']);
411 $table->primary_key('id','name']);
43b9dc7a 412 $table->primary_key(['id','name']);
413 $table->primary_key('id,name');
414 $table->primary_key(qw[ id name ]);
415
416 my $pk = $table->primary_key;
417
418=cut
419
30f4ec44 420 my $self = shift;
421 my $fields = parse_list_arg( @_ );
43b9dc7a 422
5e84ac85 423 my $constraint;
43b9dc7a 424 if ( @$fields ) {
425 for my $f ( @$fields ) {
426 return $self->error(qq[Invalid field "$f"]) unless
427 $self->get_field($f);
428 }
429
430 my $has_pk;
431 for my $c ( $self->get_constraints ) {
432 if ( $c->type eq PRIMARY_KEY ) {
433 $has_pk = 1;
434 $c->fields( @{ $c->fields }, @$fields );
5e84ac85 435 $constraint = $c;
43b9dc7a 436 }
437 }
438
439 unless ( $has_pk ) {
5e84ac85 440 $constraint = $self->add_constraint(
43b9dc7a 441 type => PRIMARY_KEY,
442 fields => $fields,
443 );
444 }
445 }
446
5e84ac85 447 if ( $constraint ) {
448 return $constraint;
449 }
450 else {
451 for my $c ( $self->get_constraints ) {
452 return $c if $c->type eq PRIMARY_KEY;
453 }
43b9dc7a 454 }
455
456 return $self->error('No primary key');
457}
458
459# ----------------------------------------------------------------------
460sub options {
461
462=pod
463
464=head2 options
465
466Get or set the table's options (e.g., table types for MySQL). Returns
467an array or array reference.
468
469 my @options = $table->options;
470
471=cut
472
473 my $self = shift;
30f4ec44 474 my $options = parse_list_arg( @_ );
43b9dc7a 475
476 push @{ $self->{'options'} }, @$options;
477
478 if ( ref $self->{'options'} ) {
479 return wantarray ? @{ $self->{'options'} || [] } : $self->{'options'};
480 }
481 else {
482 return wantarray ? () : [];
483 }
484}
485
30f4ec44 486# ----------------------------------------------------------------------
487sub order {
488
489=pod
490
491=head2 order
492
493Get or set the table's order.
494
495 my $order = $table->order(3);
496
497=cut
498
499 my ( $self, $arg ) = @_;
500
501 if ( defined $arg && $arg =~ /^\d+$/ ) {
502 $self->{'order'} = $arg;
503 }
504
505 return $self->{'order'} || 0;
506}
507
508# ----------------------------------------------------------------------
509sub DESTROY {
510 my $self = shift;
511 undef $self->{'schema'}; # destroy cyclical reference
512 undef $_ for @{ $self->{'constraints'} };
513 undef $_ for @{ $self->{'indices'} };
514 undef $_ for values %{ $self->{'fields'} };
515}
516
3c5de62a 5171;
518
519# ----------------------------------------------------------------------
520
521=pod
522
523=head1 AUTHOR
524
525Ken Y. Clark E<lt>kclark@cpan.orgE<gt>
526
527=cut