Showing 2 changed files with 47 additions and 35 deletions
+42 -30
lib/DBIx/Custom.pm
... ...
@@ -241,9 +241,9 @@ our %VALID_DELETE_ARGS
241 241
 sub delete {
242 242
     my ($self, %args) = @_;
243 243
     
244
-    # Check arguments
244
+    # Check argument names
245 245
     foreach my $name (keys %args) {
246
-        croak qq{"$name" is invalid argument}
246
+        croak qq{Argument "$name" is invalid name}
247 247
           unless $VALID_DELETE_ARGS{$name};
248 248
     }
249 249
     
... ...
@@ -307,9 +307,9 @@ our %VALID_DELETE_AT_ARGS
307 307
 sub delete_at {
308 308
     my ($self, %args) = @_;
309 309
     
310
-    # Check arguments
310
+    # Check argument names
311 311
     foreach my $name (keys %args) {
312
-        croak qq{"$name" is invalid argument}
312
+        croak qq{Argument "$name" is invalid name}
313 313
           unless $VALID_DELETE_AT_ARGS{$name};
314 314
     }
315 315
     
... ...
@@ -349,9 +349,9 @@ our %VALID_EXECUTE_ARGS = map { $_ => 1 } qw/param filter table/;
349 349
 sub execute{
350 350
     my ($self, $query, %args)  = @_;
351 351
     
352
-    # Check arguments
352
+    # Check argument names
353 353
     foreach my $name (keys %args) {
354
-        croak qq{"$name" is invalid argument}
354
+        croak qq{Argument "$name" is invalid name}
355 355
           unless $VALID_EXECUTE_ARGS{$name};
356 356
     }
357 357
     
... ...
@@ -369,6 +369,13 @@ sub execute{
369 369
     $arg_tables = [$arg_tables]
370 370
       unless ref $arg_tables eq 'ARRAY';
371 371
     push @$tables, @$arg_tables;
372
+
373
+    # Organize tables
374
+    my %table_set = map {defined $_ ? ($_ => 1) : ()} @$tables;
375
+    my $main_table = pop @$tables;
376
+    delete $table_set{$main_table} if $main_table;
377
+    $tables = [keys %table_set];
378
+    push @$tables, $main_table if $main_table;
372 379
     
373 380
     foreach my $table (@$tables) {
374 381
         next unless $table;
... ...
@@ -442,9 +449,9 @@ our %VALID_INSERT_ARGS = map { $_ => 1 } qw/table param append
442 449
 sub insert {
443 450
     my ($self, %args) = @_;
444 451
 
445
-    # Check arguments
452
+    # Check argument names
446 453
     foreach my $name (keys %args) {
447
-        croak qq{"$name" is invalid argument}
454
+        croak qq{Argument "$name" is invalid name}
448 455
           unless $VALID_INSERT_ARGS{$name};
449 456
     }
450 457
     
... ...
@@ -497,9 +504,9 @@ our %VALID_INSERT_AT_ARGS
497 504
 sub insert_at {
498 505
     my ($self, %args) = @_;
499 506
     
500
-    # Check arguments
507
+    # Check argument names
501 508
     foreach my $name (keys %args) {
502
-        croak qq{"$name" is invalid argument}
509
+        croak qq{Argument "$name" is invalid name}
503 510
           unless $VALID_INSERT_AT_ARGS{$name};
504 511
     }
505 512
     
... ...
@@ -630,9 +637,9 @@ sub _need_tables {
630 637
 sub select {
631 638
     my ($self, %args) = @_;
632 639
     
633
-    # Check arguments
640
+    # Check argument names
634 641
     foreach my $name (keys %args) {
635
-        croak qq{"$name" is invalid argument}
642
+        croak qq{Argument "$name" is invalid name}
636 643
           unless $VALID_SELECT_ARGS{$name};
637 644
     }
638 645
     
... ...
@@ -651,23 +658,26 @@ sub select {
651 658
     croak qq{"join" must be array reference}
652 659
       unless ref $join eq 'ARRAY';
653 660
     
661
+    # Join tables
654 662
     my @join_tables;
655 663
     unshift @join_tables, $tables->[-1];
656 664
     
657
-    # Relation table(DEPRECATED!);
658
-    $self->_add_relation_table($args{relation}, $tables);
665
+    # Add relation tables(DEPRECATED!);
666
+    $self->_add_relation_table($tables, $args{relation});
659 667
     
660 668
     # SQL stack
661 669
     my @sql;
662
-    
663 670
     push @sql, 'select';
664 671
     
672
+    # Selection
665 673
     if ($selection) {
666 674
         push @sql, $selection;
667 675
         push @join_tables, @{$self->_tables($selection)};
668 676
     }
677
+    
678
+    # Column names and table name
669 679
     else {
670
-        # Column clause
680
+        # Column names
671 681
         if (@$columns) {
672 682
             foreach my $column (@$columns) {
673 683
                 push @join_tables, @{$self->_tables($column)};
... ...
@@ -691,9 +701,7 @@ sub select {
691 701
     if (ref $where eq 'HASH') {
692 702
         my $clause = ['and'];
693 703
         push @$clause, "{= $_}" for keys %$where;
694
-        $w = $self->where;
695
-        $w->clause($clause);
696
-        $w->param($where);
704
+        $w = $self->where(clause => $clause, param => $where);
697 705
     }
698 706
     elsif (ref $where eq 'DBIx::Custom::Where') {
699 707
         $w = $where;
... ...
@@ -706,7 +714,7 @@ sub select {
706 714
     # String where
707 715
     my $swhere = "$w";
708 716
     
709
-    # Table name in Where
717
+    # Add table names in where clause to join talbes.
710 718
     unshift @join_tables, @{$self->_tables($swhere)};
711 719
     
712 720
     # Join
... ...
@@ -744,7 +752,7 @@ sub select {
744 752
         }
745 753
     }
746 754
     
747
-    # Add where
755
+    # Add where clause
748 756
     push @sql, $swhere;
749 757
     
750 758
     # Relation(DEPRECATED!);
... ...
@@ -777,9 +785,9 @@ our %VALID_SELECT_AT_ARGS
777 785
 sub select_at {
778 786
     my ($self, %args) = @_;
779 787
     
780
-    # Check arguments
788
+    # Check argument names
781 789
     foreach my $name (keys %args) {
782
-        croak qq{"$name" is invalid argument}
790
+        croak qq{Argument "$name" is invalid name}
783 791
           unless $VALID_SELECT_AT_ARGS{$name};
784 792
     }
785 793
     
... ...
@@ -921,9 +929,9 @@ our %VALID_UPDATE_ARGS
921 929
 sub update {
922 930
     my ($self, %args) = @_;
923 931
     
924
-    # Check arguments
932
+    # Check argument names
925 933
     foreach my $name (keys %args) {
926
-        croak qq{"$name" is invalid argument}
934
+        croak qq{Argument "$name" is invalid name}
927 935
           unless $VALID_UPDATE_ARGS{$name};
928 936
     }
929 937
     
... ...
@@ -1020,9 +1028,9 @@ our %VALID_UPDATE_AT_ARGS
1020 1028
 sub update_at {
1021 1029
     my ($self, %args) = @_;
1022 1030
     
1023
-    # Check arguments
1031
+    # Check argument names
1024 1032
     foreach my $name (keys %args) {
1025
-        croak qq{"$name" is invalid argument}
1033
+        croak qq{Argument "$name" is invalid name}
1026 1034
           unless $VALID_UPDATE_AT_ARGS{$name};
1027 1035
     }
1028 1036
     
... ...
@@ -1078,7 +1086,8 @@ sub where {
1078 1086
 
1079 1087
     return DBIx::Custom::Where->new(
1080 1088
         query_builder => $self->query_builder,
1081
-        safety_column_name => $self->safety_column_name
1089
+        safety_column_name => $self->safety_column_name,
1090
+        @_
1082 1091
     );
1083 1092
 }
1084 1093
 
... ...
@@ -1234,7 +1243,7 @@ sub _push_relation {
1234 1243
 
1235 1244
 # DEPRECATED!
1236 1245
 sub _add_relation_table {
1237
-    my ($self, $relation, $tables) = @_;
1246
+    my ($self, $tables, $relation) = @_;
1238 1247
     
1239 1248
     if (keys %{$relation || {}}) {
1240 1249
         foreach my $rcolumn (keys %$relation) {
... ...
@@ -1887,7 +1896,10 @@ the key and value is delete from C<param>.
1887 1896
 
1888 1897
 =head2 C<(experimental) where>
1889 1898
 
1890
-    my $where = $dbi->where;
1899
+    my $where = $dbi->where(
1900
+        clause => ['and', '{= title}', '{= author}'],
1901
+        param => {title => 'Perl', author => 'Ken'}
1902
+    );
1891 1903
 
1892 1904
 Create a new L<DBIx::Custom::Where> object.
1893 1905
 
+5 -5
t/dbix-custom-core-sqlite.t
... ...
@@ -236,7 +236,7 @@ $rows = $dbi->select(table => 'table1')->fetch_hash_all;
236 236
 is_deeply($rows, [{key1 => 1, key2 => 2}], 'insert append');
237 237
 
238 238
 eval{$dbi->insert(table => 'table1', noexist => 1)};
239
-like($@, qr/noexist/, "invalid argument");
239
+like($@, qr/noexist/, "invalid");
240 240
 
241 241
 eval{$dbi->insert(table => 'table', param => {';' => 1})};
242 242
 like($@, qr/safety/);
... ...
@@ -285,7 +285,7 @@ is_deeply($rows, [{key1 => 1, key2 => 22, key3 => 3, key4 => 4, key5 => 5},
285 285
 $result = $dbi->update(table => 'table1', param => {key2 => 11}, where => {key1 => 1}, append => '   ');
286 286
 
287 287
 eval{$dbi->update(table => 'table1', noexist => 1)};
288
-like($@, qr/noexist/, "invalid argument");
288
+like($@, qr/noexist/, "invalid");
289 289
 
290 290
 eval{$dbi->update(table => 'table1')};
291 291
 like($@, qr/where/, "not contain where");
... ...
@@ -359,7 +359,7 @@ $rows = $dbi->select(table => 'table1')->fetch_hash_all;
359 359
 is_deeply($rows, [{key1 => 3, key2 => 4}], "delete multi key");
360 360
 
361 361
 eval{$dbi->delete(table => 'table1', noexist => 1)};
362
-like($@, qr/noexist/, "invalid argument");
362
+like($@, qr/noexist/, "invalid");
363 363
 
364 364
 $dbi = DBIx::Custom->connect($NEW_ARGS->{0});
365 365
 $dbi->execute($CREATE_TABLE->{0});
... ...
@@ -437,7 +437,7 @@ $rows = $dbi->select(
437 437
 is_deeply($rows, [{table1_key1 => 1, table2_key1 => 1, key2 => 2, key3 => 5}], "relation : no exists where");
438 438
 
439 439
 eval{$dbi->select(table => 'table1', noexist => 1)};
440
-like($@, qr/noexist/, "invalid argument");
440
+like($@, qr/noexist/, "invalid");
441 441
 
442 442
 
443 443
 test 'fetch filter';
... ...
@@ -514,7 +514,7 @@ $dbi->execute($CREATE_TABLE->{0});
514 514
 }
515 515
 
516 516
 eval{$dbi->execute('select * from table1', no_exists => 1)};
517
-like($@, qr/\Q"no_exists" is invalid argument/, "invald SQL");
517
+like($@, qr/invalid/, "invald SQL");
518 518
 
519 519
 $query = $dbi->create_query('select * from table1 where {= key1}');
520 520
 $dbi->dbh->disconnect;