Librarian.pm

filter: Statementstmt 19.4% 29 / 149 Branchbran 0.0% 0 / 50 Conditioncond 0.0% 0 / 28 MC/DCmcdc 0.0% 0 / 17 Subroutinesub 28.0% 7 / 25 Podpod 83.3% 10 / 12 totaltotal 16.3% 46 / 281 CC 41 SCAR 34.8
CC
41
Cov
13%
_execute
90.0
_prepare
72.0
_init
42.0
CRAP 32.5
« Librarian/Statement.pm ↑ Summary Librarian/Statement/SelectExactlyOne.pm »
1 package DBIx::Librarian;
2  
3 1 require 5.005;
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 #use warnings; # needs 5.6
6 1 ▶use vars qw($VERSION);
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 1 $VERSION = '0.6';
9  
10 1 ▶use Data::Library::OnePerFile; # default archiver
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use DBIx::Librarian::Statement;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 =head1 NAME
14  
15 DBIx::Librarian - Manage SQL in repository outside code
16  
17 =head1 SYNOPSIS
18  
19 use DBIx::Librarian;
20  
21 my $dblbn = new DBIx::Librarian;
22  
23 my $data = { id => 473 };
24 eval { $dblbn->execute("lookup_employee", $data); };
25 die $@ if $@;
26 print "Employee $data->{id} is $data->{name}\n";
27  
28 $dblbn->disconnect;
29  
30 =head1 OBJECTIVES
31  
32 Separation of database logic from application logic (SQL from Perl)
33  
34 Simple interface - sacrifices some flexibility in exchange for
35 code readability and development speed
36  
37 Leave SQL syntax untouched if possible; support any extensions that are
38 supported by the underlying database
39  
40 Support transaction capability if the database allows it
41  
42 This is NOT an object-to-relational-mapping toolkit or a persistence
43 framework. For that sort of thing, see SPOPS or any of several other
44 excellent modules. The combination of DBIx::Librarian and Template
45 Toolkit or one of the other templating packages will give the basis
46 of a fairly comprehensive database-driven application framework.
47  
48 =head1 FEATURES
49  
50 =over
51  
52 =item *
53  
54 Support full complexity of Perl associative data structures
55  
56 =item *
57  
58 Multiple SQL statements chained in a single execute() invocation.
59 Use results from one call as inputs to the next.
60  
61 =item *
62  
63 Each execute() is automatically a transaction, comprising one or
64 more statements per the above. Optional delayed commit to
65 collect multiple invocations into a transaction. Note that if your
66 database doesn't support transactions (e.g. vanilla mySQL), then
67 you're still out of luck here.
68  
69 =item *
70  
71 Processing modes for select statements: exactly one row, zero-or-one,
72 multiple rows (zero to many); optional exception on receiving multiple
73 rows when expecting just one. SQL syntax is extended to provide these
74 controls.
75  
76 =item *
77  
78 Support bind variables, and on-the-fly SQL generation through substitution
79 of entire SQL fragments.
80  
81 =item *
82  
83 Supports multiple repositories for queries - currently supports
84 individual files and multiple-query files.
85  
86 =item *
87  
88 Database connection can be passed into the Librarian initializer, or
89 it will create it internally.
90  
91 =item *
92  
93 If the database connection is down when execute() is called, Librarian
94 will attempt to re-connect.
95  
96 =item *
97  
98 Sets DBI LongReadLen and LongTruncOk to allow for fetching long values.
99 Optional LONGREADLEN parameter to DBIx::Librarian::new will be passed
100 through to DBI (default 1000).
101  
102 =back
103  
104 =head1 ENVIRONMENT VARIABLES
105  
106 DBIx::Librarian will use the following:
107  
108 DBI_DSN standard DBI connection parameters
109 DBI_USER
110 DBI_PASS
111  
112  
113 =head1 DESCRIPTION
114  
115 This is for data manipulation (SELECT, INSERT, UPDATE, DELETE), not for
116 data definition (CREATE, DROP, ALTER). Some DDL statements may work
117 inside this module, but correct behavior is not guaranteed.
118  
119 Results of "SELECT1 colname FROM table", expected to return a single row:
120  
121 {
122 colname => "value"
123 }
124  
125 Access via $data->{colname}
126  
127 If more than one row is returned, raise an exception.
128  
129 Results of "SELECT* colname FROM table", expected to return multiple rows
130 (note alteration to standard SQL syntax):
131  
132 [
133 {
134 colname => "vala"
135 },
136 {
137 colname => "valb"
138 },
139 {
140 colname => "valc"
141 }
142 ]
143  
144 Access via $data->[n]->{colname}
145  
146 Results of "SELECT1 col1, col2 FROM table", expected to return a single row:
147  
148 {
149 col1 => "valA",
150 col2 => "valB",
151 }
152  
153 Access via $data->{colname}
154  
155 If more than one row is returned, raise an exception.
156  
157 Results of
158  
159 SELECT* col1 "record.col1",
160 col2 "record.col2",
161 col3 "record.col3"
162 FROM table
163  
164 expected to return multiple rows:
165  
166 {
167 record =>
168 [
169 {
170 col1 => "val1a",
171 col2 => "val2a",
172 col3 => "val3a"
173 },
174 {
175 col1 => "val1b",
176 col2 => "val2b",
177 col3 => "val3b"
178 },
179 {
180 col1 => "val1c",
181 col2 => "val2c",
182 col3 => "val3c"
183 },
184 ]
185 }
186  
187 Access via $data->{record}[n]->{colname}
188  
189 =head1 TO DO
190  
191 =over
192  
193 =item *
194  
195 Endeavor to consolidate some of this work with other similar modules
196  
197 =item *
198  
199 Optional constraint on number of rows returned by SELECT statements
200  
201 =item *
202  
203 Optional cancellation of long-running queries
204  
205 =item *
206  
207 Verbosity controls for logging during initialization and query execution;
208 tie in with DBI tracing
209  
210 =item *
211  
212 Limits on number of cached statement handles. Some databases may place
213 limits on the number of concurrent handles. Some sort of LRU stack of
214 handles would be useful for this.
215  
216 =item *
217  
218 Consider whether DBI Taint mode would be appropriate here.
219  
220 =item *
221  
222 Make sure this works properly with threads.
223  
224 =item *
225  
226 Improve regex matching for substitution variables in SQL statements so
227 they handle quoting and comments.
228  
229 =item *
230  
231 Additional SQL storage options, e.g. SQL::Catalog (store in a database -
232 should be able to keep SQL in a different database from the app data),
233 Class::Phrasebook::SQL (store in XML).
234  
235 =back
236  
237 =head1 WARNINGS
238  
239 You must call $dblbn->disconnect explicitly before your program terminates.
240  
241 This module uses strict throughout. There is one notable side-effect;
242 if you have a scalar value in a hash element:
243  
244 $data->{name} = "John"
245  
246 and you run a multi-row SELECT with the same field as a target:
247  
248 select* name,
249 department
250 from EMPLOYEE
251  
252 then you are likely to get an error like this:
253  
254 Can't use string ("John") as an ARRAY ref while "strict refs"
255 in use at .../DBIx/Librarian/Statement/SelectMany.pm line XXX.
256  
257 This is because it is trying to write values into
258  
259 $data->{name}[0]
260 $data->{name}[1]
261 etc.
262  
263 Recommended syntax for multi-row, multi-column SELECTs is:
264  
265 select* name "employee.name",
266 department "employee.dept"
267 from EMPLOYEE
268  
269 so then you can access the information via
270  
271 $data->{employee}[0]->{name}
272 $data->{employee}[0]->{dept}
273 $data->{employee}[1]->{name}
274 etc.
275  
276 =head1 METHODS
277  
278 =cut
279  
280 1 ▶use DBI;
Subroutine BEGIN: called CC=1 SCAR=0.0
281 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
282  
283 1 ▶use Log::Channel;
Subroutine BEGIN: called CC=1 SCAR=0.0
284  
285 {
286 1 my $initlog = new Log::Channel ("init");
287 0 ▶ sub initlog { $initlog->(@_) }
Subroutine initlog: not called CC=1 SCAR=6.9
Pod undocumented
288  
289 1 my $execlog = new Log::Channel ("exec");
290 0 ▶ sub execlog { $execlog->(@_) }
Subroutine execlog: not called CC=1 SCAR=6.9
Pod undocumented
291 }
292  
293 1 my %select_mode = (
294 "*" => "zero_or_more",
295 "?" => "zero_or_one",
296 "1" => "exactly_one",
297 "" => "zero_or_more",
298 );
299  
300 # defaults
301 1 my %parameters = (
302 "ARCHIVER" => undef,
303 "LIB" => undef,
304 "EXTENSION" => "sql",
305 "AUTOCOMMIT" => 1,
306 "ALLARRAYS" => 0,
307 "DBH" => undef,
308 "DBI_DSN" => undef,
309 "DBI_USER" => undef,
310 "DBI_PASS" => undef,
311 "LONGREADLEN" => 10000,
312 "MAXSELECTROWS" => 1000,
313 );
314  
315 =item B<new>
316  
317 my $dblbn = new DBIx::Librarian({ name => "value" ... });
318  
319 Supported Librarian parameters:
320  
321 ARCHIVER Reference to class responsible for caching SQL statements.
322 Default is Data::Library::OnePerFile.
323  
324 LIB If set, passed through to archiver
325  
326 EXTENSION If set, passed through to archiver
327  
328 AUTOCOMMIT If set, will commit() upon completion of all the SQL
329 statements in a tag (not after each statement).
330 If not set, the application must call commit() directly.
331 Default is set.
332  
333 ALLARRAYS If set, all bind and direct substition variables will
334 be obtained from element 0 of the named array, rather
335 than from scalars. Default is off.
336  
337 DBH If set, Librarian will use this database handle and
338 will not open one itself.
339  
340 DBI_DSN passed directly to DBI::connect
341 DBI_USER passed directly to DBI::connect
342 DBI_PASS passed directly to DBI::connect
343  
344 LONGREADLEN passed through to "LongReadLen" DBI parameter.
345 Defaults to 10000.
346  
347 MAXSELECTROWS Set to a numeric value. Limits the number of rows returned
348 by a SELECT call. Defaults to 1000.
349  
350 =cut
351  
352 0 ▶sub new {
Subroutine new: not called CC=3 SCAR=24.8
Pod documented
353 0 my ($proto, $config) = @_;
354 0 ▶ my $class = ref ($proto) || $proto;
Condition0%0/3
ref $proto || $proto
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $proto || $proto
ref $proto $proto
Truth table0%0/3
A || B
A= ref $protoB= $proto
ABresult
1X1
011
000
355  
356 0 ▶ my $self = $config || {};
Condition0%0/2
$config || {}
l!l
00
MC/DC0%0/1
$config || {}
$config
Truth table0%0/2
A
A= $config
Aresult
11
00
357  
358 0 bless ($self, $class);
359  
360 0 $self->_init;
361  
362 0 return $self;
363 }
364  
365  
366 0 ▶sub _init {
Subroutine _init: not called CC=6 SCAR=37.4
367 0 my ($self) = shift;
368  
369 # verify input params and set defaults
370 # dies on any unknown parameter
371 # fills in the default for anything that is not provided
372  
373 0 foreach my $key (keys %$self) {
374 0 ▶ if (!exists $parameters{$key}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $parameters{$key}) 0 0 0
375 0 croak "Undefined Librarian parameter $key";
376 }
377 }
378  
379 0 foreach my $key (keys %parameters) {
380 0 ▶ $self->{$key} = $parameters{$key} unless defined $self->{$key};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{$key} 0 0 0
381 }
382  
383 0 ▶ if (! defined $self->{DBH}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"DBH"}) 0 0 0
384 0 $self->_connect;
385 }
386  
387 0 $self->_init_archiver;
388 }
389  
390  
391 0 ▶sub _init_archiver {
Subroutine _init_archiver: not called CC=4 SCAR=30.0
392 0 my ($self) = shift;
393  
394 0 my $archiver = $self->{ARCHIVER};
395 0 my $config = {};
396 0 ▶ $config->{LIB} = $self->{LIB} if $self->{LIB};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"LIB"} 0 0 0
397 0 ▶ $config->{EXTENSION} = $self->{EXTENSION} if $self->{EXTENSION};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"EXTENSION"} 0 0 0
398  
399 0 ▶ if (!$archiver) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($archiver) 0 0 0
400 # use default archiver
401  
402 0 $archiver = new Data::Library::OnePerFile($config);
403 }
404  
405 0 $self->{SQL} = $archiver;
406 }
407  
408  
409 0 ▶sub _connect {
Subroutine _connect: not called CC=5 SCAR=34.0
410 0 my ($self) = shift;
411  
412 initlog sprintf ("CONNECTING to %s as %s\n",
413 $self->{DBI_DSN} || $ENV{DBI_DSN},
414 0 ▶ $self->{DBI_USER} || $ENV{DBI_USER} || "(none)");
Condition0%0/8
$self->{'DBI_DSN'} || $ENV{'DBI_DSN'}
l!l&&r!l&&!r
000
$self->{'DBI_USER'} || $ENV{'DBI_USER'} || '(none)'
l!l
00
$self->{"DBI_USER"} or $ENV{"DBI_USER"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{'DBI_DSN'} || $ENV{'DBI_DSN'}
$self->{'DBI_DSN'} $ENV{'DBI_DSN'}
MC/DC0%0/2
$self->{'DBI_USER'} || $ENV{'DBI_USER'} || '(none)'
$self->{"DBI_USER"} $ENV{"DBI_USER"}
Truth table0%0/3
A || B
A= $self->{'DBI_DSN'}B= $ENV{'DBI_DSN'}
ABresult
1X1
011
000
Truth table0%0/3
A or B
A= $self->{"DBI_USER"}B= $ENV{"DBI_USER"}
ABresult
1X1
011
000
415  
416 my $dbh = DBI->connect (
417 $self->{DBI_DSN},
418 $self->{DBI_USER},
419 $self->{DBI_PASS},
420 {
421 0 RaiseError => 0,
422 PrintError => 0,
423 AutoCommit => 0,
424 }
425 );
426  
427 0 ▶ if (!$dbh) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($dbh) 0 0 0
428 0 croak $DBI::errstr;
429 }
430  
431 0 $dbh->{LongReadLen} = $self->{LONGREADLEN};
432 0 $dbh->{LongTruncOk} = 1;
433  
434 0 $self->{DBH} = $dbh;
435 }
436  
437  
438 =item B<prepare>
439  
440 $dblbn->prepare(@tag_list);
441  
442 Retrieves, prepares and caches a list of SQL queries.
443  
444 =cut
445  
446 0 ▶sub prepare {
Subroutine prepare: not called CC=3 SCAR=24.8
Pod documented
447 0 my ($self, @tags) = @_;
448  
449 0 foreach my $tag (@tags) {
450 0 ▶ if (! $self->{SQL}->lookup($tag)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"SQL"}->lookup($tag)) 0 0 0
451 0 $self->_prepare($tag);
452 }
453 }
454 }
455  
456  
457 =item B<can>
458  
459 $dblbn->can("label");
460  
461 Returns true if a valid SQL block exists for tag "label". Side effect is
462 that the SQL is prepared for later execution.
463  
464 =cut
465  
466 0 ▶sub can {
Subroutine can: not called CC=3 SCAR=24.8
Pod documented
467 0 my ($self, $tag) = @_;
468  
469 0 ▶ return 1 if $self->{SQL}->lookup($tag);
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"SQL"}->lookup($tag) 0 0 0
470  
471 0 eval { $self->_prepare($tag) };
472 0 ▶ return 1 if $self->{SQL}->lookup($tag);
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"SQL"}->lookup($tag) 0 0 0
473  
474 0 return;
475 }
476  
477  
478  
479  
480 =item B<execute>
481  
482 $dblbn->execute("label", $data);
483  
484 $data is assumed to be a hash reference. Inputs for bind variables will
485 be obtained from $data. SELECT results will be written back to $data.
486  
487 The SQL block is obtained from the repository specified above.
488  
489 An array of two values is returned:
490 Total number of rows affected by all SQL statements (including SELECTs)
491 Reference to a list of the individual rowcounts for each statement
492  
493 May abort for various reasons, primarily Oracle errors. Will abort
494 if a SELECT is attempted without a $data target.
495  
496 =cut
497  
498 0 ▶sub execute {
Subroutine execute: not called CC=3 SCAR=24.8
Pod documented
499 0 my ($self, $tag, $data) = @_;
500  
501 0 ▶ if (! $self->is_connected) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->is_connected) 0 0 0
502 0 $self->disconnect; # clean up
503 0 $self->_connect;
504 }
505  
506 0 my $prepped = $self->{SQL}->lookup($tag);
507 0 ▶ if (!$prepped) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($prepped) 0 0 0
508 0 $prepped = $self->_prepare($tag);
509 }
510  
511 0 execlog "EXECUTE $tag\n";
512  
513 0 my @rowcounts = $self->_execute($prepped, $data);
514 0 my $totalrows = 0;
515 0 map { $totalrows += $_ } @rowcounts;
516  
517 0 return $totalrows, \@rowcounts;
518 }
519  
520  
521 0 ▶sub _prepare {
Subroutine _prepare: not called CC=8 SCAR=42.8
522 0 my ($self, $tag) = @_;
523  
524 0 my $sql = $self->{SQL}->find($tag);
525 0 ▶ croak "Unable to find $tag" unless $sql;
Branch: 1 branch
BranchTrueFalseTotal
unless $sql 0 0 0
526  
527 0 execlog "PREPARE $tag\n";
528  
529 0 my @stmts;
530  
531 # for Oracle, support PL/SQL blocks marked by BEGIN...END;
532 # a PL/SQL statement block may contain nothing else
533 0 ▶ if (($self->{DBH}->{Driver}->{Name} =~ /Oracle/i)
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'DBH'}{'Driver'}{'Name'} =~ /Oracle/i and $sql =~ /^\s*BEGIN/) { } 0 0 0
Condition0%0/3
$self->{"DBH"}{"Driver"}{"Name"} =~ /Oracle/i and $sql =~ /^\s*BEGIN/
!ll&&!rl&&r
000
MC/DC0%0/2
$self->{"DBH"}{"Driver"}{"Name"} =~ /Oracle/i and $sql =~ /^\s*BEGIN/
$self->{"DBH"}{"Driver"}{"Name"} =~ /Oracle/i $sql =~ /^\s*BEGIN/
Truth table0%0/3
A and B
A= $self->{"DBH"}{"Driver"}{"Name"} =~ /Oracle/iB= $sql =~ /^\s*BEGIN/
ABresult
0X0
100
111
534 && ($sql =~ /^\s*BEGIN/))
535 {
536 # print STDERR "\tOracle PL/SQL block\n" if $self->{TRACE};
537 # treat the entire thing as a single statement.
538 0 push @stmts, $sql;
539 } else {
540  
541 # a SQL statement is identified as a unit
542 # separated from others by whitespace
543 # containing at least one word at the beginning of a line
544 #
545 # Note that comments are NOT stripped, since the comment syntax
546 # varies between databases. But a bunch of stuff that doesn't
547 # look like a SQL statement will be silently ignored, regardless
548 # of syntax.
549  
550 @stmts =
551 0 grep { /^\s*\w/ms }
552 0 grep { !/^\s*$/ }
553 split (/\s*(\n\s*){2,}/, $sql);
554 }
555  
556 0 my @preps;
557  
558 0 foreach my $stmt (@stmts) {
559 0 ▶ if ($stmt =~ /^include\s+/io) {
Branch: 1 branch
BranchTrueFalseTotal
if ($stmt =~ /^include\s+/io) { } 0 0 0
560 0 my ($include) = $stmt =~ /^include\s+(\S+)/o;
561 0 push @preps, $include;
562 0 $self->_prepare($include);
563 } else {
564 0 ▶ if ($self->{DBH}->{Driver}->{Name} =~ /Oracle/io) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'DBH'}{'Driver'}{'Name'} =~ /Oracle/io) { } 0 0 0
565 0 $stmt =~ s/--.*//mog; # strip out Oracle comments
566 0 ▶ if ($stmt !~ /^\s*BEGIN/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($stmt =~ /^\s*BEGIN/) 0 0 0
567 0 $stmt =~ s/\s*;$//mso; # erase trailing semicolon
568 }
569 } else {
570 0 $stmt =~ s/\s*;$//mso; # erase trailing semicolon
571 0 $stmt =~ s/\s*$//mso; # erase trailing whitespace
572 }
573  
574 my $statement = new DBIx::Librarian::Statement (
575 $self->{DBH},
576 $stmt,
577 MAXSELECTROWS => $self->{MAXSELECTROWS},
578 0 NAME => $tag,
579 );
580 0 $statement->{ALLARRAYS} = $self->{ALLARRAYS};
581 0 push @preps, $statement;
582 }
583 }
584  
585 0 $self->{SQL}->cache($tag, \@preps);
586  
587 0 return \@preps;
588 }
589  
590  
591 0 ▶sub _execute {
Subroutine _execute: not called CC=9 SCAR=45.0
592 0 my ($self, $prep, $data) = @_;
593  
594 0 my @rowcounts;
595  
596 0 my $changes = 0;
597 0 foreach my $stmt_prep (@{$prep}) {
598 0 ▶ if (!ref($stmt_prep)) {
Branch: 1 branch
BranchTrueFalseTotal
if (not ref $stmt_prep) { } 0 0 0
599 # found an include
600 0 push @rowcounts, $self->execute($stmt_prep, $data);
601 } else {
602 0 eval {
603 0 my $rows = $stmt_prep->execute($data);
604 0 push @rowcounts, $rows;
605 };
606 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
607 0 ▶ if ($self->{AUTOCOMMIT} && $changes) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"AUTOCOMMIT"} and $changes) 0 0 0
Condition0%0/3
$self->{"AUTOCOMMIT"} and $changes
!ll&&!rl&&r
000
MC/DC0%0/2
$self->{"AUTOCOMMIT"} and $changes
$self->{"AUTOCOMMIT"} $changes
Truth table0%0/3
A and B
A= $self->{"AUTOCOMMIT"}B= $changes
ABresult
0X0
100
111
608 0 $self->rollback;
609 }
610 0 die $@;
611 }
612 0 ▶ $changes++ unless $stmt_prep->{IS_SELECT};
Branch: 1 branch
BranchTrueFalseTotal
unless $stmt_prep->{"IS_SELECT"} 0 0 0
613 }
614 }
615  
616 0 ▶ if ($self->{AUTOCOMMIT} && $changes) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"AUTOCOMMIT"} and $changes) 0 0 0
Condition0%0/3
$self->{"AUTOCOMMIT"} and $changes
!ll&&!rl&&r
000
MC/DC0%0/2
$self->{"AUTOCOMMIT"} and $changes
$self->{"AUTOCOMMIT"} $changes
Truth table0%0/3
A and B
A= $self->{"AUTOCOMMIT"}B= $changes
ABresult
0X0
100
111
617 # # there was at least one non-SELECT, so better commit here
618 0 $self->commit;
619 }
620  
621 0 return @rowcounts;
622 }
623  
624  
625 =item B<commit>
626  
627 Invokes commit() on the database handle. Not needed unless
628 $dblbn->delaycommit() has been called.
629  
630 =cut
631  
632 0 ▶sub commit {
Subroutine commit: not called CC=1 SCAR=6.9
Pod documented
633 0 my ($self) = @_;
634  
635 0 execlog "COMMIT\n";
636  
637 0 $self->{DBH}->commit;
638 }
639  
640 =item B<rollback>
641  
642 Invokes rollback() on the database handle. Not needed unless
643 $dblbn->delaycommit() has been called.
644  
645 =cut
646  
647 0 ▶sub rollback {
Subroutine rollback: not called CC=1 SCAR=6.9
Pod documented
648 0 my ($self) = @_;
649  
650 0 execlog "ROLLBACK\n";
651  
652 0 $self->{DBH}->rollback;
653 }
654  
655 =item B<autocommit>
656  
657 Sets the AUTOCOMMIT flag. Once set, explicit commit and rollback
658 are not needed.
659  
660 =cut
661  
662 0 ▶sub autocommit {
Subroutine autocommit: not called CC=1 SCAR=6.9
Pod documented
663 0 my ($self) = @_;
664  
665 0 $self->{AUTOCOMMIT} = 1;
666 }
667  
668 =item B<delaycommit>
669  
670 Clears the AUTOCOMMIT flag. Explicit commit and rollback will be
671 needed to apply changes to the database.
672  
673 =cut
674  
675 0 ▶sub delaycommit {
Subroutine delaycommit: not called CC=1 SCAR=6.9
Pod documented
676 0 my ($self) = @_;
677  
678 0 $self->{AUTOCOMMIT} = 0;
679 }
680  
681 =item B<disconnect>
682  
683 $dblbn->disconnect;
684  
685 Disconnect from the database. Database handle and any active statements
686 are discarded.
687  
688 =cut
689  
690 0 ▶sub disconnect {
Subroutine disconnect: not called CC=3 SCAR=24.8
Pod documented
691 0 my ($self) = @_;
692  
693 initlog sprintf ("DISCONNECT %s\n",
694 0 ▶ $self->{DBI_DSN} || $ENV{DBI_DSN});
Condition0%0/3
$self->{'DBI_DSN'} || $ENV{'DBI_DSN'}
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{'DBI_DSN'} || $ENV{'DBI_DSN'}
$self->{'DBI_DSN'} $ENV{'DBI_DSN'}
Truth table0%0/3
A || B
A= $self->{'DBI_DSN'}B= $ENV{'DBI_DSN'}
ABresult
1X1
011
000
695  
696 0 ▶ $self->{DBH}->disconnect if $self->{DBH};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"DBH"} 0 0 0
697 0 undef $self->{DBH};
698 0 $self->{SQL}->reset;
699 }
700  
701 =item B<is_connected>
702  
703 $dblbn->is_connected;
704  
705 Returns boolean indicator whether the database connection is active. This
706 depends on the $dbh->{Active} flag set by DBI, which is driver-specific.
707  
708 =cut
709  
710 0 ▶sub is_connected {
Subroutine is_connected: not called CC=3 SCAR=24.8
Pod documented
711 0 my ($self) = @_;
712  
713 0 ▶ return 1 if $self->{DBH} && $self->{DBH}->ping;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"DBH"} and $self->{"DBH"}->ping 0 0 0
Condition0%0/3
$self->{"DBH"} and $self->{"DBH"}->ping
!ll&&!rl&&r
000
MC/DC0%0/2
$self->{"DBH"} and $self->{"DBH"}->ping
$self->{"DBH"} $self->{"DBH"}->ping
Truth table0%0/3
A and B
A= $self->{"DBH"}B= $self->{"DBH"}->ping
ABresult
0X0
100
111
714 }
715  
716 0 ▶sub DESTROY {
Subroutine DESTROY: not called CC=2 SCAR=17.9
717 0 my ($self) = @_;
718  
719 0 ▶ $self->disconnect if $self->is_connected;
Branch: 1 branch
BranchTrueFalseTotal
if $self->is_connected 0 0 0
720 }
721  
722 1 1;
723  
724 =head1 LOGGING
725  
726 Declares two log channels using Log::Channel, "init" and "exec".
727 Connect and disconnect events are logged to the init channel,
728 query execution (prepare, execute, commit, rollback) to exec.
729  
730 See also the channels for DBIx::Librarian::Statement logging.
731  
732 =head1 AUTHOR
733  
734 Jason W. May <jmay@pobox.com>
735  
736 =head1 COPYRIGHT
737  
738 Copyright (C) 2001-2003 Jason W. May. All rights reserved.
739 This module is free software; you can redistribute it and/or
740 modify it under the same terms as Perl itself.
741  
742 =head1 TEST SUITE
743  
744 Under development.
745  
746 =head1 SEE ALSO
747  
748 Class:Phrasebook::SQL
749 Ima::DBI
750 SQL::Catalog
751 DBIx::SearchProfiles
752 DBIx::Abstract
753 DBIx::Recordset
754 Tie::DBI
755  
756 Relevant links stolen from SQL::Catalog documentation:
757 http://perlmonks.org/index.pl?node_id=96268&lastnode_id=96273
758 http://perlmonks.org/index.pl?node=Leashing%20DBI&lastnode_id=96268
759  
760 =cut
« Librarian/Statement.pm ↑ Summary Librarian/Statement/SelectExactlyOne.pm »