Showing 5 changed files with 56 additions and 21 deletions
+2
Changes
... ...
@@ -1,3 +1,5 @@
1
+0.1680
2
+    - select, update, and delete where option can receive string where clause
1 3
 0.1679
2 4
     - added EXPERIMENTAL select() wrap option to support Oracle ROWNUM
3 5
 0.1678
+25 -15
lib/DBIx/Custom.pm
... ...
@@ -265,7 +265,7 @@ sub dbh {
265 265
 }
266 266
 
267 267
 our %DELETE_ARGS
268
-  = map { $_ => 1 } @COMMON_ARGS, qw/where append allow_delete_all/;
268
+  = map { $_ => 1 } @COMMON_ARGS, qw/where append allow_delete_all param/;
269 269
 
270 270
 sub delete {
271 271
     my ($self, %args) = @_;
... ...
@@ -284,12 +284,19 @@ sub delete {
284 284
     my $append           = delete $args{append};
285 285
     my $allow_delete_all = delete $args{allow_delete_all};
286 286
     my $query_return     = delete $args{query};
287
+    my $param            = delete $args{param} || {};
287 288
 
288 289
     # Where
289
-    $where = $self->_where_to_obj($where);
290
-    
291
-    # Where clause
292
-    my $where_clause = $where->to_string;
290
+    my $where_clause = '';
291
+    if (ref $where) {
292
+        $where = $self->_where_to_obj($where);
293
+        $param = keys %$param ? $self->merge_param($param, $where->param)
294
+                              : $where->param;
295
+        
296
+        # String where
297
+        $where_clause = $where->to_string;
298
+    }
299
+    elsif ($where) { $where_clause = "where $where" }
293 300
     croak qq{"where" must be specified } . _subname
294 301
       if $where_clause eq '' && !$allow_delete_all;
295 302
 
... ...
@@ -307,7 +314,7 @@ sub delete {
307 314
     # Execute query
308 315
     return $self->execute(
309 316
         $query,
310
-        param => $where->param,
317
+        param => $param,
311 318
         table => $table,
312 319
         %args
313 320
     );
... ...
@@ -869,7 +876,7 @@ sub select {
869 876
     unshift @$tables, @{$self->_search_tables(join(' ', keys %$param) || '')};
870 877
     
871 878
     # Where
872
-    my $where_clause;
879
+    my $where_clause = '';
873 880
     if (ref $where) {
874 881
         $where = $self->_where_to_obj($where);
875 882
         $param = keys %$param ? $self->merge_param($param, $where->param)
... ...
@@ -878,9 +885,7 @@ sub select {
878 885
         # String where
879 886
         $where_clause = $where->to_string;
880 887
     }
881
-    else {
882
-        $where_clause = $where;
883
-    }
888
+    elsif ($where) { $where_clause = "where $where" }
884 889
     
885 890
     # Add table names in where clause
886 891
     unshift @$tables, @{$self->_search_tables($where_clause)};
... ...
@@ -1003,8 +1008,16 @@ sub update {
1003 1008
     my $update_clause = '{update_param ' . join(' ', @columns) . '}';
1004 1009
 
1005 1010
     # Where
1006
-    $where = $self->_where_to_obj($where);
1007
-    my $where_clause = $where->to_string;
1011
+    my $where_clause = '';
1012
+    if (ref $where) {
1013
+        $where = $self->_where_to_obj($where);
1014
+        $param = keys %$param ? $self->merge_param($param, $where->param)
1015
+                              : $where->param;
1016
+        
1017
+        # String where
1018
+        $where_clause = $where->to_string;
1019
+    }
1020
+    elsif ($where) { $where_clause = "where $where" }
1008 1021
     croak qq{"where" must be specified } . _subname
1009 1022
       if "$where_clause" eq '' && !$allow_update_all;
1010 1023
     
... ...
@@ -1013,9 +1026,6 @@ sub update {
1013 1026
     push @sql, "update $q$table$q $update_clause $where_clause";
1014 1027
     push @sql, $append if $append;
1015 1028
     
1016
-    # Merge parameters
1017
-    $param = $self->merge_param($param, $where->param);
1018
-    
1019 1029
     # SQL
1020 1030
     my $sql = join(' ', @sql);
1021 1031
     
+1 -1
lib/DBIx/Custom/QueryBuilder.pm
... ...
@@ -78,7 +78,7 @@ sub _build_query {
78 78
                              || $self->tags->{$tag_name};
79 79
             
80 80
             # Tag is not registered
81
-            croak qq{Tag "$tag_name" in "{a }" is not registered } . _subname
81
+            croak qq{Tag "$tag_name" is not registered } . _subname
82 82
               unless $tag;
83 83
             
84 84
             # Tag not sub reference
+27 -4
t/dbix-custom-core-sqlite.t
... ...
@@ -2151,8 +2151,6 @@ $dbi->select(
2151 2151
 };
2152 2152
 like($@, qr/array/);
2153 2153
 
2154
-__END__
2155
-
2156 2154
 test 'select() string where';
2157 2155
 $dbi = DBIx::Custom->connect($NEW_ARGS->{0});
2158 2156
 $dbi->execute($CREATE_TABLE->{0});
... ...
@@ -2160,8 +2158,33 @@ $dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
2160 2158
 $dbi->insert(table => 'table1', param => {key1 => 2, key2 => 3});
2161 2159
 $rows = $dbi->select(
2162 2160
     table => 'table1',
2163
-    column => 'key1',
2164
-    where => '{= key1} and {=key2}',
2161
+    where => '{= key1} and {= key2}',
2165 2162
     param => {key1 => 1, key2 => 2}
2166 2163
 )->fetch_hash_all;
2167 2164
 is_deeply($rows, [{key1 => 1, key2 => 2}]);
2165
+
2166
+test 'delete() string where';
2167
+$dbi = DBIx::Custom->connect($NEW_ARGS->{0});
2168
+$dbi->execute($CREATE_TABLE->{0});
2169
+$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
2170
+$dbi->insert(table => 'table1', param => {key1 => 2, key2 => 3});
2171
+$dbi->delete(
2172
+    table => 'table1',
2173
+    where => '{= key1} and {= key2}',
2174
+    param => {key1 => 1, key2 => 2}
2175
+);
2176
+$rows = $dbi->select(table => 'table1')->fetch_hash_all;
2177
+is_deeply($rows, [{key1 => 2, key2 => 3}]);
2178
+
2179
+
2180
+test 'update() string where';
2181
+$dbi = DBIx::Custom->connect($NEW_ARGS->{0});
2182
+$dbi->execute($CREATE_TABLE->{0});
2183
+$dbi->insert(table => 'table1', param => {key1 => 1, key2 => 2});
2184
+$dbi->update(
2185
+    table => 'table1',
2186
+    param => {key1 => [5, 1], key2 => 2},
2187
+    where => '{= key1} and {= key2}'
2188
+);
2189
+$rows = $dbi->select(table => 'table1')->fetch_hash_all;
2190
+is_deeply($rows, [{key1 => 5, key2 => 2}]);
+1 -1
t/dbix-custom-querybuilder.t
... ...
@@ -87,7 +87,7 @@ eval{$builder->build_query('{? }')};
87 87
 like($@, qr/\QColumn name must be specified in tag "{? }"/, "? not arguments");
88 88
 
89 89
 eval{$builder->build_query("{a }")};
90
-like($@, qr/\QTag "a" in "{a }" is not registered/, "tag not exist");
90
+like($@, qr/\QTag "a" is not registered/, "tag not exist");
91 91
 
92 92
 $builder->register_tag({
93 93
     q => 'string'