Skip to content

Commit

Permalink
Starting to implement a TupleComparison expression class, this will be
Browse files Browse the repository at this point in the history
used for eager loading associations with composite foreignKeys
  • Loading branch information
lorenzo committed Jan 11, 2014
1 parent 6d89a90 commit 923cf27
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 7 deletions.
9 changes: 3 additions & 6 deletions Cake/Database/Expression/Comparison.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)
*
Expand Down Expand Up @@ -58,18 +56,17 @@ class Comparison extends QueryExpression {
* @return void
*/
public function __construct($field, $value, $type, $conjuntion) {
$this->_field = $field;
$this->_value = $value;
$this->field($field);
$this->value($value);
$this->type($conjuntion);

if (is_string($type)) {
$this->_type = $type;
}

if (is_string($field) && isset($types[$this->_field])) {
$this->_type = current($types);
}

$this->_conditions[$field] = $value;
}

/**
Expand Down
59 changes: 59 additions & 0 deletions Cake/Database/Expression/TupleComparison.php
@@ -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));
}

}
@@ -1,6 +1,5 @@
<?php
/**
* PHP Version 5.4
*
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
Expand Down
42 changes: 42 additions & 0 deletions Test/TestCase/Database/Expression/TupleComparisonTest.php
@@ -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']);
}

}

0 comments on commit 923cf27

Please sign in to comment.