| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
Condition100%3/3
$info and is_boolean_type($info->{"data_type"})
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"})
| |||||||||||||||||||||||||||||||||||||||||||
| 61 | 26 | ▶ | my $plain = ref $value eq 'SCALAR' ? $$value : $value; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 62 | 26 | ▶ | if (!ref $plain) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 63 | 26 | ▶ | return 'FALSE' if $plain =~ /\A(?:0|false)\z/i; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 64 | 19 | ▶ | return 'TRUE' if $plain =~ /\A(?:1|true)\z/i; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 65 | } | ||||||||||||||||||||||||||||||||||||||||||
| 66 | 11 | ▶ | DBIO::Exception->throw(sprintf | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 157 | 382 | ▶ | next unless $info->{attrs} && $info->{attrs}{$attr}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$info->{"attrs"} and $info->{"attrs"}{$attr}
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}
| |||||||||||||||||||||||||||||||||||||||||||
| 158 | 144 | ▶ | next unless ref $info->{cond} eq 'HASH'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 159 | 134 | ▶ | my $other = eval { $target->related_source($rel) } or next; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 160 | 51 | my $oname = $other->name; | |||||||||||||||||||||||||||||||||||||||||
| 161 | 51 | ▶ | $oname = $$oname if ref $oname eq 'SCALAR'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 162 | 51 | ▶ | next unless !ref $oname && $oname eq $table; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
not ref $oname and $oname eq $table
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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 166 | 51 | ▶ | my ($sc) = ($info->{cond}{$k} // '') =~ /\Aself\.(\w+)\z/ or next; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%1/2
$info->{"cond"}{$k} // ""
MC/DC0%0/1
$info->{"cond"}{$k} // ""
$info->{"cond"}{$k}
Truth table50%1/2
A
A= $info->{"cond"}{$k}
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
Condition100%3/3
$r eq "" or $r eq "RESTRICT"
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"
| |||||||||||||||||||||||||||||||||||||||||||
| 184 | 64 | ▶ | DBIO::Exception->throw( | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
Condition50%3/6
$info and $info->{"attrs"} and $info->{"attrs"}{"is_foreign_key_constraint"}
$info and $info->{"attrs"}
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"}
| |||||||||||||||||||||||||||||||||||||||||||
| 231 | 82 | my $foreign = $info->{class}; | |||||||||||||||||||||||||||||||||||||||||
| 232 | 82 | ▶ | next unless eval { $schema->source($foreign) } // eval { $schema->source($foreign =~ s/.*:://r) }; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
eval {
do {
$schema->source($foreign)
}
} // eval {
do {
$schema->source($foreign =~ s/.*:://r)
}
}
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)
}
}
| |||||||||||||||||||||||||||||||||||||||||||
| 233 | 63 | my $cond = $info->{cond}; | |||||||||||||||||||||||||||||||||||||||||
| 234 | 63 | ▶ | next unless ref $cond eq 'HASH'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
Condition33%1/3
defined $version and $version =~ /\A\s*(\d+)/
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+)/
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 295 | 6 | ▶ | next if $source->can('is_virtual') && $source->is_virtual; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$source->can("is_virtual") and $source->is_virtual
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
| |||||||||||||||||||||||||||||||||||||||||||
| 296 | 6 | my $vliteral = ref $table_name eq 'SCALAR'; | |||||||||||||||||||||||||||||||||||||||||
| 297 | 6 | ▶ | my $vname = $vliteral ? $$table_name : $table_name; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 298 | # no schemas before Firebird 6: a dot would become part of the name | ||||||||||||||||||||||||||||||||||||||||||
| 299 | 6 | ▶ | $source->throw_exception( | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition83%5/6
not $vliteral || ref $vname and $vname =~ /\./
$vliteral || ref $vname
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 =~ /\./
| |||||||||||||||||||||||||||||||||||||||||||
| 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
Condition33%1/3
ref $vname or $seen_view{$vname}++
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}++
| |||||||||||||||||||||||||||||||||||||||||||
| 304 | 5 | ▶ | my $def = $source->can('view_definition') ? $source->view_definition : undef; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 305 | 5 | ▶ | next unless defined $def && length $def; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $def and length $def
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
| |||||||||||||||||||||||||||||||||||||||||||
| 306 | 5 | ▶ | push @view_stmts, sprintf 'CREATE VIEW %s AS %s;', | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 317 | 6 | $table_name = $$table_name; | |||||||||||||||||||||||||||||||||||||||||
| 318 | 6 | ▶ | next if $table_name =~ /\s|\(/; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 319 | } | ||||||||||||||||||||||||||||||||||||||||||
| 320 | 173 | ▶ | next if ref $table_name; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
Condition100%3/3
not $literal and $table_name =~ /\./
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 =~ /\./
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
Condition100%2/2
$info->{"data_type"} // ""
MC/DC100%1/1
$info->{"data_type"} // ""
$info->{"data_type"}
Truth table100%2/2
A
A= $info->{"data_type"}
| |||||||||||||||||||||||||||||||||||||||||||
| 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
Condition100%2/2
$info->{"data_type"} // ""
MC/DC100%1/1
$info->{"data_type"} // ""
$info->{"data_type"}
Truth table100%2/2
A
A= $info->{"data_type"}
| |||||||||||||||||||||||||||||||||||||||||||
| 347 | 10 | my $major = $class->server_major_version($schema); | |||||||||||||||||||||||||||||||||||||||||
| 348 | 10 | ▶ | $source->throw_exception( | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $major and $major < 4
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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
Condition100%2/2
$info->{"data_type"} // ""
MC/DC100%1/1
$info->{"data_type"} // ""
$info->{"data_type"}
Truth table100%2/2
A
A= $info->{"data_type"}
| |||||||||||||||||||||||||||||||||||||||||||
| 358 | 38 | ▶ | my $precision = ref $info->{size} eq 'ARRAY' ? $info->{size}[0] : $info->{size}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 359 | 38 | ▶ | $source->throw_exception( | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition83%5/6
defined $precision and $precision =~ /\A\d+\z/ and $precision > 18
defined $precision and $precision =~ /\A\d+\z/
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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 371 | 34 | ▶ | $source->throw_exception( | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 388 | 345 | push @col_defs, $def; | |||||||||||||||||||||||||||||||||||||||||
| 389 | } | ||||||||||||||||||||||||||||||||||||||||||
| 390 | |||||||||||||||||||||||||||||||||||||||||||
| 391 | 135 | ▶ | if (@pk_cols) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
Condition100%3/3
defined $decl->{"type"} and lc $decl->{"type"} ne "unique"
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"
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 429 | 48 | ▶ | $constraint_alone{ $u{$uname}[0] } = 1 if @{ $u{$uname} } == 1; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 435 | 294 | ▶ | next if $constraint_alone{$col_name}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 436 | 269 | ▶ | if ($info->{is_unique} || $info->{is_single_unique_key}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$info->{"is_unique"} or $info->{"is_single_unique_key"}
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"}
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 455 | 48 | ▶ | next if join("\0", sort @{ $u{$uname} }) eq join("\0", sort @pk_cols); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
Condition100%6/6
$has_index{$key} or not $decl->{"unique"} and $has_fk_index{$key}
not $decl->{"unique"} and $has_fk_index{$key}
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}
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
Condition50%3/6
$info and $info->{"attrs"} and $info->{"attrs"}{"is_foreign_key_constraint"}
$info and $info->{"attrs"}
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"}
| |||||||||||||||||||||||||||||||||||||||||||
| 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)
}
}
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)
}
}
| |||||||||||||||||||||||||||||||||||||||||||
| 503 | 82 | ▶ | next unless $foreign_source; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 504 | 63 | my $cond = $info->{cond}; | |||||||||||||||||||||||||||||||||||||||||
| 505 | 63 | ▶ | next unless ref $cond eq 'HASH'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 523 | 56 | ▶ | my $to_sql = ref $to_name eq 'SCALAR' ? $$to_name : _firebird_quote_ident($to_name); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 542 | 13 | ▶ | next if $prev eq $stmt; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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__ | ||||||||||||||||||||||||||||||||||||||||||