Informix/DDL.pm

filter: Statementstmt 98.2% 220 / 224 Branchbran 74.1% 89 / 120 Conditioncond 62.6% 47 / 75 MC/DCmcdc 40.9% 18 / 44 Subroutinesub 100% 18 / 18 Podpod 100% 1 / 1 totaltotal 81.5% 393 / 482 CC 99 SCAR 37.9
CC
99
Cov
85%
install_ddl
72.3
_unique_index_only
22.6
_cascading_has_many
19.0
CRAP 44.4
« Informix/Storage.pm ↑ Summary Informix/Diff/ForeignKey.pm »
1 package DBIO::Informix::DDL;
2 # ABSTRACT: Generate Informix DDL from DBIO Result classes
3  
4 22 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 22 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 22 our $VERSION = '0.900007';
8  
9 22 ▶use DBIO::Informix::Util qw(_quote_ident);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 22 ▶use DBIO::Informix::ColumnDef qw(column_def_sql);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 22 ▶use DBIO::Informix::Types qw(ddl_type);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 22 ▶use DBIO::Informix::Diff::ForeignKey ();
Subroutine BEGIN: called CC=1 SCAR=0.0
13 22 ▶use DBIO::Diff::Op ();
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 # What the shared conformance tables ask to be refused rather than built as
16 # something else: no Informix type or object means what the declaration says.
17 22 my $NO_SCHEMAS_REASON = 'Informix has no schemas: owner.name names a user, and a dot '
18 . 'would become part of a literal name';
19  
20 22 my %REFUSED_TYPE = (
21 bit => 'Informix has no type for a bit string of a fixed width: BOOLEAN is one '
22 . 'bit and BYTE is a binary large object, so any mapping changes what the size means',
23 enum => 'Informix has no enumerated type, and a VARCHAR with a CHECK would '
24 . 'be a different column (its values are not a list the server knows)',
25 );
26  
27  
28  
29 93 ▶sub install_ddl {
Subroutine install_ddl: called CC=62 SCAR=42.8
Pod documented
30 93 my ($class, $schema) = @_;
31  
32 93 my (@stmts, @unique_idx_stmts, @fk_stmts, @index_stmts, @view_stmts);
33 93 my (%seen_table, %seen_view, %seen_fk, %view_stmt, %view_def);
34  
35 93 for my $source_name (sort $schema->sources) {
36 131 my $source = $schema->source($source_name);
37 131 my $table_name = $source->name;
38  
39 # Views: emit CREATE VIEW after all tables; skip virtual views.
40 131 ▶ if ($source->isa('DBIO::ResultSource::View')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($source->isa("DBIO::ResultSource::View")) 6 125 131
41 6 ▶ next if $source->can('is_virtual') && $source->is_virtual;
Branch: 1 branch
BranchTrueFalseTotal
if $source->can("is_virtual") and $source->is_virtual 0 6 6
Condition33%1/3
$source->can("is_virtual") and $source->is_virtual
!ll&&!rl&&r
060
MC/DC0%0/2
$source->can("is_virtual") and $source->is_virtual
$source->can("is_virtual") $source->is_virtual
Truth table33%1/3
A and B
A= $source->can("is_virtual")B= $source->is_virtual
ABresult
0X0
100
111
42 6 my $vliteral = ref $table_name eq 'SCALAR';
43 6 ▶ my $vname = $vliteral ? $$table_name : $table_name;
Branch: 1 branch
BranchTrueFalseTotal
$vliteral ? : 0 6 6
44 6 ▶ $source->throw_exception(
Branch: 1 branch
BranchTrueFalseTotal
if not $vliteral || ref $vname and $vname =~ /\./ 1 5 6
Condition50%3/6
not $vliteral || ref $vname and $vname =~ /\./
!ll&&!rl&&r
051
$vliteral || ref $vname
l!l&&r!l&&!r
006
MC/DC33%1/3
not $vliteral || ref $vname and $vname =~ /\./
$vliteral ref $vname $vname =~ /\./
Truth table50%2/4
not(A || B) and C
A= $vliteralB= ref $vnameC= $vname =~ /\./
ABCresult
1XX0
01X0
0000
0011
45 "view '$vname': refused, $NO_SCHEMAS_REASON")
46 if !$vliteral && !ref $vname && $vname =~ /\./;
47 5 ▶ next if ref $vname || $seen_view{$vname}++;
Branch: 1 branch
BranchTrueFalseTotal
if ref $vname or $seen_view{$vname}++ 0 5 5
Condition33%1/3
ref $vname or $seen_view{$vname}++
l!l&&r!l&&!r
005
MC/DC0%0/2
ref $vname or $seen_view{$vname}++
ref $vname $seen_view{$vname}++
Truth table33%1/3
A or B
A= ref $vnameB= $seen_view{$vname}++
ABresult
1X1
011
000
48 5 ▶ my $def = $source->can('view_definition') ? $source->view_definition : undef;
Branch: 1 branch
BranchTrueFalseTotal
$source->can('view_definition') ? : 5 0 5
49 5 ▶ next unless defined $def && length $def;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $def and length $def 0 5 5
Condition33%1/3
defined $def and length $def
!ll&&!rl&&r
005
MC/DC0%0/2
defined $def and length $def
defined $def length $def
Truth table33%1/3
A and B
A= defined $defB= length $def
ABresult
0X0
100
111
50 5 ▶ $view_stmt{$vname} = sprintf 'CREATE VIEW %s AS %s;',
Branch: 1 branch
BranchTrueFalseTotal
$vliteral ? : 0 5 5
51 ($vliteral ? $vname : _quote_ident($vname)), $def;
52 5 $view_def{$vname} = $def;
53 5 next;
54 }
55  
56 # A scalar-ref name is either a literal table name (\'cd', emitted
57 # unquoted) or an inline subquery source; deref the former, skip the latter.
58 125 my $literal = ref $table_name eq 'SCALAR';
59 125 ▶ if ($literal) {
Branch: 1 branch
BranchTrueFalseTotal
if ($literal) 1 124 125
60 1 $table_name = $$table_name;
61 1 ▶ next if $table_name =~ /\s|\(/;
Branch: 1 branch
BranchTrueFalseTotal
if $table_name =~ /\s|\(/ 0 1 1
62 }
63 125 ▶ next if ref $table_name;
Branch: 1 branch
BranchTrueFalseTotal
if ref $table_name 0 125 125
64  
65 # Informix has no schemas: owner.table names a user, not a place of the
66 # database, and a dotted name would become part of a literal name.
67 125 ▶ $source->throw_exception("table '$table_name': refused, $NO_SCHEMAS_REASON")
Branch: 1 branch
BranchTrueFalseTotal
if not $literal and $table_name =~ /\./ 1 124 125
Condition100%3/3
not $literal and $table_name =~ /\./
!ll&&!rl&&r
11231
MC/DC100%2/2
not $literal and $table_name =~ /\./
not $literal $table_name =~ /\./
Truth table100%3/3
not(A) and B
A= not $literalB= $table_name =~ /\./
ABresult
0X0
100
111
68 if !$literal && $table_name =~ /\./;
69  
70 # Several result sources can map to one physical table; emit it once.
71 124 ▶ next if $seen_table{$table_name}++;
Branch: 1 branch
BranchTrueFalseTotal
if $seen_table{$table_name}++ 0 124 124
72  
73 124 my @col_defs;
74 124 my @pk_cols = $source->primary_columns;
75  
76 124 for my $col_name ($source->columns) {
77 278 my $info = $source->column_info($col_name);
78 278 ▶ if (my $why = $REFUSED_TYPE{ lc($info->{data_type} // '') }) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $why = $REFUSED_TYPE{lc($info->{"data_type"} // "")}) 2 276 278
Condition50%1/2
$info->{"data_type"} // ""
l!l
2780
MC/DC0%0/1
$info->{"data_type"} // ""
$info->{"data_type"}
Truth table50%1/2
A
A= $info->{"data_type"}
Aresult
11
00
79 2 $source->throw_exception(
80 "column '$col_name' on '$table_name': data_type '$info->{data_type}' "
81 . "is refused: $why");
82 }
83 push @col_defs, ' ' . column_def_sql(
84 name => $col_name,
85 type => _informix_column_type($info),
86 not_null => !$info->{is_nullable}, # ADR 0043
87 default_value => $info->{default_value},
88 is_auto_increment => $info->{is_auto_increment},
89 276 );
90 }
91  
92 # Primary key constraint
93 push @col_defs, sprintf ' PRIMARY KEY (%s)',
94 122 ▶ join(', ', map { _quote_ident($_) } @pk_cols)
Branch: 1 branch
BranchTrueFalseTotal
if @pk_cols 122 0 122
95 if @pk_cols;
96  
97 # Unique constraints (add_unique_constraint) under the name declared.
98 # One over exactly the primary key's columns says nothing more and is not
99 # written. Informix holds one index over a column list (-350): a second
100 # declaration over the same columns is refused, naming both, never one
101 # of them quietly dropped (karr k27).
102 122 my %uniques = $source->unique_constraints;
103 122 my $pk_key = join "\0", sort @pk_cols;
104 122 my %has_index; # column list => what declares it, for the message
105 122 ▶ $has_index{ join "\0", @pk_cols } = 'the primary key' if @pk_cols;
Branch: 1 branch
BranchTrueFalseTotal
if @pk_cols 122 0 122
106 46 ▶ my $same_columns = sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
107 46 my ($key, $what, $cols) = @_;
108 46 ▶ my $prev = $has_index{$key} // return;
Condition100%2/2
$has_index{$key} // (return)
l!l
1135
MC/DC100%1/1
$has_index{$key} // (return)
$has_index{$key}
Truth table100%2/2
A
A= $has_index{$key}
Aresult
11
00
109 11 $source->throw_exception(
110 "table '$table_name': refused, $what and $prev are over the same columns ("
111 . join(', ', @$cols) . "); Informix holds only one index there (-350), "
112 . "declare the key once");
113 122 };
114 122 for my $uname (sort keys %uniques) {
115 145 ▶ next if $uname eq 'primary';
Branch: 1 branch
BranchTrueFalseTotal
if $uname eq "primary" 122 23 145
116 23 ▶ next if join("\0", sort @{ $uniques{$uname} }) eq $pk_key;
Branch: 1 branch
BranchTrueFalseTotal
if join("\0", sort(@{$uniques{$uname};})) eq $pk_key 4 19 23
117 19 my $ukey = join "\0", @{ $uniques{$uname} };
118 19 $same_columns->($ukey, "unique constraint '$uname'", $uniques{$uname});
119 18 $has_index{$ukey} = "unique constraint '$uname'";
120 push @col_defs, sprintf ' UNIQUE (%s) CONSTRAINT %s',
121 18 join(', ', map { _quote_ident($_) } @{ $uniques{$uname} }), _quote_ident($uname);
122 }
123  
124 121 ▶ my $qualified = $literal ? $table_name : _quote_ident($table_name);
Branch: 1 branch
BranchTrueFalseTotal
$literal ? : 1 120 121
125 121 push @stmts, sprintf "CREATE TABLE %s (\n%s\n);", $qualified, join(",\n", @col_defs);
126  
127 # Foreign keys from the belongs_to relationships (is_foreign_key_constraint).
128 # Informix knows ON DELETE CASCADE and nothing else: any other action, an
129 # ON UPDATE and DEFERRABLE are refused, never left out. Where the
130 # relationship declares no on_delete, the other side decides as
131 # DBIO::Relationship::Base describes (a has_many with cascade_delete).
132 121 my %fk_cols;
133 121 for my $rel (sort $source->relationships) {
134 56 my $info = $source->relationship_info($rel);
135 56 ▶ next unless $info && $info->{attrs} && $info->{attrs}{is_foreign_key_constraint};
Branch: 1 branch
BranchTrueFalseTotal
unless $info and $info->{"attrs"} and $info->{"attrs"}{"is_foreign_key_constraint"} 5 51 56
Condition50%3/6
$info and $info->{"attrs"} and $info->{"attrs"}{"is_foreign_key_constraint"}
!ll&&!rl&&r
0551
$info and $info->{"attrs"}
!ll&&!rl&&r
0056
MC/DC33%1/3
$info and $info->{"attrs"} and $info->{"attrs"}{"is_foreign_key_constraint"}
$info $info->{"attrs"} $info->{"attrs"}{"is_foreign_key_constraint"}
Truth table50%2/4
A and B and C
A= $infoB= $info->{"attrs"}C= $info->{"attrs"}{"is_foreign_key_constraint"}
ABCresult
0XX0
10X0
1100
1111
136  
137 51 my $foreign = $info->{class};
138 51 my $foreign_source = eval { $schema->source($foreign) }
139 0 ▶ // eval { $schema->source($foreign =~ s/.*:://r) };
Condition33%1/3
eval { do { $schema->source($foreign) } } // eval { do { $schema->source($foreign =~ s/.*:://r) } }
l!l&&r!l&&!r
5100
MC/DC0%0/2
eval { do { $schema->source($foreign) } } // eval { do { $schema->source($foreign =~ s/.*:://r) } }
eval { do { $schema->source($foreign) } } eval { do { $schema->source($foreign =~ s/.*:://r) } }
Truth table33%1/3
A // B
A= eval { do { $schema->source($foreign) } }B= eval { do { $schema->source($foreign =~ s/.*:://r) } }
ABresult
1X1
011
000
140 51 ▶ next unless $foreign_source;
Branch: 1 branch
BranchTrueFalseTotal
unless $foreign_source 0 51 51
141 51 my $cond = $info->{cond};
142 51 ▶ next unless ref $cond eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $cond eq "HASH" 0 51 51
143  
144 51 my $attrs = $info->{attrs};
145 $source->throw_exception(
146 "relationship '$rel' on '$table_name': refused, is_deferrable cannot be built, "
147 . "Informix has no deferrable constraints")
148 51 ▶ if $attrs->{is_deferrable};
Branch: 1 branch
BranchTrueFalseTotal
if $attrs->{"is_deferrable"} 1 50 51
149  
150 50 my @pairs;
151 50 for my $foreign_col (sort keys %$cond) {
152 56 (my $fcol = $foreign_col) =~ s/^foreign\.//;
153 56 (my $scol = $cond->{$foreign_col}) =~ s/^self\.//;
154 56 push @pairs, [ $scol, $fcol ];
155 }
156 # The pairs go in the order of the target key (the first primary key
157 # or unique constraint whose columns are these): the server takes a
158 # reference only in the key's own order (-297), so a key in another
159 # order than the foreign column names is built, not refused (karr k29).
160 50 ▶ if (my $key = _key_matching_set($foreign_source, [ map { $_->[1] } @pairs ])) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $key = _key_matching_set($foreign_source, [map({$_->[1];} @pairs)])) 48 2 50
161 48 my $i = 0;
162 48 my %pos = map { $_ => $i++ } @$key;
163 48 @pairs = sort { $pos{ $a->[1] } <=> $pos{ $b->[1] } } @pairs;
164 }
165 50 my @from = map { $_->[0] } @pairs;
166 50 my @to = map { $_->[1] } @pairs;
167  
168 50 my $to_name = $foreign_source->name;
169 50 ▶ my $to_bare = ref $to_name eq 'SCALAR' ? $$to_name : $to_name;
Branch: 1 branch
BranchTrueFalseTotal
ref $to_name eq 'SCALAR' ? : 0 50 50
170  
171 # The target of a key is a primary key or a unique constraint; a
172 # unique index alone is -297 on the server (karr k27).
173 50 ▶ if (my $index = _unique_index_only($foreign_source, \@to)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $index = _unique_index_only($foreign_source, \@to)) 2 48 50
174 2 $source->throw_exception(
175 "relationship '$rel' on '$table_name': refused, the foreign key over "
176 . join(', ', @from) . " references $to_bare (" . join(', ', @to) . ") which only "
177 . "$index covers; Informix wants a primary key or a unique constraint as the target "
178 . "(-297). Declare it with add_unique_constraint, as a unique constraint "
179 . "instead of a unique index");
180 }
181  
182 my $on_delete = exists $attrs->{on_delete} ? $attrs->{on_delete}
183 48 ▶ : _cascading_has_many($foreign_source, $table_name, \@from, \@to, 'cascade_delete')
Branch: 2 branches
BranchTrueFalseTotal
exists $attrs->{'on_delete'} ? : 7 41 48
_cascading_has_many($foreign_source, $table_name, \@from, \@to, 'cascade_delete') ? : 3 38 41
184 ? 'CASCADE' : undef;
185 # Only a declared on_update is asked of the server. cascade_copy is on by
186 # default on every has_many, so deriving ON UPDATE CASCADE from it would
187 # refuse the ordinary has_many + belongs_to pair; Informix has no ON
188 # UPDATE and a derived value is no declaration (karr k25).
189 48 my $on_update = $attrs->{on_update};
190 48 my $where = "relationship '$rel' on '$table_name'";
191 48 $on_delete = _fk_action($source, 'on_delete', $on_delete, $where, 'CASCADE');
192 47 $on_update = _fk_action($source, 'on_update', $on_update, $where);
193  
194 # Relationships that declare the same key are one key; the same columns
195 # with another action are two keys the server would take as one:
196 # refused, never one of them quietly.
197 45 my $by_cols = join "\0", $table_name, @from, '->', $to_bare, @to;
198 45 my $stmt = sprintf 'ALTER TABLE %s ADD CONSTRAINT %s;', $qualified,
199 DBIO::Informix::Diff::ForeignKey::fk_clause($table_name, {
200 from_columns => \@from, to_table => $to_bare, to_columns => \@to,
201 on_delete => $on_delete,
202 });
203 45 ▶ if (defined(my $prev = $seen_fk{$by_cols})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $prev = $seen_fk{$by_cols})) 2 43 45
204 2 ▶ next if $prev eq $stmt;
Branch: 1 branch
BranchTrueFalseTotal
if $prev eq $stmt 1 1 2
205 1 $source->throw_exception(
206 "relationship '$rel' on '$table_name': refused, it declares a foreign key over "
207 . join(', ', @from) . " that another relationship declares with another "
208 . "on_delete action; Informix cannot hold both");
209 }
210 43 $seen_fk{$by_cols} = $stmt;
211 43 $fk_cols{ join "\0", @from } = 1;
212 43 push @fk_stmts, $stmt;
213 }
214  
215 # Unique indexes of a column (is_unique), then the declared indexes
216 # (ADR 0061). Informix refuses a second index over the same columns in
217 # the same order (-350), and builds one itself for a primary key, a
218 # unique constraint and a foreign key: a declaration over the columns of
219 # one of the first two is refused (above), a plain index over a foreign
220 # key's columns is not written again. A UNIQUE index over a foreign key's columns says more
221 # than the key and goes before the key, which then shares it.
222 114 my %declared;
223 114 for my $decl ($source->index_declarations_for('informix', [])) {
224 27 ▶ if (defined $decl->{type} && lc $decl->{type} ne 'unique') {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $decl->{"type"} and lc $decl->{"type"} ne "unique") 1 26 27
Condition100%3/3
defined $decl->{"type"} and lc $decl->{"type"} ne "unique"
!ll&&!rl&&r
2061
MC/DC100%2/2
defined $decl->{"type"} and lc $decl->{"type"} ne "unique"
defined $decl->{"type"} lc $decl->{"type"} ne "unique"
Truth table100%3/3
A and B
A= defined $decl->{"type"}B= lc $decl->{"type"} ne "unique"
ABresult
0X0
100
111
225 1 $source->throw_exception(
226 "index '$decl->{name}' on '$table_name': type '$decl->{type}' "
227 . "cannot be built on Informix (only a plain or unique index)");
228 }
229 $source->throw_exception(
230 "index '$decl->{name}' on '$table_name': options cannot be built on Informix")
231 26 ▶ if defined $decl->{options};
Branch: 1 branch
BranchTrueFalseTotal
if defined $decl->{"options"} 1 25 26
232 25 $declared{ $decl->{name} } = $decl;
233 }
234  
235 109 for my $col_name ($source->columns) {
236 239 my $info = $source->column_info($col_name);
237 239 ▶ next unless $info->{is_unique} || $info->{is_single_unique_key};
Branch: 1 branch
BranchTrueFalseTotal
unless $info->{"is_unique"} or $info->{"is_single_unique_key"} 233 6 239
Condition66%2/3
$info->{"is_unique"} or $info->{"is_single_unique_key"}
l!l&&r!l&&!r
60233
MC/DC50%1/2
$info->{"is_unique"} or $info->{"is_single_unique_key"}
$info->{"is_unique"} $info->{"is_single_unique_key"}
Truth table66%2/3
A or B
A= $info->{"is_unique"}B= $info->{"is_single_unique_key"}
ABresult
1X1
011
000
238 6 ▶ next if @pk_cols == 1 && $pk_cols[0] eq $col_name; # the primary key says it
Branch: 1 branch
BranchTrueFalseTotal
if @pk_cols == 1 and $pk_cols[0] eq $col_name 0 6 6
Condition33%1/3
@pk_cols == 1 and $pk_cols[0] eq $col_name
!ll&&!rl&&r
060
MC/DC0%0/2
@pk_cols == 1 and $pk_cols[0] eq $col_name
@pk_cols == 1 $pk_cols[0] eq $col_name
Truth table33%1/3
A and B
A= @pk_cols == 1B= $pk_cols[0] eq $col_name
ABresult
0X0
100
111
239 6 $same_columns->($col_name, "the unique column '$col_name'", [$col_name]);
240 6 $has_index{$col_name} = "the unique column '$col_name'";
241 6 push @unique_idx_stmts, sprintf 'CREATE UNIQUE INDEX %s ON %s (%s);',
242 _quote_ident("${table_name}_$col_name"), $qualified, _quote_ident($col_name);
243 }
244  
245 109 my %fk_index; # column list of a foreign key => the plain index declared over it
246 109 for my $name (sort keys %declared) {
247 25 my $decl = $declared{$name};
248 25 my $key = join "\0", @{ $decl->{columns} };
249 # a plain index over a foreign key's columns: the key builds one
250 25 ▶ if (!$decl->{unique} && $fk_cols{$key} && !defined $has_index{$key}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $decl->{"unique"} and $fk_cols{$key} and not defined $has_index{$key}) 4 21 25
Condition83%5/6
not $decl->{"unique"} and $fk_cols{$key} and not defined $has_index{$key}
!ll&&!rl&&r
2104
not $decl->{"unique"} and $fk_cols{$key}
!ll&&!rl&&r
6154
MC/DC66%2/3
not $decl->{"unique"} and $fk_cols{$key} and not defined $has_index{$key}
not $decl->{"unique"} $fk_cols{$key} not defined $has_index{$key}
Truth table75%3/4
not(A) and B and not(C)
A= not $decl->{"unique"}B= $fk_cols{$key}C= not defined $has_index{$key}
ABCresult
0XX0
10X0
1100
1111
251 4 ▶ $fk_index{$key} //= $name;
Condition66%2/3
$fk_index{$key} //= $name
l!l&&r!l&&!r
130
MC/DC0%0/2
$fk_index{$key} //= $name
$fk_index{$key} $name
Truth table66%2/3
A //= B
A= $fk_index{$key}B= $name
ABresult
1X1
011
000
252 $source->throw_exception(
253 "table '$table_name': refused, index '$name' and index '$fk_index{$key}' are over the same "
254 1 . "columns (" . join(', ', @{ $decl->{columns} }) . "); Informix holds only one index there "
255 4 ▶ . "(-350), declare the key once") if $fk_index{$key} ne $name;
Branch: 1 branch
BranchTrueFalseTotal
if $fk_index{$key} ne $name 1 3 4
256 3 next;
257 }
258 21 $same_columns->($key, "index '$name'", $decl->{columns});
259 11 $has_index{$key} = "index '$name'";
260 11 ▶ push @{ $decl->{unique} ? \@unique_idx_stmts : \@index_stmts },
Branch: 1 branch
BranchTrueFalseTotal
$decl->{'unique'} ? : 2 9 11
261 _create_index($name, $qualified, $decl);
262 }
263 }
264  
265 # A view that reads another is created after it, whatever the alphabet says.
266 5 ▶ @view_stmts = map { $view_stmt{$_} } DBIO::Diff::Op::_order_by_dependency(sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
267 5 my $name = $_[0];
268 5 ▶ [ grep { $_ ne $name && $view_def{$name} =~ /(?<![\w\$])\Q$_\E(?![\w\$])/i } keys %view_def ];
Condition100%3/3
$_ ne $name && $view_def{$name} =~ /(?<![\w\$])\Q$_\E(?![\w\$])/i
!ll&&!rl&&r
511
MC/DC100%2/2
$_ ne $name && $view_def{$name} =~ /(?<![\w\$])\Q$_\E(?![\w\$])/i
$_ ne $name $view_def{$name} =~ /(?<![\w\$])\Q$_\E(?![\w\$])/i
Truth table100%3/3
A && B
A= $_ ne $nameB= $view_def{$name} =~ /(?<![\w\$])\Q$_\E(?![\w\$])/i
ABresult
0X0
100
111
269 65 }, keys %view_def);
270  
271 65 return join "
272  
273 ", @stmts, @unique_idx_stmts, @fk_stmts, @index_stmts, @view_stmts;
274 }
275  
276 # What covers exactly these columns of $target in a way the server cannot
277 # reference: a unique index (a declared one, or a column's is_unique), and
278 # neither the primary key nor a unique constraint. Returns its description,
279 # or undef when the columns are covered by a key (or by nothing).
280 50 ▶sub _unique_index_only {
Subroutine _unique_index_only: called CC=13 SCAR=31.2
281 50 my ($target, $cols) = @_;
282 50 my $want = join "\0", sort @$cols;
283 50 ▶ return undef if _key_in_order($target, $cols);
Branch: 1 branch
BranchTrueFalseTotal
if _key_in_order($target, $cols) 48 2 50
284 2 ▶ return undef if join("\0", sort $target->primary_columns) eq $want;
Branch: 1 branch
BranchTrueFalseTotal
if join("\0", sort($target->primary_columns)) eq $want 0 2 2
285 2 my %uniques = $target->unique_constraints;
286 2 for my $uname (keys %uniques) {
287 2 ▶ next if $uname eq 'primary';
Branch: 1 branch
BranchTrueFalseTotal
if $uname eq "primary" 2 0 2
288 0 ▶ return undef if join("\0", sort @{ $uniques{$uname} }) eq $want;
Branch: 1 branch
BranchTrueFalseTotal
if join("\0", sort(@{$uniques{$uname};})) eq $want 0 0 0
289 }
290 2 for my $decl ($target->index_declarations_for('informix', [])) {
291 1 ▶ next unless $decl->{unique};
Branch: 1 branch
BranchTrueFalseTotal
unless $decl->{"unique"} 0 1 1
292 1 ▶ return "the unique index '$decl->{name}'" if join("\0", sort @{ $decl->{columns} }) eq $want;
Branch: 1 branch
BranchTrueFalseTotal
if join("\0", sort(@{$decl->{"columns"};})) eq $want 1 0 1
293 }
294 1 ▶ if (@$cols == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$cols == 1) 1 0 1
295 1 my $info = $target->column_info($cols->[0]);
296 1 ▶ return "the unique column '$cols->[0]'" if $info && ($info->{is_unique} || $info->{is_single_unique_key});
Branch: 1 branch
BranchTrueFalseTotal
if $info and $info->{"is_unique"} || $info->{"is_single_unique_key"} 1 0 1
Condition33%2/6
$info and $info->{"is_unique"} || $info->{"is_single_unique_key"}
!ll&&!rl&&r
001
$info->{"is_unique"} or $info->{"is_single_unique_key"}
l!l&&r!l&&!r
100
MC/DC0%0/3
$info and $info->{"is_unique"} || $info->{"is_single_unique_key"}
$info $info->{"is_unique"} $info->{"is_single_unique_key"}
Truth table25%1/4
A and B or C
A= $infoB= $info->{"is_unique"}C= $info->{"is_single_unique_key"}
ABCresult
0XX0
1000
11X1
1011
297 }
298 0 return undef;
299 }
300  
301 # The primary key and the unique constraints of $target, as [description,
302 # columns in key order].
303 100 ▶sub _target_keys {
Subroutine _target_keys: called CC=1 SCAR=0.0
304 100 my ($target) = @_;
305 100 my %uniques = $target->unique_constraints;
306 return (
307 [ 'the primary key', [ $target->primary_columns ] ],
308 100 map { [ "the unique constraint '$_'", $uniques{$_} ] } grep { $_ ne 'primary' } sort keys %uniques,
309 );
310 }
311  
312 # True when a primary key or unique constraint of $target covers exactly
313 # these columns in this order (what the server accepts as the target).
314 50 ▶sub _key_in_order {
Subroutine _key_in_order: called CC=1 SCAR=0.0
315 50 my ($target, $cols) = @_;
316 50 my $want = join "\0", @$cols;
317 50 return scalar grep { join("\0", @{ $_->[1] }) eq $want } _target_keys($target);
318 }
319  
320 # The columns (in key order) of the first primary key or unique constraint
321 # of $target that covers exactly this set of columns, else undef.
322 50 ▶sub _key_matching_set {
Subroutine _key_matching_set: called CC=2 SCAR=6.9
323 50 my ($target, $cols) = @_;
324 50 my $set = join "\0", sort @$cols;
325 50 my ($key) = grep { join("\0", sort @{ $_->[1] }) eq $set } _target_keys($target);
326 50 ▶ return $key ? $key->[1] : undef;
Branch: 1 branch
BranchTrueFalseTotal
$key ? : 48 2 50
327 }
328  
329 11 ▶sub _create_index {
Subroutine _create_index: called CC=2 SCAR=6.9
330 11 my ($name, $table_sql, $decl) = @_;
331 return sprintf 'CREATE %sINDEX %s ON %s (%s);',
332 ($decl->{unique} ? 'UNIQUE ' : ''),
333 _quote_ident($name), $table_sql,
334 11 ▶ join(', ', map { _quote_ident($_) } @{ $decl->{columns} });
Branch: 1 branch
BranchTrueFalseTotal
$decl->{'unique'} ? : 2 9 11
335 }
336  
337 # The action of one rule as Informix can hold it: ON DELETE CASCADE, or none.
338 # RESTRICT / NO ACTION are what a key without a clause does. Anything else,
339 # and any ON UPDATE action, is refused.
340 95 ▶sub _fk_action {
Subroutine _fk_action: called CC=8 SCAR=20.8
341 95 my ($source, $event, $rule, $where, $allowed) = @_;
342 95 ▶ return undef unless defined $rule;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $rule 82 13 95
343 13 (my $r = uc $rule) =~ s/\A\s+|\s+\z//g;
344 13 $r =~ s/\s+/ /g;
345 13 ▶ return undef if $r eq '' || $r eq 'RESTRICT' || $r eq 'NO ACTION';
Branch: 1 branch
BranchTrueFalseTotal
if $r eq "" or $r eq "RESTRICT" or $r eq "NO ACTION" 4 9 13
Condition100%6/6
$r eq "" or $r eq "RESTRICT" or $r eq "NO ACTION"
l!l&&r!l&&!r
229
$r eq "" or $r eq "RESTRICT"
l!l&&r!l&&!r
1111
MC/DC100%3/3
$r eq "" or $r eq "RESTRICT" or $r eq "NO ACTION"
$r eq "" $r eq "RESTRICT" $r eq "NO ACTION"
Truth table100%4/4
A or B or C
A= $r eq ""B= $r eq "RESTRICT"C= $r eq "NO ACTION"
ABCresult
1XX1
01X1
0011
0000
346 9 ▶ return $r if defined $allowed && $r eq $allowed;
Branch: 1 branch
BranchTrueFalseTotal
if defined $allowed and $r eq $allowed 6 3 9
Condition100%3/3
defined $allowed and $r eq $allowed
!ll&&!rl&&r
216
MC/DC100%2/2
defined $allowed and $r eq $allowed
defined $allowed $r eq $allowed
Truth table100%3/3
A and B
A= defined $allowedB= $r eq $allowed
ABresult
0X0
100
111
347 3 ▶ $source->throw_exception(
Branch: 1 branch
BranchTrueFalseTotal
defined $allowed ? : 1 2 3
348 "$where: refused, $event '$rule' cannot be built, Informix has "
349 . (defined $allowed ? 'only ON DELETE CASCADE' : 'no ON UPDATE action'));
350 }
351  
352 # Whether some has_many of $target (the referenced table's source) over the
353 # column pairs of this key has $attr set: the other side of the belongs_to,
354 # which is what DBIO::Relationship::Base describes as the autodetection of
355 # on_delete (cascade_delete) and on_update (cascade_copy).
356 41 ▶sub _cascading_has_many {
Subroutine _cascading_has_many: called CC=15 SCAR=29.4
357 41 my ($target, $table, $from, $to, $attr) = @_;
358 41 my $want = join "\0", sort map { "$from->[$_]\t$to->[$_]" } 0 .. $#$from;
359 41 for my $rel ($target->relationships) {
360 6 ▶ my $info = $target->relationship_info($rel) or next;
Branch: 1 branch
BranchTrueFalseTotal
my $info = $target->relationship_info($rel) or next 6 0 6
361 6 ▶ next unless $info->{attrs} && $info->{attrs}{$attr};
Branch: 1 branch
BranchTrueFalseTotal
unless $info->{"attrs"} and $info->{"attrs"}{$attr} 3 3 6
Condition66%2/3
$info->{"attrs"} and $info->{"attrs"}{$attr}
!ll&&!rl&&r
033
MC/DC50%1/2
$info->{"attrs"} and $info->{"attrs"}{$attr}
$info->{"attrs"} $info->{"attrs"}{$attr}
Truth table66%2/3
A and B
A= $info->{"attrs"}B= $info->{"attrs"}{$attr}
ABresult
0X0
100
111
362 3 ▶ next unless ref $info->{cond} eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $info->{"cond"} eq "HASH" 0 3 3
363 3 ▶ my $other = eval { $target->related_source($rel) } or next;
Branch: 1 branch
BranchTrueFalseTotal
my $other = eval { do { $target->related_source($rel) } } or next 3 0 3
364 3 my $oname = $other->name;
365 3 ▶ $oname = $$oname if ref $oname eq 'SCALAR';
Branch: 1 branch
BranchTrueFalseTotal
if ref $oname eq "SCALAR" 0 3 3
366 3 ▶ next unless !ref $oname && $oname eq $table;
Branch: 1 branch
BranchTrueFalseTotal
unless not ref $oname and $oname eq $table 0 3 3
Condition33%1/3
not ref $oname and $oname eq $table
!ll&&!rl&&r
003
MC/DC0%0/2
not ref $oname and $oname eq $table
not ref $oname $oname eq $table
Truth table33%1/3
not(A) and B
A= not ref $onameB= $oname eq $table
ABresult
0X0
100
111
367 3 my @pairs;
368 3 for my $k (keys %{ $info->{cond} }) {
369 3 ▶ my ($f) = $k =~ /\Aforeign\.(\w+)\z/ or next;
Branch: 1 branch
BranchTrueFalseTotal
my($f) = $k =~ /\Aforeign\.(\w+)\z/ or next 3 0 3
370 3 ▶ my ($sc) = ($info->{cond}{$k} // '') =~ /\Aself\.(\w+)\z/ or next;
Branch: 1 branch
BranchTrueFalseTotal
my($sc) = ($info->{"cond"}{$k} // "") =~ /\Aself\.(\w+)\z/ or next 3 0 3
Condition50%1/2
$info->{"cond"}{$k} // ""
l!l
30
MC/DC0%0/1
$info->{"cond"}{$k} // ""
$info->{"cond"}{$k}
Truth table50%1/2
A
A= $info->{"cond"}{$k}
Aresult
11
00
371 3 push @pairs, "$f\t$sc"; # the child's column, the parent's column
372 }
373 3 ▶ return 1 if (join "\0", sort @pairs) eq $want;
Branch: 1 branch
BranchTrueFalseTotal
if join("\0", sort(@pairs)) eq $want 3 0 3
374 }
375 38 return 0;
376 }
377  
378 298 ▶sub _informix_column_type {
Subroutine _informix_column_type: called CC=1 SCAR=0.0
379 298 my ($info) = @_;
380 298 return ddl_type($info->{data_type}, $info->{size});
381 }
382  
383  
384 22 1;
385  
386 __END__
« Informix/Storage.pm ↑ Summary Informix/Diff/ForeignKey.pm »