Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Starting to implement a TupleComparison expression class, this will be
used for eager loading associations with composite foreignKeys
- Loading branch information
Showing
4 changed files
with
104 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
<?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 CakePHP(tm) v 3.0.0 | ||
* @license MIT License (http://www.opensource.org/licenses/mit-license.php) | ||
*/ | ||
namespace Cake\Database\Expression; | ||
|
||
use Cake\Database\Expression\Compariso; | ||
use Cake\Database\ValueBinder; | ||
|
||
/** | ||
* | ||
*/ | ||
class TupleComparison extends Comparison { | ||
|
||
/** | ||
* Constructor | ||
* | ||
* @param string $field the field name to compare to a value | ||
* @param mixed $value the value to be used in comparison | ||
* @param string $type the type name used to cast the value | ||
* @param string $conjunction the operator used for comparing field and value | ||
* @return void | ||
*/ | ||
public function __construct($field, $value, $type = [], $conjuntion = '=') { | ||
parent::__construct($field, $value, $type, $conjuntion); | ||
$this->_type = (array)$type; | ||
} | ||
|
||
/** | ||
* Convert the expression into a SQL fragment. | ||
* | ||
* @param Cake\Database\ValueBinder $generator Placeholder generator object | ||
* @return string | ||
*/ | ||
public function sql(ValueBinder $generator) { | ||
$template = '(%s) %s (%s)'; | ||
$values = []; | ||
foreach ($this->getValue() as $i => $value) { | ||
$values[] = $this->_bindValue( | ||
$generator, | ||
$value, | ||
isset($this->_type[$i]) ? $this->_type[$i] : null | ||
); | ||
} | ||
$field = implode(', ', (array)$this->getField()); | ||
return sprintf($template, $field, $this->_conjunction, implode(', ', $values)); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
<?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 CakePHP(tm) v 3.0.0 | ||
* @license MIT License (http://www.opensource.org/licenses/mit-license.php) | ||
*/ | ||
namespace Cake\Test\TestCase\Database\Expression; | ||
|
||
use Cake\Database\Expression\TupleComparison; | ||
use Cake\Database\ValueBinder; | ||
use Cake\TestSuite\TestCase; | ||
|
||
/** | ||
* Tests TupleComparison class | ||
* | ||
**/ | ||
class TupleComparisonTest extends TestCase { | ||
|
||
/** | ||
* Tests generating a function with no arguments | ||
* | ||
* @return void | ||
*/ | ||
public function testsSimpleTuple() { | ||
$f = new TupleComparison(['field1', 'field2'], [1, 2], ['integer', 'integer'], '='); | ||
$binder = new ValueBinder; | ||
$this->assertEquals('(field1, field2) = (:c0, :c1)', $f->sql($binder)); | ||
$this->assertSame(1, $binder->bindings()[':c0']['value']); | ||
$this->assertSame(2, $binder->bindings()[':c1']['value']); | ||
$this->assertSame('integer', $binder->bindings()[':c0']['type']); | ||
$this->assertSame('integer', $binder->bindings()[':c1']['type']); | ||
} | ||
|
||
} |