Skip to content

Commit

Permalink
Refactoring for method consistency, made bind() also return the instance
Browse files Browse the repository at this point in the history
  • Loading branch information
lorenzo committed Jan 6, 2013
1 parent 3250af2 commit 571ff61
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 23 deletions.
58 changes: 37 additions & 21 deletions lib/Cake/Model/Datasource/Database/Expression/QueryExpression.php
Expand Up @@ -126,7 +126,18 @@ public function not($conditions, $types = []) {
* to database
* @return string placeholder name or question mark to be used in the query string
*/
public function bind($token, $value, $type) {
public function bind($param, $value, $type) {
$number = $this->_bindingsCount;
$this->_bindings[$number] = compact('value', 'type') + [
'placeholder' => substr($param, 1)
];
if (strpos($type, '[]') !== false) {
$this->_replaceArrayParams = true;
}
return $this;
}

public function placeholder($token) {
$param = $token;
$number = $this->_bindingsCount++;

Expand All @@ -136,14 +147,6 @@ public function bind($token, $value, $type) {
$param = sprintf(':c%s%s', $this->_identifier, $number);
}

if (strpos($type, '[]') !== false) {
$param = sprintf(':array%d', $number);
$type = str_replace('[]', '', $type);
}

$this->_bindings[$number] = compact('value', 'type', 'token') + [
'placeholder' => substr($param, 1)
];
return $param;
}

Expand Down Expand Up @@ -235,27 +238,40 @@ protected function _parseCondition($field, $value, $types) {
$type = $type ?: 'string';
$type .= strpos($type, '[]') === false ? '[]' : null;
$template = '%s %s (%s)';
$this->_replaceArrayParams = true;
}

return sprintf($template, $expression, $operator, $this->bind($field, $value, $type));
return sprintf($template, $expression, $operator, $this->_bindValue($field, $value, $type));
}

protected function _bindValue($field, $value, $type) {
$param = $this->placeholder($field);
$this->bind($param, $value, $type);
return $param;
}

protected function _replaceArrays() {
$replacements = [];
foreach ($this->_bindings as $n => $b) {
if (strpos($b['type'], '[]') === false) {
continue;
}
$type = str_replace('[]', '', $b['type']);
$params = [];
foreach ($b['value'] as $value) {
$params[] = $this->_bindValue($b['placeholder'], $value, $type);
}
$token = ':' . $b['placeholder'];
$replacements[$token] = implode(', ', $params);
unset($this->_bindings[$n]);
}

foreach ($this->_conditions as $k => $condition) {
if (!is_string($condition)) {
continue;
}
$condition = preg_replace_callback('/(:array(\d+))/', function($match) {
$params = [];
$binding = $this->_bindings[$match[2]];
foreach ($this->_bindings[$match[2]]['value'] as $value) {
$params[] = $this->bind($binding['token'], $value, $binding['type']);
}
unset($this->_bindings[$match[2]]);
return implode(', ', $params);
}, $condition);
$this->_conditions[$k] = $condition;
foreach ($replacements as $token => $r) {
$this->_conditions[$k] = str_replace($token, $r, $condition);
}
}
}

Expand Down
Expand Up @@ -981,8 +981,9 @@ public function testSelectOrderBy() {
$this->assertEquals(['id' => 5], $result->fetch('assoc'));
$this->assertEquals(['id' => 3], $result->fetch('assoc'));

$expression = $query->newExpr()->add(['(id + :offset) % 2 = 0']);
$expression->bind(':offset', 1, null);
$expression = $query->newExpr()
->add(['(id + :offset) % 2 = 0'])
->bind(':offset', 1, null);
$result = $query->order([$expression, 'id' => 'desc'], true)->execute();
$this->assertEquals(['id' => 4], $result->fetch('assoc'));
$this->assertEquals(['id' => 2], $result->fetch('assoc'));
Expand Down

0 comments on commit 571ff61

Please sign in to comment.