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