Showing 3 changed files with 23 additions and 20 deletions
+1 -1
Changes
... ...
@@ -1,5 +1,5 @@
1 1
 0.1744
2
-    - added EXPERIMENTAL reuse_sth option to execute method
2
+    - added EXPERIMENTAL reuse_query 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.
+18 -15
lib/DBIx/Custom.pm
... ...
@@ -363,10 +363,15 @@ sub execute {
363 363
     $sql .= $opt{append} if defined $opt{append} && !ref $sql;
364 364
     
365 365
     # Query
366
-    my $query = ref $sql ? $sql
367
-      : $self->_create_query($sql,$opt{after_build_sql} || $opt{sqlfilter},
368
-          $opt{reuse_sth});
369
-    
366
+    my $query;
367
+    if (ref $sql) { $query = $sql }
368
+    else {
369
+        $query = $opt{reuse}->{$sql} if $opt{reuse};
370
+        $query = $self->_create_query($sql,$opt{after_build_sql} || $opt{sqlfilter})
371
+          unless $query;
372
+        $opt{reuse}->{$sql} = $query if $opt{reuse};
373
+    }
374
+        
370 375
     # Save query
371 376
     $self->last_sql($query->sql);
372 377
 
... ...
@@ -1096,7 +1101,7 @@ sub where { DBIx::Custom::Where->new(dbi => shift, @_) }
1096 1101
 
1097 1102
 sub _create_query {
1098 1103
     
1099
-    my ($self, $source, $after_build_sql, $reuse_sth) = @_;
1104
+    my ($self, $source, $after_build_sql) = @_;
1100 1105
     
1101 1106
     # Cache
1102 1107
     my $cache = $self->cache;
... ...
@@ -1153,9 +1158,7 @@ sub _create_query {
1153 1158
     
1154 1159
     # Prepare statement handle
1155 1160
     my $sth;
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;
1161
+    eval { $sth = $self->dbh->prepare($query->{sql}) };
1159 1162
     
1160 1163
     if ($@) {
1161 1164
         $self->_croak($@, qq{. Following SQL is executed.\n}
... ...
@@ -2581,17 +2584,17 @@ Table alias. Key is real table name, value is alias table name.
2581 2584
 If you set C<table_alias>, you can enable C<into1> and C<into2> type rule
2582 2585
 on alias table name.
2583 2586
 
2584
-=item C<reuse_sth EXPERIMENTAL>
2587
+=item C<reuse EXPERIMENTAL>
2585 2588
     
2586
-    reuse_sth => $has_ref
2589
+    reuse_query => $has_ref
2587 2590
 
2588
-Reuse statament handle if the hash reference variable is set.
2591
+Reuse query object if the hash reference variable is set.
2589 2592
     
2590
-    my $sth = {};
2591
-    $dbi->execute($sql, $param, sth => $sth);
2593
+    my $queries = {};
2594
+    $dbi->execute($sql, $param, reuse => $queries);
2592 2595
 
2593
-This will improved performance when same sql is executed repeatedly
2594
-because generally creating statement handle is slow.
2596
+This will improved performance when you want to execute same query repeatedly
2597
+because generally creating query object is slow.
2595 2598
 
2596 2599
 =item C<type_rule_off>
2597 2600
 
+4 -4
t/common.t
... ...
@@ -80,7 +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
+my $reuse;
84 84
 
85 85
 require MyDBI1;
86 86
 {
... ...
@@ -238,12 +238,12 @@ require MyDBI1;
238 238
     }
239 239
 }
240 240
 
241
-test 'execute reuse_sth option';
241
+test 'execute reuse option';
242 242
 eval { $dbi->execute("drop table $table1") };
243 243
 $dbi->execute($create_table1);
244
-$reuse_sth = {};
244
+$reuse = {};
245 245
 for my $i (1 .. 2) {
246
-  $dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2}, reuse_sth => $reuse_sth);
246
+  $dbi->insert(table => $table1, param => {$key1 => 1, $key2 => 2}, reuse => $reuse);
247 247
 }
248 248
 $rows = $dbi->select(table => $table1)->all;
249 249
 is_deeply($rows, [{$key1 => 1, $key2 => 2}, {$key1 => 1, $key2 => 2}]);