use DBI and a recent revision of DBD::Pg
Paul Harrington [Mon, 13 Oct 2003 19:51:31 +0000 (19:51 +0000)]
lib/SQL/Translator/Parser/DBI/PostgreSQL.pm

index 9fc3359..8722244 100644 (file)
@@ -1,7 +1,7 @@
 package SQL::Translator::Parser::DBI::PostgreSQL;
 
 # -------------------------------------------------------------------
-# $Id: PostgreSQL.pm,v 1.1 2003-10-10 15:24:04 kycl4rk Exp $
+# $Id: PostgreSQL.pm,v 1.2 2003-10-13 19:51:31 phrrngtn Exp $
 # -------------------------------------------------------------------
 # Copyright (C) 2003 Ken Y. Clark <kclark@cpan.org>.
 #
@@ -36,117 +36,76 @@ Uses DBIx::DBSchema.
 
 use strict;
 use DBI;
-use DBIx::DBSchema;
+use DBD::Pg 1.31;
 use Data::Dumper;
 use SQL::Translator::Schema::Constants;
 
 use vars qw[ $DEBUG $VERSION @EXPORT_OK ];
-$VERSION = sprintf "%d.%02d", q$Revision: 1.1 $ =~ /(\d+)\.(\d+)/;
+$VERSION = sprintf "%d.%02d", q$Revision: 1.2 $ =~ /(\d+)\.(\d+)/;
 $DEBUG   = 0 unless defined $DEBUG;
 
 # -------------------------------------------------------------------
 sub parse {
     my ( $tr, $dbh ) = @_;
 
-    my $db_schema = DBIx::DBSchema->new_native( $dbh );
+    my $schema = $tr->schema;
 
-#    warn "DBIx::DBSchema =\n", Dumper( $,b_schema ), "\n";
+    my ($sth, @tables, $columns);
+    my $stuff;
 
-    my $schema = $tr->schema;
 
-    my @table_names = $db_schema->tables;
+    if ($dbh->{FetchHashKeyName} ne 'NAME_uc') {
+#        warn "setting dbh attribute {FetchHashKeyName} to NAME_uc";
+        $dbh->{FetchHashKeyName} = 'NAME_uc';
+    }
 
-    for my $table_name ( @table_names ) {
-        my $db_table = $db_schema->table( $table_name );
+    if ($dbh->{ChopBlanks} != 1) {
+#        warn "setting dbh attribute {ChopBlanks} to 1";
+        $dbh->{ChopBlanks} = 1;
+    }
 
-        my $table =  $schema->add_table( 
-            name  => $table_name,
-        ) or die $schema->error;
+    $sth = $dbh->column_info();
+    foreach my $c (@{$sth->fetchall_arrayref({})}) {
+        $columns
+            ->{$c->{TABLE_SCHEM}}
+                ->{$c->{TABLE_NAME}}
+                    ->{columns}
+                        ->{$c->{COLUMN_NAME}}= $c;
+    }
 
-        my @col_names = $db_table->columns;
+    $sth = $dbh->table_info();
 
-        for my $col_name ( @col_names ) {
-            my $db_col      = $db_table->column( $col_name );
-            my $fname       = $db_col->name or next;
-            my $data_type   = $db_col->type or next;
-            my $size        = $db_col->length;
-            my $is_nullable = $db_col->null eq 'NULL' ? 1 : 0;
-            my $default     = $db_col->default;
+    @tables   = @{$sth->fetchall_arrayref({})};
 
-            my $field             =  $table->add_field(
-                name              => $fname,
-                data_type         => $data_type,
-                size              => $size,
-                default_value     => $default,
-                is_nullable       => $is_nullable,
-            ) or die $table->error;
+    foreach my $table_info (@tables) {
+        next
+            unless (defined($table_info->{TABLE_TYPE}));
 
-            my $pk = $db_table->primary_key;
-            $table->primary_key( $pk ) if $pk;
-        }
+        if ($table_info->{TABLE_TYPE} eq 'TABLE'&&
+            $table_info->{TABLE_SCHEM} eq 'public') {
+            my $table = $schema->add_table(
+                                           name => $table_info->{TABLE_NAME},
+                                           type => $table_info->{TABLE_TYPE},
+                                          ) || die $schema->error;
 
-#        my $indices = $dbh->selectall_arrayref(
-#            "show index from $table_name",
-#            { Columns => {} },
-#        );
-#
-#        my ( %keys, %constraints, $order );
-#        for my $index ( @$indices ) {
-#            my $table        = $index->{'table'};
-#            my $non_unique   = $index->{'non_unique'};
-#            my $key_name     = $index->{'key_name'} || '';
-#            my $seq_in_index = $index->{'seq_in_index'};
-#            my $column_name  = $index->{'column_name'};
-#            my $collation    = $index->{'collation'};
-#            my $cardinality  = $index->{'cardinality'};
-#            my $sub_part     = $index->{'sub_part'};
-#            my $packed       = $index->{'packed'};
-#            my $null         = $index->{'null'};
-#            my $index_type   = $index->{'index_type'};
-#            my $comment      = $index->{'comment'};
-#
-#            my $is_constraint = $key_name eq 'PRIMARY' || $non_unique == 0;
-#
-#            if ( $is_constraint ) {
-#                $constraints{ $key_name }{'order'} = ++$order;
-#                push @{ $constraints{ $key_name }{'fields'} }, $column_name;
-#
-#                if ( $key_name eq 'PRIMARY' ) {
-#                    $constraints{ $key_name }{'type'} = PRIMARY_KEY;
-#                }
-#                elsif ( $non_unique == 0 ) {
-#                    $constraints{ $key_name }{'type'} = UNIQUE;
-#                }
-#            }
-#            else {
-#                $keys{ $key_name }{'order'} = ++$order;
-#                push @{ $keys{ $key_name }{'fields'} }, $column_name;
-#            }
-#        }
-#
-#        for my $key_name (
-#            sort { $keys{ $a }{'order'} <=> $keys{ $b }{'order'} }
-#            keys %keys
-#        ) {
-#            my $key    = $keys{ $key_name };
-#            my $index  =  $table->add_index(
-#                name   => $key_name,
-#                type   => NORMAL,
-#                fields => $key->{'fields'},
-#            ) or die $table->error;
-#        }
-#    
-#        for my $constraint_name (
-#            sort { $constraints{ $a }{'order'} <=> $constraints{ $b }{'order'} }
-#            keys %constraints
-#        ) {
-#            my $def        = $constraints{ $constraint_name };
-#            my $constraint =  $table->add_constraint(
-#                name       => $constraint_name,
-#                type       => $def->{'type'},
-#                fields     => $def->{'fields'},
-#            ) or die $table->error;
-#        }
+
+            my $cols =
+                $columns->{$table_info->{TABLE_SCHEM}}
+                        ->{$table_info->{TABLE_NAME}}
+                            ->{columns};
+
+            foreach my $c (values %{$cols}) {
+                my $f = $table->add_field(
+                                          name        => $c->{COLUMN_NAME},
+                                          data_type   => $c->{TYPE_NAME},
+                                          order       => $c->{ORDINAL_POSITION},
+                                          size        => $c->{COLUMN_SIZE},
+                                         ) || die $table->error;
+
+                $f->is_nullable(1)
+                    if ($c->{NULLABLE} == 1);
+            }
+        }
     }
 
     return 1;