Toolkit/Connector/DBI.pm

filter: Statementstmt 94.6% 246 / 260 Branchbran 54.0% 80 / 148 Conditioncond 39.3% 13 / 33 MC/DCmcdc 0.0% 0 / 22 Subroutinesub 95.6% 22 / 23 Podpod 100% 14 / 14 totaltotal 75.0% 375 / 500 SCAR 52.8
CC
92
Cov
77%
add
18.9
update
18.9
delete
18.9
CRAP 196.7
« Toolkit/Connector/LDAP.pm ↑ Summary Toolkit/Entry.pm »
1 #!/usr/bin/perl -w
2 #
3 # Data::Toolkit::Connector::DBI
4 #
5 # Andrew Findlay
6 # Dec 2006
7 # andrew.findlay@skills-1st.co.uk
8 #
9 # $Id: DBI.pm 388 2013-08-30 15:19:23Z remotesvn $
10  
11 package Data::Toolkit::Connector::DBI;
12  
13 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 1 ▶use Clone qw(clone);
Subroutine BEGIN: called CC=1 SCAR=0.0
16 1 ▶use DBI;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 1 ▶use Data::Toolkit::Entry;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 1 ▶use Data::Toolkit::Connector;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 1 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 1 our @ISA = ("Data::Toolkit::Connector");
22  
23 =head1 NAME
24  
25 Data::Toolkit::Connector::DBI
26  
27 =head1 DESCRIPTION
28  
29 Connector for relational databases accessed through Perl's DBI methods
30  
31 =head1 SYNOPSIS
32  
33 $dbiConn = Data::Toolkit::Connector::DBI->new();
34  
35 $dbi = DBI->connect( $data_source, $username, $auth, \%attr ) or die "$@";
36  
37 $dbiConn->server( $dbi );
38  
39 $spec = $dbiConn->filterspec( "SELECT joinkey,name FROM people WHERE joinkey = '%mykey%'" );
40 $msg = $dbiConn->search( $entry );
41 while ( $entry = $dbiConn->next() ) {
42 process $entry.....
43 }
44  
45 $msg = $dbiConn->search( $entry );
46 $entry = $dbiConn->allrows();
47  
48 $spec = $dbiConn->addspec( "INSERT INTO people (joinkey,name) VALUES (%mykey%,%myname%)" );
49 $dbiConn->add( $entry );
50  
51 $spec = $dbiConn->updatespec( "UPDATE people set name = %myname% WHERE joinkey = %mykey%" );
52 $dbiConn->update( $entry );
53  
54 $spec = $dbiConn->deletespec( "DELETE FROM people WHERE joinkey = %mykey%" );
55 $msg = $dbiConn->delete( $entry );
56  
57 Data::Toolkit::Connector::DBI does not do any commits or rollbacks. If you need
58 transactions, you should call the DBI commit and rollback methods directly.
59  
60 Note that all data is supplied via placeholders rather than being interpolated
61 into the SQL strings. Thus for example, this addspec:
62  
63 INSERT INTO people (joinkey,name) VALUES (%mykey%,%myname%)
64  
65 is translated before passing to the database engine, becoming:
66  
67 INSERT INTO people (joinkey,name) VALUES (?,?)
68  
69 and the actual values of the 'mykey' and 'myname' attributes are passed as parameters.
70 This avoids all problems with quoting and SQL-injection attacks. It does make some
71 SELECT statements a bit harder to compose, particularly when you want to use LIKE
72 to do substring searches. The solution is to use CONCAT():
73  
74 SELECT joinkey,sn FROM people WHERE sn LIKE CONCAT(%firstletter%, '%%')
75  
76 The value of the 'firstletter' attribute will become a parameter when the
77 select operation is executed.
78  
79 =head1 Non-SQL databases
80  
81 Not using SQL? No problem: Data::Toolkit::Connector::DBI does not attempt to
82 understand the strings that you give it. All it does is attribute-name
83 substitution, so provided your database query language understands the '?'
84 placeholder convention it will all work.
85  
86 =head1 DEPENDENCIES
87  
88 Carp
89 Clone
90 DBI
91 DBD::CSV (for testing)
92  
93 =cut
94  
95 ########################################################################
96 # Package globals
97 ########################################################################
98  
99 1 ▶use vars qw($VERSION);
Subroutine BEGIN: called CC=1 SCAR=0.0
100 1 $VERSION = '1.0';
101  
102 # Set this non-zero for debug logging
103 #
104 1 my $debug = 0;
105  
106 ########################################################################
107 # Constructors and destructors
108 ########################################################################
109  
110 =head1 Constructor
111  
112 =head2 new
113  
114 my $dbiConn = Data::Toolkit::Connector::DBI->new();
115  
116 Creates an object of type Data::Toolkit::Connector::DBI
117  
118 =cut
119  
120 sub new {
121 1 ▶ my $class = shift;
Subroutine new: called CC=2 SCAR=7.0
Pod documented
122  
123 1 my $self = $class->SUPER::new(@_);
124 1 bless ($self, $class);
125  
126 1 ▶ carp "Data::Toolkit::Connector::DBI->new $self" if $debug;
Branch: 1 branch
BranchTrueFalseTotal
if $debug 0 1 1
127 1 return $self;
128 }
129  
130 sub DESTROY {
131 1 ▶ my $self = shift;
Subroutine DESTROY: called CC=2 SCAR=7.2
132 1 ▶ carp "Data::Toolkit::Connector::DBI Destroying $self" if $debug;
Branch: 1 branch
BranchTrueFalseTotal
if $debug 0 1 1
133 }
134  
135 ########################################################################
136 # Methods
137 ########################################################################
138  
139 =head1 Methods
140  
141 =cut
142  
143 ########################################
144  
145 =head2 server
146  
147 Define the database server for the connector to use.
148 This should be an object of type DBI
149  
150 my $res = $dbiConn->server( DBI->connect($data_source, $username, $auth) );
151  
152 Returns the object that it is passed.
153  
154 =cut
155  
156 sub server {
157 1 ▶ my $self = shift;
Subroutine server: called CC=3 SCAR=11.3
Pod documented
158 1 my $server = shift;
159  
160 1 ▶ croak "Data::Toolkit::Connector::DBI->server expects a parameter" if !$server;
Branch: 1 branch
BranchTrueFalseTotal
unless $server 0 1 1
161 1 ▶ carp "Data::Toolkit::Connector::DBI->server $self" if $debug;
Branch: 1 branch
BranchTrueFalseTotal
if $debug 0 1 1
162  
163 1 return $self->{server} = $server;
164 }
165  
166  
167  
168 ########################################
169  
170 =head2 filterspec
171  
172 Supply or fetch filterspec
173  
174 $hashref = $ldapConn->filterspec();
175 $hashref = $ldapConn->filterspec( "SELECT key,name FROM people WHERE key = '%mykey'" );
176  
177 Parameters are indicated thus: %name% - this will result in a '?'-style placeholder in
178 the SQL statement and the named attribute will be extracted from the supplied entry
179 by the search() method.
180  
181 =cut
182  
183 sub filterspec {
184 7 ▶ my $self = shift;
Subroutine filterspec: called CC=7 SCAR=19.6
Pod documented
185 7 my $filterspec = shift;
186  
187 7 ▶ carp "Data::Toolkit::Connector::DBI->filterspec $self $filterspec " if $debug;
Branch: 1 branch
BranchTrueFalseTotal
if $debug 0 7 7
188  
189 7 ▶ croak "Data::Toolkit::Connector::DBI->filterspec called before server connection opened" if !$self->{server};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"server"} 0 7 7
190  
191 # No arg supplied - just return existing setting
192 7 ▶ return $self->{filterspec} if (!$filterspec);
Branch: 1 branch
BranchTrueFalseTotal
unless $filterspec 0 7 7
193  
194 # We have a new filterspec so stash it for future reference
195 7 $self->{filterspec} = $filterspec;
196  
197 # We need to parse the spec to find the list of args that it calls for.
198 # Start by clearing the arglist and filter string
199 7 my $filter = '';
200 7 my @arglist;
201 7 $self->{search_arglist} = \@arglist;
202  
203 # Parameter names are between pairs of % characters
204 # Where we want a literal '%' it is represented by '%%'
205 # so if the search string has at least two '%' left then there is work to be done
206 7 ▶ while ($filterspec =~ /%.*%/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($filterspec =~ /%.*%/) 4 7 11
207 4 my ($left,$name,$right) = ($filterspec =~ /^([^%]*)%([a-zA-Z0-9_]*)%(.*)$/);
208 # Everything before the first % gets added to the filter
209 4 $filter .= $left;
210 4 ▶ if ($name) {
Branch: 1 branch
BranchTrueFalseTotal
if ($name) { } 2 2 4
211 # Add the name to the list of attributes needed when the search is performed
212 2 push @arglist, $name;
213 # Put the placeholder in the actual filter
214 2 $filter .= '?';
215 }
216 else {
217 # We got '%%' so add a literal '%' to the filter
218 2 $filter .= '%';
219 }
220 # The remainder of the filterspec goes round again
221 4 $filterspec = $right;
222 }
223 # Anything left in the filterspec gets appended to the filter
224 7 $filter .= $filterspec;
225  
226 # Stash the resulting string and associated list of attributes
227 7 $self->{selectstatement} = $filter;
228  
229 # Prepare the statement and stash the statement handle
230 7 $self->{search_sth} = $self->{server}->prepare( $filter );
231 7 ▶ croak "Failed to prepare filter '$filter'" if !$self->{search_sth};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"search_sth"} 0 7 7
232  
233 # Return the spec string that we were given
234 7 return $self->{filterspec};
235 }
236  
237 ########################################
238  
239 =head2 search
240  
241 Search the database.
242 If an entry is supplied, attributes from it may be used in the search.
243  
244 $msg = $dbiConn->search();
245 $msg = $dbiConn->search( $entry );
246  
247 Returns the result of the DBI execute() operation.
248 This will be false if an error occurred.
249  
250 =cut
251  
252 sub search {
253 9 ▶ my $self = shift;
Subroutine search: called CC=10 SCAR=24.6
Pod documented
254 9 my $entry = shift;
255  
256 9 ▶ croak "Data::Toolkit::Connector::DBI->search called before searchspec has been defined" if !$self->{search_sth};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"search_sth"} 0 9 9
257 9 ▶ carp "Data::Toolkit::Connector::DBI->search $self" if $debug;
Branch: 1 branch
BranchTrueFalseTotal
if $debug 0 9 9
258  
259 # Sanity check
260 9 ▶ if ($entry and not $entry->isa('Data::Toolkit::Entry')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($entry and not $entry->isa("Data::Toolkit::Entry")) 0 9 9
Condition66%2/3
$entry and not $entry->isa("Data::Toolkit::Entry")
!ll&&!rl&&r
630
MC/DC0%0/2
$entry and not $entry->isa("Data::Toolkit::Entry")
$entry not $entry->isa("Data::Toolkit::Entry")
Truth table66%2/3
A and not(B)
A= $entryB= not $entry->isa("Data::Toolkit::Entry")
ABresult
0X0
100
111
261 0 croak "Data::Toolkit::Connector::DBI->search parameter must be an entry";
262 }
263  
264 # Invalidate the current result entry
265 9 $self->{current} = undef;
266 9 $self->{currentDBRow} = undef;
267  
268 # The args to be passed to the SQL SELECT statement
269 9 my @args;
270 # The array of names that came from the filterspec
271 9 my @arglist = @{$self->{search_arglist}};
272 # Check that we have an entry to get params from
273 9 ▶ print "ARGLIST for search: ", (join '/', @arglist), "\n" if $debug;
Branch: 1 branch
BranchTrueFalseTotal
if $debug 0 9 9
274 9 ▶ if ($arglist[0] and !$entry) {
Branch: 1 branch
BranchTrueFalseTotal
if ($arglist[0] and not $entry) 0 9 9
Condition66%2/3
$arglist[0] and not $entry
!ll&&!rl&&r
630
MC/DC0%0/2
$arglist[0] and not $entry
$arglist[0] not $entry
Truth table66%2/3
A and not(B)
A= $arglist[0]B= not $entry
ABresult
0X0
100
111
275 0 croak "Data::Toolkit::Connector::DBI->search requires an entry when the searchspec includes parameters";
276 }
277  
278 # Extract the args from the entry
279 9 my $arg;
280 9 foreach $arg (@arglist) {
281 3 my $value = $entry->get($arg);
282 3 ▶ croak "search spec calls for a '$arg' attribute but the entry does not have one" if !$value;
Branch: 1 branch
BranchTrueFalseTotal
unless $value 0 3 3
283 # We only use the first value from the list.
284 # This is permitted to bu undef or the null string.
285 3 $value = $value->[0];
286 3 push @args, $value;
287 }
288  
289 # Start the search and return the statement handle having stashed a copy internally
290 9 return $self->{searchresult} = $self->{search_sth}->execute( @args );
291 }
292  
293  
294  
295 ########################################
296  
297 =head2 next
298  
299 Return the next entry from the SQL search as a Data::Toolkit::Entry object.
300 Optionally apply a map to the data.
301  
302 Updates the "current" entry (see "current" method description below).
303  
304 my $entry = $dbConn->next();
305 my $entry = $dbConn->next( $map );
306  
307 The result is a Data::Toolkit::Entry object if there is data left to be read,
308 otherwise it is undef.
309  
310 =cut
311  
312 sub next {
313 8 ▶ my $self = shift;
Subroutine next: called CC=7 SCAR=19.9
Pod documented
314 8 my $map = shift;
315  
316 8 ▶ carp "Data::Toolkit::Connector::DBI->next $self" if $debug;
Branch: 1 branch
BranchTrueFalseTotal
if $debug 0 8 8
317  
318 # Invalidate the old 'current entry' in case we have to return early
319 8 $self->{current} = undef;
320  
321 # Sanity check
322 8 ▶ croak "Data::Toolkit::Connector::DBI->next called but no search has been started" if !$self->{search_sth};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"search_sth"} 0 8 8
323  
324 # Pull out the next row from the database
325 8 my $dbRow = $self->{search_sth}->fetchrow_hashref("NAME_lc");
326 8 ▶ return undef if !$dbRow;
Branch: 1 branch
BranchTrueFalseTotal
unless $dbRow 2 6 8
327  
328 # Build an entry
329 6 my $entry = Data::Toolkit::Entry->new();
330  
331 # Now step through the list of columns and assign data to attributes in the entry
332 6 my $attrib;
333  
334 6 foreach $attrib (keys %$dbRow) {
335 14 $entry->set( $attrib, [ $dbRow->{$attrib} ] );
336 }
337  
338 # Save this as the current entry
339 6 $self->{current} = $entry;
340 6 $self->{currentRow} = $dbRow;
341  
342 6 ▶ carp "Data::Toolkit::Connector::DBI->next using row: ".(join ',', (keys %$dbRow)) if $debug;
Branch: 1 branch
BranchTrueFalseTotal
if $debug 0 6 6
343 # Do we have a map to apply?
344 6 ▶ if ($map) {
Branch: 1 branch
BranchTrueFalseTotal
if ($map) 0 6 6
345 0 return $entry->map($map);
346 }
347  
348 6 return $entry;
349 }
350  
351 ########################################
352  
353 =head2 allrows
354  
355 Merges the data from all rows returned by the SQL search into a single
356 Data::Toolkit::Entry object, so that each attribute has multiple values.
357 Optionally apply a map to the data.
358  
359 After this method if called, the "current" entry will be empty
360 (see "current" method description below).
361  
362 my $entry = $dbConn->allrows();
363 my $entry = $dbConn->allrows( $map );
364  
365 The result is a Data::Toolkit::Entry object if there is data left to be read,
366 otherwise it is undef.
367  
368 =cut
369  
370 sub allrows {
371 1 ▶ my $self = shift;
Subroutine allrows: called CC=8 SCAR=21.3
Pod documented
372 1 my $map = shift;
373  
374 1 ▶ carp "Data::Toolkit::Connector::DBI->allrows $self" if $debug;
Branch: 1 branch
BranchTrueFalseTotal
if $debug 0 1 1
375  
376 # Invalidate the old 'current entry' in case we have to return early
377 1 $self->{current} = undef;
378 # We will use up all the rows so clear this
379 1 $self->{currentRow} = undef;
380  
381 # Sanity check
382 1 ▶ croak "Data::Toolkit::Connector::DBI->allrows called but no search has been started" if !$self->{search_sth};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"search_sth"} 0 1 1
383  
384 # Pull out the next row from the database
385 1 my $dbRow = $self->{search_sth}->fetchrow_hashref("NAME_lc");
386 1 ▶ return undef if !$dbRow;
Branch: 1 branch
BranchTrueFalseTotal
unless $dbRow 0 1 1
387  
388 # Build an entry
389 1 my $entry = Data::Toolkit::Entry->new();
390  
391 # While the search returns rows of data, slurp them up and add the values to the entry
392 1 my $count = 0;
393 1 ▶ while ($dbRow) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dbRow) 2 1 3
394 # Step through the list of columns and add data to attributes in the entry
395 2 my $attrib;
396 2 $count++;
397  
398 2 foreach $attrib (keys %$dbRow) {
399 4 $entry->add( $attrib, [ $dbRow->{$attrib} ] );
400 }
401  
402 # Fetch the next row
403 2 $dbRow = $self->{search_sth}->fetchrow_hashref("NAME_lc");
404 }
405  
406 # Save this as the current entry
407 1 $self->{current} = $entry;
408  
409 1 ▶ carp "Data::Toolkit::Connector::DBI->allrows found $count rows" if $debug;
Branch: 1 branch
BranchTrueFalseTotal
if $debug 0 1 1
410  
411 # Do we have a map to apply?
412 1 ▶ if ($map) {
Branch: 1 branch
BranchTrueFalseTotal
if ($map) 0 1 1
413 0 return $entry->map($map);
414 }
415  
416 1 return $entry;
417 }
418  
419  
420 ########################################
421  
422 =head2 current
423  
424 Return the current entry in the list of search results.
425 The current entry is not defined until the "next" method has been called after a search.
426  
427 $entry = $dbConn->current();
428  
429 =cut
430  
431 sub current {
432 0 ▶ my $self = shift;
Subroutine current: not called CC=2 SCAR=17.9
Pod documented
433  
434 0 ▶ carp "Data::Toolkit::Connector::DBI->current $self" if $debug;
Branch: 1 branch
BranchTrueFalseTotal
if $debug 0 0 0
435  
436 0 return $self->{current};
437 }
438  
439  
440  
441 ########################################
442  
443 =head2 addspec
444  
445 Supply or fetch spec for add
446  
447 $spec = $dbiConn->addspec();
448 $spec = $dbiConn->addspec( "INSERT INTO people (joinkey,name) VALUES (%key%, %myname%)" );
449  
450 Parameters are indicated thus: %name% - this will result in a '?'-style placeholder in
451 the SQL statement and the named attribute will be extracted from the supplied entry
452 by the add() method. Note that these parameters should not be quoted - they are not
453 passed as text to the database engine.
454  
455 =cut
456  
457 sub addspec {
458 1 ▶ my $self = shift;
Subroutine addspec: called CC=7 SCAR=19.8
Pod documented
459 1 my $addspec = shift;
460  
461 1 ▶ carp "Data::Toolkit::Connector::DBI->addspec $self $addspec " if $debug;
Branch: 1 branch
BranchTrueFalseTotal
if $debug 0 1 1
462  
463 1 ▶ croak "Data::Toolkit::Connector::DBI->addspec called before server connection opened" if !$self->{server};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"server"} 0 1 1
464  
465 # No arg supplied - just return existing setting
466 1 ▶ return $self->{addspec} if (!$addspec);
Branch: 1 branch
BranchTrueFalseTotal
unless $addspec 0 1 1
467  
468 # We have a new addspec so stash it for future reference
469 1 $self->{addspec} = $addspec;
470  
471 # We need to parse the spec to find the list of args that it calls for.
472 # Start by clearing the arglist and add string
473 1 my $add = '';
474 1 my @arglist;
475 1 $self->{add_arglist} = \@arglist;
476  
477 # Parameter names are between pairs of % characters
478 # Where we want a literal '%' it is represented by '%%'
479 # so if the add string has at least two '%' left then there is work to be done
480 1 ▶ while ($addspec =~ /%.*%/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($addspec =~ /%.*%/) 3 1 4
481 3 my ($left,$name,$right) = ($addspec =~ /^([^%]*)%([a-zA-Z0-9_]*)%(.*)$/);
482 # Everything before the first % gets added to the add string
483 3 $add .= $left;
484 3 ▶ if ($name) {
Branch: 1 branch
BranchTrueFalseTotal
if ($name) { } 3 0 3
485 # Add the name to the list of attributes needed when the add is performed
486 3 push @arglist, $name;
487 # Put the placeholder in the actual add string
488 3 $add .= '?';
489 }
490 else {
491 # We got '%%' so add a literal '%' to the add string
492 0 $add .= '%';
493 }
494 # The remainder of the addspec goes round again
495 3 $addspec = $right;
496 }
497 # Anything left in the addspec gets appended to the add string
498 1 $add .= $addspec;
499  
500 # Stash the resulting string
501 1 $self->{add_statement} = $add;
502  
503 # Prepare the statement and stash the statement handle
504 1 $self->{add_sth} = $self->{server}->prepare( $add );
505 1 ▶ croak "Failed to prepare add '$add'" if !$self->{add_sth};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"add_sth"} 0 1 1
506  
507 # Return the spec string that we were given
508 1 return $self->{addspec};
509 }
510  
511 ########################################
512  
513 =head2 add
514  
515 Update a row in the database using data from a source entry and an optional map.
516 If a map is supplied, it is used to transform data from the source entry before
517 it is applied to the database operation.
518  
519 Returns the result of the DBI execute operation.
520  
521 $msg = $dbConn->add($sourceEntry);
522 $msg = $dbConn->add($sourceEntry, $addMap);
523  
524 A suitable add operation must have been defined using the addspec() method
525 before add() is called:
526  
527 $spec = $dbiConn->addspec( "INSERT INTO people (joinkey,name) VALUES (%key%, %myname%)" );
528 $msg = $dbiConn->add( $entry );
529  
530 NOTE that only the first value of a given attribute is used, as relational databases expect
531 a single value for each column in a given row.
532  
533 =cut
534  
535 sub add {
536 1 ▶ my $self = shift;
Subroutine add: called CC=13 SCAR=29.4
Pod documented
537 1 my $source = shift;
538 1 my $map = shift;
539  
540 1 ▶ croak "Data::Toolkit::Connector::DBI->add called before addspec has been defined" if !$self->{add_sth};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"add_sth"} 0 1 1
541 1 ▶ croak "Data::Toolkit::Connector::DBI->add first parameter should be a Data::Toolkit::Entry"
Branch: 1 branch
BranchTrueFalseTotal
if $source and not $source->isa("Data::Toolkit::Entry") 0 1 1
Condition33%1/3
$source and not $source->isa("Data::Toolkit::Entry")
!ll&&!rl&&r
010
MC/DC0%0/2
$source and not $source->isa("Data::Toolkit::Entry")
$source not $source->isa("Data::Toolkit::Entry")
Truth table33%1/3
A and not(B)
A= $sourceB= not $source->isa("Data::Toolkit::Entry")
ABresult
0X0
100
111
542 if ($source and !$source->isa('Data::Toolkit::Entry'));
543 1 ▶ croak "Data::Toolkit::Connector::DBI->add second parameter should be a Data::Toolkit::Map"
Branch: 1 branch
BranchTrueFalseTotal
if $map and not $map->isa("Data::Toolkit::Map") 0 1 1
Condition33%1/3
$map and not $map->isa("Data::Toolkit::Map")
!ll&&!rl&&r
100
MC/DC0%0/2
$map and not $map->isa("Data::Toolkit::Map")
$map not $map->isa("Data::Toolkit::Map")
Truth table33%1/3
A and not(B)
A= $mapB= not $map->isa("Data::Toolkit::Map")
ABresult
0X0
100
111
544 if ($map and !$map->isa('Data::Toolkit::Map'));
545  
546 1 ▶ carp "Data::Toolkit::Connector::DBI->add $self $source" if $debug;
Branch: 1 branch
BranchTrueFalseTotal
if $debug 0 1 1
547  
548 # Apply the map if we have one
549 1 ▶ $source = $source->map($map) if $map;
Branch: 1 branch
BranchTrueFalseTotal
if $map 0 1 1
550  
551 # The args to be passed to the SQL UPDATE statement
552 1 my @args;
553 # The array of names that came from the addspec
554 1 my @arglist = @{$self->{add_arglist}};
555 # Check that we have an entry to get params from
556 1 ▶ print "ARGLIST for add: ", (join '/', @arglist), "\n" if $debug;
Branch: 1 branch
BranchTrueFalseTotal
if $debug 0 1 1
557 1 ▶ if ($arglist[0] and !$source) {
Branch: 1 branch
BranchTrueFalseTotal
if ($arglist[0] and not $source) 0 1 1
Condition33%1/3
$arglist[0] and not $source
!ll&&!rl&&r
010
MC/DC0%0/2
$arglist[0] and not $source
$arglist[0] not $source
Truth table33%1/3
A and not(B)
A= $arglist[0]B= not $source
ABresult
0X0
100
111
558 0 croak "Data::Toolkit::Connector::DBI->add requires an entry when the addspec includes parameters";
559 }
560  
561 # Extract the args from the entry
562 1 my $arg;
563 1 foreach $arg (@arglist) {
564 3 my $value = $source->get($arg);
565 3 ▶ croak "add spec calls for a '$arg' attribute but the entry does not have one" if !$value;
Branch: 1 branch
BranchTrueFalseTotal
unless $value 0 3 3
566 # We only use the first value from the list.
567 # This is permitted to be undef or the null string.
568 3 $value = $value->[0];
569 3 push @args, $value;
570 }
571  
572 # Start the operation and return the statement handle having stashed a copy internally
573 1 return $self->{add_result} = $self->{add_sth}->execute( @args );
574 }
575  
576  
577 ########################################
578  
579 =head2 updatespec
580  
581 Supply or fetch spec for update
582  
583 $spec = $dbiConn->updatespec();
584 $spec = $dbiConn->updatespec( "UPDATE people set name = %myname% WHERE joinkey = %mykey%" );
585  
586 Parameters are indicated thus: %name% - this will result in a '?'-style placeholder in
587 the SQL statement and the named attribute will be extracted from the supplied entry
588 by the update() method.
589  
590 =cut
591  
592 sub updatespec {
593 1 ▶ my $self = shift;
Subroutine updatespec: called CC=8 SCAR=21.3
Pod documented
594 1 my $updatespec = shift;
595  
596 1 ▶ carp "Data::Toolkit::Connector::DBI->updatespec $self $updatespec " if $debug;
Branch: 1 branch
BranchTrueFalseTotal
if $debug 0 1 1
597  
598 1 ▶ croak "Data::Toolkit::Connector::DBI->updatespec called before server connection opened" if !$self->{server};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"server"} 0 1 1
599  
600 # No arg supplied - just return existing setting
601 1 ▶ return $self->{updatespec} if (!$updatespec);
Branch: 1 branch
BranchTrueFalseTotal
unless $updatespec 0 1 1
602  
603 # We have a new updatespec so stash it for future reference
604 1 $self->{updatespec} = $updatespec;
605  
606 # We need to parse the spec to find the list of args that it calls for.
607 # Start by clearing the arglist and update string
608 1 my $update = '';
609 1 my @arglist;
610 1 $self->{update_arglist} = \@arglist;
611  
612 # Parameter names are between pairs of % characters
613 # Where we want a literal '%' it is represented by '%%'
614 # so if the update string has at least two '%' left then there is work to be done
615 1 ▶ while ($updatespec =~ /%.*%/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($updatespec =~ /%.*%/) 3 1 4
616 3 my ($left,$name,$right) = ($updatespec =~ /^([^%]*)%([a-zA-Z0-9_]*)%(.*)$/);
617 # Everything before the first % gets added to the update string
618 3 $update .= $left;
619 3 ▶ if ($name) {
Branch: 1 branch
BranchTrueFalseTotal
if ($name) { } 3 0 3
620 # Add the name to the list of attributes needed when the update is performed
621 3 push @arglist, $name;
622 # Put the placeholder in the actual update string
623 3 $update .= '?';
624 }
625 else {
626 # We got '%%' so add a literal '%' to the update string
627 0 $update .= '%';
628 }
629 # The remainder of the updatespec goes round again
630 3 $updatespec = $right;
631 }
632 # Anything left in the updatespec gets appended to the update string
633 1 $update .= $updatespec;
634  
635 # Stash the resulting string and associated list of attributes
636 1 $self->{update_statement} = $update;
637  
638 # Prepare the statement and stash the statement handle
639 1 $self->{update_sth} = $self->{server}->prepare( $update );
640 1 ▶ croak "Failed to prepare update '$update'" if !$self->{update_sth};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"update_sth"} 0 1 1
641  
642 1 ▶ carp "Data::Toolkit::Connector::DBI->updatespec setting '$update', (" . (join ',',@arglist) . ")" if $debug;
Branch: 1 branch
BranchTrueFalseTotal
if $debug 0 1 1
643  
644 # Return the spec string that we were given
645 1 return $self->{updatespec};
646 }
647  
648 ########################################
649  
650 =head2 update
651  
652 Update a row in the database using data from a source entry and an optional map.
653 If a map is supplied, it is used to transform data from the source entry before
654 it is applied to the database operation.
655  
656 Returns the result of the DBI execute operation.
657  
658 $msg = $dbConn->update($sourceEntry);
659 $msg = $dbConn->update($sourceEntry, $updateMap);
660  
661 A suitable update operation must have been defined using the updatespec() method
662 before update() is called:
663  
664 $spec = $dbiConn->updatespec( "UPDATE people set name = %myname% WHERE key = %mykey%" );
665 $msg = $dbiConn->update( $entry );
666  
667 NOTE that only the first value of a given attribute is used, as relational databases expect
668 a single value for each column in a given row.
669  
670 Note also that multiple rows could be affected by a single call to this method, depending
671 on how the updatespec has been defined.
672  
673 =cut
674  
675 sub update {
676 1 ▶ my $self = shift;
Subroutine update: called CC=13 SCAR=29.4
Pod documented
677 1 my $source = shift;
678 1 my $map = shift;
679  
680 1 ▶ croak "Data::Toolkit::Connector::DBI->update called before updatespec has been defined" if !$self->{update_sth};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"update_sth"} 0 1 1
681 1 ▶ croak "Data::Toolkit::Connector::DBI->update first parameter should be a Data::Toolkit::Entry"
Branch: 1 branch
BranchTrueFalseTotal
if $source and not $source->isa("Data::Toolkit::Entry") 0 1 1
Condition33%1/3
$source and not $source->isa("Data::Toolkit::Entry")
!ll&&!rl&&r
010
MC/DC0%0/2
$source and not $source->isa("Data::Toolkit::Entry")
$source not $source->isa("Data::Toolkit::Entry")
Truth table33%1/3
A and not(B)
A= $sourceB= not $source->isa("Data::Toolkit::Entry")
ABresult
0X0
100
111
682 if ($source and !$source->isa('Data::Toolkit::Entry'));
683 1 ▶ croak "Data::Toolkit::Connector::DBI->update second parameter should be a Data::Toolkit::Map"
Branch: 1 branch
BranchTrueFalseTotal
if $map and not $map->isa("Data::Toolkit::Map") 0 1 1
Condition33%1/3
$map and not $map->isa("Data::Toolkit::Map")
!ll&&!rl&&r
100
MC/DC0%0/2
$map and not $map->isa("Data::Toolkit::Map")
$map not $map->isa("Data::Toolkit::Map")
Truth table33%1/3
A and not(B)
A= $mapB= not $map->isa("Data::Toolkit::Map")
ABresult
0X0
100
111
684 if ($map and !$map->isa('Data::Toolkit::Map'));
685  
686 1 ▶ carp "Data::Toolkit::Connector::DBI->update $self $source" if $debug;
Branch: 1 branch
BranchTrueFalseTotal
if $debug 0 1 1
687  
688 # Apply the map if we have one
689 1 ▶ $source = $source->map($map) if $map;
Branch: 1 branch
BranchTrueFalseTotal
if $map 0 1 1
690  
691 # The args to be passed to the SQL UPDATE statement
692 1 my @args;
693 # The array of names that came from the updatespec
694 1 my @arglist = @{$self->{update_arglist}};
695 # Check that we have an entry to get params from
696 1 ▶ print "ARGLIST for update: ", (join ',', @arglist), "\n" if $debug;
Branch: 1 branch
BranchTrueFalseTotal
if $debug 0 1 1
697 1 ▶ if ($arglist[0] and !$source) {
Branch: 1 branch
BranchTrueFalseTotal
if ($arglist[0] and not $source) 0 1 1
Condition33%1/3
$arglist[0] and not $source
!ll&&!rl&&r
010
MC/DC0%0/2
$arglist[0] and not $source
$arglist[0] not $source
Truth table33%1/3
A and not(B)
A= $arglist[0]B= not $source
ABresult
0X0
100
111
698 0 croak "Data::Toolkit::Connector::DBI->update requires an entry when the updatespec includes parameters";
699 }
700  
701 # Extract the args from the entry
702 1 my $arg;
703 1 foreach $arg (@arglist) {
704 3 my $value = $source->get($arg);
705 3 ▶ croak "update spec calls for a '$arg' attribute but the entry does not have one" if !$value;
Branch: 1 branch
BranchTrueFalseTotal
unless $value 0 3 3
706 # We only use the first value from the list.
707 # This is permitted to be the null string.
708 # It should not be undef, as that would need an 'IS NULL' clause in SQL.
709 3 $value = $value->[0];
710 3 push @args, $value;
711 }
712  
713 # Start the search and return the statement handle having stashed a copy internally
714 1 return $self->{update_result} = $self->{update_sth}->execute( @args );
715 }
716  
717  
718 ########################################
719  
720 =head2 deletespec
721  
722 Supply or fetch spec for delete
723  
724 $spec = $dbiConn->deletespec();
725 $spec = $dbiConn->deletespec( "DELETE from people WHERE joinkey = %mykey%" );
726  
727 Parameters are indicated thus: %name% - this will result in a '?'-style placeholder in
728 the SQL statement and the named attribute will be extracted from the supplied entry
729 by the delete() method.
730  
731 =cut
732  
733 sub deletespec {
734 1 ▶ my $self = shift;
Subroutine deletespec: called CC=8 SCAR=21.3
Pod documented
735 1 my $deletespec = shift;
736  
737 1 ▶ carp "Data::Toolkit::Connector::DBI->deletespec $self $deletespec " if $debug;
Branch: 1 branch
BranchTrueFalseTotal
if $debug 0 1 1
738  
739 1 ▶ croak "Data::Toolkit::Connector::DBI->deletespec called before server connection opened" if !$self->{server};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"server"} 0 1 1
740  
741 # No arg supplied - just return existing setting
742 1 ▶ return $self->{deletespec} if (!$deletespec);
Branch: 1 branch
BranchTrueFalseTotal
unless $deletespec 0 1 1
743  
744 # We have a new deletespec so stash it for future reference
745 1 $self->{deletespec} = $deletespec;
746  
747 # We need to parse the spec to find the list of args that it calls for.
748 # Start by clearing the arglist and delete string
749 1 my $delete = '';
750 1 my @arglist;
751 1 $self->{delete_arglist} = \@arglist;
752  
753 # Parameter names are between pairs of % characters
754 # Where we want a literal '%' it is represented by '%%'
755 # so if the delete string has at least two '%' left then there is work to be done
756 1 ▶ while ($deletespec =~ /%.*%/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($deletespec =~ /%.*%/) 1 1 2
757 1 my ($left,$name,$right) = ($deletespec =~ /^([^%]*)%([a-zA-Z0-9_]*)%(.*)$/);
758 # Everything before the first % gets added to the delete string
759 1 $delete .= $left;
760 1 ▶ if ($name) {
Branch: 1 branch
BranchTrueFalseTotal
if ($name) { } 1 0 1
761 # Add the name to the list of attributes needed when the delete is performed
762 1 push @arglist, $name;
763 # Put the placeholder in the actual delete string
764 1 $delete .= '?';
765 }
766 else {
767 # We got '%%' so add a literal '%' to the delete string
768 0 $delete .= '%';
769 }
770 # The remainder of the deletespec goes round again
771 1 $deletespec = $right;
772 }
773 # Anything left in the deletespec gets appended to the delete string
774 1 $delete .= $deletespec;
775  
776 # Stash the resulting string and associated list of attributes
777 1 $self->{delete_statement} = $delete;
778  
779 # Prepare the statement and stash the statement handle
780 1 $self->{delete_sth} = $self->{server}->prepare( $delete );
781 1 ▶ croak "Failed to prepare delete '$delete'" if !$self->{delete_sth};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"delete_sth"} 0 1 1
782  
783 1 ▶ carp "Data::Toolkit::Connector::DBI->deletespec setting '$delete', (" . (join ',',@arglist) . ")" if $debug;
Branch: 1 branch
BranchTrueFalseTotal
if $debug 0 1 1
784  
785 # Return the spec string that we were given
786 1 return $self->{deletespec};
787 }
788  
789 ########################################
790  
791 =head2 delete
792  
793 Delete a row from the database using data from a source entry and an optional map.
794 If a map is supplied, it is used to transform data from the source entry before
795 it is applied to the database operation.
796  
797 Returns the result of the DBI execute operation.
798  
799 $msg = $dbConn->delete($sourceEntry);
800 $msg = $dbConn->delete($sourceEntry, $deleteMap);
801  
802 A suitable delete operation must have been defined using the deletespec() method
803 before delete() is called:
804  
805 $spec = $dbiConn->deletespec( "DELETE FROM people WHERE joinkey = %mykey%" );
806 $msg = $dbiConn->delete( $entry );
807  
808 NOTE that only the first value of a given attribute is used.
809  
810 Note also that multiple rows could be affected by a single call to this method, depending
811 on how the deletespec has been defined.
812  
813 =cut
814  
815 sub delete {
816 1 ▶ my $self = shift;
Subroutine delete: called CC=13 SCAR=29.4
Pod documented
817 1 my $source = shift;
818 1 my $map = shift;
819  
820 1 ▶ croak "Data::Toolkit::Connector::DBI->delete called before deletespec has been defined" if !$self->{delete_sth};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"delete_sth"} 0 1 1
821 1 ▶ croak "Data::Toolkit::Connector::DBI->delete first parameter should be a Data::Toolkit::Entry"
Branch: 1 branch
BranchTrueFalseTotal
if $source and not $source->isa("Data::Toolkit::Entry") 0 1 1
Condition33%1/3
$source and not $source->isa("Data::Toolkit::Entry")
!ll&&!rl&&r
010
MC/DC0%0/2
$source and not $source->isa("Data::Toolkit::Entry")
$source not $source->isa("Data::Toolkit::Entry")
Truth table33%1/3
A and not(B)
A= $sourceB= not $source->isa("Data::Toolkit::Entry")
ABresult
0X0
100
111
822 if ($source and !$source->isa('Data::Toolkit::Entry'));
823 1 ▶ croak "Data::Toolkit::Connector::DBI->delete second parameter should be a Data::Toolkit::Map"
Branch: 1 branch
BranchTrueFalseTotal
if $map and not $map->isa("Data::Toolkit::Map") 0 1 1
Condition33%1/3
$map and not $map->isa("Data::Toolkit::Map")
!ll&&!rl&&r
100
MC/DC0%0/2
$map and not $map->isa("Data::Toolkit::Map")
$map not $map->isa("Data::Toolkit::Map")
Truth table33%1/3
A and not(B)
A= $mapB= not $map->isa("Data::Toolkit::Map")
ABresult
0X0
100
111
824 if ($map and !$map->isa('Data::Toolkit::Map'));
825  
826 1 ▶ carp "Data::Toolkit::Connector::DBI->delete $self $source" if $debug;
Branch: 1 branch
BranchTrueFalseTotal
if $debug 0 1 1
827  
828 # Apply the map if we have one
829 1 ▶ $source = $source->map($map) if $map;
Branch: 1 branch
BranchTrueFalseTotal
if $map 0 1 1
830  
831 # The args to be passed to the SQL UPDATE statement
832 1 my @args;
833 # The array of names that came from the deletespec
834 1 my @arglist = @{$self->{delete_arglist}};
835 # Check that we have an entry to get params from
836 1 ▶ print "ARGLIST for delete: ", (join ',', @arglist), "\n" if $debug;
Branch: 1 branch
BranchTrueFalseTotal
if $debug 0 1 1
837 1 ▶ if ($arglist[0] and !$source) {
Branch: 1 branch
BranchTrueFalseTotal
if ($arglist[0] and not $source) 0 1 1
Condition33%1/3
$arglist[0] and not $source
!ll&&!rl&&r
010
MC/DC0%0/2
$arglist[0] and not $source
$arglist[0] not $source
Truth table33%1/3
A and not(B)
A= $arglist[0]B= not $source
ABresult
0X0
100
111
838 0 croak "Data::Toolkit::Connector::DBI->delete requires an entry when the deletespec includes parameters";
839 }
840  
841 # Extract the args from the entry
842 1 my $arg;
843 1 foreach $arg (@arglist) {
844 1 my $value = $source->get($arg);
845 1 ▶ croak "delete spec calls for a '$arg' attribute but the entry does not have one" if !$value;
Branch: 1 branch
BranchTrueFalseTotal
unless $value 0 1 1
846 # We only use the first value from the list.
847 # This is permitted to be the null string.
848 # It should not be undef, as that would need an 'IS NULL' clause in SQL.
849 1 $value = $value->[0];
850 1 push @args, $value;
851 }
852  
853 # Start the search and return the statement handle having stashed a copy internally
854 1 return $self->{delete_result} = $self->{delete_sth}->execute( @args );
855 }
856  
857 ########################################################################
858 # Debugging methods
859 ########################################################################
860  
861 =head1 Debugging methods
862  
863 =head2 debug
864  
865 Set and/or get the debug level for Data::Toolkit::Connector
866  
867 my $currentDebugLevel = Data::Toolkit::Connector::LDAP->debug();
868 my $newDebugLevel = Data::Toolkit::Connector::LDAP->debug(1);
869  
870 Any non-zero debug level causes the module to print copious debugging information.
871  
872 Note that this is a package method, not an object method. It should always be
873 called exactly as shown above.
874  
875 All debug information is reported using "carp" from the Carp module, so if
876 you want a full stack backtrace included you can run your program like this:
877  
878 perl -MCarp=verbose myProg
879  
880 =cut
881  
882 # Class method to set and/or get debug level
883 #
884 sub debug {
885 1 ▶ my $class = shift;
Subroutine debug: called CC=3 SCAR=12.0
Pod documented
886 1 ▶ if (ref $class) { croak "Class method 'debug' called as object method" }
Branch: 1 branch
BranchTrueFalseTotal
if (ref $class) 0 1 1
887 # print "DEBUG: ", (join '/', @_), "\n";
888 1 ▶ $debug = shift if (@_ == 1);
Branch: 1 branch
BranchTrueFalseTotal
if @_ == 1 1 0 1
889 1 return $debug
890 }
891  
892  
893 ########################################################################
894 ########################################################################
895  
896 =head1 Author
897  
898 Andrew Findlay
899  
900 Skills 1st Ltd
901  
902 andrew.findlay@skills-1st.co.uk
903  
904 http://www.skills-1st.co.uk/
905  
906 =cut
907  
908 ########################################################################
909 ########################################################################
910 1 1;
« Toolkit/Connector/LDAP.pm ↑ Summary Toolkit/Entry.pm »