Skip navigation
Help

database.inc

  1. drupal
    1. 6 drupal/includes/database.inc
    2. 7 drupal/includes/database/database.inc
    3. 7 drupal/includes/database/mysql/database.inc
    4. 7 drupal/includes/database/sqlite/database.inc
    5. 7 drupal/includes/database/pgsql/database.inc

Database interface code for PostgreSQL database servers.

Classes

NameDescription
DatabaseConnection_pgsql

Constants

NameDescription
POSTGRESQL_NEXTID_LOCKThe name by which to obtain a lock for retrive the next insert id.

File

drupal/includes/database/pgsql/database.inc
View source
  1. <?php
  2. /**
  3. * @file
  4. * Database interface code for PostgreSQL database servers.
  5. */
  6. /**
  7. * @ingroup database
  8. * @{
  9. */
  10. /**
  11. * The name by which to obtain a lock for retrive the next insert id.
  12. */
  13. define('POSTGRESQL_NEXTID_LOCK', 1000);
  14. class DatabaseConnection_pgsql extends DatabaseConnection {
  15. public function __construct(array $connection_options = array()) {
  16. // This driver defaults to transaction support, except if explicitly passed FALSE.
  17. $this->transactionSupport = !isset($connection_options['transactions']) || ($connection_options['transactions'] !== FALSE);
  18. // Transactional DDL is always available in PostgreSQL,
  19. // but we'll only enable it if standard transactions are.
  20. $this->transactionalDDLSupport = $this->transactionSupport;
  21. // Default to TCP connection on port 5432.
  22. if (empty($connection_options['port'])) {
  23. $connection_options['port'] = 5432;
  24. }
  25. // PostgreSQL in trust mode doesn't require a password to be supplied.
  26. if (empty($connection_options['password'])) {
  27. $connection_options['password'] = NULL;
  28. }
  29. // If the password contains a backslash it is treated as an escape character
  30. // http://bugs.php.net/bug.php?id=53217
  31. // so backslashes in the password need to be doubled up.
  32. // The bug was reported against pdo_pgsql 1.0.2, backslashes in passwords
  33. // will break on this doubling up when the bug is fixed, so check the version
  34. //elseif (phpversion('pdo_pgsql') < 'version_this_was_fixed_in') {
  35. else {
  36. $connection_options['password'] = str_replace('\\', '\\\\', $connection_options['password']);
  37. }
  38. $this->connectionOptions = $connection_options;
  39. $dsn = 'pgsql:host=' . $connection_options['host'] . ' dbname=' . $connection_options['database'] . ' port=' . $connection_options['port'];
  40. parent::__construct($dsn, $connection_options['username'], $connection_options['password'], array(
  41. // Prepared statements are most effective for performance when queries
  42. // are recycled (used several times). However, if they are not re-used,
  43. // prepared statements become ineffecient. Since most of Drupal's
  44. // prepared queries are not re-used, it should be faster to emulate
  45. // the preparation than to actually ready statements for re-use. If in
  46. // doubt, reset to FALSE and measure performance.
  47. PDO::ATTR_EMULATE_PREPARES => TRUE,
  48. // Convert numeric values to strings when fetching.
  49. PDO::ATTR_STRINGIFY_FETCHES => TRUE,
  50. // Force column names to lower case.
  51. PDO::ATTR_CASE => PDO::CASE_LOWER,
  52. ));
  53. // Force PostgreSQL to use the UTF-8 character set by default.
  54. $this->exec("SET NAMES 'UTF8'");
  55. }
  56. public function query($query, array $args = array(), $options = array()) {
  57. $options += $this->defaultOptions();
  58. // The PDO PostgreSQL driver has a bug which
  59. // doesn't type cast booleans correctly when
  60. // parameters are bound using associative
  61. // arrays.
  62. // See http://bugs.php.net/bug.php?id=48383
  63. foreach ($args as &$value) {
  64. if (is_bool($value)) {
  65. $value = (int) $value;
  66. }
  67. }
  68. try {
  69. if ($query instanceof DatabaseStatementInterface) {
  70. $stmt = $query;
  71. $stmt->execute(NULL, $options);
  72. }
  73. else {
  74. $this->expandArguments($query, $args);
  75. $stmt = $this->prepareQuery($query);
  76. $stmt->execute($args, $options);
  77. }
  78. switch ($options['return']) {
  79. case Database::RETURN_STATEMENT:
  80. return $stmt;
  81. case Database::RETURN_AFFECTED:
  82. return $stmt->rowCount();
  83. case Database::RETURN_INSERT_ID:
  84. return $this->lastInsertId($options['sequence_name']);
  85. case Database::RETURN_NULL:
  86. return;
  87. default:
  88. throw new PDOException('Invalid return directive: ' . $options['return']);
  89. }
  90. }
  91. catch (PDOException $e) {
  92. if ($options['throw_exception']) {
  93. // Add additional debug information.
  94. if ($query instanceof DatabaseStatementInterface) {
  95. $e->query_string = $stmt->getQueryString();
  96. }
  97. else {
  98. $e->query_string = $query;
  99. }
  100. $e->args = $args;
  101. throw $e;
  102. }
  103. return NULL;
  104. }
  105. }
  106. public function queryRange($query, $from, $count, array $args = array(), array $options = array()) {
  107. return $this->query($query . ' LIMIT ' . (int) $count . ' OFFSET ' . (int) $from, $args, $options);
  108. }
  109. public function queryTemporary($query, array $args = array(), array $options = array()) {
  110. $tablename = $this->generateTemporaryTableName();
  111. $this->query(preg_replace('/^SELECT/i', 'CREATE TEMPORARY TABLE {' . $tablename . '} AS SELECT', $query), $args, $options);
  112. return $tablename;
  113. }
  114. public function driver() {
  115. return 'pgsql';
  116. }
  117. public function databaseType() {
  118. return 'pgsql';
  119. }
  120. public function mapConditionOperator($operator) {
  121. static $specials;
  122. // Function calls not allowed in static declarations, thus this method.
  123. if (!isset($specials)) {
  124. $specials = array(
  125. // In PostgreSQL, 'LIKE' is case-sensitive. For case-insensitive LIKE
  126. // statements, we need to use ILIKE instead.
  127. 'LIKE' => array('operator' => 'ILIKE'),
  128. 'NOT LIKE' => array('operator' => 'NOT ILIKE'),
  129. );
  130. }
  131. return isset($specials[$operator]) ? $specials[$operator] : NULL;
  132. }
  133. /**
  134. * Retrive a the next id in a sequence.
  135. *
  136. * PostgreSQL has built in sequences. We'll use these instead of inserting
  137. * and updating a sequences table.
  138. */
  139. public function nextId($existing = 0) {
  140. // Retrive the name of the sequence. This information cannot be cached
  141. // because the prefix may change, for example, like it does in simpletests.
  142. $sequence_name = $this->makeSequenceName('sequences', 'value');
  143. // When PostgreSQL gets a value too small then it will lock the table,
  144. // retry the INSERT and if it's still too small then alter the sequence.
  145. $id = $this->query("SELECT nextval('" . $sequence_name . "')")->fetchField();
  146. if ($id > $existing) {
  147. return $id;
  148. }
  149. // PostgreSQL advisory locks are simply locks to be used by an
  150. // application such as Drupal. This will prevent other Drupal proccesses
  151. // from altering the sequence while we are.
  152. $this->query("SELECT pg_advisory_lock(" . POSTGRESQL_NEXTID_LOCK . ")");
  153. // While waiting to obtain the lock, the sequence may have been altered
  154. // so lets try again to obtain an adequate value.
  155. $id = $this->query("SELECT nextval('" . $sequence_name . "')")->fetchField();
  156. if ($id > $existing) {
  157. $this->query("SELECT pg_advisory_unlock(" . POSTGRESQL_NEXTID_LOCK . ")");
  158. return $id;
  159. }
  160. // Reset the sequence to a higher value than the existing id.
  161. $this->query("ALTER SEQUENCE " . $sequence_name . " RESTART WITH " . ($existing + 1));
  162. // Retrive the next id. We know this will be as high as we want it.
  163. $id = $this->query("SELECT nextval('" . $sequence_name . "')")->fetchField();
  164. $this->query("SELECT pg_advisory_unlock(" . POSTGRESQL_NEXTID_LOCK . ")");
  165. return $id;
  166. }
  167. }
  168. /**
  169. * @} End of "ingroup database".
  170. */