Stories
Slash Boxes
Comments
NOTE: use Perl; is on undef hiatus. You can read content, but you can't post it. More info will be forthcoming forthcomingly.

All the Perl that's Practical to Extract and Report

use Perl Log In

Log In

[ Create a new account ]

Fletch (75)

Fletch
  (email not shown publicly)
http://phydeaux.org/
AOL IM: lemurnomicon (Add Buddy, Send Message)

Frink? Frink? Phtang!

Journal of Fletch (75)

Wednesday May 05, 2004
03:12 PM

Class::DBI::Pg and multi-column primary keys

[ #18634 ]

This will let Class::DBI::Pg deal with tables which have a primary key consisting of more than one column (e.g. CREATE TABLE foo ( a integer, b integer, PRIMARY KEY ( a, b ) ))

Patch sent to author as well, but just in case anyone else is interested . . .

--- /usr/lib/perl5/site_perl/5.8.0/Class/DBI/Pg.pm      2003-09-10 03:59:40.000000000 -0400
+++ Class/DBI/Pg.pm     2004-05-05 13:53:57.000000000 -0400
@@ -23,7 +23,7 @@
WHERE relname = ?)
SQL
     $sth->execute($table);
-    my $prinum = $sth->fetchrow_array;
+    my @prinums = split( /\s+/, ( $sth->fetchrow_array )[0] );
     $sth->finish;

     # find all columns
@@ -50,17 +50,17 @@
     $sth->finish;
     my($sequence) = $nextval_str =~ m/^nextval\('"?([^"']+)"?'::text\)/;

-    my(@cols, $primary);
+    my(@cols, @primary);
     foreach my $col(@$columns) {
        # skip dropped column.
        next if $col->[0] =~ /^\.+pg\.dropped\.\d+\.+$/;
        push @cols, $col->[0];
-       next unless $prinum && $col->[1] eq $prinum;
-       $primary = $col->[0];
+       next unless @prinums && grep $col->[1] eq $_, @prinums;
+       push @primary, $col->[0];
     }
-    _croak("$table has no primary key") unless $primary;
+    _croak("$table has no primary key") unless @primary;
     $class->table($table);
-    $class->columns(Primary => $primary);
+    $class->columns(Primary => @primary);
     $class->columns(All => @cols);
     $class->sequence($sequence) if $sequence;
}

The Fine Print: The following comments are owned by whoever posted them. We are not responsible for them in any way.
 Full
 Abbreviated
 Hidden
More | Login | Reply
Loading... please wait.