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

filter: Statementstmt 100% 48 / 48 Branchbran 100% 10 / 10 Conditioncond 87.5% 7 / 8 MC/DCmcdc 80.0% 4 / 5 Subroutinesub 100% 16 / 16 Podpod 100% 11 / 11 totaltotal 97.9% 96 / 98 CC 9 SCAR 11.0
CC
9
Cov
98%
affinity
7.0
supports_second_connection
3.0
BEGIN
1.0
CRAP 3.0
« blib/lib/DBIx/QuickORM/Value.pm ↑ Summary blib/lib/DBIx/QuickORM/Exception/DB.pm »
1 package DBIx::QuickORM::Dialect::SQLite;
2 56 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 56 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 56 our $VERSION = '0.000031';
6  
7 56 ▶use DBD::SQLite::Constants qw/DBD_SQLITE_STRING_MODE_UNICODE_STRICT/;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 56 ▶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 306 ▶sub namespace { 'main' }
Subroutine namespace: called CC=1 SCAR=0.0
Pod documented
51 30 ▶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 307 ▶sub init_session {
Subroutine init_session: called CC=1 SCAR=0.0
Pod documented
77 307 my $self = shift;
78 307 my ($dbh) = @_;
79 307 $dbh->{sqlite_string_mode} = DBD_SQLITE_STRING_MODE_UNICODE_STRICT;
80 307 $dbh->{sqlite_extended_result_codes} = 1;
81 307 $dbh->do('PRAGMA foreign_keys = ON');
82 307 return;
83 }
84  
85 my %NATIVE_KIND = (
86 56 (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 39 ▶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>)
127 belongs to its one connection. Another would open a new, empty database.
128  
129 =back
130  
131 =cut
132  
133 47 ▶sub supports_second_connection {
Subroutine supports_second_connection: called CC=3 SCAR=11.0
Pod documented
134 47 my $self = shift;
135 47 my ($dbh) = @_;
136 47 my $file = $dbh->sqlite_db_filename;
137 47 ▶ return defined($file) && length($file) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
defined $file && length $file ? : 43 4 47
Condition66%2/3
defined $file and length $file
!ll&&!rl&&r
0443
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
138 }
139  
140 =pod
141  
142 =over 4
143  
144 =item $sql = $dialect->tables_sql
145  
146 =item $sql = $dialect->fields_sql
147  
148 =item $sql = $dialect->pk_sql
149  
150 =item $sql = $dialect->unique_sql
151  
152 =item $sql = $dialect->fk_sql
153  
154 The scan queries (see L<C<scan>|DBIx::QuickORM::Dialect/"PUBLIC METHODS">),
155 over C<main.sqlite_master> and the table pragmas, leaving out SQLite's own
156 C<sqlite_*> tables.
157  
158 Fields come from C<pragma_table_xinfo>, so generated columns are included and
159 the hidden columns of virtual tables are not.
160  
161 A column the database fills has a default, is generated, or is the rowid
162 alias. The alias is a key column of a table with no C<pk> index: that rules
163 out C<INT PRIMARY KEY>, C<INTEGER PRIMARY KEY DESC>, C<WITHOUT ROWID>
164 tables, and compound keys, as SQLite does. Generated columns refuse values.
165  
166 =back
167  
168 =cut
169  
170 56 my $TABLES = <<'EOT';
171 FROM main.sqlite_master m
172 EOT
173  
174 56 my $USER_TABLES = <<'EOT';
175 WHERE m.type = 'table'
176 AND m.name NOT LIKE 'sqlite\_%' ESCAPE '\'
177 EOT
178  
179 286 ▶sub tables_sql { "SELECT m.name\n$TABLES$USER_TABLES" }
Subroutine tables_sql: called CC=1 SCAR=0.0
Pod documented
180  
181 # hidden = 1 marks the hidden columns of virtual tables. Generated columns use 2 and 3.
182 286 ▶sub fields_sql {
Subroutine fields_sql: called CC=1 SCAR=0.0
Pod documented
183 286 return <<" EOT";
184 SELECT m.name, x.name, x.type, NULL,
185 CASE WHEN x.hidden IN (2, 3)
186 OR upper(x.dflt_value) <> 'NULL'
187 OR (x.pk > 0 AND NOT EXISTS (SELECT 1 FROM pragma_index_list(m.name, 'main') il WHERE il.origin = 'pk'))
188 THEN 1 END,
189 CASE WHEN x.hidden IN (2, 3) THEN 1 END
190 $TABLES
191 JOIN pragma_table_xinfo(m.name, 'main') x
192 $USER_TABLES
193 AND x.hidden != 1
194 EOT
195 }
196  
197 286 ▶sub pk_sql {
Subroutine pk_sql: called CC=1 SCAR=0.0
Pod documented
198 286 return <<" EOT";
199 SELECT m.name, x.name
200 $TABLES
201 JOIN pragma_table_xinfo(m.name, 'main') x
202 $USER_TABLES
203 AND x.pk > 0
204 ORDER BY m.name, x.pk
205 EOT
206 }
207  
208 # An expression column has no name.
209 286 ▶sub unique_sql {
Subroutine unique_sql: called CC=1 SCAR=0.0
Pod documented
210 286 return <<" EOT";
211 SELECT m.name, il.name, ii.name
212 $TABLES
213 JOIN pragma_index_list(m.name, 'main') il
214 JOIN pragma_index_info(il.name, 'main') ii
215 $USER_TABLES
216 AND il."unique" = 1
217 AND il.partial = 0
218 EOT
219 }
220  
221 286 ▶sub fk_sql {
Subroutine fk_sql: called CC=1 SCAR=0.0
Pod documented
222 286 return <<" EOT";
223 SELECT m.name, fk.id, fk."from", fk."table", fk."to"
224 $TABLES
225 JOIN pragma_foreign_key_list(m.name, 'main') fk
226 $USER_TABLES
227 ORDER BY m.name, fk.id, fk.seq
228 EOT
229 }
230  
231 =pod
232  
233 =over 4
234  
235 =item $affinity = $dialect->affinity($db_type)
236  
237 SQLite's own rules on the declared type, then the inherited lookup, then
238 C<numeric>. The rules, in order:
239  
240 INT numeric
241 CHAR, CLOB, TEXT string
242 BLOB, or no type binary
243 REAL, FLOA, DOUB numeric
244  
245 =back
246  
247 =cut
248  
249 1956 ▶sub affinity {
Subroutine affinity: called CC=7 SCAR=19.5
Pod documented
250 1956 my $self = shift;
251 1956 my ($db_type) = @_;
252  
253 1956 my $t = uc($db_type);
254 1956 ▶ return 'numeric' if $t =~ m/INT/;
Branch: 1 branch
BranchTrueFalseTotal
if $t =~ /INT/ 824 1132 1956
255 1132 ▶ return 'string' if $t =~ m/CHAR|CLOB|TEXT/;
Branch: 1 branch
BranchTrueFalseTotal
if $t =~ /CHAR|CLOB|TEXT/ 926 206 1132
256 206 ▶ return 'binary' if $t =~ m/BLOB/ || $t !~ m/\S/;
Branch: 1 branch
BranchTrueFalseTotal
if $t =~ /BLOB/ or not $t =~ /\S/ 20 186 206
Condition100%3/3
$t =~ /BLOB/ or not $t =~ /\S/
l!l&&r!l&&!r
164186
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
257 186 ▶ return 'numeric' if $t =~ m/REAL|FLOA|DOUB/;
Branch: 1 branch
BranchTrueFalseTotal
if $t =~ /REAL|FLOA|DOUB/ 5 181 186
258  
259 181 ▶ return $self->_known_affinity($db_type) // 'numeric';
Condition100%2/2
$self->_known_affinity($db_type) // "numeric"
l!l
1801
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
260 }
261  
262 =head1 PRIVATE METHODS
263  
264 =over 4
265  
266 =item $version = $class->_server_version($dbh)
267  
268 C<SELECT sqlite_version()>, through C<_version_number>.
269  
270 =back
271  
272 =cut
273  
274 309 ▶sub _server_version {
Subroutine _server_version: called CC=1 SCAR=0.0
275 309 my $class = shift;
276 309 my ($dbh) = @_;
277 309 my ($version) = $dbh->selectrow_array('SELECT sqlite_version()');
278 309 return $class->_version_number($version);
279 }
280  
281 56 1;
282  
283 __END__
« blib/lib/DBIx/QuickORM/Value.pm ↑ Summary blib/lib/DBIx/QuickORM/Exception/DB.pm »