Skip navigation
Help

select.inc

  1. drupal
    1. 7 drupal/includes/database/select.inc
    2. 7 drupal/includes/database/sqlite/select.inc
    3. 7 drupal/includes/database/pgsql/select.inc

Select builder for PostgreSQL database engine.

Classes

NameDescription
SelectQuery_pgsql

File

drupal/includes/database/pgsql/select.inc
View source
  1. <?php
  2. /**
  3. * @file
  4. * Select builder for PostgreSQL database engine.
  5. */
  6. /**
  7. * @ingroup database
  8. * @{
  9. */
  10. class SelectQuery_pgsql extends SelectQuery {
  11. public function orderRandom() {
  12. $alias = $this->addExpression('RANDOM()', 'random_field');
  13. $this->orderBy($alias);
  14. return $this;
  15. }
  16. /**
  17. * Overrides SelectQuery::orderBy().
  18. *
  19. * PostgreSQL adheres strictly to the SQL-92 standard and requires that when
  20. * using DISTINCT or GROUP BY conditions, fields and expressions that are
  21. * ordered on also need to be selected. This is a best effort implementation
  22. * to handle the cases that can be automated by adding the field if it is not
  23. * yet selected.
  24. *
  25. * @code
  26. * $query = db_select('node', 'n');
  27. * $query->join('node_revision', 'nr', 'n.vid = nr.vid');
  28. * $query
  29. * ->distinct()
  30. * ->fields('n')
  31. * ->orderBy('timestamp');
  32. * @endcode
  33. *
  34. * In this query, it is not possible (without relying on the schema) to know
  35. * whether timestamp belongs to node_revisions and needs to be added or
  36. * belongs to node and is already selected. Queries like this will need to be
  37. * corrected in the original query by adding an explicit call to
  38. * SelectQuery::addField() or SelectQuery::fields().
  39. *
  40. * Since this has a small performance impact, both by the additional
  41. * processing in this function and in the database that needs to return the
  42. * additional fields, this is done as an override instead of implementing it
  43. * directly in SelectQuery::orderBy().
  44. */
  45. public function orderBy($field, $direction = 'ASC') {
  46. // Call parent function to order on this.
  47. $return = parent::orderBy($field, $direction);
  48. // If there is a table alias specified, split it up.
  49. if (strpos($field, '.') !== FALSE) {
  50. list($table, $table_field) = explode('.', $field);
  51. }
  52. // Figure out if the field has already been added.
  53. foreach ($this->fields as $existing_field) {
  54. if (!empty($table)) {
  55. // If table alias is given, check if field and table exists.
  56. if ($existing_field['table'] == $table && $existing_field['field'] == $table_field) {
  57. return $return;
  58. }
  59. }
  60. else {
  61. // If there is no table, simply check if the field exists as a field or
  62. // an aliased field.
  63. if ($existing_field['alias'] == $field) {
  64. return $return;
  65. }
  66. }
  67. }
  68. // Also check expression aliases.
  69. foreach ($this->expressions as $expression) {
  70. if ($expression['alias'] == $field) {
  71. return $return;
  72. }
  73. }
  74. // If a table loads all fields, it can not be added again. It would
  75. // result in an ambigious alias error because that field would be loaded
  76. // twice: Once through table_alias.* and once directly. If the field
  77. // actually belongs to a different table, it must be added manually.
  78. foreach ($this->tables as $table) {
  79. if (!empty($table['all_fields'])) {
  80. return $return;
  81. }
  82. }
  83. // If $field contains an characters which are not allowed in a field name
  84. // it is considered an expression, these can't be handeld automatically
  85. // either.
  86. if ($this->connection->escapeField($field) != $field) {
  87. return $return;
  88. }
  89. // This is a case that can be handled automatically, add the field.
  90. $this->addField(NULL, $field);
  91. return $return;
  92. }
  93. }
  94. /**
  95. * @} End of "ingroup database".
  96. */