1 package SQL::Translator::Schema;
3 # ----------------------------------------------------------------------
4 # $Id: Schema.pm,v 1.12 2003-10-08 18:35:15 phrrngtn 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 - SQL::Translator schema object
31 use SQL::Translator::Schema;
32 my $schema = SQL::Translator::Schema->new;
33 my $table = $schema->add_table( name => 'foo' );
34 my $view = $schema->add_view( name => 'bar', sql => '...' );
38 C<SQL::Translator::Schema> is the object that accepts, validates, and
39 returns the database structure.
47 use SQL::Translator::Schema::Constants;
48 use SQL::Translator::Schema::Procedure;
49 use SQL::Translator::Schema::Table;
50 use SQL::Translator::Schema::Trigger;
51 use SQL::Translator::Schema::View;
52 use SQL::Translator::Utils 'parse_list_arg';
54 use base 'Class::Base';
55 use vars qw[ $VERSION $TABLE_ORDER $VIEW_ORDER $TRIGGER_ORDER $PROC_ORDER ];
57 $VERSION = sprintf "%d.%02d", q$Revision: 1.12 $ =~ /(\d+)\.(\d+)/;
59 # ----------------------------------------------------------------------
68 my $schema = SQL::Translator->new(
75 my ( $self, $config ) = @_;
76 $self->params( $config, qw[ name database ] ) || return undef;
80 # ----------------------------------------------------------------------
87 Add a table object. Returns the new SQL::Translator::Schema::Table object.
88 The "name" parameter is required. If you try to create a table with the
89 same name as an existing table, you will get an error and the table will
92 my $t1 = $schema->add_table( name => 'foo' ) or die $schema->error;
93 my $t2 = SQL::Translator::Schema::Table->new( name => 'bar' );
94 $t2 = $schema->add_table( $table_bar ) or die $schema->error;
99 my $table_class = 'SQL::Translator::Schema::Table';
102 if ( UNIVERSAL::isa( $_[0], $table_class ) ) {
104 $table->schema( $self );
108 $args{'schema'} = $self;
109 $table = $table_class->new( \%args ) or return
110 $self->error( $table_class->error );
113 $table->order( ++$TABLE_ORDER );
114 my $table_name = $table->name or return $self->error('No table name');
116 if ( defined $self->{'tables'}{ $table_name } ) {
117 return $self->error(qq[Can't create table: "$table_name" exists]);
120 $self->{'tables'}{ $table_name } = $table;
126 # ----------------------------------------------------------------------
133 Add a procedure object. Returns the new
134 SQL::Translator::Schema::Procedure object. The "name" parameter is
135 required. If you try to create a procedure with the same name as an
136 existing procedure, you will get an error and the procedure will not
139 my $p1 = $schema->add_procedure( name => 'foo' );
140 my $p2 = SQL::Translator::Schema::Procedure->new( name => 'bar' );
141 $p2 = $schema->add_procedure( $procedure_bar ) or die $schema->error;
146 my $procedure_class = 'SQL::Translator::Schema::Procedure';
149 if ( UNIVERSAL::isa( $_[0], $procedure_class ) ) {
151 $procedure->schema( $self );
155 $args{'schema'} = $self;
156 return $self->error('No procedure name') unless $args{'name'};
157 $procedure = $procedure_class->new( \%args ) or
158 return $self->error( $procedure_class->error );
161 $procedure->order( ++$PROC_ORDER );
162 my $procedure_name = $procedure->name or return
163 $self->error('No procedure name');
165 if ( defined $self->{'procedures'}{ $procedure_name } ) {
167 qq[Can't create procedure: "$procedure_name" exists]
171 $self->{'procedures'}{ $procedure_name } = $procedure;
177 # ----------------------------------------------------------------------
184 Add a trigger object. Returns the new SQL::Translator::Schema::Trigger object.
185 The "name" parameter is required. If you try to create a trigger with the
186 same name as an existing trigger, you will get an error and the trigger will
189 my $t1 = $schema->add_trigger( name => 'foo' );
190 my $t2 = SQL::Translator::Schema::Trigger->new( name => 'bar' );
191 $t2 = $schema->add_trigger( $trigger_bar ) or die $schema->error;
196 my $trigger_class = 'SQL::Translator::Schema::Trigger';
199 if ( UNIVERSAL::isa( $_[0], $trigger_class ) ) {
201 $trigger->schema( $self );
205 $args{'schema'} = $self;
206 return $self->error('No trigger name') unless $args{'name'};
207 $trigger = $trigger_class->new( \%args ) or
208 return $self->error( $trigger_class->error );
211 $trigger->order( ++$TRIGGER_ORDER );
212 my $trigger_name = $trigger->name or return $self->error('No trigger name');
214 if ( defined $self->{'triggers'}{ $trigger_name } ) {
215 return $self->error(qq[Can't create trigger: "$trigger_name" exists]);
218 $self->{'triggers'}{ $trigger_name } = $trigger;
224 # ----------------------------------------------------------------------
231 Add a view object. Returns the new SQL::Translator::Schema::View object.
232 The "name" parameter is required. If you try to create a view with the
233 same name as an existing view, you will get an error and the view will
236 my $v1 = $schema->add_view( name => 'foo' );
237 my $v2 = SQL::Translator::Schema::View->new( name => 'bar' );
238 $v2 = $schema->add_view( $view_bar ) or die $schema->error;
243 my $view_class = 'SQL::Translator::Schema::View';
246 if ( UNIVERSAL::isa( $_[0], $view_class ) ) {
248 $view->schema( $self );
252 $args{'schema'} = $self;
253 return $self->error('No view name') unless $args{'name'};
254 $view = $view_class->new( \%args ) or return $view_class->error;
257 $view->order( ++$VIEW_ORDER );
258 my $view_name = $view->name or return $self->error('No view name');
260 if ( defined $self->{'views'}{ $view_name } ) {
261 return $self->error(qq[Can't create view: "$view_name" exists]);
264 $self->{'views'}{ $view_name } = $view;
270 # ----------------------------------------------------------------------
277 Get or set the schema's database. (optional)
279 my $database = $schema->database('PostgreSQL');
284 $self->{'database'} = shift if @_;
285 return $self->{'database'} || '';
288 # ----------------------------------------------------------------------
295 Returns true if all the tables and views are valid.
297 my $ok = $schema->is_valid or die $schema->error;
303 return $self->error('No tables') unless $self->get_tables;
305 for my $object ( $self->get_tables, $self->get_views ) {
306 return $object->error unless $object->is_valid;
312 # ----------------------------------------------------------------------
319 Returns a procedure by the name provided.
321 my $procedure = $schema->get_procedure('foo');
326 my $procedure_name = shift or return $self->error('No procedure name');
327 return $self->error( qq[Table "$procedure_name" does not exist] ) unless
328 exists $self->{'procedures'}{ $procedure_name };
329 return $self->{'procedures'}{ $procedure_name };
332 # ----------------------------------------------------------------------
337 =head2 get_procedures
339 Returns all the procedures as an array or array reference.
341 my @procedures = $schema->get_procedures;
348 sort { $a->[0] <=> $b->[0] }
349 map { [ $_->order, $_ ] }
350 values %{ $self->{'procedures'} };
353 return wantarray ? @procedures : \@procedures;
356 $self->error('No procedures');
357 return wantarray ? () : undef;
361 # ----------------------------------------------------------------------
368 Returns a table by the name provided.
370 my $table = $schema->get_table('foo');
375 my $table_name = shift or return $self->error('No table name');
376 return $self->error( qq[Table "$table_name" does not exist] ) unless
377 exists $self->{'tables'}{ $table_name };
378 return $self->{'tables'}{ $table_name };
381 # ----------------------------------------------------------------------
388 Returns all the tables as an array or array reference.
390 my @tables = $schema->get_tables;
397 sort { $a->[0] <=> $b->[0] }
398 map { [ $_->order, $_ ] }
399 values %{ $self->{'tables'} };
402 return wantarray ? @tables : \@tables;
405 $self->error('No tables');
406 return wantarray ? () : undef;
410 # ----------------------------------------------------------------------
417 Returns a trigger by the name provided.
419 my $trigger = $schema->get_trigger('foo');
424 my $trigger_name = shift or return $self->error('No trigger name');
425 return $self->error( qq[Table "$trigger_name" does not exist] ) unless
426 exists $self->{'triggers'}{ $trigger_name };
427 return $self->{'triggers'}{ $trigger_name };
430 # ----------------------------------------------------------------------
437 Returns all the triggers as an array or array reference.
439 my @triggers = $schema->get_triggers;
446 sort { $a->[0] <=> $b->[0] }
447 map { [ $_->order, $_ ] }
448 values %{ $self->{'triggers'} };
451 return wantarray ? @triggers : \@triggers;
454 $self->error('No triggers');
455 return wantarray ? () : undef;
459 # ----------------------------------------------------------------------
466 Returns a view by the name provided.
468 my $view = $schema->get_view('foo');
473 my $view_name = shift or return $self->error('No view name');
474 return $self->error('View "$view_name" does not exist') unless
475 exists $self->{'views'}{ $view_name };
476 return $self->{'views'}{ $view_name };
479 # ----------------------------------------------------------------------
486 Returns all the views as an array or array reference.
488 my @views = $schema->get_views;
495 sort { $a->[0] <=> $b->[0] }
496 map { [ $_->order, $_ ] }
497 values %{ $self->{'views'} };
500 return wantarray ? @views : \@views;
503 $self->error('No views');
504 return wantarray ? () : undef;
508 # ----------------------------------------------------------------------
509 sub make_natural_joins {
513 =head2 make_natural_joins
515 Creates foriegn key relationships among like-named fields in different
516 tables. Accepts the following arguments:
522 A True or False argument which determins whether or not to perform
523 the joins from primary keys to fields of the same name in other tables
527 A list of fields to skip in the joins
531 $schema->make_natural_joins(
533 skip_fields => 'name,department_id',
540 my $join_pk_only = $args{'join_pk_only'} || 0;
541 my %skip_fields = map { s/^\s+|\s+$//g; $_, 1 } @{
542 parse_list_arg( $args{'skip_fields'} )
545 my ( %common_keys, %pk );
546 for my $table ( $self->get_tables ) {
547 for my $field ( $table->get_fields ) {
548 my $field_name = $field->name or next;
549 next if $skip_fields{ $field_name };
550 $pk{ $field_name } = 1 if $field->is_primary_key;
551 push @{ $common_keys{ $field_name } }, $table->name;
555 for my $field ( keys %common_keys ) {
556 next if $join_pk_only and !defined $pk{ $field };
558 my @table_names = @{ $common_keys{ $field } };
559 next unless scalar @table_names > 1;
561 for my $i ( 0 .. $#table_names ) {
562 my $table1 = $self->get_table( $table_names[ $i ] ) or next;
564 for my $j ( 1 .. $#table_names ) {
565 my $table2 = $self->get_table( $table_names[ $j ] ) or next;
566 next if $table1->name eq $table2->name;
568 $table1->add_constraint(
571 reference_table => $table2->name,
572 reference_fields => $field,
581 # ----------------------------------------------------------------------
588 Get or set the schema's name. (optional)
590 my $schema_name = $schema->name('Foo Database');
595 $self->{'name'} = shift if @_;
596 return $self->{'name'} || '';
599 # ----------------------------------------------------------------------
602 undef $_ for values %{ $self->{'tables'} };
603 undef $_ for values %{ $self->{'views'} };
608 # ----------------------------------------------------------------------
614 Ken Y. Clark E<lt>kclark@cpan.orgE<gt>