Showing 2 changed files with 42 additions and 142 deletions
+27 -45
lib/DBIx/Custom.pm
... ...
@@ -61,7 +61,7 @@ sub AUTOLOAD {
61 61
     return $self->$helper(@_);
62 62
 }
63 63
 
64
-sub auto_filter {
64
+sub apply_filter {
65 65
     my $self = shift;
66 66
     
67 67
     $self->{auto_filter} ||= {};
... ...
@@ -238,7 +238,7 @@ sub create_query {
238 238
 }
239 239
 
240 240
 our %VALID_DELETE_ARGS
241
-  = map { $_ => 1 } qw/auto_filter_table table where append filter allow_delete_all/;
241
+  = map { $_ => 1 } qw/table where append filter allow_delete_all/;
242 242
 
243 243
 sub delete {
244 244
     my ($self, %args) = @_;
... ...
@@ -256,11 +256,6 @@ sub delete {
256 256
     my $filter           = $args{filter};
257 257
     my $allow_delete_all = $args{allow_delete_all};
258 258
 
259
-    my $auto_filter_table = exists $args{auto_filter_table}
260
-                          ? $args{auto_filter_table}
261
-                          : [$table];
262
-    $auto_filter_table ||= [];    
263
-
264 259
     # Where keys
265 260
     my @where_keys = keys %$where;
266 261
     
... ...
@@ -284,7 +279,7 @@ sub delete {
284 279
     # Execute query
285 280
     my $ret_val = $self->execute(
286 281
         $source, param  => $where, filter => $filter,
287
-        auto_filter_table => $auto_filter_table);
282
+        table => $table);
288 283
     
289 284
     return $ret_val;
290 285
 }
... ...
@@ -293,7 +288,7 @@ sub delete_all { shift->delete(allow_delete_all => 1, @_) }
293 288
 
294 289
 sub DESTROY { }
295 290
 
296
-our %VALID_EXECUTE_ARGS = map { $_ => 1 } qw/param filter auto_filter_table/;
291
+our %VALID_EXECUTE_ARGS = map { $_ => 1 } qw/param filter table/;
297 292
 
298 293
 sub execute{
299 294
     my ($self, $query, %args)  = @_;
... ...
@@ -312,8 +307,10 @@ sub execute{
312 307
     
313 308
     # Auto filter
314 309
     my $auto_filter = {};
315
-    my $auto_filter_tables = $args{auto_filter_table} || [];
316
-    foreach my $table (@$auto_filter_tables) {
310
+    my $tables = $args{table} || [];
311
+    $tables = [$tables]
312
+      unless ref $tables eq 'ARRAY';
313
+    foreach my $table (@$tables) {
317 314
         $auto_filter = {
318 315
             %$auto_filter,
319 316
             %{$self->{auto_filter}{in}->{$table} || {}}
... ...
@@ -324,7 +321,10 @@ sub execute{
324 321
     my $filter = $args{filter} || $query->filter || {};
325 322
     foreach my $column (keys %$filter) {
326 323
         my $fname = $filter->{$column};
327
-        unless (ref $fname eq 'CODE') {
324
+        if (!defined $fname) {
325
+            $filter->{$column} = undef;
326
+        }
327
+        elsif (ref $fname ne 'CODE') {
328 328
           croak qq{"$fname" is not registered"}
329 329
             unless exists $self->filters->{$fname};
330 330
           
... ...
@@ -347,7 +347,7 @@ sub execute{
347 347
         
348 348
         # Auto in filter
349 349
         my $auto_in_filter = {};
350
-	      foreach my $table (@$auto_filter_tables) {
350
+	      foreach my $table (@$tables) {
351 351
 	          $auto_in_filter = {
352 352
 	              %$auto_filter,
353 353
 	              %{$self->{auto_filter}{in}{$table} || {}}
... ...
@@ -384,7 +384,7 @@ sub expand {
384 384
 }
385 385
 
386 386
 our %VALID_INSERT_ARGS = map { $_ => 1 } qw/table param append
387
-                                            filter auto_filter_table/;
387
+                                            filter/;
388 388
 sub insert {
389 389
     my ($self, %args) = @_;
390 390
 
... ...
@@ -400,11 +400,6 @@ sub insert {
400 400
     my $append = $args{append} || '';
401 401
     my $filter = $args{filter};
402 402
     
403
-    my $auto_filter_table = exists $args{auto_filter_table}
404
-                          ? $args{auto_filter_table}
405
-                          : [$table];
406
-    $auto_filter_table ||= [];
407
-    
408 403
     # Insert keys
409 404
     my @insert_keys = keys %$param;
410 405
     
... ...
@@ -418,7 +413,7 @@ sub insert {
418 413
         $source,
419 414
         param  => $param,
420 415
         filter => $filter,
421
-        auto_filter_table => $auto_filter_table
416
+        table => $table
422 417
     );
423 418
     
424 419
     return $ret_val;
... ...
@@ -467,7 +462,7 @@ sub register_filter {
467 462
 }
468 463
 
469 464
 our %VALID_SELECT_ARGS
470
-  = map { $_ => 1 } qw/auto_filter_table table column where append relation filter/;
465
+  = map { $_ => 1 } qw/table column where append relation filter/;
471 466
 
472 467
 sub select {
473 468
     my ($self, %args) = @_;
... ...
@@ -486,10 +481,6 @@ sub select {
486 481
     my $relation = $args{relation};
487 482
     my $append   = $args{append};
488 483
     my $filter   = $args{filter};
489
-
490
-    my $auto_filter_table = exists $args{auto_filter_table}
491
-                          ? $args{auto_filter_table}
492
-                          : $tables;
493 484
     
494 485
     # Source of SQL
495 486
     my $source = 'select ';
... ...
@@ -545,7 +536,7 @@ sub select {
545 536
     # Execute query
546 537
     my $result = $self->execute(
547 538
         $source, param  => $param, filter => $filter,
548
-        auto_filter_table => $auto_filter_table);    
539
+        table => $tables);    
549 540
     
550 541
     return $result;
551 542
 }
... ...
@@ -586,7 +577,7 @@ sub txn_scope {
586 577
 }
587 578
 
588 579
 our %VALID_UPDATE_ARGS
589
-  = map { $_ => 1 } qw/auto_filter_table table param
580
+  = map { $_ => 1 } qw/table param
590 581
                        where append filter allow_update_all/;
591 582
 
592 583
 sub update {
... ...
@@ -606,11 +597,6 @@ sub update {
606 597
     my $filter           = $args{filter};
607 598
     my $allow_update_all = $args{allow_update_all};
608 599
     
609
-    my $auto_filter_table = exists $args{auto_filter_table}
610
-                          ? $args{auto_filter_table}
611
-                          : [$table];
612
-    $auto_filter_table ||= [];
613
-    
614 600
     # Update keys
615 601
     my @update_keys = keys %$param;
616 602
     
... ...
@@ -656,7 +642,7 @@ sub update {
656 642
     # Execute query
657 643
     my $ret_val = $self->execute($source, param  => $param, 
658 644
                                  filter => $filter,
659
-                                 auto_filter_table => $auto_filter_table);
645
+                                 table => $table);
660 646
     
661 647
     return $ret_val;
662 648
 }
... ...
@@ -981,32 +967,28 @@ C<connect()> method use this value to connect the database.
981 967
 L<DBIx::Custom> inherits all methods from L<Object::Simple>
982 968
 and implements the following new ones.
983 969
 
984
-=head2 C<(experimental) auto_filter >
970
+=head2 C<(experimental) apply_filter >
985 971
 
986
-    $dbi->auto_filter(
972
+    $dbi->apply_filter(
987 973
         $table,
988
-        [$column1, $bind_filter1, $fetch_filter1],
989
-        [$column2, $bind_filter2, $fetch_filter2],
990
-        [...],
974
+        $column1 => {in => $infilter1, out => $outfilter1}
975
+        $column2 => {in => $infilter2, out => $outfilter2}
976
+        ...,
991 977
     );
992 978
 
993
-C<auto_filter> is automatically filter for columns of table.
979
+C<apply_filter> is automatically filter for columns of table.
994 980
 This have effect C<insert>, C<update>, C<delete>. C<select>
995 981
 and L<DBIx::Custom::Result> object. but this has'nt C<execute> method.
996 982
 
997
-If you want to have effect <execute< method, use C<auto_filter_table>
983
+If you want to have effect <execute< method, use C<table>
998 984
 arguments.
999 985
 
1000 986
     $result = $dbi->execute(
1001 987
         "select * from table1 where {= key1} and {= key2};",
1002 988
          param => {key1 => 1, key2 => 2},
1003
-         auto_filter_table => ['table1']
989
+         table => ['table1']
1004 990
     );
1005 991
     
1006
-B<Example:>
1007
-
1008
-    $dbi->auto_filter('book', 'sale_date', 'to_date', 'date_to');
1009
-
1010 992
 =head2 C<begin_work>
1011 993
 
1012 994
     $dbi->begin_work;
+15 -97
t/dbix-custom-core-sqlite.t
... ...
@@ -554,7 +554,7 @@ $dbi = DBIx::Custom->connect($NEW_ARGS->{0});
554 554
 $dbi->execute($CREATE_TABLE->{0});
555 555
 $dbi->register_filter(twice => sub { $_[0] * 2 });
556 556
 $dbi->register_filter(three_times => sub { $_[0] * 3});
557
-$dbi->auto_filter(
557
+$dbi->apply_filter(
558 558
     'table1', ['key1', 'twice', 'twice'], ['key2', 'three_times', 'three_times']);
559 559
 $dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
560 560
 $result = $dbi->execute($SELECT_SOURCES->{0});
... ...
@@ -564,32 +564,10 @@ is_deeply($row, {key1 => 2, key2 => 6}, "$test : insert");
564 564
 $dbi = DBIx::Custom->connect($NEW_ARGS->{0});
565 565
 $dbi->execute($CREATE_TABLE->{0});
566 566
 $dbi->register_filter(twice => sub { $_[0] * 2 });
567
-$dbi->register_filter(three_times => sub { $_[0] * 3});
568
-$dbi->auto_filter(
569
-    'table1', ['key1', 'twice', 'twice'], ['key2', 'three_times', 'three_times']);
570
-$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}, auto_filter_table => undef);
571
-$result = $dbi->execute($SELECT_SOURCES->{0});
572
-$row   = $result->fetch_hash_first;
573
-is_deeply($row, {key1 => 1, key2 => 2}, "$test : insert disabe auto_filter_table 1");
574
-
575
-$dbi = DBIx::Custom->connect($NEW_ARGS->{0});
576
-$dbi->execute($CREATE_TABLE->{0});
577
-$dbi->register_filter(twice => sub { $_[0] * 2 });
578
-$dbi->register_filter(three_times => sub { $_[0] * 3});
579
-$dbi->auto_filter(
580
-    'table1', ['key1', 'twice', 'twice'], ['key2', 'three_times', 'three_times']);
581
-$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}, auto_filter_table => []);
582
-$result = $dbi->execute($SELECT_SOURCES->{0});
583
-$row   = $result->fetch_hash_first;
584
-is_deeply($row, {key1 => 1, key2 => 2}, "$test : insert disabe auto_filter_table 2");
585
-
586
-$dbi = DBIx::Custom->connect($NEW_ARGS->{0});
587
-$dbi->execute($CREATE_TABLE->{0});
588
-$dbi->register_filter(twice => sub { $_[0] * 2 });
589
-$dbi->auto_filter(
567
+$dbi->apply_filter(
590 568
     'table1', ['key1', 'twice', 'twice']
591 569
 );
592
-$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}, auto_filter_table => undef);
570
+$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}, filter => {key1 => undef});
593 571
 $dbi->update(table => 'table1', param => {key1 => 2}, where => {key2 => 2});
594 572
 $result = $dbi->execute($SELECT_SOURCES->{0});
595 573
 $row   = $result->fetch_hash_first;
... ...
@@ -598,34 +576,10 @@ is_deeply($row, {key1 => 4, key2 => 2}, "$test : update");
598 576
 $dbi = DBIx::Custom->connect($NEW_ARGS->{0});
599 577
 $dbi->execute($CREATE_TABLE->{0});
600 578
 $dbi->register_filter(twice => sub { $_[0] * 2 });
601
-$dbi->auto_filter(
602
-    'table1', ['key1', 'twice', 'twice']
603
-);
604
-$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}, auto_filter_table => undef);
605
-$dbi->update(table => 'table1', param => {key1 => 2}, where => {key2 => 2}, auto_filter_table => []);
606
-$result = $dbi->execute($SELECT_SOURCES->{0});
607
-$row   = $result->fetch_hash_first;
608
-is_deeply($row, {key1 => 2, key2 => 2}, "$test : update : disable bind filter1");
609
-
610
-$dbi = DBIx::Custom->connect($NEW_ARGS->{0});
611
-$dbi->execute($CREATE_TABLE->{0});
612
-$dbi->register_filter(twice => sub { $_[0] * 2 });
613
-$dbi->auto_filter(
614
-    'table1', ['key1', 'twice', 'twice']
615
-);
616
-$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2}, auto_filter_table => undef);
617
-$dbi->update(table => 'table1', param => {key1 => 2}, where => {key2 => 2}, auto_filter_table => undef);
618
-$result = $dbi->execute($SELECT_SOURCES->{0});
619
-$row   = $result->fetch_hash_first;
620
-is_deeply($row, {key1 => 2, key2 => 2}, "$test : update : disable bind filter2");
621
-
622
-$dbi = DBIx::Custom->connect($NEW_ARGS->{0});
623
-$dbi->execute($CREATE_TABLE->{0});
624
-$dbi->register_filter(twice => sub { $_[0] * 2 });
625
-$dbi->auto_filter(
579
+$dbi->apply_filter(
626 580
     'table1', ['key1', 'twice', 'twice']
627 581
 );
628
-$dbi->insert(table => 'table1', param => {key1 => 2, key2 => 2}, auto_filter_table => undef);
582
+$dbi->insert(table => 'table1', param => {key1 => 2, key2 => 2}, filter => {key1=> undef});
629 583
 $dbi->delete(table => 'table1', where => {key1 => 1});
630 584
 $result = $dbi->execute($SELECT_SOURCES->{0});
631 585
 $rows   = $result->fetch_hash_all;
... ...
@@ -634,34 +588,10 @@ is_deeply($rows, [], "$test : delete");
634 588
 $dbi = DBIx::Custom->connect($NEW_ARGS->{0});
635 589
 $dbi->execute($CREATE_TABLE->{0});
636 590
 $dbi->register_filter(twice => sub { $_[0] * 2 });
637
-$dbi->auto_filter(
638
-    'table1', ['key1', 'twice', 'twice']
639
-);
640
-$dbi->insert(table => 'table1', param => {key1 => 2, key2 => 2}, auto_filter_table => undef);
641
-$dbi->delete(table => 'table1', where => {key1 => 1}, auto_filter_table => undef);
642
-$result = $dbi->execute($SELECT_SOURCES->{0});
643
-$rows   = $result->fetch_hash_all;
644
-is_deeply($rows, [{key1 => 2, key2 => 2}], "$test : delete : disable1");
645
-
646
-$dbi = DBIx::Custom->connect($NEW_ARGS->{0});
647
-$dbi->execute($CREATE_TABLE->{0});
648
-$dbi->register_filter(twice => sub { $_[0] * 2 });
649
-$dbi->auto_filter(
650
-    'table1', ['key1', 'twice', 'twice']
651
-);
652
-$dbi->insert(table => 'table1', param => {key1 => 2, key2 => 2}, auto_filter_table => undef);
653
-$dbi->delete(table => 'table1', where => {key1 => 1}, auto_filter_table => []);
654
-$result = $dbi->execute($SELECT_SOURCES->{0});
655
-$rows   = $result->fetch_hash_all;
656
-is_deeply($rows, [{key1 => 2, key2 => 2}], "$test : delete : disable2");
657
-
658
-$dbi = DBIx::Custom->connect($NEW_ARGS->{0});
659
-$dbi->execute($CREATE_TABLE->{0});
660
-$dbi->register_filter(twice => sub { $_[0] * 2 });
661
-$dbi->auto_filter(
591
+$dbi->apply_filter(
662 592
     'table1', ['key1', 'twice', 'twice']
663 593
 );
664
-$dbi->insert(table => 'table1', param => {key1 => 2, key2 => 2}, auto_filter_table => undef);
594
+$dbi->insert(table => 'table1', param => {key1 => 2, key2 => 2}, filter => {key1 => undef});
665 595
 $result = $dbi->select(table => 'table1', where => {key1 => 1});
666 596
 $result->filter({'key2' => 'twice'});
667 597
 $rows   = $result->fetch_hash_all;
... ...
@@ -670,25 +600,13 @@ is_deeply($rows, [{key1 => 4, key2 => 4}], "$test : select");
670 600
 $dbi = DBIx::Custom->connect($NEW_ARGS->{0});
671 601
 $dbi->execute($CREATE_TABLE->{0});
672 602
 $dbi->register_filter(twice => sub { $_[0] * 2 });
673
-$dbi->auto_filter(
674
-    'table1', ['key1', 'twice', 'twice']
675
-);
676
-$dbi->insert(table => 'table1', param => {key1 => 2, key2 => 2}, auto_filter_table => undef);
677
-$result = $dbi->select(table => 'table1', where => {key1 => 2}, auto_filter_table => []);
678
-$result->filter({'key2' => 'twice'});
679
-$rows   = $result->fetch_hash_all;
680
-is_deeply($rows, [{key1 => 2, key2 => 4}], "$test : select : disable");
681
-
682
-$dbi = DBIx::Custom->connect($NEW_ARGS->{0});
683
-$dbi->execute($CREATE_TABLE->{0});
684
-$dbi->register_filter(twice => sub { $_[0] * 2 });
685
-$dbi->auto_filter(
603
+$dbi->apply_filter(
686 604
     'table1', ['key1', 'twice', 'twice']
687 605
 );
688
-$dbi->insert(table => 'table1', param => {key1 => 2, key2 => 2}, auto_filter_table => undef);
606
+$dbi->insert(table => 'table1', param => {key1 => 2, key2 => 2}, filter => {key1 => undef});
689 607
 $result = $dbi->execute("select * from table1 where {= key1} and {= key2};",
690 608
                         param => {key1 => 1, key2 => 2},
691
-                        auto_filter_table => ['table1']);
609
+                        table => ['table1']);
692 610
 $rows   = $result->fetch_hash_all;
693 611
 is_deeply($rows, [{key1 => 4, key2 => 2}], "$test : execute");
694 612
 
... ...
@@ -697,14 +615,14 @@ $dbi->execute($CREATE_TABLE->{0});
697 615
 $dbi->execute($CREATE_TABLE->{2});
698 616
 $dbi->register_filter(twice => sub { $_[0] * 2 });
699 617
 $dbi->register_filter(three_times => sub { $_[0] * 3 });
700
-$dbi->auto_filter(
618
+$dbi->apply_filter(
701 619
     'table1', ['key2', 'twice', 'twice']
702 620
 );
703
-$dbi->auto_filter(
621
+$dbi->apply_filter(
704 622
     'table2', ['key3', 'three_times', 'three_times']
705 623
 );
706
-$dbi->insert(table => 'table1', param => {key1 => 5, key2 => 2}, auto_filter_table => undef);
707
-$dbi->insert(table => 'table2', param => {key1 => 5, key3 => 6}, auto_filter_table => undef);
624
+$dbi->insert(table => 'table1', param => {key1 => 5, key2 => 2}, filter => {key2 => undef});
625
+$dbi->insert(table => 'table2', param => {key1 => 5, key3 => 6}, filter => {key3 => undef});
708 626
 $result = $dbi->select(
709 627
      table => ['table1', 'table2'],
710 628
      column => ['key2', 'key3'],
... ...
@@ -723,7 +641,7 @@ $result->filter({'key2' => 'twice'});
723 641
 $rows   = $result->fetch_hash_all;
724 642
 is_deeply($rows, [{key2 => 4, key3 => 18}], "$test : select : join : omit");
725 643
 
726
-test 'auto_filter_easy_build';
644
+test 'apply_filter_easy_build';
727 645
 $dbi = DBIx::Custom->connect($NEW_ARGS->{0});
728 646
 $dbi->execute($CREATE_TABLE->{2});
729 647
 $dbi->execute($CREATE_TABLE->{3});