File Coverage

blib/lib/SQL/Translator/Parser/DBI/PostgreSQL.pm
Criterion Covered Total %
statement 15 58 25.8
branch 0 22 0.0
condition 0 10 0.0
subroutine 5 6 83.3
pod 0 1 0.0
total 20 97 20.6


line stmt bran cond sub pod time code
1             package SQL::Translator::Parser::DBI::PostgreSQL;
2              
3             =head1 NAME
4              
5             SQL::Translator::Parser::DBI::PostgreSQL - parser for DBD::Pg
6              
7             =head1 SYNOPSIS
8              
9             See SQL::Translator::Parser::DBI.
10              
11             =head1 DESCRIPTION
12              
13             Uses DBI to query PostgreSQL system tables to determine schema structure.
14              
15             =cut
16              
17 1     1   6 use strict;
  1         2  
  1         24  
18 1     1   4 use warnings;
  1         2  
  1         19  
19 1     1   5 use DBI;
  1         2  
  1         41  
20 1     1   6 use Data::Dumper;
  1         2  
  1         42  
21 1     1   5 use SQL::Translator::Schema::Constants;
  1         2  
  1         660  
22              
23             our ( $DEBUG, @EXPORT_OK );
24             our $VERSION = '1.6_3';
25             $DEBUG = 0 unless defined $DEBUG;
26              
27             my $actions = {c => 'cascade',
28             r => 'restrict',
29             a => 'no action',
30             n => 'set null',
31             d => 'set default',
32             };
33              
34             sub parse {
35 0     0 0   my ( $tr, $dbh ) = @_;
36              
37 0           my $schema = $tr->schema;
38              
39 0           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, pg_get_expr(ad.adbin, ad.adrelid) as adsrc,
42             d.description
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
47             AND a.atttypid=t.oid
48             ORDER BY a.attnum"
49             );
50              
51 0           my $index_select = $dbh->prepare(
52             "SELECT oid, c.relname, i.indkey, i.indnatts, i.indisunique,
53             i.indisprimary, pg_get_indexdef(oid) AS create_string
54             FROM pg_class c,pg_index i
55             WHERE c.relnamespace IN (SELECT oid FROM pg_namespace WHERE nspname='public') AND c.relkind='i'
56             AND c.oid=i.indexrelid AND i.indrelid=?"
57             );
58              
59 0           my $table_select = $dbh->prepare(
60             "SELECT c.oid, c.relname, d.description
61             FROM pg_class c
62             LEFT JOIN pg_description d ON c.oid=d.objoid AND d.objsubid=0
63             WHERE relnamespace IN
64             (SELECT oid FROM pg_namespace WHERE nspname='public')
65             AND relkind='r';"
66             );
67              
68 0 0         my $fk_select = $dbh->prepare(
69             q/
70             SELECT r.conname,
71             c.relname,
72             d.relname AS frelname,
73             r.conkey,
74             ARRAY(SELECT column_name::varchar
75             FROM information_schema.columns
76             WHERE ordinal_position = ANY (r.conkey)
77             AND table_schema = n.nspname
78             AND table_name = c.relname ) AS fields,
79             r.confkey,
80             ARRAY(SELECT column_name::varchar
81             FROM information_schema.columns
82             WHERE ordinal_position = ANY (r.confkey)
83             AND table_schema = n.nspname
84             AND table_name = d.relname ) AS reference_fields,
85             r.confupdtype,
86             r.confdeltype,
87             r.confmatchtype
88              
89             FROM pg_catalog.pg_constraint r
90              
91             JOIN pg_catalog.pg_class c
92             ON c.oid = r.conrelid
93             AND r.contype = 'f'
94              
95             JOIN pg_catalog.pg_class d
96             ON d.oid = r.confrelid
97              
98             JOIN pg_catalog.pg_namespace n
99             ON n.oid = c.relnamespace
100              
101             WHERE pg_catalog.pg_table_is_visible(c.oid)
102             AND n.nspname = ?
103             AND c.relname = ?
104             ORDER BY 1;
105             /) or die "Can't prepare: $@";
106              
107 0           $table_select->execute();
108              
109 0           while ( my $tablehash = $table_select->fetchrow_hashref ) {
110              
111 0           my $table_name = $$tablehash{'relname'};
112 0           my $table_oid = $$tablehash{'oid'};
113 0   0       my $table = $schema->add_table(
114             name => $table_name,
115             #what is type? type => $table_info->{TABLE_TYPE},
116             ) || die $schema->error;
117              
118 0 0         $table->comments($$tablehash{'description'}) if $$tablehash{'description'};
119              
120 0           $column_select->execute($table_oid);
121              
122 0           while (my $columnhash = $column_select->fetchrow_hashref ) {
123              
124             #data_type seems to not be populated; perhaps there needs to
125             #be a mapping of query output to reserved constants in sqlt?
126              
127             my $col = $table->add_field(
128             name => $$columnhash{'attname'},
129             default_value => $$columnhash{'adsrc'},
130             data_type => $$columnhash{'typname'},
131 0   0       order => $$columnhash{'attnum'},
132             ) || die $table->error;
133              
134             $col->{size} = [$$columnhash{'length'}]
135 0 0 0       if $$columnhash{'length'}>0 && $$columnhash{'length'}<=0xFFFF;
136 0 0         $col->{is_nullable} = $$columnhash{'attnotnull'} ? 0 : 1;
137 0 0         $col->comments($$columnhash{'description'}) if $$columnhash{'description'};
138             }
139              
140 0           $index_select->execute($table_oid);
141              
142 0           my @column_names = $table->field_names();
143 0           while (my $indexhash = $index_select->fetchrow_hashref ) {
144             #don't deal with function indexes at the moment
145             next if ($$indexhash{'indkey'} eq ''
146 0 0 0       or !defined($$indexhash{'indkey'}) );
147              
148 0           my @columns = map $column_names[$_ - 1], split /\s+/, $$indexhash{'indkey'};
149              
150 0           my $type;
151 0 0         if ($$indexhash{'indisprimary'}) {
    0          
152 0           $type = UNIQUE; #PRIMARY_KEY;
153              
154             #tell sqlt that this is the primary key:
155 0           for my $column (@columns) {
156 0           $table->get_field($column)->{is_primary_key}=1;
157             }
158              
159             } elsif ($$indexhash{'indisunique'}) {
160 0           $type = UNIQUE;
161             } else {
162 0           $type = NORMAL;
163             }
164              
165              
166             $table->add_index(
167 0 0         name => $$indexhash{'relname'},
168             type => $type,
169             fields => \@columns,
170             ) || die $table->error;
171             }
172              
173 0 0         $fk_select->execute('public',$table_name) or die "Can't execute: $@";
174 0           my $fkeys = $fk_select->fetchall_arrayref({});
175 0 0         $DEBUG and print Dumper $fkeys;
176 0           for my $con (@$fkeys){
177 0           my $con_name = $con->{conname};
178 0           my $fields = $con->{fields};
179 0           my $reference_fields = $con->{reference_fields};
180 0           my $reference_table = $con->{frelname};
181 0           my $on_upd = $con->{confupdtype};
182 0           my $on_del = $con->{confdeltype};
183             $table->add_constraint(
184             name => $con_name,
185             type => 'foreign_key',
186             fields => $fields,
187             reference_fields => $reference_fields,
188             reference_table => $reference_table,
189             on_delete => $actions->{$on_upd},
190 0           on_update => $actions->{$on_del},
191             );
192             }
193             }
194              
195              
196 0           return 1;
197             }
198              
199             1;
200              
201             # -------------------------------------------------------------------
202             # Time is a waste of money.
203             # Oscar Wilde
204             # -------------------------------------------------------------------
205              
206             =pod
207              
208             =head1 AUTHOR
209              
210             Scott Cain Ecain@cshl.eduE, previous author:
211             Paul Harrington Eharringp@deshaw.comE.
212              
213             =head1 SEE ALSO
214              
215             SQL::Translator, DBD::Pg.
216              
217             =cut