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