database.inc 4.04 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
<?php
// $Id$

/**
 * @file
 * Database interface code for PostgreSQL database servers.
 */

/**
 * @ingroup database
 * @{
 */

class DatabaseConnection_pgsql extends DatabaseConnection {

16
  public function __construct(array $connection_options = array()) {
17
    // This driver defaults to transaction support, except if explicitly passed FALSE.
18
    $this->transactionSupport = !isset($connection_options['transactions']) || ($connection_options['transactions'] !== FALSE);
Dries's avatar
Dries committed
19

20 21 22
    // Transactional DDL is always available in PostgreSQL,
    // but we'll only enable it if standard transactions are.
    $this->transactionalDDLSupport = $this->transactionSupport;
23 24 25 26 27

    // Default to TCP connection on port 5432.
    if (empty($connection_options['port'])) {
      $connection_options['port'] = 5432;
    }
28

29
    $dsn = 'pgsql:host=' . $connection_options['host'] . ' dbname=' . $connection_options['database'] . ' port=' . $connection_options['port'];
30 31 32
    parent::__construct($dsn, $connection_options['username'], $connection_options['password'], array(
      // Convert numeric values to strings when fetching.
      PDO::ATTR_STRINGIFY_FETCHES => TRUE,
33 34
      // Force column names to lower case.
      PDO::ATTR_CASE => PDO::CASE_LOWER,
35
    ));
36 37

    // Force PostgreSQL to use the UTF-8 character set by default.
38
    $this->exec("SET NAMES 'UTF8'");
39 40
  }

41
  public function query($query, array $args = array(), $options = array()) {
42 43

    $options += $this->defaultOptions();
44

45
    try {
46
      if ($query instanceof DatabaseStatementInterface) {
47 48 49 50
        $stmt = $query;
        $stmt->execute(NULL, $options);
      }
      else {
51 52
        $modified = $this->expandArguments($query, $args);
        $stmt = $this->prepareQuery($query, !$modified);
53 54
        $stmt->execute($args, $options);
      }
55

56 57 58 59 60 61 62 63 64 65 66 67 68 69
      switch ($options['return']) {
        case Database::RETURN_STATEMENT:
          return $stmt;
        case Database::RETURN_AFFECTED:
          return $stmt->rowCount();
        case Database::RETURN_INSERT_ID:
          return $this->lastInsertId($options['sequence_name']);
        case Database::RETURN_NULL:
          return;
        default:
          throw new PDOException('Invalid return directive: ' . $options['return']);
      }
    }
    catch (PDOException $e) {
70
      _db_check_install_needed();
71
      if ($options['throw_exception']) {
72 73
        if ($query instanceof DatabaseStatementInterface) {
          $query_string = $stmt->getQueryString();
74 75 76 77
        }
        else {
          $query_string = $query;
        }
78
        throw new PDOException($query_string . " - \n" . print_r($args, 1) . $e->getMessage());
79 80 81 82
      }
      return NULL;
    }
  }
83

84
  public function queryRange($query, array $args, $from, $count, array $options = array()) {
85 86 87
    return $this->query($query . ' LIMIT ' . $count . ' OFFSET ' . $from, $args, $options);
  }

88 89 90 91
  public function queryTemporary($query, array $args, array $options = array()) {
    $tablename = $this->generateTemporaryTableName();
    $this->query(preg_replace('/^SELECT/i', 'CREATE TEMPORARY TABLE {' . $tablename . '} AS SELECT', $query), $args, $options);
    return $tablename;
92 93 94 95 96 97 98 99 100 101 102
  }

  public function driver() {
    return 'pgsql';
  }

  public function databaseType() {
    return 'pgsql';
  }

  public function mapConditionOperator($operator) {
103 104 105 106 107 108 109
    static $specials = array(
      // In PostgreSQL, 'LIKE' is case-sensitive. For case-insensitive LIKE
      // statements, we need to use ILIKE instead.
      'LIKE' => array('operator' => 'ILIKE'),
    );

    return isset($specials[$operator]) ? $specials[$operator] : NULL;
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
  }

  /**
   * @todo Remove this as soon as db_rewrite_sql() has been exterminated.
   */
  public function distinctField($table, $field, $query) {
    $field_to_select = 'DISTINCT(' . $table . '.' . $field . ')';
    // (?<!text) is a negative look-behind (no need to rewrite queries that already use DISTINCT).
    return preg_replace('/(SELECT.*)(?:' . $table . '\.|\s)(?<!DISTINCT\()(?<!DISTINCT\(' . $table . '\.)' . $field . '(.*FROM )/AUsi', '\1 ' . $field_to_select . '\2', $query);
  }
}

/**
 * @} End of "ingroup database".
 */