1 package DBIx::DBSchema::DBD::Pg;
4 use vars qw($VERSION @ISA %typemap);
5 use DBIx::DBSchema::DBD;
8 @ISA = qw(DBIx::DBSchema::DBD);
12 'LONG VARBINARY' => 'BYTEA',
17 DBIx::DBSchema::DBD::Pg - PostgreSQL native driver for DBIx::DBSchema
24 $dbh = DBI->connect('dbi:Pg:dbname=database', 'user', 'pass');
25 $schema = new_native DBIx::DBSchema $dbh;
29 This module implements a PostgreSQL-native driver for DBIx::DBSchema.
34 my($proto, $dbh, $table) = @_;
35 my $sth = $dbh->prepare(<<END) or die $dbh->errstr;
36 SELECT a.attname, t.typname, a.attlen, a.atttypmod, a.attnotnull,
38 FROM pg_class c, pg_attribute a, pg_type t
39 WHERE c.relname = '$table'
40 AND a.attnum > 0 AND a.attrelid = c.oid AND a.atttypid = t.oid
43 $sth->execute or die $sth->errstr;
48 if ( $_->{atthasdef} ) {
49 my $attnum = $_->{attnum};
50 my $d_sth = $dbh->prepare(<<END) or die $dbh->errstr;
51 SELECT substring(d.adsrc for 128) FROM pg_attrdef d, pg_class c
52 WHERE c.relname = '$table' AND c.oid = d.adrelid AND d.adnum = $attnum
54 $d_sth->execute or die $d_sth->errstr;
56 $default = $d_sth->fetchrow_arrayref->[0];
60 if ( $_->{attlen} == -1 && $_->{typname} ne 'text' ) {
61 $len = $_->{atttypmod} - 4;
62 if ( $_->{typname} eq 'numeric' ) {
63 $len = ($len >> 16). ','. ($len & 0xffff);
67 my $type = $_->{'typname'};
68 $type = 'char' if $type eq 'bpchar';
79 } @{ $sth->fetchall_arrayref({}) };
83 my($proto, $dbh, $table) = @_;
84 my $sth = $dbh->prepare(<<END) or die $dbh->errstr;
85 SELECT a.attname, a.attnum
86 FROM pg_class c, pg_attribute a, pg_type t
87 WHERE c.relname = '${table}_pkey'
88 AND a.attnum > 0 AND a.attrelid = c.oid AND a.atttypid = t.oid
90 $sth->execute or die $sth->errstr;
91 my $row = $sth->fetchrow_hashref or return '';
96 my($proto, $dbh, $table) = @_;
97 my $gratuitous = { map { $_ => [ $proto->_index_fields($dbh, $_ ) ] }
98 grep { $proto->_is_unique($dbh, $_ ) }
99 $proto->_all_indices($dbh, $table)
104 my($proto, $dbh, $table) = @_;
105 my $gratuitous = { map { $_ => [ $proto->_index_fields($dbh, $_ ) ] }
106 grep { ! $proto->_is_unique($dbh, $_ ) }
107 $proto->_all_indices($dbh, $table)
112 my($proto, $dbh, $table) = @_;
113 my $sth = $dbh->prepare(<<END) or die $dbh->errstr;
115 FROM pg_class c, pg_class c2, pg_index i
116 WHERE c.relname = '$table' AND c.oid = i.indrelid AND i.indexrelid = c2.oid
118 $sth->execute or die $sth->errstr;
119 map { $_->{'relname'} }
120 grep { $_->{'relname'} !~ /_pkey$/ }
121 @{ $sth->fetchall_arrayref({}) };
125 my($proto, $dbh, $index) = @_;
126 my $sth = $dbh->prepare(<<END) or die $dbh->errstr;
127 SELECT a.attname, a.attnum
128 FROM pg_class c, pg_attribute a, pg_type t
129 WHERE c.relname = '$index'
130 AND a.attnum > 0 AND a.attrelid = c.oid AND a.atttypid = t.oid
132 $sth->execute or die $sth->errstr;
133 map { $_->{'attname'} } @{ $sth->fetchall_arrayref({}) };
137 my($proto, $dbh, $index) = @_;
138 my $sth = $dbh->prepare(<<END) or die $dbh->errstr;
140 FROM pg_index i, pg_class c, pg_am a
141 WHERE i.indexrelid = c.oid AND c.relname = '$index' AND c.relam = a.oid
143 $sth->execute or die $sth->errstr;
144 my $row = $sth->fetchrow_hashref or die 'guru meditation #420';
145 $row->{'indisunique'};
150 Ivan Kohler <ivan-dbix-dbschema@420.am>
154 Copyright (c) 2000 Ivan Kohler
155 Copyright (c) 2000 Mail Abuse Prevention System LLC
157 This program is free software; you can redistribute it and/or modify it under
158 the same terms as Perl itself.
164 columns doesn't return column default information.
168 L<DBIx::DBSchema>, L<DBIx::DBSchema::DBD>, L<DBI>, L<DBI::DBD>