blib/lib/Teng.pm

filter: Statementstmt 42.3% 162 / 383 Branchbran 26.1% 46 / 176 Conditioncond 20.0% 15 / 75 MC/DCmcdc 4.1% 2 / 48 Subroutinesub 50.8% 29 / 57 Podpod 96.9% 32 / 33 totaltotal 37.0% 286 / 772 CC 125 SCAR 32.3
CC
125
Cov
35%
bulk_insert
156.0
single_by_sql
42.0
new_row_from_hash
42.0
CRAP 25.2
« blib/lib/Teng/Row.pm ↑ Summary blib/lib/Teng/Plugin/BulkInsert.pm »
1 package Teng;
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 Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use Class::Load 0.06 ();
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 ▶use DBI 1.33;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 3 ▶use Scalar::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 3 ▶use SQL::Maker::SQLType qw(sql_type);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 3 ▶use Teng::Row;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 3 ▶use Teng::Iterator;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 3 ▶use Teng::Schema;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 3 ▶use DBIx::TransactionManager 1.06;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 3 ▶use Teng::QueryBuilder;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 use Class::Accessor::Lite 0.05
15 3 rw => [ qw(
16 connect_info
17 on_connect_do
18 schema
19 schema_class
20 suppress_row_objects
21 sql_builder
22 sql_comment
23 owner_pid
24 no_ping
25 fields_case
26 apply_sql_types
27 guess_sql_types
28 )]
29 3 ▶;
Subroutine BEGIN: called CC=1 SCAR=0.0
30  
31 3 our $VERSION = '0.32';
32  
33 sub load_plugin {
34 0 ▶ my ($class, $pkg, $opt) = @_;
Subroutine load_plugin: not called CC=7 SCAR=37.0
Pod documented
35 0 ▶ $pkg = $pkg =~ s/^\+// ? $pkg : "Teng::Plugin::$pkg";
Branch: 1 branch
BranchTrueFalseTotal
$pkg =~ s/^\+// ? : 0 0 0
36 0 Class::Load::load_class($pkg);
37  
38 0 ▶ $class = ref($class) if ref($class);
Branch: 1 branch
BranchTrueFalseTotal
if ref $class 0 0 0
39  
40 0 ▶ my $alias = delete $opt->{alias} || +{};
Condition0%0/2
delete $opt->{'alias'} || {}
l!l
00
MC/DC0%0/1
delete $opt->{'alias'} || {}
delete $opt->{'alias'}
Truth table0%0/2
A
A= delete $opt->{'alias'}
Aresult
11
00
41 {
42 3 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.2
43 0 for my $method ( @{"${pkg}::EXPORT"} ){
44 0 ▶ *{$class . '::' . ($alias->{$method} || $method)} = $pkg->can($method);
Condition0%0/3
$alias->{$method} || $method
l!l&&r!l&&!r
000
MC/DC0%0/2
$alias->{$method} || $method
$alias->{$method} $method
Truth table0%0/3
A || B
A= $alias->{$method}B= $method
ABresult
1X1
011
000
45 }
46 }
47  
48 0 ▶ $pkg->init($class, $opt) if $pkg->can('init');
Branch: 1 branch
BranchTrueFalseTotal
if $pkg->can("init") 0 0 0
49 }
50  
51 sub new {
52 5 ▶ my $class = shift;
Subroutine new: called CC=11 SCAR=28.0
Pod documented
53 5 ▶ my %args = @_ == 1 ? %{$_[0]} : @_;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 ? : 0 5 5
54 5 my $loader = delete $args{loader};
55  
56 5 ▶ if ( my $mode = delete $args{mode} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $mode = delete $args{"mode"}) 0 5 5
57 0 warn "IMPORTANT: 'mode' option is DEPRECATED AND *WILL* BE REMOVED. PLEASE USE 'no_ping' option.\n";
58 0 ▶ if ( !exists $args{no_ping} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $args{"no_ping"}) 0 0 0
59 0 ▶ $args{no_ping} = $mode eq 'ping' ? 0 : 1;
Branch: 1 branch
BranchTrueFalseTotal
$mode eq 'ping' ? : 0 0 0
60 }
61 }
62  
63 5 my $self = bless {
64 schema_class => "$class\::Schema",
65 owner_pid => $$,
66 no_ping => 0,
67 fields_case => 'NAME_lc',
68 boolean_value => {true => 1, false => 0},
69 %args,
70 }, $class;
71  
72 5 ▶ if (!$loader && ! $self->schema) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($loader or $self->schema) 1 4 5
Condition66%2/3
$loader or $self->schema
l!l&&r!l&&!r
401
MC/DC50%1/2
$loader or $self->schema
$loader $self->schema
Truth table66%2/3
A or B
A= $loaderB= $self->schema
ABresult
1X1
011
000
73 1 my $schema_class = $self->{schema_class};
74 1 Class::Load::load_class( $schema_class );
75 1 my $schema = $schema_class->instance;
76 1 ▶ if (! $schema) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($schema) 0 1 1
77 0 Carp::croak("schema object was not passed, and could not get schema instance from $schema_class");
78 }
79 1 $schema->namespace($class);
80 1 $self->schema( $schema );
81 }
82  
83 5 ▶ unless ($self->connect_info || $self->{dbh}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->connect_info or $self->{"dbh"}) 0 5 5
Condition66%2/3
$self->connect_info or $self->{"dbh"}
l!l&&r!l&&!r
230
MC/DC0%0/2
$self->connect_info or $self->{"dbh"}
$self->connect_info $self->{"dbh"}
Truth table66%2/3
A or B
A= $self->connect_infoB= $self->{"dbh"}
ABresult
1X1
011
000
84 0 Carp::croak("'dbh' or 'connect_info' is required.");
85 }
86  
87 5 ▶ if ( ! $self->{dbh} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->{'dbh'}) { } 2 3 5
88 2 $self->connect;
89 } else {
90 3 $self->_prepare_from_dbh;
91 }
92  
93 5 return $self;
94 }
95  
96 sub set_boolean_value {
97 0 ▶ my $self = shift;
Subroutine set_boolean_value: not called CC=2 SCAR=17.9
Pod documented
98 0 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
99 0 my ($true, $false) = @_;
100 0 $self->{boolean_value} = {true => $true, false => $false};
101 }
102 0 return $self->{boolean_value};
103 }
104  
105 sub mode {
106 0 ▶ my $self = shift;
Subroutine mode: not called CC=4 SCAR=30.0
Pod documented
107 0 warn "IMPORTANT: 'mode' option is DEPRECATED AND *WILL* BE REMOVED. PLEASE USE 'no_ping' option.\n";
108  
109 0 ▶ if ( @_ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
110 0 my $mode = shift;
111 0 ▶ if ( $mode eq 'ping' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mode eq 'ping') { } 0 0 0
112 0 $self->no_ping(0);
113 }
114 else {
115 0 $self->no_ping(1);
116 }
117 }
118  
119 0 ▶ return $self->no_ping ? 'no_ping' : 'ping';
Branch: 1 branch
BranchTrueFalseTotal
$self->no_ping ? : 0 0 0
120 }
121  
122 # forcefully connect
123 sub connect {
124 2 ▶ my ($self, @args) = @_;
Subroutine connect: called CC=5 SCAR=17.6
Pod documented
125  
126 2 $self->in_transaction_check;
127  
128 2 ▶ if (@args) {
Branch: 1 branch
BranchTrueFalseTotal
if (@args) 0 2 2
129 0 $self->connect_info( \@args );
130 }
131 2 my $connect_info = $self->connect_info;
132 $connect_info->[3] = {
133 # basic defaults
134 AutoCommit => 1,
135 PrintError => 0,
136 RaiseError => 1,
137 2 ▶ %{ $connect_info->[3] || {} },
Branch: 1 branch
BranchTrueFalseTotal
$connect_info->[3] or {} 2 0 2
138 };
139  
140 2 ▶ $self->{dbh} = eval { DBI->connect(@$connect_info) }
Branch: 1 branch
BranchTrueFalseTotal
$self->{"dbh"} = eval { do { "DBI"->connect(@$connect_info) } } or Carp::croak('Connection error: ' . ($@ || $DBI::errstr)) 0 2 2
Condition0%0/3
$@ || $DBI::errstr
l!l&&r!l&&!r
000
MC/DC0%0/2
$@ || $DBI::errstr
$@ $DBI::errstr
Truth table0%0/3
A || B
A= $@B= $DBI::errstr
ABresult
1X1
011
000
141 or Carp::croak("Connection error: " . ($@ || $DBI::errstr));
142 2 delete $self->{txn_manager};
143  
144 2 $self->owner_pid($$);
145  
146 2 $self->_on_connect_do;
147 2 $self->_prepare_from_dbh;
148 }
149  
150 sub _on_connect_do {
151 2 ▶ my $self = shift;
Subroutine _on_connect_do: called CC=6 SCAR=32.0
152  
153 2 ▶ if ( my $on_connect_do = $self->on_connect_do ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $on_connect_do = $self->on_connect_do) 0 2 2
154 0 ▶ if (not ref($on_connect_do)) {
Branch: 3 branches
BranchTrueFalseTotal
if (not ref $on_connect_do) { } 0 0 0
elsif (ref $on_connect_do eq 'CODE') { } 0 0 0
elsif (ref $on_connect_do eq 'ARRAY') { } 0 0 0
155 0 $self->do($on_connect_do);
156 } elsif (ref($on_connect_do) eq 'CODE') {
157 0 $on_connect_do->($self);
158 } elsif (ref($on_connect_do) eq 'ARRAY') {
159 0 $self->do($_) for @$on_connect_do;
160 } else {
161 0 Carp::croak('Invalid on_connect_do: '.ref($on_connect_do));
162 }
163 }
164 }
165  
166 sub reconnect {
167 0 ▶ my $self = shift;
Subroutine reconnect: not called CC=4 SCAR=30.0
Pod documented
168  
169 0 $self->in_transaction_check;
170  
171 0 my $dbh = $self->{dbh};
172  
173 0 $self->disconnect();
174  
175 0 ▶ if ( @_ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) { } 0 0 0
176 0 $self->connect(@_);
177 }
178 else {
179 # Why don't use $dbh->clone({InactiveDestroy => 0}) ?
180 # because, DBI v1.616 clone with \%attr has bug.
181 # my $dbh2 = $dbh->clone({});
182 # my $dbh3 = $dbh2->clone({});
183 # $dbh2 is ok, but $dbh3 is undef.
184 # ---
185 # Don't assign $self-{dbh} directry.
186 # Because if $self->{dbh} is undef then reconnect fail always.
187 # https://github.com/nekokak/p5-Teng/pull/98
188 0 ▶ my $new_dbh = eval { $dbh->clone }
Branch: 1 branch
BranchTrueFalseTotal
my $new_dbh = eval { do { $dbh->clone } } or Carp::croak('ReConnection error: ' . ($@ || $DBI::errstr)) 0 0 0
Condition0%0/3
$@ || $DBI::errstr
l!l&&r!l&&!r
000
MC/DC0%0/2
$@ || $DBI::errstr
$@ $DBI::errstr
Truth table0%0/3
A || B
A= $@B= $DBI::errstr
ABresult
1X1
011
000
189 or Carp::croak("ReConnection error: " . ($@ || $DBI::errstr));
190 0 $self->{dbh} = $new_dbh;
191 0 $self->{dbh}->{InactiveDestroy} = 0;
192  
193 0 $self->owner_pid($$);
194 0 $self->_on_connect_do;
195 0 $self->_prepare_from_dbh;
196 }
197 }
198  
199 sub disconnect {
200 0 ▶ my $self = shift;
Subroutine disconnect: not called CC=4 SCAR=30.0
Pod documented
201  
202 0 delete $self->{txn_manager};
203 0 ▶ if ( my $dbh = $self->{dbh} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $dbh = $self->{"dbh"}) 0 0 0
204 0 ▶ if ( $self->owner_pid && ($self->owner_pid != $$) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->owner_pid and $self->owner_pid != $$) { } 0 0 0
Condition0%0/3
$self->owner_pid and $self->owner_pid != $$
!ll&&!rl&&r
000
MC/DC0%0/2
$self->owner_pid and $self->owner_pid != $$
$self->owner_pid $self->owner_pid != $$
Truth table0%0/3
A and B
A= $self->owner_pidB= $self->owner_pid != $$
ABresult
0X0
100
111
205 0 $dbh->{InactiveDestroy} = 1;
206 }
207 else {
208 0 $dbh->disconnect;
209 }
210 }
211 0 $self->owner_pid(undef);
212 }
213  
214 sub _prepare_from_dbh {
215 5 ▶ my $self = shift;
Subroutine _prepare_from_dbh: called CC=5 SCAR=16.3
216  
217 5 $self->{driver_name} = $self->{dbh}->{Driver}->{Name};
218 5 my $builder = $self->{sql_builder};
219 5 ▶ if (! $builder ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($builder) 5 0 5
220 5 ▶ my $sql_builder_class = $self->{sql_builder_class} || 'Teng::QueryBuilder';
Condition50%1/2
$self->{'sql_builder_class'} || 'Teng::QueryBuilder'
l!l
05
MC/DC0%0/1
$self->{'sql_builder_class'} || 'Teng::QueryBuilder'
$self->{'sql_builder_class'}
Truth table50%1/2
A
A= $self->{'sql_builder_class'}
Aresult
11
00
221 $builder = $sql_builder_class->new(
222 driver => $self->{driver_name},
223 5 ▶ %{ $self->{sql_builder_args} || {} }
Branch: 1 branch
BranchTrueFalseTotal
$self->{"sql_builder_args"} or {} 5 0 5
224 );
225 5 $self->sql_builder( $builder );
226 }
227 5 $self->{dbh}->{FetchHashKeyName} = $self->{fields_case};
228  
229 5 ▶ $self->{schema}->prepare_from_dbh($self->{dbh}) if $self->{schema};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"schema"} 1 4 5
230 }
231  
232 sub _verify_pid {
233 16 ▶ my $self = shift;
Subroutine _verify_pid: called CC=7 SCAR=27.2
234  
235 16 ▶ if ( !$self->owner_pid || $self->owner_pid != $$ ) {
Branch: 2 branches
BranchTrueFalseTotal
if (not $self->owner_pid or $self->owner_pid != $$) { } 0 16 16
elsif (my $dbh = $self->{'dbh'}) { } 16 0 16
Condition33%1/3
not $self->owner_pid or $self->owner_pid != $$
l!l&&r!l&&!r
0016
MC/DC0%0/2
not $self->owner_pid or $self->owner_pid != $$
not $self->owner_pid $self->owner_pid != $$
Truth table33%1/3
not(A) or B
A= not $self->owner_pidB= $self->owner_pid != $$
ABresult
1X1
011
000
236 0 $self->reconnect;
237 }
238 elsif ( my $dbh = $self->{dbh} ) {
239 16 ▶ if ( !$dbh->FETCH('Active') ) {
Branch: 2 branches
BranchTrueFalseTotal
if (not $dbh->FETCH('Active')) { } 0 16 16
elsif (not $self->no_ping || $dbh->ping) { } 0 16 16
240 0 $self->reconnect;
241 }
242 ▶ elsif ( !$self->no_ping && !$dbh->ping) {
Condition33%1/3
$self->no_ping || $dbh->ping
l!l&&r!l&&!r
0160
MC/DC0%0/2
$self->no_ping || $dbh->ping
$self->no_ping $dbh->ping
Truth table33%1/3
A || B
A= $self->no_pingB= $dbh->ping
ABresult
1X1
011
000
243 0 $self->reconnect;
244 }
245 }
246 }
247  
248 sub dbh {
249 16 ▶ my $self = shift;
Subroutine dbh: called CC=1 SCAR=0.0
Pod documented
250  
251 16 $self->_verify_pid;
252 16 $self->{dbh};
253 }
254  
255 sub connected {
256 0 ▶ my $self = shift;
Subroutine connected: not called CC=2 SCAR=17.9
Pod documented
257 0 my $dbh = $self->{dbh};
258 0 ▶ return $self->owner_pid && $dbh->ping;
Condition0%0/3
$self->owner_pid && $dbh->ping
!ll&&!rl&&r
000
MC/DC0%0/2
$self->owner_pid && $dbh->ping
$self->owner_pid $dbh->ping
Truth table0%0/3
A && B
A= $self->owner_pidB= $dbh->ping
ABresult
0X0
100
111
259 }
260  
261 sub _execute {
262 0 ▶ my $self = shift;
Subroutine _execute: not called CC=1 SCAR=6.9
263 0 warn "IMPORTANT: '_execute' method is DEPRECATED AND *WILL* BE REMOVED. PLEASE USE 'execute' method.\n";
264 0 return $self->execute(@_);
265 }
266  
267 3 our $SQL_COMMENT_LEVEL = 0;
268 sub execute {
269 11 ▶ my ($self, $sql, $binds) = @_;
Subroutine execute: called CC=13 SCAR=34.9
Pod documented
270  
271 11 ▶ if ($ENV{TENG_SQL_COMMENT} || $self->sql_comment) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ENV{"TENG_SQL_COMMENT"} or $self->sql_comment) 0 11 11
Condition33%1/3
$ENV{"TENG_SQL_COMMENT"} or $self->sql_comment
l!l&&r!l&&!r
0011
MC/DC0%0/2
$ENV{"TENG_SQL_COMMENT"} or $self->sql_comment
$ENV{"TENG_SQL_COMMENT"} $self->sql_comment
Truth table33%1/3
A or B
A= $ENV{"TENG_SQL_COMMENT"}B= $self->sql_comment
ABresult
1X1
011
000
272 0 my $i = $SQL_COMMENT_LEVEL; # optimize, as we would *NEVER* be called
273 0 ▶ while ( my (@caller) = caller($i++) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@caller) = caller $i++) 0 0 0
274 0 ▶ next if ( $caller[0]->isa( __PACKAGE__ ) );
Branch: 1 branch
BranchTrueFalseTotal
if $caller[0]->isa(__PACKAGE__) 0 0 0
275 0 ▶ next if $caller[0] =~ /^Teng::/; # skip Teng::Row, Teng::Plugin::* etc.
Branch: 1 branch
BranchTrueFalseTotal
if $caller[0] =~ /^Teng::/ 0 0 0
276 0 my $comment = "$caller[1] at line $caller[2]";
277 0 $comment =~ s/\*\// /g;
278 0 $sql = "/* $comment */\n$sql";
279 0 last;
280 }
281 }
282  
283 11 my $sth;
284 11 eval {
285 11 $sth = $self->dbh->prepare($sql);
286 11 my $i = 1;
287 11 ▶ for my $v ( @{ $binds || [] } ) {
Branch: 1 branch
BranchTrueFalseTotal
$binds or [] 0 11 11
288 15 ▶ if (Scalar::Util::blessed($v) && ref($v) eq 'SQL::Maker::SQLType') {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($v) and ref $v eq 'SQL::Maker::SQLType') { } 8 7 15
Condition66%2/3
builtin::blessed($v) and ref $v eq "SQL::Maker::SQLType"
!ll&&!rl&&r
708
MC/DC50%1/2
builtin::blessed($v) and ref $v eq "SQL::Maker::SQLType"
builtin::blessed($v) ref $v eq "SQL::Maker::SQLType"
Truth table66%2/3
A and B
A= builtin::blessed($v)B= ref $v eq "SQL::Maker::SQLType"
ABresult
0X0
100
111
289 8 $sth->bind_param($i++, ${$v->value_ref}, $v->type);
290 } else {
291 # allow array ref for using pg_types. e.g. [ $value => { pg_type => PG_BYTEA } ]
292 # ref. https://metacpan.org/pod/DBD::Pg#quote
293 7 ▶ $sth->bind_param( $i++, ref($v) eq 'ARRAY' ? @$v : $v );
Branch: 1 branch
BranchTrueFalseTotal
ref $v eq 'ARRAY' ? : 0 7 7
294 }
295 }
296 11 $sth->execute();
297 };
298  
299 11 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 11 11
300 0 $self->handle_error($sql, $binds, $@);
301 }
302  
303 # When the return value is never used, should finish statement handler
304 11 ▶ unless (defined wantarray) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined wantarray) 0 11 11
305 0 $sth->finish();
306 0 return;
307 }
308  
309 11 return $sth;
310 }
311  
312 sub _last_insert_id {
313 1 ▶ my ($self, $table_name, $column) = @_;
Subroutine _last_insert_id: called CC=6 SCAR=27.7
314  
315 1 my $driver = $self->{driver_name};
316 1 ▶ if ( $driver eq 'mysql' ) {
Branch: 4 branches
BranchTrueFalseTotal
if ($driver eq 'mysql') { } 0 1 1
elsif ($driver eq 'Pg') { } 0 1 1
elsif ($driver eq 'SQLite') { } 1 0 1
elsif ($driver eq 'Oracle') { } 0 0 0
317 0 return $self->{dbh}->{mysql_insertid};
318 } elsif ( $driver eq 'Pg' ) {
319 0 ▶ if (defined $column) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $column) { } 0 0 0
320 0 return $self->dbh->last_insert_id( undef, undef, undef, undef,{ sequence => join( '_', $table_name, $column, 'seq' ) } );
321 } else {
322 0 return $self->dbh->last_insert_id( undef, undef, $table_name, undef);
323 }
324 } elsif ( $driver eq 'SQLite' ) {
325 1 return $self->dbh->func('last_insert_rowid');
326 } elsif ( $driver eq 'Oracle' ) {
327 0 return;
328 } else {
329 0 Carp::croak "Don't know how to get last insert id for $driver";
330 }
331 }
332  
333 sub _bind_sql_type_to_args {
334 4 ▶ my ( $self, $table, $args ) = @_;
Subroutine _bind_sql_type_to_args: called CC=3 SCAR=11.0
335 4 my $bind_args = {};
336  
337 4 for my $col (keys %{$args}) {
338 # if $args->{$col} is a ref, it is scalar ref or already
339 # sql type bined parameter. so ignored.
340 8 ▶ $bind_args->{$col} = ref $args->{$col} ? $args->{$col} : sql_type(\$args->{$col}, $table->get_sql_type($col));
Branch: 1 branch
BranchTrueFalseTotal
ref $args->{$col} ? : 0 8 8
341 }
342  
343 4 return $bind_args;
344 }
345  
346 sub do_insert {
347 4 ▶ my ($self, $table_name, $args, $prefix) = @_;
Subroutine do_insert: called CC=4 SCAR=14.5
Pod documented
348  
349 4 ▶ $prefix ||= 'INSERT INTO';
Condition50%1/2
$prefix ||= "INSERT INTO"
l!l
04
MC/DC0%0/1
$prefix ||= "INSERT INTO"
$prefix
Truth table50%1/2
A
A= $prefix
Aresult
11
00
350 4 my $table = $self->schema->get_table($table_name);
351 4 ▶ if (! $table) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($table) 0 4 4
352 0 local $Carp::CarpLevel = $Carp::CarpLevel + 1;
353 0 Carp::croak( "Table definition for $table_name does not exist (Did you declare it in our schema?)" );
354 }
355  
356 4 for my $col (keys %{$args}) {
357 8 $args->{$col} = $table->call_deflate($col, $args->{$col});
358 }
359 4 my $bind_args = $self->_bind_sql_type_to_args( $table, $args );
360 4 my ($sql, @binds) = $self->{sql_builder}->insert( $table_name, $bind_args, { prefix => $prefix } );
361 4 $self->execute($sql, \@binds);
362 }
363  
364 sub fast_insert {
365 0 ▶ my ($self, $table_name, $args, $prefix) = @_;
Subroutine fast_insert: not called CC=2 SCAR=17.9
Pod documented
366  
367 0 my $sth = $self->do_insert($table_name, $args, $prefix);
368  
369 # XXX in MySQL 5.7.8 or later, $self->dbh->{mysql_insertid} will always return 0,
370 # so that get mysql_insertid from $sth. (https://bugs.mysql.com/bug.php?id=78778)
371 0 ▶ return $sth->{mysql_insertid} if defined $sth->{mysql_insertid};
Branch: 1 branch
BranchTrueFalseTotal
if defined $sth->{"mysql_insertid"} 0 0 0
372  
373 # XXX in Pg, _last_insert_id has potential failure when inserting to non Serial table or explicitly inserting Serrial id
374 0 $self->_last_insert_id($table_name);
375 }
376  
377 sub insert {
378 4 ▶ my ($self, $table_name, $args, $prefix) = @_;
Subroutine insert: called CC=9 SCAR=23.9
Pod documented
379  
380 4 my $sth = $self->do_insert($table_name, $args, $prefix);
381 4 ▶ return unless defined wantarray;
Branch: 1 branch
BranchTrueFalseTotal
unless defined wantarray 0 4 4
382  
383 4 my $table = $self->schema->get_table($table_name);
384 4 my $pk = $table->primary_keys();
385  
386 4 my @missing_primary_keys = grep { not defined $args->{$_} } @$pk;
387 4 ▶ if (@missing_primary_keys == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@missing_primary_keys == 1) 1 3 4
388 # XXX in MySQL 5.7.8 or later, $self->dbh->{mysql_insertid} will always return 0,
389 # so that get mysql_insertid from $sth. (https://bugs.mysql.com/bug.php?id=78778)
390 $args->{$missing_primary_keys[0]} = defined $sth->{mysql_insertid} ? $sth->{mysql_insertid}
391 1 ▶ : $self->_last_insert_id($table_name, $missing_primary_keys[0]);
Branch: 1 branch
BranchTrueFalseTotal
defined $sth->{'mysql_insertid'} ? : 0 1 1
392 }
393  
394 4 ▶ return $args if $self->suppress_row_objects;
Branch: 1 branch
BranchTrueFalseTotal
if $self->suppress_row_objects 0 4 4
395  
396 4 my %where;
397 4 my $refetch = 1;
398 4 for my $key (@$pk) {
399 4 ▶ if (ref $args->{$key}) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $args->{$key}) 0 4 4
400 # care references. eg. \'NOW()'
401 0 $refetch = undef;
402 0 last;
403 }
404 4 $where{$key} = $args->{$key};
405 }
406 4 ▶ if (%where && $refetch) {
Branch: 1 branch
BranchTrueFalseTotal
if (%where and $refetch) 4 0 4
Condition33%1/3
%where and $refetch
!ll&&!rl&&r
004
MC/DC0%0/2
%where and $refetch
%where $refetch
Truth table33%1/3
A and B
A= %whereB= $refetch
ABresult
0X0
100
111
407 # refetch the row for cleanup scalar refs and fill default values
408 4 return $self->single($table_name, \%where);
409 }
410  
411 $table->row_class->new(
412 {
413 0 row_data => $args,
414 teng => $self,
415 table_name => $table_name,
416 }
417 );
418 }
419  
420 sub bulk_insert {
421 0 ▶ my ($self, $table_name, $args, $opt) = @_;
Subroutine bulk_insert: not called CC=12 SCAR=50.5
Pod documented
422  
423 0 ▶ return unless scalar(@{$args||[]});
Branch: 2 branches
BranchTrueFalseTotal
unless scalar @{$args or [];} 0 0 0
$args or [] 0 0 0
424  
425 0 my $dbh = $self->dbh;
426 my $can_multi_insert = $dbh->{Driver}->{Name} eq 'mysql' ? 1
427 : $dbh->{Driver}->{Name} eq 'Pg'
428 0 ▶ && $dbh->{ pg_server_version } >= 82000 ? 1
Branch: 2 branches
BranchTrueFalseTotal
$dbh->{'Driver'}{'Name'} eq 'mysql' ? : 0 0 0
$dbh->{'Driver'}{'Name'} eq 'Pg' && $dbh->{'pg_server_version'} >= 82000 ? : 0 0 0
Condition0%0/3
$dbh->{"Driver"}{"Name"} eq "Pg" and $dbh->{"pg_server_version"} >= 82000
!ll&&!rl&&r
000
MC/DC0%0/2
$dbh->{"Driver"}{"Name"} eq "Pg" and $dbh->{"pg_server_version"} >= 82000
$dbh->{"Driver"}{"Name"} eq "Pg" $dbh->{"pg_server_version"} >= 82000
Truth table0%0/3
A and B
A= $dbh->{"Driver"}{"Name"} eq "Pg"B= $dbh->{"pg_server_version"} >= 82000
ABresult
0X0
100
111
429 : 0;
430  
431 0 ▶ if ($can_multi_insert) {
Branch: 1 branch
BranchTrueFalseTotal
if ($can_multi_insert) { } 0 0 0
432 0 my $table = $self->schema->get_table($table_name);
433 0 ▶ if (! $table) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($table) 0 0 0
434 0 Carp::croak( "Table definition for $table_name does not exist (Did you declare it in our schema?)" );
435 }
436  
437 0 ▶ if ( $table->has_deflators ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($table->has_deflators) 0 0 0
438 0 for my $row (@$args) {
439 0 for my $col (keys %{$row}) {
440 0 $row->{$col} = $table->call_deflate($col, $row->{$col});
441 }
442 }
443 }
444  
445 0 my ($sql, @binds) = $self->sql_builder->insert_multi( $table_name, $args, $opt );
446 0 $self->execute($sql, \@binds);
447 } else {
448 # use transaction for better performance and atomicity.
449 0 my $txn = $self->txn_scope();
450 0 for my $arg (@$args) {
451 # do not run trigger for consistency with mysql.
452 0 $self->insert($table_name, $arg, $opt->{prefix});
453 }
454 0 $txn->commit;
455 }
456 }
457  
458 sub do_update {
459 0 ▶ my ($self, $table_name, $args, $where) = @_;
Subroutine do_update: not called CC=1 SCAR=6.9
Pod documented
460  
461 0 my ($sql, @binds) = $self->{sql_builder}->update( $table_name, $args, $where );
462 0 my $sth = $self->execute($sql, \@binds);
463 0 my $rows = $sth->rows;
464 0 $sth->finish;
465  
466 0 $rows;
467 }
468  
469 sub update {
470 0 ▶ my ($self, $table_name, $args, $where) = @_;
Subroutine update: not called CC=3 SCAR=24.8
Pod documented
471  
472 0 my $table = $self->schema->get_table($table_name);
473 0 ▶ if (! $table) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($table) 0 0 0
474 0 Carp::croak( "Table definition for $table_name does not exist (Did you declare it in our schema?)" );
475 }
476  
477 0 for my $col (keys %{$args}) {
478 0 $args->{$col} = $table->call_deflate($col, $args->{$col});
479 }
480
481 0 $self->do_update($table_name, $self->_bind_sql_type_to_args( $table, $args ), $where);
482 }
483  
484 sub delete {
485 0 ▶ my ($self, $table_name, $where) = @_;
Subroutine delete: not called CC=1 SCAR=6.9
Pod documented
486  
487 0 my ($sql, @binds) = $self->{sql_builder}->delete( $table_name, $where );
488 0 my $sth = $self->execute($sql, \@binds);
489 0 my $rows = $sth->rows;
490 0 $sth->finish;
491  
492 0 $rows;
493 }
494  
495 #--------------------------------------------------------------------------------
496 # for transaction
497 sub txn_manager {
498 0 ▶ my $self = shift;
Subroutine txn_manager: not called CC=3 SCAR=24.8
Pod documented
499 0 $self->_verify_pid;
500 $self->{txn_manager} ||= ($self->{txn_manager_class})
501 0 ▶ ? $self->{txn_manager_class}->new($self->dbh)
Branch: 1 branch
BranchTrueFalseTotal
$self->{'txn_manager_class'} ? : 0 0 0
Condition0%0/3
$self->{"txn_manager"} ||= $self->{"txn_manager_class"} ? $self->{"txn_manager_class"}->new($self->dbh) : "DBIx::TransactionManager"->new($self->dbh)
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{"txn_manager"} ||= $self->{"txn_manager_class"} ? $self->{"txn_manager_class"}->new($self->dbh) : "DBIx::TransactionManager"->new($self->dbh)
$self->{"txn_manager"} $self->{"txn_manager_class"} ? $self->{"txn_manager_class"}->new($self->dbh) : "DBIx::TransactionManager"->new($self->dbh)
Truth table0%0/3
A ||= B
A= $self->{"txn_manager"}B= $self->{"txn_manager_class"} ? $self->{"txn_manager_class"}->new($self->dbh) : "DBIx::TransactionManager"->new($self->dbh)
ABresult
1X1
011
000
502 : DBIx::TransactionManager->new($self->dbh);
503 }
504  
505 sub in_transaction_check {
506 2 ▶ my $self = shift;
Subroutine in_transaction_check: called CC=3 SCAR=18.1
Pod undocumented
507  
508 2 ▶ return unless $self->{txn_manager};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"txn_manager"} 2 0 2
509  
510 0 ▶ if ( my $info = $self->{txn_manager}->in_transaction ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $info = $self->{"txn_manager"}->in_transaction) 0 0 0
511 0 my $caller = $info->{caller};
512 0 my $pid = $info->{pid};
513 0 Carp::confess("Detected transaction during a connect operation (last known transaction at $caller->[1] line $caller->[2], pid $pid). Refusing to proceed at");
514 }
515 }
516  
517 sub txn_scope {
518 0 ▶ my $self = shift;
Subroutine txn_scope: not called CC=1 SCAR=6.9
Pod documented
519 0 my @caller = caller();
520  
521 0 $self->txn_manager->txn_scope(caller => \@caller);
522 }
523  
524 sub txn_begin {
525 0 ▶ my $self = shift;
Subroutine txn_begin: not called CC=1 SCAR=6.9
Pod documented
526  
527 0 $self->txn_manager->txn_begin;
528 }
529 0 ▶sub txn_rollback { $_[0]->txn_manager->txn_rollback }
Subroutine txn_rollback: not called CC=1 SCAR=6.9
Pod documented
530 0 ▶sub txn_commit { $_[0]->txn_manager->txn_commit }
Subroutine txn_commit: not called CC=1 SCAR=6.9
Pod documented
531 0 ▶sub txn_end { $_[0]->txn_manager->txn_end }
Subroutine txn_end: not called CC=1 SCAR=6.9
Pod documented
532  
533 #--------------------------------------------------------------------------------
534  
535 sub do {
536 0 ▶ my ($self, $sql, $attr, @bind_vars) = @_;
Subroutine do: not called CC=3 SCAR=24.8
Pod documented
537 0 my $ret;
538 0 eval { $ret = $self->dbh->do($sql, $attr, @bind_vars) };
539 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
540 0 ▶ $self->handle_error($sql, @bind_vars ? \@bind_vars : '', $@);
Branch: 1 branch
BranchTrueFalseTotal
@bind_vars ? : 0 0 0
541 }
542 0 $ret;
543 }
544  
545 sub _get_select_columns {
546 7 ▶ my ($self, $table, $opt) = @_;
Subroutine _get_select_columns: called CC=3 SCAR=15.1
547  
548 return $opt->{'+columns'}
549 0 ▶ ? [@{$table->{escaped_columns}{$self->{driver_name}}}, @{$opt->{'+columns'}}]
Condition33%1/3
$opt->{"columns"} or $table->{"escaped_columns"}{$self->{"driver_name"}}
l!l&&r!l&&!r
070
MC/DC0%0/2
$opt->{"columns"} or $table->{"escaped_columns"}{$self->{"driver_name"}}
$opt->{"columns"} $table->{"escaped_columns"}{$self->{"driver_name"}}
Truth table33%1/3
A or B
A= $opt->{"columns"}B= $table->{"escaped_columns"}{$self->{"driver_name"}}
ABresult
1X1
011
000
550 : ($opt->{columns} || $table->{escaped_columns}{$self->{driver_name}})
551 7 ▶ ;
Branch: 1 branch
BranchTrueFalseTotal
$opt->{'+columns'} ? : 0 7 7
552 }
553  
554 sub search {
555 0 ▶ my ($self, $table_name, $where, $opt) = @_;
Subroutine search: not called CC=2 SCAR=17.9
Pod documented
556  
557 0 my $table = $self->{schema}->get_table( $table_name );
558 0 ▶ if (! $table) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($table) 0 0 0
559 0 Carp::croak("No such table $table_name");
560 }
561  
562 my ($sql, @binds) = $self->{sql_builder}->select(
563 0 $table_name,
564 $self->_get_select_columns($table, $opt),
565 $where,
566 $opt
567 );
568  
569 0 $self->search_by_sql($sql, \@binds, $table_name);
570 }
571  
572 sub _bind_named {
573 0 ▶ my ($self, $sql, $args ) = @_;
Subroutine _bind_named: not called CC=4 SCAR=30.0
574  
575 0 my @bind;
576 0 $sql =~ s{:([A-Za-z_][A-Za-z0-9_]*)}{
577 0 ▶ Carp::croak("'$1' does not exist in bind hash") if !exists $args->{$1};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $args->{$1} 0 0 0
578 0 ▶ if ( ref $args->{$1} && ref $args->{$1} eq "ARRAY" ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $args->{$1} and ref $args->{$1} eq 'ARRAY') { } 0 0 0
Condition0%0/3
ref $args->{$1} and ref $args->{$1} eq "ARRAY"
!ll&&!rl&&r
000
MC/DC0%0/2
ref $args->{$1} and ref $args->{$1} eq "ARRAY"
ref $args->{$1} ref $args->{$1} eq "ARRAY"
Truth table0%0/3
A and B
A= ref $args->{$1}B= ref $args->{$1} eq "ARRAY"
ABresult
0X0
100
111
579 0 push @bind, @{ $args->{$1} };
580 0 my $tmp = join ',', map { '?' } @{ $args->{$1} };
581 0 "( $tmp )";
582 } else {
583 0 push @bind, $args->{$1};
584 0 '?'
585 }
586 }ge;
587  
588 0 return ($sql, \@bind);
589 }
590  
591 sub search_named {
592 0 ▶ my ($self, $sql, $args, $table_name) = @_;
Subroutine search_named: not called CC=1 SCAR=6.9
Pod documented
593  
594 0 $self->search_by_sql($self->_bind_named($sql, $args), $table_name);
595 }
596  
597 sub single {
598 7 ▶ my ($self, $table_name, $where, $opt) = @_;
Subroutine single: called CC=5 SCAR=17.2
Pod documented
599  
600 7 $opt->{limit} = 1;
601  
602 7 my $table = $self->{schema}->get_table( $table_name );
603 7 ▶ Carp::croak("No such table $table_name") unless $table;
Branch: 1 branch
BranchTrueFalseTotal
unless $table 0 7 7
604  
605 my ($sql, @binds) = $self->{sql_builder}->select(
606 7 $table_name,
607 $self->_get_select_columns($table, $opt),
608 $where,
609 $opt
610 );
611 7 my $sth = $self->execute($sql, \@binds);
612  
613 # When the return value is never used, should not create row object
614 # case example: use `FOR UPDATE` query for global locking
615 7 ▶ unless (defined wantarray) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined wantarray) 0 7 7
616 0 $sth->finish();
617 0 return;
618 }
619  
620 7 my $row = $sth->fetchrow_hashref($self->{fields_case});
621  
622 7 ▶ return undef unless $row; ## no critic
Branch: 1 branch
BranchTrueFalseTotal
unless $row 0 7 7
623 7 ▶ return $row if $self->{suppress_row_objects};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"suppress_row_objects"} 0 7 7
624  
625 $table->{row_class}->new(
626 {
627 7 sql => $sql,
628 row_data => $row,
629 teng => $self,
630 table => $table,
631 table_name => $table_name,
632 }
633 );
634 }
635  
636 sub search_by_sql {
637 0 ▶ my ($self, $sql, $bind, $table_name) = @_;
Subroutine search_by_sql: not called CC=5 SCAR=34.0
Pod documented
638  
639 0 ▶ $table_name ||= $self->_guess_table_name( $sql );
Condition0%0/3
$table_name ||= $self->_guess_table_name($sql)
l!l&&r!l&&!r
000
MC/DC0%0/2
$table_name ||= $self->_guess_table_name($sql)
$table_name $self->_guess_table_name($sql)
Truth table0%0/3
A ||= B
A= $table_nameB= $self->_guess_table_name($sql)
ABresult
1X1
011
000
640 0 my $sth = $self->execute($sql, $bind);
641  
642 # When the return value is never used, should not create iterator object
643 # case example: use `FOR UPDATE` query for global locking
644 0 ▶ unless (defined wantarray) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined wantarray) 0 0 0
645 0 $sth->finish();
646 0 return;
647 }
648  
649 my $itr = Teng::Iterator->new(
650 teng => $self,
651 sth => $sth,
652 sql => $sql,
653 row_class => $self->{schema}->get_row_class($table_name),
654 table => $self->{schema}->get_table( $table_name ),
655 table_name => $table_name,
656 apply_sql_types => $self->{apply_sql_types} || $self->{guess_sql_types},
657 guess_sql_types => $self->{guess_sql_types},
658 suppress_object_creation => $self->{suppress_row_objects},
659 0 ▶ );
Condition0%0/3
$self->{'apply_sql_types'} || $self->{'guess_sql_types'}
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{'apply_sql_types'} || $self->{'guess_sql_types'}
$self->{'apply_sql_types'} $self->{'guess_sql_types'}
Truth table0%0/3
A || B
A= $self->{'apply_sql_types'}B= $self->{'guess_sql_types'}
ABresult
1X1
011
000
660 0 ▶ return wantarray ? $itr->all : $itr;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
661 }
662  
663 sub single_by_sql {
664 0 ▶ my ($self, $sql, $bind, $table_name) = @_;
Subroutine single_by_sql: not called CC=6 SCAR=37.4
Pod documented
665  
666 0 ▶ $table_name ||= $self->_guess_table_name( $sql );
Condition0%0/3
$table_name ||= $self->_guess_table_name($sql)
l!l&&r!l&&!r
000
MC/DC0%0/2
$table_name ||= $self->_guess_table_name($sql)
$table_name $self->_guess_table_name($sql)
Truth table0%0/3
A ||= B
A= $table_nameB= $self->_guess_table_name($sql)
ABresult
1X1
011
000
667 0 my $table = $self->{schema}->get_table( $table_name );
668 0 ▶ Carp::croak("No such table $table_name") unless $table;
Branch: 1 branch
BranchTrueFalseTotal
unless $table 0 0 0
669  
670 0 my $sth = $self->execute($sql, $bind);
671  
672 # When the return value is never used, should not create row object
673 # case example: use `FOR UPDATE` query for global locking
674 0 ▶ unless (defined wantarray) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined wantarray) 0 0 0
675 0 $sth->finish();
676 0 return;
677 }
678  
679 0 my $row = $sth->fetchrow_hashref($self->{fields_case});
680  
681 0 ▶ return unless $row;
Branch: 1 branch
BranchTrueFalseTotal
unless $row 0 0 0
682 0 ▶ return $row if $self->{suppress_row_objects};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"suppress_row_objects"} 0 0 0
683  
684 $table->{row_class}->new(
685 {
686 0 sql => $sql,
687 row_data => $row,
688 teng => $self,
689 table => $table,
690 table_name => $table_name,
691 }
692 );
693 }
694  
695 sub new_row_from_hash {
696 0 ▶ my ($self, $table_name, $data, $sql) = @_;
Subroutine new_row_from_hash: not called CC=6 SCAR=37.4
Pod documented
697  
698 0 my $table = $self->{schema}->get_table( $table_name );
699 0 ▶ Carp::croak("No such table $table_name") unless $table;
Branch: 1 branch
BranchTrueFalseTotal
unless $table 0 0 0
700  
701 0 ▶ return $data if $self->{suppress_row_objects};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"suppress_row_objects"} 0 0 0
702  
703 $table->{row_class}->new(
704 {
705 0 ▶ sql => $sql || do {
Condition0%0/3
$sql || do {
l!l&&r!l&&!r
000
MC/DC0%0/2
$sql || do { my(@caller) = caller 0; my $level = 0; while ($caller[0] eq __PACKAGE__ or $caller[0] eq ref $self) { @caller = caller ++$level; } ; sprintf '/* DUMMY QUERY %s->new_row_from_hash created from %s line %d */', ref $self, $caller[1], $caller[2] }
$sql do { my(@caller) = caller 0; my $level = 0; while ($caller[0] eq __PACKAGE__ or $caller[0] eq ref $self) { @caller = caller ++$level; } ; sprintf '/* DUMMY QUERY %s->new_row_from_hash created from %s line %d */', ref $self, $caller[1], $caller[2] }
Truth table0%0/3
A || B
A= $sqlB= do { my(@caller) = caller 0; my $level = 0; while ($caller[0] eq __PACKAGE__ or $caller[0] eq ref $self) { @caller = caller ++$level; } ; sprintf '/* DUMMY QUERY %s->new_row_from_hash created from %s line %d */', ref $self, $caller[1], $caller[2] }
ABresult
1X1
011
000
706 0 my @caller = caller(0);
707 0 my $level = 0;
708 0 ▶ while ($caller[0] eq __PACKAGE__ || $caller[0] eq ref $self) {
Branch: 1 branch
BranchTrueFalseTotal
if ($caller[0] eq __PACKAGE__ or $caller[0] eq ref $self) 0 0 0
Condition0%0/3
$caller[0] eq __PACKAGE__ or $caller[0] eq ref $self
l!l&&r!l&&!r
000
MC/DC0%0/2
$caller[0] eq __PACKAGE__ or $caller[0] eq ref $self
$caller[0] eq __PACKAGE__ $caller[0] eq ref $self
Truth table0%0/3
A or B
A= $caller[0] eq __PACKAGE__B= $caller[0] eq ref $self
ABresult
1X1
011
000
709 0 @caller = caller(++$level);
710 }
711 0 sprintf '/* DUMMY QUERY %s->new_row_from_hash created from %s line %d */', ref $self, $caller[1], $caller[2];
712 },
713 row_data => $data,
714 teng => $self,
715 table => $table,
716 table_name => $table_name,
717 }
718 );
719 }
720  
721 sub single_named {
722 0 ▶ my ($self, $sql, $args, $table_name) = @_;
Subroutine single_named: not called CC=1 SCAR=6.9
Pod documented
723  
724 0 $self->single_by_sql($self->_bind_named($sql, $args), $table_name);
725 }
726  
727 sub _guess_table_name {
728 0 ▶ my ($class, $sql) = @_;
Subroutine _guess_table_name: not called CC=2 SCAR=17.9
729  
730 0 ▶ if ($sql =~ /\sfrom\s+["`]?([\w]+)["`]?\s*/si) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sql =~ /\sfrom\s+["`]?([\w]+)["`]?\s*/is) 0 0 0
731 0 return $1;
732 }
733 0 return;
734 }
735  
736 sub handle_error {
737 0 ▶ my ($self, $stmt, $bind, $reason) = @_;
Subroutine handle_error: not called CC=1 SCAR=6.9
Pod documented
738 0 require Data::Dumper;
739  
740 0 local $Data::Dumper::Maxdepth = 2;
741 0 $stmt =~ s/\n/\n /gm;
742 0 Carp::croak sprintf <<"TRACE", $reason, $stmt, Data::Dumper::Dumper($bind);
743 @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
744 @@@@@ Teng 's Exception @@@@@
745 Reason : %s
746 SQL : %s
747 BIND : %s
748 @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
749 TRACE
750 }
751  
752 sub DESTROY {
753 5 ▶ my $self = shift;
Subroutine DESTROY: called CC=4 SCAR=18.9
754  
755 5 ▶ if ( $self->owner_pid and $self->owner_pid != $$ and my $dbh = $self->{dbh} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->owner_pid and $self->owner_pid != $$ and my $dbh = $self->{"dbh"}) 0 5 5
Condition33%2/6
$self->owner_pid and $self->owner_pid != $$ and my $dbh = $self->{"dbh"}
!ll&&!rl&&r
500
$self->owner_pid and $self->owner_pid != $$
!ll&&!rl&&r
050
MC/DC0%0/3
$self->owner_pid and $self->owner_pid != $$ and my $dbh = $self->{"dbh"}
$self->owner_pid $self->owner_pid != $$ my $dbh = $self->{"dbh"}
Truth table25%1/4
A and B and C
A= $self->owner_pidB= $self->owner_pid != $$C= my $dbh = $self->{"dbh"}
ABCresult
0XX0
10X0
1100
1111
756 0 $dbh->{InactiveDestroy} = 1;
757 }
758 }
759  
760 3 1;
761  
762 __END__
763 =head1 NAME
764  
765 Teng - very simple DBI wrapper/ORMapper
766  
767 =head1 SYNOPSIS
768  
769 my $db = MyDB->new({ connect_info => [ 'dbi:SQLite:' ] });
770 my $row = $db->insert( 'table' => {
771 col1 => $value
772 } );
773  
774 =head1 DESCRIPTION
775  
776 Teng is very simple DBI wrapper and simple O/R Mapper.
777 It aims to be lightweight, with minimal dependencies so it's easier to install.
778  
779 =head1 BASIC USAGE
780  
781 create your db model base class.
782  
783 package Your::Model;
784 use parent 'Teng';
785 1;
786
787 create your db schema class.
788 See Teng::Schema for docs on defining schema class.
789  
790 package Your::Model::Schema;
791 use Teng::Schema::Declare;
792 table {
793 name 'user';
794 pk 'id';
795 columns qw( foo bar baz );
796 };
797 1;
798
799 in your script.
800  
801 use Your::Model;
802
803 my $teng = Your::Model->new(\%args);
804 # insert new record.
805 my $row = $teng->insert('user',
806 {
807 id => 1,
808 }
809 );
810 $row->update({name => 'nekokak'}); # same do { $row->name('nekokak'); $row->update; }
811  
812 $row = $teng->single_by_sql(q{SELECT id, name FROM user WHERE id = ?}, [ 1 ]);
813 $row->delete();
814  
815 =head1 ARCHITECTURE
816  
817 Teng classes are comprised of three distinct components:
818  
819 =head2 MODEL
820  
821 The C<model> is where you say
822  
823 package MyApp::Model;
824 use parent 'Teng';
825  
826 This is the entry point to using Teng. You connect, insert, update, delete, select stuff using this object.
827  
828 =head2 SCHEMA
829  
830 The C<schema> is a simple class that describes your table definitions. Note that this is different from DBIx::Class terms.
831 DBIC's schema is equivalent to Teng's model + schema, where the actual schema information is scattered across the result classes.
832  
833 In Teng, you simply use Teng::Schema's domain specific language to define a set of tables
834  
835 package MyApp::Model::Schema;
836 use Teng::Schema::Declare;
837  
838 table {
839 name $table_name;
840 pk $primary_key_column;
841 columns qw(
842 column1
843 column2
844 column3
845 );
846 }
847  
848 ... and other tables ...
849  
850 =head2 ROW
851  
852 Unlike DBIx::Class, you don't need to have a set of classes that represent a row type (i.e. "result" classes in DBIC terms).
853 In Teng, the row objects are blessed into anonymous classes that inherit from Teng::Row,
854 so you don't have to create these classes if you just want to use some simple queries.
855  
856 If you want to define methods to be performed by your row objects, simply create a row class like so:
857  
858 package MyApp::Model::Row::Camelizedtable_name;
859 use parent qw(Teng::Row);
860  
861 Note that your table name will be camelized.
862  
863 =head1 METHODS
864  
865 Teng provides a number of methods to all your classes,
866  
867 =over
868  
869 =item $teng = Teng->new(\%args)
870  
871 Creates a new Teng instance.
872  
873 # connect new database connection.
874 my $db = Your::Model->new(
875 connect_info => [ $dsn, $username, $password, \%connect_options ]
876 );
877  
878 Arguments can be:
879  
880 =over
881  
882 =item * C<connect_info>
883  
884 Specifies the information required to connect to the database.
885 The argument should be a reference to a array in the form:
886  
887 [ $dsn, $user, $password, \%options ]
888  
889 You must pass C<connect_info> or C<dbh> to the constructor.
890  
891 =item * C<dbh>
892  
893 Specifies the database handle to use.
894  
895 =item * C<no_ping>
896  
897 By default, ping before each executing query.
898 If it affect performance then you can set to true for ping stopping.
899  
900 =item * C<fields_case>
901  
902 specific DBI.pm's FetchHashKeyName.
903  
904 =item * C<schema>
905  
906 Specifies the Teng::Schema instance to use.
907 If not specified, the value specified in C<schema_class> is loaded and
908 instantiated for you.
909  
910 =item * C<schema_class>
911  
912 Specifies the schema class to use.
913 By default {YOUR_MODEL_CLASS}::Schema is used.
914  
915 =item * C<txn_manager_class>
916  
917 Specifies the transaction manager class.
918 By default DBIx::TransactionManager is used.
919  
920 =item * C<suppress_row_objects>
921  
922 Specifies the row object creation mode. By default this value is C<false>.
923 If you specifies this to a C<true> value, no row object will be created when
924 a C<SELECT> statement is issued..
925  
926 =item * C<force_deflate_set_column>
927  
928 Specifies C<set_column>, C<set_columns> and column name method behaviour. By default this value is C<false>.
929 If you specifies this to a C<true> value, C<set_column> or column name method will deflate argument.
930  
931 =item * C<sql_builder>
932  
933 Speficies the SQL builder object. By default SQL::Maker is used, and as such,
934 if you provide your own SQL builder the interface needs to be compatible
935 with SQL::Maker.
936  
937 =item * C<sql_builder_class> : Str
938  
939 Speficies the SQL builder class name. By default SQL::Maker is used, and as such,
940 if you provide your own SQL builder the interface needs to be compatible
941 with SQL::Maker.
942  
943 Specified C<sql_builder_class> is instantiated with following:
944  
945 $sql_builder_class->new(
946 driver => $teng->{driver_name},
947 %{ $teng->{sql_builder_args} }
948 )
949  
950 This is not used when C<sql_builder> is specified.
951  
952 =item * C<sql_builder_args> : HashRef
953  
954 Speficies the arguments for constructor of C<sql_builder_class>. This is not used when C<sql_builder> is specified.
955  
956 =back
957  
958 =item C<$row = $teng-E<gt>insert($table_name, \%row_data)>
959  
960 Inserts a new record. Returns the inserted row object.
961  
962 my $row = $teng->insert('user',{
963 id => 1,
964 name => 'nekokak',
965 });
966  
967 If a primary key is available, it will be fetched after the insert -- so
968 an INSERT followed by SELECT is performed. If you do not want this, use
969 C<fast_insert>.
970  
971 =item C<$last_insert_id = $teng-E<gt>fast_insert($table_name, \%row_data);>
972  
973 insert new record and get last_insert_id.
974  
975 no creation row object.
976  
977 =item C<< $teng->do_insert >>
978  
979 Internal method called from C<insert> and C<fast_insert>. You can hook it on your responsibility.
980  
981 =item C<$teng-E<gt>bulk_insert($table_name, \@rows_data, \%opt)>
982  
983 Accepts either an arrayref of hashrefs.
984 each hashref should be a structure suitable
985 for submitting to a Your::Model->insert(...) method.
986 The second argument is an arrayref of hashrefs. All of the keys in these hashrefs must be exactly the same.
987  
988 insert many record by bulk.
989  
990 example:
991  
992 Your::Model->bulk_insert('user',[
993 {
994 id => 1,
995 name => 'nekokak',
996 },
997 {
998 id => 2,
999 name => 'yappo',
1000 },
1001 {
1002 id => 3,
1003 name => 'walf443',
1004 },
1005 ]);
1006  
1007 You can specify C<$opt> like C<< { prefix => 'INSERT IGNORE INTO' } >> or C<< { update => { name => 'updated' } } >> optionally, which will be passed to query builder.
1008  
1009 =item C<$update_row_count = $teng-E<gt>update($table_name, \%update_row_data, [\%update_condition])>
1010  
1011 Calls UPDATE on C<$table_name>, with values specified in C<%update_ro_data>, and returns the number of rows updated. You may optionally specify C<%update_condition> to create a conditional update query.
1012  
1013 my $update_row_count = $teng->update('user',
1014 {
1015 name => 'nomaneko',
1016 },
1017 {
1018 id => 1
1019 }
1020 );
1021 # Executes UPDATE user SET name = 'nomaneko' WHERE id = 1
1022  
1023 You can also call update on a row object:
1024  
1025 my $row = $teng->single('user',{id => 1});
1026 $row->update({name => 'nomaneko'});
1027  
1028 You can use the set_column method:
1029  
1030 my $row = $teng->single('user', {id => 1});
1031 $row->set_column( name => 'yappo' );
1032 $row->update;
1033  
1034 you can column update by using column method:
1035  
1036 my $row = $teng->single('user', {id => 1});
1037 $row->name('yappo');
1038 $row->update;
1039  
1040 =item C<$updated_row_count = $teng-E<gt>do_update($table_name, \%set, \%where)>
1041  
1042 This is low level API for UPDATE. Normally, you should use update method instead of this.
1043  
1044 This method does not deflate \%args.
1045  
1046 =item C<$delete_row_count = $teng-E<gt>delete($table, \%delete_condition)>
1047  
1048 Deletes the specified record(s) from C<$table> and returns the number of rows deleted. You may optionally specify C<%delete_condition> to create a conditional delete query.
1049  
1050 my $rows_deleted = $teng->delete( 'user', {
1051 id => 1
1052 } );
1053 # Executes DELETE FROM user WHERE id = 1
1054  
1055 You can also call delete on a row object:
1056  
1057 my $row = $teng->single('user', {id => 1});
1058 $row->delete
1059  
1060 =item C<$itr = $teng-E<gt>search($table_name, [\%search_condition, [\%search_attr]])>
1061  
1062 simple search method.
1063 search method get Teng::Iterator's instance object.
1064  
1065 see L<Teng::Iterator>
1066  
1067 get iterator:
1068  
1069 my $itr = $teng->search('user',{id => 1},{order_by => 'id'});
1070  
1071 get rows:
1072  
1073 my @rows = $teng->search('user',{id => 1},{order_by => 'id'});
1074  
1075 =item C<$row = $teng-E<gt>single($table_name, \%search_condition)>
1076  
1077 get one record.
1078 give back one case of the beginning when it is acquired plural records by single method.
1079  
1080 my $row = $teng->single('user',{id =>1});
1081  
1082 =item C<$row = $teng-E<gt>new_row_from_hash($table_name, \%row_data, [$sql])>
1083  
1084 create row object from data. (not fetch from db.)
1085 It's useful in such as testing.
1086  
1087 my $row = $teng->new_row_from_hash('user', { id => 1, foo => "bar" });
1088 say $row->foo; # say bar
1089  
1090 =item C<$itr = $teng-E<gt>search_named($sql, [\%bind_values, [$table_name]])>
1091  
1092 execute named query
1093  
1094 my $itr = $teng->search_named(q{SELECT * FROM user WHERE id = :id}, {id => 1});
1095  
1096 If you give ArrayRef to value, that is expanded to "(?,?,?,?)" in SQL.
1097 It's useful in case use IN statement.
1098  
1099 # SELECT * FROM user WHERE id IN (?,?,?);
1100 # bind [1,2,3]
1101 my $itr = $teng->search_named(q{SELECT * FROM user WHERE id IN :ids}, {ids => [1, 2, 3]});
1102  
1103 If you give table_name. It is assumed the hint that makes Teng::Row's Object.
1104  
1105 =item C<$itr = $teng-E<gt>search_by_sql($sql, [\@bind_values, [$table_name]])>
1106  
1107 execute your SQL
1108  
1109 my $itr = $teng->search_by_sql(q{
1110 SELECT
1111 id, name
1112 FROM
1113 user
1114 WHERE
1115 id = ?
1116 },[ 1 ]);
1117  
1118 If $table is specified, it set table information to result iterator.
1119 So, you can use table row class to search_by_sql result.
1120  
1121 =item C<$row = $teng-E<gt>single_by_sql($sql, [\@bind_values, [$table_name]])>
1122  
1123 get one record from your SQL.
1124  
1125 my $row = $teng->single_by_sql(q{SELECT id,name FROM user WHERE id = ? LIMIT 1}, [1], 'user');
1126  
1127 This is a shortcut for
1128  
1129 my $row = $teng->search_by_sql(q{SELECT id,name FROM user WHERE id = ? LIMIT 1}, [1], 'user')->next;
1130  
1131 But optimized implementation.
1132  
1133 =item C<$row = $teng-E<gt>single_named($sql, [\%bind_values, [$table_name]])>
1134  
1135 get one record from execute named query
1136  
1137 my $row = $teng->single_named(q{SELECT id,name FROM user WHERE id = :id LIMIT 1}, {id => 1}, 'user');
1138  
1139 This is a shortcut for
1140  
1141 my $row = $teng->search_named(q{SELECT id,name FROM user WHERE id = :id LIMIT 1}, {id => 1}, 'user')->next;
1142  
1143 But optimized implementation.
1144  
1145 =item C<$sth = $teng-E<gt>execute($sql, [\@bind_values])>
1146  
1147 execute query and get statement handler.
1148 and will be inserted caller's file and line as a comment in the SQL if $ENV{TENG_SQL_COMMENT} or sql_comment is true value.
1149  
1150 =item C<$teng-E<gt>txn_scope>
1151  
1152 Creates a new transaction scope guard object.
1153  
1154 do {
1155 my $txn = $teng->txn_scope;
1156  
1157 $row->update({foo => 'bar'});
1158  
1159 $txn->commit;
1160 }
1161  
1162 If an exception occurs, or the guard object otherwise leaves the scope
1163 before C<< $txn->commit >> is called, the transaction will be rolled
1164 back by an explicit L</txn_rollback> call. In essence this is akin to
1165 using a L</txn_begin>/L</txn_commit> pair, without having to worry
1166 about calling L</txn_rollback> at the right places. Note that since there
1167 is no defined code closure, there will be no retries and other magic upon
1168 database disconnection.
1169  
1170 =item C<$txn_manager = $teng-E<gt>txn_manager>
1171  
1172 Create the transaction manager instance with specified C<txn_manager_class>.
1173  
1174 =item C<$teng-E<gt>txn_begin>
1175  
1176 start new transaction.
1177  
1178 =item C<$teng-E<gt>txn_commit>
1179  
1180 commit transaction.
1181  
1182 =item C<$teng-E<gt>txn_rollback>
1183  
1184 rollback transaction.
1185  
1186 =item C<$teng-E<gt>txn_end>
1187  
1188 finish transaction.
1189  
1190 =item C<$teng-E<gt>do($sql, [\%option, @bind_values])>
1191  
1192 Execute the query specified by C<$sql>, using C<%option> and C<@bind_values> as necessary. This pretty much a wrapper around L<http://search.cpan.org/dist/DBI/DBI.pm#do>
1193  
1194 =item C<$teng-E<gt>dbh>
1195  
1196 get database handle.
1197  
1198 =item C<$teng-E<gt>connect(\@connect_info)>
1199  
1200 connect database handle.
1201  
1202 connect_info is [$dsn, $user, $password, $options].
1203  
1204 If you give \@connect_info, create new database connection.
1205  
1206 =item C<$teng-E<gt>disconnect()>
1207  
1208 Disconnects from the currently connected database.
1209  
1210 =item C<$teng-E<gt>suppress_row_objects($flag)>
1211  
1212 set row object creation mode.
1213  
1214 =item C<$teng-E<gt>apply_sql_types($flag)>
1215  
1216 set SQL type application mode.
1217  
1218 see apply_sql_types in L<Teng::Iterator/METHODS>
1219  
1220 =item C<$teng-E<gt>guess_sql_types($flag)>
1221  
1222 set SQL type guessing mode.
1223 this implies apply_sql_types true.
1224  
1225 see guess_sql_types in L<Teng::Iterator/METHODS>
1226  
1227 =item C<$teng-E<gt>set_boolean_value($true, $false)>
1228  
1229 set scalar to correspond boolean.
1230 this is ignored when apply_sql_types is not true.
1231  
1232 $teng->set_boolean_value(JSON::XS::true, JSON::XS::false);
1233  
1234 =item C<$teng-E<gt>load_plugin();>
1235  
1236 $teng->load_plugin($plugin_class, $options);
1237  
1238 This imports plugin class's methods to C<$teng> class
1239 and it calls $plugin_class's init method if it has.
1240  
1241 $plugin_class->init($teng, $options);
1242  
1243 If you want to change imported method name, use C<alias> option.
1244 for example:
1245  
1246 YourDB->load_plugin('BulkInsert', { alias => { bulk_insert => 'isnert_bulk' } });
1247  
1248 BulkInsert's "bulk_insert" method is imported as "insert_bulk".
1249  
1250 =item C<$teng-E<gt>handle_error>
1251  
1252 handling error method.
1253  
1254 =item C<< $teng->connected >>
1255  
1256 check connected or not.
1257  
1258 =item C<< $teng->reconnect >>
1259  
1260 reconnect database
1261  
1262 =item C<< $teng->mode >>
1263  
1264 DEPRECATED AND *WILL* BE REMOVED. PLEASE USE C< no_ping > option.
1265  
1266 =item How do you use display the profiling result?
1267  
1268 use L<Devel::KYTProf>.
1269  
1270 =back
1271  
1272 =head1 TRIGGERS
1273  
1274 Teng does not support triggers (NOTE: do not confuse it with SQL triggers - we're talking about Perl level triggers). If you really want to hook into the various methods, use something like L<Moose>, L<Mouse>, and L<Class::Method::Modifiers>.
1275  
1276 =head1 SEE ALSO
1277  
1278 =head2 Fork
1279  
1280 This module was forked from L<DBIx::Skinny>, around version 0.0732.
1281 many incompatible changes have been made.
1282  
1283 =head1 BUGS AND LIMITATIONS
1284  
1285 No bugs have been reported.
1286  
1287 =head1 AUTHORS
1288  
1289 Atsushi Kobayashi C<< <nekokak __at__ gmail.com> >>
1290  
1291 Tokuhiro Matsuno E<lt>tokuhirom@gmail.comE<gt>
1292  
1293 Daisuke Maki C<< <daisuke@endeworks.jp> >>
1294  
1295 =head1 SUPPORT
1296  
1297 irc: #dbix-skinny@irc.perl.org
1298  
1299 ML: http://groups.google.com/group/dbix-skinny
1300  
1301 =head1 REPOSITORY
1302  
1303 git clone git://github.com/nekokak/p5-teng.git
1304  
1305 =head1 LICENCE AND COPYRIGHT
1306  
1307 Copyright (c) 2010, the Teng L</AUTHOR>. All rights reserved.
1308  
1309 This module is free software; you can redistribute it and/or
1310 modify it under the same terms as Perl itself. See L<perlartistic>.
1311  
1312 =cut
1313  
« blib/lib/Teng/Row.pm ↑ Summary blib/lib/Teng/Plugin/BulkInsert.pm »