Skip to content

Commit

Permalink
Ensure that fields in orderAsc/orderDesc are quoted.
Browse files Browse the repository at this point in the history
String fields should be quoted when used with orderDesc/orderAsc.
  • Loading branch information
markstory committed Aug 8, 2015
1 parent f65eddd commit 80908ec
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 7 deletions.
11 changes: 4 additions & 7 deletions src/Database/Expression/OrderClauseExpression.php
Expand Up @@ -15,21 +15,18 @@
namespace Cake\Database\Expression;

use Cake\Database\ExpressionInterface;
use Cake\Database\Expression\FieldInterface;
use Cake\Database\Expression\FieldTrait;
use Cake\Database\ValueBinder;

/**
* An expression object for complex ORDER BY clauses
*
* @internal
*/
class OrderClauseExpression implements ExpressionInterface
class OrderClauseExpression implements ExpressionInterface, FieldInterface
{
/**
* The field being sorted on.
*
* @var \Cake\Database\ExpressionInterface|string
*/
protected $_field;
use FieldTrait;

/**
* The direction of sorting.
Expand Down
13 changes: 13 additions & 0 deletions tests/TestCase/Database/QueryTest.php
Expand Up @@ -1504,13 +1504,20 @@ public function testSelectOrderAsc()
$query->select(['id'])
->from('articles')
->orderAsc('id');

$sql = $query->sql();
$result = $query->execute()->fetchAll('assoc');
$expected = [
['id' => 1],
['id' => 2],
['id' => 3],
];
$this->assertEquals($expected, $result);
$this->assertQuotedQuery(
'SELECT <id> FROM <articles> ORDER BY <id> ASC',
$sql,
!$this->autoQuote
);

$query = new Query($this->connection);
$query->select(['id'])
Expand All @@ -1537,13 +1544,19 @@ public function testSelectOrderDesc()
$query->select(['id'])
->from('articles')
->orderDesc('id');
$sql = $query->sql();
$result = $query->execute()->fetchAll('assoc');
$expected = [
['id' => 3],
['id' => 2],
['id' => 1],
];
$this->assertEquals($expected, $result);
$this->assertQuotedQuery(
'SELECT <id> FROM <articles> ORDER BY <id> DESC',
$sql,
!$this->autoQuote
);

$query = new Query($this->connection);
$query->select(['id'])
Expand Down

0 comments on commit 80908ec

Please sign in to comment.