blib/lib/DBIx/QuickORM/Dialect/DuckDB.pm

filter: Statementstmt 58.1% 57 / 98 Branchbran 56.2% 9 / 16 Conditioncond 43.4% 10 / 23 MC/DCmcdc 27.2% 3 / 11 Subroutinesub 70.0% 21 / 30 Podpod 100% 21 / 21 totaltotal 60.8% 121 / 199 CC 22 SCAR 16.6
CC
22
Cov
55%
scan_cache_key
20.0
_unique_rows
12.0
_scope
12.0
CRAP 5.3
« blib/lib/DBIx/QuickORM/Source/Query.pm ↑ Summary blib/lib/DBIx/QuickORM/Source/Table.pm »
1 package DBIx::QuickORM::Dialect::DuckDB;
2 10 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 10 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 10 our $VERSION = '0.000033';
6  
7 10 ▶use Carp qw/croak/;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 10 ▶use parent 'DBIx::QuickORM::Dialect';
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 =pod
12  
13 =encoding UTF-8
14  
15 =head1 NAME
16  
17 DBIx::QuickORM::Dialect::DuckDB - DuckDB dialect.
18  
19 =head1 DESCRIPTION
20  
21 DuckDB through L<DBD::DuckDB>. It keeps the base defaults of
22 L<DBIx::QuickORM::Dialect>, but has no savepoints (so transactions cannot
23 nest), allows no second connection, and classifies errors by message. The
24 namespace is C<current_schema()>.
25  
26 DBD::DuckDB exchanges bytes, so this dialect decodes fetched text and
27 upgrades text it sends.
28  
29 =head1 SYNOPSIS
30  
31 my $dialect = DBIx::QuickORM::Dialect->for_dbh($dbh); # this class for DBD::DuckDB
32  
33 =head1 PUBLIC METHODS
34  
35 =over 4
36  
37 =item $bool = $dialect->supports_savepoints
38  
39 False.
40  
41 =item $bool = $dialect->supports_second_connection($dbh)
42  
43 False: DuckDB holds an exclusive lock on its database file, and an in-memory
44 database belongs to its one connection.
45  
46 =item $n = $dialect->bind_limit
47  
48 7000.
49  
50 DuckDB sets no limit, but bulk inserts measured about 30% slower at 65535
51 binds a statement than at 3,000 to 7,000.
52  
53 =back
54  
55 =cut
56  
57 1 ▶sub supports_savepoints { 0 }
Subroutine supports_savepoints: called CC=1 SCAR=0.0
Pod documented
58 1 ▶sub supports_second_connection { 0 }
Subroutine supports_second_connection: called CC=1 SCAR=0.0
Pod documented
59 1 ▶sub bind_limit { 7000 }
Subroutine bind_limit: called CC=1 SCAR=0.0
Pod documented
60  
61 =pod
62  
63 =over 4
64  
65 =item $sql = $dialect->identity_sql($table, \%field, $inline_pk)
66  
67 C<DEFAULT nextval(...)> from a temporary sequence named for the table and
68 field, which C<create_table_sql> creates first and C<drop_sql>
69 drops after the table. DuckDB has no identity columns.
70  
71 my $default = $dialect->identity_sql('drafts', {name => 'id'}, 0);
72 # DEFAULT nextval('drafts_id_seq')
73  
74  
75 =item $sql = $dialect->generated_sql($expression)
76  
77 C<GENERATED ALWAYS AS ($expression) VIRTUAL>: DuckDB stores no generated
78 column.
79  
80 =item $sql = $dialect->fk_action_sql($action)
81  
82 Croaks: DuckDB has no foreign key actions.
83  
84 =item @sql = $dialect->extension_sql($name)
85  
86 C<INSTALL> and C<LOAD> the extension.
87  
88 =item $sql = $dialect->add_foreign_key_sql($table, \%fk)
89  
90 Croaks: DuckDB cannot add a foreign key to a table already created, so
91 tables that reference each other cannot be built.
92  
93 =item @sql = $dialect->drop_sql($name, $kind, \%spec, temp => 1)
94  
95 As inherited, then a C<DROP SEQUENCE> for each identity field of C<%spec>.
96  
97 my $spec = {
98 columns => [
99 {
100 name => 'id',
101 db_type => 'integer',
102 identity => 1,
103 },
104 ],
105 pk => ['id'],
106 };
107  
108 $dbh->do($_) for $dialect->create_table_sql('drafts', $spec, temp => 1); # the sequence, then the table
109  
110 my @drop = $dialect->drop_sql('drafts', 'table', $spec, temp => 1);
111 # ('DROP TABLE "drafts"', 'DROP SEQUENCE "drafts_id_seq"')
112 $dbh->do($_) for @drop;
113  
114  
115 =item $sql = $dialect->quote_binary($dbh, $bytes)
116  
117 A C<BLOB> literal of C<\xHH> escapes.
118  
119 =back
120  
121 =cut
122  
123 2 ▶sub identity_sql { "DEFAULT nextval('" . ($_[0]->_sequence_name($_[1], $_[2]->{name}) =~ s/'/''/gr) . "')" }
Subroutine identity_sql: called CC=1 SCAR=0.0
Pod documented
124 1 ▶sub generated_sql { "GENERATED ALWAYS AS ($_[1]) VIRTUAL" }
Subroutine generated_sql: called CC=1 SCAR=0.0
Pod documented
125 1 ▶sub extension_sql { my $q = $_[0]->quote_identifier($_[1]); ("INSTALL $q", "LOAD $q") }
Subroutine extension_sql: called CC=1 SCAR=0.0
Pod documented
126 1 ▶sub add_foreign_key_sql { croak "DuckDB cannot add a foreign key to an existing table, so tables that reference each other cannot be built" }
Subroutine add_foreign_key_sql: called CC=1 SCAR=0.0
Pod documented
127 1 ▶sub fk_action_sql { croak "DuckDB has no foreign key actions, so 'on_delete' and 'on_update' cannot be used" }
Subroutine fk_action_sql: called CC=1 SCAR=0.0
Pod documented
128  
129 2 ▶sub quote_binary {
Subroutine quote_binary: called CC=1 SCAR=0.0
Pod documented
130 2 "'" . join('' => map { sprintf('\\x%02X', ord) } split // => $_[2]) . "'::BLOB";
131 }
132  
133 1 ▶sub drop_sql {
Subroutine drop_sql: called CC=1 SCAR=0.0
Pod documented
134 1 my $self = shift;
135 1 my ($name, $kind, $spec) = @_;
136 1 return ($self->SUPER::drop_sql(@_), map { "DROP SEQUENCE " . $self->quote_identifier($self->_sequence_name($name, $_->{name})) } $self->_identity_columns($spec));
137 }
138  
139 =pod
140  
141 =over 4
142  
143 =item $key = $dialect->scan_cache_key($dbh, $namespace)
144  
145 The database file's path and the namespace, or undef for an in-memory
146 database, which is never cached.
147  
148 =back
149  
150 =cut
151  
152 0 ▶sub scan_cache_key {
Subroutine scan_cache_key: not called CC=4 SCAR=30.0
Pod documented
153 0 my $self = shift;
154 0 my ($dbh, $namespace) = @_;
155 0 my ($path) = $dbh->selectrow_array('SELECT path FROM duckdb_databases() WHERE database_name = current_database()');
156 0 ▶ return undef unless defined($path) && length($path);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $path and length $path 0 0 0
Condition0%0/3
defined $path and length $path
!ll&&!rl&&r
000
MC/DC0%0/2
defined $path and length $path
defined $path length $path
Truth table0%0/3
A and B
A= defined $pathB= length $path
ABresult
0X0
100
111
157 0 ▶ return join "\0" => 'DuckDB', $path, $namespace // '';
Condition0%0/2
$namespace // ""
l!l
00
MC/DC0%0/1
$namespace // ""
$namespace
Truth table0%0/2
A
A= $namespace
Aresult
11
00
158 }
159  
160 =pod
161  
162 =over 4
163  
164 =item $dialect->init_session($dbh, $time_zone)
165  
166 C<SET TimeZone>. DuckDB's sessions are UTF-8 already. The text conversion is
167 done by C<text_from_db> and C<text_to_db>.
168  
169 =back
170  
171 =cut
172  
173 1 ▶sub init_session {
Subroutine init_session: called CC=1 SCAR=0.0
Pod documented
174 1 my $self = shift;
175 1 my ($dbh, $time_zone) = @_;
176 1 return $dbh->do('SET TimeZone = ' . $dbh->quote($time_zone));
177 }
178  
179 =pod
180  
181 =over 4
182  
183 =item $dialect->text_from_db(@values)
184  
185 Decodes each value holding non-ASCII bytes from UTF-8, in place. DBD::DuckDB
186 returns text as bytes.
187  
188 =back
189  
190 =cut
191  
192 3 ▶sub text_from_db {
Subroutine text_from_db: called CC=5 SCAR=16.1
Pod documented
193 3 my $self = shift;
194 3 ▶ for (@_) { utf8::decode($_) if defined($_) && !ref($_) && m/[^\x00-\x7F]/ }
Branch: 1 branch
BranchTrueFalseTotal
if defined $_ and not ref $_ and /[^\x00-\x7F]/ 3 8 11
Condition100%6/6
defined $_ and not ref $_ and /[^\x00-\x7F]/
!ll&&!rl&&r
353
defined $_ and not ref $_
!ll&&!rl&&r
218
MC/DC100%3/3
defined $_ and not ref $_ and /[^\x00-\x7F]/
defined $_ not ref $_ /[^\x00-\x7F]/
Truth table100%4/4
A and not(B) and C
A= defined $_B= not ref $_C= /[^\x00-\x7F]/
ABCresult
0XX0
10X0
1100
1111
195 3 return;
196 }
197  
198 =pod
199  
200 =over 4
201  
202 =item $dialect->text_to_db(@values)
203  
204 Upgrades each value holding non-ASCII characters in place, so its bytes are
205 UTF-8.
206  
207 DBD::DuckDB sends a string's internal bytes as they are, which for a Latin-1
208 string that was never upgraded are not UTF-8.
209  
210 =back
211  
212 =cut
213  
214 1 ▶sub text_to_db {
Subroutine text_to_db: called CC=5 SCAR=19.2
Pod documented
215 1 my $self = shift;
216 1 ▶ for (@_) { utf8::upgrade($_) if defined($_) && !ref($_) && m/[^\x00-\x7F]/ }
Branch: 1 branch
BranchTrueFalseTotal
if defined $_ and not ref $_ and /[^\x00-\x7F]/ 1 0 1
Condition33%2/6
defined $_ and not ref $_ and /[^\x00-\x7F]/
!ll&&!rl&&r
001
defined $_ and not ref $_
!ll&&!rl&&r
001
MC/DC0%0/3
defined $_ and not ref $_ and /[^\x00-\x7F]/
defined $_ not ref $_ /[^\x00-\x7F]/
Truth table25%1/4
A and not(B) and C
A= defined $_B= not ref $_C= /[^\x00-\x7F]/
ABCresult
0XX0
10X0
1100
1111
217 1 return;
218 }
219  
220 =pod
221  
222 =over 4
223  
224 =item $kind = $dialect->error_kind($native, $state, $errstr)
225  
226 The kind from the error text, since DBD::DuckDB reports the same code and
227 SQLSTATE for every error.
228  
229 $dbh->do('CREATE TEMPORARY TABLE notes (body TEXT NOT NULL)');
230  
231 eval { $dbh->do('INSERT INTO notes VALUES (NULL)') };
232 my $kind = $dialect->error_kind($dbh->err, $dbh->state, $dbh->errstr); # 'not_null_violation'
233  
234  
235 C<Constraint Error> messages give the constraint kinds, and a
236 C<TransactionContext Error> naming a conflict gives
237 C<serialization_failure>. Anything else is undef.
238  
239 =back
240  
241 =cut
242  
243 10 my @MESSAGE_KIND = (
244 [qr/Constraint Error: Duplicate key .* violates (?:primary key|unique) constraint/ => 'unique_violation'],
245 [qr/Constraint Error: PRIMARY KEY or UNIQUE constraint violation/ => 'unique_violation'],
246 [qr/Constraint Error: Violates foreign key constraint/ => 'foreign_key_violation'],
247 [qr/Constraint Error: NOT NULL constraint failed/ => 'not_null_violation'],
248 [qr/Constraint Error: CHECK constraint failed/ => 'check_violation'],
249 [qr/TransactionContext Error: .*\bconflict\b/i => 'serialization_failure'],
250 );
251  
252 10 ▶sub error_kind {
Subroutine error_kind: called CC=4 SCAR=13.9
Pod documented
253 10 my $self = shift;
254 10 my (undef, undef, $errstr) = @_;
255 10 ▶ return undef unless defined $errstr;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $errstr 1 9 10
256  
257 9 for my $pair (@MESSAGE_KIND) {
258 34 ▶ return $pair->[1] if $errstr =~ $pair->[0];
Branch: 1 branch
BranchTrueFalseTotal
if $errstr =~ /$pair->[0]/ 8 26 34
259 }
260  
261 1 return undef;
262 }
263  
264 =pod
265  
266 =over 4
267  
268 =item $sql = $dialect->tables_sql($dbh, $temp)
269  
270 =item $sql = $dialect->fields_sql($dbh, $temp)
271  
272 =item $sql = $dialect->pk_sql($dbh, $temp)
273  
274 =item $sql = $dialect->unique_sql($dbh, $temp)
275  
276 =item $sql = $dialect->fk_sql($dbh, $temp)
277  
278 The scan queries (see L<C<scan>|DBIx::QuickORM::Dialect/"PUBLIC METHODS">),
279 over C<duckdb_tables()>, C<duckdb_views()>, C<duckdb_columns()>, and
280 C<duckdb_constraints()> in C<current_database()> and C<current_schema()>, or
281 for the one temporary object named, in the C<temp> database.
282  
283 A column the database fills has a default, such as C<nextval('seq')>.
284 No column is marked as refusing values: the catalog does not mark generated
285 columns, and DuckDB allows no key on one.
286  
287 Unique keys come from C<UNIQUE> constraints and from unique indexes (see
288 C<_unique_rows>).
289  
290 =back
291  
292 =cut
293  
294 0 ▶sub tables_sql {
Subroutine tables_sql: not called CC=1 SCAR=6.9
Pod documented
295 0 my $self = shift;
296 0 my ($dbh, $temp) = @_;
297 0 my $scope = $self->_scope($dbh, $temp);
298 0 my $views = $self->_scope($dbh, $temp, 'view_name');
299 0 return <<" EOT";
300 SELECT table_name, NULL
301 FROM duckdb_tables()
302 $scope
303 AND NOT internal
304 UNION ALL
305 SELECT view_name, 'view'
306 FROM duckdb_views()
307 $views
308 AND NOT internal
309 EOT
310 }
311  
312 0 ▶sub fields_sql {
Subroutine fields_sql: not called CC=1 SCAR=6.9
Pod documented
313 0 my $self = shift;
314 0 my $scope = $self->_scope(@_);
315 0 return <<" EOT";
316 SELECT table_name, column_name, data_type, NULL,
317 CASE WHEN upper(column_default) <> 'NULL' THEN 1 END,
318 NULL
319 FROM duckdb_columns()
320 $scope
321 EOT
322 }
323  
324 0 ▶sub pk_sql {
Subroutine pk_sql: not called CC=1 SCAR=6.9
Pod documented
325 0 my $self = shift;
326 0 my $scope = $self->_scope(@_);
327 0 return <<" EOT";
328 SELECT table_name, unnest(constraint_column_names)
329 FROM duckdb_constraints()
330 $scope
331 AND constraint_type = 'PRIMARY KEY'
332 EOT
333 }
334  
335 0 ▶sub unique_sql {
Subroutine unique_sql: not called CC=1 SCAR=6.9
Pod documented
336 0 my $self = shift;
337 0 my $scope = $self->_scope(@_);
338 0 return <<" EOT";
339 SELECT table_name, constraint_index, unnest(constraint_column_names)
340 FROM duckdb_constraints()
341 $scope
342 AND constraint_type = 'UNIQUE'
343 EOT
344 }
345  
346 =pod
347  
348 =over 4
349  
350 =item $sql = $dialect->unique_index_sql($dbh, $temp)
351  
352 The C<(table, index, expressions)> rows of the unique indexes, from
353 C<duckdb_indexes()>.
354  
355 =back
356  
357 =cut
358  
359 0 ▶sub unique_index_sql {
Subroutine unique_index_sql: not called CC=1 SCAR=6.9
Pod documented
360 0 my $self = shift;
361 0 my $scope = $self->_scope(@_);
362 0 return <<" EOT";
363 SELECT table_name, index_name, expressions
364 FROM duckdb_indexes()
365 $scope
366 AND is_unique
367 EOT
368 }
369  
370 0 ▶sub fk_sql {
Subroutine fk_sql: not called CC=1 SCAR=6.9
Pod documented
371 0 my $self = shift;
372 0 my $scope = $self->_scope(@_);
373 0 return <<" EOT";
374 SELECT table_name, constraint_index, unnest(constraint_column_names), referenced_table, unnest(referenced_column_names)
375 FROM duckdb_constraints()
376 $scope
377 AND constraint_type = 'FOREIGN KEY'
378 EOT
379 }
380  
381 =head1 PRIVATE METHODS
382  
383 =over 4
384  
385 =item \@rows = $dialect->_unique_rows($dbh, $temp)
386  
387 The unique constraints' rows, then a C<(table, "index $name", field)> row for
388 each field of each unique index.
389  
390 DuckDB reports an index's fields as a string such as C<[b, a]>, quoting a
391 name that needs it (C<['"we ird"']>). Only a plain list of such names is
392 read. Anything else, such as an expression (C<['(lower(c))']>), gives one
393 undef field, so the scan leaves the index out.
394  
395 =back
396  
397 =cut
398  
399 # A bracketed list of bare names, or names in '"..."' holding no quote or comma.
400 10 my $INDEX_FIELDS = qr/^\[((?:\w+|'"[^"',]+"')(?:, (?:\w+|'"[^"',]+"'))*)\]\z/;
401  
402 0 ▶sub _unique_rows {
Subroutine _unique_rows: not called CC=3 SCAR=24.8
403 0 my $self = shift;
404 0 my ($dbh, $temp) = @_;
405  
406 0 my @rows = @{$self->SUPER::_unique_rows($dbh, $temp)};
407 0 for my $index (@{$self->_rows($dbh, $self->unique_index_sql($dbh, $temp))}) {
408 0 my ($table, $name, $expressions) = @$index;
409 0 ▶ my @fields = $expressions =~ $INDEX_FIELDS ? map { s/^'"(.*)"'\z/$1/r } split /, / => $1 : (undef);
Branch: 1 branch
BranchTrueFalseTotal
$expressions =~ /$INDEX_FIELDS/ ? : 0 0 0
410 0 push @rows => map { [$table, "index $name", $_] } @fields;
411 }
412  
413 0 return \@rows;
414 }
415  
416 =pod
417  
418 =over 4
419  
420 =item @sql = $dialect->_sequence_sql($table, \%spec, $temp)
421  
422 A C<CREATE SEQUENCE> (temporary with C<$temp>) for each identity field.
423  
424 =item $name = $dialect->_sequence_name($table, $field)
425  
426 =item @columns = $dialect->_identity_columns(\%spec)
427  
428 The sequence behind an identity column, and the spec's identity columns.
429  
430 =back
431  
432 =cut
433  
434 3 ▶sub _sequence_sql {
Subroutine _sequence_sql: called CC=2 SCAR=6.9
435 3 my $self = shift;
436 3 my ($table, $spec, $temp) = @_;
437 3 ▶ my $create = $temp ? 'CREATE TEMPORARY SEQUENCE' : 'CREATE SEQUENCE';
Branch: 1 branch
BranchTrueFalseTotal
$temp ? : 1 2 3
438 3 return map { "$create " . $self->quote_identifier($self->_sequence_name($table, $_->{name})) } $self->_identity_columns($spec);
439 }
440  
441 5 ▶sub _sequence_name { "$_[1]_$_[2]_seq" }
Subroutine _sequence_name: called CC=1 SCAR=0.0
442  
443 4 ▶sub _identity_columns {
Subroutine _identity_columns: called CC=3 SCAR=11.7
444 4 ▶ grep { $_->{identity} } @{($_[1] // {})->{columns} // []};
Condition50%2/4
+($_[1] // {})->{"columns"} // []
l!l
40
$_[1] // {}
l!l
40
MC/DC0%0/1
+($_[1] // {})->{"columns"} // []
$_[1]
Truth table50%1/2
A
A= $_[1]
Aresult
11
00
445 }
446  
447 =pod
448  
449 =over 4
450  
451 =item $sql = $dialect->_scope($dbh, $temp, $name_column)
452  
453 The scan queries' C<WHERE> lines: the current database and schema, or with
454 C<$temp> the C<temp> database and that one name in C<$name_column> (default
455 C<table_name>).
456  
457 =back
458  
459 =cut
460  
461 0 ▶sub _scope {
Subroutine _scope: not called CC=3 SCAR=24.8
462 0 my $self = shift;
463 0 my ($dbh, $temp, $column) = @_;
464 0 ▶ return " WHERE database_name = current_database()\n AND schema_name = current_schema()\n" unless defined $temp;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $temp 0 0 0
465 0 ▶ return " WHERE database_name = 'temp'\n AND " . ($column // 'table_name') . " = " . $dbh->quote($temp) . "\n";
Condition0%0/2
$column // "table_name"
l!l
00
MC/DC0%0/1
$column // "table_name"
$column
Truth table0%0/2
A
A= $column
Aresult
11
00
466 }
467  
468 10 1;
469  
470 __END__
« blib/lib/DBIx/QuickORM/Source/Query.pm ↑ Summary blib/lib/DBIx/QuickORM/Source/Table.pm »