DuckDB/Storage.pm

filter: Statementstmt 18.5% 66 / 356 Branchbran 1.3% 2 / 144 Conditioncond 5.7% 5 / 87 MC/DCmcdc 4.2% 2 / 47 Subroutinesub 25.7% 17 / 66 Podpod 93.7% 15 / 16 totaltotal 14.9% 107 / 716 CC 112 SCAR 37.6
CC
112
Cov
12%
__ANON__
182.0
__ANON__
110.0
_duckdb_release_statements
90.0
CRAP 42.9
« DuckDB/Introspect/Tables.pm ↑ Summary DuckDB/Introspect/Uniques.pm »
1 package DBIO::DuckDB::Storage;
2 # ABSTRACT: DuckDB storage driver for DBIO
3  
4 33 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 33 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 33 our $VERSION = '0.900004';
8  
9 33 ▶use base qw/DBIO::Storage::DBI/;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 33 ▶use mro 'c3';
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 33 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 33 ▶use Math::BigInt ();
Subroutine BEGIN: called CC=1 SCAR=0.0
14 33 ▶use FFI::Platypus::Buffer ();
Subroutine BEGIN: called CC=1 SCAR=0.0
15 33 ▶use version ();
Subroutine BEGIN: called CC=1 SCAR=0.0
16 33 ▶use DBIO::Util qw(mask_connect_secrets);
Subroutine BEGIN: called CC=1 SCAR=0.0
17 33 ▶use DBIO::DuckDB::Extension ();
Subroutine BEGIN: called CC=1 SCAR=0.0
18 33 ▶use DBIO::DuckDB::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
19 33 ▶use DBIO::Extension::Protocol ();
Subroutine BEGIN: called CC=1 SCAR=0.0
20 33 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 33 __PACKAGE__->register_driver('DuckDB' => __PACKAGE__);
23  
24 0 ▶sub dbio_deploy_class { 'DBIO::DuckDB::Deploy' }
Subroutine dbio_deploy_class: not called CC=1 SCAR=6.9
Pod undocumented
25  
26 33 __PACKAGE__->sql_maker_class('DBIO::DuckDB::SQLMaker');
27 33 __PACKAGE__->sql_quote_char ('"');
28  
29 # ADR 0037: core's base _exists_value_sql renders
30 # SELECT CASE WHEN EXISTS <inner> THEN 1 ELSE 0 END
31 # which DuckDB accepts unchanged -- so this driver declares the capability
32 # and overrides nothing. See the CAPABILITIES POD below for the three
33 # assumptions that form makes and where each is documented.
34 33 __PACKAGE__->_use_supports_exists_query(1);
35  
36 # F02 / F10 -- transactional DDL. Declared 1: DuckDB the engine has
37 # transactional DDL, comprehensively, and this storage now ends the
38 # transactions the DBI driver underneath cannot (ADR 0005).
39 #
40 # DuckDB the engine has transactional DDL, comprehensively. Measured on
41 # libduckdb v1.5.3 via DBD::DuckDB 0.16: CREATE TABLE, DROP TABLE, ALTER TABLE
42 # (ADD / DROP / ALTER COLUMN), CREATE INDEX, DROP INDEX, CREATE SEQUENCE and
43 # CREATE VIEW all roll back inside BEGIN ... ROLLBACK, and the whole install
44 # batch this driver emits -- CREATE SEQUENCE, CREATE TABLE with
45 # DEFAULT nextval('...'), CREATE INDEX -- commits as one transaction with the
46 # sequence live afterwards. DuckDB documents the ALTER TABLE half outright --
47 # "All the changes made by ALTER TABLE fully respect the transactional
48 # semantics ... and can be fully reverted through a rollback"
49 # (duckdb.org/docs/stable/sql/statements/alter_table) -- and documents no
50 # statement this driver emits as one that may not be in a transaction, unlike
51 # PostgreSQL's ALTER TYPE ... ADD VALUE. Nothing here needs core's
52 # _ddl_statement_is_transactional escape. Nor is the deploy path the reason:
53 # DBIO::DuckDB::Diff emits plain statements in tables-then-columns-then-indexes
54 # order, with no SQLite-style create/copy/drop/rename rebuild and no pragma
55 # brackets that a txn_do wrap could swallow. Bracketed, the real path works --
56 # install, diff, apply and a second empty diff all pass, and a batch whose
57 # last statement fails rolls the earlier ones back instead of leaving them
58 # applied.
59 #
60 # One engine-side caveat, forward-looking rather than a breakage today. DuckDB
61 # refuses to alter a column an index depends on, and documents the remedy as
62 # "drop the index, alter the table, and then recreate the index" (same page).
63 # That remedy only works UNBRACKETED: measured here, DROP INDEX followed by
64 # ALTER TABLE ... ALTER COLUMN ... TYPE in one transaction still dies with
65 # "Cannot change the type of this column: an index depends on it!", because the
66 # type-change check does not see the uncommitted DROP INDEX. It is specific to
67 # the type change -- DROP COLUMN and SET NOT NULL after a DROP INDEX both
68 # commit fine. The diff cannot emit that pair today (indexes are diffed after
69 # columns, so a type change on an indexed column already fails, wrapped or
70 # not), so nothing regresses; but a wrap does foreclose the documented fix for
71 # it, and that is worth knowing before anyone reorders the phases.
72 #
73 # What used to block the 1 is that DBD::DuckDB could not end such a
74 # transaction. Its rollback issues ROLLBACK and then immediately reopens one:
75 #
76 # $dbh->do('ROLLBACK');
77 # $dbh->do('BEGIN TRANSACTION');
78 #
79 # which is right for AutoCommit=0, and DBI restores AutoCommit=1 afterwards --
80 # but the AutoCommit STORE handler that would COMMIT the reopened transaction
81 # is unreachable. It guards the commit branch with
82 # C<my $never_set = !$old_value>, so the preceding branch, C<!$old_value &&
83 # $value && $never_set>, has the identical condition and always wins; the
84 # 0 -> 1 transition is a silent no-op. (DBD::DuckDB 0.16, the current CPAN
85 # release, sub STORE.)
86 #
87 # So every txn_do that ROLLS BACK used to leave DuckDB inside an open
88 # transaction while DBI believes AutoCommit is on, and the next txn_begin died
89 # -- "cannot start a transaction within a transaction", or "Current transaction
90 # is aborted (please ROLLBACK)" when the rollback followed a failed statement.
91 # On the :memory: DSN that is DuckDB's headline mode it was worse than an
92 # error: DBIO::Storage::DBI::_exec_txn_begin deliberately retries begin_work on
93 # reconnect, so the poisoned handle was silently replaced by a connection to a
94 # brand-new, EMPTY in-memory database -- the deployed schema simply gone, with
95 # nothing raised.
96 #
97 # _exec_txn_rollback below closes the stray transaction itself (ADR 0005), so
98 # the engine argument for a 1 stands on its own: a failed apply rolls back
99 # whole AND the connection survives, and core's documented recovery -- re-run
100 # the upgrade, the diff is recomputed against live state -- works. The upstream
101 # STORE-branch fix (giterlizzi/perl-DBD-DuckDB#54) is still worth landing; the
102 # override is a no-op in behaviour once it is.
103 33 __PACKAGE__->_use_transactional_ddl(1);
104  
105  
106 38 ▶sub sql_maker {
Subroutine sql_maker: called CC=3 SCAR=12.3
Pod documented
107 38 my $self = shift;
108 38 my $sm = $self->next::method(@_);
109 38 ▶ $sm->{quote_char} //= $self->sql_quote_char;
Condition33%1/3
$sm->{"quote_char"} //= $self->sql_quote_char
l!l&&r!l&&!r
3800
MC/DC0%0/2
$sm->{"quote_char"} //= $self->sql_quote_char
$sm->{"quote_char"} $self->sql_quote_char
Truth table33%1/3
A //= B
A= $sm->{"quote_char"}B= $self->sql_quote_char
ABresult
1X1
011
000
110 38 ▶ $sm->{name_sep} //= $self->sql_name_sep;
Condition33%1/3
$sm->{"name_sep"} //= $self->sql_name_sep
l!l&&r!l&&!r
3800
MC/DC0%0/2
$sm->{"name_sep"} //= $self->sql_name_sep
$sm->{"name_sep"} $self->sql_name_sep
Truth table33%1/3
A //= B
A= $sm->{"name_sep"}B= $self->sql_name_sep
ABresult
1X1
011
000
111 38 $sm;
112 }
113  
114 0 ▶sub _determine_supports_insert_returning { 1 }
Subroutine _determine_supports_insert_returning: not called CC=1 SCAR=6.9
115  
116 # DBD::DuckDB 0.16's rollback issues ROLLBACK and then immediately reopens
117 # a transaction (BEGIN TRANSACTION), and the AutoCommit STORE branch that
118 # would close it again is unreachable code -- so after any txn_do that
119 # rolls back, DuckDB is still inside a transaction DBI believes is closed,
120 # and the next txn_begin dies (karr k15, ADR 0005). Close the orphan
121 # ourselves. The "no transaction is active" error is the expected outcome
122 # once DBD::DuckDB's STORE branch is fixed upstream -- swallow exactly
123 # that, so the normal rollback path is not broken.
124 0 ▶sub _exec_txn_rollback {
Subroutine _exec_txn_rollback: not called CC=1 SCAR=6.9
125 0 my $self = shift;
126 0 $self->next::method(@_);
127 0 ▶ try {
Subroutine __ANON__: not called CC=1 SCAR=6.9
128 0 $self->_dbh->do('ROLLBACK');
129 }
130 0 ▶ catch {
Subroutine __ANON__: not called CC=2 SCAR=17.9
131 0 ▶ die $_ unless $_ =~ /no transaction is active/i;
Branch: 1 branch
BranchTrueFalseTotal
unless $_ =~ /no transaction is active/i 0 0 0
132 0 };
133 }
134  
135 0 ▶sub _ping {
Subroutine _ping: not called CC=3 SCAR=24.8
136 0 my $self = shift;
137 0 ▶ my $dbh = $self->_dbh or return undef;
Branch: 1 branch
BranchTrueFalseTotal
my $dbh = $self->_dbh or return undef 0 0 0
138 0 ▶ return undef unless $dbh->FETCH('Active');
Branch: 1 branch
BranchTrueFalseTotal
unless $dbh->FETCH("Active") 0 0 0
139 0 ▶ return try { $dbh->do('SELECT 1'); 1 } catch { undef };
Subroutine __ANON__: not called CC=1 SCAR=6.9
Subroutine __ANON__: not called CC=1 SCAR=6.9
140 }
141  
142 # A DBD::DuckDB 0.16 statement handle cannot be used a second time, and DBI's
143 # statement cache exists to hand it out a second time (karr k33, ADR 0007).
144 # Three defects in DBD::DuckDB::st, independent of one another, measured on
145 # libduckdb v1.5.3, see docs/upstream/:
146 #
147 # - finish deletes duckdb_stmt. The handle stays in CachedKids and
148 # prepare_cached returns it dead: the second auto-increment insert, find
149 # or single of one SQL shape dies in bind_param, and the second populate
150 # -- core finishes after execute_for_fetch -- is a segmentation fault.
151 # - execute never sets Active, so prepare_cached($sql, {}, 3) cannot see
152 # that a handle is being read, and two open cursors of one SQL shape share
153 # one handle.
154 # - execute leaves duckdb_chunk and duckdb_chunk_row of the previous result
155 # in place. A handle that was not read to its end goes on reading the OLD
156 # chunk after the next execute: ->first for one value, then ->first for
157 # another, answers the second row of the first search. No error.
158 #
159 # Core asks this accessor, in _prepare_sth, whether to prepare or to
160 # prepare_cached, so answering here covers every statement core prepares. It
161 # is a method and not a value set at construction for the sake of a schema
162 # that names no driver: its storage is configured as the generic
163 # DBIO::Storage::DBI and becomes this class on the first use of the handle,
164 # and connect_info resets every storage option on each call besides. A value
165 # would have to survive both; a method of the class has nothing to survive.
166 #
167 # Recognising dead handles as they come out of the cache (dbio-firebird's
168 # _sth_is_alive) would mend the first defect only. Unlike the statement
169 # release below this is not bound to a DBD::DuckDB version: not caching is
170 # correct against any release, and caching again is a decision to take once a
171 # release has been measured -- t/49-statement-reuse.t carries the sentinel.
172 30 ▶sub disable_sth_caching { 1 }
Subroutine disable_sth_caching: called CC=1 SCAR=0.0
Pod documented
173  
174 # karr k43 (core k298, k307) -- which lowercase keys of a broker's answer are
175 # attributes of the DBD and may go to DBI->connect. DBI's own registry, which
176 # core asks, has no prefix for DBD::DuckDB (DBI->driver_prefix("DuckDB") is
177 # empty), though the DBD takes duckdb_* attributes. $driver is the DBD name off
178 # the DSN: any other one is core's to answer.
179 7 ▶sub _dbd_attribute_prefixes {
Subroutine _dbd_attribute_prefixes: called CC=3 SCAR=11.0
180 7 my ($self, $driver) = @_;
181  
182 7 ▶ return 'duckdb_' if defined $driver && $driver eq 'DuckDB';
Branch: 1 branch
BranchTrueFalseTotal
if defined $driver and $driver eq "DuckDB" 1 6 7
Condition100%3/3
defined $driver and $driver eq "DuckDB"
!ll&&!rl&&r
151
MC/DC100%2/2
defined $driver and $driver eq "DuckDB"
defined $driver $driver eq "DuckDB"
Truth table100%3/3
A and B
A= defined $driverB= $driver eq "DuckDB"
ABresult
0X0
100
111
183  
184 6 return $self->next::method($driver);
185 }
186  
187 # DBD::DuckDB 0.16 never frees a prepared statement (karr k25). Its
188 # st::finish calls duckdb_destroy_prepare($stmt) with the handle BY VALUE,
189 # where the C function takes duckdb_prepared_statement* -- FFI::Platypus
190 # turns a non-reference argument for a pointer type into NULL, without a
191 # warning, and libduckdb does nothing on NULL. And there is no st::DESTROY,
192 # so a handle nobody called finish on -- every $dbh->do, every handle DBIO
193 # prepares for an update, a delete or a cursor -- is not even attempted. Each
194 # leaked statement keeps the database instance alive, so duckdb_close in
195 # disconnect cannot drop the last reference and the lock on the database file
196 # outlives the handle until the process exits. Measured on libduckdb v1.5.3,
197 # see docs/upstream/.
198 #
199 # So this storage frees them: a DBI per-handle callback on the statement
200 # handles of ITS OWN connection, before finish and before DESTROY. Nothing in
201 # the DBD::DuckDB namespace is touched and no other DuckDB handle in the
202 # process is affected.
203 #
204 # What keeps that from becoming a double free once upstream frees them too:
205 #
206 # - it takes the pointer out of the handle BEFORE freeing it. DBI runs a
207 # callback ahead of the method, so whatever DBD::DuckDB does afterwards
208 # finds undef, which arrives in C as NULL (by value) or as a pointer to
209 # NULL (by reference) -- libduckdb does nothing on either (measured).
210 # - it is bound to the releases whose finish has been read. A newer
211 # DBD::DuckDB is left alone until someone has looked at it;
212 # t/47-disconnect-releases-file-lock.t goes red if that release still
213 # leaks, and its TODO passes if it does not.
214 #
215 # It stands down in a forked child, as core does for the connection itself
216 # (_verify_pid): only the process that opened the database releases anything
217 # in it. One thing it cannot reach: with AutoCommit => 0, DBD::DuckDB runs
218 # BEGIN TRANSACTION inside DBI->connect, before there is a handle to install
219 # a callback on, and that one statement stays.
220 #
221 # The same finish has a second defect, and the same callback answers it (karr
222 # k38). It hands libduckdb the handle's result unconditionally --
223 #
224 # my $res = delete $sth->{duckdb_res};
225 # duckdb_destroy_result($res);
226 #
227 # -- and a handle has a result only once an execute has succeeded: execute
228 # stores it after the engine accepted the statement, finish deletes it. On a
229 # handle that was never executed, whose only executes were refused, or that
230 # was finished before, $res is undef, which reaches C as NULL, and
231 # duckdb_destroy_result(NULL) is a segmentation fault (measured). Core
232 # finishes the handle of a bulk insert whatever happened to it, so a populate
233 # in void context whose first row the engine refuses did not throw: it ended
234 # the process.
235 #
236 # So in front of finish, a handle without a result is given an empty one --
237 # a zeroed duckdb_result, which is the state duckdb_destroy_result itself
238 # leaves a result in and does nothing on (measured, any number of times).
239 # DBD::DuckDB's finish then runs as written. It is in the callback and not in
240 # an override of core's two bulk-insert methods because the defect is in
241 # finish and not in populate: this covers every finish on the connection,
242 # cleanup code after a refused execute in dbh_do included, where an override
243 # would cover the one caller and copy a hundred lines of core to do it.
244 #
245 # Not for DESTROY -- DBD::DuckDB 0.16 has none, a dropped handle touches no
246 # result -- and not under the pid check: an empty result frees nothing, and a
247 # forked child that finishes a handle it inherited would crash just the same.
248 # A release that guards its own finish gets no callback at all (the version
249 # bound above), and would find nothing wrong with an empty result if it did.
250 # t/51-finish-without-result.t carries the sentinel.
251 33 my $_STATEMENT_LEAK_READ_UP_TO = version->parse('0.16');
252  
253 0 ▶sub _run_connection_actions {
Subroutine _run_connection_actions: not called CC=1 SCAR=6.9
254 0 my $self = shift;
255 0 $self->_duckdb_release_statements($self->_dbh);
256 0 $self->_duckdb_text_callbacks($self->_dbh);
257 0 $self->_duckdb_exact_decimals;
258 0 return $self->next::method(@_);
259 }
260  
261 0 ▶sub _duckdb_release_statements {
Subroutine _duckdb_release_statements: not called CC=9 SCAR=45.0
262 0 my ($self, $dbh) = @_;
263  
264 0 ▶ return unless ($dbh->{Driver}{Name} // '') eq 'DuckDB';
Branch: 1 branch
BranchTrueFalseTotal
unless ($dbh->{"Driver"}{"Name"} // "") eq "DuckDB" 0 0 0
Condition0%0/2
$dbh->{"Driver"}{"Name"} // ""
l!l
00
MC/DC0%0/1
$dbh->{"Driver"}{"Name"} // ""
$dbh->{"Driver"}{"Name"}
Truth table0%0/2
A
A= $dbh->{"Driver"}{"Name"}
Aresult
11
00
265 0 ▶ return if version->parse(DBD::DuckDB->VERSION) > $_STATEMENT_LEAK_READ_UP_TO;
Branch: 1 branch
BranchTrueFalseTotal
if "version"->parse("DBD::DuckDB"->VERSION) > $_STATEMENT_LEAK_READ_UP_TO 0 0 0
266 0 ▶ return if $dbh->{private_dbio_duckdb_statement_release};
Branch: 1 branch
BranchTrueFalseTotal
if $dbh->{"private_dbio_duckdb_statement_release"} 0 0 0
267  
268 0 my $pid = $$;
269 0 ▶ my $release = sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
270 0 my $sth = shift;
271 0 ▶ return if $$ != $pid;
Branch: 1 branch
BranchTrueFalseTotal
if $$ != $pid 0 0 0
272 0 ▶ my $stmt = $sth->{duckdb_stmt} or return;
Branch: 1 branch
BranchTrueFalseTotal
my $stmt = $sth->{"duckdb_stmt"} or return 0 0 0
273 0 $sth->{duckdb_stmt} = undef;
274 0 DBD::DuckDB::FFI::duckdb_destroy_prepare(\$stmt);
275 0 return;
276 0 };
277  
278 # finish only: something for it to destroy where the handle has no result.
279 0 ▶ my $give_result = sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
280 0 my $sth = shift;
281 0 ▶ $sth->{duckdb_res} //= DBD::DuckDB::FFI::Result->new;
Condition0%0/3
$sth->{"duckdb_res"} //= "DBD::DuckDB::FFI::Result"->new
l!l&&r!l&&!r
000
MC/DC0%0/2
$sth->{"duckdb_res"} //= "DBD::DuckDB::FFI::Result"->new
$sth->{"duckdb_res"} "DBD::DuckDB::FFI::Result"->new
Truth table0%0/3
A //= B
A= $sth->{"duckdb_res"}B= "DBD::DuckDB::FFI::Result"->new
ABresult
1X1
011
000
282 0 return;
283 0 };
284  
285 my %before = (
286 0 ▶ finish => sub { $release->($_[0]); $give_result->($_[0]); return },
Subroutine __ANON__: not called CC=1 SCAR=6.9
287 0 DESTROY => $release,
288 );
289  
290 # Chain in front of whatever the application installed, on a copy: the
291 # hash it handed to connect may be shared between connections.
292 0 ▶ my $callbacks = $dbh->{Callbacks} || {};
Condition0%0/2
$dbh->{'Callbacks'} || {}
l!l
00
MC/DC0%0/1
$dbh->{'Callbacks'} || {}
$dbh->{'Callbacks'}
Truth table0%0/2
A
A= $dbh->{'Callbacks'}
Aresult
11
00
293 0 ▶ my $child = $callbacks->{ChildCallbacks} || {};
Condition0%0/2
$callbacks->{'ChildCallbacks'} || {}
l!l
00
MC/DC0%0/1
$callbacks->{'ChildCallbacks'} || {}
$callbacks->{'ChildCallbacks'}
Truth table0%0/2
A
A= $callbacks->{'ChildCallbacks'}
Aresult
11
00
294 0 my %ours;
295 0 for my $method (qw/finish DESTROY/) {
296 0 my $mine = $before{$method};
297 0 my $theirs = $child->{$method};
298 $ours{$method} = $theirs
299 0 ▶ ? sub { $mine->($_[0]); goto &$theirs }
Subroutine __ANON__: not called CC=1 SCAR=6.9
300 0 ▶ : sub { $mine->($_[0]); return };
Subroutine __ANON__: not called CC=1 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
$theirs ? : 0 0 0
301 }
302  
303 0 $dbh->{Callbacks} = { %$callbacks, ChildCallbacks => { %$child, %ours } };
304 0 $dbh->{private_dbio_duckdb_statement_release} = 1;
305  
306 0 return;
307 }
308  
309 # DECIMAL, read back exact (karr k49, Bind conformance).
310 #
311 # DBD::DuckDB 0.16 reads a DECIMAL as its unscaled integer divided by 10**scale
312 # -- a perl double -- and prints that with sprintf("%.${scale}f"). A double holds
313 # 15 to 17 digits, so the 20-digit 1234567890123456.7890 of a numeric(20,4)
314 # comes back as 1234567890123456.7500: the stored value is right and the read
315 # invents the tail, with no error. Its HUGEINT reader, which a wide DECIMAL
316 # goes through, also subtracts 2**128 from every value whose upper word is odd
317 # (`$upper & (1 << 64)` is `$upper & 1`), so a numeric(20,4) above 2**64
318 # unscaled units reads wrong too.
319 #
320 # So the reader is replaced, once, by one that formats the unscaled integer as
321 # a string and puts the point in by hand; same output shape as before (scale
322 # digits, a sign only when negative). Only the readers of the 0.16 DBD that
323 # were read; a newer one is left alone. The replacement is of the DBD's
324 # private function, which is why it is bound to the version: 0.16 is the
325 # newest DBD::DuckDB there is, and the only one measured. t/58 holds the DBD's
326 # own misreading as a fact, in a child process that never loads this class, and
327 # goes red when a DBD reads these values right -- then this replacement goes.
328 33 my $_DECIMAL_READER_FIXED;
329  
330 0 ▶sub _duckdb_exact_decimals {
Subroutine _duckdb_exact_decimals: not called CC=5 SCAR=32.3
331 0 ▶ return if $_DECIMAL_READER_FIXED;
Branch: 1 branch
BranchTrueFalseTotal
if $_DECIMAL_READER_FIXED 0 0 0
332 0 ▶ return unless DBD::DuckDB->can('VERSION');
Branch: 1 branch
BranchTrueFalseTotal
unless "DBD::DuckDB"->can("VERSION") 0 0 0
333 0 ▶ return if version->parse(DBD::DuckDB->VERSION) > $_STATEMENT_LEAK_READ_UP_TO;
Branch: 1 branch
BranchTrueFalseTotal
if "version"->parse("DBD::DuckDB"->VERSION) > $_STATEMENT_LEAK_READ_UP_TO 0 0 0
334 0 ▶ return unless defined &DBD::DuckDB::st::_vector_decimal;
Branch: 1 branch
BranchTrueFalseTotal
unless defined &DBD::DuckDB::st::_vector_decimal 0 0 0
335  
336 0 my %width = (
337 DBD::DuckDB::Constants::DUCKDB_TYPE_SMALLINT() => [ 2, 's<' ],
338 DBD::DuckDB::Constants::DUCKDB_TYPE_INTEGER() => [ 4, 'l<' ],
339 DBD::DuckDB::Constants::DUCKDB_TYPE_BIGINT() => [ 8, 'q<' ],
340 );
341 0 my $hugeint = DBD::DuckDB::Constants::DUCKDB_TYPE_HUGEINT();
342  
343 0 my $original = \&DBD::DuckDB::st::_vector_decimal;
344 33 ▶ no warnings 'redefine';
Subroutine BEGIN: called CC=1 SCAR=0.0
345 0 ▶ *DBD::DuckDB::st::_vector_decimal = sub {
Subroutine __ANON__: not called CC=6 SCAR=37.4
346 0 my ($logical_type, $vector_data, $row_idx) = @_;
347  
348 0 my $scale = DBD::DuckDB::FFI::duckdb_decimal_scale($logical_type);
349 0 my $type = DBD::DuckDB::FFI::duckdb_decimal_internal_type($logical_type);
350  
351 0 my $unscaled;
352 0 ▶ if (my $w = $width{$type}) {
Branch: 2 branches
BranchTrueFalseTotal
if (my $w = $width{$type}) { } 0 0 0
elsif ($type == $hugeint) { } 0 0 0
353 0 $unscaled = Math::BigInt->new(
354 unpack $w->[1], FFI::Platypus::Buffer::buffer_to_scalar(
355 $vector_data + $row_idx * $w->[0], $w->[0]
356 )
357 );
358 }
359 elsif ($type == $hugeint) {
360 0 my ($lower, $upper) = unpack 'Q< q<',
361 FFI::Platypus::Buffer::buffer_to_scalar($vector_data + $row_idx * 16, 16);
362 0 $unscaled = Math::BigInt->new($upper)->blsft(64)->badd($lower);
363 }
364 else {
365 0 goto &$original;
366 }
367  
368 0 my $negative = $unscaled->is_neg;
369 0 my $digits = $unscaled->copy->babs->bstr;
370 0 ▶ $digits = ('0' x ($scale + 1 - length $digits)) . $digits
Branch: 1 branch
BranchTrueFalseTotal
if length $digits <= $scale 0 0 0
371 if length $digits <= $scale;
372 0 ▶ substr($digits, length($digits) - $scale, 0, '.') if $scale;
Branch: 1 branch
BranchTrueFalseTotal
if $scale 0 0 0
373 0 ▶ return ($negative ? '-' : '') . $digits;
Branch: 1 branch
BranchTrueFalseTotal
$negative ? : 0 0 0
374 0 };
375  
376 0 $_DECIMAL_READER_FIXED = 1;
377 0 return;
378 }
379  
380 # Text in, text out; typed binds that stay typed (karr k49, Bind conformance).
381 #
382 # DBD::DuckDB 0.16 does three things to a string that lose it, none of them
383 # with an error:
384 #
385 # - it writes a perl string to libduckdb as it sits in memory, so a string
386 # with characters above 0x7f that perl holds as latin-1 ("\x{fc}" without
387 # the utf8 flag) is invalid UTF-8 and the bind dies, and one perl holds
388 # as UTF-8 only works by accident;
389 # - it hands every VARCHAR back as the bytes libduckdb stores, never as
390 # characters, so "\x{20ac}" comes back as three characters;
391 # - bind_param takes its type from an attribute only for that one call:
392 # execute(@bind), which is what execute_for_fetch -- a populate -- calls,
393 # binds again without it, so a bytes value meant for a BLOB is sniffed
394 # into a VARCHAR and refused.
395 #
396 # The callbacks below sit in front of bind_param and the fetch methods of every
397 # statement of this connection. bind_param remembers a bind type per parameter
398 # and applies it to a later bind without one, and encodes a text value as UTF-8
399 # before the DBD sees it (a copy: the application's variable is never touched);
400 # a fetch decodes the VARCHAR and ENUM columns of the row it returns, in place,
401 # so a bound column (bind_columns) sees characters as well. BLOB columns are
402 # left as bytes. Both run the DBD's own method with the callbacks standing down and
403 # replace the dispatch (undef $_), so nothing runs twice. Bound to DBD::DuckDB <= 0.16 like
404 # the statement release above.
405 0 ▶sub _duckdb_text_callbacks {
Subroutine _duckdb_text_callbacks: not called CC=9 SCAR=45.0
406 0 my ($self, $dbh) = @_;
407  
408 0 ▶ return unless ($dbh->{Driver}{Name} // '') eq 'DuckDB';
Branch: 1 branch
BranchTrueFalseTotal
unless ($dbh->{"Driver"}{"Name"} // "") eq "DuckDB" 0 0 0
Condition0%0/2
$dbh->{"Driver"}{"Name"} // ""
l!l
00
MC/DC0%0/1
$dbh->{"Driver"}{"Name"} // ""
$dbh->{"Driver"}{"Name"}
Truth table0%0/2
A
A= $dbh->{"Driver"}{"Name"}
Aresult
11
00
409 0 ▶ return if version->parse(DBD::DuckDB->VERSION) > $_STATEMENT_LEAK_READ_UP_TO;
Branch: 1 branch
BranchTrueFalseTotal
if "version"->parse("DBD::DuckDB"->VERSION) > $_STATEMENT_LEAK_READ_UP_TO 0 0 0
410 0 ▶ return if $dbh->{private_dbio_duckdb_text_callbacks};
Branch: 1 branch
BranchTrueFalseTotal
if $dbh->{"private_dbio_duckdb_text_callbacks"} 0 0 0
411  
412 # the method itself, run again through dispatch with our callbacks standing
413 # down (the flag), and the dispatch of the original call replaced by its
414 # result
415 0 ▶ my $plain = sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
416 0 my ($sth, $method, @args) = @_;
417 0 my ($ok, @result);
418 0 my $error;
419 0 $sth->{private_dbio_duckdb_in_callback} = 1;
420 {
421 0 local $@;
422 0 $ok = eval { @result = $sth->$method(@args); 1 };
423 0 ▶ $error = $@ unless $ok;
Branch: 1 branch
BranchTrueFalseTotal
unless $ok 0 0 0
424 }
425 0 $sth->{private_dbio_duckdb_in_callback} = 0;
426 0 ▶ die $error unless $ok;
Branch: 1 branch
BranchTrueFalseTotal
unless $ok 0 0 0
427 0 undef $_;
428 0 return $result[0];
429 0 };
430  
431 0 ▶ my $bind_param = sub {
Subroutine __ANON__: not called CC=13 SCAR=52.0
432 0 my ($sth, $i, $value, $attr) = @_;
433 0 ▶ return if $sth->{private_dbio_duckdb_in_callback};
Branch: 1 branch
BranchTrueFalseTotal
if $sth->{"private_dbio_duckdb_in_callback"} 0 0 0
434 0 ▶ my $types = $sth->{private_dbio_duckdb_bind_types} ||= {};
Condition0%0/2
$sth->{"private_dbio_duckdb_bind_types"} ||= {}
l!l
00
MC/DC0%0/1
$sth->{"private_dbio_duckdb_bind_types"} ||= {}
$sth->{"private_dbio_duckdb_bind_types"}
Truth table0%0/2
A
A= $sth->{"private_dbio_duckdb_bind_types"}
Aresult
11
00
435 0 ▶ if (defined $attr and not ref $attr) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $attr and not ref $attr) { } 0 0 0
elsif (not defined $attr and defined $types->{$i}) { } 0 0 0
Condition0%0/3
defined $attr and not ref $attr
!ll&&!rl&&r
000
MC/DC0%0/2
defined $attr and not ref $attr
defined $attr not ref $attr
Truth table0%0/3
A and not(B)
A= defined $attrB= not ref $attr
ABresult
0X0
100
111
436 0 $types->{$i} = $attr;
437 }
438 ▶ elsif (not defined $attr and defined $types->{$i}) {
Condition0%0/3
not defined $attr and defined $types->{$i}
!ll&&!rl&&r
000
MC/DC0%0/2
not defined $attr and defined $types->{$i}
not defined $attr defined $types->{$i}
Truth table0%0/3
not(A) and B
A= not defined $attrB= defined $types->{$i}
ABresult
0X0
100
111
439 0 $attr = $types->{$i};
440 }
441 0 ▶ if (defined $value and not ref $value
Branch: 1 branch
BranchTrueFalseTotal
if (defined $value and not ref $value and !defined($attr) || ref $attr || $attr != &DBI::SQL_BLOB() and $value =~ /[^\x00-\x7f]/) 0 0 0
Condition0%0/15
defined $value and not ref $value and !defined($attr) || ref $attr || $attr != &DBI::SQL_BLOB() and $value =~ /[^\x00-\x7f]/
!ll&&!rl&&r
000
defined $value and not ref $value and !defined($attr) || ref $attr || $attr != &DBI::SQL_BLOB()
!ll&&!rl&&r
000
defined $value and not ref $value
!ll&&!rl&&r
000
not defined $attr or ref $attr or $attr != &DBI::SQL_BLOB()
l!l&&r!l&&!r
000
not defined $attr or ref $attr
l!l&&r!l&&!r
000
MC/DC0%0/6
defined $value and not ref $value and !defined($attr) || ref $attr || $attr != &DBI::SQL_BLOB() and $value =~ /[^\x00-\x7f]/
defined $value not ref $value not defined $attr ref $attr $attr != &DBI::SQL_BLOB() $value =~ /[^\x00-\x7f]/
Truth table0%0/9
A and not(B) and not(C) or D or E and F
A= defined $valueB= not ref $valueC= not defined $attrD= ref $attrE= $attr != &DBI::SQL_BLOB()F= $value =~ /[^\x00-\x7f]/
ABCDEFresult
0XXXXX0
10XXXX0
11000X0
111XX00
1101X00
1100100
111XX11
1101X11
1100111
442 and (not defined $attr or ref $attr or $attr != DBI::SQL_BLOB())
443 and $value =~ /[^\x00-\x7f]/) {
444 0 utf8::encode($value);
445 }
446 0 return $plain->($sth, 'bind_param', $i, $value, $attr);
447 0 };
448  
449 0 ▶ my $fetch = sub {
Subroutine __ANON__: not called CC=10 SCAR=47.0
450 0 my $sth = $_[0];
451 0 ▶ return if $sth->{private_dbio_duckdb_in_callback};
Branch: 1 branch
BranchTrueFalseTotal
if $sth->{"private_dbio_duckdb_in_callback"} 0 0 0
452 0 my $row = $plain->($sth, 'fetchrow_arrayref');
453 0 ▶ return $row unless $row;
Branch: 1 branch
BranchTrueFalseTotal
unless $row 0 0 0
454  
455 0 ▶ my $logical = $sth->{duckdb_col_logical_types} or return $row;
Branch: 1 branch
BranchTrueFalseTotal
my $logical = $sth->{"duckdb_col_logical_types"} or return $row 0 0 0
456 0 my $cache = $sth->{private_dbio_duckdb_text_columns};
457 0 ▶ unless ($cache and $cache->[0] == $logical) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($cache and $cache->[0] == $logical) 0 0 0
Condition0%0/3
$cache and $cache->[0] == $logical
!ll&&!rl&&r
000
MC/DC0%0/2
$cache and $cache->[0] == $logical
$cache $cache->[0] == $logical
Truth table0%0/3
A and B
A= $cacheB= $cache->[0] == $logical
ABresult
0X0
100
111
458 my @text = grep {
459 0 my $id = DBD::DuckDB::FFI::duckdb_get_type_id($logical->[$_]);
460 0 ▶ $id == DBD::DuckDB::Constants::DUCKDB_TYPE_VARCHAR()
Condition0%0/3
$id == DBD::DuckDB::Constants::DUCKDB_TYPE_VARCHAR() || $id == DBD::DuckDB::Constants::DUCKDB_TYPE_ENUM()
l!l&&r!l&&!r
000
MC/DC0%0/2
$id == DBD::DuckDB::Constants::DUCKDB_TYPE_VARCHAR() || $id == DBD::DuckDB::Constants::DUCKDB_TYPE_ENUM()
$id == DBD::DuckDB::Constants::DUCKDB_TYPE_VARCHAR() $id == DBD::DuckDB::Constants::DUCKDB_TYPE_ENUM()
Truth table0%0/3
A || B
A= $id == DBD::DuckDB::Constants::DUCKDB_TYPE_VARCHAR()B= $id == DBD::DuckDB::Constants::DUCKDB_TYPE_ENUM()
ABresult
1X1
011
000
461 or $id == DBD::DuckDB::Constants::DUCKDB_TYPE_ENUM()
462 } 0 .. $#$logical;
463 0 $cache = $sth->{private_dbio_duckdb_text_columns} = [ $logical, \@text ];
464 }
465 0 for my $c (@{ $cache->[1] }) {
466 0 ▶ utf8::decode($row->[$c])
Branch: 1 branch
BranchTrueFalseTotal
if defined $row->[$c] and not utf8::is_utf8($row->[$c]) 0 0 0
Condition0%0/3
defined $row->[$c] and not utf8::is_utf8($row->[$c])
!ll&&!rl&&r
000
MC/DC0%0/2
defined $row->[$c] and not utf8::is_utf8($row->[$c])
defined $row->[$c] not utf8::is_utf8($row->[$c])
Truth table0%0/3
A and not(B)
A= defined $row->[$c]B= not utf8::is_utf8($row->[$c])
ABresult
0X0
100
111
467 if defined $row->[$c] and not utf8::is_utf8($row->[$c]);
468 }
469 0 return $row;
470 0 };
471  
472 0 my %ours = (
473 bind_param => $bind_param,
474 fetch => $fetch,
475 fetchrow_arrayref => $fetch,
476 );
477  
478 0 ▶ my $callbacks = $dbh->{Callbacks} || {};
Condition0%0/2
$dbh->{'Callbacks'} || {}
l!l
00
MC/DC0%0/1
$dbh->{'Callbacks'} || {}
$dbh->{'Callbacks'}
Truth table0%0/2
A
A= $dbh->{'Callbacks'}
Aresult
11
00
479 0 ▶ my $child = $callbacks->{ChildCallbacks} || {};
Condition0%0/2
$callbacks->{'ChildCallbacks'} || {}
l!l
00
MC/DC0%0/1
$callbacks->{'ChildCallbacks'} || {}
$callbacks->{'ChildCallbacks'}
Truth table0%0/2
A
A= $callbacks->{'ChildCallbacks'}
Aresult
11
00
480 0 my %chained;
481 0 for my $method (keys %ours) {
482 0 my $mine = $ours{$method};
483 0 my $theirs = $child->{$method};
484 $chained{$method} = $theirs
485 0 ▶ ? sub { my $r = $theirs->(@_); defined $_ ? $mine->(@_) : $r }
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
defined $_ ? : 0 0 0
486 0 ▶ : $mine;
Branch: 1 branch
BranchTrueFalseTotal
$theirs ? : 0 0 0
487 }
488  
489 0 $dbh->{Callbacks} = { %$callbacks, ChildCallbacks => { %$child, %chained } };
490 0 $dbh->{private_dbio_duckdb_text_callbacks} = 1;
491  
492 0 return;
493 }
494  
495  
496 0 ▶sub _mask_error_values {
Subroutine _mask_error_values: not called CC=8 SCAR=42.8
497 0 my ($self, $text, $values, $statement) = @_;
498 0 ▶ return $text unless defined $text and !ref $text;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $text and not ref $text 0 0 0
Condition0%0/3
defined $text and not ref $text
!ll&&!rl&&r
000
MC/DC0%0/2
defined $text and not ref $text
defined $text not ref $text
Truth table0%0/3
A and not(B)
A= defined $textB= not ref $text
ABresult
0X0
100
111
499  
500 # a non-ASCII value reaches the engine as UTF-8 bytes and comes back in its
501 # text in that form, which core's net (the value as written) does not look for
502 $values = [ map {
503 0 my $v = $_;
504 (defined $v && !ref $v && $v =~ /[^\x00-\x7f]/ && utf8::is_utf8($v))
505 0 ▶ ? ($v, do { my $b = $v; utf8::encode($b); $b })
Branch: 1 branch
BranchTrueFalseTotal
defined $v && !ref($v) && $v =~ /[^\x00-\x7f]/ && utf8::is_utf8($v) ? : 0 0 0
Condition0%0/9
defined $v and not ref $v and $v =~ /[^\x00-\x7f]/ and utf8::is_utf8($v)
!ll&&!rl&&r
000
defined $v and not ref $v and $v =~ /[^\x00-\x7f]/
!ll&&!rl&&r
000
defined $v and not ref $v
!ll&&!rl&&r
000
MC/DC0%0/4
defined $v and not ref $v and $v =~ /[^\x00-\x7f]/ and utf8::is_utf8($v)
defined $v not ref $v $v =~ /[^\x00-\x7f]/ utf8::is_utf8($v)
Truth table0%0/5
A and not(B) and C and D
A= defined $vB= not ref $vC= $v =~ /[^\x00-\x7f]/D= utf8::is_utf8($v)
ABCDresult
0XXX0
10XX0
110X0
11100
11111
506 : $v;
507 0 ▶ } @{ $values // [] } ];
Condition0%0/2
$values // []
l!l
00
MC/DC0%0/1
$values // []
$values
Truth table0%0/2
A
A= $values
Aresult
11
00
508  
509 0 my ($head, $tail) = DBIO::DuckDB::Util::_split_excerpt($text, $statement);
510 0 $head = DBIO::DuckDB::Util::mask_error_details($head, $values);
511 0 return $self->next::method($head, $values, $statement) . $tail;
512 }
513  
514  
515 0 ▶sub duckdb_appender {
Subroutine duckdb_appender: not called CC=2 SCAR=17.9
Pod documented
516 0 my ($self, $table, $schema) = @_;
517 0 ▶ $self->throw_exception('Usage: $storage->duckdb_appender($table [, $schema])') unless $table;
Branch: 1 branch
BranchTrueFalseTotal
unless $table 0 0 0
518 0 return $self->_get_dbh->x_duckdb_appender($table, $schema);
519 }
520  
521  
522 0 ▶sub duckdb_arrow_fetch {
Subroutine duckdb_arrow_fetch: not called CC=3 SCAR=24.8
Pod documented
523 0 my ($self, $sql, $binds) = @_;
524 0 ▶ $self->throw_exception('Usage: $storage->duckdb_arrow_fetch($sql [, \@binds])')
Branch: 1 branch
BranchTrueFalseTotal
unless defined $sql and length $sql 0 0 0
Condition0%0/3
defined $sql and length $sql
!ll&&!rl&&r
000
MC/DC0%0/2
defined $sql and length $sql
defined $sql length $sql
Truth table0%0/3
A and B
A= defined $sqlB= length $sql
ABresult
0X0
100
111
525 unless defined $sql and length $sql;
526 0 my $dbh = $self->_get_dbh;
527 0 ▶ return try {
Subroutine __ANON__: not called CC=2 SCAR=17.9
528 # loaded here, not at the top: the module binds libduckdb's Arrow
529 # functions when it loads, and a missing library is an exception of this
530 # method, not a storage class that does not load
531 0 require DBIO::DuckDB::Arrow;
532 DBIO::DuckDB::Arrow->fetch($dbh, $sql, $binds // [],
533 0 ▶ sub { $self->_mask_error_values($_[0], $binds // [], $sql) });
Subroutine __ANON__: not called CC=2 SCAR=17.9
Condition0%0/4
$binds // []
l!l
00
$binds // []
l!l
00
MC/DC0%0/1
$binds // []
$binds
MC/DC0%0/1
$binds // []
$binds
Truth table0%0/2
A
A= $binds
Aresult
11
00
Truth table0%0/2
A
A= $binds
Aresult
11
00
534 }
535 0 ▶ catch {
Subroutine __ANON__: not called CC=1 SCAR=6.9
536 0 $self->throw_exception("$_");
537 0 };
538 }
539  
540  
541 0 ▶sub duckdb_read_csv {
Subroutine duckdb_read_csv: not called CC=4 SCAR=30.0
Pod documented
542 0 my ($self, $path, $opts) = @_;
543 0 ▶ $self->throw_exception('Usage: $storage->duckdb_read_csv($path [, \%opts])') unless $path;
Branch: 1 branch
BranchTrueFalseTotal
unless $path 0 0 0
544 0 ▶ my $sth = $self->_get_dbh->x_duckdb_read_csv($path, $opts // {});
Condition0%0/2
$opts // {}
l!l
00
MC/DC0%0/1
$opts // {}
$opts
Truth table0%0/2
A
A= $opts
Aresult
11
00
545 0 my @rows;
546 0 ▶ while (my $row = $sth->fetchrow_hashref) { push @rows, { %$row } }
Branch: 1 branch
BranchTrueFalseTotal
if (my $row = $sth->fetchrow_hashref) 0 0 0
547 0 return \@rows;
548 }
549  
550  
551 0 ▶sub duckdb_read_parquet {
Subroutine duckdb_read_parquet: not called CC=2 SCAR=17.9
Pod documented
552 0 my ($self, $path) = @_;
553 0 ▶ $self->throw_exception('Usage: $storage->duckdb_read_parquet($path)') unless $path;
Branch: 1 branch
BranchTrueFalseTotal
unless $path 0 0 0
554 0 return $self->_get_dbh->selectall_arrayref(
555 'SELECT * FROM read_parquet(?)',
556 { Slice => {} },
557 $path,
558 );
559 }
560  
561  
562 0 ▶sub duckdb_read_json {
Subroutine duckdb_read_json: not called CC=4 SCAR=30.0
Pod documented
563 0 my ($self, $path, $opts) = @_;
564 0 ▶ $self->throw_exception('Usage: $storage->duckdb_read_json($path [, \%opts])') unless $path;
Branch: 1 branch
BranchTrueFalseTotal
unless $path 0 0 0
565 0 ▶ my $sth = $self->_get_dbh->x_duckdb_read_json($path, $opts // {});
Condition0%0/2
$opts // {}
l!l
00
MC/DC0%0/1
$opts // {}
$opts
Truth table0%0/2
A
A= $opts
Aresult
11
00
566 0 my @rows;
567 0 ▶ while (my $row = $sth->fetchrow_hashref) { push @rows, { %$row } }
Branch: 1 branch
BranchTrueFalseTotal
if (my $row = $sth->fetchrow_hashref) 0 0 0
568 0 return \@rows;
569 }
570  
571  
572 0 ▶sub duckdb_version {
Subroutine duckdb_version: not called CC=1 SCAR=6.9
Pod documented
573 0 my $self = shift;
574 0 return $self->_get_dbh->x_duckdb_version;
575 }
576  
577  
578 0 ▶sub duckdb_install_extension {
Subroutine duckdb_install_extension: not called CC=4 SCAR=30.0
Pod documented
579 0 my ($self, $name, %opts) = @_;
580 0 ▶ $self->throw_exception('extension name required') unless $name;
Branch: 1 branch
BranchTrueFalseTotal
unless $name 0 0 0
581 0 ▶ $name =~ /\A[A-Za-z_][A-Za-z0-9_]*\z/
Branch: 1 branch
BranchTrueFalseTotal
$name =~ /\A[A-Za-z_][A-Za-z0-9_]*\z/ or $self->throw_exception("invalid extension name: $name") 0 0 0
582 or $self->throw_exception("invalid extension name: $name");
583 0 my $dbh = $self->_get_dbh;
584 0 $dbh->do("INSTALL $name");
585 0 ▶ $dbh->do("LOAD $name") if $opts{load};
Branch: 1 branch
BranchTrueFalseTotal
if $opts{"load"} 0 0 0
586 0 return 1;
587 }
588  
589  
590 0 ▶sub _extension_activation {
Subroutine _extension_activation: not called CC=2 SCAR=17.9
591 0 my ($self, $name) = @_;
592 0 my $none = { load => [], search_path => [] };
593  
594 0 ▶ my $list = DBIO::DuckDB::Extension->resolve_list($self->schema)
Branch: 1 branch
BranchTrueFalseTotal
my $list = "DBIO::DuckDB::Extension"->resolve_list($self->schema) or return $none 0 0 0
595 or return $none;
596  
597 0 return DBIO::Extension::Protocol->new(
598 entries => [ $list->entries ],
599 driver => {},
600 connection_schema => $name,
601 restricted => 0,
602 )->activation;
603 }
604  
605  
606 0 ▶sub _activate_extensions {
Subroutine _activate_extensions: not called CC=2 SCAR=17.9
607 0 my ($self, $activation) = @_;
608 0 $self->_do_query($_) for $self->_extension_activation_sql($activation);
609 0 return $self;
610 }
611  
612  
613 0 ▶sub _extension_activation_sql {
Subroutine _extension_activation_sql: not called CC=6 SCAR=37.4
614 0 my ($self, $activation) = @_;
615  
616 $self->throw_exception('DuckDB extensions have no search_path; an extension asked for '
617 0 . join(', ', @{ $activation->{search_path} }))
618 0 ▶ if @{ $activation->{search_path} || [] };
Branch: 1 branch
BranchTrueFalseTotal
if @{$activation->{"search_path"} or [];} 0 0 0
Condition0%0/2
$activation->{'search_path'} || []
l!l
00
MC/DC0%0/1
$activation->{'search_path'} || []
$activation->{'search_path'}
Truth table0%0/2
A
A= $activation->{'search_path'}
Aresult
11
00
619  
620 0 my @sql;
621 0 ▶ for my $name (@{ $activation->{load} || [] }) {
Condition0%0/2
$activation->{'load'} || []
l!l
00
MC/DC0%0/1
$activation->{'load'} || []
$activation->{'load'}
Truth table0%0/2
A
A= $activation->{'load'}
Aresult
11
00
622 0 ▶ $name =~ /\A[A-Za-z_][A-Za-z0-9_]*\z/
Branch: 1 branch
BranchTrueFalseTotal
$name =~ /\A[A-Za-z_][A-Za-z0-9_]*\z/ or $self->throw_exception("invalid extension name: $name") 0 0 0
623 or $self->throw_exception("invalid extension name: $name");
624 0 push @sql, "INSTALL $name", "LOAD $name";
625 }
626 0 return @sql;
627 }
628  
629  
630 0 ▶sub duckdb_checkpoint {
Subroutine duckdb_checkpoint: not called CC=1 SCAR=6.9
Pod documented
631 0 my $self = shift;
632 0 $self->_get_dbh->do('CHECKPOINT');
633 0 return 1;
634 }
635  
636  
637 0 ▶sub quack_serve {
Subroutine quack_serve: not called CC=3 SCAR=24.8
Pod documented
638 0 my ($self, $addr, %opts) = @_;
639 0 ▶ $addr //= 'quack:localhost';
Condition0%0/2
$addr //= "quack:localhost"
l!l
00
MC/DC0%0/1
$addr //= "quack:localhost"
$addr
Truth table0%0/2
A
A= $addr
Aresult
11
00
640 0 _validate_quack_addr($self, $addr);
641  
642 0 $self->duckdb_install_extension('quack', load => 1);
643  
644 0 my $dbh = $self->_get_dbh;
645  
646 0 ▶ if (defined $opts{token}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opts{'token'}) { } 0 0 0
647 0 _validate_quack_token($self, $opts{token});
648 0 my $token = $opts{token};
649 0 my $tok_lit = $opts{token};
650 0 $tok_lit =~ s/'/''/g;
651 0 my $addr_lit = $addr;
652 0 $addr_lit =~ s/'/''/g;
653 0 $dbh->do("CALL quack_serve('$addr_lit', token => '$tok_lit')");
654 }
655 else {
656 0 my $addr_lit = $addr;
657 0 $addr_lit =~ s/'/''/g;
658 0 $dbh->do("CALL quack_serve('$addr_lit')");
659 }
660  
661 0 return 1;
662 }
663  
664  
665 0 ▶sub quack_attach {
Subroutine quack_attach: not called CC=6 SCAR=37.4
Pod documented
666 0 my ($self, $addr, %opts) = @_;
667 0 ▶ $self->throw_exception('Usage: $storage->quack_attach($addr, as => $name, ...)') unless $addr;
Branch: 1 branch
BranchTrueFalseTotal
unless $addr 0 0 0
668 0 _validate_quack_addr($self, $addr);
669  
670 my $as = $opts{as}
671 0 ▶ or $self->throw_exception('quack_attach: "as" option (catalog alias) is required');
Branch: 1 branch
BranchTrueFalseTotal
my $as = $opts{"as"} or $self->throw_exception("quack_attach: \"as\" option (catalog alias) is required") 0 0 0
672 0 ▶ $as =~ /\A[A-Za-z_][A-Za-z0-9_]*\z/
Branch: 1 branch
BranchTrueFalseTotal
$as =~ /\A[A-Za-z_][A-Za-z0-9_]*\z/ or $self->throw_exception("quack_attach: invalid catalog alias '${as}'") 0 0 0
673 or $self->throw_exception("quack_attach: invalid catalog alias '$as'");
674  
675 0 $self->duckdb_install_extension('quack', load => 1);
676  
677 0 my $addr_lit = $addr;
678 0 $addr_lit =~ s/'/''/g;
679 0 my $attach_sql = "ATTACH '$addr_lit' AS $as";
680 0 ▶ $attach_sql .= ' (READ_ONLY)' if $opts{read_only};
Branch: 1 branch
BranchTrueFalseTotal
if $opts{"read_only"} 0 0 0
681  
682 0 ▶ if (defined $opts{token}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opts{'token'}) { } 0 0 0
683 0 _validate_quack_token($self, $opts{token});
684 0 my $tok_lit = $opts{token};
685 0 $tok_lit =~ s/'/''/g;
686 0 my $secret = _quack_secret_name($self, $as);
687  
688 # One transaction, because DuckDB's secret store is transactional and a
689 # leftover is not harmless: of two secrets with the same scope the name
690 # that sorts first wins, so the secret of a failed attach would shadow
691 # the token of a later alias for the same address. The rollback also
692 # puts back a secret this CREATE OR REPLACE overwrote (karr k32).
693 #
694 # DuckDB quotes the failed statement in its error text, and the CREATE
695 # SECRET one holds the token (a NUL in it fails the parse, and the parser
696 # repeats it): the token goes through mask_connect_secrets as the
697 # password of the connect info (karr k42).
698 0 ▶ try {
Subroutine __ANON__: not called CC=1 SCAR=6.9
699 0 ▶ $self->txn_do(sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
700 0 my $dbh = $self->_get_dbh;
701 0 $dbh->do("CREATE OR REPLACE SECRET $secret (TYPE quack, TOKEN '$tok_lit', SCOPE '$addr_lit')");
702 0 $dbh->do($attach_sql);
703 0 });
704 }
705 0 ▶ catch {
Subroutine __ANON__: not called CC=2 SCAR=17.9
706 0 my $msg = mask_connect_secrets("$_", [ undef, undef, $opts{token}, {} ]);
707 0 ▶ $msg .= "\n" unless $msg =~ /\n\z/;
Branch: 1 branch
BranchTrueFalseTotal
unless $msg =~ /\n\z/ 0 0 0
708 0 die $msg;
709 0 };
710 }
711 else {
712 0 $self->_get_dbh->do($attach_sql);
713 }
714  
715 0 return 1;
716 }
717  
718  
719 0 ▶sub quack_detach {
Subroutine quack_detach: not called CC=5 SCAR=34.0
Pod documented
720 0 my ($self, $name) = @_;
721 0 ▶ $self->throw_exception('Usage: $storage->quack_detach($name)') unless defined $name;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $name 0 0 0
722 0 ▶ $name =~ /\A[A-Za-z_][A-Za-z0-9_]*\z/
Branch: 1 branch
BranchTrueFalseTotal
$name =~ /\A[A-Za-z_][A-Za-z0-9_]*\z/ or $self->throw_exception("quack_detach: invalid catalog name '${name}'") 0 0 0
723 or $self->throw_exception("quack_detach: invalid catalog name '$name'");
724 0 my $dbh = $self->_get_dbh;
725 # Cleaning up wins over the DETACH result: the DROP runs even when the
726 # DETACH died, and the DETACH error is the one reported, also when the
727 # DROP dies too (karr k36).
728 0 my $detached = eval { $dbh->do("DETACH $name"); 1 };
729 0 my $detach_error = $@;
730 0 my $dropped = eval {
731 0 $dbh->do('DROP SECRET IF EXISTS ' . _quack_secret_name($self, $name));
732 0 1;
733 };
734 0 my $drop_error = $@;
735 0 ▶ die $detach_error unless $detached;
Branch: 1 branch
BranchTrueFalseTotal
unless $detached 0 0 0
736 0 ▶ die $drop_error unless $dropped;
Branch: 1 branch
BranchTrueFalseTotal
unless $dropped 0 0 0
737 0 return 1;
738 }
739  
740  
741 0 ▶sub connect_call_quack_attach {
Subroutine connect_call_quack_attach: not called CC=3 SCAR=24.8
Pod documented
742 0 my ($self, $args) = @_;
743 0 ▶ $self->throw_exception('connect_call_quack_attach: hashref argument required')
Branch: 1 branch
BranchTrueFalseTotal
unless ref $args eq "HASH" 0 0 0
744 unless ref $args eq 'HASH';
745 my $addr = $args->{addr}
746 0 ▶ or $self->throw_exception('connect_call_quack_attach: "addr" required in args hashref');
Branch: 1 branch
BranchTrueFalseTotal
my $addr = $args->{"addr"} or $self->throw_exception("connect_call_quack_attach: \"addr\" required in args hashref") 0 0 0
747 0 return $self->quack_attach($addr, %{ $args }{ grep { $_ ne 'addr' } keys %$args });
748 }
749  
750 # ---- private helpers ----
751  
752 0 ▶sub _validate_quack_addr {
Subroutine _validate_quack_addr: not called CC=4 SCAR=30.0
753 0 my ($self, $addr) = @_;
754 0 ▶ $addr =~ /^quack:/
Branch: 1 branch
BranchTrueFalseTotal
$addr =~ /^quack:/ or $self->throw_exception(q[quack address must start with 'quack:' (got: ] . mask_connect_secrets($addr) . ')') 0 0 0
755 or $self->throw_exception(
756 "quack address must start with 'quack:' (got: " . mask_connect_secrets($addr) . ')'
757 );
758 0 ▶ $addr =~ /'/
Branch: 1 branch
BranchTrueFalseTotal
$addr =~ /'/ and $self->throw_exception("quack address must not contain single quotes") 0 0 0
759 and $self->throw_exception("quack address must not contain single quotes");
760 0 ▶ $addr =~ /\n/
Branch: 1 branch
BranchTrueFalseTotal
$addr =~ /\n/ and $self->throw_exception("quack address must not contain newlines") 0 0 0
761 and $self->throw_exception("quack address must not contain newlines");
762 0 return 1;
763 }
764  
765 0 ▶sub _validate_quack_token {
Subroutine _validate_quack_token: not called CC=2 SCAR=17.9
766 0 my ($self, $tok) = @_;
767 0 ▶ $tok =~ /\n/
Branch: 1 branch
BranchTrueFalseTotal
$tok =~ /\n/ and $self->throw_exception("quack token must not contain newlines") 0 0 0
768 and $self->throw_exception("quack token must not contain newlines");
769 0 return 1;
770 }
771  
772 # Name of the secret quack_attach creates for an alias and quack_detach
773 # drops again. Both validate the alias as an identifier before asking.
774 0 ▶sub _quack_secret_name {
Subroutine _quack_secret_name: not called CC=1 SCAR=6.9
775 0 my ($self, $alias) = @_;
776 0 return "dbio_quack_$alias";
777 }
778  
779  
780 # Types a double cannot represent exactly, keyed by the lower-cased data_type
781 # with any size/precision suffix removed (so 'decimal(38,10)' arrives here as
782 # 'decimal'). Spellings cover both what DBIO::DuckDB::DDL accepts and what
783 # DuckDB itself reports back through introspection.
784 33 my %_BIND_AS_STRING = map { $_ => 1 } qw(
785 bigint bigserial int8 int64 long
786 ubigint uint64
787 hugeint uhugeint int128 uint128
788 varint bignum
789 decimal numeric dec money
790 );
791  
792 # Text and binary columns: DBD::DuckDB sniffs a value that looks like a number
793 # or a boolean ('01234', '5.0', '1') and binds it as one, so a text column hands
794 # back 1234.0 for '01234' and a comparison fails with a conversion error; a
795 # value with a NUL or a byte string fails to bind as text. The column's own
796 # type decides instead (karr k49, Bind conformance).
797 33 my %_BIND_AS_TEXT = map { $_ => 1 } qw(
798 varchar char bpchar text string nvarchar nchar clob json
799 );
800 33 my %_BIND_AS_BLOB = map { $_ => 1 } qw(
801 blob bytea binary varbinary longblob mediumblob tinyblob
802 );
803  
804 0 ▶sub bind_attribute_by_data_type {
Subroutine bind_attribute_by_data_type: not called CC=5 SCAR=34.0
Pod documented
805 0 my ($self, $data_type) = @_;
806  
807 0 ▶ return undef unless defined $data_type;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $data_type 0 0 0
808  
809 0 (my $type = lc $data_type) =~ s/\(.*//s;
810 0 $type =~ s/\A\s+//;
811 0 $type =~ s/\s+\z//;
812  
813 0 ▶ return DBI::SQL_BIGINT() if $_BIND_AS_STRING{$type};
Branch: 1 branch
BranchTrueFalseTotal
if $_BIND_AS_STRING{$type} 0 0 0
814 0 ▶ return DBI::SQL_VARCHAR() if $_BIND_AS_TEXT{$type};
Branch: 1 branch
BranchTrueFalseTotal
if $_BIND_AS_TEXT{$type} 0 0 0
815 0 ▶ return DBI::SQL_BLOB() if $_BIND_AS_BLOB{$type};
Branch: 1 branch
BranchTrueFalseTotal
if $_BIND_AS_BLOB{$type} 0 0 0
816 0 return undef;
817 }
818  
819  
820 0 ▶sub _get_server_version {
Subroutine _get_server_version: not called CC=2 SCAR=17.9
821 0 my $self = shift;
822  
823 0 my $version = $self->next::method(@_);
824  
825 0 ▶ $version =~ s/^v(?=[0-9])// if defined $version;
Branch: 1 branch
BranchTrueFalseTotal
if defined $version 0 0 0
826  
827 0 return $version;
828 }
829  
830  
831 33 1;
832  
833 __END__
« DuckDB/Introspect/Tables.pm ↑ Summary DuckDB/Introspect/Uniques.pm »