bump version to 0.03999_01, added $VERSION to all packages
[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
32f784fc 9our $VERSION = '0.03999_01';
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
21 __PACKAGE__->loader_options(
22 relationships => 1,
23 );
24
25 1;
26
27=head1 DESCRIPTION
28
29See L<DBIx::Class::Schema::Loader::Base>.
30
31=cut
32
33sub _setup {
34 my $self = shift;
35
36 $self->next::method(@_);
37 $self->{db_schema} ||= 'public';
38}
39
40sub _table_uniq_info {
41 my ($self, $table) = @_;
42
43 my @uniqs;
44 my $dbh = $self->schema->storage->dbh;
45
5223f24a 46 # Most of the SQL here is mostly based on
47 # Rose::DB::Object::Metadata::Auto::Pg, after some prodding from
48 # John Siracusa to use his superior SQL code :)
49
50 my $attr_sth = $self->{_cache}->{pg_attr_sth} ||= $dbh->prepare(
51 q{SELECT attname FROM pg_catalog.pg_attribute
52 WHERE attrelid = ? AND attnum = ?}
53 );
54
55 my $uniq_sth = $self->{_cache}->{pg_uniq_sth} ||= $dbh->prepare(
56 q{SELECT x.indrelid, i.relname, x.indkey
57 FROM
58 pg_catalog.pg_index x
59 JOIN pg_catalog.pg_class c ON c.oid = x.indrelid
60 JOIN pg_catalog.pg_class i ON i.oid = x.indexrelid
61 JOIN pg_catalog.pg_constraint con ON con.conname = i.relname
62 LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace
63 WHERE
64 x.indisunique = 't' AND
65 c.relkind = 'r' AND
66 i.relkind = 'i' AND
67 con.contype = 'u' AND
68 n.nspname = ? AND
69 c.relname = ?}
70 );
71
72 $uniq_sth->execute($self->db_schema, $table);
73 while(my $row = $uniq_sth->fetchrow_arrayref) {
74 my ($tableid, $indexname, $col_nums) = @$row;
75 $col_nums =~ s/^\s+//;
76 my @col_nums = split(/\s+/, $col_nums);
77 my @col_names;
78
79 foreach (@col_nums) {
80 $attr_sth->execute($tableid, $_);
81 my $name_aref = $attr_sth->fetchrow_arrayref;
82 push(@col_names, $name_aref->[0]) if $name_aref;
996be9ee 83 }
5223f24a 84
85 if(!@col_names) {
8f9d7ce5 86 warn "Failed to parse UNIQUE constraint $indexname on $table";
996be9ee 87 }
88 else {
5223f24a 89 push(@uniqs, [ $indexname => \@col_names ]);
996be9ee 90 }
91 }
92
93 return \@uniqs;
94}
95
96=head1 SEE ALSO
97
98L<DBIx::Class::Schema::Loader>, L<DBIx::Class::Schema::Loader::Base>,
99L<DBIx::Class::Schema::Loader::DBI>
100
101=cut
102
1031;