Browse Source

Implmented ValueBinder::generateMany() as a way to reduce funcitons calls
Also calling bindValue() directly instead of the bind() proxy for the same
reaason.

Jose Lorenzo Rodriguez 10 years ago
parent
commit
adce289385

+ 16 - 14
src/Database/Expression/Comparison.php

@@ -84,7 +84,7 @@ class Comparison implements ExpressionInterface, FieldInterface
      */
     public function setValue($value)
     {
-        $hasType = isset($this->_type);
+        $hasType = isset($this->_type) && is_string($this->_type);
         $isMultiple = $hasType && strpos($this->_type, '[]') !== false;
 
         if ($hasType) {
@@ -170,11 +170,9 @@ class Comparison implements ExpressionInterface, FieldInterface
             $this->_value->traverse($callable);
         }
 
-        if (!empty($this->_valueExpressions)) {
-            foreach ($this->_valueExpressions as $v) {
-                $callable($v);
-                $v->traverse($callable);
-            }
+        foreach ($this->_valueExpressions as $v) {
+            $callable($v);
+            $v->traverse($callable);
         }
     }
 
@@ -254,15 +252,19 @@ class Comparison implements ExpressionInterface, FieldInterface
      * @param string|array|null $type the type to cast values to
      * @return string
      */
-    protected function _flattenValue($value, $generator, $type = null)
+    protected function _flattenValue($value, $generator, $type = 'string')
     {
-        $parts = [];
-        foreach ($value as $k => $v) {
-            if (isset($this->_valueExpressions[$k])) {
-                $parts[] = $this->_valueExpressions[$k]->sql($generator);
-                continue;
-            }
-            $parts[] = $this->_bindValue($v, $generator, $type);
+        $expressions = [];
+
+        foreach ($this->_valueExpressions as $k => $v) {
+            $expressions[$k] = $v->sql($generator);
+            unset($value[$k]);
+        }
+
+        $parts = $expressions;
+
+        if (!empty($value)) {
+            $parts += $generator->generateManyNamed($value, $type);
         }
 
         return implode(',', $parts);

+ 18 - 3
src/Database/ValueBinder.php

@@ -73,6 +73,23 @@ class ValueBinder
         return $token;
     }
 
+    public function generateManyNamed($values, $type = 'string')
+    {
+        $placeholders = [];
+        foreach ($values as $k => $value) {
+            $param = ":c" . $this->_bindingsCount;
+            $this->_bindings[$param] = [
+                'value' => $value,
+                'type' => $type,
+                'placeholder' => $param
+            ];
+            $placeholders[$k] = $param;
+            $this->_bindingsCount++;
+        }
+
+        return $placeholders;
+    }
+
     /**
      * Returns all values bound to this expression object at this nesting level.
      * Subexpression bound values will not be returned with this function.
@@ -119,9 +136,7 @@ class ValueBinder
         }
         $params = $types = [];
         foreach ($bindings as $b) {
-            $params[$b['placeholder']] = $b['value'];
-            $types[$b['placeholder']] = $b['type'];
+            $statement->bindValue($b['placeholder'], $b['value'], $b['type']);
         }
-        $statement->bind($params, $types);
     }
 }

+ 1 - 1
tests/TestCase/Database/ExpressionTypeCastingTest.php

@@ -35,7 +35,7 @@ class TestType extends StringType implements ExpressionTypeInterface
  * using the type classes
  *
  */
-class FunctionsBuilderTest extends TestCase
+class ExpressionTypeCastingTest extends TestCase
 {
 
     /**