blib/lib/CCCP/SQLiteWrap.pm

filter: Statementstmt 12.3% 29 / 235 Branchbran 0.0% 0 / 124 Conditioncond 0.0% 0 / 85 MC/DCmcdc 0.0% 0 / 42 Subroutinesub 36.3% 8 / 22 Podpod 85.7% 12 / 14 totaltotal 9.3% 49 / 522 CC 95 SCAR 64.0
CC
95
Cov
7%
create_table
1482.0
create_trigger
420.0
create_index
240.0
CRAP 599.5
↑ Summary
1 package CCCP::SQLiteWrap;
2  
3 1 ▶use 5.010000;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 1 our $VERSION = '0.01';
7  
8 1 ▶use DBI;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use File::Temp;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use File::Copy;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use Data::UUID;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use Digest::MD5 qw(md5_hex);
Subroutine BEGIN: called CC=1 SCAR=0.0
13
14 1 $CCCP::SQLiteWrap::OnlyPrint = 0;
15  
16 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
17
18 1 my $t_create_pattern = 'CREATE TABLE IF NOT EXISTS %s (%s)';
19 1 my $i_create_pattern = 'CREATE INDEX %s ON %s(%s)';
20 1 my $tr_create_pattern = ['DROP TRIGGER IF EXISTS %s','CREATE TRIGGER IF NOT EXISTS %s %s %s ON %s FOR EACH ROW BEGIN %s; END;'];
21  
22 # one argument - abs path to sqolite base
23 0 ▶sub connect {
Subroutine connect: not called CC=3 SCAR=24.8
Pod documented
24 0 my ($class,$path) = @_;
25
26 0 ▶ if (ref $class) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $class) { } 0 0 0
27 # reconnect
28 0 $class->{db} = DBI->connect('dbi:SQLite:dbname='.$class->path, '', '',{RaiseError => 1, InactiveDestroy => 1});
29 } else {
30 # init new handler
31 0 my $obj = bless(
32 {
33 db => DBI->connect('dbi:SQLite:dbname='.$path, '', '',{RaiseError => 1, InactiveDestroy => 1}),
34 path => $path
35 },
36 $class
37 );
38
39 # check connect error
40 0 ▶ if ($DBI::errstr) {
Branch: 1 branch
BranchTrueFalseTotal
if ($DBI::errstr) 0 0 0
41 0 die $DBI::errstr;
42 };
43
44 0 return $obj;
45 }
46 }
47  
48 0 ▶sub check {
Subroutine check: not called CC=7 SCAR=40.3
Pod documented
49 0 my ($obj) = @_;
50
51 # check live connection
52 0 ▶ unless ($obj->db->ping()) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($obj->db->ping) 0 0 0
53 0 return "Can't ping SQLite base from ".$obj->path."\n";
54 };
55
56 # check database structure
57 0 my $need_rebackup = 0;
58 0 my @table = $obj->show_tables;
59 0 foreach my $table (@table) {
60 0 ▶ next unless $table;
Branch: 1 branch
BranchTrueFalseTotal
unless $table 0 0 0
61 0 eval{$obj->db->selectall_arrayref("SELECT * FROM $table LIMIT 1")};
62 0 ▶ if ($DBI::errstr) {
Branch: 1 branch
BranchTrueFalseTotal
if ($DBI::errstr) 0 0 0
63 0 $need_rebackup++;
64 0 last;
65 };
66 };
67 0 ▶ if ($need_rebackup) {
Branch: 1 branch
BranchTrueFalseTotal
if ($need_rebackup) 0 0 0
68 0 return "SQLite base from ".$obj->path." return error like 'database disk image is malformed' and goto re-dump";
69 0 ▶ return "Bug in re-dump SQLite" unless $obj->redump();
Branch: 1 branch
BranchTrueFalseTotal
unless $obj->redump 0 0 0
70 };
71
72 0 return;
73 }
74  
75 0 ▶sub db {$_[0]->{'db'}}
Subroutine db: not called CC=1 SCAR=6.9
Pod documented
76 0 ▶sub path {$_[0]->{'path'}}
Subroutine path: not called CC=1 SCAR=6.9
Pod documented
77  
78 # return [{'field1'=>'some_value1',...},{'field1'=>'some_value2',...}]
79 0 ▶sub select2arhash {
Subroutine select2arhash: not called CC=1 SCAR=6.9
Pod undocumented
80 0 my ($obj,$query,@param) = @_;
81 0 my $sth = $obj->db->prepare($query);
82 0 $sth->execute(@param);
83 0 return $sth->fetchall_arrayref({});
84 }
85  
86 0 ▶sub create_table {
Subroutine create_table: not called CC=38 SCAR=73.0
Pod documented
87 0 my $obj = shift;
88 0 ▶ return unless (@_ or scalar @_ % 2 == 0);
Branch: 1 branch
BranchTrueFalseTotal
unless @_ or scalar @_ % 2 == 0 0 0 0
Condition0%0/3
@_ or scalar @_ % 2 == 0
l!l&&r!l&&!r
000
MC/DC0%0/2
@_ or scalar @_ % 2 == 0
@_ scalar @_ % 2 == 0
Truth table0%0/3
A or B
A= @_B= scalar @_ % 2 == 0
ABresult
1X1
011
000
89
90 0 my $exisis_table = $obj->show_tables;
91 0 my @new_table = ();
92 0 my @create_table = ();
93 0 my $can_fk = $obj->db->selectrow_arrayref('PRAGMA foreign_keys');
94 0 ▶ while (my ($name,$param) = splice(@_,0,2)) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = splice(@_, 0, 2)) 0 0 0
95 0 ▶ next if (not $name or ref $name or not $param or ref $param ne 'HASH' or not exists $param->{fields});
Branch: 1 branch
BranchTrueFalseTotal
if not $name or ref $name or not $param or ref $param ne "HASH" or not exists $param->{"fields"} 0 0 0
Condition0%0/12
not $name or ref $name or not $param or ref $param ne "HASH" or not exists $param->{"fields"}
l!l&&r!l&&!r
000
not $name or ref $name or not $param or ref $param ne "HASH"
l!l&&r!l&&!r
000
not $name or ref $name or not $param
l!l&&r!l&&!r
000
not $name or ref $name
l!l&&r!l&&!r
000
MC/DC0%0/5
not $name or ref $name or not $param or ref $param ne "HASH" or not exists $param->{"fields"}
not $name ref $name not $param ref $param ne "HASH" not exists $param->{"fields"}
Truth table0%0/6
not(A) or B or not(C) or D or not(E)
A= not $nameB= ref $nameC= not $paramD= ref $param ne "HASH"E= not exists $param->{"fields"}
ABCDEresult
1XXXX1
01XXX1
001XX1
0001X1
000011
000000
96 0 $name = lc($name);
97 0 ▶ next if $exisis_table->{$name}++;
Branch: 1 branch
BranchTrueFalseTotal
if $exisis_table->{$name}++ 0 0 0
98
99 0 my $desc = ''; my @index = ();
100 0 ▶ if (exists $param->{meta}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $param->{"meta"}) 0 0 0
101 # set default value
102 0 ▶ if (exists $param->{meta}->{default} and scalar @{$param->{meta}->{default}} % 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $param->{"meta"}{"default"} and scalar @{$param->{"meta"}{"default"};} % 2) 0 0 0
Condition0%0/2
exists $param->{"meta"}{"default"} and scalar @{$param->{"meta"}{"default"};} % 2
!ll
00
MC/DC0%0/1
exists $param->{"meta"}{"default"} and scalar @{$param->{"meta"}{"default"};} % 2
exists $param->{"meta"}{"default"}
Truth table0%0/2
A
A= exists $param->{"meta"}{"default"}
Aresult
00
11
103 0 ▶ while (my ($fild,$defval) = splice(@{$param->{meta}->{default}},0,2)) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = splice(@{$$param{"meta"}{"default"};}, 0, 2)) 0 0 0
104 0 ▶ if (exists $param->{fields}->{$fild}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $param->{"fields"}{$fild}) 0 0 0
105 0 $param->{fields}->{$fild} .= ' DEFAULT '.$obj->db->quote($defval);
106 };
107 }
108 };
109
110 # set not null
111 0 ▶ if (exists $param->{meta}->{not_null}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $param->{"meta"}{"not_null"}) 0 0 0
112 map {
113 0 ▶ if (exists $param->{fields}->{$_}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $param->{"fields"}{$_}) 0 0 0
114 0 $param->{fields}->{$_} .= ' NOT NULL';
115 };
116 0 } @{$param->{meta}->{not_null}};
117 };
118
119 # set unique
120 0 ▶ if (exists $param->{meta}->{unique}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $param->{"meta"}{"unique"}) 0 0 0
121 map {
122 0 ▶ if (exists $param->{fields}->{$_}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $param->{"fields"}{$_}) 0 0 0
123 0 $param->{fields}->{$_} .= ' UNIQUE';
124 };
125 0 } @{$param->{meta}->{unique}};
126 };
127  
128 # set primary key
129 0 ▶ if (exists $param->{meta}->{pk}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $param->{"meta"}{"pk"}) 0 0 0
130 0 $param->{fields}->{'PRIMARY KEY'} = "(".join(',',map {$obj->db->quote($_)} @{$param->{meta}->{pk}}).")";
131 };
132
133 # set fk
134 0 ▶ if ($can_fk and exists $param->{meta}->{fk}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($can_fk and exists $param->{"meta"}{"fk"}) 0 0 0
Condition0%0/3
$can_fk and exists $param->{"meta"}{"fk"}
!ll&&!rl&&r
000
MC/DC0%0/2
$can_fk and exists $param->{"meta"}{"fk"}
$can_fk exists $param->{"meta"}{"fk"}
Truth table0%0/3
A and B
A= $can_fkB= exists $param->{"meta"}{"fk"}
ABresult
0X0
100
111
135 0 ▶ unless ($can_fk->[0]) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($can_fk->[0]) 0 0 0
136 0 $obj->db->do('PRAGMA foreign_keys = ON');
137 0 $can_fk->[0] = 1;
138 };
139 0 my @fk = @{$param->{meta}->{fk}};
140 0 ▶ if (@fk and scalar @fk % 2 == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@fk and scalar @fk % 2 == 0) 0 0 0
Condition0%0/3
@fk and scalar @fk % 2 == 0
!ll&&!rl&&r
000
MC/DC0%0/2
@fk and scalar @fk % 2 == 0
@fk scalar @fk % 2 == 0
Truth table0%0/3
A and B
A= @fkB= scalar @fk % 2 == 0
ABresult
0X0
100
111
141 0 ▶ while (my ($fk_field,$fk_param) = splice(@fk,0,2)) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = splice(@fk, 0, 2)) 0 0 0
142 # REFERENCES artist(artistid) ON DELETE SET DEFAULT
143 0 ▶ next if (not $fk_field or ref $fk_field or ref $fk_param ne 'HASH' or not exists $param->{fields}->{$fk_field});
Branch: 1 branch
BranchTrueFalseTotal
if not $fk_field or ref $fk_field or ref $fk_param ne "HASH" or not exists $param->{"fields"}{$fk_field} 0 0 0
Condition0%0/9
not $fk_field or ref $fk_field or ref $fk_param ne "HASH" or not exists $param->{"fields"}{$fk_field}
l!l&&r!l&&!r
000
not $fk_field or ref $fk_field or ref $fk_param ne "HASH"
l!l&&r!l&&!r
000
not $fk_field or ref $fk_field
l!l&&r!l&&!r
000
MC/DC0%0/4
not $fk_field or ref $fk_field or ref $fk_param ne "HASH" or not exists $param->{"fields"}{$fk_field}
not $fk_field ref $fk_field ref $fk_param ne "HASH" not exists $param->{"fields"}{$fk_field}
Truth table0%0/5
not(A) or B or C or not(D)
A= not $fk_fieldB= ref $fk_fieldC= ref $fk_param ne "HASH"D= not exists $param->{"fields"}{$fk_field}
ABCDresult
1XXX1
01XX1
001X1
00011
00000
144 0 $param->{fields}->{$fk_field} .= sprintf(' REFERENCES %s(%s)',$fk_param->{table},$fk_param->{field});
145 0 ▶ $param->{fields}->{$fk_field} .= ' ON UPDATE '.$fk_param->{on_update} if exists $fk_param->{on_update};
Branch: 1 branch
BranchTrueFalseTotal
if exists $fk_param->{"on_update"} 0 0 0
146 0 ▶ $param->{fields}->{$fk_field} .= ' ON DELETE '.$fk_param->{on_delete} if exists $fk_param->{on_delete};
Branch: 1 branch
BranchTrueFalseTotal
if exists $fk_param->{"on_delete"} 0 0 0
147 };
148 };
149 };
150
151 # set index
152 0 ▶ if (exists $param->{meta}->{index}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $param->{"meta"}{"index"}) 0 0 0
153 0 my $index = {};
154 0 @index = grep {$_} map {
155 0 my $ind_md5 = md5_hex(join(',',sort {$a cmp $b} @{$_}));
156 $index->{$ind_md5}++ ?
157 undef :
158 sprintf(
159 $i_create_pattern,
160 sprintf('_%s',Data::UUID->new()->create_hex()),
161 $name,
162 0 ▶ join(',',map {$obj->db->quote($_)} @{$_})
Branch: 1 branch
BranchTrueFalseTotal
$index->{$ind_md5}++ ? : 0 0 0
163 );
164 0 } @{$param->{meta}->{index}};
165 };
166
167 };
168
169 my $create_table = sprintf(
170 $t_create_pattern,
171 $name,
172 join(',',
173 0 grep {$_}
174 map {
175 exists $param->{fields}->{$_} ?
176 0 ▶ join(' ',$_ eq 'PRIMARY KEY' ? $_ : $obj->db->quote($_),$param->{fields}->{$_}) :
Branch: 2 branches
BranchTrueFalseTotal
exists $param->{'fields'}{$_} ? : 0 0 0
$_ eq 'PRIMARY KEY' ? : 0 0 0
177 undef
178 0 } ((grep {!/^PRIMARY KEY$/} keys %{$param->{fields}}), 'PRIMARY KEY')
179 )
180 );
181
182 0 ▶ if ($CCCP::SQLiteWrap::OnlyPrint) {
Branch: 1 branch
BranchTrueFalseTotal
if ($CCCP::SQLiteWrap::OnlyPrint) { } 0 0 0
183 0 print join("\n",$create_table,@index);
184 0 print "\n------------------------------\n";
185 } else {
186 0 $obj->do_transaction($create_table,@index)
187 };
188 0 push @new_table,$name;
189 }
190
191 0 ▶ return wantarray() ? @new_table : scalar @new_table;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
192 }
193  
194 # do over transaction
195 0 ▶sub do_transaction {
Subroutine do_transaction: not called CC=4 SCAR=30.0
Pod documented
196 0 my ($obj, @query) = @_;
197 0 ▶ return unless @query;
Branch: 1 branch
BranchTrueFalseTotal
unless @query 0 0 0
198 0 ▶ $obj->db->begin_work or die $obj->db->errstr;
Branch: 1 branch
BranchTrueFalseTotal
$obj->db->begin_work or die $obj->db->errstr 0 0 0
199 0 ▶ map {$obj->db->do($_) if $_} @query;
Branch: 1 branch
BranchTrueFalseTotal
if $_ 0 0 0
200 0 $obj->db->commit;
201 0 return;
202 };
203  
204 0 ▶sub show_tables {
Subroutine show_tables: not called CC=2 SCAR=17.9
Pod undocumented
205 0 ▶ if (wantarray() ){
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray) { } 0 0 0
206 0 return grep {$_!~/^sqlite_/} map {$_=~s/"//g; $_;} $_[0]->db->tables;
207 } else {
208 0 my $tab_hash = {};
209 0 map {$tab_hash->{$_}++} grep {$_!~/^sqlite_/} map {$_=~s/"//g; $_;} $_[0]->db->tables;
210 0 return $tab_hash;
211 };
212 }
213  
214 0 ▶sub close {
Subroutine close: not called CC=1 SCAR=6.9
Pod documented
215 0 my ($obj) = @_;
216 0 $obj->db->disconnect;
217 }
218  
219 # re-dump need for
220 # fix error like "database disk image is malformed"
221 # shutdown server (or kill process) while go insert itnto sqlite-base
222 0 ▶sub redump {
Subroutine redump: not called CC=4 SCAR=30.0
Pod documented
223 0 my ($obj) = @_;
224 0 $obj->close();
225 0 ▶ if (-e $obj->path and -s _) {
Branch: 1 branch
BranchTrueFalseTotal
if (-e $obj->path and -s _) { } 0 0 0
Condition0%0/3
-e $obj->path and -s _
!ll&&!rl&&r
000
MC/DC0%0/2
-e $obj->path and -s _
-e $obj->path -s _
Truth table0%0/3
A and B
A= -e $obj->pathB= -s _
ABresult
0X0
100
111
226 0 my $tmp_file = File::Temp->new()->filename;
227 0 my $dump_command = sprintf('sqlite3 %s ".dump" | sqlite3 %s',$obj->path,$tmp_file);
228 0 system($dump_command);
229 0 move($tmp_file,$obj->path);
230 } else {
231 0 unlink $obj->path;
232 0 my $create_command = sprintf('sqlite3 %s "select 1"',$obj->path);
233 0 system($create_command);
234 };
235 0 $obj->connect();
236 0 ▶ return -e $obj->path ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
-e $obj->path ? : 0 0 0
237 }
238  
239 # $obj->create_index('tablename' => [asfd,asfds,sdf], 'safasf' => [asfdsf,asfd])
240 0 ▶sub create_index {
Subroutine create_index: not called CC=15 SCAR=54.8
Pod documented
241 0 my $obj = shift;
242 0 ▶ return unless (@_ or scalar @_ % 2 == 0);
Branch: 1 branch
BranchTrueFalseTotal
unless @_ or scalar @_ % 2 == 0 0 0 0
Condition0%0/3
@_ or scalar @_ % 2 == 0
l!l&&r!l&&!r
000
MC/DC0%0/2
@_ or scalar @_ % 2 == 0
@_ scalar @_ % 2 == 0
Truth table0%0/3
A or B
A= @_B= scalar @_ % 2 == 0
ABresult
1X1
011
000
243
244 0 my $exisis_table = $obj->show_tables;
245 0 my $ret = 0;
246
247 # geting param
248 0 my $new_index = {};
249 0 ▶ while (my ($t_name,$ind_array) = splice(@_,0,2)) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = splice(@_, 0, 2)) 0 0 0
250 0 ▶ next if (not $t_name or not exists $exisis_table->{$t_name} or not $ind_array);
Branch: 1 branch
BranchTrueFalseTotal
unless $t_name and exists $exisis_table->{$t_name} and $ind_array 0 0 0
Condition0%0/6
$t_name and exists $exisis_table->{$t_name} and $ind_array
!ll&&!rl&&r
000
$t_name and exists $exisis_table->{$t_name}
!ll&&!rl&&r
000
MC/DC0%0/3
$t_name and exists $exisis_table->{$t_name} and $ind_array
$t_name exists $exisis_table->{$t_name} $ind_array
Truth table0%0/4
A and B and C
A= $t_nameB= exists $exisis_table->{$t_name}C= $ind_array
ABCresult
0XX0
10X0
1100
1111
251 0 push @{$new_index->{$t_name}}, $ind_array;
252 };
253
254 # check exists index
255 0 foreach my $table (keys %$new_index) {
256 0 my @index = ();
257 0 my $exists_index = {};
258 0 my $index_name = $obj->db->selectall_arrayref('PRAGMA index_list('.$obj->db->quote($table).')');
259 0 ▶ next unless $index_name;
Branch: 1 branch
BranchTrueFalseTotal
unless $index_name 0 0 0
260 map {
261 0 my $i_name = $_->[1];
262 0 my $index_fields = $obj->db->selectrow_arrayref('PRAGMA index_info('.$obj->db->quote($i_name).')');
263 0 ▶ $exists_index->{md5_hex(join(',',sort {$a cmp $b} @$index_fields))}++ if $index_fields;
Branch: 1 branch
BranchTrueFalseTotal
if $index_fields 0 0 0
264 } @$index_name;
265 # create new index sql
266 0 foreach my $new_index_fields (@{$new_index->{$table}}) {
267 0 ▶ next if (not $new_index_fields or ref $new_index_fields ne 'ARRAY');
Branch: 1 branch
BranchTrueFalseTotal
if not $new_index_fields or ref $new_index_fields ne "ARRAY" 0 0 0
Condition0%0/3
not $new_index_fields or ref $new_index_fields ne "ARRAY"
l!l&&r!l&&!r
000
MC/DC0%0/2
not $new_index_fields or ref $new_index_fields ne "ARRAY"
not $new_index_fields ref $new_index_fields ne "ARRAY"
Truth table0%0/3
not(A) or B
A= not $new_index_fieldsB= ref $new_index_fields ne "ARRAY"
ABresult
1X1
011
000
268 0 ▶ next if $exists_index->{md5_hex(join(',',sort {$a cmp $b} @$new_index_fields))}++;
Branch: 1 branch
BranchTrueFalseTotal
if $$exists_index{md5_hex(join(",", (sort {$a cmp $b;} @$new_index_fields)))}++ 0 0 0
269 0 my $unic_name = sprintf('_%s',Data::UUID->new()->create_hex());
270 push @index, sprintf(
271 $i_create_pattern,
272 $unic_name,
273 $table,
274 0 join(',',map {$obj->db->quote($_)} @$new_index_fields)
275 );
276 0 $ret++;
277 };
278
279 # create new index in base
280 0 ▶ if ($CCCP::SQLiteWrap::OnlyPrint) {
Branch: 1 branch
BranchTrueFalseTotal
if ($CCCP::SQLiteWrap::OnlyPrint) { } 0 0 0
281 0 print join("\n",@index);
282 0 print "\n------------------------------\n";
283 } else {
284 0 $obj->do_transaction(@index);
285 };
286 };
287
288 0 return $ret;
289 }
290  
291 # check table on exists (bool)
292 0 ▶sub table_exists {
Subroutine table_exists: not called CC=3 SCAR=24.8
Pod documented
293 0 my ($obj,$table) = @_;
294 0 ▶ return unless $table;
Branch: 1 branch
BranchTrueFalseTotal
unless $table 0 0 0
295
296 0 ▶ return scalar(grep {/^\Q$table\E$/i} map {$_=~s/"//g; $_;} $obj->db->tables) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
scalar grep({/^\Q$table\E$/i;} map({$_ =~ s/"//g; $_;} $obj->db->tables)) ? : 0 0 0
297 }
298  
299 # $obj->index_exists('name' => ['field1','field2']);
300 # return name index if exists for whis fields
301 0 ▶sub index_exists {
Subroutine index_exists: not called CC=8 SCAR=42.8
Pod documented
302 0 my ($obj,$table,$ind_fields) = @_;
303
304 0 ▶ return unless ($table and $ind_fields and ref $ind_fields eq 'ARRAY');
Branch: 1 branch
BranchTrueFalseTotal
unless $table and $ind_fields and ref $ind_fields eq "ARRAY" 0 0 0
Condition0%0/6
$table and $ind_fields and ref $ind_fields eq "ARRAY"
!ll&&!rl&&r
000
$table and $ind_fields
!ll&&!rl&&r
000
MC/DC0%0/3
$table and $ind_fields and ref $ind_fields eq "ARRAY"
$table $ind_fields ref $ind_fields eq "ARRAY"
Truth table0%0/4
A and B and C
A= $tableB= $ind_fieldsC= ref $ind_fields eq "ARRAY"
ABCresult
0XX0
10X0
1100
1111
305 0 ▶ return unless $obj->table_exists($table);
Branch: 1 branch
BranchTrueFalseTotal
unless $obj->table_exists($table) 0 0 0
306
307 0 my $index_name = $obj->db->selectall_arrayref('PRAGMA index_list('.$obj->db->quote($table).')');
308 0 ▶ return unless $index_name;
Branch: 1 branch
BranchTrueFalseTotal
unless $index_name 0 0 0
309 0 my $exists_index = {};
310 map {
311 0 my $i_name = $_->[1];
312 0 my $index_fields = $obj->db->selectall_arrayref('PRAGMA index_info('.$obj->db->quote($i_name).')');
313 0 ▶ if ($index_fields) {
Branch: 1 branch
BranchTrueFalseTotal
if ($index_fields) 0 0 0
314 0 $index_fields = [map {$_->[2]} @$index_fields];
315 0 $exists_index->{md5_hex(join(',',sort {$a cmp $b} @$index_fields))} = $i_name;
316 };
317 } @$index_name;
318
319 0 my $ind_fields_md5 = md5_hex(join(',',sort {$a cmp $b} @$ind_fields));
320
321 0 ▶ return exists $exists_index->{$ind_fields_md5} ? $exists_index->{$ind_fields_md5} : 0;
Branch: 1 branch
BranchTrueFalseTotal
exists $exists_index->{$ind_fields_md5} ? : 0 0 0
322 }
323  
324 0 ▶sub create_trigger {
Subroutine create_trigger: not called CC=20 SCAR=60.4
Pod documented
325 0 my $obj = shift;
326 0 ▶ return unless (@_ or scalar @_ % 2 == 0);
Branch: 1 branch
BranchTrueFalseTotal
unless @_ or scalar @_ % 2 == 0 0 0 0
Condition0%0/3
@_ or scalar @_ % 2 == 0
l!l&&r!l&&!r
000
MC/DC0%0/2
@_ or scalar @_ % 2 == 0
@_ scalar @_ % 2 == 0
Truth table0%0/3
A or B
A= @_B= scalar @_ % 2 == 0
ABresult
1X1
011
000
327
328 0 my @transaction_query = ();
329
330 0 my $exisis_table = $obj->show_tables;
331 # cycle for table
332 0 ▶ while (my ($t_name,$param) = splice(@_,0,2)) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = splice(@_, 0, 2)) 0 0 0
333 0 ▶ next if (not $t_name or not exists $exisis_table->{$t_name} or not $param or ref $param ne 'HASH' or not keys %$param);
Branch: 1 branch
BranchTrueFalseTotal
if not $t_name && exists $exisis_table->{$t_name} && $param or ref $param ne "HASH" or not keys %$param 0 0 0
Condition0%0/12
not $t_name && exists $exisis_table->{$t_name} && $param or ref $param ne "HASH" or not keys %$param
l!l&&r!l&&!r
000
not $t_name && exists $exisis_table->{$t_name} && $param or ref $param ne "HASH"
l!l&&r!l&&!r
000
$t_name && exists $exisis_table->{$t_name} && $param
!ll&&!rl&&r
000
$t_name and exists $exisis_table->{$t_name}
!ll&&!rl&&r
000
MC/DC0%0/5
not $t_name && exists $exisis_table->{$t_name} && $param or ref $param ne "HASH" or not keys %$param
$t_name exists $exisis_table->{$t_name} $param ref $param ne "HASH" not keys %$param
Truth table0%0/6
not(A and B && C) or D or not(E)
A= $t_nameB= exists $exisis_table->{$t_name}C= $paramD= ref $param ne "HASH"E= not keys %$param
ABCDEresult
0XXXX1
10XXX1
110XX1
1111X1
111011
111000
334 # cycle for event listener
335 0 ▶ while (my ($t_event_1,$event_param) = each %$param) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$param) 0 0 0
336 0 ▶ next if (not $t_event_1 or not $event_param or ref $event_param ne 'HASH' or not keys %$event_param);
Branch: 1 branch
BranchTrueFalseTotal
if not $t_event_1 && $event_param or ref $event_param ne "HASH" or not keys %$event_param 0 0 0
Condition0%0/9
not $t_event_1 && $event_param or ref $event_param ne "HASH" or not keys %$event_param
l!l&&r!l&&!r
000
not $t_event_1 && $event_param or ref $event_param ne "HASH"
l!l&&r!l&&!r
000
$t_event_1 && $event_param
!ll&&!rl&&r
000
MC/DC0%0/4
not $t_event_1 && $event_param or ref $event_param ne "HASH" or not keys %$event_param
$t_event_1 $event_param ref $event_param ne "HASH" not keys %$event_param
Truth table0%0/5
not(A && B) or C or not(D)
A= $t_event_1B= $event_paramC= ref $event_param ne "HASH"D= not keys %$event_param
ABCDresult
0XXX1
10XX1
111X1
11011
11000
337 # last cycle
338 0 ▶ while (my ($t_event_2,$sql) = each %$event_param) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$event_param) 0 0 0
339 0 ▶ next unless ($t_event_2 and $sql and ref $sql eq 'ARRAY' and scalar @$sql);
Branch: 1 branch
BranchTrueFalseTotal
unless $t_event_2 and $sql and ref $sql eq "ARRAY" and scalar @$sql 0 0 0
Condition0%0/8
$t_event_2 and $sql and ref $sql eq "ARRAY" and scalar @$sql
!ll
00
$t_event_2 and $sql and ref $sql eq "ARRAY"
!ll&&!rl&&r
000
$t_event_2 and $sql
!ll&&!rl&&r
000
MC/DC0%0/3
$t_event_2 and $sql and ref $sql eq "ARRAY" and scalar @$sql
$t_event_2 $sql ref $sql eq "ARRAY"
Truth table0%0/4
A and B and C
A= $t_event_2B= $sqlC= ref $sql eq "ARRAY"
ABCresult
0XX0
10X0
1100
1111
340 0 $sql = [map {s/;\s*$//s; $_} @$sql];
341 0 my $tr_name = join('_',map {lc($_)} ('trigger',$t_name,$t_event_1,$t_event_2,md5_hex(lc(join('',@$sql)))));
342 # delete trigger
343 0 push @transaction_query,sprintf(
344 $tr_create_pattern->[0],
345 $tr_name
346 );
347 # create trigger
348 0 push @transaction_query,sprintf(
349 $tr_create_pattern->[1],
350 $tr_name,
351 uc($t_event_1),
352 uc($t_event_2),
353 $t_name,
354 join(';',@$sql)
355 );
356 };
357 };
358 };
359
360 # create transaction in base
361 0 ▶ if ($CCCP::SQLiteWrap::OnlyPrint) {
Branch: 1 branch
BranchTrueFalseTotal
if ($CCCP::SQLiteWrap::OnlyPrint) { } 0 0 0
362 0 print join("\n",@transaction_query);
363 0 print "\n------------------------------\n";
364 } else {
365 0 $obj->do_transaction(@transaction_query);
366 };
367 }
368  
369  
370 1 1;
371 __END__
↑ Summary