Skip to content

Commit

Permalink
Allowing Query::newExpr() to wrap any passed string or array as a sho…
Browse files Browse the repository at this point in the history
…rthad for writing raw sql
  • Loading branch information
lorenzo committed Jul 18, 2014
1 parent 5aa85db commit 1b44f4b
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 17 deletions.
19 changes: 17 additions & 2 deletions src/Database/Query.php
Expand Up @@ -1212,10 +1212,25 @@ public function type() {
* this function in subclasses to use a more specialized QueryExpression class
* if required.
*
* You can optionally pass a single raw SQL string or an array or expressions in
* any format accepted by \Cake\Database\QueryExpression:
*
* {{{
*
* $expression = $query->newExpression(); // Returns an empty expression object
* $expression = $query->newExpression('Table.column = Table2.column'); // Return a raw SQL expression
* }}}
*
* @return \Cake\Database\QueryExpression
*/
public function newExpr() {
return new QueryExpression([], $this->typeMap());
public function newExpr($rawExpression = null) {
$expression = new QueryExpression([], $this->typeMap());

if ($rawExpression !== null) {
$expression->add($rawExpression);
}

return $expression;
}

/**
Expand Down
28 changes: 13 additions & 15 deletions tests/TestCase/Database/QueryTest.php
Expand Up @@ -140,8 +140,8 @@ public function testSelectAliasedFieldsFromTable() {
$this->assertEquals(array('foo' => 'Second Article Body', 'text' => 'Second Article Body', 'author_id' => 3), $result->fetch('assoc'));

$query = new Query($this->connection);
$exp = $query->newExpr()->add('1 + 1');
$comp = $query->newExpr()->add(['author_id +' => 2]);
$exp = $query->newExpr('1 + 1');
$comp = $query->newExpr(['author_id +' => 2]);
$result = $query->select(['text' => 'body', 'two' => $exp, 'three' => $comp])
->from('articles')->execute();
$this->assertEquals(array('text' => 'First Article Body', 'two' => 2, 'three' => 3), $result->fetch('assoc'));
Expand Down Expand Up @@ -220,7 +220,7 @@ public function testSelectWithJoinsConditions() {
$this->assertEquals(array('title' => 'Second Article', 'name' => 'larry'), $result->fetch('assoc'));

$query = new Query($this->connection);
$conditions = $query->newExpr()->add('author_id = a.id');
$conditions = $query->newExpr('author_id = a.id');
$result = $query
->select(['title', 'name'])
->from('articles')
Expand Down Expand Up @@ -258,7 +258,7 @@ public function testSelectAliasedJoins() {
$this->assertEquals(array('title' => 'Second Article', 'name' => 'nate'), $result->fetch('assoc'));

$query = new Query($this->connection);
$conditions = $query->newExpr()->add('author_id = a.id');
$conditions = $query->newExpr('author_id = a.id');
$result = $query
->select(['title', 'name'])
->from('articles')
Expand Down Expand Up @@ -921,7 +921,7 @@ public function testInValueCast() {
->where(function($exp, $q) {
return $exp->in(
'created',
$q->newExpr()->add("'2007-03-18 10:45:23'"),
$q->newExpr("'2007-03-18 10:45:23'"),
'datetime'
);
})
Expand All @@ -936,7 +936,7 @@ public function testInValueCast() {
->where(function($exp, $q) {
return $exp->notIn(
'created',
$q->newExpr()->add("'2007-03-18 10:45:23'"),
$q->newExpr("'2007-03-18 10:45:23'"),
'datetime'
);
})
Expand Down Expand Up @@ -1124,8 +1124,7 @@ public function testSelectOrderBy() {
$this->assertEquals(['id' => 2], $result->fetch('assoc'));
$this->assertEquals(['id' => 3], $result->fetch('assoc'));

$expression = $query->newExpr()
->add(['(id + :offset) % 2']);
$expression = $query->newExpr(['(id + :offset) % 2']);
$result = $query
->order([$expression, 'id' => 'desc'], true)
->bind(':offset', 1, null)
Expand Down Expand Up @@ -1862,8 +1861,7 @@ public function testUpdateMultipleFieldsArray() {
public function testUpdateWithExpression() {
$query = new Query($this->connection);

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

$query->update('articles')
->set($expr)
Expand Down Expand Up @@ -2334,7 +2332,7 @@ public function testQuotingSelectFieldsAndAlias() {
$this->assertQuotedQuery('SELECT <1 \+ 1> AS <foo>$', $sql);

$query = new Query($this->connection);
$sql = $query->select(['foo' => $query->newExpr()->add('1 + 1')])->sql();
$sql = $query->select(['foo' => $query->newExpr('1 + 1')])->sql();
$this->assertQuotedQuery('SELECT \(1 \+ 1\) AS <foo>$', $sql);

$query = new Query($this->connection);
Expand All @@ -2358,7 +2356,7 @@ public function testQuotingFromAndAlias() {
$this->assertQuotedQuery('FROM <something> AS <foo>$', $sql);

$query = new Query($this->connection);
$sql = $query->select('*')->from(['foo' => $query->newExpr()->add('bar')])->sql();
$sql = $query->select('*')->from(['foo' => $query->newExpr('bar')])->sql();
$this->assertQuotedQuery('FROM \(bar\) AS <foo>$', $sql);
}

Expand Down Expand Up @@ -2390,7 +2388,7 @@ public function testQuotingJoinsAndAlias() {
$this->assertQuotedQuery('JOIN <something> <foo>', $sql);

$query = new Query($this->connection);
$sql = $query->select('*')->join(['foo' => $query->newExpr()->add('bar')])->sql();
$sql = $query->select('*')->join(['foo' => $query->newExpr('bar')])->sql();
$this->assertQuotedQuery('JOIN \(bar\) <foo>', $sql);
}

Expand All @@ -2406,7 +2404,7 @@ public function testQuotingGroupBy() {
$this->assertQuotedQuery('GROUP BY <something>', $sql);

$query = new Query($this->connection);
$sql = $query->select('*')->group([$query->newExpr()->add('bar')])->sql();
$sql = $query->select('*')->group([$query->newExpr('bar')])->sql();
$this->assertQuotedQuery('GROUP BY \(bar\)', $sql);

$query = new Query($this->connection);
Expand Down Expand Up @@ -2453,7 +2451,7 @@ public function testQuotingInsert() {
$this->assertQuotedQuery('INSERT INTO <foo> \(<bar>, <baz>\)', $sql);

$query = new Query($this->connection);
$sql = $query->insert([$query->newExpr()->add('bar')])
$sql = $query->insert([$query->newExpr('bar')])
->into('foo')
->where(['something' => 'value'])
->sql();
Expand Down

0 comments on commit 1b44f4b

Please sign in to comment.