[CDBI] CDBI::Loader and MySQL 5

[prev] [thread] [next] [Date index for 2006/01/19]

From: Frank Wiegand
Subject: [CDBI] CDBI::Loader and MySQL 5
Date: 12:01 on 19 Jan 2006
------=_Part_5647_6517492.1137672110145
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable
Content-Disposition: inline

Hi,

here's a little patch that adds support for setting up relationships
automatically with MySQL 5.

The patch is against the SVN rev. 3104 (at dev.catalyst.perl.org).

Bye, Frank

------=_Part_5647_6517492.1137672110145
Content-Type: text/x-patch; name=mysql.pm.patch; charset=us-ascii
Content-Transfer-Encoding: 7bit
Content-Disposition: attachment; filename="mysql.pm.patch"

Index: Class/DBI/Loader/mysql.pm
===================================================================
--- Class/DBI/Loader/mysql.pm	(Revision 3104)
+++ Class/DBI/Loader/mysql.pm	(Arbeitskopie)
@@ -50,19 +50,39 @@
     my $dbname = $conn{database} || $conn{dbname} || $conn{db};
     die("Can't figure out the table name automatically.") if !$dbname;
     my $quoter = $dbh->get_info(29);
+    my $is_mysql5 = $dbh->get_info(18) =~ /^5./;
 
     foreach my $table (@tables) {
-        my $query = "SHOW TABLE STATUS FROM $dbname LIKE '$table'";
-        my $sth   = $dbh->prepare($query)
-          or die("Cannot get table status: $table");
-        $sth->execute;
-        my $comment = $sth->fetchrow_hashref->{comment};
-        $comment =~ s/$quoter//g if ($quoter);
-        while ( $comment =~ m!\(`?(\w+)`?\)\sREFER\s`?\w+/(\w+)`?\(`?\w+`?\)!g ) {
-            eval { $self->_has_a_many( $table, $1, $2 ) };
-            warn qq/\# has_a_many failed "$@"\n\n/ if $@ && $self->debug;
+        if ($is_mysql5) {
+            my $query = qq(
+                SELECT column_name,
+                       referenced_table_name
+                  FROM information_schema.key_column_usage
+                 WHERE referenced_table_name IS NOT NULL
+                   AND table_name = ?
+            );
+            my $sth = $dbh->prepare($query)
+                or die("Cannot get table information: $table");
+            $sth->execute($table);
+            while ( my $data = $sth->fetchrow_hashref ) {
+                eval { $self->_has_a_many( $table, $data->{column_name}, $data->{referenced_table_name} ) };
+                warn qq/\# has_a_many failed "$@"\n\n/ if $@ && $self->debug;
+            }
+            $sth->finish;
         }
-        $sth->finish;
+        else {
+            my $query = "SHOW TABLE STATUS FROM $dbname LIKE '$table'";
+            my $sth   = $dbh->prepare($query)
+              or die("Cannot get table status: $table");
+            $sth->execute;
+            my $comment = $sth->fetchrow_hashref->{comment};
+            $comment =~ s/$quoter//g if ($quoter);
+            while ( $comment =~ m!\(`?(\w+)`?\)\sREFER\s`?\w+/(\w+)`?\(`?\w+`?\)!g ) {
+                eval { $self->_has_a_many( $table, $1, $2 ) };
+                warn qq/\# has_a_many failed "$@"\n\n/ if $@ && $self->debug;
+            }
+            $sth->finish;
+        }
     }
 }
 


------=_Part_5647_6517492.1137672110145
Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: inline

_______________________________________________
ClassDBI mailing list
ClassDBI@xxxxx.xxxxxxxxxxxxxxxx.xxx
http://lists.digitalcraftsmen.net/mailman/listinfo/classdbi

------=_Part_5647_6517492.1137672110145--

[CDBI] CDBI::Loader and MySQL 5
Frank Wiegand 12:01 on 19 Jan 2006

Re: [CDBI] CDBI::Loader and MySQL 5
Perrin Harkins 16:43 on 20 Jan 2006

Re: [CDBI] CDBI::Loader and MySQL 5
Matt S Trout 17:24 on 20 Jan 2006

Generated at 09:31 on 23 Jan 2006 by mariachi v0.52