packaging one directory
|
1 |
package DBIx::Custom::SQL::Template; |
2 | ||
update document
|
3 |
use strict; |
4 |
use warnings; |
|
packaging one directory
|
5 | |
update document
|
6 |
use base 'Object::Simple'; |
7 |
use Carp 'croak'; |
|
cleanup and update docment
|
8 |
use DBIx::Custom::Query; |
9 | ||
Simplify key search
|
10 |
__PACKAGE__->attr('table'); |
catch up with Object::Simple...
|
11 |
__PACKAGE__->dual_attr('tag_processors', default => sub { {} }, |
catch up with Object::Simple...
|
12 |
inherit => 'hash_copy'); |
cleanup
|
13 | |
catch up with Object::Simple...
|
14 |
__PACKAGE__->dual_attr('tag_start', default => '{', inherit => 'scalar_copy'); |
15 |
__PACKAGE__->dual_attr('tag_end', default => '}', inherit => 'scalar_copy'); |
|
cleanup
|
16 | |
catch up with Object::Simple...
|
17 |
__PACKAGE__->dual_attr('tag_syntax', inherit => 'scalar_copy'); |
cleanup
|
18 | |
catch up with Object::Simple...
|
19 |
__PACKAGE__->add_tag_processor( |
cleanup
|
20 |
'?' => \&DBIx::Custom::SQL::Template::TagProcessors::expand_basic_tag, |
21 |
'=' => \&DBIx::Custom::SQL::Template::TagProcessors::expand_basic_tag, |
|
22 |
'<>' => \&DBIx::Custom::SQL::Template::TagProcessors::expand_basic_tag, |
|
23 |
'>' => \&DBIx::Custom::SQL::Template::TagProcessors::expand_basic_tag, |
|
24 |
'<' => \&DBIx::Custom::SQL::Template::TagProcessors::expand_basic_tag, |
|
25 |
'>=' => \&DBIx::Custom::SQL::Template::TagProcessors::expand_basic_tag, |
|
26 |
'<=' => \&DBIx::Custom::SQL::Template::TagProcessors::expand_basic_tag, |
|
27 |
'like' => \&DBIx::Custom::SQL::Template::TagProcessors::expand_basic_tag, |
|
28 |
'in' => \&DBIx::Custom::SQL::Template::TagProcessors::expand_in_tag, |
|
29 |
'insert' => \&DBIx::Custom::SQL::Template::TagProcessors::expand_insert_tag, |
|
30 |
'update' => \&DBIx::Custom::SQL::Template::TagProcessors::expand_update_tag |
|
31 |
); |
|
packaging one directory
|
32 | |
catch up with Object::Simple...
|
33 |
__PACKAGE__->tag_syntax(<< 'EOS'); |
packaging one directory
|
34 |
[tag] [expand] |
35 |
{? name} ? |
|
36 |
{= name} name = ? |
|
37 |
{<> name} name <> ? |
|
38 | ||
39 |
{< name} name < ? |
|
40 |
{> name} name > ? |
|
41 |
{>= name} name >= ? |
|
42 |
{<= name} name <= ? |
|
43 | ||
44 |
{like name} name like ? |
|
45 |
{in name number} name in [?, ?, ..] |
|
46 | ||
47 |
{insert key1 key2} (key1, key2) values (?, ?) |
|
48 |
{update key1 key2} set key1 = ?, key2 = ? |
|
49 |
EOS |
|
50 | ||
51 | ||
52 |
sub add_tag_processor { |
|
53 |
my $invocant = shift; |
|
54 |
my $tag_processors = ref $_[0] eq 'HASH' ? $_[0] : {@_}; |
|
catch up with Object::Simple...
|
55 |
$invocant->tag_processors({%{$invocant->tag_processors}, %{$tag_processors}}); |
packaging one directory
|
56 |
return $invocant; |
57 |
} |
|
58 | ||
59 |
sub clone { |
|
60 |
my $self = shift; |
|
61 |
my $new = $self->new; |
|
62 |
|
|
63 |
$new->tag_start($self->tag_start); |
|
64 |
$new->tag_end($self->tag_end); |
|
65 |
$new->tag_syntax($self->tag_syntax); |
|
66 |
$new->tag_processors({%{$self->tag_processors || {}}}); |
|
67 |
|
|
68 |
return $new; |
|
69 |
} |
|
70 | ||
71 |
sub create_query { |
|
72 |
my ($self, $template) = @_; |
|
73 |
|
|
74 |
# Parse template |
|
75 |
my $tree = $self->_parse_template($template); |
|
76 |
|
|
77 |
# Build query |
|
78 |
my $query = $self->_build_query($tree); |
|
79 |
|
|
80 |
return $query; |
|
81 |
} |
|
82 | ||
83 |
sub _parse_template { |
|
84 |
my ($self, $template) = @_; |
|
cleanup
|
85 |
|
86 |
my $table = ''; |
|
87 |
if (ref $template eq 'ARRAY') { |
|
88 |
$table = $template->[0]; |
|
89 |
$template = $template->[1]; |
|
90 |
} |
|
packaging one directory
|
91 |
$template ||= ''; |
92 |
|
|
93 |
my $tree = []; |
|
94 |
|
|
95 |
# Tags |
|
96 |
my $tag_start = quotemeta $self->tag_start; |
|
97 |
my $tag_end = quotemeta $self->tag_end; |
|
98 |
|
|
99 |
# Tokenize |
|
100 |
my $state = 'text'; |
|
101 |
|
|
102 |
# Save original template |
|
103 |
my $original_template = $template; |
|
104 |
|
|
105 |
# Parse template |
|
106 |
while ($template =~ s/([^$tag_start]*?)$tag_start([^$tag_end].*?)$tag_end//sm) { |
|
107 |
my $text = $1; |
|
108 |
my $tag = $2; |
|
109 |
|
|
110 |
# Parse tree |
|
111 |
push @$tree, {type => 'text', tag_args => [$text]} if $text; |
|
112 |
|
|
113 |
if ($tag) { |
|
114 |
# Get tag name and arguments |
|
115 |
my ($tag_name, @tag_args) = split /\s+/, $tag; |
|
116 |
|
|
117 |
# Tag processor is exist? |
|
118 |
unless ($self->tag_processors->{$tag_name}) { |
|
119 |
my $tag_syntax = $self->tag_syntax; |
|
120 |
croak("Tag '{$tag}' in SQL template is not exist.\n\n" . |
|
121 |
"<SQL template tag syntax>\n" . |
|
122 |
"$tag_syntax\n" . |
|
123 |
"<Your SQL template>\n" . |
|
124 |
"$original_template\n\n"); |
|
125 |
} |
|
126 |
|
|
127 |
# Check tag arguments |
|
128 |
foreach my $tag_arg (@tag_args) { |
|
129 |
# Cannot cantain placehosder '?' |
|
130 |
croak("Tag '{t }' arguments cannot contain '?'") |
|
131 |
if $tag_arg =~ /\?/; |
|
132 |
} |
|
133 |
|
|
134 |
# Add tag to parsing tree |
|
135 |
push @$tree, {type => 'tag', tag_name => $tag_name, tag_args => [@tag_args]}; |
|
136 |
} |
|
137 |
} |
|
138 |
|
|
139 |
# Add text to parsing tree |
|
140 |
push @$tree, {type => 'text', tag_args => [$template]} if $template; |
|
141 |
|
|
142 |
return $tree; |
|
143 |
} |
|
144 | ||
145 |
sub _build_query { |
|
146 |
my ($self, $tree) = @_; |
|
147 |
|
|
148 |
# SQL |
|
149 |
my $sql = ''; |
|
150 |
|
|
151 |
# All parameter key infomation |
|
152 |
my $all_key_infos = []; |
|
153 |
|
|
154 |
# Build SQL |
|
155 |
foreach my $node (@$tree) { |
|
156 |
|
|
157 |
# Get type, tag name, and arguments |
|
158 |
my $type = $node->{type}; |
|
159 |
my $tag_name = $node->{tag_name}; |
|
160 |
my $tag_args = $node->{tag_args}; |
|
161 |
|
|
162 |
# Text |
|
163 |
if ($type eq 'text') { |
|
164 |
# Join text |
|
165 |
$sql .= $tag_args->[0]; |
|
166 |
} |
|
167 |
|
|
168 |
# Tag |
|
169 |
elsif ($type eq 'tag') { |
|
170 |
|
|
171 |
# Get tag processor |
|
172 |
my $tag_processor = $self->tag_processors->{$tag_name}; |
|
173 |
|
|
174 |
# Tag processor is code ref? |
|
175 |
croak("Tag processor '$tag_name' must be code reference") |
|
176 |
unless ref $tag_processor eq 'CODE'; |
|
177 |
|
|
178 |
# Expand tag using tag processor |
|
179 |
my ($expand, $key_infos) |
|
Simplify key search
|
180 |
= $tag_processor->($tag_name, $tag_args, $self->table); |
packaging one directory
|
181 |
|
182 |
# Check tag processor return value |
|
183 |
croak("Tag processor '$tag_name' must return (\$expand, \$key_infos)") |
|
184 |
if !defined $expand || ref $key_infos ne 'ARRAY'; |
|
185 |
|
|
186 |
# Check placeholder count |
|
187 |
croak("Placeholder count in SQL created by tag processor '$tag_name' " . |
|
188 |
"must be same as key informations count") |
|
189 |
unless $self->_placeholder_count($expand) eq @$key_infos; |
|
190 |
|
|
191 |
# Add key information |
|
192 |
push @$all_key_infos, @$key_infos; |
|
193 |
|
|
194 |
# Join expand tag to SQL |
|
195 |
$sql .= $expand; |
|
196 |
} |
|
197 |
} |
|
198 |
|
|
199 |
# Add semicolon |
|
200 |
$sql .= ';' unless $sql =~ /;$/; |
|
201 |
|
|
202 |
# Query |
|
cleanup and update docment
|
203 |
my $query = DBIx::Custom::Query->new(sql => $sql, key_infos => $all_key_infos); |
packaging one directory
|
204 |
|
205 |
return $query; |
|
206 |
} |
|
207 | ||
208 |
sub _placeholder_count { |
|
209 |
my ($self, $expand) = @_; |
|
210 |
$expand ||= ''; |
|
211 |
|
|
212 |
my $count = 0; |
|
213 |
my $pos = -1; |
|
214 |
while (($pos = index($expand, '?', $pos + 1)) != -1) { |
|
215 |
$count++; |
|
216 |
} |
|
217 |
return $count; |
|
218 |
} |
|
219 | ||
cleanup
|
220 |
1; |
packaging one directory
|
221 | |
cleanup and update docment
|
222 |
package DBIx::Custom::SQL::Template::TagProcessors; |
223 | ||
packaging one directory
|
224 |
use strict; |
225 |
use warnings; |
|
add DBIx::Custom::Column
|
226 | |
packaging one directory
|
227 |
use Carp 'croak'; |
Simplify key search
|
228 |
use DBIx::Custom::KeyInfo; |
packaging one directory
|
229 | |
230 |
sub expand_basic_tag { |
|
Simplify key search
|
231 |
my ($tag_name, $tag_args, $table) = @_; |
232 |
|
|
233 |
# Key |
|
234 |
my $key = $tag_args->[0]; |
|
packaging one directory
|
235 |
|
236 |
# Key is not exist |
|
237 |
croak("You must be pass key as argument to tag '{$tag_name }'") |
|
Simplify key search
|
238 |
unless $key; |
packaging one directory
|
239 |
|
240 |
# Expanded tag |
|
241 |
my $expand = $tag_name eq '?' |
|
242 |
? '?' |
|
Simplify key search
|
243 |
: "$key $tag_name ?"; |
packaging one directory
|
244 |
|
Simplify key search
|
245 |
my $key_info = DBIx::Custom::KeyInfo->new($key); |
246 |
$key_info->table($table) unless $key_info->table; |
|
packaging one directory
|
247 |
|
Simplify key search
|
248 |
return ($expand, [$key_info]); |
packaging one directory
|
249 |
} |
250 | ||
251 |
sub expand_in_tag { |
|
Simplify key search
|
252 |
my ($tag_name, $tag_args, $table) = @_; |
253 |
my ($key, $placeholder_count) = @$tag_args; |
|
packaging one directory
|
254 |
|
255 |
# Key must be specified |
|
256 |
croak("You must be pass key as first argument of tag '{$tag_name }'\n" . |
|
257 |
"Usage: {$tag_name \$key \$placeholder_count}") |
|
Simplify key search
|
258 |
unless $key; |
packaging one directory
|
259 |
|
260 |
# Place holder count must be specified |
|
261 |
croak("You must be pass placeholder count as second argument of tag '{$tag_name }'\n" . |
|
262 |
"Usage: {$tag_name \$key \$placeholder_count}") |
|
263 |
if !$placeholder_count || $placeholder_count =~ /\D/; |
|
264 | ||
265 |
# Expand tag |
|
Simplify key search
|
266 |
my $expand = "$key $tag_name ("; |
packaging one directory
|
267 |
for (my $i = 0; $i < $placeholder_count; $i++) { |
268 |
$expand .= '?, '; |
|
269 |
} |
|
270 |
|
|
271 |
$expand =~ s/, $//; |
|
272 |
$expand .= ')'; |
|
273 |
|
|
274 |
# Create parameter key infomations |
|
275 |
my $key_infos = []; |
|
276 |
for (my $i = 0; $i < $placeholder_count; $i++) { |
|
277 |
|
|
278 |
# Add parameter key infos |
|
Simplify key search
|
279 |
my $key_info = DBIx::Custom::KeyInfo->new($key); |
280 |
$key_info->table($table) unless $key_info->table; |
|
281 |
$key_info->pos($i); |
|
packaging one directory
|
282 |
push @$key_infos, $key_info; |
283 |
} |
|
284 |
|
|
285 |
return ($expand, $key_infos); |
|
286 |
} |
|
287 | ||
288 |
sub expand_insert_tag { |
|
Simplify key search
|
289 |
my ($tag_name, $tag_args, $table) = @_; |
290 |
my $keys = $tag_args; |
|
packaging one directory
|
291 |
|
292 |
# Insert key (k1, k2, k3, ..) |
|
293 |
my $insert_keys = '('; |
|
294 |
|
|
295 |
# placeholder (?, ?, ?, ..) |
|
296 |
my $place_holders = '('; |
|
297 |
|
|
Simplify key search
|
298 |
foreach my $key (@$keys) { |
add DBIx::Custom::Column
|
299 |
# Get table and clumn name |
Simplify key search
|
300 |
my $key_info = DBIx::Custom::KeyInfo->new($key); |
301 |
my $column = $key_info->column; |
|
packaging one directory
|
302 |
|
303 |
# Join insert column |
|
304 |
$insert_keys .= "$column, "; |
|
305 |
|
|
306 |
# Join place holder |
|
307 |
$place_holders .= "?, "; |
|
308 |
} |
|
309 |
|
|
310 |
# Delete last ', ' |
|
311 |
$insert_keys =~ s/, $//; |
|
312 |
|
|
313 |
# Close |
|
314 |
$insert_keys .= ')'; |
|
315 |
$place_holders =~ s/, $//; |
|
316 |
$place_holders .= ')'; |
|
317 |
|
|
318 |
# Expand tag |
|
319 |
my $expand = "$insert_keys values $place_holders"; |
|
320 |
|
|
321 |
# Create parameter key infomations |
|
322 |
my $key_infos = []; |
|
Simplify key search
|
323 |
foreach my $key (@$keys) { |
324 |
my $key_info = DBIx::Custom::KeyInfo->new($key); |
|
325 |
$key_info->table($table) unless $key_info->table; |
|
packaging one directory
|
326 |
push @$key_infos, $key_info; |
327 |
} |
|
328 |
|
|
329 |
return ($expand, $key_infos); |
|
330 |
} |
|
331 | ||
332 |
sub expand_update_tag { |
|
Simplify key search
|
333 |
my ($tag_name, $tag_args, $table) = @_; |
334 |
my $keys = $tag_args; |
|
packaging one directory
|
335 |
|
336 |
# Expanded tag |
|
337 |
my $expand = 'set '; |
|
338 |
|
|
Simplify key search
|
339 |
foreach my $key (@$keys) { |
packaging one directory
|
340 |
# Get table and clumn name |
Simplify key search
|
341 |
my $key_info = DBIx::Custom::KeyInfo->new($key); |
342 |
my $column = $key_info->column; |
|
packaging one directory
|
343 | |
344 |
# Join key and placeholder |
|
345 |
$expand .= "$column = ?, "; |
|
346 |
} |
|
347 |
|
|
348 |
# Delete last ', ' |
|
349 |
$expand =~ s/, $//; |
|
350 |
|
|
351 |
my $key_infos = []; |
|
Simplify key search
|
352 |
foreach my $key (@$keys) { |
353 |
my $key_info = DBIx::Custom::KeyInfo->new($key); |
|
354 |
$key_info->table($table) unless $key_info->table; |
|
packaging one directory
|
355 |
push @$key_infos, $key_info; |
356 |
} |
|
357 |
|
|
358 |
return ($expand, $key_infos); |
|
359 |
} |
|
360 | ||
update document
|
361 |
package DBIx::Custom::SQL::Template; |
362 | ||
packaging one directory
|
363 |
1; |
364 | ||
365 |
=head1 NAME |
|
366 | ||
update document
|
367 |
DBIx::Custom::SQL::Template - DBIx::Custom SQL Template |
packaging one directory
|
368 | |
update document
|
369 |
=head1 SYNOPSIS |
packaging one directory
|
370 |
|
371 |
my $sql_tmpl = DBIx::Custom::SQL::Template->new; |
|
372 |
|
|
373 |
my $tmpl = "select from table {= k1} && {<> k2} || {like k3}"; |
|
374 |
my $param = {k1 => 1, k2 => 2, k3 => 3}; |
|
375 |
|
|
376 |
my $query = $sql_template->create_query($tmpl); |
|
377 | ||
update document
|
378 |
=head1 ATTRIBUTES |
packaging one directory
|
379 | |
cleanup and update docment
|
380 |
=head2 tag_processors |
packaging one directory
|
381 | |
version 0.0901
|
382 |
$sql_tmpl = $sql_tmpl->tag_processors($name1 => $tag_processor1 |
383 |
$name2 => $tag_processor2); |
|
384 |
$tag_processors = $sql_tmpl->tag_processors; |
|
packaging one directory
|
385 | |
386 |
=head2 tag_start |
|
cleanup and update docment
|
387 |
|
version 0.0901
|
388 |
$sql_tmpl = $sql_tmpl->tag_start('{'); |
389 |
$tag_start = $sql_tmpl->tag_start; |
|
packaging one directory
|
390 | |
update document
|
391 |
Default is '{' |
packaging one directory
|
392 | |
393 |
=head2 tag_end |
|
cleanup and update docment
|
394 |
|
version 0.0901
|
395 |
$sql_tmpl = $sql_tmpl->tag_start('}'); |
396 |
$tag_end = $sql_tmpl->tag_start; |
|
packaging one directory
|
397 | |
update document
|
398 |
Default is '}' |
packaging one directory
|
399 |
|
400 |
=head2 tag_syntax |
|
401 |
|
|
version 0.0901
|
402 |
$sql_tmpl = $sql_tmpl->tag_syntax($tag_syntax); |
403 |
$tag_syntax = $sql_tmpl->tag_syntax; |
|
packaging one directory
|
404 | |
update document
|
405 |
=head1 METHODS |
406 | ||
407 |
This class is L<Object::Simple> subclass. |
|
408 |
You can use all methods of L<Object::Simple> |
|
packaging one directory
|
409 | |
410 |
=head2 create_query |
|
411 |
|
|
cleanup and update docment
|
412 |
Create L<DBIx::Custom::Query> object parsing SQL template |
413 | ||
version 0.0901
|
414 |
$query = $sql_tmpl->create_query($tmpl); |
packaging one directory
|
415 |
|
416 |
# Sample |
|
417 |
$query = $sql_tmpl->create_sql( |
|
418 |
"select * from table where {= title} && {like author} || {<= price}") |
|
419 |
|
|
cleanup and update docment
|
420 |
# Expanded |
421 |
$qeury->sql : "select * from table where title = ? && author like ? price <= ?;" |
|
422 |
$query->key_infos : [['title'], ['author'], ['price']] |
|
packaging one directory
|
423 |
|
cleanup and update docment
|
424 |
# Sample with table name |
packaging one directory
|
425 |
($sql, @bind_values) = $sql_tmpl->create_sql( |
426 |
"select * from table where {= table.title} && {like table.author}", |
|
427 |
{table => {title => 'Perl', author => '%Taro%'}} |
|
428 |
) |
|
429 |
|
|
cleanup and update docment
|
430 |
# Expanded |
431 |
$query->sql : "select * from table where table.title = ? && table.title like ?;" |
|
432 |
$query->key_infos :[ [['table.title'],['table', 'title']], |
|
433 |
[['table.author'],['table', 'author']] ] |
|
packaging one directory
|
434 | |
cleanup and update docment
|
435 |
This method create query using by L<DBIx::Custom>. |
436 |
query has two infomation |
|
packaging one directory
|
437 | |
cleanup and update docment
|
438 |
1. sql : SQL |
439 |
2. key_infos : Parameter access key information |
|
packaging one directory
|
440 | |
441 |
=head2 add_tag_processor |
|
442 | ||
443 |
Add tag processor |
|
cleanup and update docment
|
444 |
|
version 0.0901
|
445 |
$sql_tmpl = $sql_tmpl->add_tag_processor($tag_processor); |
446 | ||
447 |
The following is add_tag_processor sample |
|
448 | ||
packaging one directory
|
449 |
$sql_tmpl->add_tag_processor( |
450 |
'?' => sub { |
|
451 |
my ($tag_name, $tag_args) = @_; |
|
452 |
|
|
453 |
my $key1 = $tag_args->[0]; |
|
454 |
my $key2 = $tag_args->[1]; |
|
455 |
|
|
456 |
my $key_infos = []; |
|
457 |
|
|
458 |
# Expand tag and create key informations |
|
459 |
|
|
460 |
# Return expand tags and key informations |
|
461 |
return ($expand, $key_infos); |
|
462 |
} |
|
463 |
); |
|
464 | ||
465 |
Tag processor recieve 2 argument |
|
466 | ||
467 |
1. Tag name (?, =, <>, or etc) |
|
468 |
2. Tag arguments (arg1 and arg2 in {tag_name arg1 arg2}) |
|
469 | ||
470 |
Tag processor return 2 value |
|
471 | ||
472 |
1. Expanded Tag (For exsample, '{= title}' is expanded to 'title = ?') |
|
473 |
2. Key infomations |
|
474 |
|
|
475 |
You must be return expanded tag and key infomations. |
|
476 | ||
477 |
Key information is a little complex. so I will explan this in future. |
|
478 | ||
479 |
If you want to know more, Please see DBIx::Custom::SQL::Template source code. |
|
480 | ||
481 |
=head2 clone |
|
482 | ||
cleanup and update docment
|
483 |
Clone DBIx::Custom::SQL::Template object |
484 | ||
version 0.0901
|
485 |
$clone = $sql_tmpl->clone; |
packaging one directory
|
486 |
|
487 |
=head1 Available Tags |
|
488 |
|
|
cleanup and update docment
|
489 |
Available Tags |
490 | ||
packaging one directory
|
491 |
[tag] [expand] |
492 |
{? name} ? |
|
493 |
{= name} name = ? |
|
494 |
{<> name} name <> ? |
|
495 |
|
|
496 |
{< name} name < ? |
|
497 |
{> name} name > ? |
|
498 |
{>= name} name >= ? |
|
499 |
{<= name} name <= ? |
|
500 |
|
|
501 |
{like name} name like ? |
|
502 |
{in name} name in [?, ?, ..] |
|
503 |
|
|
cleanup and update docment
|
504 |
{insert} (key1, key2, key3) values (?, ?, ?) |
505 |
{update} set key1 = ?, key2 = ?, key3 = ? |
|
packaging one directory
|
506 |
|
version 0.0901
|
507 | |
508 |
The following is insert SQL sample |
|
509 | ||
packaging one directory
|
510 |
$query = $sql_tmpl->create_sql( |
511 |
"insert into table {insert key1 key2}" |
|
512 |
); |
|
version 0.0901
|
513 |
|
cleanup and update docment
|
514 |
# Expanded |
515 |
$query->sql : "insert into table (key1, key2) values (?, ?)" |
|
version 0.0901
|
516 | |
517 |
The following is update SQL sample |
|
packaging one directory
|
518 |
|
519 |
$query = $sql_tmpl->create_sql( |
|
520 |
"update table {update key1 key2} where {= key3}" |
|
521 |
); |
|
522 |
|
|
cleanup and update docment
|
523 |
# Expanded |
524 |
$query->sql : "update table set key1 = ?, key2 = ? where key3 = ?;" |
|
packaging one directory
|
525 |
|
526 |
=cut |
|
Simplify key search
|
527 |