Skip to content
Permalink
Browse files

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

…this
  • Loading branch information...
lorenzo committed Mar 21, 2016
1 parent 7c650f0 commit 3158cf6fc647aa320a06c38014e394d7cdd9578c
@@ -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;
@@ -136,12 +136,18 @@ protected function _addExpressions($conditions, $values, $types)
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]);
}
}
@@ -16,6 +16,7 @@
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 @@ public function __construct($field, $value, $type, $operator)
*/
public function setValue($value)
{
if (isset($this->_type)) {
$value = $this->_castToExpression($value, $this->_type);
}
$this->_value = $value;
}
@@ -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 @@ public function add($params, $types = [], $prepend = false)
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;
@@ -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);
}
@@ -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);
}
}

0 comments on commit 3158cf6

Please sign in to comment.
You can’t perform that action at this time.