1 package SQL::Translator::Parser::DBI::PostgreSQL;
3 # -------------------------------------------------------------------
4 # $Id: PostgreSQL.pm,v 1.7 2004-09-15 21:13:43 schiffbruechige Exp $
5 # -------------------------------------------------------------------
6 # Copyright (C) 2002-4 SQLFairy Authors
8 # This program is free software; you can redistribute it and/or
9 # modify it under the terms of the GNU General Public License as
10 # published by the Free Software Foundation; version 2.
12 # This program is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 # General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with this program; if not, write to the Free Software
19 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
21 # -------------------------------------------------------------------
25 SQL::Translator::Parser::DBI::PostgreSQL - parser for DBD::Pg
29 See SQL::Translator::Parser::DBI.
33 Uses DBI methods to determine schema structure. DBI, of course,
34 delegates to DBD::Pg, and versions < 1.31 of DBD::Pg don't return very
35 useful information. It is recommended that you upgrade this module.
42 use SQL::Translator::Schema::Constants;
44 use vars qw[ $DEBUG $VERSION @EXPORT_OK ];
45 $VERSION = sprintf "%d.%02d", q$Revision: 1.7 $ =~ /(\d+)\.(\d+)/;
46 $DEBUG = 0 unless defined $DEBUG;
48 # -------------------------------------------------------------------
50 my ( $tr, $dbh ) = @_;
52 my $schema = $tr->schema;
54 my ($sth, @tables, $columns);
57 warn "DBD:Pg $DBD::Pg::VERSION is not likely to produce anything ".
58 "useful. Upgrade to 1.31 or better if available.\n"
59 unless ($DBD::Pg::VERSION ge '1.31');
61 if ($dbh->{FetchHashKeyName} ne 'NAME_uc') {
62 $dbh->{FetchHashKeyName} = 'NAME_uc';
65 if ($dbh->{ChopBlanks} != 1) {
66 $dbh->{ChopBlanks} = 1;
69 $sth = $dbh->table_info();
71 @tables = @{$sth->fetchall_arrayref({})};
73 foreach my $table_info (@tables) {
75 unless (defined($table_info->{TABLE_TYPE}));
77 if ($table_info->{TABLE_TYPE} eq 'TABLE'&&
78 $table_info->{TABLE_SCHEM} eq 'public') {
79 my $table = $schema->add_table(
80 name => $table_info->{TABLE_NAME},
81 type => $table_info->{TABLE_TYPE},
82 ) || die $schema->error;
85 my $cols = $dbh->column_info(undef,
86 $table_info->{TABLE_SCHEM},
87 $table_info->{TABLE_NAME},
88 "%" )->fetchall_arrayref({});
90 foreach my $c (@{$cols}) {
91 print Dumper($c) if $DEBUG;
92 my $f = $table->add_field(
93 name => $c->{COLUMN_NAME},
94 default_value => $c->{COLUMN_DEF},
95 data_type => $c->{TYPE_NAME},
96 order => $c->{ORDINAL_POSITION},
97 size => $c->{COLUMN_SIZE},
98 ) || die $table->error;
101 $f->is_nullable($c->{NULLABLE} == 1);
111 # -------------------------------------------------------------------
112 # Time is a waste of money.
114 # -------------------------------------------------------------------
120 Paul Harrington E<lt>harringp@deshaw.comE<gt>.
124 SQL::Translator, DBD::Pg.