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