blib/lib/DBIx/Composer.pm

filter: Statementstmt 43.7% 66 / 151 Branchbran 23.2% 26 / 112 Conditioncond 50.0% 3 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 35.4% 11 / 31 Podpod 70.3% 19 / 27 totaltotal 37.7% 125 / 331 CC 59 SCAR 30.7
CC
59
Cov
35%
fetch
72.0
fetchrow_hashref
72.0
delete
30.0
CRAP 21.6
↑ Summary
1 # Author: I.Plisco
2 # $Id: Composer.pm,v 1.2 2003/10/29 16:55:23 plisco Exp $
3  
4 package DBIx::Composer;
5 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 2 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶ use Exporter ();
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶ use vars qw ($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 $VERSION = '1.00';
11 2 @ISA = qw (Exporter);
12 2 @EXPORT = qw ();
13 2 @EXPORT_OK = qw ();
14 2 %EXPORT_TAGS = ();
15 }
16  
17 # Default global values
18 2 our $DEBUG = 0;
19 2 our $DBH;
20  
21 ########################################### main pod documentation begin ##
22 # Below is documentation for module.
23  
24  
25 =head1 NAME
26  
27 DBIx::Composer - Composes and runs SQL statement.
28  
29 =head1 SYNOPSIS
30  
31 use DBIx::Composer
32 $cmd = new DBIx::Composer();
33 $cmd->{table} = 'table1';
34 $cmd->{fields} = 'name, email';
35 $cmd->{where} = "where login = 'peter'";
36 print $cmd->compose_select;
37 # Prints "select name, surname from table1 where login = 'peter'"
38  
39 $dbh = open_database(); # Open database yourself
40 use DBIx::Composer
41 $cmd = new DBIx::Composer(dbh=>$dbh, debug=> 1);
42 $cmd->{table} = 'table1';
43 $cmd->{fields} = 'login, name, email';
44 $cmd->{values} = "'john', 'John Smith', 'john@smith.com'";
45 $cmd->insert();
46 # Executes command "insert into table1 (login, name, email) values
47 # ('john', 'John Smith', 'john@smith.com')"
48 # Prints this command on STDERR before execution.
49  
50 =head1 DESCRIPTION
51  
52 This module helps you to compose and run SQL statements. First you
53 create new object and fill its hash for common parts of SQL
54 statements. Then you may either compose SQL statement from these parts
55 or both compose and execute it.
56  
57 =head1 USAGE
58  
59 You connect to database using your favorite
60 method of connection and supply DBIx::Composer object with standard database
61 handler $dbh. If you don't plan to execute statements, you may omit
62 connection to database.
63  
64 So, after creating new object you set its parameters, or SQL command parts.
65 Modifiers for command, such as "where ...", "order ...", "limit ..."
66 must be full modifiers like "where a=b", not only "a=b".
67  
68 You don't need to prepare() SQL fetch statements - they are prepared
69 internally. You cant execute statements right after setting their parts - the
70 module checks whether command has been composed, prepared and executed. Because
71 of such behaviour don't try to reset command parts after executing, but better
72 create new DBIx::Composer object.
73  
74 =head2 Command parts
75  
76 Valid command parts are:
77  
78 table - table name.
79 Examples:
80 $cmd->{table} = 'table1'
81 $cmd->{table} = 'db left join user using (host)';
82  
83 fields - fields to select, insert, update, etc.
84 Examples:
85 $cmd->{fields} = 'login, email, tries + 5';
86 $cmd->{fields} = 'curdate(), now()';
87 $cmd->{fields} = 'ip, traf_in+thaf_out as sum';
88  
89 where
90 Examples:
91 $cmd->{where} = 'login = peter';
92 $cmd->{where} = 'tries > limit + 2';
93  
94 order
95 Examples:
96 $cmd->{order} = "order by ip desc";
97  
98 limit
99 Examples:
100 $cmd->{limit} = "limit 20";
101 $cmd->{limit} = "limit 100, 20";
102  
103 =head2 Opening database
104  
105 DBIx::Composer doesn't touch opening database. You should open it
106 yourself somewhere else in your program. As for me, I use special function
107 open_database() like this:
108  
109 #====================
110 sub open_database {
111 #====================
112
113 # Read config from some file
114 my ($db_db, $db_login, $db_passwd, $debug) = read_config();
115 $driver_name = "dbi:mysql:$db_db";
116
117 # Connect to database
118 $dbh = DBI->connect
119 ($driver_name, $db_login, $db_passwd)||
120 die $DBI::errstr;
121
122 # Initialize DBIx::Composer
123 $DBIx::Composer::DBH = $dbh;
124 $DBIx::Composer::DEBUG = $debug;
125  
126 return $dbh; # returns true on success
127 }
128  
129 Then in your program you don't need to set $dbh anymore, so you can simply
130 write:
131  
132 $cmd = new $DBIx::Composer;
133  
134 and $cmd knows yet about dbh handler and debug level.
135  
136 You have to set $dbh in new() explicitly only if you want to make two or more
137 database connections in one program, e.g. on migrating from old database to new
138 one.
139  
140 =head2 Debug levels
141  
142 If debug level is set to 1 or more, the module prints SQL commands
143 to STDERR (for cgi scripts it's error_log of web server).
144  
145 If debug level >= 1, it prints executed statements before executing them.
146  
147 If debug level >= 2, it prints composed statements after composing them. So
148 usually (not always) on level 2 you have 2 lines in STDERR - of composing and
149 of executing.
150  
151 Debug line starts from prefix ("SQL run" or "SQL debug") and program name (it's
152 convenient for cgi-bin's).
153  
154 =head2 Things you must pay attention to
155  
156 =item *
157  
158 In order to avoid confusing, don't reuse DBIx::Composer objects after executing
159 them, but simply create new object. It's because the module remembers its state
160 and don't composes statement again. Probably such behaviour will change
161 partially in future.
162  
163 =item *
164  
165 Command parts 'where', 'limit', 'order' must be started from these words, that
166 is, 'where a=b', not 'a=b'; 'order by c desc', not 'c desc' or 'by c desc'. You
167 may have in your mind the string "select $fields from $table $where".
168  
169 =head1 REQUIRES
170  
171 Perl 5.6.1 (for lvalue functions)
172 ExtUtils::MakeMaker - for installation
173 Test::More - for installation tests
174 DBI etc. - if you want to execute statements
175  
176  
177 =head1 BUGS
178  
179 I have tested it only in environment with MySQL database and localhost as
180 database host.
181  
182 Installation tests doesn't check that it works on real database - they only
183 check that the module composes statements.
184  
185  
186 =head1 AUTHOR
187  
188 Igor Plisco
189 igor at plisco dot ru
190 http://plisco.ru/soft
191  
192 =head1 COPYRIGHT
193  
194 This program is free software; you can redistribute
195 it and/or modify it under the same terms as Perl itself.
196  
197 The full text of the license can be found in the
198 LICENSE file included with this module.
199  
200  
201 =head1 SEE ALSO
202  
203 perl(1).
204  
205 =head1 FUNCTION DESCRIPTIONS
206  
207  
208 =cut
209  
210 ############################################# main pod documentation end ##
211  
212  
213 ################################################ subroutine header begin ##
214  
215 #============================================================
216 2 ▶sub new {
Subroutine new: called CC=4 SCAR=15.5
Pod documented
217 #============================================================
218  
219 =head2 new
220  
221 Purpose : Creates new DBIx::Object object.
222 Usage : DBIx::Composer->new()
223 Returns : Object handler.
224 Argument : Nothing or dbh handler or config.
225 Throws : None.
226 Comments : No.
227  
228 See Also : N/A
229  
230 =cut
231  
232 #------------------------------------------------------------
233  
234 # my ($class, %parameters) = @_;
235 2 my $class = shift;
236  
237 2 my $self = {
238 '_config' => {
239 debug => $DEBUG, # default debug level
240 dbh => $DBH # default database handler
241 }
242 };
243
244 2 ▶ $self->{'_config'}->{'dbh'} = shift if @_ == 1;
Branch: 1 branch
BranchTrueFalseTotal
if @_ == 1 0 2 2
245 0 ▶ %{ $self->{'_config'} } = @_ if @_ >= 2;
Branch: 1 branch
BranchTrueFalseTotal
if @_ >= 2 0 2 2
246  
247 2 ▶ bless ($self, ref ($class) || $class);
Condition33%1/3
ref $class || $class
l!l&&r!l&&!r
020
MC/DC0%0/2
ref $class || $class
ref $class $class
Truth table33%1/3
A || B
A= ref $classB= $class
ABresult
1X1
011
000
248 2 return $self;
249  
250 }
251  
252 #============================================================
253 0 ▶sub debug_level {
Subroutine debug_level: not called CC=2 SCAR=17.9
Pod documented
254 #============================================================
255  
256 =head2 debug_level
257  
258 Usage : DBIx::Composer->debug_level()
259 Purpose : Sets default debug level for all newly created objects.
260 Returns : current value of debug level
261 Argument : Debug level as integer.
262 Throws : None.
263 Comments : Warning: now returns global $dbh, not local for object.
264  
265 See Also :
266  
267 =cut
268  
269 #------------------------------------------------------------
270 0 my $self = shift;
271  
272 # Set it
273 0 ▶ $DEBUG = shift if(@_);
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 0 0
274 0 return $DEBUG;
275  
276 }
277  
278 #============================================================
279 0 ▶sub dbh {
Subroutine dbh: not called CC=2 SCAR=17.9
Pod documented
280 #============================================================
281  
282 =head2 dbh
283  
284 Usage : DBIx::Composer->dbh()
285 Purpose : Sets default database handler for all newly created objects.
286 Returns : current value of $dbh
287 Argument : database handler
288 Throws : None.
289 Comments : Warning: now returns global $dbh, not local for object.
290  
291 See Also :
292  
293 =cut
294  
295 #------------------------------------------------------------
296 0 my $self = shift;
297  
298 # Set it
299 0 ▶ $DBH = shift if(@_);
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 0 0
300 0 return $DBH;
301  
302 }
303  
304 #============================================================
305 0 ▶sub quote {
Subroutine quote: not called CC=1 SCAR=6.9
Pod documented
306 #============================================================
307  
308 =head2 quote
309  
310 Usage : DBIx::Composer->quote()
311 Purpose : Quotes its value by calling $dbh->quote.
312 Returns : Quoted string
313 Argument : String or number to quote
314 Throws : None.
315 Comments : None.
316  
317 See Also :
318  
319 =cut
320  
321 #------------------------------------------------------------
322 # my ($self, $str) = @_;
323 # my $dbh;
324 #
325 # $dbh = $self->{_config}->{dbh};
326 # return $dbh->quote($str);
327 0 my $self = shift;
328 0 return $self->{_config}->{dbh}->quote(shift);
329  
330 }
331  
332 #============================================================
333 1 ▶sub compose_select {
Subroutine compose_select: called CC=7 SCAR=20.8
Pod documented
334 #============================================================
335  
336 =head2 compose_select
337  
338 Usage : $cmd->compose_select()
339 Purpose : Composes select statement for given object.
340 Returns : Composed statement
341 Argument : None.
342 Throws : Returns undef, if required fields are missed.
343 Comments : Composes "select $fields from $table $where $order $limit"
344 statement where $fields stands for $cmd->fields and so on.
345 $where, $group, $order and $limit are optional.
346  
347  
348 See Also :
349  
350 =cut
351  
352 #------------------------------------------------------------
353 1 my ($self, %parameters) = @_;
354  
355 # Check required keys
356 1 ▶ return undef unless ($self->{fields});
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"fields"} 0 1 1
357  
358 # Compose
359 1 $self->{cmd} = "select $self->{fields}";
360 1 ▶ $self->{cmd} .= " from $self->{table}" if $self->{table};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"table"} 1 0 1
361 1 ▶ $self->{cmd} .= " $self->{where}" if $self->{where};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"where"} 1 0 1
362 1 ▶ $self->{cmd} .= " $self->{group}" if $self->{group};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"group"} 1 0 1
363 1 ▶ $self->{cmd} .= " $self->{order}" if $self->{order};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"order"} 1 0 1
364 1 ▶ $self->{cmd} .= " $self->{limit}" if $self->{limit};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"limit"} 1 0 1
365  
366 # Print debug, if needed
367 1 $self->debug;
368
369 1 return $self->{cmd};
370 }
371  
372 #============================================================
373 2 ▶sub compose_insert {
Subroutine compose_insert: called CC=4 SCAR=14.0
Pod documented
374 #============================================================
375  
376 =head2 compose_insert
377  
378 Usage : $cmd->compose_insert()
379 Purpose : Composes insert statement for given object.
380 Returns : Composed statement
381 Argument : None.
382 Throws : Returns undef, if required fields are missed.
383 Comments : Composes "insert into $table ($fields) values ($values)"
384 or "insert into $table values ($values)" if $fields omitted.
385  
386 See Also :
387  
388 =cut
389  
390 #------------------------------------------------------------
391 2 my ($self, %parameters) = @_;
392  
393 # Check required keys
394 2 ▶ return undef unless ($self->{table});
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"table"} 0 2 2
395 2 ▶ return undef unless ($self->{values});
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"values"} 0 2 2
396  
397 # Compose
398 2 $self->{cmd} = "insert into $self->{table}";
399 2 ▶ $self->{cmd} .= " ($self->{fields})" if $self->{fields};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"fields"} 1 1 2
400 2 $self->{cmd} .= " values ($self->{values})";
401
402 # Print debug, if needed
403 2 $self->debug;
404  
405 2 return $self->{cmd};
406 }
407  
408 #============================================================
409 3 ▶sub compose_replace {
Subroutine compose_replace: called CC=6 SCAR=18.1
Pod documented
410 #============================================================
411  
412 =head2 compose_replace
413  
414 Usage : $cmd->compose_replace()
415 Purpose : Composes replace statement for given object.
416 Returns : Composed statement
417 Argument : None.
418 Throws : Returns undef, if required fields are missed.
419 Comments : Composes "replace into $table ($fields) values ($values)"
420 or "replace into $table values ($values)" if $fields omitted.
421  
422 See Also :
423  
424 =cut
425  
426 #------------------------------------------------------------
427 3 my ($self, %parameters) = @_;
428  
429 # Check required keys
430 3 ▶ return undef unless ($self->{table});
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"table"} 0 3 3
431 3 ▶ return undef unless ($self->{values} or $self->{set});
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"values"} or $self->{"set"} 0 3 3
Condition66%2/3
$self->{"values"} or $self->{"set"}
l!l&&r!l&&!r
210
MC/DC0%0/2
$self->{"values"} or $self->{"set"}
$self->{"values"} $self->{"set"}
Truth table66%2/3
A or B
A= $self->{"values"}B= $self->{"set"}
ABresult
1X1
011
000
432  
433 # Compose
434 3 $self->{cmd} = "replace into $self->{table}";
435  
436 # Insert-like syntax
437 3 ▶ if($self->{values}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'values'}) { } 2 1 3
438 2 ▶ $self->{cmd} .= " ($self->{fields})" if $self->{fields};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"fields"} 1 1 2
439 2 $self->{cmd} .= " values ($self->{values})";
440  
441 # Update-like syntax
442 } else {
443 1 $self->{cmd} .= " $self->{set}";
444 }
445
446 # Print debug, if needed
447 3 $self->debug;
448  
449 3 return $self->{cmd};
450 }
451  
452  
453 #============================================================
454 2 ▶sub compose_delete {
Subroutine compose_delete: called CC=3 SCAR=11.0
Pod documented
455 #============================================================
456  
457 =head2 compose_delete
458  
459 Usage : $cmd->compose_delete()
460 Purpose : Composes delete statement for given object.
461 Returns : Composed statement
462 Argument : None.
463 Throws : Returns undef, if required fields are missed.
464 Comments : Composes "delete from $table $where"
465 or "delete from $table" if $where omitted.
466  
467  
468 See Also :
469  
470 =cut
471  
472 #------------------------------------------------------------
473 2 my ($self, %parameters) = @_;
474  
475 # Check required keys
476 2 ▶ return undef unless ($self->{table});
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"table"} 0 2 2
477  
478 # Compose
479 2 $self->{cmd} = "delete from $self->{table}";
480 2 ▶ $self->{cmd} .= " $self->{where}" if $self->{where};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"where"} 1 1 2
481
482 # Print debug, if needed
483 2 $self->debug;
484
485 2 return $self->{cmd};
486 }
487  
488 #============================================================
489 2 ▶sub compose_update {
Subroutine compose_update: called CC=4 SCAR=14.0
Pod documented
490 #============================================================
491  
492 =head2 compose_update
493  
494 Usage : $cmd->compose_update()
495 Purpose : Composes update statement for given object.
496 Returns : Composed statement
497 Argument : None.
498 Throws : Returns undef, if required fields are missed.
499 Comments : Composes "update $table $set $where"
500 or "update $table $set" if $where omitted.
501  
502 See Also :
503  
504 =cut
505  
506 #------------------------------------------------------------
507 2 my ($self, %parameters) = @_;
508  
509 # Check required keys
510 2 ▶ return undef unless ($self->{table});
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"table"} 0 2 2
511 2 ▶ return undef unless ($self->{set});
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"set"} 0 2 2
512  
513 # Compose
514 2 $self->{cmd} = "update $self->{table} $self->{set}";
515 2 ▶ $self->{cmd} .= " $self->{where}" if $self->{where};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"where"} 1 1 2
516
517 # Print debug, if needed
518 2 $self->debug;
519
520 2 return $self->{cmd};
521 }
522  
523 #============================================================
524 0 ▶sub selectrow_array {
Subroutine selectrow_array: not called CC=4 SCAR=30.0
Pod documented
525 #============================================================
526  
527 =head2 selectrow_array
528  
529 Usage : $cmd->selectrow_array()
530 Purpose : Makes DBI call of selectrow_array
531 Returns : Array or scalar
532 Argument : None.
533 Throws : Returns undef, if required fields are missed.
534 Comments :
535  
536 See Also :
537  
538 =cut
539  
540 #------------------------------------------------------------
541 0 my ($self, %parameters) = @_;
542  
543 # Check required keys
544 0 ▶ return undef unless ($self->{_config}->{dbh});
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"_config"}{"dbh"} 0 0 0
545  
546 # Compose
547 0 ▶ $self->compose_select unless $self->{cmd};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"cmd"} 0 0 0
548 0 ▶ return undef unless ($self->{cmd});
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"cmd"} 0 0 0
549  
550 # Make DBI call
551 0 $self->log;
552 # return $self->{_config}->{dbh}->selectrow_array($self->{cmd});
553  
554 # if(wantarray()) {
555 # my @arr = $self->{_config}->{dbh}->selectrow_array($self->{cmd});
556 # return @arr;
557 # } else {
558 # return $self->{_config}->{dbh}->selectrow_array($self->{cmd});
559 # }
560 0 return $self->{_config}->{dbh}->selectrow_array($self->{cmd});
561  
562 }
563  
564 #============================================================
565 0 ▶sub fetch {
Subroutine fetch: not called CC=8 SCAR=42.8
Pod documented
566 #============================================================
567  
568 =head2 fetch
569  
570 Usage : $cmd->fetch()
571 Purpose : Makes DBI call of fetch
572 Returns : Array of data
573 Argument : None.
574 Throws : Returns undef, if error occured. See $dbh->errstr for errors.
575 Comments : After last row returns undef too.
576  
577 See Also :
578  
579 =cut
580  
581 #------------------------------------------------------------
582 0 my ($self, %parameters) = @_;
583  
584 # execute() called yet?
585 0 ▶ unless($self->{_executed}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"_executed"}) 0 0 0
586  
587 # Is $sth ready?
588 0 ▶ unless($self->{sth}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"sth"}) 0 0 0
589  
590 # Was command prepared yet?
591 0 ▶ unless ($self->{cmd}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"cmd"}) 0 0 0
592 0 ▶ $self->compose_select unless $self->{cmd};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"cmd"} 0 0 0
593 0 ▶ return undef unless ($self->{cmd});
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"cmd"} 0 0 0
594 }
595  
596 # Prepare
597 0 $self->{sth} = $self->{_config}->{dbh}->prepare($self->{cmd});
598 0 ▶ return undef unless ($self->{sth});
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"sth"} 0 0 0
599 }
600  
601 # Execute
602 0 $self->log;
603 0 ▶ $self->{sth}->execute || return undef;
Branch: 1 branch
BranchTrueFalseTotal
$self->{"sth"}->execute or return undef 0 0 0
604 0 $self->{_executed} = 1;
605 }
606  
607 0 return $self->{sth}->fetch();
608  
609 }
610  
611 #============================================================
612 0 ▶sub fetchrow_hashref {
Subroutine fetchrow_hashref: not called CC=8 SCAR=42.8
Pod documented
613 #============================================================
614  
615 =head2 fetchrow_hashref
616  
617 Usage : $cmd->fetchrow_hashref()
618 Purpose : Makes DBI call of fetchrow_hashref
619 Returns : Array of data
620 Argument : None.
621 Throws : Returns undef, if error occured. See $dbh->errstr for errors.
622 Comments : After last row returns undef too.
623  
624 See Also :
625  
626 =cut
627  
628 #------------------------------------------------------------
629 0 my ($self, %parameters) = @_;
630  
631 # execute() called yet?
632 0 ▶ unless($self->{_executed}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"_executed"}) 0 0 0
633  
634 # Is $sth ready?
635 0 ▶ unless($self->{sth}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"sth"}) 0 0 0
636  
637 # Was command prepared yet?
638 0 ▶ unless ($self->{cmd}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"cmd"}) 0 0 0
639 0 ▶ $self->compose_select unless $self->{cmd};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"cmd"} 0 0 0
640 0 ▶ return undef unless ($self->{cmd});
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"cmd"} 0 0 0
641 }
642  
643 # Prepare
644 0 $self->{sth} = $self->{_config}->{dbh}->prepare($self->{cmd});
645 0 ▶ return undef unless ($self->{sth});
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"sth"} 0 0 0
646 }
647  
648 # Execute
649 0 ▶ $self->{sth}->execute || return undef;
Branch: 1 branch
BranchTrueFalseTotal
$self->{"sth"}->execute or return undef 0 0 0
650 0 $self->{_executed} = 1;
651 }
652  
653 0 return $self->{sth}->fetchrow_hashref();
654  
655 }
656  
657 #============================================================
658 0 ▶sub insert {
Subroutine insert: not called CC=4 SCAR=30.0
Pod documented
659 #============================================================
660  
661 =head2 insert
662  
663 Usage : $cmd->insert()
664 Purpose : Makes DBI call of insert
665 Returns : ID of inserted row (as "last insert id").
666 Argument : None.
667 Throws : Returns undef, if required fields are missed.
668 Comments :
669  
670 See Also :
671  
672 =cut
673  
674 #------------------------------------------------------------
675 0 my ($self, %parameters) = @_;
676  
677 # Check required keys
678 0 ▶ return undef unless ($self->{table});
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"table"} 0 0 0
679  
680 # Compose
681 0 ▶ $self->compose_insert unless $self->{cmd};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"cmd"} 0 0 0
682 0 ▶ return undef unless ($self->{cmd});
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"cmd"} 0 0 0
683  
684 # Make DBI call
685 0 $self->log;
686 0 $self->{_config}->{dbh}->do($self->{cmd});
687  
688 0 return $self->{_config}->{dbh}->{'mysql_insertid'};
689  
690 }
691  
692 #============================================================
693 0 ▶sub replace {
Subroutine replace: not called CC=4 SCAR=30.0
Pod documented
694 #============================================================
695  
696 =head2 replace
697  
698 Usage : $cmd->replace()
699 Purpose : Makes DBI call of replace
700 Returns : ID of replaced row (as "last insert id").
701 Argument : None.
702 Throws : Returns undef, if required fields are missed.
703 Comments :
704  
705 See Also :
706  
707 =cut
708  
709 #------------------------------------------------------------
710 0 my ($self, %parameters) = @_;
711  
712 # Check required keys
713 0 ▶ return undef unless ($self->{table});
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"table"} 0 0 0
714  
715 # Compose
716 0 ▶ $self->compose_replace unless $self->{cmd};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"cmd"} 0 0 0
717 0 ▶ return undef unless ($self->{cmd});
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"cmd"} 0 0 0
718  
719 # Make DBI call
720 0 $self->log;
721 0 $self->{_config}->{dbh}->do($self->{cmd});
722  
723 0 return $self->{_config}->{dbh}->{'mysql_insertid'};
724  
725 }
726  
727 #============================================================
728 0 ▶sub delete {
Subroutine delete: not called CC=5 SCAR=34.0
Pod documented
729 #============================================================
730  
731 =head2 delete
732  
733 Usage : $cmd->delete()
734 Purpose : Makes DBI call of delete
735 Returns : 1 if OK, false otherwise.
736 Argument : None.
737 Throws : Returns undef, if required fields are missed.
738 Comments :
739  
740 See Also :
741  
742 =cut
743  
744 #------------------------------------------------------------
745 0 my ($self, %parameters) = @_;
746  
747 # Check required keys
748 0 ▶ return undef unless ($self->{table});
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"table"} 0 0 0
749  
750 # Compose
751 0 ▶ $self->compose_delete unless $self->{cmd};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"cmd"} 0 0 0
752 0 ▶ return undef unless ($self->{cmd});
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"cmd"} 0 0 0
753  
754 # Make DBI call
755 0 $self->log;
756 0 ▶ unless ($self->{_config}->{dbh}->do($self->{cmd})) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"_config"}{"dbh"}->do($self->{"cmd"})) 0 0 0
757 0 warn $self->{_config}->{dbh}->errstr;
758 0 return 0;
759 }
760  
761 0 return 1;
762  
763 }
764  
765 #============================================================
766 0 ▶sub update {
Subroutine update: not called CC=5 SCAR=34.0
Pod documented
767 #============================================================
768  
769 =head2 update
770  
771 Usage : $cmd->update()
772 Purpose : Makes DBI call of update
773 Returns : 1 if OK, false otherwise.
774 Argument : None.
775 Throws : Returns undef, if required fields are missed.
776 Comments :
777  
778 See Also :
779  
780 =cut
781  
782 #------------------------------------------------------------
783 0 my ($self, %parameters) = @_;
784  
785 # Check required keys
786 0 ▶ return undef unless ($self->{table});
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"table"} 0 0 0
787  
788 # Compose
789 0 ▶ $self->compose_update unless $self->{cmd};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"cmd"} 0 0 0
790 0 ▶ return undef unless ($self->{cmd});
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"cmd"} 0 0 0
791  
792 # Make DBI call
793 0 $self->log;
794 0 ▶ unless ($self->{_config}->{dbh}->do($self->{cmd})) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"_config"}{"dbh"}->do($self->{"cmd"})) 0 0 0
795 0 warn $self->{_config}->{dbh}->errstr;
796 0 return 0;
797 }
798  
799 0 return 1;
800  
801 }
802  
803 #============================================================
804 0 ▶sub log_cmd {
Subroutine log_cmd: not called CC=2 SCAR=17.9
Pod documented
805 #============================================================
806  
807 =head2 log_cmd
808  
809 Usage : $cmd->log_cmd()
810 Purpose : Logs SQL command to STDERR.
811 Returns : Nothing.
812 Argument : 1 - output format
813 Throws : Returns undef, if required fields are missed.
814 Comments : Should't be called directly. Set flag debug
815 instead when called new().
816  
817 See Also :
818  
819 =cut
820  
821 #------------------------------------------------------------
822 0 my ($self, $fmt) = @_;
823  
824 # Check format
825 0 ▶ $fmt = "%s\n" unless $fmt;
Branch: 1 branch
BranchTrueFalseTotal
unless $fmt 0 0 0
826 0 printf STDERR $fmt, $self->{cmd};
827  
828 # Log command into STDERR
829  
830 }
831  
832 #============================================================
833 10 ▶sub debug {
Subroutine debug: called CC=2 SCAR=8.1
Pod documented
834 #============================================================
835  
836 =head2 debug
837  
838 Usage : $cmd->debug()
839 Purpose : Logs SQL command to STDERR.
840 Returns : Nothing.
841 Argument : None.
842 Throws : Returns undef, if required fields are missed.
843 Comments : Should't be called directly. Set flag debug > 0
844 instead when called new().
845  
846 See Also :
847  
848 =cut
849  
850 #------------------------------------------------------------
851 10 my ($self, %parameters) = @_;
852  
853 # If debug level < 1 - do nothing
854 10 ▶ return unless($self->{_config}->{debug} > 1);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"_config"}{"debug"} > 1 10 0 10
855  
856 # Log command into STDERR
857 0 $self->log_cmd("SQL debug: $0 -> [%s]\n");
858  
859 }
860  
861 #============================================================
862 0 ▶sub log {
Subroutine log: not called CC=2 SCAR=17.9
Pod documented
863 #============================================================
864  
865 =head2 log
866  
867 Usage : $cmd->log()
868 Purpose : Logs SQL command to STDERR.
869 Returns : Nothing.
870 Argument : None.
871 Throws : Returns undef, if required fields are missed.
872 Comments : Should't be called directly. Set flag debug > 1
873 instead when called new().
874  
875 See Also :
876  
877 =cut
878  
879 #------------------------------------------------------------
880 0 my ($self, %parameters) = @_;
881  
882 # If debug level < 0 - do nothing
883 0 ▶ return unless($self->{_config}->{debug} > 0);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"_config"}{"debug"} > 0 0 0 0
884  
885 # Log command into STDERR
886 0 $self->log_cmd("SQL run: $0 -> [%s]\n");
887  
888 }
889  
890 =head2 Functions for access to inner object data as lvalue
891  
892 Usage : $cmd->table = "users"; or: $table_sav = $cmd->table;
893 Purpose : Make access to inner variables without hash curlies.
894 Comments : Warning: don't work in Perl < 5.6. Use form $cmd->{table} instead.
895  
896 Currently supported functions:
897 table()
898 fields()
899 values()
900 where()
901 set()
902 order()
903 group()
904 limit()
905  
906 =cut
907  
908 # use 5.6.1; # for lvalues
909  
910 #============================================================
911 0 ▶sub table : lvalue {
Subroutine table: not called CC=1 SCAR=6.9
Pod undocumented
912 #------------------------------------------------------------
913 0 shift()->{table};
914 }
915  
916 #============================================================
917 0 ▶sub fields : lvalue {
Subroutine fields: not called CC=1 SCAR=6.9
Pod undocumented
918 #------------------------------------------------------------
919 0 shift()->{fields};
920 }
921  
922 #============================================================
923 0 ▶sub values : lvalue {
Subroutine values: not called CC=1 SCAR=6.9
Pod undocumented
924 #------------------------------------------------------------
925 0 shift()->{values};
926 }
927  
928 #============================================================
929 0 ▶sub where : lvalue {
Subroutine where: not called CC=1 SCAR=6.9
Pod undocumented
930 #------------------------------------------------------------
931 0 shift()->{where};
932 }
933  
934 #============================================================
935 0 ▶sub set : lvalue {
Subroutine set: not called CC=1 SCAR=6.9
Pod undocumented
936 #------------------------------------------------------------
937 0 shift()->{set};
938 }
939  
940 #============================================================
941 0 ▶sub order : lvalue {
Subroutine order: not called CC=1 SCAR=6.9
Pod undocumented
942 #------------------------------------------------------------
943 0 shift()->{order};
944 }
945  
946 #============================================================
947 0 ▶sub group : lvalue {
Subroutine group: not called CC=1 SCAR=6.9
Pod undocumented
948 #------------------------------------------------------------
949 0 shift()->{group};
950 }
951  
952 #============================================================
953 0 ▶sub limit : lvalue {
Subroutine limit: not called CC=1 SCAR=6.9
Pod undocumented
954 #------------------------------------------------------------
955 0 shift()->{limit};
956 }
957  
958  
959 2 1; #this line is important and will help the module return a true value
960 __END__
↑ Summary