From adce2893850d5e6b33d82635e26096bcdddfbc42 Mon Sep 17 00:00:00 2001 From: Jose Lorenzo Rodriguez Date: Mon, 21 Mar 2016 23:27:41 +0100 Subject: [PATCH] Implmented ValueBinder::generateMany() as a way to reduce funcitons calls Also calling bindValue() directly instead of the bind() proxy for the same reaason. --- src/Database/Expression/Comparison.php | 30 ++++++++++--------- src/Database/ValueBinder.php | 21 +++++++++++-- .../Database/ExpressionTypeCastingTest.php | 2 +- 3 files changed, 35 insertions(+), 18 deletions(-) diff --git a/src/Database/Expression/Comparison.php b/src/Database/Expression/Comparison.php index 2858466a0c9..e1d9c4a3e58 100644 --- a/src/Database/Expression/Comparison.php +++ b/src/Database/Expression/Comparison.php @@ -84,7 +84,7 @@ public function __construct($field, $value, $type, $operator) */ 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 @@ public function traverse(callable $callable) $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 @@ protected function _bindValue($value, $generator, $type) * @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); diff --git a/src/Database/ValueBinder.php b/src/Database/ValueBinder.php index b7ea93a8595..6b764353f35 100644 --- a/src/Database/ValueBinder.php +++ b/src/Database/ValueBinder.php @@ -73,6 +73,23 @@ public function placeholder($token) 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 @@ public function attachTo($statement) } $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); } } diff --git a/tests/TestCase/Database/ExpressionTypeCastingTest.php b/tests/TestCase/Database/ExpressionTypeCastingTest.php index 5d404108c70..09fe8db129b 100644 --- a/tests/TestCase/Database/ExpressionTypeCastingTest.php +++ b/tests/TestCase/Database/ExpressionTypeCastingTest.php @@ -35,7 +35,7 @@ public function toExpression($value) * using the type classes * */ -class FunctionsBuilderTest extends TestCase +class ExpressionTypeCastingTest extends TestCase { /**