support quoted PostgreSQL schema names with special chars (RT#64766)
[dbsrgits/DBIx-Class-Schema-Loader.git] / lib / DBIx / Class / Schema / Loader / DBI / Pg.pm
index e92fbaf..913e1d1 100644 (file)
@@ -7,9 +7,9 @@ use base qw/
     DBIx::Class::Schema::Loader::DBI
 /;
 use Carp::Clan qw/^DBIx::Class/;
-use Class::C3;
+use mro 'c3';
 
-our $VERSION = '0.07000';
+our $VERSION = '0.07010';
 
 =head1 NAME
 
@@ -41,6 +41,30 @@ sub _setup {
     if (not defined $self->preserve_case) {
         $self->preserve_case(0);
     }
+    elsif ($self->preserve_case) {
+        $self->schema->storage->sql_maker->quote_char('"');
+        $self->schema->storage->sql_maker->name_sep('.');
+    }
+}
+
+sub _tables_list {
+    my ($self, $opts) = @_;
+
+    my $dbh = $self->schema->storage->dbh;
+    my @tables = $dbh->tables(undef, $self->db_schema, '%', '%');
+
+    my $schema_quoted = $tables[0] =~ /^"/;
+
+    if ($schema_quoted) {
+        s/^"[^"]+"\.// for @tables;
+    }
+    else {
+        s/^[^.]+\.// for @tables;
+    }
+
+    s/^"([^"]+)"\z/$1/ for @tables;
+
+    return $self->_filter_tables(\@tables, $opts);
 }
 
 sub _table_uniq_info {
@@ -141,6 +165,7 @@ sub _columns_info_for {
         my $data_type = $info->{data_type};
 
         # these types are fixed size
+        # XXX should this be a negative match?
         if ($data_type =~
 /^(?:bigint|int8|bigserial|serial8|boolean|bool|box|bytea|cidr|circle|date|double precision|float8|inet|integer|int|int4|line|lseg|macaddr|money|path|point|polygon|real|float4|smallint|int2|serial|serial4|text)\z/i) {
             delete $info->{size};
@@ -219,9 +244,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 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};