From: Robert Buels Date: Fri, 4 Sep 2009 18:35:10 +0000 (+0000) Subject: added code to use DBD::Pg column_info to fetch column default if recent enough X-Git-Tag: v0.08111~14^2~2 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=777f75276c79ae7d418ae1e75ab654aebb8e530a;p=dbsrgits%2FDBIx-Class.git added code to use DBD::Pg column_info to fetch column default if recent enough --- diff --git a/lib/DBIx/Class/Storage/DBI/Pg.pm b/lib/DBIx/Class/Storage/DBI/Pg.pm index d13f48a..8e024f0 100644 --- a/lib/DBIx/Class/Storage/DBI/Pg.pm +++ b/lib/DBIx/Class/Storage/DBI/Pg.pm @@ -54,6 +54,32 @@ sub _dbh_get_autoinc_seq { ( $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,25 +122,9 @@ $where 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';