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