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

Fletch on 2004-05-05T20:12:10

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;
 }