Add method to populate a column extra fields
Graham Barr [Sat, 20 Feb 2010 01:04:40 +0000 (19:04 -0600)]
lib/SQL/Translator/Parser/DBI.pm
lib/SQL/Translator/Parser/DBI/MySQL.pm

index fe03f4c..1eadf91 100644 (file)
@@ -14,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',
@@ -58,6 +58,8 @@ role SQL::Translator::Parser::DBI {
 
     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) {
@@ -91,6 +93,7 @@ role SQL::Translator::Parser::DBI {
                                                                 is_auto_increment => $self->_is_auto_increment($column_info),
                                                                 is_nullable => $column_info->{NULLABLE},
                                                               });
+            $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;
index 817aaad..fbbc3bb 100644 (file)
@@ -1,7 +1,7 @@
 use MooseX::Declare;
 role SQL::Translator::Parser::DBI::MySQL {
     use MooseX::Types::Moose qw(HashRef Maybe Str);
-    use SQL::Translator::Types qw(View Table Schema);
+    use SQL::Translator::Types qw(View Table Schema Column);
 
     has 'schema_name' => (
       is      => 'rw',
@@ -30,6 +30,17 @@ role SQL::Translator::Parser::DBI::MySQL {
         return $default_value;
     }
 
+    method _column_data_type(HashRef $column_info) { $column_info->{TYPE_NAME} }
+
+    method _add_column_extra(Column $column, HashRef $column_info) {
+        $column->add_extra(unsigned => 1) if $column_info->{mysql_type_name} =~ /unsigned/;
+        $column->add_extra(zerofill => 1) if $column_info->{mysql_type_name} =~ /zerofill/;
+        $column->add_extra(values => $column_info->{mysql_values}) if $column_info->{mysql_values};
+        if (my ($size) = $column_info->{mysql_type_name} =~ /\((\d+,\d+)\)/) {
+            $column->size($size);
+        }
+    }
+
     method _add_foreign_keys(Table $table, Schema $schema) {
         my $fk_info = $self->dbh->foreign_key_info($self->catalog_name, $self->schema_name, $table->name, $self->catalog_name, $self->schema_name, undef);
         return unless $fk_info;