blib/lib/CHI/Driver/MySQL.pm

filter: Statementstmt 26.9% 14 / 52 Branchbran 0.0% 0 / 2 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 20.0% 3 / 15 Podpod 100% 6 / 6 totaltotal 28.7% 23 / 80 CC 3 SCAR 10.0
CC
3
Cov
25%
fetch
6.0
store
6.0
__ANON__
2.0
CRAP 2.7
↑ Summary
1 package CHI::Driver::MySQL;
2 1 ▶use Mojo::Base 'CHI::Driver';
Subroutine BEGIN: called CC=1 SCAR=0.0
3
4 1 our $VERSION = '0.3';
5
6 1 ▶use Encode qw(encode);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use Mojo::mysql;
Subroutine BEGIN: called CC=1 SCAR=0.0
8
9 0 ▶has 'namespace' => sub { 'default' };
Subroutine __ANON__: not called CC=1 SCAR=1.2
10
11 0 ▶has 'dsn' => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
12 0 return shift->constructor_params->{ dsn };
13 1 };
14
15 0 ▶has 'mysql' => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
16 0 my $self = shift;
17
18 0 my $mysql = Mojo::mysql->new( $self->dsn );
19
20 0 $mysql->max_connections(1);
21 0 $mysql->migrations->name('chi_cache')->from_data;
22
23 0 ▶ $mysql->once(connection => sub { shift->migrations->migrate });
Subroutine __ANON__: not called CC=1 SCAR=6.9
24
25 0 return $mysql;
26 1 };
27
28 0 ▶sub clear {
Subroutine clear: not called CC=1 SCAR=6.9
Pod documented
29 0 my $self = shift;
30
31 0 my $sql = 'delete from `chi_cache` where `namespace` = ?';
32 0 $self->mysql->db->query($sql, $self->namespace);
33
34 0 return 1;
35 }
36
37 0 ▶sub fetch {
Subroutine fetch: not called CC=2 SCAR=17.9
Pod documented
38 0 my ( $self, $key ) = @_;
39
40 0 my $sql = 'select `value` from `chi_cache` where `namespace` = ? and `key` = ? order by id desc limit 1';
41 0 my $result = $self->mysql->db->query($sql, $self->namespace, $key)
42 ->hash;
43
44 0 ▶ return $result && $result->{ value };
Condition0%0/3
$result && $result->{'value'}
!ll&&!rl&&r
000
MC/DC0%0/2
$result && $result->{'value'}
$result $result->{'value'}
Truth table0%0/3
A && B
A= $resultB= $result->{'value'}
ABresult
0X0
100
111
45 }
46
47 0 ▶sub get_keys {
Subroutine get_keys: not called CC=1 SCAR=6.9
Pod documented
48 0 my $self = shift;
49
50 0 my $sql = 'select `key` from `chi_cache` where `namespace` = ?';
51 my $keys = $self->mysql->db->query($sql, $self->namespace)
52 0 ▶ ->arrays->map(sub { $_->[0] })->to_array;
Subroutine __ANON__: not called CC=1 SCAR=6.9
53
54 0 return @$keys;
55 }
56
57 0 ▶sub get_namespaces {
Subroutine get_namespaces: not called CC=1 SCAR=6.9
Pod documented
58 0 my $self = shift;
59
60 0 my $sql = 'select distinct(`namespace`) from `chi_cache`';
61 my $namespaces = $self->mysql->db->query($sql)
62 0 ▶ ->arrays->map(sub { $_->[0] })->to_array;
Subroutine __ANON__: not called CC=1 SCAR=6.9
63
64 0 return @$namespaces;
65 }
66
67 0 ▶sub remove {
Subroutine remove: not called CC=1 SCAR=6.9
Pod documented
68 0 my ( $self, $key ) = @_;
69
70 0 my $sql = 'delete from `chi_cache` where `namespace` = ? and `key` = ?';
71 0 $self->mysql->db->query($sql, $self->namespace, $key);
72
73 0 return 1;
74 }
75
76 0 ▶sub store {
Subroutine store: not called CC=2 SCAR=17.9
Pod documented
77 0 my ( $self, $key, $data ) = @_;
78
79 0 ▶ if ( $self->fetch($key) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->fetch($key)) { } 0 0 0
80 0 my $sql = 'update `chi_cache` set `value` = ?, `updated_at` = now() where `namespace` = ? and `key` = ?';
81 0 $self->mysql->db->query($sql, encode('UTF-8', $data), $self->namespace, $key);
82 } else {
83 0 my $sql = 'insert into `chi_cache` (`namespace`, `key`, `value`, `created_at`) values (?, ?, ?, now())';
84 0 $self->mysql->db->query($sql, $self->namespace, $key, encode('UTF-8', $data));
85 }
86
87 0 return 1;
88 }
89
90 1 1;
91
92 __END__
↑ Summary