File Coverage

blib/lib/SQL/Translator/Parser/DBI/PostgreSQL.pm
Criterion Covered Total %
statement 15 69 21.7
branch 0 30 0.0
condition 0 7 0.0
subroutine 5 6 83.3
pod 0 1 0.0
total 20 113 17.7


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   7 use strict;
  1         2  
  1         37  
18 1     1   8 use warnings;
  1         3  
  1         26  
19 1     1   5 use DBI;
  1         2  
  1         40  
20 1     1   5 use Data::Dumper;
  1         2  
  1         47  
21 1     1   6 use SQL::Translator::Schema::Constants;
  1         2  
  1         986  
22              
23             our ( $DEBUG, @EXPORT_OK );
24             our $VERSION = '1.63';
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           my %column_by_attrid;
123 0           while (my $columnhash = $column_select->fetchrow_hashref ) {
124 0           my $type = $$columnhash{'typname'};
125             # For the case of character varying(50), atttypmod will be 54 and the (50)
126             # will be listed as part of the type. For numeric(8,5) the atttypmod will
127             # be a meaningless large number. To make this compatible with the
128             # rest of SQL::Translator, remove the size from the type and change the
129             # size to whatever was removed from the type.
130 0 0         my @size= ($type =~ s/\(([0-9,]+)\)$//)? (split /,/, $1) : ();
131             my $col = $table->add_field(
132             name => $$columnhash{'attname'},
133             data_type => $type,
134 0   0       order => $$columnhash{'attnum'},
135             ) || die $table->error;
136 0 0         $col->size(\@size) if @size;
137             # default values are a DDL expression. Convert the obvious ones like '...'::text
138             # to a plain value and let the rest be scalarrefs.
139 0           my $default= $$columnhash{'adsrc'};
140 0 0         if (defined $default) {
141 0 0         if ($default =~ /^[0-9.]+$/) { $col->default_value($default) }
  0 0          
142             elsif ($default =~ /^'(.*?)'(::\Q$type\E)?$/) {
143 0           my $str= $1;
144 0           $str =~ s/''/'/g;
145 0           $col->default_value($str);
146             }
147 0           else { $col->default_value(\$default) }
148             }
149 0 0         $col->is_nullable( $$columnhash{'attnotnull'} ? 0 : 1 );
150 0 0         $col->comments($$columnhash{'description'}) if $$columnhash{'description'};
151 0           $column_by_attrid{$$columnhash{'attnum'}}= $$columnhash{'attname'};
152             }
153              
154 0           $index_select->execute($table_oid);
155              
156 0           while (my $indexhash = $index_select->fetchrow_hashref ) {
157             #don't deal with function indexes at the moment
158             next if ($$indexhash{'indkey'} eq ''
159 0 0 0       or !defined($$indexhash{'indkey'}) );
160              
161 0           my @columns = map $column_by_attrid{$_}, split /\s+/, $$indexhash{'indkey'};
162              
163 0           my $type;
164 0 0         if ($$indexhash{'indisprimary'}) {
    0          
165 0           $type = UNIQUE; #PRIMARY_KEY;
166              
167             #tell sqlt that this is the primary key:
168 0           for my $column (@columns) {
169 0           $table->get_field($column)->{is_primary_key}=1;
170             }
171              
172             } elsif ($$indexhash{'indisunique'}) {
173 0           $type = UNIQUE;
174             } else {
175 0           $type = NORMAL;
176             }
177              
178              
179             $table->add_index(
180 0 0         name => $$indexhash{'relname'},
181             type => $type,
182             fields => \@columns,
183             ) || die $table->error;
184             }
185              
186 0 0         $fk_select->execute('public',$table_name) or die "Can't execute: $@";
187 0           my $fkeys = $fk_select->fetchall_arrayref({});
188 0 0         $DEBUG and print Dumper $fkeys;
189 0           for my $con (@$fkeys){
190 0           my $con_name = $con->{conname};
191 0           my $fields = $con->{fields};
192 0           my $reference_fields = $con->{reference_fields};
193 0           my $reference_table = $con->{frelname};
194 0           my $on_upd = $con->{confupdtype};
195 0           my $on_del = $con->{confdeltype};
196             $table->add_constraint(
197             name => $con_name,
198             type => 'foreign_key',
199             fields => $fields,
200             reference_fields => $reference_fields,
201             reference_table => $reference_table,
202             on_delete => $actions->{$on_upd},
203 0           on_update => $actions->{$on_del},
204             );
205             }
206             }
207              
208              
209 0           return 1;
210             }
211              
212             1;
213              
214             # -------------------------------------------------------------------
215             # Time is a waste of money.
216             # Oscar Wilde
217             # -------------------------------------------------------------------
218              
219             =pod
220              
221             =head1 AUTHOR
222              
223             Scott Cain Ecain@cshl.eduE, previous author:
224             Paul Harrington Eharringp@deshaw.comE.
225              
226             =head1 SEE ALSO
227              
228             SQL::Translator, DBD::Pg.
229              
230             =cut