| 1 | package Crop::Object::Warehouse::Lang::SQL::Clause::Item; | ||
| 2 | use base qw/ Crop::Object /; | ||
| 3 | |||
| 4 | =begin nd | ||
| 5 | Class: Crop::Object::Warehouse::Lang::SQL::Clause::Item | ||
| 6 | Single logical condition for WHERE. | ||
| 7 | =cut | ||
| 8 | |||
| 9 | use v5.14; | ||
| 10 | use warnings; | ||
| 11 | |||
| 12 | use Crop::Error; | ||
| 13 | |||
| 14 | =begin nd | ||
| 15 | Variable: my %Op | ||
| 16 | All the known operators. | ||
| 17 | =cut | ||
| 18 | my %Op = ( | ||
| 19 | EQ => '=', | ||
| 20 | GT => '>', | ||
| 21 | GE => '>=', | ||
| 22 | LE => '<=', | ||
| 23 | LT => '<', | ||
| 24 | ); | ||
| 25 | |||
| 26 | =begin nd | ||
| 27 | Variable: our %Attributes | ||
| 28 | Class members: | ||
| 29 | |||
| 30 | has_value - either item requires rhs | ||
| 31 | lhs - left operand | ||
| 32 | operator - such 'EQ', 'GE', so on | ||
| 33 | rhs - right operand | ||
| 34 | =cut | ||
| 35 | our %Attributes = ( | ||
| 36 | has_value => {mode => 'read', default => 1}, | ||
| 37 | lhs => {mode => 'read'}, | ||
| 38 | operator => undef, | ||
| 39 | rhs => {mode => 'read'}, | ||
| 40 | ); | ||
| 41 | |||
| 42 | =begin nd | ||
| 43 | Method: print_sql ( ) | ||
| 44 | Get SQL string corresponging to the clause. | ||
| 45 | |||
| 46 | Returns: | ||
| 47 | the array of three elements, sql string, pseudo attribute, and attribute value | ||
| 48 | =cut | ||
| 49 | sub print_sql { | ||
| 50 | my $self = shift; | ||
| 51 | |||
| 52 | my @sql; | ||
| 53 | if ($self->{operator} eq 'IN') { | ||
| 54 | my $ph = join ', ', split '', '?' x @{$self->{rhs}}; | ||
| 55 | @sql = ("$self->{lhs} IN ($ph)", $self->{lhs}, $self->{rhs}); | ||
| 56 | } elsif ($self->{operator} eq 'NOTNULL') { | ||
| 57 | $self->{has_value} = 0; | ||
| 58 | @sql = ("$self->{lhs} IS NOT NULL", $self->{rhs}); | ||
| 59 | } elsif ($self->{operator} eq 'ISNULL') { | ||
| 60 | $self->{has_value} = 0; | ||
| 61 | @sql = ("$self->{lhs} IS NULL", $self->{rhs}); | ||
| 62 | } else { | ||
| 63 | @sql = ("$self->{lhs} $Op{$self->{operator}} ?", $self->{lhs} => $self->{rhs}); | ||
| 64 | } | ||
| 65 | |||
| 66 | @sql; | ||
| 67 | } | ||
| 68 | |||
| 69 | 1; |