| 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
Condition33%1/3
$self->{"dbh"} and not $self->{"dbh"}->ping
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
| |||||||||||||||||||||||||||||||||
| 24 | 0 | $self->{dbh}->disconnect(); | |||||||||||||||||||||||||||||||
| 25 | 0 | delete $self->{dbh}; | |||||||||||||||||||||||||||||||
| 26 | } | ||||||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||||||
| 28 | 1 | ▶ | if(!$self->{dbh}) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 29 | 1 | my $dsn_opts = q[]; | |||||||||||||||||||||||||||||||
| 30 | 1 | ▶ | if($self->{dsn_opts}) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 31 | 0 | ▶ | if(ref $self->{dsn_opts} && scalar keys %{$self->{dsn_opts}}) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/2
ref $self->{"dsn_opts"} and scalar keys %{$$self{"dsn_opts"};}
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"}
| |||||||||||||||||||||||||||||||||
| 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'} || ''
$self->{'dbhost'} || 'localhost'
$self->{'dbport'} || '3306'
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'}
Truth table50%1/2
A
A= $self->{'dbhost'}
Truth table50%1/2
A
A= $self->{'dbport'}
| |||||||||||||||||||||||||||||||||
| 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'} || ''
MC/DC0%0/1
$self->{'dbuser'} || ''
$self->{'dbuser'}
Truth table50%1/2
A
A= $self->{'dbuser'}
| |||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||
| 77 | 1 | ▶ | croak qq[Failed to connect to $dsn using @{[$self->{dbuser}||q['']]}\n$EVAL_ERROR]; | ||||||||||||||||||||||||||||||
|
Condition50%1/2
$self->{'dbuser'} || q['']
MC/DC0%0/1
$self->{'dbuser'} || q['']
$self->{'dbuser'}
Truth table50%1/2
A
A= $self->{'dbuser'}
| |||||||||||||||||||||||||||||||||
| 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
Condition0%0/3
defined $start and defined $len
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
| |||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||
| 124 | 0 | return $self->{dbh}->selectall_arrayref(q[SHOW WARNINGS]); | |||||||||||||||||||||||||||||||
| 125 | } | ||||||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||||||
| 127 | 0 | return; | |||||||||||||||||||||||||||||||
| 128 | } | ||||||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||||||
| 131 | 15 | 1; | |||||||||||||||||||||||||||||||
| 132 | __END__ | ||||||||||||||||||||||||||||||||