Author: turnstep
Date: Sun Mar 23 09:42:29 2008
New Revision: 10987
Modified:
DBD-Pg/trunk/Changes
DBD-Pg/trunk/Pg.pm
DBD-Pg/trunk/t/03dbmethod.t
Log:
Add pg_enum_values to $dbh->column_info(), by Dave Rolsky. CPAN ticket 34351.
Modified: DBD-Pg/trunk/Changes
==============================================================================
--- DBD-Pg/trunk/Changes (original)
+++ DBD-Pg/trunk/Changes Sun Mar 23 09:42:29 2008
@@ -1,5 +1,11 @@
('GSM' is Greg Sabino Mullane, [EMAIL PROTECTED])
+2.5.0 Released ??
+
+ - Add pg_enum_values to $dbh->column_info()
+ [Dave Rolsky] (CPAN ticket #34351)
+ - Minor test fixes.
+
2.4.0 Released March 21, 2008 (subversion r10974)
- Remove problematic and unneeded Test::Warn test from 00basic.t.
Modified: DBD-Pg/trunk/Pg.pm
==============================================================================
--- DBD-Pg/trunk/Pg.pm (original)
+++ DBD-Pg/trunk/Pg.pm Sun Mar 23 09:42:29 2008
@@ -443,6 +443,8 @@
, a.attrelid AS "pg_attrelid"
, a.attnum AS "pg_attnum"
, a.atttypmod AS "pg_atttypmod"
+ , t.typtype AS "_pg_type_typtype"
+ , t.oid AS "_pg_type_oid"
FROM
pg_catalog.pg_type t
JOIN pg_catalog.pg_attribute a ON (t.oid =
a.atttypid)
@@ -485,11 +487,14 @@
pg_schema 20
pg_table 21
pg_column 22
+ pg_enum_values 23
/);
my $oldconstraint_sth;
for my $row (@$data) {
+ my $typoid = pop @$row;
+ my $typtype = pop @$row;
my $typmod = pop @$row;
my $attnum = pop @$row;
my $aid = pop @$row;
@@ -498,9 +503,7 @@
_calc_col_size($typmod,$row->[$col_map{COLUMN_SIZE}]);
# Replace the Pg type with the SQL_ type
- my $w = $row->[$col_map{DATA_TYPE}];
$row->[$col_map{DATA_TYPE}] =
DBD::Pg::db::pg_type_info($dbh,$row->[$col_map{DATA_TYPE}]);
- $w = $row->[$col_map{DATA_TYPE}];
# Add pg_constraint
my $SQL = "SELECT consrc FROM pg_catalog.pg_constraint
WHERE contype = 'c' AND ".
@@ -512,11 +515,16 @@
else {
$row->[19] = undef;
}
- $col_map{pg_constraint} = 19;
- }
- # get rid of atttypmod that we no longer need
- delete $col_map{pg_atttypmod};
+ if ( $typtype eq 'e' ) {
+ my $SQL = "SELECT enumlabel FROM
pg_catalog.pg_enum "
+ ."WHERE enumtypid = $typoid ORDER BY
oid";
+ $row->[23] = $dbh->selectcol_arrayref($SQL);
+ }
+ else {
+ $row->[23] = undef;
+ }
+ }
# Since we've processed the data in Perl, we have to jump
through a hoop
# To turn it back into a statement handle
@@ -2620,13 +2628,14 @@
SQL_DATETIME_SUB
CHAR_OCTET_LENGTH
-Also, five additional non-standard fields are returned:
+Also, six additional non-standard fields are returned:
pg_type - data type with additional info i.e. "character varying(20)"
pg_constraint - holds column constraint definition
pg_schema - the unquoted name of the schema
pg_table - the unquoted name of the table
pg_column - the unquoted name of the column
+ pg_enum_values - an array reference of allowed values for an enum column
Note that the TABLE_SCHEM, TABLE_NAME, and COLUMN_NAME fields all return
output wrapped in quote_ident(). If you need the unquoted version, use
Modified: DBD-Pg/trunk/t/03dbmethod.t
==============================================================================
--- DBD-Pg/trunk/t/03dbmethod.t (original)
+++ DBD-Pg/trunk/t/03dbmethod.t Sun Mar 23 09:42:29 2008
@@ -25,7 +25,7 @@
if (! defined $dbh) {
plan skip_all => 'Connection to database failed, cannot continue
testing';
}
-plan tests => 216;
+plan tests => 218;
isnt( $dbh, undef, 'Connect to database for database handle method testing');
@@ -490,6 +490,28 @@
$t = q{DB handle method "column_info" works with non-lowercased columns};
is( $result->{COLUMN_NAME}, q{"CaseTest"}, $t);
+SKIP: {
+
+ skip 'DB handle method column_info attribute "pg_enum_values" requires at
least Postgres 8.3', 2
+ unless $dbh->{pg_server_version} >= 80300;
+
+ {
+ local $dbh->{Warn} = 0;
+
+ $dbh->do( q{CREATE TYPE dbd_pg_enumerated AS ENUM ('foo', 'bar',
'baz', 'buz')} );
+ $dbh->do( q{CREATE TEMP TABLE dbd_pg_enum_test ( is_enum
dbd_pg_enumerated NOT NULL )} );
+ }
+
+ $sth = $dbh->column_info('','','dbd_pg_enum_test','is_enum');
+ $result = $sth->fetchall_arrayref({})->[0];
+ $t = q{DB handle method "column_info" returns proper pg_type};
+ is( $result->{pg_type}, 'dbd_pg_enumerated', $t);
+ $t = q{'DB handle method "column_info" returns proper pg_enum_values'};
+ is_deeply( $result->{pg_enum_values}, [ qw( foo bar baz buz ) ], $t);
+
+ $dbh->do("DROP TABLE dbd_pg_enum_test");
+ $dbh->do("DROP TYPE dbd_pg_enumerated");
+}
#
# Test of the "primary_key_info" database handle method