Showing 3 changed files with 41 additions and 32 deletions
+28 -22
lib/DBIx/Custom.pm
... ...
@@ -513,8 +513,8 @@ sub update {
513 513
 sub update_all {
514 514
     my ($self, $table, $update_params, $args) = @_;
515 515
     
516
+    # Allow all update
516 517
     $args ||= {};
517
-    
518 518
     $args->{allow_update_all} = 1;
519 519
     
520 520
     # Update all rows
... ...
@@ -582,8 +582,8 @@ sub delete {
582 582
 sub delete_all {
583 583
     my ($self, $table, $args) = @_;
584 584
     
585
+    # Allow all delete
585 586
     $args ||= {};
586
-    
587 587
     $args->{allow_delete_all} = 1;
588 588
     
589 589
     # Delete all rows
... ...
@@ -591,34 +591,40 @@ sub delete_all {
591 591
 }
592 592
 
593 593
 sub _select_usage { return << 'EOS' }
594
-Your select arguments is wrong.
595
-select usage:
594
+Select usage:
596 595
 $dbi->select(
597
-    $table,                # String or array ref
598
-    [@$columns],           # Array reference. this can be ommited
599
-    {%$where_params},      # Hash reference.  this can be ommited
600
-    $append_statement,     # String.          this can be ommited
601
-    $query_edit_callback   # Sub reference.   this can be ommited
596
+    $table,                   # String or array ref
597
+    {
598
+        columns => $columns   # Array reference
599
+        where   => $params    # Hash reference.
600
+        append  => $statement # String. 
601
+        query_edit_cb => $cb  # Sub reference
602
+    }
602 603
 );
603 604
 EOS
604 605
 
606
+our %VALID_SELECT_ARGS
607
+  = map { $_ => 1 } qw/columns where append query_edit_cb/;
608
+
609
+
605 610
 sub select {
606
-    my $self = shift;
607
-    
608
-    # Check argument
609
-    croak($self->_select_usage) unless @_;
611
+    my ($self, $tables, $args) = @_;
610 612
     
611
-    # Arguments
612
-    my $tables = shift || '';
613
-    $tables    = [$tables] unless ref $tables;
613
+    # Table
614
+    $tables ||= '';
615
+    $tables = [$tables] unless ref $tables;
614 616
     
615
-    my $columns          = ref $_[0] eq 'ARRAY' ? shift : [];
616
-    my $where_params     = ref $_[0] eq 'HASH'  ? shift : {};
617
-    my $append_statement = $_[0] && !ref $_[0]  ? shift : '';
618
-    my $query_edit_cb    = shift if ref $_[0] eq 'CODE';
617
+    # Check arguments
618
+    foreach my $name (keys %$args) {
619
+        croak "\"$name\" is invalid name"
620
+          unless $VALID_SELECT_ARGS{$name};
621
+    }
619 622
     
620
-    # Check rest argument
621
-    croak($self->_select_usage) if @_;
623
+    # Arguments
624
+    my $columns          = $args->{columns} || [];
625
+    my $where_params     = $args->{where} || {};
626
+    my $append_statement = $args->{append} || '';
627
+    my $query_edit_cb    = $args->{query_edit_cb};
622 628
     
623 629
     # SQL template for select statement
624 630
     my $template = 'select ';
+12 -9
t/dbix-custom-core-sqlite.t
... ...
@@ -624,19 +624,19 @@ $rows = $dbi->select('table1')->fetch_hash_all;
624 624
 is_deeply($rows, [{key1 => 1, key2 => 2},
625 625
                   {key1 => 3, key2 => 4}], "$test : table");
626 626
 
627
-$rows = $dbi->select('table1', ['key1'])->fetch_hash_all;
627
+$rows = $dbi->select('table1', {columns => ['key1']})->fetch_hash_all;
628 628
 is_deeply($rows, [{key1 => 1}, {key1 => 3}], "$test : table and columns and where key");
629 629
 
630
-$rows = $dbi->select('table1', {key1 => 1})->fetch_hash_all;
630
+$rows = $dbi->select('table1', {where => {key1 => 1}})->fetch_hash_all;
631 631
 is_deeply($rows, [{key1 => 1, key2 => 2}], "$test : table and columns and where key");
632 632
 
633
-$rows = $dbi->select('table1', ['key1'], {key1 => 3})->fetch_hash_all;
633
+$rows = $dbi->select('table1', {columns => ['key1'], where => {key1 => 3}})->fetch_hash_all;
634 634
 is_deeply($rows, [{key1 => 3}], "$test : table and columns and where key");
635 635
 
636
-$rows = $dbi->select('table1', "order by key1 desc limit 1")->fetch_hash_all;
636
+$rows = $dbi->select('table1', {append => "order by key1 desc limit 1"})->fetch_hash_all;
637 637
 is_deeply($rows, [{key1 => 3, key2 => 4}], "$test : append statement");
638 638
 
639
-$rows = $dbi->select('table1', {key1 => 2}, sub {
639
+$rows = $dbi->select('table1', {where => {key1 => 2}, query_edit_cb =>sub {
640 640
     my $query = shift;
641 641
     $query->bind_filter(sub {
642 642
         my ($value, $table, $column, $dbi) = @_;
... ...
@@ -645,15 +645,18 @@ $rows = $dbi->select('table1', {key1 => 2}, sub {
645 645
         }
646 646
         return $value;
647 647
     });
648
-})->fetch_hash_all;
648
+}})->fetch_hash_all;
649 649
 is_deeply($rows, [{key1 => 1, key2 => 2}], "$test : query edit call back");
650 650
 
651 651
 $dbi->do($CREATE_TABLE->{2});
652 652
 $dbi->insert('table2', {key1 => 1, key3 => 5});
653 653
 $rows = $dbi->select([qw/table1 table2/],
654
-                     ['table1.key1 as table1_key1', 'table2.key1 as table2_key1', 'key2', 'key3'],
655
-                     {'table1.key2' => 2},
656
-                     "where table1.key1 = table2.key1")->fetch_hash_all;
654
+                      {
655
+                         columns => ['table1.key1 as table1_key1', 'table2.key1 as table2_key1', 'key2', 'key3'],
656
+                         where   => {'table1.key2' => 2},
657
+                         append  => "where table1.key1 = table2.key1"
658
+                      }
659
+                    )->fetch_hash_all;
657 660
 is_deeply($rows, [{table1_key1 => 1, table2_key1 => 1, key2 => 2, key3 => 5}], "$test : join");
658 661
 
659 662
 test 'Cache';
+1 -1
t/dbix-custom-sqlite.t
... ...
@@ -40,7 +40,7 @@ $dbi->connect_memory;
40 40
 $ret_val = $dbi->do($CREATE_TABLE->{0});
41 41
 ok(defined $ret_val, $test);
42 42
 $dbi->insert('table1', {key1 => 'a', key2 => 2});
43
-$rows = $dbi->select('table1', {key1 => 'a'})->fetch_hash_all;
43
+$rows = $dbi->select('table1', {where => {key1 => 'a'}})->fetch_hash_all;
44 44
 is_deeply($rows, [{key1 => 'a', key2 => 2}], "$test : select rows");
45 45
 
46 46
 test 'connect_memory error';