DBIx/Otogiri.pm

filter: Statementstmt 98.6% 144 / 146 Branchbran 88.0% 44 / 50 Conditioncond 52.1% 12 / 23 MC/DCmcdc 33.3% 4 / 12 Subroutinesub 100% 30 / 30 Podpod 93.3% 14 / 15 totaltotal 89.8% 248 / 276 CC 36 SCAR 12.8
CC
36
Cov
91%
DESTROY
8.1
_assert_valid_column_keys
6.0
last_insert_id
6.0
CRAP 3.6
↑ Summary DBIx/Otogiri/Iterator.pm »
1 package DBIx::Otogiri;
2 15 ▶use 5.008005;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 15 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 15 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 15 ▶use Class::Accessor::Lite (
Subroutine BEGIN: called CC=1 SCAR=0.0
7 15 ro => [qw/connect_info strict dburl/],
8 rw => [qw/maker owner_pid row_class_schema/],
9 new => 0,
10 15 );
11  
12 15 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
13 15 ▶use SQL::Maker;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 15 ▶use DBIx::Sunny;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 15 ▶use DBIx::Otogiri::Iterator;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 15 ▶use URI::db;
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 16 ▶sub new {
Subroutine new: called CC=3 SCAR=11.0
Pod documented
19 16 my ($class, %opts) = @_;
20 16 my $self = bless {%opts}, $class;
21 16 ▶ if ($self->{dburl}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"dburl"}) 1 15 16
22 1 my $dburl = URI::db->new($self->{dburl});
23 1 $self->{connect_info} = [$dburl->dbi_dsn, $dburl->user, $dburl->password];
24 }
25 ( $self->{dsn}{scheme},
26 $self->{dsn}{driver},
27 $self->{dsn}{attr_str},
28 $self->{dsn}{attributes},
29 $self->{dsn}{driver_dsn}
30 16 ) = DBI->parse_dsn($self->{connect_info}[0]);
31 16 ▶ my $strict = defined $self->strict ? $self->strict : 1;
Branch: 1 branch
BranchTrueFalseTotal
defined $self->strict ? : 1 15 16
32 16 $self->{dbh} = DBIx::Sunny->connect(@{$self->{connect_info}});
33 16 $self->{maker} = SQL::Maker->new(driver => $self->{dsn}{driver}, strict => $strict);
34 16 $self->owner_pid($$);
35 16 return $self;
36 }
37  
38 4 ▶sub row_class {
Subroutine row_class: called CC=2 SCAR=6.9
Pod documented
39 4 my ($self, $class_name) = @_;
40 4 ▶ if ($class_name) {
Branch: 1 branch
BranchTrueFalseTotal
if ($class_name) 2 2 4
41 2 $self->row_class_schema($class_name);
42 }
43 4 return $self;
44 }
45  
46 2 ▶sub no_row_class {
Subroutine no_row_class: called CC=1 SCAR=0.0
Pod documented
47 2 my ($self) = @_;
48 2 delete $self->{row_class_schema};
49 2 return $self;
50 }
51  
52 26 ▶sub _deflate_param {
Subroutine _deflate_param: called CC=2 SCAR=6.9
53 26 my ($self, $table, $param) = @_;
54 26 ▶ if ($self->{deflate}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"deflate"}) 6 20 26
55 6 $param = $self->{deflate}->({%$param}, $table, $self);
56 }
57 26 return $param;
58 }
59  
60 19 ▶sub _inflate_rows {
Subroutine _inflate_rows: called CC=3 SCAR=11.0
61 19 my ($self, $table, @rows) = @_;
62 19 ▶ @rows = $self->{inflate} ? map {$self->{inflate}->($_, $table, $self)} grep {defined $_} @rows : @rows;
Branch: 1 branch
BranchTrueFalseTotal
$self->{'inflate'} ? : 8 11 19
63 19 ▶ wantarray ? @rows : $rows[0];
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 15 4 19
64 }
65  
66 # SQL::Maker quotes identifiers (table/column names) but does not escape a
67 # quote character embedded in them, so an untrusted identifier can break out
68 # of the quoting and inject arbitrary SQL. Reject anything but a plain name.
69 164 ▶sub _assert_valid_identifier {
Subroutine _assert_valid_identifier: called CC=3 SCAR=11.1
70 164 my ($self, $name) = @_;
71 164 ▶ return unless defined $name;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $name 0 164 164
72 164 ▶ Carp::croak(qq{Invalid identifier: "$name"}) if $name !~ /\A[A-Za-z_][A-Za-z0-9_.]*\z/;
Branch: 1 branch
BranchTrueFalseTotal
if not $name =~ /\A[A-Za-z_][A-Za-z0-9_.]*\z/ 10 154 164
73 }
74  
75 68 ▶sub _assert_valid_column_keys {
Subroutine _assert_valid_column_keys: called CC=6 SCAR=17.9
76 68 my ($self, $param) = @_;
77 68 ▶ return unless defined $param;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $param 1 67 68
78 67 ▶ if (ref $param eq 'HASH') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $param eq 'HASH') { } 64 3 67
elsif (ref $param eq 'ARRAY') { } 2 1 3
79 64 $self->_assert_valid_identifier($_) for keys %$param;
80 }
81 elsif (ref $param eq 'ARRAY') {
82 2 $self->_assert_valid_identifier($param->[$_]) for grep {$_ % 2 == 0} 0..$#$param;
83 }
84 }
85  
86 14 ▶sub select {
Subroutine select: called CC=1 SCAR=0.0
Pod documented
87 14 my ($self, $table, $param, @opts) = @_;
88 14 $self->_assert_valid_identifier($table);
89 13 $self->_assert_valid_column_keys($param);
90 12 my ($sql, @binds) = $self->maker->select($table, ['*'], $param, @opts);
91 11 $self->search_by_sql($sql, \@binds, $table);
92 }
93  
94 15 *search = *select;
95  
96 13 ▶sub search_by_sql {
Subroutine search_by_sql: called CC=5 SCAR=16.2
Pod documented
97 13 my ($self, $sql, $binds_aref, $table) = @_;
98  
99 13 ▶ return DBIx::Otogiri::Iterator->new(
Branch: 1 branch
BranchTrueFalseTotal
unless wantarray 2 11 13
100 db => $self,
101 sql => $sql,
102 binds => $binds_aref,
103 table => $table,
104 ) unless wantarray;
105  
106 11 ▶ my @binds = @{$binds_aref || []};
Condition50%1/2
$binds_aref || []
l!l
110
MC/DC0%0/1
$binds_aref || []
$binds_aref
Truth table50%1/2
A
A= $binds_aref
Aresult
11
00
107 11 my $dbh = $self->dbh;
108 11 my $row_class = $self->row_class_schema;
109 11 ▶ my $rtn = $row_class ? $dbh->select_all_as($row_class, $sql, @binds) : $dbh->select_all($sql, @binds);
Branch: 1 branch
BranchTrueFalseTotal
$row_class ? : 1 10 11
110 11 ▶ $rtn ? $self->_inflate_rows($table, @$rtn) : ();
Branch: 1 branch
BranchTrueFalseTotal
$rtn ? : 11 0 11
111 }
112  
113 18 ▶sub single {
Subroutine single: called CC=3 SCAR=11.0
Pod documented
114 18 my ($self, $table, $param, @opts) = @_;
115 18 $self->_assert_valid_identifier($table);
116 17 $self->_assert_valid_column_keys($param);
117 17 my ($sql, @binds) = $self->maker->select($table, ['*'], $param, @opts);
118 17 my $dbh = $self->dbh;
119 17 my $row_class = $self->row_class_schema;
120 17 ▶ my $row = $row_class ? $dbh->select_row_as($row_class, $sql, @binds) : $dbh->select_row($sql, @binds);
Branch: 1 branch
BranchTrueFalseTotal
$row_class ? : 1 16 17
121 17 ▶ $self->{inflate} ? $self->_inflate_rows($table, $row) : $row;
Branch: 1 branch
BranchTrueFalseTotal
$self->{'inflate'} ? : 4 13 17
122 }
123  
124 15 *fetch = *single;
125  
126 24 ▶sub fast_insert {
Subroutine fast_insert: called CC=2 SCAR=6.9
Pod documented
127 24 my ($self, $table, $param, @opts) = @_;
128 24 $self->_assert_valid_identifier($table);
129 23 $self->_assert_valid_column_keys($param);
130 22 $param = $self->_deflate_param($table, $param);
131 22 my ($sql, @binds) = $self->maker->insert($table, $param, @opts);
132 22 $self->dbh->query($sql, @binds);
133  
134 21 ▶ if ( defined wantarray() ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined wantarray) 3 18 21
135 3 return $self->last_insert_id;
136 }
137 18 return;
138 }
139  
140 15 *insert = *fast_insert;
141  
142 5 ▶sub delete {
Subroutine delete: called CC=1 SCAR=0.0
Pod documented
143 5 my ($self, $table, $param, @opts) = @_;
144 5 $self->_assert_valid_identifier($table);
145 4 $self->_assert_valid_column_keys($param);
146 3 my ($sql, @binds) = $self->maker->delete($table, $param, @opts);
147 3 $self->dbh->query($sql, @binds);
148 }
149  
150 7 ▶sub update {
Subroutine update: called CC=3 SCAR=11.0
Pod documented
151 7 my ($self, $table, $param, @opts) = @_;
152 7 $self->_assert_valid_identifier($table);
153 6 $self->_assert_valid_column_keys($param);
154 5 ▶ $self->_assert_valid_column_keys($opts[0]) if @opts;
Branch: 1 branch
BranchTrueFalseTotal
if @opts 5 0 5
155 4 ▶ if (ref $param eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $param eq "HASH") 2 2 4
156 2 $param = [%$param];
157 }
158 4 $param = $self->_deflate_param($table, $param);
159 4 my ($sql, @binds) = $self->maker->update($table, $param, @opts);
160 4 $self->dbh->query($sql, @binds);
161 }
162  
163 13 ▶sub do {
Subroutine do: called CC=1 SCAR=0.0
Pod documented
164 13 my $self = shift;
165 13 $self->dbh->query(@_);
166 }
167  
168 4 ▶sub txn_scope {
Subroutine txn_scope: called CC=1 SCAR=0.0
Pod documented
169 4 my $self = shift;
170 4 $self->dbh->txn_scope;
171 }
172  
173 11 ▶sub last_insert_id {
Subroutine last_insert_id: called CC=4 SCAR=17.9
Pod documented
174 11 my ($self, $catalog, $schema, $table, $field, $attr_href) = @_;
175 11 my $driver_name = $self->{dsn}{driver};
176 11 ▶ if ($driver_name eq 'Pg' && !defined $table && !exists $attr_href->{sequence}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($driver_name eq "Pg" and not defined $table and not exists $attr_href->{"sequence"}) 0 11 11
Condition33%2/6
$driver_name eq "Pg" and not defined $table and not exists $attr_href->{"sequence"}
!ll&&!rl&&r
1100
$driver_name eq "Pg" and not defined $table
!ll&&!rl&&r
1100
MC/DC0%0/3
$driver_name eq "Pg" and not defined $table and not exists $attr_href->{"sequence"}
$driver_name eq "Pg" not defined $table not exists $attr_href->{"sequence"}
Truth table25%1/4
A and not(B) and not(C)
A= $driver_name eq "Pg"B= not defined $tableC= not exists $attr_href->{"sequence"}
ABCresult
0XX0
10X0
1100
1111
177 0 my @rows = $self->search_by_sql('SELECT LASTVAL() AS lastval');
178 0 return $rows[0]->{lastval};
179 }
180 11 return $self->{dbh}->last_insert_id($catalog, $schema, $table, $field, $attr_href);
181 }
182  
183 6 ▶sub reconnect {
Subroutine reconnect: called CC=1 SCAR=0.0
Pod documented
184 6 my ($self) = @_;
185  
186 6 $self->_in_transaction_check();
187  
188 5 $self->disconnect();
189  
190 5 my $dbh = $self->{dbh};
191 5 $self->{dbh} = $dbh->clone();
192 5 $self->owner_pid($$);
193 }
194  
195 23 ▶sub disconnect {
Subroutine disconnect: called CC=1 SCAR=0.0
Pod documented
196 23 my ($self) = @_;
197 23 $self->{dbh}->disconnect();
198 23 $self->owner_pid(undef);
199 }
200  
201 80 ▶sub dbh {
Subroutine dbh: called CC=5 SCAR=16.1
Pod undocumented
202 80 my ($self) = @_;
203 80 my $dbh = $self->{dbh};
204  
205 80 ▶ if ( !defined $self->owner_pid || $self->owner_pid != $$ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $self->owner_pid or $self->owner_pid != $$) 2 78 80
Condition100%3/3
not defined $self->owner_pid or $self->owner_pid != $$
l!l&&r!l&&!r
1178
MC/DC100%2/2
not defined $self->owner_pid or $self->owner_pid != $$
not defined $self->owner_pid $self->owner_pid != $$
Truth table100%3/3
not(A) or B
A= not defined $self->owner_pidB= $self->owner_pid != $$
ABresult
1X1
011
000
206 2 $self->reconnect;
207 }
208 80 ▶ if ( !$dbh->FETCH('Active') || !$dbh->ping ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($dbh->FETCH("Active") and $dbh->ping) 3 77 80
Condition100%3/3
$dbh->FETCH("Active") and $dbh->ping
!ll&&!rl&&r
2177
MC/DC100%2/2
$dbh->FETCH("Active") and $dbh->ping
$dbh->FETCH("Active") $dbh->ping
Truth table100%3/3
A and B
A= $dbh->FETCH("Active")B= $dbh->ping
ABresult
0X0
100
111
209 3 $self->reconnect;
210 }
211 79 return $self->{dbh};
212 }
213  
214 6 ▶sub _in_transaction_check {
Subroutine _in_transaction_check: called CC=3 SCAR=11.0
215 6 my ($self) = @_;
216  
217 6 ▶ return if ( !defined $self->{dbh}->{private_txt_manager} );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"dbh"}{"private_txt_manager"} 5 1 6
218  
219 1 ▶ if ( my $info = $self->{dbh}->{private_txt_manager}->in_transaction() ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $info = $self->{"dbh"}{"private_txt_manager"}->in_transaction) 1 0 1
220 1 my $caller = $info->{caller};
221 1 my $pid = $info->{pid};
222 1 Carp::confess("Detected transaction during a connect operation (last known transaction at $caller->[1] line $caller->[2], pid $pid). Refusing to proceed at");
223 }
224 }
225  
226 16 ▶sub DESTROY {
Subroutine DESTROY: called CC=5 SCAR=20.9
227 16 my ($self) = @_;
228
229 # Automatically call disconnect when the object is destroyed or program terminates
230 # Skip calling disconnect in forked processes (only call in the process that owns the connection)
231 16 ▶ if ($self->{dbh} &&
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"dbh"} and $self->{"dbh"}->FETCH("Active") and defined $self->owner_pid and $self->owner_pid == $$) 16 0 16
Condition33%3/9
$self->{"dbh"} and $self->{"dbh"}->FETCH("Active") and defined $self->owner_pid and $self->owner_pid == $$
!ll&&!rl&&r
0016
$self->{"dbh"} and $self->{"dbh"}->FETCH("Active") and defined $self->owner_pid
!ll&&!rl&&r
0016
$self->{"dbh"} and $self->{"dbh"}->FETCH("Active")
!ll&&!rl&&r
0016
MC/DC0%0/4
$self->{"dbh"} and $self->{"dbh"}->FETCH("Active") and defined $self->owner_pid and $self->owner_pid == $$
$self->{"dbh"} $self->{"dbh"}->FETCH("Active") defined $self->owner_pid $self->owner_pid == $$
Truth table20%1/5
A and B and C and D
A= $self->{"dbh"}B= $self->{"dbh"}->FETCH("Active")C= defined $self->owner_pidD= $self->owner_pid == $$
ABCDresult
0XXX0
10XX0
110X0
11100
11111
232 $self->{dbh}->FETCH('Active') &&
233 defined $self->owner_pid &&
234 $self->owner_pid == $$) {
235 16 $self->disconnect();
236 }
237 }
238  
239  
240 15 1;
241 __END__
↑ Summary DBIx/Otogiri/Iterator.pm »