blib/lib/ClearPress/driver/mysql.pm

filter: Statementstmt 52.5% 31 / 59 Branchbran 25.0% 4 / 16 Conditioncond 33.3% 6 / 18 MC/DCmcdc 0.0% 0 / 10 Subroutinesub 58.3% 7 / 12 Podpod 100% 6 / 6 totaltotal 44.6% 54 / 121 CC 17 SCAR 31.7
CC
17
Cov
44%
dbh
43.5
bounded_select
20.0
sth_has_warnings
6.0
CRAP 23.9
« blib/lib/ClearPress/controller.pm ↑ Summary blib/lib/ClearPress/view.pm »
1 # -*- mode: cperl; tab-width: 8; indent-tabs-mode: nil; basic-offset: 2 -*-
2 # vim:ts=8:sw=2:et:sta:sts=2
3 #########
4 # Author: rmp
5 # Created: 2006-10-31
6 #
7 package ClearPress::driver::mysql;
8 15 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 15 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 15 ▶use base qw(ClearPress::driver);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 15 ▶use English qw(-no_match_vars);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 15 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 15 ▶use Readonly;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 15 our $VERSION = q[2026.07.25];
16  
17 15 Readonly::Scalar our $TYPES => {
18 'primary key' => 'bigint unsigned not null auto_increment primary key',
19 };
20 1 ▶sub dbh {
Subroutine dbh: called CC=13 SCAR=37.7
Pod documented
21 1 my $self = shift;
22  
23 1 ▶ if($self->{dbh} && !$self->{dbh}->ping()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"dbh"} and not $self->{"dbh"}->ping) 0 1 1
Condition33%1/3
$self->{"dbh"} and not $self->{"dbh"}->ping
!ll&&!rl&&r
100
MC/DC0%0/2
$self->{"dbh"} and not $self->{"dbh"}->ping
$self->{"dbh"} not $self->{"dbh"}->ping
Truth table33%1/3
A and not(B)
A= $self->{"dbh"}B= not $self->{"dbh"}->ping
ABresult
0X0
100
111
24 0 $self->{dbh}->disconnect();
25 0 delete $self->{dbh};
26 }
27  
28 1 ▶ if(!$self->{dbh}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"dbh"}) 1 0 1
29 1 my $dsn_opts = q[];
30 1 ▶ if($self->{dsn_opts}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"dsn_opts"}) 0 1 1
31 0 ▶ if(ref $self->{dsn_opts} && scalar keys %{$self->{dsn_opts}}) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self->{'dsn_opts'} and scalar keys %{$$self{'dsn_opts'};}) { } 0 0 0
Condition0%0/2
ref $self->{"dsn_opts"} and scalar keys %{$$self{"dsn_opts"};}
!ll
00
MC/DC0%0/1
ref $self->{"dsn_opts"} and scalar keys %{$$self{"dsn_opts"};}
ref $self->{"dsn_opts"}
Truth table0%0/2
A
A= ref $self->{"dsn_opts"}
Aresult
00
11
32 #########
33 # structured key:value pairs
34 #
35 0 $dsn_opts = join q[;], q[], map { sprintf q[%s=%s], $_, $self->{dsn_opts}->{$_} } sort keys %{$self->{dsn_opts}};
36  
37 } else {
38 #########
39 # scalar line e.g. straight out of config.ini
40 #
41 0 $dsn_opts = sprintf q[;%s], $self->{dsn_opts};
42 }
43 }
44  
45 my $dsn = sprintf q(DBI:mysql:database=%s;host=%s;port=%s%s),
46 $self->{dbname} || q[],
47 $self->{dbhost} || q[localhost],
48 1 ▶ $self->{dbport} || q[3306],
Condition50%3/6
$self->{'dbname'} || ''
l!l
10
$self->{'dbhost'} || 'localhost'
l!l
10
$self->{'dbport'} || '3306'
l!l
01
MC/DC0%0/1
$self->{'dbname'} || ''
$self->{'dbname'}
MC/DC0%0/1
$self->{'dbhost'} || 'localhost'
$self->{'dbhost'}
MC/DC0%0/1
$self->{'dbport'} || '3306'
$self->{'dbport'}
Truth table50%1/2
A
A= $self->{'dbname'}
Aresult
11
00
Truth table50%1/2
A
A= $self->{'dbhost'}
Aresult
11
00
Truth table50%1/2
A
A= $self->{'dbport'}
Aresult
11
00
49 $dsn_opts;
50  
51 eval {
52 $self->{dbh} = DBI->connect($dsn,
53 $self->{dbuser} || q[],
54 $self->{dbpass},
55 {
56 1 ▶ RaiseError => 1,
Condition50%1/2
$self->{'dbuser'} || ''
l!l
10
MC/DC0%0/1
$self->{'dbuser'} || ''
$self->{'dbuser'}
Truth table50%1/2
A
A= $self->{'dbuser'}
Aresult
11
00
57 AutoCommit => 0,
58 mysql_enable_utf8 => 1,
59 });
60  
61 # 2010-05-12 post-connect SET NAMES utf8 demonstrated to work a lot better than connect with mysql_enable_utf8 => 1
62 #
63 # Using test data: update run set payload='{"comment":"abc øéü"}' where id_run=2;
64 #
65 # this works on OSX MacPorts MySQL 5.1 but not on CentOS 5.4 MySQL 5.0
66 # perl -MDBI -e 'my $dbh = DBI->connect("DBI:mysql:host=localhost;dbname=ontrackt", "root", "", {RaiseError=>1});$dbh->do(q[update run set payload=? where id_run=2],{},q[abc øéµ]);print $dbh->selectall_arrayref(q[SELECT payload FROM run WHERE id_run=2])->[0]->[0],"\n";'
67 #
68 # this works on OSX and CentOS:
69 # perl -MDBI -e 'my $dbh = DBI->connect("DBI:mysql:host=localhost;dbname=ontrackt", "root", "", {RaiseError=>1});$dbh->do(q[SET NAMES utf8]);$dbh->do(q[update run set payload=? where id_run=2],{},q[abc øéµ]);print $dbh->selectall_arrayref(q[SELECT payload FROM run WHERE id_run=2])->[0]->[0],"\n";'
70 #
71 # this works on neither OSX nor CentOS
72 # perl -MDBI -e 'my $dbh = DBI->connect("DBI:mysql:host=localhost;dbname=ontrackt", "root", "", {RaiseError=>1,mysql_enable_utf8 =>1});$dbh->do(q[update run set payload=? where id_run=2],{},q[abc øéµ]);print $dbh->selectall_arrayref(q[SELECT payload FROM run WHERE id_run=2])->[0]->[0],"\n";'
73  
74 0 $self->{dbh}->do(q[SET NAMES utf8]);
75  
76 1 ▶ } or do {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $self->{"dbh"} = "DBI"->connect($dsn, $self->{"dbuser"} || "", $self->{"dbpass"}, {"RaiseError", 1, "AutoCommit", 0, "mysql_enable_utf8", 1}); $self->{"dbh"}->do("SET NAMES utf8") } } or do { croak(qq[Failed to connect to $dsn using @{[$self->{"dbuser"} || "''"];}\n$ClearPress::driver::mysql::EVAL_ERROR]) } 0 1 1
77 1 ▶ croak qq[Failed to connect to $dsn using @{[$self->{dbuser}||q['']]}\n$EVAL_ERROR];
Condition50%1/2
$self->{'dbuser'} || q['']
l!l
10
MC/DC0%0/1
$self->{'dbuser'} || q['']
$self->{'dbuser'}
Truth table50%1/2
A
A= $self->{'dbuser'}
Aresult
11
00
78 };
79  
80 #########
81 # rollback any junk left behind if this is a cached handle
82 #
83 0 $self->{dbh}->rollback();
84 }
85  
86 0 return $self->{dbh};
87 }
88  
89 0 ▶sub create {
Subroutine create: not called CC=1 SCAR=6.9
Pod documented
90 0 my ($self, $query, @args) = @_;
91 0 my $dbh = $self->dbh();
92  
93 0 $dbh->do($query, {}, @args);
94 0 my $idref = $dbh->selectall_arrayref('SELECT LAST_INSERT_ID()');
95  
96 0 return $idref->[0]->[0];
97 }
98  
99 0 ▶sub create_table {
Subroutine create_table: not called CC=1 SCAR=6.9
Pod documented
100 0 my ($self, $table_name, $ref) = @_;
101 0 return $self->SUPER::create_table($table_name, $ref, { engine=>'InnoDB'});
102 }
103  
104 0 ▶sub types {
Subroutine types: not called CC=1 SCAR=6.9
Pod documented
105 0 return $TYPES;
106 }
107  
108 0 ▶sub bounded_select {
Subroutine bounded_select: not called CC=4 SCAR=30.0
Pod documented
109 0 my ($self, $query, $len, $start) = @_;
110  
111 0 ▶ if(defined $start && defined $len) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $start and defined $len) { } 0 0 0
elsif (defined $len) { } 0 0 0
Condition0%0/3
defined $start and defined $len
!ll&&!rl&&r
000
MC/DC0%0/2
defined $start and defined $len
defined $start defined $len
Truth table0%0/3
A and B
A= defined $startB= defined $len
ABresult
0X0
100
111
112 0 $query .= qq[ LIMIT $start, $len];
113 } elsif(defined $len) {
114 0 $query .= qq[ LIMIT $len];
115 }
116  
117 0 return $query;
118 }
119  
120 0 ▶sub sth_has_warnings {
Subroutine sth_has_warnings: not called CC=2 SCAR=17.9
Pod documented
121 0 my ($self, $sth) = @_;
122  
123 0 ▶ if($sth->{mysql_warning_count}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sth->{"mysql_warning_count"}) 0 0 0
124 0 return $self->{dbh}->selectall_arrayref(q[SHOW WARNINGS]);
125 }
126  
127 0 return;
128 }
129  
130  
131 15 1;
132 __END__
« blib/lib/ClearPress/controller.pm ↑ Summary blib/lib/ClearPress/view.pm »