Firebird/Storage/Common.pm

filter: Statementstmt 53.8% 35 / 65 Branchbran 12.5% 3 / 24 Conditioncond 50.0% 3 / 6 MC/DCmcdc 25.0% 1 / 4 Subroutinesub 62.5% 10 / 16 Podpod 100% 1 / 1 totaltotal 45.6% 53 / 116 CC 15 SCAR 33.8
CC
15
Cov
43%
_dbh_get_autoinc_seq
90.0
_sequence_fetch
12.0
_mask_error_values
3.3
CRAP 29.4
« Firebird/Introspect/Tables.pm ↑ Summary Firebird/Diff/Constraint.pm »
1 package DBIO::Firebird::Storage::Common;
2 # ABSTRACT: Shared Firebird / InterBase storage logic
3  
4 21 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 21 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 21 our $VERSION = '0.900007';
8  
9 21 ▶use base qw/DBIO::Storage::DBI/;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 21 ▶use mro 'c3';
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 21 ▶use DBIO::Firebird::Util qw(mask_error_details);
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14  
15 21 __PACKAGE__->_use_insert_returning (1);
16 21 __PACKAGE__->sql_quote_char ('"');
17 21 __PACKAGE__->sql_maker_class ('DBIO::Firebird::SQLMaker');
18  
19 1 ▶sub sqlt_type {
Subroutine sqlt_type: called CC=1 SCAR=0.0
Pod documented
20 1 return 'Firebird';
21 }
22  
23  
24 # ADR 0037 -- the complete 1/0 statement behind DBIO::ResultSet::results_exist
25 # and its two siblings. Core's base rendering has no FROM clause at all, and
26 # Firebird has no FROM-less SELECT; RDB$DATABASE is the one-row system table
27 # that makes it a legal statement here. Only the dummy table is ours -- the
28 # CASE spelling stays core's, so there is one copy of it.
29 #
30 # The literal is single-quoted on purpose: in a double-quoted string $DATABASE
31 # interpolates as an (empty) global and the statement silently becomes
32 # "FROM RDB", which parses as a table reference and fails far from here.
33 6 ▶sub _exists_value_sql {
Subroutine _exists_value_sql: called CC=1 SCAR=0.0
34 6 my $self = shift;
35 6 return $self->next::method(@_) . ' FROM RDB$DATABASE';
36 }
37  
38 # ... and the capability that lets core call the above at all. Not vacuous
39 # next to the override: it is this driver asserting that somebody checked the
40 # rendering against Firebird, which is why the default is 0 and why declaring
41 # is a separate act from overriding.
42 21 __PACKAGE__->_use_supports_exists_query (1);
43  
44 # F02 / F10 -- transactional DDL, and the one capability in this file whose
45 # value is NOT the engine's answer. Firebird HAS transactional DDL: metadata
46 # lives in ordinary MVCC records in the rdb$ system tables, a DDL statement
47 # stays in the user transaction until that transaction ends, and a ROLLBACK
48 # takes it back. That is not inferred -- it is the whole reason isql carries
49 # SET AUTODDL, whose default ON exists to commit each DDL statement as it runs
50 # and whose OFF (isql -n) hands DDL back to the transaction (Firebird 5.0 isql
51 # manual, "Transaction Handling" and "SET AUTOddl"). Core's own
52 # DBIO::Deploy::Base lists this engine in its transactional set.
53 #
54 # The 0 below is this driver declining to promise the ATOMICITY of a bracketed
55 # batch, which is a narrower claim than "the engine has transactional DDL" and
56 # the only claim DBIO::Deploy::Base::_execute_ddl actually acts on. Three
57 # things stand between the engine fact and that promise, and none of them could
58 # be settled here: nothing in this distribution has ever run against a Firebird
59 # server (t/10, t/15, t/17, t/20, t/30 all need DBIO_TEST_FIREBIRD_DSN), so
60 # every claim below is documentation plus the shipped DBD binary, never a
61 # measurement.
62 #
63 # 1. The diff emits a same-name DROP/CREATE PAIR that a bracket would put in
64 # one transaction. DBIO::Firebird::Diff::Index turns a changed index into
65 # DROP INDEX "n" followed by CREATE INDEX "n" -- the same name, adjacent in
66 # the batch. Today a real COMMIT separates them, because each statement runs
67 # on its own. Under one txn_do the re-CREATE stores an rdb$indices row whose
68 # rdb$index_name duplicates one this transaction has deleted but not
69 # committed, and Firebird enforces that name through its own unique index
70 # rdb$index_5 with "visible to active transactions" semantics -- the
71 # spelling of the error is literally "attempt to store duplicate value
72 # (visible to active transactions) in unique index RDB$INDEX_5" (the shape
73 # is documented for a plain duplicate index name; whether an uncommitted
74 # self-delete also counts as visible is exactly what needs a server).
75 # What makes this decisive rather than merely unknown is that it is a
76 # constraint on a PAIR. The _ddl_statement_is_transactional hook next door
77 # is per statement: it can say "this statement may not be inside a
78 # transaction", it cannot say "these two may not be inside the SAME one".
79 # Answering false for DROP INDEX would split the pair by accident while
80 # stating something untrue about the statement, and would take index drops
81 # out of the atomic batch anyway -- i.e. pay the price the 1 was for.
82 #
83 # 2. CREATE INDEX does its work at COMMIT, not at the statement. Firebird 5.0
84 # Language Reference, ALTER INDEX: "if our ALTER INDEX succeeds and starts
85 # to rebuild the index at COMMIT, other transactions modifying that table
86 # will fail or wait, according to their WAIT/NO WAIT attributes. The
87 # situation is the same for CREATE INDEX." (Unchanged from the 2.5
88 # reference, so this is not version-bounded.) Bracketing the batch moves
89 # every index build in it onto the single commit at the end: a failing
90 # CREATE UNIQUE INDEX then surfaces from the commit rather than from the
91 # statement that caused it, and concurrent writers to those tables are held
92 # against for the length of the whole deploy instead of one statement.
93 #
94 # 3. The DBD half, which is where the family's DuckDB finding applies here: the
95 # engine may manage it and the transport still not. This driver TELLS users
96 # to switch on ib_softcommit (see DBIO::Firebird::Storage::InterBase and its
97 # CAVEATS -- without it a commit closes the cursors still open on the
98 # connection), and this repo's own live tests set it for both
99 # native DSNs. ib_softcommit makes DBD::Firebird use isc_commit_retaining
100 # AND isc_rollback_retaining -- both are undefined symbols of the shipped
101 # DBD::Firebird 1.39 object, next to the strings "try isc_commit_retaining",
102 # "try isc_rollback_retaining" and "dbd_db_STORE: switch ib_softcommit from
103 # %d to %d"; its own POD says soft commit "retains the internal transaction
104 # handle". Firebird 5.0 Language Reference, COMMIT and ROLLBACK: RETAIN
105 # means "the transaction is committed, but some server resources are
106 # retained and a new transaction is restarted transparently with the same
107 # Transaction ID", and for the rollback "although all work of the
108 # transaction is to be rolled back, the transaction context is to be
109 # retained". So on the transport this driver recommends, the COMMIT that
110 # would close a bracketed DDL batch and the ROLLBACK that would take it back
111 # are both retaining calls that deliberately do not end the transaction.
112 # Nothing today depends on that, because nothing ever rolls DDL back; a 1
113 # would make recovery depend on isc_rollback_retaining undoing a batch of
114 # METADATA changes, which no documentation covers. Jaybird, the official
115 # Firebird JDBC driver, declines to use commit retaining "during normal
116 # execution" at all.
117 #
118 # Supporting rather than deciding: the engine's own history says several DDL
119 # statements in one transaction is not the well-trodden path here. Firebird
120 # release notes: "Database corruption could occur if two different indexes on a
121 # table were dropped within the same transaction" (CORE-2397 / CORE-2359, fixed
122 # 2.1.1 / 2.5.6 -- and a diff routinely drops several indexes off one table);
123 # "Problems could occur if further DDL commands were issued in the same
124 # transaction following a CREATE COLLATION command" (CORE-3056); "SET AUTODDL
125 # OFF and sequence of CREATE + ALTER + DROP VIEW would lead to a server crash"
126 # (CORE-5107, fixed 3.0.3); FirebirdSQL/firebird#8801, still open against 5.0.x,
127 # is database corruption reached with set autoddl OFF. Those particular ones are
128 # fixed; the pattern is why SET AUTODDL ON is the default in the first place.
129 #
130 # The price of the 0 is core's one-time non-transactional-DDL notice and a
131 # batch that is not atomic: an apply that fails part way leaves the earlier
132 # statements applied. Re-running upgrade is safe -- the diff is recomputed
133 # against live state, so the second run picks up only what is left.
134 #
135 # The flip condition is concrete, not a promise. On a live server, check (1)
136 # whether DROP INDEX "n" + CREATE INDEX "n" survives one transaction, and (3)
137 # whether a txn_do rollback under ib_softcommit really takes a batch of rdb$
138 # changes back. If both hold, this becomes 1 and the notice goes away. If only
139 # (1) fails, the honest fix is in DBIO::Firebird::Diff::Index -- rename through
140 # a distinct index name so the batch no longer contains the pair -- not in the
141 # per-statement hook, which cannot express the constraint.
142 #
143 # Level: this sits on Storage::Common, so all three transports inherit it.
144 # Reasons 1 and 2 are facts about this engine and about the DDL these shared
145 # Diff/DDL classes emit, and they reach DBD::Firebird, DBD::InterBase and
146 # DBD::ODBC alike (ADR 0010's cut: engine facts go here). Reason 3 is a fact
147 # about the two native DBDs only and needs no declaration of its own, since it
148 # can only push the answer further towards the conservative value this already
149 # is. If this ever flips, DBIO::Firebird::Storage::ODBC is where it could flip
150 # first, and that would be the moment to split the declaration.
151 21 __PACKAGE__->_use_transactional_ddl (0);
152  
153 # ADR 0039 -- the keyword DBIO::ResultSet joins the operands of a set
154 # operation with, or the refusal when this engine has no such operation.
155 # Firebird has UNION and UNION ALL and nothing else: EXCEPT, INTERSECT and
156 # MINUS are absent from the <query-expression> grammar and from the complete
157 # keyword list of the Firebird 5.0 Language Reference alike, and
158 # FirebirdSQL/firebird#1194 has asked for them since 2004 without being
159 # implemented. Refusal is unconditional rather than version-gated -- see the
160 # DESCRIPTION for why that costs nothing here, unlike on Oracle. The two
161 # operations we do have fall through to core's base map, which spells them
162 # UNION and UNION ALL.
163 21 my %UNSUPPORTED_SET_OPERATION = map { $_ => 1 }
164 qw(intersect intersect_all except except_all);
165  
166 16 ▶sub _set_operation_keyword {
Subroutine _set_operation_keyword: called CC=3 SCAR=11.0
167 16 my ($self, $op) = @_;
168  
169 $self->throw_exception(
170 "Firebird cannot express the set operation '$op': UNION and UNION ALL are "
171 . 'the only set operators in the language -- EXCEPT, INTERSECT and MINUS are '
172 . 'not keywords of this engine at all (Firebird 5.0 Language Reference, the '
173 . '<query-expression> production and Appendix C; requested in '
174 . 'FirebirdSQL/firebird#1194 since 2004, still open). InterBase is the same. '
175 . 'The refusal is unconditional and covers every release: this hook chooses '
176 . 'the keyword without a connection (ADR 0039 section 7), and no version has '
177 . 'the operators anyway. There is no drop-in replacement, but set difference '
178 . 'is expressible as a search with a NOT EXISTS subquery and intersection '
179 . 'with EXISTS.'
180 16 ▶ ) if defined $op and $UNSUPPORTED_SET_OPERATION{$op};
Branch: 1 branch
BranchTrueFalseTotal
if defined $op and $UNSUPPORTED_SET_OPERATION{$op} 10 6 16
Condition66%2/3
defined $op and $UNSUPPORTED_SET_OPERATION{$op}
!ll&&!rl&&r
0610
MC/DC50%1/2
defined $op and $UNSUPPORTED_SET_OPERATION{$op}
defined $op $UNSUPPORTED_SET_OPERATION{$op}
Truth table66%2/3
A and B
A= defined $opB= $UNSUPPORTED_SET_OPERATION{$op}
ABresult
0X0
100
111
181  
182 6 return $self->next::method($op);
183 }
184  
185 # ADR 0011 / karr k17 -- the function DBIO::ResultSet::rand drops into an
186 # ORDER BY literal. Core answers RANDOM(), which is right on PostgreSQL and
187 # SQLite and is not a function either of these engines has; the failure mode of
188 # inheriting it is loud, an unresolvable function name in the server's own
189 # error. Firebird spells it RAND() (internal since 2.1), and so does the ib_udf
190 # external function that covers everything older and InterBase -- one string
191 # for both engines and every version, which is why it sits on this class and
192 # not on either driver. See the DESCRIPTION for the sources, including the one
193 # for per-row evaluation: an engine that computed this once per statement would
194 # not error, it would answer the rows unshuffled.
195 4 ▶sub _random_function { 'RAND()' }
Subroutine _random_function: called CC=1 SCAR=0.0
196  
197 0 ▶sub _sequence_fetch {
Subroutine _sequence_fetch: not called CC=3 SCAR=24.8
198 0 my ($self, $nextval, $sequence) = @_;
199  
200 0 ▶ $self->throw_exception("Can only fetch 'nextval' for a sequence")
Branch: 1 branch
BranchTrueFalseTotal
unless $nextval =~ /^nextval$/i 0 0 0
201 if $nextval !~ /^nextval$/i;
202  
203 0 ▶ $self->throw_exception('No sequence to fetch') unless $sequence;
Branch: 1 branch
BranchTrueFalseTotal
unless $sequence 0 0 0
204  
205 0 my ($val) = $self->_get_dbh->selectrow_array(sprintf
206 'SELECT GEN_ID(%s, 1) FROM rdb$database',
207 $self->sql_maker->_quote($sequence)
208 );
209  
210 0 return $val;
211 }
212  
213 0 ▶sub _dbh_get_autoinc_seq {
Subroutine _dbh_get_autoinc_seq: not called CC=9 SCAR=45.0
214 0 my ($self, $dbh, $source, $col) = @_;
215  
216 0 my $table_name = $source->from;
217 0 ▶ $table_name = $$table_name if ref $table_name;
Branch: 1 branch
BranchTrueFalseTotal
if ref $table_name 0 0 0
218 0 ▶ $table_name = $self->sql_maker->quote_char ? $table_name : uc($table_name);
Branch: 1 branch
BranchTrueFalseTotal
$self->sql_maker->quote_char ? : 0 0 0
219  
220 0 local $dbh->{LongReadLen} = 100000;
221 0 local $dbh->{LongTruncOk} = 1;
222  
223 0 my $sth = $dbh->prepare(<<'EOF');
224 SELECT t.rdb$trigger_source
225 FROM rdb$triggers t
226 WHERE t.rdb$relation_name = ?
227 AND t.rdb$system_flag = 0 -- user defined
228 AND t.rdb$trigger_type = 1 -- BEFORE INSERT
229 EOF
230 0 $sth->execute($table_name);
231  
232 0 ▶ while (my ($trigger) = $sth->fetchrow_array) {
Branch: 1 branch
BranchTrueFalseTotal
if (my($trigger) = $sth->fetchrow_array) 0 0 0
233 my @trig_cols = map
234 0 ▶ { /^"([^"]+)/ ? $1 : uc($_) }
Branch: 1 branch
BranchTrueFalseTotal
/^"([^"]+)/ ? : 0 0 0
235 $trigger =~ /new\.("?\w+"?)/ig
236 ;
237  
238 0 my ($quoted, $generator) = $trigger =~
239 /(?:gen_id\s* \( \s* |next \s* value \s* for \s*)(")?(\w+)/ix;
240  
241 0 ▶ if ($generator) {
Branch: 1 branch
BranchTrueFalseTotal
if ($generator) 0 0 0
242 0 ▶ $generator = uc $generator unless $quoted;
Branch: 1 branch
BranchTrueFalseTotal
unless $quoted 0 0 0
243  
244 return $generator
245 if grep {
246 0 ▶ $self->sql_maker->quote_char ? ($_ eq $col) : (uc($_) eq uc($col))
Branch: 2 branches
BranchTrueFalseTotal
if grep {$self->sql_maker->quote_char ? $_ eq $col : uc $_ eq uc $col;} @trig_cols 0 0 0
$self->sql_maker->quote_char ? : 0 0 0
247 } @trig_cols;
248 }
249 }
250  
251 0 return undef;
252 }
253  
254 0 ▶sub _exec_svp_begin {
Subroutine _exec_svp_begin: not called CC=1 SCAR=6.9
255 0 my ($self, $name) = @_;
256  
257 0 $self->_dbh->do("SAVEPOINT $name");
258 }
259  
260 0 ▶sub _exec_svp_release {
Subroutine _exec_svp_release: not called CC=1 SCAR=6.9
261 0 my ($self, $name) = @_;
262  
263 0 $self->_dbh->do("RELEASE SAVEPOINT $name");
264 }
265  
266 0 ▶sub _exec_svp_rollback {
Subroutine _exec_svp_rollback: not called CC=1 SCAR=6.9
267 0 my ($self, $name) = @_;
268  
269 0 $self->_dbh->do("ROLLBACK TO SAVEPOINT $name")
270 }
271  
272 # http://www.firebirdfaq.org/faq223/
273 0 ▶sub _get_server_version {
Subroutine _get_server_version: not called CC=1 SCAR=6.9
274 0 my $self = shift;
275  
276 0 return $self->_get_dbh->selectrow_array(q{
277 SELECT rdb$get_context('SYSTEM', 'ENGINE_VERSION') FROM rdb$database
278 });
279 }
280  
281  
282 21 ▶sub _mask_error_values {
Subroutine _mask_error_values: called CC=3 SCAR=12.0
283 21 my ($self, $text, $values, $statement) = @_;
284  
285 21 ▶ return $text unless defined $text and !ref $text;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $text and not ref $text 0 21 21
Condition33%1/3
defined $text and not ref $text
!ll&&!rl&&r
0021
MC/DC0%0/2
defined $text and not ref $text
defined $text not ref $text
Truth table33%1/3
A and not(B)
A= defined $textB= not ref $text
ABresult
0X0
100
111
286  
287 21 $text = mask_error_details($text, $values);
288 21 return $self->next::method($text, $values, $statement);
289 }
290  
291  
292 21 1;
293  
294 __END__
« Firebird/Introspect/Tables.pm ↑ Summary Firebird/Diff/Constraint.pm »