added experimental DBIx::Cus...
|
1 |
package DBIx::Custom::Where; |
updatedd pod
|
2 |
use Object::Simple -base; |
added experimental DBIx::Cus...
|
3 | |
DBIx::Custom::Model type att...
|
4 |
use Carp 'croak'; |
5 |
use DBIx::Custom::Util '_subname'; |
|
added experimental DBIx::Cus...
|
6 |
use overload 'bool' => sub {1}, fallback => 1; |
7 |
use overload '""' => sub { shift->to_string }, fallback => 1; |
|
8 | ||
updated document
|
9 |
# Carp trust relationship |
10 |
push @DBIx::Custom::CARP_NOT, __PACKAGE__; |
|
11 | ||
sub module use DBIx::Custom ...
|
12 |
has [qw/dbi param/], |
added map method(not complet...
|
13 |
clause => sub { [] }; |
14 | ||
map cleanup
|
15 |
sub map { |
16 |
my ($self, %map) = @_; |
|
17 |
|
|
added tests
|
18 |
if ($self->if ne 'exists' || keys %map) { |
19 |
my $param = $self->_map_param($self->param, %map); |
|
20 |
$self->param($param); |
|
21 |
} |
|
map cleanup
|
22 |
return $self; |
23 |
} |
|
24 | ||
added map method(not complet...
|
25 |
sub _map_param { |
26 |
my $self = shift; |
|
27 |
my $param = shift; |
|
28 |
|
|
29 |
return $param if !defined $param; |
|
30 |
|
|
31 |
my %map = @_; |
|
32 |
|
|
33 |
# Mapping |
|
34 |
my $map_param = {}; |
|
35 |
foreach my $key (keys %$param) { |
|
36 |
|
|
37 |
my $value_cb; |
|
38 |
my $condition; |
|
39 |
my $map_key; |
|
40 |
|
|
41 |
# Get mapping information |
|
42 |
if (ref $map{$key} eq 'ARRAY') { |
|
43 |
foreach my $some (@{$map{$key}}) { |
|
44 |
$map_key = $some unless ref $some; |
|
45 |
$condition = $some->{if} if ref $some eq 'HASH'; |
|
46 |
$value_cb = $some if ref $some eq 'CODE'; |
|
47 |
} |
|
48 |
} |
|
cleanup
|
49 |
elsif (defined $map{$key}) { |
added map method(not complet...
|
50 |
$map_key = $map{$key}; |
51 |
} |
|
cleanup
|
52 |
else { |
53 |
$map_key = $key; |
|
54 |
} |
|
55 |
|
|
added map method(not complet...
|
56 |
$value_cb ||= sub { $_[0] }; |
57 |
$condition ||= $self->if || 'exists'; |
|
58 | ||
59 |
# Map parameter |
|
60 |
my $value; |
|
61 |
if (ref $condition eq 'CODE') { |
|
62 |
if (ref $param->{$key} eq 'ARRAY') { |
|
63 |
$map_param->{$map_key} = []; |
|
64 |
for (my $i = 0; $i < @{$param->{$key}}; $i++) { |
|
65 |
$map_param->{$map_key}->[$i] |
|
66 |
= $condition->($param->{$key}->[$i]) ? $param->{$key}->[$i] |
|
67 |
: $self->dbi->not_exists; |
|
68 |
} |
|
69 |
} |
|
70 |
else { |
|
71 |
$map_param->{$map_key} = $value_cb->($param->{$key}) |
|
72 |
if $condition->($param->{$key}); |
|
73 |
} |
|
74 |
} |
|
75 |
elsif ($condition eq 'exists') { |
|
76 |
if (ref $param->{$key} eq 'ARRAY') { |
|
77 |
$map_param->{$map_key} = []; |
|
78 |
for (my $i = 0; $i < @{$param->{$key}}; $i++) { |
|
79 |
$map_param->{$map_key}->[$i] |
|
80 |
= exists $param->{$key}->[$i] ? $param->{$key}->[$i] |
|
81 |
: $self->dbi->not_exists; |
|
82 |
} |
|
83 |
} |
|
84 |
else { |
|
85 |
$map_param->{$map_key} = $value_cb->($param->{$key}) |
|
86 |
if exists $param->{$key}; |
|
87 |
} |
|
88 |
} |
|
89 |
else { croak qq/Condition must be code reference or "exists" / . _subname } |
|
90 |
} |
|
91 |
|
|
92 |
return $map_param; |
|
93 |
} |
|
94 | ||
95 |
sub if { |
|
96 |
my $self = shift; |
|
97 |
if (@_) { |
|
98 |
my $if = $_[0]; |
|
99 |
|
|
100 |
$if = $if eq 'exists' ? $if |
|
101 |
: $if eq 'defined' ? sub { defined $_[0] } |
|
added tests
|
102 |
: $if eq 'length' ? sub { defined $_[0] && length $_[0] } |
added map method(not complet...
|
103 |
: ref $if eq 'CODE' ? $if |
104 |
: undef; |
|
105 | ||
106 |
croak "You can must specify right value to C<if> " . _subname |
|
107 |
unless $if; |
|
108 | ||
109 |
$self->{if} = $if; |
|
110 |
return $self; |
|
111 |
} |
|
112 |
$self->{if} = 'exists' unless exists $self->{if}; |
|
113 |
return $self->{if}; |
|
114 |
} |
|
added experimental DBIx::Cus...
|
115 | |
improved error messages
|
116 |
sub new { |
117 |
my $self = shift->SUPER::new(@_); |
|
118 |
|
|
119 |
# Check attribute names |
|
120 |
my @attrs = keys %$self; |
|
121 |
foreach my $attr (@attrs) { |
|
cleanup
|
122 |
croak qq{"$attr" is invalid attribute name (} . _subname . ")" |
improved error messages
|
123 |
unless $self->can($attr); |
124 |
} |
|
125 |
|
|
126 |
return $self; |
|
127 |
} |
|
128 | ||
changed DBIx::Custom::Where ...
|
129 |
sub to_string { |
added test
|
130 |
my $self = shift; |
added experimental DBIx::Cus...
|
131 |
|
- update_param_tag is DEPREC...
|
132 |
# Check if column name is safety character; |
cleanup
|
133 |
my $safety = $self->dbi->safety_character; |
- update_param_tag is DEPREC...
|
134 |
if (ref $self->param eq 'HASH') { |
135 |
foreach my $column (keys %{$self->param}) { |
|
136 |
croak qq{"$column" is not safety column name (} . _subname . ")" |
|
137 |
unless $column =~ /^[$safety\.]+$/; |
|
138 |
} |
|
139 |
} |
|
many changed
|
140 |
# Clause |
added test
|
141 |
my $clause = $self->clause; |
changed DBIx::Custom::Where ...
|
142 |
$clause = ['and', $clause] unless ref $clause eq 'ARRAY'; |
added test
|
143 |
$clause->[0] = 'and' unless @$clause; |
added EXPERIMENTAL DBIx::Cus...
|
144 |
|
145 |
# Map condition |
|
added map method(not complet...
|
146 |
my $if = $self->if || ''; |
147 |
$if = $if eq 'exists' ? $if |
|
148 |
: $if eq 'defined' ? sub { defined $_[0] } |
|
149 |
: $if eq 'length' ? sub { length $_[0] } |
|
150 |
: ref $if eq 'CODE' ? $if |
|
added EXPERIMENTAL DBIx::Cus...
|
151 |
: undef; |
152 |
|
|
added map method(not complet...
|
153 |
croak "You can must specify right value to C<if> " . _subname |
154 |
unless $if; |
|
155 |
$self->{_if} = $if; |
|
156 |
|
|
many changed
|
157 |
# Parse |
added test
|
158 |
my $where = []; |
159 |
my $count = {}; |
|
many changed
|
160 |
$self->_parse($clause, $where, $count, 'and'); |
changed DBIx::Custom::Where ...
|
161 |
|
many changed
|
162 |
# Stringify |
163 |
unshift @$where, 'where' if @$where; |
|
added test
|
164 |
return join(' ', @$where); |
added experimental DBIx::Cus...
|
165 |
} |
166 | ||
added test
|
167 |
our %VALID_OPERATIONS = map { $_ => 1 } qw/and or/; |
many changed
|
168 |
sub _parse { |
added test
|
169 |
my ($self, $clause, $where, $count, $op) = @_; |
added experimental DBIx::Cus...
|
170 |
|
many changed
|
171 |
# Array |
changed DBIx::Custom::Where ...
|
172 |
if (ref $clause eq 'ARRAY') { |
many changed
|
173 |
|
174 |
# Start |
|
added test
|
175 |
push @$where, '('; |
changed DBIx::Custom::Where ...
|
176 |
|
many changed
|
177 |
# Operation |
changed DBIx::Custom::Where ...
|
178 |
my $op = $clause->[0] || ''; |
improved error message
|
179 |
croak qq{First argument must be "and" or "or" in where clause } . |
180 |
qq{"$op" is passed} . _subname . ")" |
|
changed DBIx::Custom::Where ...
|
181 |
unless $VALID_OPERATIONS{$op}; |
many changed
|
182 |
|
fixed DBIx::Custom::Where to...
|
183 |
my $pushed_array; |
many changed
|
184 |
# Parse internal clause |
changed DBIx::Custom::Where ...
|
185 |
for (my $i = 1; $i < @$clause; $i++) { |
many changed
|
186 |
my $pushed = $self->_parse($clause->[$i], $where, $count, $op); |
added test
|
187 |
push @$where, $op if $pushed; |
fixed DBIx::Custom::Where to...
|
188 |
$pushed_array = 1 if $pushed; |
changed DBIx::Custom::Where ...
|
189 |
} |
added test
|
190 |
pop @$where if $where->[-1] eq $op; |
191 |
|
|
many changed
|
192 |
# Undo |
added test
|
193 |
if ($where->[-1] eq '(') { |
194 |
pop @$where; |
|
fixed DBIx::Custom::Where to...
|
195 |
pop @$where if ($where->[-1] || '') eq $op; |
added test
|
196 |
} |
many changed
|
197 |
# End |
renamed DBIx::Custom::TagPro...
|
198 |
else { push @$where, ')' } |
fixed DBIx::Custom::Where to...
|
199 |
|
200 |
return $pushed_array; |
|
changed DBIx::Custom::Where ...
|
201 |
} |
many changed
|
202 |
|
203 |
# String |
|
changed DBIx::Custom::Where ...
|
204 |
else { |
DBIx::Custom::Where clause a...
|
205 |
# Pushed |
206 |
my $pushed; |
|
changed DBIx::Custom::Where ...
|
207 |
|
added test
|
208 |
# Column |
cleanup
|
209 |
my $columns = $self->dbi->query_builder->build_query($clause)->columns; |
DBIx::Custom::Where clause a...
|
210 |
if (@$columns == 0) { |
211 |
push @$where, $clause; |
|
212 |
$pushed = 1; |
|
213 |
return $pushed; |
|
214 |
} |
|
215 |
elsif (@$columns != 1) { |
|
- update_param_tag is DEPREC...
|
216 |
croak qq{Each part contains one column name: "$clause" (} |
cleanup
|
217 |
. _subname . ")"; |
DBIx::Custom::Where clause a...
|
218 |
} |
DBIx::Custom::Model type att...
|
219 |
|
220 |
# Remove quote |
|
changed DBIx::Custom::Where ...
|
221 |
my $column = $columns->[0]; |
sub module use DBIx::Custom ...
|
222 |
if (my $q = $self->dbi->_quote) { |
added quote method's two cha...
|
223 |
$q = quotemeta($q); |
224 |
$column =~ s/[$q]//g; |
|
added EXPERIMENTAL reserved_...
|
225 |
} |
226 |
|
|
DBIx::Custom::Model type att...
|
227 |
# Check safety |
cleanup
|
228 |
my $safety = $self->dbi->safety_character; |
cleanup
|
229 |
croak qq{"$column" is not safety column name (} . _subname . ")" |
- remaned experimental safty...
|
230 |
unless $column =~ /^[$safety\.]+$/; |
changed DBIx::Custom::Where ...
|
231 |
|
many changed
|
232 |
# Column count up |
added test
|
233 |
my $count = ++$count->{$column}; |
changed DBIx::Custom::Where ...
|
234 |
|
many changed
|
235 |
# Push |
map cleanup
|
236 |
my $param = $self->param; |
added experimental not_exist...
|
237 |
if (ref $param eq 'HASH') { |
renamed DBIx::Custom::TagPro...
|
238 |
if (exists $param->{$column}) { |
added map method(not complet...
|
239 |
my $if = $self->{_if}; |
added EXPERIMENTAL DBIx::Cus...
|
240 |
|
renamed DBIx::Custom::TagPro...
|
241 |
if (ref $param->{$column} eq 'ARRAY') { |
cleanup
|
242 |
$pushed = 1 if exists $param->{$column}->[$count - 1] |
243 |
&& ref $param->{$column}->[$count - 1] ne 'DBIx::Custom::NotExists' |
|
renamed DBIx::Custom::TagPro...
|
244 |
} |
cleanup
|
245 |
elsif ($count == 1) { $pushed = 1 } |
added experimental DBIx::Cus...
|
246 |
} |
renamed DBIx::Custom::TagPro...
|
247 |
push @$where, $clause if $pushed; |
248 |
} |
|
added experimental not_exist...
|
249 |
elsif (!defined $param) { |
renamed DBIx::Custom::TagPro...
|
250 |
push @$where, $clause; |
251 |
$pushed = 1; |
|
added experimental DBIx::Cus...
|
252 |
} |
improved error messages
|
253 |
else { |
cleanup
|
254 |
croak "Parameter must be hash reference or undfined value (" |
255 |
. _subname . ")" |
|
improved error messages
|
256 |
} |
added test
|
257 |
return $pushed; |
added experimental DBIx::Cus...
|
258 |
} |
fixed DBIx::Custom::Where to...
|
259 |
return; |
added experimental DBIx::Cus...
|
260 |
} |
261 | ||
262 |
1; |
|
263 | ||
264 |
=head1 NAME |
|
265 | ||
removed EXPERIMETNAL flag fr...
|
266 |
DBIx::Custom::Where - Where clause |
added experimental DBIx::Cus...
|
267 | |
268 |
=head1 SYNOPSYS |
|
269 | ||
many changed
|
270 |
my $where = DBIx::Custom::Where->new; |
DBIx::Custom::Model type att...
|
271 |
my $string_where = "$where"; |
added experimental DBIx::Cus...
|
272 | |
273 |
=head1 ATTRIBUTES |
|
274 | ||
275 |
=head2 C<clause> |
|
276 | ||
DBIx::Custom::Model type att...
|
277 |
my $clause = $where->clause; |
278 |
$where = $where->clause( |
|
279 |
['and', |
|
280 |
'title = :title', |
|
281 |
['or', 'date < :date', 'date > :date'] |
|
282 |
] |
|
added test
|
283 |
); |
added experimental DBIx::Cus...
|
284 | |
many changed
|
285 |
Where clause. Above one is expanded to the following SQL by to_string |
286 |
If all parameter names is exists. |
|
287 | ||
DBIx::Custom::Model type att...
|
288 |
"where ( title = :title and ( date < :date or date > :date ) )" |
added experimental DBIx::Cus...
|
289 | |
map cleanup
|
290 |
=head2 C<param> |
291 | ||
292 |
my $param = $where->param; |
|
293 |
$where = $where->param({ |
|
294 |
title => 'Perl', |
|
295 |
date => ['2010-11-11', '2011-03-05'], |
|
296 |
}); |
|
297 | ||
298 |
=head2 C<dbi> |
|
299 | ||
300 |
my $dbi = $where->dbi; |
|
301 |
$where = $where->dbi($dbi); |
|
302 | ||
303 |
L<DBIx::Custom> object. |
|
304 | ||
305 |
=head1 METHODS |
|
306 | ||
307 |
L<DBIx::Custom::Where> inherits all methods from L<Object::Simple> |
|
308 |
and implements the following new ones. |
|
309 | ||
310 |
=head2 C<if EXPERIMENTAL> |
|
311 |
|
|
312 |
my $if = $where->if($condition); |
|
313 |
$where->if($condition); |
|
314 | ||
315 |
C<if> is default of C<map> method C<if> option. |
|
316 | ||
added map method(not complet...
|
317 |
=head2 C<map EXPERIMENTAL> |
318 | ||
updated pod
|
319 |
Mapping parameter key and value. C<param> is converted based on, |
map cleanup
|
320 |
so this method must be called after C<param> is set. |
updated pod
|
321 |
Set C<if> if you need before C<map> method call. |
added map method(not complet...
|
322 | |
map cleanup
|
323 |
$where->map( |
added map method(not complet...
|
324 |
'id' => 'book.id', |
325 |
'author' => ['book.author' => sub { '%' . $_[0] . '%' }], |
|
326 |
'price' => [ |
|
327 |
'book.price', {if => sub { length $_[0] } |
|
328 |
] |
|
map cleanup
|
329 |
); |
added map method(not complet...
|
330 | |
updated pod
|
331 |
The followin parameter |
332 | ||
333 |
{ |
|
334 |
id => 1, |
|
335 |
auhtor => 'Ken', |
|
336 |
price => 1000 |
|
337 |
} |
|
338 | ||
339 |
is converted to |
|
340 | ||
341 |
{ |
|
342 |
'book.id' => 1, |
|
343 |
'book.author' => '%Ken%', |
|
344 |
'book.price' => 1000 |
|
345 |
} |
|
346 | ||
added map method(not complet...
|
347 |
The following option is available. |
added EXPERIMENTAL DBIx::Cus...
|
348 | |
added map method(not complet...
|
349 |
=over 4 |
added EXPERIMENTAL DBIx::Cus...
|
350 | |
added map method(not complet...
|
351 |
=item * C<if> |
352 | ||
353 |
By default, if parameter key is exists, mapping is done. |
|
added EXPERIMENTAL DBIx::Cus...
|
354 |
|
added map method(not complet...
|
355 |
if => 'exists'; |
added EXPERIMENTAL DBIx::Cus...
|
356 | |
357 |
In case C<defined> is specified, if the value is defined, |
|
358 |
mapping is done. |
|
359 | ||
added map method(not complet...
|
360 |
if => 'defined'; |
added EXPERIMENTAL DBIx::Cus...
|
361 | |
362 |
In case C<length> is specified, the value is defined |
|
363 |
and the length is bigger than 0, mappting is done. |
|
364 | ||
added map method(not complet...
|
365 |
if => 'length'; |
added EXPERIMENTAL DBIx::Cus...
|
366 | |
367 |
You can also subroutine like C<sub { defined $_[0] }> for mappging. |
|
368 | ||
added map method(not complet...
|
369 |
if => sub { defined $_[0] } |
370 | ||
371 |
=back |
|
added EXPERIMENTAL DBIx::Cus...
|
372 | |
added experimental DBIx::Cus...
|
373 |
=head2 C<to_string> |
374 | ||
375 |
$where->to_string; |
|
added test
|
376 | |
DBIx::Custom::Model type att...
|
377 |
Convert where clause to string. |
378 | ||
379 |
double quote is override to execute C<to_string> method. |
|
380 | ||
381 |
my $string_where = "$where"; |
|
added test
|
382 | |
- update_param_tag is DEPREC...
|
383 |
=cut |