File Coverage

blib/lib/SQL/Composer/Delete.pm
Criterion Covered Total %
statement 39 39 100.0
branch 7 8 87.5
condition 1 3 33.3
subroutine 9 9 100.0
pod 0 4 0.0
total 56 63 88.8


line stmt bran cond sub pod time code
1             package SQL::Composer::Delete;
2              
3 3     3   15546 use strict;
  3         7  
  3         74  
4 3     3   13 use warnings;
  3         6  
  3         114  
5              
6             require Carp;
7 3     3   343 use SQL::Composer::Quoter;
  3         7  
  3         56  
8 3     3   343 use SQL::Composer::Expression;
  3         7  
  3         685  
9              
10             sub new {
11 6     6 0 7156 my $class = shift;
12 6         28 my (%params) = @_;
13              
14 6         22 my $self = { table => $params{from} };
15 6         17 bless $self, $class;
16              
17             $self->{quoter} =
18 6   33     75 $params{quoter} || SQL::Composer::Quoter->new(driver => $params{driver});
19              
20 6         35 my $sql = '';
21 6         10 my @bind;
22              
23 6         15 $sql .= 'DELETE FROM ';
24              
25 6         24 $sql .= $self->_quote($params{from});
26              
27 6 100       26 if ($params{where}) {
28             my $expr = SQL::Composer::Expression->new(
29             quoter => $self->{quoter},
30             expr => $params{where}
31 1         9 );
32 1         4 $sql .= ' WHERE ' . $expr->to_sql;
33 1         4 push @bind, $expr->to_bind;
34             }
35              
36 6 100       25 if (defined(my $limit = $params{limit})) {
37 2         6 $sql .= ' LIMIT ' . $limit;
38             }
39              
40 6 100       26 if (defined(my $offset = $params{offset})) {
41 1         3 $sql .= ' OFFSET ' . $offset;
42             }
43              
44 6         17 $self->{sql} = $sql;
45 6         16 $self->{bind} = \@bind;
46              
47 6         24 return $self;
48             }
49              
50 2     2 0 1234 sub table { shift->{table} }
51              
52 5     5 0 38 sub to_sql { shift->{sql} }
53 5 50   5 0 2545 sub to_bind { @{shift->{bind} || []} }
  5         28  
54              
55             sub _quote {
56 6     6   12 my $self = shift;
57 6         34 my ($column) = @_;
58              
59 6         25 return $self->{quoter}->quote($column);
60             }
61              
62             1;
63             __END__