Browse Source

Fixing function expressions for SQLServer
Users will have to make a tradeoff here, as it was not possible to make
SQLServer work the same as the other drivers. The main difference is
that we now force UTC dates in some functions and the second difference
is that arguments will need to be reversed for DATEDIFF when using
SQLServer

Jose Lorenzo Rodriguez 12 years ago
parent
commit
40a430083b

+ 5 - 12
src/Database/Dialect/SqlserverDialectTrait.php

@@ -14,6 +14,7 @@
  */
 namespace Cake\Database\Dialect;
 
+use Cake\Database\Expression\FunctionExpression;
 use Cake\Database\SqlDialectTrait;
 
 /**
@@ -107,27 +108,19 @@ trait SqlserverDialectTrait {
 				// CONCAT function is expressed as exp1 + exp2
 				$expression->name('')->type(' +');
 				break;
-/*
-			@todo Implement prepend method on FunctionExpression
 			case 'DATEDIFF':
-				$expression
-					->name('')
-					->type('-')
-					->iterateParts(function($p) {
-						return new FunctionExpression('DATE', [$p['value']], [$p['type']]);
-					});
+				$expression->add(['day' => 'literal'], [], true);
 				break;
-*/
 			case 'CURRENT_DATE':
-				$time = new FunctionExpression('GETDATE');
+				$time = new FunctionExpression('GETUTCDATE');
 				$expression->name('CONVERT')->add(['date' => 'literal', $time]);
 				break;
 			case 'CURRENT_TIME':
-				$time = new FunctionExpression('GETDATE');
+				$time = new FunctionExpression('GETUTCDATE');
 				$expression->name('CONVERT')->add(['time' => 'literal', $time]);
 				break;
 			case 'NOW':
-				$expression->name('GETDATE');
+				$expression->name('GETUTCDATE');
 				break;
 		}
 	}

+ 6 - 4
src/Database/Expression/FunctionExpression.php

@@ -83,23 +83,25 @@ class FunctionExpression extends QueryExpression {
  * If associative the key would be used as argument when value is 'literal'
  * @param array $types associative array of types to be associated with the
  * passed arguments
+ * @param boolean $prepend Whehter to prepend or append to the list of arguments
  * @see FunctionExpression::__construct() for more details.
  * @return FunctionExpression
  */
-	public function add($params, $types = []) {
+	public function add($params, $types = [], $prepend = false) {
+		$put = $prepend ? 'array_unshift' : 'array_push';
 		foreach ($params as $k => $p) {
 			if (is_string($k) && $p === 'literal') {
-				$this->_conditions[] = $k;
+				$put($this->_conditions, $k);
 				continue;
 			}
 
 			if ($p instanceof ExpressionInterface) {
-				$this->_conditions[] = $p;
+				$put($this->_conditions, $p);
 				continue;
 			}
 
 			$type = isset($types[$k]) ? $types[$k] : null;
-			$this->_conditions[] = ['value' => $p, 'type' => $type];
+			$put($this->_conditions, ['value' => $p, 'type' => $type]);
 		}
 
 		return $this;

+ 0 - 2
src/Database/Schema/SqlserverSchema.php

@@ -1,7 +1,5 @@
 <?php
 /**
- * PHP Version 5.4
- *
  * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  *

+ 3 - 2
tests/TestCase/Database/QueryTest.php

@@ -2105,8 +2105,9 @@ class QueryTest extends TestCase {
 		$query = new Query($this->connection);
 		$result = $query
 			->select(['d' => $query->func()->dateDiff(['2012-01-05', '2012-01-02'])])
-			->execute();
-		$this->assertEquals([['d' => '3.0']], $result->fetchAll('assoc'));
+			->execute()
+			->fetchAll('assoc');
+		$this->assertEquals(3, abs($result[0]['d']));
 
 		$query = new Query($this->connection);
 		$result = $query