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',
18 'TIMESTAMP' => 'TIMESTAMP WITH TIME ZONE',
23 DBIx::DBSchema::DBD::Pg - PostgreSQL native driver for DBIx::DBSchema
30 $dbh = DBI->connect('dbi:Pg:dbname=database', 'user', 'pass');
31 $schema = new_native DBIx::DBSchema $dbh;
35 This module implements a PostgreSQL-native driver for DBIx::DBSchema.
39 sub default_db_schema { 'public'; }
42 my($proto, $dbh, $table) = @_;
43 my $sth = $dbh->prepare(<<END) or die $dbh->errstr;
44 SELECT a.attname, t.typname, a.attlen, a.atttypmod, a.attnotnull,
46 FROM pg_class c, pg_attribute a, pg_type t
47 WHERE c.relname = '$table'
48 AND a.attnum > 0 AND a.attrelid = c.oid AND a.atttypid = t.oid
51 $sth->execute or die $sth->errstr;
55 my $type = $_->{'typname'};
56 $type = 'char' if $type eq 'bpchar';
59 if ( $_->{attlen} == -1 && $_->{atttypmod} != -1
60 && $_->{typname} ne 'text' ) {
61 $len = $_->{atttypmod} - 4;
62 if ( $_->{typname} eq 'numeric' ) {
63 $len = ($len >> 16). ','. ($len & 0xffff);
68 if ( $_->{atthasdef} ) {
69 my $attnum = $_->{attnum};
70 my $d_sth = $dbh->prepare(<<END) or die $dbh->errstr;
71 SELECT substring(d.adsrc for 128) FROM pg_attrdef d, pg_class c
72 WHERE c.relname = '$table' AND c.oid = d.adrelid AND d.adnum = $attnum
74 $d_sth->execute or die $d_sth->errstr;
76 $default = $d_sth->fetchrow_arrayref->[0];
78 if ( _type_needs_quoting($type) ) {
79 $default =~ s/::([\w ]+)$//; #save typecast info?
80 if ( $default =~ /^'(.*)'$/ ) {
82 $default = \"''" if $default eq '';
87 } elsif ( $default =~ /^[a-z]/i ) { #sloppy, but it'll do
102 } @{ $sth->fetchall_arrayref({}) };
106 my($proto, $dbh, $table) = @_;
107 my $sth = $dbh->prepare(<<END) or die $dbh->errstr;
108 SELECT a.attname, a.attnum
109 FROM pg_class c, pg_attribute a, pg_type t
110 WHERE c.relname = '${table}_pkey'
111 AND a.attnum > 0 AND a.attrelid = c.oid AND a.atttypid = t.oid
113 $sth->execute or die $sth->errstr;
114 my $row = $sth->fetchrow_hashref or return '';
119 my($proto, $dbh, $table) = @_;
120 my $gratuitous = { map { $_ => [ $proto->_index_fields($dbh, $_ ) ] }
121 grep { $proto->_is_unique($dbh, $_ ) }
122 $proto->_all_indices($dbh, $table)
127 my($proto, $dbh, $table) = @_;
128 my $gratuitous = { map { $_ => [ $proto->_index_fields($dbh, $_ ) ] }
129 grep { ! $proto->_is_unique($dbh, $_ ) }
130 $proto->_all_indices($dbh, $table)
135 my($proto, $dbh, $table) = @_;
136 my $sth = $dbh->prepare(<<END) or die $dbh->errstr;
138 FROM pg_class c, pg_class c2, pg_index i
139 WHERE c.relname = '$table' AND c.oid = i.indrelid AND i.indexrelid = c2.oid
141 $sth->execute or die $sth->errstr;
142 map { $_->{'relname'} }
143 grep { $_->{'relname'} !~ /_pkey$/ }
144 @{ $sth->fetchall_arrayref({}) };
148 my($proto, $dbh, $index) = @_;
149 my $sth = $dbh->prepare(<<END) or die $dbh->errstr;
150 SELECT a.attname, a.attnum
151 FROM pg_class c, pg_attribute a, pg_type t
152 WHERE c.relname = '$index'
153 AND a.attnum > 0 AND a.attrelid = c.oid AND a.atttypid = t.oid
156 $sth->execute or die $sth->errstr;
157 map { $_->{'attname'} } @{ $sth->fetchall_arrayref({}) };
161 my($proto, $dbh, $index) = @_;
162 my $sth = $dbh->prepare(<<END) or die $dbh->errstr;
164 FROM pg_index i, pg_class c, pg_am a
165 WHERE i.indexrelid = c.oid AND c.relname = '$index' AND c.relam = a.oid
167 $sth->execute or die $sth->errstr;
168 my $row = $sth->fetchrow_hashref or die 'guru meditation #420';
169 $row->{'indisunique'};
172 sub add_column_callback {
173 my( $proto, $dbh, $table, $column_obj ) = @_;
174 my $name = $column_obj->name;
176 my $pg_server_version = $dbh->{'pg_server_version'};
178 unless ( $pg_server_version =~ /\d/ ) {
179 $warning = "WARNING: no pg_server_version! Assuming >= 7.3\n";
180 $pg_server_version = 70300;
183 my $hashref = { 'sql_after' => [], };
185 if ( $column_obj->type =~ /^(\w*)SERIAL$/i ) {
187 $hashref->{'effective_type'} = uc($1).'INT';
189 #needs more work for old Pg?
192 warn $warning if $warning;
193 if ( $pg_server_version >= 70300 ) {
194 my $db_schema = default_db_schema();
195 $nextval = "nextval('$db_schema.${table}_${name}_seq'::text)";
197 $nextval = "nextval('${table}_${name}_seq'::text)";
200 push @{ $hashref->{'sql_after'} },
201 "ALTER TABLE $table ALTER COLUMN $name SET DEFAULT $nextval",
202 "CREATE SEQUENCE ${table}_${name}_seq",
203 "UPDATE $table SET $name = $nextval WHERE $name IS NULL",
208 if ( ! $column_obj->null ) {
209 $hashref->{'effective_null'} = 'NULL';
211 warn $warning if $warning;
212 if ( $pg_server_version >= 70300 ) {
214 push @{ $hashref->{'sql_after'} },
215 "ALTER TABLE $table ALTER $name SET NOT NULL";
219 push @{ $hashref->{'sql_after'} },
220 "UPDATE pg_attribute SET attnotnull = TRUE ".
221 " WHERE attname = '$name' ".
222 " AND attrelid = ( SELECT oid FROM pg_class WHERE relname = '$table' )";
232 sub alter_column_callback {
233 my( $proto, $dbh, $table, $old_column, $new_column ) = @_;
234 my $name = $old_column->name;
237 'SMALLINT' => 'INT2',
241 'BIGSERIAL' => 'INT8',
242 'DECIMAL' => 'NUMERIC',
245 'TIMESTAMP' => 'TIMESTAMPTZ',
247 foreach ($old_column, $new_column) {
248 $_->type($canonical{uc($_->type)}) if $canonical{uc($_->type)};
251 my $pg_server_version = $dbh->{'pg_server_version'};
253 unless ( $pg_server_version =~ /\d/ ) {
254 $warning = "WARNING: no pg_server_version! Assuming >= 7.3\n";
255 $pg_server_version = 70300;
261 if ( ( $canonical{uc($old_column->type)} || uc($old_column->type) )
262 ne ( $canonical{uc($new_column->type)} || uc($new_column->type) )
263 || $old_column->length ne $new_column->length
267 warn $warning if $warning;
268 if ( $pg_server_version >= 80000 ) {
270 $hashref->{'sql_alter_type'} =
271 "ALTER TABLE $table ALTER COLUMN ". $new_column->name.
272 " TYPE ". $new_column->type.
273 ( ( defined($new_column->length) && $new_column->length )
274 ? '('.$new_column->length.')'
279 warn "WARNING: can't yet change column types for Pg < version 8\n";
284 # change nullability from NOT NULL to NULL
285 if ( ! $old_column->null && $new_column->null ) {
287 warn $warning if $warning;
288 if ( $pg_server_version < 70300 ) {
289 $hashref->{'sql_alter_null'} =
290 "UPDATE pg_attribute SET attnotnull = FALSE
291 WHERE attname = '$name'
292 AND attrelid = ( SELECT oid FROM pg_class
293 WHERE relname = '$table'
299 # change nullability from NULL to NOT NULL...
300 # this one could be more complicated, need to set a DEFAULT value and update
302 if ( $old_column->null && ! $new_column->null ) {
304 warn $warning if $warning;
305 if ( $pg_server_version < 70300 ) {
306 $hashref->{'sql_alter_null'} =
307 "UPDATE pg_attribute SET attnotnull = TRUE
308 WHERE attname = '$name'
309 AND attrelid = ( SELECT oid FROM pg_class
310 WHERE relname = '$table'
320 sub column_value_needs_quoting {
321 my($proto, $col) = @_;
322 _type_needs_quoting($col->type);
325 sub _type_needs_quoting {
332 | (?:numeric|decimal)(?:\(\d+(?:\s*\,\s*\d+\))?)?
344 Ivan Kohler <ivan-dbix-dbschema@420.am>
348 Copyright (c) 2000 Ivan Kohler
349 Copyright (c) 2000 Mail Abuse Prevention System LLC
350 Copyright (c) 2009-2010 Freeside Internet Services, Inc.
352 This program is free software; you can redistribute it and/or modify it under
353 the same terms as Perl itself.
359 columns doesn't return column default information.
363 L<DBIx::DBSchema>, L<DBIx::DBSchema::DBD>, L<DBI>, L<DBI::DBD>