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

filter: Statementstmt 100% 53 / 53 Branchbran 100% 12 / 12 Conditioncond 81.8% 9 / 11 MC/DCmcdc 71.4% 5 / 7 Subroutinesub 100% 17 / 17 Podpod 100% 12 / 12 totaltotal 96.4% 108 / 112 CC 11 SCAR 11.0
CC
11
Cov
97%
affinity
7.0
supports_second_connection
3.0
scan_cache_key
3.0
CRAP 3.0
« blib/lib/DBIx/QuickORM/Dialect/PostgreSQL.pm ↑ Summary blib/lib/DBIx/QuickORM/Exception/DB.pm »
1 package DBIx::QuickORM::Dialect::SQLite;
2 65 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 65 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 65 our $VERSION = '0.000032';
6  
7 65 ▶use DBD::SQLite::Constants qw/DBD_SQLITE_STRING_MODE_UNICODE_STRICT/;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 65 ▶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::SQLite - SQLite dialect.
18  
19 =head1 DESCRIPTION
20  
21 SQLite through L<DBD::SQLite>. It keeps the base defaults of
22 L<DBIx::QuickORM::Dialect>, with strict UTF-8 sessions, foreign keys
23 enforced, SQLite's own type affinity rules, and its extended result codes for
24 error kinds. The namespace is always C<main>. Attached databases and C<temp>
25 are not part of it. SQLite has no session time zone.
26  
27 =head1 SYNOPSIS
28  
29 my $dialect = DBIx::QuickORM::Dialect->for_dbh($dbh); # this class for DBD::SQLite
30  
31 =head1 PUBLIC METHODS
32  
33 =over 4
34  
35 =item $ns = $dialect->namespace($dbh)
36  
37 C<main>.
38  
39 =item ($parser, $formatter) = $dialect->datetime_format
40  
41 L<DateTime::Format::Pg> parses, and the formatter is undef.
42  
43 The DateTime type then writes values itself, keeping the colon in an offset
44 (C<+05:30>) that SQLite's date functions need.
45  
46 =back
47  
48 =cut
49  
50 365 ▶sub namespace { 'main' }
Subroutine namespace: called CC=1 SCAR=0.0
Pod documented
51 33 ▶sub datetime_format { ('DateTime::Format::Pg', undef) }
Subroutine datetime_format: called CC=1 SCAR=0.0
Pod documented
52  
53 =pod
54  
55 =over 4
56  
57 =item $dialect->init_session($dbh, $time_zone)
58  
59 Sets C<sqlite_string_mode> to C<DBD_SQLITE_STRING_MODE_UNICODE_STRICT>,
60 turns on C<sqlite_extended_result_codes>, and sends
61 C<PRAGMA foreign_keys = ON>. The time zone is not used.
62  
63 In strict mode text is UTF-8 in the database and characters in Perl, and
64 invalid UTF-8 read from the database croaks. Extended result codes tell the
65 constraint errors apart.
66  
67 SQLite enforces foreign keys only when a connection asks, so every connection
68 the ORM opens or is given asks, a forked child's reconnect included. It is a
69 setting of the connection, not of a transaction: transaction isolation is
70 still never set at connect.
71  
72 =back
73  
74 =cut
75  
76 366 ▶sub init_session {
Subroutine init_session: called CC=1 SCAR=0.0
Pod documented
77 366 my $self = shift;
78 366 my ($dbh) = @_;
79 366 $dbh->{sqlite_string_mode} = DBD_SQLITE_STRING_MODE_UNICODE_STRICT;
80 366 $dbh->{sqlite_extended_result_codes} = 1;
81 366 $dbh->do('PRAGMA foreign_keys = ON');
82 366 return;
83 }
84  
85 my %NATIVE_KIND = (
86 65 (map { $_ => 'lock_timeout' } 5, 261, 517, 773, 6, 262, 518),
87 2067 => 'unique_violation',
88 1555 => 'unique_violation',
89 787 => 'foreign_key_violation',
90 1299 => 'not_null_violation',
91 275 => 'check_violation',
92 );
93  
94 =pod
95  
96 =over 4
97  
98 =item \%kinds = $dialect->native_error_kinds
99  
100 The extended result codes:
101  
102 =for highlighter language=text
103  
104 SQLITE_BUSY (5) and its extended codes lock_timeout
105 SQLITE_LOCKED (6) and its extended codes lock_timeout
106 SQLITE_CONSTRAINT_UNIQUE (2067) unique_violation
107 SQLITE_CONSTRAINT_PRIMARYKEY (1555) unique_violation
108 SQLITE_CONSTRAINT_FOREIGNKEY (787) foreign_key_violation
109 SQLITE_CONSTRAINT_NOTNULL (1299) not_null_violation
110 SQLITE_CONSTRAINT_CHECK (275) check_violation
111  
112 =back
113  
114 =cut
115  
116 40 ▶sub native_error_kinds { \%NATIVE_KIND }
Subroutine native_error_kinds: called CC=1 SCAR=0.0
Pod documented
117  
118 =pod
119  
120 =over 4
121  
122 =item $bool = $dialect->supports_second_connection($dbh)
123  
124 True only for a database file.
125  
126 An in-memory database (C<:memory:>, an empty file name, or C<mode=memory>) is
127 not reopened: another connection would open a new, empty database, unless it
128 names a shared in-memory database, which this does not try to tell apart.
129  
130 =back
131  
132 =cut
133  
134 49 ▶sub supports_second_connection {
Subroutine supports_second_connection: called CC=3 SCAR=11.0
Pod documented
135 49 my $self = shift;
136 49 my ($dbh) = @_;
137 49 my $file = $dbh->sqlite_db_filename;
138 49 ▶ return defined($file) && length($file) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
defined $file && length $file ? : 45 4 49
Condition66%2/3
defined $file and length $file
!ll&&!rl&&r
0445
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
139 }
140  
141 =pod
142  
143 =over 4
144  
145 =item $key = $dialect->scan_cache_key($dbh, $namespace)
146  
147 The database file's absolute path, or undef for an in-memory database. An
148 in-memory database is never cached: its tables can change with no scan to
149 notice, and a named one can be shared with other handles.
150  
151 =back
152  
153 =cut
154  
155 334 ▶sub scan_cache_key {
Subroutine scan_cache_key: called CC=3 SCAR=11.0
Pod documented
156 334 my $self = shift;
157 334 my ($dbh) = @_;
158 334 my $file = $dbh->sqlite_db_filename;
159 334 ▶ return undef unless defined($file) && length($file);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $file and length $file 187 147 334
Condition66%2/3
defined $file and length $file
!ll&&!rl&&r
0187147
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
160 147 return join "\0" => 'SQLite', $file;
161 }
162  
163 =pod
164  
165 =over 4
166  
167 =item $sql = $dialect->tables_sql
168  
169 =item $sql = $dialect->fields_sql
170  
171 =item $sql = $dialect->pk_sql
172  
173 =item $sql = $dialect->unique_sql
174  
175 =item $sql = $dialect->fk_sql
176  
177 The scan queries (see L<C<scan>|DBIx::QuickORM::Dialect/"PUBLIC METHODS">),
178 over C<main.sqlite_master> and the table pragmas, leaving out SQLite's own
179 C<sqlite_*> tables.
180  
181 Fields come from C<pragma_table_xinfo>, so generated columns are included and
182 the hidden columns of virtual tables are not.
183  
184 A column the database fills has a default, is generated, or is the rowid
185 alias. The alias is a key column of a table with no C<pk> index: that rules
186 out C<INT PRIMARY KEY>, C<INTEGER PRIMARY KEY DESC>, C<WITHOUT ROWID>
187 tables, and compound keys, as SQLite does. Generated columns refuse values.
188  
189 =back
190  
191 =cut
192  
193 65 my $TABLES = <<'EOT';
194 FROM main.sqlite_master m
195 EOT
196  
197 65 my $USER_TABLES = <<'EOT';
198 WHERE m.type = 'table'
199 AND m.name NOT LIKE 'sqlite\_%' ESCAPE '\'
200 EOT
201  
202 237 ▶sub tables_sql { "SELECT m.name\n$TABLES$USER_TABLES" }
Subroutine tables_sql: called CC=1 SCAR=0.0
Pod documented
203  
204 # hidden = 1 marks the hidden columns of virtual tables. Generated columns use 2 and 3.
205 237 ▶sub fields_sql {
Subroutine fields_sql: called CC=1 SCAR=0.0
Pod documented
206 237 return <<" EOT";
207 SELECT m.name, x.name, x.type, NULL,
208 CASE WHEN x.hidden IN (2, 3)
209 OR upper(x.dflt_value) <> 'NULL'
210 OR (x.pk > 0 AND NOT EXISTS (SELECT 1 FROM pragma_index_list(m.name, 'main') il WHERE il.origin = 'pk'))
211 THEN 1 END,
212 CASE WHEN x.hidden IN (2, 3) THEN 1 END
213 $TABLES
214 JOIN pragma_table_xinfo(m.name, 'main') x
215 $USER_TABLES
216 AND x.hidden != 1
217 EOT
218 }
219  
220 237 ▶sub pk_sql {
Subroutine pk_sql: called CC=1 SCAR=0.0
Pod documented
221 237 return <<" EOT";
222 SELECT m.name, x.name
223 $TABLES
224 JOIN pragma_table_xinfo(m.name, 'main') x
225 $USER_TABLES
226 AND x.pk > 0
227 ORDER BY m.name, x.pk
228 EOT
229 }
230  
231 # An expression column has no name.
232 237 ▶sub unique_sql {
Subroutine unique_sql: called CC=1 SCAR=0.0
Pod documented
233 237 return <<" EOT";
234 SELECT m.name, il.name, ii.name
235 $TABLES
236 JOIN pragma_index_list(m.name, 'main') il
237 JOIN pragma_index_info(il.name, 'main') ii
238 $USER_TABLES
239 AND il."unique" = 1
240 AND il.partial = 0
241 EOT
242 }
243  
244 237 ▶sub fk_sql {
Subroutine fk_sql: called CC=1 SCAR=0.0
Pod documented
245 237 return <<" EOT";
246 SELECT m.name, fk.id, fk."from", fk."table", fk."to"
247 $TABLES
248 JOIN pragma_foreign_key_list(m.name, 'main') fk
249 $USER_TABLES
250 ORDER BY m.name, fk.id, fk.seq
251 EOT
252 }
253  
254 =pod
255  
256 =over 4
257  
258 =item $affinity = $dialect->affinity($db_type)
259  
260 SQLite's own rules on the declared type, then the inherited lookup, then
261 C<numeric>. The rules, in order:
262  
263 INT numeric
264 CHAR, CLOB, TEXT string
265 BLOB, or no type binary
266 REAL, FLOA, DOUB numeric
267  
268 =back
269  
270 =cut
271  
272 1848 ▶sub affinity {
Subroutine affinity: called CC=7 SCAR=19.5
Pod documented
273 1848 my $self = shift;
274 1848 my ($db_type) = @_;
275  
276 1848 my $t = uc($db_type);
277 1848 ▶ return 'numeric' if $t =~ m/INT/;
Branch: 1 branch
BranchTrueFalseTotal
if $t =~ /INT/ 782 1066 1848
278 1066 ▶ return 'string' if $t =~ m/CHAR|CLOB|TEXT/;
Branch: 1 branch
BranchTrueFalseTotal
if $t =~ /CHAR|CLOB|TEXT/ 883 183 1066
279 183 ▶ return 'binary' if $t =~ m/BLOB/ || $t !~ m/\S/;
Branch: 1 branch
BranchTrueFalseTotal
if $t =~ /BLOB/ or not $t =~ /\S/ 14 169 183
Condition100%3/3
$t =~ /BLOB/ or not $t =~ /\S/
l!l&&r!l&&!r
104169
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
280 169 ▶ return 'numeric' if $t =~ m/REAL|FLOA|DOUB/;
Branch: 1 branch
BranchTrueFalseTotal
if $t =~ /REAL|FLOA|DOUB/ 3 166 169
281  
282 166 ▶ return $self->_known_affinity($db_type) // 'numeric';
Condition100%2/2
$self->_known_affinity($db_type) // "numeric"
l!l
1651
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
283 }
284  
285 =head1 PRIVATE METHODS
286  
287 =over 4
288  
289 =item $version = $class->_server_version($dbh)
290  
291 C<SELECT sqlite_version()>, through C<_version_number>.
292  
293 =back
294  
295 =cut
296  
297 368 ▶sub _server_version {
Subroutine _server_version: called CC=1 SCAR=0.0
298 368 my $class = shift;
299 368 my ($dbh) = @_;
300 368 my ($version) = $dbh->selectrow_array('SELECT sqlite_version()');
301 368 return $class->_version_number($version);
302 }
303  
304 65 1;
305  
306 __END__
« blib/lib/DBIx/QuickORM/Dialect/PostgreSQL.pm ↑ Summary blib/lib/DBIx/QuickORM/Exception/DB.pm »