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

filter: Statementstmt 90.5% 394 / 435 Branchbran 72.7% 179 / 246 Conditioncond 76.7% 86 / 112 MC/DCmcdc 61.5% 40 / 65 Subroutinesub 94.7% 36 / 38 Podpod 35.7% 10 / 28 totaltotal 80.6% 745 / 924 SCAR 57.9
CC
176
Cov
83%
mk_name
272.0
convert_datatype
26.9
create_table
25.6
CRAP 325.5
« blib/lib/SQL/Translator/Parser/DBI/PostgreSQL.pm ↑ Summary blib/lib/SQL/Translator/Parser/DB2.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.63';
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 6 ▶use SQL::Translator::Utils qw(debug header_comment parse_dbms_version batch_alter_table_statements normalize_quote_options);
Subroutine BEGIN: called CC=1 SCAR=0.0
114 6 ▶use SQL::Translator::Generator::DDL::PostgreSQL;
Subroutine BEGIN: called CC=1 SCAR=0.0
115 6 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
116  
117 6 ▶use constant MAX_ID_LENGTH => 62;
Subroutine BEGIN: called CC=1 SCAR=0.0
118  
119 {
120 6 my ($quoting_generator, $nonquoting_generator);
121 sub _generator {
122 321 ▶ my $options = shift;
Subroutine _generator: called CC=5 SCAR=16.3
123 321 ▶ return $options->{generator} if exists $options->{generator};
Branch: 1 branch
BranchTrueFalseTotal
if exists $options->{"generator"} 159 162 321
124  
125 162 ▶ return normalize_quote_options($options)
Branch: 1 branch
BranchTrueFalseTotal
normalize_quote_options($options) ? : 85 77 162
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
23920
$quoting_generator ||= "SQL::Translator::Generator::DDL::PostgreSQL"->new
l!l&&r!l&&!r
7960
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
126 ? $quoting_generator ||= SQL::Translator::Generator::DDL::PostgreSQL->new
127 : $nonquoting_generator ||= SQL::Translator::Generator::DDL::PostgreSQL->new(
128 quote_chars => [],
129 );
130 }
131 }
132  
133 6 my ( %translate );
134  
135 BEGIN {
136  
137 6 ▶ %translate = (
Subroutine BEGIN: called CC=1 SCAR=0.0
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 FOREIGN KEY ( column_name [, ... ] ) REFERENCES reftable [ ( refcolumn [, ... ] ) ]
208 [ MATCH FULL | MATCH PARTIAL ] [ ON DELETE action ] [ ON UPDATE action ] }
209 [ DEFERRABLE | NOT DEFERRABLE ] [ INITIALLY DEFERRED | INITIALLY IMMEDIATE ]
210  
211 =head1 Create Index Syntax
212  
213 CREATE [ UNIQUE ] INDEX index_name ON table
214 [ USING acc_method ] ( column [ ops_name ] [, ...] )
215 [ INCLUDE ( column [, ...] ) ]
216 [ WHERE predicate ]
217 CREATE [ UNIQUE ] INDEX index_name ON table
218 [ USING acc_method ] ( func_name( column [, ... ]) [ ops_name ] )
219 [ WHERE predicate ]
220  
221 =cut
222  
223 sub produce {
224 8 ▶ my $translator = shift;
Subroutine produce: called CC=10 SCAR=23.2
Pod documented
225 8 local $DEBUG = $translator->debug;
226 8 local $WARN = $translator->show_warnings;
227 8 my $no_comments = $translator->no_comments;
228 8 my $add_drop_table = $translator->add_drop_table;
229 8 my $schema = $translator->schema;
230 8 my $pargs = $translator->producer_args;
231 my $postgres_version = parse_dbms_version(
232 8 $pargs->{postgres_version}, 'perl'
233 );
234  
235 8 my $generator = _generator({ quote_identifiers => $translator->quote_identifiers });
236  
237 8 my @output;
238 8 ▶ push @output, header_comment unless ($no_comments);
Branch: 1 branch
BranchTrueFalseTotal
unless $no_comments 0 8 8
239  
240 8 my (@table_defs, @fks);
241 8 my %type_defs;
242 8 for my $table ( $schema->get_tables ) {
243  
244 my ($table_def, $fks) = create_table($table, {
245 generator => $generator,
246 no_comments => $no_comments,
247 postgres_version => $postgres_version,
248 add_drop_table => $add_drop_table,
249 type_defs => \%type_defs,
250 attach_comments => $pargs->{attach_comments}
251 22 });
252  
253 22 push @table_defs, $table_def;
254 22 push @fks, @$fks;
255 }
256  
257 8 for my $view ( $schema->get_views ) {
258 5 push @table_defs, create_view($view, {
259 postgres_version => $postgres_version,
260 add_drop_view => $add_drop_table,
261 generator => $generator,
262 no_comments => $no_comments,
263 });
264 }
265  
266 8 for my $trigger ( $schema->get_triggers ) {
267 14 push @table_defs, create_trigger( $trigger, {
268 add_drop_trigger => $add_drop_table,
269 generator => $generator,
270 no_comments => $no_comments,
271 });
272 }
273  
274 8 push @output, map { "$_;\n\n" } values %type_defs;
275 8 push @output, map { "$_;\n\n" } @table_defs;
276 8 ▶ if ( @fks ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@fks) 5 3 8
277 5 ▶ push @output, "--\n-- Foreign Key Definitions\n--\n\n" unless $no_comments;
Branch: 1 branch
BranchTrueFalseTotal
unless $no_comments 0 5 5
278 5 push @output, map { "$_;\n\n" } @fks;
279 }
280  
281 8 ▶ if ( $WARN ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($SQL::Translator::Producer::PostgreSQL::WARN) 1 7 8
282 1 ▶ if ( %truncated ) {
Branch: 1 branch
BranchTrueFalseTotal
if (%truncated) 0 1 1
283 0 warn "Truncated " . keys( %truncated ) . " names:\n";
284 0 warn "\t" . join( "\n\t", sort keys %truncated ) . "\n";
285 }
286 }
287  
288 return wantarray
289 ? @output
290 8 ▶ : join ('', @output);
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 2 6 8
291 }
292  
293 {
294 6 my %global_names;
295 sub mk_name {
296 0 ▶ my $basename = shift || '';
Subroutine mk_name: not called CC=16 SCAR=56.1
Pod undocumented
Condition0%0/2
shift() || ''
l!l
00
MC/DC0%0/1
shift() || ''
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
297 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
298 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
299 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
300 0 my $basename_orig = $basename;
301  
302 0 ▶ my $max_name = $type
Branch: 1 branch
BranchTrueFalseTotal
$type ? : 0 0 0
303 ? MAX_ID_LENGTH - (length($type) + 1)
304 : MAX_ID_LENGTH;
305 0 ▶ $basename = substr( $basename, 0, $max_name )
Branch: 1 branch
BranchTrueFalseTotal
if length $basename > $max_name 0 0 0
306 if length( $basename ) > $max_name;
307 0 ▶ my $name = $type ? "${type}_$basename" : $basename;
Branch: 1 branch
BranchTrueFalseTotal
$type ? : 0 0 0
308  
309 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
310 0 ▶ my $show_type = $type ? "+'$type'" : "";
Branch: 1 branch
BranchTrueFalseTotal
$type ? : 0 0 0
311 0 ▶ warn "Truncating '$basename_orig'$show_type to ", MAX_ID_LENGTH,
Branch: 1 branch
BranchTrueFalseTotal
if $SQL::Translator::Producer::PostgreSQL::WARN 0 0 0
312 " character limit to make '$name'\n" if $WARN;
313 0 $truncated{ $basename_orig } = $name;
314 }
315  
316 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
317 0 ▶ if ( my $prev = $scope->{ $name } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $prev = $scope->{$name}) 0 0 0
318 0 my $name_orig = $name;
319 0 $name .= sprintf( "%02d", ++$prev );
320 0 ▶ substr($name, MAX_ID_LENGTH - 3) = "00"
Branch: 1 branch
BranchTrueFalseTotal
if length $name > 62 0 0 0
321 if length( $name ) > MAX_ID_LENGTH;
322  
323 0 ▶ warn "The name '$name_orig' has been changed to ",
Branch: 1 branch
BranchTrueFalseTotal
if $SQL::Translator::Producer::PostgreSQL::WARN 0 0 0
324 "'$name' to make it unique.\n" if $WARN;
325  
326 0 $scope->{ $name_orig }++;
327 }
328  
329 0 $scope->{ $name }++;
330 0 return $name;
331 }
332 }
333  
334 6 sub is_geometry {
335 238 ▶ my $field = shift;
Subroutine is_geometry: called CC=2 SCAR=6.9
Pod undocumented
336 238 ▶ return 1 if $field->data_type eq 'geometry';
Branch: 1 branch
BranchTrueFalseTotal
if $field->data_type eq "geometry" 10 228 238
337 }
338  
339 sub is_geography {
340 0 ▶ my $field = shift;
Subroutine is_geography: not called CC=2 SCAR=17.9
Pod undocumented
341 0 ▶ return 1 if $field->data_type eq 'geography';
Branch: 1 branch
BranchTrueFalseTotal
if $field->data_type eq "geography" 0 0 0
342 }
343  
344 sub create_table
345 {
346 24 ▶ my ($table, $options) = @_;
Subroutine create_table: called CC=24 SCAR=32.4
Pod documented
347  
348 24 my $generator = _generator($options);
349 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
350 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
351 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
352 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
353 24 my $attach_comments = $options->{attach_comments};
354  
355 24 ▶ my $table_name = $table->name or next;
Branch: 1 branch
BranchTrueFalseTotal
my $table_name = $table->name or next 0 24 24
356 24 my $table_name_qt = $generator->quote($table_name);
357  
358 24 my ( @comments, @field_defs, @index_defs, @constraint_defs, @fks );
359  
360 24 ▶ push @comments, "--\n-- Table: $table_name\n--\n" unless $no_comments;
Branch: 1 branch
BranchTrueFalseTotal
unless $no_comments 2 22 24
361  
362 24 my @comment_statements;
363 24 ▶ if ( my $comments = $table->comments ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $comments = $table->comments) 1 23 24
364 1 ▶ if ( $attach_comments) {
Branch: 2 branches
BranchTrueFalseTotal
if ($attach_comments) { } 1 0 1
elsif (not $no_comments) { } 0 0 0
365 # this follows the example in the MySQL producer, where all comments are added as
366 # table comments, even though they could have originally been parsed as DDL comments
367 # quoted via $$ string so there can be 'quotes' inside the comments
368 1 my $comment_ddl = "COMMENT on TABLE $table_name_qt IS \$comment\$$comments\$comment\$";
369 1 push @comment_statements, $comment_ddl;
370 } elsif (!$no_comments) {
371 0 $comments =~ s/^/-- /mg;
372 0 push @comments, "-- Comments:\n$comments\n--\n";
373 }
374 }
375  
376 #
377 # Fields
378 #
379 24 for my $field ( $table->get_fields ) {
380 74 push @field_defs, create_field($field, {
381 generator => $generator,
382 postgres_version => $postgres_version,
383 type_defs => $type_defs,
384 constraint_defs => \@constraint_defs,
385 attach_comments => $attach_comments
386 });
387 74 ▶ if ( $attach_comments ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($attach_comments) 12 62 74
388 12 my $field_comments = $field->comments;
389 12 ▶ next unless $field_comments;
Branch: 1 branch
BranchTrueFalseTotal
unless $field_comments 10 2 12
390 2 my $field_name_qt = $generator->quote($field->name);
391 2 my $comment_ddl =
392 "COMMENT on COLUMN $table_name_qt.$field_name_qt IS \$comment\$$field_comments\$comment\$";
393 2 push @comment_statements, $comment_ddl;
394 }
395  
396 }
397  
398 #
399 # Index Declarations
400 #
401 24 for my $index ( $table->get_indices ) {
402 5 my ($idef, $constraints) = create_index($index, {
403 generator => $generator,
404 postgres_version => $postgres_version,
405 });
406 5 ▶ $idef and push @index_defs, $idef;
Branch: 1 branch
BranchTrueFalseTotal
$idef and push @index_defs, $idef 5 0 5
407 5 push @constraint_defs, @$constraints;
408 }
409  
410 #
411 # Table constraints
412 #
413 24 for my $c ( $table->get_constraints ) {
414 35 my ($cdefs, $fks) = create_constraint($c, {
415 generator => $generator,
416 });
417 35 push @constraint_defs, @$cdefs;
418 35 push @fks, @$fks;
419 }
420  
421  
422 24 my $create_statement = join("\n", @comments);
423 24 ▶ if ($add_drop_table) {
Branch: 1 branch
BranchTrueFalseTotal
if ($add_drop_table) 20 4 24
424 20 ▶ if ($postgres_version >= 8.002) {
Branch: 1 branch
BranchTrueFalseTotal
if ($postgres_version >= 8.002) { } 0 20 20
425 0 $create_statement .= "DROP TABLE IF EXISTS $table_name_qt CASCADE;\n";
426 } else {
427 20 $create_statement .= "DROP TABLE $table_name_qt CASCADE;\n";
428 }
429 }
430 24 ▶ my $temporary = $table->extra->{temporary} ? "TEMPORARY " : "";
Branch: 1 branch
BranchTrueFalseTotal
$table->extra->{'temporary'} ? : 0 24 24
431 $create_statement .= "CREATE ${temporary}TABLE $table_name_qt (\n" .
432 24 join( ",\n", map { " $_" } @field_defs, @constraint_defs ).
433 "\n)"
434 ;
435 24 ▶ $create_statement .= @index_defs ? ';' : q{};
Branch: 1 branch
BranchTrueFalseTotal
@index_defs ? : 5 19 24
436 24 ▶ $create_statement .= ( $create_statement =~ /;$/ ? "\n" : q{} )
Branch: 1 branch
BranchTrueFalseTotal
$create_statement =~ /;$/ ? : 5 19 24
437 . join(";\n", @index_defs);
438  
439 #
440 # Geometry
441 #
442 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
443 1 ▶ $create_statement .= join(";\n", '', map{ drop_geometry_column($_, $options) } @geometry_columns) if $options->{add_drop_table};
Branch: 1 branch
BranchTrueFalseTotal
if $options->{"add_drop_table"} 0 1 1
444 1 $create_statement .= join(";\n", '', map{ add_geometry_column($_, $options) } @geometry_columns);
445 }
446  
447 24 ▶ if (@comment_statements) {
Branch: 1 branch
BranchTrueFalseTotal
if (@comment_statements) 2 22 24
448 2 $create_statement .= join(";\n", '', @comment_statements);
449 }
450  
451 24 return $create_statement, \@fks;
452 }
453  
454 sub create_view {
455 10 ▶ my ($view, $options) = @_;
Subroutine create_view: called CC=12 SCAR=24.9
Pod documented
456 10 my $generator = _generator($options);
457 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
458 10 my $add_drop_view = $options->{add_drop_view};
459  
460 10 my $view_name = $view->name;
461 10 debug("PKG: Looking at view '${view_name}'\n");
462  
463 10 my $create = '';
464 $create .= "--\n-- View: " . $generator->quote($view_name) . "\n--\n"
465 10 ▶ unless $options->{no_comments};
Branch: 1 branch
BranchTrueFalseTotal
unless $options->{"no_comments"} 0 10 10
466 10 ▶ if ($add_drop_view) {
Branch: 1 branch
BranchTrueFalseTotal
if ($add_drop_view) 7 3 10
467 7 ▶ if ($postgres_version >= 8.002) {
Branch: 1 branch
BranchTrueFalseTotal
if ($postgres_version >= 8.002) { } 1 6 7
468 1 $create .= "DROP VIEW IF EXISTS " . $generator->quote($view_name) . ";\n";
469 } else {
470 6 $create .= "DROP VIEW " . $generator->quote($view_name) . ";\n";
471 }
472 }
473 10 $create .= 'CREATE';
474  
475 10 my $extra = $view->extra;
476 10 ▶ $create .= " TEMPORARY" 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
477 10 ▶ $create .= " MATERIALIZED" 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
478 10 $create .= " VIEW " . $generator->quote($view_name);
479  
480 10 ▶ if ( my @fields = $view->fields ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@fields) = $view->fields) 9 1 10
481 9 my $field_list = join ', ', map { $generator->quote($_) } @fields;
482 9 $create .= " ( ${field_list} )";
483 }
484  
485 10 ▶ if ( my $sql = $view->sql ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $sql = $view->sql) 10 0 10
486 10 $create .= " AS\n ${sql}\n";
487 }
488  
489 10 ▶ if ( $extra->{check_option} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($extra->{"check_option"}) 1 9 10
490 1 $create .= ' WITH ' . uc $extra->{check_option} . ' CHECK OPTION';
491 }
492  
493 10 return $create;
494 }
495  
496 {
497  
498 6 my %field_name_scope;
499  
500 sub create_field
501 {
502 102 ▶ my ($field, $options) = @_;
Subroutine create_field: called CC=16 SCAR=27.8
Pod documented
503  
504 102 my $generator = _generator($options);
505 102 my $table_name = $field->table->name;
506 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
507 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
508 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
509 102 my $attach_comments = $options->{attach_comments};
510  
511 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
512 102 my $field_name = $field->name;
513  
514 102 my $field_comments = '';
515 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
516 4 $comments =~ s/(?<!\A)^/ -- /mg;
517 4 $field_comments = "-- $comments\n ";
518 }
519  
520 102 my $field_def = $field_comments . $generator->quote($field_name);
521  
522 #
523 # Datatype
524 #
525 102 my $data_type = lc $field->data_type;
526 102 my %extra = $field->extra;
527 102 ▶ my $list = $extra{'list'} || [];
Condition100%2/2
$extra{'list'} || []
l!l
2100
MC/DC100%1/1
$extra{'list'} || []
$extra{'list'}
Truth table100%2/2
A
A= $extra{'list'}
Aresult
11
00
528 102 my $commalist = join( ', ', map { __PACKAGE__->_quote_string($_) } @$list );
529  
530 102 ▶ if ($postgres_version >= 8.003 && $data_type eq 'enum') {
Branch: 1 branch
BranchTrueFalseTotal
if ($postgres_version >= 8.003 and $data_type eq 'enum') { } 2 100 102
Condition100%3/3
$postgres_version >= "8.003" and $data_type eq "enum"
!ll&&!rl&&r
9552
MC/DC100%2/2
$postgres_version >= "8.003" and $data_type eq "enum"
$postgres_version >= "8.003" $data_type eq "enum"
Truth table100%3/3
A and B
A= $postgres_version >= "8.003"B= $data_type eq "enum"
ABresult
0X0
100
111
531 2 ▶ my $type_name = $extra{'custom_type_name'} || $field->table->name . '_' . $field->name . '_type';
Condition100%2/2
$extra{'custom_type_name'} || $field->table->name . '_' . $field->name . '_type'
l!l
11
MC/DC100%1/1
$extra{'custom_type_name'} || $field->table->name . '_' . $field->name . '_type'
$extra{'custom_type_name'}
Truth table100%2/2
A
A= $extra{'custom_type_name'}
Aresult
11
00
532 2 $field_def .= ' '. $type_name;
533 2 my $new_type_def = "DROP TYPE IF EXISTS $type_name CASCADE;\n" .
534 "CREATE TYPE $type_name AS ENUM ($commalist)";
535 2 ▶ if (! exists $type_defs->{$type_name} ) {
Branch: 2 branches
BranchTrueFalseTotal
if (not exists $type_defs->{$type_name}) { } 2 0 2
elsif ($type_defs->{$type_name} ne $new_type_def) { } 0 0 0
536 2 $type_defs->{$type_name} = $new_type_def;
537 } elsif ( $type_defs->{$type_name} ne $new_type_def ) {
538 0 die "Attempted to redefine type name '$type_name' as a different type.\n";
539 }
540 } else {
541 100 $field_def .= ' '. convert_datatype($field);
542 }
543  
544 #
545 # Default value
546 #
547 102 __PACKAGE__->_apply_default_value(
548 $field,
549 \$field_def,
550 [
551 'NULL' => \'NULL',
552 'now()' => 'now()',
553 'CURRENT_TIMESTAMP' => 'CURRENT_TIMESTAMP',
554 ],
555 );
556  
557 #
558 # Not null constraint
559 #
560 102 ▶ $field_def .= ' NOT NULL' unless $field->is_nullable;
Branch: 1 branch
BranchTrueFalseTotal
unless $field->is_nullable 38 64 102
561  
562 #
563 # Geometry constraints
564 #
565 102 ▶ if (is_geometry($field)) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_geometry($field)) 3 99 102
566 3 foreach ( create_geometry_constraints($field, $options) ) {
567 9 my ($cdefs, $fks) = create_constraint($_, $options);
568 9 push @$constraint_defs, @$cdefs;
569 9 push @$fks, @$fks;
570 }
571 }
572  
573 102 return $field_def;
574 }
575 }
576  
577 6 sub create_geometry_constraints {
578 7 ▶ my ($field, $options) = @_;
Subroutine create_geometry_constraints: called CC=1 SCAR=0.0
Pod undocumented
579  
580 7 my $fname = _generator($options)->quote($field);
581 7 my @constraints;
582 push @constraints, SQL::Translator::Schema::Constraint->new(
583 name => "enforce_dims_".$field->name,
584 7 expression => "(ST_NDims($fname) = ".$field->extra->{dimensions}.")",
585 table => $field->table,
586 type => CHECK_C,
587 );
588  
589 push @constraints, SQL::Translator::Schema::Constraint->new(
590 name => "enforce_srid_".$field->name,
591 7 expression => "(ST_SRID($fname) = ".$field->extra->{srid}.")",
592 table => $field->table,
593 type => CHECK_C,
594 );
595 push @constraints, SQL::Translator::Schema::Constraint->new(
596 name => "enforce_geotype_".$field->name,
597 7 expression => "(GeometryType($fname) = ". __PACKAGE__->_quote_string($field->extra->{geometry_type}) ."::text OR $fname IS NULL)",
598 table => $field->table,
599 type => CHECK_C,
600 );
601  
602 7 return @constraints;
603 }
604  
605 {
606 6 my %index_name;
607 sub create_index
608 {
609 16 ▶ my ($index, $options) = @_;
Subroutine create_index: called CC=19 SCAR=31.2
Pod documented
610  
611 16 my $generator = _generator($options);
612 16 my $table_name = $index->table->name;
613 16 ▶ my $postgres_version = $options->{postgres_version} || 0;
Condition100%2/2
$options->{'postgres_version'} || 0
l!l
115
MC/DC100%1/1
$options->{'postgres_version'} || 0
$options->{'postgres_version'}
Truth table100%2/2
A
A= $options->{'postgres_version'}
Aresult
11
00
614  
615 16 my ($index_def, @constraint_defs);
616  
617 my $name
618 = $index->name
619 16 ▶ || 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
1600
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
620  
621 16 ▶ my $type = $index->type || NORMAL;
Condition50%1/2
$index->type || 'NORMAL'
l!l
160
MC/DC0%0/1
$index->type || 'NORMAL'
$index->type
Truth table50%1/2
A
A= $index->type
Aresult
11
00
622 16 my @fields = $index->fields;
623 16 ▶ return unless @fields;
Branch: 1 branch
BranchTrueFalseTotal
unless @fields 0 16 16
624  
625 16 my %index_extras;
626 16 for my $opt ( $index->options ) {
627 6 ▶ if ( ref $opt eq 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $opt eq "HASH") 6 0 6
628 6 foreach my $key (keys %$opt) {
629 6 my $value = $opt->{$key};
630 6 ▶ next unless defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 0 6 6
631 6 ▶ if ( uc($key) eq 'USING' ) {
Branch: 3 branches
BranchTrueFalseTotal
if (uc $key eq 'USING') { } 2 4 6
elsif (uc $key eq 'WHERE') { } 2 2 4
elsif (uc $key eq 'INCLUDE') { } 2 0 2
632 2 $index_extras{using} = "USING $value";
633 }
634 elsif ( uc($key) eq 'WHERE' ) {
635 2 $index_extras{where} = "WHERE $value";
636 }
637 elsif ( uc($key) eq 'INCLUDE' ) {
638 2 ▶ next unless $postgres_version >= 11;
Branch: 1 branch
BranchTrueFalseTotal
unless $postgres_version >= 11 1 1 2
639 1 ▶ die 'Include list must be an arrayref' unless ref $value eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $value eq "ARRAY" 0 1 1
640 1 my $value_list = join ', ', @$value;
641 1 $index_extras{include} = "INCLUDE ($value_list)"
642 }
643 }
644 }
645 }
646  
647 16 my $def_start = 'CONSTRAINT ' . $generator->quote($name) . ' ';
648 16 ▶ my $field_names = '(' . join(", ", (map { $_ =~ /\(.*\)/ ? $_ : ( $generator->quote($_) ) } @fields)) . ')';
Branch: 1 branch
BranchTrueFalseTotal
$_ =~ /\(.*\)/ ? : 6 14 20
649 16 ▶ if ( $type eq PRIMARY_KEY ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($type eq 'PRIMARY KEY') { } 0 16 16
elsif ($type eq 'UNIQUE') { } 1 15 16
elsif ($type eq 'NORMAL') { } 15 0 15
650 0 push @constraint_defs, "${def_start}PRIMARY KEY ".$field_names;
651 }
652 elsif ( $type eq UNIQUE ) {
653 1 push @constraint_defs, "${def_start}UNIQUE " .$field_names;
654 }
655 elsif ( $type eq NORMAL ) {
656 $index_def =
657 'CREATE INDEX ' . $generator->quote($name) . ' on ' . $generator->quote($table_name) . ' ' .
658 15 join ' ', grep { defined } $index_extras{using}, $field_names, @index_extras{'include', 'where'};
659 }
660 else {
661 0 ▶ warn "Unknown index type ($type) on table $table_name.\n"
Branch: 1 branch
BranchTrueFalseTotal
if $SQL::Translator::Producer::PostgreSQL::WARN 0 0 0
662 if $WARN;
663 }
664  
665 16 return $index_def, \@constraint_defs;
666 }
667 }
668  
669 sub create_constraint
670 6 {
671 69 ▶ my ($c, $options) = @_;
Subroutine create_constraint: called CC=16 SCAR=28.8
Pod documented
672  
673 69 my $generator = _generator($options);
674 69 my $table_name = $c->table->name;
675 69 my (@constraint_defs, @fks);
676  
677 69 ▶ my $name = $c->name || '';
Condition100%2/2
$c->name || ''
l!l
4227
MC/DC100%1/1
$c->name || ''
$c->name
Truth table100%2/2
A
A= $c->name
Aresult
11
00
678  
679 69 my @fields = grep { defined } $c->fields;
680  
681 69 my @rfields = grep { defined } $c->reference_fields;
682  
683 69 ▶ next if !@fields && $c->type ne CHECK_C;
Branch: 1 branch
BranchTrueFalseTotal
if not @fields and $c->type ne "CHECK" 0 69 69
Condition66%2/3
not @fields and $c->type ne "CHECK"
!ll&&!rl&&r
51180
MC/DC0%0/2
not @fields and $c->type ne "CHECK"
not @fields $c->type ne "CHECK"
Truth table66%2/3
not(A) and B
A= not @fieldsB= $c->type ne "CHECK"
ABresult
0X0
100
111
684 69 ▶ my $def_start = $name ? 'CONSTRAINT ' . $generator->quote($name) . ' ' : '';
Branch: 1 branch
BranchTrueFalseTotal
$name ? : 42 27 69
685 69 ▶ my $field_names = '(' . join(", ", (map { $_ =~ /\(.*\)/ ? $_ : ( $generator->quote($_) ) } @fields)) . ')';
Branch: 1 branch
BranchTrueFalseTotal
$_ =~ /\(.*\)/ ? : 4 51 55
686 69 ▶ if ( $c->type eq PRIMARY_KEY ) {
Branch: 4 branches
BranchTrueFalseTotal
if ($c->type eq 'PRIMARY KEY') { } 17 52 69
elsif ($c->type eq 'UNIQUE') { } 16 36 52
elsif ($c->type eq 'CHECK') { } 18 18 36
elsif ($c->type eq 'FOREIGN KEY') { } 8 10 18
687 17 push @constraint_defs, "${def_start}PRIMARY KEY ".$field_names;
688 }
689 elsif ( $c->type eq UNIQUE ) {
690 16 push @constraint_defs, "${def_start}UNIQUE " .$field_names;
691 }
692 elsif ( $c->type eq CHECK_C ) {
693 18 my $expression = $c->expression;
694 18 push @constraint_defs, "${def_start}CHECK ($expression)";
695 }
696 elsif ( $c->type eq FOREIGN_KEY ) {
697 8 my $def .= "ALTER TABLE " . $generator->quote($table_name) . " ADD ${def_start}FOREIGN KEY $field_names"
698 . "\n REFERENCES " . $generator->quote($c->reference_table);
699  
700 8 ▶ if ( @rfields ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@rfields) 8 0 8
701 8 $def .= ' (' . join( ', ', map { $generator->quote($_) } @rfields ) . ')';
702 }
703  
704 8 ▶ if ( $c->match_type ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($c->match_type) 0 8 8
705 0 ▶ $def .= ' MATCH ' .
Branch: 1 branch
BranchTrueFalseTotal
' MATCH ' . $c->match_type =~ /full/i ? : 0 0 0
706 ( $c->match_type =~ /full/i ) ? 'FULL' : 'PARTIAL';
707 }
708  
709 8 ▶ if ( $c->on_delete ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($c->on_delete) 0 8 8
710 0 $def .= ' ON DELETE '. $c->on_delete;
711 }
712  
713 8 ▶ if ( $c->on_update ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($c->on_update) 0 8 8
714 0 $def .= ' ON UPDATE '. $c->on_update;
715 }
716  
717 8 ▶ if ( $c->deferrable ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($c->deferrable) 8 0 8
718 8 $def .= ' DEFERRABLE';
719 }
720  
721 8 push @fks, "$def";
722 }
723  
724 69 return \@constraint_defs, \@fks;
725 }
726  
727 sub create_trigger {
728 14 ▶ my ($trigger,$options) = @_;
Subroutine create_trigger: called CC=4 SCAR=13.9
Pod documented
729 14 my $generator = _generator($options);
730  
731 14 my @statements;
732  
733 push @statements, sprintf( 'DROP TRIGGER IF EXISTS %s', $generator->quote($trigger->name) )
734 14 ▶ if $options->{add_drop_trigger};
Branch: 1 branch
BranchTrueFalseTotal
if $options->{"add_drop_trigger"} 14 0 14
735  
736 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
737 14 ▶ $scope = " FOR EACH $scope" if $scope;
Branch: 1 branch
BranchTrueFalseTotal
if $scope 2 12 14
738  
739 push @statements, sprintf(
740 'CREATE TRIGGER %s %s %s ON %s%s %s',
741 $generator->quote($trigger->name),
742 $trigger->perform_action_when,
743 14 join( ' OR ', @{ $trigger->database_events } ),
744 $generator->quote($trigger->on_table),
745 $scope,
746 $trigger->action,
747 );
748  
749 14 return @statements;
750 }
751  
752 sub convert_datatype
753 {
754 144 ▶ my ($field) = @_;
Subroutine convert_datatype: called CC=26 SCAR=32.9
Pod undocumented
755  
756 144 my @size = $field->size;
757 144 my $data_type = lc $field->data_type;
758 144 my $array = $data_type =~ s/\[\]$//;
759  
760 144 ▶ if ( $data_type eq 'enum' ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($data_type eq 'enum') { } 0 144 144
elsif ($data_type eq 'set') { } 0 144 144
elsif ($field->is_auto_increment) { } 19 125 144
761 # my $len = 0;
762 # $len = ($len < length($_)) ? length($_) : $len for (@$list);
763 # my $chk_name = mk_name( $table_name.'_'.$field_name, 'chk' );
764 # push @$constraint_defs,
765 # 'CONSTRAINT "$chk_name" CHECK (' . $generator->quote(field_name) .
766 # qq[IN ($commalist))];
767 0 $data_type = 'character varying';
768 }
769 elsif ( $data_type eq 'set' ) {
770 0 $data_type = 'character varying';
771 }
772 elsif ( $field->is_auto_increment ) {
773 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
774 2 $data_type = 'bigserial';
775 }
776 else {
777 17 $data_type = 'serial';
778 }
779 19 undef @size;
780 }
781 else {
782 $data_type = defined $translate{ lc $data_type } ?
783 125 ▶ $translate{ lc $data_type } :
Branch: 1 branch
BranchTrueFalseTotal
defined $translate{lc $data_type} ? : 65 60 125
784 $data_type;
785 }
786  
787 144 ▶ if ( $data_type =~ /^time/i || $data_type =~ /^interval/i ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($data_type =~ /^time/i or $data_type =~ /^interval/i) 13 131 144
Condition100%3/3
$data_type =~ /^time/i or $data_type =~ /^interval/i
l!l&&r!l&&!r
121131
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
788 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
789 1 $size[0] = 6;
790 }
791 }
792  
793 144 ▶ if ( $data_type eq 'integer' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($data_type eq "integer") 17 127 144
794 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
795 15 ▶ if ( $size[0] > 10 ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($size[0] > 10) { } 10 5 15
elsif ($size[0] < 5) { } 0 5 5
796 10 $data_type = 'bigint';
797 }
798 elsif ( $size[0] < 5 ) {
799 0 $data_type = 'smallint';
800 }
801 else {
802 5 $data_type = 'integer';
803 }
804 }
805 else {
806 2 $data_type = 'integer';
807 }
808 }
809  
810 144 my $type_with_size = join('|',
811 'bit', 'varbit', 'character', 'bit varying', 'character varying',
812 'time', 'timestamp', 'interval', 'numeric', 'float'
813 );
814  
815 144 ▶ if ( $data_type !~ /$type_with_size/ ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($data_type =~ /$type_with_size/) 60 84 144
816 60 @size = ();
817 }
818  
819 144 ▶ 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 139 144
elsif (defined $size[0] and $size[0] > 0) { } 69 130 199
Condition100%6/6
defined $size[0] and $size[0] > 0 and $data_type =~ /^time/i
!ll&&!rl&&r
70695
defined $size[0] and $size[0] > 0
!ll&&!rl&&r
601074
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
820 5 $data_type =~ s/^(time.*?)( with.*)?$/$1($size[0])/;
821 5 ▶ $data_type .= $2 if(defined $2);
Branch: 1 branch
BranchTrueFalseTotal
if defined $2 5 0 5
822 ▶ } elsif ( defined $size[0] && $size[0] > 0 ) {
Condition100%3/3
defined $size[0] and $size[0] > 0
!ll&&!rl&&r
601069
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
823 69 $data_type .= '(' . join( ',', @size ) . ')';
824 }
825 144 ▶ if($array)
Branch: 1 branch
BranchTrueFalseTotal
if ($array) 1 143 144
826 {
827 1 $data_type .= '[]';
828 }
829  
830 #
831 # Geography
832 #
833 144 ▶ if($data_type eq 'geography'){
Branch: 1 branch
BranchTrueFalseTotal
if ($data_type eq "geography") 1 143 144
834 1 $data_type .= '('.$field->extra->{geography_type}.','. $field->extra->{srid} .')'
835 }
836  
837 144 return $data_type;
838 }
839  
840  
841 sub alter_field
842 {
843 22 ▶ my ($from_field, $to_field, $options) = @_;
Subroutine alter_field: called CC=19 SCAR=29.4
Pod documented
844  
845 22 ▶ die "Can't alter field in another table"
Branch: 1 branch
BranchTrueFalseTotal
if $from_field->table->name ne $to_field->table->name 0 22 22
846 if($from_field->table->name ne $to_field->table->name);
847  
848 22 my $generator = _generator($options);
849 22 my @out;
850  
851 # drop geometry column and constraints
852 22 ▶ push @out,
Branch: 1 branch
BranchTrueFalseTotal
if is_geometry($from_field) 1 21 22
853 drop_geometry_column($from_field, $options),
854 drop_geometry_constraints($from_field, $options),
855 if is_geometry($from_field);
856  
857 # it's necessary to start with rename column cause this would affect
858 # all of the following statements which would be broken if do the
859 # rename later
860 # BUT: drop geometry is done before the rename, cause it work's on the
861 # $from_field directly
862 22 ▶ push @out, sprintf('ALTER TABLE %s RENAME COLUMN %s TO %s',
Branch: 1 branch
BranchTrueFalseTotal
if $from_field->name ne $to_field->name 3 19 22
863 map($generator->quote($_),
864 $to_field->table->name,
865 $from_field->name,
866 $to_field->name,
867 ),
868 )
869 if($from_field->name ne $to_field->name);
870  
871 22 ▶ push @out, sprintf('ALTER TABLE %s ALTER COLUMN %s SET NOT NULL',
Branch: 1 branch
BranchTrueFalseTotal
if not $to_field->is_nullable and $from_field->is_nullable 6 16 22
Condition100%3/3
not $to_field->is_nullable and $from_field->is_nullable
!ll&&!rl&&r
1156
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
872 map($generator->quote($_),
873 $to_field->table->name,
874 $to_field->name
875 ),
876 )
877 if(!$to_field->is_nullable and $from_field->is_nullable);
878  
879 22 ▶ push @out, sprintf('ALTER TABLE %s ALTER COLUMN %s DROP NOT NULL',
Branch: 1 branch
BranchTrueFalseTotal
if not $from_field->is_nullable and $to_field->is_nullable 4 18 22
Condition100%3/3
not $from_field->is_nullable and $to_field->is_nullable
!ll&&!rl&&r
1354
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
880 map($generator->quote($_),
881 $to_field->table->name,
882 $to_field->name
883 ),
884 )
885 if (!$from_field->is_nullable and $to_field->is_nullable);
886  
887  
888 22 my $from_dt = convert_datatype($from_field);
889 22 my $to_dt = convert_datatype($to_field);
890 22 ▶ push @out, sprintf('ALTER TABLE %s ALTER COLUMN %s TYPE %s',
Branch: 1 branch
BranchTrueFalseTotal
if $to_dt ne $from_dt 10 12 22
891 map($generator->quote($_),
892 $to_field->table->name,
893 $to_field->name
894 ),
895 $to_dt,
896 )
897 if($to_dt ne $from_dt);
898  
899 22 my $old_default = $from_field->default_value;
900 22 my $new_default = $to_field->default_value;
901 22 my $default_value = $to_field->default_value;
902  
903 # fixes bug where output like this was created:
904 # ALTER TABLE users ALTER COLUMN column SET DEFAULT ThisIsUnescaped;
905 22 ▶ if(ref $default_value eq "SCALAR" ) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $default_value eq 'SCALAR') { } 1 21 22
elsif (defined $default_value and $to_dt =~ /^(character|text|timestamp|date)/imsx) { } 3 32 35
906 1 $default_value = $$default_value;
907 ▶ } elsif( defined $default_value && $to_dt =~ /^(character|text|timestamp|date)/xsmi ) {
Condition100%3/3
defined $default_value and $to_dt =~ /^(character|text|timestamp|date)/imsx
!ll&&!rl&&r
1443
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
908 3 $default_value = __PACKAGE__->_quote_string($default_value);
909 }
910  
911 22 ▶ push @out, sprintf('ALTER TABLE %s ALTER COLUMN %s SET DEFAULT %s',
Branch: 1 branch
BranchTrueFalseTotal
if defined $new_default and !defined($old_default) || $old_default ne $new_default 5 17 22
Condition100%6/6
defined $new_default and !defined($old_default) || $old_default ne $new_default
!ll&&!rl&&r
1453
not defined $old_default or $old_default ne $new_default
l!l&&r!l&&!r
323
MC/DC66%2/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 table75%3/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
912 map($generator->quote($_),
913 $to_field->table->name,
914 $to_field->name,
915 ),
916 $default_value,
917 )
918 if ( defined $new_default &&
919 (!defined $old_default || $old_default ne $new_default) );
920  
921 # fixes bug where removing the DEFAULT statement of a column
922 # would result in no change
923  
924 22 ▶ push @out, sprintf('ALTER TABLE %s ALTER COLUMN %s DROP DEFAULT',
Branch: 1 branch
BranchTrueFalseTotal
if not defined $new_default and defined $old_default 1 21 22
Condition100%3/3
not defined $new_default and defined $old_default
!ll&&!rl&&r
8131
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
925 map($generator->quote($_),
926 $to_field->table->name,
927 $to_field->name,
928 ),
929 )
930 if ( !defined $new_default && defined $old_default );
931  
932 # add geometry column and constraints
933 22 ▶ push @out,
Branch: 1 branch
BranchTrueFalseTotal
if is_geometry($to_field) 1 21 22
934 add_geometry_column($to_field, $options),
935 add_geometry_constraints($to_field, $options),
936 if is_geometry($to_field);
937  
938 22 ▶ return wantarray ? @out : join(";\n", @out);
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 14 8 22
939 }
940  
941 2 ▶sub rename_field { alter_field(@_) }
Subroutine rename_field: called CC=1 SCAR=0.0
Pod undocumented
942  
943 sub add_field
944 {
945 6 ▶ my ($new_field,$options) = @_;
Subroutine add_field: called CC=2 SCAR=6.9
Pod documented
946  
947 6 my $out = sprintf('ALTER TABLE %s ADD COLUMN %s',
948 _generator($options)->quote($new_field->table->name),
949 create_field($new_field, $options));
950 6 ▶ $out .= ";\n".add_geometry_column($new_field, $options)
Branch: 1 branch
BranchTrueFalseTotal
if is_geometry($new_field) 1 5 6
951 . ";\n".add_geometry_constraints($new_field, $options)
952 if is_geometry($new_field);
953 6 return $out;
954  
955 }
956  
957 sub drop_field
958 {
959 4 ▶ my ($old_field, $options) = @_;
Subroutine drop_field: called CC=2 SCAR=6.9
Pod documented
960  
961 4 my $generator = _generator($options);
962  
963 4 my $out = sprintf('ALTER TABLE %s DROP COLUMN %s',
964 $generator->quote($old_field->table->name),
965 $generator->quote($old_field->name));
966 4 ▶ $out .= ";\n".drop_geometry_column($old_field, $options)
Branch: 1 branch
BranchTrueFalseTotal
if is_geometry($old_field) 1 3 4
967 if is_geometry($old_field);
968 4 return $out;
969 }
970  
971 sub add_geometry_column {
972 5 ▶ my ($field, $options) = @_;
Subroutine add_geometry_column: called CC=2 SCAR=6.9
Pod undocumented
973  
974 return sprintf(
975 "INSERT INTO geometry_columns VALUES (%s,%s,%s,%s,%s,%s,%s)",
976 map(__PACKAGE__->_quote_string($_),
977 '',
978 $field->table->schema->name,
979 $options->{table} ? $options->{table} : $field->table->name,
980 $field->name,
981 $field->extra->{dimensions},
982 $field->extra->{srid},
983 $field->extra->{geometry_type},
984 5 ▶ ),
Branch: 1 branch
BranchTrueFalseTotal
$options->{'table'} ? : 1 4 5
985 );
986 }
987  
988 sub drop_geometry_column {
989 4 ▶ my ($field) = @_;
Subroutine drop_geometry_column: called CC=1 SCAR=0.0
Pod undocumented
990  
991 4 return sprintf(
992 "DELETE FROM geometry_columns WHERE f_table_schema = %s AND f_table_name = %s AND f_geometry_column = %s",
993 map(__PACKAGE__->_quote_string($_),
994 $field->table->schema->name,
995 $field->table->name,
996 $field->name,
997 ),
998 );
999 }
1000  
1001 sub add_geometry_constraints {
1002 3 ▶ my ($field, $options) = @_;
Subroutine add_geometry_constraints: called CC=1 SCAR=0.0
Pod undocumented
1003  
1004 3 return join(";\n", map { alter_create_constraint($_, $options) }
1005 create_geometry_constraints($field, $options));
1006 }
1007  
1008 sub drop_geometry_constraints {
1009 1 ▶ my ($field, $options) = @_;
Subroutine drop_geometry_constraints: called CC=1 SCAR=0.0
Pod undocumented
1010  
1011 1 return join(";\n", map { alter_drop_constraint($_, $options) }
1012 create_geometry_constraints($field, $options));
1013  
1014 }
1015  
1016 sub alter_table {
1017 3 ▶ my ($to_table, $options) = @_;
Subroutine alter_table: called CC=2 SCAR=6.9
Pod undocumented
1018 3 my $generator = _generator($options);
1019 my $out = sprintf('ALTER TABLE %s %s',
1020 $generator->quote($to_table->name),
1021 3 $options->{alter_table_action});
1022 3 ▶ $out .= ";\n".$options->{geometry_changes} if $options->{geometry_changes};
Branch: 1 branch
BranchTrueFalseTotal
if $options->{"geometry_changes"} 1 2 3
1023 3 return $out;
1024 }
1025  
1026 sub rename_table {
1027 3 ▶ my ($old_table, $new_table, $options) = @_;
Subroutine rename_table: called CC=2 SCAR=6.9
Pod undocumented
1028 3 my $generator = _generator($options);
1029 3 $options->{alter_table_action} = "RENAME TO " . $generator->quote($new_table);
1030  
1031 my @geometry_changes = map {
1032 drop_geometry_column($_, $options),
1033 1 add_geometry_column($_, { %{$options}, table => $new_table }),
1034 3 } grep { is_geometry($_) } $old_table->get_fields;
1035  
1036 3 ▶ $options->{geometry_changes} = join (";\n",@geometry_changes) if @geometry_changes;
Branch: 1 branch
BranchTrueFalseTotal
if @geometry_changes 1 2 3
1037  
1038 3 return alter_table($old_table, $options);
1039 }
1040  
1041 sub alter_create_index {
1042 1 ▶ my ($index, $options) = @_;
Subroutine alter_create_index: called CC=2 SCAR=7.0
Pod undocumented
1043 1 my $generator = _generator($options);
1044 1 my ($idef, $constraints) = create_index($index, $options);
1045 1 ▶ return $index->type eq NORMAL ? $idef
Branch: 1 branch
BranchTrueFalseTotal
$index->type eq 'NORMAL' ? : 0 1 1
1046 : sprintf('ALTER TABLE %s ADD %s',
1047 $generator->quote($index->table->name),
1048 join(q{}, @$constraints)
1049 );
1050 }
1051  
1052 sub alter_drop_index {
1053 1 ▶ my ($index, $options) = @_;
Subroutine alter_drop_index: called CC=1 SCAR=0.0
Pod undocumented
1054 1 return 'DROP INDEX '. _generator($options)->quote($index->name);
1055 }
1056  
1057 sub alter_drop_constraint {
1058 14 ▶ my ($c, $options) = @_;
Subroutine alter_drop_constraint: called CC=5 SCAR=16.1
Pod undocumented
1059 14 my $generator = _generator($options);
1060  
1061 # NOT NULL constraint does not require a DROP CONSTRAINT statement
1062 14 ▶ if ( $c->type eq NOT_NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if ($c->type eq "NOT NULL") 2 12 14
1063 2 return;
1064 }
1065  
1066 # attention: Postgres has a very special naming structure for naming
1067 # foreign keys and primary keys. It names them using the name of the
1068 # table as prefix and fkey or pkey as suffix, concatenated by an underscore
1069 12 my $c_name;
1070 12 ▶ if( $c->name ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($c->name) { } 9 3 12
1071 # Already has a name, just use it
1072 9 $c_name = $c->name;
1073 } else {
1074 # if the name is dotted we need the table, not schema nor database
1075 3 my ($tablename) = reverse split /[.]/, $c->table->name;
1076 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
1077 # Doesn't have a name, and is foreign key, append '_fkey'
1078 1 $c_name = $tablename . '_' . ($c->fields)[0] . '_fkey';
1079 } elsif ( $c->type eq PRIMARY_KEY ) {
1080 # Doesn't have a name, and is primary key, append '_pkey'
1081 2 $c_name = $tablename . '_pkey';
1082 }
1083 }
1084  
1085 return sprintf(
1086 'ALTER TABLE %s DROP CONSTRAINT %s',
1087 12 map { $generator->quote($_) } $c->table->name, $c_name,
1088 );
1089 }
1090  
1091 sub alter_create_constraint {
1092 14 ▶ my ($index, $options) = @_;
Subroutine alter_create_constraint: called CC=4 SCAR=13.9
Pod undocumented
1093 14 my $generator = _generator($options);
1094 14 my ($defs, $fks) = create_constraint(@_);
1095  
1096 # return if there are no constraint definitions so we don't run
1097 # into output like this:
1098 # ALTER TABLE users ADD ;
1099  
1100 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
1101 1 return $index->type eq FOREIGN_KEY ? join(q{}, @{$fks})
1102 : join( ' ', 'ALTER TABLE', $generator->quote($index->table->name),
1103 14 ▶ 'ADD', join(q{}, @{$defs}, @{$fks})
Branch: 1 branch
BranchTrueFalseTotal
$index->type eq 'FOREIGN KEY' ? : 1 13 14
1104 );
1105 }
1106  
1107 sub drop_table {
1108 3 ▶ my ($table, $options) = @_;
Subroutine drop_table: called CC=2 SCAR=6.9
Pod undocumented
1109 3 my $generator = _generator($options);
1110 3 my $out = "DROP TABLE " . $generator->quote($table) . " CASCADE";
1111  
1112 3 my @geometry_drops = map { drop_geometry_column($_); } grep { is_geometry($_) } $table->get_fields;
1113  
1114 3 ▶ $out .= join(";\n", '', @geometry_drops) if @geometry_drops;
Branch: 1 branch
BranchTrueFalseTotal
if @geometry_drops 1 2 3
1115 3 return $out;
1116 }
1117  
1118 sub batch_alter_table {
1119 12 ▶ my ( $table, $diff_hash, $options ) = @_;
Subroutine batch_alter_table: called CC=4 SCAR=14.8
Pod undocumented
1120  
1121 # as long as we're not renaming the table we don't need to be here
1122 12 ▶ if ( @{$diff_hash->{rename_table}} == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$diff_hash->{"rename_table"};} == 0) 10 2 12
1123 10 return batch_alter_table_statements($diff_hash, $options);
1124 }
1125  
1126 # first we need to perform drops which are on old table
1127 2 my @sql = batch_alter_table_statements($diff_hash, $options, qw(
1128 alter_drop_constraint
1129 alter_drop_index
1130 drop_field
1131 ));
1132  
1133 # next comes the rename_table
1134 2 my $old_table = $diff_hash->{rename_table}[0][0];
1135 2 push @sql, rename_table( $old_table, $table, $options );
1136  
1137 # for alter_field (and so also rename_field) we need to make sure old
1138 # field has table name set to new table otherwise calling alter_field dies
1139 $diff_hash->{alter_field} =
1140 2 ▶ [map { $_->[0]->table($table) && $_ } @{$diff_hash->{alter_field}}];
Branch: 1 branch
BranchTrueFalseTotal
$_->[0]->table($table) and $_ 0 0 0
1141 $diff_hash->{rename_field} =
1142 2 ▶ [map { $_->[0]->table($table) && $_ } @{$diff_hash->{rename_field}}];
Branch: 1 branch
BranchTrueFalseTotal
$_->[0]->table($table) and $_ 0 0 0
1143  
1144 # now add everything else
1145 2 push @sql, batch_alter_table_statements($diff_hash, $options, qw(
1146 add_field
1147 alter_field
1148 rename_field
1149 alter_create_index
1150 alter_create_constraint
1151 alter_table
1152 ));
1153  
1154 2 return @sql;
1155 }
1156  
1157 6 1;
1158  
1159 # -------------------------------------------------------------------
1160 # Life is full of misery, loneliness, and suffering --
1161 # and it's all over much too soon.
1162 # Woody Allen
1163 # -------------------------------------------------------------------
1164  
1165 =pod
1166  
1167 =head1 SEE ALSO
1168  
1169 SQL::Translator, SQL::Translator::Producer::Oracle.
1170  
1171 =head1 AUTHOR
1172  
1173 Ken Youens-Clark E<lt>kclark@cpan.orgE<gt>.
1174  
1175 =cut
« blib/lib/SQL/Translator/Parser/DBI/PostgreSQL.pm ↑ Summary blib/lib/SQL/Translator/Parser/DB2.pm »