dbc04702589721f3d6f0cfc83ee229b43dbff93b
[dbsrgits/DBIx-Class-Schema-Loader.git] / lib / DBIx / Class / Schema / Loader / DBI / Pg.pm
1 package DBIx::Class::Schema::Loader::DBI::Pg;
2
3 use strict;
4 use warnings;
5 use base 'DBIx::Class::Schema::Loader::DBI';
6 use Class::C3;
7
8 =head1 NAME
9
10 DBIx::Class::Schema::Loader::DBI::Pg - DBIx::Class::Schema::Loader::DBI Postgres Implementation.
11
12 =head1 SYNOPSIS
13
14   package My::Schema;
15   use base qw/DBIx::Class::Schema::Loader/;
16
17   __PACKAGE__->loader_options(
18     relationships => 1,
19   );
20
21   1;
22
23 =head1 DESCRIPTION
24
25 See L<DBIx::Class::Schema::Loader::Base>.
26
27 =cut
28
29 sub _setup {
30     my $self = shift;
31
32     $self->next::method(@_);
33     $self->{db_schema} ||= 'public';
34 }
35
36 sub _table_uniq_info {
37     my ($self, $table) = @_;
38
39     my @uniqs;
40     my $dbh = $self->schema->storage->dbh;
41
42     my $sth = $dbh->prepare_cached(
43         qq{SELECT conname,indexdef FROM pg_indexes JOIN pg_constraint }
44       . qq{ON (pg_indexes.indexname = pg_constraint.conname) }
45       . qq{WHERE schemaname=? and tablename=? and contype = 'u'}
46     ,{}, 1);
47
48     $sth->execute($self->db_schema, $table);
49     while(my $constr = $sth->fetchrow_arrayref) {
50         my $constr_name = $constr->[0];
51         my $constr_def  = $constr->[1];
52         my @cols;
53         if($constr_def =~ /\(\s*([^)]+)\)\s*$/) {
54             my $cols_text = $1;
55             $cols_text =~ s/\s+$//;
56             @cols = map { lc } split(/\s*,\s*/, $cols_text);
57             s/\Q$self->{_quoter}\E// for @cols;
58         }
59         if(!@cols) {
60             warn "Failed to parse unique constraint $constr_name on $table";
61         }
62         else {
63             push(@uniqs, [ $constr_name => \@cols ]);
64         }
65     }
66
67     return \@uniqs;
68 }
69
70 =head1 SEE ALSO
71
72 L<DBIx::Class::Schema::Loader>, L<DBIx::Class::Schema::Loader::Base>,
73 L<DBIx::Class::Schema::Loader::DBI>
74
75 =cut
76
77 1;