blib/lib/SQL/Translator/Parser/DBI/PostgreSQL.pm

filter: Statementstmt 27.0% 20 / 74 Branchbran 2.6% 1 / 38 Conditioncond 0.0% 0 / 7 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 83.3% 5 / 6 Podpod 0.0% 0 / 1 totaltotal 20.0% 26 / 130 SCAR 58.5
CC
24
Cov
18%
parse
600.0
BEGIN
1.0
BEGIN
1.0
CRAP 345.7
« blib/lib/SQL/Translator/Producer/Oracle.pm ↑ Summary blib/lib/SQL/Translator/Producer/PostgreSQL.pm »
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 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 1 ▶use DBI;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 1 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 1 ▶use SQL::Translator::Schema::Constants;
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 1 our ( $DEBUG, @EXPORT_OK );
24 1 our $VERSION = '1.63';
25 1 ▶$DEBUG = 0 unless defined $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $SQL::Translator::Parser::DBI::PostgreSQL::DEBUG 1 0 1
26  
27 1 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 ▶ my ( $tr, $dbh ) = @_;
Subroutine parse: not called CC=24 SCAR=64.0
Pod undocumented
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 ▶ my $fk_select = $dbh->prepare(
Branch: 1 branch
BranchTrueFalseTotal
my $fk_select = $dbh->prepare("\nSELECT r.conname,\n c.relname,\n d.relname AS frelname,\n r.conkey,\n ARRAY(SELECT column_name::varchar\n FROM information_schema.columns\n WHERE ordinal_position = ANY (r.conkey)\n AND table_schema = n.nspname\n AND table_name = c.relname ) AS fields,\n r.confkey,\n ARRAY(SELECT column_name::varchar\n FROM information_schema.columns\n WHERE ordinal_position = ANY (r.confkey)\n AND table_schema = n.nspname\n AND table_name = d.relname ) AS reference_fields,\n r.confupdtype,\n r.confdeltype,\n r.confmatchtype\n\nFROM pg_catalog.pg_constraint r\n\nJOIN pg_catalog.pg_class c\n ON c.oid = r.conrelid\n AND r.contype = 'f'\n\nJOIN pg_catalog.pg_class d\n ON d.oid = r.confrelid\n\nJOIN pg_catalog.pg_namespace n\n ON n.oid = c.relnamespace\n\nWHERE pg_catalog.pg_table_is_visible(c.oid)\n AND n.nspname = ?\n AND c.relname = ?\nORDER BY 1;\n ") or die "Can't prepare: $@" 0 0 0
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 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $tablehash = $table_select->fetchrow_hashref) 0 0 0
110  
111 0 my $table_name = $$tablehash{'relname'};
112 0 my $table_oid = $$tablehash{'oid'};
113 0 ▶ my $table = $schema->add_table(
Condition0%0/2
$schema->add_table('name', $table_name) || die($schema->error)
l!l
00
MC/DC0%0/1
$schema->add_table('name', $table_name) || die($schema->error)
$schema->add_table('name', $table_name)
Truth table0%0/2
A
A= $schema->add_table('name', $table_name)
Aresult
11
00
114 name => $table_name,
115 #what is type? type => $table_info->{TABLE_TYPE},
116 ) || die $schema->error;
117  
118 0 ▶ $table->comments($$tablehash{'description'}) if $$tablehash{'description'};
Branch: 1 branch
BranchTrueFalseTotal
if $tablehash->{"description"} 0 0 0
119  
120 0 $column_select->execute($table_oid);
121  
122 0 my %column_by_attrid;
123 0 ▶ while (my $columnhash = $column_select->fetchrow_hashref ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $columnhash = $column_select->fetchrow_hashref) 0 0 0
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 ▶ my @size= ($type =~ s/\(([0-9,]+)\)$//)? (split /,/, $1) : ();
Branch: 1 branch
BranchTrueFalseTotal
$type =~ s/\(([0-9,]+)\)$// ? : 0 0 0
131 my $col = $table->add_field(
132 name => $$columnhash{'attname'},
133 data_type => $type,
134 0 ▶ order => $$columnhash{'attnum'},
Condition0%0/2
$table->add_field('name', $columnhash->{'attname'}, 'data_type', $type, 'order', $columnhash->{'attnum'}) || die($table->error)
l!l
00
MC/DC0%0/1
$table->add_field('name', $columnhash->{'attname'}, 'data_type', $type, 'order', $columnhash->{'attnum'}) || die($table->error)
$table->add_field('name', $columnhash->{'attname'}, 'data_type', $type, 'order', $columnhash->{'attnum'})
Truth table0%0/2
A
A= $table->add_field('name', $columnhash->{'attname'}, 'data_type', $type, 'order', $columnhash->{'attnum'})
Aresult
11
00
135 ) || die $table->error;
136 0 ▶ $col->size(\@size) if @size;
Branch: 1 branch
BranchTrueFalseTotal
if @size 0 0 0
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 ▶ if (defined $default) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $default) 0 0 0
141 0 ▶ if ($default =~ /^[0-9.]+$/) { $col->default_value($default) }
Branch: 2 branches
BranchTrueFalseTotal
if ($default =~ /^[0-9.]+$/) { } 0 0 0
elsif ($default =~ /^'(.*?)'(::\Q$type\E)?$/) { } 0 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 ▶ $col->is_nullable( $$columnhash{'attnotnull'} ? 0 : 1 );
Branch: 1 branch
BranchTrueFalseTotal
$columnhash->{'attnotnull'} ? : 0 0 0
150 0 ▶ $col->comments($$columnhash{'description'}) if $$columnhash{'description'};
Branch: 1 branch
BranchTrueFalseTotal
if $columnhash->{"description"} 0 0 0
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 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $indexhash = $index_select->fetchrow_hashref) 0 0 0
157 #don't deal with function indexes at the moment
158 next if ($$indexhash{'indkey'} eq ''
159 0 ▶ or !defined($$indexhash{'indkey'}) );
Branch: 1 branch
BranchTrueFalseTotal
if $indexhash->{"indkey"} eq "" or not defined $indexhash->{"indkey"} 0 0 0
Condition0%0/3
$indexhash->{"indkey"} eq "" or not defined $indexhash->{"indkey"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$indexhash->{"indkey"} eq "" or not defined $indexhash->{"indkey"}
$indexhash->{"indkey"} eq "" not defined $indexhash->{"indkey"}
Truth table0%0/3
A or not(B)
A= $indexhash->{"indkey"} eq ""B= not defined $indexhash->{"indkey"}
ABresult
1X1
011
000
160  
161 0 my @columns = map $column_by_attrid{$_}, split /\s+/, $$indexhash{'indkey'};
162  
163 0 my $type;
164 0 ▶ if ($$indexhash{'indisprimary'}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($indexhash->{'indisprimary'}) { } 0 0 0
elsif ($indexhash->{'indisunique'}) { } 0 0 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 ▶ name => $$indexhash{'relname'},
Branch: 1 branch
BranchTrueFalseTotal
$table->add_index("name", $indexhash->{"relname"}, "type", $type, "fields", \@columns) or die $table->error 0 0 0
181 type => $type,
182 fields => \@columns,
183 ) || die $table->error;
184 }
185  
186 0 ▶ $fk_select->execute('public',$table_name) or die "Can't execute: $@";
Branch: 1 branch
BranchTrueFalseTotal
$fk_select->execute("public", $table_name) or die "Can't execute: $@" 0 0 0
187 0 my $fkeys = $fk_select->fetchall_arrayref({});
188 0 ▶ $DEBUG and print Dumper $fkeys;
Branch: 1 branch
BranchTrueFalseTotal
$SQL::Translator::Parser::DBI::PostgreSQL::DEBUG and print Dumper($fkeys) 0 0 0
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 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 E<lt>cain@cshl.eduE<gt>, previous author:
224 Paul Harrington E<lt>harringp@deshaw.comE<gt>.
225  
226 =head1 SEE ALSO
227  
228 SQL::Translator, DBD::Pg.
229  
230 =cut
« blib/lib/SQL/Translator/Producer/Oracle.pm ↑ Summary blib/lib/SQL/Translator/Producer/PostgreSQL.pm »