release 0.07018
[dbsrgits/DBIx-Class-Schema-Loader.git] / lib / DBIx / Class / Schema / Loader / DBI.pm
index e584e6d..91a6c7e 100644 (file)
@@ -9,7 +9,7 @@ use List::MoreUtils 'any';
 use namespace::clean;
 use DBIx::Class::Schema::Loader::Table ();
 
-our $VERSION = '0.07010';
+our $VERSION = '0.07018';
 
 __PACKAGE__->mk_group_accessors('simple', qw/
     _disable_pk_detection
@@ -256,10 +256,12 @@ sub _table_columns {
 
     my $sth = $self->_sth_for($table, undef, \'1 = 0');
     $sth->execute;
-    my $retval = $self->preserve_case ? \@{$sth->{NAME}} : \@{$sth->{NAME_lc}};
+
+    my $retval = [ map $self->_lc($_), @{$sth->{NAME}} ];
+
     $sth->finish;
 
-    $retval;
+    return $retval;
 }
 
 # Returns arrayref of pk col names
@@ -323,32 +325,53 @@ sub _table_uniq_info {
 
 sub _table_comment {
     my ($self, $table) = @_;
+    my $dbh = $self->dbh;
 
     my $comments_table = $table->clone;
     $comments_table->name($self->table_comments_table);
 
-    my ($comment) = try { $self->dbh->selectrow_array(<<"EOF") };
+    my ($comment) =
+        (exists $self->_tables->{$comments_table->sql_name} || undef)
+        && try { $dbh->selectrow_array(<<"EOF") };
 SELECT comment_text
 FROM @{[ $comments_table->sql_name ]}
-WHERE table_name = @{[ $self->dbh->quote($table->name) ]}
+WHERE table_name = @{[ $dbh->quote($table->name) ]}
 EOF
 
+    # Failback: try the REMARKS column on table_info
+    if (!$comment && $dbh->can('table_info')) {
+        my $sth = $self->_dbh_table_info( $dbh, undef, $table->schema, $table->name );
+        my $info = $sth->fetchrow_hashref();
+        $comment = $info->{REMARKS};
+    }
+
     return $comment;
 }
 
 sub _column_comment {
     my ($self, $table, $column_number, $column_name) = @_;
+    my $dbh = $self->dbh;
 
     my $comments_table = $table->clone;
     $comments_table->name($self->column_comments_table);
 
-    my ($comment) = try { $self->dbh->selectrow_array(<<"EOF") };
+    my ($comment) =
+        (exists $self->_tables->{$comments_table->sql_name} || undef)
+        && try { $dbh->selectrow_array(<<"EOF") };
 SELECT comment_text
 FROM @{[ $comments_table->sql_name ]}
-WHERE table_name = @{[ $self->dbh->quote($table->name) ]}
-AND column_name = @{[ $self->dbh->quote($column_name) ]}
+WHERE table_name = @{[ $dbh->quote($table->name) ]}
+AND column_name = @{[ $dbh->quote($column_name) ]}
 EOF
-        
+
+    # Failback: try the REMARKS column on column_info
+    if (!$comment && $dbh->can('column_info')) {
+        if (my $sth = try { $self->_dbh_column_info( $dbh, undef, $table->schema, $table->name, $column_name ) }) {
+            my $info = $sth->fetchrow_hashref();
+            $comment = $info->{REMARKS};
+        }
+    }
+
     return $comment;
 }
 
@@ -382,7 +405,7 @@ sub _table_fk_info {
         my $relid   = ($raw_rel->[11] || ( "__dcsld__" . $i++ ));
 
         foreach my $var ($uk_scm, $uk_tbl, $uk_col, $fk_scm, $fk_col, $relid) {
-            $var =~ s/[\Q$self->{quote_char}\E]//g;
+            $var =~ s/[\Q$self->{quote_char}\E]//g if defined $var;
         }
 
         if ($self->db_schema && $self->db_schema->[0] ne '%'
@@ -423,9 +446,10 @@ sub _columns_info_for {
 
     my %result;
 
-    if ($dbh->can('column_info')) {
-        my $sth = $self->_dbh_column_info($dbh, undef, $table->schema, $table->name, '%' );
-        while ( my $info = $sth->fetchrow_hashref() ){
+    if (my $sth = try { $self->_dbh_column_info($dbh, undef, $table->schema, $table->name, '%' ) }) {
+        COL_INFO: while (my $info = try { $sth->fetchrow_hashref } catch { +{} }) {
+            next COL_INFO unless %$info;
+
             my $column_info = {};
             $column_info->{data_type}     = lc $info->{TYPE_NAME};
 
@@ -453,8 +477,6 @@ sub _columns_info_for {
             $result{$col_name} = $column_info;
         }
         $sth->finish;
-
-        return \%result if %result;
     }
 
     my $sth = $self->_sth_for($table, undef, \'1 = 0');
@@ -462,7 +484,9 @@ sub _columns_info_for {
 
     my @columns = @{ $sth->{NAME} };
 
-    for my $i (0 .. $#columns) {
+    COL: for my $i (0 .. $#columns) {
+        next COL if %{ $result{ $self->_lc($columns[$i]) }||{} };
+
         my $column_info = {};
         $column_info->{data_type} = lc $sth->{TYPE}[$i];
 
@@ -516,6 +540,13 @@ sub _dbh_type_info_type_name {
 # do not use this, override _columns_info_for instead
 sub _extra_column_info {}
 
+# override to mask warnings if needed
+sub _dbh_table_info {
+    my ($self, $dbh) = (shift, shift);
+
+    return $dbh->table_info(@_);
+}
+
 # override to mask warnings if needed (see mysql)
 sub _dbh_column_info {
     my ($self, $dbh) = (shift, shift);