X-Git-Url: http://git.freeside.biz/gitweb/?a=blobdiff_plain;f=DBSchema%2FDBD%2FPg.pm;h=0bf4ae4e386daba88008908dd0d4cb97c21d7806;hb=01e769f4c32d9fcae64746252d2ded26fc0e35ae;hp=30e7ecfcb0b99c2ea7e4fe0ba297db903916d0d4;hpb=c09b11401093a3ce923b631ffdd8a8c4a26c35aa;p=DBIx-DBSchema.git diff --git a/DBSchema/DBD/Pg.pm b/DBSchema/DBD/Pg.pm index 30e7ecf..0bf4ae4 100644 --- a/DBSchema/DBD/Pg.pm +++ b/DBSchema/DBD/Pg.pm @@ -1,18 +1,12 @@ 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; +use DBD::Pg 1.41; -$VERSION = '0.17'; -@ISA = qw(DBIx::DBSchema::DBD); +our $VERSION = '0.20'; -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', @@ -170,6 +164,56 @@ 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*(.*)$/i + or die "unparsable constraint: ". $_->{condef}; + my($columns, $table, $references, $etc ) = ($1, $2, $3, $4); + my $match = ( $etc =~ /MATCH (\w+)/i ) ? "MATCH $1" : ''; + my $on_delete = ( $etc =~ /ON DELETE ((NO |SET )?\w+)/i ) ? $1 : ''; + my $on_update = ( $etc =~ /ON UPDATE ((NO |SET )?\w+)/i ) ? $1 : ''; + warn $etc if $etc; + +{ 'constraint' => $_->{conname}, + 'columns' => [ split(/,\s*/, $columns) ], + 'table' => $table, + 'references' => [ split(/,\s*/, $references) ], + 'match' => $match, + 'on_delete' => $on_delete, + 'on_update' => $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; @@ -235,15 +279,16 @@ sub alter_column_callback { my $name = $old_column->name; my %canonical = ( - 'SMALLINT' => 'INT2', - 'INT' => 'INT4', - 'BIGINT' => 'INT8', - 'SERIAL' => 'INT4', - 'BIGSERIAL' => 'INT8', - 'DECIMAL' => 'NUMERIC', - 'REAL' => 'FLOAT4', - 'BLOB' => 'BYTEA', - 'TIMESTAMP' => 'TIMESTAMPTZ', + '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)}; @@ -269,7 +314,7 @@ sub alter_column_callback { if ( $pg_server_version >= 80000 ) { $hashref->{'sql_alter_type'} = - "ALTER TABLE $table ALTER COLUMN ". $new_column->name. + "ALTER COLUMN ". $new_column->name. " TYPE ". $new_column->type. ( ( defined($new_column->length) && $new_column->length ) ? '('.$new_column->length.')' @@ -348,15 +393,13 @@ Ivan Kohler Copyright (c) 2000 Ivan Kohler Copyright (c) 2000 Mail Abuse Prevention System LLC -Copyright (c) 2009-2010 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