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