Crop/Object/Warehouse/Lang/SQL/Clause.pm untested

filter: Statementstmt 0.0% 0 / 62 Branchbran 0.0% 0 / 18 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 0.0% 0 / 10 Podpod 0.0% 0 / 3 totaltotal 0.0% 0 / 93 CC 10 SCAR 32.5
CC
10
Cov
0%
new
56.0
print_sql
20.0
add
2.0
CRAP 25.7
1 package Crop::Object::Warehouse::Lang::SQL::Clause;
2 use base qw/ Crop::Object /;
3  
4 =begin nd
5 Class: Crop::Object::Warehouse::Lang::SQL::Clause
6 Complex logical condition for WHERE.
7
8 Clause contains Collection of <Crop::Object::Warehouse::Lang::SQL::Clause::Item>.
9 =cut
10  
11 use v5.14;
12 use warnings;
13  
14 use Crop::Error;
15 use Crop::Object::Collection;
16 use Crop::Object::Warehouse::Lang::SQL::Clause::Item;
17  
18 use Crop::Debug;
19  
20 =begin nd
21 Variable: our %Attributes
22 Class members:
23  
24 items - Collection of clause items
25 =cut
26 our %Attributes = (
27 items => {default => Crop::Object::Collection->new('Crop::Object::Warehouse::Lang::SQL::Clause::Item')},
28 );
29  
30 =begin nd
31 Constructor: new ($clause)
32 =cut
33 sub new {
34 my ($class, $clause) = @_;
35
36 my $self = $class->SUPER::new;
37
38 my @clause;
39 if (ref $clause eq 'HASH') {
40 @clause = %$clause;
41 } elsif (ref $clause eq 'ARRAY') {
42 @clause = @$clause;
43 } else {
44 return warn 'DBASE: clause must be an Array either Hash ref';
45 }
46
47 while (@clause) {
48 my ($attr, $expr) = splice @clause, 0, 2;
49  
50 my $item;
51 unless (defined $expr) {
52 $item = Crop::Object::Warehouse::Lang::SQL::Clause::Item->new(
53 lhs => $attr,
54 operator => 'ISNULL',
55 );
56 } elsif (ref $expr eq 'HASH') {
57 unless (keys %$expr) { # attr=>{}
58 $item = Crop::Object::Warehouse::Lang::SQL::Clause::Item->new(
59 lhs => $attr,
60 operator => 'NOTNULL',
61 );
62 } else {
63 my ($sign, $val) = %$expr;
64 $item = Crop::Object::Warehouse::Lang::SQL::Clause::Item->new(
65 lhs => $attr,
66 operator => $sign,
67 rhs => $val,
68 );
69 }
70 } elsif (ref $expr eq 'ARRAY') {
71 $item = Crop::Object::Warehouse::Lang::SQL::Clause::Item->new(
72 lhs => $attr,
73 operator => 'IN',
74 rhs => $expr,
75 );
76 } else {
77 $item = Crop::Object::Warehouse::Lang::SQL::Clause::Item->new(
78 lhs => $attr,
79 operator => 'EQ',
80 rhs => $expr,
81 );
82 }
83 $self->{items}->Push($item);
84 }
85
86 $self;
87 }
88  
89 =begin nd
90 Method: add ($item)
91 Add an $item to the Clause.
92
93 Parameters:
94 $item - hashref presents >Crop::Object::Warehouse::Lang::SQL::Clause::Item>
95
96 Returns:
97 $self
98 =cut
99 sub add {
100 my ($self, $item) = @_;
101  
102
103 $self->{items}->Push(Crop::Object::Warehouse::Lang::SQL::Clause::Item->new($item));
104
105 $self;
106 }
107  
108 =begin nd
109 Method: print_sql ($table)
110 Get string corresponging to the clause.
111
112 Parameters:
113 $table - defines prefix for attribites; optional
114
115 Returns:
116 the two element list of SQL string and array of values
117 =cut
118 sub print_sql {
119 my ($self, $table) = @_;
120
121 my $table_prefix = '';
122 $table_prefix = "$table." if defined $table;
123
124 my (@sql, @val);
125 for ($self->{items}->List) {
126 my ($sql, $attr, $val) = $_->print_sql;
127  
128 push @sql, $table_prefix . $sql;
129  
130 if ($_->has_value) {
131 if (ref $val eq 'ARRAY') {
132 for (@$val) {
133 push @val, "$table_prefix$attr", $_;
134 }
135 } else {
136 push @val, "$table_prefix$attr", $val;
137 }
138 }
139 }
140
141 my $result;
142 {
143 local $" = ' AND ';
144 $result = "@sql";
145 }
146
147 ($result, \@val);
148 }
149  
150 1;