| 1 | package Data::Model::Driver::DBI; | ||||||||||||||||||||||||||||||||||||||||||
| 2 | 3 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 3 | 3 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 4 | 3 | ▶ | use base 'Data::Model::Driver'; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||||||||||||||||
| 6 | 3 | ▶ | use Carp (); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 7 | 3 | $Carp::Internal{(__PACKAGE__)}++; | |||||||||||||||||||||||||||||||||||||||||
| 8 | 3 | ▶ | use DBI; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||||||||||||||||
| 10 | 3 | ▶ | use Data::Model::SQL; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 11 | 3 | ▶ | use Data::Model::Driver::DBI::DBD; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||||||||||||||||
| 13 | 12 | ▶ | sub dbd { $_[0]->{dbd} } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
dbd: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||||||||||||||||
| 15 | 18 | ▶ | sub dbi_config { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
dbi_config: called CC=2 SCAR=7.2
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 16 | 18 | my($self, $name) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 17 | 18 | ▶ | $self->{dbi_config}->{$name} | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 18 | or Carp::croak "has not dbi_config name '$name'"; | ||||||||||||||||||||||||||||||||||||||||||
| 19 | } | ||||||||||||||||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||||||||||||||||
| 21 | 4 | ▶ | sub init { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
init: called CC=2 SCAR=7.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 22 | 4 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 23 | 4 | ▶ | if (my($type) = $self->{dsn} =~ /^dbi:(\w*)/i) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 24 | 4 | $self->{dbd} = Data::Model::Driver::DBI::DBD->new($type); | |||||||||||||||||||||||||||||||||||||||||
| 25 | } | ||||||||||||||||||||||||||||||||||||||||||
| 26 | $self->{dbi_config} = +{ | ||||||||||||||||||||||||||||||||||||||||||
| 27 | rw => +{ | ||||||||||||||||||||||||||||||||||||||||||
| 28 | dsn => delete $self->{dsn}, | ||||||||||||||||||||||||||||||||||||||||||
| 29 | username => delete $self->{username}, | ||||||||||||||||||||||||||||||||||||||||||
| 30 | password => delete $self->{password}, | ||||||||||||||||||||||||||||||||||||||||||
| 31 | connect_options => delete $self->{connect_options}, | ||||||||||||||||||||||||||||||||||||||||||
| 32 | 4 | dbh => undef, | |||||||||||||||||||||||||||||||||||||||||
| 33 | }, | ||||||||||||||||||||||||||||||||||||||||||
| 34 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 35 | } | ||||||||||||||||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||||||||||||||||
| 37 | 3 | my %reuse_handles; | |||||||||||||||||||||||||||||||||||||||||
| 38 | 4 | ▶ | sub init_db { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
init_db: called CC=8 SCAR=21.2
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 39 | 4 | my($self, $name, %args) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 40 | 4 | my $dbi_config = $self->dbi_config($name); | |||||||||||||||||||||||||||||||||||||||||
| 41 | 4 | my $dsn = $dbi_config->{dsn}; | |||||||||||||||||||||||||||||||||||||||||
| 42 | 4 | my $dbh; | |||||||||||||||||||||||||||||||||||||||||
| 43 | 4 | ▶ | if ($self->{reuse_dbh}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 44 | 2 | $dbh = $reuse_handles{$dsn}; | |||||||||||||||||||||||||||||||||||||||||
| 45 | } | ||||||||||||||||||||||||||||||||||||||||||
| 46 | 4 | ▶ | unless ($dbh && ($args{no_ping} || $dbh->ping)) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%3/6
$dbh and $args{"no_ping"} || $dbh->ping
$args{"no_ping"} or $dbh->ping
MC/DC33%1/3
$dbh and $args{"no_ping"} || $dbh->ping
$dbh
$args{"no_ping"}
$dbh->ping
Truth table50%2/4
A and B or C
A= $dbhB= $args{"no_ping"}C= $dbh->ping
| |||||||||||||||||||||||||||||||||||||||||||
| 47 | $dbh = DBI->connect( | ||||||||||||||||||||||||||||||||||||||||||
| 48 | $dsn, $dbi_config->{username}, $dbi_config->{password}, | ||||||||||||||||||||||||||||||||||||||||||
| 49 | 3 | ▶ | { RaiseError => 1, PrintError => 0, AutoCommit => 1, %{ $dbi_config->{connect_options} || {} } }, | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%1/2
$dbi_config->{'connect_options'} || {}
MC/DC0%0/1
$dbi_config->{'connect_options'} || {}
$dbi_config->{'connect_options'}
Truth table50%1/2
A
A= $dbi_config->{'connect_options'}
| |||||||||||||||||||||||||||||||||||||||||||
| 50 | ) or Carp::croak("Connection error: " . $DBI::errstr); | ||||||||||||||||||||||||||||||||||||||||||
| 51 | 3 | ▶ | if ($self->{reuse_dbh}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 52 | 1 | $reuse_handles{$dsn} = $dbh; | |||||||||||||||||||||||||||||||||||||||||
| 53 | } | ||||||||||||||||||||||||||||||||||||||||||
| 54 | } | ||||||||||||||||||||||||||||||||||||||||||
| 55 | 4 | $self->{__dbh_init_by_driver} = 1; | |||||||||||||||||||||||||||||||||||||||||
| 56 | 4 | $dbh; | |||||||||||||||||||||||||||||||||||||||||
| 57 | } | ||||||||||||||||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||||||||||||||||
| 59 | 14 | ▶ | sub _get_dbh { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_get_dbh: called CC=9 SCAR=23.7
| |||||||||||||||||||||||||||||||||||||||||||
| 60 | 14 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 61 | 14 | ▶ | my $name = shift || 'rw'; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
shift() || 'rw'
MC/DC0%0/1
shift() || 'rw'
shift()
Truth table50%1/2
A
A= shift()
| |||||||||||||||||||||||||||||||||||||||||||
| 62 | 14 | my %args = @_; # this option is experimental | |||||||||||||||||||||||||||||||||||||||||
| 63 | 14 | my $dbi_config = $self->dbi_config($name); | |||||||||||||||||||||||||||||||||||||||||
| 64 | 14 | ▶ | unless ($args{no_ping}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 65 | 14 | ▶ | $dbi_config->{dbh} = undef if $dbi_config->{dbh} and !$dbi_config->{dbh}->ping; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$dbi_config->{"dbh"} and not $dbi_config->{"dbh"}->ping
MC/DC0%0/2
$dbi_config->{"dbh"} and not $dbi_config->{"dbh"}->ping
$dbi_config->{"dbh"}
not $dbi_config->{"dbh"}->ping
Truth table66%2/3
A and not(B)
A= $dbi_config->{"dbh"}B= not $dbi_config->{"dbh"}->ping
| |||||||||||||||||||||||||||||||||||||||||||
| 66 | } | ||||||||||||||||||||||||||||||||||||||||||
| 67 | 14 | ▶ | unless ($dbi_config->{dbh} || $args{cannot_reconnect}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$dbi_config->{"dbh"} or $args{"cannot_reconnect"}
MC/DC50%1/2
$dbi_config->{"dbh"} or $args{"cannot_reconnect"}
$dbi_config->{"dbh"}
$args{"cannot_reconnect"}
Truth table66%2/3
A or B
A= $dbi_config->{"dbh"}B= $args{"cannot_reconnect"}
| |||||||||||||||||||||||||||||||||||||||||||
| 68 | 4 | ▶ | if (my $getter = $self->{get_dbh}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 69 | 0 | $dbi_config->{dbh} = $getter->(); | |||||||||||||||||||||||||||||||||||||||||
| 70 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 71 | 4 | ▶ | $dbi_config->{dbh} = $self->init_db($name, %args) or Carp::croak $self->last_error; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 72 | } | ||||||||||||||||||||||||||||||||||||||||||
| 73 | } | ||||||||||||||||||||||||||||||||||||||||||
| 74 | 14 | $dbi_config->{dbh}; | |||||||||||||||||||||||||||||||||||||||||
| 75 | } | ||||||||||||||||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||||||||||||||||
| 77 | 14 | ▶ | sub rw_handle { shift->_get_dbh('rw', @_) }; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
rw_handle: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 78 | 3 | ▶ | sub r_handle { shift->rw_handle(@_) } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
r_handle: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||||||||||||||||
| 80 | 0 | ▶ | sub last_error {} | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
last_error: not called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||||||||||||||||
| 82 | 3 | ▶ | sub add_key_to_where { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
add_key_to_where: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 83 | 3 | my($self, $stmt, $columns, $key) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 84 | 3 | ▶ | if ($key) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 85 | # add where | ||||||||||||||||||||||||||||||||||||||||||
| 86 | 3 | my $i = 0; | |||||||||||||||||||||||||||||||||||||||||
| 87 | 3 | for my $i (0..( scalar(@{ $key }) - 1 )) { | |||||||||||||||||||||||||||||||||||||||||
| 88 | 3 | $stmt->add_where( $columns->[$i] => $key->[$i] ); | |||||||||||||||||||||||||||||||||||||||||
| 89 | } | ||||||||||||||||||||||||||||||||||||||||||
| 90 | } | ||||||||||||||||||||||||||||||||||||||||||
| 91 | } | ||||||||||||||||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||||||||||||||||
| 93 | 0 | ▶ | sub add_index_to_where { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
add_index_to_where: not called CC=5 SCAR=34.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 94 | 0 | my($self, $schema, $stmt, $index_obj) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 95 | 0 | ▶ | return unless my($index, $index_key) = (%{ $index_obj }); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 96 | 0 | ▶ | $index_key = [ $index_key ] unless ref($index_key) eq 'ARRAY'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 97 | 0 | for my $index_type (qw/ unique index /) { | |||||||||||||||||||||||||||||||||||||||||
| 98 | 0 | ▶ | if (exists $schema->$index_type->{$index}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 99 | 0 | $self->add_key_to_where($stmt, $schema->$index_type->{$index}, $index_key); | |||||||||||||||||||||||||||||||||||||||||
| 100 | 0 | last; | |||||||||||||||||||||||||||||||||||||||||
| 101 | } | ||||||||||||||||||||||||||||||||||||||||||
| 102 | } | ||||||||||||||||||||||||||||||||||||||||||
| 103 | } | ||||||||||||||||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||||||||||||||||
| 105 | 5 | ▶ | sub bind_params { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
bind_params: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 106 | 5 | my($self, $schema, $columns, $sth) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 107 | 5 | my $i = 1; | |||||||||||||||||||||||||||||||||||||||||
| 108 | 5 | for my $column (@{ $columns }) { | |||||||||||||||||||||||||||||||||||||||||
| 109 | 5 | my($col, $val) = @{ $column }; | |||||||||||||||||||||||||||||||||||||||||
| 110 | 5 | my $type = $schema->column_type($col); | |||||||||||||||||||||||||||||||||||||||||
| 111 | 5 | my $attr = $self->dbd->bind_param_attributes($type, $columns, $col); | |||||||||||||||||||||||||||||||||||||||||
| 112 | 5 | ▶ | $sth->bind_param($i++, $val, $attr || undef); | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$attr || undef
MC/DC0%0/1
$attr || undef
$attr
Truth table50%1/2
A
A= $attr
| |||||||||||||||||||||||||||||||||||||||||||
| 113 | } | ||||||||||||||||||||||||||||||||||||||||||
| 114 | } | ||||||||||||||||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||||||||||||||||
| 116 | 3 | ▶ | sub fetch { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
fetch: called CC=11 SCAR=24.7
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 117 | 3 | my($self, $rec, $schema, $key, $columns, %args) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||||||||||||||||
| 119 | 3 | ▶ | $columns = +{} unless $columns; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||||||||||||||||
| 121 | 3 | ▶ | $columns->{select} ||= [ $schema->column_names ]; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$columns->{"select"} ||= [$schema->column_names]
MC/DC0%0/1
$columns->{"select"} ||= [$schema->column_names]
$columns->{"select"}
Truth table50%1/2
A
A= $columns->{"select"}
| |||||||||||||||||||||||||||||||||||||||||||
| 122 | 3 | ▶ | $columns->{from} ||= []; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$columns->{"from"} ||= []
MC/DC0%0/1
$columns->{"from"} ||= []
$columns->{"from"}
Truth table50%1/2
A
A= $columns->{"from"}
| |||||||||||||||||||||||||||||||||||||||||||
| 123 | 3 | unshift @{ $columns->{from} }, $schema->model; | |||||||||||||||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||||||||||||||||
| 125 | 3 | my $index_query = delete $columns->{index}; | |||||||||||||||||||||||||||||||||||||||||
| 126 | 3 | my $stmt = Data::Model::SQL->new(%{ $columns }); | |||||||||||||||||||||||||||||||||||||||||
| 127 | 3 | ▶ | $self->add_key_to_where($stmt, $schema->key, $key) if $key; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 128 | 3 | ▶ | $self->add_index_to_where($schema, $stmt, $index_query) if $index_query; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 129 | 3 | my $sql = $stmt->as_sql; | |||||||||||||||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||||||||||||||||
| 131 | # bind_params | ||||||||||||||||||||||||||||||||||||||||||
| 132 | 3 | my @params; | |||||||||||||||||||||||||||||||||||||||||
| 133 | 3 | for my $i (1..scalar(@{ $stmt->bind })) { | |||||||||||||||||||||||||||||||||||||||||
| 134 | 3 | push @params, [ $stmt->bind_column->[$i - 1], $stmt->bind->[$i - 1] ]; | |||||||||||||||||||||||||||||||||||||||||
| 135 | } | ||||||||||||||||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||||||||||||||||
| 137 | 3 | my @bind; | |||||||||||||||||||||||||||||||||||||||||
| 138 | 3 | my $map = $stmt->select_map; | |||||||||||||||||||||||||||||||||||||||||
| 139 | 3 | for my $col (@{ $stmt->select }) { | |||||||||||||||||||||||||||||||||||||||||
| 140 | 3 | ▶ | push @bind, \$rec->{ exists $map->{$col} ? $map->{$col} : $col }; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 141 | } | ||||||||||||||||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||||||||||||||||
| 143 | 3 | my $sth; | |||||||||||||||||||||||||||||||||||||||||
| 144 | 3 | eval { | |||||||||||||||||||||||||||||||||||||||||
| 145 | 3 | my $dbh = $self->r_handle; | |||||||||||||||||||||||||||||||||||||||||
| 146 | 3 | $self->start_query($sql, $stmt->bind); | |||||||||||||||||||||||||||||||||||||||||
| 147 | 3 | ▶ | $sth = $args{no_cached_prepare} ? $dbh->prepare($sql) : $dbh->prepare_cached($sql); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 148 | 3 | $self->bind_params($schema, \@params, $sth); | |||||||||||||||||||||||||||||||||||||||||
| 149 | 3 | $sth->execute; | |||||||||||||||||||||||||||||||||||||||||
| 150 | 3 | $sth->bind_columns(undef, @bind); | |||||||||||||||||||||||||||||||||||||||||
| 151 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 152 | 3 | ▶ | if ($@) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 153 | 0 | $self->_stack_trace($sth, $sql, $stmt->bind, $@); | |||||||||||||||||||||||||||||||||||||||||
| 154 | } | ||||||||||||||||||||||||||||||||||||||||||
| 155 | 3 | $sth; | |||||||||||||||||||||||||||||||||||||||||
| 156 | } | ||||||||||||||||||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||||||||||||||||
| 158 | 0 | ▶ | sub lookup { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
lookup: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 159 | 0 | my($self, $schema, $id, %args) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||||||||||||||||
| 161 | 0 | my $rec = +{}; | |||||||||||||||||||||||||||||||||||||||||
| 162 | 0 | my $sth = $self->fetch($rec, $schema, $id, {}, %args); | |||||||||||||||||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||||||||||||||||
| 164 | 0 | my $rv = $sth->fetch; | |||||||||||||||||||||||||||||||||||||||||
| 165 | 0 | $sth->finish; | |||||||||||||||||||||||||||||||||||||||||
| 166 | 0 | $self->end_query($sth); | |||||||||||||||||||||||||||||||||||||||||
| 167 | 0 | undef $sth; | |||||||||||||||||||||||||||||||||||||||||
| 168 | 0 | ▶ | return unless $rv; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 169 | 0 | return $rec; | |||||||||||||||||||||||||||||||||||||||||
| 170 | } | ||||||||||||||||||||||||||||||||||||||||||
| 171 | |||||||||||||||||||||||||||||||||||||||||||
| 172 | 0 | ▶ | sub lookup_multi { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
lookup_multi: not called CC=4 SCAR=30.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 173 | 0 | my($self, $schema, $ids, %args) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||||||||||||||||
| 175 | 0 | my @keys = @{ $schema->key }; | |||||||||||||||||||||||||||||||||||||||||
| 176 | 0 | my $query = {}; | |||||||||||||||||||||||||||||||||||||||||
| 177 | 0 | ▶ | if (@keys == 1) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 178 | 0 | my @id_list = map { $_->[0] } @{ $ids }; | |||||||||||||||||||||||||||||||||||||||||
| 179 | 0 | $query = { where => [ $keys[0] => \@id_list ] }; | |||||||||||||||||||||||||||||||||||||||||
| 180 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 181 | 0 | my @queries; | |||||||||||||||||||||||||||||||||||||||||
| 182 | 0 | for my $id (@{ $ids }) { | |||||||||||||||||||||||||||||||||||||||||
| 183 | 0 | my %query; | |||||||||||||||||||||||||||||||||||||||||
| 184 | 0 | @query{@keys} = @{ $id }; | |||||||||||||||||||||||||||||||||||||||||
| 185 | 0 | push @queries, '-and' => [ %query ]; | |||||||||||||||||||||||||||||||||||||||||
| 186 | } | ||||||||||||||||||||||||||||||||||||||||||
| 187 | 0 | $query = { where => [ -or => \@queries ] }; | |||||||||||||||||||||||||||||||||||||||||
| 188 | } | ||||||||||||||||||||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||||||||||||||||||||
| 190 | 0 | my $rec = +{}; | |||||||||||||||||||||||||||||||||||||||||
| 191 | 0 | local $args{no_cached_prepare} = 1; | |||||||||||||||||||||||||||||||||||||||||
| 192 | 0 | my $sth = $self->fetch($rec, $schema, undef, $query, %args); | |||||||||||||||||||||||||||||||||||||||||
| 193 | |||||||||||||||||||||||||||||||||||||||||||
| 194 | 0 | my %resultlist; | |||||||||||||||||||||||||||||||||||||||||
| 195 | 0 | ▶ | while ($sth->fetch) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 196 | 0 | my $key = $schema->get_key_array_by_hash($rec); | |||||||||||||||||||||||||||||||||||||||||
| 197 | 0 | $resultlist{join "\0", @{ $key }} = +{ %{ $rec } }; | |||||||||||||||||||||||||||||||||||||||||
| 198 | } | ||||||||||||||||||||||||||||||||||||||||||
| 199 | |||||||||||||||||||||||||||||||||||||||||||
| 200 | 0 | $sth->finish; | |||||||||||||||||||||||||||||||||||||||||
| 201 | 0 | $self->end_query($sth); | |||||||||||||||||||||||||||||||||||||||||
| 202 | 0 | undef $sth; | |||||||||||||||||||||||||||||||||||||||||
| 203 | |||||||||||||||||||||||||||||||||||||||||||
| 204 | 0 | \%resultlist; | |||||||||||||||||||||||||||||||||||||||||
| 205 | } | ||||||||||||||||||||||||||||||||||||||||||
| 206 | |||||||||||||||||||||||||||||||||||||||||||
| 207 | 3 | ▶ | sub get { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
get: called CC=2 SCAR=7.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 208 | 3 | my($self, $schema, $key, $columns, %args) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 209 | |||||||||||||||||||||||||||||||||||||||||||
| 210 | 3 | my $rec = +{}; | |||||||||||||||||||||||||||||||||||||||||
| 211 | 3 | my $sth = $self->fetch($rec, $schema, $key, $columns, %args); | |||||||||||||||||||||||||||||||||||||||||
| 212 | |||||||||||||||||||||||||||||||||||||||||||
| 213 | 3 | my $i = 0; | |||||||||||||||||||||||||||||||||||||||||
| 214 | 7 | ▶ | my $iterator = sub { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||||||||||||||||
| 215 | 7 | ▶ | return unless $sth; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 216 | 7 | ▶ | return $rec if $i++ eq 1; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 217 | 5 | ▶ | unless ($sth->fetch) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 218 | 3 | $sth->finish; | |||||||||||||||||||||||||||||||||||||||||
| 219 | 3 | $self->end_query($sth); | |||||||||||||||||||||||||||||||||||||||||
| 220 | 3 | undef $sth; | |||||||||||||||||||||||||||||||||||||||||
| 221 | 3 | return; | |||||||||||||||||||||||||||||||||||||||||
| 222 | } | ||||||||||||||||||||||||||||||||||||||||||
| 223 | 2 | $rec; | |||||||||||||||||||||||||||||||||||||||||
| 224 | 3 | }; | |||||||||||||||||||||||||||||||||||||||||
| 225 | |||||||||||||||||||||||||||||||||||||||||||
| 226 | # pre load | ||||||||||||||||||||||||||||||||||||||||||
| 227 | 3 | ▶ | return unless $iterator->(); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 228 | return $iterator, +{ | ||||||||||||||||||||||||||||||||||||||||||
| 229 | 0 | ▶ | end => sub { if ($sth) { $sth->finish; $self->end_query($sth); undef $sth; } }, | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=11.6
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 230 | 2 | }; | |||||||||||||||||||||||||||||||||||||||||
| 231 | } | ||||||||||||||||||||||||||||||||||||||||||
| 232 | |||||||||||||||||||||||||||||||||||||||||||
| 233 | # insert or replace | ||||||||||||||||||||||||||||||||||||||||||
| 234 | 2 | ▶ | sub set { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
set: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 235 | 2 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 236 | 2 | $self->_insert_or_replace(0, @_); | |||||||||||||||||||||||||||||||||||||||||
| 237 | } | ||||||||||||||||||||||||||||||||||||||||||
| 238 | |||||||||||||||||||||||||||||||||||||||||||
| 239 | 0 | ▶ | sub replace { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
replace: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 240 | 0 | my($self, $schema, $key, $columns, %args) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 241 | 0 | ▶ | if ($self->dbd->can_replace) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 242 | 0 | return $self->_insert_or_replace(1, $schema, $key, $columns, %args); | |||||||||||||||||||||||||||||||||||||||||
| 243 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 244 | # $self->thx(sub { | ||||||||||||||||||||||||||||||||||||||||||
| 245 | 0 | $self->delete($schema, $key, +{}, %args); | |||||||||||||||||||||||||||||||||||||||||
| 246 | 0 | return $self->set($schema, $key, $columns, %args); | |||||||||||||||||||||||||||||||||||||||||
| 247 | # }); | ||||||||||||||||||||||||||||||||||||||||||
| 248 | } | ||||||||||||||||||||||||||||||||||||||||||
| 249 | } | ||||||||||||||||||||||||||||||||||||||||||
| 250 | |||||||||||||||||||||||||||||||||||||||||||
| 251 | 0 | ▶ | sub _on_duplicate_key_update { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_on_duplicate_key_update: not called CC=12 SCAR=50.5
| |||||||||||||||||||||||||||||||||||||||||||
| 252 | 0 | my($self, $schema, $columns, $args, $sql, $column_list) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 253 | 0 | my $table = $schema->model; | |||||||||||||||||||||||||||||||||||||||||
| 254 | |||||||||||||||||||||||||||||||||||||||||||
| 255 | # check unique keys | ||||||||||||||||||||||||||||||||||||||||||
| 256 | 0 | my $keys = $schema->key; | |||||||||||||||||||||||||||||||||||||||||
| 257 | 0 | my $unique = $schema->unique; | |||||||||||||||||||||||||||||||||||||||||
| 258 | 0 | my $key_columns = []; | |||||||||||||||||||||||||||||||||||||||||
| 259 | 0 | ▶ | if (scalar(@{ $keys }) >= 1) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 260 | 0 | ▶ | if (scalar(keys %{ $unique }) >= 1) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 261 | 0 | Carp::croak "on_duplicate_key_update support: $table has multi unique key"; | |||||||||||||||||||||||||||||||||||||||||
| 262 | } | ||||||||||||||||||||||||||||||||||||||||||
| 263 | # OK | ||||||||||||||||||||||||||||||||||||||||||
| 264 | 0 | $key_columns = $keys; | |||||||||||||||||||||||||||||||||||||||||
| 265 | 0 | } elsif (scalar(keys %{ $unique }) > 1) { | |||||||||||||||||||||||||||||||||||||||||
| 266 | 0 | Carp::croak "on_duplicate_key_update support: $table has multi unique key"; | |||||||||||||||||||||||||||||||||||||||||
| 267 | 0 | } elsif (scalar(keys %{ $unique }) == 1) { | |||||||||||||||||||||||||||||||||||||||||
| 268 | # OK | ||||||||||||||||||||||||||||||||||||||||||
| 269 | 0 | ▶ | while (my($k, $v) = each %{ $unique }) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 270 | 0 | $key_columns = $v; | |||||||||||||||||||||||||||||||||||||||||
| 271 | } | ||||||||||||||||||||||||||||||||||||||||||
| 272 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 273 | 0 | Carp::croak "on_duplicate_key_update support: $table not has key or unique index"; | |||||||||||||||||||||||||||||||||||||||||
| 274 | } | ||||||||||||||||||||||||||||||||||||||||||
| 275 | |||||||||||||||||||||||||||||||||||||||||||
| 276 | # check key num | ||||||||||||||||||||||||||||||||||||||||||
| 277 | 0 | my $has_keys = 1; | |||||||||||||||||||||||||||||||||||||||||
| 278 | 0 | for my $k (@{ $key_columns }) { | |||||||||||||||||||||||||||||||||||||||||
| 279 | 0 | ▶ | $has_keys = 0 unless defined $columns->{$k}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 280 | } | ||||||||||||||||||||||||||||||||||||||||||
| 281 | 0 | ▶ | Carp::croak "on_duplicate_key_update support: $table is insufficient keys" unless $has_keys; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 282 | |||||||||||||||||||||||||||||||||||||||||||
| 283 | # append sql | ||||||||||||||||||||||||||||||||||||||||||
| 284 | 0 | my @set; | |||||||||||||||||||||||||||||||||||||||||
| 285 | 0 | for my $column (keys %{ $args }) { | |||||||||||||||||||||||||||||||||||||||||
| 286 | 0 | my $val = $args->{$column}; | |||||||||||||||||||||||||||||||||||||||||
| 287 | 0 | ▶ | if (ref($val) eq 'SCALAR') { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 288 | 0 | push @set, "$column = " . ${ $val }; | |||||||||||||||||||||||||||||||||||||||||
| 289 | } elsif (!ref($val)) { | ||||||||||||||||||||||||||||||||||||||||||
| 290 | 0 | push @set, "$column = ?"; | |||||||||||||||||||||||||||||||||||||||||
| 291 | 0 | push @{ $column_list }, [ $column => $val ]; | |||||||||||||||||||||||||||||||||||||||||
| 292 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 293 | 0 | Carp::confess 'No references other than a SCALAR reference can use a update column'; | |||||||||||||||||||||||||||||||||||||||||
| 294 | } | ||||||||||||||||||||||||||||||||||||||||||
| 295 | } | ||||||||||||||||||||||||||||||||||||||||||
| 296 | 0 | ${ $sql } .= ' ON DUPLICATE KEY UPDATE ' . join(', ', @set) . "\n"; | |||||||||||||||||||||||||||||||||||||||||
| 297 | } | ||||||||||||||||||||||||||||||||||||||||||
| 298 | |||||||||||||||||||||||||||||||||||||||||||
| 299 | 2 | ▶ | sub _insert_or_replace { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_insert_or_replace: called CC=5 SCAR=16.6
| |||||||||||||||||||||||||||||||||||||||||||
| 300 | 2 | my($self, $is_replace, $schema, $key, $columns, %args) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 301 | 2 | ▶ | my $select_or_replace = $is_replace ? 'REPLACE' : 'INSERT'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 302 | |||||||||||||||||||||||||||||||||||||||||||
| 303 | 2 | my $table = $schema->model; | |||||||||||||||||||||||||||||||||||||||||
| 304 | 2 | my $cols = [ keys %{ $columns } ]; | |||||||||||||||||||||||||||||||||||||||||
| 305 | my @column_list = map { | ||||||||||||||||||||||||||||||||||||||||||
| 306 | 2 | [ $_ => $columns->{$_} ] | |||||||||||||||||||||||||||||||||||||||||
| 307 | 2 | } @{ $cols }; | |||||||||||||||||||||||||||||||||||||||||
| 308 | 2 | my $sql = "$select_or_replace INTO $table\n"; | |||||||||||||||||||||||||||||||||||||||||
| 309 | 2 | $sql .= '(' . join(', ', @{ $cols }) . ')' . "\n" . | |||||||||||||||||||||||||||||||||||||||||
| 310 | 2 | 'VALUES (' . join(', ', ('?') x @{ $cols }) . ')' . "\n"; | |||||||||||||||||||||||||||||||||||||||||
| 311 | |||||||||||||||||||||||||||||||||||||||||||
| 312 | # ON DUPLICATE KEY UPDATE support for MySQL | ||||||||||||||||||||||||||||||||||||||||||
| 313 | 2 | ▶ | if ($args{on_duplicate_key_update} && $self->dbd->has_support('on_duplicate_key_update')) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$args{"on_duplicate_key_update"} and $self->dbd->has_support("on_duplicate_key_update")
MC/DC0%0/2
$args{"on_duplicate_key_update"} and $self->dbd->has_support("on_duplicate_key_update")
$args{"on_duplicate_key_update"}
$self->dbd->has_support("on_duplicate_key_update")
Truth table33%1/3
A and B
A= $args{"on_duplicate_key_update"}B= $self->dbd->has_support("on_duplicate_key_update")
| |||||||||||||||||||||||||||||||||||||||||||
| 314 | 0 | $self->_on_duplicate_key_update($schema, $columns, $args{on_duplicate_key_update}, \$sql, \@column_list); | |||||||||||||||||||||||||||||||||||||||||
| 315 | } | ||||||||||||||||||||||||||||||||||||||||||
| 316 | |||||||||||||||||||||||||||||||||||||||||||
| 317 | 2 | my $sth; | |||||||||||||||||||||||||||||||||||||||||
| 318 | 2 | eval { | |||||||||||||||||||||||||||||||||||||||||
| 319 | 2 | my $dbh = $self->rw_handle; | |||||||||||||||||||||||||||||||||||||||||
| 320 | 2 | $self->start_query($sql, $columns); | |||||||||||||||||||||||||||||||||||||||||
| 321 | 2 | $sth = $dbh->prepare_cached($sql); | |||||||||||||||||||||||||||||||||||||||||
| 322 | 2 | $self->bind_params($schema, \@column_list, $sth); | |||||||||||||||||||||||||||||||||||||||||
| 323 | 2 | $sth->execute; | |||||||||||||||||||||||||||||||||||||||||
| 324 | 2 | $sth->finish; | |||||||||||||||||||||||||||||||||||||||||
| 325 | 2 | $self->end_query($sth); | |||||||||||||||||||||||||||||||||||||||||
| 326 | |||||||||||||||||||||||||||||||||||||||||||
| 327 | # set autoincrement key | ||||||||||||||||||||||||||||||||||||||||||
| 328 | 0 | ▶ | $self->_set_auto_increment($schema, $columns, sub { $self->dbd->fetch_last_id( $schema, $columns, $dbh, $sth ) }); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
| |||||||||||||||||||||||||||||||||||||||||||
| 329 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 330 | 2 | ▶ | if ($@) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 331 | 0 | $self->_stack_trace($sth, $sql, \@column_list, $@); | |||||||||||||||||||||||||||||||||||||||||
| 332 | } | ||||||||||||||||||||||||||||||||||||||||||
| 333 | |||||||||||||||||||||||||||||||||||||||||||
| 334 | 2 | undef $sth; | |||||||||||||||||||||||||||||||||||||||||
| 335 | 2 | $columns; | |||||||||||||||||||||||||||||||||||||||||
| 336 | } | ||||||||||||||||||||||||||||||||||||||||||
| 337 | |||||||||||||||||||||||||||||||||||||||||||
| 338 | # update | ||||||||||||||||||||||||||||||||||||||||||
| 339 | 0 | ▶ | sub _update { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_update: not called CC=7 SCAR=40.3
| |||||||||||||||||||||||||||||||||||||||||||
| 340 | 0 | my($self, $schema, $changed_columns, $columns, $where_sql, $pre_bind, $pre_bind_column) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 341 | |||||||||||||||||||||||||||||||||||||||||||
| 342 | 0 | my @bind; | |||||||||||||||||||||||||||||||||||||||||
| 343 | my @bind_column; | ||||||||||||||||||||||||||||||||||||||||||
| 344 | 0 | my @set; | |||||||||||||||||||||||||||||||||||||||||
| 345 | 0 | for my $column (keys %{ $changed_columns }) { | |||||||||||||||||||||||||||||||||||||||||
| 346 | 0 | my $val = $columns->{$column}; | |||||||||||||||||||||||||||||||||||||||||
| 347 | 0 | ▶ | if (ref($val) eq 'SCALAR') { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 348 | 0 | push @set, "$column = " . ${ $val }; | |||||||||||||||||||||||||||||||||||||||||
| 349 | } elsif (!ref($val)) { | ||||||||||||||||||||||||||||||||||||||||||
| 350 | 0 | push @set, "$column = ?"; | |||||||||||||||||||||||||||||||||||||||||
| 351 | 0 | push @bind, $val; | |||||||||||||||||||||||||||||||||||||||||
| 352 | 0 | push @bind_column, $column; | |||||||||||||||||||||||||||||||||||||||||
| 353 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 354 | 0 | Carp::confess 'No references other than a SCALAR reference can use a update column'; | |||||||||||||||||||||||||||||||||||||||||
| 355 | } | ||||||||||||||||||||||||||||||||||||||||||
| 356 | } | ||||||||||||||||||||||||||||||||||||||||||
| 357 | 0 | push @bind, @{ $pre_bind }; | |||||||||||||||||||||||||||||||||||||||||
| 358 | 0 | push @bind_column, @{ $pre_bind_column }; | |||||||||||||||||||||||||||||||||||||||||
| 359 | |||||||||||||||||||||||||||||||||||||||||||
| 360 | # bind_params | ||||||||||||||||||||||||||||||||||||||||||
| 361 | 0 | my @params; | |||||||||||||||||||||||||||||||||||||||||
| 362 | 0 | for my $i (1..scalar(@bind)) { | |||||||||||||||||||||||||||||||||||||||||
| 363 | 0 | push @params, [ $bind_column[$i - 1], $bind[$i - 1] ]; | |||||||||||||||||||||||||||||||||||||||||
| 364 | } | ||||||||||||||||||||||||||||||||||||||||||
| 365 | |||||||||||||||||||||||||||||||||||||||||||
| 366 | 0 | my $sql = 'UPDATE ' . $schema->model . ' SET ' . join(', ', @set) . ' ' . $where_sql; | |||||||||||||||||||||||||||||||||||||||||
| 367 | 0 | my $sth; | |||||||||||||||||||||||||||||||||||||||||
| 368 | 0 | eval { | |||||||||||||||||||||||||||||||||||||||||
| 369 | 0 | my $dbh = $self->rw_handle; | |||||||||||||||||||||||||||||||||||||||||
| 370 | 0 | $self->start_query($sql, \@bind); | |||||||||||||||||||||||||||||||||||||||||
| 371 | 0 | $sth = $dbh->prepare_cached($sql); | |||||||||||||||||||||||||||||||||||||||||
| 372 | 0 | $self->bind_params($schema, \@params, $sth); | |||||||||||||||||||||||||||||||||||||||||
| 373 | 0 | $sth->execute; | |||||||||||||||||||||||||||||||||||||||||
| 374 | 0 | $sth->finish; | |||||||||||||||||||||||||||||||||||||||||
| 375 | 0 | $self->end_query($sth); | |||||||||||||||||||||||||||||||||||||||||
| 376 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 377 | 0 | ▶ | if ($@) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 378 | 0 | $self->_stack_trace($sth, $sql, \@params, $@); | |||||||||||||||||||||||||||||||||||||||||
| 379 | } | ||||||||||||||||||||||||||||||||||||||||||
| 380 | |||||||||||||||||||||||||||||||||||||||||||
| 381 | 0 | ▶ | if (wantarray) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 382 | 0 | my @ret = $sth->rows; | |||||||||||||||||||||||||||||||||||||||||
| 383 | 0 | undef $sth; | |||||||||||||||||||||||||||||||||||||||||
| 384 | 0 | return @ret; | |||||||||||||||||||||||||||||||||||||||||
| 385 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 386 | 0 | my $ret = $sth->rows; | |||||||||||||||||||||||||||||||||||||||||
| 387 | 0 | undef $sth; | |||||||||||||||||||||||||||||||||||||||||
| 388 | 0 | return $ret; | |||||||||||||||||||||||||||||||||||||||||
| 389 | } | ||||||||||||||||||||||||||||||||||||||||||
| 390 | } | ||||||||||||||||||||||||||||||||||||||||||
| 391 | |||||||||||||||||||||||||||||||||||||||||||
| 392 | 0 | ▶ | sub update { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
update: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 393 | 0 | my($self, $schema, $old_key, $key, $old_columns, $columns, $changed_columns, %args) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 394 | |||||||||||||||||||||||||||||||||||||||||||
| 395 | 0 | my $stmt = Data::Model::SQL->new; | |||||||||||||||||||||||||||||||||||||||||
| 396 | 0 | $self->add_key_to_where($stmt, $schema->key, $old_key); | |||||||||||||||||||||||||||||||||||||||||
| 397 | |||||||||||||||||||||||||||||||||||||||||||
| 398 | 0 | my $where_sql = $stmt->as_sql_where; | |||||||||||||||||||||||||||||||||||||||||
| 399 | 0 | ▶ | return unless $where_sql; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 400 | |||||||||||||||||||||||||||||||||||||||||||
| 401 | 0 | return $self->_update($schema, $changed_columns, $columns, $where_sql, $stmt->bind, $stmt->bind_column); | |||||||||||||||||||||||||||||||||||||||||
| 402 | } | ||||||||||||||||||||||||||||||||||||||||||
| 403 | |||||||||||||||||||||||||||||||||||||||||||
| 404 | 0 | ▶ | sub update_direct { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
update_direct: not called CC=4 SCAR=30.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 405 | 0 | my($self, $schema, $key, $query, $columns, %args) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 406 | |||||||||||||||||||||||||||||||||||||||||||
| 407 | 0 | my $index_query = delete $query->{index}; | |||||||||||||||||||||||||||||||||||||||||
| 408 | 0 | my $stmt = Data::Model::SQL->new(%{ $query }); | |||||||||||||||||||||||||||||||||||||||||
| 409 | 0 | ▶ | $self->add_key_to_where($stmt, $schema->key, $key) if $key; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 410 | 0 | ▶ | $self->add_index_to_where($schema, $stmt, $index_query) if $index_query; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 411 | |||||||||||||||||||||||||||||||||||||||||||
| 412 | 0 | my $where_sql = $stmt->as_sql_where; | |||||||||||||||||||||||||||||||||||||||||
| 413 | 0 | ▶ | return unless $where_sql; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 414 | |||||||||||||||||||||||||||||||||||||||||||
| 415 | 0 | return $self->_update($schema, $columns, $columns, $where_sql, $stmt->bind, $stmt->bind_column); | |||||||||||||||||||||||||||||||||||||||||
| 416 | } | ||||||||||||||||||||||||||||||||||||||||||
| 417 | |||||||||||||||||||||||||||||||||||||||||||
| 418 | # delete | ||||||||||||||||||||||||||||||||||||||||||
| 419 | 0 | ▶ | sub delete { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
delete: not called CC=6 SCAR=37.4
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 420 | 0 | my($self, $schema, $key, $columns, %args) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 421 | |||||||||||||||||||||||||||||||||||||||||||
| 422 | 0 | $columns->{from} = [ $schema->model ]; | |||||||||||||||||||||||||||||||||||||||||
| 423 | 0 | my $index_query = delete $columns->{index}; | |||||||||||||||||||||||||||||||||||||||||
| 424 | 0 | my $stmt = Data::Model::SQL->new(%{ $columns }); | |||||||||||||||||||||||||||||||||||||||||
| 425 | 0 | ▶ | $self->add_key_to_where($stmt, $schema->key, $key) if $key; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 426 | 0 | ▶ | $self->add_index_to_where($schema, $stmt, $index_query) if $index_query; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 427 | |||||||||||||||||||||||||||||||||||||||||||
| 428 | # bind_params | ||||||||||||||||||||||||||||||||||||||||||
| 429 | 0 | my @params; | |||||||||||||||||||||||||||||||||||||||||
| 430 | 0 | for my $i (1..scalar(@{ $stmt->bind })) { | |||||||||||||||||||||||||||||||||||||||||
| 431 | 0 | push @params, [ $stmt->bind_column->[$i - 1], $stmt->bind->[$i - 1] ]; | |||||||||||||||||||||||||||||||||||||||||
| 432 | } | ||||||||||||||||||||||||||||||||||||||||||
| 433 | |||||||||||||||||||||||||||||||||||||||||||
| 434 | 0 | my $sql = "DELETE " . $stmt->as_sql; | |||||||||||||||||||||||||||||||||||||||||
| 435 | 0 | my $sth; | |||||||||||||||||||||||||||||||||||||||||
| 436 | 0 | eval { | |||||||||||||||||||||||||||||||||||||||||
| 437 | 0 | my $dbh = $self->rw_handle; | |||||||||||||||||||||||||||||||||||||||||
| 438 | 0 | $self->start_query($sql, $stmt->bind); | |||||||||||||||||||||||||||||||||||||||||
| 439 | 0 | $sth = $dbh->prepare_cached($sql); | |||||||||||||||||||||||||||||||||||||||||
| 440 | 0 | $self->bind_params($schema, \@params, $sth); | |||||||||||||||||||||||||||||||||||||||||
| 441 | 0 | $sth->execute; | |||||||||||||||||||||||||||||||||||||||||
| 442 | 0 | $sth->finish; | |||||||||||||||||||||||||||||||||||||||||
| 443 | 0 | $self->end_query($sth); | |||||||||||||||||||||||||||||||||||||||||
| 444 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 445 | 0 | ▶ | if ($@) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 446 | 0 | $self->_stack_trace($sth, $sql, $stmt->bind, $@); | |||||||||||||||||||||||||||||||||||||||||
| 447 | } | ||||||||||||||||||||||||||||||||||||||||||
| 448 | |||||||||||||||||||||||||||||||||||||||||||
| 449 | 0 | ▶ | if (wantarray) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 450 | 0 | my @ret = $sth->rows; | |||||||||||||||||||||||||||||||||||||||||
| 451 | 0 | undef $sth; | |||||||||||||||||||||||||||||||||||||||||
| 452 | 0 | return @ret; | |||||||||||||||||||||||||||||||||||||||||
| 453 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 454 | 0 | my $ret = $sth->rows; | |||||||||||||||||||||||||||||||||||||||||
| 455 | 0 | undef $sth; | |||||||||||||||||||||||||||||||||||||||||
| 456 | 0 | return $ret; | |||||||||||||||||||||||||||||||||||||||||
| 457 | } | ||||||||||||||||||||||||||||||||||||||||||
| 458 | } | ||||||||||||||||||||||||||||||||||||||||||
| 459 | |||||||||||||||||||||||||||||||||||||||||||
| 460 | # for schema | ||||||||||||||||||||||||||||||||||||||||||
| 461 | 7 | ▶ | sub _as_sql_hook { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_as_sql_hook: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 462 | 7 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 463 | 7 | $self->dbd->_as_sql_hook(@_); | |||||||||||||||||||||||||||||||||||||||||
| 464 | } | ||||||||||||||||||||||||||||||||||||||||||
| 465 | |||||||||||||||||||||||||||||||||||||||||||
| 466 | # stack trace | ||||||||||||||||||||||||||||||||||||||||||
| 467 | 0 | ▶ | sub _stack_trace { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_stack_trace: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||||||||||||||||
| 468 | 0 | my($self, $sth, $sql, $binds, $reason) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 469 | 0 | require Data::Dumper; | |||||||||||||||||||||||||||||||||||||||||
| 470 | |||||||||||||||||||||||||||||||||||||||||||
| 471 | 0 | ▶ | if ($sth) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 472 | # finalize sth handle | ||||||||||||||||||||||||||||||||||||||||||
| 473 | 0 | $sth->finish; | |||||||||||||||||||||||||||||||||||||||||
| 474 | 0 | $self->end_query($sth); | |||||||||||||||||||||||||||||||||||||||||
| 475 | } | ||||||||||||||||||||||||||||||||||||||||||
| 476 | |||||||||||||||||||||||||||||||||||||||||||
| 477 | 0 | $sql =~ s/\n/\n /gm; | |||||||||||||||||||||||||||||||||||||||||
| 478 | 0 | Carp::croak sprintf <<"TRACE", $reason, $sql, Data::Dumper::Dumper($binds); | |||||||||||||||||||||||||||||||||||||||||
| 479 | **** { Data::Model::Driver::DBI 's Exception **** | ||||||||||||||||||||||||||||||||||||||||||
| 480 | Reason : %s | ||||||||||||||||||||||||||||||||||||||||||
| 481 | SQL : %s | ||||||||||||||||||||||||||||||||||||||||||
| 482 | **** BINDS DUMP **** | ||||||||||||||||||||||||||||||||||||||||||
| 483 | %s | ||||||||||||||||||||||||||||||||||||||||||
| 484 | **** Data::Model::Driver::DBI 's Exception } **** | ||||||||||||||||||||||||||||||||||||||||||
| 485 | TRACE | ||||||||||||||||||||||||||||||||||||||||||
| 486 | } | ||||||||||||||||||||||||||||||||||||||||||
| 487 | |||||||||||||||||||||||||||||||||||||||||||
| 488 | # profile | ||||||||||||||||||||||||||||||||||||||||||
| 489 | 5 | ▶ | sub start_query {} | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
start_query: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 490 | 5 | ▶ | sub end_query {} | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
end_query: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 491 | |||||||||||||||||||||||||||||||||||||||||||
| 492 | 3 | ▶ | sub DESTROY { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
DESTROY: called CC=2 SCAR=7.2
| |||||||||||||||||||||||||||||||||||||||||||
| 493 | 3 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 494 | 3 | ▶ | return unless $self->{__dbh_init_by_driver}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 495 | |||||||||||||||||||||||||||||||||||||||||||
| 496 | # if (my $dbh = $self->dbh) { | ||||||||||||||||||||||||||||||||||||||||||
| 497 | # $dbh->disconnect if $dbh; | ||||||||||||||||||||||||||||||||||||||||||
| 498 | # } | ||||||||||||||||||||||||||||||||||||||||||
| 499 | } | ||||||||||||||||||||||||||||||||||||||||||
| 500 | |||||||||||||||||||||||||||||||||||||||||||
| 501 | # for transactions | ||||||||||||||||||||||||||||||||||||||||||
| 502 | 0 | ▶ | sub txn_begin { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
txn_begin: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 503 | 0 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 504 | 0 | $self->{active_transaction} = 1; | |||||||||||||||||||||||||||||||||||||||||
| 505 | 0 | my $dbh = $self->rw_handle; | |||||||||||||||||||||||||||||||||||||||||
| 506 | 0 | ▶ | eval { $dbh->begin_work } or Carp::croak $@; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 507 | } | ||||||||||||||||||||||||||||||||||||||||||
| 508 | |||||||||||||||||||||||||||||||||||||||||||
| 509 | 0 | ▶ | sub txn_rollback { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
txn_rollback: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 510 | 0 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 511 | 0 | ▶ | return unless $self->{active_transaction}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 512 | 0 | my $dbh = $self->rw_handle; | |||||||||||||||||||||||||||||||||||||||||
| 513 | 0 | ▶ | eval { $dbh->rollback } or Carp::croak $@; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 514 | } | ||||||||||||||||||||||||||||||||||||||||||
| 515 | |||||||||||||||||||||||||||||||||||||||||||
| 516 | 0 | ▶ | sub txn_commit { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
txn_commit: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 517 | 0 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 518 | 0 | ▶ | return unless $self->{active_transaction}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 519 | 0 | my $dbh = $self->rw_handle; | |||||||||||||||||||||||||||||||||||||||||
| 520 | 0 | ▶ | eval { $dbh->commit } or Carp::croak $@; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 521 | } | ||||||||||||||||||||||||||||||||||||||||||
| 522 | |||||||||||||||||||||||||||||||||||||||||||
| 523 | 0 | ▶ | sub txn_end { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
txn_end: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 524 | 0 | $_[0]->{active_transaction} = 0; | |||||||||||||||||||||||||||||||||||||||||
| 525 | } | ||||||||||||||||||||||||||||||||||||||||||
| 526 | |||||||||||||||||||||||||||||||||||||||||||
| 527 | 3 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 528 | |||||||||||||||||||||||||||||||||||||||||||
| 529 | =head1 NAME | ||||||||||||||||||||||||||||||||||||||||||
| 530 | |||||||||||||||||||||||||||||||||||||||||||
| 531 | Data::Model::Driver::DBI - storage driver for DBI | ||||||||||||||||||||||||||||||||||||||||||
| 532 | |||||||||||||||||||||||||||||||||||||||||||
| 533 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||||||||||||||||
| 534 | |||||||||||||||||||||||||||||||||||||||||||
| 535 | package MyDB; | ||||||||||||||||||||||||||||||||||||||||||
| 536 | use base 'Data::Model'; | ||||||||||||||||||||||||||||||||||||||||||
| 537 | use Data::Model::Schema; | ||||||||||||||||||||||||||||||||||||||||||
| 538 | use Data::Model::Driver::DBI; | ||||||||||||||||||||||||||||||||||||||||||
| 539 | |||||||||||||||||||||||||||||||||||||||||||
| 540 | my $dbi_connect_options = {}; | ||||||||||||||||||||||||||||||||||||||||||
| 541 | my $driver = Data::Model::Driver::DBI->new( | ||||||||||||||||||||||||||||||||||||||||||
| 542 | dsn => 'dbi:mysql:host=localhost:database=test', | ||||||||||||||||||||||||||||||||||||||||||
| 543 | username => 'user', | ||||||||||||||||||||||||||||||||||||||||||
| 544 | password => 'password', | ||||||||||||||||||||||||||||||||||||||||||
| 545 | connect_options => $dbi_connect_options, | ||||||||||||||||||||||||||||||||||||||||||
| 546 | reuse_dbh => 1, # sharing dbh (experimental option) | ||||||||||||||||||||||||||||||||||||||||||
| 547 | # When you use by MySQL, please set up | ||||||||||||||||||||||||||||||||||||||||||
| 548 | # connect_options => { mysql_auto_reconnect => 1 }, | ||||||||||||||||||||||||||||||||||||||||||
| 549 | # simultaneously. but mysql_auto_reconnect is very unsettled. | ||||||||||||||||||||||||||||||||||||||||||
| 550 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 551 | |||||||||||||||||||||||||||||||||||||||||||
| 552 | base_driver $driver; | ||||||||||||||||||||||||||||||||||||||||||
| 553 | install_model model_name => schema { | ||||||||||||||||||||||||||||||||||||||||||
| 554 | .... | ||||||||||||||||||||||||||||||||||||||||||
| 555 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 556 | |||||||||||||||||||||||||||||||||||||||||||
| 557 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||||||||||||||||
| 558 | |||||||||||||||||||||||||||||||||||||||||||
| 559 | DBD that is working now is only mysql and SQLite. | ||||||||||||||||||||||||||||||||||||||||||
| 560 | |||||||||||||||||||||||||||||||||||||||||||
| 561 | =head1 SEE ALSO | ||||||||||||||||||||||||||||||||||||||||||
| 562 | |||||||||||||||||||||||||||||||||||||||||||
| 563 | L<DBI>, | ||||||||||||||||||||||||||||||||||||||||||
| 564 | L<Data::Model> | ||||||||||||||||||||||||||||||||||||||||||
| 565 | |||||||||||||||||||||||||||||||||||||||||||
| 566 | =head1 AUTHOR | ||||||||||||||||||||||||||||||||||||||||||
| 567 | |||||||||||||||||||||||||||||||||||||||||||
| 568 | Kazuhiro Osawa E<lt>yappo <at> shibuya <döt> plE<gt> | ||||||||||||||||||||||||||||||||||||||||||
| 569 | |||||||||||||||||||||||||||||||||||||||||||
| 570 | =head1 LICENSE | ||||||||||||||||||||||||||||||||||||||||||
| 571 | |||||||||||||||||||||||||||||||||||||||||||
| 572 | This library is free software; you can redistribute it and/or modify | ||||||||||||||||||||||||||||||||||||||||||
| 573 | it under the same terms as Perl itself. | ||||||||||||||||||||||||||||||||||||||||||
| 574 | |||||||||||||||||||||||||||||||||||||||||||
| 575 | =cut | ||||||||||||||||||||||||||||||||||||||||||