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 && $_->{atttypmod} != -1
61 && $_->{typname} ne 'text' ) {
62 $len = $_->{atttypmod} - 4;
63 if ( $_->{typname} eq 'numeric' ) {
64 $len = ($len >> 16). ','. ($len & 0xffff);
68 my $type = $_->{'typname'};
69 $type = 'char' if $type eq 'bpchar';
80 } @{ $sth->fetchall_arrayref({}) };
84 my($proto, $dbh, $table) = @_;
85 my $sth = $dbh->prepare(<<END) or die $dbh->errstr;
86 SELECT a.attname, a.attnum
87 FROM pg_class c, pg_attribute a, pg_type t
88 WHERE c.relname = '${table}_pkey'
89 AND a.attnum > 0 AND a.attrelid = c.oid AND a.atttypid = t.oid
91 $sth->execute or die $sth->errstr;
92 my $row = $sth->fetchrow_hashref or return '';
97 my($proto, $dbh, $table) = @_;
98 my $gratuitous = { map { $_ => [ $proto->_index_fields($dbh, $_ ) ] }
99 grep { $proto->_is_unique($dbh, $_ ) }
100 $proto->_all_indices($dbh, $table)
105 my($proto, $dbh, $table) = @_;
106 my $gratuitous = { map { $_ => [ $proto->_index_fields($dbh, $_ ) ] }
107 grep { ! $proto->_is_unique($dbh, $_ ) }
108 $proto->_all_indices($dbh, $table)
113 my($proto, $dbh, $table) = @_;
114 my $sth = $dbh->prepare(<<END) or die $dbh->errstr;
116 FROM pg_class c, pg_class c2, pg_index i
117 WHERE c.relname = '$table' AND c.oid = i.indrelid AND i.indexrelid = c2.oid
119 $sth->execute or die $sth->errstr;
120 map { $_->{'relname'} }
121 grep { $_->{'relname'} !~ /_pkey$/ }
122 @{ $sth->fetchall_arrayref({}) };
126 my($proto, $dbh, $index) = @_;
127 my $sth = $dbh->prepare(<<END) or die $dbh->errstr;
128 SELECT a.attname, a.attnum
129 FROM pg_class c, pg_attribute a, pg_type t
130 WHERE c.relname = '$index'
131 AND a.attnum > 0 AND a.attrelid = c.oid AND a.atttypid = t.oid
133 $sth->execute or die $sth->errstr;
134 map { $_->{'attname'} } @{ $sth->fetchall_arrayref({}) };
138 my($proto, $dbh, $index) = @_;
139 my $sth = $dbh->prepare(<<END) or die $dbh->errstr;
141 FROM pg_index i, pg_class c, pg_am a
142 WHERE i.indexrelid = c.oid AND c.relname = '$index' AND c.relam = a.oid
144 $sth->execute or die $sth->errstr;
145 my $row = $sth->fetchrow_hashref or die 'guru meditation #420';
146 $row->{'indisunique'};
151 Ivan Kohler <ivan-dbix-dbschema@420.am>
155 Copyright (c) 2000 Ivan Kohler
156 Copyright (c) 2000 Mail Abuse Prevention System LLC
158 This program is free software; you can redistribute it and/or modify it under
159 the same terms as Perl itself.
165 columns doesn't return column default information.
169 L<DBIx::DBSchema>, L<DBIx::DBSchema::DBD>, L<DBI>, L<DBI::DBD>