Showing 2 changed files with 86 additions and 29 deletions
+36 -29
lib/DBIx/Custom.pm
... ...
@@ -829,7 +829,13 @@ sub select {
829 829
     my $where_param = delete $args{where_param} || $param || {};
830 830
     my $query_return = $args{query};
831 831
     my $wrap = delete $args{wrap};
832
-
832
+    my $id = delete $args{id};
833
+    my $primary_key = delete $args{primary_key};
834
+    croak "update method primary_key option " .
835
+          "must be specified when id is specified " . _subname
836
+      if defined $id && !defined $primary_key;
837
+    $primary_key = [$primary_key] unless ref $primary_key eq 'ARRAY';
838
+    
833 839
     # Check arguments
834 840
     foreach my $name (keys %args) {
835 841
         croak qq{"$name" is wrong option } . _subname
... ...
@@ -879,6 +885,7 @@ sub select {
879 885
     
880 886
     # Where
881 887
     my $where_clause = '';
888
+    $where = $self->_create_param_from_id($id, $primary_key) if $id;
882 889
     if (ref $where) {
883 890
         $where = $self->_where_to_obj($where);
884 891
         $where_param = keys %$where_param
... ...
@@ -931,34 +938,6 @@ sub select {
931 938
     return $result;
932 939
 }
933 940
 
934
-our %SELECT_AT_ARGS = (%SELECT_ARGS, where => 1, primary_key => 1);
935
-
936
-sub select_at {
937
-    my ($self, %args) = @_;
938
-
939
-    # Arguments
940
-    my $primary_keys = delete $args{primary_key};
941
-    $primary_keys = [$primary_keys] unless ref $primary_keys;
942
-    my $where = delete $args{where};
943
-    my $param = delete $args{param};
944
-    
945
-    # Check arguments
946
-    foreach my $name (keys %args) {
947
-        croak qq{"$name" is wrong option } . _subname
948
-          unless $SELECT_AT_ARGS{$name};
949
-    }
950
-    
951
-    # Table
952
-    croak qq{"table" option must be specified } . _subname
953
-      unless $args{table};
954
-    my $table = ref $args{table} ? $args{table}->[-1] : $args{table};
955
-    
956
-    # Create where parameter
957
-    my $where_param = $self->_create_param_from_id($where, $primary_keys);
958
-    
959
-    return $self->select(where => $where_param, %args);
960
-}
961
-
962 941
 sub setup_model {
963 942
     my $self = shift;
964 943
     
... ...
@@ -1316,6 +1295,34 @@ sub _where_to_obj {
1316 1295
     return $obj;
1317 1296
 }
1318 1297
 
1298
+# DEPRECATED!
1299
+our %SELECT_AT_ARGS = (%SELECT_ARGS, where => 1, primary_key => 1);
1300
+sub select_at {
1301
+    my ($self, %args) = @_;
1302
+
1303
+    # Arguments
1304
+    my $primary_keys = delete $args{primary_key};
1305
+    $primary_keys = [$primary_keys] unless ref $primary_keys;
1306
+    my $where = delete $args{where};
1307
+    my $param = delete $args{param};
1308
+    
1309
+    # Check arguments
1310
+    foreach my $name (keys %args) {
1311
+        croak qq{"$name" is wrong option } . _subname
1312
+          unless $SELECT_AT_ARGS{$name};
1313
+    }
1314
+    
1315
+    # Table
1316
+    croak qq{"table" option must be specified } . _subname
1317
+      unless $args{table};
1318
+    my $table = ref $args{table} ? $args{table}->[-1] : $args{table};
1319
+    
1320
+    # Create where parameter
1321
+    my $where_param = $self->_create_param_from_id($where, $primary_keys);
1322
+    
1323
+    return $self->select(where => $where_param, %args);
1324
+}
1325
+
1319 1326
 # DEPRECATED!
1320 1327
 our %DELETE_AT_ARGS = (%DELETE_ARGS, where => 1, primary_key => 1);
1321 1328
 sub delete_at {
+50
t/dbix-custom-core-sqlite.t
... ...
@@ -2466,4 +2466,54 @@ $dbi->insert(table => 'table3', param => {key1 => 1, key2 => 2, key3 => 3});
2466 2466
 $dbi->model('table1_3')->delete(id => [1, 2]);
2467 2467
 is_deeply($dbi->select(table => 'table1')->fetch_hash_all, []);
2468 2468
 
2469
+
2470
+test 'select and id option';
2471
+$dbi = DBIx::Custom->connect($NEW_ARGS->{0});
2472
+$dbi->execute($CREATE_TABLE->{1});
2473
+$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
2474
+$result = $dbi->select(
2475
+    table => 'table1',
2476
+    primary_key => ['key1', 'key2'],
2477
+    id => [1, 2]
2478
+);
2479
+$row = $result->one;
2480
+is($row->{key1}, 1);
2481
+is($row->{key2}, 2);
2482
+is($row->{key3}, 3);
2483
+
2484
+$dbi->delete_all(table => 'table1');
2485
+$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
2486
+$result = $dbi->select(
2487
+    table => 'table1',
2488
+    primary_key => 'key1',
2489
+    id => 1,
2490
+);
2491
+$row = $result->one;
2492
+is($row->{key1}, 1);
2493
+is($row->{key2}, 2);
2494
+is($row->{key3}, 3);
2495
+
2496
+$dbi->delete_all(table => 'table1');
2497
+$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
2498
+$result = $dbi->select(
2499
+    table => 'table1',
2500
+    primary_key => ['key1', 'key2'],
2501
+    id => [1, 2]
2502
+);
2503
+$row = $result->one;
2504
+is($row->{key1}, 1);
2505
+is($row->{key2}, 2);
2506
+is($row->{key3}, 3);
2507
+
2508
+
2509
+test 'model select_at';
2510
+$dbi = MyDBI6->connect($NEW_ARGS->{0});
2511
+$dbi->execute($CREATE_TABLE->{1});
2512
+$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2, key3 => 3});
2513
+$result = $dbi->model('table1')->select(id => [1, 2]);
2514
+$row = $result->one;
2515
+is($row->{key1}, 1);
2516
+is($row->{key2}, 2);
2517
+is($row->{key3}, 3);
2518
+
2469 2519
 =cut