Skip to content

Commit 1b44f4b

Browse files
committed
Allowing Query::newExpr() to wrap any passed string or array as a shorthad for writing raw sql
1 parent 5aa85db commit 1b44f4b

File tree

2 files changed

+30
-17
lines changed

2 files changed

+30
-17
lines changed

src/Database/Query.php

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1212,10 +1212,25 @@ public function type() {
12121212
* this function in subclasses to use a more specialized QueryExpression class
12131213
* if required.
12141214
*
1215+
* You can optionally pass a single raw SQL string or an array or expressions in
1216+
* any format accepted by \Cake\Database\QueryExpression:
1217+
*
1218+
* {{{
1219+
*
1220+
* $expression = $query->newExpression(); // Returns an empty expression object
1221+
* $expression = $query->newExpression('Table.column = Table2.column'); // Return a raw SQL expression
1222+
* }}}
1223+
*
12151224
* @return \Cake\Database\QueryExpression
12161225
*/
1217-
public function newExpr() {
1218-
return new QueryExpression([], $this->typeMap());
1226+
public function newExpr($rawExpression = null) {
1227+
$expression = new QueryExpression([], $this->typeMap());
1228+
1229+
if ($rawExpression !== null) {
1230+
$expression->add($rawExpression);
1231+
}
1232+
1233+
return $expression;
12191234
}
12201235

12211236
/**

tests/TestCase/Database/QueryTest.php

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -140,8 +140,8 @@ public function testSelectAliasedFieldsFromTable() {
140140
$this->assertEquals(array('foo' => 'Second Article Body', 'text' => 'Second Article Body', 'author_id' => 3), $result->fetch('assoc'));
141141

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

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

260260
$query = new Query($this->connection);
261-
$conditions = $query->newExpr()->add('author_id = a.id');
261+
$conditions = $query->newExpr('author_id = a.id');
262262
$result = $query
263263
->select(['title', 'name'])
264264
->from('articles')
@@ -921,7 +921,7 @@ public function testInValueCast() {
921921
->where(function($exp, $q) {
922922
return $exp->in(
923923
'created',
924-
$q->newExpr()->add("'2007-03-18 10:45:23'"),
924+
$q->newExpr("'2007-03-18 10:45:23'"),
925925
'datetime'
926926
);
927927
})
@@ -936,7 +936,7 @@ public function testInValueCast() {
936936
->where(function($exp, $q) {
937937
return $exp->notIn(
938938
'created',
939-
$q->newExpr()->add("'2007-03-18 10:45:23'"),
939+
$q->newExpr("'2007-03-18 10:45:23'"),
940940
'datetime'
941941
);
942942
})
@@ -1124,8 +1124,7 @@ public function testSelectOrderBy() {
11241124
$this->assertEquals(['id' => 2], $result->fetch('assoc'));
11251125
$this->assertEquals(['id' => 3], $result->fetch('assoc'));
11261126

1127-
$expression = $query->newExpr()
1128-
->add(['(id + :offset) % 2']);
1127+
$expression = $query->newExpr(['(id + :offset) % 2']);
11291128
$result = $query
11301129
->order([$expression, 'id' => 'desc'], true)
11311130
->bind(':offset', 1, null)
@@ -1862,8 +1861,7 @@ public function testUpdateMultipleFieldsArray() {
18621861
public function testUpdateWithExpression() {
18631862
$query = new Query($this->connection);
18641863

1865-
$expr = $query->newExpr();
1866-
$expr->add('title = author_id');
1864+
$expr = $query->newExpr('title = author_id');
18671865

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

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

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

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

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

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

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

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

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

24552453
$query = new Query($this->connection);
2456-
$sql = $query->insert([$query->newExpr()->add('bar')])
2454+
$sql = $query->insert([$query->newExpr('bar')])
24572455
->into('foo')
24582456
->where(['something' => 'value'])
24592457
->sql();

0 commit comments

Comments
 (0)