1 package DBIx::DBSchema::DBD::Pg;
4 use vars qw($VERSION @ISA %typemap);
6 use DBIx::DBSchema::DBD;
9 @ISA = qw(DBIx::DBSchema::DBD);
11 die "DBD::Pg version 1.32 or 1.41 (or later) required--".
12 "this is only version $DBD::Pg::VERSION\n"
13 if $DBD::Pg::VERSION != 1.32 && $DBD::Pg::VERSION < 1.41;
17 'LONG VARBINARY' => 'BYTEA',
22 DBIx::DBSchema::DBD::Pg - PostgreSQL native driver for DBIx::DBSchema
29 $dbh = DBI->connect('dbi:Pg:dbname=database', 'user', 'pass');
30 $schema = new_native DBIx::DBSchema $dbh;
34 This module implements a PostgreSQL-native driver for DBIx::DBSchema.
38 sub default_db_schema { 'public'; }
41 my($proto, $dbh, $table) = @_;
42 my $sth = $dbh->prepare(<<END) or die $dbh->errstr;
43 SELECT a.attname, t.typname, a.attlen, a.atttypmod, a.attnotnull,
45 FROM pg_class c, pg_attribute a, pg_type t
46 WHERE c.relname = '$table'
47 AND a.attnum > 0 AND a.attrelid = c.oid AND a.atttypid = t.oid
50 $sth->execute or die $sth->errstr;
55 if ( $_->{atthasdef} ) {
56 my $attnum = $_->{attnum};
57 my $d_sth = $dbh->prepare(<<END) or die $dbh->errstr;
58 SELECT substring(d.adsrc for 128) FROM pg_attrdef d, pg_class c
59 WHERE c.relname = '$table' AND c.oid = d.adrelid AND d.adnum = $attnum
61 $d_sth->execute or die $d_sth->errstr;
63 $default = $d_sth->fetchrow_arrayref->[0];
67 if ( $_->{attlen} == -1 && $_->{atttypmod} != -1
68 && $_->{typname} ne 'text' ) {
69 $len = $_->{atttypmod} - 4;
70 if ( $_->{typname} eq 'numeric' ) {
71 $len = ($len >> 16). ','. ($len & 0xffff);
75 my $type = $_->{'typname'};
76 $type = 'char' if $type eq 'bpchar';
87 } @{ $sth->fetchall_arrayref({}) };
91 my($proto, $dbh, $table) = @_;
92 my $sth = $dbh->prepare(<<END) or die $dbh->errstr;
93 SELECT a.attname, a.attnum
94 FROM pg_class c, pg_attribute a, pg_type t
95 WHERE c.relname = '${table}_pkey'
96 AND a.attnum > 0 AND a.attrelid = c.oid AND a.atttypid = t.oid
98 $sth->execute or die $sth->errstr;
99 my $row = $sth->fetchrow_hashref or return '';
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 $gratuitous = { map { $_ => [ $proto->_index_fields($dbh, $_ ) ] }
114 grep { ! $proto->_is_unique($dbh, $_ ) }
115 $proto->_all_indices($dbh, $table)
120 my($proto, $dbh, $table) = @_;
121 my $sth = $dbh->prepare(<<END) or die $dbh->errstr;
123 FROM pg_class c, pg_class c2, pg_index i
124 WHERE c.relname = '$table' AND c.oid = i.indrelid AND i.indexrelid = c2.oid
126 $sth->execute or die $sth->errstr;
127 map { $_->{'relname'} }
128 grep { $_->{'relname'} !~ /_pkey$/ }
129 @{ $sth->fetchall_arrayref({}) };
133 my($proto, $dbh, $index) = @_;
134 my $sth = $dbh->prepare(<<END) or die $dbh->errstr;
135 SELECT a.attname, a.attnum
136 FROM pg_class c, pg_attribute a, pg_type t
137 WHERE c.relname = '$index'
138 AND a.attnum > 0 AND a.attrelid = c.oid AND a.atttypid = t.oid
141 $sth->execute or die $sth->errstr;
142 map { $_->{'attname'} } @{ $sth->fetchall_arrayref({}) };
146 my($proto, $dbh, $index) = @_;
147 my $sth = $dbh->prepare(<<END) or die $dbh->errstr;
149 FROM pg_index i, pg_class c, pg_am a
150 WHERE i.indexrelid = c.oid AND c.relname = '$index' AND c.relam = a.oid
152 $sth->execute or die $sth->errstr;
153 my $row = $sth->fetchrow_hashref or die 'guru meditation #420';
154 $row->{'indisunique'};
159 Ivan Kohler <ivan-dbix-dbschema@420.am>
163 Copyright (c) 2000 Ivan Kohler
164 Copyright (c) 2000 Mail Abuse Prevention System LLC
166 This program is free software; you can redistribute it and/or modify it under
167 the same terms as Perl itself.
173 columns doesn't return column default information.
177 L<DBIx::DBSchema>, L<DBIx::DBSchema::DBD>, L<DBI>, L<DBI::DBD>