Skip to content
Permalink
Browse files

Fix value where `literal` values did not work with case statements

  • Loading branch information...
dakota committed Aug 7, 2015
1 parent 8f09723 commit 9e644b43c264cacea6ec08fba2f3568b9c485a9e
@@ -111,6 +111,8 @@ public function add($conditions = [], $values = [], $types = [])
*/
protected function _addExpressions($conditions, $values, $types)
{
$rawValues = array_values($values);
$keyValues = array_keys($values);
foreach ($conditions as $k => $c) {
$numericKey = is_numeric($k);
@@ -123,10 +125,10 @@ protected function _addExpressions($conditions, $values, $types)
}
array_push($this->_conditions, $c);
$value = !empty($values[$k]) ? $values[$k] : 1;
$value = !empty($rawValues[$k]) ? $rawValues[$k] : 1;
if ($value === 'literal') {
$value = $k;
$value = $keyValues[$k];
array_push($this->_values, $value);
continue;
} elseif ($value instanceof ExpressionInterface) {
@@ -33,19 +33,24 @@ public function testSqlOutput()
{
$expr = new QueryExpression();
$expr->eq('test', 'true');
$expr2 = new QueryExpression();
$expr2->eq('test2', 'false');
$caseExpression = new CaseExpression($expr, 'foobar');
$expected = 'CASE WHEN test = :c0 THEN :c1 END';
$this->assertSame($expected, $caseExpression->sql(new ValueBinder()));
$expr2 = new QueryExpression();
$expr2->eq('test2', 'false');
$caseExpression->add($expr2);
$expected = 'CASE WHEN test = :c0 THEN :c1 WHEN test2 = :c2 THEN :c3 END';
$this->assertSame($expected, $caseExpression->sql(new ValueBinder()));
$caseExpression = new CaseExpression([$expr], ['foobar', 'else']);
$expected = 'CASE WHEN test = :c0 THEN :c1 ELSE :c2 END';
$this->assertSame($expected, $caseExpression->sql(new ValueBinder()));
$caseExpression = new CaseExpression([$expr], ['foobar' => 'literal', 'else']);
$expected = 'CASE WHEN test = :c0 THEN foobar ELSE :c1 END';
$this->assertSame($expected, $caseExpression->sql(new ValueBinder()));
}
/**

0 comments on commit 9e644b4

Please sign in to comment.
You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.