Add method to populate a column extra fields
[dbsrgits/SQL-Translator-2.0-ish.git] / lib / SQL / Translator / Parser / DBI.pm
index 8372ccf..1eadf91 100644 (file)
@@ -5,6 +5,7 @@ role SQL::Translator::Parser::DBI {
     use DBI::Const::GetInfoReturn;
 
     use MooseX::Types::Moose qw(HashRef Maybe Str);
+    use MooseX::MultiMethods;
 
     use SQL::Translator::Object::Column;
     use SQL::Translator::Object::ForeignKey;
@@ -13,7 +14,7 @@ role SQL::Translator::Parser::DBI {
     use SQL::Translator::Object::Table;
     use SQL::Translator::Object::View;
 
-    use SQL::Translator::Types qw(Schema Table);
+    use SQL::Translator::Types qw(Schema Table Column);
 
     has 'quoter' => (
         is => 'rw',
@@ -43,7 +44,7 @@ role SQL::Translator::Parser::DBI {
         default => undef
     );
 
-     method _subclass {
+    method _subclass {
         my $dbtype = $self->dbh->get_info($GetInfoType{SQL_DBMS_NAME}) || $self->dbh->{Driver}{Name};
 
         my $class = __PACKAGE__ . '::'. $dbtype;
@@ -51,16 +52,20 @@ role SQL::Translator::Parser::DBI {
         $class->meta->apply($self);
     }
 
-    method _is_auto_increment { 0 }
+    method _is_auto_increment(HashRef $column_info) { 0 }
 
-    method _column_default_value(HashRef $column_info) { return $column_info->{COLUMN_DEF}; }
+    method _column_default_value(HashRef $column_info) { $column_info->{COLUMN_DEF} }
+
+    method _column_data_type(HashRef $column_info) { $column_info->{DATA_TYPE} }
+
+    method _add_column_extra(Column $column, HashRef $column_info) { return }
 
     method _add_tables(Schema $schema) {
         my $sth = $self->dbh->table_info($self->catalog_name, $self->schema_name, '%', "TABLE,VIEW,'LOCAL TEMPORARY','GLOBAL TEMPORARY'");
         while (my $table_info = $sth->fetchrow_hashref) {
             if ($table_info->{TABLE_TYPE} =~ /^(TABLE|LOCAL TEMPORARY|GLOBAL TEMPORARY)$/) {
                 my $temp = $table_info->{TABLE_TYPE} =~ /TEMPORARY$/ ? 1 : 0;
-                my $table = SQL::Translator::Object::Table->new({ name => $table_info->{TABLE_NAME}, temporary => $temp });
+                my $table = SQL::Translator::Object::Table->new({ name => $table_info->{TABLE_NAME}, temporary => $temp, schema => $schema });
                 $schema->add_table($table);
 
                 $self->_add_columns($table);
@@ -79,16 +84,19 @@ role SQL::Translator::Parser::DBI {
 
     method _add_columns(Table $table) {
         my $sth = $self->dbh->column_info($self->catalog_name, $self->schema_name, $table->name, '%');
+        my @columns;
         while (my $column_info = $sth->fetchrow_hashref) {
             my $column = SQL::Translator::Object::Column->new({ name => $column_info->{COLUMN_NAME},
-                                                                data_type => $column_info->{DATA_TYPE},
+                                                                data_type => $self->_column_data_type($column_info),
                                                                 size => $column_info->{COLUMN_SIZE},
                                                                 default_value => $self->_column_default_value($column_info),
                                                                 is_auto_increment => $self->_is_auto_increment($column_info),
                                                                 is_nullable => $column_info->{NULLABLE},
                                                               });
-            $table->add_column($column);
+            $self->_add_column_extra($column, $column_info);
+            push @columns, { column => $column, pos =>  $column_info->{ORDINAL_POSITION} || $#columns };
         }
+        $table->add_column($_->{column}) for sort { $a->{pos} <=> $b->{pos} } @columns;
     }
 
     method _add_primary_key(Table $table) {
@@ -129,6 +137,8 @@ role SQL::Translator::Parser::DBI {
     method _add_indexes(Table $table) {
         my $index_info = $self->dbh->statistics_info($self->catalog_name, $self->schema_name, $table->name, 1, 0);
 
+        return unless defined $index_info;
+
         my ($index_name, $index_type, @index_cols);
         while (my $index_col = $index_info->fetchrow_hashref) {
             $index_name = $index_col->{INDEX_NAME};
@@ -140,4 +150,10 @@ role SQL::Translator::Parser::DBI {
         $index->add_column($table->get_column($_)) for @index_cols;
         $table->add_index($index);
     }
+
+    multi method parse(Schema $data) { $data }
+
+    multi method parse(Any $) {
+        $self->_add_tables($self->schema);
+    }
 }