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

filter: Statementstmt 100% 99 / 99 Branchbran 100% 26 / 26 Conditioncond 80.0% 24 / 30 MC/DCmcdc 62.5% 10 / 16 Subroutinesub 100% 28 / 28 Podpod 100% 20 / 20 totaltotal 94.5% 207 / 219 CC 25 SCAR 12.8
CC
25
Cov
96%
_inline_pk
7.0
affinity
7.0
rowid_alias
6.0
CRAP 3.6
« blib/lib/DBIx/QuickORM/SchemaBuilder/ORM.pm ↑ Summary blib/lib/DBIx/QuickORM/Value.pm »
1 package DBIx::QuickORM::Dialect::SQLite;
2 74 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 74 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 74 our $VERSION = '0.000033';
6  
7 74 ▶use DBD::SQLite::Constants qw/DBD_SQLITE_STRING_MODE_UNICODE_STRICT/;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 74 ▶use Carp qw/croak/;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 74 ▶use parent 'DBIx::QuickORM::Dialect';
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 =pod
14  
15 =encoding UTF-8
16  
17 =head1 NAME
18  
19 DBIx::QuickORM::Dialect::SQLite - SQLite dialect.
20  
21 =head1 DESCRIPTION
22  
23 SQLite through L<DBD::SQLite>. It keeps the base defaults of
24 L<DBIx::QuickORM::Dialect>, with strict UTF-8 sessions, foreign keys
25 enforced, SQLite's own type affinity rules, and its extended result codes for
26 error kinds. The namespace is always C<main>. Attached databases and C<temp>
27 are not part of it. SQLite has no session time zone.
28  
29 =head1 SYNOPSIS
30  
31 my $dialect = DBIx::QuickORM::Dialect->for_dbh($dbh); # this class for DBD::SQLite
32  
33 =head1 PUBLIC METHODS
34  
35 =over 4
36  
37 =item $ns = $dialect->namespace($dbh)
38  
39 C<main>.
40  
41 =item ($parser, $formatter) = $dialect->datetime_format
42  
43 L<DateTime::Format::Pg> parses, and the formatter is undef.
44  
45 The DateTime type then writes values itself, keeping the colon in an offset
46 (C<+05:30>) that SQLite's date functions need.
47  
48 =back
49  
50 =cut
51  
52 416 ▶sub namespace { 'main' }
Subroutine namespace: called CC=1 SCAR=0.0
Pod documented
53 481 ▶sub unsupported { $_[0]->_too_old(3_035_000, 'SQLite 3.35', 'RETURNING') }
Subroutine unsupported: called CC=1 SCAR=0.0
Pod documented
54 34 ▶sub datetime_format { ('DateTime::Format::Pg', undef) }
Subroutine datetime_format: called CC=1 SCAR=0.0
Pod documented
55  
56 =pod
57  
58 =over 4
59  
60 =item $sql = $dialect->identity_sql($table, \%field, $inline_pk)
61  
62 Undef: an identity is SQLite's rowid alias, a single C<integer> primary key
63 declared on its column (see C<_inline_pk>). Croaks for any other field.
64  
65 my $field = {
66 name => 'id',
67 db_type => 'integer',
68 identity => 1,
69 };
70  
71 my $none = $dialect->identity_sql('drafts', $field, 1); # undef: "id" integer PRIMARY KEY is enough
72  
73  
74 =item $sql = $dialect->quote_binary($dbh, $bytes)
75  
76 A blob literal, C<X'...'>.
77  
78 =item $sql = $dialect->setting_sql($name, $value)
79  
80 C<PRAGMA $name = $value>: SQLite's settings are pragmas.
81  
82 =item $column = $dialect->rowid_alias(\%spec)
83  
84 The single C<integer> primary key, which is the row id, so SQLite fills it.
85  
86 my $spec = {
87 pk => ['id'],
88 columns => [
89 {
90 name => 'id',
91 db_type => 'integer',
92 },
93 ],
94 };
95  
96 my $rowid = $dialect->rowid_alias($spec); # 'id'
97  
98 =item @sql = $dialect->create_enum_sql($name, \@values)
99  
100 =item ($db_type, $check) = $dialect->enum_column($column, $enum, \@values, temp => $bool)
101  
102 No enum types: an enum column is C<text> with a C<CHECK> that the value is
103 one of them, and nothing is created for the enum.
104  
105 my @none = $dialect->create_enum_sql(post_status => ['draft', 'live']); # ()
106  
107 my ($type, $check) = $dialect->enum_column('status', 'post_status', ['draft', 'live']);
108 # 'text' and q{"status" IN ('draft', 'live')}
109  
110  
111 =item $sql = $dialect->add_foreign_key_sql($table, \%fk)
112  
113 Croaks: SQLite cannot add a foreign key to a table already created, so
114 tables that reference each other cannot be built.
115  
116 =back
117  
118 =cut
119  
120 8 ▶sub identity_sql {
Subroutine identity_sql: called CC=2 SCAR=6.9
Pod documented
121 8 my $self = shift;
122 8 my ($table, $field, $inline_pk) = @_;
123 8 ▶ croak "An identity column in a SQLite table must be its only primary key column, declared 'integer'" unless $inline_pk;
Branch: 1 branch
BranchTrueFalseTotal
unless $inline_pk 2 6 8
124 6 return undef;
125 }
126  
127 3 ▶sub quote_binary { "X'" . unpack('H*', $_[2]) . "'" }
Subroutine quote_binary: called CC=1 SCAR=0.0
Pod documented
128 4 ▶sub setting_sql { "PRAGMA $_[1] = " . $_[0]->_setting_value($_[2]) }
Subroutine setting_sql: called CC=1 SCAR=0.0
Pod documented
129 19 ▶sub create_enum_sql { () }
Subroutine create_enum_sql: called CC=1 SCAR=0.0
Pod documented
130  
131 28 ▶sub rowid_alias {
Subroutine rowid_alias: called CC=6 SCAR=18.0
Pod documented
132 28 my $self = shift;
133 28 my ($spec) = @_;
134 28 my $pk = $spec->{pk};
135 28 ▶ return undef unless $pk && @$pk == 1;
Branch: 1 branch
BranchTrueFalseTotal
unless $pk and @$pk == 1 6 22 28
Condition100%3/3
$pk and @$pk == 1
!ll&&!rl&&r
1522
MC/DC100%2/2
$pk and @$pk == 1
$pk @$pk == 1
Truth table100%3/3
A and B
A= $pkB= @$pk == 1
ABresult
0X0
100
111
136 22 my ($column) = grep { $_->{name} eq $pk->[0] } @{$spec->{columns}};
137 22 ▶ return $column && lc($column->{db_type} // '') eq 'integer' ? $pk->[0] : undef;
Branch: 1 branch
BranchTrueFalseTotal
$column && lc($column->{'db_type'} // '') eq 'integer' ? : 21 1 22
Condition60%3/5
$column and lc($column->{"db_type"} // "") eq "integer"
!ll&&!rl&&r
0121
$column->{"db_type"} // ""
l!l
220
MC/DC0%0/2
$column and lc($column->{"db_type"} // "") eq "integer"
$column $column->{"db_type"}
Truth table33%1/3
A and B
A= $columnB= $column->{"db_type"}
ABresult
0X0
100
111
138 }
139  
140 29 ▶sub enum_column {
Subroutine enum_column: called CC=1 SCAR=0.0
Pod documented
141 29 my $self = shift;
142 29 my ($column, $enum, $values) = @_;
143 29 return ('text', $self->_enum_check($column, $values));
144 }
145  
146 3 ▶sub add_foreign_key_sql { croak "SQLite 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
147  
148 =pod
149  
150 =over 4
151  
152 =item $dialect->init_session($dbh, $time_zone)
153  
154 Sets C<sqlite_string_mode> to C<DBD_SQLITE_STRING_MODE_UNICODE_STRICT>,
155 turns on C<sqlite_extended_result_codes>, and sends
156 C<PRAGMA foreign_keys = ON>. The time zone is not used.
157  
158 In strict mode text is UTF-8 in the database and characters in Perl, and
159 invalid UTF-8 read from the database croaks. Extended result codes tell the
160 constraint errors apart.
161  
162 SQLite enforces foreign keys only when a connection asks, so every connection
163 the ORM opens or is given asks, a forked child's reconnect included. It is a
164 setting of the connection, not of a transaction: transaction isolation is
165 still never set at connect.
166  
167 =back
168  
169 =cut
170  
171 417 ▶sub init_session {
Subroutine init_session: called CC=1 SCAR=0.0
Pod documented
172 417 my $self = shift;
173 417 my ($dbh) = @_;
174 417 $dbh->{sqlite_string_mode} = DBD_SQLITE_STRING_MODE_UNICODE_STRICT;
175 417 $dbh->{sqlite_extended_result_codes} = 1;
176 417 $dbh->do('PRAGMA foreign_keys = ON');
177 417 return;
178 }
179  
180 my %NATIVE_KIND = (
181 74 (map { $_ => 'lock_timeout' } 5, 261, 517, 773, 6, 262, 518),
182 2067 => 'unique_violation',
183 1555 => 'unique_violation',
184 787 => 'foreign_key_violation',
185 1299 => 'not_null_violation',
186 275 => 'check_violation',
187 );
188  
189 =pod
190  
191 =over 4
192  
193 =item \%kinds = $dialect->native_error_kinds
194  
195 The extended result codes:
196  
197 =for highlighter language=text
198  
199 SQLITE_BUSY (5) and its extended codes lock_timeout
200 SQLITE_LOCKED (6) and its extended codes lock_timeout
201 SQLITE_CONSTRAINT_UNIQUE (2067) unique_violation
202 SQLITE_CONSTRAINT_PRIMARYKEY (1555) unique_violation
203 SQLITE_CONSTRAINT_FOREIGNKEY (787) foreign_key_violation
204 SQLITE_CONSTRAINT_NOTNULL (1299) not_null_violation
205 SQLITE_CONSTRAINT_CHECK (275) check_violation
206  
207 =back
208  
209 =cut
210  
211 58 ▶sub native_error_kinds { \%NATIVE_KIND }
Subroutine native_error_kinds: called CC=1 SCAR=0.0
Pod documented
212  
213 =pod
214  
215 =over 4
216  
217 =item $bool = $dialect->supports_second_connection($dbh)
218  
219 True only for a database file.
220  
221 An in-memory database (C<:memory:>, an empty file name, or C<mode=memory>) is
222 not reopened: another connection would open a new, empty database, unless it
223 names a shared in-memory database, which this does not try to tell apart.
224  
225 =back
226  
227 =cut
228  
229 52 ▶sub supports_second_connection {
Subroutine supports_second_connection: called CC=3 SCAR=11.0
Pod documented
230 52 my $self = shift;
231 52 my ($dbh) = @_;
232 52 my $file = $dbh->sqlite_db_filename;
233 52 ▶ return defined($file) && length($file) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
defined $file && length $file ? : 48 4 52
Condition66%2/3
defined $file and length $file
!ll&&!rl&&r
0448
MC/DC50%1/2
defined $file and length $file
defined $file length $file
Truth table66%2/3
A and B
A= defined $fileB= length $file
ABresult
0X0
100
111
234 }
235  
236 =pod
237  
238 =over 4
239  
240 =item $key = $dialect->scan_cache_key($dbh, $namespace)
241  
242 The database file's absolute path, or undef for an in-memory database. An
243 in-memory database is never cached: its tables can change with no scan to
244 notice, and a named one can be shared with other handles.
245  
246 =back
247  
248 =cut
249  
250 384 ▶sub scan_cache_key {
Subroutine scan_cache_key: called CC=3 SCAR=11.0
Pod documented
251 384 my $self = shift;
252 384 my ($dbh) = @_;
253 384 my $file = $dbh->sqlite_db_filename;
254 384 ▶ return undef unless defined($file) && length($file);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $file and length $file 219 165 384
Condition66%2/3
defined $file and length $file
!ll&&!rl&&r
0219165
MC/DC50%1/2
defined $file and length $file
defined $file length $file
Truth table66%2/3
A and B
A= defined $fileB= length $file
ABresult
0X0
100
111
255 165 return join "\0" => 'SQLite', $file;
256 }
257  
258 =pod
259  
260 =over 4
261  
262 =item $sql = $dialect->tables_sql($dbh, $temp)
263  
264 =item $sql = $dialect->fields_sql($dbh, $temp)
265  
266 =item $sql = $dialect->pk_sql($dbh, $temp)
267  
268 =item $sql = $dialect->unique_sql($dbh, $temp)
269  
270 =item $sql = $dialect->fk_sql($dbh, $temp)
271  
272 The scan queries (see L<C<scan>|DBIx::QuickORM::Dialect/"PUBLIC METHODS">),
273 over C<main.sqlite_master> and the table pragmas, leaving out SQLite's own
274 C<sqlite_*> tables, or with a temporary name over C<temp.sqlite_master> for
275 that one table or view.
276  
277 Fields come from C<pragma_table_xinfo>, so generated columns are included and
278 the hidden columns of virtual tables are not.
279  
280 A column the database fills has a default, is generated, or is the rowid
281 alias. The alias is a key column of a table with no C<pk> index: that rules
282 out C<INT PRIMARY KEY>, C<INTEGER PRIMARY KEY DESC>, C<WITHOUT ROWID>
283 tables, and compound keys, as SQLite does. Generated columns refuse values.
284  
285 =back
286  
287 =cut
288  
289 330 ▶sub tables_sql {
Subroutine tables_sql: called CC=1 SCAR=0.0
Pod documented
290 330 my $self = shift;
291 330 my ($from, $where) = $self->_scope(@_);
292 330 return "SELECT m.name, CASE m.type WHEN 'view' THEN 'view' END\n$from$where";
293 }
294  
295 # hidden = 1 marks the hidden columns of virtual tables. Generated columns use 2 and 3.
296 330 ▶sub fields_sql {
Subroutine fields_sql: called CC=1 SCAR=0.0
Pod documented
297 330 my $self = shift;
298 330 my ($from, $where, $schema) = $self->_scope(@_);
299 330 return <<" EOT";
300 SELECT m.name, x.name, x.type, NULL,
301 CASE WHEN x.hidden IN (2, 3)
302 OR upper(x.dflt_value) <> 'NULL'
303 OR (x.pk > 0 AND NOT EXISTS (SELECT 1 FROM pragma_index_list(m.name, '$schema') il WHERE il.origin = 'pk'))
304 THEN 1 END,
305 CASE WHEN x.hidden IN (2, 3) THEN 1 END
306 $from
307 JOIN pragma_table_xinfo(m.name, '$schema') x
308 $where
309 AND x.hidden != 1
310 EOT
311 }
312  
313 330 ▶sub pk_sql {
Subroutine pk_sql: called CC=1 SCAR=0.0
Pod documented
314 330 my $self = shift;
315 330 my ($from, $where, $schema) = $self->_scope(@_);
316 330 return <<" EOT";
317 SELECT m.name, x.name
318 $from
319 JOIN pragma_table_xinfo(m.name, '$schema') x
320 $where
321 AND x.pk > 0
322 ORDER BY m.name, x.pk
323 EOT
324 }
325  
326 # An expression column has no name.
327 330 ▶sub unique_sql {
Subroutine unique_sql: called CC=1 SCAR=0.0
Pod documented
328 330 my $self = shift;
329 330 my ($from, $where, $schema) = $self->_scope(@_);
330 330 return <<" EOT";
331 SELECT m.name, il.name, ii.name
332 $from
333 JOIN pragma_index_list(m.name, '$schema') il
334 JOIN pragma_index_info(il.name, '$schema') ii
335 $where
336 AND il."unique" = 1
337 AND il.partial = 0
338 EOT
339 }
340  
341 330 ▶sub fk_sql {
Subroutine fk_sql: called CC=1 SCAR=0.0
Pod documented
342 330 my $self = shift;
343 330 my ($from, $where, $schema) = $self->_scope(@_);
344 330 return <<" EOT";
345 SELECT m.name, fk.id, fk."from", fk."table", fk."to"
346 $from
347 JOIN pragma_foreign_key_list(m.name, '$schema') fk
348 $where
349 ORDER BY m.name, fk.id, fk.seq
350 EOT
351 }
352  
353 =pod
354  
355 =over 4
356  
357 =item $affinity = $dialect->affinity($db_type)
358  
359 SQLite's own rules on the declared type, then the inherited lookup, then
360 C<numeric>. The rules, in order:
361  
362 INT numeric
363 CHAR, CLOB, TEXT string
364 BLOB, or no type binary
365 REAL, FLOA, DOUB numeric
366  
367 =back
368  
369 =cut
370  
371 2297 ▶sub affinity {
Subroutine affinity: called CC=7 SCAR=19.5
Pod documented
372 2297 my $self = shift;
373 2297 my ($db_type) = @_;
374  
375 2297 my $t = uc($db_type);
376 2297 ▶ return 'numeric' if $t =~ m/INT/;
Branch: 1 branch
BranchTrueFalseTotal
if $t =~ /INT/ 1020 1277 2297
377 1277 ▶ return 'string' if $t =~ m/CHAR|CLOB|TEXT/;
Branch: 1 branch
BranchTrueFalseTotal
if $t =~ /CHAR|CLOB|TEXT/ 1069 208 1277
378 208 ▶ return 'binary' if $t =~ m/BLOB/ || $t !~ m/\S/;
Branch: 1 branch
BranchTrueFalseTotal
if $t =~ /BLOB/ or not $t =~ /\S/ 18 190 208
Condition100%3/3
$t =~ /BLOB/ or not $t =~ /\S/
l!l&&r!l&&!r
135190
MC/DC100%2/2
$t =~ /BLOB/ or not $t =~ /\S/
$t =~ /BLOB/ not $t =~ /\S/
Truth table100%3/3
A or not(B)
A= $t =~ /BLOB/B= not $t =~ /\S/
ABresult
1X1
011
000
379 190 ▶ return 'numeric' if $t =~ m/REAL|FLOA|DOUB/;
Branch: 1 branch
BranchTrueFalseTotal
if $t =~ /REAL|FLOA|DOUB/ 3 187 190
380  
381 187 ▶ return $self->_known_affinity($db_type) // 'numeric';
Condition100%2/2
$self->_known_affinity($db_type) // "numeric"
l!l
1861
MC/DC100%1/1
$self->_known_affinity($db_type) // "numeric"
$self->_known_affinity($db_type)
Truth table100%2/2
A
A= $self->_known_affinity($db_type)
Aresult
11
00
382 }
383  
384 =head1 PRIVATE METHODS
385  
386 =over 4
387  
388 =item $version = $class->_server_version($dbh)
389  
390 C<SELECT sqlite_version()>, through C<_version_number>.
391  
392 =back
393  
394 =cut
395  
396 489 ▶sub _server_version {
Subroutine _server_version: called CC=1 SCAR=0.0
397 489 my $class = shift;
398 489 my ($dbh) = @_;
399 489 my ($version) = $dbh->selectrow_array('SELECT sqlite_version()');
400 489 return $class->_version_number($version);
401 }
402  
403 =pod
404  
405 =over 4
406  
407 =item @columns = $dialect->_inline_pk(\%spec)
408  
409 The primary key column, when it is the only one, an identity, and declared
410 C<integer>: SQLite makes such a column the rowid alias only when the key is
411 declared on it.
412  
413 =item ($from, $where, $schema) = $dialect->_scope($dbh, $temp)
414  
415 The scan queries' C<FROM> and C<WHERE> lines over C<sqlite_master> (as C<m>),
416 and the schema the pragmas read: C<main> leaving out SQLite's own tables, or
417 with C<$temp> C<temp> and that one name.
418  
419 =back
420  
421 =cut
422  
423 78 ▶sub _inline_pk {
Subroutine _inline_pk: called CC=7 SCAR=19.5
424 78 my $self = shift;
425 78 my ($spec) = @_;
426 78 my $pk = $spec->{pk};
427 78 ▶ return () unless $pk && @$pk == 1;
Branch: 1 branch
BranchTrueFalseTotal
unless $pk and @$pk == 1 16 62 78
Condition100%3/3
$pk and @$pk == 1
!ll&&!rl&&r
13362
MC/DC100%2/2
$pk and @$pk == 1
$pk @$pk == 1
Truth table100%3/3
A and B
A= $pkB= @$pk == 1
ABresult
0X0
100
111
428 62 my ($column) = grep { $_->{name} eq $pk->[0] } @{$spec->{columns}};
429 62 ▶ return () unless $column && $column->{identity} && lc($column->{db_type} // '') eq 'integer';
Branch: 1 branch
BranchTrueFalseTotal
unless $column and $column->{"identity"} and lc($column->{"db_type"} // "") eq "integer" 57 5 62
Condition75%6/8
$column and $column->{"identity"} and lc($column->{"db_type"} // "") eq "integer"
!ll&&!rl&&r
5615
$column and $column->{"identity"}
!ll&&!rl&&r
0566
$column->{"db_type"} // ""
l!l
60
MC/DC33%1/3
$column and $column->{"identity"} and lc($column->{"db_type"} // "") eq "integer"
$column $column->{"identity"} $column->{"db_type"}
Truth table50%2/4
A and B and C
A= $columnB= $column->{"identity"}C= $column->{"db_type"}
ABCresult
0XX0
10X0
1100
1111
430 5 return ($pk->[0]);
431 }
432  
433 1650 ▶sub _scope {
Subroutine _scope: called CC=3 SCAR=11.0
434 1650 my $self = shift;
435 1650 my ($dbh, $temp) = @_;
436 1650 ▶ my $schema = defined($temp) ? 'temp' : 'main';
Branch: 1 branch
BranchTrueFalseTotal
defined $temp ? : 265 1385 1650
437 1650 my $where = " WHERE m.type IN ('table', 'view')\n";
438 1650 ▶ $where .= defined($temp) ? " AND m.name = " . $dbh->quote($temp) . "\n" : " AND m.name NOT LIKE 'sqlite\\_%' ESCAPE '\\'\n";
Branch: 1 branch
BranchTrueFalseTotal
defined $temp ? : 265 1385 1650
439 1650 return (" FROM $schema.sqlite_master m\n", $where, $schema);
440 }
441  
442 74 1;
443  
444 __END__
« blib/lib/DBIx/QuickORM/SchemaBuilder/ORM.pm ↑ Summary blib/lib/DBIx/QuickORM/Value.pm »