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

filter: Statementstmt 65.3% 34 / 52 Branchbran 70.0% 7 / 10 Conditioncond 66.6% 8 / 12 MC/DCmcdc 50.0% 3 / 6 Subroutinesub 58.8% 10 / 17 Podpod 100% 13 / 13 totaltotal 68.1% 75 / 110 CC 14 SCAR 14.7
CC
14
Cov
66%
_unique_rows
12.0
text_to_db
6.8
text_from_db
5.0
CRAP 4.3
« blib/lib/DBIx/QuickORM/JoinRow.pm ↑ Summary blib/lib/DBIx/QuickORM/Type/DateTime.pm »
1 package DBIx::QuickORM::Dialect::DuckDB;
2 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 2 our $VERSION = '0.000031';
6  
7 2 ▶use parent 'DBIx::QuickORM::Dialect';
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 =pod
10  
11 =encoding UTF-8
12  
13 =head1 NAME
14  
15 DBIx::QuickORM::Dialect::DuckDB - DuckDB dialect.
16  
17 =head1 DESCRIPTION
18  
19 DuckDB through L<DBD::DuckDB>. It keeps the base defaults of
20 L<DBIx::QuickORM::Dialect>, but has no savepoints (so transactions cannot
21 nest), allows no second connection, and classifies errors by message. The
22 namespace is C<current_schema()>.
23  
24 DBD::DuckDB exchanges bytes, so this dialect decodes fetched text and
25 upgrades text it sends.
26  
27 =head1 SYNOPSIS
28  
29 my $dialect = DBIx::QuickORM::Dialect->for_dbh($dbh); # this class for DBD::DuckDB
30  
31 =head1 PUBLIC METHODS
32  
33 =over 4
34  
35 =item $bool = $dialect->supports_savepoints
36  
37 False.
38  
39 =item $bool = $dialect->supports_second_connection($dbh)
40  
41 False: DuckDB holds an exclusive lock on its database file, and an in-memory
42 database belongs to its one connection.
43  
44 =item $n = $dialect->bind_limit
45  
46 7000.
47  
48 DuckDB sets no limit, but bulk inserts measured about 30% slower at 65535
49 binds a statement than at 3,000 to 7,000.
50  
51 =back
52  
53 =cut
54  
55 1 ▶sub supports_savepoints { 0 }
Subroutine supports_savepoints: called CC=1 SCAR=0.0
Pod documented
56 1 ▶sub supports_second_connection { 0 }
Subroutine supports_second_connection: called CC=1 SCAR=0.0
Pod documented
57 1 ▶sub bind_limit { 7000 }
Subroutine bind_limit: called CC=1 SCAR=0.0
Pod documented
58  
59 =pod
60  
61 =over 4
62  
63 =item $dialect->init_session($dbh, $time_zone)
64  
65 C<SET TimeZone>. DuckDB's sessions are UTF-8 already. The text conversion is
66 done by C<text_from_db> and C<text_to_db>.
67  
68 =back
69  
70 =cut
71  
72 1 ▶sub init_session {
Subroutine init_session: called CC=1 SCAR=0.0
Pod documented
73 1 my $self = shift;
74 1 my ($dbh, $time_zone) = @_;
75 1 return $dbh->do('SET TimeZone = ' . $dbh->quote($time_zone));
76 }
77  
78 =pod
79  
80 =over 4
81  
82 =item $dialect->text_from_db(@values)
83  
84 Decodes each value holding non-ASCII bytes from UTF-8, in place. DBD::DuckDB
85 returns text as bytes.
86  
87 =back
88  
89 =cut
90  
91 3 ▶sub text_from_db {
Subroutine text_from_db: called CC=5 SCAR=16.1
Pod documented
92 3 my $self = shift;
93 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
94 3 return;
95 }
96  
97 =pod
98  
99 =over 4
100  
101 =item $dialect->text_to_db(@values)
102  
103 Upgrades each value holding non-ASCII characters in place, so its bytes are
104 UTF-8.
105  
106 DBD::DuckDB sends a string's internal bytes as they are, which for a Latin-1
107 string that was never upgraded are not UTF-8.
108  
109 =back
110  
111 =cut
112  
113 1 ▶sub text_to_db {
Subroutine text_to_db: called CC=5 SCAR=19.2
Pod documented
114 1 my $self = shift;
115 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
116 1 return;
117 }
118  
119 =pod
120  
121 =over 4
122  
123 =item $kind = $dialect->error_kind($native, $state, $errstr)
124  
125 The kind from the error text, since DBD::DuckDB reports the same code and
126 SQLSTATE for every error.
127  
128 C<Constraint Error> messages give the constraint kinds, and a
129 C<TransactionContext Error> naming a conflict gives
130 C<serialization_failure>. Anything else is undef.
131  
132 =back
133  
134 =cut
135  
136 2 my @MESSAGE_KIND = (
137 [qr/Constraint Error: Duplicate key .* violates (?:primary key|unique) constraint/ => 'unique_violation'],
138 [qr/Constraint Error: PRIMARY KEY or UNIQUE constraint violation/ => 'unique_violation'],
139 [qr/Constraint Error: Violates foreign key constraint/ => 'foreign_key_violation'],
140 [qr/Constraint Error: NOT NULL constraint failed/ => 'not_null_violation'],
141 [qr/Constraint Error: CHECK constraint failed/ => 'check_violation'],
142 [qr/TransactionContext Error: .*\bconflict\b/i => 'serialization_failure'],
143 );
144  
145 10 ▶sub error_kind {
Subroutine error_kind: called CC=4 SCAR=13.9
Pod documented
146 10 my $self = shift;
147 10 my (undef, undef, $errstr) = @_;
148 10 ▶ return undef unless defined $errstr;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $errstr 1 9 10
149  
150 9 for my $pair (@MESSAGE_KIND) {
151 34 ▶ return $pair->[1] if $errstr =~ $pair->[0];
Branch: 1 branch
BranchTrueFalseTotal
if $errstr =~ /$pair->[0]/i 8 26 34
152 }
153  
154 1 return undef;
155 }
156  
157 =pod
158  
159 =over 4
160  
161 =item $sql = $dialect->tables_sql
162  
163 =item $sql = $dialect->fields_sql
164  
165 =item $sql = $dialect->pk_sql
166  
167 =item $sql = $dialect->unique_sql
168  
169 =item $sql = $dialect->fk_sql
170  
171 The scan queries (see L<C<scan>|DBIx::QuickORM::Dialect/"PUBLIC METHODS">),
172 over C<duckdb_tables()>, C<duckdb_columns()>, and C<duckdb_constraints()> in
173 C<current_database()> and C<current_schema()>.
174  
175 A column the database fills has a default, such as C<nextval('seq')>.
176 No column is marked as refusing values: the catalog does not mark generated
177 columns, and DuckDB allows no key on one.
178  
179 Unique keys come from C<UNIQUE> constraints and from unique indexes (see
180 C<_unique_rows>).
181  
182 =back
183  
184 =cut
185  
186 2 my $IN_SCHEMA = <<'EOT';
187 WHERE database_name = current_database()
188 AND schema_name = current_schema()
189 EOT
190  
191 0 ▶sub tables_sql {
Subroutine tables_sql: not called CC=1 SCAR=6.9
Pod documented
192 0 return <<" EOT";
193 SELECT table_name
194 FROM duckdb_tables()
195 $IN_SCHEMA
196 AND NOT internal
197 AND NOT temporary
198 EOT
199 }
200  
201 0 ▶sub fields_sql {
Subroutine fields_sql: not called CC=1 SCAR=6.9
Pod documented
202 0 return <<" EOT";
203 SELECT table_name, column_name, data_type, NULL,
204 CASE WHEN upper(column_default) <> 'NULL' THEN 1 END,
205 NULL
206 FROM duckdb_columns()
207 $IN_SCHEMA
208 EOT
209 }
210  
211 0 ▶sub pk_sql {
Subroutine pk_sql: not called CC=1 SCAR=6.9
Pod documented
212 0 return <<" EOT";
213 SELECT table_name, unnest(constraint_column_names)
214 FROM duckdb_constraints()
215 $IN_SCHEMA
216 AND constraint_type = 'PRIMARY KEY'
217 EOT
218 }
219  
220 0 ▶sub unique_sql {
Subroutine unique_sql: not called CC=1 SCAR=6.9
Pod documented
221 0 return <<" EOT";
222 SELECT table_name, constraint_index, unnest(constraint_column_names)
223 FROM duckdb_constraints()
224 $IN_SCHEMA
225 AND constraint_type = 'UNIQUE'
226 EOT
227 }
228  
229 =pod
230  
231 =over 4
232  
233 =item $sql = $dialect->unique_index_sql
234  
235 The C<(table, index, expressions)> rows of the unique indexes, from
236 C<duckdb_indexes()>.
237  
238 =back
239  
240 =cut
241  
242 0 ▶sub unique_index_sql {
Subroutine unique_index_sql: not called CC=1 SCAR=6.9
Pod documented
243 0 return <<" EOT";
244 SELECT table_name, index_name, expressions
245 FROM duckdb_indexes()
246 $IN_SCHEMA
247 AND is_unique
248 EOT
249 }
250  
251 0 ▶sub fk_sql {
Subroutine fk_sql: not called CC=1 SCAR=6.9
Pod documented
252 0 return <<" EOT";
253 SELECT table_name, constraint_index, unnest(constraint_column_names), referenced_table, unnest(referenced_column_names)
254 FROM duckdb_constraints()
255 $IN_SCHEMA
256 AND constraint_type = 'FOREIGN KEY'
257 EOT
258 }
259  
260 =head1 PRIVATE METHODS
261  
262 =over 4
263  
264 =item \@rows = $dialect->_unique_rows($dbh)
265  
266 The unique constraints' rows, then a C<(table, "index $name", field)> row for
267 each field of each unique index.
268  
269 DuckDB reports an index's fields as a string such as C<[b, a]>, quoting a
270 name that needs it (C<['"we ird"']>). Only a plain list of such names is
271 read. Anything else, such as an expression (C<['(lower(c))']>), gives one
272 undef field, so the scan leaves the index out.
273  
274 =back
275  
276 =cut
277  
278 # A bracketed list of bare names, or names in '"..."' holding no quote or comma.
279 2 my $INDEX_FIELDS = qr/^\[((?:\w+|'"[^"',]+"')(?:, (?:\w+|'"[^"',]+"'))*)\]\z/;
280  
281 0 ▶sub _unique_rows {
Subroutine _unique_rows: not called CC=3 SCAR=24.8
282 0 my $self = shift;
283 0 my ($dbh) = @_;
284  
285 0 my @rows = @{$self->SUPER::_unique_rows($dbh)};
286 0 for my $index (@{$self->_rows($dbh, $self->unique_index_sql)}) {
287 0 my ($table, $name, $expressions) = @$index;
288 0 ▶ my @fields = $expressions =~ $INDEX_FIELDS ? map { s/^'"(.*)"'\z/$1/r } split /, / => $1 : (undef);
Branch: 1 branch
BranchTrueFalseTotal
$expressions =~ /$INDEX_FIELDS/ ? : 0 0 0
289 0 push @rows => map { [$table, "index $name", $_] } @fields;
290 }
291  
292 0 return \@rows;
293 }
294  
295 2 1;
296  
297 __END__
« blib/lib/DBIx/QuickORM/JoinRow.pm ↑ Summary blib/lib/DBIx/QuickORM/Type/DateTime.pm »