blib/lib/DBIx/Sunny.pm

filter: Statementstmt 24.5% 28 / 114 Branchbran 0.0% 0 / 30 Conditioncond 0.0% 0 / 27 MC/DCmcdc 0.0% 0 / 16 Subroutinesub 36.3% 8 / 22 Podpod 100% 1 / 1 totaltotal 17.6% 37 / 210 CC 25 SCAR 32.6
CC
25
Cov
16%
connect
72.0
connected
56.0
__set_comment
20.0
CRAP 26.1
« blib/lib/DBIx/Sunny/Schema.pm ↑ Summary blib/lib/DBIx/Sunny/Util.pm »
1 package DBIx::Sunny;
2  
3 10 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 10 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 10 ▶use 5.008005;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 10 ▶use DBI 1.615;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 10 our $VERSION = '0.9991';
9 10 our $SKIP_CALLER_REGEX = qr/^(:?DBIx?|DBD|Try::Tiny|Context::Preserve)\b/;
10  
11 10 ▶use parent qw/DBI/;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 sub connect {
14 0 ▶ my $class = shift;
Subroutine connect: not called CC=8 SCAR=42.8
Pod documented
15 0 my ($dsn, $user, $pass, $attr) = @_;
16 0 $attr->{RaiseError} = 1;
17 0 $attr->{PrintError} = 0;
18 0 $attr->{ShowErrorStatement} = 1;
19 0 $attr->{AutoInactiveDestroy} = 1;
20 0 ▶ if ($dsn =~ /^(?i:dbi):SQLite:/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dsn =~ /^(?i:dbi):SQLite:/) 0 0 0
21 0 $attr->{sqlite_use_immediate_transaction} = 1;
22 0 ▶ $attr->{sqlite_unicode} = 1 unless exists $attr->{sqlite_unicode};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $attr->{"sqlite_unicode"} 0 0 0
23 }
24 0 ▶ if ($dsn =~ /^(?i:dbi):mysql:/ && ! exists $attr->{mysql_enable_utf8} && ! exists $attr->{mysql_enable_utf8mb4} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dsn =~ /^(?i:dbi):mysql:/ and not exists $attr->{"mysql_enable_utf8"} and not exists $attr->{"mysql_enable_utf8mb4"}) 0 0 0
Condition0%0/6
$dsn =~ /^(?i:dbi):mysql:/ and not exists $attr->{"mysql_enable_utf8"} and not exists $attr->{"mysql_enable_utf8mb4"}
!ll&&!rl&&r
000
$dsn =~ /^(?i:dbi):mysql:/ and not exists $attr->{"mysql_enable_utf8"}
!ll&&!rl&&r
000
MC/DC0%0/3
$dsn =~ /^(?i:dbi):mysql:/ and not exists $attr->{"mysql_enable_utf8"} and not exists $attr->{"mysql_enable_utf8mb4"}
$dsn =~ /^(?i:dbi):mysql:/ not exists $attr->{"mysql_enable_utf8"} not exists $attr->{"mysql_enable_utf8mb4"}
Truth table0%0/4
A and not(B) and not(C)
A= $dsn =~ /^(?i:dbi):mysql:/B= not exists $attr->{"mysql_enable_utf8"}C= not exists $attr->{"mysql_enable_utf8mb4"}
ABCresult
0XX0
10X0
1100
1111
25 0 $attr->{mysql_enable_utf8} = 1;
26 }
27 0 ▶ if ($dsn =~ /^(?i:dbi):Pg:/ && ! exists $attr->{pg_enable_utf8}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dsn =~ /^(?i:dbi):Pg:/ and not exists $attr->{"pg_enable_utf8"}) 0 0 0
Condition0%0/3
$dsn =~ /^(?i:dbi):Pg:/ and not exists $attr->{"pg_enable_utf8"}
!ll&&!rl&&r
000
MC/DC0%0/2
$dsn =~ /^(?i:dbi):Pg:/ and not exists $attr->{"pg_enable_utf8"}
$dsn =~ /^(?i:dbi):Pg:/ not exists $attr->{"pg_enable_utf8"}
Truth table0%0/3
A and not(B)
A= $dsn =~ /^(?i:dbi):Pg:/B= not exists $attr->{"pg_enable_utf8"}
ABresult
0X0
100
111
28 0 $attr->{pg_enable_utf8} = 1;
29 }
30 0 $class->SUPER::connect($dsn, $user, $pass, $attr);
31 }
32  
33 package DBIx::Sunny::db;
34 10 our @ISA = qw(DBI::db);
35  
36 10 ▶use DBIx::Sunny::Util qw/bind_and_execute expand_placeholder/;
Subroutine BEGIN: called CC=1 SCAR=0.0
37 10 ▶use DBIx::TransactionManager 0.13;
Subroutine BEGIN: called CC=1 SCAR=0.0
38 10 ▶use Scalar::Util qw/weaken/;
Subroutine BEGIN: called CC=1 SCAR=0.0
39  
40 sub connected {
41 0 ▶ my $dbh = shift;
Subroutine connected: not called CC=7 SCAR=40.3
42 0 my ($dsn, $user, $pass, $attr) = @_;
43 0 $dbh->{RaiseError} = 1;
44 0 $dbh->{PrintError} = 0;
45 0 $dbh->{ShowErrorStatement} = 1;
46 0 $dbh->{AutoInactiveDestroy} = 1;
47 0 ▶ if ($dsn =~ /^dbi:SQLite:/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dsn =~ /^dbi:SQLite:/) 0 0 0
48 0 $dbh->{sqlite_use_immediate_transaction} = 1;
49 0 ▶ $dbh->{sqlite_unicode} = 1 unless exists $attr->{sqlite_unicode};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $attr->{"sqlite_unicode"} 0 0 0
50  
51 0 $dbh->do("PRAGMA journal_mode = WAL");
52 0 $dbh->do("PRAGMA synchronous = NORMAL");
53  
54 }
55 0 ▶ if ($dsn =~ /^dbi:mysql:/ && ! exists $attr->{mysql_enable_utf8} && ! exists $attr->{mysql_enable_utf8mb4} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dsn =~ /^dbi:mysql:/ and not exists $attr->{"mysql_enable_utf8"} and not exists $attr->{"mysql_enable_utf8mb4"}) 0 0 0
Condition0%0/6
$dsn =~ /^dbi:mysql:/ and not exists $attr->{"mysql_enable_utf8"} and not exists $attr->{"mysql_enable_utf8mb4"}
!ll&&!rl&&r
000
$dsn =~ /^dbi:mysql:/ and not exists $attr->{"mysql_enable_utf8"}
!ll&&!rl&&r
000
MC/DC0%0/3
$dsn =~ /^dbi:mysql:/ and not exists $attr->{"mysql_enable_utf8"} and not exists $attr->{"mysql_enable_utf8mb4"}
$dsn =~ /^dbi:mysql:/ not exists $attr->{"mysql_enable_utf8"} not exists $attr->{"mysql_enable_utf8mb4"}
Truth table0%0/4
A and not(B) and not(C)
A= $dsn =~ /^dbi:mysql:/B= not exists $attr->{"mysql_enable_utf8"}C= not exists $attr->{"mysql_enable_utf8mb4"}
ABCresult
0XX0
10X0
1100
1111
56 0 $dbh->{mysql_enable_utf8} = 1;
57 0 $dbh->do("SET NAMES utf8");
58 }
59 0 ▶ if ($dsn =~ /^dbi:mysql:/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dsn =~ /^dbi:mysql:/) 0 0 0
60 0 $dbh->{mysql_auto_reconnect} = 0;
61 }
62 0 $dbh->{private_connect_info} = [@_];
63 0 $dbh->SUPER::connected(@_);
64 }
65  
66 0 ▶sub connect_info { $_[0]->{private_connect_info} }
Subroutine connect_info: not called CC=1 SCAR=6.9
67  
68 sub txn_scope {
69 0 ▶ my $self = shift;
Subroutine txn_scope: not called CC=2 SCAR=17.9
70 0 ▶ if ( ! $self->{private_txt_manager} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"private_txt_manager"}) 0 0 0
71 0 $self->{private_txt_manager} = DBIx::TransactionManager->new($self);
72 0 weaken($self->{private_txt_manager}->{dbh});
73 }
74 $self->{private_txt_manager}->txn_scope(
75 0 caller => [caller(0)]
76 );
77 }
78  
79 sub __set_comment {
80 0 ▶ my $self = shift;
Subroutine __set_comment: not called CC=4 SCAR=30.0
81 0 my $query = shift;
82  
83 0 my $trace;
84 0 my $i = 0;
85 0 ▶ while ( my @caller = caller($i) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@caller) = caller $i) 0 0 0
86 0 my $file = $caller[1];
87 0 $file =~ s!\*/!*\//!g;
88 0 $trace = "/* $file line $caller[2] */";
89 0 ▶ last if $caller[0] ne ref($self) && $caller[0] !~ $SKIP_CALLER_REGEX;
Branch: 1 branch
BranchTrueFalseTotal
if $caller[0] ne ref $self and not $caller[0] =~ /$DBIx::Sunny::SKIP_CALLER_REGEX/ 0 0 0
Condition0%0/3
$caller[0] ne ref $self and not $caller[0] =~ /$DBIx::Sunny::SKIP_CALLER_REGEX/
!ll&&!rl&&r
000
MC/DC0%0/2
$caller[0] ne ref $self and not $caller[0] =~ /$DBIx::Sunny::SKIP_CALLER_REGEX/
$caller[0] ne ref $self not $caller[0] =~ /$DBIx::Sunny::SKIP_CALLER_REGEX/
Truth table0%0/3
A and not(B)
A= $caller[0] ne ref $selfB= not $caller[0] =~ /$DBIx::Sunny::SKIP_CALLER_REGEX/
ABresult
0X0
100
111
90 0 $i++;
91 }
92 0 $query =~ s! ! $trace !;
93 0 $query;
94 }
95  
96 sub prepare {
97 0 ▶ my $self = shift;
Subroutine prepare: not called CC=1 SCAR=6.9
98 0 my $query = shift;
99 0 $self->SUPER::prepare($self->__set_comment($query), @_);
100 }
101  
102 sub do {
103 0 ▶ my $self = shift;
Subroutine do: not called CC=1 SCAR=6.9
104 0 my ($query, $attr, @bind) = @_;
105 0 $self->SUPER::do($self->__set_comment($query), $attr, @bind);
106 }
107  
108 sub fill_arrayref {
109 0 ▶ my $self = shift;
Subroutine fill_arrayref: not called CC=1 SCAR=6.9
110 0 return expand_placeholder(@_);
111 }
112  
113 sub __prepare_and_execute {
114 0 ▶ my $self = shift;
Subroutine __prepare_and_execute: not called CC=1 SCAR=6.9
115 0 my ($query, @bind) = expand_placeholder(@_);
116 0 my $sth = $self->prepare($query);
117 0 my $ret = bind_and_execute($sth, @bind);
118 0 return ($sth, $ret);
119 }
120  
121 sub select_one {
122 0 ▶ my $self = shift;
Subroutine select_one: not called CC=3 SCAR=24.8
123 0 my ($sth, $ret) = $self->__prepare_and_execute(@_);
124 0 ▶ my $row = $ret && $sth->fetchrow_arrayref;
Condition0%0/3
$ret && $sth->fetchrow_arrayref
!ll&&!rl&&r
000
MC/DC0%0/2
$ret && $sth->fetchrow_arrayref
$ret $sth->fetchrow_arrayref
Truth table0%0/3
A && B
A= $retB= $sth->fetchrow_arrayref
ABresult
0X0
100
111
125 0 ▶ return unless $row;
Branch: 1 branch
BranchTrueFalseTotal
unless $row 0 0 0
126 0 return $row->[0];
127 }
128  
129 sub select_row {
130 0 ▶ my $self = shift;
Subroutine select_row: not called CC=3 SCAR=24.8
131 0 my ($sth, $ret) = $self->__prepare_and_execute(@_);
132 0 ▶ my $row = $ret && $sth->fetchrow_hashref;
Condition0%0/3
$ret && $sth->fetchrow_hashref
!ll&&!rl&&r
000
MC/DC0%0/2
$ret && $sth->fetchrow_hashref
$ret $sth->fetchrow_hashref
Truth table0%0/3
A && B
A= $retB= $sth->fetchrow_hashref
ABresult
0X0
100
111
133 0 ▶ return unless $row;
Branch: 1 branch
BranchTrueFalseTotal
unless $row 0 0 0
134 0 return $row;
135 }
136  
137 sub select_all {
138 0 ▶ my $self = shift;
Subroutine select_all: not called CC=2 SCAR=17.9
139 0 my ($sth, $ret) = $self->__prepare_and_execute(@_);
140 0 ▶ my $rows = $ret && $sth->fetchall_arrayref({});
Condition0%0/3
$ret && $sth->fetchall_arrayref({})
!ll&&!rl&&r
000
MC/DC0%0/2
$ret && $sth->fetchall_arrayref({})
$ret $sth->fetchall_arrayref({})
Truth table0%0/3
A && B
A= $retB= $sth->fetchall_arrayref({})
ABresult
0X0
100
111
141 0 return $rows;
142 }
143  
144 sub query {
145 0 ▶ my $self = shift;
Subroutine query: not called CC=1 SCAR=6.9
146 0 (undef, my $ret) = $self->__prepare_and_execute(@_);
147 0 return $ret;
148 }
149  
150 sub last_insert_id {
151 0 ▶ my $self = shift;
Subroutine last_insert_id: not called CC=3 SCAR=24.8
152 0 my $dsn = $self->connect_info->[0];
153 0 ▶ if ($dsn =~ /^(?i:dbi):SQLite:/) {
Branch: 2 branches
BranchTrueFalseTotal
if ($dsn =~ /^(?i:dbi):SQLite:/) { } 0 0 0
elsif ($dsn =~ /^(?i:dbi):mysql:/) { } 0 0 0
154 0 return $self->func('last_insert_rowid');
155 }
156 elsif ( $dsn =~ /^(?i:dbi):mysql:/) {
157 0 return $self->{mysql_insertid};
158 }
159 0 $self->SUPER::last_insert_id(@_);
160 }
161  
162 package DBIx::Sunny::st; # statement handler
163 10 our @ISA = qw(DBI::st);
164  
165 10 1;
166  
167 __END__
168  
169 =encoding utf8
170  
171 =head1 NAME
172  
173 DBIx::Sunny - Simple DBI wrapper
174  
175 =head1 SYNOPSIS
176  
177 use DBIx::Sunny;
178  
179 my $dbh = DBIx::Sunny->connect(...);
180  
181 # or
182  
183 use DBI;
184  
185 my $dbh = DBI->connect(.., {
186 RootClass => 'DBIx::Sunny',
187 PrintError => 0,
188 RaiseError => 1,
189 });
190  
191 =head1 DESCRIPTION
192  
193 DBIx::Sunny is a simple DBI wrapper. It provides better usability for you. This module based on Amon2::DBI.
194 DBIx::Sunny supports only SQLite and MySQL.
195  
196 =head1 FEATURES
197  
198 =over 4
199  
200 =item Set AutoInactiveDestroy to true.
201  
202 DBIx::Sunny sets AutoInactiveDestroy as true.
203  
204 =item [SQLite/MySQL/Pg] Auto encode/decode UTF-8
205  
206 DBIx::Sunny sets sqlite_unicode, mysql_enable_utf8 and pg_enable_utf8 automatically.
207  
208 =item [SQLite] Performance tuning
209  
210 DBIx::Sunny sets sqlite_use_immediate_transaction to true, and executes these PRAGMA statements
211  
212 PRAGMA journal_mode = WAL
213 PRAGMA synchronous = NORMAL
214  
215 =item Nested transaction management.
216  
217 DBIx::Sunny supports nested transaction management based on RAII like DBIx::Class or DBIx::Skinny. It uses L<DBIx::TransactionManager> internally.
218  
219 =item Error Handling
220  
221 DBIx::Sunny sets RaiseError and ShowErrorStatement as true. DBIx::Sunny raises exception and shows current statement if your $dbh occurred exception.
222  
223 =item SQL comment
224  
225 DBIx::Sunny adds file name and line number as SQL comment that invokes SQL statement.
226  
227 =item Easy access to last_insert_id
228  
229 DBIx::Sunny's last_insert_id needs no arguments. It's shortcut for mysql_insertid or last_insert_rowid.
230  
231 =item Auto expanding arrayref bind parameters
232  
233 select_(one|row|all) and query methods support auto-expanding arrayref bind parameters.
234  
235 $dbh->select_all('SELECT * FROM id IN (?)', [1 2 3])
236 #SQL: 'SELECT * FROM id IN (?,?,?)'
237 #@BIND: (1, 2, 3)
238  
239 =item Named placeholder
240  
241 select_(one|row|all) and query methods support named placeholder.
242  
243 $dbh->select_all('SELECT * FROM users WHERE id IN (:ids) AND status = :status', {
244 ids => [1,2,3],
245 status => 'active',
246 });
247 #SQL: 'SELECT * FROM users WHERE id IN (?,?,?) AND status = ?'
248 #@BIND: (1, 2, 3, 'active')
249  
250 =item Typed bind parameters
251  
252 DBIx::Sunny allows you to specify data types of bind parameters. If a bind parameter is L<SQL::Maker::SQLType> object, its value is passed as its type, otherwise it is passed as default type (VARCHAR).
253  
254 use SQL::Maker::SQLType qw/sql_type/;
255 use DBI qw/:sql_types/
256  
257 $dbh->query(
258 'INSERT INTO bin_table (bin_col) VALUES (?)',
259 sql_type(\"\xDE\xAD\xBE\xEF", SQL_BINARY)),
260 );
261  
262 =back
263  
264 =head1 ADDITIONAL METHODS
265  
266 =over 4
267  
268 =item C<< $col = $dbh->select_one($query, @bind); >>
269  
270 Shortcut for prepare, execute and fetchrow_arrayref->[0]
271  
272 =item C<< $row = $dbh->select_row($query, @bind); >>
273  
274 Shortcut for prepare, execute and fetchrow_hashref
275  
276 =item C<< $rows = $dbh->select_all($query, @bind); >>
277  
278 Shortcut for prepare, execute and C<< selectall_arrayref(.., { Slice => {} }, ..) >>
279  
280 =item C<< $dbh->query($query, @bind); >>
281  
282 Shortcut for prepare, execute.
283  
284 =back
285  
286 =head1 AUTHOR
287  
288 Masahiro Nagano E<lt>kazeburo KZBRKZBR@ gmail.comE<gt>
289  
290 =head1 SEE ALSO
291  
292 L<DBI>, L<Amon2::DBI>
293  
294 =head1 LICENSE
295  
296 Copyright (C) Masahiro Nagano
297  
298 This library is free software; you can redistribute it and/or modify
299 it under the same terms as Perl itself.
300  
301 =cut
« blib/lib/DBIx/Sunny/Schema.pm ↑ Summary blib/lib/DBIx/Sunny/Util.pm »