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.
21 use SQL::Translator::Schema::Constants;
23 our ( $DEBUG, @EXPORT_OK );
24 our $VERSION = '1.59';
25 $DEBUG = 0 unless defined $DEBUG;
27 my $actions = {c => 'cascade',
35 my ( $tr, $dbh ) = @_;
37 my $schema = $tr->schema;
39 my $column_select = $dbh->prepare(
40 "SELECT a.attname, format_type(t.oid, a.atttypmod) as typname, a.attnum,
41 a.atttypmod as length, a.attnotnull, a.atthasdef, ad.adsrc,
43 FROM pg_type t, pg_attribute a
44 LEFT JOIN pg_attrdef ad ON (ad.adrelid = a.attrelid AND a.attnum = ad.adnum)
45 LEFT JOIN pg_description d ON (a.attrelid=d.objoid AND a.attnum=d.objsubid)
46 WHERE a.attrelid=? AND attnum>0
51 my $index_select = $dbh->prepare(
52 "SELECT oid, c.relname, i.indkey, i.indnatts, i.indisunique,
53 ARRAY(SELECT a.attname
55 WHERE a.attrelid=i.indrelid AND a.attnum = ANY(i.indkey)
57 i.indisprimary, pg_get_indexdef(oid) AS create_string
58 FROM pg_class c,pg_index i
59 WHERE c.relnamespace IN (SELECT oid FROM pg_namespace WHERE nspname='public') AND c.relkind='i'
60 AND c.oid=i.indexrelid AND i.indrelid=?"
63 my $table_select = $dbh->prepare(
64 "SELECT c.oid, c.relname, d.description
66 LEFT JOIN pg_description d ON c.oid=d.objoid AND d.objsubid=0
68 (SELECT oid FROM pg_namespace WHERE nspname='public')
72 my $fk_select = $dbh->prepare(
76 d.relname AS frelname,
78 ARRAY(SELECT column_name::varchar
79 FROM information_schema.columns
80 WHERE ordinal_position = ANY (r.conkey)
81 AND table_schema = n.nspname
82 AND table_name = c.relname ) AS fields,
84 ARRAY(SELECT column_name::varchar
85 FROM information_schema.columns
86 WHERE ordinal_position = ANY (r.confkey)
87 AND table_schema = n.nspname
88 AND table_name = d.relname ) AS reference_fields,
93 FROM pg_catalog.pg_constraint r
95 JOIN pg_catalog.pg_class c
99 JOIN pg_catalog.pg_class d
100 ON d.oid = r.confrelid
102 JOIN pg_catalog.pg_namespace n
103 ON n.oid = c.relnamespace
105 WHERE pg_catalog.pg_table_is_visible(c.oid)
109 /) or die "Can't prepare: $@";
111 $table_select->execute();
113 while ( my $tablehash = $table_select->fetchrow_hashref ) {
115 my $table_name = $$tablehash{'relname'};
116 my $table_oid = $$tablehash{'oid'};
117 my $table = $schema->add_table(
119 #what is type? type => $table_info->{TABLE_TYPE},
120 ) || die $schema->error;
122 $table->comments($$tablehash{'description'}) if $$tablehash{'description'};
124 $column_select->execute($table_oid);
126 while (my $columnhash = $column_select->fetchrow_hashref ) {
128 #data_type seems to not be populated; perhaps there needs to
129 #be a mapping of query output to reserved constants in sqlt?
131 my $col = $table->add_field(
132 name => $$columnhash{'attname'},
133 default_value => $$columnhash{'adsrc'},
134 data_type => $$columnhash{'typname'},
135 order => $$columnhash{'attnum'},
136 ) || die $table->error;
138 $col->{size} = [$$columnhash{'length'}]
139 if $$columnhash{'length'}>0 && $$columnhash{'length'}<=0xFFFF;
140 $col->{is_nullable} = $$columnhash{'attnotnull'} ? 0 : 1;
141 $col->comments($$columnhash{'description'}) if $$columnhash{'description'};
144 $index_select->execute($table_oid);
146 my @column_names = $table->field_names();
147 while (my $indexhash = $index_select->fetchrow_hashref ) {
148 #don't deal with function indexes at the moment
149 next if ($$indexhash{'indkey'} eq ''
150 or !defined($$indexhash{'indkey'}) );
153 if ($$indexhash{'indisprimary'}) {
154 $type = UNIQUE; #PRIMARY_KEY;
156 #tell sqlt that this is the primary key:
157 my $col_name=$column_names[($$indexhash{'indkey'} - 1)];
158 $table->get_field($col_name)->{is_primary_key}=1;
160 } elsif ($$indexhash{'indisunique'}) {
167 my @column_ids = split /\s+/, $$indexhash{'indkey'};
168 my @columns = split /\s+/, $$indexhash{'attname'};
171 name => $$indexhash{'relname'},
174 ) || die $table->error;
177 $fk_select->execute('public',$table_name) or die "Can't execute: $@";
178 my $fkeys = $fk_select->fetchall_arrayref({});
179 $DEBUG and print Dumper $fkeys;
180 for my $con (@$fkeys){
181 my $con_name = $con->{conname};
182 my $fields = $con->{fields};
183 my $reference_fields = $con->{reference_fields};
184 my $reference_table = $con->{frelname};
185 my $on_upd = $con->{confupdtype};
186 my $on_del = $con->{confdeltype};
187 $table->add_constraint(
189 type => 'foreign_key',
191 reference_fields => $reference_fields,
192 reference_table => $reference_table,
193 on_delete => $actions->{$on_upd},
194 on_update => $actions->{$on_del},
205 # -------------------------------------------------------------------
206 # Time is a waste of money.
208 # -------------------------------------------------------------------
214 Scott Cain E<lt>cain@cshl.eduE<gt>, previous author:
215 Paul Harrington E<lt>harringp@deshaw.comE<gt>.
219 SQL::Translator, DBD::Pg.