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 | ||
improved error messages
|
15 |
sub new { |
16 |
my $self = shift->SUPER::new(@_); |
|
17 |
|
|
18 |
# Check attribute names |
|
19 |
my @attrs = keys %$self; |
|
cleanup
|
20 |
for my $attr (@attrs) { |
cleanup
|
21 |
croak qq{"$attr" is invalid attribute name (} . _subname . ")" |
improved error messages
|
22 |
unless $self->can($attr); |
23 |
} |
|
24 |
|
|
25 |
return $self; |
|
26 |
} |
|
27 | ||
changed DBIx::Custom::Where ...
|
28 |
sub to_string { |
added test
|
29 |
my $self = shift; |
added experimental DBIx::Cus...
|
30 |
|
many changed
|
31 |
# Clause |
added test
|
32 |
my $clause = $self->clause; |
changed DBIx::Custom::Where ...
|
33 |
$clause = ['and', $clause] unless ref $clause eq 'ARRAY'; |
added test
|
34 |
$clause->[0] = 'and' unless @$clause; |
added EXPERIMENTAL DBIx::Cus...
|
35 |
|
many changed
|
36 |
# Parse |
added test
|
37 |
my $where = []; |
38 |
my $count = {}; |
|
micro optimization
|
39 |
$self->{_query_builder} = $self->dbi->query_builder; |
40 |
$self->{_safety_character} = $self->dbi->safety_character; |
|
41 |
$self->{_quote} = $self->dbi->_quote; |
|
cleanup
|
42 |
$self->{_tag_parse} = exists $ENV{DBIX_CUSTOM_TAG_PARSE} |
43 |
? $ENV{DBIX_CUSTOM_TAG_PARSE} : $self->dbi->{tag_parse}; |
|
many changed
|
44 |
$self->_parse($clause, $where, $count, 'and'); |
micro optimization
|
45 | |
many changed
|
46 |
# Stringify |
47 |
unshift @$where, 'where' if @$where; |
|
added test
|
48 |
return join(' ', @$where); |
added experimental DBIx::Cus...
|
49 |
} |
- DBIx::Custom::QueryBuilder...
|
50 |
|
added test
|
51 |
our %VALID_OPERATIONS = map { $_ => 1 } qw/and or/; |
many changed
|
52 |
sub _parse { |
micro optimization
|
53 |
my ($self, $clause, $where, $count, $op, $info) = @_; |
added experimental DBIx::Cus...
|
54 |
|
many changed
|
55 |
# Array |
changed DBIx::Custom::Where ...
|
56 |
if (ref $clause eq 'ARRAY') { |
many changed
|
57 |
|
58 |
# Start |
|
added test
|
59 |
push @$where, '('; |
changed DBIx::Custom::Where ...
|
60 |
|
many changed
|
61 |
# Operation |
changed DBIx::Custom::Where ...
|
62 |
my $op = $clause->[0] || ''; |
improved error message
|
63 |
croak qq{First argument must be "and" or "or" in where clause } . |
64 |
qq{"$op" is passed} . _subname . ")" |
|
changed DBIx::Custom::Where ...
|
65 |
unless $VALID_OPERATIONS{$op}; |
many changed
|
66 |
|
fixed DBIx::Custom::Where to...
|
67 |
my $pushed_array; |
many changed
|
68 |
# Parse internal clause |
changed DBIx::Custom::Where ...
|
69 |
for (my $i = 1; $i < @$clause; $i++) { |
many changed
|
70 |
my $pushed = $self->_parse($clause->[$i], $where, $count, $op); |
added test
|
71 |
push @$where, $op if $pushed; |
fixed DBIx::Custom::Where to...
|
72 |
$pushed_array = 1 if $pushed; |
changed DBIx::Custom::Where ...
|
73 |
} |
added test
|
74 |
pop @$where if $where->[-1] eq $op; |
75 |
|
|
many changed
|
76 |
# Undo |
added test
|
77 |
if ($where->[-1] eq '(') { |
78 |
pop @$where; |
|
fixed DBIx::Custom::Where to...
|
79 |
pop @$where if ($where->[-1] || '') eq $op; |
added test
|
80 |
} |
many changed
|
81 |
# End |
renamed DBIx::Custom::TagPro...
|
82 |
else { push @$where, ')' } |
fixed DBIx::Custom::Where to...
|
83 |
|
84 |
return $pushed_array; |
|
changed DBIx::Custom::Where ...
|
85 |
} |
many changed
|
86 |
|
87 |
# String |
|
changed DBIx::Custom::Where ...
|
88 |
else { |
DBIx::Custom::Where clause a...
|
89 |
# Pushed |
90 |
my $pushed; |
|
changed DBIx::Custom::Where ...
|
91 |
|
added test
|
92 |
# Column |
micro optimization
|
93 |
my $c = $self->{_safety_character}; |
94 |
|
|
95 |
my $column; |
|
cleanup
|
96 |
if ($self->{_tag_parse} && ($clause =~ /\s\{/ || $clause =~ /^\{/)) { |
micro optimization
|
97 |
my $columns = $self->dbi->query_builder->build_query($clause)->{columns}; |
98 |
$column = $columns->[0]; |
|
99 |
} |
|
fixed where clause parsing b...
|
100 |
else { |
micro optimization
|
101 |
my $sql = " " . $clause || ''; |
fixed where clause parsing b...
|
102 |
$sql =~ s/([0-9]):/$1\\:/g; |
micro optimization
|
103 |
($column) = $sql =~ /[^\\]:([$c\.]+)/s |
fixed where clause parsing b...
|
104 |
} |
micro optimization
|
105 |
unless (defined $column) { |
DBIx::Custom::Where clause a...
|
106 |
push @$where, $clause; |
107 |
$pushed = 1; |
|
108 |
return $pushed; |
|
109 |
} |
|
added EXPERIMENTAL reserved_...
|
110 |
|
many changed
|
111 |
# Column count up |
added test
|
112 |
my $count = ++$count->{$column}; |
changed DBIx::Custom::Where ...
|
113 |
|
many changed
|
114 |
# Push |
micro optimization
|
115 |
my $param = $self->{param}; |
added experimental not_exist...
|
116 |
if (ref $param eq 'HASH') { |
renamed DBIx::Custom::TagPro...
|
117 |
if (exists $param->{$column}) { |
added map method(not complet...
|
118 |
my $if = $self->{_if}; |
added EXPERIMENTAL DBIx::Cus...
|
119 |
|
renamed DBIx::Custom::TagPro...
|
120 |
if (ref $param->{$column} eq 'ARRAY') { |
cleanup
|
121 |
$pushed = 1 if exists $param->{$column}->[$count - 1] |
122 |
&& ref $param->{$column}->[$count - 1] ne 'DBIx::Custom::NotExists' |
|
renamed DBIx::Custom::TagPro...
|
123 |
} |
cleanup
|
124 |
elsif ($count == 1) { $pushed = 1 } |
added experimental DBIx::Cus...
|
125 |
} |
renamed DBIx::Custom::TagPro...
|
126 |
push @$where, $clause if $pushed; |
127 |
} |
|
added experimental not_exist...
|
128 |
elsif (!defined $param) { |
renamed DBIx::Custom::TagPro...
|
129 |
push @$where, $clause; |
130 |
$pushed = 1; |
|
added experimental DBIx::Cus...
|
131 |
} |
improved error messages
|
132 |
else { |
cleanup
|
133 |
croak "Parameter must be hash reference or undfined value (" |
134 |
. _subname . ")" |
|
improved error messages
|
135 |
} |
added test
|
136 |
return $pushed; |
added experimental DBIx::Cus...
|
137 |
} |
fixed DBIx::Custom::Where to...
|
138 |
return; |
added experimental DBIx::Cus...
|
139 |
} |
140 |
1; |
|
141 | ||
142 |
=head1 NAME |
|
143 | ||
removed EXPERIMETNAL flag fr...
|
144 |
DBIx::Custom::Where - Where clause |
added experimental DBIx::Cus...
|
145 | |
146 |
=head1 SYNOPSYS |
|
- id option work if id count...
|
147 |
|
148 |
# Create DBIx::Custom::Where object |
|
149 |
my $where = $dbi->where; |
|
150 |
|
|
151 |
# Set clause and parameter |
|
152 |
$where->clause(['and', ':title{like}', ':price{=}']); |
|
153 |
|
|
154 |
# Create where clause by to_string method |
|
155 |
my $where_clause = $where->to_string; |
|
156 |
|
|
157 |
# Create where clause by stringify |
|
158 |
my $where_clause = "$where"; |
|
159 |
|
|
160 |
# Created where clause in the above way |
|
161 |
where :title{=} and :price{like} |
|
162 |
|
|
163 |
# Only price condition |
|
164 |
$where->clause(['and', ':title{like}', ':price{=}']); |
|
165 |
$where->param({price => 1900}); |
|
166 |
my $where_clause = "$where"; |
|
167 |
|
|
168 |
# Created where clause in the above way |
|
169 |
where :price{=} |
|
170 |
|
|
171 |
# Only title condition |
|
172 |
$where->clause(['and', ':title{like}', ':price{=}']); |
|
173 |
$where->param({title => 'Perl'}); |
|
174 |
my $where_clause = "$where"; |
|
175 |
|
|
176 |
# Created where clause in the above way |
|
177 |
where :title{like} |
|
178 |
|
|
179 |
# Nothing |
|
180 |
$where->clause(['and', ':title{like}', ':price{=}']); |
|
181 |
$where->param({}); |
|
182 |
my $where_clause = "$where"; |
|
183 |
|
|
184 |
# or condition |
|
185 |
$where->clause(['or', ':title{like}', ':price{=}']); |
|
186 |
|
|
187 |
# More than one parameter |
|
188 |
$where->clause(['and', ':price{>}', ':price{<}']); |
|
189 |
$where->param({price => [1000, 2000]}); |
|
190 |
|
|
191 |
# Only first condition |
|
192 |
$where->clause(['and', ':price{>}', ':price{<}']); |
|
193 |
$where->param({price => [1000, $dbi->not_exists]}); |
|
194 |
|
|
195 |
# Only second condition |
|
196 |
$where->clause(['and', ':price{>}', ':price{<}']); |
|
197 |
$where->param({price => [$dbi->not_exists, 2000]}); |
|
198 |
|
|
199 |
# More complex condition |
|
200 |
$where->clause( |
|
201 |
[ |
|
202 |
'and', |
|
203 |
':price{=}', |
|
204 |
['or', ':title{=}', ':title{=}', ':title{=}'] |
|
205 |
] |
|
206 |
); |
|
207 |
my $where_clause = "$where"; |
|
208 |
|
|
209 |
# Created where clause in the above way |
|
210 |
where :price{=} and (:title{=} or :title{=} or :title{=}) |
|
211 |
|
|
212 |
# Using Full-qualified column name |
|
213 |
$where->clause(['and', ':book.title{like}', ':book.price{=}']); |
|
added experimental DBIx::Cus...
|
214 | |
215 |
=head1 ATTRIBUTES |
|
216 | ||
217 |
=head2 C<clause> |
|
218 | ||
DBIx::Custom::Model type att...
|
219 |
my $clause = $where->clause; |
220 |
$where = $where->clause( |
|
221 |
['and', |
|
- id option work if id count...
|
222 |
':title{=}', |
223 |
['or', ':date{<}', ':date{>}'] |
|
DBIx::Custom::Model type att...
|
224 |
] |
added test
|
225 |
); |
added experimental DBIx::Cus...
|
226 | |
many changed
|
227 |
Where clause. Above one is expanded to the following SQL by to_string |
228 |
If all parameter names is exists. |
|
229 | ||
- id option work if id count...
|
230 |
where title = :title and ( date < :date or date > :date ) |
added experimental DBIx::Cus...
|
231 | |
map cleanup
|
232 |
=head2 C<param> |
233 | ||
234 |
my $param = $where->param; |
|
235 |
$where = $where->param({ |
|
236 |
title => 'Perl', |
|
237 |
date => ['2010-11-11', '2011-03-05'], |
|
238 |
}); |
|
239 | ||
240 |
=head2 C<dbi> |
|
241 | ||
242 |
my $dbi = $where->dbi; |
|
243 |
$where = $where->dbi($dbi); |
|
244 | ||
245 |
L<DBIx::Custom> object. |
|
246 | ||
247 |
=head1 METHODS |
|
248 | ||
249 |
L<DBIx::Custom::Where> inherits all methods from L<Object::Simple> |
|
250 |
and implements the following new ones. |
|
251 | ||
added experimental DBIx::Cus...
|
252 |
=head2 C<to_string> |
253 | ||
254 |
$where->to_string; |
|
added test
|
255 | |
DBIx::Custom::Model type att...
|
256 |
Convert where clause to string. |
257 | ||
258 |
double quote is override to execute C<to_string> method. |
|
259 | ||
260 |
my $string_where = "$where"; |
|
added test
|
261 | |
- update_param_tag is DEPREC...
|
262 |
=cut |