Skip to content

Commit

Permalink
Implmented ValueBinder::generateMany() as a way to reduce funcitons c…
Browse files Browse the repository at this point in the history
…alls

Also calling bindValue() directly instead of the bind() proxy for the same
reaason.
  • Loading branch information
lorenzo committed Mar 26, 2016
1 parent 98a5267 commit adce289
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 18 deletions.
30 changes: 16 additions & 14 deletions src/Database/Expression/Comparison.php
Expand Up @@ -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) {
Expand Down Expand Up @@ -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);
}
}

Expand Down Expand Up @@ -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);
Expand Down
21 changes: 18 additions & 3 deletions src/Database/ValueBinder.php
Expand Up @@ -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.
Expand Down Expand Up @@ -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);
}
}
2 changes: 1 addition & 1 deletion tests/TestCase/Database/ExpressionTypeCastingTest.php
Expand Up @@ -35,7 +35,7 @@ public function toExpression($value)
* using the type classes
*
*/
class FunctionsBuilderTest extends TestCase
class ExpressionTypeCastingTest extends TestCase
{

/**
Expand Down

0 comments on commit adce289

Please sign in to comment.