Jeff,
        Attached (included) is a patch and test to fix the
        table_attributes function to mark all the columns in the primary
        key instead of just the first column.

Tom

*** Pg.pm.orig  Fri May 31 22:11:21 2002
--- Pg.pm.tal   Fri May 31 20:45:12 2002
***************
*** 220,234 ****
      
          return $result unless scalar(@$attrs);
  
          # Get the primary key
!         my ($pri_key) = $dbh->selectrow_array("SELECT pg_attribute.attname
                                                 FROM   pg_class, pg_attribute, 
pg_index
                                                 WHERE  pg_class.oid          = 
pg_attribute.attrelid 
                                                 AND    pg_class.oid          = 
pg_index.indrelid 
!                                                AND    pg_index.indkey[0]    = 
pg_attribute.attnum
                                                 AND    pg_index.indisprimary = 't'
!                                                AND    pg_class.relname      = 
'$table'");
!         $pri_key = '' unless $pri_key;
  
          foreach my $attr (reverse @$attrs) {
              my ($col_name, $col_type, $size, $mod, $notnull, $hasdef, $attnum) = 
@$attr;
--- 220,250 ----
      
          return $result unless scalar(@$attrs);
  
+       # Select the array value for tables primary key.
+       my $pk_key_sql = qq{SELECT pg_index.indkey
+                             FROM   pg_class, pg_index
+                             WHERE
+                                    pg_class.oid          = pg_index.indrelid
+                             AND    pg_class.relname      = '$table'
+                             AND    pg_index.indisprimary = 't'
+                       };
+       # Expand this (returned as a string) a real array.
+       my @pk = split( /\s+/, $dbh->selectrow_array( $pk_key_sql ) || [] );
+       my $pk_bt = 
+               (@pk)   ? "AND    pg_attribute.attnum in (" . join ( ", ", @pk ) . ")"
+                       : "";
+               
          # Get the primary key
!         my $pri_key = $dbh->selectcol_arrayref("SELECT pg_attribute.attname
                                                 FROM   pg_class, pg_attribute, 
pg_index
                                                 WHERE  pg_class.oid          = 
pg_attribute.attrelid 
                                                 AND    pg_class.oid          = 
pg_index.indrelid 
!                                              $pk_bt
                                                 AND    pg_index.indisprimary = 't'
!                                                AND    pg_class.relname      = 
'$table'
!                                              order by pg_attribute.attnum
!                                              ");
!         $pri_key = [] unless $pri_key;
  
          foreach my $attr (reverse @$attrs) {
              my ($col_name, $col_type, $size, $mod, $notnull, $hasdef, $attnum) = 
@$attr;
***************
*** 256,262 ****
              $constraint = '' unless $constraint;
  
              # Check to see if this is the primary key
!             my $is_primary_key = (lc $pri_key eq lc $col_name) ? 1 : 0;
  
              push @$result,
                  { NAME        => $col_name,
--- 272,278 ----
              $constraint = '' unless $constraint;
  
              # Check to see if this is the primary key
!             my $is_primary_key = scalar(grep { /^$col_name$/i } @$pri_key) ? 1 : 0;
  
              push @$result,
                  { NAME        => $col_name,



-- 
Thomas A. Lowery
See DBI/FAQ http://xmlproj.dyndns.org/cgi-bin/fom
*** Pg.pm.orig  Fri May 31 22:11:21 2002
--- Pg.pm.tal   Fri May 31 20:45:12 2002
***************
*** 220,234 ****
      
          return $result unless scalar(@$attrs);
  
          # Get the primary key
!         my ($pri_key) = $dbh->selectrow_array("SELECT pg_attribute.attname
                                                 FROM   pg_class, pg_attribute, 
pg_index
                                                 WHERE  pg_class.oid          = 
pg_attribute.attrelid 
                                                 AND    pg_class.oid          = 
pg_index.indrelid 
!                                                AND    pg_index.indkey[0]    = 
pg_attribute.attnum
                                                 AND    pg_index.indisprimary = 't'
!                                                AND    pg_class.relname      = 
'$table'");
!         $pri_key = '' unless $pri_key;
  
          foreach my $attr (reverse @$attrs) {
              my ($col_name, $col_type, $size, $mod, $notnull, $hasdef, $attnum) = 
@$attr;
--- 220,250 ----
      
          return $result unless scalar(@$attrs);
  
+       # Select the array value for tables primary key.
+       my $pk_key_sql = qq{SELECT pg_index.indkey
+                             FROM   pg_class, pg_index
+                             WHERE
+                                    pg_class.oid          = pg_index.indrelid
+                             AND    pg_class.relname      = '$table'
+                             AND    pg_index.indisprimary = 't'
+                       };
+       # Expand this (returned as a string) a real array.
+       my @pk = split( /\s+/, $dbh->selectrow_array( $pk_key_sql ) || [] );
+       my $pk_bt = 
+               (@pk)   ? "AND    pg_attribute.attnum in (" . join ( ", ", @pk ) . ")"
+                       : "";
+               
          # Get the primary key
!         my $pri_key = $dbh->selectcol_arrayref("SELECT pg_attribute.attname
                                                 FROM   pg_class, pg_attribute, 
pg_index
                                                 WHERE  pg_class.oid          = 
pg_attribute.attrelid 
                                                 AND    pg_class.oid          = 
pg_index.indrelid 
!                                              $pk_bt
                                                 AND    pg_index.indisprimary = 't'
!                                                AND    pg_class.relname      = 
'$table'
!                                              order by pg_attribute.attnum
!                                              ");
!         $pri_key = [] unless $pri_key;
  
          foreach my $attr (reverse @$attrs) {
              my ($col_name, $col_type, $size, $mod, $notnull, $hasdef, $attnum) = 
@$attr;
***************
*** 256,262 ****
              $constraint = '' unless $constraint;
  
              # Check to see if this is the primary key
!             my $is_primary_key = (lc $pri_key eq lc $col_name) ? 1 : 0;
  
              push @$result,
                  { NAME        => $col_name,
--- 272,278 ----
              $constraint = '' unless $constraint;
  
              # Check to see if this is the primary key
!             my $is_primary_key = scalar(grep { /^$col_name$/i } @$pri_key) ? 1 : 0;
  
              push @$result,
                  { NAME        => $col_name,

Reply via email to