| ... | ... |
@@ -186,7 +186,7 @@ sub create_query {
|
| 186 | 186 |
if ($cached_query) {
|
| 187 | 187 |
$query = DBIx::Custom::Query->new( |
| 188 | 188 |
sql => $cached_query->sql, |
| 189 |
- key_infos => $cached_query->key_infos |
|
| 189 |
+ columns => $cached_query->columns |
|
| 190 | 190 |
); |
| 191 | 191 |
} |
| 192 | 192 |
else {
|
| ... | ... |
@@ -238,7 +238,6 @@ sub query{
|
| 238 | 238 |
if (my $execute_error = $@) {
|
| 239 | 239 |
require Data::Dumper; |
| 240 | 240 |
my $sql = $query->{sql} || '';
|
| 241 |
- my $key_infos_dump = Data::Dumper->Dump([$query->key_infos], ['*key_infos']); |
|
| 242 | 241 |
my $params_dump = Data::Dumper->Dump([$params], ['*params']); |
| 243 | 242 |
|
| 244 | 243 |
croak("$execute_error" .
|
| ... | ... |
@@ -265,28 +264,32 @@ sub query{
|
| 265 | 264 |
|
| 266 | 265 |
sub _build_bind_values {
|
| 267 | 266 |
my ($self, $query, $params, $filter) = @_; |
| 268 |
- my $key_infos = $query->key_infos; |
|
| 269 |
- my $default_filter = $self->default_query_filter || ''; |
|
| 270 |
- my $filters = $self->filters; |
|
| 271 |
- $filter ||= {};
|
|
| 272 | 267 |
|
| 273 | 268 |
# binding values |
| 274 | 269 |
my @bind_values; |
| 275 | 270 |
|
| 276 | 271 |
# Build bind values |
| 277 |
- foreach my $key_info (@$key_infos) {
|
|
| 278 |
- my $column = $key_info->{column};
|
|
| 279 |
- my $pos = $key_info->{pos};
|
|
| 272 |
+ my $count = {};
|
|
| 273 |
+ foreach my $column (@{$query->columns}) {
|
|
| 280 | 274 |
|
| 281 | 275 |
# Value |
| 282 |
- my $value = defined $pos ? $params->{$column}->[$pos] : $params->{$column};
|
|
| 276 |
+ my $value = ref $params->{$column}
|
|
| 277 |
+ ? $params->{$column}->[$count->{$column} || 0]
|
|
| 278 |
+ : $params->{$column};
|
|
| 283 | 279 |
|
| 284 | 280 |
# Filter |
| 285 |
- my $fname = $filter->{$column} || $default_filter || '';
|
|
| 281 |
+ $filter ||= {};
|
|
| 286 | 282 |
|
| 283 |
+ # Filter name |
|
| 284 |
+ my $fname = $filter->{$column} || $self->default_query_filter || '';
|
|
| 285 |
+ |
|
| 286 |
+ my $filters = $self->filters; |
|
| 287 | 287 |
push @bind_values, $filters->{$fname}
|
| 288 | 288 |
? $filters->{$fname}->($value)
|
| 289 | 289 |
: $value; |
| 290 |
+ |
|
| 291 |
+ # Count up |
|
| 292 |
+ $count->{$column}++;
|
|
| 290 | 293 |
} |
| 291 | 294 |
|
| 292 | 295 |
return \@bind_values; |
| ... | ... |
@@ -420,7 +423,7 @@ our %VALID_UPDATE_ARGS |
| 420 | 423 |
= map { $_ => 1 } qw/where append filter allow_update_all/;
|
| 421 | 424 |
|
| 422 | 425 |
sub update {
|
| 423 |
- my ($self, $table, $update_params, $args) = @_; |
|
| 426 |
+ my ($self, $table, $params, $args) = @_; |
|
| 424 | 427 |
|
| 425 | 428 |
# Check arguments |
| 426 | 429 |
foreach my $name (keys %$args) {
|
| ... | ... |
@@ -435,7 +438,7 @@ sub update {
|
| 435 | 438 |
my $allow_update_all = $args->{allow_update_all};
|
| 436 | 439 |
|
| 437 | 440 |
# Update keys |
| 438 |
- my @update_keys = keys %$update_params; |
|
| 441 |
+ my @update_keys = keys %$params; |
|
| 439 | 442 |
|
| 440 | 443 |
# Not exists update kyes |
| 441 | 444 |
croak("Key-value pairs for update must be specified to 'update' second argument")
|
| ... | ... |
@@ -453,15 +456,13 @@ sub update {
|
| 453 | 456 |
|
| 454 | 457 |
# Where clause |
| 455 | 458 |
my $where_clause = ''; |
| 456 |
- my $new_where_params = {};
|
|
| 459 |
+ my $new_where = {};
|
|
| 457 | 460 |
|
| 458 | 461 |
if (@where_keys) {
|
| 459 | 462 |
$where_clause = 'where '; |
| 460 | 463 |
foreach my $where_key (@where_keys) {
|
| 461 |
- $new_where_params->{"$where_key@where"}
|
|
| 462 |
- = $where_params->{$where_key};
|
|
| 463 |
- |
|
| 464 |
- $where_clause .= "{= $where_key@where} and ";
|
|
| 464 |
+ |
|
| 465 |
+ $where_clause .= "{= $where_key} and ";
|
|
| 465 | 466 |
} |
| 466 | 467 |
$where_clause =~ s/ and $//; |
| 467 | 468 |
} |
| ... | ... |
@@ -471,7 +472,15 @@ sub update {
|
| 471 | 472 |
$template .= " $append_statement" if $append_statement; |
| 472 | 473 |
|
| 473 | 474 |
# Rearrange parammeters |
| 474 |
- my $params = {%$update_params, %$new_where_params};
|
|
| 475 |
+ foreach my $where_key (@where_keys) {
|
|
| 476 |
+ |
|
| 477 |
+ if (exists $params->{$where_key}) {
|
|
| 478 |
+ $params->{$where_key} = [$params->{$where_key}]
|
|
| 479 |
+ unless ref $params->{$where_key} eq 'ARRAY';
|
|
| 480 |
+ |
|
| 481 |
+ push @{$params->{$where_key}}, $where_params->{$where_key}};
|
|
| 482 |
+ } |
|
| 483 |
+ } |
|
| 475 | 484 |
|
| 476 | 485 |
# Execute query |
| 477 | 486 |
my $ret_val = $self->query($template, $params, {filter => $filter});
|
| ... | ... |
@@ -595,23 +604,11 @@ sub select {
|
| 595 | 604 |
# Where clause keys |
| 596 | 605 |
my @where_keys = keys %$where_params; |
| 597 | 606 |
|
| 598 |
- my $where_params_new = {};
|
|
| 599 |
- |
|
| 600 | 607 |
# Join where clause |
| 601 | 608 |
if (@where_keys) {
|
| 602 | 609 |
$template .= 'where '; |
| 603 | 610 |
foreach my $where_key (@where_keys) {
|
| 604 |
- my $key_info = DBIx::Custom::KeyInfo->new($where_key); |
|
| 605 |
- |
|
| 606 |
- my $table_new = $key_info->table || $tables->[0]; |
|
| 607 |
- my $column = $table_new . '.' . $key_info->column |
|
| 608 |
- . '#' . $table_new; |
|
| 609 |
- |
|
| 610 | 611 |
$template .= "{= $column} and ";
|
| 611 |
- |
|
| 612 |
- $where_params_new->{$table_new} ||= {};
|
|
| 613 |
- $where_params_new->{$table_new}->{$key_info->column}
|
|
| 614 |
- = $where_params->{$where_key};
|
|
| 615 | 612 |
} |
| 616 | 613 |
} |
| 617 | 614 |
$template =~ s/ and $//; |
| ... | ... |
@@ -630,7 +627,7 @@ sub select {
|
| 630 | 627 |
} |
| 631 | 628 |
|
| 632 | 629 |
# Execute query |
| 633 |
- my $result = $self->query($template, $where_params_new, {filter => $filter});
|
|
| 630 |
+ my $result = $self->query($template, $where_params, {filter => $filter});
|
|
| 634 | 631 |
|
| 635 | 632 |
return $result; |
| 636 | 633 |
} |
| ... | ... |
@@ -5,7 +5,7 @@ use warnings; |
| 5 | 5 |
|
| 6 | 6 |
use base 'Object::Simple'; |
| 7 | 7 |
|
| 8 |
-__PACKAGE__->attr([qw/sql key_infos default_filter filter sth/]); |
|
| 8 |
+__PACKAGE__->attr([qw/sql columns default_filter filter sth/]); |
|
| 9 | 9 |
|
| 10 | 10 |
1; |
| 11 | 11 |
|
| ... | ... |
@@ -51,12 +51,12 @@ Filter excuted when value is bind |
| 51 | 51 |
$query = $query->filter($filter); |
| 52 | 52 |
$filter = $query->filter; |
| 53 | 53 |
|
| 54 |
-=head2 key_infos |
|
| 54 |
+=head2 columns |
|
| 55 | 55 |
|
| 56 | 56 |
Key informations |
| 57 | 57 |
|
| 58 |
- $query = $query->key_infos($key_infos); |
|
| 59 |
- $key_infos = $query->key_infos; |
|
| 58 |
+ $query = $query->columns($columns); |
|
| 59 |
+ $columns = $query->columns; |
|
| 60 | 60 |
|
| 61 | 61 |
=head1 METHODS |
| 62 | 62 |
|
| ... | ... |
@@ -4,8 +4,10 @@ use strict; |
| 4 | 4 |
use warnings; |
| 5 | 5 |
|
| 6 | 6 |
use base 'Object::Simple'; |
| 7 |
+ |
|
| 7 | 8 |
use Carp 'croak'; |
| 8 | 9 |
use DBIx::Custom::Query; |
| 10 |
+use DBIx::Custom::SQLTemplate::TagProcessors; |
|
| 9 | 11 |
|
| 10 | 12 |
__PACKAGE__->dual_attr('tag_processors', default => sub { {} },
|
| 11 | 13 |
inherit => 'hash_copy'); |
| ... | ... |
@@ -15,7 +17,7 @@ __PACKAGE__->dual_attr('tag_end', default => '}', inherit => 'scalar_copy');
|
| 15 | 17 |
|
| 16 | 18 |
__PACKAGE__->dual_attr('tag_syntax', inherit => 'scalar_copy');
|
| 17 | 19 |
|
| 18 |
-__PACKAGE__->add_tag_processor( |
|
| 20 |
+__PACKAGE__->resist_tag_processor( |
|
| 19 | 21 |
'?' => \&DBIx::Custom::SQLTemplate::TagProcessors::expand_basic_tag, |
| 20 | 22 |
'=' => \&DBIx::Custom::SQLTemplate::TagProcessors::expand_basic_tag, |
| 21 | 23 |
'<>' => \&DBIx::Custom::SQLTemplate::TagProcessors::expand_basic_tag, |
| ... | ... |
@@ -48,7 +50,7 @@ __PACKAGE__->tag_syntax(<< 'EOS'); |
| 48 | 50 |
EOS |
| 49 | 51 |
|
| 50 | 52 |
|
| 51 |
-sub add_tag_processor {
|
|
| 53 |
+sub resist_tag_processor {
|
|
| 52 | 54 |
my $invocant = shift; |
| 53 | 55 |
my $tag_processors = ref $_[0] eq 'HASH' ? $_[0] : {@_};
|
| 54 | 56 |
$invocant->tag_processors({%{$invocant->tag_processors}, %{$tag_processors}});
|
| ... | ... |
@@ -146,7 +148,7 @@ sub _build_query {
|
| 146 | 148 |
my $sql = ''; |
| 147 | 149 |
|
| 148 | 150 |
# All parameter key infomation |
| 149 |
- my $all_key_infos = []; |
|
| 151 |
+ my $all_columns = []; |
|
| 150 | 152 |
|
| 151 | 153 |
# Build SQL |
| 152 | 154 |
foreach my $node (@$tree) {
|
| ... | ... |
@@ -173,20 +175,19 @@ sub _build_query {
|
| 173 | 175 |
unless ref $tag_processor eq 'CODE'; |
| 174 | 176 |
|
| 175 | 177 |
# Expand tag using tag processor |
| 176 |
- my ($expand, $key_infos) |
|
| 177 |
- = $tag_processor->($tag_name, $tag_args); |
|
| 178 |
+ my ($expand, $columns) = $tag_processor->($tag_name, $tag_args); |
|
| 178 | 179 |
|
| 179 | 180 |
# Check tag processor return value |
| 180 |
- croak("Tag processor '$tag_name' must return (\$expand, \$key_infos)")
|
|
| 181 |
- if !defined $expand || ref $key_infos ne 'ARRAY'; |
|
| 181 |
+ croak("Tag processor '$tag_name' must return (\$expand, \$columns)")
|
|
| 182 |
+ if !defined $expand || ref $columns ne 'ARRAY'; |
|
| 182 | 183 |
|
| 183 | 184 |
# Check placeholder count |
| 184 | 185 |
croak("Placeholder count in SQL created by tag processor '$tag_name' " .
|
| 185 | 186 |
"must be same as key informations count") |
| 186 |
- unless $self->_placeholder_count($expand) eq @$key_infos; |
|
| 187 |
+ unless $self->_placeholder_count($expand) eq @$columns; |
|
| 187 | 188 |
|
| 188 | 189 |
# Add key information |
| 189 |
- push @$all_key_infos, @$key_infos; |
|
| 190 |
+ push @$all_columns, @$columns; |
|
| 190 | 191 |
|
| 191 | 192 |
# Join expand tag to SQL |
| 192 | 193 |
$sql .= $expand; |
| ... | ... |
@@ -197,7 +198,7 @@ sub _build_query {
|
| 197 | 198 |
$sql .= ';' unless $sql =~ /;$/; |
| 198 | 199 |
|
| 199 | 200 |
# Query |
| 200 |
- my $query = DBIx::Custom::Query->new(sql => $sql, key_infos => $all_key_infos); |
|
| 201 |
+ my $query = DBIx::Custom::Query->new(sql => $sql, columns => $all_columns); |
|
| 201 | 202 |
|
| 202 | 203 |
return $query; |
| 203 | 204 |
} |
| ... | ... |
@@ -216,134 +217,6 @@ sub _placeholder_count {
|
| 216 | 217 |
|
| 217 | 218 |
1; |
| 218 | 219 |
|
| 219 |
-package DBIx::Custom::SQLTemplate::TagProcessors; |
|
| 220 |
- |
|
| 221 |
-use strict; |
|
| 222 |
-use warnings; |
|
| 223 |
- |
|
| 224 |
-use Carp 'croak'; |
|
| 225 |
-use DBIx::Custom::KeyInfo; |
|
| 226 |
- |
|
| 227 |
-sub expand_basic_tag {
|
|
| 228 |
- my ($tag_name, $tag_args) = @_; |
|
| 229 |
- |
|
| 230 |
- # Key |
|
| 231 |
- my $column = $tag_args->[0]; |
|
| 232 |
- |
|
| 233 |
- # Key is not exist |
|
| 234 |
- croak("You must be pass key as argument to tag '{$tag_name }'")
|
|
| 235 |
- unless $column; |
|
| 236 |
- |
|
| 237 |
- # delete ID |
|
| 238 |
- |
|
| 239 |
- |
|
| 240 |
- # Expanded tag |
|
| 241 |
- my $expand = $tag_name eq '?' |
|
| 242 |
- ? '?' |
|
| 243 |
- : "$column $tag_name ?"; |
|
| 244 |
- |
|
| 245 |
- return ($expand, [{column => $column}]);
|
|
| 246 |
-} |
|
| 247 |
- |
|
| 248 |
-sub expand_in_tag {
|
|
| 249 |
- my ($tag_name, $tag_args) = @_; |
|
| 250 |
- my ($column, $placeholder_count) = @$tag_args; |
|
| 251 |
- |
|
| 252 |
- # Key must be specified |
|
| 253 |
- croak("You must be pass key as first argument of tag '{$tag_name }'\n" .
|
|
| 254 |
- "Usage: {$tag_name \$key \$placeholder_count}")
|
|
| 255 |
- unless $column; |
|
| 256 |
- |
|
| 257 |
- # Place holder count must be specified |
|
| 258 |
- croak("You must be pass placeholder count as second argument of tag '{$tag_name }'\n" .
|
|
| 259 |
- "Usage: {$tag_name \$key \$placeholder_count}")
|
|
| 260 |
- if !$placeholder_count || $placeholder_count =~ /\D/; |
|
| 261 |
- |
|
| 262 |
- # Expand tag |
|
| 263 |
- my $expand = "$column $tag_name (";
|
|
| 264 |
- for (my $i = 0; $i < $placeholder_count; $i++) {
|
|
| 265 |
- $expand .= '?, '; |
|
| 266 |
- } |
|
| 267 |
- |
|
| 268 |
- $expand =~ s/, $//; |
|
| 269 |
- $expand .= ')'; |
|
| 270 |
- |
|
| 271 |
- # Create parameter key infomations |
|
| 272 |
- my $key_infos = []; |
|
| 273 |
- for (my $i = 0; $i < $placeholder_count; $i++) {
|
|
| 274 |
- |
|
| 275 |
- # Add parameter key infos |
|
| 276 |
- push @$key_infos, {column => $column, pos => $i};
|
|
| 277 |
- } |
|
| 278 |
- |
|
| 279 |
- return ($expand, $key_infos); |
|
| 280 |
-} |
|
| 281 |
- |
|
| 282 |
-sub expand_insert_tag {
|
|
| 283 |
- my ($tag_name, $columns) = @_; |
|
| 284 |
- |
|
| 285 |
- # Insert key (k1, k2, k3, ..) |
|
| 286 |
- my $insert_keys = '(';
|
|
| 287 |
- |
|
| 288 |
- # placeholder (?, ?, ?, ..) |
|
| 289 |
- my $place_holders = '(';
|
|
| 290 |
- |
|
| 291 |
- foreach my $column (@$columns) {
|
|
| 292 |
- |
|
| 293 |
- # Join insert column |
|
| 294 |
- $insert_keys .= "$column, "; |
|
| 295 |
- |
|
| 296 |
- # Join place holder |
|
| 297 |
- $place_holders .= "?, "; |
|
| 298 |
- } |
|
| 299 |
- |
|
| 300 |
- # Delete last ', ' |
|
| 301 |
- $insert_keys =~ s/, $//; |
|
| 302 |
- |
|
| 303 |
- # Close |
|
| 304 |
- $insert_keys .= ')'; |
|
| 305 |
- $place_holders =~ s/, $//; |
|
| 306 |
- $place_holders .= ')'; |
|
| 307 |
- |
|
| 308 |
- # Expand tag |
|
| 309 |
- my $expand = "$insert_keys values $place_holders"; |
|
| 310 |
- |
|
| 311 |
- # Create parameter key infomations |
|
| 312 |
- my $key_infos = []; |
|
| 313 |
- foreach my $column (@$columns) {
|
|
| 314 |
- push @$key_infos, {column => $column};
|
|
| 315 |
- } |
|
| 316 |
- |
|
| 317 |
- return ($expand, $key_infos); |
|
| 318 |
-} |
|
| 319 |
- |
|
| 320 |
-sub expand_update_tag {
|
|
| 321 |
- my ($tag_name, $columns) = @_; |
|
| 322 |
- |
|
| 323 |
- # Expanded tag |
|
| 324 |
- my $expand = 'set '; |
|
| 325 |
- |
|
| 326 |
- foreach my $column (@$columns) {
|
|
| 327 |
- |
|
| 328 |
- # Join key and placeholder |
|
| 329 |
- $expand .= "$column = ?, "; |
|
| 330 |
- } |
|
| 331 |
- |
|
| 332 |
- # Delete last ', ' |
|
| 333 |
- $expand =~ s/, $//; |
|
| 334 |
- |
|
| 335 |
- my $key_infos = []; |
|
| 336 |
- foreach my $column (@$columns) {
|
|
| 337 |
- push @$key_infos, {column => $column};
|
|
| 338 |
- } |
|
| 339 |
- |
|
| 340 |
- return ($expand, $key_infos); |
|
| 341 |
-} |
|
| 342 |
- |
|
| 343 |
-package DBIx::Custom::SQLTemplate; |
|
| 344 |
- |
|
| 345 |
-1; |
|
| 346 |
- |
|
| 347 | 220 |
=head1 NAME |
| 348 | 221 |
|
| 349 | 222 |
DBIx::Custom::SQLTemplate - DBIx::Custom SQL Template |
| ... | ... |
@@ -420,15 +293,15 @@ query has two infomation |
| 420 | 293 |
1. sql : SQL |
| 421 | 294 |
2. key_infos : Parameter access key information |
| 422 | 295 |
|
| 423 |
-=head2 add_tag_processor |
|
| 296 |
+=head2 resist_tag_processor |
|
| 424 | 297 |
|
| 425 | 298 |
Add tag processor |
| 426 | 299 |
|
| 427 |
- $sql_tmpl = $sql_tmpl->add_tag_processor($tag_processor); |
|
| 300 |
+ $sql_tmpl = $sql_tmpl->resist_tag_processor($tag_processor); |
|
| 428 | 301 |
|
| 429 |
-The following is add_tag_processor sample |
|
| 302 |
+The following is resist_tag_processor sample |
|
| 430 | 303 |
|
| 431 |
- $sql_tmpl->add_tag_processor( |
|
| 304 |
+ $sql_tmpl->resist_tag_processor( |
|
| 432 | 305 |
'?' => sub {
|
| 433 | 306 |
my ($tag_name, $tag_args) = @_; |
| 434 | 307 |
|
| ... | ... |
@@ -0,0 +1,131 @@ |
| 1 |
+package DBIx::Custom::SQLTemplate::TagProcessors; |
|
| 2 |
+ |
|
| 3 |
+use strict; |
|
| 4 |
+use warnings; |
|
| 5 |
+ |
|
| 6 |
+use Carp 'croak'; |
|
| 7 |
+ |
|
| 8 |
+sub expand_basic_tag {
|
|
| 9 |
+ my ($tag_name, $tag_args) = @_; |
|
| 10 |
+ |
|
| 11 |
+ # Key |
|
| 12 |
+ my $column = $tag_args->[0]; |
|
| 13 |
+ |
|
| 14 |
+ # Key is not exist |
|
| 15 |
+ croak("You must be pass key as argument to tag '{$tag_name }'")
|
|
| 16 |
+ unless $column; |
|
| 17 |
+ |
|
| 18 |
+ # Expand |
|
| 19 |
+ return ("$column $tag_name ?", [$column]);
|
|
| 20 |
+} |
|
| 21 |
+ |
|
| 22 |
+sub expand_placeholder_tag {
|
|
| 23 |
+ my ($tag_name, $tag_args) = @_; |
|
| 24 |
+ |
|
| 25 |
+ # Key |
|
| 26 |
+ my $column = $tag_args->[0]; |
|
| 27 |
+ |
|
| 28 |
+ # Key is not exist |
|
| 29 |
+ croak("You must be pass key as argument to tag '{$tag_name }'")
|
|
| 30 |
+ unless $column; |
|
| 31 |
+ |
|
| 32 |
+ # Expand |
|
| 33 |
+ return ('?', [$column]);
|
|
| 34 |
+} |
|
| 35 |
+ |
|
| 36 |
+sub expand_in_tag {
|
|
| 37 |
+ my ($tag_name, $tag_args) = @_; |
|
| 38 |
+ my ($column, $count) = @$tag_args; |
|
| 39 |
+ |
|
| 40 |
+ # Key must be specified |
|
| 41 |
+ croak("You must be pass key as first argument of tag '{$tag_name }'\n" .
|
|
| 42 |
+ "Usage: {$tag_name \$key \$count}")
|
|
| 43 |
+ unless $column; |
|
| 44 |
+ |
|
| 45 |
+ # Place holder count must be specified |
|
| 46 |
+ croak("You must be pass placeholder count as second argument of tag '{$tag_name }'\n" .
|
|
| 47 |
+ "Usage: {$tag_name \$key \$count}")
|
|
| 48 |
+ if !$count || $count =~ /\D/; |
|
| 49 |
+ |
|
| 50 |
+ # Expand tag |
|
| 51 |
+ my $expand = "$column $tag_name (";
|
|
| 52 |
+ for (my $i = 0; $i < $count; $i++) {
|
|
| 53 |
+ $expand .= '?, '; |
|
| 54 |
+ } |
|
| 55 |
+ |
|
| 56 |
+ $expand =~ s/, $//; |
|
| 57 |
+ $expand .= ')'; |
|
| 58 |
+ |
|
| 59 |
+ # Columns |
|
| 60 |
+ my $columns = []; |
|
| 61 |
+ puhs @$columns, $column for (0 .. $count - 1); |
|
| 62 |
+ |
|
| 63 |
+ return ($expand, $columns); |
|
| 64 |
+} |
|
| 65 |
+ |
|
| 66 |
+sub expand_insert_tag {
|
|
| 67 |
+ my ($tag_name, $columns) = @_; |
|
| 68 |
+ |
|
| 69 |
+ # Insert key (k1, k2, k3, ..) |
|
| 70 |
+ my $insert_keys = '(';
|
|
| 71 |
+ |
|
| 72 |
+ # placeholder (?, ?, ?, ..) |
|
| 73 |
+ my $place_holders = '(';
|
|
| 74 |
+ |
|
| 75 |
+ foreach my $column (@$columns) {
|
|
| 76 |
+ |
|
| 77 |
+ # Join insert column |
|
| 78 |
+ $insert_keys .= "$column, "; |
|
| 79 |
+ |
|
| 80 |
+ # Join place holder |
|
| 81 |
+ $place_holders .= "?, "; |
|
| 82 |
+ } |
|
| 83 |
+ |
|
| 84 |
+ # Delete last ', ' |
|
| 85 |
+ $insert_keys =~ s/, $//; |
|
| 86 |
+ |
|
| 87 |
+ # Close |
|
| 88 |
+ $insert_keys .= ')'; |
|
| 89 |
+ $place_holders =~ s/, $//; |
|
| 90 |
+ $place_holders .= ')'; |
|
| 91 |
+ |
|
| 92 |
+ # Expand tag |
|
| 93 |
+ my $expand = "$insert_keys values $place_holders"; |
|
| 94 |
+ |
|
| 95 |
+ return ($expand, [@$columns]); |
|
| 96 |
+} |
|
| 97 |
+ |
|
| 98 |
+sub expand_update_tag {
|
|
| 99 |
+ my ($tag_name, $columns) = @_; |
|
| 100 |
+ |
|
| 101 |
+ # Expanded tag |
|
| 102 |
+ my $expand = 'set '; |
|
| 103 |
+ |
|
| 104 |
+ foreach my $column (@$columns) {
|
|
| 105 |
+ |
|
| 106 |
+ # Join key and placeholder |
|
| 107 |
+ $expand .= "$column = ?, "; |
|
| 108 |
+ } |
|
| 109 |
+ |
|
| 110 |
+ # Delete last ', ' |
|
| 111 |
+ $expand =~ s/, $//; |
|
| 112 |
+ |
|
| 113 |
+ return ($expand, [@$columns]); |
|
| 114 |
+} |
|
| 115 |
+ |
|
| 116 |
+1; |
|
| 117 |
+ |
|
| 118 |
+=head1 NAME |
|
| 119 |
+ |
|
| 120 |
+DBIx::Custom::SQLTemplate::TagProcessor - Tag processor |
|
| 121 |
+ |
|
| 122 |
+=head1 FUNCTIONS |
|
| 123 |
+ |
|
| 124 |
+=head2 expand_basic_tag |
|
| 125 |
+ |
|
| 126 |
+=head2 expand_in_tag |
|
| 127 |
+ |
|
| 128 |
+=head2 expand_insert_tag |
|
| 129 |
+ |
|
| 130 |
+=head2 expand_update_tag |
|
| 131 |
+ |
| ... | ... |
@@ -87,7 +87,7 @@ for (my $i = 0; $i < @$datas; $i++) {
|
| 87 | 87 |
test 'Original tag processor'; |
| 88 | 88 |
$sql_tmpl = DBIx::Custom::SQLTemplate->new; |
| 89 | 89 |
|
| 90 |
-$ret_val = $sql_tmpl->add_tag_processor( |
|
| 90 |
+$ret_val = $sql_tmpl->resist_tag_processor( |
|
| 91 | 91 |
p => sub {
|
| 92 | 92 |
my ($tag_name, $args) = @_; |
| 93 | 93 |
|
| ... | ... |
@@ -98,8 +98,8 @@ $ret_val = $sql_tmpl->add_tag_processor( |
| 98 | 98 |
); |
| 99 | 99 |
|
| 100 | 100 |
$query = $sql_tmpl->create_query("{p a b}");
|
| 101 |
-is($query->{sql}, "p ? a b;", "$test : add_tag_processor sql");
|
|
| 102 |
-is_deeply($query->{key_infos}, [2], "$test : add_tag_processor key_infos");
|
|
| 101 |
+is($query->{sql}, "p ? a b;", "$test : resist_tag_processor sql");
|
|
| 102 |
+is_deeply($query->{key_infos}, [2], "$test : resist_tag_processor key_infos");
|
|
| 103 | 103 |
isa_ok($ret_val, 'DBIx::Custom::SQLTemplate'); |
| 104 | 104 |
|
| 105 | 105 |
|
| ... | ... |
@@ -110,28 +110,28 @@ $sql_tmpl = DBIx::Custom::SQLTemplate->new; |
| 110 | 110 |
eval{$sql_tmpl->create_query("{a }")};
|
| 111 | 111 |
like($@, qr/Tag '{a }' in SQL template is not exist/, "$test : tag_processor not exist");
|
| 112 | 112 |
|
| 113 |
-$sql_tmpl->add_tag_processor({
|
|
| 113 |
+$sql_tmpl->resist_tag_processor({
|
|
| 114 | 114 |
q => 'string' |
| 115 | 115 |
}); |
| 116 | 116 |
|
| 117 | 117 |
eval{$sql_tmpl->create_query("{q}", {})};
|
| 118 | 118 |
like($@, qr/Tag processor 'q' must be code reference/, "$test : tag_processor not code ref"); |
| 119 | 119 |
|
| 120 |
-$sql_tmpl->add_tag_processor({
|
|
| 120 |
+$sql_tmpl->resist_tag_processor({
|
|
| 121 | 121 |
r => sub {}
|
| 122 | 122 |
}); |
| 123 | 123 |
|
| 124 | 124 |
eval{$sql_tmpl->create_query("{r}")};
|
| 125 | 125 |
like($@, qr/\QTag processor 'r' must return (\E\$expand\Q, \E\$key_infos\Q)/, "$test : tag processor return noting"); |
| 126 | 126 |
|
| 127 |
-$sql_tmpl->add_tag_processor({
|
|
| 127 |
+$sql_tmpl->resist_tag_processor({
|
|
| 128 | 128 |
s => sub { return ("a", "")}
|
| 129 | 129 |
}); |
| 130 | 130 |
|
| 131 | 131 |
eval{$sql_tmpl->create_query("{s}")};
|
| 132 | 132 |
like($@, qr/\QTag processor 's' must return (\E\$expand\Q, \E\$key_infos\Q)/, "$test : tag processor return not array key_infos"); |
| 133 | 133 |
|
| 134 |
-$sql_tmpl->add_tag_processor( |
|
| 134 |
+$sql_tmpl->resist_tag_processor( |
|
| 135 | 135 |
t => sub {return ("a", [])}
|
| 136 | 136 |
); |
| 137 | 137 |
|
| ... | ... |
@@ -141,7 +141,7 @@ like($@, qr/Tag '{t }' arguments cannot contain '?'/, "$test : cannot contain '?
|
| 141 | 141 |
|
| 142 | 142 |
test 'General error case'; |
| 143 | 143 |
$sql_tmpl = DBIx::Custom::SQLTemplate->new; |
| 144 |
-$sql_tmpl->add_tag_processor( |
|
| 144 |
+$sql_tmpl->resist_tag_processor( |
|
| 145 | 145 |
a => sub {
|
| 146 | 146 |
return ("? ? ?", [[],[]]);
|
| 147 | 147 |
} |