Skip navigation
Help

install.inc

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

Install functions for PostgreSQL embedded database engine.

Classes

NameDescription
DatabaseTasks_pgsql

File

drupal/includes/database/pgsql/install.inc
View source
  1. <?php
  2. /**
  3. * @file
  4. * Install functions for PostgreSQL embedded database engine.
  5. */
  6. // PostgreSQL specific install functions
  7. class DatabaseTasks_pgsql extends DatabaseTasks {
  8. protected $pdoDriver = 'pgsql';
  9. public function __construct() {
  10. $this->tasks[] = array(
  11. 'function' => 'checkEncoding',
  12. 'arguments' => array(),
  13. );
  14. $this->tasks[] = array(
  15. 'function' => 'checkPHPVersion',
  16. 'arguments' => array(),
  17. );
  18. $this->tasks[] = array(
  19. 'function' => 'checkBinaryOutput',
  20. 'arguments' => array(),
  21. );
  22. $this->tasks[] = array(
  23. 'function' => 'initializeDatabase',
  24. 'arguments' => array(),
  25. );
  26. }
  27. public function name() {
  28. return st('PostgreSQL');
  29. }
  30. public function minimumVersion() {
  31. return '8.3';
  32. }
  33. /**
  34. * Check encoding is UTF8.
  35. */
  36. protected function checkEncoding() {
  37. try {
  38. if (db_query('SHOW server_encoding')->fetchField() == 'UTF8') {
  39. $this->pass(st('Database is encoded in UTF-8'));
  40. }
  41. else {
  42. $replacements = array(
  43. '%encoding' => 'UTF8',
  44. '%driver' => $this->name(),
  45. '!link' => '<a href="INSTALL.pgsql.txt">INSTALL.pgsql.txt</a>'
  46. );
  47. $text = 'The %driver database must use %encoding encoding to work with Drupal.';
  48. $text .= 'Recreate the database with %encoding encoding. See !link for more details.';
  49. $this->fail(st($text, $replacements));
  50. }
  51. }
  52. catch (Exception $e) {
  53. $this->fail(st('Drupal could not determine the encoding of the database was set to UTF-8'));
  54. }
  55. }
  56. /**
  57. * Check PHP version.
  58. *
  59. * There are two bugs in PDO_pgsql affecting Drupal:
  60. *
  61. * - in versions < 5.2.7, PDO_pgsql refuses to insert an empty string into
  62. * a NOT NULL BLOB column. See: http://bugs.php.net/bug.php?id=46249
  63. * - in versions < 5.2.11 and < 5.3.1 that prevents inserting integer values
  64. * into numeric columns that exceed the PHP_INT_MAX value.
  65. * See: http://bugs.php.net/bug.php?id=48924
  66. */
  67. function checkPHPVersion() {
  68. if (!version_compare(PHP_VERSION, '5.2.11', '>=') || (version_compare(PHP_VERSION, '5.3.0', '>=') && !version_compare(PHP_VERSION, '5.3.1', '>='))) {
  69. $this->fail(st('The version of PHP you are using has known issues with PostgreSQL. You need to upgrade PHP to 5.2.11, 5.3.1 or greater.'));
  70. };
  71. }
  72. /**
  73. * Check Binary Output.
  74. *
  75. * Unserializing does not work on Postgresql 9 when bytea_output is 'hex'.
  76. */
  77. function checkBinaryOutput() {
  78. // PostgreSQL < 9 doesn't support bytea_output, so verify we are running
  79. // at least PostgreSQL 9.
  80. $database_connection = Database::getConnection();
  81. if (version_compare($database_connection->version(), '9') >= 0) {
  82. if (!$this->checkBinaryOutputSuccess()) {
  83. // First try to alter the database. If it fails, raise an error telling
  84. // the user to do it themselves.
  85. $connection_options = $database_connection->getConnectionOptions();
  86. // It is safe to include the database name directly here, because this
  87. // code is only called when a connection to the database is already
  88. // established, thus the database name is guaranteed to be a correct
  89. // value.
  90. $query = "ALTER DATABASE \"" . $connection_options['database'] . "\" SET bytea_output = 'escape';";
  91. try {
  92. db_query($query);
  93. }
  94. catch (Exception $e) {
  95. // Ignore possible errors when the user doesn't have the necessary
  96. // privileges to ALTER the database.
  97. }
  98. // Close the database connection so that the configuration parameter
  99. // is applied to the current connection.
  100. db_close();
  101. // Recheck, if it fails, finally just rely on the end user to do the
  102. // right thing.
  103. if (!$this->checkBinaryOutputSuccess()) {
  104. $replacements = array(
  105. '%setting' => 'bytea_output',
  106. '%current_value' => 'hex',
  107. '%needed_value' => 'escape',
  108. '!query' => "<code>" . $query . "</code>",
  109. );
  110. $this->fail(st("The %setting setting is currently set to '%current_value', but needs to be '%needed_value'. Change this by running the following query: !query", $replacements));
  111. }
  112. }
  113. }
  114. }
  115. /**
  116. * Verify that a binary data roundtrip returns the original string.
  117. */
  118. protected function checkBinaryOutputSuccess() {
  119. $bytea_output = db_query("SELECT 'encoding'::bytea AS output")->fetchField();
  120. return ($bytea_output == 'encoding');
  121. }
  122. /**
  123. * Make PostgreSQL Drupal friendly.
  124. */
  125. function initializeDatabase() {
  126. // We create some functions using global names instead of prefixing them
  127. // like we do with table names. This is so that we don't double up if more
  128. // than one instance of Drupal is running on a single database. We therefore
  129. // avoid trying to create them again in that case.
  130. try {
  131. // Create functions.
  132. db_query('CREATE OR REPLACE FUNCTION "greatest"(numeric, numeric) RETURNS numeric AS
  133. \'SELECT CASE WHEN (($1 > $2) OR ($2 IS NULL)) THEN $1 ELSE $2 END;\'
  134. LANGUAGE \'sql\''
  135. );
  136. db_query('CREATE OR REPLACE FUNCTION "greatest"(numeric, numeric, numeric) RETURNS numeric AS
  137. \'SELECT greatest($1, greatest($2, $3));\'
  138. LANGUAGE \'sql\''
  139. );
  140. // Don't use {} around pg_proc table.
  141. if (!db_query("SELECT COUNT(*) FROM pg_proc WHERE proname = 'rand'")->fetchField()) {
  142. db_query('CREATE OR REPLACE FUNCTION "rand"() RETURNS float AS
  143. \'SELECT random();\'
  144. LANGUAGE \'sql\''
  145. );
  146. }
  147. db_query('CREATE OR REPLACE FUNCTION "substring_index"(text, text, integer) RETURNS text AS
  148. \'SELECT array_to_string((string_to_array($1, $2)) [1:$3], $2);\'
  149. LANGUAGE \'sql\''
  150. );
  151. // Using || to concatenate in Drupal is not recommeneded because there are
  152. // database drivers for Drupal that do not support the syntax, however
  153. // they do support CONCAT(item1, item2) which we can replicate in
  154. // PostgreSQL. PostgreSQL requires the function to be defined for each
  155. // different argument variation the function can handle.
  156. db_query('CREATE OR REPLACE FUNCTION "concat"(anynonarray, anynonarray) RETURNS text AS
  157. \'SELECT CAST($1 AS text) || CAST($2 AS text);\'
  158. LANGUAGE \'sql\'
  159. ');
  160. db_query('CREATE OR REPLACE FUNCTION "concat"(text, anynonarray) RETURNS text AS
  161. \'SELECT $1 || CAST($2 AS text);\'
  162. LANGUAGE \'sql\'
  163. ');
  164. db_query('CREATE OR REPLACE FUNCTION "concat"(anynonarray, text) RETURNS text AS
  165. \'SELECT CAST($1 AS text) || $2;\'
  166. LANGUAGE \'sql\'
  167. ');
  168. db_query('CREATE OR REPLACE FUNCTION "concat"(text, text) RETURNS text AS
  169. \'SELECT $1 || $2;\'
  170. LANGUAGE \'sql\'
  171. ');
  172. $this->pass(st('PostgreSQL has initialized itself.'));
  173. }
  174. catch (Exception $e) {
  175. $this->fail(st('Drupal could not be correctly setup with the existing database. Revise any errors.'));
  176. }
  177. }
  178. }