fix table count test in common tests, inc version for dev release, add extra tests...
[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
fdd8ff16 9our $VERSION = '0.04999_11';
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
fbcfebdd 38
996be9ee 39sub _table_uniq_info {
40 my ($self, $table) = @_;
41
fd589700 42 # Use the default support if available
43 return $self->next::method($table)
79fe0081 44 if $DBD::Pg::VERSION >= 1.50;
fd589700 45
996be9ee 46 my @uniqs;
47 my $dbh = $self->schema->storage->dbh;
48
5223f24a 49 # Most of the SQL here is mostly based on
50 # Rose::DB::Object::Metadata::Auto::Pg, after some prodding from
51 # John Siracusa to use his superior SQL code :)
52
53 my $attr_sth = $self->{_cache}->{pg_attr_sth} ||= $dbh->prepare(
54 q{SELECT attname FROM pg_catalog.pg_attribute
55 WHERE attrelid = ? AND attnum = ?}
56 );
57
58 my $uniq_sth = $self->{_cache}->{pg_uniq_sth} ||= $dbh->prepare(
59 q{SELECT x.indrelid, i.relname, x.indkey
60 FROM
61 pg_catalog.pg_index x
62 JOIN pg_catalog.pg_class c ON c.oid = x.indrelid
63 JOIN pg_catalog.pg_class i ON i.oid = x.indexrelid
64 JOIN pg_catalog.pg_constraint con ON con.conname = i.relname
65 LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace
66 WHERE
67 x.indisunique = 't' AND
68 c.relkind = 'r' AND
69 i.relkind = 'i' AND
70 con.contype = 'u' AND
71 n.nspname = ? AND
72 c.relname = ?}
73 );
74
75 $uniq_sth->execute($self->db_schema, $table);
76 while(my $row = $uniq_sth->fetchrow_arrayref) {
77 my ($tableid, $indexname, $col_nums) = @$row;
78 $col_nums =~ s/^\s+//;
79 my @col_nums = split(/\s+/, $col_nums);
80 my @col_names;
81
82 foreach (@col_nums) {
83 $attr_sth->execute($tableid, $_);
84 my $name_aref = $attr_sth->fetchrow_arrayref;
85 push(@col_names, $name_aref->[0]) if $name_aref;
996be9ee 86 }
5223f24a 87
88 if(!@col_names) {
8f9d7ce5 89 warn "Failed to parse UNIQUE constraint $indexname on $table";
996be9ee 90 }
91 else {
5223f24a 92 push(@uniqs, [ $indexname => \@col_names ]);
996be9ee 93 }
94 }
95
96 return \@uniqs;
97}
98
fbcfebdd 99sub _table_comment {
100 my ( $self, $table ) = @_;
101 my ($table_comment) = $self->schema->storage->dbh->selectrow_array(
102 q{SELECT obj_description(oid)
103 FROM pg_class
104 WHERE relname=? AND relnamespace=(
105 SELECT oid FROM pg_namespace WHERE nspname=?)
106 }, undef, $table, $self->db_schema
107 );
108 return $table_comment
109}
110
111
112sub _column_comment {
113 my ( $self, $table, $column_number ) = @_;
114 my ($table_oid) = $self->schema->storage->dbh->selectrow_array(
115 q{SELECT oid
116 FROM pg_class
117 WHERE relname=? AND relnamespace=(
118 SELECT oid FROM pg_namespace WHERE nspname=?)
119 }, undef, $table, $self->db_schema
120 );
121 return $self->schema->storage->dbh->selectrow_array('SELECT col_description(?,?)', undef, $table_oid,
122 $column_number );
123}
124
a8df0345 125sub _extra_column_info {
78b7ccaa 126 my ($self, $info) = @_;
a8df0345 127 my %extra_info;
78b7ccaa 128
a8df0345 129 if ($info->{COLUMN_DEF} && $info->{COLUMN_DEF} =~ /\bnextval\(/i) {
130 $extra_info{is_auto_increment} = 1;
131 }
132
133 return \%extra_info;
78b7ccaa 134}
135
996be9ee 136=head1 SEE ALSO
137
138L<DBIx::Class::Schema::Loader>, L<DBIx::Class::Schema::Loader::Base>,
139L<DBIx::Class::Schema::Loader::DBI>
140
be80bba7 141=head1 AUTHOR
142
143See L<DBIx::Class::Schema::Loader/CONTRIBUTORS>.
144
145=head1 LICENSE
146
147This library is free software; you can redistribute it and/or modify it under
148the same terms as Perl itself.
149
996be9ee 150=cut
151
1521;