X-Git-Url: http://git.freeside.biz/gitweb/?a=blobdiff_plain;f=DBSchema%2FDBD%2FPg.pm;h=c3d818f1a3a56a571da3e3ad2c4d3d814c9cb2ea;hb=400aa157abf08369aef787b093814ab9f4523015;hp=da4900983a2acfbf50c1b9227f506927b3ca1d50;hpb=67bdf8ab29e1fe0b2a057d9955a30bb363cda1f6;p=DBIx-DBSchema.git diff --git a/DBSchema/DBD/Pg.pm b/DBSchema/DBD/Pg.pm index da49009..c3d818f 100644 --- a/DBSchema/DBD/Pg.pm +++ b/DBSchema/DBD/Pg.pm @@ -1,18 +1,16 @@ package DBIx::DBSchema::DBD::Pg; +use base qw(DBIx::DBSchema::DBD); use strict; -use vars qw($VERSION @ISA %typemap); use DBD::Pg 1.32; -use DBIx::DBSchema::DBD; -$VERSION = '0.13'; -@ISA = qw(DBIx::DBSchema::DBD); +our $VERSION = '0.19'; die "DBD::Pg version 1.32 or 1.41 (or later) required--". "this is only version $DBD::Pg::VERSION\n" if $DBD::Pg::VERSION != 1.32 && $DBD::Pg::VERSION < 1.41; -%typemap = ( +our %typemap = ( 'BLOB' => 'BYTEA', 'LONG VARBINARY' => 'BYTEA', 'TIMESTAMP' => 'TIMESTAMP WITH TIME ZONE', @@ -52,6 +50,18 @@ END map { + my $type = $_->{'typname'}; + $type = 'char' if $type eq 'bpchar'; + + my $len = ''; + if ( $_->{attlen} == -1 && $_->{atttypmod} != -1 + && $_->{typname} ne 'text' ) { + $len = $_->{atttypmod} - 4; + if ( $_->{typname} eq 'numeric' ) { + $len = ($len >> 16). ','. ($len & 0xffff); + } + } + my $default = ''; if ( $_->{atthasdef} ) { my $attnum = $_->{attnum}; @@ -62,19 +72,22 @@ END $d_sth->execute or die $d_sth->errstr; $default = $d_sth->fetchrow_arrayref->[0]; - }; - my $len = ''; - if ( $_->{attlen} == -1 && $_->{atttypmod} != -1 - && $_->{typname} ne 'text' ) { - $len = $_->{atttypmod} - 4; - if ( $_->{typname} eq 'numeric' ) { - $len = ($len >> 16). ','. ($len & 0xffff); + if ( _type_needs_quoting($type) ) { + $default =~ s/::([\w ]+)$//; #save typecast info? + if ( $default =~ /^'(.*)'$/ ) { + $default = $1; + $default = \"''" if $default eq ''; + } else { + my $value = $default; + $default = \$value; + } + } elsif ( $default =~ /^[a-z]/i ) { #sloppy, but it'll do + my $value = $default; + $default = \$value; } - } - my $type = $_->{'typname'}; - $type = 'char' if $type eq 'bpchar'; + } [ $_->{'attname'}, @@ -155,6 +168,50 @@ END $row->{'indisunique'}; } +#using this +#******** QUERY ********** +#SELECT conname, +# pg_catalog.pg_get_constraintdef(r.oid, true) as condef +#FROM pg_catalog.pg_constraint r +#WHERE r.conrelid = '16457' AND r.contype = 'f' ORDER BY 1; +#************************** + +# what's this do? +#********* QUERY ********** +#SELECT conname, conrelid::pg_catalog.regclass, +# pg_catalog.pg_get_constraintdef(c.oid, true) as condef +#FROM pg_catalog.pg_constraint c +#WHERE c.confrelid = '16457' AND c.contype = 'f' ORDER BY 1; +#************************** + +sub constraints { + my($proto, $dbh, $table) = @_; + my $sth = $dbh->prepare(<errstr; + SELECT conname, pg_catalog.pg_get_constraintdef(r.oid, true) as condef + FROM pg_catalog.pg_constraint r + WHERE r.conrelid = ( SELECT oid FROM pg_class + WHERE relname = '$table' + AND pg_catalog.pg_table_is_visible(oid) + ) + AND r.contype = 'f' +END + $sth->execute; + + map { $_->{condef} + =~ /^FOREIGN KEY \(([\w\, ]+)\) REFERENCES (\w+)\(([\w\, ]+)\)\s*(.*)$/ + or die "unparsable constraint: ". $_->{condef}; + my($columns, $table, $references, $etc ) = ($1, $2, $3, $4); + +{ 'constraint' => $_->{conname}, + 'columns' => [ split(/,\s*/, $columns) ], + 'table' => $table, + 'references' => [ split(/,\s*/, $references) ], + #XXX $etc not handled yet for MATCH, ON DELETE, ON UPDATE + }; + } + grep $_->{condef} =~ /^\s*FOREIGN\s+KEY/, + @{ $sth->fetchall_arrayref( {} ) }; +} + sub add_column_callback { my( $proto, $dbh, $table, $column_obj ) = @_; my $name = $column_obj->name; @@ -219,6 +276,22 @@ sub alter_column_callback { my( $proto, $dbh, $table, $old_column, $new_column ) = @_; my $name = $old_column->name; + my %canonical = ( + 'SMALLINT' => 'INT2', + 'INT' => 'INT4', + 'BIGINT' => 'INT8', + 'SERIAL' => 'INT4', + 'BIGSERIAL' => 'INT8', + 'DECIMAL' => 'NUMERIC', + 'REAL' => 'FLOAT4', + 'DOUBLE PRECISION' => 'FLOAT8', + 'BLOB' => 'BYTEA', + 'TIMESTAMP' => 'TIMESTAMPTZ', + ); + foreach ($old_column, $new_column) { + $_->type($canonical{uc($_->type)}) if $canonical{uc($_->type)}; + } + my $pg_server_version = $dbh->{'pg_server_version'}; my $warning = ''; unless ( $pg_server_version =~ /\d/ ) { @@ -228,6 +301,30 @@ sub alter_column_callback { my $hashref = {}; + #change type + if ( ( $canonical{uc($old_column->type)} || uc($old_column->type) ) + ne ( $canonical{uc($new_column->type)} || uc($new_column->type) ) + || $old_column->length ne $new_column->length + ) + { + + warn $warning if $warning; + if ( $pg_server_version >= 80000 ) { + + $hashref->{'sql_alter_type'} = + "ALTER COLUMN ". $new_column->name. + " TYPE ". $new_column->type. + ( ( defined($new_column->length) && $new_column->length ) + ? '('.$new_column->length.')' + : '' + ) + + } else { + warn "WARNING: can't yet change column types for Pg < version 8\n"; + } + + } + # change nullability from NOT NULL to NULL if ( ! $old_column->null && $new_column->null ) { @@ -264,20 +361,25 @@ sub alter_column_callback { } -sub _column_value_needs_quoting { +sub column_value_needs_quoting { my($proto, $col) = @_; - $col->type !~ m{^( - int(?:2|4|8)? - | smallint - | integer - | bigint - | (?:numeric|decimal)(?:\(\d+(?:\s*\,\s*\d+\))?)? - | real - | double\s+precision - | float(?:\(\d+\))? - | serial(?:4|8)? - | bigserial - )$}x; + _type_needs_quoting($col->type); +} + +sub _type_needs_quoting { + my $type = shift; + $type !~ m{^( + int(?:2|4|8)? + | smallint + | integer + | bigint + | (?:numeric|decimal)(?:\(\d+(?:\s*\,\s*\d+\))?)? + | real + | double\s+precision + | float(?:\(\d+\))? + | serial(?:4|8)? + | bigserial + )$}ix; } @@ -289,15 +391,13 @@ Ivan Kohler Copyright (c) 2000 Ivan Kohler Copyright (c) 2000 Mail Abuse Prevention System LLC -Copyright (c) 2009 Freeside Internet Services, Inc. +Copyright (c) 2009-2013 Freeside Internet Services, Inc. All rights reserved. This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself. =head1 BUGS -Yes. - columns doesn't return column default information. =head1 SEE ALSO