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