1 package DBIx::DBSchema::DBD::Pg;
2 use base qw(DBIx::DBSchema::DBD);
9 die "DBD::Pg version 1.32 or 1.41 (or later) required--".
10 "this is only version $DBD::Pg::VERSION\n"
11 if $DBD::Pg::VERSION != 1.32 && $DBD::Pg::VERSION < 1.41;
15 'LONG VARBINARY' => 'BYTEA',
16 'TIMESTAMP' => 'TIMESTAMP WITH TIME ZONE',
21 DBIx::DBSchema::DBD::Pg - PostgreSQL native driver for DBIx::DBSchema
28 $dbh = DBI->connect('dbi:Pg:dbname=database', 'user', 'pass');
29 $schema = new_native DBIx::DBSchema $dbh;
33 This module implements a PostgreSQL-native driver for DBIx::DBSchema.
37 sub default_db_schema { 'public'; }
40 my($proto, $dbh, $table) = @_;
41 my $sth = $dbh->prepare(<<END) or die $dbh->errstr;
42 SELECT a.attname, t.typname, a.attlen, a.atttypmod, a.attnotnull,
44 FROM pg_class c, pg_attribute a, pg_type t
45 WHERE c.relname = '$table'
46 AND a.attnum > 0 AND a.attrelid = c.oid AND a.atttypid = t.oid
49 $sth->execute or die $sth->errstr;
53 my $type = $_->{'typname'};
54 $type = 'char' if $type eq 'bpchar';
57 if ( $_->{attlen} == -1 && $_->{atttypmod} != -1
58 && $_->{typname} ne 'text' ) {
59 $len = $_->{atttypmod} - 4;
60 if ( $_->{typname} eq 'numeric' ) {
61 $len = ($len >> 16). ','. ($len & 0xffff);
66 if ( $_->{atthasdef} ) {
67 my $attnum = $_->{attnum};
68 my $d_sth = $dbh->prepare(<<END) or die $dbh->errstr;
69 SELECT substring(d.adsrc for 128) FROM pg_attrdef d, pg_class c
70 WHERE c.relname = '$table' AND c.oid = d.adrelid AND d.adnum = $attnum
72 $d_sth->execute or die $d_sth->errstr;
74 $default = $d_sth->fetchrow_arrayref->[0];
76 if ( _type_needs_quoting($type) ) {
77 $default =~ s/::([\w ]+)$//; #save typecast info?
78 if ( $default =~ /^'(.*)'$/ ) {
80 $default = \"''" if $default eq '';
85 } elsif ( $default =~ /^[a-z]/i ) { #sloppy, but it'll do
101 } @{ $sth->fetchall_arrayref({}) };
105 my($proto, $dbh, $table) = @_;
106 my $sth = $dbh->prepare(<<END) or die $dbh->errstr;
107 SELECT a.attname, a.attnum
108 FROM pg_class c, pg_attribute a, pg_type t
109 WHERE c.relname = '${table}_pkey'
110 AND a.attnum > 0 AND a.attrelid = c.oid AND a.atttypid = t.oid
112 $sth->execute or die $sth->errstr;
113 my $row = $sth->fetchrow_hashref or return '';
118 my($proto, $dbh, $table) = @_;
119 my $gratuitous = { map { $_ => [ $proto->_index_fields($dbh, $_ ) ] }
120 grep { $proto->_is_unique($dbh, $_ ) }
121 $proto->_all_indices($dbh, $table)
126 my($proto, $dbh, $table) = @_;
127 my $gratuitous = { map { $_ => [ $proto->_index_fields($dbh, $_ ) ] }
128 grep { ! $proto->_is_unique($dbh, $_ ) }
129 $proto->_all_indices($dbh, $table)
134 my($proto, $dbh, $table) = @_;
135 my $sth = $dbh->prepare(<<END) or die $dbh->errstr;
137 FROM pg_class c, pg_class c2, pg_index i
138 WHERE c.relname = '$table' AND c.oid = i.indrelid AND i.indexrelid = c2.oid
140 $sth->execute or die $sth->errstr;
141 map { $_->{'relname'} }
142 grep { $_->{'relname'} !~ /_pkey$/ }
143 @{ $sth->fetchall_arrayref({}) };
147 my($proto, $dbh, $index) = @_;
148 my $sth = $dbh->prepare(<<END) or die $dbh->errstr;
149 SELECT a.attname, a.attnum
150 FROM pg_class c, pg_attribute a, pg_type t
151 WHERE c.relname = '$index'
152 AND a.attnum > 0 AND a.attrelid = c.oid AND a.atttypid = t.oid
155 $sth->execute or die $sth->errstr;
156 map { $_->{'attname'} } @{ $sth->fetchall_arrayref({}) };
160 my($proto, $dbh, $index) = @_;
161 my $sth = $dbh->prepare(<<END) or die $dbh->errstr;
163 FROM pg_index i, pg_class c, pg_am a
164 WHERE i.indexrelid = c.oid AND c.relname = '$index' AND c.relam = a.oid
166 $sth->execute or die $sth->errstr;
167 my $row = $sth->fetchrow_hashref or die 'guru meditation #420';
168 $row->{'indisunique'};
172 #******** QUERY **********
174 # pg_catalog.pg_get_constraintdef(r.oid, true) as condef
175 #FROM pg_catalog.pg_constraint r
176 #WHERE r.conrelid = '16457' AND r.contype = 'f' ORDER BY 1;
177 #**************************
180 #********* QUERY **********
181 #SELECT conname, conrelid::pg_catalog.regclass,
182 # pg_catalog.pg_get_constraintdef(c.oid, true) as condef
183 #FROM pg_catalog.pg_constraint c
184 #WHERE c.confrelid = '16457' AND c.contype = 'f' ORDER BY 1;
185 #**************************
188 my($proto, $dbh, $table) = @_;
189 my $sth = $dbh->prepare(<<END) or die $dbh->errstr;
190 SELECT conname, pg_catalog.pg_get_constraintdef(r.oid, true) as condef
191 FROM pg_catalog.pg_constraint r
192 WHERE r.conrelid = ( SELECT oid FROM pg_class
193 WHERE relname = '$table'
194 AND pg_catalog.pg_table_is_visible(oid)
201 =~ /^FOREIGN KEY \(([\w\, ]+)\) REFERENCES (\w+)\(([\w\, ]+)\)\s*(.*)$/
202 or die "unparsable constraint: ". $_->{condef};
203 my($columns, $table, $references, $etc ) = ($1, $2, $3, $4);
204 +{ 'constraint' => $_->{conname},
205 'columns' => [ split(/,\s*/, $columns) ],
207 'references' => [ split(/,\s*/, $references) ],
208 #XXX $etc not handled yet for MATCH, ON DELETE, ON UPDATE
211 grep $_->{condef} =~ /^\s*FOREIGN\s+KEY/,
212 @{ $sth->fetchall_arrayref( {} ) };
215 sub add_column_callback {
216 my( $proto, $dbh, $table, $column_obj ) = @_;
217 my $name = $column_obj->name;
219 my $pg_server_version = $dbh->{'pg_server_version'};
221 unless ( $pg_server_version =~ /\d/ ) {
222 $warning = "WARNING: no pg_server_version! Assuming >= 7.3\n";
223 $pg_server_version = 70300;
226 my $hashref = { 'sql_after' => [], };
228 if ( $column_obj->type =~ /^(\w*)SERIAL$/i ) {
230 $hashref->{'effective_type'} = uc($1).'INT';
232 #needs more work for old Pg?
235 warn $warning if $warning;
236 if ( $pg_server_version >= 70300 ) {
237 my $db_schema = default_db_schema();
238 $nextval = "nextval('$db_schema.${table}_${name}_seq'::text)";
240 $nextval = "nextval('${table}_${name}_seq'::text)";
243 push @{ $hashref->{'sql_after'} },
244 "ALTER TABLE $table ALTER COLUMN $name SET DEFAULT $nextval",
245 "CREATE SEQUENCE ${table}_${name}_seq",
246 "UPDATE $table SET $name = $nextval WHERE $name IS NULL",
251 if ( ! $column_obj->null ) {
252 $hashref->{'effective_null'} = 'NULL';
254 warn $warning if $warning;
255 if ( $pg_server_version >= 70300 ) {
257 push @{ $hashref->{'sql_after'} },
258 "ALTER TABLE $table ALTER $name SET NOT NULL";
262 push @{ $hashref->{'sql_after'} },
263 "UPDATE pg_attribute SET attnotnull = TRUE ".
264 " WHERE attname = '$name' ".
265 " AND attrelid = ( SELECT oid FROM pg_class WHERE relname = '$table' )";
275 sub alter_column_callback {
276 my( $proto, $dbh, $table, $old_column, $new_column ) = @_;
277 my $name = $old_column->name;
280 'SMALLINT' => 'INT2',
284 'BIGSERIAL' => 'INT8',
285 'DECIMAL' => 'NUMERIC',
287 'DOUBLE PRECISION' => 'FLOAT8',
289 'TIMESTAMP' => 'TIMESTAMPTZ',
291 foreach ($old_column, $new_column) {
292 $_->type($canonical{uc($_->type)}) if $canonical{uc($_->type)};
295 my $pg_server_version = $dbh->{'pg_server_version'};
297 unless ( $pg_server_version =~ /\d/ ) {
298 $warning = "WARNING: no pg_server_version! Assuming >= 7.3\n";
299 $pg_server_version = 70300;
305 if ( ( $canonical{uc($old_column->type)} || uc($old_column->type) )
306 ne ( $canonical{uc($new_column->type)} || uc($new_column->type) )
307 || $old_column->length ne $new_column->length
311 warn $warning if $warning;
312 if ( $pg_server_version >= 80000 ) {
314 $hashref->{'sql_alter_type'} =
315 "ALTER COLUMN ". $new_column->name.
316 " TYPE ". $new_column->type.
317 ( ( defined($new_column->length) && $new_column->length )
318 ? '('.$new_column->length.')'
323 warn "WARNING: can't yet change column types for Pg < version 8\n";
328 # change nullability from NOT NULL to NULL
329 if ( ! $old_column->null && $new_column->null ) {
331 warn $warning if $warning;
332 if ( $pg_server_version < 70300 ) {
333 $hashref->{'sql_alter_null'} =
334 "UPDATE pg_attribute SET attnotnull = FALSE
335 WHERE attname = '$name'
336 AND attrelid = ( SELECT oid FROM pg_class
337 WHERE relname = '$table'
343 # change nullability from NULL to NOT NULL...
344 # this one could be more complicated, need to set a DEFAULT value and update
346 if ( $old_column->null && ! $new_column->null ) {
348 warn $warning if $warning;
349 if ( $pg_server_version < 70300 ) {
350 $hashref->{'sql_alter_null'} =
351 "UPDATE pg_attribute SET attnotnull = TRUE
352 WHERE attname = '$name'
353 AND attrelid = ( SELECT oid FROM pg_class
354 WHERE relname = '$table'
364 sub column_value_needs_quoting {
365 my($proto, $col) = @_;
366 _type_needs_quoting($col->type);
369 sub _type_needs_quoting {
376 | (?:numeric|decimal)(?:\(\d+(?:\s*\,\s*\d+\))?)?
388 Ivan Kohler <ivan-dbix-dbschema@420.am>
392 Copyright (c) 2000 Ivan Kohler
393 Copyright (c) 2000 Mail Abuse Prevention System LLC
394 Copyright (c) 2009-2013 Freeside Internet Services, Inc.
396 This program is free software; you can redistribute it and/or modify it under
397 the same terms as Perl itself.
401 columns doesn't return column default information.
405 L<DBIx::DBSchema>, L<DBIx::DBSchema::DBD>, L<DBI>, L<DBI::DBD>