database.inc 3.62 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
18
19
20
21
22
23
    // This driver defaults to transaction support, except if explicitly passed FALSE.
    $this->transactionSupport = !isset($connection_options['transactions']) || $connection_options['transactions'] === FALSE;

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

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

34
  public function query($query, array $args = array(), $options = array()) {
35
36

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

38
    try {
39
      if ($query instanceof DatabaseStatementInterface) {
40
41
42
43
44
45
46
        $stmt = $query;
        $stmt->execute(NULL, $options);
      }
      else {
        $stmt = $this->prepareQuery($query);
        $stmt->execute($args, $options);
      }
47

48
49
50
51
52
53
54
55
56
57
58
59
60
61
      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) {
62
      _db_check_install_needed();
63
      if ($options['throw_exception']) {
64
65
        if ($query instanceof DatabaseStatementInterface) {
          $query_string = $stmt->getQueryString();
66
67
68
69
70
71
72
73
74
        }
        else {
          $query_string = $query;
        }
        throw new PDOException($query_string . " - \n" . print_r($args,1) . $e->getMessage());
      }
      return NULL;
    }
  }
75

76
  public function queryRange($query, array $args, $from, $count, array $options = array()) {
77
78
79
    return $this->query($query . ' LIMIT ' . $count . ' OFFSET ' . $from, $args, $options);
  }

80
  public function queryTemporary($query, array $args, $tablename, array $options = array()) {
81
    return $this->query(preg_replace('/^SELECT/i', 'CREATE TEMPORARY TABLE ' . $tablename . ' AS SELECT', $query), $args, $options);
82
83
84
85
86
87
88
89
90
91
92
93
  }

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

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

  public function mapConditionOperator($operator) {
    static $specials = array(
94
      // In PostgreSQL, 'LIKE' is case-sensitive. For case-insensitive LIKE
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
      // statements, we need to use ILIKE instead.
      'LIKE' => array('operator' => 'ILIKE'),
    );

    return isset($specials[$operator]) ? $specials[$operator] : NULL;
  }

  /**
   * @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".
 */