File Coverage

blib/lib/DBR/Query/Update.pm
Criterion Covered Total %
statement 38 38 100.0
branch 8 18 44.4
condition 1 4 25.0
subroutine 9 9 100.0
pod 0 3 0.0
total 56 72 77.7


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::Update;
8              
9 18     18   173 use strict;
  18         44  
  18         640  
10 18     18   102 use base 'DBR::Query';
  18         754  
  18         6255  
11 18     18   104 use Carp;
  18         26  
  18         13027  
12              
13 58     58   248 sub _params { qw (sets tables where limit quiet_error) }
14 58     58   303 sub _reqparams { qw (sets tables) }
15 58     58   702 sub _validate_self{ 1 } # If I exist, I'm valid
16              
17             sub sets{
18 58     58 0 107 my $self = shift;
19 58 0 0     372 exists( $_[0] ) or return wantarray?( @$self->{sets} ) : $self->{sets} || undef;
    50          
20 58         317 my @sets = $self->_arrayify(@_);
21 58 50       191 scalar(@sets) || croak('must provide at least one set');
22              
23 58         195 for (@sets){
24 58 50       272 ref($_) eq 'DBR::Query::Part::Set' || croak('arguments must be Sets');
25             }
26              
27 58         160 $self->{sets} = \@sets;
28              
29 58         256 return 1;
30             }
31              
32             # do not run this until the last possible moment, and then only once
33             sub sql{
34 58     58 0 117 my $self = shift;
35              
36 58 50       208 my $conn = $self->instance->connect('conn') or return $self->_error('failed to connect');
37 58         234 my $sql;
38 58         125 my $tables = join(',', map { $_->sql( $conn ) } @{$self->{tables}} );
  58         306  
  58         226  
39 58         162 my $sets = join(',', map { $_->sql( $conn ) } @{$self->{sets}} );
  58         266  
  58         149  
40              
41 58         218 $sql = "UPDATE $tables SET $sets";
42 58 50       747 $sql .= ' WHERE ' . $self->{where}->sql($conn) if $self->{where};
43 58 50       252 $sql .= ' LIMIT ' . $self->{limit} if $self->{limit};
44              
45 58         341 $self->_logDebug2( $sql );
46 58         338 return $sql;
47             }
48              
49             sub run{
50 58     58 0 120 my $self = shift;
51 58 50       366 my $conn = $self->instance->connect('conn') or return $self->_error('failed to connect');
52              
53 58 50       334 $conn->quiet_next_error if $self->quiet_error;
54              
55 58   50     274 return $conn->do( $self->sql ) || 0;
56             }
57              
58             1;