more work on components, base classes, and resultset_components - still broken in...
[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 warnings;
5 use Class::C3;
6 use base 'DBIx::Class::Schema::Loader::Generic';
7
8 =head1 NAME
9
10 DBIx::Class::Schema::Loader::Pg - DBIx::Class::Schema::Loader Postgres Implementation.
11
12 =head1 SYNOPSIS
13
14   package My::Schema;
15   use base qw/DBIx::Class::Schema::Loader/;
16
17   __PACKAGE__->load_from_connection(
18     dsn       => "dbi:Pg:dbname=dbname",
19     user      => "postgres",
20     password  => "",
21   );
22
23   1;
24
25 =head1 DESCRIPTION
26
27 See L<DBIx::Class::Schema::Loader>.
28
29 =head1 METHODS
30
31 =head2 new
32
33 Overrides L<DBIx::Class::Schema::Loader::Generic>'s C<new()> to default the postgres
34 schema to C<public> rather than blank.
35
36 =cut
37
38 sub new {
39     my ($class, %args) = @_;
40
41     my $self = $class->next::method(%args);
42     $self->{db_schema} ||= 'public';
43
44     $self;
45 }
46
47 sub _db_classes {
48     return qw/PK::Auto::Pg/;
49 }
50
51 sub _tables {
52     my $self = shift;
53     my $dbh = $self->schema->storage->dbh;
54     my $quoter = $dbh->get_info(29) || q{"};
55
56     # This is split out to avoid version parsing errors...
57     my $is_dbd_pg_gte_131 = ( $DBD::Pg::VERSION >= 1.31 );
58     my @tables = $is_dbd_pg_gte_131
59         ?  $dbh->tables( undef, $self->db_schema, "",
60                          "table", { noprefix => 1, pg_noprefix => 1 } )
61         : $dbh->tables;
62
63     s/$quoter//g for @tables;
64     return @tables;
65 }
66
67 sub _table_info {
68     my ( $self, $table ) = @_;
69     my $dbh = $self->schema->storage->dbh;
70     my $quoter = $dbh->get_info(29) || q{"};
71
72     my $sth = $dbh->column_info(undef, $self->db_schema, $table, undef);
73     my @cols = map { $_->[3] } @{ $sth->fetchall_arrayref };
74     s/$quoter//g for @cols;
75     
76     my @primary = $dbh->primary_key(undef, $self->db_schema, $table);
77
78     s/$quoter//g for @primary;
79
80     return ( \@cols, \@primary );
81 }
82
83 =head1 SEE ALSO
84
85 L<DBIx::Class::Schema::Loader>
86
87 =cut
88
89 1;