Skip to content

Commit

Permalink
Starting to convert values to expressions when the type class offers …
Browse files Browse the repository at this point in the history
…this
  • Loading branch information
lorenzo committed Mar 26, 2016
1 parent 7c650f0 commit 3158cf6
Show file tree
Hide file tree
Showing 6 changed files with 116 additions and 6 deletions.
7 changes: 7 additions & 0 deletions src/Database/Expression/BetweenExpression.php
Expand Up @@ -15,6 +15,7 @@
namespace Cake\Database\Expression;

use Cake\Database\ExpressionInterface;
use Cake\Database\Type\TypeExpressionCasterTrait;
use Cake\Database\ValueBinder;

/**
Expand All @@ -26,6 +27,7 @@ class BetweenExpression implements ExpressionInterface, FieldInterface
{

use FieldTrait;
use TypeExpressionCasterTrait;

/**
* The first value in the expression
Expand Down Expand Up @@ -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;
Expand Down
8 changes: 7 additions & 1 deletion src/Database/Expression/CaseExpression.php
Expand Up @@ -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]);
}
}
Expand Down
14 changes: 10 additions & 4 deletions src/Database/Expression/Comparison.php
Expand Up @@ -16,6 +16,7 @@

use Cake\Database\Exception as DatabaseException;
use Cake\Database\ExpressionInterface;
use Cake\Database\Type\TypeExpressionCasterTrait;
use Cake\Database\ValueBinder;

/**
Expand All @@ -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
Expand Down Expand Up @@ -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;
}

/**
Expand All @@ -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;
}

Expand Down
11 changes: 10 additions & 1 deletion src/Database/Expression/FunctionExpression.php
Expand Up @@ -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;
Expand All @@ -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
Expand Down Expand Up @@ -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;
Expand Down
34 changes: 34 additions & 0 deletions 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 changes: 48 additions & 0 deletions 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);
}
}

0 comments on commit 3158cf6

Please sign in to comment.