[Solar-svn] Revision 3058

pmjones at solarphp.com pmjones at solarphp.com
Sat Mar 29 17:54:08 CDT 2008


Solar_Sql_Select: [CHG] Methods multiWhere() and multiHaving() now honor individual conditions starting with "AND" or "OR".  The previous behavior of using a default operator for conditions remains in place, with the one change that explicit "AND" or "OR" operators at the start of a condition will override the default operator.


Modified: trunk/Solar/Sql/Select.php
===================================================================
--- trunk/Solar/Sql/Select.php	2008-03-29 17:23:58 UTC (rev 3057)
+++ trunk/Solar/Sql/Select.php	2008-03-29 22:54:08 UTC (rev 3058)
@@ -501,39 +501,30 @@
      * 
      * Adds multiple WHERE conditions to the query.
      * 
-     * Otherwise identical to where()/orWhere().
+     * @param array $list An array of WHERE conditions.  Conditions starting
+     * with "OR" and "AND" are honored correctly.
      * 
-     * @param array $list An array of WHERE conditions.
+     * @param string $op If a condition does not explicitly start with "AND"
+     * or "OR", connect the condition with this operator.  Default "AND".
      * 
-     * @param string $op How to add the conditions, by 'AND' (the
-     * default) or by 'OR'.
-     * 
      * @return Solar_Sql_Select
      * 
-     * @see where()
+     * @see _multiWhere()
      * 
-     * @see orWhere()
-     * 
      */
     public function multiWhere($list, $op = 'AND')
     {
-        // normally use where() ...
-        $method = 'where';
-        if (strtoupper($op) == 'OR') {
-            // unless it's orWhere().
-            $method = 'orWhere';
-        }
+        $op = strtoupper(trim($op));
         
-        // add each condition.
         foreach ((array) $list as $key => $val) {
             if (is_int($key)) {
                 // integer key means a literal condition
                 // and no value to be quoted into it
-                $this->$method($val);
+                $this->_multiWhere($val, Solar_Sql_Select::IGNORE, $op);
             } else {
                 // string $key means the key is a condition,
                 // and the $val should be quoted into it.
-                $this->$method($key, $val);
+                $this->_multiWhere($key, $val, $op);
             }
         }
         
@@ -543,6 +534,41 @@
     
     /**
      * 
+     * Backend support for multiWhere().
+     * 
+     * @param string $cond The WHERE condition.
+     * 
+     * @param mixed $val A value (if any) to quote into the condition.
+     * 
+     * @param string $op The implicit operator to use for the condition, if
+     * needed.
+     * 
+     * @see where()
+     * 
+     * @see orWhere()
+     * 
+     */
+    public function _multiWhere($cond, $val, $op)
+    {
+        if (strtoupper(substr($cond, 0, 3)) == 'OR ') {
+            // explicit OR
+            $cond = substr($cond, 3);
+            $this->orWhere($cond, $val);
+        } elseif (strtoupper(substr($cond, 0, 4)) == 'AND ') {
+            // explicit AND
+            $cond = substr($cond, 4);
+            $this->where($cond, $val);
+        } elseif ($op == 'OR') {
+            // implicit OR
+            $this->orWhere($cond, $val);
+        } else {
+            // implicit AND (the default)
+            $this->where($cond, $val);
+        }
+    }
+    
+    /**
+     * 
      * Adds grouping to the query.
      * 
      * @param string|array $spec The column(s) to group by.
@@ -654,36 +680,30 @@
      * 
      * Adds multiple HAVING conditions to the query.
      * 
-     * Otherwise identical to having()/orHaving().
+     * @param array $list An array of HAVING conditions.  Conditions starting
+     * with "OR" and "AND" are honored correctly.
      * 
-     * @param array $list An array of HAVING conditions.
+     * @param string $op If a condition does not explicitly start with "AND"
+     * or "OR", connect the condition with this operator.  Default "AND".
      * 
-     * @param string $op How to add the conditions, by 'AND' (the
-     * default) or by 'OR'.
-     * 
      * @return Solar_Sql_Select
      * 
-     * @see having()
+     * @see _multiHaving()
      * 
-     * @see orHaving()
-     * 
      */
     public function multiHaving($list, $op = 'AND')
     {
-        $method = 'having';
-        if (strtoupper($op) == 'OR') {
-            $method = 'orHaving';
-        }
+        $op = strtoupper(trim($op));
         
         foreach ((array) $list as $key => $val) {
             if (is_int($key)) {
                 // integer key means a literal condition
                 // and no value to be quoted into it
-                $this->$method($val);
+                $this->_multiHaving($val, Solar_Sql_Select::IGNORE, $op);
             } else {
                 // string $key means the key is a condition,
                 // and the $val should be quoted into it.
-                $this->$method($key, $val);
+                $this->_multiHaving($key, $val, $op);
             }
         }
         
@@ -693,6 +713,41 @@
     
     /**
      * 
+     * Backend support for multiHaving().
+     * 
+     * @param string $cond The HAVING condition.
+     * 
+     * @param mixed $val A value (if any) to quote into the condition.
+     * 
+     * @param string $op The implicit operator to use for the condition, if
+     * needed.
+     * 
+     * @see having()
+     * 
+     * @see orHaving()
+     * 
+     */
+    public function _multiHaving($cond, $val, $op)
+    {
+        if (strtoupper(substr($cond, 0, 3)) == 'OR ') {
+            // explicit OR
+            $cond = substr($cond, 3);
+            $this->orHaving($cond, $val);
+        } elseif (strtoupper(substr($cond, 0, 4)) == 'AND ') {
+            // explicit AND
+            $cond = substr($cond, 4);
+            $this->having($cond, $val);
+        } elseif ($op == 'OR') {
+            // implicit OR
+            $this->orHaving($cond, $val);
+        } else {
+            // implicit AND (the default)
+            $this->having($cond, $val);
+        }
+    }
+    
+    /**
+     * 
      * Adds a row order to the query.
      * 
      * @param string|array $spec The column(s) and direction to order by.




More information about the Solar-svn mailing list