it was buggy
Allen Day [Mon, 9 Jun 2003 05:38:11 +0000 (05:38 +0000)]
lib/SQL/Translator/Producer/ClassDBI.pm

index d611cf4..df68e70 100755 (executable)
@@ -1,7 +1,7 @@
 package SQL::Translator::Producer::ClassDBI;
 
 # -------------------------------------------------------------------
-# $Id: ClassDBI.pm,v 1.10 2003-06-09 03:38:38 allenday Exp $
+# $Id: ClassDBI.pm,v 1.11 2003-06-09 05:38:11 allenday Exp $
 # -------------------------------------------------------------------
 # Copyright (C) 2003 Ying Zhang <zyolive@yahoo.com>,
 #                    Allen Day <allenday@ucla.edu>,
@@ -23,7 +23,7 @@ package SQL::Translator::Producer::ClassDBI;
 
 use strict;
 use vars qw[ $VERSION $DEBUG ];
-$VERSION = sprintf "%d.%02d", q$Revision: 1.10 $ =~ /(\d+)\.(\d+)/;
+$VERSION = sprintf "%d.%02d", q$Revision: 1.11 $ =~ /(\d+)\.(\d+)/;
 $DEBUG   = 1 unless defined $DEBUG;
 
 use SQL::Translator::Schema::Constants;
@@ -50,34 +50,6 @@ sub produce {
         $translator->format_package_name('DBI') . 
         "->set_db('Main', 'dbi:$from:_', \$USER, \$PASS);\n\n";
        
-
-       #
-       # Identify link tables, defined as tables that have only PK and FK
-       # fields
-       #
-       my %linkable;
-       foreach my $table ($schema->get_tables) {
-         my $is_link = 1;
-         foreach my $field ($table->get_fields){
-               $is_link = 0 and last unless $field->is_primary_key or $field->is_foreign_key;
-         }
-
-         if($is_link){
-               foreach my $left ($table->get_fields){
-                 next unless $left->is_foreign_key;
-                 foreach my $right ($table->get_fields){
-                       next unless $right->is_foreign_key;
-
-                       $linkable{$left->foreign_key_reference->reference_table}
-                         {$right->foreign_key_reference->reference_table} = $table;
-                       $linkable{$right->foreign_key_reference->reference_table}
-                         {$left->foreign_key_reference->reference_table} = $table;
-                 }
-               }
-         }
-
-       }
-
     #
     # Iterate over all tables
     #
@@ -131,154 +103,84 @@ sub produce {
               $create .= "sub " .
                     $translator->format_fk_name($ref_table, $field_name).
                     " {\n    return shift->$field_name\n}\n\n";
+
             }
         }
 
+               #
+               # Identify link tables, defined as tables that have only PK and FK
+               # fields
+               #
+               my %linkable;
+               foreach my $table ($schema->get_tables) {
+                 my $is_link = 1;
+                 foreach my $field ($table->get_fields){
+                       $is_link = 0 and last unless $field->is_primary_key or $field->is_foreign_key;
+                 }
+                 
+                 if($is_link){
+                       foreach my $left ($table->get_fields){
+                         next unless $left->is_foreign_key and 
+                               $schema->get_table(
+                                                                  $left->foreign_key_reference->reference_table
+                                                                 )->get_field(
+                                                                                          ($left->foreign_key_reference->reference_fields)[0]
+                                                                                         )->is_primary_key;
+                         
+                         foreach my $right ($table->get_fields){
+                               #skip the diagonal
+                               next if $left->name eq $right->name;
+                               
+                               next unless $right->is_foreign_key and
+                                 $schema->get_table(
+                                                                        $right->foreign_key_reference->reference_table
+                                                                       )->get_field(
+                                                                                                ($right->foreign_key_reference->reference_fields)[0]
+                                                                                               )->is_primary_key;
+                               
+                               
+                               $linkable{$left->foreign_key_reference->reference_table}{$right->foreign_key_reference->reference_table} = $table;
+                               $linkable{$right->foreign_key_reference->reference_table}{$left->foreign_key_reference->reference_table} = $table;
+
+#                              if($left->foreign_key_reference->reference_table eq 'feature' and
+#                                 $right->foreign_key_reference->reference_table eq 'pub'){
+#                                warn $left->foreign_key_reference->reference_table . " to " . $right->foreign_key_reference->reference_table . " via " . $table->name;
+#                                warn "\tleft:  ".$left->name;
+#                                warn "\tright: ".$right->name;
+#                        }
+                               
+                         }
+                       }
+                 }
+                 
+               }
+
+
+               #
+               # Generate many-to-many linking methods for data tables
+               #
                my $is_data = 0;
                map {(!$_->is_foreign_key and !$_->is_primary_key) ? $is_data++ : 0} $table->get_fields;
+               my %linked;
                if($is_data){
-                 my %linked;
-                 foreach my $field ( $table->get_fields ) {
-                       if($field->is_foreign_key){
-                         my $fk = $field->foreign_key_reference;
+                 foreach my $link (keys %{$linkable{$table->name}}){
+                       my $linkmethodname = "_".$translator->format_fk_name($table->name,$link)."_refs";
 
-#                        next if $linked{($fk->reference_fields)[0]};
-                         next unless $linkable{$fk->reference_table};
 
-                         foreach my $link (keys %{$linkable{$fk->reference_table}}){
-                               my $linkmethodname = "_".$translator->format_fk_name($fk->reference_table,$field->name)."_refs";
-
-                               next if $linked{$linkmethodname};
-
-                               $create .= $translator->format_package_name($table_name).
+                       $create .= $translator->format_package_name($table->name).
                                  "->has_many('$linkmethodname','".
-                                 $translator->format_package_name($linkable{$fk->reference_table}{$link}->name)."','".
-                                 ($fk->reference_fields)[0]."');\n";
-                               $create .= "sub ". $translator->format_fk_name($fk->reference_table,$field->name).
-####HARDCODED S HERE.  ADD CALLBACK FOR PLURALIZATION MANGLING
-                                 "s {\n    my \$self = shift; return map \$_->".($fk->reference_fields)[0].", \$self->".$linkmethodname.";\n}\n\n";
-
-                               $linked{$linkmethodname}++;
-                         }
-                       }
+                                 $translator->format_package_name($linkable{$table->name}{$link}->name)."','".
+                                 $link."');\n";
+                       $create .= "sub ". $translator->format_fk_name($table,$link).
+                                 ### HARDCODED 's' HERE.  ADD CALLBACK FOR PLURALIZATION MANGLING
+                                 "s {\n    my \$self = shift; return map \$_->".$link.
+                                 ", \$self->".$linkmethodname.";\n}\n\n";
                  }
                }
-
-    }
+         }
 
     $create .= '1;';
 
-#    for my $table (keys %{$data}) {
-#        my $table_data = $data->{$table};
-#        my @fields =  keys %{$table_data->{'fields'}};
-#        my %pk;
-#
-#        $create .= "##\n## Package: " .$translator->format_package_name($table). "\n##\n" unless $no_comments;
-#        $create .= "package ". $translator->format_package_name($table). ";\n";
-#
-#        $create .= "use base \'Chado::DBI\';\n";
-#        $create .= "use mixin \'Class::DBI::Join\';\n";
-#        $create .= "use Class::DBI::Pager;\n\n";
-#
-#        $create .= $translator->format_package_name($table). "->set_up_table('$table');\n\n";
-#
-#        #
-#        # Primary key?
-#        #
-#        foreach my $constraint ( @{ $table_data->{'constraints'} } ) {
-#          my $name       = $constraint->{'name'} || '';
-#          my $type       = $constraint->{'type'};
-#          my $ref_table  = $constraint->{'reference_table'};
-#          my $ref_fields = $constraint->{'reference_fields'};
-#
-#          if ( $type eq 'primary_key') {
-#            $pk{$table} = $constraint->{'fields'}->[0];
-#            $create .= "sub " .$translator->format_pk_name(
-#                                                           $translator->format_package_name($table),
-#                                                           $constraint->{'fields'}->[0]
-#                                                          ) . " { shift->".$constraint->{'fields'}->[0]." }\n\n";
-#          }
-#        }
-#
-#        #
-#        # Foreign key?
-#        #
-#        foreach my $field (@fields){
-#          my $field_data = $table_data->{'fields'}->{$field}->{'constraints'};
-#          my $type = $field_data->[1]->{'type'} || '';
-#          my $ref_table = $field_data->[1]->{'reference_table'};
-#          my $ref_field = $field_data->[1]->{'reference_fields'}->[0];
-#          my $field = $field_data->[1]->{'fields'}->[0];
-#
-#          if ($type eq 'foreign_key') {
-#
-#    #THIS IS IMPOSSIBLE UNTIL WE HAVE A BETTER DATA MODEL.  THIS GIANT HASH SUCKS !!!
-#    #         my $r_link     = 0; #not a link table (yet)
-#    #         my $r_linkthis = 0;
-#    #         my $r_linkthat = 0;
-#    #         my $r_linkdata = 0;
-#    #         my $r_table = $data->{$ref_table};
-#    #         my @r_fields = keys %{$r_table->{'fields'}};
-#    #         foreach my $r_field ( keys %{$r_table->{'fields'}} ){
-#    #           $r_linkthis++ and next if $r_field eq $ref_field; #remote table links to local table
-#    #           if($r_table->{'fields'}->{$r_field}->{'constraints'}){
-#
-#    #             foreach my $r_constraint ($r_table->{'fields'}->{$r_field}->{'constraints'}){
-#    #               $create .= Dumper($r_constraint);
-#    #             }
-#
-#    #           } else {
-#    #             $r_linkdata++; #if not constraints, assume it's data (safe?)
-#    #           }
-#    #           foreach my $r_constraint ( @{ $r_table->{'fields'}->{$r_field}->{'constraints'} } ) {
-#    #             next unless $r_constraint->{'constraint_type'} eq 'foreign_key';
-#
-#    #             $r_linkthat++ unless $r_constraint->{'reference_table'} eq $table; #remote table links to non-local table
-#    #           }
-#    #         }
-#
-#    #        my $link = $r_linkthis && $r_linkthat && !$r_linkdata ? '_link' : '';
-#            $create .= $translator->format_package_name($table). "->has_a(" .$translator->format_package_name($ref_table). " => \'$field\');\n";
-#            $create .= "sub " .$translator->format_fk_name($ref_table, $field)." { return shift->$field }\n\n";
-#          }
-#        }
-#
-#    #THIS IS IMPOSSIBLE UNTIL WE HAVE A BETTER DATA MODEL.  THIS GIANT HASH SUCKS !!!
-#    #     #
-#    #     # Remote foreign key?
-#    #     #
-#    #     print "****$table\n";
-#    #     # find tables that refer to this table
-#    #     my %refers = ();
-#    #     for my $remote_table (keys %{$data}){
-#    #       next if $remote_table eq $table;
-#    # #      print "********".$remote_table."\n";
-#    #       my $remote_table_data = $data->{$remote_table};
-#
-#    #       foreach my $remote_field ( keys %{$remote_table_data->{'fields'}} ){
-#    #         foreach my $remote_constraint ( @{ $remote_table_data->{'fields'}->{$remote_field}->{'constraints'} } ) {
-#    #           next unless $remote_constraint->{'constraint_type'} eq 'foreign_key'; #only interested in foreign keys...
-#
-#    #           $refers{$remote_table} = 1 if $pk{$remote_constraint->{'reference_table'}} ;#eq $remote_constraint->{'reference_fields'}->[0];
-#    #            }
-#    #       }
-#    #     }
-#
-#    #     foreach my $refer (keys %refers){
-#    #       foreach my $refer_field ( keys %{$data->{$refer}->{'fields'}} ){
-#    #         foreach my $refer_constraint ( @{ $data->{$refer}->{'fields'}->{$refer_field}->{'constraints'} } ) {
-#    #           next unless $refer_constraint->{'constraint_type'} eq 'foreign_key'; #only interested in foreign keys...
-#    #           next if $refer_constraint->{'reference_table'} eq $table; #don't want to consider the current table vs itself
-#    #           print "********".$refer."\t".$refer_field."\t****\t".$refer_constraint->{'reference_table'}."\t".$refer_constraint->{'reference_fields'}->[0]."\n";
-#
-#    #           $create .= "****sub " .$translator->format_fk_name($refer_constraint->{'reference_table'}, $refer_constraint->{'reference_fields'}->[0]). " { return shift->".$refer_constraint->{'reference_fields'}->[0]." }\n\n";
-#    #         }
-#    #       }
-#    #     }
-#
-#        $create .= "1;\n\n\n";
-#    }
-
     return $create;
 }