Commit 9b5042c1 authored by alexpott's avatar alexpott
Browse files

Issue #2443657 by daffie, bzrudi71: PostgreSQL: Fix system\Tests\Entity\EntityQueryTest

parent 8d87a928
......@@ -52,11 +52,15 @@ public function orderRandom() {
* directly in SelectQuery::orderBy().
*/
public function orderBy($field, $direction = 'ASC') {
// Call parent function to order on this.
$return = parent::orderBy($field, $direction);
// Only allow ASC and DESC, default to ASC.
// Emulate MySQL default behavior to sort NULL values first for ascending,
// and last for descending.
// @see http://www.postgresql.org/docs/9.3/static/queries-order.html
$direction = strtoupper($direction) == 'DESC' ? 'DESC NULLS LAST' : 'ASC NULLS FIRST';
$this->order[$field] = $direction;
if ($this->hasTag('entity_query')) {
return $return;
return $this;
}
// If there is a table alias specified, split it up.
......@@ -68,14 +72,14 @@ public function orderBy($field, $direction = 'ASC') {
if (!empty($table)) {
// If table alias is given, check if field and table exists.
if ($existing_field['table'] == $table && $existing_field['field'] == $table_field) {
return $return;
return $this;
}
}
else {
// If there is no table, simply check if the field exists as a field or
// an aliased field.
if ($existing_field['alias'] == $field) {
return $return;
return $this;
}
}
}
......@@ -83,7 +87,7 @@ public function orderBy($field, $direction = 'ASC') {
// Also check expression aliases.
foreach ($this->expressions as $expression) {
if ($expression['alias'] == $this->connection->escapeAlias($field)) {
return $return;
return $this;
}
}
......@@ -93,7 +97,7 @@ public function orderBy($field, $direction = 'ASC') {
// actually belongs to a different table, it must be added manually.
foreach ($this->tables as $table) {
if (!empty($table['all_fields'])) {
return $return;
return $this;
}
}
......@@ -101,12 +105,12 @@ public function orderBy($field, $direction = 'ASC') {
// it is considered an expression, these can't be handled automatically
// either.
if ($this->connection->escapeField($field) != $field) {
return $return;
return $this;
}
// This is a case that can be handled automatically, add the field.
$this->addField(NULL, $field);
return $return;
return $this;
}
/**
......
......@@ -7,7 +7,8 @@
namespace Drupal\system\Tests\Database;
use \Drupal\Core\Database\RowCountException;
use Drupal\Core\Database\Database;
use Drupal\Core\Database\RowCountException;
/**
* Tests the Select query builder with more complex queries.
......@@ -220,7 +221,10 @@ function testCountQueryRemovals() {
// Check that the ordering clause is handled properly.
$orderby = $query->getOrderBy();
$this->assertEqual($orderby['name'], 'ASC', 'Query correctly sets ordering clause.');
// The orderby string is different for PostgreSQL.
// @see Drupal\Core\Database\Driver\pgsql\Select::orderBy()
$db_type = Database::getConnection()->databaseType();
$this->assertEqual($orderby['name'], ($db_type == 'pgsql' ? 'ASC NULLS FIRST' : 'ASC'), 'Query correctly sets ordering clause.');
$orderby = $count->getOrderBy();
$this->assertFalse(isset($orderby['name']), 'Count query correctly unsets ordering caluse.');
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment