Skip to content
Permalink
Browse files

Allowing Query::newExpr() to wrap any passed string or array as a sho…

…rthad for writing raw sql
  • Loading branch information...
lorenzo committed Jul 18, 2014
1 parent 5aa85db commit 1b44f4b4e0aa19f7c3a2c3822b94c15fbdb36f11
Showing with 30 additions and 17 deletions.
  1. +17 −2 src/Database/Query.php
  2. +13 −15 tests/TestCase/Database/QueryTest.php
@@ -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;
}
/**
@@ -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'));
@@ -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')
@@ -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')
@@ -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'
);
})
@@ -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'
);
})
@@ -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)
@@ -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)
@@ -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);
@@ -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);
}
@@ -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);
}
@@ -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);
@@ -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();

0 comments on commit 1b44f4b

Please sign in to comment.
You can’t perform that action at this time.