a44db20fcd415535dd0676e7be24ac1fc72d7920
[dbsrgits/DBIx-Class-Schema-Loader.git] / lib / DBIx / Class / Loader / Pg.pm
1 package DBIx::Class::Loader::Pg;
2
3 use strict;
4 use base 'DBIx::Class::Loader::Generic';
5 use DBI;
6 use Carp;
7
8 =head1 NAME
9
10 DBIx::Class::Loader::Pg - DBIx::Class::Loader Postgres Implementation.
11
12 =head1 SYNOPSIS
13
14   use DBIx::Class::Loader;
15
16   # $loader is a DBIx::Class::Loader::Pg
17   my $loader = DBIx::Class::Loader->new(
18     dsn       => "dbi:Pg:dbname=dbname",
19     user      => "postgres",
20     password  => "",
21     namespace => "Data",
22   );
23   my $class = $loader->find_class('film'); # $class => Data::Film
24   my $obj = $class->retrieve(1);
25
26 =head1 DESCRIPTION
27
28 See L<DBIx::Class::Loader>.
29
30 =cut
31
32 sub _db_classes {
33     return qw/DBIx::Class::PK::Auto::Pg/;
34 }
35
36 sub _tables {
37     my $self = shift;
38     my $dbh = $self->{_storage}->dbh;
39
40     # This is split out to avoid version parsing errors...
41     my $is_dbd_pg_gte_131 = ( $DBD::Pg::VERSION >= 1.31 );
42     my @tables = $is_dbd_pg_gte_131 ? 
43         $dbh->tables( undef, $self->{_db_schema}, "", "table", { noprefix => 1, pg_noprefix => 1 } )
44         : $dbh->tables;
45
46     s/"//g for @tables;
47     return @tables;
48 }
49
50 sub _table_info {
51     my ( $self, $table ) = @_;
52     my $dbh = $self->{_storage}->dbh;
53
54     my $sth = $dbh->column_info(undef, $self->{_db_schema}, $table, undef);
55     my @cols = map { $_->[3] } @{ $sth->fetchall_arrayref };
56     s/"//g for @cols;
57     
58     my @primary = $dbh->primary_key(undef, $self->{_db_schema}, $table);
59
60     s/"//g for @primary;
61
62     return ( \@cols, \@primary );
63 }
64
65 =head1 SEE ALSO
66
67 L<DBIx::Class::Loader>
68
69 =cut
70
71 1;