Browse Source

Starting to convert values to expressions when the type class offers this

Jose Lorenzo Rodriguez 10 years ago
parent
commit
3158cf6fc6

+ 7 - 0
src/Database/Expression/BetweenExpression.php

@@ -15,6 +15,7 @@
 namespace Cake\Database\Expression;
 
 use Cake\Database\ExpressionInterface;
+use Cake\Database\Type\TypeExpressionCasterTrait;
 use Cake\Database\ValueBinder;
 
 /**
@@ -26,6 +27,7 @@ class BetweenExpression implements ExpressionInterface, FieldInterface
 {
 
     use FieldTrait;
+    use TypeExpressionCasterTrait;
 
     /**
      * The first value in the expression
@@ -58,6 +60,11 @@ class BetweenExpression implements ExpressionInterface, FieldInterface
      */
     public function __construct($field, $from, $to, $type = null)
     {
+        if ($type !== null) {
+            $from = $this->_castToExpression($from, $type);
+            $to = $this->_castToExpression($to, $type);
+        }
+
         $this->_field = $field;
         $this->_from = $from;
         $this->_to = $to;

+ 7 - 1
src/Database/Expression/CaseExpression.php

@@ -136,12 +136,18 @@ class CaseExpression implements ExpressionInterface
                 array_push($this->_values, $value);
                 continue;
             }
+
+            $type = isset($types[$k]) ? $types[$k] : null;
+
+            if ($type !== null && !$value instanceof ExpressionInterface) {
+                $value = $this->_castToExpression($value, $type);
+            }
+
             if ($value instanceof ExpressionInterface) {
                 array_push($this->_values, $value);
                 continue;
             }
 
-            $type = isset($types[$k]) ? $types[$k] : null;
             array_push($this->_values, ['value' => $value, 'type' => $type]);
         }
     }

+ 10 - 4
src/Database/Expression/Comparison.php

@@ -16,6 +16,7 @@ namespace Cake\Database\Expression;
 
 use Cake\Database\Exception as DatabaseException;
 use Cake\Database\ExpressionInterface;
+use Cake\Database\Type\TypeExpressionCasterTrait;
 use Cake\Database\ValueBinder;
 
 /**
@@ -29,6 +30,7 @@ class Comparison implements ExpressionInterface, FieldInterface
 {
 
     use FieldTrait;
+    use TypeExpressionCasterTrait;
 
     /**
      * The value to be used in the right hand side of the operation
@@ -61,13 +63,13 @@ class Comparison implements ExpressionInterface, FieldInterface
      */
     public function __construct($field, $value, $type, $operator)
     {
-        $this->setField($field);
-        $this->setValue($value);
-        $this->_operator = $operator;
-
         if (is_string($type)) {
             $this->_type = $type;
         }
+
+        $this->setField($field);
+        $this->setValue($value);
+        $this->_operator = $operator;
     }
 
     /**
@@ -78,6 +80,10 @@ class Comparison implements ExpressionInterface, FieldInterface
      */
     public function setValue($value)
     {
+        if (isset($this->_type)) {
+            $value = $this->_castToExpression($value, $this->_type);
+        }
+
         $this->_value = $value;
     }
 

+ 10 - 1
src/Database/Expression/FunctionExpression.php

@@ -15,6 +15,7 @@
 namespace Cake\Database\Expression;
 
 use Cake\Database\ExpressionInterface;
+use Cake\Database\Type\TypeExpressionCasterTrait;
 use Cake\Database\TypedResultInterface;
 use Cake\Database\TypedResultTrait;
 use Cake\Database\ValueBinder;
@@ -31,6 +32,7 @@ class FunctionExpression extends QueryExpression implements TypedResultInterface
 {
 
     use TypedResultTrait;
+    use TypeExpressionCasterTrait;
 
     /**
      * The name of the function to be constructed when generating the SQL string
@@ -113,11 +115,18 @@ class FunctionExpression extends QueryExpression implements TypedResultInterface
                 continue;
             }
 
+            $type = $typeMap->type($k);
+
+            if ($type !== null && !$p instanceof ExpressionInterface) {
+                $p = $this->_castToExpression($p, $type);
+            }
+
             if ($p instanceof ExpressionInterface) {
                 $put($this->_conditions, $p);
                 continue;
             }
-            $put($this->_conditions, ['value' => $p, 'type' => $typeMap->type($k)]);
+
+            $put($this->_conditions, ['value' => $p, 'type' => $type]);
         }
 
         return $this;

+ 34 - 0
src/Database/Type/ExpressionTypeInterface.php

@@ -0,0 +1,34 @@
+<?php
+/**
+ * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
+ * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
+ *
+ * Licensed under The MIT License
+ * For full copyright and license information, please see the LICENSE.txt
+ * Redistributions of files must retain the above copyright notice.
+ *
+ * @copyright     Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
+ * @link          http://cakephp.org CakePHP(tm) Project
+ * @since         3.3.0
+ * @license       http://www.opensource.org/licenses/mit-license.php MIT License
+ */
+namespace Cake\Database\Type;
+
+use Cake\Database\Driver;
+
+/**
+ * An interface used by Type objects to signal whether the value should
+ * be converted to an ExpressionInterface instead of a string when sent
+ * to the database.
+ */
+interface ExpressionTypeInterface
+{
+
+    /**
+     * Returns an ExpressionInterface object for the given value that can
+     * be used in queries.
+     *
+     * @return \Cake\Database\ExpressionInterface
+     */
+    public function toExpression($value, Driver $driver);
+}

+ 48 - 0
src/Database/Type/TypeExpressionCasterTrait.php

@@ -0,0 +1,48 @@
+<?php
+/**
+ * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
+ * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
+ *
+ * Licensed under The MIT License
+ * For full copyright and license information, please see the LICENSE.txt
+ * Redistributions of files must retain the above copyright notice.
+ *
+ * @copyright     Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
+ * @link          http://cakephp.org CakePHP(tm) Project
+ * @since         3.3.0
+ * @license       http://www.opensource.org/licenses/mit-license.php MIT License
+ */
+namespace Cake\Database\Type;
+
+use Cake\Database\Type;
+use Cake\Database\Type\ExpressionTypeInterface;
+
+/**
+ *
+ * @internal
+ */
+trait TypeExpressionCasterTrait
+{
+
+    protected function _castToExpression($value, $type)
+    {
+        return $value;
+        $baseType = str_replace('[]', '', $type);
+        $multi = $type !== $baseType;
+        $converter = Type::build($baseType);
+
+        if (!$converter instanceof ExpressionTypeInterface) {
+            return $value;
+        }
+
+        if ($multi) {
+            $result = [];
+            foreach ($value as $k => $v) {
+                $result[$k] = $converter->toExpression($v);
+            }
+            return $result;
+        }
+
+        return $converter->toExpression($value);
+    }
+}