lib/UR/DBI.pm

filter: Statementstmt 21.4% 106 / 495 Branchbran 4.0% 7 / 174 Conditioncond 2.3% 1 / 42 MC/DCmcdc 0.0% 0 / 27 Subroutinesub 28.3% 21 / 74 Podpod 6.2% 1 / 16 totaltotal 16.4% 136 / 828 CC 100 SCAR 37.9
CC
100
Cov
16%
before_execute
208.4
_check_query_timing
132.0
after_all_fetches_with_sth
72.0
CRAP 44.3
« blib/lib/UR/Namespace/Command/Define/Class.pm ↑ Summary lib/UR/ModuleBase.pm »
1 # Additional methods for DBI.
2  
3 package UR::DBI;
4  
5 =pod
6  
7 =head1 NAME
8  
9 UR::DBI - methods for interacting with a database.
10  
11 =head1 SYNOPSIS
12  
13 ##- use UR::DBI;
14 UR::DBI->monitor_sql(1);
15 my $dbh = UR::DBI->connect(...);
16  
17 =head1 DESCRIPTION
18  
19 This module subclasses DBI, and provides a few extra methods useful when using a database.
20  
21 =head1 METHODS
22  
23 =over 4
24  
25 =cut
26  
27 # set up package
28 266 require 5.006_000;
29 266 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
30 266 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
31 266 our $VERSION = "0.47"; # UR $VERSION;
32  
33 # set up module
34 266 ▶use base qw(Exporter DBI);
Subroutine BEGIN: called CC=1 SCAR=0.0
35 266 our (@EXPORT, @EXPORT_OK);
36 266 @EXPORT = qw();
37 266 @EXPORT_OK = qw();
38  
39 266 ▶use IO::Handle;
Subroutine BEGIN: called CC=1 SCAR=0.0
40 266 ▶use IO::File;
Subroutine BEGIN: called CC=1 SCAR=0.0
41 266 ▶use Time::HiRes;
Subroutine BEGIN: called CC=1 SCAR=0.0
42 # do not use UR::ModuleBase as base class because it does not play nice with DBI
43  
44 #
45 # UR::DBI control flags
46 #
47  
48 # Build a few class methods to manipulate the environment variables
49 # that control SQL monitoring
50  
51 266 my %sub_env_map = ( monitor_sql => 'UR_DBI_MONITOR_SQL',
52 monitor_dml => 'UR_DBI_MONITOR_DML',
53 explain_sql_if => 'UR_DBI_EXPLAIN_SQL_IF',
54 explain_sql_slow => 'UR_DBI_EXPLAIN_SQL_SLOW',
55 explain_sql_match => 'UR_DBI_EXPLAIN_SQL_MATCH',
56 explain_sql_callstack => 'UR_DBI_EXPLAIN_SQL_CALLSTACK',
57 no_commit => 'UR_DBI_NO_COMMIT',
58 monitor_every_fetch => 'UR_DBI_MONITOR_EVERY_FETCH',
59 dump_stack_on_connect => 'UR_DBI_DUMP_STACK_ON_CONNECT',
60 );
61  
62 266 our ($monitor_sql,$monitor_dml,$no_commit,$monitor_every_fetch,$dump_stack_on_connect,
63 $explain_sql_slow,$explain_sql_if,$explain_sql_match,$explain_sql_callstack);
64  
65 266 ▶while ( my($subname, $envname) = each ( %sub_env_map ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %sub_env_map) 2394 266 2660
66 266 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
67 # There's a scalar of the same name as the sub to hold the value, hook them together
68 2394 *{$subname} = \$ENV{$envname};
69 0 ▶ my $subref = sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
70 0 ▶ if (@_ > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ > 1) 0 0 0
71 0 $$subname = $_[1];
72 }
73 0 return $$subname;
74 2394 };
75 2394 ▶ if ($subname =~ /explain/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($subname =~ /explain/) { } 1064 1330 2394
76 1064 eval "\$$subname = '' if not defined \$$subname";
77 }
78 else {
79 1330 eval "\$$subname = 0 if not defined \$$subname";
80 }
81 2394 ▶ die $@ if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 2394 2394
82 2394 *$subname = $subref;
83 }
84  
85 # by default, monitored SQL goes to STDOUT
86 # FIXME change this 'our' back to a 'my' after we're transisitioned off of the old App API
87 266 our $sql_fh = IO::Handle->new;
88 266 $sql_fh->fdopen(fileno(STDERR), 'w');
89 266 $sql_fh->autoflush(1);
90 sub sql_fh
91 0 ▶{
Subroutine sql_fh: not called CC=2 SCAR=17.9
Pod undocumented
92 0 ▶ $sql_fh = $_[1] if @_ > 1;
Branch: 1 branch
BranchTrueFalseTotal
if @_ > 1 0 0 0
93 0 return $sql_fh;
94 }
95  
96 #
97 # Logging methods
98 #
99  
100 266 our $log_file;
101 0 ▶sub log_file {
Subroutine log_file: not called CC=2 SCAR=17.9
Pod undocumented
102 0 ▶ $log_file = pop if @_ > 1;
Branch: 1 branch
BranchTrueFalseTotal
if @_ > 1 0 0 0
103 0 return $log_file;
104 }
105  
106 266 our $log_fh;
107 266 my $create_time=0;
108 0 ▶sub start_logging {
Subroutine start_logging: not called CC=4 SCAR=30.0
Pod undocumented
109 0 ▶ return 1 if(defined($log_fh));
Branch: 1 branch
BranchTrueFalseTotal
if defined $UR::DBI::log_fh 0 0 0
110 0 ▶ return 0 if(-e "$log_file");
Branch: 1 branch
BranchTrueFalseTotal
if -e "$UR::DBI::log_file" 0 0 0
111 0 $log_fh = new IO::File("> ${log_file}");
112 0 ▶ unless(defined($log_fh)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $UR::DBI::log_fh) 0 0 0
113 0 warn "Logging File $log_file Could not be created\n";
114 0 return 0;
115 }
116 0 $create_time=Time::HiRes::time();
117 0 return 1;
118 }
119  
120 0 ▶sub stop_logging {
Subroutine stop_logging: not called CC=2 SCAR=17.9
Pod undocumented
121 0 ▶ return 1 unless(defined($log_fh));
Branch: 1 branch
BranchTrueFalseTotal
unless defined $UR::DBI::log_fh 0 0 0
122 0 $log_fh->close;
123 0 undef $log_fh;
124 }
125  
126 0 ▶sub log_sql {
Subroutine log_sql: not called CC=4 SCAR=30.0
Pod undocumented
127 0 ▶ return 1 unless(defined($log_fh));
Branch: 1 branch
BranchTrueFalseTotal
unless defined $UR::DBI::log_fh 0 0 0
128 0 my $sql=pop;
129 0 my $no_timestamp=pop;
130 0 ▶ print $log_fh '=' x 10, "\n" unless($no_timestamp);
Branch: 1 branch
BranchTrueFalseTotal
unless $no_timestamp 0 0 0
131 0 ▶ print $log_fh Time::HiRes::time()-$create_time, "\n" unless($no_timestamp);
Branch: 1 branch
BranchTrueFalseTotal
unless $no_timestamp 0 0 0
132 0 print $log_fh $sql;
133 }
134  
135 #
136 # Standard DBI overrides
137 #
138  
139 sub connect
140 0 ▶{
Subroutine connect: not called CC=7 SCAR=40.3
Pod documented
141 0 my $self = shift;
142 0 my @params = @_;
143  
144 0 ▶ if ($monitor_sql or $dump_stack_on_connect) {
Branch: 1 branch
BranchTrueFalseTotal
if ($UR::DBI::monitor_sql or $UR::DBI::dump_stack_on_connect) 0 0 0
Condition0%0/3
$UR::DBI::monitor_sql or $UR::DBI::dump_stack_on_connect
l!l&&r!l&&!r
000
MC/DC0%0/2
$UR::DBI::monitor_sql or $UR::DBI::dump_stack_on_connect
$UR::DBI::monitor_sql $UR::DBI::dump_stack_on_connect
Truth table0%0/3
A or B
A= $UR::DBI::monitor_sqlB= $UR::DBI::dump_stack_on_connect
ABresult
1X1
011
000
145 0 my $time = time;
146 0 my $time_string = join(' ', $time, '[' . localtime($time) . ']');
147 0 $sql_fh->print("DB CONNECT AT: $time_string");
148 }
149 0 ▶ if ($dump_stack_on_connect) {
Branch: 1 branch
BranchTrueFalseTotal
if ($UR::DBI::dump_stack_on_connect) 0 0 0
150 0 $sql_fh->print(Carp::longmess());
151 }
152
153 0 $params[2] = 'xxx';
154  
155 # Param 3 is usually a hashref of connection modifiers
156 0 ▶ if (ref($params[3]) and ref($params[3]) =~ m/HASH/) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $params[3] and ref($params[3]) =~ /HASH/) 0 0 0
Condition0%0/3
ref $params[3] and ref($params[3]) =~ /HASH/
!ll&&!rl&&r
000
MC/DC0%0/2
ref $params[3] and ref($params[3]) =~ /HASH/
ref $params[3] ref($params[3]) =~ /HASH/
Truth table0%0/3
A and B
A= ref $params[3]B= ref($params[3]) =~ /HASH/
ABresult
0X0
100
111
157 my $string = join(', ',
158 0 map { $_ . ' => ' . $params[3]->{$_} }
159 0 keys(%{$params[3]})
160 );
161 0 $params[3] = "{ $string }";
162 }
163
164 0 ▶ my $params_stringified = join(",", map { defined($_) ? "'$_'" : 'undef' } @params);
Branch: 1 branch
BranchTrueFalseTotal
defined $_ ? : 0 0 0
165 0 UR::DBI::before_execute("connecting with params: ($params_stringified)");
166
167 0 my $rv = $self->SUPER::connect(@_);
168 0 UR::DBI::after_execute();
169 0 return $rv;
170 }
171  
172 #
173 # UR::Object hooks
174 #
175  
176 0 ▶sub commit_all_app_db_objects {
Subroutine commit_all_app_db_objects: not called CC=4 SCAR=30.0
Pod undocumented
177 0 my $this_class = shift;
178 0 my $handle = shift;
179
180 0 my $data_source;
181 0 ▶ if ($handle->isa("UR::DBI::db")) {
Branch: 2 branches
BranchTrueFalseTotal
if ($handle->isa('UR::DBI::db')) { } 0 0 0
elsif ($handle->isa('UR::DBI::st')) { } 0 0 0
182 0 $data_source = UR::DataSource::RDBMS->get_for_dbh($handle);
183 }
184 elsif ($handle->isa("UR::DBI::st")) {
185 0 $data_source = UR::DataSource::RDBMS->get_for_dbh($handle->{Database});
186 }
187 else {
188 0 Carp::confess("No handle passed to method!?")
189 }
190
191 0 ▶ unless ($data_source) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($data_source) 0 0 0
192 0 return;
193 }
194  
195 0 return $data_source->_set_all_objects_saved_committed();
196 }
197  
198 0 ▶sub rollback_all_app_db_objects {
Subroutine rollback_all_app_db_objects: not called CC=4 SCAR=30.0
Pod undocumented
199 0 my $this_class = shift;
200 0 my $handle = shift;
201
202 0 my $data_source;
203 0 ▶ if ($handle->isa("UR::DBI::db")) {
Branch: 2 branches
BranchTrueFalseTotal
if ($handle->isa('UR::DBI::db')) { } 0 0 0
elsif ($handle->isa('UR::DBI::st')) { } 0 0 0
204 0 $data_source = UR::DataSource::RDBMS->get_for_dbh($handle);
205 }
206 elsif ($handle->isa("UR::DBI::st")) {
207 0 $data_source = UR::DataSource::RDBMS->get_for_dbh($handle->{Database});
208 }
209 else {
210 0 Carp::confess("No handle passed to method!?")
211 }
212
213 0 ▶ unless ($data_source) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($data_source) 0 0 0
214 0 Carp::confess("No data source found for database handle! $handle")
215 }
216
217 0 return $data_source->_set_all_objects_saved_rolled_back();
218 }
219  
220 266 my @disable_dump_and_explain;
221 sub _disable_dump_explain
222 0 ▶{
Subroutine _disable_dump_explain: not called CC=1 SCAR=6.9
223 0 push @disable_dump_and_explain,
224 [$monitor_sql,$explain_sql_slow,$explain_sql_match];
225 0 $monitor_sql = 0;
226 0 $explain_sql_slow = '';
227 0 $explain_sql_match = '';
228 }
229  
230 sub _restore_dump_explain
231 0 ▶{
Subroutine _restore_dump_explain: not called CC=2 SCAR=17.9
232 0 ▶ if (@disable_dump_and_explain) {
Branch: 1 branch
BranchTrueFalseTotal
if (@disable_dump_and_explain) { } 0 0 0
233 0 my $vars = pop @disable_dump_and_explain;
234 0 ($monitor_sql,$explain_sql_slow,$explain_sql_match) = @$vars;
235 }
236 else {
237 0 Carp::confess("No state saved for disabled dump/explain");
238 }
239 }
240  
241 # The before_execute/after_execute subroutine pair
242 # are callbacks called by execute() and by other
243 # methods which implicitly execute a statement.
244  
245 # They use these three varaibles to track state,
246 # presuming that the callback pair cannot be nested. <!!
247  
248 266 our ($start_time, $elapsed_time);
249  
250 # This gets around a bug which prevents variables
251 # which are strings internally utf8 encoded from working with DBI
252 # as execution parameters.
253 266 ▶if ($^O eq "MSWin32" || $^O eq 'cygwin') {
Branch: 2 branches
BranchTrueFalseTotal
if ($^O eq 'MSWin32' or $^O eq 'cygwin') { } 0 266 266
elsif ($^V le v5.8.0) { } 0 266 266
Condition33%1/3
$^O eq "MSWin32" or $^O eq "cygwin"
l!l&&r!l&&!r
00266
MC/DC0%0/2
$^O eq "MSWin32" or $^O eq "cygwin"
$^O eq "MSWin32" $^O eq "cygwin"
Truth table33%1/3
A or B
A= $^O eq "MSWin32"B= $^O eq "cygwin"
ABresult
1X1
011
000
254 0 ▶ *normalize_parameter = sub { $_[0] = substr($_[0],0) };
Subroutine __ANON__: not called CC=1 SCAR=6.9
255 }
256 elsif ($^V le v5.8.0) {
257 # perl 5.6.1 utf8 module does not have a downgrade function
258 0 ▶ *normalize_parameter = sub { $_[0] = substr($_[0],0) };
Subroutine __ANON__: not called CC=1 SCAR=6.9
259 }
260 else {
261 266 require utf8;
262 266 *normalize_parameter = \&utf8::downgrade;
263 }
264  
265 sub before_execute
266 0 ▶{
Subroutine before_execute: not called CC=15 SCAR=53.4
Pod undocumented
267 #my ($dbh,$sql,@params) = @_;
268 # $dbh is optional
269
270 0 my $dbh;
271 0 ▶ $dbh = shift if ref($_[0]);
Branch: 1 branch
BranchTrueFalseTotal
if ref $_[0] 0 0 0
272
273 0 my $sql = shift;
274  
275 # Odd errors occur sometimes with values which have not gone through
276 # updgrade, downgrade or $_ = substr($_,0). The query fails w/o error.
277 # This has some connection to a language/encoding problem, and has so
278 # far only been seen with Tk, Gtk2, and XML parser derived data.
279 # Note: when this error occurs it happens with a seeminly normal Perl variable.
280 0 for (@_) {
281 0 normalize_parameter($_);
282 }
283
284 0 ▶ if ($dbh and length($explain_sql_match)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dbh and length $UR::DBI::explain_sql_match) 0 0 0
Condition0%0/3
$dbh and length $UR::DBI::explain_sql_match
!ll&&!rl&&r
000
MC/DC0%0/2
$dbh and length $UR::DBI::explain_sql_match
$dbh length $UR::DBI::explain_sql_match
Truth table0%0/3
A and B
A= $dbhB= length $UR::DBI::explain_sql_match
ABresult
0X0
100
111
285 0 for my $val ($sql,@_) {
286 0 ▶ if ($val =~ /$explain_sql_match/gi) {
Branch: 1 branch
BranchTrueFalseTotal
if ($val =~ /$UR::DBI::explain_sql_match/gi) 0 0 0
287 0 ▶ $sql_fh->print("\nEXPLAIN QUERY MATCHING /$explain_sql_match/gi"
Branch: 1 branch
BranchTrueFalseTotal
$val ne $sql ? : 0 0 0
288 . ($val ne $sql ? " (on value '$val') " : "")
289 );
290 0 ▶ if ($monitor_sql) {
Branch: 1 branch
BranchTrueFalseTotal
if ($UR::DBI::monitor_sql) { } 0 0 0
291 0 $sql_fh->print("\n");
292 }
293 else {
294 0 _print_sql_and_params($sql,@_);
295 }
296 0 ▶ if ($explain_sql_callstack) {
Branch: 1 branch
BranchTrueFalseTotal
if ($UR::DBI::explain_sql_callstack) 0 0 0
297 0 $sql_fh->print(Carp::longmess("callstack begins"),"\n");
298 }
299 0 ▶ if ($UR::DBI::explained_queries{$sql}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($UR::DBI::explained_queries{$sql}) { } 0 0 0
300 0 $sql_fh->print("(query explained above)\n");
301 }
302 else {
303 0 UR::DBI::_print_query_plan($sql,$dbh);
304 0 $UR::DBI::explained_queries{$sql} = 1;
305 }
306 0 last;
307 }
308 }
309 }
310
311 0 my $start_time = _set_start_time();
312 0 ▶ if ($monitor_sql){
Branch: 2 branches
BranchTrueFalseTotal
if ($UR::DBI::monitor_sql) { } 0 0 0
elsif ($UR::DBI::monitor_dml and not $sql =~ /^\s*select/i) { } 0 0 0
313 0 _print_sql_and_params($sql,@_);
314 0 ▶ if ($monitor_sql > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($UR::DBI::monitor_sql > 1) 0 0 0
315 0 $sql_fh->print(Carp::longmess("callstack begins"),"\n");
316 }
317 0 _print_monitor_label("EXECUTE");
318 }
319 ▶ elsif($monitor_dml && $sql !~ /^\s*select/i){
Condition0%0/3
$UR::DBI::monitor_dml and not $sql =~ /^\s*select/i
!ll&&!rl&&r
000
MC/DC0%0/2
$UR::DBI::monitor_dml and not $sql =~ /^\s*select/i
$UR::DBI::monitor_dml not $sql =~ /^\s*select/i
Truth table0%0/3
A and not(B)
A= $UR::DBI::monitor_dmlB= not $sql =~ /^\s*select/i
ABresult
0X0
100
111
320 0 _print_sql_and_params($sql,@_);
321 0 _print_monitor_label("EXECUTE");
322 0 $monitor_dml=2;
323 }
324 266 ▶ no warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
325
326 0 UR::DBI::log_sql_for_summary($sql); # $ENV{UR_DBI_SUMMARIZE_SQL}
327  
328 0 my $log_sql_str = _generate_sql_and_params_log_entry($sql, @_);
329 0 UR::DBI::log_sql($log_sql_str);
330 0 return $start_time;
331 }
332  
333 sub after_execute
334 0 ▶{
Subroutine after_execute: not called CC=3 SCAR=24.8
Pod undocumented
335 #my ($sql,@params) = @_;
336 0 my $elapsed_time = _set_elapsed_time();
337 0 ▶ if ($monitor_sql){
Branch: 2 branches
BranchTrueFalseTotal
if ($UR::DBI::monitor_sql) { } 0 0 0
elsif ($UR::DBI::monitor_dml == 2) { } 0 0 0
338 0 _print_elapsed_time();
339 }
340 elsif($monitor_dml == 2){
341 0 _print_elapsed_time();
342 0 $monitor_dml = 1;
343 }
344 0 UR::DBI::log_sql(1, ($elapsed_time)."\n");
345 0 return $elapsed_time;
346 }
347  
348 # The before_fetch/after_fetch pair are callback
349 # called by fetch() and by other methods which implicitly
350 # fetch data w/o explicitly calling fetch().
351  
352 266 our $_fetching = 0;
353  
354 0 ▶sub before_fetch {
Subroutine before_fetch: not called CC=7 SCAR=40.3
Pod undocumented
355 0 my $sth = shift;
356 0 ▶ return if @disable_dump_and_explain;
Branch: 1 branch
BranchTrueFalseTotal
if @disable_dump_and_explain 0 0 0
357 0 ▶ if ($_fetching) {
Branch: 1 branch
BranchTrueFalseTotal
if ($UR::DBI::_fetching) 0 0 0
358 0 Carp::cluck("before_fetch called after another before_fetch w/o intervening after_fetch!");
359 }
360 0 $_fetching = 1;
361 0 my $fetch_timing_arrayref = $sth->fetch_timing_arrayref;
362 0 ▶ if ($monitor_sql) {
Branch: 1 branch
BranchTrueFalseTotal
if ($UR::DBI::monitor_sql) 0 0 0
363 0 ▶ if ($fetch_timing_arrayref and @$fetch_timing_arrayref == 0) {
Branch: 2 branches
BranchTrueFalseTotal
if ($fetch_timing_arrayref and @$fetch_timing_arrayref == 0) { } 0 0 0
elsif ($UR::DBI::monitor_every_fetch) { } 0 0 0
Condition0%0/3
$fetch_timing_arrayref and @$fetch_timing_arrayref == 0
!ll&&!rl&&r
000
MC/DC0%0/2
$fetch_timing_arrayref and @$fetch_timing_arrayref == 0
$fetch_timing_arrayref @$fetch_timing_arrayref == 0
Truth table0%0/3
A and B
A= $fetch_timing_arrayrefB= @$fetch_timing_arrayref == 0
ABresult
0X0
100
111
364 0 UR::DBI::_print_monitor_label('FIRST FETCH');
365 }
366 elsif ($monitor_every_fetch) {
367 0 UR::DBI::_print_monitor_label('NTH FETCH');
368 }
369 }
370 0 return UR::DBI::_set_start_time();
371 }
372  
373 0 ▶sub after_fetch {
Subroutine after_fetch: not called CC=6 SCAR=37.4
Pod undocumented
374 0 my $sth = shift;
375 0 ▶ return if @disable_dump_and_explain;
Branch: 1 branch
BranchTrueFalseTotal
if @disable_dump_and_explain 0 0 0
376 0 $_fetching = 0;
377 0 my $fetch_timing_arrayref = $sth->fetch_timing_arrayref;
378 0 my $time;
379 0 push @$fetch_timing_arrayref, UR::DBI::_set_elapsed_time();
380 0 ▶ if ($monitor_sql) {
Branch: 1 branch
BranchTrueFalseTotal
if ($UR::DBI::monitor_sql) 0 0 0
381 0 ▶ if ($monitor_every_fetch || @$fetch_timing_arrayref == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($UR::DBI::monitor_every_fetch or @$fetch_timing_arrayref == 1) 0 0 0
Condition0%0/3
$UR::DBI::monitor_every_fetch or @$fetch_timing_arrayref == 1
l!l&&r!l&&!r
000
MC/DC0%0/2
$UR::DBI::monitor_every_fetch or @$fetch_timing_arrayref == 1
$UR::DBI::monitor_every_fetch @$fetch_timing_arrayref == 1
Truth table0%0/3
A or B
A= $UR::DBI::monitor_every_fetchB= @$fetch_timing_arrayref == 1
ABresult
1X1
011
000
382 0 $time = UR::DBI::_print_elapsed_time();
383 }
384 }
385 0 ▶ if (@$fetch_timing_arrayref == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$fetch_timing_arrayref == 1) 0 0 0
386 0 my $time = $sth->execute_time + $fetch_timing_arrayref->[0];
387 0 UR::DBI::_check_query_timing($sth->{Statement},$time,$sth->{Database},$sth->last_params);
388 }
389 0 return $time;
390 }
391  
392 0 ▶sub after_all_fetches_with_sth {
Subroutine after_all_fetches_with_sth: not called CC=8 SCAR=42.8
Pod undocumented
393 0 my $sth = shift;
394
395 0 my $fetch_timing_arrayref = $sth->fetch_timing_arrayref;
396
397 # This arrayref is set when it goes through the subclass' execute(),
398 # and is removed when we finish all fetches().
399
400 # Since a variety of things attempt to call this from the various "final"
401 # positions of an $sth we delete this so the final callback operates only once.
402 # Also, internally generated $sths which do not get executed() normally
403 # will be skipped by this check.
404
405 0 ▶ if (!$fetch_timing_arrayref) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($fetch_timing_arrayref) 0 0 0
406 # internal sth which did not go through prepare()
407 #print $sql_fh "SKIP STH\n";
408 0 return;
409 }
410 0 $sth->fetch_timing_arrayref(undef);
411
412 0 my $print_fetch_summary;
413 0 ▶ if ($monitor_sql and $sth->{Statement} =~ /select/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($UR::DBI::monitor_sql and $sth->{"Statement"} =~ /select/i) 0 0 0
Condition0%0/3
$UR::DBI::monitor_sql and $sth->{"Statement"} =~ /select/i
!ll&&!rl&&r
000
MC/DC0%0/2
$UR::DBI::monitor_sql and $sth->{"Statement"} =~ /select/i
$UR::DBI::monitor_sql $sth->{"Statement"} =~ /select/i
Truth table0%0/3
A and B
A= $UR::DBI::monitor_sqlB= $sth->{"Statement"} =~ /select/i
ABresult
0X0
100
111
414 0 $print_fetch_summary = 1;
415 0 UR::DBI::_print_monitor_label('TOTAL EXECUTE-FETCH');
416 }
417
418 0 my $time = $sth->execute_time;
419
420 0 ▶ if (@$fetch_timing_arrayref) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$fetch_timing_arrayref) { } 0 0 0
421 0 for my $fetch_time (@$fetch_timing_arrayref ) {
422 0 $time += $fetch_time;
423 }
424 0 ▶ if ($print_fetch_summary) {
Branch: 1 branch
BranchTrueFalseTotal
if ($print_fetch_summary) 0 0 0
425 0 UR::DBI::_print_monitor_time($time);
426 }
427 # since there WERE fetches, we already checked query timing
428 }
429 else {
430 0 ▶ if ($print_fetch_summary) {
Branch: 1 branch
BranchTrueFalseTotal
if ($print_fetch_summary) 0 0 0
431 0 UR::DBI::_print_monitor_time($time);
432 }
433 # since there were NOT fetches, we check query timing now
434 0 UR::DBI::_check_query_timing($sth->{Statement},$time,$sth->{Database},$sth->last_params);
435 }
436 0 return $time;
437 }
438  
439 0 ▶sub after_all_fetches_no_sth {
Subroutine after_all_fetches_no_sth: not called CC=4 SCAR=30.0
Pod undocumented
440 0 my ($sql, $time, $dbh, @params) = @_;
441 0 ▶ $time = _set_elapsed_time() unless defined $time;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $time 0 0 0
442 0 ▶ if ($monitor_sql and $sql =~ /select/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($UR::DBI::monitor_sql and $sql =~ /select/i) 0 0 0
Condition0%0/3
$UR::DBI::monitor_sql and $sql =~ /select/i
!ll&&!rl&&r
000
MC/DC0%0/2
$UR::DBI::monitor_sql and $sql =~ /select/i
$UR::DBI::monitor_sql $sql =~ /select/i
Truth table0%0/3
A and B
A= $UR::DBI::monitor_sqlB= $sql =~ /select/i
ABresult
0X0
100
111
443 0 UR::DBI::_print_monitor_label('TOTAL EXECUTE-FETCH');
444 0 UR::DBI::_print_monitor_time($time);
445 }
446 # no sth = no fetches = no query timing check done yet...
447 0 UR::DBI::_check_query_timing($sql,$time,$dbh,@params);
448 0 return $time;
449 }
450  
451  
452 266 my $__SQL_SUMMARY__ = {};
453 0 ▶sub log_sql_for_summary {
Subroutine log_sql_for_summary: not called CC=1 SCAR=6.9
Pod undocumented
454 0 my ($sql) = @_;
455 0 $__SQL_SUMMARY__->{$sql}++;
456 }
457  
458 0 ▶sub print_sql_summary {
Subroutine print_sql_summary: not called CC=2 SCAR=17.9
Pod undocumented
459 0 for my $sql (sort {$__SQL_SUMMARY__->{$b} <=> $__SQL_SUMMARY__->{$a}} keys %$__SQL_SUMMARY__) {
460 0 print STDERR join('',"********************\n", $__SQL_SUMMARY__->{$sql}, " instances of query: $sql\n");
461 }
462 }
463  
464 # These methods are called by the above.
465  
466 sub _generate_sql_and_params_log_entry
467 0 ▶{
Subroutine _generate_sql_and_params_log_entry: not called CC=4 SCAR=26.1
468  
469 0 my $sql = shift;
470  
471 266 ▶ no warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
472 0 my $sql_log_str = "\nSQL: $sql\n";
473 0 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
474 0 $sql_log_str .= "PARAMS: ";
475 $sql_log_str .= join(", ",
476 0 ▶ map { defined($_) ? "'$_'" : "NULL" }
Branch: 1 branch
BranchTrueFalseTotal
defined $_ ? : 0 0 0
477 0 ▶ map { scalar(grep { $_ } map { 128 & ord $_ } split(//, substr($_, 0, 64))) ? '<BLOBISH>' : $_ }
Branch: 1 branch
BranchTrueFalseTotal
scalar grep({$_;} map({128 & ord $_;} split(//, substr($_, 0, 64), 0))) ? : 0 0 0
478 @_ )
479 . "\n";
480 }
481  
482 0 return $sql_log_str;
483 }
484  
485 sub _print_sql_and_params
486 0 ▶{
Subroutine _print_sql_and_params: not called CC=1 SCAR=1.2
487 0 my $sql = shift;
488 0 my $entry = _generate_sql_and_params_log_entry($sql, @_);
489 266 ▶ no warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
490 0 print $sql_fh $entry;
491 }
492  
493 sub _set_start_time
494 0 ▶{
Subroutine _set_start_time: not called CC=1 SCAR=6.9
495 0 $start_time=&Time::HiRes::time();
496 }
497  
498 266 our $_print_monitor_label_or_time_is_ready_for = "label";
499 sub _print_monitor_label
500 0 ▶{
Subroutine _print_monitor_label: not called CC=1 SCAR=6.9
501 #Carp::cluck() unless $_print_monitor_label_or_time_is_ready_for eq "label";
502 0 my $time_label = shift;
503 0 $sql_fh->print("$time_label TIME: ");
504 0 $_print_monitor_label_or_time_is_ready_for = "time";
505 }
506  
507 sub _print_monitor_time
508 0 ▶{
Subroutine _print_monitor_time: not called CC=1 SCAR=6.9
509 #Carp::cluck() unless $_print_monitor_label_or_time_is_ready_for eq "time";
510 0 $sql_fh->printf( "%.4f s\n", shift);
511 0 $_print_monitor_label_or_time_is_ready_for = "label";
512 }
513  
514 sub _set_elapsed_time
515 0 ▶{
Subroutine _set_elapsed_time: not called CC=1 SCAR=6.9
516 0 $elapsed_time = &Time::HiRes::time()-$start_time;
517 }
518  
519 sub _print_elapsed_time
520 0 ▶{
Subroutine _print_elapsed_time: not called CC=1 SCAR=6.9
521 0 _print_monitor_time($elapsed_time);
522 }
523  
524 266 our $_print_check_for_slow_query = 0;
525 sub _check_query_timing
526 0 ▶{
Subroutine _check_query_timing: not called CC=11 SCAR=48.8
527 0 my ($sql,$time,$dbh,@params) = @_;
528 0 ▶ return if @disable_dump_and_explain;
Branch: 1 branch
BranchTrueFalseTotal
if @disable_dump_and_explain 0 0 0
529 0 ▶ return unless $sql =~ /select/i;
Branch: 1 branch
BranchTrueFalseTotal
unless $sql =~ /select/i 0 0 0
530 0 ▶ print $sql_fh "CHECK FOR SLOW QUERY:\n" if $_print_check_for_slow_query; # used only by a test case
Branch: 1 branch
BranchTrueFalseTotal
if $UR::DBI::_print_check_for_slow_query 0 0 0
531 0 ▶ if (length($explain_sql_slow) and $time >= $explain_sql_slow) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $UR::DBI::explain_sql_slow and $time >= $UR::DBI::explain_sql_slow) 0 0 0
Condition0%0/3
length $UR::DBI::explain_sql_slow and $time >= $UR::DBI::explain_sql_slow
!ll&&!rl&&r
000
MC/DC0%0/2
length $UR::DBI::explain_sql_slow and $time >= $UR::DBI::explain_sql_slow
length $UR::DBI::explain_sql_slow $time >= $UR::DBI::explain_sql_slow
Truth table0%0/3
A and B
A= length $UR::DBI::explain_sql_slowB= $time >= $UR::DBI::explain_sql_slow
ABresult
0X0
100
111
532 0 $sql_fh->print("EXPLAIN QUERY SLOWER THAN $explain_sql_slow seconds ($time):");
533 0 ▶ if ($monitor_sql
Branch: 1 branch
BranchTrueFalseTotal
if ($UR::DBI::monitor_sql or $UR::DBI::monitor_dml and not $sql =~ /^\s*select/i) { } 0 0 0
Condition0%0/6
$UR::DBI::monitor_sql or $UR::DBI::monitor_dml and not $sql =~ /^\s*select/i
l!l&&r!l&&!r
000
$UR::DBI::monitor_dml and not $sql =~ /^\s*select/i
!ll&&!rl&&r
000
MC/DC0%0/3
$UR::DBI::monitor_sql or $UR::DBI::monitor_dml and not $sql =~ /^\s*select/i
$UR::DBI::monitor_sql $UR::DBI::monitor_dml not $sql =~ /^\s*select/i
Truth table0%0/4
A or B and not(C)
A= $UR::DBI::monitor_sqlB= $UR::DBI::monitor_dmlC= not $sql =~ /^\s*select/i
ABCresult
1XX1
0111
00X0
0100
534 || ($monitor_dml && $sql !~ /^\s*select/i)) {
535 0 $sql_fh->print("\n");
536 }
537 else {
538 0 _print_sql_and_params($sql,@params);
539 }
540 0 ▶ if ($explain_sql_callstack) {
Branch: 1 branch
BranchTrueFalseTotal
if ($UR::DBI::explain_sql_callstack) 0 0 0
541 0 $sql_fh->print(Carp::longmess("callstack begins"),"\n");
542 }
543 0 ▶ if ($UR::DBI::explained_queries{$sql}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($UR::DBI::explained_queries{$sql}) { } 0 0 0
544 0 $sql_fh->print("(query explained above)\n");
545 }
546 else {
547 0 $UR::DBI::explained_queries{$sql} = 1;
548 0 UR::DBI::_print_query_plan($sql,$dbh);
549 }
550 }
551 }
552  
553 sub _print_query_plan
554 0 ▶{
Subroutine _print_query_plan: not called CC=4 SCAR=30.0
555 0 my ($sql,$dbh,%params) = @_;
556 0 UR::DBI::_disable_dump_explain();
557 0 $dbh->do($UR::DBI::EXPLAIN_PLAN_CLEANUP_DML);
558
559 # placeholders in explain plan queries on windows
560 # results in Oracle throwing an ORA-00600 error,
561 # likely due to interaction with DBI. Replace with
562 # literals.
563  
564 0 ▶ if ($^O eq "MSWin32" || $^O eq 'cygwin') {
Branch: 1 branch
BranchTrueFalseTotal
if ($^O eq "MSWin32" or $^O eq "cygwin") 0 0 0
Condition0%0/3
$^O eq "MSWin32" or $^O eq "cygwin"
l!l&&r!l&&!r
000
MC/DC0%0/2
$^O eq "MSWin32" or $^O eq "cygwin"
$^O eq "MSWin32" $^O eq "cygwin"
Truth table0%0/3
A or B
A= $^O eq "MSWin32"B= $^O eq "cygwin"
ABresult
1X1
011
000
565 0 $sql =~ s/\?/'1'/g;
566 }
567
568 0 ▶ $dbh->do($UR::DBI::EXPLAIN_PLAN_DML . "\n" . $sql)
Branch: 1 branch
BranchTrueFalseTotal
$dbh->do($UR::DBI::EXPLAIN_PLAN_DML . "\n" . $sql) or die 'Failed to produce query plan! ' . $dbh->errstr 0 0 0
569 or die "Failed to produce query plan! " . $dbh->errstr;
570 0 UR::DBI::Report->generate(
571 sql => [$UR::DBI::EXPLAIN_PLAN_SQL],
572 dbh => $dbh,
573 count => 0,
574 outfh => $sql_fh,
575 %params,
576 "explain-sql" => 0,
577 "echo" => 0,
578 );
579 0 $sql_fh->print("\n");
580 0 $dbh->do($UR::DBI::EXPLAIN_PLAN_CLEANUP_DML);
581 0 UR::DBI::_restore_dump_explain();
582
583 0 return 1;
584 }
585  
586  
587 ############
588 #
589 # Database handle subclass
590 #
591 ############
592  
593  
594 package UR::DBI::db;
595  
596 266 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
597 266 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
598  
599 266 our @ISA = qw(DBI::db);
600  
601 sub commit
602 0 ▶{
Subroutine commit: not called CC=4 SCAR=30.0
603 0 my $self = shift;
604  
605 # unless ($no_commit) {
606 # print "\n\n\n************* FORCIBLY SETTING NO-COMMIT FOR TESTING. This would have committeed!!!! **********\n\n\n";
607 # $no_commit = 1;
608 # }
609
610 0 ▶ if ($no_commit)
Branch: 1 branch
BranchTrueFalseTotal
if ($UR::DBI::no_commit) { } 0 0 0
611 {
612 # Respect the ->no_commit(1) setting.
613 0 UR::DBI::before_execute("commit (ignored)");
614 0 UR::DBI::after_execute;
615 0 return 1;
616 }
617 else
618 {
619 0 ▶ if(UR::DataSource->use_dummy_autogenerated_ids) {
Branch: 1 branch
BranchTrueFalseTotal
if ('UR::DataSource'->use_dummy_autogenerated_ids) { } 0 0 0
620 # Not cool...you shouldn't have dummy-ids on and no-commit off
621 # Don't commit, and notify the authorities
622 0 UR::DBI::before_execute("commit (ignored)");
623 0 $UR::Context::current->error_message('Tried to commit with dummy-ids on and no-commit off');
624 0 UR::DBI::after_execute;
625 #$UR::Context::current->send_email(
626 # To => 'example@example.edu',
627 # Subject => 'attempt to commit with dummy-ids on and no-commit off '.
628 # "by $ENV{USER} on $ENV{HOST} running ".
629 # UR::Context::Process->original_program_path." as pid $$",
630 # Message => "Call stack:\n" .Carp::longmess()
631 #);
632 } else {
633 # Commit and update the associated objects.
634 0 UR::DBI::before_execute("commit");
635 0 my $rv = $self->SUPER::commit(@_);
636 0 UR::DBI::after_execute;
637 0 ▶ if ($rv) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rv) 0 0 0
638 0 UR::DBI->commit_all_app_db_objects($self)
639 }
640 0 return $rv;
641 }
642 }
643 }
644  
645 sub commit_without_object_update
646 0 ▶{
Subroutine commit_without_object_update: not called CC=1 SCAR=6.9
647 0 UR::DBI::before_execute("commit (no object updates)");
648 0 my $rv = shift->SUPER::commit(@_);
649 0 UR::DBI::after_execute();
650 0 return $rv;
651 }
652  
653 sub rollback
654 0 ▶{
Subroutine rollback: not called CC=2 SCAR=17.9
655 0 my $self = shift;
656 0 UR::DBI::before_execute("rollback");
657 0 my $rv = $self->SUPER::rollback(@_);
658 0 UR::DBI::after_execute();
659 0 ▶ if ($rv) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rv) 0 0 0
660 0 UR::DBI->rollback_all_app_db_objects($self)
661 }
662 0 return $rv;
663 }
664  
665 sub rollback_without_object_update
666 0 ▶{
Subroutine rollback_without_object_update: not called CC=1 SCAR=6.9
667 0 UR::DBI::before_execute("rollback (w/o object updates)");
668 0 my $rv = shift->SUPER::commit(@_);
669 0 UR::DBI::after_execute();
670 0 return $rv;
671 }
672  
673 sub disconnect
674 0 ▶{
Subroutine disconnect: not called CC=4 SCAR=30.0
675 0 my $self = shift;
676 # Rollback if AutoCommit is 0. Oracle commits by default on disconnect.
677 # Rolling back when AutoCommit is on will generate a DBI warning.
678 0 ▶ if ($self->{'AutoCommit'} == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"AutoCommit"} == 0) 0 0 0
679 0 $self->rollback;
680 }
681
682 # Msg and disconnect.
683 0 UR::DBI::before_execute("disconnecting");
684 0 my $rv = $self->SUPER::disconnect(@_);
685 0 UR::DBI::after_execute();
686
687 # There doesn't seem to be anything less which
688 # sets this, but legacy tools did
689 0 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if (defined $UR::DBI::common_dbh and $self eq $UR::DBI::common_dbh) 0 0 0
Condition0%0/3
defined $UR::DBI::common_dbh and $self eq $UR::DBI::common_dbh
!ll&&!rl&&r
000
MC/DC0%0/2
defined $UR::DBI::common_dbh and $self eq $UR::DBI::common_dbh
defined $UR::DBI::common_dbh $self eq $UR::DBI::common_dbh
Truth table0%0/3
A and B
A= defined $UR::DBI::common_dbhB= $self eq $UR::DBI::common_dbh
ABresult
0X0
100
111
690 (defined $UR::DBI::common_dbh)
691 and
692 ($self eq $UR::DBI::common_dbh)
693 )
694 {
695 0 UR::DBI::before_execute("common dbh removed");
696 0 $UR::DBI::common_dbh = undef;
697 0 UR::DBI::after_execute("common dbh removed");
698 }
699 0 return $rv;
700 }
701  
702 sub prepare
703 0 ▶{
Subroutine prepare: not called CC=7 SCAR=40.3
704 0 my $self = shift;
705 0 my $sql = $_[0];
706 0 my $sth;
707
708 #print $sql_fh "PREPARE: $sql\n";
709
710 0 ▶ if ($sql =~ /^\s*(commit|rollback)\s*$/i)
Branch: 1 branch
BranchTrueFalseTotal
if ($sql =~ /^\s*(commit|rollback)\s*$/i) { } 0 0 0
711 {
712 0 ▶ unless ($sql =~ /^(commit|rollback)$/i) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($sql =~ /^(commit|rollback)$/i) 0 0 0
713 0 Carp::confess("Executing a statement with an embedded commit/rollback?\n$sql\n");
714 }
715
716 0 ▶ if ($sth = $self->SUPER::prepare(@_))
Branch: 1 branch
BranchTrueFalseTotal
if ($sth = $self->SUPER::prepare(@_)) 0 0 0
717 {
718 0 ▶ if ($1 =~ /commit/i)
Branch: 2 branches
BranchTrueFalseTotal
if ($1 =~ /commit/i) { } 0 0 0
elsif ($1 =~ /rollback/) { } 0 0 0
719 {
720 0 $UR::DBI::prepared_commit{$sth} = 1;
721 }
722 elsif ($1 =~ /rollback/)
723 {
724 0 $UR::DBI::prepared_rollback{$sth} = 1;
725 }
726 }
727 }
728 else
729 {
730 0 ▶ $sth = $self->SUPER::prepare(@_) or return;
Branch: 1 branch
BranchTrueFalseTotal
$sth = $self->SUPER::prepare(@_) or return 0 0 0
731 }
732
733 0 return $sth;
734 }
735  
736 # For newer versions of DBI, some of the $dbh->select* methods do not
737 # call execute internally, so SQL dumping and logging will not occur.
738 # These are listed below, and the bad ones are overridden.
739  
740 # selectall_hashref ok
741 # selectcol_arrayref ok
742 # selectrow_hashref ok
743  
744 # selectall_arrayref bad
745 # selectrow_arrayref bad
746 # selectrow_array bad
747  
748 sub selectall_arrayref
749 0 ▶{
Subroutine selectall_arrayref: not called CC=1 SCAR=6.9
750 0 my $self = shift;
751 0 my @p = ($_[0],@_[2..$#_]);
752 0 UR::DBI::before_execute($self,@p);
753 0 my $ar = $self->SUPER::selectall_arrayref(@_);
754 0 my $time = UR::DBI::after_execute($self,@p);
755 0 UR::DBI::after_all_fetches_no_sth($_[0],$time,$self,@p);
756 0 return $ar;
757 }
758  
759  
760 sub selectcol_arrayref
761 0 ▶{
Subroutine selectcol_arrayref: not called CC=1 SCAR=6.9
762 0 my $self = shift;
763 0 my @p = ($_[0],@_[2..$#_]);
764 0 UR::DBI::before_execute($self,@p);
765 0 UR::DBI::_disable_dump_explain();
766 0 my $ar = $self->SUPER::selectcol_arrayref(@_);
767 0 UR::DBI::_restore_dump_explain();
768 0 my $time = UR::DBI::after_execute($self,@p);
769 0 UR::DBI::after_all_fetches_no_sth($_[0],$time,$self,@p);
770 0 return $ar;
771 }
772  
773  
774 sub selectall_hashref
775 0 ▶{
Subroutine selectall_hashref: not called CC=1 SCAR=6.9
776 0 my $self = shift;
777 0 my @p = ($_[0],@_[3..$#_]);
778 0 UR::DBI::before_execute($self,@p);
779 0 UR::DBI::_disable_dump_explain();
780 0 my $ar = $self->SUPER::selectall_hashref(@_);
781 0 UR::DBI::_restore_dump_explain();
782 0 my $time = UR::DBI::after_execute($self,@p);
783 0 UR::DBI::after_all_fetches_no_sth($_[0],$time,$self,@p);
784 0 return $ar;
785 }
786  
787 sub selectrow_arrayref
788 0 ▶{
Subroutine selectrow_arrayref: not called CC=1 SCAR=6.9
789 0 my $self = shift;
790 0 my @p = ($_[0],@_[2..$#_]);
791 0 UR::DBI::before_execute($self,@p);
792 0 my $ar = $self->SUPER::selectrow_arrayref(@_);
793 0 my $time = UR::DBI::after_execute($self,@p);
794 0 UR::DBI::after_all_fetches_no_sth($_[0],$time,$self,@p);
795 0 return $ar;
796 }
797  
798 sub selectrow_array
799 0 ▶{
Subroutine selectrow_array: not called CC=2 SCAR=17.9
800 0 my $self = shift;
801 0 my @p = ($_[0],@_[2..$#_]);
802 0 UR::DBI::before_execute($self,@p);
803 0 my @a = $self->SUPER::selectrow_array(@_);
804 0 my $time = UR::DBI::after_execute($self,@p);
805 0 UR::DBI::after_all_fetches_no_sth($_[0],$time,$self,@p);
806 0 ▶ return @a if wantarray;
Branch: 1 branch
BranchTrueFalseTotal
if wantarray 0 0 0
807 0 return $a[0];
808 }
809  
810 sub DESTROY
811 0 ▶{
Subroutine DESTROY: not called CC=1 SCAR=6.9
812 0 UR::DBI::before_execute("destroying connection");
813 0 shift->SUPER::DESTROY(@_);
814 0 UR::DBI::after_execute("destroying connection");
815 }
816  
817 #########
818 #
819 # Statement handle subclass
820 #
821 #########
822  
823 package UR::DBI::st;
824  
825 266 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
826 266 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
827  
828 266 ▶use Time::HiRes;
Subroutine BEGIN: called CC=1 SCAR=0.0
829 266 ▶use Sys::Hostname;
Subroutine BEGIN: called CC=1 SCAR=0.0
830 266 ▶use Devel::GlobalDestruction;
Subroutine BEGIN: called CC=1 SCAR=0.0
831  
832 266 our @ISA = qw(DBI::st);
833  
834 798 ▶sub _mk_mutator {
Subroutine _mk_mutator: called CC=1 SCAR=0.5
835 798 my ($class, $method) = @_;
836  
837 # Make a more specific key based on the package
838 # to try not to conflict with anything else.
839 # This must start with 'private_'. See DBI docs on subclassing.
840 798 my $hash_key = join('_', 'private', lc $class, lc $method);
841 798 $hash_key =~ s/::/_/g;
842  
843 0 ▶ my $sub = sub {
Subroutine __ANON__: not called CC=3 SCAR=16.0
844 0 ▶ return if Devel::GlobalDestruction::in_global_destruction;
Branch: 1 branch
BranchTrueFalseTotal
if &Devel::GlobalDestruction::in_global_destruction() 0 0 0
845 0 my $sth = shift;
846 0 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
847 266 ▶ no warnings 'uninitialized';
Subroutine BEGIN: called CC=1 SCAR=0.0
848 0 $sth->{$hash_key} = shift;
849 }
850 266 ▶ no warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
851 0 return $sth->{$hash_key};
852 798 };
853  
854 266 ▶ no strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
855 798 *{$class . '::' . $method} = $sub;
856 }
857  
858 266 for my $method (qw(execute_time fetch_timing_arrayref last_params_arrayref)) {
859 798 __PACKAGE__->_mk_mutator($method);
860 }
861  
862 sub last_params
863 0 ▶{
Subroutine last_params: not called CC=2 SCAR=17.9
864 0 my $ret = shift->last_params_arrayref;
865 0 ▶ unless (defined $ret) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $ret) 0 0 0
866 0 $ret = [];
867 }
868 0 @{ $ret };
869 }
870  
871 sub execute
872 0 ▶{
Subroutine execute: not called CC=5 SCAR=34.0
873 0 my $sth = shift;
874
875 # (re)-initialize the timing array
876 0 ▶ if (my $a = $sth->fetch_timing_arrayref()) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $a = $sth->fetch_timing_arrayref) { } 0 0 0
877 # re-executing on a previously used $sth.
878 0 UR::DBI::after_all_fetches_with_sth($sth);
879 }
880 else {
881 # initialize the $sth on first execute.
882 0 $sth->fetch_timing_arrayref([]);
883 }
884
885 0 $sth->last_params_arrayref([@_]);
886
887 0 UR::DBI::before_execute($sth->{Database},$sth->{Statement},@_);
888 0 my $rv = $sth->SUPER::execute(@_);
889 0 UR::DBI::after_execute($sth->{Database},$sth->{Statement},@_);
890
891 # record the elapsed time for execution.
892 0 $sth->execute_time($UR::DBI::elapsed_time);
893
894 0 ▶ if ($rv)
Branch: 1 branch
BranchTrueFalseTotal
if ($rv) 0 0 0
895 {
896 0 ▶ if (my $prev = $UR::DBI::prepared_commit{$sth})
Branch: 1 branch
BranchTrueFalseTotal
if (my $prev = $UR::DBI::prepared_commit{$sth}) 0 0 0
897 {
898 0 UR::DBI->commit_all_app_db_objects($sth);
899 }
900 0 ▶ if (my $prev = $UR::DBI::prepared_rollback{$sth})
Branch: 1 branch
BranchTrueFalseTotal
if (my $prev = $UR::DBI::prepared_rollback{$sth}) 0 0 0
901 {
902 0 UR::DBI->rollback_all_app_db_objects($sth);
903 }
904  
905 }
906 0 return $rv;
907 }
908  
909  
910 sub fetchrow_array
911 0 ▶{
Subroutine fetchrow_array: not called CC=2 SCAR=17.9
912 0 my $sth = shift;
913 0 UR::DBI::before_fetch($sth,@_);
914 0 UR::DBI::_disable_dump_explain();
915 0 my @a = $sth->SUPER::fetchrow_array(@_);
916 0 UR::DBI::_restore_dump_explain();
917 0 UR::DBI::after_fetch($sth,@_);
918 0 ▶ return @a if wantarray;
Branch: 1 branch
BranchTrueFalseTotal
if wantarray 0 0 0
919 0 return $a[0];
920 }
921  
922 sub fetchrow_arrayref
923 0 ▶{
Subroutine fetchrow_arrayref: not called CC=1 SCAR=6.9
924 0 my $sth = shift;
925 0 UR::DBI::before_fetch($sth,@_);
926 0 UR::DBI::_disable_dump_explain();
927 0 my $ar = $sth->SUPER::fetchrow_arrayref(@_);
928 0 UR::DBI::_restore_dump_explain();
929 0 UR::DBI::after_fetch($sth,@_);
930 0 return $ar;
931 }
932  
933  
934 sub fetchall_arrayref
935 0 ▶{
Subroutine fetchall_arrayref: not called CC=1 SCAR=6.9
936 0 my $sth = shift;
937 0 UR::DBI::before_fetch($sth,@_);
938 0 UR::DBI::_disable_dump_explain();
939 0 my $ar = $sth->SUPER::fetchall_arrayref(@_);
940 0 UR::DBI::_restore_dump_explain();
941 0 UR::DBI::after_fetch($sth,@_);
942 0 UR::DBI::after_all_fetches_with_sth($sth,@_);
943 0 return $ar;
944 }
945  
946 sub fetchall_hashref
947 0 ▶{
Subroutine fetchall_hashref: not called CC=1 SCAR=6.9
948 0 my $sth = shift;
949 0 my @p = @_[1,$#_];
950 0 UR::DBI::before_fetch($sth,@p);
951 0 UR::DBI::_disable_dump_explain();
952 0 my $ar = $sth->SUPER::fetchall_hashref(@_);
953 0 UR::DBI::_restore_dump_explain();
954 0 UR::DBI::after_fetch($sth,@p);
955 0 UR::DBI::after_all_fetches_with_sth($sth,@_[1,$#_]);
956 0 return $ar;
957 }
958  
959 sub fetchrow_hashref
960 0 ▶{
Subroutine fetchrow_hashref: not called CC=1 SCAR=6.9
961 0 my $sth = shift;
962 0 UR::DBI::before_fetch($sth,@_);
963 0 UR::DBI::_disable_dump_explain();
964 0 my $ar = $sth->SUPER::fetchrow_hashref(@_);
965 0 UR::DBI::_restore_dump_explain();
966 0 UR::DBI::after_fetch($sth,@_);
967 0 return $ar;
968 }
969  
970  
971 0 ▶sub fetch {
Subroutine fetch: not called CC=1 SCAR=6.9
972 0 my $sth = shift;
973 0 UR::DBI::before_fetch($sth,@_);
974 0 my $rv = $sth->SUPER::fetch(@_);
975 0 UR::DBI::after_fetch($sth,@_);
976 0 return $rv;
977 }
978  
979 0 ▶sub finish {
Subroutine finish: not called CC=1 SCAR=6.9
980 0 my $sth = shift;
981 0 UR::DBI::after_all_fetches_with_sth($sth);
982 0 return $sth->SUPER::finish(@_);
983 }
984  
985 sub DESTROY
986 0 ▶{
Subroutine DESTROY: not called CC=1 SCAR=6.9
987 0 delete $UR::DBI::prepared_commit{$_[0]};
988 0 delete $UR::DBI::prepared_rollback{$_[0]};
989 #print $sql_fh "DESTROY1\n";
990 0 UR::DBI::after_all_fetches_with_sth(@_); # does nothing if called previously by finish()
991 #print $sql_fh "DESTROY2\n";
992 #Carp::cluck();
993 0 shift->SUPER::DESTROY(@_);
994 }
995  
996 266 $UR::DBI::STATEMENT_ID = $$ . '@' . hostname();
997  
998  
999 266 $UR::DBI::EXPLAIN_PLAN_DML = "explain plan set statement_id = '$UR::DBI::STATEMENT_ID' into plan_table for ";
1000  
1001  
1002 266 $UR::DBI::EXPLAIN_PLAN_SQL = qq/
1003 select
1004 LPAD(' ',p.LVL-1) || OPERATION OPERATION,
1005 OPTIONS,
1006 --(case when p.OBJECT_OWNER is null then '' else p.OBJECT_OWNER || '.' end)
1007 -- ||
1008 p.OBJECT_NAME
1009 ||
1010 (case when p.OBJECT_TYPE is null then '' else ' (' || p.OBJECT_TYPE || ')' end)
1011 "OBJECT",
1012 (case
1013 when i.table_name is not null then i.table_name
1014 || '('
1015 || index_column_names
1016 || ')'
1017 else ''
1018 end) "OBJECT_IS_ON",
1019 p.COST,
1020 p.CARDINALITY CARD,
1021 p.BYTES,
1022 p.OPTIMIZER,
1023 p.CPU_COST CPU,
1024 p.IO_COST IO,
1025 p.TEMP_SPACE TEMP,
1026 i.index_type "index_type",
1027 i.last_analyzed "index_analyzed"
1028 from
1029 (
1030 SELECT plan_table.*, level lvl
1031 FROM PLAN_TABLE
1032 CONNECT BY prior id = parent_id AND prior statement_id = statement_id
1033 START WITH id = 0
1034 AND statement_id = '$UR::DBI::STATEMENT_ID'
1035 ) p
1036 full join dual on dummy = dummy
1037 left join all_indexes i
1038 on i.index_name = p.object_name
1039 and i.owner = p.object_owner
1040 left join
1041 (
1042 select
1043 index_owner,
1044 index_name,
1045 LTRIM(MAX(SYS_CONNECT_BY_PATH(ic.column_name,',')) KEEP (DENSE_RANK LAST ORDER BY ic.column_position),',') index_column_names
1046 from (
1047 select ic.index_owner, ic.index_name, ic.column_name, ic.column_position
1048 from all_ind_columns ic
1049 ) ic
1050 group by ic.index_owner, ic.index_name
1051 connect by
1052 index_owner = prior index_owner
1053 and index_name = prior index_name
1054 and column_position = PRIOR column_position + 1
1055 start with column_position = 1
1056 ) index_columns_stringified
1057 on index_columns_stringified.index_owner = i.owner
1058 and index_columns_stringified.index_name = i.index_name
1059 where p.object_name is not null
1060 ORDER BY p.id
1061 /;
1062  
1063 266 $UR::DBI::EXPLAIN_PLAN_CLEANUP_DML = "delete from plan_table where statement_id = '$UR::DBI::STATEMENT_ID'";
1064  
1065  
1066 266 1;
1067 __END__
« blib/lib/UR/Namespace/Command/Define/Class.pm ↑ Summary lib/UR/ModuleBase.pm »