... | ... |
@@ -1,5 +1,5 @@ |
1 | 1 |
0.1744 |
2 |
- - improved performance |
|
2 |
+ - added EXPERIMENTAL reuse_sth option to execute method |
|
3 | 3 |
- moved DBIx::Custom::Guide to wiki |
4 | 4 |
0.1733 |
5 | 5 |
- select method join option can receive string. |
... | ... |
@@ -363,9 +363,9 @@ sub execute { |
363 | 363 |
$sql .= $opt{append} if defined $opt{append} && !ref $sql; |
364 | 364 |
|
365 | 365 |
# Query |
366 |
- my $query = ref $sql |
|
367 |
- ? $sql |
|
368 |
- : $self->_create_query($sql,$opt{after_build_sql} || $opt{sqlfilter}); |
|
366 |
+ my $query = ref $sql ? $sql |
|
367 |
+ : $self->_create_query($sql,$opt{after_build_sql} || $opt{sqlfilter}, |
|
368 |
+ $opt{reuse_sth}); |
|
369 | 369 |
|
370 | 370 |
# Save query |
371 | 371 |
$self->last_sql($query->sql); |
... | ... |
@@ -1096,7 +1096,7 @@ sub where { DBIx::Custom::Where->new(dbi => shift, @_) } |
1096 | 1096 |
|
1097 | 1097 |
sub _create_query { |
1098 | 1098 |
|
1099 |
- my ($self, $source, $after_build_sql) = @_; |
|
1099 |
+ my ($self, $source, $after_build_sql, $reuse_sth) = @_; |
|
1100 | 1100 |
|
1101 | 1101 |
# Cache |
1102 | 1102 |
my $cache = $self->cache; |
... | ... |
@@ -1153,7 +1153,9 @@ sub _create_query { |
1153 | 1153 |
|
1154 | 1154 |
# Prepare statement handle |
1155 | 1155 |
my $sth; |
1156 |
- eval { $sth = $self->dbh->prepare($query->{sql})}; |
|
1156 |
+ $sth = $reuse_sth->{$query->{sql}} if $reuse_sth; |
|
1157 |
+ eval { $sth = $self->dbh->prepare($query->{sql}) } unless $sth; |
|
1158 |
+ $reuse_sth->{$query->{sql}} = $sth if $reuse_sth; |
|
1157 | 1159 |
|
1158 | 1160 |
if ($@) { |
1159 | 1161 |
$self->_croak($@, qq{. Following SQL is executed.\n} |
... | ... |
@@ -2429,6 +2431,26 @@ The following opitons are available. |
2429 | 2431 |
|
2430 | 2432 |
=over 4 |
2431 | 2433 |
|
2434 |
+=item C<after_build_sql> |
|
2435 |
+ |
|
2436 |
+You can filter sql after the sql is build. |
|
2437 |
+ |
|
2438 |
+ after_build_sql => $code_ref |
|
2439 |
+ |
|
2440 |
+The following one is one example. |
|
2441 |
+ |
|
2442 |
+ $dbi->select( |
|
2443 |
+ table => 'book', |
|
2444 |
+ column => 'distinct(name)', |
|
2445 |
+ after_build_sql => sub { |
|
2446 |
+ "select count(*) from ($_[0]) as t1" |
|
2447 |
+ } |
|
2448 |
+ ); |
|
2449 |
+ |
|
2450 |
+The following SQL is executed. |
|
2451 |
+ |
|
2452 |
+ select count(*) from (select distinct(name) from book) as t1; |
|
2453 |
+ |
|
2432 | 2454 |
=item C<append> |
2433 | 2455 |
|
2434 | 2456 |
append => 'order by name' |
... | ... |
@@ -2535,26 +2557,6 @@ and don't forget to sort $row values by $row key asc order. |
2535 | 2557 |
|
2536 | 2558 |
Priamry key. This is used when C<id> option find primary key. |
2537 | 2559 |
|
2538 |
-=item C<after_build_sql> |
|
2539 |
- |
|
2540 |
-You can filter sql after the sql is build. |
|
2541 |
- |
|
2542 |
- after_build_sql => $code_ref |
|
2543 |
- |
|
2544 |
-The following one is one example. |
|
2545 |
- |
|
2546 |
- $dbi->select( |
|
2547 |
- table => 'book', |
|
2548 |
- column => 'distinct(name)', |
|
2549 |
- after_build_sql => sub { |
|
2550 |
- "select count(*) from ($_[0]) as t1" |
|
2551 |
- } |
|
2552 |
- ); |
|
2553 |
- |
|
2554 |
-The following SQL is executed. |
|
2555 |
- |
|
2556 |
- select count(*) from (select distinct(name) from book) as t1; |
|
2557 |
- |
|
2558 | 2560 |
=item C<table> |
2559 | 2561 |
|
2560 | 2562 |
table => 'author' |
... | ... |
@@ -2579,6 +2581,18 @@ Table alias. Key is real table name, value is alias table name. |
2579 | 2581 |
If you set C<table_alias>, you can enable C<into1> and C<into2> type rule |
2580 | 2582 |
on alias table name. |
2581 | 2583 |
|
2584 |
+=item C<reuse_sth EXPERIMENTAL> |
|
2585 |
+ |
|
2586 |
+ reuse_sth => $has_ref |
|
2587 |
+ |
|
2588 |
+Reuse statament handle if the hash reference variable is set. |
|
2589 |
+ |
|
2590 |
+ my $sth = {}; |
|
2591 |
+ $dbi->execute($sql, $param, sth => $sth); |
|
2592 |
+ |
|
2593 |
+This will improved performance when same sql is executed repeatedly |
|
2594 |
+because generally creating statement handle is slow. |
|
2595 |
+ |
|
2582 | 2596 |
=item C<type_rule_off> |
2583 | 2597 |
|
2584 | 2598 |
type_rule_off => 1 |
... | ... |
@@ -80,6 +80,7 @@ my $user_table_info; |
80 | 80 |
my $user_column_info; |
81 | 81 |
my $values_clause; |
82 | 82 |
my $assign_clause; |
83 |
+my $reuse_sth; |
|
83 | 84 |
|
84 | 85 |
require MyDBI1; |
85 | 86 |
{ |
... | ... |
@@ -237,6 +238,16 @@ require MyDBI1; |
237 | 238 |
} |
238 | 239 |
} |
239 | 240 |
|
241 |
+test 'execute reuse_sth option'; |
|
242 |
+eval { $dbi->execute("drop table $table1") }; |
|
243 |
+$dbi->execute($create_table1); |
|
244 |
+$reuse_sth = {}; |
|
245 |
+for my $i (1 .. 2) { |
|
246 |
+ $dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2}, reuse_sth => $reuse_sth); |
|
247 |
+} |
|
248 |
+$rows = $dbi->select(table => $table1)->all; |
|
249 |
+is_deeply($rows, [{$key1 => 1, $key2 => 2}, {$key1 => 1, $key2 => 2}]); |
|
250 |
+ |
|
240 | 251 |
# Get user table info |
241 | 252 |
$dbi = DBIx::Custom->connect; |
242 | 253 |
eval { $dbi->execute("drop table $table1") }; |
... | ... |
@@ -324,7 +335,7 @@ $rows = $result->all; |
324 | 335 |
is_deeply($rows, [{$key1 => 2, $key2 => 4}], "filter"); |
325 | 336 |
|
326 | 337 |
test 'DBIx::Custom::SQLTemplate basic tag'; |
327 |
-$dbi->execute("drop table $table1"); |
|
338 |
+eval { $dbi->execute("drop table $table1") }; |
|
328 | 339 |
$dbi->execute($create_table1_2); |
329 | 340 |
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2, $key3 => 3, $key4 => 4, $key5 => 5}); |
330 | 341 |
$dbi->insert(table => $table1, param => {$key1 => 6, $key2 => 7, $key3 => 8, $key4 => 9, $key5 => 10}); |
... | ... |
@@ -348,7 +359,7 @@ $rows = $result->all; |
348 | 359 |
is_deeply($rows, [{$key1 => 1, $key2 => 2, $key3 => 3, $key4 => 4, $key5 => 5}], "basic tag2"); |
349 | 360 |
|
350 | 361 |
test 'DIB::Custom::SQLTemplate in tag'; |
351 |
-$dbi->execute("drop table $table1"); |
|
362 |
+eval { $dbi->execute("drop table $table1") }; |
|
352 | 363 |
$dbi->execute($create_table1_2); |
353 | 364 |
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2, $key3 => 3, $key4 => 4, $key5 => 5}); |
354 | 365 |
$dbi->insert(table => $table1, param => {$key1 => 6, $key2 => 7, $key3 => 8, $key4 => 9, $key5 => 10}); |
... | ... |
@@ -383,7 +394,7 @@ is_deeply($rows, [{$key1 => 1, $key2 => 1, $key3 => 1, $key4 => 1, $key5 => 5}, |
383 | 394 |
{$key1 => 6, $key2 => 7, $key3 => 8, $key4 => 9, $key5 => 10}], "basic"); |
384 | 395 |
|
385 | 396 |
test 'Named placeholder'; |
386 |
-$dbi->execute("drop table $table1"); |
|
397 |
+eval { $dbi->execute("drop table $table1") }; |
|
387 | 398 |
$dbi->execute($create_table1_2); |
388 | 399 |
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2, $key3 => 3, $key4 => 4, $key5 => 5}); |
389 | 400 |
$dbi->insert(table => $table1, param => {$key1 => 6, $key2 => 7, $key3 => 8, $key4 => 9, $key5 => 10}); |
... | ... |
@@ -412,7 +423,7 @@ $result = $dbi->execute( |
412 | 423 |
$rows = $result->all; |
413 | 424 |
is_deeply($rows, [{$key1 => 1, $key2 => 2, $key3 => 3, $key4 => 4, $key5 => 5}]); |
414 | 425 |
|
415 |
-$dbi->execute("drop table $table1"); |
|
426 |
+eval { $dbi->execute("drop table $table1") }; |
|
416 | 427 |
$dbi->execute($create_table1); |
417 | 428 |
$dbi->insert(table => $table1, param => {$key1 => '2011-10-14 12:19:18', $key2 => 2}); |
418 | 429 |
$source = "select * from $table1 where $key1 = '2011-10-14 12:19:18' and $key2 = :$key2"; |
... | ... |
@@ -463,7 +474,7 @@ $rows = $result->all; |
463 | 474 |
is_deeply($rows, [{$key1 => 3, $key2 => 4}], "filter"); |
464 | 475 |
$dbi->default_bind_filter(undef); |
465 | 476 |
|
466 |
-$dbi->execute("drop table $table1"); |
|
477 |
+eval { $dbi->execute("drop table $table1") }; |
|
467 | 478 |
$dbi->execute($create_table1); |
468 | 479 |
$dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2}, append => ' '); |
469 | 480 |
$rows = $dbi->select(table => $table1)->all; |