lib/DBIx/Oro/Driver/SQLite.pm

filter: Statementstmt 85.8% 260 / 303 Branchbran 61.7% 100 / 162 Conditioncond 58.4% 52 / 89 MC/DCmcdc 26.5% 13 / 49 Subroutinesub 94.5% 35 / 37 Podpod 100% 18 / 18 totaltotal 72.6% 478 / 658 SCAR 59.3
CC
123
Cov
74%
foreign_keys
42.0
delete
29.9
insert
28.2
CRAP 377.8
« lib/DBIx/Oro.pm ↑ Summary blib/lib/DBIx/Oro/Driver/MySQL.pm »
1 package DBIx::Oro::Driver::SQLite;
2 24 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 24 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 24 ▶use DBIx::Oro;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 24 our @ISA;
6 24 ▶BEGIN { @ISA = 'DBIx::Oro' };
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 24 ▶use v5.10.1;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 # Todo: use 'truncate' for table deletion
11  
12 # Defaults to 500 for SQLITE_MAX_COMPOUND_SELECT
13 24 our $MAX_COMP_SELECT;
14  
15 BEGIN {
16 24 ▶ $MAX_COMP_SELECT = 500;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 };
18  
19 24 ▶use Carp qw/carp/;
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 # Find and create database file
22 24 ▶use File::Path;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 24 ▶use File::Basename;
Subroutine BEGIN: called CC=1 SCAR=0.0
24  
25 # Default arguments for snippet function
26 24 my @arguments =
27 qw/start end ellipsis column token/;
28  
29 24 my $arguments = qr/^(?:start|end|ellipsis|column|token)$/;
30  
31 24 my @default = ('<b>', '</b>', '<b>...</b>', -1, -15);
32  
33  
34 # Constructor
35 sub new {
36 28 ▶ my $class = shift;
Subroutine new: called CC=10 SCAR=23.5
Pod documented
37 28 my %param = @_;
38 28 ▶ $param{created} //= 0;
Condition50%1/2
$param{"created"} //= 0
l!l
028
MC/DC0%0/1
$param{"created"} //= 0
$param{"created"}
Truth table50%1/2
A
A= $param{"created"}
Aresult
11
00
39  
40 28 my $autocommit = delete $param{autocommit};
41  
42 # Bless object with hash
43 28 my $self = bless \%param, $class;
44  
45 # Store filename
46 28 ▶ my $file = $self->{file} = $param{file} // '';
Condition50%1/2
$param{"file"} // ""
l!l
280
MC/DC0%0/1
$param{"file"} // ""
$param{"file"}
Truth table50%1/2
A
A= $param{"file"}
Aresult
11
00
47  
48 # Temporary or memory file
49 28 ▶ if (!$file || $file eq ':memory:') {
Branch: 2 branches
BranchTrueFalseTotal
if (not $file or $file eq ':memory:') { } 25 3 28
elsif (not -e $file) { } 2 1 3
Condition100%3/3
not $file or $file eq ":memory:"
l!l&&r!l&&!r
1243
MC/DC100%2/2
not $file or $file eq ":memory:"
not $file $file eq ":memory:"
Truth table100%3/3
not(A) or B
A= not $fileB= $file eq ":memory:"
ABresult
1X1
011
000
50 25 $self->{created} = 1;
51 }
52  
53 # Create path for file - based on ORLite
54 elsif (!-e $file) {
55  
56 2 my $dir = File::Basename::dirname($file);
57 2 ▶ unless (-d $dir) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-d $dir) 0 2 2
58 0 File::Path::mkpath( $dir, { verbose => 0 } );
59 };
60  
61 # Touch the file
62 2 ▶ if (open(TOUCH,'>' . $file)) {
Branch: 1 branch
BranchTrueFalseTotal
if (open TOUCH, '>' . $file) 2 0 2
63 2 $self->{created} = 1;
64 2 close(TOUCH);
65 };
66 };
67  
68 # Data source name
69 28 $self->{dsn} = 'dbi:SQLite:dbname=' . $self->{file};
70  
71 # Attach hash
72 28 ▶ $self->{attached} = {} unless exists $self->{attached};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"attached"} 28 0 28
73  
74 # Autocommit
75 28 ${$self->{autocommit}} =
76 28 ${$self->{_autocounter}} = 0;
77  
78 # Set autocommit
79 28 ▶ $self->autocommit($autocommit) if $autocommit;
Branch: 1 branch
BranchTrueFalseTotal
if $autocommit 0 28 28
80  
81 # Return object
82 28 $self;
83 };
84  
85  
86 # Initialize database if newly created
87 sub _init {
88 28 ▶ my $self = shift;
Subroutine _init: called CC=9 SCAR=26.0
89  
90 # Get callback
91 my $cb = delete $self->{init} if $self->{init} &&
92 28 ▶ (ref $self->{init} || '') eq 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"init"} and (ref $self->{"init"} || "") eq "CODE" 6 22 28
Condition60%3/5
$self->{"init"} and (ref $self->{"init"} || "") eq "CODE"
!ll&&!rl&&r
2206
ref $self->{'init'} || ''
l!l
60
MC/DC50%1/2
$self->{"init"} and (ref $self->{"init"} || "") eq "CODE"
$self->{"init"} ref $self->{'init'}
Truth table66%2/3
A and B
A= $self->{"init"}B= ref $self->{'init'}
ABresult
0X0
100
111
93  
94 # Import SQL file
95 28 my $import = delete $self->{import};
96 28 my $import_cb = delete $self->{import_cb};
97  
98 # Initialize database if newly created
99 28 ▶ if ($self->created && ($import || $cb)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->created and $import || $cb) 6 22 28
Condition83%5/6
$self->created and $import || $cb
!ll&&!rl&&r
1216
$import or $cb
l!l&&r!l&&!r
0621
MC/DC66%2/3
$self->created and $import || $cb
$self->created $import $cb
Truth table75%3/4
A and B or C
A= $self->createdB= $importC= $cb
ABCresult
0XX0
1000
11X1
1011
100  
101 # Start creation transaction
102 6 ▶ unless (
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->txn(sub { if ($import) { $self->import_sql($import, $import_cb) or return -1; } ; if ($cb) { local $_ = $self; return &$cb($self); } ; return 1; } )) 0 6 6
103 $self->txn(
104 sub {
105  
106 # Import SQL file
107 6 ▶ if ($import) {
Subroutine __ANON__: called CC=4 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
if ($import) 0 6 6
108 0 ▶ $self->import_sql($import, $import_cb) or return -1;
Branch: 1 branch
BranchTrueFalseTotal
$self->import_sql($import, $import_cb) or return -1 0 0 0
109 };
110  
111 # Release callback
112 6 ▶ if ($cb) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cb) 6 0 6
113 6 local $_ = $self;
114 6 return $cb->($self);
115 };
116 0 return 1;
117 })
118 ) {
119  
120 # Unlink SQLite database
121 0 ▶ if (index($self->file, ':') != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (index($self->file, ":") != 0) 0 0 0
122 0 unlink $self->file;
123 };
124  
125 # Not successful
126 0 $self = undef;
127 0 return;
128 };
129 };
130  
131 28 return 1;
132 };
133  
134  
135 # Connect to database
136 sub _connect {
137 31 ▶ my $self = shift;
Subroutine _connect: called CC=5 SCAR=16.8
138 my $dbh = $self->SUPER::_connect(
139 31 ▶ sqlite_unicode => defined $self->{unicode} ? $self->{unicode} : 1
Branch: 1 branch
BranchTrueFalseTotal
defined $self->{'unicode'} ? : 1 30 31
140 );
141  
142 # Turn foreign keys on as default
143 31 ▶ $dbh->do('PRAGMA foreign_keys = ON') unless $self->{foreign_keys};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"foreign_keys"} 31 0 31
144  
145 # Set busy timeout
146 31 ▶ $dbh->sqlite_busy_timeout( $self->{busy_timeout} || 300 );
Condition50%1/2
$self->{'busy_timeout'} || 300
l!l
031
MC/DC0%0/1
$self->{'busy_timeout'} || 300
$self->{'busy_timeout'}
Truth table50%1/2
A
A= $self->{'busy_timeout'}
Aresult
11
00
147  
148 # Reattach possibly attached databases
149 31 ▶ while (my ($db_name, $file) = each %{$self->{attached}}) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$$self{"attached"};}) 0 31 31
150 0 $self->prep_and_exec("ATTACH '$file' AS ?", [$db_name]);
151 };
152  
153 # Return database handle
154 31 $dbh;
155 };
156  
157  
158 # File of database
159 0 ▶sub file { $_[0]->{file} // '' };
Subroutine file: not called CC=2 SCAR=17.9
Pod documented
Condition0%0/2
$_[0]{"file"} // ""
l!l
00
MC/DC0%0/1
$_[0]{"file"} // ""
$_[0]{"file"}
Truth table0%0/2
A
A= $_[0]{"file"}
Aresult
11
00
160  
161  
162 # Database driver
163 1 ▶sub driver { 'SQLite' };
Subroutine driver: called CC=1 SCAR=0.0
Pod documented
164  
165  
166 # Database was just created
167 sub created {
168 29 ▶ my $self = shift;
Subroutine created: called CC=5 SCAR=18.9
Pod documented
169  
170 # Creation state is 0
171 29 ▶ return 0 unless $self->{created};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"created"} 1 28 29
172  
173 # Check for thread id
174 28 ▶ if (defined $self->{tid} && $self->{tid} != threads->tid) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $self->{'tid'} and $self->{'tid'} != 'threads'->tid) { } 0 28 28
elsif ($self->{'pid'} != $$) { } 0 28 28
Condition33%1/3
defined $self->{"tid"} and $self->{"tid"} != "threads"->tid
!ll&&!rl&&r
2800
MC/DC0%0/2
defined $self->{"tid"} and $self->{"tid"} != "threads"->tid
defined $self->{"tid"} $self->{"tid"} != "threads"->tid
Truth table33%1/3
A and B
A= defined $self->{"tid"}B= $self->{"tid"} != "threads"->tid
ABresult
0X0
100
111
175 0 return ($self->{created} = 0);
176 }
177  
178 # Check for process id
179 elsif ($self->{pid} != $$) {
180 0 return ($self->{created} = 0);
181 };
182  
183 # Return creation state
184 28 return 1;
185 };
186  
187  
188 # Explain query plan
189 sub explain {
190 1 ▶ my $self = shift;
Subroutine explain: called CC=3 SCAR=11.0
Pod documented
191  
192 # Prepare and execute explain query plan
193 1 my ($rv, $sth) = $self->prep_and_exec(
194 'EXPLAIN QUERY PLAN ' . shift, @_
195 );
196  
197 # Query was not succesfull
198 1 ▶ return unless $rv;
Branch: 1 branch
BranchTrueFalseTotal
unless $rv 0 1 1
199  
200 # Create string
201 1 my $string;
202 1 foreach ( @{ $sth->fetchall_arrayref([]) }) {
203 2 $string .= sprintf("%3d | %3d | %3d | %-60s\n", @$_);
204 };
205  
206 # Return query plan string
207 1 return $string;
208 };
209  
210  
211 # Delete with SQLite feature
212 sub delete {
213 11 ▶ my $self = shift;
Subroutine delete: called CC=11 SCAR=34.0
Pod documented
214 11 my $secure;
215  
216 # Check if -secure parameter is set
217 11 ▶ if ($_[-1] && ref $_[-1] && ref $_[-1] eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if ($_[-1] and ref $_[-1] and ref $_[-1] eq "HASH") 5 6 11
Condition66%4/6
$_[-1] and ref $_[-1] and ref $_[-1] eq "HASH"
!ll&&!rl&&r
605
$_[-1] and ref $_[-1]
!ll&&!rl&&r
065
MC/DC33%1/3
$_[-1] and ref $_[-1] and ref $_[-1] eq "HASH"
$_[-1] ref $_[-1] ref $_[-1] eq "HASH"
Truth table50%2/4
A and B and C
A= $_[-1]B= ref $_[-1]C= ref $_[-1] eq "HASH"
ABCresult
0XX0
10X0
1100
1111
218 5 ▶ $secure = delete $_[-1]->{-secure} || 0;
Condition50%1/2
delete $_[-1]{'-secure'} || 0
l!l
05
MC/DC0%0/1
delete $_[-1]{'-secure'} || 0
delete $_[-1]{'-secure'}
Truth table50%1/2
A
A= delete $_[-1]{'-secure'}
Aresult
11
00
219 };
220  
221 # Delete
222 11 ▶ unless ($secure) {
Branch: 1 branch
BranchTrueFalseTotal
if ($secure) { } 0 11 11
223  
224 11 my $rv = $self->SUPER::delete(@_);
225  
226 # Decrement autocommit
227 11 ▶ $self->_decr_commit if $rv;
Branch: 1 branch
BranchTrueFalseTotal
if $rv 7 4 11
228  
229 11 return $rv;
230 }
231  
232 # Delete securely
233 else {
234  
235 # Security value
236 0 my $sec_value;
237  
238 # Retrieve secure delete pragma
239 0 my ($rv, $sth) = $self->prep_and_exec('PRAGMA secure_delete');
240 0 ▶ $sec_value = $sth->fetchrow_array if $rv;
Branch: 1 branch
BranchTrueFalseTotal
if $rv 0 0 0
241 0 $sth->finish;
242  
243 # Set secure_delete pragma
244 0 ▶ $self->do('PRAGMA secure_delete = ON') unless $sec_value;
Branch: 1 branch
BranchTrueFalseTotal
unless $sec_value 0 0 0
245  
246 # Delete
247 0 $rv = $self->SUPER::delete(@_);
248  
249 # Decrement autocommit
250 0 ▶ $self->_decr_commit if $rv;
Branch: 1 branch
BranchTrueFalseTotal
if $rv 0 0 0
251  
252 # Reset secure_delete pragma
253 0 ▶ $self->do('PRAGMA secure_delete = OFF') unless $sec_value;
Branch: 1 branch
BranchTrueFalseTotal
unless $sec_value 0 0 0
254  
255 # Return value
256 0 return $rv;
257 };
258 };
259  
260  
261 # Insert values to database
262 sub insert {
263 939 ▶ my $self = shift;
Subroutine insert: called CC=23 SCAR=33.4
Pod documented
264  
265 # Get table name
266 939 ▶ my $table = $self->_table_name(\@_) or return;
Branch: 1 branch
BranchTrueFalseTotal
my $table = $self->_table_name(\@_) or return 0 939 939
267  
268 # No parameters
269 939 ▶ return unless $_[0];
Branch: 1 branch
BranchTrueFalseTotal
unless $_[0] 0 939 939
270  
271 # Properties
272 939 ▶ my $prop = shift if ref $_[0] eq 'HASH' && ref $_[1];
Branch: 1 branch
BranchTrueFalseTotal
if ref $_[0] eq "HASH" and ref $_[1] 0 939 939
Condition66%2/3
ref $_[0] eq "HASH" and ref $_[1]
!ll&&!rl&&r
349050
MC/DC0%0/2
ref $_[0] eq "HASH" and ref $_[1]
ref $_[0] eq "HASH" ref $_[1]
Truth table66%2/3
A and B
A= ref $_[0] eq "HASH"B= ref $_[1]
ABresult
0X0
100
111
273  
274 # Single insert
275 939 ▶ if (ref $_[0] eq 'HASH') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $_[0] eq 'HASH') { } 905 34 939
elsif (ref $_[0] eq 'ARRAY') { } 34 0 34
276  
277 # Param
278 905 my %param = %{ shift(@_) };
279  
280 # Create insert arrays
281 905 my (@keys, @values);
282 905 ▶ while (my ($key, $value) = each %param) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %param) 1351 905 2256
283 # Insert pairs
284 1351 ▶ next if !ref $key && $key !~ $DBIx::Oro::KEY_REGEX;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $key or $key =~ /$DBIx::Oro::KEY_REGEX/ 0 1351 1351
Condition33%1/3
ref $key or $key =~ /$DBIx::Oro::KEY_REGEX/
l!l&&r!l&&!r
013510
MC/DC0%0/2
ref $key or $key =~ /$DBIx::Oro::KEY_REGEX/
ref $key $key =~ /$DBIx::Oro::KEY_REGEX/
Truth table33%1/3
A or B
A= ref $keyB= $key =~ /$DBIx::Oro::KEY_REGEX/
ABresult
1X1
011
000
285 1351 push @keys, $key;
286 1351 push @values, $value;
287 };
288  
289 # Nothing to insert
290 905 ▶ return unless @keys;
Branch: 1 branch
BranchTrueFalseTotal
unless @keys 0 905 905
291  
292 # Create insert string
293 905 my $sql = 'INSERT ';
294  
295 905 ▶ if ($prop && (my $oc = $prop->{-on_conflict})) {
Branch: 1 branch
BranchTrueFalseTotal
if ($prop and my $oc = $prop->{"-on_conflict"}) 0 905 905
Condition33%1/3
$prop and my $oc = $prop->{"-on_conflict"}
!ll&&!rl&&r
90500
MC/DC0%0/2
$prop and my $oc = $prop->{"-on_conflict"}
$prop my $oc = $prop->{"-on_conflict"}
Truth table33%1/3
A and B
A= $propB= my $oc = $prop->{"-on_conflict"}
ABresult
0X0
100
111
296 0 ▶ if ($oc eq 'replace') {
Branch: 2 branches
BranchTrueFalseTotal
if ($oc eq 'replace') { } 0 0 0
elsif ($oc eq 'ignore') { } 0 0 0
297 0 $sql = 'REPLACE '
298 }
299 elsif ($oc eq 'ignore') {
300 0 $sql .= 'IGNORE '
301 };
302 };
303  
304 $sql .= 'INTO ' . $table .
305 905 ' (' . join(', ', map { "`$_`" } @keys) . ') VALUES (' . DBIx::Oro::_q(\@values) . ')';
306  
307 # Prepare and execute
308 905 my $rv = $self->prep_and_exec( $sql, \@values );
309  
310 # Decrement autocommit
311 905 ▶ $self->_decr_commit if $rv;
Branch: 1 branch
BranchTrueFalseTotal
if $rv 902 3 905
312  
313 905 return $rv;
314 }
315  
316 # Multiple inserts
317 elsif (ref($_[0]) eq 'ARRAY') {
318  
319 34 ▶ return unless $_[1];
Branch: 1 branch
BranchTrueFalseTotal
unless $_[1] 1 33 34
320  
321 33 my @keys = @{ shift(@_) };
322  
323 # Default values
324 33 my @default = ();
325  
326 # Check if keys are defaults
327 33 my $i = 0;
328 33 my @default_keys;
329 33 ▶ while ($keys[$i]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($keys[$i]) 89 33 122
330  
331 # No default - next
332 89 ▶ $i++, next unless ref $keys[$i];
Branch: 1 branch
BranchTrueFalseTotal
unless ref $keys[$i] 81 8 89
333  
334 # Has default value
335 8 my ($key, $value) = @{ splice( @keys, $i, 1) };
336 8 push(@default_keys, $key);
337 8 push(@default, $value);
338 };
339  
340 # Unshift default keys to front
341 33 unshift(@keys, @default_keys);
342  
343 33 my $sql = 'INSERT INTO ' . $table . ' (' . join(', ', map { "`$_`" } @keys) . ') ';
344 33 my $union = 'SELECT ' . DBIx::Oro::_q(\@keys);
345  
346 # Maximum bind variables
347 33 my $max = ($MAX_COMP_SELECT / @keys) - @keys;
348  
349 33 ▶ if (scalar @_ <= $max) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @_ <= $max) { } 30 3 33
350  
351 # Add data unions
352 30 $sql .= $union . ((' UNION ' . $union) x ( scalar(@_) - 1 ));
353  
354 # Prepare and execute with prepended defaults
355 my @rv = $self->prep_and_exec(
356 $sql,
357 30 [ map { (@default, @$_); } @_ ]
358 );
359  
360 # Decrement autocommit
361 30 ▶ $self->_decr_commit if $rv[0];
Branch: 1 branch
BranchTrueFalseTotal
if $rv[0] 28 2 30
362  
363 30 return @rv;
364 }
365  
366 # More than SQLite MAX_COMP_SELECT insertions
367 else {
368  
369 3 my ($rv, @v_array);
370 3 my @values = @_;
371  
372 # Start transaction
373 $self->txn(
374 sub {
375 3 ▶ while (@v_array = splice(@values, 0, $max - 1)) {
Subroutine __ANON__: called CC=4 SCAR=14.0
Branch: 1 branch
BranchTrueFalseTotal
if (@v_array = splice(@values, 0, $max - 1)) 28 3 31
376  
377 # Delete undef values
378 28 ▶ @v_array = grep($_, @v_array) unless @_;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 0 28 28
379  
380 # Add data unions
381 28 my $sub_sql = $sql . $union .
382 ((' UNION ' . $union) x ( scalar(@v_array) - 1 ));
383  
384 # Prepare and execute
385 my $rv_part = $self->prep_and_exec(
386 $sub_sql,
387 28 [ map { (@default, @$_); } @v_array ]
388 );
389  
390 # Rollback transaction
391 28 ▶ return -1 unless $rv_part;
Branch: 1 branch
BranchTrueFalseTotal
unless $rv_part 0 28 28
392 28 $rv += $rv_part;
393 };
394  
395 3 ▶ }) or return;
Branch: 1 branch
BranchTrueFalseTotal
$self->txn(sub { while (@v_array = splice(@values, 0, $max - 1)) { @v_array = grep($_, @v_array) unless @_; my $sub_sql = $sql . $union . (' UNION ' . $union) x (scalar(@v_array) - 1); my $rv_part = $self->prep_and_exec($sub_sql, [map({@default, @$_;} @v_array)]); return -1 unless $rv_part; $rv += $rv_part; } ; } ) or return 0 3 3
396  
397 # Decrement autocommit
398 3 ▶ $self->_decr_commit if $rv;
Branch: 1 branch
BranchTrueFalseTotal
if $rv 3 0 3
399  
400 # Everything went fine
401 3 return $rv;
402 };
403 };
404  
405 # Unknown query
406 0 return;
407 };
408  
409  
410 # Update existing values in the database
411 sub update {
412 25 ▶ my $self = shift;
Subroutine update: called CC=2 SCAR=6.9
Pod documented
413  
414 25 my $rv = $self->SUPER::update(@_);
415  
416 # Decrement autocommit
417 25 ▶ $self->_decr_commit if $rv;
Branch: 1 branch
BranchTrueFalseTotal
if $rv 16 9 25
418  
419 25 return $rv;
420 };
421  
422  
423 sub merge {
424 10 ▶ my $self = shift;
Subroutine merge: called CC=5 SCAR=17.3
Pod documented
425  
426 10 my ($rv, $type) = $self->SUPER::merge(@_);
427  
428 10 ▶ if ($rv && $type eq 'insert' && ${$self->{autocommit}}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rv and $type eq "insert" and ${$self->{"autocommit"};}) 0 10 10
Condition83%5/6
$rv and $type eq "insert" and ${$self->{"autocommit"};}
!ll&&!rl&&r
640
$rv and $type eq "insert"
!ll&&!rl&&r
154
MC/DC0%0/3
$rv and $type eq "insert" and ${$self->{"autocommit"};}
$rv $type eq "insert" ${$self->{"autocommit"};}
Truth table75%3/4
A and B and C
A= $rvB= $type eq "insert"C= ${$self->{"autocommit"};}
ABCresult
0XX0
10X0
1100
1111
429 0 ${$self->{_autocounter}}--;
430 };
431  
432 10 ▶ return wantarray ? ($rv, $type) : $rv;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 10 10
433 };
434  
435 # Attach database
436 sub attach {
437 6 ▶ my ($self, $db_name, $file) = @_;
Subroutine attach: called CC=2 SCAR=6.9
Pod documented
438  
439 6 ▶ $file //= '';
Condition100%2/2
$file //= ""
l!l
24
MC/DC100%1/1
$file //= ""
$file
Truth table100%2/2
A
A= $file
Aresult
11
00
440  
441 # Attach file, memory or temporary database
442 6 my $rv = scalar $self->prep_and_exec("ATTACH '$file' AS ?", [$db_name]);
443  
444 6 $self->{attached}->{$db_name} = $file;
445  
446 6 return $rv;
447 };
448  
449  
450 # Detach database
451 sub detach {
452 4 ▶ my $self = shift;
Subroutine detach: called CC=4 SCAR=13.9
Pod documented
453 4 ▶ return unless $_[0];
Branch: 1 branch
BranchTrueFalseTotal
unless $_[0] 0 4 4
454  
455 # Detach all databases
456 4 foreach my $db_name (@_) {
457 5 delete $self->{attached}->{$db_name};
458 5 ▶ return unless $self->prep_and_exec('DETACH ?', [$db_name]);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->prep_and_exec("DETACH ?", [$db_name]) 1 4 5
459 };
460  
461 3 return 1;
462 };
463  
464  
465 # Wrapper for sqlite last_insert_row_id
466 sub last_insert_id {
467 14 ▶ shift->dbh->sqlite_last_insert_rowid;
Subroutine last_insert_id: called CC=1 SCAR=0.0
Pod documented
468 };
469  
470  
471 # Create matchinfo function
472 sub matchinfo {
473 2 ▶ my $self = shift;
Subroutine matchinfo: called CC=3 SCAR=11.9
Pod documented
474  
475 # Use no multibyte characters
476 # use bytes;
477  
478 # Format string
479 2 ▶ my $format = lc(shift) if $_[0] && $_[0] =~ /^[pcnalsx]+$/i;
Branch: 1 branch
BranchTrueFalseTotal
if $_[0] and $_[0] =~ /^[pcnalsx]+$/i 2 0 2
Condition33%1/3
$_[0] and $_[0] =~ /^[pcnalsx]+$/i
!ll&&!rl&&r
002
MC/DC0%0/2
$_[0] and $_[0] =~ /^[pcnalsx]+$/i
$_[0] $_[0] =~ /^[pcnalsx]+$/i
Truth table33%1/3
A and B
A= $_[0]B= $_[0] =~ /^[pcnalsx]+$/i
ABresult
0X0
100
111
480  
481 # Return anonymous subroutine
482 return sub {
483 3 ▶ my $column;
Subroutine __ANON__: called CC=8 SCAR=22.7
484 3 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 3 0 3
485 3 ▶ $column = shift || 'content';
Condition50%1/2
shift() || 'content'
l!l
30
MC/DC0%0/1
shift() || 'content'
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
486  
487 # Format string
488 3 ▶ $format = lc(shift) if $_[0] && $_[0] =~ /^[pcnalsx]+$/i;
Branch: 1 branch
BranchTrueFalseTotal
if $_[0] and $_[0] =~ /^[pcnalsx]+$/i 0 3 3
Condition33%1/3
$_[0] and $_[0] =~ /^[pcnalsx]+$/i
!ll&&!rl&&r
300
MC/DC0%0/2
$_[0] and $_[0] =~ /^[pcnalsx]+$/i
$_[0] $_[0] =~ /^[pcnalsx]+$/i
Truth table33%1/3
A and B
A= $_[0]B= $_[0] =~ /^[pcnalsx]+$/i
ABresult
0X0
100
111
489 };
490  
491 # Sort format for leading 'pc' if needed
492 3 ▶ if ($format) {
Branch: 1 branch
BranchTrueFalseTotal
if ($format) { } 3 0 3
493 3 for ($format) {
494  
495 # Sort alphabetically
496 3 $_ = join('', sort split('', $_));
497  
498 # Delete repeating characters
499 3 s/(.)\1+/$1/g;
500  
501 # Prepend 'pc' if necessary
502 3 ▶ if (/[xals]/) {
Branch: 1 branch
BranchTrueFalseTotal
if (/[xals]/) 3 0 3
503 3 tr/pc//d; # Delete 'pc'
504 3 $_ = 'pc' . $format; # Prepend 'pc'
505 };
506 };
507 }
508  
509 # No format given
510 else {
511 0 $format = 'pcx';
512 };
513  
514 # Return anonymous subroutine
515 return sub {
516 return
517 3 ▶ 'matchinfo(' . $column . ', "' . $format . '")',
Subroutine __ANON__: called CC=1 SCAR=0.0
518 \&_matchinfo_return,
519 $format;
520 3 };
521 2 };
522 };
523  
524  
525 # Treat matchinfo return
526 sub _matchinfo_return {
527 6 ▶ my ($blob, $format) = @_;
Subroutine _matchinfo_return: called CC=10 SCAR=23.1
528  
529 # Get 32-bit blob chunks
530 6 my @matchinfo = unpack('l' . (length($blob) * 4), $blob);
531  
532 # Parse format character
533 6 my %match;
534 6 foreach (split '', $format) {
535  
536 # Characters: p, c, n
537 22 ▶ if ($_ eq 'p' or $_ eq 'c' or $_ eq 'n') {
Branch: 3 branches
BranchTrueFalseTotal
if ($_ eq 'p' or $_ eq 'c' or $_ eq 'n') { } 14 8 22
elsif ($_ eq 'a' or $_ eq 'l' or $_ eq 's') { } 4 4 8
elsif ($_ eq 'x') { } 4 0 4
Condition100%6/6
$_ eq "p" or $_ eq "c" or $_ eq "n"
l!l&&r!l&&!r
1228
$_ eq "p" or $_ eq "c"
l!l&&r!l&&!r
6610
MC/DC100%3/3
$_ eq "p" or $_ eq "c" or $_ eq "n"
$_ eq "p" $_ eq "c" $_ eq "n"
Truth table100%4/4
A or B or C
A= $_ eq "p"B= $_ eq "c"C= $_ eq "n"
ABCresult
1XX1
01X1
0011
0000
538 14 $match{$_} = shift @matchinfo;
539 }
540  
541 # Characters: a, l, s
542 ▶ elsif ($_ eq 'a' or $_ eq 'l' or $_ eq 's') {
Condition83%5/6
$_ eq "a" or $_ eq "l" or $_ eq "s"
l!l&&r!l&&!r
224
$_ eq "a" or $_ eq "l"
l!l&&r!l&&!r
026
MC/DC66%2/3
$_ eq "a" or $_ eq "l" or $_ eq "s"
$_ eq "a" $_ eq "l" $_ eq "s"
Truth table75%3/4
A or B or C
A= $_ eq "a"B= $_ eq "l"C= $_ eq "s"
ABCresult
1XX1
01X1
0011
0000
543 4 $match{$_} = [ splice(@matchinfo, 0, $match{c}) ];
544 }
545  
546 # Characters: x
547 elsif ($_ eq 'x') {
548 4 my @match;
549 4 for (1 .. ($match{p} * $match{c})) {
550 12 push(@match, [ splice(@matchinfo, 0, 3) ]);
551 };
552  
553 4 $match{$_} = \@match;
554 }
555  
556 # Unknown character
557 else {
558 0 shift @matchinfo;
559 };
560 };
561 6 return \%match;
562 };
563  
564  
565 # Create offsets function
566 sub offsets {
567 2 ▶ my $self = shift;
Subroutine offsets: called CC=1 SCAR=0.0
Pod documented
568 2 my $field = shift;
569  
570 # Use no multibyte characters
571 # use bytes;
572  
573 # subroutine
574 return sub {
575 2 ▶ my $column = shift;
Subroutine __ANON__: called CC=3 SCAR=11.2
576 'offsets(' . ($column // $field // 'content') . ')',
577 sub {
578 3 ▶ my $blob = shift;
Subroutine __ANON__: called CC=2 SCAR=6.9
579 3 my @offset;
580 3 my @array = split(/\s/, $blob);
581 3 ▶ while (@array) {
Branch: 1 branch
BranchTrueFalseTotal
if (@array) 6 3 9
582 6 push(@offset, [ splice(@array, 0, 4) ]);
583 };
584 3 return \@offset;
585 }
586 2 ▶ };
Condition40%2/5
($column // $field) // "content"
l!l
20
$column // $field
l!l&&r!l&&!r
200
MC/DC0%0/2
($column // $field) // "content"
$column $field
Truth table33%1/3
A // B
A= $columnB= $field
ABresult
1X1
011
000
587 };
588  
589  
590 # Create snippet function
591 sub snippet {
592 5 ▶ my $self = shift;
Subroutine snippet: called CC=10 SCAR=23.2
Pod documented
593  
594 # Snippet parameters
595 5 my @snippet;
596  
597 # Parameters are given
598 5 ▶ if ($_[0]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_[0]) 3 2 5
599 3 my %snippet = ();
600  
601 # Parameters are given as a hash
602 3 ▶ if ($_[0] =~ $arguments) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_[0] =~ /$arguments/) { } 3 0 3
603 3 %snippet = @_;
604 3 foreach (keys %snippet) {
605 8 ▶ carp "Unknown snippet parameter '$_'" unless $_ =~ $arguments;
Branch: 1 branch
BranchTrueFalseTotal
unless $_ =~ /$arguments/ 0 8 8
606 };
607 }
608  
609 # Parameters are given as an array
610 else {
611 0 @snippet{@arguments} = @_;
612 };
613  
614 # Trim parameter array and fill gaps with defaults
615 3 my ($s, $i) = (0, 0);
616 3 foreach (reverse @arguments) {
617 15 ▶ $s = 1 if defined $snippet{$_};
Branch: 1 branch
BranchTrueFalseTotal
if defined $snippet{$_} 8 7 15
618 15 ▶ unshift(@snippet, $snippet{$_} // $default[$i]) if $s;
Branch: 1 branch
BranchTrueFalseTotal
if $s 12 3 15
Condition66%2/3
$snippet{$_} // $default[$i]
l!l&&r!l&&!r
840
MC/DC0%0/2
$snippet{$_} // $default[$i]
$snippet{$_} $default[$i]
Truth table66%2/3
A // B
A= $snippet{$_}B= $default[$i]
ABresult
1X1
011
000
619 15 $i++;
620 };
621 };
622  
623 # Return anonymous subroutine
624 5 my $sub = 'sub {
625 my $column = $_[0] ? shift : \'content\';
626 my $str = "snippet(" . $column ';
627  
628 5 ▶ if ($snippet[0]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($snippet[0]) 3 2 5
629 3 $sub .= ' . ", ' . join(',', map { '\"' . $_ . '\"' } @snippet) . '"';
630 };
631  
632 5 $sub .= " . \")\";\n};";
633  
634 5 return eval( $sub );
635 };
636  
637  
638 # New table object
639 sub table {
640 26 ▶ my $self = shift;
Subroutine table: called CC=2 SCAR=6.9
Pod documented
641  
642 # Get object from superclass
643 26 my $table = $self->SUPER::table(@_);
644  
645 # Add autocommit parameters
646 26 foreach (qw/autocommit _autocounter
647 foreign_keys/) {
648 78 $table->{$_} = $self->{$_};
649 };
650  
651 # Return blessed object
652 26 return $table;
653 };
654  
655  
656 # Set foreign_key pragma
657 sub foreign_keys {
658 0 ▶ my $self = shift;
Subroutine foreign_keys: not called CC=6 SCAR=37.4
Pod documented
659  
660 # Get pragma
661 0 ▶ unless (defined $_[0]) {
Branch: 3 branches
BranchTrueFalseTotal
if (defined $_[0]) { } 0 0 0
if (do { $_[0] and not $self->{'foreign_keys'} }) { } 0 0 0
elsif (not $_[0] and $self->{'foreign_keys'}) { } 0 0 0
662 0 return $self->{foreign_keys};
663 }
664  
665 # Turn foreign keys on
666 ▶ elsif ($_[0] && !$self->{foreign_keys}) {
Condition0%0/3
$_[0] and not $self->{"foreign_keys"}
!ll&&!rl&&r
000
MC/DC0%0/2
$_[0] and not $self->{"foreign_keys"}
$_[0] not $self->{"foreign_keys"}
Truth table0%0/3
A and not(B)
A= $_[0]B= not $self->{"foreign_keys"}
ABresult
0X0
100
111
667 0 $self->dbh->do('PRAGMA foreign_keys = ON');
668 0 return ($self->{foreign_keys} = 1);
669 }
670  
671 # Turn foreign keys off
672 ▶ elsif (!$_[0] && $self->{foreign_keys}) {
Condition0%0/3
not $_[0] and $self->{"foreign_keys"}
!ll&&!rl&&r
000
MC/DC0%0/2
not $_[0] and $self->{"foreign_keys"}
not $_[0] $self->{"foreign_keys"}
Truth table0%0/3
not(A) and B
A= not $_[0]B= $self->{"foreign_keys"}
ABresult
0X0
100
111
673 0 $self->dbh->do('PRAGMA foreign_keys = OFF');
674 0 return ($self->{foreign_keys} = 0);
675 };
676  
677 0 return;
678 };
679  
680  
681 # Set autocommit
682 sub autocommit {
683 14 ▶ my $self = shift;
Subroutine autocommit: called CC=9 SCAR=23.5
Pod documented
684  
685 # Get autocommit
686 14 ▶ unless (defined $_[0]) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $_[0]) { } 9 5 14
687 5 ▶ return ${$self->{autocommit}} || 0;
Condition50%1/2
${$self->{'autocommit'};} || 0
l!l
50
MC/DC0%0/1
${$self->{'autocommit'};} || 0
${$self->{'autocommit'};}
Truth table50%1/2
A
A= ${$self->{'autocommit'};}
Aresult
11
00
688 }
689  
690 # Set autocommit
691 else {
692 9 my $num = shift;
693 9 my $dbh = $self->dbh;
694  
695 # Is a number
696 9 ▶ if ($num && $num =~ m/^\d+/o) {
Branch: 2 branches
BranchTrueFalseTotal
if ($num and $num =~ /^\d+/o) { } 5 4 9
elsif (not $num) { } 4 0 4
Condition66%2/3
$num and $num =~ /^\d+/o
!ll&&!rl&&r
405
MC/DC50%1/2
$num and $num =~ /^\d+/o
$num $num =~ /^\d+/o
Truth table66%2/3
A and B
A= $numB= $num =~ /^\d+/o
ABresult
0X0
100
111
697 5 ▶ if ($num > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($num > 1) { } 5 0 5
698 5 $dbh->{AutoCommit} = 0;
699 5 ${$self->{autocommit}} =
700 5 ${$self->{_autocounter}} = $num;
701 5 return 1;
702 }
703  
704 else {
705 0 $dbh->{AutoCommit} = 1;
706 0 ${$self->{_autocounter}} =
707 0 ${$self->{autocommit}} = 0;
708 0 return 1;
709 };
710 }
711  
712 # Is null
713 elsif (!$num) {
714 4 ${$self->{autocommit}} = 0;
715 4 ▶ if (${$self->{_autocounter}}) {
Branch: 1 branch
BranchTrueFalseTotal
if (${$self->{"_autocounter"};}) 4 0 4
716 4 ${$self->{_autocounter}} = 0;
717 4 $dbh->commit;
718 };
719 4 ▶ $dbh->{AutoCommit} = 1 unless ${$self->{in_txn}};
Branch: 1 branch
BranchTrueFalseTotal
unless ${$self->{"in_txn"};} 4 0 4
720 4 return 1;
721 }
722  
723 # Failure
724 else {
725 0 return;
726 };
727 };
728 };
729  
730  
731 # Decrement commit counter
732 sub _decr_commit {
733 956 ▶ my $self = shift;
Subroutine _decr_commit: called CC=4 SCAR=13.9
734  
735 # Autocounter is set
736 956 ▶ if (${$self->{_autocounter}}) {
Branch: 1 branch
BranchTrueFalseTotal
if (${$self->{"_autocounter"};}) 66 890 956
737 66 my $auto = --${$self->{_autocounter}};
738  
739 # Commit is null
740 66 ▶ unless ($auto) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($auto) 4 62 66
741  
742 4 ▶ $self->dbh->commit unless ${$self->{in_txn}};
Branch: 1 branch
BranchTrueFalseTotal
unless ${$self->{"in_txn"};} 4 0 4
743 4 ${$self->{_autocounter}} = ${$self->{autocommit}};
744 };
745 };
746 };
747  
748  
749 24 1;
750  
751  
752 __END__
« lib/DBIx/Oro.pm ↑ Summary blib/lib/DBIx/Oro/Driver/MySQL.pm »