Firebird/Storage/InterBase.pm

filter: Statementstmt 91.8% 258 / 281 Branchbran 86.2% 150 / 174 Conditioncond 68.8% 84 / 122 MC/DCmcdc 54.9% 39 / 71 Subroutinesub 92.3% 36 / 39 Podpod 100% 3 / 3 totaltotal 82.6% 570 / 690 CC 142 SCAR 31.1
CC
142
Cov
85%
_select_table_name
119.4
_bind_host_into_dsn
38.0
_exec_txn_commit
30.0
CRAP 22.4
« Firebird/Type.pm ↑ Summary Firebird/Diff/Table.pm »
1 package DBIO::Firebird::Storage::InterBase;
2 # ABSTRACT: Driver for the Firebird RDBMS via L<DBD::InterBase>
3  
4 20 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 20 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 20 our $VERSION = '0.900007';
8  
9 20 ▶use base qw/DBIO::Firebird::Storage::Common/;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 20 ▶use mro 'c3';
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 20 DBIO::Storage::DBI->register_driver('InterBase' => __PACKAGE__);
13  
14 20 __PACKAGE__->datetime_parser_type('DBIO::Firebird::DateTime::Format');
15  
16 20 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 20 ▶use Scalar::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
18 20 ▶use DBIO::Exception ();
Subroutine BEGIN: called CC=1 SCAR=0.0
19 20 ▶use DBIO::Storage::DBI::NoBindVars ();
Subroutine BEGIN: called CC=1 SCAR=0.0
20 20 ▶use DBIO::Firebird::Type qw(is_boolean_type is_binary_type is_numeric_type lob_kind);
Subroutine BEGIN: called CC=1 SCAR=0.0
21 20 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 # The driver's Deploy class, one source for both native transports
24 # (DBIO::Firebird::Storage inherits it); Storage::ODBC does not (karr k40).
25 2 ▶sub dbio_deploy_class { 'DBIO::Firebird::Deploy' }
Subroutine dbio_deploy_class: called CC=1 SCAR=0.0
Pod documented
26  
27  
28 # karr k49 -- a handle whose last DDL commit the server refused is alive.
29 #
30 # DBD::Firebird commits after DDL and does not roll back when the server refuses
31 # the commit (DROP TABLE of a table a live statement holds: `object TABLE "..."
32 # is in use`). The transaction stays as it is and every later statement on the
33 # handle fails with the same error, the ping's select included. core's dbh_do
34 # retries when `! $storage->connected`, so the refused DDL read as a dead
35 # connection: the handle was replaced and the block run again on the new one,
36 # where nothing holds the table -- success reported for a statement the server
37 # had refused, the session's state (context variables, on_connect actions) gone.
38 #
39 # So a failed ping is not the answer yet. With AutoCommit on there is no
40 # transaction of the application on the handle, and begin_work + rollback
41 # discards the one the failed commit left (rollback alone is "ineffective with
42 # AutoCommit enabled" and does nothing; measured, Firebird 5.0.4, DBD 1.39).
43 # Then the ping is asked once more: a live connection answers, a dead one fails
44 # again -- or already in begin_work (a guard for a handle already inside a
45 # transaction; with DBD::Firebird 1.39 that case does not occur, it was not
46 # measured). With AutoCommit off the application's own
47 # transaction is on the handle, which is not ours to roll back, and the failed
48 # ping stands.
49 18 ▶sub _ping {
Subroutine _ping: called CC=5 SCAR=16.1
50 18 my $self = shift;
51  
52 18 ▶ my $dbh = $self->_dbh or return 0;
Branch: 1 branch
BranchTrueFalseTotal
my $dbh = $self->_dbh or return 0 17 1 18
53  
54 17 local $dbh->{RaiseError} = 1;
55 17 local $dbh->{PrintError} = 0;
56  
57 19 ▶ my $ping = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
58 19 ▶ return try {
Subroutine __ANON__: called CC=1 SCAR=0.0
59 19 $dbh->do('select 1 from rdb$database');
60 14 1;
61 5 ▶ } catch {
Subroutine __ANON__: called CC=1 SCAR=0.0
62 5 0;
63 19 };
64 17 };
65  
66 17 ▶ return 1 if $ping->();
Branch: 1 branch
BranchTrueFalseTotal
if &$ping() 13 4 17
67  
68 4 ▶ return 0 unless $dbh->{AutoCommit};
Branch: 1 branch
BranchTrueFalseTotal
unless $dbh->{"AutoCommit"} 1 3 4
69  
70 3 ▶ return 0 unless try { $dbh->begin_work; $dbh->rollback; 1 };
Subroutine __ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
unless &try(sub { $dbh->begin_work; $dbh->rollback; 1; } ) 1 2 3
71  
72 2 return $ping->();
73 }
74  
75 # A commit the server refuses (CREATE UNIQUE INDEX over duplicate rows is
76 # accepted as a statement and refused at the commit; karr k35). DBD::Firebird
77 # fails the commit and leaves the handle in AutoCommit; DBI then dies with
78 # "DBD driver has not implemented the AutoCommit attribute" and the server's
79 # text is only on $dbh->errstr. Raise that text as core raises a refused
80 # statement (_dbi_handle_error): a DBIO::Exception::Server reading
81 # "DBI Exception: ..." with the sqlstate of the handle, so a caller tells the
82 # error apart by class and sqlstate whether the statement or the commit was
83 # refused. Only for DBI's own text: a commit that died of anything else keeps
84 # its error, and a stale errstr is never taken in its place.
85 0 ▶sub _exec_txn_commit {
Subroutine _exec_txn_commit: not called CC=5 SCAR=34.0
86 0 my $self = shift;
87  
88 0 my $dbh = $self->_dbh;
89 0 ▶ return 1 if eval { $dbh->commit; 1 };
Branch: 1 branch
BranchTrueFalseTotal
if eval { do { $dbh->commit; 1 } } 0 0 0
90  
91 0 my $err = $@;
92 0 my $server = $dbh->errstr;
93 0 ▶ $self->_dbi_handle_error($server, $dbh)
Branch: 1 branch
BranchTrueFalseTotal
if $err =~ /has not implemented the AutoCommit attribute/ and defined $server and length $server 0 0 0
Condition0%0/6
$err =~ /has not implemented the AutoCommit attribute/ and defined $server and length $server
!ll&&!rl&&r
000
$err =~ /has not implemented the AutoCommit attribute/ and defined $server
!ll&&!rl&&r
000
MC/DC0%0/3
$err =~ /has not implemented the AutoCommit attribute/ and defined $server and length $server
$err =~ /has not implemented the AutoCommit attribute/ defined $server length $server
Truth table0%0/4
A and B and C
A= $err =~ /has not implemented the AutoCommit attribute/B= defined $serverC= length $server
ABCresult
0XX0
10X0
1100
1111
94 if $err =~ /has not implemented the AutoCommit attribute/
95 && defined $server && length $server;
96  
97 0 die $err;
98 }
99  
100 # We want dialect 3 for new features and quoting to work, DBD::InterBase uses
101 # dialect 1 (interbase compat) by default.
102 66 ▶sub _init {
Subroutine _init: called CC=1 SCAR=0.0
103 66 my $self = shift;
104 66 $self->_set_sql_dialect(3);
105 66 $self->_set_utf8_connection;
106 }
107  
108 # karr k60 -- a native connection speaks UTF8 unless its connect info says
109 # otherwise: ib_charset=UTF8 in the DSN (the DBD reads the charset from the DSN
110 # alone) and ib_enable_utf8 as an attribute, which makes the DBD encode the
111 # Perl strings it is given and decode the text it reads. Only DBD::Firebird: the
112 # attribute was not measured on DBD::InterBase. What the caller wrote stays: a
113 # DSN that names an ib_charset (any value), an ib_enable_utf8 attribute (any
114 # value, 0 to opt out). ib_enable_utf8 is set only for a UTF8 connection, the
115 # one charset the DBD decodes.
116 #
117 # Returns the DSN to use and whether ib_enable_utf8 => 1 is to be added.
118 136 ▶sub _utf8_connection_defaults {
Subroutine _utf8_connection_defaults: called CC=14 SCAR=26.4
119 136 my ($self, $dsn, @attr_hashes) = @_;
120  
121 136 ▶ return ($dsn, 0) unless defined $dsn and !ref $dsn and $dsn =~ /\Adbi:Firebird\b/i;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $dsn and not ref $dsn and $dsn =~ /\Adbi:Firebird\b/i 22 114 136
Condition50%3/6
defined $dsn and not ref $dsn and $dsn =~ /\Adbi:Firebird\b/i
!ll&&!rl&&r
022114
defined $dsn and not ref $dsn
!ll&&!rl&&r
00136
MC/DC33%1/3
defined $dsn and not ref $dsn and $dsn =~ /\Adbi:Firebird\b/i
defined $dsn not ref $dsn $dsn =~ /\Adbi:Firebird\b/i
Truth table50%2/4
A and not(B) and C
A= defined $dsnB= not ref $dsnC= $dsn =~ /\Adbi:Firebird\b/i
ABCresult
0XX0
10X0
1100
1111
122  
123 114 my $charset;
124 114 ▶ while ($dsn =~ /(?:\A|[;:])\s*ib_charset\s*=\s*([^;]*)/ig) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dsn =~ /(?:\A|[;:])\s*ib_charset\s*=\s*([^;]*)/gi) 12 114 126
125 12 ($charset = $1) =~ s/\s+\z//;
126 }
127  
128 114 my ($named, $wanted);
129 114 for my $hash (grep { ref $_ eq 'HASH' } @attr_hashes) {
130 204 for my $key (grep { /\Aib_enable_utf8\z/i } keys %$hash) {
131 18 $named = 1;
132 18 ▶ $wanted = $hash->{$key} ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$hash->{$key} ? : 2 16 18
133 }
134 }
135 114 ▶ $wanted = 1 unless $named;
Branch: 1 branch
BranchTrueFalseTotal
unless $named 102 12 114
136  
137 114 ▶ if (!defined $charset) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $charset) 102 12 114
138 # the DBD decodes nothing but UTF8: asking for ib_enable_utf8 is asking for it
139 102 ▶ return $wanted ? ("$dsn;ib_charset=UTF8", $named ? 0 : 1) : ($dsn, 0);
Branch: 2 branches
BranchTrueFalseTotal
$wanted ? : 94 8 102
$named ? : 2 92 94
140 }
141  
142 12 ▶ return ($dsn, ($named || $charset !~ /\Autf-?8\z/i) ? 0 : 1);
Branch: 1 branch
BranchTrueFalseTotal
$named || !($charset =~ /\Autf-?8\z/i) ? : 10 2 12
Condition100%3/3
$named or not $charset =~ /\Autf-?8\z/i
l!l&&r!l&&!r
282
MC/DC100%2/2
$named or not $charset =~ /\Autf-?8\z/i
$named not $charset =~ /\Autf-?8\z/i
Truth table100%3/3
A or not(B)
A= $namedB= not $charset =~ /\Autf-?8\z/i
ABresult
1X1
011
000
143 }
144  
145 # Without a broker the connect info is the storage's own and is completed here,
146 # once, as the dialect is. With one it is formed anew for every connection and
147 # completed in _access_broker_dbi_args.
148 66 ▶sub _set_utf8_connection {
Subroutine _set_utf8_connection: called CC=9 SCAR=22.0
149 66 my $self = shift;
150  
151 66 ▶ return if $self->access_broker;
Branch: 1 branch
BranchTrueFalseTotal
if $self->access_broker 36 30 66
152  
153 30 my $info = $self->_dbi_connect_info;
154 30 ▶ return unless defined $info->[0] and !ref $info->[0];
Branch: 1 branch
BranchTrueFalseTotal
unless defined $info->[0] and not ref $info->[0] 2 28 30
Condition66%2/3
defined $info->[0] and not ref $info->[0]
!ll&&!rl&&r
0228
MC/DC50%1/2
defined $info->[0] and not ref $info->[0]
defined $info->[0] not ref $info->[0]
Truth table66%2/3
A and not(B)
A= defined $info->[0]B= not ref $info->[0]
ABresult
0X0
100
111
155  
156 28 my ($dsn, $set) = $self->_utf8_connection_defaults($info->[0], $info->[3]);
157  
158 28 ▶ return if $dsn eq $info->[0] and !$set;
Branch: 1 branch
BranchTrueFalseTotal
if $dsn eq $info->[0] and not $set 10 18 28
Condition100%3/3
$dsn eq $info->[0] and not $set
!ll&&!rl&&r
16210
MC/DC100%2/2
$dsn eq $info->[0] and not $set
$dsn eq $info->[0] not $set
Truth table100%3/3
A and not(B)
A= $dsn eq $info->[0]B= not $set
ABresult
0X0
100
111
159  
160 18 $info->[0] = $dsn;
161 18 ▶ $info->[3] = { %{ $info->[3] || {} }, ib_enable_utf8 => 1 } if $set;
Branch: 1 branch
BranchTrueFalseTotal
if $set 16 2 18
Condition50%1/2
$info->[3] || {}
l!l
160
MC/DC0%0/1
$info->[3] || {}
$info->[3]
Truth table50%1/2
A
A= $info->[3]
Aresult
11
00
162  
163 18 my $connected = defined $self->_dbh;
164 18 $self->disconnect;
165 18 ▶ $self->ensure_connected if $connected;
Branch: 1 branch
BranchTrueFalseTotal
if $connected 4 14 18
166 }
167  
168 # The dialect is a DSN attribute: DBD::Firebird and DBD::InterBase read
169 # ib_dialect from the DSN alone (see "ib_ attributes through an AccessBroker").
170 #
171 # Without a broker the DSN is the storage's own, rewritten here and the
172 # connection opened anew. With one, _dbi_connect_info is not stored but derived
173 # from the broker's answer at every call (core k266), so rewriting it changed
174 # nothing: the reconnect asked the broker again, and both connections were
175 # opened with the DSN the broker answered, without ib_dialect -- dialect 1 over
176 # DBD::InterBase, silently (karr k43). With a broker the dialect therefore joins
177 # every answer in _access_broker_dbi_args, and there is nothing to rewrite or
178 # reconnect here.
179 66 ▶sub _set_sql_dialect {
Subroutine _set_sql_dialect: called CC=6 SCAR=17.9
180 66 my $self = shift;
181 66 ▶ my $val = shift || 3;
Condition50%1/2
shift() || 3
l!l
660
MC/DC0%0/1
shift() || 3
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
182  
183 66 ▶ return if $self->access_broker;
Branch: 1 branch
BranchTrueFalseTotal
if $self->access_broker 36 30 66
184  
185 30 my $dsn = $self->_dbi_connect_info->[0];
186  
187 30 ▶ return if ref($dsn) eq 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
if ref $dsn eq "CODE" 2 28 30
188  
189 28 ▶ if ($dsn !~ /ib_dialect=/) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $dsn =~ /ib_dialect=/) 25 3 28
190 25 $self->_dbi_connect_info->[0] = "$dsn;ib_dialect=$val";
191 25 my $connected = defined $self->_dbh;
192 25 $self->disconnect;
193 25 ▶ $self->ensure_connected if $connected;
Branch: 1 branch
BranchTrueFalseTotal
if $connected 2 23 25
194 }
195 }
196  
197 # karr k43 -- the DSN of every connection a broker's answer opens carries the
198 # dialect. A code reference or dbh_maker opens its connection itself and is not
199 # ours to rewrite (as without a broker, where such a DSN is left alone too); a
200 # DSN of another DBD (an ODBC one) has no ib_ attribute to carry.
201 #
202 # An answer that names ib_dialect itself is read the way the DBD reads it --
203 # the key in any letter case and with blanks, in the DSN or in the attribute
204 # hash (to refuse a value loudly) -- and only 3 stands: this storage speaks dialect 3 (quoted identifiers, the modern
205 # types) and a connection in dialect 1 would run every statement of it in a
206 # different language without a word. Refused loudly, naming the place and
207 # never the DSN or a password. An ib_dialect attribute of 3 is not enough by
208 # itself (the DBD reads the DSN alone and drops the attribute), so the DSN
209 # gets ib_dialect=3 then too. So does a DSN spelling the key the DBD does not
210 # read (_OdbcParse compares with eq and does not trim: IB_DIALECT=3 and
211 # "ib_dialect = 3" are other keys to it): ;ib_dialect=3 is appended unless the
212 # DBD itself reads 3 (_dbd_reads_dialect_3).
213 143 ▶sub _access_broker_dbi_args {
Subroutine _access_broker_dbi_args: called CC=11 SCAR=24.1
214 143 my $self = shift;
215  
216 143 my ($args, $attrs) = $self->next::method(@_);
217  
218 130 my $dsn = $args->[0];
219 130 ▶ if (defined $dsn and !ref $dsn and $dsn =~ /\Adbi:(?:Firebird|InterBase)\b/i) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $dsn and not ref $dsn and $dsn =~ /\Adbi:(?:Firebird|InterBase)\b/i) 122 8 130
Condition83%5/6
defined $dsn and not ref $dsn and $dsn =~ /\Adbi:(?:Firebird|InterBase)\b/i
!ll&&!rl&&r
26122
defined $dsn and not ref $dsn
!ll&&!rl&&r
02128
MC/DC66%2/3
defined $dsn and not ref $dsn and $dsn =~ /\Adbi:(?:Firebird|InterBase)\b/i
defined $dsn not ref $dsn $dsn =~ /\Adbi:(?:Firebird|InterBase)\b/i
Truth table75%3/4
A and not(B) and C
A= defined $dsnB= not ref $dsnC= $dsn =~ /\Adbi:(?:Firebird|InterBase)\b/i
ABCresult
0XX0
10X0
1100
1111
220 122 my @named = $self->_named_sql_dialects($dsn, $attrs, $args->[3]);
221  
222 122 my %where = map { $_->[0] => 1 } grep { $_->[1] ne '3' } @named;
223 122 ▶ $self->throw_exception(
Branch: 1 branch
BranchTrueFalseTotal
if %where 14 108 122
224 'AccessBroker ' . ref($self->access_broker) . ' answered an ib_dialect '
225 . 'other than 3 (in ' . join(' and ', sort keys %where)
226 . '). DBIO::Firebird speaks SQL dialect 3 and refuses a connection in '
227 . 'another one; answer ib_dialect=3 or none.'
228 ) if %where;
229  
230 108 ▶ if (!$self->_dbd_reads_dialect_3($dsn)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->_dbd_reads_dialect_3($dsn)) 102 6 108
231 102 $args = [ @$args ];
232 102 $args->[0] = "$dsn;ib_dialect=3";
233 102 $dsn = $args->[0];
234 }
235  
236 # karr k60: the connection speaks UTF8 unless the answer says otherwise
237 108 my ($utf8_dsn, $set) = $self->_utf8_connection_defaults($dsn, $args->[3], $attrs);
238 108 ▶ if ($utf8_dsn ne $dsn or $set) {
Branch: 1 branch
BranchTrueFalseTotal
if ($utf8_dsn ne $dsn or $set) 78 30 108
Condition66%2/3
$utf8_dsn ne $dsn or $set
l!l&&r!l&&!r
78030
MC/DC50%1/2
$utf8_dsn ne $dsn or $set
$utf8_dsn ne $dsn $set
Truth table66%2/3
A or B
A= $utf8_dsn ne $dsnB= $set
ABresult
1X1
011
000
239 78 $args = [ @$args ];
240 78 $args->[0] = $utf8_dsn;
241 78 ▶ if ($set) {
Branch: 1 branch
BranchTrueFalseTotal
if ($set) 78 0 78
242 78 ▶ $args->[3] = { %{ $args->[3] || {} }, ib_enable_utf8 => 1 };
Condition50%1/2
$args->[3] || {}
l!l
780
MC/DC0%0/1
$args->[3] || {}
$args->[3]
Truth table50%1/2
A
A= $args->[3]
Aresult
11
00
243 78 ▶ $attrs = { %{ $attrs || {} }, ib_enable_utf8 => 1 };
Condition50%1/2
$attrs || {}
l!l
780
MC/DC0%0/1
$attrs || {}
$attrs
Truth table50%1/2
A
A= $attrs
Aresult
11
00
244 }
245 }
246 }
247  
248 116 return ($args, $attrs);
249 }
250  
251 # What DBD::Firebird's _OdbcParse reads from the DSN: parts split at ';',
252 # key compared with eq (exactly 'ib_dialect'), the value as it stands, no
253 # trimming, the last one wins. "IB_DIALECT=3" or "ib_dialect = 3" are other
254 # keys to it and read as nothing.
255 108 ▶sub _dbd_reads_dialect_3 {
Subroutine _dbd_reads_dialect_3: called CC=5 SCAR=16.2
256 108 my ($self, $dsn) = @_;
257  
258 108 (my $rest = $dsn) =~ s/\Adbi:[^:]*://i;
259 108 my $read;
260 108 for my $part (split /;\r?\n?/, $rest) {
261 170 ▶ $read = $2 if $part =~ /\A([^=]*)=(.*)\z/s and $1 eq 'ib_dialect';
Branch: 1 branch
BranchTrueFalseTotal
if $part =~ /\A([^=]*)=(.*)\z/s and $1 eq "ib_dialect" 6 164 170
Condition66%2/3
$part =~ /\A([^=]*)=(.*)\z/s and $1 eq "ib_dialect"
!ll&&!rl&&r
01646
MC/DC50%1/2
$part =~ /\A([^=]*)=(.*)\z/s and $1 eq "ib_dialect"
$part =~ /\A([^=]*)=(.*)\z/s $1 eq "ib_dialect"
Truth table66%2/3
A and B
A= $part =~ /\A([^=]*)=(.*)\z/sB= $1 eq "ib_dialect"
ABresult
0X0
100
111
262 }
263 108 ▶ return defined $read && $read eq '3';
Condition66%2/3
defined $read && $read eq '3'
!ll&&!rl&&r
10206
MC/DC50%1/2
defined $read && $read eq '3'
defined $read $read eq '3'
Truth table66%2/3
A && B
A= defined $readB= $read eq '3'
ABresult
0X0
100
111
264 }
265  
266 # Every ib_dialect an answer names, as [ place, value ]: DSN parts as the
267 # DBD's parser reads them (key in any case, value up to the next semicolon,
268 # blanks around the value ignored) and attribute keys in any case.
269 122 ▶sub _named_sql_dialects {
Subroutine _named_sql_dialects: called CC=6 SCAR=18.0
270 122 my ($self, $dsn, @attr_hashes) = @_;
271  
272 122 my @named;
273 122 ▶ while ($dsn =~ /(?:\A|[;:])\s*ib_dialect\s*=\s*([^;]*)/ig) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dsn =~ /(?:\A|[;:])\s*ib_dialect\s*=\s*([^;]*)/gi) 32 122 154
274 32 (my $value = $1) =~ s/\s+\z//;
275 32 push @named, [ 'the DSN', $value ];
276 }
277  
278 122 for my $hash (@attr_hashes) {
279 244 ▶ next unless ref $hash eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $hash eq "HASH" 0 244 244
280 244 for my $key (grep { /\Aib_dialect\z/i } keys %$hash) {
281 24 my $v = $hash->{$key};
282 24 ▶ push @named, [ 'the attributes', defined $v ? "$v" : '' ];
Branch: 1 branch
BranchTrueFalseTotal
defined $v ? : 24 0 24
283 }
284 }
285  
286 122 return @named;
287 }
288  
289 # karr k39 (core k298, k307) -- which lowercase keys of a broker's answer are
290 # attributes of the DBD and may go to DBI->connect. DBI's own registry, which
291 # core asks, knows ib_ for DBD::InterBase and nothing for DBD::Firebird.
292 # $driver is the DBD name off the DSN, not this class's: any other one,
293 # InterBase included, is core's to answer. Here rather than on Storage::Common
294 # so that the ODBC transport does not vouch for ib_ (ADR 0010).
295 39 ▶sub _dbd_attribute_prefixes {
Subroutine _dbd_attribute_prefixes: called CC=3 SCAR=11.0
296 39 my ($self, $driver) = @_;
297  
298 39 ▶ return 'ib_' if defined $driver && $driver eq 'Firebird';
Branch: 1 branch
BranchTrueFalseTotal
if defined $driver and $driver eq "Firebird" 30 9 39
Condition100%3/3
defined $driver and $driver eq "Firebird"
!ll&&!rl&&r
2730
MC/DC100%2/2
defined $driver and $driver eq "Firebird"
defined $driver $driver eq "Firebird"
Truth table100%3/3
A and B
A= defined $driverB= $driver eq "Firebird"
ABresult
0X0
100
111
299  
300 9 return $self->next::method($driver);
301 }
302  
303 # karr k50 (core k276) -- the host of a HostBound view in a DSN of the native
304 # DBDs. Core's default knows a host= and port= token and declines db=host:path;
305 # these DBDs take the server three ways, and the view must reach the host it
306 # names, never the master of the answer:
307 #
308 # db=host[/port]:path the host rides in the database name
309 # host=h[/port] | hostname=h a token, the port inside it or in port=
310 # db=path no host: a local database
311 #
312 # Whichever the answer uses is rewritten where it stands; a local database gets
313 # host= (and port=) put in front, as core does. What is not translated is
314 # declined with the form named -- a key or a dialect, never a value of the DSN:
315 # a part that is not key=value, a host named in two places, several database
316 # keys, a URL-style database name (inet://...), a view host the form cannot
317 # carry, and any other DBD's DSN (this storage knows Firebird and InterBase).
318 # Like core, a view without a port leaves the answer's.
319 61 ▶sub _bind_host_into_dsn {
Subroutine _bind_host_into_dsn: called CC=38 SCAR=36.4
320 61 my ($self, $driver, $dsn, $host, $port) = @_;
321  
322 61 ▶ return (undef, 'a DSN without a dbi:Driver: prefix')
Branch: 1 branch
BranchTrueFalseTotal
unless defined $dsn and $dsn =~ /\A(dbi:([^:(]*)(?:\([^)]*\))?:)(.*)\z/is 1 60 61
Condition66%2/3
defined $dsn and $dsn =~ /\A(dbi:([^:(]*)(?:\([^)]*\))?:)(.*)\z/is
!ll&&!rl&&r
0160
MC/DC50%1/2
defined $dsn and $dsn =~ /\A(dbi:([^:(]*)(?:\([^)]*\))?:)(.*)\z/is
defined $dsn $dsn =~ /\A(dbi:([^:(]*)(?:\([^)]*\))?:)(.*)\z/is
Truth table66%2/3
A and B
A= defined $dsnB= $dsn =~ /\A(dbi:([^:(]*)(?:\([^)]*\))?:)(.*)\z/is
ABresult
0X0
100
111
323 unless defined $dsn and $dsn =~ /\A(dbi:([^:(]*)(?:\([^)]*\))?:)(.*)\z/is;
324 60 my ($prefix, $dbd, $rest) = ($1, $2, $3);
325  
326 60 ▶ return (undef, "a DSN of the DBD '$dbd', not Firebird or InterBase")
Branch: 1 branch
BranchTrueFalseTotal
unless $dbd =~ /\A(?:Firebird|InterBase)\z/i 1 59 60
327 unless $dbd =~ /\A(?:Firebird|InterBase)\z/i;
328  
329 59 my (@tokens, %at);
330 59 for my $part (split /;/, $rest) {
331 93 ▶ next unless $part =~ /\S/;
Branch: 1 branch
BranchTrueFalseTotal
unless $part =~ /\S/ 0 93 93
332 93 ▶ return (undef, 'a part without key=value form')
Branch: 1 branch
BranchTrueFalseTotal
unless $part =~ /\A\s*(\w+)\s*=(.*)\z/s 1 92 93
333 unless $part =~ /\A\s*(\w+)\s*=(.*)\z/s;
334 92 my ($key, $value) = ($1, $2);
335 92 my $slot = lc $key;
336 92 ▶ $slot = 'db' if $slot eq 'dbname' or $slot eq 'database';
Branch: 1 branch
BranchTrueFalseTotal
if $slot eq "dbname" or $slot eq "database" 23 69 92
Condition100%3/3
$slot eq "dbname" or $slot eq "database"
l!l&&r!l&&!r
21269
MC/DC100%2/2
$slot eq "dbname" or $slot eq "database"
$slot eq "dbname" $slot eq "database"
Truth table100%3/3
A or B
A= $slot eq "dbname"B= $slot eq "database"
ABresult
1X1
011
000
337 92 ▶ $slot = 'host' if $slot eq 'hostname';
Branch: 1 branch
BranchTrueFalseTotal
if $slot eq "hostname" 5 87 92
338 92 ▶ return (undef, 'more than one of the keys db, dbname and database') if $slot eq 'db' and $at{db};
Branch: 1 branch
BranchTrueFalseTotal
if $slot eq "db" and $at{"db"} 1 91 92
Condition100%3/3
$slot eq "db" and $at{"db"}
!ll&&!rl&&r
34571
MC/DC100%2/2
$slot eq "db" and $at{"db"}
$slot eq "db" $at{"db"}
Truth table100%3/3
A and B
A= $slot eq "db"B= $at{"db"}
ABresult
0X0
100
111
339 91 ▶ return (undef, 'more than one of the keys host and hostname') if $slot eq 'host' and $at{host};
Branch: 1 branch
BranchTrueFalseTotal
if $slot eq "host" and $at{"host"} 1 90 91
Condition100%3/3
$slot eq "host" and $at{"host"}
!ll&&!rl&&r
70201
MC/DC100%2/2
$slot eq "host" and $at{"host"}
$slot eq "host" $at{"host"}
Truth table100%3/3
A and B
A= $slot eq "host"B= $at{"host"}
ABresult
0X0
100
111
340 90 ▶ return (undef, "more than one '$key' key") if $slot eq 'port' and $at{port};
Branch: 1 branch
BranchTrueFalseTotal
if $slot eq "port" and $at{"port"} 0 90 90
Condition66%2/3
$slot eq "port" and $at{"port"}
!ll&&!rl&&r
8370
MC/DC0%0/2
$slot eq "port" and $at{"port"}
$slot eq "port" $at{"port"}
Truth table66%2/3
A and B
A= $slot eq "port"B= $at{"port"}
ABresult
0X0
100
111
341 90 push @tokens, { raw => $part, key => $key, value => $value };
342 90 ▶ $at{$slot} = $tokens[-1] if $slot =~ /\A(?:db|host|port)\z/;
Branch: 1 branch
BranchTrueFalseTotal
if $slot =~ /\A(?:db|host|port)\z/ 84 6 90
343 }
344  
345 my $db = $at{db}
346 56 ▶ or return (undef, 'no db, dbname or database key');
Branch: 1 branch
BranchTrueFalseTotal
my $db = $at{"db"} or return undef, "no db, dbname or database key" 55 1 56
347  
348 return (undef, "the key '$db->{key}' with a URL-style database name")
349 55 ▶ if $db->{value} =~ m{\A\s*[A-Za-z][A-Za-z0-9+.-]*://};
Branch: 1 branch
BranchTrueFalseTotal
if $db->{"value"} =~ m[\A\s*[A-Za-z][A-Za-z0-9+.-]*://] 2 53 55
350  
351 # the view's host must fit the place it is written to
352 53 ▶ return (undef, 'a view host with a colon, slash or space in it')
Branch: 1 branch
BranchTrueFalseTotal
if $host =~ m[[:/\s]] 3 50 53
353 if $host =~ m{[:/\s]};
354  
355 # forms of the database name that name a server in a way this translation
356 # does not carry, or that cannot be told from a local path: refused, never
357 # translated into something else
358 return (undef, "an IPv6 address in the key '$db->{key}'")
359 50 ▶ if $db->{value} =~ /\A\s*\[/;
Branch: 1 branch
BranchTrueFalseTotal
if $db->{"value"} =~ /\A\s*\[/ 1 49 50
360 return (undef, "a drive-relative path in the key '$db->{key}'")
361 49 ▶ if $db->{value} =~ m{\A\s*[A-Za-z]:(?![\\/])};
Branch: 1 branch
BranchTrueFalseTotal
if $db->{"value"} =~ m[\A\s*[A-Za-z]:(?![\\/])] 1 48 49
362 return (undef, "a service name after the host in the key '$db->{key}'")
363 48 ▶ if $db->{value} =~ m{\A[^:/\\\s]+/[^:/\\\s]*\D[^:/\\\s]*:};
Branch: 1 branch
BranchTrueFalseTotal
if $db->{"value"} =~ m[\A[^:/\\\s]+/[^:/\\\s]*\D[^:/\\\s]*:] 1 47 48
364  
365 # a host in the database name -- but a drive letter (C:\data\a.fdb) is not
366 47 my ($dbhost, $dbport, $path) = $db->{value} =~ m{\A([^:/\\\s]+)(?:/(\d+))?:(.*)\z}s;
367 47 ▶ undef $dbhost if defined $dbhost and $db->{value} =~ m{\A[A-Za-z]:[\\/]};
Branch: 1 branch
BranchTrueFalseTotal
if defined $dbhost and $db->{"value"} =~ m[\A[A-Za-z]:[\\/]] 2 45 47
Condition100%3/3
defined $dbhost and $db->{"value"} =~ m[\A[A-Za-z]:[\\/]]
!ll&&!rl&&r
25202
MC/DC100%2/2
defined $dbhost and $db->{"value"} =~ m[\A[A-Za-z]:[\\/]]
defined $dbhost $db->{"value"} =~ m[\A[A-Za-z]:[\\/]]
Truth table100%3/3
A and B
A= defined $dbhostB= $db->{"value"} =~ m[\A[A-Za-z]:[\\/]]
ABresult
0X0
100
111
368  
369 47 ▶ if (defined $dbhost) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $dbhost) 20 27 47
370 return (undef, "a host in both the key '$db->{key}' and a host key")
371 20 ▶ if $at{host};
Branch: 1 branch
BranchTrueFalseTotal
if $at{"host"} 1 19 20
372 return (undef, "a port key beside a host in the key '$db->{key}'")
373 19 ▶ if $at{port};
Branch: 1 branch
BranchTrueFalseTotal
if $at{"port"} 1 18 19
374  
375 18 ▶ $port //= $dbport;
Condition100%3/3
$port //= $dbport
l!l&&r!l&&!r
6210
MC/DC100%2/2
$port //= $dbport
$port $dbport
Truth table100%3/3
A //= B
A= $portB= $dbport
ABresult
1X1
011
000
376 18 ▶ $db->{raw} = "$db->{key}=$host" . (defined $port ? "/$port" : '') . ":$path";
Branch: 1 branch
BranchTrueFalseTotal
defined $port ? : 8 10 18
377 18 return $prefix . join ';', map { $_->{raw} } @tokens;
378 }
379  
380 # no host in the database name: a host token, the port inside it (h/3050) or
381 # in port=. A view port goes to port= and out of the host; without one the
382 # answer's port stays where it was.
383 27 my $hostvalue = $host;
384 27 ▶ if (my $h = $at{host}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $h = $at{'host'}) { } 17 10 27
385 17 my ($oldport) = $h->{value} =~ m{/(\d+)\s*\z};
386 return (undef, "a service name in the key '$h->{key}' and a view without a port")
387 17 ▶ if !defined $port and $h->{value} =~ m{/} and !defined $oldport;
Branch: 1 branch
BranchTrueFalseTotal
if not defined $port and $h->{"value"} =~ m[/] and not defined $oldport 1 16 17
Condition100%6/6
not defined $port and $h->{"value"} =~ m[/] and not defined $oldport
!ll&&!rl&&r
1421
not defined $port and $h->{"value"} =~ m[/]
!ll&&!rl&&r
683
MC/DC100%3/3
not defined $port and $h->{"value"} =~ m[/] and not defined $oldport
not defined $port $h->{"value"} =~ m[/] not defined $oldport
Truth table100%4/4
not(A) and B and not(C)
A= not defined $portB= $h->{"value"} =~ m[/]C= not defined $oldport
ABCresult
0XX0
10X0
1100
1111
388 16 ▶ $hostvalue .= "/$oldport" if defined $oldport and !defined $port;
Branch: 1 branch
BranchTrueFalseTotal
if defined $oldport and not defined $port 2 14 16
Condition100%3/3
defined $oldport and not defined $port
!ll&&!rl&&r
1222
MC/DC100%2/2
defined $oldport and not defined $port
defined $oldport not defined $port
Truth table100%3/3
A and not(B)
A= defined $oldportB= not defined $port
ABresult
0X0
100
111
389 16 $h->{raw} = "$h->{key}=$hostvalue";
390 }
391 else {
392 10 unshift @tokens, $at{host} = { raw => "host=$hostvalue" };
393 }
394  
395 26 ▶ if (defined $port) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $port) 10 16 26
396 10 ▶ if ($at{port}) { $at{port}{raw} = "$at{port}{key}=$port" }
Branch: 1 branch
BranchTrueFalseTotal
if ($at{'port'}) { } 4 6 10
397 else {
398 6 my ($index) = grep { $tokens[$_] == $at{host} } 0 .. $#tokens;
399 6 splice @tokens, $index + 1, 0, { raw => "port=$port" };
400 }
401 }
402  
403 26 return $prefix . join ';', map { $_->{raw} } @tokens;
404 }
405  
406 # karr k37 -- never hand out a statement handle the DBD has destroyed.
407 #
408 # DBD::Firebird 1.39 (dbdimp.c) keeps a list of the statements of a connection
409 # and calls dbd_st_finish + dbd_st_destroy on every one of them: in
410 # ib_commit_transaction when DDL ran since the last commit (with or without
411 # ib_softcommit; ib_set_tx_param under AutoCommit counts itself as DDL and
412 # commits), and in ib_rollback_transaction on a hard rollback inside a
413 # transaction. The Perl handles know nothing of it: they stay in
414 # $dbh->{CachedKids} and prepare_cached goes on handing them out. See
415 # "Statements the DBD destroys" in the POD for what such a handle then does.
416 #
417 # This is the one place every statement of the storage is prepared through
418 # (_dbh_execute, _dbh_prepare and the bulk insert), so it is where a destroyed
419 # handle is stopped. On this class and not on Storage::Common: it is a fact
420 # about the DBD's API, not about the engine's SQL, and the ODBC transport has
421 # no part in it (ADR 0010).
422 #
423 # Two things here are shaped by a second fact about that list. dbd_st_destroy
424 # unlinks its statement unconditionally, and Perl runs it AGAIN when the handle
425 # is finally freed; for a statement that is already unlinked that second run
426 # reads as "the list is empty now" (first_sth = last_sth = NULL). Every
427 # statement prepared in between is then alive but off the list: the DBD no
428 # longer destroys it before a DDL commit, and it keeps its table "in use" for a
429 # later DROP TABLE on this connection (measured, Firebird 5.0). So:
430 #
431 # - the whole cache goes, not only the one entry. Every cached handle
432 # prepared before the event is destroyed as well, and each of them freed
433 # later, one by one as its SQL comes up again, would take the handles
434 # prepared since off the list. A handle prepared after the event that is
435 # thrown out along the way costs one prepare;
436 # - the destroyed handle is let go before the new one is prepared, not after.
437 #
438 # What this cannot reach is a destroyed handle something else still holds --
439 # a cursor that was being read from. Whenever that is freed, the list is
440 # emptied the same way.
441 #
442 # The new handle is not asked again: one freshly prepared is alive, and a DBD
443 # that says otherwise must cost a prepare per statement, not a loop.
444 16 ▶sub _prepare_sth {
Subroutine _prepare_sth: called CC=4 SCAR=13.9
445 16 my ($self, $dbh, $sql) = @_;
446  
447 16 my $sth = $self->next::method($dbh, $sql);
448  
449 16 ▶ return $sth
Branch: 1 branch
BranchTrueFalseTotal
if $self->disable_sth_caching or $self->_sth_is_alive($sth) 12 4 16
Condition100%3/3
$self->disable_sth_caching or $self->_sth_is_alive($sth)
l!l&&r!l&&!r
2104
MC/DC100%2/2
$self->disable_sth_caching or $self->_sth_is_alive($sth)
$self->disable_sth_caching $self->_sth_is_alive($sth)
Truth table100%3/3
A or B
A= $self->disable_sth_cachingB= $self->_sth_is_alive($sth)
ABresult
1X1
011
000
450 if $self->disable_sth_caching or $self->_sth_is_alive($sth);
451  
452 4 undef $sth;
453 4 ▶ %{ $dbh->{CachedKids} } = () if $dbh->{CachedKids};
Branch: 1 branch
BranchTrueFalseTotal
if $dbh->{"CachedKids"} 4 0 4
454  
455 4 return $self->next::method($dbh, $sql);
456 }
457  
458 # A value bound for a BOOLEAN column (karr k36). DBD::Firebird 1.39 binds every
459 # parameter as text and, for a BOOLEAN, takes Perl's truth of that text:
460 # '0', '' and 0 are FALSE, every other string is TRUE -- 'false' and 'x'
461 # included, stored as TRUE without an error (measured against Firebird 5.0.4).
462 # The words are what SQL spells the values with, so they are translated; ''
463 # is what the same DBD hands back for a FALSE it has read, so it is FALSE too
464 # (a read value must be writable: $row->copy, search({ on => $row->on })).
465 # Any other value is refused instead of being stored as whatever its truth is.
466 # The column is known from the bind's sqlt_datatype, so a parameter without
467 # column information is left as the caller wrote it.
468 94 ▶sub _boolean_bind_value {
Subroutine _boolean_bind_value: called CC=5 SCAR=16.1
469 94 my ($self, $info, $v) = @_;
470  
471 94 ▶ if ($v =~ /\A[01]\z/) { return $v }
Branch: 3 branches
BranchTrueFalseTotal
if ($v =~ /\A[01]\z/) { } 30 64 94
elsif ($v =~ /\Atrue\z/i) { } 18 46 64
elsif ($v =~ /\A(?:false)?\z/i) { } 32 14 46
472 18 elsif ($v =~ /\Atrue\z/i) { return 1 }
473 32 elsif ($v =~ /\A(?:false)?\z/i) { return 0 }
474  
475 $self->throw_exception(sprintf
476 q{The value bound to the BOOLEAN column '%s' is none of 1, 0, 'true', 'false', '' or undef; }
477 . q{DBD::Firebird would store any other string as TRUE},
478 14 ▶ $info->{dbic_colname} // '?',
Condition50%1/2
$info->{"dbic_colname"} // "?"
l!l
140
MC/DC0%0/1
$info->{"dbic_colname"} // "?"
$info->{"dbic_colname"}
Truth table50%1/2
A
A= $info->{"dbic_colname"}
Aresult
11
00
479 );
480 }
481  
482 # karr k60 -- DBD::Firebird converts the text it is given for a numeric
483 # parameter in Perl, and a text that is no number becomes 0 ('', ' ', 'x') or
484 # the number its first characters read as ('5x' is 5), with a warning and no
485 # error; the server answers a conversion error to the same text (measured on
486 # Firebird 5.0.4, DBD::Firebird 1.39: INTEGER, BIGINT, DOUBLE PRECISION and
487 # NUMERIC(9,2) alike). A row the server would refuse is stored as another
488 # number, so a value that is no number for a numeric column is refused before
489 # the DBD reads it. Whatever Perl calls a number is one (leading and trailing
490 # blanks, an exponent, '.5').
491 306 ▶sub _numeric_bind_check {
Subroutine _numeric_bind_check: called CC=3 SCAR=11.1
492 306 my ($self, $info, $v) = @_;
493  
494 306 ▶ return if Scalar::Util::looks_like_number($v);
Branch: 1 branch
BranchTrueFalseTotal
if &Scalar::Util::looks_like_number($v) 284 22 306
495  
496 DBIO::Exception->throw(sprintf
497 q{refusing the value bound to the numeric column '%s': it is no number, and DBD::Firebird would }
498 . q{store 0 (or the number its first characters read as) where the server answers a conversion error},
499 22 ▶ $info->{dbic_colname} // '?',
Condition50%1/2
$info->{"dbic_colname"} // "?"
l!l
220
MC/DC0%0/1
$info->{"dbic_colname"} // "?"
$info->{"dbic_colname"}
Truth table50%1/2
A
A= $info->{"dbic_colname"}
Aresult
11
00
500 );
501 }
502  
503 # karr k60 -- on a connection with ib_enable_utf8 the DBD encodes the Perl
504 # strings it is given to UTF-8, but only those Perl flags as such: "\x{fc}"
505 # written in a source file is a one-byte string and reaches the server as the
506 # byte FC, a malformed UTF-8 string (measured, Firebird 5.0.4 / DBD::Firebird
507 # 1.39). A string is characters, so a text bound with a character above 0x7F
508 # is flagged here. A binary column's bytes are not text and stay as they are.
509 # Whether the connection decodes is asked of the handle the statement belongs
510 # to.
511 56 ▶sub _utf8_bind_value {
Subroutine _utf8_bind_value: called CC=8 SCAR=21.1
512 56 my ($self, $sth, $info, $v) = @_;
513  
514 56 ▶ return $v if !defined $v or ref $v or utf8::is_utf8($v) or $v !~ /[^\x00-\x7f]/;
Branch: 1 branch
BranchTrueFalseTotal
if not defined $v or ref $v or utf8::is_utf8($v) or not $v =~ /[^\x00-\x7f]/ 32 24 56
Condition66%6/9
not defined $v or ref $v or utf8::is_utf8($v) or not $v =~ /[^\x00-\x7f]/
l!l&&r!l&&!r
23024
not defined $v or ref $v or utf8::is_utf8($v)
l!l&&r!l&&!r
0254
not defined $v or ref $v
l!l&&r!l&&!r
0056
MC/DC50%2/4
not defined $v or ref $v or utf8::is_utf8($v) or not $v =~ /[^\x00-\x7f]/
not defined $v ref $v utf8::is_utf8($v) not $v =~ /[^\x00-\x7f]/
Truth table60%3/5
not(A) or B or C or not(D)
A= not defined $vB= ref $vC= utf8::is_utf8($v)D= not $v =~ /[^\x00-\x7f]/
ABCDresult
1XXX1
01XX1
001X1
00011
00000
515 24 ▶ return $v unless eval { $sth->{Database}{ib_enable_utf8} };
Branch: 1 branch
BranchTrueFalseTotal
unless eval { do { $sth->{"Database"}{"ib_enable_utf8"} } } 6 18 24
516 18 ▶ return $v if ref $info eq 'HASH' and is_binary_type($info->{sqlt_datatype});
Branch: 1 branch
BranchTrueFalseTotal
if ref $info eq "HASH" and is_binary_type($info->{"sqlt_datatype"}) 4 14 18
Condition66%2/3
ref $info eq "HASH" and is_binary_type($info->{"sqlt_datatype"})
!ll&&!rl&&r
0144
MC/DC50%1/2
ref $info eq "HASH" and is_binary_type($info->{"sqlt_datatype"})
ref $info eq "HASH" is_binary_type($info->{"sqlt_datatype"})
Truth table66%2/3
A and B
A= ref $info eq "HASH"B= is_binary_type($info->{"sqlt_datatype"})
ABresult
0X0
100
111
517  
518 14 utf8::upgrade(my $copy = $v);
519 14 return $copy;
520 }
521  
522 # karr k60 -- DBD::Firebird dies on a BLOB parameter in a SELECT ("BLOB as an
523 # input param for SELECT is not allowed"), and the server compares a BLOB with
524 # a VARCHAR. So the placeholder of a value bound against a text or binary BLOB
525 # column of a SELECT is written CAST(? AS VARCHAR(n) ...): the text with
526 # CHARACTER SET UTF8 (the connection's, see "Text is UTF8 on every native
527 # connection"), 8191 characters at most, the bytes with OCTETS, 32765 at most
528 # -- Firebird's VARCHAR is 32765 bytes. A value above them cannot be compared
529 # at all and is refused here, before the DBD says something less clear.
530 # UPDATE and DELETE are left alone: the DBD takes a BLOB parameter there.
531 #
532 # The placeholders are found in the generated SQL (the binds carry their
533 # column only after the SQL maker has run, see _resolve_bindattrs) by core's
534 # scanner, DBIO::Storage::DBI::NoBindVars::placeholder_offsets: a '?' in a
535 # string, an identifier or a comment is not one (core k388). If the count does not match
536 # the binds the statement is left as it is.
537 20 my %LOB_CAST = (
538 text => [ 'CAST(? AS VARCHAR(8191) CHARACTER SET UTF8)', 8191, 'characters' ],
539 binary => [ 'CAST(? AS VARCHAR(32765) CHARACTER SET OCTETS)', 32765, 'bytes' ],
540 );
541  
542 47 ▶sub _gen_sql_bind {
Subroutine _gen_sql_bind: called CC=2 SCAR=6.9
543 47 my ($self, $op, $ident) = @_;
544  
545 47 my ($sql, $bind) = $self->next::method(@_[1 .. $#_]);
546  
547 47 ▶ return ($sql, $bind) unless $op eq 'select';
Branch: 1 branch
BranchTrueFalseTotal
unless $op eq "select" 2 45 47
548 45 return ($self->_cast_lob_binds($sql, $bind, $ident), $bind);
549 }
550  
551 45 ▶sub _cast_lob_binds {
Subroutine _cast_lob_binds: called CC=11 SCAR=24.0
552 45 my ($self, $sql, $bind, $ident) = @_;
553  
554 45 my @lobs;
555 45 for my $i (0 .. $#$bind) {
556 56 my ($info, $v) = @{ $bind->[$i] };
557 56 ▶ next unless ref $info eq 'HASH' and defined $v and !ref $v;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $info eq "HASH" and defined $v and not ref $v 2 54 56
Condition66%4/6
ref $info eq "HASH" and defined $v and not ref $v
!ll&&!rl&&r
2054
ref $info eq "HASH" and defined $v
!ll&&!rl&&r
0254
MC/DC33%1/3
ref $info eq "HASH" and defined $v and not ref $v
ref $info eq "HASH" defined $v not ref $v
Truth table50%2/4
A and B and not(C)
A= ref $info eq "HASH"B= defined $vC= not ref $v
ABCresult
0XX0
10X0
1100
1111
558 54 ▶ my $kind = lob_kind($info->{sqlt_datatype}, $info->{sqlt_size}) or next;
Branch: 1 branch
BranchTrueFalseTotal
my $kind = lob_kind($info->{"sqlt_datatype"}, $info->{"sqlt_size"}) or next 32 22 54
559 32 push @lobs, [ $i, $kind, $info, $v ];
560 }
561 45 ▶ return $sql unless @lobs;
Branch: 1 branch
BranchTrueFalseTotal
unless @lobs 15 30 45
562  
563 30 my @at = DBIO::Storage::DBI::NoBindVars::placeholder_offsets($sql);
564 30 ▶ return $sql unless @at == @$bind;
Branch: 1 branch
BranchTrueFalseTotal
unless @at == @$bind 2 28 30
565  
566 28 for my $lob (reverse @lobs) {
567 30 my ($i, $kind, $info, $v) = @$lob;
568 30 my ($cast, $limit, $unit) = @{ $LOB_CAST{$kind} };
569  
570 30 ▶ if (length($v) > $limit) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $v > $limit) 4 26 30
571 4 ▶ my $column = $info->{dbic_colname} // '?';
Condition50%1/2
$info->{"dbic_colname"} // "?"
l!l
40
MC/DC0%0/1
$info->{"dbic_colname"} // "?"
$info->{"dbic_colname"}
Truth table50%1/2
A
A= $info->{"dbic_colname"}
Aresult
11
00
572 4 my ($alias) = $column =~ /\A(.*)\.[^.]*\z/;
573 4 $column =~ s/\A.*\.//;
574 4 DBIO::Exception->throw(sprintf
575 'refusing to compare the %s BLOB column %s.%s with a value of %d %s: DBD::Firebird '
576 . 'binds no BLOB parameter in a SELECT, so the value is compared as a VARCHAR, which '
577 . 'holds %d %s at most (32765 bytes, Firebird Language Reference, Character data '
578 . 'types). Compare a shorter value, or a column that is not a BLOB.',
579 $kind, $self->_select_table_name($ident, $alias), $column, length($v), $unit, $limit, $unit,
580 );
581 }
582  
583 26 substr($sql, $at[$i], 1) = $cast;
584 }
585  
586 24 return $sql;
587 }
588  
589 # The table behind an alias of a SELECT's from, for a message. A source gives
590 # its name; a from is [ { alias => table }, [ { alias => table, -join_type ... },
591 # \%condition ], ... ].
592 4 ▶sub _select_table_name {
Subroutine _select_table_name: called CC=13 SCAR=47.8
593 4 my ($self, $ident, $alias) = @_;
594  
595 4 ▶ return $ident->name if Scalar::Util::blessed($ident) and $ident->can('name') and !ref $ident->name;
Branch: 1 branch
BranchTrueFalseTotal
if builtin::blessed($ident) and $ident->can("name") and not ref $ident->name 4 0 4
Condition33%2/6
builtin::blessed($ident) and $ident->can("name") and not ref $ident->name
!ll&&!rl&&r
004
builtin::blessed($ident) and $ident->can("name")
!ll&&!rl&&r
004
MC/DC0%0/3
builtin::blessed($ident) and $ident->can("name") and not ref $ident->name
builtin::blessed($ident) $ident->can("name") not ref $ident->name
Truth table25%1/4
A and B and not(C)
A= builtin::blessed($ident)B= $ident->can("name")C= not ref $ident->name
ABCresult
0XX0
10X0
1100
1111
596  
597 0 my %table;
598 0 ▶ for my $part (ref $ident eq 'ARRAY' ? @$ident : ()) {
Branch: 1 branch
BranchTrueFalseTotal
ref $ident eq 'ARRAY' ? : 0 0 0
599 0 ▶ my $hash = ref $part eq 'ARRAY' ? $part->[0] : $part;
Branch: 1 branch
BranchTrueFalseTotal
ref $part eq 'ARRAY' ? : 0 0 0
600 0 ▶ next unless ref $hash eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $hash eq "HASH" 0 0 0
601 0 for my $key (grep { !/\A-/ } keys %$hash) {
602 0 ▶ $table{$key} = $hash->{$key} unless ref $hash->{$key};
Branch: 1 branch
BranchTrueFalseTotal
unless ref $hash->{$key} 0 0 0
603 }
604 }
605  
606 0 ▶ return $table{$alias} if defined $alias and defined $table{$alias};
Branch: 1 branch
BranchTrueFalseTotal
if defined $alias and defined $table{$alias} 0 0 0
Condition0%0/3
defined $alias and defined $table{$alias}
!ll&&!rl&&r
000
MC/DC0%0/2
defined $alias and defined $table{$alias}
defined $alias defined $table{$alias}
Truth table0%0/3
A and B
A= defined $aliasB= defined $table{$alias}
ABresult
0X0
100
111
607 0 ▶ return (values %table)[0] if keys %table == 1;
Branch: 1 branch
BranchTrueFalseTotal
if keys %table == 1 0 0 0
608 0 return '?';
609 }
610  
611 # The one place the values of a bind are adjusted (karr k63), for core's two
612 # ways of binding: _bind_sth_params (create, search, update) and, per row,
613 # _dbh_execute_for_fetch (populate in void context). This storage overrides
614 # neither of them. A literal bind (\[ '?', [ on => 'false' ] ]) is resolved by
615 # core into a pair of its own before it gets here, so it is adjusted like any
616 # other. The column is known from the meta's sqlt_datatype, which the
617 # core's _resolve_bindattrs fills from the source for both ways.
618 #
619 # The answer is a new list of new pairs; the pairs of the caller are never
620 # written to. A reference in the value slot (a bind_param_inout target) is
621 # left as it is. The attributes are not touched.
622 150 ▶sub _adjust_bind_values {
Subroutine _adjust_bind_values: called CC=9 SCAR=22.1
623 150 my ($self, $sth, $bind, $bind_attrs) = @_;
624  
625 150 my @bind;
626 150 for my $b (@$bind) {
627 492 my ($info, $v) = @$b;
628 492 my $new = $v;
629  
630 492 ▶ if (defined $v and !ref $v) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $v and not ref $v) 456 36 492
Condition100%3/3
defined $v and not ref $v
!ll&&!rl&&r
342456
MC/DC100%2/2
defined $v and not ref $v
defined $v not ref $v
Truth table100%3/3
A and not(B)
A= defined $vB= not ref $v
ABresult
0X0
100
111
631 456 ▶ my $type = ref $info eq 'HASH' ? $info->{sqlt_datatype} : undef;
Branch: 1 branch
BranchTrueFalseTotal
ref $info eq 'HASH' ? : 456 0 456
632  
633 456 ▶ if (ref $info eq 'HASH' and is_boolean_type($type)) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $info eq 'HASH' and is_boolean_type($type)) { } 94 362 456
elsif (ref $info eq 'HASH' and is_numeric_type($type)) { } 306 56 362
Condition66%2/3
ref $info eq "HASH" and is_boolean_type($type)
!ll&&!rl&&r
036294
MC/DC50%1/2
ref $info eq "HASH" and is_boolean_type($type)
ref $info eq "HASH" is_boolean_type($type)
Truth table66%2/3
A and B
A= ref $info eq "HASH"B= is_boolean_type($type)
ABresult
0X0
100
111
634 94 $new = $self->_boolean_bind_value($info, $v);
635 }
636 ▶ elsif (ref $info eq 'HASH' and is_numeric_type($type)) {
Condition66%2/3
ref $info eq "HASH" and is_numeric_type($type)
!ll&&!rl&&r
056306
MC/DC50%1/2
ref $info eq "HASH" and is_numeric_type($type)
ref $info eq "HASH" is_numeric_type($type)
Truth table66%2/3
A and B
A= ref $info eq "HASH"B= is_numeric_type($type)
ABresult
0X0
100
111
637 306 $self->_numeric_bind_check($info, $v);
638 }
639 else {
640 56 $new = $self->_utf8_bind_value($sth, $info, $v);
641 }
642 }
643  
644 456 push @bind, [ $info, $new ];
645 }
646  
647 114 return \@bind;
648 }
649  
650 # Whether the DBD still has the statement behind this handle, asked without a
651 # round trip to the server: bind a placeholder one past the last. dbd_bind_ph
652 # of DBD::Firebird 1.39 reads
653 #
654 # if (!imp_sth || !imp_sth->in_sqlda) return FALSE;
655 # if ((int)SvIV(param) > imp_sth->in_sqlda->sqld) return TRUE;
656 #
657 # and in_sqlda is what dbd_st_destroy frees and sets to NULL. So a prepared
658 # statement answers true and nothing is bound, and a destroyed one answers
659 # false -- without setting an error, which is why nothing else notices.
660 #
661 # This rests on those two lines. A DBD that answered false for a prepared
662 # handle would have every statement prepared anew on each use: slower, never
663 # wrong. One that answered true for a destroyed handle would bring k37 back;
664 # t/23-statement-cache-ddl-live.t asks a handle before and after DDL for that.
665 16 ▶sub _sth_is_alive {
Subroutine _sth_is_alive: called CC=2 SCAR=6.9
666 16 my ($self, $sth) = @_;
667  
668 16 ▶ return $sth->bind_param($sth->{NUM_OF_PARAMS} + 1, undef) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$sth->bind_param($sth->{'NUM_OF_PARAMS'} + 1, undef) ? : 12 4 16
669 }
670  
671 # ADR 0040 / karr k17 -- the whole operation of getting a query plan here.
672 # There is no statement to issue: the plan is an attribute of the PREPARED
673 # statement, so the query itself is prepared and never executed, and the plan
674 # is read off the handle with the DBD's private ib_plan function. See the
675 # DESCRIPTION for why it is func('ib_plan') and not $sth->{ib_plan}, and why
676 # this lives on the DBD::InterBase class rather than on Storage::Common.
677 #
678 # _prepare_rendered is the seam core built for exactly this shape and it is not
679 # optional: reaching for $self->dbh would lose tracing and with it
680 # redact_bind_value over the caller's own bind values, dbh_do's retry,
681 # NoBindVars interpolation, and the read handler of a replicated storage.
682 2 ▶sub _explain_query {
Subroutine _explain_query: called CC=2 SCAR=6.9
683 2 my ($self, $sql, $bind) = @_;
684  
685 2 my $sth = $self->_prepare_rendered($sql, $bind);
686  
687 2 my $plan = $sth->func('ib_plan');
688  
689 # undef here is the DBD's fixed 2048-byte plan buffer overflowing, not "this
690 # query has no plan" -- every prepared statement has one. Answering
691 # [ [ undef ] ] would put that indistinguishably into the caller's plan.
692 2 ▶ $self->throw_exception(
Branch: 1 branch
BranchTrueFalseTotal
unless defined $plan 1 1 2
693 'Firebird prepared the statement but the driver returned no plan for it. '
694 . 'Every prepared statement has a plan, so this is the DBD rather than the '
695 . 'engine: DBD::Firebird and DBD::InterBase read isc_info_sql_get_plan into '
696 . 'a fixed 2048-byte buffer (PLAN_BUFFER_LEN) and answer undef when the '
697 . 'plan does not fit. Explain a smaller query, or read the plan through the '
698 . 'API directly.'
699 ) unless defined $plan;
700  
701 1 return [ [ $plan ] ];
702 }
703  
704 # Every connection reads dates and timestamps in the ISO formats (karr k55).
705 # DBD::Firebird answers in the locale's %c format until ib_time_all is set,
706 # which the DateTime parser of this driver and any string comparison do not
707 # expect -- a silent wrong value for a user who did not know to ask for
708 # connect_call_datetime_setup. Done before the caller's own actions, so an
709 # on_connect_call that sets another format wins; setting the same value again
710 # (datetime_setup) changes nothing.
711 68 ▶sub _run_connection_actions {
Subroutine _run_connection_actions: called CC=1 SCAR=0.0
712 68 my $self = shift;
713  
714 68 $self->_dbh->{ib_time_all} = 'ISO';
715  
716 68 return $self->next::method(@_);
717 }
718  
719  
720 0 ▶sub connect_call_use_softcommit {
Subroutine connect_call_use_softcommit: not called CC=1 SCAR=6.9
Pod documented
721 0 my $self = shift;
722  
723 0 $self->_dbh->{ib_softcommit} = 1;
724 }
725  
726  
727 0 ▶sub connect_call_datetime_setup {
Subroutine connect_call_datetime_setup: not called CC=1 SCAR=6.9
Pod documented
728 0 my $self = shift;
729  
730 0 $self->_get_dbh->{ib_time_all} = 'ISO';
731 }
732  
733  
734 20 1;
735  
736 __END__
« Firebird/Type.pm ↑ Summary Firebird/Diff/Table.pm »