... | ... |
@@ -1,4 +1,5 @@ |
1 | 1 |
0.1601 |
2 |
+ added cache attribute |
|
2 | 3 |
select, insert, update, update_all, delete, delete_all, execute only receive hash argument(not backword compatible) |
3 | 4 |
|
4 | 5 |
0.1503 |
... | ... |
@@ -25,6 +25,7 @@ __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 |
+__PACKAGE__->attr(cache => 1); |
|
28 | 29 |
|
29 | 30 |
sub connect { |
30 | 31 |
my $proto = shift; |
... | ... |
@@ -309,22 +310,19 @@ sub create_query { |
309 | 310 |
my $sql_template = $self->sql_template; |
310 | 311 |
|
311 | 312 |
# Get cached query |
312 |
- my $cache = $self->{_cache}->{$template}; |
|
313 |
+ my $cached = $self->{_cached}->{$template}; |
|
313 | 314 |
|
314 | 315 |
# Create query |
315 | 316 |
my $query; |
316 |
- if ($cache) { |
|
317 |
- $query = DBIx::Custom::Query->new( |
|
318 |
- sql => $cache->sql, |
|
319 |
- columns => $cache->columns |
|
320 |
- ); |
|
317 |
+ if ($cached) { |
|
318 |
+ $query = DBIx::Custom::Query->new($cached); |
|
321 | 319 |
} |
322 | 320 |
else { |
323 | 321 |
$query = eval{$sql_template->create_query($template)}; |
324 | 322 |
croak($@) if $@; |
325 | 323 |
|
326 |
- $self->{_cache}->{$template} = $query |
|
327 |
- unless $self->{_cache}->{$template}; |
|
324 |
+ $self->{_cached}->{$template} = {sql => $query->sql, columns => $query->columns} |
|
325 |
+ if $self->cache && ! $self->{_cached}->{$template}; |
|
328 | 326 |
} |
329 | 327 |
|
330 | 328 |
# Prepare statement handle |
... | ... |
@@ -414,3 +414,19 @@ $dbi->rollback; |
414 | 414 |
|
415 | 415 |
$result = $dbi->select(table => 'table1'); |
416 | 416 |
ok(! $result->fetch_first, "$test: rollback"); |
417 |
+ |
|
418 |
+test 'cache'; |
|
419 |
+$dbi = DBIx::Custom->connect($NEW_ARGS->{0}); |
|
420 |
+$dbi->execute($CREATE_TABLE->{0}); |
|
421 |
+$tmpl = 'select * from table1 where {= key1} and {= key2};'; |
|
422 |
+$dbi->create_query($tmpl); |
|
423 |
+is_deeply($dbi->{_cached}->{$tmpl}, |
|
424 |
+ {sql => "select * from table1 where key1 = ? and key2 = ?;", columns => ['key1', 'key2']}, "$test : cache"); |
|
425 |
+ |
|
426 |
+$dbi = DBIx::Custom->connect($NEW_ARGS->{0}); |
|
427 |
+$dbi->execute($CREATE_TABLE->{0}); |
|
428 |
+$dbi->{_cached} = {}; |
|
429 |
+$dbi->cache(0); |
|
430 |
+$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}); |
|
431 |
+is(scalar keys %{$dbi->{_cached}}, 0, 'not cache'); |
|
432 |
+ |