Skip to content

Commit

Permalink
Added identiferEquals method for expressions to use fields with quoti…
Browse files Browse the repository at this point in the history
…ng in join conditions. Added identifier parameter for expressions.
  • Loading branch information
skie committed Feb 11, 2016
1 parent 107c1ca commit 5ede142
Show file tree
Hide file tree
Showing 8 changed files with 56 additions and 28 deletions.
4 changes: 4 additions & 0 deletions src/Database/Expression/CaseExpression.php
Expand Up @@ -130,6 +130,10 @@ protected function _addExpressions($conditions, $values, $types)
$value = $keyValues[$k];
array_push($this->_values, $value);
continue;
} elseif ($value === 'identifier') {
$value = new IdentifierExpression($keyValues[$k]);
array_push($this->_values, $value);
continue;
} elseif ($value instanceof ExpressionInterface) {
array_push($this->_values, $value);
continue;
Expand Down
5 changes: 5 additions & 0 deletions src/Database/Expression/FunctionExpression.php
Expand Up @@ -108,6 +108,11 @@ public function add($params, $types = [], $prepend = false)
continue;
}

if ($p === 'identifier') {
$put($this->_conditions, new IdentifierExpression($k));
continue;
}

if ($p instanceof ExpressionInterface) {
$put($this->_conditions, $p);
continue;
Expand Down
12 changes: 12 additions & 0 deletions src/Database/Expression/QueryExpression.php
Expand Up @@ -438,6 +438,18 @@ public function count()
return count($this->_conditions);
}

/**
* Builds equal condition or assignment with identifier wrapping.
*
* @param string $left Left join condition field name.
* @param string $right Right join condition field name.
* @return $this
*/
public function equalFields($left, $right)
{
return $this->eq(new IdentifierExpression($left), new IdentifierExpression($right));
}

/**
* Returns the string representation of this object so that it can be used in a
* SQL query. Note that values condition values are not included in the string,
Expand Down
40 changes: 20 additions & 20 deletions tests/TestCase/Database/QueryTest.php
Expand Up @@ -196,7 +196,7 @@ public function testSelectWithJoins()
$result = $query
->select(['title', 'name'])
->from('articles')
->join(['table' => 'authors', 'alias' => 'a', 'conditions' => 'author_id = a.id'])
->join(['table' => 'authors', 'alias' => 'a', 'conditions' => $query->newExpr()->equalFields('author_id', 'a.id')])
->order(['title' => 'asc'])
->execute();

Expand All @@ -208,7 +208,7 @@ public function testSelectWithJoins()
$this->assertCount(12, $result, 'Cross join results in 12 records');

$result = $query->join([
['table' => 'authors', 'type' => 'INNER', 'conditions' => 'author_id = authors.id']
['table' => 'authors', 'type' => 'INNER', 'conditions' => $query->newExpr()->equalFields('author_id', 'authors.id')]
], [], true)->execute();
$this->assertCount(3, $result);
$this->assertEquals(['title' => 'First Article', 'name' => 'mariano'], $result->fetch('assoc'));
Expand All @@ -226,14 +226,14 @@ public function testSelectWithJoinsConditions()
$result = $query
->select(['title', 'name'])
->from('articles')
->join(['table' => 'authors', 'alias' => 'a', 'conditions' => ['author_id = a.id']])
->join(['table' => 'authors', 'alias' => 'a', 'conditions' => [$query->newExpr()->equalFields('author_id ', 'a.id')]])
->order(['title' => 'asc'])
->execute();
$this->assertEquals(['title' => 'First Article', 'name' => 'mariano'], $result->fetch('assoc'));
$this->assertEquals(['title' => 'Second Article', 'name' => 'larry'], $result->fetch('assoc'));

$query = new Query($this->connection);
$conditions = $query->newExpr('author_id = a.id');
$conditions = $query->newExpr()->equalFields('author_id', 'a.id');
$result = $query
->select(['title', 'name'])
->from('articles')
Expand Down Expand Up @@ -1296,7 +1296,7 @@ public function testWhereWithBetweenWithExpressionField()
->select(['id'])
->from('comments')
->where(function ($exp, $q) {
$field = $q->newExpr('COALESCE(id, 1)');
$field = $q->func()->coalesce([new IdentifierExpression('id'), 1 => 'literal']);
return $exp->between($field, 5, 6, 'integer');
})
->execute();
Expand Down Expand Up @@ -1560,7 +1560,7 @@ public function testSelectOrderAsc()
$query = new Query($this->connection);
$query->select(['id'])
->from('articles')
->orderAsc($query->func()->concat(['id' => 'literal', '3']));
->orderAsc($query->func()->concat(['id' => 'identifier', '3']));

$result = $query->execute()->fetchAll('assoc');
$expected = [
Expand Down Expand Up @@ -1599,7 +1599,7 @@ public function testSelectOrderDesc()
$query = new Query($this->connection);
$query->select(['id'])
->from('articles')
->orderDesc($query->func()->concat(['id' => 'literal', '3']));
->orderDesc($query->func()->concat(['id' => 'identifier', '3']));

$result = $query->execute()->fetchAll('assoc');
$expected = [
Expand Down Expand Up @@ -1767,7 +1767,7 @@ public function testSelectHaving()
$result = $query
->select(['total' => 'count(author_id)', 'author_id'])
->from('articles')
->join(['table' => 'authors', 'alias' => 'a', 'conditions' => 'author_id = a.id'])
->join(['table' => 'authors', 'alias' => 'a', 'conditions' => $query->newExpr()->equalFields('author_id', 'a.id')])
->group('author_id')
->having(['count(author_id) <' => 2], ['count(author_id)' => 'integer'])
->execute();
Expand Down Expand Up @@ -1799,7 +1799,7 @@ public function testSelectOrHaving()
$result = $query
->select(['total' => 'count(author_id)', 'author_id'])
->from('articles')
->join(['table' => 'authors', 'alias' => 'a', 'conditions' => 'author_id = a.id'])
->join(['table' => 'authors', 'alias' => 'a', 'conditions' => $query->newExpr()->equalFields('author_id', 'a.id')])
->group('author_id')
->having(['count(author_id) >' => 2], ['count(author_id)' => 'integer'])
->orHaving(['count(author_id) <' => 2], ['count(author_id)' => 'integer'])
Expand All @@ -1811,7 +1811,7 @@ public function testSelectOrHaving()
$result = $query
->select(['total' => 'count(author_id)', 'author_id'])
->from('articles')
->join(['table' => 'authors', 'alias' => 'a', 'conditions' => 'author_id = a.id'])
->join(['table' => 'authors', 'alias' => 'a', 'conditions' => $query->newExpr()->equalFields('author_id', 'a.id')])
->group('author_id')
->having(['count(author_id) >' => 2], ['count(author_id)' => 'integer'])
->orHaving(['count(author_id) <=' => 2], ['count(author_id)' => 'integer'])
Expand All @@ -1823,7 +1823,7 @@ public function testSelectOrHaving()
$result = $query
->select(['total' => 'count(author_id)', 'author_id'])
->from('articles')
->join(['table' => 'authors', 'alias' => 'a', 'conditions' => 'author_id = a.id'])
->join(['table' => 'authors', 'alias' => 'a', 'conditions' => $query->newExpr()->equalFields('author_id', 'a.id')])
->group('author_id')
->having(['count(author_id) >' => 2], ['count(author_id)' => 'integer'])
->orHaving(function ($e) {
Expand All @@ -1846,7 +1846,7 @@ public function testSelectAndHaving()
$result = $query
->select(['total' => 'count(author_id)', 'author_id'])
->from('articles')
->join(['table' => 'authors', 'alias' => 'a', 'conditions' => 'author_id = a.id'])
->join(['table' => 'authors', 'alias' => 'a', 'conditions' => $query->newExpr()->equalFields('author_id', 'a.id')])
->group('author_id')
->having(['count(author_id) >' => 2], ['count(author_id)' => 'integer'])
->andHaving(['count(author_id) <' => 2], ['count(author_id)' => 'integer'])
Expand All @@ -1857,7 +1857,7 @@ public function testSelectAndHaving()
$result = $query
->select(['total' => 'count(author_id)', 'author_id'])
->from('articles')
->join(['table' => 'authors', 'alias' => 'a', 'conditions' => 'author_id = a.id'])
->join(['table' => 'authors', 'alias' => 'a', 'conditions' => $query->newExpr()->equalFields('author_id', 'a.id')])
->group('author_id')
->having(['count(author_id)' => 2], ['count(author_id)' => 'integer'])
->andHaving(['count(author_id) >' => 1], ['count(author_id)' => 'integer'])
Expand All @@ -1869,7 +1869,7 @@ public function testSelectAndHaving()
$result = $query
->select(['total' => 'count(author_id)', 'author_id'])
->from('articles')
->join(['table' => 'authors', 'alias' => 'a', 'conditions' => 'author_id = a.id'])
->join(['table' => 'authors', 'alias' => 'a', 'conditions' => $query->newExpr()->equalFields('author_id', 'a.id')])
->group('author_id')
->andHaving(function ($e) {
return $e->add('count(author_id) = 2 - 1');
Expand Down Expand Up @@ -2005,7 +2005,7 @@ public function testSubqueryInSelect()
$subquery = (new Query($this->connection))
->select('name')
->from(['b' => 'authors'])
->where(['b.id = a.id']);
->where([$query->newExpr()->equalFields('b.id', 'a.id')]);
$result = $query
->select(['id', 'name' => $subquery])
->from(['a' => 'comments'])->execute();
Expand Down Expand Up @@ -2113,7 +2113,7 @@ public function testSubqueryInWhere()
*
* @return void
*/
public function testSubqueyInJoin()
public function testSubqueryInJoin()
{
$subquery = (new Query($this->connection))->select('*')->from('authors');

Expand All @@ -2129,7 +2129,7 @@ public function testSubqueyInJoin()
$result = $query->execute();
$this->assertCount(3, $result);

$query->join(['b' => ['table' => $subquery, 'conditions' => ['b.id = articles.id']]], [], true);
$query->join(['b' => ['table' => $subquery, 'conditions' => [$query->newExpr()->equalFields('b.id', 'articles.id')]]], [], true);
$result = $query->execute();
$this->assertCount(1, $result);
}
Expand Down Expand Up @@ -2437,15 +2437,15 @@ public function testUpdateWithExpression()
{
$query = new Query($this->connection);

$expr = $query->newExpr('title = author_id');
$expr = $query->newExpr()->equalFields('title', 'author_id');

$query->update('articles')
->set($expr)
->where(['id' => 1]);
$result = $query->sql();

$this->assertQuotedQuery(
'UPDATE <articles> SET title = author_id WHERE <id> = :',
'UPDATE <articles> SET <title> = \(<author_id>\) WHERE <id> = :',
$result,
!$this->autoQuote
);
Expand Down Expand Up @@ -2623,7 +2623,7 @@ public function testInsertSparseRow()
'published' => 'N',
]
];
$this->assertTable('articles', 1, $expected, ['id >= 4']);
$this->assertTable('articles', 1, $expected, ['id >=' => 4]);
}

/**
Expand Down
8 changes: 7 additions & 1 deletion tests/TestCase/Database/Schema/TableTest.php
Expand Up @@ -39,7 +39,13 @@ public function getBaseType()
class TableTest extends TestCase
{

public $fixtures = ['core.articles_tags', 'core.orders', 'core.products', 'core.tags'];
public $fixtures = [
'core.articles',
'core.tags',
'core.articles_tags',
'core.orders',
'core.products'
];

protected $_map;

Expand Down
4 changes: 2 additions & 2 deletions tests/TestCase/ORM/QueryRegressionTest.php
Expand Up @@ -35,13 +35,13 @@ class QueryRegressionTest extends TestCase
*/
public $fixtures = [
'core.articles',
'core.tags',
'core.articles_tags',
'core.authors',
'core.authors_tags',
'core.comments',
'core.featured_tags',
'core.special_tags',
'core.tags',
'core.tags_translations',
'core.translates',
'core.users'
Expand Down Expand Up @@ -978,7 +978,7 @@ public function testTypemapInFunctions()
$query->select([
'id',
'coalesced' => $query->func()->coalesce(
['published' => 'literal', -1],
['published' => 'identifier', -1],
['integer']
)
]);
Expand Down
9 changes: 5 additions & 4 deletions tests/TestCase/ORM/QueryTest.php
Expand Up @@ -1553,7 +1553,7 @@ public function testCountWithExpressions()
$query = $table->find();
$query->select([
'title' => $query->func()->concat(
['title' => 'literal', 'test'],
['title' => 'identifier', 'test'],
['string']
),
]);
Expand Down Expand Up @@ -2214,7 +2214,7 @@ public function testFormatBelongsToRecords()
*
* @return void
*/
public function testFormatDeepAssocationRecords()
public function testFormatDeepAssociationRecords()
{
$table = TableRegistry::get('ArticlesTags');
$table->belongsTo('Articles');
Expand Down Expand Up @@ -2479,12 +2479,13 @@ public function testEagerLoaded()
public function testColumnsFromJoin()
{
$table = TableRegistry::get('articles');
$results = $table->find()
$query = $table->find();
$results = $query
->select(['title', 'person.name'])
->join([
'person' => [
'table' => 'authors',
'conditions' => ['person.id = articles.author_id']
'conditions' => [$query->newExpr()->equalFields('person.id', 'articles.author_id')]
]
])
->order(['articles.id' => 'ASC'])
Expand Down
2 changes: 1 addition & 1 deletion tests/TestCase/ORM/TableTest.php
Expand Up @@ -53,6 +53,7 @@ class TableTest extends TestCase

public $fixtures = [
'core.articles',
'core.tags',
'core.articles_tags',
'core.authors',
'core.categories',
Expand All @@ -62,7 +63,6 @@ class TableTest extends TestCase
'core.members',
'core.polymorphic_tagged',
'core.site_articles',
'core.tags',
'core.users'
];

Expand Down

0 comments on commit 5ede142

Please sign in to comment.