Fixed problem with "deep recursion."
[dbsrgits/SQL-Translator.git] / lib / SQL / Translator / Schema / Table.pm
CommitLineData
3c5de62a 1package SQL::Translator::Schema::Table;
2
3# ----------------------------------------------------------------------
2ccf2299 4# $Id: Table.pm,v 1.14 2003-08-21 20:27:04 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
2ccf2299 53$VERSION = sprintf "%d.%02d", q$Revision: 1.14 $ =~ /(\d+)\.(\d+)/;
3c5de62a 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
88b8377e 73 for my $arg ( qw[ schema name comments ] ) {
43b9dc7a 74 next unless defined $config->{ $arg };
88b8377e 75 defined $self->$arg( $config->{ $arg } ) or return;
43b9dc7a 76 }
77
3c5de62a 78 return $self;
79}
80
81# ----------------------------------------------------------------------
3c5de62a 82sub add_constraint {
83
84=pod
85
86=head2 add_constraint
87
0f3cc5c0 88Add a constraint to the table. Returns the newly created
89C<SQL::Translator::Schema::Constraint> object.
3c5de62a 90
dfdb0568 91 my $c1 = $table->add_constraint(
43b9dc7a 92 name => 'pk',
93 type => PRIMARY_KEY,
94 fields => [ 'foo_id' ],
3c5de62a 95 );
96
dfdb0568 97 my $c2 = SQL::Translator::Schema::Constraint->new( name => 'uniq' );
98 $c2 = $table->add_constraint( $constraint );
43b9dc7a 99
3c5de62a 100=cut
101
43b9dc7a 102 my $self = shift;
103 my $constraint_class = 'SQL::Translator::Schema::Constraint';
104 my $constraint;
105
106 if ( UNIVERSAL::isa( $_[0], $constraint_class ) ) {
107 $constraint = shift;
108 $constraint->table( $self );
109 }
110 else {
111 my %args = @_;
112 $args{'table'} = $self;
113 $constraint = $constraint_class->new( \%args ) or
114 return $self->error( $constraint_class->error );
115 }
116
dfdb0568 117 #
118 # If we're trying to add a PK when one is already defined,
119 # then just add the fields to the existing definition.
120 #
3dd9026c 121 my $ok = 1;
dfdb0568 122 my $pk = $self->primary_key;
123 if ( $pk && $constraint->type eq PRIMARY_KEY ) {
124 $self->primary_key( $constraint->fields );
125 $constraint = $pk;
3dd9026c 126 $ok = 0;
dfdb0568 127 }
2ccf2299 128 elsif ( $constraint->type eq PRIMARY_KEY ) {
129 for my $fname ( $constraint->fields ) {
130 if ( my $f = $self->get_field( $fname ) ) {
131 $f->is_primary_key( 1 );
132 }
133 }
134 }
3dd9026c 135 #
136 # See if another constraint of the same type
137 # covers the same fields.
138 #
139 elsif ( $constraint->type ne CHECK_C ) {
dfdb0568 140 my @field_names = $constraint->fields;
dfdb0568 141 for my $c (
142 grep { $_->type eq $constraint->type }
143 $self->get_constraints
144 ) {
145 my %fields = map { $_, 1 } $c->fields;
146 for my $field_name ( @field_names ) {
147 if ( $fields{ $field_name } ) {
148 $constraint = $c;
149 $ok = 0;
150 last;
151 }
152 }
153 last unless $ok;
154 }
155 }
156
157 if ( $ok ) {
158 push @{ $self->{'constraints'} }, $constraint;
159 }
160
3c5de62a 161 return $constraint;
162}
163
164# ----------------------------------------------------------------------
165sub add_index {
166
167=pod
168
169=head2 add_index
170
0f3cc5c0 171Add an index to the table. Returns the newly created
172C<SQL::Translator::Schema::Index> object.
3c5de62a 173
dfdb0568 174 my $i1 = $table->add_index(
3c5de62a 175 name => 'name',
176 fields => [ 'name' ],
177 type => 'normal',
178 );
179
dfdb0568 180 my $i2 = SQL::Translator::Schema::Index->new( name => 'id' );
181 $i2 = $table->add_index( $index );
43b9dc7a 182
3c5de62a 183=cut
184
43b9dc7a 185 my $self = shift;
186 my $index_class = 'SQL::Translator::Schema::Index';
187 my $index;
188
189 if ( UNIVERSAL::isa( $_[0], $index_class ) ) {
190 $index = shift;
191 $index->table( $self );
192 }
193 else {
194 my %args = @_;
195 $args{'table'} = $self;
196 $index = $index_class->new( \%args ) or return
197 $self->error( $index_class->error );
198 }
199
3c5de62a 200 push @{ $self->{'indices'} }, $index;
201 return $index;
202}
203
204# ----------------------------------------------------------------------
205sub add_field {
206
207=pod
208
209=head2 add_field
210
43b9dc7a 211Add an field to the table. Returns the newly created
212C<SQL::Translator::Schema::Field> object. The "name" parameter is
213required. If you try to create a field with the same name as an
214existing field, you will get an error and the field will not be created.
3c5de62a 215
dfdb0568 216 my $f1 = $table->add_field(
0f3cc5c0 217 name => 'foo_id',
218 data_type => 'integer',
219 size => 11,
3c5de62a 220 );
221
dfdb0568 222 my $f2 = SQL::Translator::Schema::Field->new(
43b9dc7a 223 name => 'name',
224 table => $table,
225 );
dfdb0568 226 $f2 = $table->add_field( $field2 ) or die $table->error;
43b9dc7a 227
3c5de62a 228=cut
229
dfdb0568 230 my $self = shift;
43b9dc7a 231 my $field_class = 'SQL::Translator::Schema::Field';
232 my $field;
233
234 if ( UNIVERSAL::isa( $_[0], $field_class ) ) {
235 $field = shift;
236 $field->table( $self );
237 }
238 else {
239 my %args = @_;
240 $args{'table'} = $self;
241 $field = $field_class->new( \%args ) or return
242 $self->error( $field_class->error );
243 }
244
30f4ec44 245 $field->order( ++$FIELD_ORDER );
43b9dc7a 246 my $field_name = $field->name or return $self->error('No name');
247
248 if ( exists $self->{'fields'}{ $field_name } ) {
249 return $self->error(qq[Can't create field: "$field_name" exists]);
250 }
251 else {
252 $self->{'fields'}{ $field_name } = $field;
43b9dc7a 253 }
254
3c5de62a 255 return $field;
256}
257
258# ----------------------------------------------------------------------
88b8377e 259sub comments {
260
261=pod
262
263=head2 comments
264
265Get or set the comments on a table. May be called several times to
266set and it will accumulate the comments. Called in an array context,
267returns each comment individually; called in a scalar context, returns
268all the comments joined on newlines.
269
270 $table->comments('foo');
271 $table->comments('bar');
272 print join( ', ', $table->comments ); # prints "foo, bar"
273
274=cut
275
eb3b8ae4 276 my $self = shift;
277 my @comments = ref $_[0] ? @{ $_[0] } : @_;
b891fb49 278
eb3b8ae4 279 for my $arg ( @comments ) {
b891fb49 280 $arg = $arg->[0] if ref $arg;
eb3b8ae4 281 push @{ $self->{'comments'} }, $arg if defined $arg && $arg;
b891fb49 282 }
88b8377e 283
eb3b8ae4 284 if ( @{ $self->{'comments'} || [] } ) {
285 return wantarray
286 ? @{ $self->{'comments'} }
287 : join( "\n", @{ $self->{'comments'} } )
288 ;
289 }
290 else {
291 return wantarray ? () : undef;
292 }
88b8377e 293}
294
295# ----------------------------------------------------------------------
0f3cc5c0 296sub get_constraints {
297
298=pod
299
300=head2 get_constraints
301
302Returns all the constraint objects as an array or array reference.
303
304 my @constraints = $table->get_constraints;
305
306=cut
307
308 my $self = shift;
309
310 if ( ref $self->{'constraints'} ) {
311 return wantarray
312 ? @{ $self->{'constraints'} } : $self->{'constraints'};
313 }
314 else {
315 $self->error('No constraints');
316 return wantarray ? () : undef;
317 }
318}
319
320# ----------------------------------------------------------------------
321sub get_indices {
3c5de62a 322
323=pod
324
0f3cc5c0 325=head2 get_indices
3c5de62a 326
0f3cc5c0 327Returns all the index objects as an array or array reference.
3c5de62a 328
0f3cc5c0 329 my @indices = $table->get_indices;
3c5de62a 330
331=cut
332
333 my $self = shift;
0f3cc5c0 334
335 if ( ref $self->{'indices'} ) {
336 return wantarray
337 ? @{ $self->{'indices'} }
338 : $self->{'indices'};
339 }
340 else {
341 $self->error('No indices');
342 return wantarray ? () : undef;
343 }
344}
345
346# ----------------------------------------------------------------------
43b9dc7a 347sub get_field {
348
349=pod
350
351=head2 get_field
352
353Returns a field by the name provided.
354
355 my $field = $table->get_field('foo');
356
357=cut
358
359 my $self = shift;
360 my $field_name = shift or return $self->error('No field name');
361 return $self->error( qq[Field "$field_name" does not exist] ) unless
362 exists $self->{'fields'}{ $field_name };
363 return $self->{'fields'}{ $field_name };
364}
365
366# ----------------------------------------------------------------------
0f3cc5c0 367sub get_fields {
368
369=pod
370
371=head2 get_fields
372
373Returns all the field objects as an array or array reference.
374
375 my @fields = $table->get_fields;
376
377=cut
378
379 my $self = shift;
380 my @fields =
30f4ec44 381 map { $_->[1] }
382 sort { $a->[0] <=> $b->[0] }
383 map { [ $_->order, $_ ] }
0f3cc5c0 384 values %{ $self->{'fields'} || {} };
385
386 if ( @fields ) {
387 return wantarray ? @fields : \@fields;
388 }
389 else {
390 $self->error('No fields');
391 return wantarray ? () : undef;
392 }
3c5de62a 393}
394
395# ----------------------------------------------------------------------
396sub is_valid {
397
398=pod
399
400=head2 is_valid
401
402Determine whether the view is valid or not.
403
404 my $ok = $view->is_valid;
405
406=cut
407
408 my $self = shift;
43b9dc7a 409 return $self->error('No name') unless $self->name;
0f3cc5c0 410 return $self->error('No fields') unless $self->get_fields;
411
412 for my $object (
413 $self->get_fields, $self->get_indices, $self->get_constraints
414 ) {
415 return $object->error unless $object->is_valid;
416 }
417
418 return 1;
3c5de62a 419}
420
43b9dc7a 421# ----------------------------------------------------------------------
dfdb0568 422sub name {
423
424=pod
425
426=head2 name
427
428Get or set the table's name.
429
430If provided an argument, checks the schema object for a table of
431that name and disallows the change if one exists.
432
433 my $table_name = $table->name('foo');
434
435=cut
436
437 my $self = shift;
438
439 if ( my $arg = shift ) {
440 if ( my $schema = $self->schema ) {
441 return $self->error( qq[Can't use table name "$arg": table exists] )
442 if $schema->get_table( $arg );
443 }
444 $self->{'name'} = $arg;
445 }
446
447 return $self->{'name'} || '';
448}
449
450# ----------------------------------------------------------------------
43b9dc7a 451sub schema {
452
453=pod
454
455=head2 schema
456
457Get or set the table's schema object.
458
459 my $schema = $table->schema;
460
461=cut
462
463 my $self = shift;
464 if ( my $arg = shift ) {
465 return $self->error('Not a schema object') unless
466 UNIVERSAL::isa( $arg, 'SQL::Translator::Schema' );
467 $self->{'schema'} = $arg;
468 }
469
470 return $self->{'schema'};
471}
472
473# ----------------------------------------------------------------------
474sub primary_key {
475
476=pod
477
478=head2 options
479
5e84ac85 480Gets or sets the table's primary key(s). Takes one or more field
481names (as a string, list or array[ref]) as an argument. If the field
482names are present, it will create a new PK if none exists, or it will
483add to the fields of an existing PK (and will unique the field names).
484Returns the C<SQL::Translator::Schema::Constraint> object representing
485the primary key.
486
487These are eqivalent:
43b9dc7a 488
489 $table->primary_key('id');
5e84ac85 490 $table->primary_key(['name']);
491 $table->primary_key('id','name']);
43b9dc7a 492 $table->primary_key(['id','name']);
493 $table->primary_key('id,name');
494 $table->primary_key(qw[ id name ]);
495
496 my $pk = $table->primary_key;
497
498=cut
499
30f4ec44 500 my $self = shift;
501 my $fields = parse_list_arg( @_ );
43b9dc7a 502
5e84ac85 503 my $constraint;
43b9dc7a 504 if ( @$fields ) {
505 for my $f ( @$fields ) {
506 return $self->error(qq[Invalid field "$f"]) unless
507 $self->get_field($f);
508 }
509
510 my $has_pk;
511 for my $c ( $self->get_constraints ) {
512 if ( $c->type eq PRIMARY_KEY ) {
513 $has_pk = 1;
514 $c->fields( @{ $c->fields }, @$fields );
5e84ac85 515 $constraint = $c;
43b9dc7a 516 }
517 }
518
519 unless ( $has_pk ) {
5e84ac85 520 $constraint = $self->add_constraint(
43b9dc7a 521 type => PRIMARY_KEY,
522 fields => $fields,
88b8377e 523 ) or return;
43b9dc7a 524 }
525 }
526
5e84ac85 527 if ( $constraint ) {
528 return $constraint;
529 }
530 else {
531 for my $c ( $self->get_constraints ) {
532 return $c if $c->type eq PRIMARY_KEY;
533 }
43b9dc7a 534 }
535
dfdb0568 536 return;
43b9dc7a 537}
538
539# ----------------------------------------------------------------------
540sub options {
541
542=pod
543
544=head2 options
545
546Get or set the table's options (e.g., table types for MySQL). Returns
547an array or array reference.
548
549 my @options = $table->options;
550
551=cut
552
553 my $self = shift;
30f4ec44 554 my $options = parse_list_arg( @_ );
43b9dc7a 555
556 push @{ $self->{'options'} }, @$options;
557
558 if ( ref $self->{'options'} ) {
559 return wantarray ? @{ $self->{'options'} || [] } : $self->{'options'};
560 }
561 else {
562 return wantarray ? () : [];
563 }
564}
565
30f4ec44 566# ----------------------------------------------------------------------
567sub order {
568
569=pod
570
571=head2 order
572
573Get or set the table's order.
574
575 my $order = $table->order(3);
576
577=cut
578
579 my ( $self, $arg ) = @_;
580
581 if ( defined $arg && $arg =~ /^\d+$/ ) {
582 $self->{'order'} = $arg;
583 }
584
585 return $self->{'order'} || 0;
586}
587
588# ----------------------------------------------------------------------
589sub DESTROY {
590 my $self = shift;
591 undef $self->{'schema'}; # destroy cyclical reference
592 undef $_ for @{ $self->{'constraints'} };
593 undef $_ for @{ $self->{'indices'} };
594 undef $_ for values %{ $self->{'fields'} };
595}
596
3c5de62a 5971;
598
599# ----------------------------------------------------------------------
600
601=pod
602
603=head1 AUTHOR
604
605Ken Y. Clark E<lt>kclark@cpan.orgE<gt>
606
607=cut