more work on multi-db_schema
[dbsrgits/DBIx-Class-Schema-Loader.git] / lib / DBIx / Class / Schema / Loader / DBI / Pg.pm
index f5eefbb..4bb5d23 100644 (file)
@@ -6,28 +6,19 @@ use base qw/
     DBIx::Class::Schema::Loader::DBI::Component::QuotedDefault
     DBIx::Class::Schema::Loader::DBI
 /;
-use Carp::Clan qw/^DBIx::Class/;
-use Class::C3;
+use mro 'c3';
+use DBIx::Class::Schema::Loader::Table ();
 
-our $VERSION = '0.07001';
+our $VERSION = '0.07010';
 
 =head1 NAME
 
 DBIx::Class::Schema::Loader::DBI::Pg - DBIx::Class::Schema::Loader::DBI
 PostgreSQL Implementation.
 
-=head1 SYNOPSIS
-
-  package My::Schema;
-  use base qw/DBIx::Class::Schema::Loader/;
-
-  __PACKAGE__->loader_options( debug => 1 );
-
-  1;
-
 =head1 DESCRIPTION
 
-See L<DBIx::Class::Schema::Loader::Base>.
+See L<DBIx::Class::Schema::Loader> and L<DBIx::Class::Schema::Loader::Base>.
 
 =cut
 
@@ -36,7 +27,7 @@ sub _setup {
 
     $self->next::method(@_);
 
-    $self->{db_schema} ||= 'public';
+    $self->{db_schema} ||= ['public'];
 
     if (not defined $self->preserve_case) {
         $self->preserve_case(0);
@@ -47,6 +38,38 @@ sub _setup {
     }
 }
 
+sub _tables_list {
+    my ($self, $opts) = @_;
+
+    my $dbh = $self->schema->storage->dbh;
+
+    my @tables;
+
+    foreach my $schema (@{ $self->db_schema }) {
+        my @raw_tables = $dbh->tables(undef, $schema, '%', '%');
+
+        foreach my $table_name (@raw_tables) {
+            my $schema_quoted = $table_name =~ /^"/;
+
+            if ($schema_quoted) {
+                $table_name =~ s/^"([^"]+)"\.//;
+            }
+            else {
+                $table_name =~ s/^([^.]+)\.//;
+            }
+            my $table = DBIx::Class::Schema::Loader::Table->new(schema => $1);
+
+            $table_name =~ s/^"([^"]+)"\z/$1/;
+
+            $table->name($table_name);
+
+            push @tables, $table;
+        }
+    }
+
+    return $self->_filter_tables(\@tables, $opts);
+}
+
 sub _table_uniq_info {
     my ($self, $table) = @_;
 
@@ -83,7 +106,7 @@ sub _table_uniq_info {
           c.relname     = ?}
     );
 
-    $uniq_sth->execute($self->db_schema, $table);
+    $uniq_sth->execute($table->schema, $table);
     while(my $row = $uniq_sth->fetchrow_arrayref) {
         my ($tableid, $indexname, $col_nums) = @$row;
         $col_nums =~ s/^\s+//;
@@ -114,7 +137,7 @@ sub _table_comment {
             FROM pg_class 
             WHERE relname=? AND relnamespace=(
                 SELECT oid FROM pg_namespace WHERE nspname=?)
-        }, undef, $table, $self->db_schema
+        }, undef, $table, $table->schema
         );   
     return $table_comment
 }
@@ -127,7 +150,7 @@ sub _column_comment {
             FROM pg_class 
             WHERE relname=? AND relnamespace=(
                 SELECT oid FROM pg_namespace WHERE nspname=?)
-        }, undef, $table, $self->db_schema
+        }, undef, $table, $table->schema
         );   
     return $self->schema->storage->dbh->selectrow_array('SELECT col_description(?,?)', undef, $table_oid,
     $column_number );
@@ -224,9 +247,38 @@ EOF
         elsif (lc($data_type) eq 'character') {
             $info->{data_type} = 'char';
         }
+        else {
+            my ($typetype) = $self->schema->storage->dbh
+                ->selectrow_array(<<EOF, {}, $data_type);
+SELECT typtype
+FROM pg_catalog.pg_type
+WHERE typname = ?
+EOF
+            if ($typetype && $typetype eq 'e') {
+                # The following will extract a list of allowed values for the
+                # enum.
+                my $typevalues = $self->schema->storage->dbh
+                    ->selectall_arrayref(<<EOF, {}, $info->{data_type});
+SELECT e.enumlabel
+FROM pg_catalog.pg_enum e
+JOIN pg_catalog.pg_type t ON t.oid = e.enumtypid
+WHERE t.typname = ?
+EOF
+
+                $info->{extra}{list} = [ map { $_->[0] } @$typevalues ];
+
+                # Store its original name in extra for SQLT to pick up.
+                $info->{extra}{custom_type_name} = $info->{data_type};
+
+                $info->{data_type} = 'enum';
+                
+                delete $info->{size};
+            }
+        }
 
 # process SERIAL columns
-        if (ref($info->{default_value}) eq 'SCALAR' && ${ $info->{default_value} } =~ /\bnextval\(['"](\w+)/i) {
+        if (ref($info->{default_value}) eq 'SCALAR'
+                && ${ $info->{default_value} } =~ /\bnextval\('([^:]+)'/i) {
             $info->{is_auto_increment} = 1;
             $info->{sequence}          = $1;
             delete $info->{default_value};