renamed default_query_filter...
|
1 |
package DBIx::Custom::QueryBuilder; |
2 | ||
3 |
use strict; |
|
4 |
use warnings; |
|
5 | ||
6 |
use base 'Object::Simple'; |
|
7 | ||
8 |
use Carp 'croak'; |
|
9 |
use DBIx::Custom::Query; |
|
10 | ||
fixed Carp trast relationshi...
|
11 |
# Carp trust relationship |
12 |
push @DBIx::Custom::CARP_NOT, __PACKAGE__; |
|
updated document
|
13 |
push @DBIx::Custom::Where::CARP_NOT, __PACKAGE__; |
14 | ||
cleanup
|
15 |
# Attributes |
cleanup
|
16 |
__PACKAGE__->attr('tags' => sub { {} }); |
renamed default_query_filter...
|
17 | |
cleanup
|
18 |
sub build_query { |
19 |
my ($self, $source) = @_; |
|
20 |
|
|
21 |
# Parse |
|
22 |
my $tree = $self->_parse($source); |
|
23 |
|
|
24 |
# Build query |
|
25 |
my $query = $self->_build_query($tree); |
|
26 |
|
|
27 |
return $query; |
|
28 |
} |
|
29 | ||
renamed DBIx::Custom::TagPro...
|
30 |
sub register_tag { |
renamed default_query_filter...
|
31 |
my $self = shift; |
changed argument of tag proc...
|
32 |
|
cleanup
|
33 |
# Merge tag |
renamed DBIx::Custom::TagPro...
|
34 |
my $tags = ref $_[0] eq 'HASH' ? $_[0] : {@_}; |
35 |
$self->tags({%{$self->tags}, %$tags}); |
|
changed argument of tag proc...
|
36 |
|
renamed default_query_filter...
|
37 |
return $self; |
38 |
} |
|
39 | ||
cleanup
|
40 |
sub _build_query { |
41 |
my ($self, $tree) = @_; |
|
renamed default_query_filter...
|
42 |
|
cleanup
|
43 |
# SQL |
44 |
my $sql = ''; |
|
renamed default_query_filter...
|
45 |
|
cleanup
|
46 |
# All Columns |
47 |
my $all_columns = []; |
|
48 |
|
|
49 |
# Build SQL |
|
50 |
foreach my $node (@$tree) { |
|
51 |
|
|
52 |
# Text |
|
53 |
if ($node->{type} eq 'text') { $sql .= $node->{value} } |
|
54 |
|
|
55 |
# Tag |
|
56 |
else { |
|
57 |
|
|
58 |
# Tag name |
|
59 |
my $tag_name = $node->{tag_name}; |
|
60 |
|
|
61 |
# Tag arguments |
|
62 |
my $tag_args = $node->{tag_args}; |
|
63 |
|
|
cleanup
|
64 |
# Get tag |
65 |
my $tag = $self->tag_processors->{$tag_name} |
|
renamed DBIx::Custom::TagPro...
|
66 |
|| $self->tags->{$tag_name}; |
cleanup
|
67 |
|
cleanup
|
68 |
# Tag is not registered |
cleanup
|
69 |
croak qq{Tag "$tag_name" in "{a }" is not registered} |
cleanup
|
70 |
unless $tag; |
cleanup
|
71 |
|
cleanup
|
72 |
# Tag not sub reference |
73 |
croak qq{Tag "$tag_name" must be sub reference} |
|
74 |
unless ref $tag eq 'CODE'; |
|
cleanup
|
75 |
|
cleanup
|
76 |
# Execute tag |
77 |
my $r = $tag->(@$tag_args); |
|
cleanup
|
78 |
|
cleanup
|
79 |
# Check tag return value |
80 |
croak qq{Tag "$tag_name" must return [STRING, ARRAY_REFERENCE]} |
|
cleanup
|
81 |
unless ref $r eq 'ARRAY' && defined $r->[0] && ref $r->[1] eq 'ARRAY'; |
82 |
|
|
83 |
# Part of SQL statement and colum names |
|
84 |
my ($part, $columns) = @$r; |
|
85 |
|
|
86 |
# Add columns |
|
87 |
push @$all_columns, @$columns; |
|
88 |
|
|
89 |
# Join part tag to SQL |
|
90 |
$sql .= $part; |
|
91 |
} |
|
92 |
} |
|
93 | ||
94 |
# Check placeholder count |
|
95 |
my $placeholder_count = $self->_placeholder_count($sql); |
|
96 |
my $column_count = @$all_columns; |
|
97 |
croak qq{Placeholder count in "$sql" must be same as column count $column_count} |
|
98 |
unless $placeholder_count eq @$all_columns; |
|
99 |
|
|
100 |
# Add semicolon |
|
101 |
$sql .= ';' unless $sql =~ /;$/; |
|
102 |
|
|
103 |
# Query |
|
104 |
my $query = DBIx::Custom::Query->new(sql => $sql, columns => $all_columns); |
|
renamed default_query_filter...
|
105 |
|
106 |
return $query; |
|
107 |
} |
|
108 | ||
109 |
sub _parse { |
|
110 |
my ($self, $source) = @_; |
|
111 |
|
|
remove DBIx::Custom::QueryBu...
|
112 |
# Source |
renamed default_query_filter...
|
113 |
$source ||= ''; |
fixed tests
|
114 | |
remove DBIx::Custom::QueryBu...
|
115 |
# Tree |
removed DBIx::Custom::Query ...
|
116 |
my @tree; |
remove DBIx::Custom::QueryBu...
|
117 |
|
removed DBIx::Custom::Query ...
|
118 |
# Value |
119 |
my $value = ''; |
|
renamed default_query_filter...
|
120 |
|
removed DBIx::Custom::Query ...
|
121 |
# State |
renamed default_query_filter...
|
122 |
my $state = 'text'; |
123 |
|
|
removed DBIx::Custom::Query ...
|
124 |
# Before charactor |
125 |
my $before = ''; |
|
126 | ||
127 |
# Position |
|
fixed DBIx::Custom::QueryBui...
|
128 |
my $pos = 0; |
renamed default_query_filter...
|
129 |
|
130 |
# Parse |
|
added tests
|
131 |
my $original = $source; |
fixed DBIx::Custom::QueryBui...
|
132 |
while (defined(my $c = substr($source, $pos, 1))) { |
133 |
|
|
134 |
# Last |
|
135 |
last unless length $c; |
|
renamed default_query_filter...
|
136 |
|
removed DBIx::Custom::Query ...
|
137 |
# State is text |
138 |
if ($state eq 'text') { |
|
139 |
|
|
140 |
# Tag start charactor |
|
141 |
if ($c eq '{') { |
|
142 |
|
|
143 |
# Escaped charactor |
|
144 |
if ($before eq "\\") { |
|
145 |
substr($value, -1, 1, ''); |
|
146 |
$value .= $c; |
|
147 |
} |
|
148 |
|
|
149 |
# Tag start |
|
150 |
else { |
|
151 |
|
|
152 |
# Change state |
|
153 |
$state = 'tag'; |
|
154 |
|
|
155 |
# Add text |
|
156 |
push @tree, {type => 'text', value => $value} |
|
157 |
if $value; |
|
158 |
|
|
159 |
# Clear |
|
160 |
$value = ''; |
|
161 |
} |
|
162 |
} |
|
163 |
|
|
164 |
# Tag end charactor |
|
165 |
elsif ($c eq '}') { |
|
166 |
|
|
167 |
# Escaped charactor |
|
168 |
if ($before eq "\\") { |
|
169 |
substr($value, -1, 1, ''); |
|
170 |
$value .= $c; |
|
171 |
} |
|
172 |
|
|
173 |
# Unexpected |
|
174 |
else { |
|
175 |
croak qq/Parsing error. unexpected "}". / . |
|
added tests
|
176 |
qq/pos $pos of "$original"/; |
removed DBIx::Custom::Query ...
|
177 |
} |
178 |
} |
|
179 |
|
|
180 |
# Normal charactor |
|
181 |
else { $value .= $c } |
|
182 |
} |
|
renamed default_query_filter...
|
183 |
|
removed DBIx::Custom::Query ...
|
184 |
# State is tags |
added tests
|
185 |
else { |
removed DBIx::Custom::Query ...
|
186 |
|
187 |
# Tag start charactor |
|
188 |
if ($c eq '{') { |
|
renamed default_query_filter...
|
189 |
|
removed DBIx::Custom::Query ...
|
190 |
# Escaped charactor |
191 |
if ($before eq "\\") { |
|
192 |
substr($value, -1, 1, ''); |
|
193 |
$value .= $c; |
|
194 |
} |
|
195 |
|
|
196 |
# Unexpected |
|
197 |
else { |
|
198 |
croak qq/Parsing error. unexpected "{". / . |
|
added tests
|
199 |
qq/pos $pos of "$original"/; |
removed DBIx::Custom::Query ...
|
200 |
} |
201 |
} |
|
renamed default_query_filter...
|
202 |
|
removed DBIx::Custom::Query ...
|
203 |
# Tag end charactor |
204 |
elsif ($c eq '}') { |
|
205 |
|
|
206 |
# Escaped charactor |
|
207 |
if ($before eq "\\") { |
|
208 |
substr($value, -1, 1, ''); |
|
209 |
$value .= $c; |
|
210 |
} |
|
211 |
|
|
212 |
# Tag end |
|
213 |
else { |
|
214 |
|
|
215 |
# Change state |
|
216 |
$state = 'text'; |
|
217 |
|
|
218 |
# Add tag |
|
219 |
my ($tag_name, @tag_args) = split /\s+/, $value; |
|
220 |
push @tree, {type => 'tag', tag_name => $tag_name, |
|
221 |
tag_args => \@tag_args}; |
|
222 |
|
|
223 |
# Clear |
|
224 |
$value = ''; |
|
225 |
} |
|
226 |
} |
|
227 |
|
|
228 |
# Normal charactor |
|
229 |
else { $value .= $c } |
|
renamed default_query_filter...
|
230 |
} |
removed DBIx::Custom::Query ...
|
231 |
|
232 |
# Save before charactor |
|
233 |
$before = $c; |
|
234 |
|
|
235 |
# increment position |
|
236 |
$pos++; |
|
renamed default_query_filter...
|
237 |
} |
238 |
|
|
removed DBIx::Custom::Query ...
|
239 |
# Tag not finished |
added tests
|
240 |
croak qq{Tag not finished. "$original"} |
removed DBIx::Custom::Query ...
|
241 |
if $state eq 'tag'; |
renamed default_query_filter...
|
242 |
|
removed DBIx::Custom::Query ...
|
243 |
# Add rest text |
244 |
push @tree, {type => 'text', value => $value} |
|
245 |
if $value; |
|
246 |
|
|
247 |
return \@tree; |
|
renamed default_query_filter...
|
248 |
} |
249 | ||
250 |
sub _placeholder_count { |
|
251 |
my ($self, $expand) = @_; |
|
252 |
|
|
changed argument of tag proc...
|
253 |
# Count |
254 |
$expand ||= ''; |
|
renamed default_query_filter...
|
255 |
my $count = 0; |
256 |
my $pos = -1; |
|
257 |
while (($pos = index($expand, '?', $pos + 1)) != -1) { |
|
258 |
$count++; |
|
259 |
} |
|
260 |
return $count; |
|
261 |
} |
|
262 | ||
cleanup
|
263 |
# DEPRECATED! |
renamed DBIx::Custom::TagPro...
|
264 |
__PACKAGE__->attr('tag_processors' => sub { {} }); |
265 | ||
cleanup
|
266 |
# DEPRECATED! |
renamed DBIx::Custom::TagPro...
|
267 |
sub register_tag_processor { |
268 |
my $self = shift; |
|
269 |
|
|
cleanup
|
270 |
# Merge tag |
renamed DBIx::Custom::TagPro...
|
271 |
my $tag_processors = ref $_[0] eq 'HASH' ? $_[0] : {@_}; |
272 |
$self->tag_processors({%{$self->tag_processors}, %{$tag_processors}}); |
|
273 |
|
|
274 |
return $self; |
|
275 |
} |
|
276 | ||
renamed default_query_filter...
|
277 |
1; |
278 | ||
279 |
=head1 NAME |
|
280 | ||
281 |
DBIx::Custom::QueryBuilder - Query builder |
|
282 | ||
283 |
=head1 SYNOPSIS |
|
284 |
|
|
285 |
my $builder = DBIx::Custom::QueryBuilder->new; |
|
remove DBIx::Custom::QueryBu...
|
286 |
my $query = $builder->build_query( |
287 |
"select from table {= k1} && {<> k2} || {like k3}" |
|
288 |
); |
|
renamed default_query_filter...
|
289 | |
290 |
=head1 ATTRIBUTES |
|
291 | ||
renamed DBIx::Custom::TagPro...
|
292 |
=head2 C<tags> |
renamed default_query_filter...
|
293 | |
renamed DBIx::Custom::TagPro...
|
294 |
my $tags = $builder->tags; |
cleanup
|
295 |
$builder = $builder->tags(\%tags); |
renamed default_query_filter...
|
296 | |
cleanup
|
297 |
Tags. |
renamed default_query_filter...
|
298 | |
299 |
=head1 METHODS |
|
300 | ||
remove DBIx::Custom::QueryBu...
|
301 |
L<DBIx::Custom::QueryBuilder> inherits all methods from L<Object::Simple> |
302 |
and implements the following new ones. |
|
renamed default_query_filter...
|
303 | |
304 |
=head2 C<build_query> |
|
305 |
|
|
306 |
my $query = $builder->build_query($source); |
|
307 | ||
remove DBIx::Custom::QueryBu...
|
308 |
Create a new L<DBIx::Custom::Query> object from SQL source. |
309 |
SQL source contains tags, such as {= title}, {like author}. |
|
renamed default_query_filter...
|
310 | |
removed DBIx::Custom::Query ...
|
311 |
C<{> and C<}> is reserved. If you use these charactors, |
312 |
you must escape them using '\'. Note that '\' is |
|
313 |
already perl escaped charactor, so you must write '\\'. |
|
314 | ||
315 |
'select * from books \\{ something statement \\}' |
|
316 | ||
renamed default_query_filter...
|
317 |
B<Example:> |
318 | ||
remove DBIx::Custom::QueryBu...
|
319 |
SQL source |
renamed default_query_filter...
|
320 | |
remove DBIx::Custom::QueryBu...
|
321 |
"select * from table where {= title} && {like author} || {<= price}" |
renamed default_query_filter...
|
322 | |
remove DBIx::Custom::QueryBu...
|
323 |
Query |
renamed default_query_filter...
|
324 | |
remove DBIx::Custom::QueryBu...
|
325 |
{ |
326 |
sql => "select * from table where title = ? && author like ? price <= ?;" |
|
327 |
columns => ['title', 'author', 'price'] |
|
328 |
} |
|
renamed default_query_filter...
|
329 | |
renamed DBIx::Custom::TagPro...
|
330 |
=head2 C<register_tag> |
renamed default_query_filter...
|
331 | |
renamed DBIx::Custom::TagPro...
|
332 |
$builder->register_tag(\%tags); |
333 |
$builder->register_tag(%tags); |
|
renamed default_query_filter...
|
334 | |
cleanup
|
335 |
Register tag. |
renamed default_query_filter...
|
336 | |
renamed build_query to creat...
|
337 |
B<Example:> |
remove DBIx::Custom::QueryBu...
|
338 | |
cleanup
|
339 |
$builder->register_tag( |
renamed default_query_filter...
|
340 |
'?' => sub { |
remove DBIx::Custom::QueryBu...
|
341 |
my $column = shift; |
renamed default_query_filter...
|
342 |
|
remove DBIx::Custom::QueryBu...
|
343 |
return ['?', [$column]]; |
renamed default_query_filter...
|
344 |
} |
345 |
); |
|
346 | ||
cleanup
|
347 |
See also L<DBIx::Custom::Tag> to know tag. |
update document
|
348 |