1 package DBIx::DBSchema::DBD::Pg;
4 use vars qw($VERSION @ISA %typemap);
6 use DBIx::DBSchema::DBD;
9 @ISA = qw(DBIx::DBSchema::DBD);
13 'LONG VARBINARY' => 'BYTEA',
18 DBIx::DBSchema::DBD::Pg - PostgreSQL native driver for DBIx::DBSchema
25 $dbh = DBI->connect('dbi:Pg:dbname=database', 'user', 'pass');
26 $schema = new_native DBIx::DBSchema $dbh;
30 This module implements a PostgreSQL-native driver for DBIx::DBSchema.
35 my($proto, $dbh, $table) = @_;
36 my $sth = $dbh->prepare(<<END) or die $dbh->errstr;
37 SELECT a.attname, t.typname, a.attlen, a.atttypmod, a.attnotnull,
39 FROM pg_class c, pg_attribute a, pg_type t
40 WHERE c.relname = '$table'
41 AND a.attnum > 0 AND a.attrelid = c.oid AND a.atttypid = t.oid
44 $sth->execute or die $sth->errstr;
49 if ( $_->{atthasdef} ) {
50 my $attnum = $_->{attnum};
51 my $d_sth = $dbh->prepare(<<END) or die $dbh->errstr;
52 SELECT substring(d.adsrc for 128) FROM pg_attrdef d, pg_class c
53 WHERE c.relname = '$table' AND c.oid = d.adrelid AND d.adnum = $attnum
55 $d_sth->execute or die $d_sth->errstr;
57 $default = $d_sth->fetchrow_arrayref->[0];
61 if ( $_->{attlen} == -1 && $_->{atttypmod} != -1
62 && $_->{typname} ne 'text' ) {
63 $len = $_->{atttypmod} - 4;
64 if ( $_->{typname} eq 'numeric' ) {
65 $len = ($len >> 16). ','. ($len & 0xffff);
69 my $type = $_->{'typname'};
70 $type = 'char' if $type eq 'bpchar';
81 } @{ $sth->fetchall_arrayref({}) };
85 my($proto, $dbh, $table) = @_;
86 my $sth = $dbh->prepare(<<END) or die $dbh->errstr;
87 SELECT a.attname, a.attnum
88 FROM pg_class c, pg_attribute a, pg_type t
89 WHERE c.relname = '${table}_pkey'
90 AND a.attnum > 0 AND a.attrelid = c.oid AND a.atttypid = t.oid
92 $sth->execute or die $sth->errstr;
93 my $row = $sth->fetchrow_hashref or return '';
98 my($proto, $dbh, $table) = @_;
99 my $gratuitous = { map { $_ => [ $proto->_index_fields($dbh, $_ ) ] }
100 grep { $proto->_is_unique($dbh, $_ ) }
101 $proto->_all_indices($dbh, $table)
106 my($proto, $dbh, $table) = @_;
107 my $gratuitous = { map { $_ => [ $proto->_index_fields($dbh, $_ ) ] }
108 grep { ! $proto->_is_unique($dbh, $_ ) }
109 $proto->_all_indices($dbh, $table)
114 my($proto, $dbh, $table) = @_;
115 my $sth = $dbh->prepare(<<END) or die $dbh->errstr;
117 FROM pg_class c, pg_class c2, pg_index i
118 WHERE c.relname = '$table' AND c.oid = i.indrelid AND i.indexrelid = c2.oid
120 $sth->execute or die $sth->errstr;
121 map { $_->{'relname'} }
122 grep { $_->{'relname'} !~ /_pkey$/ }
123 @{ $sth->fetchall_arrayref({}) };
127 my($proto, $dbh, $index) = @_;
128 my $sth = $dbh->prepare(<<END) or die $dbh->errstr;
129 SELECT a.attname, a.attnum
130 FROM pg_class c, pg_attribute a, pg_type t
131 WHERE c.relname = '$index'
132 AND a.attnum > 0 AND a.attrelid = c.oid AND a.atttypid = t.oid
134 $sth->execute or die $sth->errstr;
135 map { $_->{'attname'} } @{ $sth->fetchall_arrayref({}) };
139 my($proto, $dbh, $index) = @_;
140 my $sth = $dbh->prepare(<<END) or die $dbh->errstr;
142 FROM pg_index i, pg_class c, pg_am a
143 WHERE i.indexrelid = c.oid AND c.relname = '$index' AND c.relam = a.oid
145 $sth->execute or die $sth->errstr;
146 my $row = $sth->fetchrow_hashref or die 'guru meditation #420';
147 $row->{'indisunique'};
152 Ivan Kohler <ivan-dbix-dbschema@420.am>
156 Copyright (c) 2000 Ivan Kohler
157 Copyright (c) 2000 Mail Abuse Prevention System LLC
159 This program is free software; you can redistribute it and/or modify it under
160 the same terms as Perl itself.
166 columns doesn't return column default information.
170 L<DBIx::DBSchema>, L<DBIx::DBSchema::DBD>, L<DBI>, L<DBI::DBD>