| 1 | package Crop::Object::Warehouse::Lang::SQL::Query::Delete; | ||
| 2 | use base qw/ Crop::Object::Warehouse::Lang::SQL::Query /; | ||
| 3 | |||
| 4 | =begin nd | ||
| 5 | Class: Crop::Object::Warehouse::Lang::SQL::Query::Delete | ||
| 6 | The Delete query. | ||
| 7 | =cut | ||
| 8 | |||
| 9 | use v5.14; | ||
| 10 | use warnings; | ||
| 11 | |||
| 12 | use Crop::Error; | ||
| 13 | use Crop::Object::Warehouse::Lang::SQL::Clause; | ||
| 14 | |||
| 15 | use Crop::Debug; | ||
| 16 | |||
| 17 | =begin nd | ||
| 18 | Variable: our %Attributes | ||
| 19 | Class members: | ||
| 20 | |||
| 21 | clause - exemplar of <Crop::Object::Warehouse::Lang::SQL::Clause> | ||
| 22 | table - table name | ||
| 23 | =cut | ||
| 24 | our %Attributes = ( | ||
| 25 | clause => undef, | ||
| 26 | table => undef, | ||
| 27 | ); | ||
| 28 | |||
| 29 | =begin nd | ||
| 30 | Constructor: new ( ) | ||
| 31 | Build clause. | ||
| 32 | |||
| 33 | Returns: | ||
| 34 | $self | ||
| 35 | =cut | ||
| 36 | sub new { | ||
| 37 | my ($class, %in) = @_; | ||
| 38 | |||
| 39 | my @clause = (); | ||
| 40 | my $self = $class->SUPER::new( | ||
| 41 | %in, | ||
| 42 | exists $in{clause} ? | ||
| 43 | (clause => Crop::Object::Warehouse::Lang::SQL::Clause->new($in{clause})) | ||
| 44 | : () | ||
| 45 | , | ||
| 46 | ); | ||
| 47 | |||
| 48 | $self; | ||
| 49 | } | ||
| 50 | |||
| 51 | =begin nd | ||
| 52 | Method: print_sql ( ) | ||
| 53 | Compose query prepared string based on inner presentation. | ||
| 54 | |||
| 55 | Returns: | ||
| 56 | the array of two elements, SQL string, and arrayref of values for placeholders | ||
| 57 | =cut | ||
| 58 | sub print_sql { | ||
| 59 | my $self = shift; | ||
| 60 | |||
| 61 | my $sql = "DELETE FROM $self->{table} "; | ||
| 62 | |||
| 63 | my $values; | ||
| 64 | if ($self->{clause}) { | ||
| 65 | my $where; | ||
| 66 | |||
| 67 | ($where, $values) = $self->{clause}->print_sql; | ||
| 68 | |||
| 69 | $sql .= " WHERE $where " if $where; | ||
| 70 | } | ||
| 71 | |||
| 72 | ($sql, $values); | ||
| 73 | } | ||
| 74 | |||
| 75 | 1; |