Showing 2 changed files with 54 additions and 46 deletions
+52 -46
lib/DBIx/Custom.pm
... ...
@@ -357,7 +357,7 @@ sub delete_at {
357 357
     }
358 358
     
359 359
     # Create where parameter
360
-    my $where_param = $self->_create_where_param($where, $primary_keys);
360
+    my $where_param = $self->_create_param_from_id($where, $primary_keys);
361 361
     
362 362
     return $self->delete(where => $where_param, %args);
363 363
 }
... ...
@@ -589,7 +589,7 @@ sub insert {
589 589
     my $query_return  = delete $args{query};
590 590
     my $id = delete $args{id};
591 591
     my $primary_key = delete $args{primary_key};
592
-    croak "select method primary_key option " .
592
+    croak "insert method primary_key option " .
593 593
           "must be specified when id is specified " . _subname
594 594
       if defined $id && !defined $primary_key;
595 595
     $primary_key = [$primary_key] unless ref $primary_key eq 'ARRAY';
... ...
@@ -602,7 +602,7 @@ sub insert {
602 602
 
603 603
     # Merge parameter
604 604
     if ($id) {
605
-        my $id_param = $self->_create_where_param($id, $primary_key);
605
+        my $id_param = $self->_create_param_from_id($id, $primary_key);
606 606
         $param = $self->merge_param($id_param, $param);
607 607
     }
608 608
 
... ...
@@ -969,7 +969,7 @@ sub select_at {
969 969
     my $table = ref $args{table} ? $args{table}->[-1] : $args{table};
970 970
     
971 971
     # Create where parameter
972
-    my $where_param = $self->_create_where_param($where, $primary_keys);
972
+    my $where_param = $self->_create_param_from_id($where, $primary_keys);
973 973
     
974 974
     return $self->select(where => $where_param, %args);
975 975
 }
... ...
@@ -1008,6 +1008,12 @@ sub update {
1008 1008
     my $where_param      = delete $args{where_param} || {};
1009 1009
     my $append           = delete $args{append} || '';
1010 1010
     my $allow_update_all = delete $args{allow_update_all};
1011
+    my $id = delete $args{id};
1012
+    my $primary_key = delete $args{primary_key};
1013
+    croak "update method primary_key option " .
1014
+          "must be specified when id is specified " . _subname
1015
+      if defined $id && !defined $primary_key;
1016
+    $primary_key = [$primary_key] unless ref $primary_key eq 'ARRAY';
1011 1017
     
1012 1018
     # Check argument names
1013 1019
     foreach my $name (keys %args) {
... ...
@@ -1062,33 +1068,6 @@ sub update {
1062 1068
 
1063 1069
 sub update_all { shift->update(allow_update_all => 1, @_) };
1064 1070
 
1065
-our %UPDATE_AT_ARGS = (%UPDATE_ARGS, where => 1, primary_key => 1);
1066
-
1067
-sub update_at {
1068
-    my $self = shift;
1069
-    
1070
-    # Arguments
1071
-    my $param;
1072
-    $param = shift if @_ % 2;
1073
-    my %args = @_;
1074
-    my $primary_keys = delete $args{primary_key};
1075
-    $primary_keys = [$primary_keys] unless ref $primary_keys;
1076
-    my $where = delete $args{where};
1077
-    my $p = delete $args{param} || {};
1078
-    $param  ||= $p;
1079
-    
1080
-    # Check arguments
1081
-    foreach my $name (keys %args) {
1082
-        croak qq{"$name" is wrong option } . _subname
1083
-          unless $UPDATE_AT_ARGS{$name};
1084
-    }
1085
-    
1086
-    # Create where parameter
1087
-    my $where_param = $self->_create_where_param($where, $primary_keys);
1088
-    
1089
-    return $self->update(where => $where_param, param => $param, %args);
1090
-}
1091
-
1092 1071
 sub update_param {
1093 1072
     my ($self, $param, $opt) = @_;
1094 1073
     
... ...
@@ -1156,27 +1135,25 @@ sub _create_bind_values {
1156 1135
     return $bind;
1157 1136
 }
1158 1137
 
1159
-sub _create_where_param {
1160
-    my ($self, $where, $primary_keys) = @_;
1138
+sub _create_param_from_id {
1139
+    my ($self, $id, $primary_keys) = @_;
1161 1140
     
1162
-    # Create where parameter
1163
-    my $where_param = {};
1164
-    if ($where) {
1165
-        $where = [$where] unless ref $where;
1166
-        croak qq{"where" must be constant value or array reference}
1141
+    # Create parameter
1142
+    my $param = {};
1143
+    if ($id) {
1144
+        $id = [$id] unless ref $id;
1145
+        croak qq{"id" must be constant value or array reference}
1167 1146
             . " (" . (caller 1)[3] . ")"
1168
-          unless !ref $where || ref $where eq 'ARRAY';
1169
-        
1170
-        croak qq{"where" must contain values same count as primary key}
1147
+          unless !ref $id || ref $id eq 'ARRAY';
1148
+        croak qq{"id" must contain values same count as primary key}
1171 1149
             . " (" . (caller 1)[3] . ")"
1172
-          unless @$primary_keys eq @$where;
1173
-        
1150
+          unless @$primary_keys eq @$id;
1174 1151
         for(my $i = 0; $i < @$primary_keys; $i ++) {
1175
-           $where_param->{$primary_keys->[$i]} = $where->[$i];
1152
+           $param->{$primary_keys->[$i]} = $id->[$i];
1176 1153
         }
1177 1154
     }
1178 1155
     
1179
-    return $where_param;
1156
+    return $param;
1180 1157
 }
1181 1158
 
1182 1159
 sub _connect {
... ...
@@ -1353,6 +1330,35 @@ sub _where_to_obj {
1353 1330
     return $obj;
1354 1331
 }
1355 1332
 
1333
+# DEPRECATED!
1334
+our %UPDATE_AT_ARGS = (%UPDATE_ARGS, where => 1, primary_key => 1);
1335
+sub update_at {
1336
+    my $self = shift;
1337
+
1338
+    warn "update_at is DEPRECATED! use update and id option instead";
1339
+    
1340
+    # Arguments
1341
+    my $param;
1342
+    $param = shift if @_ % 2;
1343
+    my %args = @_;
1344
+    my $primary_keys = delete $args{primary_key};
1345
+    $primary_keys = [$primary_keys] unless ref $primary_keys;
1346
+    my $where = delete $args{where};
1347
+    my $p = delete $args{param} || {};
1348
+    $param  ||= $p;
1349
+    
1350
+    # Check arguments
1351
+    foreach my $name (keys %args) {
1352
+        croak qq{"$name" is wrong option } . _subname
1353
+          unless $UPDATE_AT_ARGS{$name};
1354
+    }
1355
+    
1356
+    # Create where parameter
1357
+    my $where_param = $self->_create_param_from_id($where, $primary_keys);
1358
+    
1359
+    return $self->update(where => $where_param, param => $param, %args);
1360
+}
1361
+
1356 1362
 # DEPRECATED!
1357 1363
 our %INSERT_AT_ARGS = (%INSERT_ARGS, where => 1, primary_key => 1);
1358 1364
 sub insert_at {
... ...
@@ -1377,7 +1383,7 @@ sub insert_at {
1377 1383
     }
1378 1384
     
1379 1385
     # Create where parameter
1380
-    my $where_param = $self->_create_where_param($where, $primary_key);
1386
+    my $where_param = $self->_create_param_from_id($where, $primary_key);
1381 1387
     $param = $self->merge_param($where_param, $param);
1382 1388
     
1383 1389
     return $self->insert(param => $param, %args);
+2
t/dbix-custom-core-sqlite.t
... ...
@@ -2323,6 +2323,7 @@ $dbi->update(
2323 2323
 $rows = $dbi->select(table => 'table1')->fetch_hash_all;
2324 2324
 is_deeply($rows, [{key1 => 5, key2 => 2}]);
2325 2325
 
2326
+
2326 2327
 test 'insert id and primary_key option';
2327 2328
 $dbi = DBIx::Custom->connect($NEW_ARGS->{0});
2328 2329
 $dbi->execute($CREATE_TABLE->{1});
... ...
@@ -2361,6 +2362,7 @@ is($dbi->select(table => 'table1')->one->{key1}, 1);
2361 2362
 is($dbi->select(table => 'table1')->one->{key2}, 2);
2362 2363
 is($dbi->select(table => 'table1')->one->{key3}, 3);
2363 2364
 
2365
+
2364 2366
 test 'model insert id and primary_key option';
2365 2367
 $dbi = MyDBI6->connect($NEW_ARGS->{0});
2366 2368
 $dbi->execute($CREATE_TABLE->{1});