packaging one directory
|
1 |
package DBIx::Custom::SQL::Template; |
2 |
use Object::Simple; |
|
3 | ||
update document
|
4 |
use strict; |
5 |
use warnings; |
|
packaging one directory
|
6 |
use Carp 'croak'; |
7 | ||
cleanup and update docment
|
8 |
use DBIx::Custom::Query; |
9 | ||
10 |
# Accessor is created by Object::Simple. |
|
packaging one directory
|
11 | |
12 |
### Class-Object accessors |
|
13 | ||
14 |
# Tag start |
|
15 |
sub tag_start : ClassObjectAttr { |
|
16 |
initialize => {default => '{', clone => 'scalar'} |
|
17 |
} |
|
18 | ||
19 |
# Tag end |
|
20 |
sub tag_end : ClassObjectAttr { |
|
21 |
initialize => {default => '}', clone => 'scalar'} |
|
22 |
} |
|
23 | ||
24 |
# Tag syntax |
|
25 |
sub tag_syntax : ClassObjectAttr { |
|
26 |
initialize => {default => <<'EOS', clone => 'scalar'}} |
|
27 |
[tag] [expand] |
|
28 |
{? name} ? |
|
29 |
{= name} name = ? |
|
30 |
{<> name} name <> ? |
|
31 | ||
32 |
{< name} name < ? |
|
33 |
{> name} name > ? |
|
34 |
{>= name} name >= ? |
|
35 |
{<= name} name <= ? |
|
36 | ||
37 |
{like name} name like ? |
|
38 |
{in name number} name in [?, ?, ..] |
|
39 | ||
40 |
{insert key1 key2} (key1, key2) values (?, ?) |
|
41 |
{update key1 key2} set key1 = ?, key2 = ? |
|
42 |
EOS |
|
43 | ||
44 |
# Tag processors |
|
45 |
sub tag_processors : ClassObjectAttr { |
|
46 |
type => 'hash', |
|
47 |
deref => 1, |
|
48 |
initialize => { |
|
49 |
clone => 'hash', |
|
50 |
default => sub {{ |
|
cleanup and update docment
|
51 |
'?' => \&DBIx::Custom::SQL::Template::TagProcessors::expand_basic_tag, |
52 |
'=' => \&DBIx::Custom::SQL::Template::TagProcessors::expand_basic_tag, |
|
53 |
'<>' => \&DBIx::Custom::SQL::Template::TagProcessors::expand_basic_tag, |
|
54 |
'>' => \&DBIx::Custom::SQL::Template::TagProcessors::expand_basic_tag, |
|
55 |
'<' => \&DBIx::Custom::SQL::Template::TagProcessors::expand_basic_tag, |
|
56 |
'>=' => \&DBIx::Custom::SQL::Template::TagProcessors::expand_basic_tag, |
|
57 |
'<=' => \&DBIx::Custom::SQL::Template::TagProcessors::expand_basic_tag, |
|
58 |
'like' => \&DBIx::Custom::SQL::Template::TagProcessors::expand_basic_tag, |
|
59 |
'in' => \&DBIx::Custom::SQL::Template::TagProcessors::expand_in_tag, |
|
60 |
'insert' => \&DBIx::Custom::SQL::Template::TagProcessors::expand_insert_tag, |
|
61 |
'update' => \&DBIx::Custom::SQL::Template::TagProcessors::expand_update_tag |
|
packaging one directory
|
62 |
}} |
63 |
} |
|
64 |
} |
|
65 | ||
66 |
# Add Tag processor |
|
67 |
sub add_tag_processor { |
|
68 |
my $invocant = shift; |
|
69 |
my $tag_processors = ref $_[0] eq 'HASH' ? $_[0] : {@_}; |
|
70 |
$invocant->tag_processors(%{$invocant->tag_processors}, %{$tag_processors}); |
|
71 |
return $invocant; |
|
72 |
} |
|
73 | ||
74 |
# Clone |
|
75 |
sub clone { |
|
76 |
my $self = shift; |
|
77 |
my $new = $self->new; |
|
78 |
|
|
79 |
$new->tag_start($self->tag_start); |
|
80 |
$new->tag_end($self->tag_end); |
|
81 |
$new->tag_syntax($self->tag_syntax); |
|
82 |
$new->tag_processors({%{$self->tag_processors || {}}}); |
|
83 |
|
|
84 |
return $new; |
|
85 |
} |
|
86 | ||
87 | ||
88 |
### Object Methods |
|
89 | ||
90 |
# Create Query |
|
91 |
sub create_query { |
|
92 |
my ($self, $template) = @_; |
|
93 |
|
|
94 |
# Parse template |
|
95 |
my $tree = $self->_parse_template($template); |
|
96 |
|
|
97 |
# Build query |
|
98 |
my $query = $self->_build_query($tree); |
|
99 |
|
|
100 |
return $query; |
|
101 |
} |
|
102 | ||
103 |
# Parse template |
|
104 |
sub _parse_template { |
|
105 |
my ($self, $template) = @_; |
|
106 |
$template ||= ''; |
|
107 |
|
|
108 |
my $tree = []; |
|
109 |
|
|
110 |
# Tags |
|
111 |
my $tag_start = quotemeta $self->tag_start; |
|
112 |
my $tag_end = quotemeta $self->tag_end; |
|
113 |
|
|
114 |
# Tokenize |
|
115 |
my $state = 'text'; |
|
116 |
|
|
117 |
# Save original template |
|
118 |
my $original_template = $template; |
|
119 |
|
|
120 |
# Parse template |
|
121 |
while ($template =~ s/([^$tag_start]*?)$tag_start([^$tag_end].*?)$tag_end//sm) { |
|
122 |
my $text = $1; |
|
123 |
my $tag = $2; |
|
124 |
|
|
125 |
# Parse tree |
|
126 |
push @$tree, {type => 'text', tag_args => [$text]} if $text; |
|
127 |
|
|
128 |
if ($tag) { |
|
129 |
# Get tag name and arguments |
|
130 |
my ($tag_name, @tag_args) = split /\s+/, $tag; |
|
131 |
|
|
132 |
# Tag processor is exist? |
|
133 |
unless ($self->tag_processors->{$tag_name}) { |
|
134 |
my $tag_syntax = $self->tag_syntax; |
|
135 |
croak("Tag '{$tag}' in SQL template is not exist.\n\n" . |
|
136 |
"<SQL template tag syntax>\n" . |
|
137 |
"$tag_syntax\n" . |
|
138 |
"<Your SQL template>\n" . |
|
139 |
"$original_template\n\n"); |
|
140 |
} |
|
141 |
|
|
142 |
# Check tag arguments |
|
143 |
foreach my $tag_arg (@tag_args) { |
|
144 |
# Cannot cantain placehosder '?' |
|
145 |
croak("Tag '{t }' arguments cannot contain '?'") |
|
146 |
if $tag_arg =~ /\?/; |
|
147 |
} |
|
148 |
|
|
149 |
# Add tag to parsing tree |
|
150 |
push @$tree, {type => 'tag', tag_name => $tag_name, tag_args => [@tag_args]}; |
|
151 |
} |
|
152 |
} |
|
153 |
|
|
154 |
# Add text to parsing tree |
|
155 |
push @$tree, {type => 'text', tag_args => [$template]} if $template; |
|
156 |
|
|
157 |
return $tree; |
|
158 |
} |
|
159 | ||
160 |
# Build SQL from parsing tree |
|
161 |
sub _build_query { |
|
162 |
my ($self, $tree) = @_; |
|
163 |
|
|
164 |
# SQL |
|
165 |
my $sql = ''; |
|
166 |
|
|
167 |
# All parameter key infomation |
|
168 |
my $all_key_infos = []; |
|
169 |
|
|
170 |
# Build SQL |
|
171 |
foreach my $node (@$tree) { |
|
172 |
|
|
173 |
# Get type, tag name, and arguments |
|
174 |
my $type = $node->{type}; |
|
175 |
my $tag_name = $node->{tag_name}; |
|
176 |
my $tag_args = $node->{tag_args}; |
|
177 |
|
|
178 |
# Text |
|
179 |
if ($type eq 'text') { |
|
180 |
# Join text |
|
181 |
$sql .= $tag_args->[0]; |
|
182 |
} |
|
183 |
|
|
184 |
# Tag |
|
185 |
elsif ($type eq 'tag') { |
|
186 |
|
|
187 |
# Get tag processor |
|
188 |
my $tag_processor = $self->tag_processors->{$tag_name}; |
|
189 |
|
|
190 |
# Tag processor is code ref? |
|
191 |
croak("Tag processor '$tag_name' must be code reference") |
|
192 |
unless ref $tag_processor eq 'CODE'; |
|
193 |
|
|
194 |
# Expand tag using tag processor |
|
195 |
my ($expand, $key_infos) |
|
196 |
= $tag_processor->($tag_name, $tag_args); |
|
197 |
|
|
198 |
# Check tag processor return value |
|
199 |
croak("Tag processor '$tag_name' must return (\$expand, \$key_infos)") |
|
200 |
if !defined $expand || ref $key_infos ne 'ARRAY'; |
|
201 |
|
|
202 |
# Check placeholder count |
|
203 |
croak("Placeholder count in SQL created by tag processor '$tag_name' " . |
|
204 |
"must be same as key informations count") |
|
205 |
unless $self->_placeholder_count($expand) eq @$key_infos; |
|
206 |
|
|
207 |
# Add key information |
|
208 |
push @$all_key_infos, @$key_infos; |
|
209 |
|
|
210 |
# Join expand tag to SQL |
|
211 |
$sql .= $expand; |
|
212 |
} |
|
213 |
} |
|
214 |
|
|
215 |
# Add semicolon |
|
216 |
$sql .= ';' unless $sql =~ /;$/; |
|
217 |
|
|
218 |
# Query |
|
cleanup and update docment
|
219 |
my $query = DBIx::Custom::Query->new(sql => $sql, key_infos => $all_key_infos); |
packaging one directory
|
220 |
|
221 |
return $query; |
|
222 |
} |
|
223 | ||
224 |
# Get placeholder count |
|
225 |
sub _placeholder_count { |
|
226 |
my ($self, $expand) = @_; |
|
227 |
$expand ||= ''; |
|
228 |
|
|
229 |
my $count = 0; |
|
230 |
my $pos = -1; |
|
231 |
while (($pos = index($expand, '?', $pos + 1)) != -1) { |
|
232 |
$count++; |
|
233 |
} |
|
234 |
return $count; |
|
235 |
} |
|
236 | ||
237 |
Object::Simple->build_class; |
|
238 | ||
239 | ||
cleanup and update docment
|
240 |
package DBIx::Custom::SQL::Template::TagProcessors; |
241 | ||
packaging one directory
|
242 |
use strict; |
243 |
use warnings; |
|
244 |
use Carp 'croak'; |
|
245 | ||
246 |
# Expand tag '?', '=', '<>', '>', '<', '>=', '<=', 'like' |
|
247 |
sub expand_basic_tag { |
|
248 |
my ($tag_name, $tag_args) = @_; |
|
249 |
my $original_key = $tag_args->[0]; |
|
250 |
|
|
251 |
# Key is not exist |
|
252 |
croak("You must be pass key as argument to tag '{$tag_name }'") |
|
253 |
if !$original_key; |
|
254 |
|
|
255 |
# Expanded tag |
|
256 |
my $expand = $tag_name eq '?' |
|
257 |
? '?' |
|
258 |
: "$original_key $tag_name ?"; |
|
259 |
|
|
260 |
# Get table and clumn name |
|
261 |
my ($table, $column) = get_table_and_column($original_key); |
|
262 |
|
|
263 |
# Parameter key infomation |
|
264 |
my $key_info = {}; |
|
265 |
|
|
266 |
# Original key |
|
267 |
$key_info->{original_key} = $original_key; |
|
268 |
|
|
269 |
# Table |
|
270 |
$key_info->{table} = $table; |
|
271 |
|
|
272 |
# Column name |
|
273 |
$key_info->{column} = $column; |
|
274 |
|
|
275 |
# Access keys |
|
276 |
my $access_keys = []; |
|
277 |
push @$access_keys, [$original_key]; |
|
278 |
push @$access_keys, [$table, $column] if $table && $column; |
|
279 |
$key_info->{access_keys} = $access_keys; |
|
280 |
|
|
281 |
# Add parameter key information |
|
282 |
my $key_infos = []; |
|
283 |
push @$key_infos, $key_info; |
|
284 |
|
|
285 |
return ($expand, $key_infos); |
|
286 |
} |
|
287 | ||
288 |
# Expand tag 'in' |
|
289 |
sub expand_in_tag { |
|
290 |
my ($tag_name, $tag_args) = @_; |
|
291 |
my ($original_key, $placeholder_count) = @$tag_args; |
|
292 |
|
|
293 |
# Key must be specified |
|
294 |
croak("You must be pass key as first argument of tag '{$tag_name }'\n" . |
|
295 |
"Usage: {$tag_name \$key \$placeholder_count}") |
|
296 |
unless $original_key; |
|
297 |
|
|
298 |
|
|
299 |
# Place holder count must be specified |
|
300 |
croak("You must be pass placeholder count as second argument of tag '{$tag_name }'\n" . |
|
301 |
"Usage: {$tag_name \$key \$placeholder_count}") |
|
302 |
if !$placeholder_count || $placeholder_count =~ /\D/; |
|
303 | ||
304 |
# Expand tag |
|
305 |
my $expand = "$original_key $tag_name ("; |
|
306 |
for (my $i = 0; $i < $placeholder_count; $i++) { |
|
307 |
$expand .= '?, '; |
|
308 |
} |
|
309 |
|
|
310 |
$expand =~ s/, $//; |
|
311 |
$expand .= ')'; |
|
312 |
|
|
313 |
# Get table and clumn name |
|
314 |
my ($table, $column) = get_table_and_column($original_key); |
|
315 |
|
|
316 |
# Create parameter key infomations |
|
317 |
my $key_infos = []; |
|
318 |
for (my $i = 0; $i < $placeholder_count; $i++) { |
|
319 |
# Parameter key infomation |
|
320 |
my $key_info = {}; |
|
321 |
|
|
322 |
# Original key |
|
323 |
$key_info->{original_key} = $original_key; |
|
324 |
|
|
325 |
# Table |
|
326 |
$key_info->{table} = $table; |
|
327 |
|
|
328 |
# Column name |
|
329 |
$key_info->{column} = $column; |
|
330 |
|
|
331 |
# Access keys |
|
332 |
my $access_keys = []; |
|
333 |
push @$access_keys, [$original_key, [$i]]; |
|
334 |
push @$access_keys, [$table, $column, [$i]] if $table && $column; |
|
335 |
$key_info->{access_keys} = $access_keys; |
|
336 |
|
|
337 |
# Add parameter key infos |
|
338 |
push @$key_infos, $key_info; |
|
339 |
} |
|
340 |
|
|
341 |
return ($expand, $key_infos); |
|
342 |
} |
|
343 | ||
344 |
# Get table and column |
|
345 |
sub get_table_and_column { |
|
346 |
my $key = shift; |
|
347 |
$key ||= ''; |
|
348 |
|
|
349 |
return ('', $key) unless $key =~ /\./; |
|
350 |
|
|
351 |
my ($table, $column) = split /\./, $key; |
|
352 |
|
|
353 |
return ($table, $column); |
|
354 |
} |
|
355 | ||
356 |
# Expand tag 'insert' |
|
357 |
sub expand_insert_tag { |
|
358 |
my ($tag_name, $tag_args) = @_; |
|
359 |
my $original_keys = $tag_args; |
|
360 |
|
|
361 |
# Insert key (k1, k2, k3, ..) |
|
362 |
my $insert_keys = '('; |
|
363 |
|
|
364 |
# placeholder (?, ?, ?, ..) |
|
365 |
my $place_holders = '('; |
|
366 |
|
|
367 |
foreach my $original_key (@$original_keys) { |
|
368 |
# Get table and column |
|
369 |
my ($table, $column) = get_table_and_column($original_key); |
|
370 |
|
|
371 |
# Join insert column |
|
372 |
$insert_keys .= "$column, "; |
|
373 |
|
|
374 |
# Join place holder |
|
375 |
$place_holders .= "?, "; |
|
376 |
} |
|
377 |
|
|
378 |
# Delete last ', ' |
|
379 |
$insert_keys =~ s/, $//; |
|
380 |
|
|
381 |
# Close |
|
382 |
$insert_keys .= ')'; |
|
383 |
$place_holders =~ s/, $//; |
|
384 |
$place_holders .= ')'; |
|
385 |
|
|
386 |
# Expand tag |
|
387 |
my $expand = "$insert_keys values $place_holders"; |
|
388 |
|
|
389 |
# Create parameter key infomations |
|
390 |
my $key_infos = []; |
|
391 |
foreach my $original_key (@$original_keys) { |
|
392 |
# Get table and clumn name |
|
393 |
my ($table, $column) = get_table_and_column($original_key); |
|
394 |
|
|
395 |
# Parameter key infomation |
|
396 |
my $key_info = {}; |
|
397 |
|
|
398 |
# Original key |
|
399 |
$key_info->{original_key} = $original_key; |
|
400 |
|
|
401 |
# Table |
|
402 |
$key_info->{table} = $table; |
|
403 |
|
|
404 |
# Column name |
|
405 |
$key_info->{column} = $column; |
|
406 |
|
|
407 |
# Access keys |
|
408 |
my $access_keys = []; |
|
409 |
push @$access_keys, ['#insert', $original_key]; |
|
410 |
push @$access_keys, ['#insert', $table, $column] if $table && $column; |
|
411 |
push @$access_keys, [$original_key]; |
|
412 |
push @$access_keys, [$table, $column] if $table && $column; |
|
413 |
$key_info->{access_keys} = $access_keys; |
|
414 |
|
|
415 |
# Add parameter key infos |
|
416 |
push @$key_infos, $key_info; |
|
417 |
} |
|
418 |
|
|
419 |
return ($expand, $key_infos); |
|
420 |
} |
|
421 | ||
422 |
# Expand tag 'update' |
|
423 |
sub expand_update_tag { |
|
424 |
my ($tag_name, $tag_args) = @_; |
|
425 |
my $original_keys = $tag_args; |
|
426 |
|
|
427 |
# Expanded tag |
|
428 |
my $expand = 'set '; |
|
429 |
|
|
430 |
# |
|
431 |
foreach my $original_key (@$original_keys) { |
|
432 |
# Get table and clumn name |
|
433 |
my ($table, $column) = get_table_and_column($original_key); |
|
434 | ||
435 |
# Join key and placeholder |
|
436 |
$expand .= "$column = ?, "; |
|
437 |
} |
|
438 |
|
|
439 |
# Delete last ', ' |
|
440 |
$expand =~ s/, $//; |
|
441 |
|
|
442 |
# Create parameter key infomations |
|
443 |
my $key_infos = []; |
|
444 |
foreach my $original_key (@$original_keys) { |
|
445 |
# Get table and clumn name |
|
446 |
my ($table, $column) = get_table_and_column($original_key); |
|
447 |
|
|
448 |
# Parameter key infomation |
|
449 |
my $key_info = {}; |
|
450 |
|
|
451 |
# Original key |
|
452 |
$key_info->{original_key} = $original_key; |
|
453 |
|
|
454 |
# Table |
|
455 |
$key_info->{table} = $table; |
|
456 |
|
|
457 |
# Column name |
|
458 |
$key_info->{column} = $column; |
|
459 |
|
|
460 |
# Access keys |
|
461 |
my $access_keys = []; |
|
462 |
push @$access_keys, ['#update', $original_key]; |
|
463 |
push @$access_keys, ['#update', $table, $column] if $table && $column; |
|
464 |
push @$access_keys, [$original_key]; |
|
465 |
push @$access_keys, [$table, $column] if $table && $column; |
|
466 |
$key_info->{access_keys} = $access_keys; |
|
467 |
|
|
468 |
# Add parameter key infos |
|
469 |
push @$key_infos, $key_info; |
|
470 |
} |
|
471 |
|
|
472 |
return ($expand, $key_infos); |
|
473 |
} |
|
474 | ||
475 |
1; |
|
476 | ||
477 |
=head1 NAME |
|
478 | ||
update document
|
479 |
DBIx::Custom::SQL::Template - DBIx::Custom SQL Template |
packaging one directory
|
480 | |
cleanup and update docment
|
481 |
=head1 Synopsis |
packaging one directory
|
482 |
|
483 |
my $sql_tmpl = DBIx::Custom::SQL::Template->new; |
|
484 |
|
|
485 |
my $tmpl = "select from table {= k1} && {<> k2} || {like k3}"; |
|
486 |
my $param = {k1 => 1, k2 => 2, k3 => 3}; |
|
487 |
|
|
488 |
my $query = $sql_template->create_query($tmpl); |
|
489 |
|
|
490 |
|
|
491 |
# Using query from DBIx::Custom |
|
492 |
use DBIx::Custom; |
|
493 |
my $dbi = DBI->new( |
|
494 |
data_source => $data_source, |
|
495 |
user => $user, |
|
496 |
password => $password, |
|
497 |
dbi_options => {PrintError => 0, RaiseError => 1} |
|
498 |
); |
|
499 |
|
|
500 |
$query = $dbi->create_query($tmpl); # This is SQL::Template create_query |
|
501 |
$dbi->query($query, $param); |
|
502 | ||
cleanup and update docment
|
503 |
=head1 Accessors |
packaging one directory
|
504 | |
cleanup and update docment
|
505 |
=head2 tag_processors |
packaging one directory
|
506 | |
cleanup and update docment
|
507 |
Set and get tag processors |
packaging one directory
|
508 | |
cleanup and update docment
|
509 |
# For object |
510 |
$self = $self->tag_processors($tag_processors); |
|
511 |
$tag_processors = $self->tag_processors; |
|
packaging one directory
|
512 | |
cleanup and update docment
|
513 |
# For class |
514 |
$class = $class->tag_processors($tag_processors); |
|
515 |
$tag_processors = $class->tag_processors; |
|
packaging one directory
|
516 | |
cleanup and update docment
|
517 |
# Sample |
518 |
$placeholder_tag_processor = $sql_tmpl->tag_processor->{'?'}; |
|
packaging one directory
|
519 | |
520 |
=head2 tag_start |
|
521 | ||
cleanup and update docment
|
522 |
Set and get start tag |
523 |
|
|
524 |
# For object |
|
525 |
$self = $self->tag_start($tag_start); |
|
526 |
$tag_start = $self->tag_start; |
|
527 |
|
|
528 |
# For class |
|
529 |
$class = $class->tag_start($tag_start); |
|
530 |
$tag_start = $class->tag_start; |
|
packaging one directory
|
531 |
|
532 |
# Sample |
|
533 |
$sql_tmpl->tag_start('{'); |
|
534 | ||
535 |
Default is '{' |
|
536 | ||
537 |
=head2 tag_end |
|
538 | ||
cleanup and update docment
|
539 |
Set and get end tag |
540 |
|
|
541 |
# For object |
|
542 |
$self = $self->tag_start($tag_end); |
|
543 |
$tag_end = $self->tag_start; |
|
544 |
|
|
545 |
# For class |
|
546 |
$self = $self->tag_start($tag_end); |
|
547 |
$tag_end = $self->tag_start; |
|
packaging one directory
|
548 |
|
549 |
# Sample |
|
550 |
$sql_tmpl->tag_start('}'); |
|
551 | ||
552 |
Default is '}' |
|
553 |
|
|
554 |
=head2 tag_syntax |
|
555 |
|
|
cleanup and update docment
|
556 |
Set and get tag syntax |
557 |
|
|
558 |
# For object |
|
559 |
$self = $self->tag_syntax($tag_syntax); |
|
560 |
$tag_syntax = $self->tag_syntax; |
|
561 | ||
562 |
# For class |
|
563 |
$class = $class->tag_syntax($tag_syntax); |
|
564 |
$tag_syntax = $class->tag_syntax; |
|
packaging one directory
|
565 |
|
566 |
# Sample |
|
cleanup and update docment
|
567 |
$syntax = $sql_tmpl->tag_syntax; |
packaging one directory
|
568 | |
cleanup and update docment
|
569 |
=head1 Methods |
packaging one directory
|
570 | |
571 |
=head2 create_query |
|
572 |
|
|
cleanup and update docment
|
573 |
Create L<DBIx::Custom::Query> object parsing SQL template |
574 | ||
575 |
$query = $self->create_query($tmpl); |
|
packaging one directory
|
576 |
|
577 |
# Sample |
|
578 |
$query = $sql_tmpl->create_sql( |
|
579 |
"select * from table where {= title} && {like author} || {<= price}") |
|
580 |
|
|
cleanup and update docment
|
581 |
# Expanded |
582 |
$qeury->sql : "select * from table where title = ? && author like ? price <= ?;" |
|
583 |
$query->key_infos : [['title'], ['author'], ['price']] |
|
packaging one directory
|
584 |
|
cleanup and update docment
|
585 |
# Sample with table name |
packaging one directory
|
586 |
($sql, @bind_values) = $sql_tmpl->create_sql( |
587 |
"select * from table where {= table.title} && {like table.author}", |
|
588 |
{table => {title => 'Perl', author => '%Taro%'}} |
|
589 |
) |
|
590 |
|
|
cleanup and update docment
|
591 |
# Expanded |
592 |
$query->sql : "select * from table where table.title = ? && table.title like ?;" |
|
593 |
$query->key_infos :[ [['table.title'],['table', 'title']], |
|
594 |
[['table.author'],['table', 'author']] ] |
|
packaging one directory
|
595 | |
cleanup and update docment
|
596 |
This method create query using by L<DBIx::Custom>. |
597 |
query has two infomation |
|
packaging one directory
|
598 | |
cleanup and update docment
|
599 |
1. sql : SQL |
600 |
2. key_infos : Parameter access key information |
|
packaging one directory
|
601 | |
602 |
=head2 add_tag_processor |
|
603 | ||
604 |
Add tag processor |
|
cleanup and update docment
|
605 |
|
606 |
# For object |
|
607 |
$self = $self->add_tag_processor($tag_processor); |
|
608 |
|
|
609 |
# For class |
|
610 |
$class = $class->add_tag_processor($tag_processor); |
|
packaging one directory
|
611 |
|
612 |
# Sample |
|
613 |
$sql_tmpl->add_tag_processor( |
|
614 |
'?' => sub { |
|
615 |
my ($tag_name, $tag_args) = @_; |
|
616 |
|
|
617 |
my $key1 = $tag_args->[0]; |
|
618 |
my $key2 = $tag_args->[1]; |
|
619 |
|
|
620 |
my $key_infos = []; |
|
621 |
|
|
622 |
# Expand tag and create key informations |
|
623 |
|
|
624 |
# Return expand tags and key informations |
|
625 |
return ($expand, $key_infos); |
|
626 |
} |
|
627 |
); |
|
628 | ||
629 |
Tag processor recieve 2 argument |
|
630 | ||
631 |
1. Tag name (?, =, <>, or etc) |
|
632 |
2. Tag arguments (arg1 and arg2 in {tag_name arg1 arg2}) |
|
633 | ||
634 |
Tag processor return 2 value |
|
635 | ||
636 |
1. Expanded Tag (For exsample, '{= title}' is expanded to 'title = ?') |
|
637 |
2. Key infomations |
|
638 |
|
|
639 |
You must be return expanded tag and key infomations. |
|
640 | ||
641 |
Key information is a little complex. so I will explan this in future. |
|
642 | ||
643 |
If you want to know more, Please see DBIx::Custom::SQL::Template source code. |
|
644 | ||
645 |
=head2 clone |
|
646 | ||
cleanup and update docment
|
647 |
Clone DBIx::Custom::SQL::Template object |
648 | ||
packaging one directory
|
649 |
$clone = $self->clone; |
650 |
|
|
651 |
=head1 Available Tags |
|
652 |
|
|
cleanup and update docment
|
653 |
Available Tags |
654 | ||
packaging one directory
|
655 |
[tag] [expand] |
656 |
{? name} ? |
|
657 |
{= name} name = ? |
|
658 |
{<> name} name <> ? |
|
659 |
|
|
660 |
{< name} name < ? |
|
661 |
{> name} name > ? |
|
662 |
{>= name} name >= ? |
|
663 |
{<= name} name <= ? |
|
664 |
|
|
665 |
{like name} name like ? |
|
666 |
{in name} name in [?, ?, ..] |
|
667 |
|
|
cleanup and update docment
|
668 |
{insert} (key1, key2, key3) values (?, ?, ?) |
669 |
{update} set key1 = ?, key2 = ?, key3 = ? |
|
packaging one directory
|
670 |
|
cleanup and update docment
|
671 |
# Sample |
packaging one directory
|
672 |
$query = $sql_tmpl->create_sql( |
673 |
"insert into table {insert key1 key2}" |
|
674 |
); |
|
cleanup and update docment
|
675 |
# Expanded |
676 |
$query->sql : "insert into table (key1, key2) values (?, ?)" |
|
packaging one directory
|
677 |
|
678 |
|
|
cleanup and update docment
|
679 |
# Sample |
packaging one directory
|
680 |
$query = $sql_tmpl->create_sql( |
681 |
"update table {update key1 key2} where {= key3}" |
|
682 |
); |
|
683 |
|
|
cleanup and update docment
|
684 |
# Expanded |
685 |
$query->sql : "update table set key1 = ?, key2 = ? where key3 = ?;" |
|
packaging one directory
|
686 |
|
687 |
=head1 AUTHOR |
|
688 | ||
689 |
Yuki Kimoto, C<< <kimoto.yuki at gmail.com> >> |
|
690 | ||
691 |
Github |
|
692 |
L<http://github.com/yuki-kimoto> |
|
693 |
L<http://github.com/yuki-kimoto/DBIx-Custom-SQL-Template> |
|
694 | ||
695 |
Please let know me bag if you find |
|
696 |
Please request me if you want to do something |
|
697 | ||
698 |
=head1 COPYRIGHT & LICENSE |
|
699 | ||
700 |
Copyright 2009 Yuki Kimoto, all rights reserved. |
|
701 | ||
702 |
This program is free software; you can redistribute it and/or modify it |
|
703 |
under the same terms as Perl itself. |
|
704 | ||
705 | ||
706 |
=cut |
|
707 | ||
708 |
1; # End of DBIx::Custom::SQL::Template |