1 package SQL::Translator::Parser::DBI::PostgreSQL;
5 SQL::Translator::Parser::DBI::PostgreSQL - parser for DBD::Pg
9 See SQL::Translator::Parser::DBI.
13 Uses DBI to query PostgreSQL system tables to determine schema structure.
20 use SQL::Translator::Schema::Constants;
22 use vars qw[ $DEBUG $VERSION @EXPORT_OK ];
24 $DEBUG = 0 unless defined $DEBUG;
26 my $actions = {c => 'cascade',
34 my ( $tr, $dbh ) = @_;
36 my $schema = $tr->schema;
38 my $column_select = $dbh->prepare(
39 "SELECT a.attname, format_type(t.oid, a.atttypmod) as typname, a.attnum,
40 a.atttypmod as length, a.attnotnull, a.atthasdef, ad.adsrc,
42 FROM pg_type t, pg_attribute a
43 LEFT JOIN pg_attrdef ad ON (ad.adrelid = a.attrelid AND a.attnum = ad.adnum)
44 LEFT JOIN pg_description d ON (a.attrelid=d.objoid AND a.attnum=d.objsubid)
45 WHERE a.attrelid=? AND attnum>0
50 my $index_select = $dbh->prepare(
51 "SELECT oid, c.relname, i.indkey, i.indnatts, i.indisunique,
52 ARRAY(SELECT a.attname
54 WHERE a.attrelid=i.indrelid AND a.attnum = ANY(i.indkey)
56 i.indisprimary, pg_get_indexdef(oid) AS create_string
57 FROM pg_class c,pg_index i
58 WHERE c.relnamespace IN (SELECT oid FROM pg_namespace WHERE nspname='public') AND c.relkind='i'
59 AND c.oid=i.indexrelid AND i.indrelid=?"
62 my $table_select = $dbh->prepare(
63 "SELECT c.oid, c.relname, d.description
65 LEFT JOIN pg_description d ON c.oid=d.objoid AND d.objsubid=0
67 (SELECT oid FROM pg_namespace WHERE nspname='public')
71 my $fk_select = $dbh->prepare(
75 d.relname AS frelname,
77 ARRAY(SELECT column_name::varchar
78 FROM information_schema.columns
79 WHERE ordinal_position = ANY (r.conkey)
80 AND table_schema = n.nspname
81 AND table_name = c.relname ) AS fields,
83 ARRAY(SELECT column_name::varchar
84 FROM information_schema.columns
85 WHERE ordinal_position = ANY (r.confkey)
86 AND table_schema = n.nspname
87 AND table_name = d.relname ) AS reference_fields,
92 FROM pg_catalog.pg_constraint r
94 JOIN pg_catalog.pg_class c
98 JOIN pg_catalog.pg_class d
99 ON d.oid = r.confrelid
101 JOIN pg_catalog.pg_namespace n
102 ON n.oid = c.relnamespace
104 WHERE pg_catalog.pg_table_is_visible(c.oid)
108 /) or die "Can't prepare: $@";
110 $table_select->execute();
112 while ( my $tablehash = $table_select->fetchrow_hashref ) {
114 my $table_name = $$tablehash{'relname'};
115 my $table_oid = $$tablehash{'oid'};
116 my $table = $schema->add_table(
118 #what is type? type => $table_info->{TABLE_TYPE},
119 ) || die $schema->error;
121 $table->comments($$tablehash{'description'}) if $$tablehash{'description'};
123 $column_select->execute($table_oid);
125 while (my $columnhash = $column_select->fetchrow_hashref ) {
127 #data_type seems to not be populated; perhaps there needs to
128 #be a mapping of query output to reserved constants in sqlt?
130 my $col = $table->add_field(
131 name => $$columnhash{'attname'},
132 default_value => $$columnhash{'adsrc'},
133 data_type => $$columnhash{'typname'},
134 order => $$columnhash{'attnum'},
135 ) || die $table->error;
137 $col->{size} = [$$columnhash{'length'}]
138 if $$columnhash{'length'}>0 && $$columnhash{'length'}<=0xFFFF;
139 $col->{is_nullable} = $$columnhash{'attnotnull'} ? 0 : 1;
140 $col->comments($$columnhash{'description'}) if $$columnhash{'description'};
143 $index_select->execute($table_oid);
145 my @column_names = $table->field_names();
146 while (my $indexhash = $index_select->fetchrow_hashref ) {
147 #don't deal with function indexes at the moment
148 next if ($$indexhash{'indkey'} eq ''
149 or !defined($$indexhash{'indkey'}) );
152 if ($$indexhash{'indisprimary'}) {
153 $type = UNIQUE; #PRIMARY_KEY;
155 #tell sqlt that this is the primary key:
156 my $col_name=$column_names[($$indexhash{'indkey'} - 1)];
157 $table->get_field($col_name)->{is_primary_key}=1;
159 } elsif ($$indexhash{'indisunique'}) {
166 my @column_ids = split /\s+/, $$indexhash{'indkey'};
167 my @columns = split /\s+/, $$indexhash{'attname'};
170 name => $$indexhash{'relname'},
173 ) || die $table->error;
176 $fk_select->execute('public',$table_name) or die "Can't execute: $@";
177 my $fkeys = $fk_select->fetchall_arrayref({});
178 $DEBUG and print Dumper $fkeys;
179 for my $con (@$fkeys){
180 my $con_name = $con->{conname};
181 my $fields = $con->{fields};
182 my $reference_fields = $con->{reference_fields};
183 my $reference_table = $con->{frelname};
184 my $on_upd = $con->{confupdtype};
185 my $on_del = $con->{confdeltype};
186 $table->add_constraint(
188 type => 'foreign_key',
190 reference_fields => $reference_fields,
191 reference_table => $reference_table,
192 on_delete => $actions->{$on_upd},
193 on_update => $actions->{$on_del},
204 # -------------------------------------------------------------------
205 # Time is a waste of money.
207 # -------------------------------------------------------------------
213 Scott Cain E<lt>cain@cshl.eduE<gt>, previous author:
214 Paul Harrington E<lt>harringp@deshaw.comE<gt>.
218 SQL::Translator, DBD::Pg.