From 5cc3ef091f03a104215753e798b1f80d4310a979 Mon Sep 17 00:00:00 2001 From: Walther Lalk Date: Tue, 12 Aug 2014 11:31:35 +0200 Subject: [PATCH] Automatically set the value type for the case statement --- src/Database/Expression/CaseExpression.php | 24 +++++++++++++++++----- tests/TestCase/Database/QueryTest.php | 16 --------------- 2 files changed, 19 insertions(+), 21 deletions(-) diff --git a/src/Database/Expression/CaseExpression.php b/src/Database/Expression/CaseExpression.php index 0faa451d5e0..8e312f794df 100644 --- a/src/Database/Expression/CaseExpression.php +++ b/src/Database/Expression/CaseExpression.php @@ -113,10 +113,7 @@ protected function _addExpressions($conditions, $trueValues) { if ($trueValue === 'literal') { $trueValue = $k; } elseif (is_string($trueValue) || is_numeric($trueValue)) { - $trueValue = [ - 'value' => $trueValue, - 'type' => null - ]; + $trueValue = $this->_parseValue($trueValue); } $this->_conditions[] = $c; @@ -150,7 +147,7 @@ protected function _parseValue($value) { if (is_string($value) || is_numeric($value)) { $value = [ 'value' => $value, - 'type' => null + 'type' => is_string($value) ? null : $this->_getType($value) ]; } elseif (is_array($value) && !isset($value['value'])) { $value = array_keys($value); @@ -159,6 +156,23 @@ protected function _parseValue($value) { return $value; } +/** + * Gets the correct type for the value + * + * @param mixed $value The value to test + * + * @return null|string + */ + protected function _getType($value) { + if (is_integer($value)) { + return 'integer'; + } elseif (is_float($value)) { + return 'float'; + } + + return null; + } + /** * Compiles the relevant parts into sql * diff --git a/tests/TestCase/Database/QueryTest.php b/tests/TestCase/Database/QueryTest.php index 858ff637648..974bb3a766b 100644 --- a/tests/TestCase/Database/QueryTest.php +++ b/tests/TestCase/Database/QueryTest.php @@ -2722,8 +2722,6 @@ public function testDirectIsNull() { * @return void */ public function testSqlCaseStatement() { - $convert = $this->connection->driver() instanceof \Cake\Database\Driver\Sqlserver; - $query = new Query($this->connection); $publishedCase = $query ->newExpr() @@ -2738,20 +2736,6 @@ public function testSqlCaseStatement() { ->add(['published' => 'N']) ); - //SQLServer requires the case statements to be converted to int - if ($convert) { - $publishedCase = $query->func() - ->convert([ - 'INT' => 'literal', - $publishedCase - ]); - $notPublishedCase = $query->func() - ->convert([ - 'INT' => 'literal', - $notPublishedCase - ]); - } - $results = $query ->select([ 'published' => $query->func()->sum($publishedCase),