fix a spelling mistake and abstract some code into functions
Justin Hunter [Wed, 8 Jul 2009 01:46:36 +0000 (18:46 -0700)]
lib/SQL/Translator/Parser/DBI.pm
lib/SQL/Translator/Parser/DBI/PostgreSQL.pm

index 6937049..cbc2f14 100644 (file)
@@ -13,7 +13,7 @@ use aliased 'SQL::Translator::Object::View';
 has 'quoter' => (
     is => 'rw',
     isa => Str,
-    requried => 1,
+    required => 1,
     lazy => 1,
     default => sub { shift->dbh->get_info(29) || q{"} }
 );
@@ -52,6 +52,15 @@ sub _subclass {
     $class->meta->apply($self);
 }
 
+sub _is_auto_increment { 0 }
+
+sub _column_default_value {
+    my $self = shift;
+    my $column_info = shift;
+
+    return $column_info->{COLUMN_DEF};
+}
+
 sub _add_tables {
     my $self = shift;
     my $schema = shift;
@@ -78,12 +87,14 @@ sub _add_columns {
     my $table = shift;
 
     my $sth = $self->dbh->column_info($self->catalog_name, $self->schema_name, $table->name, '%');
-    while (my $col_info = $sth->fetchrow_hashref) {
-        my $column = Column->new({ name => $col_info->{COLUMN_NAME},
-                                   data_type => $col_info->{TYPE_NAME},
-                                   size => $col_info->{COLUMN_SIZE},
-                                   default_value => $col_info->{COLUMN_DEF},
-                                   is_nullable => $col_info->{NULLABLE}, });
+    while (my $column_info = $sth->fetchrow_hashref) {
+        my $column = Column->new({ name => $column_info->{COLUMN_NAME},
+                                   data_type => $column_info->{DATA_TYPE},
+                                   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);
     }
 }
index 065166b..ba2e8c1 100644 (file)
@@ -18,4 +18,22 @@ sub _get_view_sql {
     return $sql;
 }
 
+sub _is_auto_increment {
+    my $self = shift;
+    my $column_info = shift;
+
+    return $column_info->{COLUMN_DEF} && $column_info->{COLUMN_DEF} =~ /^nextval\(/ ? 1 : 0;
+}
+
+sub _column_default_value {
+    my $self = shift;
+    my $column_info = shift;
+    my $default_value = $column_info->{COLUMN_DEF};
+
+    if (defined $default_value) {
+        $default_value =~ s/::.*$//
+    }
+    return $default_value;
+}
+
 1;