| 1 | package DBIx::MoCo::DataBase; | ||||||||||||||||||||||||||||||||
| 2 | 13 | ▶ | use strict; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 3 | 13 | ▶ | use warnings; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 4 | 13 | ▶ | use Carp; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 5 | 13 | ▶ | use base qw (Class::Data::Inheritable); | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 6 | 13 | ▶ | use DBI; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 7 | 13 | ▶ | use SQL::Abstract; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||||||
| 9 | 13 | __PACKAGE__->mk_classdata($_) for qw(username password | |||||||||||||||||||||||||||||||
| 10 | cache_connection last_insert_id); | ||||||||||||||||||||||||||||||||
| 11 | 13 | __PACKAGE__->cache_connection(1); | |||||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||||||
| 13 | 13 | our $DEBUG = 0; | |||||||||||||||||||||||||||||||
| 14 | 13 | our $SQL_COUNT = 0; | |||||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||||||
| 16 | # $Carp::CarpLevel = 2; | ||||||||||||||||||||||||||||||||
| 17 | 13 | my $sqla = SQL::Abstract->new; | |||||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||||||
| 19 | 23 | ▶ | sub insert { | ||||||||||||||||||||||||||||||
|
Subroutine
insert: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 20 | 23 | my $class = shift; | |||||||||||||||||||||||||||||||
| 21 | 23 | my ($table, $args) = @_; | |||||||||||||||||||||||||||||||
| 22 | 23 | my ($sql, @binds) = $sqla->insert($table,$args); | |||||||||||||||||||||||||||||||
| 23 | 23 | $class->execute($sql,undef,\@binds); | |||||||||||||||||||||||||||||||
| 24 | } | ||||||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||||||
| 26 | 4 | ▶ | sub delete { | ||||||||||||||||||||||||||||||
|
Subroutine
delete: called CC=5 SCAR=17.5
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 27 | 4 | my $class = shift; | |||||||||||||||||||||||||||||||
| 28 | 4 | my ($table, $where) = @_; | |||||||||||||||||||||||||||||||
| 29 | 4 | ▶ | $where or croak "where is not specified to delete from $table"; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 30 | 4 | ▶ | (ref $where eq 'HASH' && %$where) or croak "where is not specified to delete from $table"; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
ref $where eq "HASH" and %$where
MC/DC0%0/2
ref $where eq "HASH" and %$where
ref $where eq "HASH"
%$where
Truth table33%1/3
A and B
A= ref $where eq "HASH"B= %$where
| |||||||||||||||||||||||||||||||||
| 31 | 4 | my ($sql, @binds) = $sqla->delete($table,$where); | |||||||||||||||||||||||||||||||
| 32 | 4 | ▶ | $sql =~ /WHERE/io or croak "where is not specified to delete from $table"; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 33 | 4 | $class->execute($sql,undef,\@binds); | |||||||||||||||||||||||||||||||
| 34 | } | ||||||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||||||
| 36 | 12 | ▶ | sub update { | ||||||||||||||||||||||||||||||
|
Subroutine
update: called CC=5 SCAR=17.5
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 37 | 12 | my $class = shift; | |||||||||||||||||||||||||||||||
| 38 | 12 | my ($table, $args, $where) = @_; | |||||||||||||||||||||||||||||||
| 39 | 12 | ▶ | $where or croak "where is not specified to update $table"; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 40 | 12 | ▶ | (ref $where eq 'HASH' && %$where) or croak "where is not specified to update $table"; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
ref $where eq "HASH" and %$where
MC/DC0%0/2
ref $where eq "HASH" and %$where
ref $where eq "HASH"
%$where
Truth table33%1/3
A and B
A= ref $where eq "HASH"B= %$where
| |||||||||||||||||||||||||||||||||
| 41 | 12 | my ($sql, @binds) = $sqla->update($table,$args,$where); | |||||||||||||||||||||||||||||||
| 42 | 12 | ▶ | $sql =~ /WHERE/io or croak "where is not specified to update $table"; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 43 | 12 | $class->execute($sql,undef,\@binds); | |||||||||||||||||||||||||||||||
| 44 | } | ||||||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||||||
| 46 | 35 | ▶ | sub select { | ||||||||||||||||||||||||||||||
|
Subroutine
select: called CC=3 SCAR=11.2
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 47 | 35 | my $class = shift; | |||||||||||||||||||||||||||||||
| 48 | 35 | my ($table, $args, $where, $order, $limit) = @_; | |||||||||||||||||||||||||||||||
| 49 | 35 | my ($sql, @binds) = $sqla->select($table,$args,$where,$order); | |||||||||||||||||||||||||||||||
| 50 | 35 | ▶ | $sql .= $class->_parse_limit($limit) if $limit; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 51 | 35 | my $data; | |||||||||||||||||||||||||||||||
| 52 | 35 | ▶ | $class->execute($sql,\$data,\@binds) or return; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 53 | 35 | return $data; | |||||||||||||||||||||||||||||||
| 54 | } | ||||||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||||||
| 56 | 40 | ▶ | sub search { | ||||||||||||||||||||||||||||||
|
Subroutine
search: called CC=2 SCAR=7.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 57 | 40 | my $class = shift; | |||||||||||||||||||||||||||||||
| 58 | 40 | my %args = @_; | |||||||||||||||||||||||||||||||
| 59 | 40 | my ($sql, @binds) = $class->_search_sql(\%args); | |||||||||||||||||||||||||||||||
| 60 | 40 | my $data; | |||||||||||||||||||||||||||||||
| 61 | 40 | ▶ | $class->execute($sql,\$data,\@binds) or return; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 62 | 40 | return $data; | |||||||||||||||||||||||||||||||
| 63 | } | ||||||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||||||
| 65 | 40 | ▶ | sub _search_sql { | ||||||||||||||||||||||||||||||
|
Subroutine
_search_sql: called CC=6 SCAR=18.0
| |||||||||||||||||||||||||||||||||
| 66 | 40 | my $class = shift; | |||||||||||||||||||||||||||||||
| 67 | 40 | my $args = shift; | |||||||||||||||||||||||||||||||
| 68 | 40 | ▶ | my $field = $args->{field} || "*"; | ||||||||||||||||||||||||||||||
|
Condition100%2/2
$args->{'field'} || '*'
MC/DC100%1/1
$args->{'field'} || '*'
$args->{'field'}
Truth table100%2/2
A
A= $args->{'field'}
| |||||||||||||||||||||||||||||||||
| 69 | 40 | my $sql = "SELECT $field FROM " . $args->{table}; | |||||||||||||||||||||||||||||||
| 70 | 40 | ▶ | $sql .= " USE INDEX ($args->{use_index})" if $args->{use_index}; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 71 | 40 | my ($where,@binds) = $class->_parse_where($args->{where}); | |||||||||||||||||||||||||||||||
| 72 | 40 | ▶ | $sql .= $where if $where; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 73 | 40 | ▶ | $sql .= " GROUP BY $args->{group}" if $args->{group}; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 74 | 40 | ▶ | $sql .= " ORDER BY $args->{order}" if $args->{order}; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 75 | 40 | $sql .= $class->_parse_limit($args); | |||||||||||||||||||||||||||||||
| 76 | 40 | return ($sql,@binds); | |||||||||||||||||||||||||||||||
| 77 | } | ||||||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||||||
| 79 | 40 | ▶ | sub _parse_where { | ||||||||||||||||||||||||||||||
|
Subroutine
_parse_where: called CC=9 SCAR=22.2
| |||||||||||||||||||||||||||||||||
| 80 | 40 | my ($class, $where) = @_; | |||||||||||||||||||||||||||||||
| 81 | 40 | my $binds = []; | |||||||||||||||||||||||||||||||
| 82 | 40 | ▶ | if (ref $where eq 'ARRAY') { | ||||||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||||||
| 83 | 19 | my $sql = shift @$where; | |||||||||||||||||||||||||||||||
| 84 | 19 | ▶ | if ($sql =~ m!\s*:[A-Za-z_][A-Za-z0-9_]+\s*!o) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 85 | 1 | ▶ | @$where % 2 and croak "You gave me an odd number of parameters to 'where'!"; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 86 | 1 | my %named_values = @$where; | |||||||||||||||||||||||||||||||
| 87 | 1 | my @values; | |||||||||||||||||||||||||||||||
| 88 | 1 | $sql =~ s{:([A-Za-z_][A-Za-z0-9_]*)}{ | |||||||||||||||||||||||||||||||
| 89 | 1 | ▶ | croak "$1 is not exists in hash" if !exists $named_values{$1}; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 90 | 1 | my $value = $named_values{$1}; | |||||||||||||||||||||||||||||||
| 91 | 1 | ▶ | if (ref $value eq 'ARRAY') { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 92 | 0 | push @values, $_ for @$value; | |||||||||||||||||||||||||||||||
| 93 | 0 | join ',', map('?', 1..@$value); | |||||||||||||||||||||||||||||||
| 94 | } else { | ||||||||||||||||||||||||||||||||
| 95 | 1 | push @values, $value; | |||||||||||||||||||||||||||||||
| 96 | 1 | '?' | |||||||||||||||||||||||||||||||
| 97 | } | ||||||||||||||||||||||||||||||||
| 98 | }ge; | ||||||||||||||||||||||||||||||||
| 99 | 1 | $binds = \@values; | |||||||||||||||||||||||||||||||
| 100 | } else { | ||||||||||||||||||||||||||||||||
| 101 | 18 | $binds = $where; | |||||||||||||||||||||||||||||||
| 102 | } | ||||||||||||||||||||||||||||||||
| 103 | 19 | return (' WHERE ' . $sql, @$binds); | |||||||||||||||||||||||||||||||
| 104 | } elsif (ref $where eq 'HASH') { | ||||||||||||||||||||||||||||||||
| 105 | 16 | return $sqla->where($where); | |||||||||||||||||||||||||||||||
| 106 | } elsif ($where) { | ||||||||||||||||||||||||||||||||
| 107 | 3 | return ' WHERE ' . $where; | |||||||||||||||||||||||||||||||
| 108 | } | ||||||||||||||||||||||||||||||||
| 109 | 2 | return $where; | |||||||||||||||||||||||||||||||
| 110 | } | ||||||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||||||
| 112 | 40 | ▶ | sub _parse_limit { | ||||||||||||||||||||||||||||||
|
Subroutine
_parse_limit: called CC=6 SCAR=18.0
| |||||||||||||||||||||||||||||||||
| 113 | 40 | my ($class, $args) = @_; | |||||||||||||||||||||||||||||||
| 114 | 40 | my $sql = ''; | |||||||||||||||||||||||||||||||
| 115 | 40 | ▶ | if ($args->{offset} || $args->{limit}) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$args->{"offset"} or $args->{"limit"}
MC/DC100%2/2
$args->{"offset"} or $args->{"limit"}
$args->{"offset"}
$args->{"limit"}
Truth table100%3/3
A or B
A= $args->{"offset"}B= $args->{"limit"}
| |||||||||||||||||||||||||||||||||
| 116 | 7 | $sql .= " LIMIT "; | |||||||||||||||||||||||||||||||
| 117 | 7 | ▶ | if ($args->{offset} && $args->{offset} =~ m/^\d+$/o) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$args->{"offset"} and $args->{"offset"} =~ /^\d+$/o
MC/DC50%1/2
$args->{"offset"} and $args->{"offset"} =~ /^\d+$/o
$args->{"offset"}
$args->{"offset"} =~ /^\d+$/o
Truth table66%2/3
A and B
A= $args->{"offset"}B= $args->{"offset"} =~ /^\d+$/o
| |||||||||||||||||||||||||||||||||
| 118 | 1 | $sql .= $args->{offset}.","; | |||||||||||||||||||||||||||||||
| 119 | } | ||||||||||||||||||||||||||||||||
| 120 | 7 | ▶ | $sql .= $args->{limit} =~ /^\d+$/o ? $args->{limit} : '1'; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 121 | } | ||||||||||||||||||||||||||||||||
| 122 | 40 | return $sql; | |||||||||||||||||||||||||||||||
| 123 | } | ||||||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||||||
| 125 | 20 | ▶ | sub dsn { | ||||||||||||||||||||||||||||||
|
Subroutine
dsn: called CC=10 SCAR=23.1
Pod
documented
| |||||||||||||||||||||||||||||||||
| 126 | 20 | my $class = shift; | |||||||||||||||||||||||||||||||
| 127 | 20 | my ($master_dsn, $slave_dsn); | |||||||||||||||||||||||||||||||
| 128 | 20 | ▶ | if ($_[0] && ref($_[0]) eq 'HASH') { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$_[0] and ref $_[0] eq "HASH"
MC/DC50%1/2
$_[0] and ref $_[0] eq "HASH"
$_[0]
ref $_[0] eq "HASH"
Truth table66%2/3
A and B
A= $_[0]B= ref $_[0] eq "HASH"
| |||||||||||||||||||||||||||||||||
| 129 | 1 | @_ = (%{$_[0]}); | |||||||||||||||||||||||||||||||
| 130 | } | ||||||||||||||||||||||||||||||||
| 131 | 20 | ▶ | if ($_[1]) { | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||
| 132 | 3 | my %args = @_; | |||||||||||||||||||||||||||||||
| 133 | 3 | ▶ | my $master = $args{master} or croak "master dsn is not specified"; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 134 | 3 | ▶ | $master_dsn = ref($master) eq 'ARRAY' ? $master : [$master]; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 135 | 3 | ▶ | my $slave = $args{slave} || $master; | ||||||||||||||||||||||||||||||
|
Condition66%2/3
$args{'slave'} || $master
MC/DC0%0/2
$args{'slave'} || $master
$args{'slave'}
$master
Truth table66%2/3
A || B
A= $args{'slave'}B= $master
| |||||||||||||||||||||||||||||||||
| 136 | 3 | ▶ | $slave_dsn = ref($slave) eq 'ARRAY' ? $slave : [$slave]; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 137 | } elsif ($_[0]) { | ||||||||||||||||||||||||||||||||
| 138 | 17 | ▶ | $slave_dsn = $master_dsn = ref($_[0]) eq 'ARRAY' ? $_[0] : [$_[0]]; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 139 | } else { | ||||||||||||||||||||||||||||||||
| 140 | 0 | croak "Please specify your dsn."; | |||||||||||||||||||||||||||||||
| 141 | } | ||||||||||||||||||||||||||||||||
| 142 | # $dsn->{$class} = { | ||||||||||||||||||||||||||||||||
| 143 | # master => $master_dsn, | ||||||||||||||||||||||||||||||||
| 144 | # slave => $slave_dsn, | ||||||||||||||||||||||||||||||||
| 145 | # }; | ||||||||||||||||||||||||||||||||
| 146 | 20 | my $getter = $class . '::get_dsn'; | |||||||||||||||||||||||||||||||
| 147 | { | ||||||||||||||||||||||||||||||||
| 148 | 13 | ▶ | no strict 'refs'; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 149 | 13 | ▶ | no warnings 'redefine'; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 150 | 20 | ▶ | *{$getter} = sub { | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||||
| 151 | 416 | my $class = shift; | |||||||||||||||||||||||||||||||
| 152 | 416 | my $sql = shift; | |||||||||||||||||||||||||||||||
| 153 | 416 | my $list = $master_dsn; | |||||||||||||||||||||||||||||||
| 154 | 416 | ▶ | if ($sql && $sql =~ /^SELECT/io) { $list = $slave_dsn } | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$sql and $sql =~ /^SELECT/io
MC/DC100%2/2
$sql and $sql =~ /^SELECT/io
$sql
$sql =~ /^SELECT/io
Truth table100%3/3
A and B
A= $sqlB= $sql =~ /^SELECT/io
| |||||||||||||||||||||||||||||||||
| 155 | 416 | my $dsn = shift @$list; | |||||||||||||||||||||||||||||||
| 156 | 416 | push @$list, $dsn; | |||||||||||||||||||||||||||||||
| 157 | 416 | return $dsn; | |||||||||||||||||||||||||||||||
| 158 | } | ||||||||||||||||||||||||||||||||
| 159 | 20 | } | |||||||||||||||||||||||||||||||
| 160 | } | ||||||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||||||
| 162 | 0 | ▶ | sub get_dsn { croak "You must set up your dsn first" } | ||||||||||||||||||||||||||||||
|
Subroutine
get_dsn: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||||||
| 164 | 393 | ▶ | sub dbh { | ||||||||||||||||||||||||||||||
|
Subroutine
dbh: called CC=2 SCAR=7.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 165 | 393 | my $class = shift; | |||||||||||||||||||||||||||||||
| 166 | 393 | my $sql = shift; | |||||||||||||||||||||||||||||||
| 167 | 393 | ▶ | my $connect = $class->cache_connection ? 'connect_cached' : 'connect'; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 168 | 393 | my $dsn = $class->get_dsn($sql); | |||||||||||||||||||||||||||||||
| 169 | 393 | my $opt = {RaiseError => 1}; | |||||||||||||||||||||||||||||||
| 170 | 393 | DBI->$connect($dsn, $class->username, $class->password, $opt); | |||||||||||||||||||||||||||||||
| 171 | } | ||||||||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||||||
| 173 | 340 | ▶ | sub execute { | ||||||||||||||||||||||||||||||
|
Subroutine
execute: called CC=15 SCAR=35.1
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 174 | 340 | my $class = shift; | |||||||||||||||||||||||||||||||
| 175 | 340 | my ($sql, $data, $binds) = @_; | |||||||||||||||||||||||||||||||
| 176 | 340 | ▶ | $sql or return; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 177 | 340 | ▶ | my @bind_values = ref $binds eq 'ARRAY' ? @$binds : (); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 178 | 340 | my $dbh = $class->dbh(substr($sql,0,8)); | |||||||||||||||||||||||||||||||
| 179 | 340 | ▶ | my $sth = @bind_values ? $dbh->prepare_cached($sql,undef,1) : | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 180 | $dbh->prepare($sql); | ||||||||||||||||||||||||||||||||
| 181 | 0 | ▶ | unless ($sth) { carp $dbh->errstr and return; } | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||
| 182 | 340 | ▶ | if ($DEBUG) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 183 | 0 | ▶ | my @binds = map { defined $_ ? "'$_'" : "'NULL'" } @bind_values; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 184 | 0 | carp $sql . '->execute(' . join(',', @binds) . ')'; | |||||||||||||||||||||||||||||||
| 185 | 0 | $SQL_COUNT++; | |||||||||||||||||||||||||||||||
| 186 | } | ||||||||||||||||||||||||||||||||
| 187 | |||||||||||||||||||||||||||||||||
| 188 | 0 | ▶ | my $sql_error = sub { | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||||||
| 189 | 0 | my ($sql, $sth) = @_; | |||||||||||||||||||||||||||||||
| 190 | 0 | ▶ | defined $data | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 191 | ? sprintf('SQL Error: "%s" (%s)', $sql, $sth->errstr) | ||||||||||||||||||||||||||||||||
| 192 | : sprintf('SQL Error "%s"', $sql); | ||||||||||||||||||||||||||||||||
| 193 | 340 | }; | |||||||||||||||||||||||||||||||
| 194 | |||||||||||||||||||||||||||||||||
| 195 | 340 | eval { | |||||||||||||||||||||||||||||||
| 196 | 340 | ▶ | if (defined $data) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 197 | 80 | ▶ | $sth->execute(@bind_values) or carp $sql_error->($sth, $sql) and return; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition20%1/5
$sth->execute(@bind_values) || carp(&$sql_error($sth, $sql)) && (return)
carp(&$sql_error($sth, $sql)) and return
MC/DC0%0/2
$sth->execute(@bind_values) || carp(&$sql_error($sth, $sql)) && (return)
$sth->execute(@bind_values)
carp(&$sql_error($sth, $sql))
Truth table33%1/3
A || B
A= $sth->execute(@bind_values)B= carp(&$sql_error($sth, $sql))
| |||||||||||||||||||||||||||||||||
| 198 | 80 | $$data = $sth->fetchall_arrayref({}); | |||||||||||||||||||||||||||||||
| 199 | } else { | ||||||||||||||||||||||||||||||||
| 200 | 260 | ▶ | unless ($sth->execute(@bind_values)) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 201 | 0 | $sql_error->($sql, $sth); | |||||||||||||||||||||||||||||||
| 202 | 0 | return; | |||||||||||||||||||||||||||||||
| 203 | } | ||||||||||||||||||||||||||||||||
| 204 | } | ||||||||||||||||||||||||||||||||
| 205 | }; | ||||||||||||||||||||||||||||||||
| 206 | 340 | ▶ | if ($@) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 207 | 0 | confess $sql_error->($sql, $sth); | |||||||||||||||||||||||||||||||
| 208 | } | ||||||||||||||||||||||||||||||||
| 209 | |||||||||||||||||||||||||||||||||
| 210 | 340 | ▶ | if ($sql =~ /^insert/io) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 211 | $class->last_insert_id($dbh->last_insert_id(undef,undef,undef,undef) || | ||||||||||||||||||||||||||||||||
| 212 | 179 | ▶ | $dbh->{'mysql_insertid'}); | ||||||||||||||||||||||||||||||
|
Condition66%2/3
$dbh->last_insert_id(undef, undef, undef, undef) || $dbh->{'mysql_insertid'}
MC/DC50%1/2
$dbh->last_insert_id(undef, undef, undef, undef) || $dbh->{'mysql_insertid'}
$dbh->last_insert_id(undef, undef, undef, undef)
$dbh->{'mysql_insertid'}
Truth table66%2/3
A || B
A= $dbh->last_insert_id(undef, undef, undef, undef)B= $dbh->{'mysql_insertid'}
| |||||||||||||||||||||||||||||||||
| 213 | } | ||||||||||||||||||||||||||||||||
| 214 | 340 | return !$sth->err; | |||||||||||||||||||||||||||||||
| 215 | } | ||||||||||||||||||||||||||||||||
| 216 | |||||||||||||||||||||||||||||||||
| 217 | 10 | ▶ | sub vendor { | ||||||||||||||||||||||||||||||
|
Subroutine
vendor: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 218 | 10 | my $class = shift; | |||||||||||||||||||||||||||||||
| 219 | 10 | $class->dbh->get_info(17); # SQL_DBMS_NAME | |||||||||||||||||||||||||||||||
| 220 | } | ||||||||||||||||||||||||||||||||
| 221 | |||||||||||||||||||||||||||||||||
| 222 | 8 | ▶ | sub primary_keys { | ||||||||||||||||||||||||||||||
|
Subroutine
primary_keys: called CC=6 SCAR=28.6
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 223 | 8 | my $class = shift; | |||||||||||||||||||||||||||||||
| 224 | 8 | ▶ | my $table = shift or return; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 225 | 8 | my $dbh = $class->dbh; | |||||||||||||||||||||||||||||||
| 226 | 8 | ▶ | if ($class->vendor eq 'MySQL') { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 227 | 0 | ▶ | my $sth = $dbh->column_info(undef,undef,$table,'%') or | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 228 | croak $dbh->errstr; | ||||||||||||||||||||||||||||||||
| 229 | 0 | ▶ | $dbh->err and croak $dbh->errstr; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 230 | 0 | ▶ | my @cols = @{$sth->fetchall_arrayref({})} or | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 231 | croak "couldnt get primary keys"; | ||||||||||||||||||||||||||||||||
| 232 | return [ | ||||||||||||||||||||||||||||||||
| 233 | 0 | map {$_->{COLUMN_NAME}} | |||||||||||||||||||||||||||||||
| 234 | 0 | grep {$_->{mysql_is_pri_key}} | |||||||||||||||||||||||||||||||
| 235 | @cols | ||||||||||||||||||||||||||||||||
| 236 | ]; | ||||||||||||||||||||||||||||||||
| 237 | } else { | ||||||||||||||||||||||||||||||||
| 238 | 8 | return [$dbh->primary_key(undef,undef,$table)]; | |||||||||||||||||||||||||||||||
| 239 | } | ||||||||||||||||||||||||||||||||
| 240 | } | ||||||||||||||||||||||||||||||||
| 241 | |||||||||||||||||||||||||||||||||
| 242 | 2 | ▶ | sub unique_keys { | ||||||||||||||||||||||||||||||
|
Subroutine
unique_keys: called CC=5 SCAR=25.7
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 243 | 2 | my $class = shift; | |||||||||||||||||||||||||||||||
| 244 | 2 | ▶ | my $table = shift or return; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 245 | 2 | ▶ | if ($class->vendor eq 'MySQL') { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 246 | 0 | my $sql = "SHOW INDEX FROM $table"; | |||||||||||||||||||||||||||||||
| 247 | 0 | my $data; | |||||||||||||||||||||||||||||||
| 248 | 0 | ▶ | $class->execute($sql,\$data) or | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 249 | croak "couldnt get unique keys"; | ||||||||||||||||||||||||||||||||
| 250 | 0 | ▶ | @$data or croak "couldnt get unique keys"; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 251 | return [ | ||||||||||||||||||||||||||||||||
| 252 | 0 | map {$_->{Column_name}} | |||||||||||||||||||||||||||||||
| 253 | 0 | grep {!$_->{Non_unique}} | |||||||||||||||||||||||||||||||
| 254 | @$data | ||||||||||||||||||||||||||||||||
| 255 | ]; | ||||||||||||||||||||||||||||||||
| 256 | } else { | ||||||||||||||||||||||||||||||||
| 257 | 2 | return $class->primary_keys($table); | |||||||||||||||||||||||||||||||
| 258 | } | ||||||||||||||||||||||||||||||||
| 259 | } | ||||||||||||||||||||||||||||||||
| 260 | |||||||||||||||||||||||||||||||||
| 261 | 13 | ▶ | sub columns { | ||||||||||||||||||||||||||||||
|
Subroutine
columns: called CC=6 SCAR=21.4
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 262 | 13 | my $class = shift; | |||||||||||||||||||||||||||||||
| 263 | 13 | ▶ | my $table = shift or return; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 264 | 13 | my $dbh = $class->dbh; | |||||||||||||||||||||||||||||||
| 265 | 13 | ▶ | if (my $sth = $class->dbh->column_info(undef,undef,$table,'%')) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 266 | 13 | ▶ | croak $dbh->errstr if $dbh->err; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 267 | 13 | ▶ | my @cols = @{$sth->fetchall_arrayref({})} or | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 268 | croak "couldnt get primary keys"; | ||||||||||||||||||||||||||||||||
| 269 | return [ | ||||||||||||||||||||||||||||||||
| 270 | 13 | map {$_->{COLUMN_NAME}} | |||||||||||||||||||||||||||||||
| 271 | @cols | ||||||||||||||||||||||||||||||||
| 272 | ]; | ||||||||||||||||||||||||||||||||
| 273 | } else { | ||||||||||||||||||||||||||||||||
| 274 | 0 | ▶ | my $d = $class->select($table,'*',undef,'',{limit => 1}) or return; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 275 | 0 | return [keys %{$d->[0]}]; | |||||||||||||||||||||||||||||||
| 276 | } | ||||||||||||||||||||||||||||||||
| 277 | } | ||||||||||||||||||||||||||||||||
| 278 | |||||||||||||||||||||||||||||||||
| 279 | 13 | 1; | |||||||||||||||||||||||||||||||
| 280 | |||||||||||||||||||||||||||||||||
| 281 | =head1 NAME | ||||||||||||||||||||||||||||||||
| 282 | |||||||||||||||||||||||||||||||||
| 283 | DBIx::MoCo::DataBase - Data Base Handler for DBIx::MoCo | ||||||||||||||||||||||||||||||||
| 284 | |||||||||||||||||||||||||||||||||
| 285 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||||||
| 286 | |||||||||||||||||||||||||||||||||
| 287 | package MyDataBase; | ||||||||||||||||||||||||||||||||
| 288 | use base qw(DBIx::MoCo::DataBase); | ||||||||||||||||||||||||||||||||
| 289 | |||||||||||||||||||||||||||||||||
| 290 | __PACKAGE__->dsn('dbi:mysql:myapp'); | ||||||||||||||||||||||||||||||||
| 291 | __PACKAGE__->username('test'); | ||||||||||||||||||||||||||||||||
| 292 | __PACKAGE__->password('test'); | ||||||||||||||||||||||||||||||||
| 293 | |||||||||||||||||||||||||||||||||
| 294 | 1; | ||||||||||||||||||||||||||||||||
| 295 | |||||||||||||||||||||||||||||||||
| 296 | # In your scripts | ||||||||||||||||||||||||||||||||
| 297 | MyDataBase->execute('select 1'); | ||||||||||||||||||||||||||||||||
| 298 | |||||||||||||||||||||||||||||||||
| 299 | # Configure your replication databases | ||||||||||||||||||||||||||||||||
| 300 | __PACKAGE__->dsn( | ||||||||||||||||||||||||||||||||
| 301 | master => 'dbi:mysql:dbname=test;host=db1', | ||||||||||||||||||||||||||||||||
| 302 | slave => ['dbi:mysql:dbname=test;host=db2','dbi:mysql:dbname=test;host=db3'], | ||||||||||||||||||||||||||||||||
| 303 | ); | ||||||||||||||||||||||||||||||||
| 304 | |||||||||||||||||||||||||||||||||
| 305 | =head1 METHODS | ||||||||||||||||||||||||||||||||
| 306 | |||||||||||||||||||||||||||||||||
| 307 | =over 4 | ||||||||||||||||||||||||||||||||
| 308 | |||||||||||||||||||||||||||||||||
| 309 | =item cache_connection | ||||||||||||||||||||||||||||||||
| 310 | |||||||||||||||||||||||||||||||||
| 311 | Controlls cache behavior for dbh connection. (default 1) | ||||||||||||||||||||||||||||||||
| 312 | If its set to 0, DBIx::MoCo::DataBase uses DBI->connect instead of | ||||||||||||||||||||||||||||||||
| 313 | DBI->connect_cached. | ||||||||||||||||||||||||||||||||
| 314 | |||||||||||||||||||||||||||||||||
| 315 | DBIx::MoCo::DataBase->cache_connection(0); | ||||||||||||||||||||||||||||||||
| 316 | |||||||||||||||||||||||||||||||||
| 317 | =item dsn | ||||||||||||||||||||||||||||||||
| 318 | |||||||||||||||||||||||||||||||||
| 319 | Configures dsn(s). You can specify single dsn as string, multiple dsns as an array, | ||||||||||||||||||||||||||||||||
| 320 | master/slave dsns as hash. | ||||||||||||||||||||||||||||||||
| 321 | |||||||||||||||||||||||||||||||||
| 322 | If you specify multiple dsns, they will be rotated automatically in round-robin. | ||||||||||||||||||||||||||||||||
| 323 | MoCo will use slave dsns when the sql begins with C<SELECT> if you set up slave(s). | ||||||||||||||||||||||||||||||||
| 324 | |||||||||||||||||||||||||||||||||
| 325 | MyDataBase->dsn('dbi:mysql:dbname=test'); | ||||||||||||||||||||||||||||||||
| 326 | MyDataBase->dsn(['dbi:mysql:dbname=test;host=db1','dbi:mysql:dbname=test;host=db2']); | ||||||||||||||||||||||||||||||||
| 327 | MyDataBase->dsn( | ||||||||||||||||||||||||||||||||
| 328 | master => ['dbi:mysql:dbname=test;host=db1','dbi:mysql:dbname=test;host=db2'], | ||||||||||||||||||||||||||||||||
| 329 | ); | ||||||||||||||||||||||||||||||||
| 330 | MyDataBase->dsn( | ||||||||||||||||||||||||||||||||
| 331 | master => 'dbi:mysql:dbname=test;host=db1', | ||||||||||||||||||||||||||||||||
| 332 | slave => ['dbi:mysql:dbname=test;host=db2','dbi:mysql:dbname=test;host=db3'], | ||||||||||||||||||||||||||||||||
| 333 | ); | ||||||||||||||||||||||||||||||||
| 334 | |||||||||||||||||||||||||||||||||
| 335 | =back | ||||||||||||||||||||||||||||||||
| 336 | |||||||||||||||||||||||||||||||||
| 337 | =head1 SEE ALSO | ||||||||||||||||||||||||||||||||
| 338 | |||||||||||||||||||||||||||||||||
| 339 | L<DBIx::MoCo>, L<SQL::Abstract> | ||||||||||||||||||||||||||||||||
| 340 | |||||||||||||||||||||||||||||||||
| 341 | =head1 AUTHOR | ||||||||||||||||||||||||||||||||
| 342 | |||||||||||||||||||||||||||||||||
| 343 | Junya Kondo, E<lt>jkondo@hatena.comE<gt> | ||||||||||||||||||||||||||||||||
| 344 | |||||||||||||||||||||||||||||||||
| 345 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||||||||||||||||||||
| 346 | |||||||||||||||||||||||||||||||||
| 347 | Copyright (C) Hatena Inc. All Rights Reserved. | ||||||||||||||||||||||||||||||||
| 348 | |||||||||||||||||||||||||||||||||
| 349 | This library is free software; you may redistribute it and/or modify | ||||||||||||||||||||||||||||||||
| 350 | it under the same terms as Perl itself. | ||||||||||||||||||||||||||||||||
| 351 | |||||||||||||||||||||||||||||||||
| 352 | =cut | ||||||||||||||||||||||||||||||||