use strict;
use vars qw($VERSION @ISA %typemap);
+use DBD::Pg 1.32;
use DBIx::DBSchema::DBD;
-$VERSION = '0.04';
+$VERSION = '0.11';
@ISA = qw(DBIx::DBSchema::DBD);
+die "DBD::Pg version 1.32 or 1.41 (or later) required--".
+ "this is only version $DBD::Pg::VERSION\n"
+ if $DBD::Pg::VERSION != 1.32 && $DBD::Pg::VERSION < 1.41;
+
%typemap = (
'BLOB' => 'BYTEA',
'LONG VARBINARY' => 'BYTEA',
=cut
+sub default_db_schema { 'public'; }
+
sub columns {
my($proto, $dbh, $table) = @_;
my $sth = $dbh->prepare(<<END) or die $dbh->errstr;
- SELECT a.attname, t.typname, a.attlen, a.atttypmod, a.attnotnull
+ SELECT a.attname, t.typname, a.attlen, a.atttypmod, a.attnotnull,
+ a.atthasdef, a.attnum
FROM pg_class c, pg_attribute a, pg_type t
WHERE c.relname = '$table'
AND a.attnum > 0 AND a.attrelid = c.oid AND a.atttypid = t.oid
ORDER BY a.attnum
END
$sth->execute or die $sth->errstr;
+
map {
+ my $default = '';
+ if ( $_->{atthasdef} ) {
+ my $attnum = $_->{attnum};
+ my $d_sth = $dbh->prepare(<<END) or die $dbh->errstr;
+ SELECT substring(d.adsrc for 128) FROM pg_attrdef d, pg_class c
+ WHERE c.relname = '$table' AND c.oid = d.adrelid AND d.adnum = $attnum
+END
+ $d_sth->execute or die $d_sth->errstr;
+
+ $default = $d_sth->fetchrow_arrayref->[0];
+ };
+
my $len = '';
- if ( $_->{attlen} == -1 && $_->{typname} ne 'text' ) {
+ if ( $_->{attlen} == -1 && $_->{atttypmod} != -1
+ && $_->{typname} ne 'text' ) {
$len = $_->{atttypmod} - 4;
if ( $_->{typname} eq 'numeric' ) {
$len = ($len >> 16). ','. ($len & 0xffff);
$type,
! $_->{'attnotnull'},
$len,
- '', #default
+ $default,
'' #local
];
FROM pg_class c, pg_attribute a, pg_type t
WHERE c.relname = '$index'
AND a.attnum > 0 AND a.attrelid = c.oid AND a.atttypid = t.oid
+ ORDER BY a.attnum
END
$sth->execute or die $sth->errstr;
map { $_->{'attname'} } @{ $sth->fetchall_arrayref({}) };