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
103 } @{ $sth->fetchall_arrayref({}) };
107 my($proto, $dbh, $table) = @_;
108 my $sth = $dbh->prepare(<<END) or die $dbh->errstr;
109 SELECT a.attname, a.attnum
110 FROM pg_class c, pg_attribute a, pg_type t
111 WHERE c.relname = '${table}_pkey'
112 AND a.attnum > 0 AND a.attrelid = c.oid AND a.atttypid = t.oid
114 $sth->execute or die $sth->errstr;
115 my $row = $sth->fetchrow_hashref or return '';
120 my($proto, $dbh, $table) = @_;
121 my $gratuitous = { map { $_ => [ $proto->_index_fields($dbh, $_ ) ] }
122 grep { $proto->_is_unique($dbh, $_ ) }
123 $proto->_all_indices($dbh, $table)
128 my($proto, $dbh, $table) = @_;
129 my $gratuitous = { map { $_ => [ $proto->_index_fields($dbh, $_ ) ] }
130 grep { ! $proto->_is_unique($dbh, $_ ) }
131 $proto->_all_indices($dbh, $table)
136 my($proto, $dbh, $table) = @_;
137 my $sth = $dbh->prepare(<<END) or die $dbh->errstr;
139 FROM pg_class c, pg_class c2, pg_index i
140 WHERE c.relname = '$table' AND c.oid = i.indrelid AND i.indexrelid = c2.oid
142 $sth->execute or die $sth->errstr;
143 map { $_->{'relname'} }
144 grep { $_->{'relname'} !~ /_pkey$/ }
145 @{ $sth->fetchall_arrayref({}) };
149 my($proto, $dbh, $index) = @_;
150 my $sth = $dbh->prepare(<<END) or die $dbh->errstr;
151 SELECT a.attname, a.attnum
152 FROM pg_class c, pg_attribute a, pg_type t
153 WHERE c.relname = '$index'
154 AND a.attnum > 0 AND a.attrelid = c.oid AND a.atttypid = t.oid
157 $sth->execute or die $sth->errstr;
158 map { $_->{'attname'} } @{ $sth->fetchall_arrayref({}) };
162 my($proto, $dbh, $index) = @_;
163 my $sth = $dbh->prepare(<<END) or die $dbh->errstr;
165 FROM pg_index i, pg_class c, pg_am a
166 WHERE i.indexrelid = c.oid AND c.relname = '$index' AND c.relam = a.oid
168 $sth->execute or die $sth->errstr;
169 my $row = $sth->fetchrow_hashref or die 'guru meditation #420';
170 $row->{'indisunique'};
173 sub add_column_callback {
174 my( $proto, $dbh, $table, $column_obj ) = @_;
175 my $name = $column_obj->name;
177 my $pg_server_version = $dbh->{'pg_server_version'};
179 unless ( $pg_server_version =~ /\d/ ) {
180 $warning = "WARNING: no pg_server_version! Assuming >= 7.3\n";
181 $pg_server_version = 70300;
184 my $hashref = { 'sql_after' => [], };
186 if ( $column_obj->type =~ /^(\w*)SERIAL$/i ) {
188 $hashref->{'effective_type'} = uc($1).'INT';
190 #needs more work for old Pg?
193 warn $warning if $warning;
194 if ( $pg_server_version >= 70300 ) {
195 my $db_schema = default_db_schema();
196 $nextval = "nextval('$db_schema.${table}_${name}_seq'::text)";
198 $nextval = "nextval('${table}_${name}_seq'::text)";
201 push @{ $hashref->{'sql_after'} },
202 "ALTER TABLE $table ALTER COLUMN $name SET DEFAULT $nextval",
203 "CREATE SEQUENCE ${table}_${name}_seq",
204 "UPDATE $table SET $name = $nextval WHERE $name IS NULL",
209 if ( ! $column_obj->null ) {
210 $hashref->{'effective_null'} = 'NULL';
212 warn $warning if $warning;
213 if ( $pg_server_version >= 70300 ) {
215 push @{ $hashref->{'sql_after'} },
216 "ALTER TABLE $table ALTER $name SET NOT NULL";
220 push @{ $hashref->{'sql_after'} },
221 "UPDATE pg_attribute SET attnotnull = TRUE ".
222 " WHERE attname = '$name' ".
223 " AND attrelid = ( SELECT oid FROM pg_class WHERE relname = '$table' )";
233 sub alter_column_callback {
234 my( $proto, $dbh, $table, $old_column, $new_column ) = @_;
235 my $name = $old_column->name;
238 'SMALLINT' => 'INT2',
242 'BIGSERIAL' => 'INT8',
243 'DECIMAL' => 'NUMERIC',
245 'DOUBLE PRECISION' => 'FLOAT8',
247 'TIMESTAMP' => 'TIMESTAMPTZ',
249 foreach ($old_column, $new_column) {
250 $_->type($canonical{uc($_->type)}) if $canonical{uc($_->type)};
253 my $pg_server_version = $dbh->{'pg_server_version'};
255 unless ( $pg_server_version =~ /\d/ ) {
256 $warning = "WARNING: no pg_server_version! Assuming >= 7.3\n";
257 $pg_server_version = 70300;
263 if ( ( $canonical{uc($old_column->type)} || uc($old_column->type) )
264 ne ( $canonical{uc($new_column->type)} || uc($new_column->type) )
265 || $old_column->length ne $new_column->length
269 warn $warning if $warning;
270 if ( $pg_server_version >= 80000 ) {
272 $hashref->{'sql_alter_type'} =
273 "ALTER COLUMN ". $new_column->name.
274 " TYPE ". $new_column->type.
275 ( ( defined($new_column->length) && $new_column->length )
276 ? '('.$new_column->length.')'
281 warn "WARNING: can't yet change column types for Pg < version 8\n";
286 # change nullability from NOT NULL to NULL
287 if ( ! $old_column->null && $new_column->null ) {
289 warn $warning if $warning;
290 if ( $pg_server_version < 70300 ) {
291 $hashref->{'sql_alter_null'} =
292 "UPDATE pg_attribute SET attnotnull = FALSE
293 WHERE attname = '$name'
294 AND attrelid = ( SELECT oid FROM pg_class
295 WHERE relname = '$table'
301 # change nullability from NULL to NOT NULL...
302 # this one could be more complicated, need to set a DEFAULT value and update
304 if ( $old_column->null && ! $new_column->null ) {
306 warn $warning if $warning;
307 if ( $pg_server_version < 70300 ) {
308 $hashref->{'sql_alter_null'} =
309 "UPDATE pg_attribute SET attnotnull = TRUE
310 WHERE attname = '$name'
311 AND attrelid = ( SELECT oid FROM pg_class
312 WHERE relname = '$table'
322 sub column_value_needs_quoting {
323 my($proto, $col) = @_;
324 _type_needs_quoting($col->type);
327 sub _type_needs_quoting {
334 | (?:numeric|decimal)(?:\(\d+(?:\s*\,\s*\d+\))?)?
346 Ivan Kohler <ivan-dbix-dbschema@420.am>
350 Copyright (c) 2000 Ivan Kohler
351 Copyright (c) 2000 Mail Abuse Prevention System LLC
352 Copyright (c) 2009-2013 Freeside Internet Services, Inc.
354 This program is free software; you can redistribute it and/or modify it under
355 the same terms as Perl itself.
361 columns doesn't return column default information.
365 L<DBIx::DBSchema>, L<DBIx::DBSchema::DBD>, L<DBI>, L<DBI::DBD>