[ Index ]

PHP Cross Reference of Drupal 6 (yi-drupal)

title

Body

[close]

/includes/ -> install.pgsql.inc (source)

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


Generated: Mon Jul 9 18:01:44 2012 Cross-referenced by PHPXref 0.7