0.04003 changes, version bumps
[dbsrgits/DBIx-Class-Schema-Loader.git] / lib / DBIx / Class / Schema / Loader / DBI / Pg.pm
CommitLineData
996be9ee 1package DBIx::Class::Schema::Loader::DBI::Pg;
2
3use strict;
4use warnings;
5use base 'DBIx::Class::Schema::Loader::DBI';
fa994d3c 6use Carp::Clan qw/^DBIx::Class/;
996be9ee 7use Class::C3;
8
f1f25439 9our $VERSION = '0.04003';
32f784fc 10
996be9ee 11=head1 NAME
12
8f9d7ce5 13DBIx::Class::Schema::Loader::DBI::Pg - DBIx::Class::Schema::Loader::DBI
14PostgreSQL Implementation.
996be9ee 15
16=head1 SYNOPSIS
17
18 package My::Schema;
19 use base qw/DBIx::Class::Schema::Loader/;
20
59cfa251 21 __PACKAGE__->loader_options( debug => 1 );
996be9ee 22
23 1;
24
25=head1 DESCRIPTION
26
27See L<DBIx::Class::Schema::Loader::Base>.
28
29=cut
30
31sub _setup {
32 my $self = shift;
33
34 $self->next::method(@_);
35 $self->{db_schema} ||= 'public';
36}
37
38sub _table_uniq_info {
39 my ($self, $table) = @_;
40
fd589700 41 # Use the default support if available
42 return $self->next::method($table)
79fe0081 43 if $DBD::Pg::VERSION >= 1.50;
fd589700 44
996be9ee 45 my @uniqs;
46 my $dbh = $self->schema->storage->dbh;
47
5223f24a 48 # Most of the SQL here is mostly based on
49 # Rose::DB::Object::Metadata::Auto::Pg, after some prodding from
50 # John Siracusa to use his superior SQL code :)
51
52 my $attr_sth = $self->{_cache}->{pg_attr_sth} ||= $dbh->prepare(
53 q{SELECT attname FROM pg_catalog.pg_attribute
54 WHERE attrelid = ? AND attnum = ?}
55 );
56
57 my $uniq_sth = $self->{_cache}->{pg_uniq_sth} ||= $dbh->prepare(
58 q{SELECT x.indrelid, i.relname, x.indkey
59 FROM
60 pg_catalog.pg_index x
61 JOIN pg_catalog.pg_class c ON c.oid = x.indrelid
62 JOIN pg_catalog.pg_class i ON i.oid = x.indexrelid
63 JOIN pg_catalog.pg_constraint con ON con.conname = i.relname
64 LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace
65 WHERE
66 x.indisunique = 't' AND
67 c.relkind = 'r' AND
68 i.relkind = 'i' AND
69 con.contype = 'u' AND
70 n.nspname = ? AND
71 c.relname = ?}
72 );
73
74 $uniq_sth->execute($self->db_schema, $table);
75 while(my $row = $uniq_sth->fetchrow_arrayref) {
76 my ($tableid, $indexname, $col_nums) = @$row;
77 $col_nums =~ s/^\s+//;
78 my @col_nums = split(/\s+/, $col_nums);
79 my @col_names;
80
81 foreach (@col_nums) {
82 $attr_sth->execute($tableid, $_);
83 my $name_aref = $attr_sth->fetchrow_arrayref;
84 push(@col_names, $name_aref->[0]) if $name_aref;
996be9ee 85 }
5223f24a 86
87 if(!@col_names) {
8f9d7ce5 88 warn "Failed to parse UNIQUE constraint $indexname on $table";
996be9ee 89 }
90 else {
5223f24a 91 push(@uniqs, [ $indexname => \@col_names ]);
996be9ee 92 }
93 }
94
95 return \@uniqs;
96}
97
98=head1 SEE ALSO
99
100L<DBIx::Class::Schema::Loader>, L<DBIx::Class::Schema::Loader::Base>,
101L<DBIx::Class::Schema::Loader::DBI>
102
103=cut
104
1051;