Commit | Line | Data |
3c5de62a |
1 | package SQL::Translator::Schema::Table; |
2 | |
3 | # ---------------------------------------------------------------------- |
1ea530d4 |
4 | # $Id: Table.pm,v 1.11 2003-08-20 13:50:47 dlc 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 | |
27 | SQL::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 | |
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; |
30f4ec44 |
44 | use SQL::Translator::Utils 'parse_list_arg'; |
0f3cc5c0 |
45 | use SQL::Translator::Schema::Constants; |
3c5de62a |
46 | use SQL::Translator::Schema::Constraint; |
47 | use SQL::Translator::Schema::Field; |
48 | use SQL::Translator::Schema::Index; |
49 | |
50 | use base 'Class::Base'; |
0f3cc5c0 |
51 | use vars qw( $VERSION $FIELD_ORDER ); |
3c5de62a |
52 | |
1ea530d4 |
53 | $VERSION = sprintf "%d.%02d", q$Revision: 1.11 $ =~ /(\d+)\.(\d+)/; |
3c5de62a |
54 | |
55 | # ---------------------------------------------------------------------- |
56 | sub init { |
57 | |
58 | =pod |
59 | |
60 | =head2 new |
61 | |
62 | Object 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 |
82 | sub add_constraint { |
83 | |
84 | =pod |
85 | |
86 | =head2 add_constraint |
87 | |
0f3cc5c0 |
88 | Add a constraint to the table. Returns the newly created |
89 | C<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 | # |
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 | |
3c5de62a |
151 | return $constraint; |
152 | } |
153 | |
154 | # ---------------------------------------------------------------------- |
155 | sub add_index { |
156 | |
157 | =pod |
158 | |
159 | =head2 add_index |
160 | |
0f3cc5c0 |
161 | Add an index to the table. Returns the newly created |
162 | C<SQL::Translator::Schema::Index> object. |
3c5de62a |
163 | |
dfdb0568 |
164 | my $i1 = $table->add_index( |
3c5de62a |
165 | name => 'name', |
166 | fields => [ 'name' ], |
167 | type => 'normal', |
168 | ); |
169 | |
dfdb0568 |
170 | my $i2 = SQL::Translator::Schema::Index->new( name => 'id' ); |
171 | $i2 = $table->add_index( $index ); |
43b9dc7a |
172 | |
3c5de62a |
173 | =cut |
174 | |
43b9dc7a |
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 | |
3c5de62a |
190 | push @{ $self->{'indices'} }, $index; |
191 | return $index; |
192 | } |
193 | |
194 | # ---------------------------------------------------------------------- |
195 | sub add_field { |
196 | |
197 | =pod |
198 | |
199 | =head2 add_field |
200 | |
43b9dc7a |
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. |
3c5de62a |
205 | |
dfdb0568 |
206 | my $f1 = $table->add_field( |
0f3cc5c0 |
207 | name => 'foo_id', |
208 | data_type => 'integer', |
209 | size => 11, |
3c5de62a |
210 | ); |
211 | |
dfdb0568 |
212 | my $f2 = SQL::Translator::Schema::Field->new( |
43b9dc7a |
213 | name => 'name', |
214 | table => $table, |
215 | ); |
dfdb0568 |
216 | $f2 = $table->add_field( $field2 ) or die $table->error; |
43b9dc7a |
217 | |
3c5de62a |
218 | =cut |
219 | |
dfdb0568 |
220 | my $self = shift; |
43b9dc7a |
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 | |
30f4ec44 |
235 | $field->order( ++$FIELD_ORDER ); |
43b9dc7a |
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; |
43b9dc7a |
243 | } |
244 | |
3c5de62a |
245 | return $field; |
246 | } |
247 | |
248 | # ---------------------------------------------------------------------- |
88b8377e |
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; |
1ea530d4 |
267 | $self->{'comments'} = [ ] |
268 | unless (defined $self->{'comments'} && |
269 | ref($self->{'comments'}) eq 'ARRAY'); |
b891fb49 |
270 | |
271 | for my $arg ( @_ ) { |
272 | $arg = $arg->[0] if ref $arg; |
1ea530d4 |
273 | push @{ $self->{'comments'} }, $arg |
274 | if defined $arg; |
b891fb49 |
275 | } |
88b8377e |
276 | |
277 | return wantarray |
1ea530d4 |
278 | ? @{ $self->{'comments'} } |
279 | : join( "\n", @{ $self->{'comments'} } ); |
88b8377e |
280 | } |
281 | |
282 | # ---------------------------------------------------------------------- |
0f3cc5c0 |
283 | sub get_constraints { |
284 | |
285 | =pod |
286 | |
287 | =head2 get_constraints |
288 | |
289 | Returns all the constraint objects as an array or array reference. |
290 | |
291 | my @constraints = $table->get_constraints; |
292 | |
293 | =cut |
294 | |
295 | my $self = shift; |
296 | |
297 | if ( ref $self->{'constraints'} ) { |
298 | return wantarray |
299 | ? @{ $self->{'constraints'} } : $self->{'constraints'}; |
300 | } |
301 | else { |
302 | $self->error('No constraints'); |
303 | return wantarray ? () : undef; |
304 | } |
305 | } |
306 | |
307 | # ---------------------------------------------------------------------- |
308 | sub get_indices { |
3c5de62a |
309 | |
310 | =pod |
311 | |
0f3cc5c0 |
312 | =head2 get_indices |
3c5de62a |
313 | |
0f3cc5c0 |
314 | Returns all the index objects as an array or array reference. |
3c5de62a |
315 | |
0f3cc5c0 |
316 | my @indices = $table->get_indices; |
3c5de62a |
317 | |
318 | =cut |
319 | |
320 | my $self = shift; |
0f3cc5c0 |
321 | |
322 | if ( ref $self->{'indices'} ) { |
323 | return wantarray |
324 | ? @{ $self->{'indices'} } |
325 | : $self->{'indices'}; |
326 | } |
327 | else { |
328 | $self->error('No indices'); |
329 | return wantarray ? () : undef; |
330 | } |
331 | } |
332 | |
333 | # ---------------------------------------------------------------------- |
43b9dc7a |
334 | sub get_field { |
335 | |
336 | =pod |
337 | |
338 | =head2 get_field |
339 | |
340 | Returns a field by the name provided. |
341 | |
342 | my $field = $table->get_field('foo'); |
343 | |
344 | =cut |
345 | |
346 | my $self = shift; |
347 | my $field_name = shift or return $self->error('No field name'); |
348 | return $self->error( qq[Field "$field_name" does not exist] ) unless |
349 | exists $self->{'fields'}{ $field_name }; |
350 | return $self->{'fields'}{ $field_name }; |
351 | } |
352 | |
353 | # ---------------------------------------------------------------------- |
0f3cc5c0 |
354 | sub get_fields { |
355 | |
356 | =pod |
357 | |
358 | =head2 get_fields |
359 | |
360 | Returns all the field objects as an array or array reference. |
361 | |
362 | my @fields = $table->get_fields; |
363 | |
364 | =cut |
365 | |
366 | my $self = shift; |
367 | my @fields = |
30f4ec44 |
368 | map { $_->[1] } |
369 | sort { $a->[0] <=> $b->[0] } |
370 | map { [ $_->order, $_ ] } |
0f3cc5c0 |
371 | values %{ $self->{'fields'} || {} }; |
372 | |
373 | if ( @fields ) { |
374 | return wantarray ? @fields : \@fields; |
375 | } |
376 | else { |
377 | $self->error('No fields'); |
378 | return wantarray ? () : undef; |
379 | } |
3c5de62a |
380 | } |
381 | |
382 | # ---------------------------------------------------------------------- |
383 | sub is_valid { |
384 | |
385 | =pod |
386 | |
387 | =head2 is_valid |
388 | |
389 | Determine whether the view is valid or not. |
390 | |
391 | my $ok = $view->is_valid; |
392 | |
393 | =cut |
394 | |
395 | my $self = shift; |
43b9dc7a |
396 | return $self->error('No name') unless $self->name; |
0f3cc5c0 |
397 | return $self->error('No fields') unless $self->get_fields; |
398 | |
399 | for my $object ( |
400 | $self->get_fields, $self->get_indices, $self->get_constraints |
401 | ) { |
402 | return $object->error unless $object->is_valid; |
403 | } |
404 | |
405 | return 1; |
3c5de62a |
406 | } |
407 | |
43b9dc7a |
408 | # ---------------------------------------------------------------------- |
dfdb0568 |
409 | sub name { |
410 | |
411 | =pod |
412 | |
413 | =head2 name |
414 | |
415 | Get or set the table's name. |
416 | |
417 | If provided an argument, checks the schema object for a table of |
418 | that name and disallows the change if one exists. |
419 | |
420 | my $table_name = $table->name('foo'); |
421 | |
422 | =cut |
423 | |
424 | my $self = shift; |
425 | |
426 | if ( my $arg = shift ) { |
427 | if ( my $schema = $self->schema ) { |
428 | return $self->error( qq[Can't use table name "$arg": table exists] ) |
429 | if $schema->get_table( $arg ); |
430 | } |
431 | $self->{'name'} = $arg; |
432 | } |
433 | |
434 | return $self->{'name'} || ''; |
435 | } |
436 | |
437 | # ---------------------------------------------------------------------- |
43b9dc7a |
438 | sub schema { |
439 | |
440 | =pod |
441 | |
442 | =head2 schema |
443 | |
444 | Get or set the table's schema object. |
445 | |
446 | my $schema = $table->schema; |
447 | |
448 | =cut |
449 | |
450 | my $self = shift; |
451 | if ( my $arg = shift ) { |
452 | return $self->error('Not a schema object') unless |
453 | UNIVERSAL::isa( $arg, 'SQL::Translator::Schema' ); |
454 | $self->{'schema'} = $arg; |
455 | } |
456 | |
457 | return $self->{'schema'}; |
458 | } |
459 | |
460 | # ---------------------------------------------------------------------- |
461 | sub primary_key { |
462 | |
463 | =pod |
464 | |
465 | =head2 options |
466 | |
5e84ac85 |
467 | Gets or sets the table's primary key(s). Takes one or more field |
468 | names (as a string, list or array[ref]) as an argument. If the field |
469 | names are present, it will create a new PK if none exists, or it will |
470 | add to the fields of an existing PK (and will unique the field names). |
471 | Returns the C<SQL::Translator::Schema::Constraint> object representing |
472 | the primary key. |
473 | |
474 | These are eqivalent: |
43b9dc7a |
475 | |
476 | $table->primary_key('id'); |
5e84ac85 |
477 | $table->primary_key(['name']); |
478 | $table->primary_key('id','name']); |
43b9dc7a |
479 | $table->primary_key(['id','name']); |
480 | $table->primary_key('id,name'); |
481 | $table->primary_key(qw[ id name ]); |
482 | |
483 | my $pk = $table->primary_key; |
484 | |
485 | =cut |
486 | |
30f4ec44 |
487 | my $self = shift; |
488 | my $fields = parse_list_arg( @_ ); |
43b9dc7a |
489 | |
5e84ac85 |
490 | my $constraint; |
43b9dc7a |
491 | if ( @$fields ) { |
492 | for my $f ( @$fields ) { |
493 | return $self->error(qq[Invalid field "$f"]) unless |
494 | $self->get_field($f); |
495 | } |
496 | |
497 | my $has_pk; |
498 | for my $c ( $self->get_constraints ) { |
499 | if ( $c->type eq PRIMARY_KEY ) { |
500 | $has_pk = 1; |
501 | $c->fields( @{ $c->fields }, @$fields ); |
5e84ac85 |
502 | $constraint = $c; |
43b9dc7a |
503 | } |
504 | } |
505 | |
506 | unless ( $has_pk ) { |
5e84ac85 |
507 | $constraint = $self->add_constraint( |
43b9dc7a |
508 | type => PRIMARY_KEY, |
509 | fields => $fields, |
88b8377e |
510 | ) or return; |
43b9dc7a |
511 | } |
512 | } |
513 | |
5e84ac85 |
514 | if ( $constraint ) { |
515 | return $constraint; |
516 | } |
517 | else { |
518 | for my $c ( $self->get_constraints ) { |
519 | return $c if $c->type eq PRIMARY_KEY; |
520 | } |
43b9dc7a |
521 | } |
522 | |
dfdb0568 |
523 | return; |
43b9dc7a |
524 | } |
525 | |
526 | # ---------------------------------------------------------------------- |
527 | sub options { |
528 | |
529 | =pod |
530 | |
531 | =head2 options |
532 | |
533 | Get or set the table's options (e.g., table types for MySQL). Returns |
534 | an array or array reference. |
535 | |
536 | my @options = $table->options; |
537 | |
538 | =cut |
539 | |
540 | my $self = shift; |
30f4ec44 |
541 | my $options = parse_list_arg( @_ ); |
43b9dc7a |
542 | |
543 | push @{ $self->{'options'} }, @$options; |
544 | |
545 | if ( ref $self->{'options'} ) { |
546 | return wantarray ? @{ $self->{'options'} || [] } : $self->{'options'}; |
547 | } |
548 | else { |
549 | return wantarray ? () : []; |
550 | } |
551 | } |
552 | |
30f4ec44 |
553 | # ---------------------------------------------------------------------- |
554 | sub order { |
555 | |
556 | =pod |
557 | |
558 | =head2 order |
559 | |
560 | Get or set the table's order. |
561 | |
562 | my $order = $table->order(3); |
563 | |
564 | =cut |
565 | |
566 | my ( $self, $arg ) = @_; |
567 | |
568 | if ( defined $arg && $arg =~ /^\d+$/ ) { |
569 | $self->{'order'} = $arg; |
570 | } |
571 | |
572 | return $self->{'order'} || 0; |
573 | } |
574 | |
575 | # ---------------------------------------------------------------------- |
576 | sub DESTROY { |
577 | my $self = shift; |
578 | undef $self->{'schema'}; # destroy cyclical reference |
579 | undef $_ for @{ $self->{'constraints'} }; |
580 | undef $_ for @{ $self->{'indices'} }; |
581 | undef $_ for values %{ $self->{'fields'} }; |
582 | } |
583 | |
3c5de62a |
584 | 1; |
585 | |
586 | # ---------------------------------------------------------------------- |
587 | |
588 | =pod |
589 | |
590 | =head1 AUTHOR |
591 | |
592 | Ken Y. Clark E<lt>kclark@cpan.orgE<gt> |
593 | |
594 | =cut |