File Coverage

blib/lib/DBR/Query/Delete.pm
Criterion Covered Total %
statement 29 30 96.6
branch 5 10 50.0
condition 1 2 50.0
subroutine 8 8 100.0
pod 0 2 0.0
total 43 52 82.6


line stmt bran cond sub pod time code
1             # The contents of this file are Copyright (c) 2010 Daniel Norman
2             # This program is free software; you can redistribute it and/or
3             # modify it under the terms of the GNU General Public License as
4             # published by the Free Software Foundation.
5              
6             ###########################################
7             package DBR::Query::Delete;
8              
9 18     18   109 use strict;
  18         43  
  18         721  
10 18     18   129 use base 'DBR::Query';
  18         52  
  18         1325  
11 18     18   97 use Carp;
  18         34  
  18         9043  
12              
13 2     2   6 sub _params { qw (tables where limit quiet_error) }
14 2     2   9 sub _reqparams { qw (tables where) }
15 2     2   8 sub _validate_self{ 1 } # If I exist, I'm valid
16              
17             # do not run this until the last possible moment, and then only once
18             sub sql{
19 2     2 0 4 my $self = shift;
20              
21 2 50       7 my $conn = $self->instance->connect('conn') or return $self->_error('failed to connect');
22 2         4 my $sql;
23 2         5 my $tables = join(',', map { $_->sql( $conn ) } @{$self->{tables}} );
  2         10  
  2         6  
24 2         4 my $sets = join(',', map { $_->sql( $conn ) } @{$self->{sets}} );
  0         0  
  2         11  
25              
26 2         4 $sql = "DELETE FROM $tables";
27 2 50       17 $sql .= ' WHERE ' . $self->{where}->sql($conn) if $self->{where};
28 2 50       8 $sql .= ' LIMIT ' . $self->{limit} if $self->{limit};
29              
30 2         27 $self->_logDebug2( $sql );
31 2         13 return $sql;
32             }
33              
34             sub run{
35 2     2 0 5 my $self = shift;
36 2 50       11 my $conn = $self->instance->connect('conn') or return $self->_error('failed to connect');
37              
38 2 50       12 $conn->quiet_next_error if $self->quiet_error;
39              
40 2   50     8 return $conn->do( $self->sql ) || 0;
41             }
42              
43             1;