| 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
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||||||||||||||||||||
| 127 | 166 | ▶ | return normalize_quote_options($options) | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
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", [])
$quoting_generator ||= "SQL::Translator::Generator::DDL::PostgreSQL"->new
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", [])
Truth table66%2/3
A ||= B
A= $quoting_generatorB= "SQL::Translator::Generator::DDL::PostgreSQL"->new
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||
| 287 | 5 | ▶ | push @output, "--\n-- Foreign Key Definitions\n--\n\n" | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 288 | unless $no_comments; | ||||||||||||||||||||||||||||||||||||||||||||||
| 289 | 5 | push @output, map {"$_;\n\n"} @fks; | |||||||||||||||||||||||||||||||||||||||||||||
| 290 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 291 | |||||||||||||||||||||||||||||||||||||||||||||||
| 292 | 8 | ▶ | if ($WARN) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 293 | 1 | ▶ | if (%truncated) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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() || ''
MC/DC0%0/1
shift() || ''
shift()
Truth table0%0/2
A
A= shift()
| |||||||||||||||||||||||||||||||||||||||||||||||
| 309 | 0 | ▶ | my $type = shift || ''; | ||||||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
shift() || ''
MC/DC0%0/1
shift() || ''
shift()
Truth table0%0/2
A
A= shift()
| |||||||||||||||||||||||||||||||||||||||||||||||
| 310 | 0 | ▶ | my $scope = shift || ''; | ||||||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
shift() || ''
MC/DC0%0/1
shift() || ''
shift()
Truth table0%0/2
A
A= shift()
| |||||||||||||||||||||||||||||||||||||||||||||||
| 311 | 0 | ▶ | my $critical = shift || ''; | ||||||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
shift() || ''
MC/DC0%0/1
shift() || ''
shift()
Truth table0%0/2
A
A= shift()
| |||||||||||||||||||||||||||||||||||||||||||||||
| 312 | 0 | my $basename_orig = $basename; | |||||||||||||||||||||||||||||||||||||||||||||
| 313 | |||||||||||||||||||||||||||||||||||||||||||||||
| 314 | 0 | ▶ | my $max_name | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 315 | = $type | ||||||||||||||||||||||||||||||||||||||||||||||
| 316 | ? MAX_ID_LENGTH - (length($type) + 1) | ||||||||||||||||||||||||||||||||||||||||||||||
| 317 | : MAX_ID_LENGTH; | ||||||||||||||||||||||||||||||||||||||||||||||
| 318 | 0 | ▶ | $basename = substr($basename, 0, $max_name) | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 319 | if length($basename) > $max_name; | ||||||||||||||||||||||||||||||||||||||||||||||
| 320 | 0 | ▶ | my $name = $type ? "${type}_$basename" : $basename; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 321 | |||||||||||||||||||||||||||||||||||||||||||||||
| 322 | 0 | ▶ | if ($basename ne $basename_orig and $critical) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$basename ne $basename_orig and $critical
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
| |||||||||||||||||||||||||||||||||||||||||||||||
| 323 | 0 | ▶ | my $show_type = $type ? "+'$type'" : ""; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 324 | 0 | ▶ | warn "Truncating '$basename_orig'$show_type to ", MAX_ID_LENGTH, " character limit to make '$name'\n" | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 325 | if $WARN; | ||||||||||||||||||||||||||||||||||||||||||||||
| 326 | 0 | $truncated{$basename_orig} = $name; | |||||||||||||||||||||||||||||||||||||||||||||
| 327 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 328 | |||||||||||||||||||||||||||||||||||||||||||||||
| 329 | 0 | ▶ | $scope ||= \%global_names; | ||||||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$scope ||= \%global_names
MC/DC0%0/1
$scope ||= \%global_names
$scope
Truth table0%0/2
A
A= $scope
| |||||||||||||||||||||||||||||||||||||||||||||||
| 330 | 0 | ▶ | if (my $prev = $scope->{$name}) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 331 | 0 | my $name_orig = $name; | |||||||||||||||||||||||||||||||||||||||||||||
| 332 | 0 | $name .= sprintf("%02d", ++$prev); | |||||||||||||||||||||||||||||||||||||||||||||
| 333 | 0 | ▶ | substr($name, MAX_ID_LENGTH - 3) = "00" | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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
MC/DC100%1/1
$options->{'no_comments'} || 0
$options->{'no_comments'}
Truth table100%2/2
A
A= $options->{'no_comments'}
| |||||||||||||||||||||||||||||||||||||||||||||||
| 362 | 24 | ▶ | my $add_drop_table = $options->{add_drop_table} || 0; | ||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$options->{'add_drop_table'} || 0
MC/DC100%1/1
$options->{'add_drop_table'} || 0
$options->{'add_drop_table'}
Truth table100%2/2
A
A= $options->{'add_drop_table'}
| |||||||||||||||||||||||||||||||||||||||||||||||
| 363 | 24 | ▶ | my $postgres_version = $options->{postgres_version} || 0; | ||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$options->{'postgres_version'} || 0
MC/DC0%0/1
$options->{'postgres_version'} || 0
$options->{'postgres_version'}
Truth table50%1/2
A
A= $options->{'postgres_version'}
| |||||||||||||||||||||||||||||||||||||||||||||||
| 364 | 24 | ▶ | my $type_defs = $options->{type_defs} || {}; | ||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$options->{'type_defs'} || {}
MC/DC100%1/1
$options->{'type_defs'} || {}
$options->{'type_defs'}
Truth table100%2/2
A
A= $options->{'type_defs'}
| |||||||||||||||||||||||||||||||||||||||||||||||
| 365 | 24 | my $attach_comments = $options->{attach_comments}; | |||||||||||||||||||||||||||||||||||||||||||||
| 366 | |||||||||||||||||||||||||||||||||||||||||||||||
| 367 | 24 | ▶ | my $table_name = $table->name or next; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||
| 373 | |||||||||||||||||||||||||||||||||||||||||||||||
| 374 | 24 | my @comment_statements; | |||||||||||||||||||||||||||||||||||||||||||||
| 375 | 24 | ▶ | if (my $comments = $table->comments) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 376 | 1 | ▶ | if ($attach_comments) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||
| 405 | 12 | my $field_comments = $field->comments; | |||||||||||||||||||||||||||||||||||||||||||||
| 406 | 12 | ▶ | next unless $field_comments; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||
| 445 | 20 | ▶ | if ($postgres_version >= 8.002) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||
| 455 | 24 | ▶ | $create_statement .= ($create_statement =~ /;$/ ? "\n" : q{}) . join(";\n", @index_defs); | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 456 | |||||||||||||||||||||||||||||||||||||||||||||||
| 457 | # | ||||||||||||||||||||||||||||||||||||||||||||||
| 458 | # Geometry | ||||||||||||||||||||||||||||||||||||||||||||||
| 459 | # | ||||||||||||||||||||||||||||||||||||||||||||||
| 460 | 24 | ▶ | if (my @geometry_columns = grep { is_geometry($_) } $table->get_fields) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 461 | 0 | $create_statement .= join(";\n", '', map { drop_geometry_column($_, $options) } @geometry_columns) | |||||||||||||||||||||||||||||||||||||||||||||
| 462 | 1 | ▶ | if $options->{add_drop_table}; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 463 | 1 | $create_statement .= join(";\n", '', map { add_geometry_column($_, $options) } @geometry_columns); | |||||||||||||||||||||||||||||||||||||||||||||
| 464 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 465 | |||||||||||||||||||||||||||||||||||||||||||||||
| 466 | 24 | ▶ | if (@comment_statements) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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
MC/DC100%1/1
$options->{'postgres_version'} || 0
$options->{'postgres_version'}
Truth table100%2/2
A
A= $options->{'postgres_version'}
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||
| 485 | 10 | ▶ | if ($add_drop_view) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 486 | 7 | ▶ | if ($postgres_version >= 8.002) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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
Condition66%2/3
exists $extra->{"temporary"} and $extra->{"temporary"}
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"}
| |||||||||||||||||||||||||||||||||||||||||||||||
| 497 | $create .= " MATERIALIZED" | ||||||||||||||||||||||||||||||||||||||||||||||
| 498 | 10 | ▶ | if exists($extra->{materialized}) && $extra->{materialized}; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
exists $extra->{"materialized"} and $extra->{"materialized"}
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"}
| |||||||||||||||||||||||||||||||||||||||||||||||
| 499 | 10 | $create .= " VIEW " . $generator->quote($view_name); | |||||||||||||||||||||||||||||||||||||||||||||
| 500 | |||||||||||||||||||||||||||||||||||||||||||||||
| 501 | 10 | ▶ | if (my @fields = $view->fields) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||
| 507 | 10 | $create .= " AS\n ${sql}\n"; | |||||||||||||||||||||||||||||||||||||||||||||
| 508 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 509 | |||||||||||||||||||||||||||||||||||||||||||||||
| 510 | 10 | ▶ | if ($extra->{check_option}) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||
| 521 | 4 | ▶ | if ($field->extra->{custom_type_name}) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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'} || []
MC/DC100%1/1
$options->{'constraint_defs'} || []
$options->{'constraint_defs'}
Truth table100%2/2
A
A= $options->{'constraint_defs'}
| |||||||||||||||||||||||||||||||||||||||||||||||
| 541 | 102 | ▶ | my $postgres_version = $options->{postgres_version} || 0; | ||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$options->{'postgres_version'} || 0
MC/DC100%1/1
$options->{'postgres_version'} || 0
$options->{'postgres_version'}
Truth table100%2/2
A
A= $options->{'postgres_version'}
| |||||||||||||||||||||||||||||||||||||||||||||||
| 542 | 102 | ▶ | my $type_defs = $options->{type_defs} || {}; | ||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$options->{'type_defs'} || {}
MC/DC100%1/1
$options->{'type_defs'} || {}
$options->{'type_defs'}
Truth table100%2/2
A
A= $options->{'type_defs'}
| |||||||||||||||||||||||||||||||||||||||||||||||
| 543 | 102 | my $attach_comments = $options->{attach_comments}; | |||||||||||||||||||||||||||||||||||||||||||||
| 544 | |||||||||||||||||||||||||||||||||||||||||||||||
| 545 | 102 | ▶ | $field_name_scope{$table_name} ||= {}; | ||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$field_name_scope{$table_name} ||= {}
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}
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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
Condition100%3/3
not $attach_comments and my $comments = $field->comments
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
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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
Condition100%3/3
$postgres_version >= "8.003" and $enum_typename
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
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||
| 594 | |||||||||||||||||||||||||||||||||||||||||||||||
| 595 | # | ||||||||||||||||||||||||||||||||||||||||||||||
| 596 | # Geometry constraints | ||||||||||||||||||||||||||||||||||||||||||||||
| 597 | # | ||||||||||||||||||||||||||||||||||||||||||||||
| 598 | 102 | ▶ | if (is_geometry($field)) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||
| 647 | 7 | for my $key (keys %$opt) { | |||||||||||||||||||||||||||||||||||||||||||||
| 648 | 8 | my $val = $opt->{$key}; | |||||||||||||||||||||||||||||||||||||||||||||
| 649 | 8 | ▶ | next unless defined $val; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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
MC/DC100%1/1
$options->{'postgres_version'} || 0
$options->{'postgres_version'}
Truth table100%2/2
A
A= $options->{'postgres_version'}
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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})
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})
| |||||||||||||||||||||||||||||||||||||||||||||||
| 670 | |||||||||||||||||||||||||||||||||||||||||||||||
| 671 | 18 | ▶ | my $type = $index->type || NORMAL; | ||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$index->type || 'NORMAL'
MC/DC0%0/1
$index->type || 'NORMAL'
$index->type
Truth table50%1/2
A
A= $index->type
| |||||||||||||||||||||||||||||||||||||||||||||||
| 672 | 18 | my @fields = $index->fields; | |||||||||||||||||||||||||||||||||||||||||||||
| 673 | 18 | ▶ | return unless @fields; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||
| 684 | 1 | ▶ | die 'Include list must be an arrayref' | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||
| 694 | 18 | ▶ | if ($type eq PRIMARY_KEY) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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
MC/DC0%0/1
$options->{'postgres_version'} || 0
$options->{'postgres_version'}
Truth table50%1/2
A
A= $options->{'postgres_version'}
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||
| 728 | 0 | ▶ | die 'Include list must be an arrayref' | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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 || ''
MC/DC100%1/1
$c->name || ''
$c->name
Truth table100%2/2
A
A= $c->name
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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
Condition66%4/6
not @fields and ($c->type ne "CHECK" and $c->type ne "EXCLUDE")
$c->type ne "CHECK" and $c->type ne "EXCLUDE"
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"
| |||||||||||||||||||||||||||||||||||||||||||||||
| 743 | 70 | ▶ | my $def_start = $name ? 'CONSTRAINT ' . $generator->quote($name) : ''; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 744 | 70 | ▶ | my $field_names = '(' . join(", ", (map { $_ =~ /\(.*\)/ ? $_ : ($generator->quote($_)) } @fields)) . ')'; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 745 | 70 | ▶ | my $include = $constraint_extras{include} || ''; | ||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$constraint_extras{'include'} || ''
MC/DC0%0/1
$constraint_extras{'include'} || ''
$constraint_extras{'include'}
Truth table50%1/2
A
A= $constraint_extras{'include'}
| |||||||||||||||||||||||||||||||||||||||||||||||
| 746 | 70 | ▶ | if ($c->type eq PRIMARY_KEY) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 5 branches
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||
| 760 | 8 | $def .= ' (' . join(', ', map { $generator->quote($_) } @rfields) . ')'; | |||||||||||||||||||||||||||||||||||||||||||||
| 761 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 762 | |||||||||||||||||||||||||||||||||||||||||||||||
| 763 | 8 | ▶ | if ($c->match_type) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 764 | 0 | ▶ | $def .= ' MATCH ' . ($c->match_type =~ /full/i) ? 'FULL' : 'PARTIAL'; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 765 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 766 | |||||||||||||||||||||||||||||||||||||||||||||||
| 767 | 8 | ▶ | if ($c->on_delete) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 768 | 0 | $def .= ' ON DELETE ' . $c->on_delete; | |||||||||||||||||||||||||||||||||||||||||||||
| 769 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 770 | |||||||||||||||||||||||||||||||||||||||||||||||
| 771 | 8 | ▶ | if ($c->on_update) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 772 | 0 | $def .= ' ON UPDATE ' . $c->on_update; | |||||||||||||||||||||||||||||||||||||||||||||
| 773 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 774 | |||||||||||||||||||||||||||||||||||||||||||||||
| 775 | 8 | ▶ | if ($c->deferrable) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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'} || ''
MC/DC0%0/1
$constraint_extras{'using'} || ''
$constraint_extras{'using'}
Truth table50%1/2
A
A= $constraint_extras{'using'}
| |||||||||||||||||||||||||||||||||||||||||||||||
| 782 | 1 | my $expression = $c->expression; | |||||||||||||||||||||||||||||||||||||||||||||
| 783 | 1 | ▶ | my $where = $constraint_extras{where} || ''; | ||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$constraint_extras{'where'} || ''
MC/DC0%0/1
$constraint_extras{'where'} || ''
$constraint_extras{'where'}
Truth table50%1/2
A
A= $constraint_extras{'where'}
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||
| 798 | |||||||||||||||||||||||||||||||||||||||||||||||
| 799 | 14 | ▶ | my $scope = $trigger->scope || ''; | ||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$trigger->scope || ''
MC/DC100%1/1
$trigger->scope || ''
$trigger->scope
Truth table100%2/2
A
A= $trigger->scope
| |||||||||||||||||||||||||||||||||||||||||||||||
| 800 | 14 | ▶ | $scope = " FOR EACH $scope" if $scope; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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
Condition83%5/6
defined $size[0] and $size[0] > 11 or $data_type eq "bigint"
defined $size[0] and $size[0] > 11
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"
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||
| 844 | : $data_type; | ||||||||||||||||||||||||||||||||||||||||||||||
| 845 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 846 | |||||||||||||||||||||||||||||||||||||||||||||||
| 847 | 146 | ▶ | if ($data_type =~ /^time/i || $data_type =~ /^interval/i) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$data_type =~ /^time/i or $data_type =~ /^interval/i
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
| |||||||||||||||||||||||||||||||||||||||||||||||
| 848 | 13 | ▶ | if (defined $size[0] && $size[0] > 6) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $size[0] and $size[0] > 6
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
| |||||||||||||||||||||||||||||||||||||||||||||||
| 849 | 1 | $size[0] = 6; | |||||||||||||||||||||||||||||||||||||||||||||
| 850 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 851 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 852 | |||||||||||||||||||||||||||||||||||||||||||||||
| 853 | 146 | ▶ | if ($data_type eq 'integer') { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 854 | 17 | ▶ | if (defined $size[0] && $size[0] > 0) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $size[0] and $size[0] > 0
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
| |||||||||||||||||||||||||||||||||||||||||||||||
| 855 | 15 | ▶ | if ($size[0] > 10) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||
| 872 | 60 | @size = (); | |||||||||||||||||||||||||||||||||||||||||||||
| 873 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 874 | |||||||||||||||||||||||||||||||||||||||||||||||
| 875 | 146 | ▶ | if (defined $size[0] && $size[0] > 0 && $data_type =~ /^time/i) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition100%6/6
defined $size[0] and $size[0] > 0 and $data_type =~ /^time/i
defined $size[0] and $size[0] > 0
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
| |||||||||||||||||||||||||||||||||||||||||||||||
| 876 | 5 | $data_type =~ s/^(time.*?)( with.*)?$/$1($size[0])/; | |||||||||||||||||||||||||||||||||||||||||||||
| 877 | 5 | ▶ | $data_type .= $2 if (defined $2); | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 878 | ▶ | } elsif (defined $size[0] && $size[0] > 0) { | |||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%3/3
defined $size[0] and $size[0] > 0
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
| |||||||||||||||||||||||||||||||||||||||||||||||
| 879 | 69 | $data_type .= '(' . join(',', @size) . ')'; | |||||||||||||||||||||||||||||||||||||||||||||
| 880 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 881 | 146 | ▶ | if ($array) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 882 | 1 | $data_type .= '[]'; | |||||||||||||||||||||||||||||||||||||||||||||
| 883 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 884 | |||||||||||||||||||||||||||||||||||||||||||||||
| 885 | # | ||||||||||||||||||||||||||||||||||||||||||||||
| 886 | # Geography | ||||||||||||||||||||||||||||||||||||||||||||||
| 887 | # | ||||||||||||||||||||||||||||||||||||||||||||||
| 888 | 146 | ▶ | if ($data_type eq 'geography') { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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
Condition100%3/3
not $to_field->is_nullable and $from_field->is_nullable
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
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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
Condition100%3/3
not $from_field->is_nullable and $to_field->is_nullable
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
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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
Condition66%4/6
$from_enum_typename and $to_enum_typename and $from_enum_typename eq $to_enum_typename
$from_enum_typename and $to_enum_typename
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
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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
MC/DC0%0/1
$options->{'postgres_version'} || 0
$options->{'postgres_version'}
Truth table50%1/2
A
A= $options->{'postgres_version'}
| |||||||||||||||||||||||||||||||||||||||||||||||
| 946 | 1 | ▶ | push @out, '-- Set $sqlt->producer_args->{postgres_version} >= 9.001 to alter enums' | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
not $pg_ver_ok and @add_vals
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
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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
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
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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
Condition100%6/6
defined $new_default and !defined($old_default) || $old_default ne $new_default
not defined $old_default or $old_default ne $new_default
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
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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
Condition100%3/3
not defined $new_default and defined $old_default
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
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||
| 989 | if is_geometry($to_field); | ||||||||||||||||||||||||||||||||||||||||||||||
| 990 | |||||||||||||||||||||||||||||||||||||||||||||||
| 991 | 23 | ▶ | return wantarray ? @out : join(";\n", @out); | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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
Condition66%2/3
@{$defs;} or @{$fks;}
MC/DC0%0/2
@{$defs;} or @{$fks;}
@{$defs;}
@{$fks;}
Truth table66%2/3
A or B
A= @{$defs;}B= @{$fks;}
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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) && $_
MC/DC0%0/2
$_->[0]->table($table) && $_
$_->[0]->table($table)
$_
Truth table0%0/3
A && B
A= $_->[0]->table($table)B= $_
| |||||||||||||||||||||||||||||||||||||||||||||||
| 1186 | 0 | ▶ | $diff_hash->{rename_field} = [ map { $_->[0]->table($table) && $_ } @{ $diff_hash->{rename_field} } ]; | ||||||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/3
$_->[0]->table($table) && $_
MC/DC0%0/2
$_->[0]->table($table) && $_
$_->[0]->table($table)
$_
Truth table0%0/3
A && B
A= $_->[0]->table($table)B= $_
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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 | ||||||||||||||||||||||||||||||||||||||||||||||