1 package SQL::Translator::Parser::DBI::PostgreSQL;
3 # -------------------------------------------------------------------
4 # $Id: PostgreSQL.pm,v 1.8 2005-10-06 20:33:07 scottcain 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 to query PostgreSQL system tables to determine schema structure.
40 use SQL::Translator::Schema::Constants;
42 use vars qw[ $DEBUG $VERSION @EXPORT_OK ];
43 $VERSION = sprintf "%d.%02d", q$Revision: 1.8 $ =~ /(\d+)\.(\d+)/;
44 $DEBUG = 0 unless defined $DEBUG;
46 # -------------------------------------------------------------------
48 my ( $tr, $dbh ) = @_;
50 my $schema = $tr->schema;
52 my $column_select = $dbh->prepare(
53 "SELECT a.attname, t.typname, a.attnum,a.atttypmod as length,
54 a.attnotnull, a.atthasdef, d.adsrc
55 FROM pg_type t,pg_attribute a
56 LEFT JOIN pg_attrdef d ON (d.adrelid = a.attrelid AND a.attnum = d.adnum)
57 WHERE a.attrelid=? AND attnum>0
62 my $index_select = $dbh->prepare(
63 "SELECT oid, c.relname, i.indkey, i.indnatts, i.indisunique,
64 i.indisprimary, pg_get_indexdef(oid) AS create_string
65 FROM pg_class c,pg_index i
66 WHERE c.relnamespace=2200 AND c.relkind='i'
67 AND c.oid=i.indexrelid AND i.indrelid=?"
70 my $table_select = $dbh->prepare(
71 "SELECT oid,relname FROM pg_class WHERE relnamespace IN
72 (SELECT oid FROM pg_namespace WHERE nspname='public')
75 $table_select->execute();
77 while ( my $tablehash = $table_select->fetchrow_hashref ) {
79 my $table_name = $$tablehash{'relname'};
80 my $table_oid = $$tablehash{'oid'};
82 my $table = $schema->add_table(
84 #what is type? type => $table_info->{TABLE_TYPE},
85 ) || die $schema->error;
87 $column_select->execute($table_oid);
89 while (my $columnhash = $column_select->fetchrow_hashref ) {
92 name => $$columnhash{'attname'},
93 default_value => $$columnhash{'adsrc'},
94 data_type => $$columnhash{'typename'},
95 order => $$columnhash{'attnum'},
96 size => $$columnhash{'length'},
97 nullable => $$columnhash{'attnotnull'} eq 't'
99 ) || die $table->error;
102 $index_select->execute($table_oid);
104 my @column_names = $table->field_names();
105 while (my $indexhash = $index_select->fetchrow_hashref ) {
106 #don't deal with function indexes at the moment
107 next if ($$indexhash{'indkey'} eq ''
108 or !defined($$indexhash{'indkey'}) );
111 if ($$indexhash{'indisprimary'} eq 't') {
113 } elsif ($$indexhash{'indisunique'} eq 't') {
119 my @column_ids = split /\s+/, $$indexhash{'indkey'};
121 foreach my $col (@column_ids) {
122 push @columns, $column_names[($col - 1)];
126 name => $$indexhash{'relname'},
129 ) || die $table->error;
138 # -------------------------------------------------------------------
139 # Time is a waste of money.
141 # -------------------------------------------------------------------
147 Scott Cain E<lt>cain@cshl.eduE<gt>, previous author:
148 Paul Harrington E<lt>harringp@deshaw.comE<gt>.
152 SQL::Translator, DBD::Pg.