More refactoring and code tidy. We now have get_attributes and
[dbsrgits/SQL-Translator.git] / lib / SQL / Translator / Schema.pm
index fcaa9cf..1880d6e 100644 (file)
@@ -1,7 +1,7 @@
 package SQL::Translator::Schema;
 
 # ----------------------------------------------------------------------
-# $Id: Schema.pm,v 1.3 2003-05-05 04:33:22 kycl4rk Exp $
+# $Id: Schema.pm,v 1.8 2003-06-18 23:14:31 kycl4rk Exp $
 # ----------------------------------------------------------------------
 # Copyright (C) 2003 Ken Y. Clark <kclark@cpan.org>
 #
@@ -44,13 +44,15 @@ returns the database structure.
 
 use strict;
 use Class::Base;
+use SQL::Translator::Schema::Constants;
 use SQL::Translator::Schema::Table;
+use SQL::Translator::Utils 'parse_list_arg';
 use SQL::Translator::Schema::View;
 
 use base 'Class::Base';
 use vars qw[ $VERSION $TABLE_ORDER $VIEW_ORDER ];
 
-$VERSION = 1.00;
+$VERSION = sprintf "%d.%02d", q$Revision: 1.8 $ =~ /(\d+)\.(\d+)/;
 
 # ----------------------------------------------------------------------
 sub init {
@@ -85,10 +87,9 @@ The "name" parameter is required.  If you try to create a table with the
 same name as an existing table, you will get an error and the table will 
 not be created.
 
-  my $table_foo = $schema->add_table( name => 'foo' ) or die $schema->error;
-
-  my $table_bar = SQL::Translator::Schema::Table->new( name => 'bar' );
-  $table_bar    = $schema->add_table( $table_bar ) or die $schema->error;
+  my $t1 = $schema->add_table( name => 'foo' ) or die $schema->error;
+  my $t2 = SQL::Translator::Schema::Table->new( name => 'bar' );
+  $t2    = $schema->add_table( $table_bar ) or die $schema->error;
 
 =cut
 
@@ -107,6 +108,7 @@ not be created.
             $self->error( $table_class->error );
     }
 
+    $table->order( ++$TABLE_ORDER );
     my $table_name = $table->name or return $self->error('No table name');
 
     if ( defined $self->{'tables'}{ $table_name } ) {
@@ -114,7 +116,6 @@ not be created.
     }
     else {
         $self->{'tables'}{ $table_name } = $table;
-        $self->{'tables'}{ $table_name }{'order'} = ++$TABLE_ORDER;
     }
 
     return $table;
@@ -132,10 +133,9 @@ The "name" parameter is required.  If you try to create a view with the
 same name as an existing view, you will get an error and the view will 
 not be created.
 
-  my $view_foo = $schema->add_view( name => 'foo' );
-
-  my $view_bar = SQL::Translator::Schema::View->new( name => 'bar' );
-  $view_bar    = $schema->add_view( $view_bar ) or die $schema->error;
+  my $v1 = $schema->add_view( name => 'foo' );
+  my $v2 = SQL::Translator::Schema::View->new( name => 'bar' );
+  $v2    = $schema->add_view( $view_bar ) or die $schema->error;
 
 =cut
 
@@ -152,6 +152,7 @@ not be created.
         $view = $view_class->new( \%args ) or return $view_class->error;
     }
 
+    $view->order( ++$VIEW_ORDER );
     my $view_name = $view->name or return $self->error('No view name');
 
     if ( defined $self->{'views'}{ $view_name } ) { 
@@ -159,7 +160,6 @@ not be created.
     }
     else {
         $self->{'views'}{ $view_name } = $view;
-        $self->{'views'}{ $view_name }{'order'} = ++$VIEW_ORDER;
     }
 
     return $view;
@@ -241,7 +241,10 @@ Returns all the tables as an array or array reference.
 =cut
 
     my $self   = shift;
-    my @tables = sort { $a->{'order'} <=> $b->{'order'} } 
+    my @tables = 
+        map  { $_->[1] } 
+        sort { $a->[0] <=> $b->[0] } 
+        map  { [ $_->order, $_ ] }
         values %{ $self->{'tables'} };
 
     if ( @tables ) {
@@ -287,7 +290,10 @@ Returns all the views as an array or array reference.
 =cut
 
     my $self  = shift;
-    my @views = sort { $a->{'order'} <=> $b->{'order'} } 
+    my @views = 
+        map  { $_->[1] } 
+        sort { $a->[0] <=> $b->[0] } 
+        map  { [ $_->order, $_ ] }
         values %{ $self->{'views'} };
 
     if ( @views ) {
@@ -300,6 +306,79 @@ Returns all the views as an array or array reference.
 }
 
 # ----------------------------------------------------------------------
+sub make_natural_joins {
+
+=pod
+
+=head2 make_natural_joins
+
+Creates foriegn key relationships among like-named fields in different
+tables.  Accepts the following arguments:
+
+=over 4
+
+=item * join_pk_only 
+
+A True or False argument which determins whether or not to perform 
+the joins from primary keys to fields of the same name in other tables
+
+=item * skip_fields
+
+A list of fields to skip in the joins
+
+=back 4
+
+  $schema->make_natural_joins(
+      join_pk_only => 1,
+      skip_fields  => 'name,department_id',
+  );
+
+=cut
+
+    my $self         = shift;
+    my %args         = @_;
+    my $join_pk_only = $args{'join_pk_only'} || 0;
+    my %skip_fields  = map { s/^\s+|\s+$//g; $_, 1 } @{ 
+        parse_list_arg( $args{'skip_fields'} ) 
+    };
+
+    my ( %common_keys, %pk );
+    for my $table ( $self->get_tables ) {
+        for my $field ( $table->get_fields ) {
+            my $field_name = $field->name or next;
+            next if $skip_fields{ $field_name };
+            $pk{ $field_name } = 1 if $field->is_primary_key;
+            push @{ $common_keys{ $field_name } }, $table->name;
+        }
+    } 
+   
+    for my $field ( keys %common_keys ) {
+        next if $join_pk_only and !defined $pk{ $field };
+
+        my @table_names = @{ $common_keys{ $field } };
+        next unless scalar @table_names > 1;
+
+        for my $i ( 0 .. $#table_names ) {
+            my $table1 = $self->get_table( $table_names[ $i ] ) or next;
+
+            for my $j ( 1 .. $#table_names ) {
+                my $table2 = $self->get_table( $table_names[ $j ] ) or next;
+                next if $table1->name eq $table2->name;
+
+                $table1->add_constraint(
+                    type             => FOREIGN_KEY,
+                    fields           => $field,
+                    reference_table  => $table2->name,
+                    reference_fields => $field,
+                );
+            }               
+        }
+    } 
+
+    return 1;
+}
+
+# ----------------------------------------------------------------------
 sub name {
 
 =pod
@@ -317,6 +396,13 @@ Get or set the schema's name.  (optional)
     return $self->{'name'} || '';
 }
 
+# ----------------------------------------------------------------------
+sub DESTROY {
+    my $self = shift;
+    undef $_ for values %{ $self->{'tables'} };
+    undef $_ for values %{ $self->{'views'}  };
+}
+
 1;
 
 # ----------------------------------------------------------------------