install.pgsql.inc

File

drupal-6.x/includes/install.pgsql.inc
View source
  1. <?php
  2. // PostgreSQL specific install functions
  3. /**
  4. * Check if PostgreSQL is available.
  5. *
  6. * @return
  7. * TRUE/FALSE
  8. */
  9. function pgsql_is_available() {
  10. return function_exists('pg_connect');
  11. }
  12. /**
  13. * Check if we can connect to PostgreSQL.
  14. *
  15. * @return
  16. * TRUE/FALSE
  17. */
  18. function drupal_test_pgsql($url, &$success) {
  19. if (!pgsql_is_available()) {
  20. drupal_set_message(st('PHP PostgreSQL support not enabled.'), 'error');
  21. return FALSE;
  22. }
  23. $url = parse_url($url);
  24. $conn_string = '';
  25. // Decode urlencoded information in the db connection string
  26. if (isset($url['user'])) {
  27. $conn_string .= ' user='. urldecode($url['user']);
  28. }
  29. if (isset($url['pass'])) {
  30. $conn_string .= ' password='. urldecode($url['pass']);
  31. }
  32. if (isset($url['host'])) {
  33. $conn_string .= ' host='. urldecode($url['host']);
  34. }
  35. if (isset($url['path'])) {
  36. $conn_string .= ' dbname='. substr(urldecode($url['path']), 1);
  37. }
  38. if (isset($url['port'])) {
  39. $conn_string .= ' port='. urldecode($url['port']);
  40. }
  41. // Test connecting to the database.
  42. $connection = @pg_connect($conn_string);
  43. if (!$connection) {
  44. drupal_set_message(st('Failed to connect to your PostgreSQL database server. PostgreSQL reports the following message: %error.<ul><li>Are you sure you have the correct username and password?</li><li>Are you sure that you have typed the correct database hostname?</li><li>Are you sure that the database server is running?</li><li>Are you sure you typed the correct database name?</li></ul>For more help, see the <a href="http://drupal.org/node/258">Installation and upgrading handbook</a>. If you are unsure what these terms mean you should probably contact your hosting provider.', array('%error' => 'Connection failed. See log file for failure reason')), 'error');
  45. return FALSE;
  46. }
  47. $success = array('CONNECT');
  48. // Test CREATE.
  49. $query = 'CREATE TABLE drupal_install_test (id integer NOT NULL)';
  50. $result = pg_query($connection, $query);
  51. if ($error = pg_result_error($result)) {
  52. drupal_set_message(st('Failed to create a test table on your PostgreSQL database server with the command %query. PostgreSQL reports the following message: %error.<ul><li>Are you sure the configured username has the necessary PostgreSQL permissions to create tables in the database?</li></ul>For more help, see the <a href="http://drupal.org/node/258">Installation and upgrading handbook</a>. If you are unsure what these terms mean you should probably contact your hosting provider.', array('%query' => $query, '%error' => $error)), 'error');
  53. return FALSE;
  54. }
  55. $err = FALSE;
  56. $success[] = 'SELECT';
  57. $success[] = 'CREATE';
  58. // Test INSERT.
  59. $query = 'INSERT INTO drupal_install_test (id) VALUES (1)';
  60. $result = pg_query($connection, $query);
  61. if ($error = pg_result_error($result)) {
  62. drupal_set_message(st('Failed to insert a value into a test table on your PostgreSQL database server. We tried inserting a value with the command %query and PostgreSQL reported the following error: %error.', array('%query' => $query, '%error' => $error)), 'error');
  63. $err = TRUE;
  64. }
  65. else {
  66. $success[] = 'INSERT';
  67. }
  68. // Test UPDATE.
  69. $query = 'UPDATE drupal_install_test SET id = 2';
  70. $result = pg_query($connection, $query);
  71. if ($error = pg_result_error($result)) {
  72. drupal_set_message(st('Failed to update a value in a test table on your PostgreSQL database server. We tried updating a value with the command %query and PostgreSQL reported the following error: %error.', array('%query' => $query, '%error' => $error)), 'error');
  73. $err = TRUE;
  74. }
  75. else {
  76. $success[] = 'UPDATE';
  77. }
  78. // Test LOCK.
  79. $query = 'BEGIN; LOCK drupal_install_test IN SHARE ROW EXCLUSIVE MODE';
  80. $result = pg_query($connection, $query);
  81. if ($error = pg_result_error($result)) {
  82. drupal_set_message(st('Failed to lock a test table on your PostgreSQL database server. We tried locking a table with the command %query and PostgreSQL reported the following error: %error.', array('%query' => $query, '%error' => $error)), 'error');
  83. $err = TRUE;
  84. }
  85. else {
  86. $success[] = 'LOCK';
  87. }
  88. // Test UNLOCK, which is done automatically upon transaction end in PostgreSQL
  89. $query = 'COMMIT';
  90. $result = pg_query($connection, $query);
  91. if ($error = pg_result_error()) {
  92. drupal_set_message(st('Failed to unlock a test table on your PostgreSQL database server. We tried unlocking a table with the command %query and PostgreSQL reported the following error: %error.', array('%query' => $query, '%error' => $error)), 'error');
  93. $err = TRUE;
  94. }
  95. else {
  96. $success[] = 'UNLOCK';
  97. }
  98. // Test DELETE.
  99. $query = 'DELETE FROM drupal_install_test';
  100. $result = pg_query($connection, $query);
  101. if ($error = pg_result_error()) {
  102. drupal_set_message(st('Failed to delete a value from a test table on your PostgreSQL database server. We tried deleting a value with the command %query and PostgreSQL reported the following error: %error.', array('%query' => $query, '%error' => $error)), 'error');
  103. $err = TRUE;
  104. }
  105. else {
  106. $success[] = 'DELETE';
  107. }
  108. // Test DROP.
  109. $query = 'DROP TABLE drupal_install_test';
  110. $result = pg_query($connection, $query);
  111. if ($error = pg_result_error()) {
  112. drupal_set_message(st('Failed to drop a test table from your PostgreSQL database server. We tried dropping a table with the command %query and PostgreSQL reported the following error %error.', array('%query' => $query, '%error' => $error)), 'error');
  113. $err = TRUE;
  114. }
  115. else {
  116. $success[] = 'DROP';
  117. }
  118. if ($err) {
  119. return FALSE;
  120. }
  121. pg_close($connection);
  122. return TRUE;
  123. }