loader as a schema subclass basically working (needs some cleanup and docs and whatnot)
[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 Carp;
6
7 =head1 NAME
8
9 DBIx::Class::Schema::Loader::Pg - DBIx::Class::Schema::Loader Postgres Implementation.
10
11 =head1 SYNOPSIS
12
13   use DBIx::Class::Schema::Loader;
14
15   # $loader is a DBIx::Class::Schema::Loader::Pg
16   my $loader = DBIx::Class::Schema::Loader->new(
17     dsn       => "dbi:Pg:dbname=dbname",
18     user      => "postgres",
19     password  => "",
20     namespace => "Data",
21   );
22
23 =head1 DESCRIPTION
24
25 See L<DBIx::Class::Schema::Loader>.
26
27 =cut
28
29 sub _db_classes {
30     return qw/DBIx::Class::PK::Auto::Pg/;
31 }
32
33 sub _tables {
34     my $class = shift;
35     my $dbh = $class->storage->dbh;
36
37     # This is split out to avoid version parsing errors...
38     my $is_dbd_pg_gte_131 = ( $DBD::Pg::VERSION >= 1.31 );
39     my @tables = $is_dbd_pg_gte_131 ? 
40         $dbh->tables( undef, $class->loader_data->{_db_schema}, "", "table", { noprefix => 1, pg_noprefix => 1 } )
41         : $dbh->tables;
42
43     s/"//g for @tables;
44     return @tables;
45 }
46
47 sub _table_info {
48     my ( $class, $table ) = @_;
49     my $dbh = $class->storage->dbh;
50
51     my $sth = $dbh->column_info(undef, $class->loader_data->{_db_schema}, $table, undef);
52     my @cols = map { $_->[3] } @{ $sth->fetchall_arrayref };
53     s/"//g for @cols;
54     
55     my @primary = $dbh->primary_key(undef, $class->loader_data->{_db_schema}, $table);
56
57     s/"//g for @primary;
58
59     return ( \@cols, \@primary );
60 }
61
62 =head1 SEE ALSO
63
64 L<DBIx::Class::Schema::Loader>
65
66 =cut
67
68 1;