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