[Bast-commits] r7563 - DBIx-Class/0.08/branches/pg_unqualified_schema/lib/DBIx/Class/Storage/DBI

rbuels at dev.catalyst.perl.org rbuels at dev.catalyst.perl.org
Fri Sep 4 18:35:10 GMT 2009


Author: rbuels
Date: 2009-09-04 18:35:10 +0000 (Fri, 04 Sep 2009)
New Revision: 7563

Modified:
   DBIx-Class/0.08/branches/pg_unqualified_schema/lib/DBIx/Class/Storage/DBI/Pg.pm
Log:
added code to use DBD::Pg column_info to fetch column default if recent enough

Modified: DBIx-Class/0.08/branches/pg_unqualified_schema/lib/DBIx/Class/Storage/DBI/Pg.pm
===================================================================
--- DBIx-Class/0.08/branches/pg_unqualified_schema/lib/DBIx/Class/Storage/DBI/Pg.pm	2009-09-04 17:43:17 UTC (rev 7562)
+++ DBIx-Class/0.08/branches/pg_unqualified_schema/lib/DBIx/Class/Storage/DBI/Pg.pm	2009-09-04 18:35:10 UTC (rev 7563)
@@ -54,6 +54,32 @@
     ( $schema, $table ) = ( $1, $2 );
   }
 
+  my $seq_expr =  $DBD::Pg::VERSION > 2.015001
+      # use DBD::Pg to fetch the column info if it is recent enough to work
+      ? eval{ $dbh->column_info(undef,$schema,$table,$col)->fetchrow_hashref->{COLUMN_DEF} }
+      # otherwise, use a custom SQL method
+      : $self->_dbh_get_column_default( $dbh, $schema, $table, $col );
+
+  # if no default value is set on the column, or if we can't parse the
+  # default value as a sequence, throw.
+  unless ( defined $seq_expr and $seq_expr =~ /^nextval\(+'([^']+)'::(?:text|regclass)\)/i ){
+    $seq_expr = '' unless defined $seq_expr;
+    $schema = "$schema." if defined $schema && length $schema;
+    $self->throw_exception( "no sequence found for $schema$table.$col, check table definition, "
+                            . "or explicitly set the 'sequence' for this column in the "
+                            . $source->source_name
+                            . " class"
+                          );
+  }
+
+  return $1;
+}
+
+# custom method for fetching column default, since column_info has a
+# bug with older versions of DBD::Pg
+sub _dbh_get_column_default {
+  my ( $self, $dbh, $schema, $table, $col ) = @_;
+
   # Build and execute a query into the pg_catalog to find the Pg
   # expression for the default value for this column in this table.
   # If the table name is schema-qualified, query using that specific
@@ -96,26 +122,10 @@
 
 EOS
 
-  # if no default value is set on the column, or if we can't parse the
-  # default value as a sequence, throw.
-  unless ( defined $seq_expr and $seq_expr =~ /^nextval\(+'([^']+)'::(?:text|regclass)\)/i ){
-    $seq_expr = '' unless defined $seq_expr;
-    $schema = "$schema." if defined $schema && length $schema;
-    $self->throw_exception( "no sequence found for $schema$table.$col, check table definition, "
-                            . "or explicitly set the 'sequence' for this column in the "
-                            . $source->source_name
-                            . " class"
-                          );
-  }
-
-  return $1;
+  return $seq_expr;
 }
 
-sub get_autoinc_seq {
-  my ($self,$source,$col) = @_;
 
-}
-
 sub sqlt_type {
   return 'PostgreSQL';
 }




More information about the Bast-commits mailing list