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

filter: Statementstmt 31.7% 20 / 63 Branchbran 3.3% 1 / 30 Conditioncond 0.0% 0 / 10 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 83.3% 5 / 6 Podpod 0.0% 0 / 1 totaltotal 22.4% 26 / 116 SCAR 55.0
CC
21
Cov
20%
parse
462.0
BEGIN
1.0
BEGIN
1.0
CRAP 243.5
« blib/lib/SQL/Translator/Schema/Table.pm ↑ Summary blib/lib/SQL/Translator.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.6_3';
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=21 SCAR=61.4
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 ▶ while (my $columnhash = $column_select->fetchrow_hashref ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $columnhash = $column_select->fetchrow_hashref) 0 0 0
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 ▶ order => $$columnhash{'attnum'},
Condition0%0/2
$table->add_field('name', $columnhash->{'attname'}, 'default_value', $columnhash->{'adsrc'}, 'data_type', $columnhash->{'typname'}, 'order', $columnhash->{'attnum'}) || die($table->error)
l!l
00
MC/DC0%0/1
$table->add_field('name', $columnhash->{'attname'}, 'default_value', $columnhash->{'adsrc'}, 'data_type', $columnhash->{'typname'}, 'order', $columnhash->{'attnum'}) || die($table->error)
$table->add_field('name', $columnhash->{'attname'}, 'default_value', $columnhash->{'adsrc'}, 'data_type', $columnhash->{'typname'}, 'order', $columnhash->{'attnum'})
Truth table0%0/2
A
A= $table->add_field('name', $columnhash->{'attname'}, 'default_value', $columnhash->{'adsrc'}, 'data_type', $columnhash->{'typname'}, 'order', $columnhash->{'attnum'})
Aresult
11
00
132 ) || die $table->error;
133  
134 $col->{size} = [$$columnhash{'length'}]
135 0 ▶ if $$columnhash{'length'}>0 && $$columnhash{'length'}<=0xFFFF;
Branch: 1 branch
BranchTrueFalseTotal
if $columnhash->{"length"} > 0 and $columnhash->{"length"} <= 65535 0 0 0
Condition0%0/3
$columnhash->{"length"} > 0 and $columnhash->{"length"} <= 65535
!ll&&!rl&&r
000
MC/DC0%0/2
$columnhash->{"length"} > 0 and $columnhash->{"length"} <= 65535
$columnhash->{"length"} > 0 $columnhash->{"length"} <= 65535
Truth table0%0/3
A and B
A= $columnhash->{"length"} > 0B= $columnhash->{"length"} <= 65535
ABresult
0X0
100
111
136 0 ▶ $col->{is_nullable} = $$columnhash{'attnotnull'} ? 0 : 1;
Branch: 1 branch
BranchTrueFalseTotal
$columnhash->{'attnotnull'} ? : 0 0 0
137 0 ▶ $col->comments($$columnhash{'description'}) if $$columnhash{'description'};
Branch: 1 branch
BranchTrueFalseTotal
if $columnhash->{"description"} 0 0 0
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 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $indexhash = $index_select->fetchrow_hashref) 0 0 0
144 #don't deal with function indexes at the moment
145 next if ($$indexhash{'indkey'} eq ''
146 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
147  
148 0 my @columns = map $column_names[$_ - 1], split /\s+/, $$indexhash{'indkey'};
149  
150 0 my $type;
151 0 ▶ if ($$indexhash{'indisprimary'}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($indexhash->{'indisprimary'}) { } 0 0 0
elsif ($indexhash->{'indisunique'}) { } 0 0 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 ▶ name => $$indexhash{'relname'},
Branch: 1 branch
BranchTrueFalseTotal
$table->add_index("name", $indexhash->{"relname"}, "type", $type, "fields", \@columns) or die $table->error 0 0 0
168 type => $type,
169 fields => \@columns,
170 ) || die $table->error;
171 }
172  
173 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
174 0 my $fkeys = $fk_select->fetchall_arrayref({});
175 0 ▶ $DEBUG and print Dumper $fkeys;
Branch: 1 branch
BranchTrueFalseTotal
$SQL::Translator::Parser::DBI::PostgreSQL::DEBUG and print Dumper($fkeys) 0 0 0
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 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 E<lt>cain@cshl.eduE<gt>, previous author:
211 Paul Harrington E<lt>harringp@deshaw.comE<gt>.
212  
213 =head1 SEE ALSO
214  
215 SQL::Translator, DBD::Pg.
216  
217 =cut
« blib/lib/SQL/Translator/Schema/Table.pm ↑ Summary blib/lib/SQL/Translator.pm »