Showing 2 changed files with 71 additions and 10 deletions
+2 -1
Changes
... ...
@@ -1,7 +1,8 @@
1
+0.1602
2
+  added cache_method attribute
1 3
 0.1601
2 4
   added cache attribute
3 5
   select, insert, update, update_all, delete, delete_all, execute only receive hash argument(not backword compatible)
4
-  
5 6
 0.1503
6 7
   removed reconnect method
7 8
   removed connected method
+69 -9
lib/DBIx/Custom.pm
... ...
@@ -25,7 +25,22 @@ __PACKAGE__->register_filter(
25 25
 
26 26
 __PACKAGE__->attr(result_class => 'DBIx::Custom::Result');
27 27
 __PACKAGE__->attr(sql_template => sub { DBIx::Custom::SQLTemplate->new });
28
+
28 29
 __PACKAGE__->attr(cache => 1);
30
+__PACKAGE__->attr(cache_method => sub {
31
+    sub {
32
+        my $self = shift;
33
+        
34
+        $self->{_cached} ||= {};
35
+        
36
+        if (@_ > 1) {
37
+            $self->{_cached}{$_[0]} = $_[1] 
38
+        }
39
+        else {
40
+            return $self->{_cached}{$_[0]}
41
+        }
42
+    }
43
+});
29 44
 
30 45
 sub connect {
31 46
     my $proto = shift;
... ...
@@ -309,20 +324,30 @@ sub create_query {
309 324
     # Create query from SQL template
310 325
     my $sql_template = $self->sql_template;
311 326
     
312
-    # Get cached query
313
-    my $cached = $self->{_cached}->{$template};
327
+    my $cache = $self->cache;
314 328
     
315 329
     # Create query
316 330
     my $query;
317
-    if ($cached) {
318
-        $query = DBIx::Custom::Query->new($cached);
331
+    if ($cache) {
332
+        
333
+        # Cached query
334
+        my $q = $self->cache_method->($self, $template);
335
+        
336
+        # Create query
337
+        $query = DBIx::Custom::Query->new($q) if $q;
319 338
     }
320
-    else {
339
+    
340
+    unless ($query) {
341
+        
342
+        # Create query
321 343
         $query = eval{$sql_template->create_query($template)};
322 344
         croak($@) if $@;
323 345
         
324
-        $self->{_cached}->{$template} = {sql => $query->sql, columns => $query->columns}
325
-          if $self->cache && ! $self->{_cached}->{$template};
346
+        # Cache query
347
+        $self->cache_method->($self, $template,
348
+                             {sql     => $query->sql, 
349
+                              columns => $query->columns})
350
+          if $cache;
326 351
     }
327 352
     
328 353
     # Prepare statement handle
... ...
@@ -485,11 +510,11 @@ DBIx::Custom - DBI with hash parameter binding and filtering system
485 510
 
486 511
 =head1 VERSION
487 512
 
488
-Version 0.1503
513
+Version 0.1602
489 514
 
490 515
 =cut
491 516
 
492
-our $VERSION = '0.1503';
517
+our $VERSION = '0.1602';
493 518
 $VERSION = eval $VERSION;
494 519
 
495 520
 =head1 STABILITY
... ...
@@ -920,6 +945,39 @@ This is equal to
920 945
 
921 946
     $dbi->dbh->rollback;
922 947
 
948
+=head2 cache
949
+
950
+Cache the result of parsing SQL template.
951
+
952
+    $dbi   = $dbi->cache(1);
953
+    $cache = $dbi->cache;
954
+
955
+Default to 1.
956
+
957
+=head2 cache_method
958
+
959
+Method for cache.
960
+
961
+    $dbi          = $dbi->cache_method(sub { ... });
962
+    $cache_method = $dbi->cache_method
963
+
964
+Example:
965
+    
966
+    $dbi->cache_method(
967
+        sub {
968
+            my $self = shift;
969
+            
970
+            $self->{_cached} ||= {};
971
+            
972
+            if (@_ > 1) {
973
+                $self->{_cached}{$_[0]} = $_[1] 
974
+            }
975
+            else {
976
+                return $self->{_cached}{$_[0]}
977
+            }
978
+        }
979
+    );
980
+
923 981
 =head1 AUTHOR
924 982
 
925 983
 Yuki Kimoto, C<< <kimoto.yuki at gmail.com> >>
... ...
@@ -932,3 +990,5 @@ This program is free software; you can redistribute it and/or modify it
932 990
 under the same terms as Perl itself.
933 991
 
934 992
 =cut
993
+
994
+