Firebird/DDL.pm

filter: Statementstmt 99.2% 250 / 252 Branchbran 89.6% 147 / 164 Conditioncond 72.2% 60 / 83 MC/DCmcdc 57.1% 28 / 49 Subroutinesub 95.8% 23 / 24 Podpod 100% 2 / 2 totaltotal 88.8% 510 / 574 CC 127 SCAR 39.1
CC
127
Cov
92%
install_ddl
85.2
_firebird_cascading_has_many
16.7
_firebird_default_literal
10.0
CRAP 50.0
« Firebird/Introspect/Indexes.pm ↑ Summary Firebird/Diff/Column.pm »
1 package DBIO::Firebird::DDL;
2 # ABSTRACT: Generate Firebird DDL from DBIO Result classes
3  
4 23 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 23 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 23 our $VERSION = '0.900007';
8  
9 23 ▶use Carp 'croak';
Subroutine BEGIN: called CC=1 SCAR=0.0
10 23 ▶use Digest::MD5 ();
Subroutine BEGIN: called CC=1 SCAR=0.0
11 23 ▶use DBIO::Exception ();
Subroutine BEGIN: called CC=1 SCAR=0.0
12 23 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 23 ▶use DBIO::Firebird::Type qw(ddl_type_from_info is_boolean_type);
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 23 our @EXPORT_OK = qw(
17 _firebird_quote_ident _firebird_default_literal
18 _firebird_column_clauses _firebird_identity_type_ok
19 _firebird_add_foreign_key _firebird_add_unique _firebird_fk_name
20 );
21  
22 # Firebird's identifier quoter: ALWAYS quotes. DBIO::SQL::Util::_quote_ident
23 # leaves a simple name bare, which is right where a bare name folds to lower
24 # case and wrong here: Firebird folds a bare name to UPPER case and does not
25 # parse a bare reserved word (year, position, value, type, ...) at all. The
26 # storage is a quote-everything storage (sql_quote_char '"' on
27 # Storage::Common, with or without quote_names), so the ORM asks for "artist";
28 # a table created as `CREATE TABLE artist` is ARTIST and is then
29 # `Table unknown` (karr k32). The same holds on the way back: the names in an
30 # introspected model are the server's exact spelling, and a lower-case one
31 # written bare into a diff statement addresses a different object. So every
32 # name this distribution emits -- DDL.pm and the three Diff ops -- goes through
33 # here. An embedded `"` is doubled, per the SQL standard.
34 1567 ▶sub _firebird_quote_ident {
Subroutine _firebird_quote_ident: called CC=2 SCAR=6.9
35 1567 my ($name) = @_;
36 1567 ▶ croak 'DBIO::Firebird::DDL::_firebird_quote_ident: undef identifier'
Branch: 1 branch
BranchTrueFalseTotal
unless defined $name 1 1566 1567
37 unless defined $name;
38 1566 $name =~ s/"/""/g;
39 1566 return qq{"$name"};
40 }
41  
42 # The SQL literal of a column's default_value: a string goes between single
43 # quotes with each embedded ' doubled (the SQL standard), so "it's" cannot end
44 # the literal early or let the rest run as SQL (karr k47). A number is quoted
45 # too, as ever; the server converts it to the column's type. A scalar ref is
46 # literal SQL by contract and passes unchanged. Only for a default that comes
47 # from a result class: the Diff ops get theirs from the introspected model,
48 # i.e. rdb$default_source, which is already a literal.
49 #
50 # A BOOLEAN column is the exception to "the server converts" (karr k36):
51 # Firebird 5.0.4 accepts DEFAULT '0', '1', 0 and 1 on a BOOLEAN in CREATE
52 # TABLE and then fails every insert that uses the default ("conversion error
53 # from string"). So for a boolean column the four spellings 0, 1, 'true' and
54 # 'false' (any case; also as a scalar ref holding just that word) are written
55 # as TRUE / FALSE, and any other value -- 'f', '', 'yes', 2 -- dies with a
56 # DBIO::Exception instead of becoming a default that breaks every insert. A
57 # scalar ref holding anything else is SQL as written (an expression).
58 54 ▶sub _firebird_default_literal {
Subroutine _firebird_default_literal: called CC=10 SCAR=23.1
59 54 my ($value, $info, $where) = @_;
60 54 ▶ if ($info and is_boolean_type($info->{data_type})) {
Branch: 1 branch
BranchTrueFalseTotal
if ($info and is_boolean_type($info->{"data_type"})) 26 28 54
Condition100%3/3
$info and is_boolean_type($info->{"data_type"})
!ll&&!rl&&r
62226
MC/DC100%2/2
$info and is_boolean_type($info->{"data_type"})
$info is_boolean_type($info->{"data_type"})
Truth table100%3/3
A and B
A= $infoB= is_boolean_type($info->{"data_type"})
ABresult
0X0
100
111
61 26 ▶ my $plain = ref $value eq 'SCALAR' ? $$value : $value;
Branch: 1 branch
BranchTrueFalseTotal
ref $value eq 'SCALAR' ? : 4 22 26
62 26 ▶ if (!ref $plain) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $plain) 26 0 26
63 26 ▶ return 'FALSE' if $plain =~ /\A(?:0|false)\z/i;
Branch: 1 branch
BranchTrueFalseTotal
if $plain =~ /\A(?:0|false)\z/i 7 19 26
64 19 ▶ return 'TRUE' if $plain =~ /\A(?:1|true)\z/i;
Branch: 1 branch
BranchTrueFalseTotal
if $plain =~ /\A(?:1|true)\z/i 8 11 19
65 }
66 11 ▶ DBIO::Exception->throw(sprintf
Branch: 2 branches
BranchTrueFalseTotal
unless ref $value eq "SCALAR" 10 1 11
defined $where ? : 10 0 10
67 q{%sdefault_value '%s' on a boolean column is none of 0, 1, 'true', 'false'; }
68 . q{Firebird accepts any other default at CREATE TABLE and then fails every insert that takes it},
69 (defined $where ? "$where: " : ''), $plain)
70 unless ref $value eq 'SCALAR';
71 }
72 29 ▶ return $$value if ref $value eq 'SCALAR';
Branch: 1 branch
BranchTrueFalseTotal
if ref $value eq "SCALAR" 8 21 29
73 21 $value =~ s/'/''/g;
74 21 return "'$value'";
75 }
76  
77 # What follows the type in a column definition, in the one order Firebird's
78 # column clause has: IDENTITY, DEFAULT, NOT NULL (DEFAULT after NOT NULL is
79 # `Token unknown`). Shared by install_ddl and the CREATE / ADD of the Diff ops
80 # (karr k46), so a column is spelled the same way wherever it is written.
81 # $default is SQL text, already a literal.
82 391 ▶sub _firebird_column_clauses {
Subroutine _firebird_column_clauses: called CC=4 SCAR=13.9
83 391 my (%a) = @_;
84 return join '',
85 ($a{identity} ? ' GENERATED BY DEFAULT AS IDENTITY' : ()),
86 (defined $a{default} ? " DEFAULT $a{default}" : ()),
87 391 ▶ ($a{not_null} ? ' NOT NULL' : ());
Branch: 3 branches
BranchTrueFalseTotal
$a{'identity'} ? : 33 358 391
defined $a{'default'} ? : 31 360 391
$a{'not_null'} ? : 286 105 391
88 }
89  
90 # Whether the server takes IDENTITY on a column of this type (rendered SQL
91 # text, e.g. 'INTEGER' or 'DECIMAL(9,0)'): "exact number type with zero scale"
92 # -- SMALLINT, INTEGER, BIGINT, and NUMERIC / DECIMAL with scale 0 (measured on
93 # Firebird 5.0.4: NUMERIC(9,0), NUMERIC(4,0) and DECIMAL(18,0) are accepted,
94 # NUMERIC(9,2) and VARCHAR refused).
95 34 ▶sub _firebird_identity_type_ok {
Subroutine _firebird_identity_type_ok: called CC=2 SCAR=6.9
96 34 my ($type) = @_;
97 34 ▶ return $type =~ /\A(?:SMALLINT|INTEGER|BIGINT)\z/i
Condition100%3/3
$type =~ /\A(?:SMALLINT|INTEGER|BIGINT)\z/i || $type =~ /\A(?:NUMERIC|DECIMAL)\(\d+(?:,\s*0)?\)\z/i
l!l&&r!l&&!r
2833
MC/DC100%2/2
$type =~ /\A(?:SMALLINT|INTEGER|BIGINT)\z/i || $type =~ /\A(?:NUMERIC|DECIMAL)\(\d+(?:,\s*0)?\)\z/i
$type =~ /\A(?:SMALLINT|INTEGER|BIGINT)\z/i $type =~ /\A(?:NUMERIC|DECIMAL)\(\d+(?:,\s*0)?\)\z/i
Truth table100%3/3
A || B
A= $type =~ /\A(?:SMALLINT|INTEGER|BIGINT)\z/iB= $type =~ /\A(?:NUMERIC|DECIMAL)\(\d+(?:,\s*0)?\)\z/i
ABresult
1X1
011
000
98 || $type =~ /\A(?:NUMERIC|DECIMAL)\(\d+(?:,\s*0)?\)\z/i;
99 }
100  
101 # --- constraints (karr k34) ---------------------------------------------------
102 #
103 # Written as ALTER TABLE ... ADD CONSTRAINT after every CREATE TABLE, so the
104 # order of the tables and a cycle between them do not matter. Shared by
105 # install_ddl and Diff::Constraint (a table created by upgrade).
106 #
107 # Firebird's identifier limit is 63 characters from Firebird 4 on (measured on
108 # 5.0.4: 63 accepted, 64 "Name longer than database column size") and 31 bytes
109 # on Firebird 3, the driver's minimum, which is not measured here. Firebird 5
110 # counts CHARACTERS (measured: 63 two-byte characters accepted, 64 refused);
111 # Firebird 3's 31 are bytes. A name this module makes up (a foreign key's) is
112 # therefore kept to 31 BYTES, so every server takes it; a name the schema
113 # declares (a unique constraint's) is used as declared and refused above 63
114 # characters, where no server takes it -- refusing by bytes would refuse names
115 # Firebird 4 and later accept.
116 23 my $NAME_MAX = 63;
117 23 my $MADE_UP_MAX = 31;
118  
119 # "<table>_<columns>_<target>_fk": the target is in the name, so two keys over
120 # one column to different tables do not meet in one name (a constraint name is
121 # the database's, not the table's). When that is longer than 31 BYTES (the
122 # limit of Firebird 3 is in bytes) it becomes its first characters up to 22
123 # bytes, "_" and 8 hex digits of the md5 of the whole name: the same input gives
124 # the same name every time, and two long names that share a prefix still differ.
125 61 ▶sub _firebird_fk_name {
Subroutine _firebird_fk_name: called CC=2 SCAR=6.9
126 61 my ($table, $columns, $to_table) = @_;
127 61 my $name = join '_', $table, @$columns, $to_table, 'fk';
128 61 ▶ return $name if _bytes($name) <= $MADE_UP_MAX;
Branch: 1 branch
BranchTrueFalseTotal
if _bytes($name) <= $MADE_UP_MAX 56 5 61
129 5 return _cut_bytes($name, 22) . '_' . substr(Digest::MD5::md5_hex(_encoded($name)), 0, 8);
130 }
131  
132 179 ▶sub _encoded { my ($s) = @_; utf8::encode(my $b = $s); return $b }
Subroutine _encoded: called CC=1 SCAR=0.0
133 174 ▶sub _bytes { length _encoded($_[0]) }
Subroutine _bytes: called CC=1 SCAR=0.0
134  
135 # The longest prefix of $s that is at most $n bytes, cut between characters.
136 5 ▶sub _cut_bytes {
Subroutine _cut_bytes: called CC=3 SCAR=11.0
137 5 my ($s, $n) = @_;
138 5 my $out = '';
139 5 for my $ch (split //, $s) {
140 113 ▶ last if _bytes($out . $ch) > $n;
Branch: 1 branch
BranchTrueFalseTotal
if _bytes($out . $ch) > $n 5 108 113
141 108 $out .= $ch;
142 }
143 5 return $out;
144 }
145  
146 # Whether some has_many of $target (the referenced table's source) over the
147 # column pairs of this key has $attr set: the other side of the belongs_to,
148 # which is what DBIO::Relationship::Base describes as the autodetection of
149 # on_delete (cascade_delete) and on_update (cascade_copy). Firebird holds the
150 # key itself, and DBIO's own cascade deletes the parent FIRST, so a key without
151 # the action refuses to delete a parent that has children (karr k34).
152 97 ▶sub _firebird_cascading_has_many {
Subroutine _firebird_cascading_has_many: called CC=15 SCAR=28.1
153 97 my ($target, $table, $from, $to, $attr) = @_;
154 97 my $want = join "\0", sort map { "$from->[$_]\t$to->[$_]" } 0 .. $#$from;
155 97 for my $rel ($target->relationships) {
156 382 ▶ my $info = $target->relationship_info($rel) or next;
Branch: 1 branch
BranchTrueFalseTotal
my $info = $target->relationship_info($rel) or next 382 0 382
157 382 ▶ next unless $info->{attrs} && $info->{attrs}{$attr};
Branch: 1 branch
BranchTrueFalseTotal
unless $info->{"attrs"} and $info->{"attrs"}{$attr} 238 144 382
Condition66%2/3
$info->{"attrs"} and $info->{"attrs"}{$attr}
!ll&&!rl&&r
0238144
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
158 144 ▶ next unless ref $info->{cond} eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $info->{"cond"} eq "HASH" 10 134 144
159 134 ▶ my $other = eval { $target->related_source($rel) } or next;
Branch: 1 branch
BranchTrueFalseTotal
my $other = eval { do { $target->related_source($rel) } } or next 51 83 134
160 51 my $oname = $other->name;
161 51 ▶ $oname = $$oname if ref $oname eq 'SCALAR';
Branch: 1 branch
BranchTrueFalseTotal
if ref $oname eq "SCALAR" 26 25 51
162 51 ▶ next unless !ref $oname && $oname eq $table;
Branch: 1 branch
BranchTrueFalseTotal
unless not ref $oname and $oname eq $table 0 51 51
Condition33%1/3
not ref $oname and $oname eq $table
!ll&&!rl&&r
0051
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
163 51 my @pairs;
164 51 for my $k (keys %{ $info->{cond} }) {
165 51 ▶ my ($f) = $k =~ /\Aforeign\.(\w+)\z/ or next;
Branch: 1 branch
BranchTrueFalseTotal
my($f) = $k =~ /\Aforeign\.(\w+)\z/ or next 51 0 51
166 51 ▶ my ($sc) = ($info->{cond}{$k} // '') =~ /\Aself\.(\w+)\z/ or next;
Branch: 1 branch
BranchTrueFalseTotal
my($sc) = ($info->{"cond"}{$k} // "") =~ /\Aself\.(\w+)\z/ or next 51 0 51
Condition50%1/2
$info->{"cond"}{$k} // ""
l!l
510
MC/DC0%0/1
$info->{"cond"}{$k} // ""
$info->{"cond"}{$k}
Truth table50%1/2
A
A= $info->{"cond"}{$k}
Aresult
11
00
167 51 push @pairs, "$f\t$sc"; # the child's column, the parent's column
168 }
169 51 ▶ return 1 if (join "\0", sort @pairs) eq $want;
Branch: 1 branch
BranchTrueFalseTotal
if join("\0", sort(@pairs)) eq $want 51 0 51
170 }
171 46 return 0;
172 }
173  
174 # The ON UPDATE / ON DELETE text of one rule. Firebird takes NO ACTION,
175 # CASCADE, SET NULL and SET DEFAULT; RESTRICT is a syntax error there, though
176 # it is what the server reports for a key written without a clause, so it (and
177 # an absent value) is written as no clause at all. Anything else is refused.
178 127 ▶sub _firebird_fk_action {
Subroutine _firebird_fk_action: called CC=5 SCAR=16.1
179 127 my ($event, $rule, $where) = @_;
180 127 ▶ return '' unless defined $rule;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $rule 47 80 127
181 80 (my $r = uc $rule) =~ s/\A\s+|\s+\z//g;
182 80 $r =~ s/\s+/ /g;
183 80 ▶ return '' if $r eq '' || $r eq 'RESTRICT';
Branch: 1 branch
BranchTrueFalseTotal
if $r eq "" or $r eq "RESTRICT" 16 64 80
Condition100%3/3
$r eq "" or $r eq "RESTRICT"
l!l&&r!l&&!r
31364
MC/DC100%2/2
$r eq "" or $r eq "RESTRICT"
$r eq "" $r eq "RESTRICT"
Truth table100%3/3
A or B
A= $r eq ""B= $r eq "RESTRICT"
ABresult
1X1
011
000
184 64 ▶ DBIO::Exception->throw(
Branch: 1 branch
BranchTrueFalseTotal
unless $r =~ /\A(?:NO ACTION|CASCADE|SET NULL|SET DEFAULT)\z/ 1 63 64
185 "$where: $event '$rule' is none of NO ACTION, CASCADE, SET NULL, SET DEFAULT")
186 unless $r =~ /\A(?:NO ACTION|CASCADE|SET NULL|SET DEFAULT)\z/;
187 63 return " $event $r";
188 }
189  
190 108 ▶sub _firebird_check_name_length {
Subroutine _firebird_check_name_length: called CC=2 SCAR=6.9
191 108 my ($name, $where) = @_;
192 108 ▶ DBIO::Exception->throw(
Branch: 1 branch
BranchTrueFalseTotal
if length $name > $NAME_MAX 1 107 108
193 "$where: the constraint name '$name' is " . length($name)
194 . " characters, Firebird takes at most $NAME_MAX")
195 if length $name > $NAME_MAX;
196 107 return $name;
197 }
198  
199 # $table_sql and $to_table_sql are SQL as written (quoted, or a literal table
200 # name as given); the columns are bare names.
201 64 ▶sub _firebird_add_foreign_key {
Subroutine _firebird_add_foreign_key: called CC=1 SCAR=0.0
202 64 my (%a) = @_;
203 64 my $where = "foreign key '$a{name}' on $a{table_sql}";
204 64 _firebird_check_name_length($a{name}, $where);
205 return sprintf 'ALTER TABLE %s ADD CONSTRAINT %s FOREIGN KEY (%s) REFERENCES %s (%s)%s%s;',
206 $a{table_sql}, _firebird_quote_ident($a{name}),
207 64 join(', ', map { _firebird_quote_ident($_) } @{ $a{from} }),
208 $a{to_table_sql},
209 64 join(', ', map { _firebird_quote_ident($_) } @{ $a{to} }),
210 _firebird_fk_action('ON DELETE', $a{on_delete}, $where),
211 64 _firebird_fk_action('ON UPDATE', $a{on_update}, $where);
212 }
213  
214 44 ▶sub _firebird_add_unique {
Subroutine _firebird_add_unique: called CC=1 SCAR=0.0
215 44 my (%a) = @_;
216 44 _firebird_check_name_length($a{name}, "unique constraint '$a{name}' on $a{table_sql}");
217 return sprintf 'ALTER TABLE %s ADD CONSTRAINT %s UNIQUE (%s);',
218 $a{table_sql}, _firebird_quote_ident($a{name}),
219 43 join(', ', map { _firebird_quote_ident($_) } @{ $a{columns} });
220 }
221  
222 # The column lists of the foreign keys install_ddl writes for $source, in the
223 # order it writes them (the foreign columns sorted by name): the same reading
224 # of the belongs_to relationships as the loop in install_ddl below.
225 130 ▶sub _firebird_fk_column_lists {
Subroutine _firebird_fk_column_lists: called CC=8 SCAR=21.0
226 130 my ($schema, $source) = @_;
227 130 my @lists;
228 130 for my $rel (sort $source->relationships) {
229 252 my $info = $source->relationship_info($rel);
230 252 ▶ 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"} 170 82 252
Condition50%3/6
$info and $info->{"attrs"} and $info->{"attrs"}{"is_foreign_key_constraint"}
!ll&&!rl&&r
017082
$info and $info->{"attrs"}
!ll&&!rl&&r
00252
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
231 82 my $foreign = $info->{class};
232 82 ▶ next unless eval { $schema->source($foreign) } // eval { $schema->source($foreign =~ s/.*:://r) };
Branch: 1 branch
BranchTrueFalseTotal
unless eval { do { $schema->source($foreign) } } // eval { do { $schema->source($foreign =~ s/.*:://r) } } 19 63 82
Condition66%2/3
eval { do { $schema->source($foreign) } } // eval { do { $schema->source($foreign =~ s/.*:://r) } }
l!l&&r!l&&!r
63019
MC/DC50%1/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 table66%2/3
A // B
A= eval { do { $schema->source($foreign) } }B= eval { do { $schema->source($foreign =~ s/.*:://r) } }
ABresult
1X1
011
000
233 63 my $cond = $info->{cond};
234 63 ▶ next unless ref $cond eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $cond eq "HASH" 6 57 63
235 57 push @lists, [ map { (my $c = $cond->{$_}) =~ s/^self\.//; $c } sort keys %$cond ];
236 }
237 130 return @lists;
238 }
239  
240 # What the shared conformance table (karr k55) asks to be refused rather than
241 # built as something else: no Firebird type means what the declaration says.
242 23 my $TZ_REASON = 'the server has the type (Firebird 4 and later) but DBD::Firebird 1.39 '
243 . 'does not know it: a value cannot be bound to the column and one read '
244 . 'comes back as the text "** unknown **" (t/88-timezone-types-dbd-live.t)';
245  
246 # More than 18 digits of a NUMERIC / DECIMAL is an INT128 (karr k60).
247 23 my $INT128_REASON = 'more than 18 digits is an INT128; the server has it from Firebird 4 on '
248 . 'but DBD::Firebird 1.39 cannot bind or read an INT128 (a value is not bound, '
249 . 'one read comes back wrong; t/97-numeric-int128-dbd-live.t)';
250  
251 # These types exist from Firebird 4 on; the driver supports 3. On a server
252 # below 4 install_ddl refuses them, naming the table and the column, instead
253 # of leaving the server's "Token unknown" for the middle of a deploy. (The
254 # time zone types are refused on every server, see $TZ_REASON.)
255 23 my %NEEDS_FIREBIRD_4 = map { $_ => 1 } qw(binary varbinary);
256  
257 23 my %REFUSED_TYPE = (
258 timestamptz => $TZ_REASON,
259 'timestamp with time zone' => $TZ_REASON,
260 'time with time zone' => $TZ_REASON,
261 bit => 'Firebird has no type for a bit string of a fixed width: BOOLEAN is one '
262 . 'bit and BINARY(n) counts bytes, so any mapping changes what the size means',
263 enum => 'Firebird has no enumerated type, and a VARCHAR with a CHECK would '
264 . 'be a different column (its values are not a list the server knows)',
265 );
266  
267  
268 2 ▶sub server_major_version {
Subroutine server_major_version: called CC=3 SCAR=13.5
Pod documented
269 2 my ($class, $schema) = @_;
270 2 my $version = eval {
271 0 ▶ my ($v) = $schema->storage->dbh_do(sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
272 0 $_[1]->selectrow_array(
273 q{SELECT rdb$get_context('SYSTEM', 'ENGINE_VERSION') FROM rdb$database});
274 2 });
275 0 $v;
276 };
277 2 ▶ return defined $version && $version =~ /\A\s*(\d+)/ ? $1 : undef;
Branch: 1 branch
BranchTrueFalseTotal
defined $version && $version =~ /\A\s*(\d+)/ ? : 0 2 2
Condition33%1/3
defined $version and $version =~ /\A\s*(\d+)/
!ll&&!rl&&r
200
MC/DC0%0/2
defined $version and $version =~ /\A\s*(\d+)/
defined $version $version =~ /\A\s*(\d+)/
Truth table33%1/3
A and B
A= defined $versionB= $version =~ /\A\s*(\d+)/
ABresult
0X0
100
111
278 }
279  
280  
281 136 ▶sub install_ddl {
Subroutine install_ddl: called CC=82 SCAR=44.5
Pod documented
282 136 my ($class, $schema) = @_;
283 136 my @stmts;
284 my @view_stmts;
285 136 my (@unique_stmts, @fk_stmts, %seen_fk);
286 136 my %seen_table;
287 136 my %seen_view;
288  
289 136 for my $source_name (sort $schema->sources) {
290 179 my $source = $schema->source($source_name);
291 179 my $table_name = $source->name;
292  
293 # Views: emit CREATE VIEW after all tables; skip virtual views.
294 179 ▶ if ($source->isa('DBIO::ResultSource::View')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($source->isa("DBIO::ResultSource::View")) 6 173 179
295 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
296 6 my $vliteral = ref $table_name eq 'SCALAR';
297 6 ▶ my $vname = $vliteral ? $$table_name : $table_name;
Branch: 1 branch
BranchTrueFalseTotal
$vliteral ? : 1 5 6
298 # no schemas before Firebird 6: a dot would become part of the name
299 6 ▶ $source->throw_exception(
Branch: 1 branch
BranchTrueFalseTotal
if not $vliteral || ref $vname and $vname =~ /\./ 1 5 6
Condition83%5/6
not $vliteral || ref $vname and $vname =~ /\./
!ll&&!rl&&r
141
$vliteral || ref $vname
l!l&&r!l&&!r
105
MC/DC66%2/3
not $vliteral || ref $vname and $vname =~ /\./
$vliteral ref $vname $vname =~ /\./
Truth table75%3/4
not(A || B) and C
A= $vliteralB= ref $vnameC= $vname =~ /\./
ABCresult
1XX0
01X0
0000
0011
300 "view '$vname': refused, Firebird has no schemas before version 6 "
301 . "and a dot would become part of a literal view name")
302 if !$vliteral && !ref $vname && $vname =~ /\./;
303 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
304 5 ▶ my $def = $source->can('view_definition') ? $source->view_definition : undef;
Branch: 1 branch
BranchTrueFalseTotal
$source->can('view_definition') ? : 5 0 5
305 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
306 5 ▶ push @view_stmts, sprintf 'CREATE VIEW %s AS %s;',
Branch: 1 branch
BranchTrueFalseTotal
$vliteral ? : 1 4 5
307 ($vliteral ? $vname : _firebird_quote_ident($vname)), $def;
308 5 next;
309 }
310  
311 # A scalar-ref name is either a literal table name (\'cd', emitted
312 # unquoted) or an inline subquery source; deref the former, skip the latter.
313 # The literal stays unquoted because the SQLMaker writes it into FROM as
314 # given: quoting it here would create "cd" while the ORM selects FROM cd.
315 173 my $literal = ref $table_name eq 'SCALAR';
316 173 ▶ if ($literal) {
Branch: 1 branch
BranchTrueFalseTotal
if ($literal) 6 167 173
317 6 $table_name = $$table_name;
318 6 ▶ next if $table_name =~ /\s|\(/;
Branch: 1 branch
BranchTrueFalseTotal
if $table_name =~ /\s|\(/ 0 6 6
319 }
320 173 ▶ next if ref $table_name;
Branch: 1 branch
BranchTrueFalseTotal
if ref $table_name 0 173 173
321  
322 # Firebird has no schemas before version 6 (karr k55): a dot in a quoted
323 # name is a character of the name, not a schema, so `other.t` would become
324 # a table of that literal name. Refused, not reinterpreted. A literal name
325 # (\'...') is SQL as written and is the caller's.
326 173 ▶ $source->throw_exception(
Branch: 1 branch
BranchTrueFalseTotal
if not $literal and $table_name =~ /\./ 1 172 173
Condition100%3/3
not $literal and $table_name =~ /\./
!ll&&!rl&&r
61661
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
327 "table '$table_name': refused, Firebird has no schemas before version 6 "
328 . "and a dot would become part of a literal table name")
329 if !$literal && $table_name =~ /\./;
330  
331 # Several result sources can map to one physical table; emit it once.
332 172 ▶ next if $seen_table{$table_name}++;
Branch: 1 branch
BranchTrueFalseTotal
if $seen_table{$table_name}++ 0 172 172
333  
334 172 my @col_defs;
335 my %is_pk;
336 172 my @pk_cols = $source->primary_columns;
337 172 @is_pk{@pk_cols} = (1) x @pk_cols;
338  
339 172 for my $col_name ($source->columns) {
340 382 my $info = $source->column_info($col_name);
341 382 ▶ if (my $why = $REFUSED_TYPE{ lc($info->{data_type} // '') }) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $why = $REFUSED_TYPE{lc($info->{"data_type"} // "")}) 6 376 382
Condition100%2/2
$info->{"data_type"} // ""
l!l
3775
MC/DC100%1/1
$info->{"data_type"} // ""
$info->{"data_type"}
Truth table100%2/2
A
A= $info->{"data_type"}
Aresult
11
00
342 6 $source->throw_exception(
343 "column '$col_name' on '$table_name': data_type '$info->{data_type}' "
344 . "is refused: $why");
345 }
346 376 ▶ if ($NEEDS_FIREBIRD_4{ lc($info->{data_type} // '') }) {
Branch: 1 branch
BranchTrueFalseTotal
if ($NEEDS_FIREBIRD_4{lc($info->{"data_type"} // "")}) 10 366 376
Condition100%2/2
$info->{"data_type"} // ""
l!l
3715
MC/DC100%1/1
$info->{"data_type"} // ""
$info->{"data_type"}
Truth table100%2/2
A
A= $info->{"data_type"}
Aresult
11
00
347 10 my $major = $class->server_major_version($schema);
348 10 ▶ $source->throw_exception(
Branch: 1 branch
BranchTrueFalseTotal
if defined $major and $major < 4 2 8 10
Condition100%3/3
defined $major and $major < 4
!ll&&!rl&&r
442
MC/DC100%2/2
defined $major and $major < 4
defined $major $major < 4
Truth table100%3/3
A and B
A= defined $majorB= $major < 4
ABresult
0X0
100
111
349 "column '$col_name' on '$table_name': data_type '$info->{data_type}' "
350 . "is refused: the type exists from Firebird 4 on and the server is "
351 . "Firebird $major")
352 if defined $major && $major < 4;
353 }
354 # NUMERIC / DECIMAL of more than 18 digits is stored as an INT128 (the
355 # server has it from Firebird 4 on); DBD::Firebird 1.39 can neither bind
356 # nor read one, so it is refused on every server (karr k60, t/97)
357 374 ▶ if (lc($info->{data_type} // '') =~ /\A(?:numeric|decimal)\z/) {
Branch: 1 branch
BranchTrueFalseTotal
if (lc($info->{"data_type"} // "") =~ /\A(?:numeric|decimal)\z/) 38 336 374
Condition100%2/2
$info->{"data_type"} // ""
l!l
3695
MC/DC100%1/1
$info->{"data_type"} // ""
$info->{"data_type"}
Truth table100%2/2
A
A= $info->{"data_type"}
Aresult
11
00
358 38 ▶ my $precision = ref $info->{size} eq 'ARRAY' ? $info->{size}[0] : $info->{size};
Branch: 1 branch
BranchTrueFalseTotal
ref $info->{'size'} eq 'ARRAY' ? : 28 10 38
359 38 ▶ $source->throw_exception(
Branch: 1 branch
BranchTrueFalseTotal
if defined $precision and $precision =~ /\A\d+\z/ and $precision > 18 24 14 38
Condition83%5/6
defined $precision and $precision =~ /\A\d+\z/ and $precision > 18
!ll&&!rl&&r
11324
defined $precision and $precision =~ /\A\d+\z/
!ll&&!rl&&r
1037
MC/DC66%2/3
defined $precision and $precision =~ /\A\d+\z/ and $precision > 18
defined $precision $precision =~ /\A\d+\z/ $precision > 18
Truth table75%3/4
A and B and C
A= defined $precisionB= $precision =~ /\A\d+\z/C= $precision > 18
ABCresult
0XX0
10X0
1100
1111
360 "column '$col_name' on '$table_name': data_type '$info->{data_type}' "
361 . "with $precision digits is refused: $INT128_REASON")
362 if defined $precision and $precision =~ /\A\d+\z/ and $precision > 18;
363 }
364 350 my $type = ddl_type_from_info($info);
365 350 my $def = sprintf ' %s %s', _firebird_quote_ident($col_name), $type;
366 # IDENTITY (karr k46): Firebird 3, the driver's minimum, generates the
367 # key itself; the clause sits between the type and NOT NULL. Identity
368 # and DEFAULT exclude each other, and only an integer type can carry it:
369 # refused, never simplified.
370 350 ▶ if ($info->{is_auto_increment}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($info->{"is_auto_increment"}) 34 316 350
371 34 ▶ $source->throw_exception(
Branch: 1 branch
BranchTrueFalseTotal
unless _firebird_identity_type_ok($type) 3 31 34
372 "column '$col_name' on '$table_name': is_auto_increment cannot be "
373 . "built as IDENTITY on a $type column (only SMALLINT, INTEGER, BIGINT, "
374 . "NUMERIC / DECIMAL with scale 0)")
375 unless _firebird_identity_type_ok($type);
376 $source->throw_exception(
377 "column '$col_name' on '$table_name': is_auto_increment (IDENTITY) "
378 . "and default_value exclude each other")
379 31 ▶ if defined $info->{default_value};
Branch: 1 branch
BranchTrueFalseTotal
if defined $info->{"default_value"} 1 30 31
380 }
381 # ADR 0043: a missing is_nullable means NOT NULL; only 1 is nullable.
382 $def .= _firebird_column_clauses(
383 identity => $info->{is_auto_increment},
384 default => (defined $info->{default_value}
385 ? _firebird_default_literal($info->{default_value}, $info, "column '$col_name' on '$table_name'") : undef),
386 not_null => !$info->{is_nullable},
387 346 ▶ );
Branch: 1 branch
BranchTrueFalseTotal
defined $info->{'default_value'} ? : 26 320 346
388 345 push @col_defs, $def;
389 }
390  
391 135 ▶ if (@pk_cols) {
Branch: 1 branch
BranchTrueFalseTotal
if (@pk_cols) 135 0 135
392 push @col_defs, sprintf ' PRIMARY KEY (%s)',
393 135 join(', ', map { _firebird_quote_ident($_) } @pk_cols);
394 }
395  
396 135 ▶ my $qualified = $literal ? $table_name : _firebird_quote_ident($table_name);
Branch: 1 branch
BranchTrueFalseTotal
$literal ? : 6 129 135
397 135 push @stmts, sprintf "CREATE TABLE %s (\n%s\n);", $qualified, join(",\n", @col_defs);
398  
399 # The canonical index declarations of the source (Cake idx, indices;
400 # ADR 0061 in core, karr k51). The reader refuses an entry that carries
401 # only another driver's block and an unknown key in the firebird block;
402 # no key is allowed, so a firebird block must be empty (the plain index).
403 # What Firebird can express but the introspector does not read back
404 # (COMPUTED BY, partial, DESC) is not offered, so no index is built that
405 # the diff would then drop again. type (other than unique) and options
406 # have no meaning here and are refused, never a plainer index.
407 135 my %declared;
408 135 for my $decl ($source->index_declarations_for('firebird', [])) {
409 17 ▶ if (defined $decl->{type} && lc $decl->{type} ne 'unique') {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $decl->{"type"} and lc $decl->{"type"} ne "unique") 1 16 17
Condition100%3/3
defined $decl->{"type"} and lc $decl->{"type"} ne "unique"
!ll&&!rl&&r
1151
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
410 1 $source->throw_exception(
411 "index '$decl->{name}' on '$table_name': type '$decl->{type}' "
412 . "cannot be built on Firebird (only a plain or unique index)");
413 }
414 16 ▶ if (defined $decl->{options}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $decl->{"options"}) 1 15 16
415 1 $source->throw_exception(
416 "index '$decl->{name}' on '$table_name': options cannot be built "
417 . "on Firebird");
418 }
419 15 $declared{ $decl->{name} } = $decl;
420 }
421  
422 # Unique indexes. A column that a unique constraint over exactly this one
423 # column covers gets no second index: the constraint has its own (karr k55).
424 130 my %constraint_alone;
425 {
426 130 my %u = $source->unique_constraints;
427 130 for my $uname (keys %u) {
428 178 ▶ next if $uname eq 'primary';
Branch: 1 branch
BranchTrueFalseTotal
if $uname eq "primary" 130 48 178
429 48 ▶ $constraint_alone{ $u{$uname}[0] } = 1 if @{ $u{$uname} } == 1;
Branch: 1 branch
BranchTrueFalseTotal
if @{$u{$uname};} == 1 25 23 48
430 }
431 }
432 130 for my $col_name ($source->columns) {
433 295 my $info = $source->column_info($col_name);
434 295 ▶ next if $declared{"${table_name}_${col_name}_idx"};
Branch: 1 branch
BranchTrueFalseTotal
if $declared{"${table_name}_${col_name}_idx"} 1 294 295
435 294 ▶ next if $constraint_alone{$col_name};
Branch: 1 branch
BranchTrueFalseTotal
if $constraint_alone{$col_name} 25 269 294
436 269 ▶ if ($info->{is_unique} || $info->{is_single_unique_key}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($info->{"is_unique"} or $info->{"is_single_unique_key"}) 2 267 269
Condition66%2/3
$info->{"is_unique"} or $info->{"is_single_unique_key"}
l!l&&r!l&&!r
20267
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
437 2 push @stmts, sprintf 'CREATE UNIQUE INDEX %s ON %s (%s);',
438 _firebird_quote_ident("${table_name}_${col_name}_idx"),
439 $qualified,
440 _firebird_quote_ident($col_name);
441 }
442 }
443  
444 # An index the server has already (karr k55): the key of a primary key,
445 # of a unique constraint written below, and (for a non-unique index) the
446 # one Firebird builds for each foreign key. Same columns in the same
447 # order. A UNIQUE index over a foreign key's columns says more than the
448 # key and is built.
449 130 my (%has_index, %has_fk_index);
450 130 ▶ $has_index{ join "\0", @pk_cols } = 1 if @pk_cols;
Branch: 1 branch
BranchTrueFalseTotal
if @pk_cols 130 0 130
451 {
452 130 my %u = $source->unique_constraints;
453 130 for my $uname (keys %u) {
454 178 ▶ next if $uname eq 'primary';
Branch: 1 branch
BranchTrueFalseTotal
if $uname eq "primary" 130 48 178
455 48 ▶ next if join("\0", sort @{ $u{$uname} }) eq join("\0", sort @pk_cols);
Branch: 1 branch
BranchTrueFalseTotal
if join("\0", sort(@{$u{$uname};})) eq join("\0", sort(@pk_cols)) 10 38 48
456 38 $has_index{ join "\0", @{ $u{$uname} } } = 1;
457 }
458 }
459 130 $has_fk_index{ join "\0", @$_ } = 1 for _firebird_fk_column_lists($schema, $source);
460  
461 130 for my $name (sort keys %declared) {
462 15 my $decl = $declared{$name};
463 15 my $key = join "\0", @{ $decl->{columns} };
464 15 ▶ next if $has_index{$key} || (!$decl->{unique} && $has_fk_index{$key});
Branch: 1 branch
BranchTrueFalseTotal
if $has_index{$key} or not $decl->{"unique"} and $has_fk_index{$key} 5 10 15
Condition100%6/6
$has_index{$key} or not $decl->{"unique"} and $has_fk_index{$key}
l!l&&r!l&&!r
4110
not $decl->{"unique"} and $has_fk_index{$key}
!ll&&!rl&&r
371
MC/DC100%3/3
$has_index{$key} or not $decl->{"unique"} and $has_fk_index{$key}
$has_index{$key} not $decl->{"unique"} $has_fk_index{$key}
Truth table100%4/4
A or not(B) and C
A= $has_index{$key}B= not $decl->{"unique"}C= $has_fk_index{$key}
ABCresult
1XX1
0111
00X0
0100
465 push @stmts, sprintf 'CREATE %sINDEX %s ON %s (%s);',
466 ($decl->{unique} ? 'UNIQUE ' : ''),
467 _firebird_quote_ident($name),
468 $qualified,
469 10 ▶ join(', ', map { _firebird_quote_ident($_) } @{ $decl->{columns} });
Branch: 1 branch
BranchTrueFalseTotal
$decl->{'unique'} ? : 3 7 10
470 }
471  
472 # Unique constraints (add_unique_constraint), under the name declared.
473 130 my %uniques = $source->unique_constraints;
474 130 for my $uname (sort keys %uniques) {
475 178 ▶ next if $uname eq 'primary';
Branch: 1 branch
BranchTrueFalseTotal
if $uname eq "primary" 130 48 178
476 # A unique constraint over exactly the columns of the primary key says
477 # nothing the key does not, and the server refuses it ("Same set of
478 # columns cannot be used in more than one PRIMARY KEY and/or UNIQUE
479 # constraint definition", measured on 5.0.4): it is not written.
480 48 ▶ next if join("\0", sort @{ $uniques{$uname} }) eq join("\0", sort @pk_cols);
Branch: 1 branch
BranchTrueFalseTotal
if join("\0", sort(@{$uniques{$uname};})) eq join("\0", sort(@pk_cols)) 10 38 48
481 push @unique_stmts, _firebird_add_unique(
482 38 table_sql => $qualified, name => $uname, columns => $uniques{$uname},
483 );
484 }
485  
486 # Foreign keys from the belongs_to relationships (is_foreign_key_constraint),
487 # in the order of the relationship names. on_delete / on_update are the
488 # relationship's own attributes where it declares them (exists, so
489 # `on_delete => undef` or '' declares "no action" and switches the
490 # derivation off); where it does not, they come from the other side as
491 # DBIO::Relationship::Base describes: a has_many over the same columns with
492 # cascade_delete is ON DELETE CASCADE, one with cascade_copy ON UPDATE
493 # CASCADE. DBIO's own cascade deletes the parent first, so without the
494 # action the server refuses the delete of a parent that has children
495 # (karr k34).
496 129 for my $rel (sort $source->relationships) {
497 252 my $info = $source->relationship_info($rel);
498 252 ▶ 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"} 170 82 252
Condition50%3/6
$info and $info->{"attrs"} and $info->{"attrs"}{"is_foreign_key_constraint"}
!ll&&!rl&&r
017082
$info and $info->{"attrs"}
!ll&&!rl&&r
00252
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
499  
500 82 my $foreign = $info->{class};
501 82 my $foreign_source = eval { $schema->source($foreign) }
502 82 ▶ // eval { $schema->source($foreign =~ s/.*:://r) };
Condition66%2/3
eval { do { $schema->source($foreign) } } // eval { do { $schema->source($foreign =~ s/.*:://r) } }
l!l&&r!l&&!r
63019
MC/DC50%1/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 table66%2/3
A // B
A= eval { do { $schema->source($foreign) } }B= eval { do { $schema->source($foreign =~ s/.*:://r) } }
ABresult
1X1
011
000
503 82 ▶ next unless $foreign_source;
Branch: 1 branch
BranchTrueFalseTotal
unless $foreign_source 19 63 82
504 63 my $cond = $info->{cond};
505 63 ▶ next unless ref $cond eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $cond eq "HASH" 6 57 63
506  
507 # Firebird has no DEFERRABLE; the attribute is not left out silently.
508 $source->throw_exception(
509 "relationship '$rel' on '$table_name': is_deferrable cannot be built, "
510 . "Firebird has no deferrable constraints")
511 57 ▶ if $info->{attrs}{is_deferrable};
Branch: 1 branch
BranchTrueFalseTotal
if $info->{"attrs"}{"is_deferrable"} 1 56 57
512  
513 56 my (@from, @to);
514 56 for my $foreign_col (sort keys %$cond) {
515 56 (my $fcol = $foreign_col) =~ s/^foreign\.//;
516 56 (my $scol = $cond->{$foreign_col}) =~ s/^self\.//;
517 56 push @to, $fcol;
518 56 push @from, $scol;
519 }
520  
521 56 my $to_name = $foreign_source->name;
522 56 ▶ my $to_bare = ref $to_name eq 'SCALAR' ? $$to_name : $to_name;
Branch: 1 branch
BranchTrueFalseTotal
ref $to_name eq 'SCALAR' ? : 6 50 56
523 56 ▶ my $to_sql = ref $to_name eq 'SCALAR' ? $$to_name : _firebird_quote_ident($to_name);
Branch: 1 branch
BranchTrueFalseTotal
ref $to_name eq 'SCALAR' ? : 6 50 56
524 56 my $attrs = $info->{attrs};
525 my %fk = (
526 table_sql => $qualified, name => _firebird_fk_name($table_name, \@from, $to_bare),
527 from => \@from, to_table_sql => $to_sql, to => \@to,
528 on_delete => (exists $attrs->{on_delete} ? $attrs->{on_delete}
529 : _firebird_cascading_has_many($foreign_source, $table_name, \@from, \@to, 'cascade_delete')
530 ? 'CASCADE' : undef),
531 on_update => (exists $attrs->{on_update} ? $attrs->{on_update}
532 56 ▶ : _firebird_cascading_has_many($foreign_source, $table_name, \@from, \@to, 'cascade_copy')
Branch: 4 branches
BranchTrueFalseTotal
exists $attrs->{'on_delete'} ? : 9 47 56
_firebird_cascading_has_many($foreign_source, $table_name, \@from, \@to, 'cascade_delete') ? : 29 18 47
exists $attrs->{'on_update'} ? : 6 50 56
_firebird_cascading_has_many($foreign_source, $table_name, \@from, \@to, 'cascade_copy') ? : 22 28 50
533 ? 'CASCADE' : undef),
534 );
535 56 my $stmt = _firebird_add_foreign_key(%fk);
536  
537 # Relationships that declare the same key are one key. The same columns
538 # with other actions would be a second constraint under the same name:
539 # refused, never one of them quietly.
540 55 my $by_cols = join "\0", $table_name, @from, '->', $to_sql, @to;
541 55 ▶ if (defined(my $prev = $seen_fk{$by_cols})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $prev = $seen_fk{$by_cols})) 13 42 55
542 13 ▶ next if $prev eq $stmt;
Branch: 1 branch
BranchTrueFalseTotal
if $prev eq $stmt 12 1 13
543 1 $source->throw_exception(
544 "relationship '$rel' on '$table_name' declares a foreign key over "
545 . join(', ', @from) . " that another relationship declares with other "
546 . "on_delete / on_update actions; Firebird cannot hold both");
547 }
548 42 $seen_fk{$by_cols} = $stmt;
549 42 push @fk_stmts, $stmt;
550 }
551 }
552  
553 88 return join "
554  
555 ", @stmts, @unique_stmts, @fk_stmts, @view_stmts;
556 }
557  
558 23 1;
559  
560 __END__
« Firebird/Introspect/Indexes.pm ↑ Summary Firebird/Diff/Column.pm »