1 package DBIx::DBSchema::DBD::Pg;
4 use vars qw($VERSION @ISA %typemap);
6 die "DBD::Pg version 1.32 or 1.41 (or later) required--".
7 "this is only version $DBD::Pg::VERSION\n"
8 if $DBD::Pg::VERSION != 1.32 && $DBD::Pg::VERSION < 1.41;
9 use DBIx::DBSchema::DBD;
12 @ISA = qw(DBIx::DBSchema::DBD);
16 'LONG VARBINARY' => 'BYTEA',
21 DBIx::DBSchema::DBD::Pg - PostgreSQL native driver for DBIx::DBSchema
28 $dbh = DBI->connect('dbi:Pg:dbname=database', 'user', 'pass');
29 $schema = new_native DBIx::DBSchema $dbh;
33 This module implements a PostgreSQL-native driver for DBIx::DBSchema.
38 my($proto, $dbh, $table) = @_;
39 my $sth = $dbh->prepare(<<END) or die $dbh->errstr;
40 SELECT a.attname, t.typname, a.attlen, a.atttypmod, a.attnotnull,
42 FROM pg_class c, pg_attribute a, pg_type t
43 WHERE c.relname = '$table'
44 AND a.attnum > 0 AND a.attrelid = c.oid AND a.atttypid = t.oid
47 $sth->execute or die $sth->errstr;
52 if ( $_->{atthasdef} ) {
53 my $attnum = $_->{attnum};
54 my $d_sth = $dbh->prepare(<<END) or die $dbh->errstr;
55 SELECT substring(d.adsrc for 128) FROM pg_attrdef d, pg_class c
56 WHERE c.relname = '$table' AND c.oid = d.adrelid AND d.adnum = $attnum
58 $d_sth->execute or die $d_sth->errstr;
60 $default = $d_sth->fetchrow_arrayref->[0];
64 if ( $_->{attlen} == -1 && $_->{atttypmod} != -1
65 && $_->{typname} ne 'text' ) {
66 $len = $_->{atttypmod} - 4;
67 if ( $_->{typname} eq 'numeric' ) {
68 $len = ($len >> 16). ','. ($len & 0xffff);
72 my $type = $_->{'typname'};
73 $type = 'char' if $type eq 'bpchar';
84 } @{ $sth->fetchall_arrayref({}) };
88 my($proto, $dbh, $table) = @_;
89 my $sth = $dbh->prepare(<<END) or die $dbh->errstr;
90 SELECT a.attname, a.attnum
91 FROM pg_class c, pg_attribute a, pg_type t
92 WHERE c.relname = '${table}_pkey'
93 AND a.attnum > 0 AND a.attrelid = c.oid AND a.atttypid = t.oid
95 $sth->execute or die $sth->errstr;
96 my $row = $sth->fetchrow_hashref or return '';
101 my($proto, $dbh, $table) = @_;
102 my $gratuitous = { map { $_ => [ $proto->_index_fields($dbh, $_ ) ] }
103 grep { $proto->_is_unique($dbh, $_ ) }
104 $proto->_all_indices($dbh, $table)
109 my($proto, $dbh, $table) = @_;
110 my $gratuitous = { map { $_ => [ $proto->_index_fields($dbh, $_ ) ] }
111 grep { ! $proto->_is_unique($dbh, $_ ) }
112 $proto->_all_indices($dbh, $table)
117 my($proto, $dbh, $table) = @_;
118 my $sth = $dbh->prepare(<<END) or die $dbh->errstr;
120 FROM pg_class c, pg_class c2, pg_index i
121 WHERE c.relname = '$table' AND c.oid = i.indrelid AND i.indexrelid = c2.oid
123 $sth->execute or die $sth->errstr;
124 map { $_->{'relname'} }
125 grep { $_->{'relname'} !~ /_pkey$/ }
126 @{ $sth->fetchall_arrayref({}) };
130 my($proto, $dbh, $index) = @_;
131 my $sth = $dbh->prepare(<<END) or die $dbh->errstr;
132 SELECT a.attname, a.attnum
133 FROM pg_class c, pg_attribute a, pg_type t
134 WHERE c.relname = '$index'
135 AND a.attnum > 0 AND a.attrelid = c.oid AND a.atttypid = t.oid
137 $sth->execute or die $sth->errstr;
138 map { $_->{'attname'} } @{ $sth->fetchall_arrayref({}) };
142 my($proto, $dbh, $index) = @_;
143 my $sth = $dbh->prepare(<<END) or die $dbh->errstr;
145 FROM pg_index i, pg_class c, pg_am a
146 WHERE i.indexrelid = c.oid AND c.relname = '$index' AND c.relam = a.oid
148 $sth->execute or die $sth->errstr;
149 my $row = $sth->fetchrow_hashref or die 'guru meditation #420';
150 $row->{'indisunique'};
155 Ivan Kohler <ivan-dbix-dbschema@420.am>
159 Copyright (c) 2000 Ivan Kohler
160 Copyright (c) 2000 Mail Abuse Prevention System LLC
162 This program is free software; you can redistribute it and/or modify it under
163 the same terms as Perl itself.
169 columns doesn't return column default information.
173 L<DBIx::DBSchema>, L<DBIx::DBSchema::DBD>, L<DBI>, L<DBI::DBD>