1 package SQL::Translator::Schema;
5 # ----------------------------------------------------------------------
6 # $Id: Schema.pm,v 1.23 2005-06-08 15:31:06 mwz444 Exp $
7 # ----------------------------------------------------------------------
8 # Copyright (C) 2002-4 SQLFairy Authors
10 # This program is free software; you can redistribute it and/or
11 # modify it under the terms of the GNU General Public License as
12 # published by the Free Software Foundation; version 2.
14 # This program is distributed in the hope that it will be useful, but
15 # WITHOUT ANY WARRANTY; without even the implied warranty of
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 # General Public License for more details.
19 # You should have received a copy of the GNU General Public License
20 # along with this program; if not, write to the Free Software
21 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
23 # -------------------------------------------------------------------
29 SQL::Translator::Schema - SQL::Translator schema object
33 use SQL::Translator::Schema;
34 my $schema = SQL::Translator::Schema->new(
38 my $table = $schema->add_table( name => 'foo' );
39 my $view = $schema->add_view( name => 'bar', sql => '...' );
44 C<SQL::Translator::Schema> is the object that accepts, validates, and
45 returns the database structure.
52 use SQL::Translator::Schema::Constants;
53 use SQL::Translator::Schema::Procedure;
54 use SQL::Translator::Schema::Table;
55 use SQL::Translator::Schema::Trigger;
56 use SQL::Translator::Schema::View;
57 use SQL::Translator::Schema::Graph;
58 use SQL::Translator::Utils 'parse_list_arg';
60 use base 'SQL::Translator::Schema::Object';
61 use vars qw[ $VERSION $TABLE_ORDER $VIEW_ORDER $TRIGGER_ORDER $PROC_ORDER ];
63 $VERSION = sprintf "%d.%02d", q$Revision: 1.23 $ =~ /(\d+)\.(\d+)/;
65 __PACKAGE__->_attributes(qw/name database translator/);
67 # ----------------------------------------------------------------------
74 Returns the schema as an L<SQL::Translator::Schema::Graph> object.
79 return SQL::Translator::Schema::Graph->new(
80 translator => $self->translator );
83 # ----------------------------------------------------------------------
90 Add a table object. Returns the new SQL::Translator::Schema::Table object.
91 The "name" parameter is required. If you try to create a table with the
92 same name as an existing table, you will get an error and the table will
95 my $t1 = $schema->add_table( name => 'foo' ) or die $schema->error;
96 my $t2 = SQL::Translator::Schema::Table->new( name => 'bar' );
97 $t2 = $schema->add_table( $table_bar ) or die $schema->error;
102 my $table_class = 'SQL::Translator::Schema::Table';
105 if ( UNIVERSAL::isa( $_[0], $table_class ) ) {
107 $table->schema($self);
111 $args{'schema'} = $self;
112 $table = $table_class->new( \%args )
113 or return $self->error( $table_class->error );
116 $table->order( ++$TABLE_ORDER );
118 # We know we have a name as the Table->new above errors if none given.
119 my $table_name = $table->name;
121 if ( defined $self->{'tables'}{$table_name} ) {
122 return $self->error(qq[Can't create table: "$table_name" exists]);
125 $self->{'tables'}{$table_name} = $table;
131 # ----------------------------------------------------------------------
138 Remove a table from the schema. Returns the table object if the table was found
139 and removed, an error otherwise. The single parameter can be either a table
140 name or an C<SQL::Translator::Schema::Table> object. The "cascade" parameter
141 can be set to 1 to also drop all triggers on the table, default is 0.
143 $schema->drop_table('mytable');
144 $schema->drop_table('mytable', cascade => 1);
149 my $table_class = 'SQL::Translator::Schema::Table';
152 if ( UNIVERSAL::isa( $_[0], $table_class ) ) {
153 $table_name = shift->name;
159 my $cascade = $args{'cascade'};
161 if ( !exists $self->{'tables'}{$table_name} ) {
162 return $self->error(qq[Can't drop table: $table_name" doesn't exist]);
165 my $table = delete $self->{'tables'}{$table_name};
169 # Drop all triggers on this table
170 $self->drop_trigger()
171 for ( grep { $_->on_table eq $table_name } @{ $self->{'triggers'} } );
176 # ----------------------------------------------------------------------
183 Add a procedure object. Returns the new SQL::Translator::Schema::Procedure
184 object. The "name" parameter is required. If you try to create a procedure
185 with the same name as an existing procedure, you will get an error and the
186 procedure will not be created.
188 my $p1 = $schema->add_procedure( name => 'foo' );
189 my $p2 = SQL::Translator::Schema::Procedure->new( name => 'bar' );
190 $p2 = $schema->add_procedure( $procedure_bar ) or die $schema->error;
195 my $procedure_class = 'SQL::Translator::Schema::Procedure';
198 if ( UNIVERSAL::isa( $_[0], $procedure_class ) ) {
200 $procedure->schema($self);
204 $args{'schema'} = $self;
205 return $self->error('No procedure name') unless $args{'name'};
206 $procedure = $procedure_class->new( \%args )
207 or return $self->error( $procedure_class->error );
210 $procedure->order( ++$PROC_ORDER );
211 my $procedure_name = $procedure->name
212 or return $self->error('No procedure name');
214 if ( defined $self->{'procedures'}{$procedure_name} ) {
216 qq[Can't create procedure: "$procedure_name" exists] );
219 $self->{'procedures'}{$procedure_name} = $procedure;
225 # ----------------------------------------------------------------------
230 =head2 drop_procedure
232 Remove a procedure from the schema. Returns the procedure object if the
233 procedure was found and removed, an error otherwise. The single parameter
234 can be either a procedure name or an C<SQL::Translator::Schema::Procedure>
237 $schema->drop_procedure('myprocedure');
242 my $proc_class = 'SQL::Translator::Schema::Procedure';
245 if ( UNIVERSAL::isa( $_[0], $proc_class ) ) {
246 $proc_name = shift->name;
252 if ( !exists $self->{'procedures'}{$proc_name} ) {
254 qq[Can't drop procedure: $proc_name" doesn't exist]);
257 my $proc = delete $self->{'procedures'}{$proc_name};
262 # ----------------------------------------------------------------------
269 Add a trigger object. Returns the new SQL::Translator::Schema::Trigger object.
270 The "name" parameter is required. If you try to create a trigger with the
271 same name as an existing trigger, you will get an error and the trigger will
274 my $t1 = $schema->add_trigger( name => 'foo' );
275 my $t2 = SQL::Translator::Schema::Trigger->new( name => 'bar' );
276 $t2 = $schema->add_trigger( $trigger_bar ) or die $schema->error;
281 my $trigger_class = 'SQL::Translator::Schema::Trigger';
284 if ( UNIVERSAL::isa( $_[0], $trigger_class ) ) {
286 $trigger->schema($self);
290 $args{'schema'} = $self;
291 return $self->error('No trigger name') unless $args{'name'};
292 $trigger = $trigger_class->new( \%args )
293 or return $self->error( $trigger_class->error );
296 $trigger->order( ++$TRIGGER_ORDER );
297 my $trigger_name = $trigger->name or return $self->error('No trigger name');
299 if ( defined $self->{'triggers'}{$trigger_name} ) {
300 return $self->error(qq[Can't create trigger: "$trigger_name" exists]);
303 $self->{'triggers'}{$trigger_name} = $trigger;
309 # ----------------------------------------------------------------------
316 Remove a trigger from the schema. Returns the trigger object if the trigger was
317 found and removed, an error otherwise. The single parameter can be either a
318 trigger name or an C<SQL::Translator::Schema::Trigger> object.
320 $schema->drop_trigger('mytrigger');
325 my $trigger_class = 'SQL::Translator::Schema::Trigger';
328 if ( UNIVERSAL::isa( $_[0], $trigger_class ) ) {
329 $trigger_name = shift->name;
332 $trigger_name = shift;
335 if ( !exists $self->{'triggers'}{$trigger_name} ) {
337 qq[Can't drop trigger: $trigger_name" doesn't exist]);
340 my $trigger = delete $self->{'triggers'}{$trigger_name};
345 # ----------------------------------------------------------------------
352 Add a view object. Returns the new SQL::Translator::Schema::View object.
353 The "name" parameter is required. If you try to create a view with the
354 same name as an existing view, you will get an error and the view will
357 my $v1 = $schema->add_view( name => 'foo' );
358 my $v2 = SQL::Translator::Schema::View->new( name => 'bar' );
359 $v2 = $schema->add_view( $view_bar ) or die $schema->error;
364 my $view_class = 'SQL::Translator::Schema::View';
367 if ( UNIVERSAL::isa( $_[0], $view_class ) ) {
369 $view->schema($self);
373 $args{'schema'} = $self;
374 return $self->error('No view name') unless $args{'name'};
375 $view = $view_class->new( \%args ) or return $view_class->error;
378 $view->order( ++$VIEW_ORDER );
379 my $view_name = $view->name or return $self->error('No view name');
381 if ( defined $self->{'views'}{$view_name} ) {
382 return $self->error(qq[Can't create view: "$view_name" exists]);
385 $self->{'views'}{$view_name} = $view;
391 # ----------------------------------------------------------------------
398 Remove a view from the schema. Returns the view object if the view was found
399 and removed, an error otherwise. The single parameter can be either a view
400 name or an C<SQL::Translator::Schema::View> object.
402 $schema->drop_view('myview');
407 my $view_class = 'SQL::Translator::Schema::View';
410 if ( UNIVERSAL::isa( $_[0], $view_class ) ) {
411 $view_name = shift->name;
417 if ( !exists $self->{'views'}{$view_name} ) {
418 return $self->error(qq[Can't drop view: $view_name" doesn't exist]);
421 my $view = delete $self->{'views'}{$view_name};
426 # ----------------------------------------------------------------------
433 Get or set the schema's database. (optional)
435 my $database = $schema->database('PostgreSQL');
440 $self->{'database'} = shift if @_;
441 return $self->{'database'} || '';
444 # ----------------------------------------------------------------------
451 Returns true if all the tables and views are valid.
453 my $ok = $schema->is_valid or die $schema->error;
459 return $self->error('No tables') unless $self->get_tables;
461 for my $object ( $self->get_tables, $self->get_views ) {
462 return $object->error unless $object->is_valid;
468 # ----------------------------------------------------------------------
475 Returns a procedure by the name provided.
477 my $procedure = $schema->get_procedure('foo');
482 my $procedure_name = shift or return $self->error('No procedure name');
483 return $self->error(qq[Table "$procedure_name" does not exist])
484 unless exists $self->{'procedures'}{$procedure_name};
485 return $self->{'procedures'}{$procedure_name};
488 # ----------------------------------------------------------------------
493 =head2 get_procedures
495 Returns all the procedures as an array or array reference.
497 my @procedures = $schema->get_procedures;
504 sort { $a->[0] <=> $b->[0] }
505 map { [ $_->order, $_ ] } values %{ $self->{'procedures'} };
508 return wantarray ? @procedures : \@procedures;
511 $self->error('No procedures');
512 return wantarray ? () : undef;
516 # ----------------------------------------------------------------------
523 Returns a table by the name provided.
525 my $table = $schema->get_table('foo');
530 my $table_name = shift or return $self->error('No table name');
531 return $self->error(qq[Table "$table_name" does not exist])
532 unless exists $self->{'tables'}{$table_name};
533 return $self->{'tables'}{$table_name};
536 # ----------------------------------------------------------------------
543 Returns all the tables as an array or array reference.
545 my @tables = $schema->get_tables;
552 sort { $a->[0] <=> $b->[0] }
553 map { [ $_->order, $_ ] } values %{ $self->{'tables'} };
556 return wantarray ? @tables : \@tables;
559 $self->error('No tables');
560 return wantarray ? () : undef;
564 # ----------------------------------------------------------------------
571 Returns a trigger by the name provided.
573 my $trigger = $schema->get_trigger('foo');
578 my $trigger_name = shift or return $self->error('No trigger name');
579 return $self->error(qq[Table "$trigger_name" does not exist])
580 unless exists $self->{'triggers'}{$trigger_name};
581 return $self->{'triggers'}{$trigger_name};
584 # ----------------------------------------------------------------------
591 Returns all the triggers as an array or array reference.
593 my @triggers = $schema->get_triggers;
600 sort { $a->[0] <=> $b->[0] }
601 map { [ $_->order, $_ ] } values %{ $self->{'triggers'} };
604 return wantarray ? @triggers : \@triggers;
607 $self->error('No triggers');
608 return wantarray ? () : undef;
612 # ----------------------------------------------------------------------
619 Returns a view by the name provided.
621 my $view = $schema->get_view('foo');
626 my $view_name = shift or return $self->error('No view name');
627 return $self->error('View "$view_name" does not exist')
628 unless exists $self->{'views'}{$view_name};
629 return $self->{'views'}{$view_name};
632 # ----------------------------------------------------------------------
639 Returns all the views as an array or array reference.
641 my @views = $schema->get_views;
648 sort { $a->[0] <=> $b->[0] }
649 map { [ $_->order, $_ ] } values %{ $self->{'views'} };
652 return wantarray ? @views : \@views;
655 $self->error('No views');
656 return wantarray ? () : undef;
660 # ----------------------------------------------------------------------
661 sub make_natural_joins {
665 =head2 make_natural_joins
667 Creates foriegn key relationships among like-named fields in different
668 tables. Accepts the following arguments:
674 A True or False argument which determins whether or not to perform
675 the joins from primary keys to fields of the same name in other tables
679 A list of fields to skip in the joins
683 $schema->make_natural_joins(
685 skip_fields => 'name,department_id',
692 my $join_pk_only = $args{'join_pk_only'} || 0;
694 map { s/^\s+|\s+$//g; $_, 1 } @{ parse_list_arg( $args{'skip_fields'} ) };
696 my ( %common_keys, %pk );
697 for my $table ( $self->get_tables ) {
698 for my $field ( $table->get_fields ) {
699 my $field_name = $field->name or next;
700 next if $skip_fields{$field_name};
701 $pk{$field_name} = 1 if $field->is_primary_key;
702 push @{ $common_keys{$field_name} }, $table->name;
706 for my $field ( keys %common_keys ) {
707 next if $join_pk_only and !defined $pk{$field};
709 my @table_names = @{ $common_keys{$field} };
710 next unless scalar @table_names > 1;
712 for my $i ( 0 .. $#table_names ) {
713 my $table1 = $self->get_table( $table_names[$i] ) or next;
715 for my $j ( 1 .. $#table_names ) {
716 my $table2 = $self->get_table( $table_names[$j] ) or next;
717 next if $table1->name eq $table2->name;
719 $table1->add_constraint(
722 reference_table => $table2->name,
723 reference_fields => $field,
732 # ----------------------------------------------------------------------
739 Get or set the schema's name. (optional)
741 my $schema_name = $schema->name('Foo Database');
746 $self->{'name'} = shift if @_;
747 return $self->{'name'} || '';
750 # ----------------------------------------------------------------------
757 Get the SQL::Translator instance that instantiated the parser.
762 $self->{'translator'} = shift if @_;
763 return $self->{'translator'};
766 # ----------------------------------------------------------------------
769 undef $_ for values %{ $self->{'tables'} };
770 undef $_ for values %{ $self->{'views'} };
775 # ----------------------------------------------------------------------
781 Ken Youens-Clark E<lt>kclark@cpan.orgE<gt>.