Base/DBI.pm

filter: Statementstmt 91.6% 153 / 167 Branchbran 57.1% 48 / 84 Conditioncond 43.1% 19 / 44 MC/DCmcdc 12.0% 3 / 25 Subroutinesub 83.8% 26 / 31 Podpod 85.0% 17 / 20 totaltotal 71.7% 266 / 371 CC 62 SCAR 24.0
CC
62
Cov
75%
init
36.9
dbh
16.0
pre_suspend_hook
12.0
CRAP 11.0
↑ Summary Client/MariaDB.pm »
1 package IPC::Manager::Base::DBI;
2 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 2 our $VERSION = '0.000010';
6  
7 2 ▶use Carp qw/croak/;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use Scalar::Util qw/blessed/;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use Time::HiRes qw/time/;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use File::Temp qw/tempfile/;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 2 ▶use DBI;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 2 ▶use parent 'IPC::Manager::Client';
Subroutine BEGIN: called CC=1 SCAR=0.0
15 2 use Object::HashBase qw{
16 +dbh
17 <user
18 <pass
19 <attrs
20 2 ▶};
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 0 ▶sub dsn { croak "Not Implemented" }
Subroutine dsn: not called CC=1 SCAR=6.9
Pod documented
23 0 ▶sub table_sql { croak "Not Implemented" }
Subroutine table_sql: not called CC=1 SCAR=6.9
Pod documented
24  
25 0 ▶sub escape { '' }
Subroutine escape: not called CC=1 SCAR=6.9
Pod documented
26  
27 14 ▶sub pending_messages { 0 }
Subroutine pending_messages: called CC=1 SCAR=0.0
Pod undocumented
28 14 ▶sub ready_messages { $_[0]->_get_message_ids ? 1 : 0 }
Subroutine ready_messages: called CC=2 SCAR=6.9
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
$_[0]->_get_message_ids ? : 6 8 14
29  
30 sub dbh {
31 210 ▶ my $this = shift;
Subroutine dbh: called CC=13 SCAR=27.7
Pod documented
32 210 my (%params) = @_;
33  
34 210 ▶ if (blessed($this) && $this->{+DBH}) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($this) and $this->{"dbh"}) 195 15 210
Condition100%3/3
builtin::blessed($this) and $this->{"dbh"}
!ll&&!rl&&r
411195
MC/DC100%2/2
builtin::blessed($this) and $this->{"dbh"}
builtin::blessed($this) $this->{"dbh"}
Truth table100%3/3
A and B
A= builtin::blessed($this)B= $this->{"dbh"}
ABresult
0X0
100
111
35 195 $this->pid_check;
36 195 ▶ return $this->{+DBH} if $this->{+DBH}->{Active};
Branch: 1 branch
BranchTrueFalseTotal
if $this->{"dbh"}{"Active"} 195 0 195
37 0 delete $this->{+DBH};
38 }
39  
40 15 my $dsn = $params{dsn};
41 15 my $user = $params{user};
42 15 my $pass = $params{pass};
43 15 my $attrs = $params{attrs};
44  
45 15 ▶ if (blessed($this)) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($this)) 11 4 15
46 11 $this->pid_check;
47 11 ▶ $dsn //= $this->dsn;
Condition33%1/3
$dsn //= $this->dsn
l!l&&r!l&&!r
0110
MC/DC0%0/2
$dsn //= $this->dsn
$dsn $this->dsn
Truth table33%1/3
A //= B
A= $dsnB= $this->dsn
ABresult
1X1
011
000
48 11 ▶ $user //= $this->user;
Condition33%1/3
$user //= $this->user
l!l&&r!l&&!r
0110
MC/DC0%0/2
$user //= $this->user
$user $this->user
Truth table33%1/3
A //= B
A= $userB= $this->user
ABresult
1X1
011
000
49 11 ▶ $pass //= $this->pass;
Condition33%1/3
$pass //= $this->pass
l!l&&r!l&&!r
0110
MC/DC0%0/2
$pass //= $this->pass
$pass $this->pass
Truth table33%1/3
A //= B
A= $passB= $this->pass
ABresult
1X1
011
000
50 11 ▶ $attrs //= $this->attrs;
Condition33%1/3
$attrs //= $this->attrs
l!l&&r!l&&!r
0011
MC/DC0%0/2
$attrs //= $this->attrs
$attrs $this->attrs
Truth table33%1/3
A //= B
A= $attrsB= $this->attrs
ABresult
1X1
011
000
51 }
52  
53 15 ▶ croak "No DSN" unless $dsn;
Branch: 1 branch
BranchTrueFalseTotal
unless $dsn 0 15 15
54 15 ▶ $attrs //= {};
Condition50%1/2
$attrs //= {}
l!l
015
MC/DC0%0/1
$attrs //= {}
$attrs
Truth table50%1/2
A
A= $attrs
Aresult
11
00
55  
56 15 ▶ my $dbh = DBI->connect($dsn, $user, $pass, $attrs) or die "Could not connect";
Branch: 1 branch
BranchTrueFalseTotal
my $dbh = "DBI"->connect($dsn, $user, $pass, $attrs) or die "Could not connect" 0 15 15
57  
58 15 ▶ $this->{+DBH} = $dbh if blessed($this);
Branch: 1 branch
BranchTrueFalseTotal
if builtin::blessed($this) 11 4 15
59  
60 15 return $dbh;
61 }
62  
63 sub init_db {
64 4 ▶ my $this = shift;
Subroutine init_db: called CC=3 SCAR=11.1
Pod documented
65  
66 4 my $dbh = $this->dbh(@_);
67  
68 4 for my $sql ($this->table_sql) {
69 8 local $dbh->{PrintWarn} = 0;
70 8 ▶ $dbh->do($sql) or die $dbh->errstr;
Branch: 1 branch
BranchTrueFalseTotal
$dbh->do($sql) or die $dbh->errstr 0 8 8
71 }
72 }
73  
74 ▶sub default_attrs {}
Subroutine default_attrs: called CC=1 SCAR=0.0
Pod documented
75  
76 sub init {
77 11 ▶ my $self = shift;
Subroutine init: called CC=15 SCAR=36.1
Pod undocumented
78  
79 11 $self->SUPER::init();
80  
81 11 ▶ $self->{+USER} //= delete $self->{username} // '';
Condition40%2/5
$self->{"user"} //= delete $self->{"username"} // ""
l!l&&r!l&&!r
0110
delete $self->{"username"} // ""
l!l
011
MC/DC0%0/2
$self->{"user"} //= delete $self->{"username"} // ""
$self->{"user"} delete $self->{"username"}
Truth table0%0/3
A //= B
A= $self->{"user"}B= delete $self->{"username"}
ABresult
1X1
011
000
82 11 ▶ $self->{+PASS} //= delete $self->{password} // '';
Condition40%2/5
$self->{"pass"} //= delete $self->{"password"} // ""
l!l&&r!l&&!r
0110
delete $self->{"password"} // ""
l!l
011
MC/DC0%0/2
$self->{"pass"} //= delete $self->{"password"} // ""
$self->{"pass"} delete $self->{"password"}
Truth table0%0/3
A //= B
A= $self->{"pass"}B= delete $self->{"password"}
ABresult
1X1
011
000
83  
84 11 ▶ $self->{+ATTRS} //= $self->default_attrs;
Condition33%1/3
$self->{"attrs"} //= $self->default_attrs
l!l&&r!l&&!r
0011
MC/DC0%0/2
$self->{"attrs"} //= $self->default_attrs
$self->{"attrs"} $self->default_attrs
Truth table33%1/3
A //= B
A= $self->{"attrs"}B= $self->default_attrs
ABresult
1X1
011
000
85  
86 11 my $id = $self->{+ID};
87  
88 11 my $row = $self->_get_peer($self->{+ID});
89  
90 11 ▶ croak "The '$id' peer does not exist" if $self->{+RECONNECT} && !$row;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"reconnect"} and not $row 0 11 11
Condition33%1/3
$self->{"reconnect"} and not $row
!ll&&!rl&&r
1100
MC/DC0%0/2
$self->{"reconnect"} and not $row
$self->{"reconnect"} not $row
Truth table33%1/3
A and not(B)
A= $self->{"reconnect"}B= not $row
ABresult
0X0
100
111
91  
92 croak "Looks like the connection is already running in pid $row->{pid}"
93 11 ▶ if $row && $row->{pid} && $self->pid_is_running($row->{pid});
Branch: 1 branch
BranchTrueFalseTotal
if $row and $row->{"pid"} and $self->pid_is_running($row->{"pid"}) 0 11 11
Condition33%2/6
$row and $row->{"pid"} and $self->pid_is_running($row->{"pid"})
!ll&&!rl&&r
1100
$row and $row->{"pid"}
!ll&&!rl&&r
1100
MC/DC0%0/3
$row and $row->{"pid"} and $self->pid_is_running($row->{"pid"})
$row $row->{"pid"} $self->pid_is_running($row->{"pid"})
Truth table25%1/4
A and B and C
A= $rowB= $row->{"pid"}C= $self->pid_is_running($row->{"pid"})
ABCresult
0XX0
10X0
1100
1111
94  
95 11 my $dbh = $self->dbh;
96 11 my $e = $self->escape;
97  
98 11 ▶ if ($row) {
Branch: 1 branch
BranchTrueFalseTotal
if ($row) { } 0 11 11
99 0 my $sth = $dbh->prepare("UPDATE ipcm_peers SET ${e}active${e} = ?, ${e}pid${e} = ? WHERE ${e}id${e} = ?");
100 0 ▶ $sth->execute(time, $self->{+PID}, $self->{+ID}) or die $dbh->errstr;
Branch: 1 branch
BranchTrueFalseTotal
$sth->execute(&IPC::Manager::Base::DBI::time(), $self->{"pid"}, $self->{"id"}) or die $dbh->errstr 0 0 0
101 }
102 else {
103 11 ▶ my $sth = $dbh->prepare("INSERT INTO ipcm_peers(${e}id${e}, ${e}pid${e}, ${e}active${e}) VALUES (?, ?, ?)") or die $dbh->errstr;
Branch: 1 branch
BranchTrueFalseTotal
my $sth = $dbh->prepare("INSERT INTO ipcm_peers(${e}id$e, ${e}pid$e, ${e}active$e) VALUES (?, ?, ?)") or die $dbh->errstr 0 11 11
104 11 ▶ $sth->execute($id, $self->{+PID}, time) or die $dbh->errstr;
Branch: 1 branch
BranchTrueFalseTotal
$sth->execute($id, $self->{"pid"}, &IPC::Manager::Base::DBI::time()) or die $dbh->errstr 0 11 11
105 }
106 }
107  
108 sub all_stats {
109 2 ▶ my $self = shift;
Subroutine all_stats: called CC=4 SCAR=14.0
Pod documented
110  
111 2 my $out = {};
112  
113 2 my $dbh = $self->dbh;
114 2 my $e = $self->escape;
115 2 ▶ my $sth = $dbh->prepare("SELECT ${e}id${e}, ${e}stats${e} FROM ipcm_peers") or die $dbh->errstr;
Branch: 1 branch
BranchTrueFalseTotal
my $sth = $dbh->prepare("SELECT ${e}id$e, ${e}stats$e FROM ipcm_peers") or die $dbh->errstr 0 2 2
116 2 ▶ $sth->execute() or die $dbh->errstr;
Branch: 1 branch
BranchTrueFalseTotal
$sth->execute or die $dbh->errstr 0 2 2
117  
118 2 ▶ while (my $row = $sth->fetchrow_arrayref) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $row = $sth->fetchrow_arrayref) 8 2 10
119 8 $out->{$row->[0]} = $self->{+SERIALIZER}->deserialize($row->[1]);
120 }
121  
122 2 return $out;
123 }
124  
125 sub write_stats {
126 16 ▶ my $self = shift;
Subroutine write_stats: called CC=3 SCAR=11.3
Pod documented
127  
128 16 my $dbh = $self->dbh;
129 16 my $e = $self->escape;
130 16 ▶ my $sth = $dbh->prepare("UPDATE ipcm_peers SET ${e}stats${e} = ? WHERE ${e}id${e} = ?") or die $dbh->errstr;
Branch: 1 branch
BranchTrueFalseTotal
my $sth = $dbh->prepare("UPDATE ipcm_peers SET ${e}stats$e = ? WHERE ${e}id$e = ?") or die $dbh->errstr 0 16 16
131 16 ▶ $sth->execute($self->{+SERIALIZER}->serialize($self->{+STATS}), $self->{+ID}) or die $dbh->errstr;
Branch: 1 branch
BranchTrueFalseTotal
$sth->execute($self->{"serializer"}->serialize($self->{"stats"}), $self->{"id"}) or die $dbh->errstr 0 16 16
132 }
133  
134 sub read_stats {
135 6 ▶ my $self = shift;
Subroutine read_stats: called CC=4 SCAR=14.3
Pod documented
136  
137 6 my $dbh = $self->dbh;
138 6 my $e = $self->escape;
139 6 ▶ my $sth = $dbh->prepare("SELECT ${e}stats${e} FROM ipcm_peers WHERE ${e}id${e} = ?") or die $dbh->errstr;
Branch: 1 branch
BranchTrueFalseTotal
my $sth = $dbh->prepare("SELECT ${e}stats$e FROM ipcm_peers WHERE ${e}id$e = ?") or die $dbh->errstr 0 6 6
140 6 ▶ $sth->execute($self->{+ID}) or die $dbh->errstr;
Branch: 1 branch
BranchTrueFalseTotal
$sth->execute($self->{"id"}) or die $dbh->errstr 0 6 6
141  
142 6 ▶ my $row = $sth->fetchrow_arrayref or return undef;
Branch: 1 branch
BranchTrueFalseTotal
my $row = $sth->fetchrow_arrayref or return undef 0 6 6
143 6 return $self->{+SERIALIZER}->deserialize($row->[0]);
144 }
145  
146 sub peers {
147 21 ▶ my $self = shift;
Subroutine peers: called CC=2 SCAR=7.0
Pod documented
148  
149 21 my $dbh = $self->dbh;
150 21 my $e = $self->escape;
151 21 ▶ my $sth = $dbh->prepare("SELECT ${e}id${e} FROM ipcm_peers WHERE ${e}id${e} != ? AND active IS NOT NULL ORDER BY ${e}id${e} ASC") or die $dbh->errstr;
Branch: 1 branch
BranchTrueFalseTotal
my $sth = $dbh->prepare("SELECT ${e}id$e FROM ipcm_peers WHERE ${e}id$e != ? AND active IS NOT NULL ORDER BY ${e}id$e ASC") or die $dbh->errstr 0 21 21
152 21 $sth->execute($self->{+ID});
153  
154 21 return map { $_->[0] } @{$sth->fetchall_arrayref([0])};
155 }
156  
157 sub peer_pid {
158 13 ▶ my $self = shift;
Subroutine peer_pid: called CC=4 SCAR=14.6
Pod documented
159 13 my ($id) = @_;
160  
161 13 ▶ my $row = $self->_get_peer($id) or return undef;
Branch: 1 branch
BranchTrueFalseTotal
my $row = $self->_get_peer($id) or return undef 1 12 13
162 12 ▶ return $row->{pid} // undef if $row->{active};
Branch: 1 branch
BranchTrueFalseTotal
if $row->{"active"} 12 0 12
Condition50%1/2
$row->{"pid"} // undef
l!l
120
MC/DC0%0/1
$row->{"pid"} // undef
$row->{"pid"}
Truth table50%1/2
A
A= $row->{"pid"}
Aresult
11
00
163 0 return undef;
164 }
165  
166 sub peer_exists {
167 1 ▶ my $self = shift;
Subroutine peer_exists: called CC=2 SCAR=7.1
Pod documented
168 1 my ($id) = @_;
169 1 ▶ return $self->_get_peer($id) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$self->_get_peer($id) ? : 1 0 1
170 }
171  
172 sub _get_peer {
173 25 ▶ my $self = shift;
Subroutine _get_peer: called CC=2 SCAR=7.0
174 25 my ($id) = @_;
175  
176 25 my $dbh = $self->dbh;
177 25 my $e = $self->escape;
178 25 ▶ my $sth = $dbh->prepare("SELECT * FROM ipcm_peers WHERE ${e}id${e} = ?") or die $dbh->errstr;
Branch: 1 branch
BranchTrueFalseTotal
my $sth = $dbh->prepare("SELECT * FROM ipcm_peers WHERE ${e}id$e = ?") or die $dbh->errstr 0 25 25
179 25 $sth->execute($id);
180 25 return $sth->fetchrow_hashref;
181 }
182  
183 sub send_message {
184 29 ▶ my $self = shift;
Subroutine send_message: called CC=4 SCAR=14.0
Pod documented
185 29 my $msg = $self->build_message(@_);
186  
187 29 my $dbh = $self->dbh;
188 29 my $e = $self->escape;
189 29 ▶ my $sth = $dbh->prepare("INSERT INTO ipcm_messages(${e}id${e}, ${e}from${e}, ${e}to${e}, ${e}stamp${e}, ${e}broadcast${e}, ${e}content${e}) VALUES (?, ?, ?, ?, ?, ?)")
Branch: 1 branch
BranchTrueFalseTotal
my $sth = $dbh->prepare("INSERT INTO ipcm_messages(${e}id$e, ${e}from$e, ${e}to$e, ${e}stamp$e, ${e}broadcast$e, ${e}content$e) VALUES (?, ?, ?, ?, ?, ?)") or die $dbh->errstr 0 29 29
190 or die $dbh->errstr;
191  
192 $sth->execute(
193 29 ▶ @{$msg}{qw/id from to stamp/},
Branch: 1 branch
BranchTrueFalseTotal
$msg->{'broadcast'} ? : 19 10 29
194 ($msg->{broadcast} ? 1 : 0),
195 29 ▶ $self->{+SERIALIZER}->serialize($msg->{content}),
Branch: 1 branch
BranchTrueFalseTotal
$sth->execute(@{$msg;}{"id", "from", "to", "stamp"}, $msg->{"broadcast"} ? 1 : 0, $self->{"serializer"}->serialize($msg->{"content"})) or die $dbh->errstr 0 29 29
196 ) or die $dbh->errstr;
197  
198 29 $self->{+STATS}->{sent}->{$msg->{to}}++;
199 }
200  
201 sub _get_message_ids {
202 50 ▶ my $self = shift;
Subroutine _get_message_ids: called CC=5 SCAR=16.3
203 50 my $dbh = $self->dbh;
204 50 my $e = $self->escape;
205 50 ▶ my $sth = $dbh->prepare("SELECT ${e}id${e} FROM ipcm_messages WHERE ${e}to${e} = ? ORDER BY ${e}stamp${e} ASC") or die $dbh->errstr;
Branch: 1 branch
BranchTrueFalseTotal
my $sth = $dbh->prepare("SELECT ${e}id$e FROM ipcm_messages WHERE ${e}to$e = ? ORDER BY ${e}stamp$e ASC") or die $dbh->errstr 0 50 50
206 50 ▶ $sth->execute($self->{+ID}) or die $dbh->errstr;
Branch: 1 branch
BranchTrueFalseTotal
$sth->execute($self->{"id"}) or die $dbh->errstr 0 50 50
207 50 my $ids = [map { $_->[0] } @{$sth->fetchall_arrayref([0])}];
208 50 ▶ return $ids if $ids && @$ids;
Branch: 1 branch
BranchTrueFalseTotal
if $ids and @$ids 29 21 50
Condition66%2/3
$ids and @$ids
!ll&&!rl&&r
02129
MC/DC50%1/2
$ids and @$ids
$ids @$ids
Truth table66%2/3
A and B
A= $idsB= @$ids
ABresult
0X0
100
111
209 21 return;
210 }
211  
212 sub get_messages {
213 36 ▶ my $self = shift;
Subroutine get_messages: called CC=7 SCAR=19.7
Pod documented
214 36 my $dbh = $self->dbh;
215  
216 36 ▶ my $ids = $self->_get_message_ids or return;
Branch: 1 branch
BranchTrueFalseTotal
my $ids = $self->_get_message_ids or return 13 23 36
217  
218 23 my $e = $self->escape;
219 23 my $where = "FROM ipcm_messages WHERE ${e}id${e} IN (" . join(', ' => map { '?' } 1 .. scalar(@$ids)) . ")";
220 23 ▶ my $sth = $dbh->prepare("SELECT * $where") or die $dbh->errstr;
Branch: 1 branch
BranchTrueFalseTotal
my $sth = $dbh->prepare("SELECT * $where") or die $dbh->errstr 0 23 23
221 23 ▶ $sth->execute(@$ids) or die $dbh->errstr;
Branch: 1 branch
BranchTrueFalseTotal
$sth->execute(@$ids) or die $dbh->errstr 0 23 23
222 23 my $rows = $sth->fetchall_arrayref({});
223  
224 23 my @out;
225  
226 23 for my $row (@$rows) {
227 29 $row->{content} = $self->{+SERIALIZER}->deserialize($row->{content});
228 29 $self->{+STATS}->{read}->{$row->{from}}++;
229 29 push @out => IPC::Manager::Message->new($row);
230 }
231  
232 23 ▶ $sth = $dbh->prepare("DELETE $where") or die $dbh->errstr;
Branch: 1 branch
BranchTrueFalseTotal
$sth = $dbh->prepare("DELETE $where") or die $dbh->errstr 0 23 23
233 23 ▶ $sth->execute(@$ids) or die $dbh->errstr;
Branch: 1 branch
BranchTrueFalseTotal
$sth->execute(@$ids) or die $dbh->errstr 0 23 23
234  
235  
236 23 return @out;
237 }
238  
239 sub requeue_message {
240 0 ▶ my $self = shift;
Subroutine requeue_message: not called CC=2 SCAR=17.9
Pod documented
241 0 $self->send_message($_) for @_;
242 }
243  
244 sub pre_disconnect_hook {
245 10 ▶ my $self = shift;
Subroutine pre_disconnect_hook: called CC=3 SCAR=11.3
Pod documented
246  
247 10 my $dbh = $self->dbh;
248 10 my $e = $self->escape;
249 10 ▶ my $sth = $dbh->prepare("UPDATE ipcm_peers SET ${e}pid${e} = NULL, active = NULL WHERE ${e}id${e} = ?") or die $dbh->errstr;
Branch: 1 branch
BranchTrueFalseTotal
my $sth = $dbh->prepare("UPDATE ipcm_peers SET ${e}pid$e = NULL, active = NULL WHERE ${e}id$e = ?") or die $dbh->errstr 0 10 10
250 10 ▶ $sth->execute($self->{+ID}) or die $dbh->errstr;
Branch: 1 branch
BranchTrueFalseTotal
$sth->execute($self->{"id"}) or die $dbh->errstr 0 10 10
251 }
252  
253 sub pre_suspend_hook {
254 0 ▶ my $self = shift;
Subroutine pre_suspend_hook: not called CC=3 SCAR=24.8
Pod documented
255  
256 0 my $dbh = $self->dbh;
257 0 my $e = $self->escape;
258 0 ▶ my $sth = $dbh->prepare("UPDATE ipcm_peers SET ${e}pid${e} = NULL WHERE ${e}id${e} = ?") or die $dbh->errstr;
Branch: 1 branch
BranchTrueFalseTotal
my $sth = $dbh->prepare("UPDATE ipcm_peers SET ${e}pid$e = NULL WHERE ${e}id$e = ?") or die $dbh->errstr 0 0 0
259 0 ▶ $sth->execute($self->{+ID}) or die $dbh->errstr;
Branch: 1 branch
BranchTrueFalseTotal
$sth->execute($self->{"id"}) or die $dbh->errstr 0 0 0
260 }
261  
262 2 1;
263  
264 __END__
↑ Summary Client/MariaDB.pm »