X-Git-Url: http://git.freeside.biz/gitweb/?a=blobdiff_plain;f=DBSchema%2FDBD%2FPg.pm;h=c3d818f1a3a56a571da3e3ad2c4d3d814c9cb2ea;hb=400aa157abf08369aef787b093814ab9f4523015;hp=730f63800e88d5ff2d3ab3db73a4f1d4a071e87a;hpb=1af627deee1024ac86280165da04d4cf3f7ffb6e;p=DBIx-DBSchema.git diff --git a/DBSchema/DBD/Pg.pm b/DBSchema/DBD/Pg.pm index 730f638..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.18'; -@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', @@ -170,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; @@ -356,8 +398,6 @@ the same terms as Perl itself. =head1 BUGS -Yes. - columns doesn't return column default information. =head1 SEE ALSO