| 1 | package Data::Mapper::Adapter::DBI; | ||||||||||||||||||||||||||||
| 2 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 3 | 1 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 1 | ▶ | use parent qw(Data::Mapper::Adapter); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | 1 | ▶ | use Carp (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 1 | ▶ | use Data::Dumper (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | 1 | ▶ | use SQL::Maker; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 1 | ▶ | use DBIx::Inspector; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | 1 | ▶ | use Data::Mapper::Schema; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | 0 | ▶ | sub create { | ||||||||||||||||||||||||||
|
Subroutine
create: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||||||||||||||||||
| 15 | 0 | my ($self, $table, $values) = @_; | |||||||||||||||||||||||||||
| 16 | 0 | my ($sql, @binds) = $self->sql->insert($table, $values); | |||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | 0 | $self->execute($sql, @binds); | |||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | 0 | my $schema = $self->schemata->{$table}; | |||||||||||||||||||||||||||
| 21 | 0 | my $primary_keys = $schema->primary_keys; | |||||||||||||||||||||||||||
| 22 | 0 | my $key = $primary_keys->[0]; | |||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | 0 | ▶ | if (scalar @$primary_keys == 1 && !defined $values->{$key}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
scalar @$primary_keys == 1 and not defined $values->{$key}
MC/DC0%0/2
scalar @$primary_keys == 1 and not defined $values->{$key}
scalar @$primary_keys == 1
not defined $values->{$key}
Truth table0%0/3
A and not(B)
A= scalar @$primary_keys == 1B= not defined $values->{$key}
| |||||||||||||||||||||||||||||
| 25 | 0 | $values->{$key} = $self->last_insert_id($table); | |||||||||||||||||||||||||||
| 26 | } | ||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | 0 | $values; | |||||||||||||||||||||||||||
| 29 | } | ||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | 0 | ▶ | sub find { | ||||||||||||||||||||||||||
|
Subroutine
find: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 32 | 0 | my ($self, $table, $where, $options) = @_; | |||||||||||||||||||||||||||
| 33 | 0 | my ($sql, @binds) = $self->select($table, $where, $options); | |||||||||||||||||||||||||||
| 34 | 0 | my $sth = $self->execute($sql, @binds); | |||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | 0 | $sth->fetchrow_hashref; | |||||||||||||||||||||||||||
| 37 | } | ||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | 0 | ▶ | sub search { | ||||||||||||||||||||||||||
|
Subroutine
search: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 40 | 0 | my ($self, $table, $where, $options) = @_; | |||||||||||||||||||||||||||
| 41 | 0 | my ($sql, @binds) = $self->select($table, $where, $options); | |||||||||||||||||||||||||||
| 42 | 0 | my $sth = $self->execute($sql, @binds); | |||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | 0 | my @result; | |||||||||||||||||||||||||||
| 45 | 0 | ▶ | while (my $row = $sth->fetchrow_hashref) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 46 | 0 | push @result, $row; | |||||||||||||||||||||||||||
| 47 | } | ||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | 0 | \@result; | |||||||||||||||||||||||||||
| 50 | } | ||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | 0 | ▶ | sub update { | ||||||||||||||||||||||||||
|
Subroutine
update: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 53 | 0 | my ($self, $table, $set, $where) = @_; | |||||||||||||||||||||||||||
| 54 | 0 | my ($sql, @binds) = $self->sql->update($table, $set, $where); | |||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | 0 | $self->execute($sql, @binds); | |||||||||||||||||||||||||||
| 57 | } | ||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | 0 | ▶ | sub delete { | ||||||||||||||||||||||||||
|
Subroutine
delete: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 60 | 0 | my ($self, $table, $where) = @_; | |||||||||||||||||||||||||||
| 61 | 0 | my ($sql, @binds) = $self->sql->delete($table, $where); | |||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | 0 | $self->execute($sql, @binds); | |||||||||||||||||||||||||||
| 64 | } | ||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | 0 | ▶ | sub schemata { | ||||||||||||||||||||||||||
|
Subroutine
schemata: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||||||||||||||||||
| 67 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | 0 | ▶ | if (!defined $self->{schemata}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 70 | 0 | $self->{schemata} = {}; | |||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | 0 | for my $table ($self->inspector->tables) { | |||||||||||||||||||||||||||
| 73 | $self->{schemata}{$table->name} = Data::Mapper::Schema->new({ | ||||||||||||||||||||||||||||
| 74 | table => $table->name, | ||||||||||||||||||||||||||||
| 75 | 0 | primary_keys => [ map { $_->name } $table->primary_key ], | |||||||||||||||||||||||||||
| 76 | 0 | columns => [ map { $_->name } $table->columns ], | |||||||||||||||||||||||||||
| 77 | }); | ||||||||||||||||||||||||||||
| 78 | } | ||||||||||||||||||||||||||||
| 79 | } | ||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | 0 | $self->{schemata}; | |||||||||||||||||||||||||||
| 82 | } | ||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | ### PRIVATE_METHODS ### | ||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | 0 | ▶ | sub sql { | ||||||||||||||||||||||||||
|
Subroutine
sql: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 87 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | 0 | ▶ | if (!defined $self->{sql}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 90 | 0 | $self->{sql} = SQL::Maker->new(driver => $self->driver->{Driver}{Name}); | |||||||||||||||||||||||||||
| 91 | } | ||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | 0 | $self->{sql}; | |||||||||||||||||||||||||||
| 94 | } | ||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | 0 | ▶ | sub inspector { | ||||||||||||||||||||||||||
|
Subroutine
inspector: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 97 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | 0 | ▶ | if (!defined $self->{inspector}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 100 | 0 | $self->{inspector} = DBIx::Inspector->new(dbh => $self->driver); | |||||||||||||||||||||||||||
| 101 | } | ||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||
| 103 | 0 | $self->{inspector}; | |||||||||||||||||||||||||||
| 104 | } | ||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | 0 | ▶ | sub select { | ||||||||||||||||||||||||||
|
Subroutine
select: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||||||||||||||||||
| 107 | 0 | my ($self, $table, $where, $options) = @_; | |||||||||||||||||||||||||||
| 108 | 0 | ▶ | my $fields = ($options || {})->{fields} || ['*']; | ||||||||||||||||||||||||||
|
Condition0%0/4
+($options || {})->{'fields'} || ['*']
$options || {}
MC/DC0%0/1
+($options || {})->{'fields'} || ['*']
$options
Truth table0%0/2
A
A= $options
| |||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | 0 | $self->sql->select($table, $fields, $where, $options); | |||||||||||||||||||||||||||
| 111 | } | ||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | 0 | ▶ | sub execute { | ||||||||||||||||||||||||||
|
Subroutine
execute: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 114 | 0 | my ($self, $sql, @binds) = @_; | |||||||||||||||||||||||||||
| 115 | 0 | my $sth; | |||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | 0 | eval { | |||||||||||||||||||||||||||
| 118 | 0 | $sth = $self->driver->prepare($sql); | |||||||||||||||||||||||||||
| 119 | 0 | $sth->execute(@binds); | |||||||||||||||||||||||||||
| 120 | }; | ||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | 0 | ▶ | if ($@) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 123 | 0 | $self->handle_error($sql, \@binds, $@); | |||||||||||||||||||||||||||
| 124 | } | ||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||
| 126 | 0 | $sth; | |||||||||||||||||||||||||||
| 127 | } | ||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||
| 129 | 0 | ▶ | sub handle_error { | ||||||||||||||||||||||||||
|
Subroutine
handle_error: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 130 | 0 | my ($self, $sql, $bind, $error) = @_; | |||||||||||||||||||||||||||
| 131 | 0 | $sql =~ s/\n/\n /gm; | |||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||
| 133 | 0 | Carp::croak sprintf <<"TRACE", $error, $sql, Data::Dumper::Dumper($bind); | |||||||||||||||||||||||||||
| 134 | @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ | ||||||||||||||||||||||||||||
| 135 | @@@@@ Data::Mapper Exception @@@@@ | ||||||||||||||||||||||||||||
| 136 | Reason : %s | ||||||||||||||||||||||||||||
| 137 | SQL : %s | ||||||||||||||||||||||||||||
| 138 | BIND : %s | ||||||||||||||||||||||||||||
| 139 | @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ | ||||||||||||||||||||||||||||
| 140 | TRACE | ||||||||||||||||||||||||||||
| 141 | } | ||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||
| 143 | 0 | ▶ | sub last_insert_id { | ||||||||||||||||||||||||||
|
Subroutine
last_insert_id: not called CC=4 SCAR=30.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 144 | 0 | my ($self, $table) = @_; | |||||||||||||||||||||||||||
| 145 | 0 | my $driver = $self->driver->{Driver}{Name}; | |||||||||||||||||||||||||||
| 146 | 0 | my $last_insert_id; | |||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||
| 148 | 0 | ▶ | if ($driver eq 'mysql') { | ||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||
| 149 | 0 | $last_insert_id = $self->dbh->{mysql_insertid}; | |||||||||||||||||||||||||||
| 150 | } | ||||||||||||||||||||||||||||
| 151 | elsif ($driver eq 'Pg') { | ||||||||||||||||||||||||||||
| 152 | 0 | $last_insert_id = $self->driver->last_insert_id( | |||||||||||||||||||||||||||
| 153 | undef, undef, undef, undef, { | ||||||||||||||||||||||||||||
| 154 | sequence => join('_', $table, 'id', 'seq') | ||||||||||||||||||||||||||||
| 155 | } | ||||||||||||||||||||||||||||
| 156 | ); | ||||||||||||||||||||||||||||
| 157 | } | ||||||||||||||||||||||||||||
| 158 | elsif ($driver eq 'SQLite') { | ||||||||||||||||||||||||||||
| 159 | 0 | $last_insert_id = $self->driver->func('last_insert_rowid'); | |||||||||||||||||||||||||||
| 160 | } | ||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||
| 162 | 0 | $last_insert_id; | |||||||||||||||||||||||||||
| 163 | } | ||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||
| 165 | 0 | ▶ | sub check_table { | ||||||||||||||||||||||||||
|
Subroutine
check_table: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 166 | 0 | my ($self, $table) = @_; | |||||||||||||||||||||||||||
| 167 | 0 | ▶ | $self->schemata->{$table} or Carp::croak("no such table: $table"); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 168 | } | ||||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||
| 170 | { | ||||||||||||||||||||||||||||
| 171 | 1 | ▶ | no strict 'refs'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 172 | 1 | ▶ | no warnings 'redefine'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||
| 174 | 1 | for my $method (qw(create find search update delete)) { | |||||||||||||||||||||||||||
| 175 | 5 | my $original = \&$method; | |||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||
| 177 | 5 | ▶ | *{__PACKAGE__."\::$method"} = sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=3.5
| |||||||||||||||||||||||||||||
| 178 | 0 | my ($self, $table) = @_; | |||||||||||||||||||||||||||
| 179 | 0 | $self->check_table($table); | |||||||||||||||||||||||||||
| 180 | 0 | $original->(@_); | |||||||||||||||||||||||||||
| 181 | 5 | }; | |||||||||||||||||||||||||||
| 182 | } | ||||||||||||||||||||||||||||
| 183 | } | ||||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||
| 185 | 1 | !!1; | |||||||||||||||||||||||||||