Add support for REMARKS in *_info to use for *_comments
[dbsrgits/DBIx-Class-Schema-Loader.git] / lib / DBIx / Class / Schema / Loader / DBI.pm
index baf1372..191c1d0 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.07015';
 
 __PACKAGE__->mk_group_accessors('simple', qw/
     _disable_pk_detection
@@ -324,29 +324,47 @@ sub _table_uniq_info {
 sub _table_comment {
     my ($self, $table) = @_;
 
-    my $comments_table = $self->table_comments_table;
+    my $comments_table = $table->clone;
+    $comments_table->name($self->table_comments_table);
 
-    my ($comment) = try { $self->dbh->selectrow_array(<<"EOF", {}, $table->name) };
+    my ($comment) = try { $self->dbh->selectrow_array(<<"EOF") };
 SELECT comment_text
-FROM $comments_table
-WHERE table_name = ?
+FROM @{[ $comments_table->sql_name ]}
+WHERE table_name = @{[ $self->dbh->quote($table->name) ]}
 EOF
 
+    # Failback: try the REMARKS column on table_info
+    my $dbh = $self->dbh;
+    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_counter, $column_name) = @_;
+    my ($self, $table, $column_number, $column_name) = @_;
 
-    my $comments_table = $self->column_comments_table;
+    my $comments_table = $table->clone;
+    $comments_table->name($self->column_comments_table);
 
-    my ($comment) = try { $self->dbh->selectrow_array(<<"EOF", {}, $table->name, $column_name) };
+    my ($comment) = try { $self->dbh->selectrow_array(<<"EOF") };
 SELECT comment_text
-FROM $comments_table
-WHERE table_name = ?
-AND column_name = ?
+FROM @{[ $comments_table->sql_name ]}
+WHERE table_name = @{[ $self->dbh->quote($table->name) ]}
+AND column_name = @{[ $self->dbh->quote($column_name) ]}
 EOF
-        
+
+    # Failback: try the REMARKS column on column_info
+    my $dbh = $self->dbh;
+    if (!$comment && $dbh->can('column_info')) {
+        my $sth = $self->_dbh_column_info( $dbh, undef, $table->schema, $table->name, $column_name );
+        my $info = $sth->fetchrow_hashref();
+        $comment = $info->{REMARKS};
+    }
+
     return $comment;
 }
 
@@ -504,7 +522,9 @@ sub _columns_info_for {
 sub _dbh_type_info_type_name {
     my ($self, $type_num) = @_;
 
-    my $type_info = $self->dbh->type_info($type_num);
+    # We wrap it in a try block for MSSQL+DBD::Sybase, which can have issues.
+    # TODO investigate further
+    my $type_info = try { $self->dbh->type_info($type_num) };
     
     return $type_info ? $type_info->{TYPE_NAME} : undef;
 }
@@ -512,6 +532,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);