blib/lib/SQL/Translator/Producer/PostgreSQL.pm

filter: Statementstmt 90.4% 427 / 472 Branchbran 73.0% 187 / 256 Conditioncond 69.1% 94 / 136 MC/DCmcdc 51.2% 40 / 78 Subroutinesub 93.4% 43 / 46 Podpod 35.7% 10 / 28 totaltotal 78.8% 801 / 1016 CC 191 SCAR 35.2
CC
191
Cov
82%
mk_name
272.0
create_constraint
31.3
alter_field
28.7
CRAP 33.7
« blib/lib/SQL/Translator/Schema/Procedure.pm ↑ Summary blib/lib/SQL/Translator/Parser/Excel.pm »
1 package SQL::Translator::Producer::PostgreSQL;
2  
3 =head1 NAME
4  
5 SQL::Translator::Producer::PostgreSQL - PostgreSQL producer for SQL::Translator
6  
7 =head1 SYNOPSIS
8  
9 my $t = SQL::Translator->new( parser => '...', producer => 'PostgreSQL' );
10 $t->translate;
11  
12 =head1 DESCRIPTION
13  
14 Creates a DDL suitable for PostgreSQL. Very heavily based on the Oracle
15 producer.
16  
17 Now handles PostGIS Geometry and Geography data types on table definitions.
18 Does not yet support PostGIS Views.
19  
20 =head2 Producer Args
21  
22 You can change the global behavior of the producer by passing the following options to the
23 C<producer_args> attribute of C<SQL::Translator>.
24  
25 =over 4
26  
27 =item postgres_version
28  
29 The version of postgres to generate DDL for. Turns on features only available in later versions. The following features are supported
30  
31 =over 4
32  
33 =item IF EXISTS
34  
35 If your postgres_version is higher than 8.003 (I should hope it is by now), then the DDL
36 generated for dropping objects in the database will contain IF EXISTS.
37  
38 =back
39  
40 =item attach_comments
41  
42 Generates table and column comments via the COMMENT command rather than as a comment in
43 the DDL. You could then look it up with \dt+ or \d+ (for tables and columns respectively)
44 in psql. The comment is dollar quoted with $comment$ so you can include ' in it. Just to clarify: you get this
45  
46 CREATE TABLE foo ...;
47 COMMENT on TABLE foo IS $comment$hi there$comment$;
48  
49 instead of this
50  
51 -- comment
52 CREAT TABLE foo ...;
53  
54 =back
55  
56 =head2 Extra args
57  
58 Various schema types support various options via the C<extra> attribute.
59  
60 =over 2
61  
62 =item Tables
63  
64 =over 2
65  
66 =item temporary
67  
68 Produces a temporary table.
69  
70 =back
71  
72 =item Views
73  
74 =over 2
75  
76 =item temporary
77  
78 Produces a temporary view.
79  
80 =item materialized
81  
82 Produces a materialized view.
83  
84 =back
85  
86 =item Fields
87  
88 =over 2
89  
90 =item list, custom_type_name
91  
92 For enum types, list is the list of valid values, and custom_type_name is the name that
93 the type should have. Defaults to $table_$field_type.
94  
95 =item geometry_type, srid, dimensions, geography_type
96  
97 Fields for use with PostGIS types.
98  
99 =back
100  
101 =back
102  
103 =cut
104  
105 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
106 6 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
107 6 our ($DEBUG, $WARN);
108 6 our $VERSION = '1.66';
109 6 ▶$DEBUG = 0 unless defined $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $SQL::Translator::Producer::PostgreSQL::DEBUG 6 0 6
110  
111 6 ▶use base qw(SQL::Translator::Producer);
Subroutine BEGIN: called CC=1 SCAR=0.0
112 6 ▶use SQL::Translator::Schema::Constants;
Subroutine BEGIN: called CC=1 SCAR=0.0
113 use SQL::Translator::Utils
114 6 ▶ qw(debug header_comment parse_dbms_version batch_alter_table_statements normalize_quote_options);
Subroutine BEGIN: called CC=1 SCAR=0.0
115 6 ▶use SQL::Translator::Generator::DDL::PostgreSQL;
Subroutine BEGIN: called CC=1 SCAR=0.0
116 6 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
117  
118 6 ▶use constant MAX_ID_LENGTH => 62;
Subroutine BEGIN: called CC=1 SCAR=0.0
119  
120 {
121 6 my ($quoting_generator, $nonquoting_generator);
122  
123 325 ▶ sub _generator {
Subroutine _generator: called CC=5 SCAR=16.3
124 325 my $options = shift;
125 325 ▶ return $options->{generator} if exists $options->{generator};
Branch: 1 branch
BranchTrueFalseTotal
if exists $options->{"generator"} 159 166 325
126  
127 166 ▶ return normalize_quote_options($options)
Branch: 1 branch
BranchTrueFalseTotal
normalize_quote_options($options) ? : 86 80 166
Condition66%4/6
normalize_quote_options($options) ? $quoting_generator ||= "SQL::Translator::Generator::DDL::PostgreSQL"->new : $nonquoting_generator ||= "SQL::Translator::Generator::DDL::PostgreSQL"->new("quote_chars", [])
l!l&&r!l&&!r
24420
$quoting_generator ||= "SQL::Translator::Generator::DDL::PostgreSQL"->new
l!l&&r!l&&!r
8060
MC/DC0%0/2
normalize_quote_options($options) ? $quoting_generator ||= "SQL::Translator::Generator::DDL::PostgreSQL"->new : $nonquoting_generator ||= "SQL::Translator::Generator::DDL::PostgreSQL"->new("quote_chars", [])
normalize_quote_options($options) ? $quoting_generator ||= "SQL::Translator::Generator::DDL::PostgreSQL"->new : $nonquoting_generator "SQL::Translator::Generator::DDL::PostgreSQL"->new("quote_chars", [])
MC/DC0%0/2
$quoting_generator ||= "SQL::Translator::Generator::DDL::PostgreSQL"->new
$quoting_generator "SQL::Translator::Generator::DDL::PostgreSQL"->new
Truth table66%2/3
A ||= B
A= normalize_quote_options($options) ? $quoting_generator ||= "SQL::Translator::Generator::DDL::PostgreSQL"->new : $nonquoting_generatorB= "SQL::Translator::Generator::DDL::PostgreSQL"->new("quote_chars", [])
ABresult
1X1
011
000
Truth table66%2/3
A ||= B
A= $quoting_generatorB= "SQL::Translator::Generator::DDL::PostgreSQL"->new
ABresult
1X1
011
000
128 ? $quoting_generator ||= SQL::Translator::Generator::DDL::PostgreSQL->new
129 : $nonquoting_generator ||= SQL::Translator::Generator::DDL::PostgreSQL->new(quote_chars => [],);
130 }
131 }
132  
133 6 my (%translate);
134  
135 6 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
136  
137 6 %translate = (
138 #
139 # MySQL types
140 #
141 double => 'double precision',
142 decimal => 'numeric',
143 int => 'integer',
144 mediumint => 'integer',
145 tinyint => 'smallint',
146 char => 'character',
147 varchar => 'character varying',
148 longtext => 'text',
149 mediumtext => 'text',
150 tinytext => 'text',
151 tinyblob => 'bytea',
152 blob => 'bytea',
153 mediumblob => 'bytea',
154 longblob => 'bytea',
155 enum => 'character varying',
156 set => 'character varying',
157 datetime => 'timestamp',
158 year => 'date',
159  
160 #
161 # Oracle types
162 #
163 number => 'integer',
164 varchar2 => 'character varying',
165 long => 'text',
166 clob => 'text',
167  
168 #
169 # Sybase types
170 #
171 comment => 'text',
172  
173 #
174 # MS Access types
175 #
176 memo => 'text',
177 );
178 }
179 my %truncated;
180  
181 =pod
182  
183 =head1 PostgreSQL Create Table Syntax
184  
185 CREATE [ [ LOCAL ] { TEMPORARY | TEMP } ] TABLE table_name (
186 { column_name data_type [ DEFAULT default_expr ] [ column_constraint [, ... ] ]
187 | table_constraint } [, ... ]
188 )
189 [ INHERITS ( parent_table [, ... ] ) ]
190 [ WITH OIDS | WITHOUT OIDS ]
191  
192 where column_constraint is:
193  
194 [ CONSTRAINT constraint_name ]
195 { NOT NULL | NULL | UNIQUE | PRIMARY KEY |
196 CHECK (expression) |
197 REFERENCES reftable [ ( refcolumn ) ] [ MATCH FULL | MATCH PARTIAL ]
198 [ ON DELETE action ] [ ON UPDATE action ] }
199 [ DEFERRABLE | NOT DEFERRABLE ] [ INITIALLY DEFERRED | INITIALLY IMMEDIATE ]
200  
201 and table_constraint is:
202  
203 [ CONSTRAINT constraint_name ]
204 { UNIQUE ( column_name [, ... ] ) |
205 PRIMARY KEY ( column_name [, ... ] ) |
206 CHECK ( expression ) |
207 EXCLUDE [USING acc_method] (expression) [INCLUDE (column [, ...])] [WHERE (predicate)]
208 FOREIGN KEY ( column_name [, ... ] ) REFERENCES reftable [ ( refcolumn [, ... ] ) ]
209 [ MATCH FULL | MATCH PARTIAL ] [ ON DELETE action ] [ ON UPDATE action ] }
210 [ DEFERRABLE | NOT DEFERRABLE ] [ INITIALLY DEFERRED | INITIALLY IMMEDIATE ]
211  
212 =head1 Create Index Syntax
213  
214 CREATE [ UNIQUE ] INDEX index_name ON table
215 [ USING acc_method ] ( column [ ops_name ] [, ...] )
216 [ INCLUDE ( column [, ...] ) ]
217 [ WHERE predicate ]
218 CREATE [ UNIQUE ] INDEX index_name ON table
219 [ USING acc_method ] ( func_name( column [, ... ]) [ ops_name ] )
220 [ WHERE predicate ]
221  
222 =cut
223  
224 8 ▶sub produce {
Subroutine produce: called CC=10 SCAR=23.2
Pod documented
225 8 my $translator = shift;
226 8 local $DEBUG = $translator->debug;
227 8 local $WARN = $translator->show_warnings;
228 8 my $no_comments = $translator->no_comments;
229 8 my $add_drop_table = $translator->add_drop_table;
230 8 my $schema = $translator->schema;
231 8 my $pargs = $translator->producer_args;
232 8 my $postgres_version = parse_dbms_version($pargs->{postgres_version}, 'perl');
233  
234 8 my $generator = _generator({ quote_identifiers => $translator->quote_identifiers });
235  
236 8 my @output;
237 8 ▶ push @output, header_comment unless ($no_comments);
Branch: 1 branch
BranchTrueFalseTotal
unless $no_comments 0 8 8
238  
239 8 my (@table_defs, @fks);
240 8 my %type_defs;
241 8 for my $table ($schema->get_tables) {
242  
243 my ($table_def, $fks) = create_table(
244 $table,
245 {
246 generator => $generator,
247 no_comments => $no_comments,
248 postgres_version => $postgres_version,
249 add_drop_table => $add_drop_table,
250 type_defs => \%type_defs,
251 attach_comments => $pargs->{attach_comments}
252 }
253 22 );
254  
255 22 push @table_defs, $table_def;
256 22 push @fks, @$fks;
257 }
258  
259 8 for my $view ($schema->get_views) {
260 5 push @table_defs,
261 create_view(
262 $view,
263 {
264 postgres_version => $postgres_version,
265 add_drop_view => $add_drop_table,
266 generator => $generator,
267 no_comments => $no_comments,
268 }
269 );
270 }
271  
272 8 for my $trigger ($schema->get_triggers) {
273 14 push @table_defs,
274 create_trigger(
275 $trigger,
276 {
277 add_drop_trigger => $add_drop_table,
278 generator => $generator,
279 no_comments => $no_comments,
280 }
281 );
282 }
283  
284 0 push @output, map {"$_;\n\n"} values %type_defs;
285 8 push @output, map {"$_;\n\n"} @table_defs;
286 8 ▶ if (@fks) {
Branch: 1 branch
BranchTrueFalseTotal
if (@fks) 5 3 8
287 5 ▶ push @output, "--\n-- Foreign Key Definitions\n--\n\n"
Branch: 1 branch
BranchTrueFalseTotal
unless $no_comments 0 5 5
288 unless $no_comments;
289 5 push @output, map {"$_;\n\n"} @fks;
290 }
291  
292 8 ▶ if ($WARN) {
Branch: 1 branch
BranchTrueFalseTotal
if ($SQL::Translator::Producer::PostgreSQL::WARN) 1 7 8
293 1 ▶ if (%truncated) {
Branch: 1 branch
BranchTrueFalseTotal
if (%truncated) 0 1 1
294 0 warn "Truncated " . keys(%truncated) . " names:\n";
295 0 warn "\t" . join("\n\t", sort keys %truncated) . "\n";
296 }
297 }
298  
299 return wantarray
300 ? @output
301 8 ▶ : join('', @output);
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 2 6 8
302 }
303  
304 {
305 6 my %global_names;
306  
307 0 ▶ sub mk_name {
Subroutine mk_name: not called CC=16 SCAR=56.1
Pod undocumented
308 0 ▶ my $basename = shift || '';
Condition0%0/2
shift() || ''
l!l
00
MC/DC0%0/1
shift() || ''
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
309 0 ▶ my $type = shift || '';
Condition0%0/2
shift() || ''
l!l
00
MC/DC0%0/1
shift() || ''
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
310 0 ▶ my $scope = shift || '';
Condition0%0/2
shift() || ''
l!l
00
MC/DC0%0/1
shift() || ''
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
311 0 ▶ my $critical = shift || '';
Condition0%0/2
shift() || ''
l!l
00
MC/DC0%0/1
shift() || ''
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
312 0 my $basename_orig = $basename;
313  
314 0 ▶ my $max_name
Branch: 1 branch
BranchTrueFalseTotal
$type ? : 0 0 0
315 = $type
316 ? MAX_ID_LENGTH - (length($type) + 1)
317 : MAX_ID_LENGTH;
318 0 ▶ $basename = substr($basename, 0, $max_name)
Branch: 1 branch
BranchTrueFalseTotal
if length $basename > $max_name 0 0 0
319 if length($basename) > $max_name;
320 0 ▶ my $name = $type ? "${type}_$basename" : $basename;
Branch: 1 branch
BranchTrueFalseTotal
$type ? : 0 0 0
321  
322 0 ▶ if ($basename ne $basename_orig and $critical) {
Branch: 1 branch
BranchTrueFalseTotal
if ($basename ne $basename_orig and $critical) 0 0 0
Condition0%0/3
$basename ne $basename_orig and $critical
!ll&&!rl&&r
000
MC/DC0%0/2
$basename ne $basename_orig and $critical
$basename ne $basename_orig $critical
Truth table0%0/3
A and B
A= $basename ne $basename_origB= $critical
ABresult
0X0
100
111
323 0 ▶ my $show_type = $type ? "+'$type'" : "";
Branch: 1 branch
BranchTrueFalseTotal
$type ? : 0 0 0
324 0 ▶ warn "Truncating '$basename_orig'$show_type to ", MAX_ID_LENGTH, " character limit to make '$name'\n"
Branch: 1 branch
BranchTrueFalseTotal
if $SQL::Translator::Producer::PostgreSQL::WARN 0 0 0
325 if $WARN;
326 0 $truncated{$basename_orig} = $name;
327 }
328  
329 0 ▶ $scope ||= \%global_names;
Condition0%0/2
$scope ||= \%global_names
l!l
00
MC/DC0%0/1
$scope ||= \%global_names
$scope
Truth table0%0/2
A
A= $scope
Aresult
11
00
330 0 ▶ if (my $prev = $scope->{$name}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $prev = $scope->{$name}) 0 0 0
331 0 my $name_orig = $name;
332 0 $name .= sprintf("%02d", ++$prev);
333 0 ▶ substr($name, MAX_ID_LENGTH - 3) = "00"
Branch: 1 branch
BranchTrueFalseTotal
if length $name > 62 0 0 0
334 if length($name) > MAX_ID_LENGTH;
335  
336 0 ▶ warn "The name '$name_orig' has been changed to ", "'$name' to make it unique.\n"
Branch: 1 branch
BranchTrueFalseTotal
if $SQL::Translator::Producer::PostgreSQL::WARN 0 0 0
337 if $WARN;
338  
339 0 $scope->{$name_orig}++;
340 }
341  
342 0 $scope->{$name}++;
343 0 return $name;
344 }
345 }
346  
347 6 ▶sub is_geometry {
Subroutine is_geometry: called CC=2 SCAR=6.9
Pod undocumented
348 240 my $field = shift;
349 240 ▶ return 1 if $field->data_type eq 'geometry';
Branch: 1 branch
BranchTrueFalseTotal
if $field->data_type eq "geometry" 10 230 240
350 }
351  
352 0 ▶sub is_geography {
Subroutine is_geography: not called CC=2 SCAR=17.9
Pod undocumented
353 0 my $field = shift;
354 0 ▶ return 1 if $field->data_type eq 'geography';
Branch: 1 branch
BranchTrueFalseTotal
if $field->data_type eq "geography" 0 0 0
355 }
356  
357 24 ▶sub create_table {
Subroutine create_table: called CC=24 SCAR=32.4
Pod documented
358 24 my ($table, $options) = @_;
359  
360 24 my $generator = _generator($options);
361 24 ▶ my $no_comments = $options->{no_comments} || 0;
Condition100%2/2
$options->{'no_comments'} || 0
l!l
222
MC/DC100%1/1
$options->{'no_comments'} || 0
$options->{'no_comments'}
Truth table100%2/2
A
A= $options->{'no_comments'}
Aresult
11
00
362 24 ▶ my $add_drop_table = $options->{add_drop_table} || 0;
Condition100%2/2
$options->{'add_drop_table'} || 0
l!l
204
MC/DC100%1/1
$options->{'add_drop_table'} || 0
$options->{'add_drop_table'}
Truth table100%2/2
A
A= $options->{'add_drop_table'}
Aresult
11
00
363 24 ▶ my $postgres_version = $options->{postgres_version} || 0;
Condition50%1/2
$options->{'postgres_version'} || 0
l!l
024
MC/DC0%0/1
$options->{'postgres_version'} || 0
$options->{'postgres_version'}
Truth table50%1/2
A
A= $options->{'postgres_version'}
Aresult
11
00
364 24 ▶ my $type_defs = $options->{type_defs} || {};
Condition100%2/2
$options->{'type_defs'} || {}
l!l
222
MC/DC100%1/1
$options->{'type_defs'} || {}
$options->{'type_defs'}
Truth table100%2/2
A
A= $options->{'type_defs'}
Aresult
11
00
365 24 my $attach_comments = $options->{attach_comments};
366  
367 24 ▶ my $table_name = $table->name or next;
Branch: 1 branch
BranchTrueFalseTotal
my $table_name = $table->name or next 24 0 24
368 24 my $table_name_qt = $generator->quote($table_name);
369  
370 24 my (@comments, @field_defs, @index_defs, @constraint_defs, @fks);
371  
372 24 ▶ push @comments, "--\n-- Table: $table_name\n--\n" unless $no_comments;
Branch: 1 branch
BranchTrueFalseTotal
unless $no_comments 2 22 24
373  
374 24 my @comment_statements;
375 24 ▶ if (my $comments = $table->comments) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $comments = $table->comments) 1 23 24
376 1 ▶ if ($attach_comments) {
Branch: 2 branches
BranchTrueFalseTotal
if ($attach_comments) { } 1 0 1
elsif (not $no_comments) { } 0 0 0
377  
378 # this follows the example in the MySQL producer, where all comments are added as
379 # table comments, even though they could have originally been parsed as DDL comments
380 # quoted via $$ string so there can be 'quotes' inside the comments
381 1 my $comment_ddl = "COMMENT on TABLE $table_name_qt IS \$comment\$$comments\$comment\$";
382 1 push @comment_statements, $comment_ddl;
383 } elsif (!$no_comments) {
384 0 $comments =~ s/^/-- /mg;
385 0 push @comments, "-- Comments:\n$comments\n--\n";
386 }
387 }
388  
389 #
390 # Fields
391 #
392 24 for my $field ($table->get_fields) {
393 74 push @field_defs,
394 create_field(
395 $field,
396 {
397 generator => $generator,
398 postgres_version => $postgres_version,
399 type_defs => $type_defs,
400 constraint_defs => \@constraint_defs,
401 attach_comments => $attach_comments
402 }
403 );
404 74 ▶ if ($attach_comments) {
Branch: 1 branch
BranchTrueFalseTotal
if ($attach_comments) 12 62 74
405 12 my $field_comments = $field->comments;
406 12 ▶ next unless $field_comments;
Branch: 1 branch
BranchTrueFalseTotal
unless $field_comments 10 2 12
407 2 my $field_name_qt = $generator->quote($field->name);
408 2 my $comment_ddl = "COMMENT on COLUMN $table_name_qt.$field_name_qt IS \$comment\$$field_comments\$comment\$";
409 2 push @comment_statements, $comment_ddl;
410 }
411  
412 }
413  
414 #
415 # Index Declarations
416 #
417 24 for my $index ($table->get_indices) {
418 5 my ($idef, $constraints) = create_index(
419 $index,
420 {
421 generator => $generator,
422 postgres_version => $postgres_version,
423 }
424 );
425 5 ▶ $idef and push @index_defs, $idef;
Branch: 1 branch
BranchTrueFalseTotal
$idef and push @index_defs, $idef 5 0 5
426 5 push @constraint_defs, @$constraints;
427 }
428  
429 #
430 # Table constraints
431 #
432 24 for my $c ($table->get_constraints) {
433 35 my ($cdefs, $fks) = create_constraint(
434 $c,
435 {
436 generator => $generator,
437 }
438 );
439 35 push @constraint_defs, @$cdefs;
440 35 push @fks, @$fks;
441 }
442  
443 24 my $create_statement = join("\n", @comments);
444 24 ▶ if ($add_drop_table) {
Branch: 1 branch
BranchTrueFalseTotal
if ($add_drop_table) 20 4 24
445 20 ▶ if ($postgres_version >= 8.002) {
Branch: 1 branch
BranchTrueFalseTotal
if ($postgres_version >= 8.002) { } 0 20 20
446 0 $create_statement .= "DROP TABLE IF EXISTS $table_name_qt CASCADE;\n";
447 } else {
448 20 $create_statement .= "DROP TABLE $table_name_qt CASCADE;\n";
449 }
450 }
451 24 ▶ my $temporary = $table->extra->{temporary} ? "TEMPORARY " : "";
Branch: 1 branch
BranchTrueFalseTotal
$table->extra->{'temporary'} ? : 0 24 24
452 $create_statement .= "CREATE ${temporary}TABLE $table_name_qt (\n"
453 24 . join(",\n", map {" $_"} @field_defs, @constraint_defs) . "\n)";
454 24 ▶ $create_statement .= @index_defs ? ';' : q{};
Branch: 1 branch
BranchTrueFalseTotal
@index_defs ? : 5 19 24
455 24 ▶ $create_statement .= ($create_statement =~ /;$/ ? "\n" : q{}) . join(";\n", @index_defs);
Branch: 1 branch
BranchTrueFalseTotal
$create_statement =~ /;$/ ? : 5 19 24
456  
457 #
458 # Geometry
459 #
460 24 ▶ if (my @geometry_columns = grep { is_geometry($_) } $table->get_fields) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@geometry_columns) = grep({is_geometry($_);} $table->get_fields)) 1 23 24
461 0 $create_statement .= join(";\n", '', map { drop_geometry_column($_, $options) } @geometry_columns)
462 1 ▶ if $options->{add_drop_table};
Branch: 1 branch
BranchTrueFalseTotal
if $options->{"add_drop_table"} 0 1 1
463 1 $create_statement .= join(";\n", '', map { add_geometry_column($_, $options) } @geometry_columns);
464 }
465  
466 24 ▶ if (@comment_statements) {
Branch: 1 branch
BranchTrueFalseTotal
if (@comment_statements) 2 22 24
467 2 $create_statement .= join(";\n", '', @comment_statements);
468 }
469  
470 24 return $create_statement, \@fks;
471 }
472  
473 10 ▶sub create_view {
Subroutine create_view: called CC=12 SCAR=24.9
Pod documented
474 10 my ($view, $options) = @_;
475 10 my $generator = _generator($options);
476 10 ▶ my $postgres_version = $options->{postgres_version} || 0;
Condition100%2/2
$options->{'postgres_version'} || 0
l!l
28
MC/DC100%1/1
$options->{'postgres_version'} || 0
$options->{'postgres_version'}
Truth table100%2/2
A
A= $options->{'postgres_version'}
Aresult
11
00
477 10 my $add_drop_view = $options->{add_drop_view};
478  
479 10 my $view_name = $view->name;
480 10 debug("PKG: Looking at view '${view_name}'\n");
481  
482 10 my $create = '';
483 $create .= "--\n-- View: " . $generator->quote($view_name) . "\n--\n"
484 10 ▶ unless $options->{no_comments};
Branch: 1 branch
BranchTrueFalseTotal
unless $options->{"no_comments"} 0 10 10
485 10 ▶ if ($add_drop_view) {
Branch: 1 branch
BranchTrueFalseTotal
if ($add_drop_view) 7 3 10
486 7 ▶ if ($postgres_version >= 8.002) {
Branch: 1 branch
BranchTrueFalseTotal
if ($postgres_version >= 8.002) { } 1 6 7
487 1 $create .= "DROP VIEW IF EXISTS " . $generator->quote($view_name) . ";\n";
488 } else {
489 6 $create .= "DROP VIEW " . $generator->quote($view_name) . ";\n";
490 }
491 }
492 10 $create .= 'CREATE';
493  
494 10 my $extra = $view->extra;
495 $create .= " TEMPORARY"
496 10 ▶ if exists($extra->{temporary}) && $extra->{temporary};
Branch: 1 branch
BranchTrueFalseTotal
if exists $extra->{"temporary"} and $extra->{"temporary"} 1 9 10
Condition66%2/3
exists $extra->{"temporary"} and $extra->{"temporary"}
!ll&&!rl&&r
901
MC/DC50%1/2
exists $extra->{"temporary"} and $extra->{"temporary"}
exists $extra->{"temporary"} $extra->{"temporary"}
Truth table66%2/3
A and B
A= exists $extra->{"temporary"}B= $extra->{"temporary"}
ABresult
0X0
100
111
497 $create .= " MATERIALIZED"
498 10 ▶ if exists($extra->{materialized}) && $extra->{materialized};
Branch: 1 branch
BranchTrueFalseTotal
if exists $extra->{"materialized"} and $extra->{"materialized"} 1 9 10
Condition66%2/3
exists $extra->{"materialized"} and $extra->{"materialized"}
!ll&&!rl&&r
901
MC/DC50%1/2
exists $extra->{"materialized"} and $extra->{"materialized"}
exists $extra->{"materialized"} $extra->{"materialized"}
Truth table66%2/3
A and B
A= exists $extra->{"materialized"}B= $extra->{"materialized"}
ABresult
0X0
100
111
499 10 $create .= " VIEW " . $generator->quote($view_name);
500  
501 10 ▶ if (my @fields = $view->fields) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@fields) = $view->fields) 9 1 10
502 9 my $field_list = join ', ', map { $generator->quote($_) } @fields;
503 9 $create .= " ( ${field_list} )";
504 }
505  
506 10 ▶ if (my $sql = $view->sql) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $sql = $view->sql) 10 0 10
507 10 $create .= " AS\n ${sql}\n";
508 }
509  
510 10 ▶ if ($extra->{check_option}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($extra->{"check_option"}) 1 9 10
511 1 $create .= ' WITH ' . uc $extra->{check_option} . ' CHECK OPTION';
512 }
513  
514 10 return $create;
515 }
516  
517 # Returns a enum custom type name and list of values iff the field looks like an enum.
518 148 ▶sub _enum_typename_and_values {
Subroutine _enum_typename_and_values: called CC=4 SCAR=13.9
519 148 my $field = shift;
520 148 ▶ if (ref $field->extra->{list} eq 'ARRAY') { # can't do anything unless we know the list
Branch: 1 branch
BranchTrueFalseTotal
if (ref $field->extra->{"list"} eq "ARRAY") 4 144 148
521 4 ▶ if ($field->extra->{custom_type_name}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($field->extra->{'custom_type_name'}) { } 3 1 4
elsif ($field->data_type eq 'enum') { } 1 0 1
522 3 return ($field->extra->{custom_type_name}, $field->extra->{list});
523 } elsif ($field->data_type eq 'enum') {
524 1 my $name = $field->table->name . '_' . $field->name . '_type';
525 1 return ($name, $field->extra->{list});
526 }
527 }
528 144 return ();
529 }
530  
531 {
532  
533 6 my %field_name_scope;
534  
535 102 ▶ sub create_field {
Subroutine create_field: called CC=14 SCAR=26.4
Pod documented
536 102 my ($field, $options) = @_;
537  
538 102 my $generator = _generator($options);
539 102 my $table_name = $field->table->name;
540 102 ▶ my $constraint_defs = $options->{constraint_defs} || [];
Condition100%2/2
$options->{'constraint_defs'} || []
l!l
7428
MC/DC100%1/1
$options->{'constraint_defs'} || []
$options->{'constraint_defs'}
Truth table100%2/2
A
A= $options->{'constraint_defs'}
Aresult
11
00
541 102 ▶ my $postgres_version = $options->{postgres_version} || 0;
Condition100%2/2
$options->{'postgres_version'} || 0
l!l
795
MC/DC100%1/1
$options->{'postgres_version'} || 0
$options->{'postgres_version'}
Truth table100%2/2
A
A= $options->{'postgres_version'}
Aresult
11
00
542 102 ▶ my $type_defs = $options->{type_defs} || {};
Condition100%2/2
$options->{'type_defs'} || {}
l!l
7626
MC/DC100%1/1
$options->{'type_defs'} || {}
$options->{'type_defs'}
Truth table100%2/2
A
A= $options->{'type_defs'}
Aresult
11
00
543 102 my $attach_comments = $options->{attach_comments};
544  
545 102 ▶ $field_name_scope{$table_name} ||= {};
Condition100%2/2
$field_name_scope{$table_name} ||= {}
l!l
8814
MC/DC100%1/1
$field_name_scope{$table_name} ||= {}
$field_name_scope{$table_name}
Truth table100%2/2
A
A= $field_name_scope{$table_name}
Aresult
11
00
546 102 my $field_name = $field->name;
547  
548 102 my $field_comments = '';
549 102 ▶ if (!$attach_comments and my $comments = $field->comments) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $attach_comments and my $comments = $field->comments) 4 98 102
Condition100%3/3
not $attach_comments and my $comments = $field->comments
!ll&&!rl&&r
12864
MC/DC100%2/2
not $attach_comments and my $comments = $field->comments
not $attach_comments my $comments = $field->comments
Truth table100%3/3
not(A) and B
A= not $attach_commentsB= my $comments = $field->comments
ABresult
0X0
100
111
550 4 $comments =~ s/(?<!\A)^/ -- /mg;
551 4 $field_comments = "-- $comments\n ";
552 }
553  
554 102 my $field_def = $field_comments . $generator->quote($field_name);
555  
556 #
557 # Datatype
558 #
559 102 my $data_type = lc $field->data_type;
560 102 my %extra = $field->extra;
561 102 my ($enum_typename, $list) = _enum_typename_and_values($field);
562  
563 102 ▶ if ($postgres_version >= 8.003 && $enum_typename) {
Branch: 1 branch
BranchTrueFalseTotal
if ($postgres_version >= 8.003 and $enum_typename) { } 2 100 102
Condition100%3/3
$postgres_version >= "8.003" and $enum_typename
!ll&&!rl&&r
9552
MC/DC100%2/2
$postgres_version >= "8.003" and $enum_typename
$postgres_version >= "8.003" $enum_typename
Truth table100%3/3
A and B
A= $postgres_version >= "8.003"B= $enum_typename
ABresult
0X0
100
111
564 2 my $commalist = join(', ', map { __PACKAGE__->_quote_string($_) } @$list);
565 2 $field_def .= ' ' . $enum_typename;
566 2 my $new_type_def
567 = "DROP TYPE IF EXISTS $enum_typename CASCADE;\n" . "CREATE TYPE $enum_typename AS ENUM ($commalist)";
568 2 ▶ if (!exists $type_defs->{$enum_typename}) {
Branch: 2 branches
BranchTrueFalseTotal
if (not exists $type_defs->{$enum_typename}) { } 2 0 2
elsif ($type_defs->{$enum_typename} ne $new_type_def) { } 0 0 0
569 2 $type_defs->{$enum_typename} = $new_type_def;
570 } elsif ($type_defs->{$enum_typename} ne $new_type_def) {
571 0 die "Attempted to redefine type name '$enum_typename' as a different type.\n";
572 }
573 } else {
574 100 $field_def .= ' ' . convert_datatype($field);
575 }
576  
577 #
578 # Default value
579 #
580 102 __PACKAGE__->_apply_default_value(
581 $field,
582 \$field_def,
583 [
584 'NULL' => \'NULL',
585 'now()' => 'now()',
586 'CURRENT_TIMESTAMP' => 'CURRENT_TIMESTAMP',
587 ],
588 );
589  
590 #
591 # Not null constraint
592 #
593 102 ▶ $field_def .= ' NOT NULL' unless $field->is_nullable;
Branch: 1 branch
BranchTrueFalseTotal
unless $field->is_nullable 38 64 102
594  
595 #
596 # Geometry constraints
597 #
598 102 ▶ if (is_geometry($field)) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_geometry($field)) 3 99 102
599 3 foreach (create_geometry_constraints($field, $options)) {
600 9 my ($cdefs, $fks) = create_constraint($_, $options);
601 9 push @$constraint_defs, @$cdefs;
602 9 push @$fks, @$fks;
603 }
604 }
605  
606 102 return $field_def;
607 }
608 }
609  
610 6 ▶sub create_geometry_constraints {
Subroutine create_geometry_constraints: called CC=1 SCAR=0.0
Pod undocumented
611 7 my ($field, $options) = @_;
612  
613 7 my $fname = _generator($options)->quote($field);
614 7 my @constraints;
615 push @constraints,
616 SQL::Translator::Schema::Constraint->new(
617 name => "enforce_dims_" . $field->name,
618 7 expression => "(ST_NDims($fname) = " . $field->extra->{dimensions} . ")",
619 table => $field->table,
620 type => CHECK_C,
621 );
622  
623 push @constraints,
624 SQL::Translator::Schema::Constraint->new(
625 name => "enforce_srid_" . $field->name,
626 7 expression => "(ST_SRID($fname) = " . $field->extra->{srid} . ")",
627 table => $field->table,
628 type => CHECK_C,
629 );
630 push @constraints,
631 SQL::Translator::Schema::Constraint->new(
632 name => "enforce_geotype_" . $field->name,
633 expression => "(GeometryType($fname) = "
634 . __PACKAGE__->_quote_string($field->extra->{geometry_type})
635 7 . "::text OR $fname IS NULL)",
636 table => $field->table,
637 type => CHECK_C,
638 );
639  
640 7 return @constraints;
641 }
642  
643 88 ▶sub _extract_extras_from_options {
Subroutine _extract_extras_from_options: called CC=5 SCAR=16.4
644 88 my ($options_haver, $dispatcher) = @_;
645 88 for my $opt ($options_haver->options) {
646 7 ▶ if (ref $opt eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $opt eq "HASH") 7 0 7
647 7 for my $key (keys %$opt) {
648 8 my $val = $opt->{$key};
649 8 ▶ next unless defined $val;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $val 0 8 8
650 8 $dispatcher->{ lc $key }->($val);
651 }
652 }
653 }
654 }
655  
656 {
657 6 my %index_name;
658  
659 18 ▶ sub create_index {
Subroutine create_index: called CC=10 SCAR=24.0
Pod documented
660 18 my ($index, $options) = @_;
661  
662 18 my $generator = _generator($options);
663 18 my $table_name = $index->table->name;
664 18 ▶ my $postgres_version = $options->{postgres_version} || 0;
Condition100%2/2
$options->{'postgres_version'} || 0
l!l
117
MC/DC100%1/1
$options->{'postgres_version'} || 0
$options->{'postgres_version'}
Truth table100%2/2
A
A= $options->{'postgres_version'}
Aresult
11
00
665  
666 18 my ($index_def, @constraint_defs);
667  
668 my $name = $index->name
669 18 ▶ || join('_', $table_name, 'idx', ++$index_name{$table_name});
Condition33%1/3
$index->name || join('_', $table_name, 'idx', ++$index_name{$table_name})
l!l&&r!l&&!r
1800
MC/DC0%0/2
$index->name || join('_', $table_name, 'idx', ++$index_name{$table_name})
$index->name join('_', $table_name, 'idx', ++$index_name{$table_name})
Truth table33%1/3
A || B
A= $index->nameB= join('_', $table_name, 'idx', ++$index_name{$table_name})
ABresult
1X1
011
000
670  
671 18 ▶ my $type = $index->type || NORMAL;
Condition50%1/2
$index->type || 'NORMAL'
l!l
180
MC/DC0%0/1
$index->type || 'NORMAL'
$index->type
Truth table50%1/2
A
A= $index->type
Aresult
11
00
672 18 my @fields = $index->fields;
673 18 ▶ return unless @fields;
Branch: 1 branch
BranchTrueFalseTotal
unless @fields 0 18 18
674  
675 18 my %index_extras;
676 _extract_extras_from_options(
677 $index,
678 {
679 2 ▶ using => sub { $index_extras{using} = "USING $_[0]" },
Subroutine __ANON__: called CC=1 SCAR=0.0
680 2 ▶ where => sub { $index_extras{where} = "WHERE $_[0]" },
Subroutine __ANON__: called CC=1 SCAR=0.0
681 2 ▶ include => sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
682 2 my ($value) = @_;
683 2 ▶ return unless $postgres_version >= 11;
Branch: 1 branch
BranchTrueFalseTotal
unless $postgres_version >= 11 1 1 2
684 1 ▶ die 'Include list must be an arrayref'
Branch: 1 branch
BranchTrueFalseTotal
unless ref $value eq "ARRAY" 0 1 1
685 unless ref $value eq 'ARRAY';
686 1 my $value_list = join ', ', @$value;
687 1 $index_extras{include} = "INCLUDE ($value_list)";
688 }
689 }
690 18 );
691  
692 18 my $def_start = 'CONSTRAINT ' . $generator->quote($name) . ' ';
693 18 ▶ my $field_names = '(' . join(", ", (map { $_ =~ /\(.*\)/ ? $_ : ($generator->quote($_)) } @fields)) . ')';
Branch: 1 branch
BranchTrueFalseTotal
$_ =~ /\(.*\)/ ? : 6 16 22
694 18 ▶ if ($type eq PRIMARY_KEY) {
Branch: 3 branches
BranchTrueFalseTotal
if ($type eq 'PRIMARY KEY') { } 0 18 18
elsif ($type eq 'UNIQUE') { } 1 17 18
elsif ($type eq 'NORMAL') { } 17 0 17
695 0 push @constraint_defs, "${def_start}PRIMARY KEY " . $field_names;
696 } elsif ($type eq UNIQUE) {
697 1 push @constraint_defs, "${def_start}UNIQUE " . $field_names;
698 } elsif ($type eq NORMAL) {
699 $index_def
700 = 'CREATE INDEX ' . $generator->quote($name) . ' on ' . $generator->quote($table_name) . ' ' . join ' ',
701 68 grep {defined} $index_extras{using}, $field_names,
702 17 @index_extras{ 'include', 'where' };
703 } else {
704 0 ▶ warn "Unknown index type ($type) on table $table_name.\n"
Branch: 1 branch
BranchTrueFalseTotal
if $SQL::Translator::Producer::PostgreSQL::WARN 0 0 0
705 if $WARN;
706 }
707  
708 18 return $index_def, \@constraint_defs;
709 }
710 }
711  
712 6 ▶sub create_constraint {
Subroutine create_constraint: called CC=22 SCAR=34.4
Pod documented
713 70 my ($c, $options) = @_;
714  
715 70 my $generator = _generator($options);
716 70 ▶ my $postgres_version = $options->{postgres_version} || 0;
Condition50%1/2
$options->{'postgres_version'} || 0
l!l
070
MC/DC0%0/1
$options->{'postgres_version'} || 0
$options->{'postgres_version'}
Truth table50%1/2
A
A= $options->{'postgres_version'}
Aresult
11
00
717 70 my $table_name = $c->table->name;
718 70 my (@constraint_defs, @fks);
719 70 my %constraint_extras;
720 _extract_extras_from_options(
721 $c,
722 {
723 1 ▶ using => sub { $constraint_extras{using} = "USING $_[0]" },
Subroutine __ANON__: called CC=1 SCAR=0.0
724 1 ▶ where => sub { $constraint_extras{where} = "WHERE ( $_[0] )" },
Subroutine __ANON__: called CC=1 SCAR=0.0
725 0 ▶ include => sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
726 0 my ($value) = @_;
727 0 ▶ return unless $postgres_version >= 11;
Branch: 1 branch
BranchTrueFalseTotal
unless $postgres_version >= 11 0 0 0
728 0 ▶ die 'Include list must be an arrayref'
Branch: 1 branch
BranchTrueFalseTotal
unless ref $value eq "ARRAY" 0 0 0
729 unless ref $value eq 'ARRAY';
730 0 my $value_list = join ', ', @$value;
731 0 $constraint_extras{include} = "INCLUDE ( $value_list )";
732 },
733 }
734 70 );
735  
736 70 ▶ my $name = $c->name || '';
Condition100%2/2
$c->name || ''
l!l
4327
MC/DC100%1/1
$c->name || ''
$c->name
Truth table100%2/2
A
A= $c->name
Aresult
11
00
737  
738 70 my @fields = grep {defined} $c->fields;
739  
740 70 my @rfields = grep {defined} $c->reference_fields;
741  
742 70 ▶ return if !@fields && ($c->type ne CHECK_C && $c->type ne EXCLUDE);
Branch: 1 branch
BranchTrueFalseTotal
if not @fields and ($c->type ne "CHECK" and $c->type ne "EXCLUDE") 0 70 70
Condition66%4/6
not @fields and ($c->type ne "CHECK" and $c->type ne "EXCLUDE")
!ll&&!rl&&r
51190
$c->type ne "CHECK" and $c->type ne "EXCLUDE"
!ll&&!rl&&r
1810
MC/DC0%0/3
not @fields and ($c->type ne "CHECK" and $c->type ne "EXCLUDE")
not @fields $c->type ne "CHECK" $c->type ne "EXCLUDE"
Truth table75%3/4
not(A) and B and C
A= not @fieldsB= $c->type ne "CHECK"C= $c->type ne "EXCLUDE"
ABCresult
0XX0
10X0
1100
1111
743 70 ▶ my $def_start = $name ? 'CONSTRAINT ' . $generator->quote($name) : '';
Branch: 1 branch
BranchTrueFalseTotal
$name ? : 43 27 70
744 70 ▶ my $field_names = '(' . join(", ", (map { $_ =~ /\(.*\)/ ? $_ : ($generator->quote($_)) } @fields)) . ')';
Branch: 1 branch
BranchTrueFalseTotal
$_ =~ /\(.*\)/ ? : 4 51 55
745 70 ▶ my $include = $constraint_extras{include} || '';
Condition50%1/2
$constraint_extras{'include'} || ''
l!l
070
MC/DC0%0/1
$constraint_extras{'include'} || ''
$constraint_extras{'include'}
Truth table50%1/2
A
A= $constraint_extras{'include'}
Aresult
11
00
746 70 ▶ if ($c->type eq PRIMARY_KEY) {
Branch: 5 branches
BranchTrueFalseTotal
if ($c->type eq 'PRIMARY KEY') { } 17 53 70
elsif ($c->type eq 'UNIQUE') { } 16 37 53
elsif ($c->type eq 'CHECK') { } 18 19 37
elsif ($c->type eq 'FOREIGN KEY') { } 8 11 19
elsif ($c->type eq 'EXCLUDE') { } 1 10 11
747 17 push @constraint_defs, join ' ', grep $_, $def_start, "PRIMARY KEY", $field_names, $include;
748 } elsif ($c->type eq UNIQUE) {
749 16 push @constraint_defs, join ' ', grep $_, $def_start, "UNIQUE", $field_names, $include;
750 } elsif ($c->type eq CHECK_C) {
751 18 my $expression = $c->expression;
752 18 push @constraint_defs, join ' ', grep $_, $def_start, "CHECK ($expression)";
753 } elsif ($c->type eq FOREIGN_KEY) {
754 8 my $def .= join ' ', grep $_, "ALTER TABLE",
755 $generator->quote($table_name), 'ADD', $def_start,
756 "FOREIGN KEY $field_names";
757 8 $def .= "\n REFERENCES " . $generator->quote($c->reference_table);
758  
759 8 ▶ if (@rfields) {
Branch: 1 branch
BranchTrueFalseTotal
if (@rfields) 8 0 8
760 8 $def .= ' (' . join(', ', map { $generator->quote($_) } @rfields) . ')';
761 }
762  
763 8 ▶ if ($c->match_type) {
Branch: 1 branch
BranchTrueFalseTotal
if ($c->match_type) 0 8 8
764 0 ▶ $def .= ' MATCH ' . ($c->match_type =~ /full/i) ? 'FULL' : 'PARTIAL';
Branch: 1 branch
BranchTrueFalseTotal
' MATCH ' . $c->match_type =~ /full/i ? : 0 0 0
765 }
766  
767 8 ▶ if ($c->on_delete) {
Branch: 1 branch
BranchTrueFalseTotal
if ($c->on_delete) 0 8 8
768 0 $def .= ' ON DELETE ' . $c->on_delete;
769 }
770  
771 8 ▶ if ($c->on_update) {
Branch: 1 branch
BranchTrueFalseTotal
if ($c->on_update) 0 8 8
772 0 $def .= ' ON UPDATE ' . $c->on_update;
773 }
774  
775 8 ▶ if ($c->deferrable) {
Branch: 1 branch
BranchTrueFalseTotal
if ($c->deferrable) 8 0 8
776 8 $def .= ' DEFERRABLE';
777 }
778  
779 8 push @fks, "$def";
780 } elsif ($c->type eq EXCLUDE) {
781 1 ▶ my $using = $constraint_extras{using} || '';
Condition50%1/2
$constraint_extras{'using'} || ''
l!l
10
MC/DC0%0/1
$constraint_extras{'using'} || ''
$constraint_extras{'using'}
Truth table50%1/2
A
A= $constraint_extras{'using'}
Aresult
11
00
782 1 my $expression = $c->expression;
783 1 ▶ my $where = $constraint_extras{where} || '';
Condition50%1/2
$constraint_extras{'where'} || ''
l!l
10
MC/DC0%0/1
$constraint_extras{'where'} || ''
$constraint_extras{'where'}
Truth table50%1/2
A
A= $constraint_extras{'where'}
Aresult
11
00
784 1 push @constraint_defs, join ' ', grep $_, $def_start, 'EXCLUDE', $using, "( $expression )", $include, $where;
785 }
786  
787 70 return \@constraint_defs, \@fks;
788 }
789  
790 14 ▶sub create_trigger {
Subroutine create_trigger: called CC=4 SCAR=13.9
Pod documented
791 14 my ($trigger, $options) = @_;
792 14 my $generator = _generator($options);
793  
794 14 my @statements;
795  
796 push @statements, sprintf('DROP TRIGGER IF EXISTS %s', $generator->quote($trigger->name))
797 14 ▶ if $options->{add_drop_trigger};
Branch: 1 branch
BranchTrueFalseTotal
if $options->{"add_drop_trigger"} 14 0 14
798  
799 14 ▶ my $scope = $trigger->scope || '';
Condition100%2/2
$trigger->scope || ''
l!l
212
MC/DC100%1/1
$trigger->scope || ''
$trigger->scope
Truth table100%2/2
A
A= $trigger->scope
Aresult
11
00
800 14 ▶ $scope = " FOR EACH $scope" if $scope;
Branch: 1 branch
BranchTrueFalseTotal
if $scope 2 12 14
801  
802 push @statements,
803 sprintf(
804 'CREATE TRIGGER %s %s %s ON %s%s %s',
805 $generator->quote($trigger->name),
806 $trigger->perform_action_when,
807 14 join(' OR ', @{ $trigger->database_events }),
808 $generator->quote($trigger->on_table),
809 $scope, $trigger->action,
810 );
811  
812 14 return @statements;
813 }
814  
815 146 ▶sub convert_datatype {
Subroutine convert_datatype: called CC=26 SCAR=32.8
Pod undocumented
816 146 my ($field) = @_;
817  
818 146 my @size = $field->size;
819 146 my $data_type = lc $field->data_type;
820 146 my $array = $data_type =~ s/\[\]$//;
821  
822 146 ▶ if ($data_type eq 'enum') {
Branch: 3 branches
BranchTrueFalseTotal
if ($data_type eq 'enum') { } 2 144 146
elsif ($data_type eq 'set') { } 0 144 144
elsif ($field->is_auto_increment) { } 19 125 144
823  
824 # my $len = 0;
825 # $len = ($len < length($_)) ? length($_) : $len for (@$list);
826 # my $chk_name = mk_name( $table_name.'_'.$field_name, 'chk' );
827 # push @$constraint_defs,
828 # 'CONSTRAINT "$chk_name" CHECK (' . $generator->quote(field_name) .
829 # qq[IN ($commalist))];
830 2 $data_type = 'character varying';
831 } elsif ($data_type eq 'set') {
832 0 $data_type = 'character varying';
833 } elsif ($field->is_auto_increment) {
834 19 ▶ if ((defined $size[0] && $size[0] > 11) or $data_type eq 'bigint') {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $size[0] and $size[0] > 11 or $data_type eq 'bigint') { } 2 17 19
Condition83%5/6
defined $size[0] and $size[0] > 11 or $data_type eq "bigint"
l!l&&r!l&&!r
1117
defined $size[0] and $size[0] > 11
!ll&&!rl&&r
0181
MC/DC66%2/3
defined $size[0] and $size[0] > 11 or $data_type eq "bigint"
defined $size[0] $size[0] > 11 $data_type eq "bigint"
Truth table60%3/5
A and B or C
A= defined $size[0]B= $size[0] > 11C= $data_type eq "bigint"
ABCresult
11X1
0X11
1011
0X00
1000
835 2 $data_type = 'bigserial';
836 } else {
837 17 $data_type = 'serial';
838 }
839 19 undef @size;
840 } else {
841 $data_type
842 = defined $translate{ lc $data_type }
843 125 ▶ ? $translate{ lc $data_type }
Branch: 1 branch
BranchTrueFalseTotal
defined $translate{lc $data_type} ? : 65 60 125
844 : $data_type;
845 }
846  
847 146 ▶ if ($data_type =~ /^time/i || $data_type =~ /^interval/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($data_type =~ /^time/i or $data_type =~ /^interval/i) 13 133 146
Condition100%3/3
$data_type =~ /^time/i or $data_type =~ /^interval/i
l!l&&r!l&&!r
121133
MC/DC100%2/2
$data_type =~ /^time/i or $data_type =~ /^interval/i
$data_type =~ /^time/i $data_type =~ /^interval/i
Truth table100%3/3
A or B
A= $data_type =~ /^time/iB= $data_type =~ /^interval/i
ABresult
1X1
011
000
848 13 ▶ if (defined $size[0] && $size[0] > 6) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $size[0] and $size[0] > 6) 1 12 13
Condition66%2/3
defined $size[0] and $size[0] > 6
!ll&&!rl&&r
0121
MC/DC50%1/2
defined $size[0] and $size[0] > 6
defined $size[0] $size[0] > 6
Truth table66%2/3
A and B
A= defined $size[0]B= $size[0] > 6
ABresult
0X0
100
111
849 1 $size[0] = 6;
850 }
851 }
852  
853 146 ▶ if ($data_type eq 'integer') {
Branch: 1 branch
BranchTrueFalseTotal
if ($data_type eq "integer") 17 129 146
854 17 ▶ if (defined $size[0] && $size[0] > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $size[0] and $size[0] > 0) { } 15 2 17
Condition66%2/3
defined $size[0] and $size[0] > 0
!ll&&!rl&&r
0215
MC/DC50%1/2
defined $size[0] and $size[0] > 0
defined $size[0] $size[0] > 0
Truth table66%2/3
A and B
A= defined $size[0]B= $size[0] > 0
ABresult
0X0
100
111
855 15 ▶ if ($size[0] > 10) {
Branch: 2 branches
BranchTrueFalseTotal
if ($size[0] > 10) { } 10 5 15
elsif ($size[0] < 5) { } 0 5 5
856 10 $data_type = 'bigint';
857 } elsif ($size[0] < 5) {
858 0 $data_type = 'smallint';
859 } else {
860 5 $data_type = 'integer';
861 }
862 } else {
863 2 $data_type = 'integer';
864 }
865 }
866  
867 146 my $type_with_size = join('|',
868 'bit', 'varbit', 'character', 'bit varying', 'character varying',
869 'time', 'timestamp', 'interval', 'numeric', 'float');
870  
871 146 ▶ if ($data_type !~ /$type_with_size/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($data_type =~ /$type_with_size/) 60 86 146
872 60 @size = ();
873 }
874  
875 146 ▶ if (defined $size[0] && $size[0] > 0 && $data_type =~ /^time/i) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $size[0] and $size[0] > 0 and $data_type =~ /^time/i) { } 5 141 146
elsif (defined $size[0] and $size[0] > 0) { } 69 132 201
Condition100%6/6
defined $size[0] and $size[0] > 0 and $data_type =~ /^time/i
!ll&&!rl&&r
72695
defined $size[0] and $size[0] > 0
!ll&&!rl&&r
601274
MC/DC100%3/3
defined $size[0] and $size[0] > 0 and $data_type =~ /^time/i
defined $size[0] $size[0] > 0 $data_type =~ /^time/i
Truth table100%4/4
A and B and C
A= defined $size[0]B= $size[0] > 0C= $data_type =~ /^time/i
ABCresult
0XX0
10X0
1100
1111
876 5 $data_type =~ s/^(time.*?)( with.*)?$/$1($size[0])/;
877 5 ▶ $data_type .= $2 if (defined $2);
Branch: 1 branch
BranchTrueFalseTotal
if defined $2 5 0 5
878 ▶ } elsif (defined $size[0] && $size[0] > 0) {
Condition100%3/3
defined $size[0] and $size[0] > 0
!ll&&!rl&&r
601269
MC/DC100%2/2
defined $size[0] and $size[0] > 0
defined $size[0] $size[0] > 0
Truth table100%3/3
A and B
A= defined $size[0]B= $size[0] > 0
ABresult
0X0
100
111
879 69 $data_type .= '(' . join(',', @size) . ')';
880 }
881 146 ▶ if ($array) {
Branch: 1 branch
BranchTrueFalseTotal
if ($array) 1 145 146
882 1 $data_type .= '[]';
883 }
884  
885 #
886 # Geography
887 #
888 146 ▶ if ($data_type eq 'geography') {
Branch: 1 branch
BranchTrueFalseTotal
if ($data_type eq "geography") 1 145 146
889 1 $data_type .= '(' . $field->extra->{geography_type} . ',' . $field->extra->{srid} . ')';
890 }
891  
892 146 return $data_type;
893 }
894  
895 23 ▶sub alter_field {
Subroutine alter_field: called CC=28 SCAR=33.6
Pod documented
896 23 my ($from_field, $to_field, $options) = @_;
897  
898 23 ▶ die "Can't alter field in another table"
Branch: 1 branch
BranchTrueFalseTotal
if $from_field->table->name ne $to_field->table->name 0 23 23
899 if ($from_field->table->name ne $to_field->table->name);
900  
901 23 my $generator = _generator($options);
902 23 my @out;
903  
904 # drop geometry column and constraints
905 23 ▶ push @out, drop_geometry_column($from_field, $options), drop_geometry_constraints($from_field, $options),
Branch: 1 branch
BranchTrueFalseTotal
if is_geometry($from_field) 1 22 23
906 if is_geometry($from_field);
907  
908 # it's necessary to start with rename column cause this would affect
909 # all of the following statements which would be broken if do the
910 # rename later
911 # BUT: drop geometry is done before the rename, cause it work's on the
912 # $from_field directly
913 23 ▶ push @out,
Branch: 1 branch
BranchTrueFalseTotal
if $from_field->name ne $to_field->name 3 20 23
914 sprintf('ALTER TABLE %s RENAME COLUMN %s TO %s',
915 map($generator->quote($_), $to_field->table->name, $from_field->name, $to_field->name,),)
916 if ($from_field->name ne $to_field->name);
917  
918 23 ▶ push @out,
Branch: 1 branch
BranchTrueFalseTotal
if not $to_field->is_nullable and $from_field->is_nullable 6 17 23
Condition100%3/3
not $to_field->is_nullable and $from_field->is_nullable
!ll&&!rl&&r
1166
MC/DC100%2/2
not $to_field->is_nullable and $from_field->is_nullable
not $to_field->is_nullable $from_field->is_nullable
Truth table100%3/3
not(A) and B
A= not $to_field->is_nullableB= $from_field->is_nullable
ABresult
0X0
100
111
919 sprintf('ALTER TABLE %s ALTER COLUMN %s SET NOT NULL',
920 map($generator->quote($_), $to_field->table->name, $to_field->name),)
921 if (!$to_field->is_nullable and $from_field->is_nullable);
922  
923 23 ▶ push @out,
Branch: 1 branch
BranchTrueFalseTotal
if not $from_field->is_nullable and $to_field->is_nullable 4 19 23
Condition100%3/3
not $from_field->is_nullable and $to_field->is_nullable
!ll&&!rl&&r
1364
MC/DC100%2/2
not $from_field->is_nullable and $to_field->is_nullable
not $from_field->is_nullable $to_field->is_nullable
Truth table100%3/3
not(A) and B
A= not $from_field->is_nullableB= $to_field->is_nullable
ABresult
0X0
100
111
924 sprintf('ALTER TABLE %s ALTER COLUMN %s DROP NOT NULL',
925 map($generator->quote($_), $to_field->table->name, $to_field->name),)
926 if (!$from_field->is_nullable and $to_field->is_nullable);
927  
928 23 my $from_dt = convert_datatype($from_field);
929 23 my $to_dt = convert_datatype($to_field);
930 23 ▶ push @out,
Branch: 1 branch
BranchTrueFalseTotal
if $to_dt ne $from_dt 10 13 23
931 sprintf('ALTER TABLE %s ALTER COLUMN %s TYPE %s',
932 map($generator->quote($_), $to_field->table->name, $to_field->name), $to_dt,)
933 if ($to_dt ne $from_dt);
934  
935 23 my ($from_enum_typename, $from_list) = _enum_typename_and_values($from_field);
936 23 my ($to_enum_typename, $to_list) = _enum_typename_and_values($to_field);
937 23 ▶ if ( $from_enum_typename
Branch: 1 branch
BranchTrueFalseTotal
if ($from_enum_typename and $to_enum_typename and $from_enum_typename eq $to_enum_typename) 1 22 23
Condition66%4/6
$from_enum_typename and $to_enum_typename and $from_enum_typename eq $to_enum_typename
!ll&&!rl&&r
2201
$from_enum_typename and $to_enum_typename
!ll&&!rl&&r
2201
MC/DC33%1/3
$from_enum_typename and $to_enum_typename and $from_enum_typename eq $to_enum_typename
$from_enum_typename $to_enum_typename $from_enum_typename eq $to_enum_typename
Truth table50%2/4
A and B and C
A= $from_enum_typenameB= $to_enum_typenameC= $from_enum_typename eq $to_enum_typename
ABCresult
0XX0
10X0
1100
1111
938 && $to_enum_typename
939 && $from_enum_typename eq $to_enum_typename) {
940 # See if new enum values were added, and update the enum
941 1 my %existing_vals = map +($_ => 1), @$from_list;
942 1 my %desired_vals = map +($_ => 1), @$to_list;
943 1 my @add_vals = grep !$existing_vals{$_}, keys %desired_vals;
944 1 my @del_vals = grep !$desired_vals{$_}, keys %existing_vals;
945 1 ▶ my $pg_ver_ok = ($options->{postgres_version} || 0) >= 9.001;
Condition50%1/2
$options->{'postgres_version'} || 0
l!l
10
MC/DC0%0/1
$options->{'postgres_version'} || 0
$options->{'postgres_version'}
Truth table50%1/2
A
A= $options->{'postgres_version'}
Aresult
11
00
946 1 ▶ push @out, '-- Set $sqlt->producer_args->{postgres_version} >= 9.001 to alter enums'
Branch: 1 branch
BranchTrueFalseTotal
if not $pg_ver_ok and @add_vals 0 1 1
Condition33%1/3
not $pg_ver_ok and @add_vals
!ll&&!rl&&r
100
MC/DC0%0/2
not $pg_ver_ok and @add_vals
not $pg_ver_ok @add_vals
Truth table33%1/3
not(A) and B
A= not $pg_ver_okB= @add_vals
ABresult
0X0
100
111
947 if !$pg_ver_ok && @add_vals;
948 1 for (@add_vals) {
949 1 ▶ push @out, sprintf '%sALTER TYPE %s ADD VALUE IF NOT EXISTS %s',
Branch: 1 branch
BranchTrueFalseTotal
$pg_ver_ok ? : 1 0 1
950 ($pg_ver_ok ? '' : '-- '), $to_enum_typename,
951 $generator->quote_string($_);
952 }
953 1 ▶ push @out, "-- Unimplemented: delete values from enum type '$to_enum_typename': " . join(", ", @del_vals)
Branch: 1 branch
BranchTrueFalseTotal
if @del_vals 0 1 1
954 if @del_vals;
955 }
956  
957 23 my $old_default = $from_field->default_value;
958 23 my $new_default = $to_field->default_value;
959 23 my $default_value = $to_field->default_value;
960  
961 # fixes bug where output like this was created:
962 # ALTER TABLE users ALTER COLUMN column SET DEFAULT ThisIsUnescaped;
963 23 ▶ if (ref $default_value eq "SCALAR") {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $default_value eq 'SCALAR') { } 1 22 23
elsif (defined $default_value and $to_dt =~ /^(character|text|timestamp|date)/imsx) { } 3 34 37
964 1 $default_value = $$default_value;
965 ▶ } elsif (defined $default_value
Condition100%3/3
defined $default_value and $to_dt =~ /^(character|text|timestamp|date)/imsx
!ll&&!rl&&r
1543
MC/DC100%2/2
defined $default_value and $to_dt =~ /^(character|text|timestamp|date)/imsx
defined $default_value $to_dt =~ /^(character|text|timestamp|date)/imsx
Truth table100%3/3
A and B
A= defined $default_valueB= $to_dt =~ /^(character|text|timestamp|date)/imsx
ABresult
0X0
100
111
966 && $to_dt =~ /^(character|text|timestamp|date)/xsmi) {
967 3 $default_value = __PACKAGE__->_quote_string($default_value);
968 }
969  
970 23 ▶ push @out,
Branch: 1 branch
BranchTrueFalseTotal
if defined $new_default and !defined($old_default) || $old_default ne $new_default 5 18 23
Condition100%6/6
defined $new_default and !defined($old_default) || $old_default ne $new_default
!ll&&!rl&&r
1535
not defined $old_default or $old_default ne $new_default
l!l&&r!l&&!r
323
MC/DC100%3/3
defined $new_default and !defined($old_default) || $old_default ne $new_default
defined $new_default not defined $old_default $old_default ne $new_default
Truth table100%4/4
A and not(B) or C
A= defined $new_defaultB= not defined $old_defaultC= $old_default ne $new_default
ABCresult
0XX0
1000
11X1
1011
971 sprintf(
972 'ALTER TABLE %s ALTER COLUMN %s SET DEFAULT %s',
973 map($generator->quote($_), $to_field->table->name, $to_field->name,),
974 $default_value,
975 )
976 if (defined $new_default
977 && (!defined $old_default || $old_default ne $new_default));
978  
979 # fixes bug where removing the DEFAULT statement of a column
980 # would result in no change
981  
982 23 ▶ push @out,
Branch: 1 branch
BranchTrueFalseTotal
if not defined $new_default and defined $old_default 1 22 23
Condition100%3/3
not defined $new_default and defined $old_default
!ll&&!rl&&r
8141
MC/DC100%2/2
not defined $new_default and defined $old_default
not defined $new_default defined $old_default
Truth table100%3/3
not(A) and B
A= not defined $new_defaultB= defined $old_default
ABresult
0X0
100
111
983 sprintf('ALTER TABLE %s ALTER COLUMN %s DROP DEFAULT',
984 map($generator->quote($_), $to_field->table->name, $to_field->name,),)
985 if (!defined $new_default && defined $old_default);
986  
987 # add geometry column and constraints
988 23 ▶ push @out, add_geometry_column($to_field, $options), add_geometry_constraints($to_field, $options),
Branch: 1 branch
BranchTrueFalseTotal
if is_geometry($to_field) 1 22 23
989 if is_geometry($to_field);
990  
991 23 ▶ return wantarray ? @out : join(";\n", @out);
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 14 9 23
992 }
993  
994 2 ▶sub rename_field { alter_field(@_) }
Subroutine rename_field: called CC=1 SCAR=0.0
Pod undocumented
995  
996 6 ▶sub add_field {
Subroutine add_field: called CC=2 SCAR=6.9
Pod documented
997 6 my ($new_field, $options) = @_;
998  
999 6 my $out = sprintf(
1000 'ALTER TABLE %s ADD COLUMN %s',
1001 _generator($options)->quote($new_field->table->name),
1002 create_field($new_field, $options)
1003 );
1004 6 ▶ $out .= ";\n" . add_geometry_column($new_field, $options) . ";\n" . add_geometry_constraints($new_field, $options)
Branch: 1 branch
BranchTrueFalseTotal
if is_geometry($new_field) 1 5 6
1005 if is_geometry($new_field);
1006 6 return $out;
1007  
1008 }
1009  
1010 4 ▶sub drop_field {
Subroutine drop_field: called CC=2 SCAR=6.9
Pod documented
1011 4 my ($old_field, $options) = @_;
1012  
1013 4 my $generator = _generator($options);
1014  
1015 4 my $out = sprintf(
1016 'ALTER TABLE %s DROP COLUMN %s',
1017 $generator->quote($old_field->table->name),
1018 $generator->quote($old_field->name)
1019 );
1020 4 ▶ $out .= ";\n" . drop_geometry_column($old_field, $options)
Branch: 1 branch
BranchTrueFalseTotal
if is_geometry($old_field) 1 3 4
1021 if is_geometry($old_field);
1022 4 return $out;
1023 }
1024  
1025 5 ▶sub add_geometry_column {
Subroutine add_geometry_column: called CC=2 SCAR=6.9
Pod undocumented
1026 5 my ($field, $options) = @_;
1027  
1028 return sprintf(
1029 "INSERT INTO geometry_columns VALUES (%s,%s,%s,%s,%s,%s,%s)",
1030 map(__PACKAGE__->_quote_string($_),
1031 '',
1032 $field->table->schema->name,
1033 $options->{table} ? $options->{table} : $field->table->name,
1034 $field->name,
1035 $field->extra->{dimensions},
1036 $field->extra->{srid},
1037 $field->extra->{geometry_type},
1038 5 ▶ ),
Branch: 1 branch
BranchTrueFalseTotal
$options->{'table'} ? : 1 4 5
1039 );
1040 }
1041  
1042 4 ▶sub drop_geometry_column {
Subroutine drop_geometry_column: called CC=1 SCAR=0.0
Pod undocumented
1043 4 my ($field) = @_;
1044  
1045 return
1046 4 sprintf("DELETE FROM geometry_columns WHERE f_table_schema = %s AND f_table_name = %s AND f_geometry_column = %s",
1047 map(__PACKAGE__->_quote_string($_), $field->table->schema->name, $field->table->name, $field->name,),);
1048 }
1049  
1050 3 ▶sub add_geometry_constraints {
Subroutine add_geometry_constraints: called CC=1 SCAR=0.0
Pod undocumented
1051 3 my ($field, $options) = @_;
1052  
1053 3 return join(";\n", map { alter_create_constraint($_, $options) } create_geometry_constraints($field, $options));
1054 }
1055  
1056 1 ▶sub drop_geometry_constraints {
Subroutine drop_geometry_constraints: called CC=1 SCAR=0.0
Pod undocumented
1057 1 my ($field, $options) = @_;
1058  
1059 1 return join(";\n", map { alter_drop_constraint($_, $options) } create_geometry_constraints($field, $options));
1060  
1061 }
1062  
1063 3 ▶sub alter_table {
Subroutine alter_table: called CC=2 SCAR=6.9
Pod undocumented
1064 3 my ($to_table, $options) = @_;
1065 3 my $generator = _generator($options);
1066 3 my $out = sprintf('ALTER TABLE %s %s', $generator->quote($to_table->name), $options->{alter_table_action});
1067 $out .= ";\n" . $options->{geometry_changes}
1068 3 ▶ if $options->{geometry_changes};
Branch: 1 branch
BranchTrueFalseTotal
if $options->{"geometry_changes"} 1 2 3
1069 3 return $out;
1070 }
1071  
1072 3 ▶sub rename_table {
Subroutine rename_table: called CC=2 SCAR=6.9
Pod undocumented
1073 3 my ($old_table, $new_table, $options) = @_;
1074 3 my $generator = _generator($options);
1075 3 $options->{alter_table_action} = "RENAME TO " . $generator->quote($new_table);
1076  
1077 my @geometry_changes
1078 1 = map { drop_geometry_column($_, $options), add_geometry_column($_, { %{$options}, table => $new_table }), }
1079 3 grep { is_geometry($_) } $old_table->get_fields;
1080  
1081 3 ▶ $options->{geometry_changes} = join(";\n", @geometry_changes)
Branch: 1 branch
BranchTrueFalseTotal
if @geometry_changes 1 2 3
1082 if @geometry_changes;
1083  
1084 3 return alter_table($old_table, $options);
1085 }
1086  
1087 1 ▶sub alter_create_index {
Subroutine alter_create_index: called CC=2 SCAR=7.0
Pod undocumented
1088 1 my ($index, $options) = @_;
1089 1 my $generator = _generator($options);
1090 1 my ($idef, $constraints) = create_index($index, $options);
1091 1 ▶ return $index->type eq NORMAL
Branch: 1 branch
BranchTrueFalseTotal
$index->type eq 'NORMAL' ? : 0 1 1
1092 ? $idef
1093 : sprintf('ALTER TABLE %s ADD %s', $generator->quote($index->table->name), join(q{}, @$constraints));
1094 }
1095  
1096 1 ▶sub alter_drop_index {
Subroutine alter_drop_index: called CC=1 SCAR=0.0
Pod undocumented
1097 1 my ($index, $options) = @_;
1098 1 return 'DROP INDEX ' . _generator($options)->quote($index->name);
1099 }
1100  
1101 14 ▶sub alter_drop_constraint {
Subroutine alter_drop_constraint: called CC=5 SCAR=16.1
Pod undocumented
1102 14 my ($c, $options) = @_;
1103 14 my $generator = _generator($options);
1104  
1105 # NOT NULL constraint does not require a DROP CONSTRAINT statement
1106 14 ▶ if ($c->type eq NOT_NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if ($c->type eq "NOT NULL") 2 12 14
1107 2 return;
1108 }
1109  
1110 # attention: Postgres has a very special naming structure for naming
1111 # foreign keys and primary keys. It names them using the name of the
1112 # table as prefix and fkey or pkey as suffix, concatenated by an underscore
1113 12 my $c_name;
1114 12 ▶ if ($c->name) {
Branch: 1 branch
BranchTrueFalseTotal
if ($c->name) { } 9 3 12
1115  
1116 # Already has a name, just use it
1117 9 $c_name = $c->name;
1118 } else {
1119 # if the name is dotted we need the table, not schema nor database
1120 3 my ($tablename) = reverse split /[.]/, $c->table->name;
1121 3 ▶ if ($c->type eq FOREIGN_KEY) {
Branch: 2 branches
BranchTrueFalseTotal
if ($c->type eq 'FOREIGN KEY') { } 1 2 3
elsif ($c->type eq 'PRIMARY KEY') { } 2 0 2
1122  
1123 # Doesn't have a name, and is foreign key, append '_fkey'
1124 1 $c_name = $tablename . '_' . ($c->fields)[0] . '_fkey';
1125 } elsif ($c->type eq PRIMARY_KEY) {
1126  
1127 # Doesn't have a name, and is primary key, append '_pkey'
1128 2 $c_name = $tablename . '_pkey';
1129 }
1130 }
1131  
1132 12 return sprintf('ALTER TABLE %s DROP CONSTRAINT %s', map { $generator->quote($_) } $c->table->name, $c_name,);
1133 }
1134  
1135 14 ▶sub alter_create_constraint {
Subroutine alter_create_constraint: called CC=4 SCAR=13.9
Pod undocumented
1136 14 my ($index, $options) = @_;
1137 14 my $generator = _generator($options);
1138 14 my ($defs, $fks) = create_constraint(@_);
1139  
1140 # return if there are no constraint definitions so we don't run
1141 # into output like this:
1142 # ALTER TABLE users ADD ;
1143  
1144 14 ▶ return unless (@{$defs} || @{$fks});
Branch: 1 branch
BranchTrueFalseTotal
unless @{$defs;} or @{$fks;} 0 14 14
Condition66%2/3
@{$defs;} or @{$fks;}
l!l&&r!l&&!r
1310
MC/DC0%0/2
@{$defs;} or @{$fks;}
@{$defs;} @{$fks;}
Truth table66%2/3
A or B
A= @{$defs;}B= @{$fks;}
ABresult
1X1
011
000
1145 return $index->type eq FOREIGN_KEY
1146 1 ? join(q{}, @{$fks})
1147 14 ▶ : join(' ', 'ALTER TABLE', $generator->quote($index->table->name), 'ADD', join(q{}, @{$defs}, @{$fks}));
Branch: 1 branch
BranchTrueFalseTotal
$index->type eq 'FOREIGN KEY' ? : 1 13 14
1148 }
1149  
1150 3 ▶sub drop_table {
Subroutine drop_table: called CC=2 SCAR=6.9
Pod undocumented
1151 3 my ($table, $options) = @_;
1152 3 my $generator = _generator($options);
1153 3 my $out = "DROP TABLE " . $generator->quote($table) . " CASCADE";
1154  
1155 1 my @geometry_drops = map { drop_geometry_column($_); }
1156 3 grep { is_geometry($_) } $table->get_fields;
1157  
1158 3 ▶ $out .= join(";\n", '', @geometry_drops) if @geometry_drops;
Branch: 1 branch
BranchTrueFalseTotal
if @geometry_drops 1 2 3
1159 3 return $out;
1160 }
1161  
1162 12 ▶sub batch_alter_table {
Subroutine batch_alter_table: called CC=4 SCAR=15.4
Pod undocumented
1163 12 my ($table, $diff_hash, $options) = @_;
1164  
1165 # as long as we're not renaming the table we don't need to be here
1166 12 ▶ if (@{ $diff_hash->{rename_table} } == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$diff_hash->{"rename_table"};} == 0) 10 2 12
1167 10 return batch_alter_table_statements($diff_hash, $options);
1168 }
1169  
1170 # first we need to perform drops which are on old table
1171 2 my @sql = batch_alter_table_statements(
1172 $diff_hash, $options, qw(
1173 alter_drop_constraint
1174 alter_drop_index
1175 drop_field
1176 )
1177 );
1178  
1179 # next comes the rename_table
1180 2 my $old_table = $diff_hash->{rename_table}[0][0];
1181 2 push @sql, rename_table($old_table, $table, $options);
1182  
1183 # for alter_field (and so also rename_field) we need to make sure old
1184 # field has table name set to new table otherwise calling alter_field dies
1185 0 ▶ $diff_hash->{alter_field} = [ map { $_->[0]->table($table) && $_ } @{ $diff_hash->{alter_field} } ];
Condition0%0/3
$_->[0]->table($table) && $_
!ll&&!rl&&r
000
MC/DC0%0/2
$_->[0]->table($table) && $_
$_->[0]->table($table) $_
Truth table0%0/3
A && B
A= $_->[0]->table($table)B= $_
ABresult
0X0
100
111
1186 0 ▶ $diff_hash->{rename_field} = [ map { $_->[0]->table($table) && $_ } @{ $diff_hash->{rename_field} } ];
Condition0%0/3
$_->[0]->table($table) && $_
!ll&&!rl&&r
000
MC/DC0%0/2
$_->[0]->table($table) && $_
$_->[0]->table($table) $_
Truth table0%0/3
A && B
A= $_->[0]->table($table)B= $_
ABresult
0X0
100
111
1187  
1188 # now add everything else
1189 2 push @sql, batch_alter_table_statements(
1190 $diff_hash, $options, qw(
1191 add_field
1192 alter_field
1193 rename_field
1194 alter_create_index
1195 alter_create_constraint
1196 alter_table
1197 )
1198 );
1199  
1200 2 return @sql;
1201 }
1202  
1203 6 1;
1204  
1205 # -------------------------------------------------------------------
1206 # Life is full of misery, loneliness, and suffering --
1207 # and it's all over much too soon.
1208 # Woody Allen
1209 # -------------------------------------------------------------------
1210  
1211 =pod
1212  
1213 =head1 SEE ALSO
1214  
1215 SQL::Translator, SQL::Translator::Producer::Oracle.
1216  
1217 =head1 AUTHOR
1218  
1219 Ken Youens-Clark E<lt>kclark@cpan.orgE<gt>.
1220  
1221 =cut
« blib/lib/SQL/Translator/Schema/Procedure.pm ↑ Summary blib/lib/SQL/Translator/Parser/Excel.pm »