X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FDBIx%2FClass%2FSchema%2FLoader%2FDBI%2FPg.pm;h=117c19740111a5483573a6f29c5d1e3e3a2aa3a9;hb=d372611d61d0051ccbda0744b38d2244188fa3f8;hp=c80c4204c7270df5218992c195256d773e8dbaa6;hpb=8e6c80c9ded48d2f9450de4200c4490b13d0c942;p=dbsrgits%2FDBIx-Class-Schema-Loader.git diff --git a/lib/DBIx/Class/Schema/Loader/DBI/Pg.pm b/lib/DBIx/Class/Schema/Loader/DBI/Pg.pm index c80c420..117c197 100644 --- a/lib/DBIx/Class/Schema/Loader/DBI/Pg.pm +++ b/lib/DBIx/Class/Schema/Loader/DBI/Pg.pm @@ -2,13 +2,10 @@ package DBIx::Class::Schema::Loader::DBI::Pg; use strict; use warnings; -use base qw/ - DBIx::Class::Schema::Loader::DBI::Component::QuotedDefault - DBIx::Class::Schema::Loader::DBI -/; +use base 'DBIx::Class::Schema::Loader::DBI::Component::QuotedDefault'; use mro 'c3'; -our $VERSION = '0.07011'; +our $VERSION = '0.07036'; =head1 NAME @@ -43,6 +40,65 @@ sub _system_schemas { return ($self->next::method(@_), 'pg_catalog'); } +my %pg_rules = ( + a => 'NO ACTION', + r => 'RESTRICT', + c => 'CASCADE', + n => 'SET NULL', + d => 'SET DEFAULT', +); + +sub _table_fk_info { + my ($self, $table) = @_; + + my $sth = $self->dbh->prepare_cached(<<"EOF"); +select q.constr_name, q.to_schema, q.to_table, from_cols.attname from_col, to_cols.attname to_col, + q.on_delete, q.on_update, q.is_deferrable +from (select constr.conname constr_name, to_ns.nspname to_schema, to_class.relname to_table, + unnest(constr.conkey) from_colnum, unnest(constr.confkey) to_colnum, + constr.confdeltype on_delete, constr.confupdtype on_update, + constr.condeferrable is_deferrable, + constr.conrelid conrelid, constr.confrelid confrelid + from pg_constraint constr + join pg_namespace from_ns on constr.connamespace = from_ns.oid + join pg_class from_class on constr.conrelid = from_class.oid and from_class.relnamespace = from_ns.oid + join pg_class to_class on constr.confrelid = to_class.oid + join pg_namespace to_ns on to_class.relnamespace = to_ns.oid + where from_ns.nspname = ? + and from_class.relname = ? + and from_class.relkind = 'r' + and constr.contype = 'f' +) q +join pg_attribute from_cols on from_cols.attrelid = q.conrelid and from_cols.attnum = q.from_colnum +join pg_attribute to_cols on to_cols.attrelid = q.confrelid and to_cols.attnum = q.to_colnum; +EOF + + $sth->execute($table->schema, $table->name); + + my %rels; + + while (my ($fk, $remote_schema, $remote_table, $col, $remote_col, + $delete_rule, $update_rule, $is_deferrable) = $sth->fetchrow_array) { + push @{ $rels{$fk}{local_columns} }, $self->_lc($col); + push @{ $rels{$fk}{remote_columns} }, $self->_lc($remote_col); + + $rels{$fk}{remote_table} = DBIx::Class::Schema::Loader::Table->new( + loader => $self, + name => $remote_table, + schema => $remote_schema, + ) unless exists $rels{$fk}{remote_table}; + + $rels{$fk}{attrs} ||= { + on_delete => $pg_rules{$delete_rule}, + on_update => $pg_rules{$update_rule}, + is_deferrable => $is_deferrable, + }; + } + + return [ map { $rels{$_} } sort keys %rels ]; +} + + sub _table_uniq_info { my ($self, $table) = @_; @@ -78,7 +134,7 @@ sub _table_uniq_info { c.relname = ?} ); - $uniq_sth->execute($table->schema, $table); + $uniq_sth->execute($table->schema, $table->name); while(my $row = $uniq_sth->fetchrow_arrayref) { my ($tableid, $indexname, $col_nums) = @$row; $col_nums =~ s/^\s+//; @@ -88,7 +144,7 @@ sub _table_uniq_info { foreach (@col_nums) { $attr_sth->execute($tableid, $_); my $name_aref = $attr_sth->fetchrow_arrayref; - push(@col_names, $name_aref->[0]) if $name_aref; + push(@col_names, $self->_lc($name_aref->[0])) if $name_aref; } if(!@col_names) { @@ -111,8 +167,8 @@ sub _table_comment { return $table_comment if $table_comment; ($table_comment) = $self->dbh->selectrow_array(<<'EOF', {}, $table->name, $table->schema); -SELECT obj_description(oid) -FROM pg_class +SELECT obj_description(oid) +FROM pg_class WHERE relname=? AND relnamespace=(SELECT oid FROM pg_namespace WHERE nspname=?) EOF @@ -130,7 +186,7 @@ sub _column_comment { my ($table_oid) = $self->dbh->selectrow_array(<<'EOF', {}, $table->name, $table->schema); SELECT oid -FROM pg_class +FROM pg_class WHERE relname=? AND relnamespace=(SELECT oid FROM pg_namespace WHERE nspname=?) EOF @@ -164,7 +220,7 @@ sub _columns_info_for { } my ($precision) = $self->schema->storage->dbh - ->selectrow_array(<selectrow_array(<name, $col); SELECT datetime_precision FROM information_schema.columns WHERE table_name = ? and column_name = ? @@ -199,7 +255,7 @@ EOF elsif ($data_type =~ /^(?:bit(?: varying)?|varbit)\z/i) { $info->{data_type} = 'varbit' if $data_type =~ /var/i; - my ($precision) = $self->dbh->selectrow_array(<dbh->selectrow_array(<name, $col); SELECT character_maximum_length FROM information_schema.columns WHERE table_name = ? and column_name = ? @@ -237,12 +293,14 @@ EOF if ($typetype && $typetype eq 'e') { # The following will extract a list of allowed values for the # enum. + my $order_column = $self->dbh->{pg_server_version} >= 90100 ? 'enumsortorder' : 'oid'; my $typevalues = $self->dbh ->selectall_arrayref(<{data_type}); SELECT e.enumlabel FROM pg_catalog.pg_enum e JOIN pg_catalog.pg_type t ON t.oid = e.enumtypid WHERE t.typname = ? +ORDER BY e.$order_column EOF $info->{extra}{list} = [ map { $_->[0] } @$typevalues ]; @@ -251,7 +309,7 @@ EOF $info->{extra}{custom_type_name} = $info->{data_type}; $info->{data_type} = 'enum'; - + delete $info->{size}; } }