Showing 1 changed files with 57 additions and 66 deletions
+57 -66
lib/DBIx/Custom.pm
... ...
@@ -241,17 +241,17 @@ sub delete {
241 241
       if !$opt{where} && !defined $opt{id} && !$opt{allow_delete_all};
242 242
     
243 243
     # Where
244
-    my $wcp = $self->_where_clause_and_param($opt{where}, $opt{where_param},
244
+    my $w = $self->_where_clause_and_param($opt{where}, $opt{where_param},
245 245
       delete $opt{id}, $opt{primary_key}, $opt{table});
246 246
 
247 247
     # Delete statement
248 248
     my $sql = "delete ";
249 249
     $sql .= "$opt{prefix} " if defined $opt{prefix};
250
-    $sql .= "from " . $self->_q($opt{table}) . " $wcp->{clause} ";
250
+    $sql .= "from " . $self->_q($opt{table}) . " $w->{clause} ";
251 251
     
252 252
     # Execute query
253 253
     $opt{statement} = 'delete';
254
-    $self->execute($sql, $wcp->{param}, %opt);
254
+    $self->execute($sql, $w->{param}, %opt);
255 255
 }
256 256
 
257 257
 sub delete_all { shift->delete(allow_delete_all => 1, @_) }
... ...
@@ -1049,13 +1049,9 @@ sub update {
1049 1049
     # Assign clause
1050 1050
     my $assign_clause = $self->assign_clause($param, {wrap => $opt{wrap}});
1051 1051
     
1052
-    # Convert id to where parameter
1053
-    my $where = defined $opt{id}
1054
-      ? $self->_id_to_param(delete $opt{id}, $opt{primary_key}, $opt{table})
1055
-      : $opt{where};
1056
-
1057 1052
     # Where
1058
-    my $w = $self->_where_clause_and_param($where, $opt{where_param});
1053
+    my $w = $self->_where_clause_and_param($opt{where}, $opt{where_param},
1054
+      delete $opt{id}, $opt{primary_key}, $opt{table});
1059 1055
     
1060 1056
     # Merge where parameter to parameter
1061 1057
     $param = $self->merge_param($param, $w->{param}) if keys %{$w->{param}};
... ...
@@ -1526,78 +1522,73 @@ sub _search_tables {
1526 1522
     return $tables;
1527 1523
 }
1528 1524
 
1529
-sub _where_to_obj {
1530
-    my ($self, $where) = @_;
1531
-    
1532
-    my $obj;
1533
-    
1534
-    # Hash
1535
-    if (ref $where eq 'HASH') {
1536
-        my $clause = ['and'];
1537
-        my $q = $self->_quote;
1538
-        for my $column (keys %$where) {
1539
-            my $table;
1540
-            my $c;
1541
-            if ($column =~ /(?:(.*?)\.)?(.*)/) {
1542
-                $table = $1;
1543
-                $c = $2;
1544
-            }
1545
-            
1546
-            my $table_quote;
1547
-            $table_quote = $self->_q($table) if defined $table;
1548
-            my $column_quote = $self->_q($c);
1549
-            $column_quote = $table_quote . '.' . $column_quote
1550
-              if defined $table_quote;
1551
-            push @$clause, "$column_quote = :$column" for keys %$where;
1552
-        }
1553
-        $obj = $self->where(clause => $clause, param => $where);
1554
-    }
1555
-    
1556
-    # DBIx::Custom::Where object
1557
-    elsif (ref $where eq 'DBIx::Custom::Where') {
1558
-        $obj = $where;
1559
-    }
1560
-    
1561
-    # Array
1562
-    elsif (ref $where eq 'ARRAY') {
1563
-        $obj = $self->where(
1564
-            clause => $where->[0],
1565
-            param  => $where->[1]
1566
-        );
1567
-    }
1568
-    
1569
-    # Check where argument
1570
-    croak qq{"where" must be hash reference or DBIx::Custom::Where object}
1571
-        . qq{or array reference, which contains where clause and parameter}
1572
-        . _subname
1573
-      unless ref $obj eq 'DBIx::Custom::Where';
1574
-    
1575
-    return $obj;
1576
-}
1577
-
1578 1525
 sub _where_clause_and_param {
1579
-    my ($self, $where, $param, $id, $primary_key, $table) = @_;
1526
+    my ($self, $where, $where_param, $id, $primary_key, $table) = @_;
1580 1527
 
1581 1528
     $where ||= {};
1582 1529
     $where = $self->_id_to_param($id, $primary_key, $table) if defined $id;
1583
-    $param ||= {};
1530
+    $where_param ||= {};
1584 1531
     my $w = {};
1585 1532
     my $where_clause = '';
1586 1533
 
1534
+    my $obj;
1535
+    
1587 1536
     if (ref $where eq 'ARRAY' && !ref $where->[0]) {
1588 1537
         $w->{clause} = "where " . $where->[0];
1589 1538
         $w->{param} = $where->[1];
1590 1539
     }
1591 1540
     elsif (ref $where) {
1592
-        $where = $self->_where_to_obj($where);
1593
-        $w->{param} = keys %$param
1594
-                    ? $self->merge_param($param, $where->param)
1595
-                    : $where->param;
1596
-        $w->{clause} = $where->to_string;
1541
+
1542
+        # Hash
1543
+        if (ref $where eq 'HASH') {
1544
+            my $clause = ['and'];
1545
+            my $q = $self->_quote;
1546
+            for my $column (keys %$where) {
1547
+                my $table;
1548
+                my $c;
1549
+                if ($column =~ /(?:(.*?)\.)?(.*)/) {
1550
+                    $table = $1;
1551
+                    $c = $2;
1552
+                }
1553
+                
1554
+                my $table_quote;
1555
+                $table_quote = $self->_q($table) if defined $table;
1556
+                my $column_quote = $self->_q($c);
1557
+                $column_quote = $table_quote . '.' . $column_quote
1558
+                  if defined $table_quote;
1559
+                push @$clause, "$column_quote = :$column" for keys %$where;
1560
+            }
1561
+            $obj = $self->where(clause => $clause, param => $where);
1562
+        }
1563
+        
1564
+        # DBIx::Custom::Where object
1565
+        elsif (ref $where eq 'DBIx::Custom::Where') {
1566
+            $obj = $where;
1567
+        }
1568
+        
1569
+        # Array
1570
+        elsif (ref $where eq 'ARRAY') {
1571
+            $obj = $self->where(
1572
+                clause => $where->[0],
1573
+                param  => $where->[1]
1574
+            );
1575
+        }
1576
+        
1577
+        # Check where argument
1578
+        croak qq{"where" must be hash reference or DBIx::Custom::Where object}
1579
+            . qq{or array reference, which contains where clause and parameter}
1580
+            . _subname
1581
+          unless ref $obj eq 'DBIx::Custom::Where';
1582
+
1583
+
1584
+        $w->{param} = keys %$where_param
1585
+                    ? $self->merge_param($where_param, $obj->param)
1586
+                    : $obj->param;
1587
+        $w->{clause} = $obj->to_string;
1597 1588
     }
1598 1589
     elsif ($where) {
1599 1590
         $w->{clause} = "where $where";
1600
-        $w->{param} = $param;
1591
+        $w->{param} = $where_param;
1601 1592
     }
1602 1593
     
1603 1594
     return $w;