| File: | lib/SQL/Update.pm |
| Coverage: | 90.9% |
| line | stmt | bran | cond | sub | pod | time | code |
|---|---|---|---|---|---|---|---|
| 1 | package SQL::Update; | ||||||
| 2 | |||||||
| 3 | 23 23 23 | 221 73 189 | use strict; | ||||
| 4 | 23 23 23 | 258 74 186 | use warnings; | ||||
| 5 | 23 23 23 | 268 69 184 | use base 'SQL::Statement'; | ||||
| 6 | |||||||
| 7 | sub new { | ||||||
| 8 | 54 | 0 | 1450 | my $class = ref $_[0] ? ref shift : shift; | |||
| 9 | 54 | 13562 | bless { | ||||
| 10 | update => $_[0], | ||||||
| 11 | set => [], | ||||||
| 12 | where => [] | ||||||
| 13 | }, $class; | ||||||
| 14 | } | ||||||
| 15 | |||||||
| 16 | sub update { | ||||||
| 17 | 1 | 0 | 8 | $_[0]->{update} = $_[1]; | |||
| 18 | 1 | 7 | $_[0] | ||||
| 19 | } | ||||||
| 20 | |||||||
| 21 | sub set { | ||||||
| 22 | 56 | 0 | 330 | my $self = shift; | |||
| 23 | 56 56 | 212 662 | push @{ $self->{set} }, "$_[0] = $_[1]"; | ||||
| 24 | 56 | 394 | $self; | ||||
| 25 | } | ||||||
| 26 | |||||||
| 27 | sub generate { | ||||||
| 28 | 44 | 0 | 229 | my $self = shift; | |||
| 29 | 44 | 126 | my $sql; | ||||
| 30 | 44 | 213 | local $" = ', '; | ||||
| 31 | 44 44 | 424 412 | $sql .= "update $self->{update} | ||||
| 32 | set @{ $self->{set} }"; | ||||||
| 33 | 44 44 | 392 356 | my @where = @{ $self->{where} }; | ||||
| 34 | 44 52 | 257 518 | my $where = join ' and ', map { join(' ', @$_) } @where; | ||||
| 35 | 44 41 | 299 271 | if ($where) { $sql .= "\nwhere $where" } | ||||
| 36 | 44 | 360 | return $sql . ";\n"; | ||||
| 37 | } | ||||||
| 38 | |||||||
| 39 | 1; | ||||||
| 40 | |||||||