Database/Util.pm

filter: Statementstmt 3.6% 19 / 515 Branchbran 0.0% 0 / 140 Conditioncond 0.0% 0 / 24 MC/DCmcdc 0.0% 0 / 14 Subroutinesub 7.1% 5 / 70 Podpod n/a totaltotal 3.1% 24 / 763 CC 84 SCAR 32.3
CC
84
Cov
3%
insert_with_placeholders
156.0
sql_fetch_multisinglefield
42.0
sql_fetcharray_singlerow
30.0
CRAP 25.2
↑ Summary Controller.pm »
1 package LIMS::Database::Util;
2  
3 1 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 1 our $VERSION = '1.42';
6  
7 { package lims_database;
8  
9 1 require LIMS::Base;
10 1 ▶ use Class::DBI;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶ use DBI;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶ use Date::EzDate;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 1 our @ISA = qw( lims );
15
16 0 ▶ sub DESTROY {
Subroutine DESTROY: not called CC=1 SCAR=6.9
17 0 my $self = shift;
18 0 $self->disconnect_dbh;
19 0 $self->SUPER::DESTROY;
20 }
21 0 ▶ sub load_dbi {
Subroutine load_dbi: not called CC=3 SCAR=19.2
22 1 ▶ use Class::DBI::Loader;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 0 my $self = shift;
24 0 ▶ return if ((defined $ENV{ LIMS_DBLOADER }) && ($ENV{ LIMS_DBLOADER } eq 'LOADED'));
Branch: 1 branch
BranchTrueFalseTotal
if defined $ENV{"LIMS_DBLOADER"} and $ENV{"LIMS_DBLOADER"} eq "LOADED" 0 0 0
Condition0%0/3
defined $ENV{"LIMS_DBLOADER"} and $ENV{"LIMS_DBLOADER"} eq "LOADED"
!ll&&!rl&&r
000
MC/DC0%0/2
defined $ENV{"LIMS_DBLOADER"} and $ENV{"LIMS_DBLOADER"} eq "LOADED"
defined $ENV{"LIMS_DBLOADER"} $ENV{"LIMS_DBLOADER"} eq "LOADED"
Truth table0%0/3
A and B
A= defined $ENV{"LIMS_DBLOADER"}B= $ENV{"LIMS_DBLOADER"} eq "LOADED"
ABresult
0X0
100
111
25 0 my $loader = Class::DBI::Loader->new(
26 dsn => $self->get_dsn,
27 user => $self->admin_name,
28 password => $self->admin_pass,
29 namespace => "DBLoader",
30 options => { RaiseError => 1,
31 PrintError => 1,
32 AutoCommit => 0
33 }
34 );
35 0 $ENV{ LIMS_DBLOADER } = 'LOADED';
36 }
37 0 ▶ sub finish {
Subroutine finish: not called CC=1 SCAR=6.9
38 0 my $self = shift;
39 0 $self->disconnect_dbh;
40 }
41 0 ▶ sub is_unrepentant {
Subroutine is_unrepentant: not called CC=1 SCAR=6.9
42 0 my $self = shift;
43 0 $self->{ _unrepentant } = 1;
44 }
45 0 ▶ sub unrepentant {
Subroutine unrepentant: not called CC=1 SCAR=6.9
46 0 my $self = shift;
47 0 $self->{ _unrepentant };
48 }
49 0 ▶ sub print_db_errors {
Subroutine print_db_errors: not called CC=2 SCAR=17.9
50 0 my $self = shift;
51 0 ▶ return unless (my $aErrors = $self->db_error);
Branch: 1 branch
BranchTrueFalseTotal
unless my $aErrors = $self->db_error 0 0 0
52 0 print $self->get_error_string($aErrors);
53 }
54 0 ▶ sub text_errors {
Subroutine text_errors: not called CC=1 SCAR=6.9
55 0 my $self = shift;
56 0 return $self->get_error_string($self->db_error,$self->standard_error);
57 }
58 0 ▶ sub any_error {
Subroutine any_error: not called CC=3 SCAR=24.8
59 0 my $self = shift;
60 0 ▶ if (($self->standard_error) || ($self->db_error)){
Branch: 1 branch
BranchTrueFalseTotal
if ($self->standard_error or $self->db_error) { } 0 0 0
Condition0%0/3
$self->standard_error or $self->db_error
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->standard_error or $self->db_error
$self->standard_error $self->db_error
Truth table0%0/3
A or B
A= $self->standard_errorB= $self->db_error
ABresult
1X1
011
000
61 0 return 1;
62 } else {
63 0 return;
64 }
65 }
66 0 ▶ sub print_errors {
Subroutine print_errors: not called CC=1 SCAR=6.9
67 0 my $self = shift;
68 0 $self->print_db_errors;
69 0 $self->print_standard_errors;
70 }
71 0 ▶ sub clear_db_errors {
Subroutine clear_db_errors: not called CC=1 SCAR=6.9
72 0 my $self = shift;
73 0 $self->{ _db_error } = [];
74 }
75 0 ▶ sub clear_all_errors {
Subroutine clear_all_errors: not called CC=1 SCAR=6.9
76 0 my $self = shift;
77 0 $self->clear_db_errors;
78 0 $self->clear_standard_errors;
79 }
80
81 ### Database methods ###
82
83 0 ▶ sub rollback_session {
Subroutine rollback_session: not called CC=2 SCAR=17.9
84 0 my $self = shift;
85 0 my $dbh = $self->get_dbh;
86 0 eval {
87 0 $dbh->rollback;
88 };
89 0 ▶ if ($@){
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
90 0 $self->db_error($@);
91 0 $self->kill_pipeline;
92 }
93 }
94 0 ▶ sub commit_session {
Subroutine commit_session: not called CC=2 SCAR=17.9
95 0 my $self = shift;
96 0 my $dbh = $self->get_dbh;
97 0 eval {
98 0 $dbh->commit;
99 };
100 0 ▶ if ($@){
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
101 0 $self->db_error($@);
102 0 $self->rollback_session;
103 0 $self->kill_pipeline;
104 }
105 }
106 0 ▶ sub disconnect_dbh {
Subroutine disconnect_dbh: not called CC=3 SCAR=24.8
107 0 my $self = shift;
108 0 ▶ if (defined $self->{ _dbh }){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"_dbh"}) 0 0 0
109 0 my $dbh = $self->{ _dbh };
110 0 eval {
111 0 $dbh->disconnect;
112 };
113 0 ▶ if ($@){
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
114 0 $self->db_error($@);
115 0 return;
116 }
117 }
118 }
119 0 ▶ sub get_dbh {
Subroutine get_dbh: not called CC=2 SCAR=17.9
120 0 my $self = shift;
121 0 ▶ unless (defined $self->{ _dbh }){
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"_dbh"}) 0 0 0
122 0 $self->set_dbh;
123 }
124 0 $self->{ _dbh };
125 }
126 0 ▶ sub set_dbh {
Subroutine set_dbh: not called CC=2 SCAR=17.9
127 0 my $self = shift;
128 0 ▶ $self->{ _dbh } = DBI->connect($self->get_dsn, $self->admin_name, $self->admin_pass,
Branch: 1 branch
BranchTrueFalseTotal
$self->{"_dbh"} = "DBI"->connect($self->get_dsn, $self->admin_name, $self->admin_pass, {"PrintError", 0, "RaiseError", 1, "AutoCommit", 0}) or die "LIMS::Database::Util ERROR;<BR>$@" 0 0 0
129 {
130 PrintError => 0,
131 RaiseError => 1,
132 AutoCommit => 0
133 }) or die "LIMS::Database::Util ERROR;<BR>$@";
134 }
135 0 ▶ sub get_dsn {
Subroutine get_dsn: not called CC=1 SCAR=6.9
136 0 my $self = shift;
137 0 "DBI:".$self->db_driver.":".$self->database_name.":".$self->host_name;
138 }
139 0 ▶ sub db_driver {
Subroutine db_driver: not called CC=1 SCAR=6.9
140 0 'mysql'
141 }
142 0 ▶ sub host_name {
Subroutine host_name: not called CC=1 SCAR=6.9
143 0 'localhost'
144 }
145 0 ▶ sub database_name {
Subroutine database_name: not called CC=1 SCAR=6.9
146 0 'test'
147 }
148 0 ▶ sub admin_name {
Subroutine admin_name: not called CC=1 SCAR=6.9
149 0 'test'
150 }
151 0 ▶ sub admin_pass {
Subroutine admin_pass: not called CC=1 SCAR=6.9
152 0 ''
153 }
154 0 ▶ sub db_error {
Subroutine db_error: not called CC=3 SCAR=24.8
155 0 my $self = shift;
156 0 ▶ if (@_){
Branch: 1 branch
BranchTrueFalseTotal
if (@_) { } 0 0 0
157 0 my @aErrors = @_;
158 0 ▶ if (defined $self->{ _db_error }){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{'_db_error'}) { } 0 0 0
159 0 my $aErrors = $self->{ _db_error };
160 0 push (@$aErrors, @aErrors);
161 } else {
162 0 $self->{ _db_error } = \@aErrors;
163 }
164 } else {
165 0 $self->{ _db_error };
166 }
167 }
168 # this method is deprecated
169 # insert_into_table now returns the insert_id if there is one
170 0 ▶ sub last_insert_id {
Subroutine last_insert_id: not called CC=1 SCAR=6.9
171 0 my $self = shift;
172 0 my $statement = "SELECT last_insert_id()";
173 0 $self->sql_fetch_singlefield($statement);
174 }
175 0 ▶ sub prepare_sth {
Subroutine prepare_sth: not called CC=2 SCAR=17.9
176 0 my $self = shift;
177 0 my $statement = shift;
178 0 my $dbh = $self->get_dbh;
179 0 my $sth;
180 0 eval {
181 0 $sth = $dbh->prepare($statement);
182 };
183 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) { } 0 0 0
184 0 $self->db_error($statement,$@);
185 0 $self->kill_pipeline;
186 } else {
187 0 return $sth;
188 }
189 }
190 0 ▶ sub get_sth {
Subroutine get_sth: not called CC=2 SCAR=17.9
191 0 my $self = shift;
192 0 ▶ if (@_){
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
193 0 $self->set_sth($self->prepare_sth(shift));
194 }
195 0 $self->{ _sth };
196 }
197 0 ▶ sub set_sth {
Subroutine set_sth: not called CC=1 SCAR=6.9
198 0 my $self = shift;
199 0 $self->{ _sth } = shift;
200 }
201 0 ▶ sub sth_finish {
Subroutine sth_finish: not called CC=2 SCAR=17.9
202 0 my $self = shift;
203 0 my $sth = $self->{ _sth };
204 0 eval {
205 0 $sth->finish;
206 };
207 0 ▶ if ($@){
Branch: 1 branch
BranchTrueFalseTotal
if ($@) { } 0 0 0
208 0 $self->kill_pipeline($@);
209 } else {
210 0 return 1;
211 }
212 }
213 0 ▶ sub sql_fetch_bindparam {
Subroutine sql_fetch_bindparam: not called CC=4 SCAR=30.0
214 0 my $self = shift;
215 0 my $statement = shift;
216 0 my $query = shift;
217 0 my $dbh;
218 0 ▶ if (@_){
Branch: 1 branch
BranchTrueFalseTotal
if (@_) { } 0 0 0
219 0 $dbh = shift;
220 } else {
221 0 $dbh = $self->get_dbh;
222 }
223 0 my $value;
224 0 eval {
225 0 my $sth = $dbh->prepare($statement);
226 0 $sth->bind_param(1,$query);
227 0 $sth->execute();
228 0 $sth->bind_columns(undef, \$value);
229 0 $sth->fetch();
230 0 $sth->finish();
231 };
232 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) { } 0 0 0
233 0 $self->db_error($@);
234 0 $self->standard_error($statement);
235 0 ▶ if ($self->unrepentant){
Branch: 1 branch
BranchTrueFalseTotal
if ($self->unrepentant) { } 0 0 0
236 0 $self->kill_pipeline;
237 } else {
238 0 return;
239 }
240 } else {
241 0 return $value;
242 }
243 }
244 0 ▶ sub sql_fetch_singlefield {
Subroutine sql_fetch_singlefield: not called CC=4 SCAR=30.0
245 0 my $self = shift;
246 0 my $statement = shift;
247 0 my $dbh;
248 0 ▶ if (@_){
Branch: 1 branch
BranchTrueFalseTotal
if (@_) { } 0 0 0
249 0 $dbh = shift;
250 } else {
251 0 $dbh = $self->get_dbh;
252 }
253 0 my $value;
254 0 eval {
255 0 my $sth = $dbh->prepare($statement);
256 0 $sth->execute();
257 0 $sth->bind_columns(undef, \$value);
258 0 $sth->fetch();
259 0 $sth->finish();
260 };
261 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) { } 0 0 0
262 0 $self->db_error($statement,$@);
263 0 ▶ if ($self->unrepentant){
Branch: 1 branch
BranchTrueFalseTotal
if ($self->unrepentant) { } 0 0 0
264 0 $self->kill_pipeline;
265 } else {
266 0 return;
267 }
268 } else {
269 0 return $value;
270 }
271 }
272 0 ▶ sub sql_fetch_multisinglefield {
Subroutine sql_fetch_multisinglefield: not called CC=6 SCAR=37.4
273 0 my $self = shift;
274 0 my $statement = shift;
275 0 my $dbh;
276 0 ▶ if (@_){
Branch: 1 branch
BranchTrueFalseTotal
if (@_) { } 0 0 0
277 0 $dbh = shift;
278 } else {
279 0 $dbh = $self->get_dbh;
280 }
281 0 my @aValues;
282 my $value;
283 0 eval {
284 0 my $sth = $dbh->prepare($statement);
285 0 $sth->execute();
286 0 $sth->bind_columns(undef, \$value);
287 0 ▶ while($sth->fetch()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sth->fetch) 0 0 0
288 0 push @aValues, $value;
289 }
290 0 $sth->finish();
291 };
292 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) { } 0 0 0
293 0 $self->db_error($statement,$@);
294 0 ▶ if ($self->unrepentant){
Branch: 1 branch
BranchTrueFalseTotal
if ($self->unrepentant) { } 0 0 0
295 0 $self->kill_pipeline;
296 } else {
297 0 return;
298 }
299 } else {
300 0 ▶ if (wantarray()){
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray) { } 0 0 0
301 0 return @aValues;
302 } else {
303 0 return \@aValues;
304 }
305 }
306 }
307 0 ▶ sub sql_fetcharray_singlerow {
Subroutine sql_fetcharray_singlerow: not called CC=5 SCAR=34.0
308 0 my $self = shift;
309 0 my $statement = shift;
310 0 my $dbh;
311 0 ▶ if (@_){
Branch: 1 branch
BranchTrueFalseTotal
if (@_) { } 0 0 0
312 0 $dbh = shift;
313 } else {
314 0 $dbh = $self->get_dbh;
315 }
316 0 my $aResult_Row;
317 0 eval {
318 0 my $sth = $dbh->prepare($statement);
319 0 $sth->execute();
320 0 $aResult_Row = $sth->fetchrow_arrayref();
321 0 $sth->finish();
322 };
323 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) { } 0 0 0
324 0 $self->db_error($statement,$@);
325 0 ▶ if ($self->unrepentant){
Branch: 1 branch
BranchTrueFalseTotal
if ($self->unrepentant) { } 0 0 0
326 0 $self->kill_pipeline;
327 } else {
328 0 return;
329 }
330 } else {
331 0 ▶ if ($aResult_Row){
Branch: 1 branch
BranchTrueFalseTotal
if ($aResult_Row) { } 0 0 0
332 0 return $aResult_Row;
333 } else {
334 0 return;
335 }
336 }
337 }
338 # returns an array list - NOT a reference to an array
339 0 ▶ sub sql_fetchlist_singlerow {
Subroutine sql_fetchlist_singlerow: not called CC=5 SCAR=34.0
340 0 my $self = shift;
341 0 my $statement = shift;
342 0 my $dbh;
343 0 ▶ if (@_){
Branch: 1 branch
BranchTrueFalseTotal
if (@_) { } 0 0 0
344 0 $dbh = shift;
345 } else {
346 0 $dbh = $self->get_dbh;
347 }
348 0 my @aResult_Row;
349 0 eval {
350 0 my $sth = $dbh->prepare($statement);
351 0 $sth->execute();
352 0 @aResult_Row = $sth->fetchrow_array();
353 0 $sth->finish();
354 };
355 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) { } 0 0 0
356 0 $self->db_error($statement,$@);
357 0 ▶ if ($self->unrepentant){
Branch: 1 branch
BranchTrueFalseTotal
if ($self->unrepentant) { } 0 0 0
358 0 $self->kill_pipeline;
359 } else {
360 0 return;
361 }
362 } else {
363 0 ▶ if (@aResult_Row){
Branch: 1 branch
BranchTrueFalseTotal
if (@aResult_Row) { } 0 0 0
364 0 return @aResult_Row;
365 } else {
366 0 return;
367 }
368 }
369 }
370 0 ▶ sub sql_fetcharray_multirow {
Subroutine sql_fetcharray_multirow: not called CC=4 SCAR=30.0
371 0 my $self = shift;
372 0 my $statement = shift;
373 0 my $dbh;
374 0 ▶ if (@_){
Branch: 1 branch
BranchTrueFalseTotal
if (@_) { } 0 0 0
375 0 $dbh = shift;
376 } else {
377 0 $dbh = $self->get_dbh;
378 }
379 0 my $aaResults_Rows;
380 0 eval {
381 0 my $sth = $dbh->prepare($statement);
382 0 $sth->execute();
383 0 $self->set_col_names($sth->{NAME});
384 0 $aaResults_Rows = $sth->fetchall_arrayref();
385 0 $sth->finish();
386 };
387 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) { } 0 0 0
388 0 $self->db_error($statement,$@);
389 0 ▶ if ($self->unrepentant){
Branch: 1 branch
BranchTrueFalseTotal
if ($self->unrepentant) { } 0 0 0
390 0 $self->kill_pipeline;
391 } else {
392 0 return;
393 }
394 } else {
395 0 return $aaResults_Rows;
396 }
397 }
398 0 ▶ sub sql_fetch_dataframe {
Subroutine sql_fetch_dataframe: not called CC=2 SCAR=17.9
399 0 my $self = shift;
400 0 my $table = shift;
401 0 my $aColumns = shift;
402 0 my $where = shift;
403 0 my $hData = {};
404 0 for my $column (@$aColumns) {
405 0 my $statement = "SELECT $column FROM $table $where";
406 0 my $aData = $self->sql_fetch_multisinglefield($statement);
407 0 $hData->{$column} = $aData;
408 }
409 0 return $hData;
410 }
411 0 ▶ sub set_col_names {
Subroutine set_col_names: not called CC=1 SCAR=6.9
412 0 my $self = shift;
413 0 $self->{ _sth_col_names } = shift;
414 }
415 0 ▶ sub get_col_names {
Subroutine get_col_names: not called CC=1 SCAR=6.9
416 0 my $self = shift;
417 0 $self->{ _sth_col_names };
418 }
419 0 ▶ sub sql_fetch_twofieldhash {
Subroutine sql_fetch_twofieldhash: not called CC=4 SCAR=30.0
420 0 my $self = shift;
421 0 my $statement = shift;
422 0 my $dbh;
423 0 ▶ if (@_){
Branch: 1 branch
BranchTrueFalseTotal
if (@_) { } 0 0 0
424 0 $dbh = shift;
425 } else {
426 0 $dbh = $self->get_dbh;
427 }
428 0 my $aaResults = $self->sql_fetcharray_multirow($statement,$dbh); # shifting ext dbh, if passed
429 0 ▶ if ($aaResults){
Branch: 1 branch
BranchTrueFalseTotal
if ($aaResults) { } 0 0 0
430 0 my $hResults = { };
431 0 for my $aRow (@$aaResults){
432 0 $hResults->{ $$aRow[0] } = $$aRow[1];
433 }
434 0 return $hResults;
435 } else {
436 0 return;
437 }
438 }
439 0 ▶ sub sql_fetchhash_singlerow {
Subroutine sql_fetchhash_singlerow: not called CC=4 SCAR=30.0
440 0 my $self = shift;
441 0 my $statement = shift;
442 0 my $dbh;
443 0 ▶ if (@_){
Branch: 1 branch
BranchTrueFalseTotal
if (@_) { } 0 0 0
444 0 $dbh = shift;
445 } else {
446 0 $dbh = $self->get_dbh;
447 }
448 0 my $hResultRow;
449 0 eval {
450 0 my $sth = $dbh->prepare($statement);
451 0 $sth->execute();
452 0 $hResultRow = $sth->fetchrow_hashref();
453 0 $sth->finish();
454 };
455 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) { } 0 0 0
456 0 $self->db_error($statement,$@);
457 0 ▶ if ($self->unrepentant){
Branch: 1 branch
BranchTrueFalseTotal
if ($self->unrepentant) { } 0 0 0
458 0 $self->kill_pipeline;
459 } else {
460 0 return;
461 }
462 } else {
463 0 return $hResultRow;
464 }
465 }
466 0 ▶ sub sql_fetchhash_multirow {
Subroutine sql_fetchhash_multirow: not called CC=5 SCAR=34.0
467 0 my $self = shift;
468 0 my $statement = shift;
469 0 my $dbh;
470 0 ▶ if (@_){
Branch: 1 branch
BranchTrueFalseTotal
if (@_) { } 0 0 0
471 0 $dbh = shift;
472 } else {
473 0 $dbh = $self->get_dbh;
474 }
475 0 my @ahResults_Rows;
476 0 eval {
477 0 my $sth = $dbh->prepare($statement);
478 0 $sth->execute();
479 0 ▶ while(my $hResult_Row = $sth->fetchrow_hashref()) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $hResult_Row = $sth->fetchrow_hashref) 0 0 0
480 0 push @ahResults_Rows, $hResult_Row;
481 }
482 0 $sth->finish(); # we're done with this query
483 };
484 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) { } 0 0 0
485 0 $self->db_error($statement,$@);
486 0 ▶ if ($self->unrepentant){
Branch: 1 branch
BranchTrueFalseTotal
if ($self->unrepentant) { } 0 0 0
487 0 $self->kill_pipeline;
488 } else {
489 0 return;
490 }
491 } else {
492 0 return \@ahResults_Rows;
493 }
494 }
495 0 ▶ sub return_fields_array {
Subroutine return_fields_array: not called CC=5 SCAR=34.0
496 0 my $self = shift;
497 0 my $table = shift;
498 0 my $all = '';
499 0 ▶ if (@_){
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
500 0 $all = shift; # 'all' indicates return all fields
501 }
502 0 my $dbh = $self->get_dbh;
503  
504 0 my $statement = "DESCRIBE $table";
505 0 my $ahResults = $self->sql_fetchhash_multirow($statement);
506 0 my $aFields = [];
507 0 for my $hResult (@$ahResults){
508 0 ▶ if ($$hResult{Extra} eq 'auto_increment'){
Branch: 1 branch
BranchTrueFalseTotal
if ($hResult->{"Extra"} eq "auto_increment") 0 0 0
509 0 ▶ next unless ($all eq 'all');
Branch: 1 branch
BranchTrueFalseTotal
unless $all eq "all" 0 0 0
510 }
511 0 my $field = $$hResult{Field};
512 0 push (@$aFields, $field);
513 }
514 0 return $aFields;
515 }
516 0 ▶ sub return_cs_fields {
Subroutine return_cs_fields: not called CC=2 SCAR=17.9
517 0 my $self = shift;
518 0 my $table = shift;
519
520 0 ▶ if (my $aFields = $self->return_fields_array($table)){
Branch: 1 branch
BranchTrueFalseTotal
if (my $aFields = $self->return_fields_array($table)) { } 0 0 0
521 # my $cs_fields = "";
522 # for my $field (@$aFields){
523 # $cs_fields = $cs_fields.$field.",";
524 # }
525 # $cs_fields =~ s/,$//;
526 0 my $cs_fields = join(",",@$aFields);
527 0 return $cs_fields;
528 } else {
529 0 return;
530 }
531 }
532 0 ▶ sub table_fields {
Subroutine table_fields: not called CC=2 SCAR=17.9
533 0 my $self = shift;
534 0 my $table = shift;
535 0 my $hTable_Fields = $self->{ _table_fields };
536 0 ▶ unless (defined $hTable_Fields->{ $table }){
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $hTable_Fields->{$table}) 0 0 0
537 0 $hTable_Fields->{ $table } = $self->return_cs_fields($table);
538 }
539 0 $hTable_Fields->{ $table };
540 }
541 0 ▶ sub insert_into_table {
Subroutine insert_into_table: not called CC=4 SCAR=30.0
542 0 my $self = shift;
543 0 my $table = shift;
544 0 my $values = shift;
545 0 my $fields;
546 0 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) { } 0 0 0
547 0 $fields = shift;
548 } else {
549 0 $fields = $self->table_fields($table);
550 }
551 0 ▶ unless ($values && $fields) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($values and $fields) 0 0 0
Condition0%0/3
$values and $fields
!ll&&!rl&&r
000
MC/DC0%0/2
$values and $fields
$values $fields
Truth table0%0/3
A and B
A= $valuesB= $fields
ABresult
0X0
100
111
552 0 $self->standard_error('insert statement could not be completed; values or fields were undefined');
553 0 return;
554 }
555 0 my $statement = "
556 INSERT INTO $table ($fields)
557 VALUES ($values)
558 ";
559 0 return $self->execute_no_commit($statement);
560 }
561 # faster and more reliable method,
562 # especially for quoted strings and multiple inserts
563 # use insert_and_execute_placeholders where a single value
564 # will kill at any execute error
565 #Êreturns either array of insert_ids, or number of inserted rows
566 0 ▶ sub insert_with_placeholders {
Subroutine insert_with_placeholders: not called CC=12 SCAR=50.5
567 0 my $self = shift;
568 0 my $table = shift;
569 0 my $aaValues = shift;
570 0 my ($fields,$placeholders);
571 0 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) { } 0 0 0
572 0 $fields = shift;
573 0 $placeholders = shift;
574 } else {
575 0 $fields = $self->table_fields($table);
576 0 $placeholders = $self->return_table_placeholders($table);
577 }
578 0 ▶ $self->kill_pipeline('insert statement could not be completed; values, fields or placeholders were undefined')
Branch: 1 branch
BranchTrueFalseTotal
unless $aaValues and @$aaValues and $fields and $placeholders 0 0 0
Condition0%0/9
$aaValues and @$aaValues and $fields and $placeholders
!ll&&!rl&&r
000
$aaValues and @$aaValues and $fields
!ll&&!rl&&r
000
$aaValues and @$aaValues
!ll&&!rl&&r
000
MC/DC0%0/4
$aaValues and @$aaValues and $fields and $placeholders
$aaValues @$aaValues $fields $placeholders
Truth table0%0/5
A and B and C and D
A= $aaValuesB= @$aaValuesC= $fieldsD= $placeholders
ABCDresult
0XXX0
10XX0
110X0
11100
11111
579 unless ($aaValues && @$aaValues && $fields && $placeholders);
580 0 $self->set_dbh_errors(1,0);
581 0 my $statement = "INSERT INTO $table ($fields) VALUES ($placeholders)";
582 0 my $sth = $self->get_sth($statement);
583 0 my $inserts = 0;
584 0 my $insert_id;
585 0 my @aInsert_IDs = ();
586 0 for my $aValue (@$aaValues){
587 0 eval{
588 0 $sth->execute(@$aValue);
589 0 $insert_id = $sth->{'mysql_insertid'};
590 };
591 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) { } 0 0 0
592 0 $self->db_error($statement,$@);
593 0 $self->kill_pipeline;
594 } else {
595 0 ▶ if ($insert_id){
Branch: 1 branch
BranchTrueFalseTotal
if ($insert_id) 0 0 0
596 0 push(@aInsert_IDs,$insert_id);
597 }
598 0 $inserts += $sth->rows;
599 }
600 }
601 0 $self->rows_affected($inserts);
602 0 $sth->finish();
603 0 ▶ $self->commit_session unless ($self->dont_commit);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->dont_commit 0 0 0
604 0 ▶ if (($insert_id)&&(wantarray( ))){
Branch: 1 branch
BranchTrueFalseTotal
if ($insert_id and wantarray) { } 0 0 0
Condition0%0/3
$insert_id and wantarray
!ll&&!rl&&r
000
MC/DC0%0/2
$insert_id and wantarray
$insert_id wantarray
Truth table0%0/3
A and B
A= $insert_idB= wantarray
ABresult
0X0
100
111
605 0 return @aInsert_IDs;
606 } else {
607 0 return $inserts; # number of rows inserted
608 }
609 }
610 0 ▶ sub force_no_commit {
Subroutine force_no_commit: not called CC=1 SCAR=6.9
611 0 my $self = shift;
612 0 $self->{ _no_commit }++;
613 }
614 0 ▶ sub dont_commit {
Subroutine dont_commit: not called CC=1 SCAR=6.9
615 0 my $self = shift;
616 0 $self->{ _no_commit };
617 }
618 0 ▶ sub reset_commit {
Subroutine reset_commit: not called CC=1 SCAR=6.9
619 0 my $self = shift;
620 0 delete $self->{ _no_commit };
621 }
622 0 ▶ sub set_dbh_errors {
Subroutine set_dbh_errors: not called CC=1 SCAR=6.9
623 0 my $self = shift;
624 0 my $dbh = $self->get_dbh;
625 0 $dbh->{RaiseError} = shift;
626 0 $dbh->{PrintError} = shift;
627 }
628 0 ▶ sub return_table_placeholders {
Subroutine return_table_placeholders: not called CC=2 SCAR=17.9
629 0 my $self = shift;
630 0 my $table = shift;
631 0 my $values = '';
632 0 for (@{$self->return_fields_array($table)}){
633 0 $values .= '?,';
634 }
635 0 $values =~ s/,$//;
636 0 return $values;
637 }
638 0 ▶ sub delete_file {
Subroutine delete_file: not called CC=1 SCAR=6.9
639 0 my $self = shift;
640 0 my $file_id = shift;
641 0 my $statement = "
642 DELETE FROM files
643 WHERE file_id = '$file_id'
644 ";
645 0 return $self->execute_no_commit($statement);
646 }
647 0 ▶ sub delete_from {
Subroutine delete_from: not called CC=1 SCAR=6.9
648 0 my $self = shift;
649 0 my $table = shift;
650 0 my $field = shift;
651 0 my $value = shift;
652 0 my $statement = "
653 DELETE FROM $table
654 WHERE $field = '$value'
655 ";
656 0 return $self->execute_no_commit($statement);
657 }
658 # this method is deprecated - use simple_update_placeholders() instead
659 0 ▶ sub simple_table_update {
Subroutine simple_table_update: not called CC=1 SCAR=6.9
660 0 my $self = shift;
661 0 my $table = shift;
662 0 my $field = shift;
663 0 my $value = shift;
664 0 my $where = shift;
665
666 0 my $statement = "
667 UPDATE $table
668 SET $field='$value'
669 WHERE $where
670 ";
671 0 return $self->execute_no_commit($statement);
672 }
673 0 ▶ sub simple_update_placeholders {
Subroutine simple_update_placeholders: not called CC=1 SCAR=6.9
674 0 my $self = shift;
675 0 my $table = shift;
676 0 my $field = shift;
677 0 my $value = shift;
678 0 my $where = shift;
679
680 0 my $statement = "
681 UPDATE $table
682 SET $field=?
683 WHERE $where
684 ";
685
686 0 return $self->execute_no_commit($statement,[$value]);
687 }
688 # using placeholders with a single execute
689 0 ▶ sub insert_and_execute_placeholders {
Subroutine insert_and_execute_placeholders: not called CC=3 SCAR=24.8
690 0 my $self = shift;
691 0 my $table = shift;
692 0 my $values = shift;
693 0 my $fields = $self->table_fields($table);
694 0 ▶ unless ($values && $fields) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($values and $fields) 0 0 0
Condition0%0/3
$values and $fields
!ll&&!rl&&r
000
MC/DC0%0/2
$values and $fields
$values $fields
Truth table0%0/3
A and B
A= $valuesB= $fields
ABresult
0X0
100
111
695 0 $self->standard_error('insert statement could not be completed; values or fields were undefined');
696 0 return;
697 }
698 0 my $placeholders = $self->return_table_placeholders($table);
699 0 my $statement = "
700 INSERT INTO $table ($fields)
701 VALUES ($placeholders)
702 ";
703 0 return $self->execute_no_commit($statement,$values);
704 }
705 0 ▶ sub execute_no_commit {
Subroutine execute_no_commit: not called CC=5 SCAR=34.0
706 0 my $self = shift;
707 0 my $statement = shift;
708 0 $self->set_dbh_errors(1,0);
709 0 my $sth = $self->get_sth($statement);
710 0 my $insert_id;
711
712 0 ▶ if (@_){
Branch: 1 branch
BranchTrueFalseTotal
if (@_) { } 0 0 0
713 0 my $aValues = shift;
714 0 eval {
715 0 $sth->execute(@$aValues);
716 0 $insert_id = $sth->{'mysql_insertid'};
717 };
718 } else {
719 0 eval {
720 0 $sth->execute();
721 0 $insert_id = $sth->{'mysql_insertid'};
722 };
723 }
724 0 ▶ if ($@){
Branch: 1 branch
BranchTrueFalseTotal
if ($@) { } 0 0 0
725 0 $self->db_error($statement,$@);
726 0 ▶ if ($self->unrepentant){
Branch: 1 branch
BranchTrueFalseTotal
if ($self->unrepentant) { } 0 0 0
727 0 $self->kill_pipeline;
728 } else {
729 0 $self->sth_finish;
730 0 return;
731 }
732 } else {
733 0 $self->rows_affected($sth->rows);
734 0 $self->sth_finish;
735 0 ▶ if ($insert_id){
Branch: 1 branch
BranchTrueFalseTotal
if ($insert_id) { } 0 0 0
736 0 return $insert_id;
737 } else {
738 0 return $statement;
739 }
740 }
741 }
742 0 ▶ sub rows_affected {
Subroutine rows_affected: not called CC=2 SCAR=17.9
743 0 my $self = shift;
744 @_ ? $self->{ _rows_affected } = shift
745 0 ▶ : $self->{ _rows_affected };
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 0 0 0
746 }
747 0 ▶ sub execute_and_commit {
Subroutine execute_and_commit: not called CC=2 SCAR=17.9
748 0 my $self = shift;
749 0 my $result = $self->execute_no_commit(shift);
750 0 ▶ if ($self->db_error){
Branch: 1 branch
BranchTrueFalseTotal
if ($self->db_error) { } 0 0 0
751 0 $self->kill_pipeline;
752 } else {
753 0 $self->commit_session;
754 }
755 0 return $result;
756 }
757 0 ▶ sub execute_sth_values {
Subroutine execute_sth_values: not called CC=4 SCAR=30.0
758 0 my $self = shift;
759 0 my $sth = $self->get_sth;
760 0 my $dbh = $self->get_dbh;
761 0 my $insert_id;
762 0 ▶ if (@_){
Branch: 1 branch
BranchTrueFalseTotal
if (@_) { } 0 0 0
763 0 eval {
764 0 $sth->execute(@_);
765 0 $insert_id = $sth->{'mysql_insertid'};
766 };
767 0 $self->rows_affected($sth->rows);
768 0 ▶ if ($@){
Branch: 1 branch
BranchTrueFalseTotal
if ($@) { } 0 0 0
769 0 $self->standard_error($dbh->{Statement});
770 0 $self->kill_pipeline($@);
771 } else {
772 0 ▶ if ($insert_id){
Branch: 1 branch
BranchTrueFalseTotal
if ($insert_id) { } 0 0 0
773 0 return $insert_id;
774 } else {
775 0 return $dbh->{Statement};
776 }
777 }
778 } else {
779 0 $self->kill_pipeline('LIMS::Database::Util ERROR: No values were passed to execute_sth_values()');
780 }
781 }
782 0 ▶ sub do_statement {
Subroutine do_statement: not called CC=2 SCAR=17.9
783 0 my $self = shift;
784 0 my $statement = shift;
785 0 my $dbh = $self->get_dbh;
786 0 my $rows;
787 0 eval{
788 0 $rows = $dbh->do($statement);
789 };
790 0 ▶ if ($@){
Branch: 1 branch
BranchTrueFalseTotal
if ($@) { } 0 0 0
791 0 $self->standard_error($statement);
792 0 $self->kill_pipeline($@);
793 } else {
794 0 return $rows;
795 }
796 }
797 0 ▶ sub do_commit_statement {
Subroutine do_commit_statement: not called CC=1 SCAR=6.9
798 0 my $self = shift;
799 0 my $result = $self->do_statement(shift);
800 0 $self->commit_session;
801 0 return $result;
802 }
803 0 ▶ sub file_name {
Subroutine file_name: not called CC=1 SCAR=6.9
804 0 my $self = shift;
805 0 my $file = shift;
806 0 $file =~ s/.*\///;
807 0 return $file;
808 }
809 0 ▶ sub retrieve_file_blob {
Subroutine retrieve_file_blob: not called CC=1 SCAR=6.9
810 0 my $self = shift;
811 0 my $statement = shift; # sql statement to retrieve a blob
812 0 my $dbh = $self->get_dbh;
813 0 $dbh->{LongReadLen} = $self->max_blob_length; # will throw error if truncated
814 0 return $self->sql_fetch_singlefield($statement);
815 }
816 0 ▶ sub filehandle_to_blob {
Subroutine filehandle_to_blob: not called CC=2 SCAR=17.9
817 0 my $self = shift;
818 0 my $filehandle = shift;
819 0 my $file_name = shift;
820 0 my $dbh = $self->get_dbh;
821 0 binmode($filehandle);
822 0 my $file_str;
823 {
824 0 local( $/, undef ) ;
825 0 $file_str = <$filehandle>;
826 }
827 0 my $file_id = $self->insert_and_execute_placeholders('files',[$file_str,$file_name]);
828 0 ▶ if ($self->any_error) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->any_error) { } 0 0 0
829 0 $self->rollback_session;
830 0 $self->kill_pipeline;
831 } else {
832 0 $self->commit_session;
833 0 return $file_id;
834 }
835 }
836 # a db specific blob length should be set. this defaults
837 0 ▶ sub max_blob_length {
Subroutine max_blob_length: not called CC=1 SCAR=6.9
838 0 32000;
839 }
840 }
841  
842 1 1;
843  
844  
845 __END__
↑ Summary Controller.pm »