added common test executing ...
|
1 |
package DBIx::Custom::Where; |
2 |
use Object::Simple -base; |
|
3 | ||
4 |
use Carp 'croak'; |
|
5 |
use DBIx::Custom::Util '_subname'; |
|
6 |
use overload 'bool' => sub {1}, fallback => 1; |
|
7 |
use overload '""' => sub { shift->to_string }, fallback => 1; |
|
8 | ||
9 |
# Carp trust relationship |
|
10 |
push @DBIx::Custom::CARP_NOT, __PACKAGE__; |
|
11 | ||
12 |
has [qw/dbi param/], |
|
13 |
clause => sub { [] }; |
|
14 | ||
15 |
sub new { |
|
16 |
my $self = shift->SUPER::new(@_); |
|
17 |
|
|
18 |
# Check attribute names |
|
19 |
my @attrs = keys %$self; |
|
20 |
foreach my $attr (@attrs) { |
|
21 |
croak qq{"$attr" is invalid attribute name (} . _subname . ")" |
|
22 |
unless $self->can($attr); |
|
23 |
} |
|
24 |
|
|
25 |
return $self; |
|
26 |
} |
|
27 | ||
28 |
sub to_string { |
|
29 |
my $self = shift; |
|
30 |
|
|
31 |
# Check if column name is safety character; |
|
32 |
my $safety = $self->dbi->safety_character; |
|
33 |
if (ref $self->param eq 'HASH') { |
|
34 |
foreach my $column (keys %{$self->param}) { |
|
35 |
croak qq{"$column" is not safety column name (} . _subname . ")" |
|
36 |
unless $column =~ /^[$safety\.]+$/; |
|
37 |
} |
|
38 |
} |
|
39 |
# Clause |
|
40 |
my $clause = $self->clause; |
|
41 |
$clause = ['and', $clause] unless ref $clause eq 'ARRAY'; |
|
42 |
$clause->[0] = 'and' unless @$clause; |
|
43 | ||
44 |
# Parse |
|
45 |
my $where = []; |
|
46 |
my $count = {}; |
|
47 |
$self->_parse($clause, $where, $count, 'and'); |
|
48 |
|
|
49 |
# Stringify |
|
50 |
unshift @$where, 'where' if @$where; |
|
51 |
return join(' ', @$where); |
|
52 |
} |
|
53 | ||
54 |
our %VALID_OPERATIONS = map { $_ => 1 } qw/and or/; |
|
55 |
sub _parse { |
|
56 |
my ($self, $clause, $where, $count, $op) = @_; |
|
57 |
|
|
58 |
# Array |
|
59 |
if (ref $clause eq 'ARRAY') { |
|
60 |
|
|
61 |
# Start |
|
62 |
push @$where, '('; |
|
63 |
|
|
64 |
# Operation |
|
65 |
my $op = $clause->[0] || ''; |
|
66 |
croak qq{First argument must be "and" or "or" in where clause } . |
|
67 |
qq{"$op" is passed} . _subname . ")" |
|
68 |
unless $VALID_OPERATIONS{$op}; |
|
69 |
|
|
70 |
my $pushed_array; |
|
71 |
# Parse internal clause |
|
72 |
for (my $i = 1; $i < @$clause; $i++) { |
|
73 |
my $pushed = $self->_parse($clause->[$i], $where, $count, $op); |
|
74 |
push @$where, $op if $pushed; |
|
75 |
$pushed_array = 1 if $pushed; |
|
76 |
} |
|
77 |
pop @$where if $where->[-1] eq $op; |
|
78 |
|
|
79 |
# Undo |
|
80 |
if ($where->[-1] eq '(') { |
|
81 |
pop @$where; |
|
82 |
pop @$where if ($where->[-1] || '') eq $op; |
|
83 |
} |
|
84 |
# End |
|
85 |
else { push @$where, ')' } |
|
86 |
|
|
87 |
return $pushed_array; |
|
88 |
} |
|
89 |
|
|
90 |
# String |
|
91 |
else { |
|
92 |
# Pushed |
|
93 |
my $pushed; |
|
94 |
|
|
95 |
# Column |
|
96 |
my $columns = $self->dbi->query_builder->build_query($clause)->columns; |
|
97 |
if (@$columns == 0) { |
|
98 |
push @$where, $clause; |
|
99 |
$pushed = 1; |
|
100 |
return $pushed; |
|
101 |
} |
|
102 |
elsif (@$columns != 1) { |
|
103 |
croak qq{Each part contains one column name: "$clause" (} |
|
104 |
. _subname . ")"; |
|
105 |
} |
|
106 |
|
|
107 |
# Remove quote |
|
108 |
my $column = $columns->[0]; |
|
109 |
if (my $q = $self->dbi->_quote) { |
|
110 |
$q = quotemeta($q); |
|
111 |
$column =~ s/[$q]//g; |
|
112 |
} |
|
113 |
|
|
114 |
# Check safety |
|
115 |
my $safety = $self->dbi->safety_character; |
|
116 |
croak qq{"$column" is not safety column name (} . _subname . ")" |
|
117 |
unless $column =~ /^[$safety\.]+$/; |
|
118 |
|
|
119 |
# Column count up |
|
120 |
my $count = ++$count->{$column}; |
|
121 |
|
|
122 |
# Push |
|
123 |
my $param = $self->param; |
|
124 |
if (ref $param eq 'HASH') { |
|
125 |
if (exists $param->{$column}) { |
|
126 |
if (ref $param->{$column} eq 'ARRAY') { |
|
127 |
$pushed = 1 |
|
128 |
if exists $param->{$column}->[$count - 1] |
|
129 |
&& ref $param->{$column}->[$count - 1] ne 'DBIx::Custom::NotExists'; |
|
130 |
} |
|
131 |
elsif ($count == 1) { |
|
132 |
$pushed = 1; |
|
133 |
} |
|
134 |
} |
|
135 |
push @$where, $clause if $pushed; |
|
136 |
} |
|
137 |
elsif (!defined $param) { |
|
138 |
push @$where, $clause; |
|
139 |
$pushed = 1; |
|
140 |
} |
|
141 |
else { |
|
142 |
croak "Parameter must be hash reference or undfined value (" |
|
143 |
. _subname . ")" |
|
144 |
} |
|
145 |
return $pushed; |
|
146 |
} |
|
147 |
return; |
|
148 |
} |
|
149 | ||
150 |
1; |
|
151 | ||
152 |
=head1 NAME |
|
153 | ||
154 |
DBIx::Custom::Where - Where clause |
|
155 | ||
156 |
=head1 SYNOPSYS |
|
157 | ||
158 |
my $where = DBIx::Custom::Where->new; |
|
159 |
my $string_where = "$where"; |
|
160 | ||
161 |
=head1 ATTRIBUTES |
|
162 | ||
163 |
=head2 C<clause> |
|
164 | ||
165 |
my $clause = $where->clause; |
|
166 |
$where = $where->clause( |
|
167 |
['and', |
|
168 |
'title = :title', |
|
169 |
['or', 'date < :date', 'date > :date'] |
|
170 |
] |
|
171 |
); |
|
172 | ||
173 |
Where clause. Above one is expanded to the following SQL by to_string |
|
174 |
If all parameter names is exists. |
|
175 | ||
176 |
"where ( title = :title and ( date < :date or date > :date ) )" |
|
177 | ||
178 |
=head2 C<param> |
|
179 | ||
180 |
my $param = $where->param; |
|
181 |
$where = $where->param({ |
|
182 |
title => 'Perl', |
|
183 |
date => ['2010-11-11', '2011-03-05'], |
|
184 |
}); |
|
185 | ||
186 |
=head2 C<dbi> |
|
187 | ||
188 |
my $dbi = $where->dbi; |
|
189 |
$where = $where->dbi($dbi); |
|
190 | ||
191 |
L<DBIx::Custom> object. |
|
192 | ||
193 |
=head1 METHODS |
|
194 | ||
195 |
L<DBIx::Custom::Where> inherits all methods from L<Object::Simple> |
|
196 |
and implements the following new ones. |
|
197 | ||
198 |
=head2 C<to_string> |
|
199 | ||
200 |
$where->to_string; |
|
201 | ||
202 |
Convert where clause to string. |
|
203 | ||
204 |
double quote is override to execute C<to_string> method. |
|
205 | ||
206 |
my $string_where = "$where"; |
|
207 | ||
208 |
=cut |