annotate includes/install.pgsql.inc @ 20:e3d20ebd63d1 tip

Added tag 6.9 for changeset 3edae6ecd6c6
author Franck Deroche <franck@defr.org>
date Thu, 15 Jan 2009 10:16:10 +0100
parents c1f4ac30525a
children
rev   line source
webmaster@1 1 <?php
webmaster@1 2 // $Id: install.pgsql.inc,v 1.6 2007/10/22 15:22:39 dries Exp $
webmaster@1 3
webmaster@1 4 // PostgreSQL specific install functions
webmaster@1 5
webmaster@1 6 /**
webmaster@1 7 * Check if PostgreSQL is available.
webmaster@1 8 *
webmaster@1 9 * @return
webmaster@1 10 * TRUE/FALSE
webmaster@1 11 */
webmaster@1 12 function pgsql_is_available() {
webmaster@1 13 return function_exists('pg_connect');
webmaster@1 14 }
webmaster@1 15
webmaster@1 16 /**
webmaster@1 17 * Check if we can connect to PostgreSQL.
webmaster@1 18 *
webmaster@1 19 * @return
webmaster@1 20 * TRUE/FALSE
webmaster@1 21 */
webmaster@1 22 function drupal_test_pgsql($url, &$success) {
webmaster@1 23 if (!pgsql_is_available()) {
webmaster@1 24 drupal_set_message(st('PHP PostgreSQL support not enabled.'), 'error');
webmaster@1 25 return FALSE;
webmaster@1 26 }
webmaster@1 27
webmaster@1 28 $url = parse_url($url);
webmaster@1 29 $conn_string = '';
webmaster@1 30
webmaster@1 31 // Decode url-encoded information in the db connection string
webmaster@1 32 if (isset($url['user'])) {
webmaster@1 33 $conn_string .= ' user='. urldecode($url['user']);
webmaster@1 34 }
webmaster@1 35 if (isset($url['pass'])) {
webmaster@1 36 $conn_string .= ' password='. urldecode($url['pass']);
webmaster@1 37 }
webmaster@1 38 if (isset($url['host'])) {
webmaster@1 39 $conn_string .= ' host='. urldecode($url['host']);
webmaster@1 40 }
webmaster@1 41 if (isset($url['path'])) {
webmaster@1 42 $conn_string .= ' dbname='. substr(urldecode($url['path']), 1);
webmaster@1 43 }
webmaster@1 44 if (isset($url['port'])) {
webmaster@1 45 $conn_string .= ' port='. urldecode($url['port']);
webmaster@1 46 }
webmaster@1 47
webmaster@1 48 // Test connecting to the database.
webmaster@1 49 $connection = @pg_connect($conn_string);
webmaster@1 50 if (!$connection) {
webmaster@1 51 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');
webmaster@1 52 return FALSE;
webmaster@1 53 }
webmaster@1 54
webmaster@1 55 $success = array('CONNECT');
webmaster@1 56
webmaster@1 57 // Test CREATE.
webmaster@1 58 $query = 'CREATE TABLE drupal_install_test (id integer NOT NULL)';
webmaster@1 59 $result = pg_query($connection, $query);
webmaster@1 60 if ($error = pg_result_error($result)) {
webmaster@1 61 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');
webmaster@1 62 return FALSE;
webmaster@1 63 }
webmaster@1 64 $err = FALSE;
webmaster@1 65 $success[] = 'SELECT';
webmaster@1 66 $success[] = 'CREATE';
webmaster@1 67
webmaster@1 68 // Test INSERT.
webmaster@1 69 $query = 'INSERT INTO drupal_install_test (id) VALUES (1)';
webmaster@1 70 $result = pg_query($connection, $query);
webmaster@1 71 if ($error = pg_result_error($result)) {
webmaster@1 72 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');
webmaster@1 73 $err = TRUE;
webmaster@1 74 }
webmaster@1 75 else {
webmaster@1 76 $success[] = 'INSERT';
webmaster@1 77 }
webmaster@1 78
webmaster@1 79 // Test UPDATE.
webmaster@1 80 $query = 'UPDATE drupal_install_test SET id = 2';
webmaster@1 81 $result = pg_query($connection, $query);
webmaster@1 82 if ($error = pg_result_error($result)) {
webmaster@1 83 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');
webmaster@1 84 $err = TRUE;
webmaster@1 85 }
webmaster@1 86 else {
webmaster@1 87 $success[] = 'UPDATE';
webmaster@1 88 }
webmaster@1 89
webmaster@1 90 // Test LOCK.
webmaster@1 91 $query = 'BEGIN; LOCK drupal_install_test IN SHARE ROW EXCLUSIVE MODE';
webmaster@1 92 $result = pg_query($connection, $query);
webmaster@1 93 if ($error = pg_result_error($result)) {
webmaster@1 94 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');
webmaster@1 95 $err = TRUE;
webmaster@1 96 }
webmaster@1 97 else {
webmaster@1 98 $success[] = 'LOCK';
webmaster@1 99 }
webmaster@1 100
webmaster@1 101 // Test UNLOCK, which is done automatically upon transaction end in PostgreSQL
webmaster@1 102 $query = 'COMMIT';
webmaster@1 103 $result = pg_query($connection, $query);
webmaster@1 104 if ($error = pg_result_error()) {
webmaster@1 105 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');
webmaster@1 106 $err = TRUE;
webmaster@1 107 }
webmaster@1 108 else {
webmaster@1 109 $success[] = 'UNLOCK';
webmaster@1 110 }
webmaster@1 111
webmaster@1 112 // Test DELETE.
webmaster@1 113 $query = 'DELETE FROM drupal_install_test';
webmaster@1 114 $result = pg_query($connection, $query);
webmaster@1 115 if ($error = pg_result_error()) {
webmaster@1 116 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');
webmaster@1 117 $err = TRUE;
webmaster@1 118 }
webmaster@1 119 else {
webmaster@1 120 $success[] = 'DELETE';
webmaster@1 121 }
webmaster@1 122
webmaster@1 123 // Test DROP.
webmaster@1 124 $query = 'DROP TABLE drupal_install_test';
webmaster@1 125 $result = pg_query($connection, $query);
webmaster@1 126 if ($error = pg_result_error()) {
webmaster@1 127 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');
webmaster@1 128 $err = TRUE;
webmaster@1 129 }
webmaster@1 130 else {
webmaster@1 131 $success[] = 'DROP';
webmaster@1 132 }
webmaster@1 133
webmaster@1 134 if ($err) {
webmaster@1 135 return FALSE;
webmaster@1 136 }
webmaster@1 137
webmaster@1 138 pg_close($connection);
webmaster@1 139 return TRUE;
webmaster@1 140 }