Showing 4 changed files with 55 additions and 171 deletions
+15 -16
lib/DBIx/Custom.pm
... ...
@@ -10,7 +10,6 @@ use DBI;
10 10
 use DBIx::Custom::Result;
11 11
 use DBIx::Custom::SQLTemplate;
12 12
 use DBIx::Custom::Query;
13
-use DBIx::Custom::KeyInfo;
14 13
 
15 14
 __PACKAGE__->attr('dbh');
16 15
 
... ...
@@ -213,9 +212,6 @@ sub query{
213 212
     my ($self, $query, $params, $args)  = @_;
214 213
     $params ||= {};
215 214
     
216
-    # Filter
217
-    my $filter = $args->{filter} || {};
218
-    
219 215
     # First argument is SQL template
220 216
     unless (ref $query eq 'DBIx::Custom::Query') {
221 217
         my $template;
... ...
@@ -228,6 +224,9 @@ sub query{
228 224
         $query = $self->create_query($template);
229 225
     }
230 226
 
227
+    # Filter
228
+    my $filter = $args->{filter} || $query->filter || {};
229
+
231 230
     # Create bind value
232 231
     my $bind_values = $self->_build_bind_values($query, $params, $filter);
233 232
     
... ...
@@ -267,8 +266,9 @@ sub query{
267 266
 sub _build_bind_values {
268 267
     my ($self, $query, $params, $filter) = @_;
269 268
     my $key_infos      = $query->key_infos;
270
-    my $default_filter = $self->default_query_filter;
269
+    my $default_filter = $self->default_query_filter || '';
271 270
     my $filters        = $self->filters;
271
+    $filter            ||= {};
272 272
     
273 273
     # binding values
274 274
     my @bind_values;
... ...
@@ -282,11 +282,11 @@ sub _build_bind_values {
282 282
         my $value = defined $pos ? $params->{$column}->[$pos] : $params->{$column};
283 283
         
284 284
         # Filter
285
-        $filter = $filters->{$filter} || $filters->{$default_filter};
285
+        my $fname = $filter->{$column} || $default_filter || '';
286 286
         
287
-        push @bind_values, 
288
-             $filter ? $filter->($value)
289
-                     : $value;
287
+        push @bind_values, $filters->{$fname}
288
+                         ? $filters->{$fname}->($value)
289
+                         : $value;
290 290
     }
291 291
     
292 292
     return \@bind_values;
... ...
@@ -453,16 +453,15 @@ sub update {
453 453
     
454 454
     # Where clause
455 455
     my $where_clause = '';
456
+    my $new_where_params = {};
457
+    
456 458
     if (@where_keys) {
457 459
         $where_clause = 'where ';
458 460
         foreach my $where_key (@where_keys) {
459
-            my $key_info = DBIx::Custom::KeyInfo->new($where_key);
460
-            
461
-            my $table_new = $key_info->table || $table;
462
-            my $column = $table_new . '.' . $key_info->column
463
-                         . '#@where';
461
+            $new_where_params->{"$where_key@where"}
462
+              = $where_params->{$where_key};
464 463
 
465
-            $where_clause .= "{= $column} and ";
464
+            $where_clause .= "{= $where_key@where} and ";
466 465
         }
467 466
         $where_clause =~ s/ and $//;
468 467
     }
... ...
@@ -472,7 +471,7 @@ sub update {
472 471
     $template .= " $append_statement" if $append_statement;
473 472
     
474 473
     # Rearrange parammeters
475
-    my $params = {%$update_params, '@where' => $where_params};
474
+    my $params = {%$update_params, %$new_where_params};
476 475
     
477 476
     # Execute query
478 477
     my $ret_val = $self->query($template, $params, {filter => $filter});
+7 -7
lib/DBIx/Custom/Result.pm
... ...
@@ -14,7 +14,7 @@ sub fetch {
14 14
     
15 15
     my $sth            = $self->sth;
16 16
     my $filters        = $self->filters || {};
17
-    my $default_filter = $self->default_filter;
17
+    my $default_filter = $self->default_filter || '';
18 18
     my $filter         = $self->filter || {};
19 19
     
20 20
     # Fetch
... ...
@@ -28,7 +28,7 @@ sub fetch {
28 28
     
29 29
     # Filter
30 30
     for (my $i = 0; $i < @$columns; $i++) {
31
-        my $fname  = $filter->{$columns->[$i]} || $filters->{$default_filter};
31
+        my $fname  = $filter->{$columns->[$i]} || $filters->{$default_filter} || '';
32 32
         my $filter = $filters->{$fname};
33 33
         $row->[$i] = $filter->($row->[$i]) if $filter;
34 34
     }
... ...
@@ -41,7 +41,7 @@ sub fetch_hash {
41 41
 
42 42
     my $sth            = $self->sth;
43 43
     my $filters        = $self->filters || {};
44
-    my $default_filter = $self->default_filter;
44
+    my $default_filter = $self->default_filter || '';
45 45
     my $filter         = $self->filter || {};
46 46
     
47 47
     # Fetch
... ...
@@ -56,11 +56,11 @@ sub fetch_hash {
56 56
     # Filter
57 57
     my $row_hash = {};
58 58
     for (my $i = 0; $i < @$columns; $i++) {
59
-        my $fname  = $filter->{$columns->[$i]} || $filters->{$default_filter};
59
+        my $fname  = $filter->{$columns->[$i]} || $filters->{$default_filter} || '';
60 60
         my $filter = $filters->{$fname};
61 61
         $row_hash->{$columns->[$i]} = $filter
62
-                                    ? $filter->($columns->[$i])
63
-                                    : $columns->[$i];
62
+                                    ? $filter->($row->[$i])
63
+                                    : $row->[$i];
64 64
     }
65 65
     
66 66
     return wantarray ? %$row_hash : $row_hash;
... ...
@@ -204,7 +204,7 @@ Statement handle
204 204
 
205 205
 Filter excuted when data is fetched
206 206
 
207
-    $result         = $result->default_filter($sth);
207
+    $result         = $result->default_filter($default_filter);
208 208
     $default_filter = $result->default_filter;
209 209
 
210 210
 =head2 filter
+4 -1
lib/DBIx/Custom/SQLTemplate.pm
... ...
@@ -233,7 +233,10 @@ sub expand_basic_tag {
233 233
     # Key is not exist
234 234
     croak("You must be pass key as argument to tag '{$tag_name }'")
235 235
       unless $column;
236
-
236
+    
237
+    # delete ID
238
+    
239
+    
237 240
     # Expanded tag
238 241
     my $expand = $tag_name eq '?'
239 242
                ? '?'
+29 -147
t/dbix-custom-core-sqlite.t
... ...
@@ -177,88 +177,37 @@ test 'Direct query';
177 177
 $dbi->do($DROP_TABLE->{0});
178 178
 $dbi->do($CREATE_TABLE->{0});
179 179
 $insert_tmpl = "insert into table1 {insert key1 key2}";
180
-$dbi->query($insert_tmpl, {key1 => 1, key2 => 2}, sub {
181
-    my $query = shift;
182
-    $query->query_filter(sub {
183
-        my ($value, $table, $column, $dbi) = @_;
184
-        if ($column eq 'key2' && $dbi->isa('DBIx::Custom')) {
185
-            return $value + 1;
186
-        }
187
-        return $value;
188
-    });
189
-});
190
-$result = $dbi->query(['table1', $SELECT_TMPLS->{0}]);
180
+$dbi->query($insert_tmpl, {key1 => 1, key2 => 2});
181
+$result = $dbi->query($SELECT_TMPLS->{0});
191 182
 $rows = $result->fetch_hash_all;
192
-is_deeply($rows, [{key1 => 1, key2 => 3}], $test);
183
+is_deeply($rows, [{key1 => 1, key2 => 2}], $test);
193 184
 
194 185
 test 'Filter basic';
195 186
 $dbi->do($DROP_TABLE->{0});
196 187
 $dbi->do($CREATE_TABLE->{0});
188
+$dbi->resist_filter(twice       => sub { $_[0] * 2}, 
189
+                    three_times => sub { $_[0] * 3});
197 190
 
198 191
 $insert_tmpl  = "insert into table1 {insert key1 key2};";
199 192
 $insert_query = $dbi->create_query($insert_tmpl);
200
-$insert_query->query_filter(sub {
201
-    my ($value, $table, $column, $dbi) = @_;
202
-    
203
-    if ($table eq '' && $column eq 'key1')
204
-    {
205
-        return $value * 2;
206
-    }
207
-    return $value;
208
-});
193
+$insert_query->filter({key1 => 'twice'});
209 194
 $dbi->query($insert_query, {key1 => 1, key2 => 2});
210
-$select_query = $dbi->create_query($SELECT_TMPLS->{0});
211
-$select_query->fetch_filter(sub {
212
-    my ($value, $key, $dbi, $infos) = @_;
213
-    my ($type, $sth, $i) = @{$infos}{qw/type sth index/};
214
-    
215
-    if ($key eq 'key2' && $type =~ /char/ && ref $sth eq 'DBI::st' 
216
-        && $i == 1 && $dbi->isa('DBIx::Custom'))
217
-    {
218
-        return $value * 3;
219
-    }
220
-    return $value;
221
-});
222
-$result = $dbi->query($select_query);
223
-$rows = $result->fetch_hash_all;
224
-is_deeply($rows, [{key1 => 2, key2 => 6}], "$test : query_filter fetch_filter");
225
-
195
+$result = $dbi->query($SELECT_TMPLS->{0});
196
+$rows = $result->filter({key2 => 'three_times'})->fetch_hash_all;
197
+is_deeply($rows, [{key1 => 2, key2 => 6}], "$test : filter fetch_filter");
226 198
 $dbi->do($DROP_TABLE->{0});
227
-$dbi->do($CREATE_TABLE->{0});
228
-$insert_tmpl  = "insert into table1 {insert table1.key1 table1.key2}";
229
-$insert_query = $dbi->create_query($insert_tmpl);
230
-$insert_query->query_filter(sub {
231
-    my ($value, $table, $column, $dbi) = @_;
232
-    
233
-    if ($table eq 'table1' && $column eq 'key1') {
234
-        return $value * 3;
235
-    }
236
-    return $value;
237
-});
238
-$dbi->query($insert_query, {key1 => 1, key2 => 2});
239
-$select_query = $dbi->create_query($SELECT_TMPLS->{0});
240
-$result       = $dbi->query($select_query);
241
-$rows = $result->fetch_hash_all;
242
-is_deeply($rows, [{key1 => 3, key2 => 2}], "$test : insert with id");
243 199
 
244 200
 test 'Filter in';
201
+$dbi->do($CREATE_TABLE->{0});
245 202
 $insert_tmpl  = "insert into table1 {insert key1 key2};";
246 203
 $insert_query = $dbi->create_query($insert_tmpl);
247 204
 $dbi->query($insert_query, {key1 => 2, key2 => 4});
248 205
 $select_tmpl = "select * from table1 where {in table1.key1 2} and {in table1.key2 2}";
249 206
 $select_query = $dbi->create_query($select_tmpl);
250
-$select_query->query_filter(sub {
251
-    my ($value, $table, $column, $dbi) = @_;
252
-    
253
-    if ($table eq 'table1' && $column eq 'key1') {
254
-        return $value * 2;
255
-    }
256
-    return $value;
257
-});
258
-$result = $dbi->query($select_query, {key1 => [1,5], key2 => [2,4]});
207
+$select_query->filter({'table1.key1' => 'twice'});
208
+$result = $dbi->query($select_query, {'table1.key1' => [1,5], 'table1.key2' => [2,4]});
259 209
 $rows = $result->fetch_hash_all;
260
-is_deeply($rows, [{key1 => 2, key2 => 4}], "$test : query_filter");
261
-
210
+is_deeply($rows, [{key1 => 2, key2 => 4}], "$test : filter");
262 211
 
263 212
 test 'DBIx::Custom::SQLTemplate basic tag';
264 213
 $dbi->do($DROP_TABLE->{0});
... ...
@@ -273,37 +222,12 @@ $result = $dbi->query($query, {key1 => 1, key2 => 3, key3 => 4, key4 => 3, key5
273 222
 $rows = $result->fetch_hash_all;
274 223
 is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}], "$test : basic tag1");
275 224
 
276
-$tmpl = "select * from table1 where {= table1.key1#id} and {<> table1.key2#id} and {< table1.key3#id} and {> table1.key4#id} and {>= table1.key5#id};";
277
-$query = $dbi->create_query($tmpl);
278
-$result = $dbi->query($query, {id => {key1 => 1, key2 => 3, key3 => 4, key4 => 3, key5 => 5}});
279
-$rows = $result->fetch_hash_all;
280
-is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}], "$test : basic tag1 with id");
281
-
282
-$tmpl = "select * from table1 where {= table1.key1} and {<> table1.key2} and {< table1.key3} and {> table1.key4} and {>= table1.key5};";
283
-$query = $dbi->create_query($tmpl);
284
-$result = $dbi->query($query, {key1 => 1, key2 => 3, key3 => 4, key4 => 3, key5 => 5});
285
-$rows = $result->fetch_hash_all;
286
-is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}], "$test : basic tag1 with table dot");
287
-
288 225
 $tmpl = "select * from table1 where {<= key1} and {like key2};";
289 226
 $query = $dbi->create_query($tmpl);
290 227
 $result = $dbi->query($query, {key1 => 1, key2 => '%2%'});
291 228
 $rows = $result->fetch_hash_all;
292 229
 is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}], "$test : basic tag2");
293 230
 
294
-$tmpl = "select * from table1 where {<= table1.key1#id} and {like table1.key2#id};";
295
-$query = $dbi->create_query($tmpl);
296
-$result = $dbi->query($query, {id => {key1 => 1, key2 => '%2%'}});
297
-$rows = $result->fetch_hash_all;
298
-is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}], "$test : basic tag2 with table");
299
-
300
-$tmpl = "select * from table1 where {<= table1.key1} and {like table1.key2};";
301
-$query = $dbi->create_query($tmpl);
302
-$result = $dbi->query($query, {'key1' => 1, 'key2' => '%2%'});
303
-$rows = $result->fetch_hash_all;
304
-is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}], "$test : basic tag2 with table dot");
305
-
306
-
307 231
 test 'DIB::Custom::SQLTemplate in tag';
308 232
 $dbi->do($DROP_TABLE->{0});
309 233
 $dbi->do($CREATE_TABLE->{1});
... ...
@@ -317,19 +241,6 @@ $result = $dbi->query($query, {key1 => [9, 1]});
317 241
 $rows = $result->fetch_hash_all;
318 242
 is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}], "$test : basic");
319 243
 
320
-$tmpl = "select * from table1 where {in table1.key1#id 2};";
321
-$query = $dbi->create_query($tmpl);
322
-$result = $dbi->query($query, {id => {key1 => [9, 1]}});
323
-$rows = $result->fetch_hash_all;
324
-is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}], "$test : with table");
325
-
326
-$tmpl = "select * from table1 where {in table1.key1#id 2};";
327
-$query = $dbi->create_query($tmpl);
328
-$result = $dbi->query($query, {id => {'key1' => [9, 1]}});
329
-$rows = $result->fetch_hash_all;
330
-is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}], "$test : with table dot");
331
-
332
-
333 244
 test 'DBIx::Custom::SQLTemplate insert tag';
334 245
 $dbi->do("delete from table1");
335 246
 $insert_tmpl = 'insert into table1 {insert key1 key2 key3 key4 key5}';
... ...
@@ -339,13 +250,6 @@ $result = $dbi->query($SELECT_TMPLS->{0});
339 250
 $rows = $result->fetch_hash_all;
340 251
 is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}], "$test : basic");
341 252
 
342
-$dbi->do("delete from table1");
343
-$insert_tmpl = 'insert into table1 {insert table1.key1#id table1.key2#id table1.key3#id table1.key4#id table1.key5#id}';
344
-$dbi->query($insert_tmpl, {id => {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}});
345
-$result = $dbi->query($SELECT_TMPLS->{0});
346
-$rows = $result->fetch_hash_all;
347
-is_deeply($rows, [{key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5}], "$test : with table name");
348
-
349 253
 test 'DBIx::Custom::SQLTemplate update tag';
350 254
 $dbi->do("delete from table1");
351 255
 $insert_tmpl = "insert into table1 {insert key1 key2 key3 key4 key5}";
... ...
@@ -360,13 +264,6 @@ $rows = $result->fetch_hash_all;
360 264
 is_deeply($rows, [{key1 => 1, key2 => 1, key3 => 1, key4 => 1, key5 => 5},
361 265
                   {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10}], "$test : basic");
362 266
 
363
-$update_tmpl = 'update table1 {update table1.key1#id table1.key2#id table1.key3#id table1.key4#id} where {= table1.key5#id}';
364
-$dbi->query($update_tmpl, {id => {key1 => 3, key2 => 3, key3 => 3, key4 => 3, key5 => 5}});
365
-$result = $dbi->query($SELECT_TMPLS->{0});
366
-$rows = $result->fetch_hash_all;
367
-is_deeply($rows, [{key1 => 3, key2 => 3, key3 => 3, key4 => 3, key5 => 5},
368
-                  {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10}], "$test : with table name");
369
-
370 267
 test 'transaction';
371 268
 $dbi->do($DROP_TABLE->{0});
372 269
 $dbi->do($CREATE_TABLE->{0});
... ...
@@ -438,38 +335,23 @@ $rows   = $result->fetch_hash_all;
438 335
 is_deeply($rows, [{key1 => 1, key2 => 2}, {key1 => 3, key2 => 4}], "$test : basic");
439 336
 
440 337
 $dbi->do('delete from table1');
441
-$dbi->insert('table1', {key1 => 1, key2 => 2}, 
442
-    {
443
-        query_edit_cb => sub {
444
-            my $query = shift;
445
-            $query->query_filter(sub {
446
-                my ($value, $table, $column, $dbi) = @_;
447
-                if ($column eq 'key1') {
448
-                    return $value * 3;
449
-                }
450
-                return $value;
451
-            });
452
-        }
453
-    }
454
-);
338
+$dbi->resist_filter(three_times => sub { $_[0] * 3});
339
+$dbi->insert('table1', {key1 => 1, key2 => 2}, {filter => {key1 => 'three_times'}});
455 340
 $result = $dbi->query($SELECT_TMPLS->{0});
456 341
 $rows   = $result->fetch_hash_all;
457 342
 is_deeply($rows, [{key1 => 3, key2 => 2}], "$test : edit_query_callback");
458 343
 
459
-$dbi->insert('table1', {key1 => 1, key2 => 2}, {append => '   ', query_edit_cb => sub {
460
-    my $query = shift;
461
-    like($query->sql, qr/insert into table1 \(.+\) values \(\?, \?\)    ;/, 
462
-        "$test: append statement");
463
-}});
344
+$dbi->do($DROP_TABLE->{0});
345
+$dbi->do($CREATE_TABLE->{0});
346
+$dbi->insert('table1', {key1 => 1, key2 => 2}, {append => '   '});
347
+$rows = $dbi->select('table1')->fetch_hash_all;
348
+is_deeply($rows, [{key1 => 1, key2 => 2}], 'insert append');
349
+
464 350
 
465 351
 test 'insert error';
466 352
 eval{$dbi->insert('table1')};
467 353
 like($@, qr/Key-value pairs for insert must be specified to 'insert' second argument/, "$test : insert key-value not specifed");
468 354
 
469
-eval{$dbi->insert('table1', {key1 => 1, key2 => 2}, {append => '', query_edit_cb => 'aaa'})};
470
-like($@, qr/Query edit callback must be code reference/, "$test : query edit callback not code ref");
471
-
472
-
473 355
 test 'update';
474 356
 $dbi = DBIx::Custom->new($NEW_ARGS->{0});
475 357
 $dbi->do($CREATE_TABLE->{1});
... ...
@@ -497,7 +379,7 @@ $dbi->insert('table1', {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5});
497 379
 $dbi->insert('table1', {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10});
498 380
 $dbi->update('table1', {key2 => 11}, {where => {key1 => 1}, query_edit_cb => sub {
499 381
     my $query = shift;
500
-    $query->query_filter(sub {
382
+    $query->filter(sub {
501 383
         my ($value, $table, $column, $dbi) = @_;
502 384
         if ($column eq 'key2') {
503 385
             return $value * 2;
... ...
@@ -541,7 +423,7 @@ $dbi->insert('table1', {key1 => 1, key2 => 2, key3 => 3, key4 => 4, key5 => 5});
541 423
 $dbi->insert('table1', {key1 => 6, key2 => 7, key3 => 8, key4 => 9, key5 => 10});
542 424
 $dbi->update_all('table1', {key2 => 10}, {query_edit_cb => sub {
543 425
     my $query = shift;
544
-    $query->query_filter(sub {
426
+    $query->filter(sub {
545 427
         my ($value, $table, $column, $dbi) = @_;
546 428
         return $value * 2;
547 429
     })
... ...
@@ -568,7 +450,7 @@ $dbi->insert('table1', {key1 => 1, key2 => 2});
568 450
 $dbi->insert('table1', {key1 => 3, key2 => 4});
569 451
 $dbi->delete('table1', {where => {key2 => 1}, query_edit_cb => sub {
570 452
     my $query = shift;
571
-    $query->query_filter(sub {
453
+    $query->filter(sub {
572 454
         my ($value, $table, $column, $dbi) = @_;
573 455
         return $value * 2;
574 456
     });
... ...
@@ -638,7 +520,7 @@ is_deeply($rows, [{key1 => 3, key2 => 4}], "$test : append statement");
638 520
 
639 521
 $rows = $dbi->select('table1', {where => {key1 => 2}, query_edit_cb =>sub {
640 522
     my $query = shift;
641
-    $query->query_filter(sub {
523
+    $query->filter(sub {
642 524
         my ($value, $table, $column, $dbi) = @_;
643 525
         if ($column eq 'key1') {
644 526
             return $value - 1;
... ...
@@ -697,11 +579,11 @@ is_deeply(DBIx::Custom->_query_caches->{$tmpls[2]}{key_infos}, $queries[2]->key_
697 579
 is_deeply(DBIx::Custom->_query_cache_keys, [@tmpls[1, 2]], "$test : cache key third");
698 580
 
699 581
 $query = $dbi->create_query($tmpls[0]);
700
-$query->query_filter('aaa');
582
+$query->filter('aaa');
701 583
 $query = $dbi->create_query($tmpls[0]);
702
-ok(!$query->query_filter, "$test : only cached sql and key_infos");
703
-$query->query_filter('bbb');
584
+ok(!$query->filter, "$test : only cached sql and key_infos");
585
+$query->filter('bbb');
704 586
 $query = $dbi->create_query($tmpls[0]);
705
-ok(!$query->query_filter, "$test : only cached sql and key_infos");
587
+ok(!$query->filter, "$test : only cached sql and key_infos");
706 588
 
707 589