1. 8.3.x core/lib/Drupal/Core/Database/Driver/pgsql/Select.php
  2. 8.3.x core/lib/Drupal/Core/Database/Driver/mysql/Select.php
  3. 8.3.x core/lib/Drupal/Core/Render/Element/Select.php
  4. 8.3.x core/lib/Drupal/Core/Database/Query/Select.php
  5. 8.3.x core/tests/Drupal/Tests/Core/Database/Stub/Select.php
  6. 8.3.x core/lib/Drupal/Core/Database/Driver/sqlite/Select.php
  7. 8.0.x core/lib/Drupal/Core/Database/Query/Select.php
  8. 8.0.x core/lib/Drupal/Core/Database/Driver/mysql/Select.php
  9. 8.0.x core/lib/Drupal/Core/Database/Driver/pgsql/Select.php
  10. 8.0.x core/lib/Drupal/Core/Render/Element/Select.php
  11. 8.0.x core/lib/Drupal/Core/Database/Driver/sqlite/Select.php
  12. 8.1.x core/lib/Drupal/Core/Database/Driver/mysql/Select.php
  13. 8.1.x core/tests/Drupal/Tests/Core/Database/Stub/Select.php
  14. 8.1.x core/lib/Drupal/Core/Database/Driver/sqlite/Select.php
  15. 8.1.x core/lib/Drupal/Core/Database/Driver/pgsql/Select.php
  16. 8.1.x core/lib/Drupal/Core/Render/Element/Select.php
  17. 8.1.x core/lib/Drupal/Core/Database/Query/Select.php
  18. 8.2.x core/lib/Drupal/Core/Render/Element/Select.php
  19. 8.2.x core/lib/Drupal/Core/Database/Driver/pgsql/Select.php
  20. 8.2.x core/lib/Drupal/Core/Database/Query/Select.php
  21. 8.2.x core/lib/Drupal/Core/Database/Driver/mysql/Select.php
  22. 8.2.x core/tests/Drupal/Tests/Core/Database/Stub/Select.php
  23. 8.2.x core/lib/Drupal/Core/Database/Driver/sqlite/Select.php
  24. 8.4.x core/lib/Drupal/Core/Database/Driver/sqlite/Select.php
  25. 8.4.x core/tests/Drupal/Tests/Core/Database/Stub/Select.php
  26. 8.4.x core/lib/Drupal/Core/Database/Driver/pgsql/Select.php
  27. 8.4.x core/lib/Drupal/Core/Render/Element/Select.php
  28. 8.4.x core/lib/Drupal/Core/Database/Query/Select.php
  29. 8.4.x core/lib/Drupal/Core/Database/Driver/mysql/Select.php

Namespace

Drupal\Core\Database\Driver\pgsql

File

core/lib/Drupal/Core/Database/Driver/pgsql/Select.php
View source
  1. <?php
  2. namespace Drupal\Core\Database\Driver\pgsql;
  3. use Drupal\Core\Database\Query\Select as QuerySelect;
  4. /**
  5. * @addtogroup database
  6. * @{
  7. */
  8. /**
  9. * PostgreSQL implementation of \Drupal\Core\Database\Query\Select.
  10. */
  11. class Select extends QuerySelect {
  12. public function orderRandom() {
  13. $alias = $this->addExpression('RANDOM()', 'random_field');
  14. $this->orderBy($alias);
  15. return $this;
  16. }
  17. /**
  18. * Overrides SelectQuery::orderBy().
  19. *
  20. * PostgreSQL adheres strictly to the SQL-92 standard and requires that when
  21. * using DISTINCT or GROUP BY conditions, fields and expressions that are
  22. * ordered on also need to be selected. This is a best effort implementation
  23. * to handle the cases that can be automated by adding the field if it is not
  24. * yet selected.
  25. *
  26. * @code
  27. * $query = db_select('example', 'e');
  28. * $query->join('example_revision', 'er', 'e.vid = er.vid');
  29. * $query
  30. * ->distinct()
  31. * ->fields('e')
  32. * ->orderBy('timestamp');
  33. * @endcode
  34. *
  35. * In this query, it is not possible (without relying on the schema) to know
  36. * whether timestamp belongs to example_revision and needs to be added or
  37. * belongs to node and is already selected. Queries like this will need to be
  38. * corrected in the original query by adding an explicit call to
  39. * SelectQuery::addField() or SelectQuery::fields().
  40. *
  41. * Since this has a small performance impact, both by the additional
  42. * processing in this function and in the database that needs to return the
  43. * additional fields, this is done as an override instead of implementing it
  44. * directly in SelectQuery::orderBy().
  45. */
  46. public function orderBy($field, $direction = 'ASC') {
  47. // Only allow ASC and DESC, default to ASC.
  48. // Emulate MySQL default behavior to sort NULL values first for ascending,
  49. // and last for descending.
  50. // @see http://www.postgresql.org/docs/9.3/static/queries-order.html
  51. $direction = strtoupper($direction) == 'DESC' ? 'DESC NULLS LAST' : 'ASC NULLS FIRST';
  52. $this->order[$field] = $direction;
  53. if ($this->hasTag('entity_query')) {
  54. return $this;
  55. }
  56. // If there is a table alias specified, split it up.
  57. if (strpos($field, '.') !== FALSE) {
  58. list($table, $table_field) = explode('.', $field);
  59. }
  60. // Figure out if the field has already been added.
  61. foreach ($this->fields as $existing_field) {
  62. if (!empty($table)) {
  63. // If table alias is given, check if field and table exists.
  64. if ($existing_field['table'] == $table && $existing_field['field'] == $table_field) {
  65. return $this;
  66. }
  67. }
  68. else {
  69. // If there is no table, simply check if the field exists as a field or
  70. // an aliased field.
  71. if ($existing_field['alias'] == $field) {
  72. return $this;
  73. }
  74. }
  75. }
  76. // Also check expression aliases.
  77. foreach ($this->expressions as $expression) {
  78. if ($expression['alias'] == $this->connection->escapeAlias($field)) {
  79. return $this;
  80. }
  81. }
  82. // If a table loads all fields, it can not be added again. It would
  83. // result in an ambiguous alias error because that field would be loaded
  84. // twice: Once through table_alias.* and once directly. If the field
  85. // actually belongs to a different table, it must be added manually.
  86. foreach ($this->tables as $table) {
  87. if (!empty($table['all_fields'])) {
  88. return $this;
  89. }
  90. }
  91. // If $field contains an characters which are not allowed in a field name
  92. // it is considered an expression, these can't be handled automatically
  93. // either.
  94. if ($this->connection->escapeField($field) != $field) {
  95. return $this;
  96. }
  97. // This is a case that can be handled automatically, add the field.
  98. $this->addField(NULL, $field);
  99. return $this;
  100. }
  101. /**
  102. * {@inheritdoc}
  103. */
  104. public function addExpression($expression, $alias = NULL, $arguments = array()) {
  105. if (empty($alias)) {
  106. $alias = 'expression';
  107. }
  108. // This implements counting in the same manner as the parent method.
  109. $alias_candidate = $alias;
  110. $count = 2;
  111. while (!empty($this->expressions[$alias_candidate])) {
  112. $alias_candidate = $alias . '_' . $count++;
  113. }
  114. $alias = $alias_candidate;
  115. $this->expressions[$alias] = array(
  116. 'expression' => $expression,
  117. 'alias' => $this->connection->escapeAlias($alias_candidate),
  118. 'arguments' => $arguments,
  119. );
  120. return $alias;
  121. }
  122. /**
  123. * {@inheritdoc}
  124. */
  125. public function execute() {
  126. $this->connection->addSavepoint();
  127. try {
  128. $result = parent::execute();
  129. }
  130. catch (\Exception $e) {
  131. $this->connection->rollbackSavepoint();
  132. throw $e;
  133. }
  134. $this->connection->releaseSavepoint();
  135. return $result;
  136. }
  137. }
  138. /**
  139. * @} End of "addtogroup database".
  140. */

Classes

Namesort descending Description
Select PostgreSQL implementation of \Drupal\Core\Database\Query\Select.