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 |
|
|
improved where document
|
153 |
# Clause |
154 |
$where->clause(['and', 'title like :title', 'price = :price']); |
|
cleanup
|
155 |
$where->clause(['and', ':title{like}', ':price{=}']); |
156 |
|
|
improved where document
|
157 |
# Stringify where clause |
cleanup
|
158 |
my $where_clause = "$where"; |
improved where document
|
159 |
my $where_clause = $where->to_string; |
160 |
# -> where title like :title and price = :price |
|
cleanup
|
161 |
|
162 |
# Only price condition |
|
163 |
$where->clause(['and', ':title{like}', ':price{=}']); |
|
164 |
$where->param({price => 1900}); |
|
improved where document
|
165 |
# -> where price = :price |
cleanup
|
166 |
|
167 |
# Only title condition |
|
168 |
$where->clause(['and', ':title{like}', ':price{=}']); |
|
169 |
$where->param({title => 'Perl'}); |
|
improved where document
|
170 |
# -> where title like :title |
cleanup
|
171 |
|
172 |
# Nothing |
|
173 |
$where->clause(['and', ':title{like}', ':price{=}']); |
|
174 |
$where->param({}); |
|
improved where document
|
175 |
# => Nothing |
cleanup
|
176 |
|
177 |
# or condition |
|
178 |
$where->clause(['or', ':title{like}', ':price{=}']); |
|
improved where document
|
179 |
# -> where title = :title or price like :price |
cleanup
|
180 |
|
181 |
# More than one parameter |
|
182 |
$where->clause(['and', ':price{>}', ':price{<}']); |
|
183 |
$where->param({price => [1000, 2000]}); |
|
improved where document
|
184 |
# -> where price > :price and price < :price |
cleanup
|
185 |
|
186 |
# Only first condition |
|
187 |
$where->clause(['and', ':price{>}', ':price{<}']); |
|
188 |
$where->param({price => [1000, $dbi->not_exists]}); |
|
improved where document
|
189 |
# -> where price > :price |
cleanup
|
190 |
|
191 |
# Only second condition |
|
192 |
$where->clause(['and', ':price{>}', ':price{<}']); |
|
193 |
$where->param({price => [$dbi->not_exists, 2000]}); |
|
improved where document
|
194 |
# -> where price < :price |
cleanup
|
195 |
|
196 |
# More complex condition |
|
197 |
$where->clause( |
|
198 |
[ |
|
199 |
'and', |
|
200 |
':price{=}', |
|
201 |
['or', ':title{=}', ':title{=}', ':title{=}'] |
|
202 |
] |
|
203 |
); |
|
improved where document
|
204 |
# -> pirce = :price and (title = :title or title = :title or tilte = :title) |
cleanup
|
205 |
|
206 |
# Using Full-qualified column name |
|
207 |
$where->clause(['and', ':book.title{like}', ':book.price{=}']); |
|
improved where document
|
208 |
# -> book.title like :book.title and book.price = :book.price |
added experimental DBIx::Cus...
|
209 | |
210 |
=head1 ATTRIBUTES |
|
211 | ||
cleanup and added deprecated...
|
212 |
=head2 clause |
added experimental DBIx::Cus...
|
213 | |
cleanup
|
214 |
my $clause = $where->clause; |
215 |
$where = $where->clause( |
|
216 |
['and', |
|
217 |
':title{=}', |
|
218 |
['or', ':date{<}', ':date{>}'] |
|
219 |
] |
|
220 |
); |
|
added experimental DBIx::Cus...
|
221 | |
many changed
|
222 |
Where clause. Above one is expanded to the following SQL by to_string |
223 |
If all parameter names is exists. |
|
224 | ||
cleanup
|
225 |
where title = :title and ( date < :date or date > :date ) |
added experimental DBIx::Cus...
|
226 | |
cleanup and added deprecated...
|
227 |
=head2 param |
map cleanup
|
228 | |
cleanup
|
229 |
my $param = $where->param; |
230 |
$where = $where->param({ |
|
231 |
title => 'Perl', |
|
232 |
date => ['2010-11-11', '2011-03-05'], |
|
233 |
}); |
|
map cleanup
|
234 | |
cleanup and added deprecated...
|
235 |
=head2 dbi |
map cleanup
|
236 | |
cleanup
|
237 |
my $dbi = $where->dbi; |
238 |
$where = $where->dbi($dbi); |
|
map cleanup
|
239 | |
240 |
L<DBIx::Custom> object. |
|
241 | ||
242 |
=head1 METHODS |
|
243 | ||
244 |
L<DBIx::Custom::Where> inherits all methods from L<Object::Simple> |
|
245 |
and implements the following new ones. |
|
246 | ||
cleanup and added deprecated...
|
247 |
=head2 to_string |
added experimental DBIx::Cus...
|
248 | |
cleanup
|
249 |
$where->to_string; |
added test
|
250 | |
DBIx::Custom::Model type att...
|
251 |
Convert where clause to string. |
252 | ||
253 |
double quote is override to execute C<to_string> method. |
|
254 | ||
cleanup
|
255 |
my $string_where = "$where"; |
added test
|
256 | |
- update_param_tag is DEPREC...
|
257 |
=cut |