Projet

Général

Profil

Paste
Télécharger (7,69 ko) Statistiques
| Branche: | Révision:

root / drupal7 / includes / database / pgsql / query.inc @ bfb52287

1
<?php
2

    
3
/**
4
 * @ingroup database
5
 * @{
6
 */
7

    
8
/**
9
 * @file
10
 * Query code for PostgreSQL embedded database engine.
11
 */
12

    
13

    
14
class InsertQuery_pgsql extends InsertQuery {
15

    
16
  public function execute() {
17
    if (!$this->preExecute()) {
18
      return NULL;
19
    }
20

    
21
    $stmt = $this->connection->prepareQuery((string) $this);
22

    
23
    // Fetch the list of blobs and sequences used on that table.
24
    $table_information = $this->connection->schema()->queryTableInformation($this->table);
25

    
26
    $max_placeholder = 0;
27
    $blobs = array();
28
    $blob_count = 0;
29
    foreach ($this->insertValues as $insert_values) {
30
      foreach ($this->insertFields as $idx => $field) {
31
        if (isset($table_information->blob_fields[$field])) {
32
          $blobs[$blob_count] = fopen('php://memory', 'a');
33
          fwrite($blobs[$blob_count], $insert_values[$idx]);
34
          rewind($blobs[$blob_count]);
35

    
36
          $stmt->bindParam(':db_insert_placeholder_' . $max_placeholder++, $blobs[$blob_count], PDO::PARAM_LOB);
37

    
38
          // Pre-increment is faster in PHP than increment.
39
          ++$blob_count;
40
        }
41
        else {
42
          $stmt->bindParam(':db_insert_placeholder_' . $max_placeholder++, $insert_values[$idx]);
43
        }
44
      }
45
      // Check if values for a serial field has been passed.
46
      if (!empty($table_information->serial_fields)) {
47
        foreach ($table_information->serial_fields as $index => $serial_field) {
48
          $serial_key = array_search($serial_field, $this->insertFields);
49
          if ($serial_key !== FALSE) {
50
            $serial_value = $insert_values[$serial_key];
51

    
52
            // Force $last_insert_id to the specified value. This is only done
53
            // if $index is 0.
54
            if ($index == 0) {
55
              $last_insert_id = $serial_value;
56
            }
57
            // Set the sequence to the bigger value of either the passed
58
            // value or the max value of the column. It can happen that another
59
            // thread calls nextval() which could lead to a serial number being
60
            // used twice. However, trying to insert a value into a serial
61
            // column should only be done in very rare cases and is not thread
62
            // safe by definition.
63
            $this->connection->query("SELECT setval('" . $table_information->sequences[$index] . "', GREATEST(MAX(" . $serial_field . "), :serial_value)) FROM {" . $this->table . "}", array(':serial_value' => (int)$serial_value));
64
          }
65
        }
66
      }
67
    }
68
    if (!empty($this->fromQuery)) {
69
      // bindParam stores only a reference to the variable that is followed when
70
      // the statement is executed. We pass $arguments[$key] instead of $value
71
      // because the second argument to bindParam is passed by reference and
72
      // the foreach statement assigns the element to the existing reference.
73
      $arguments = $this->fromQuery->getArguments();
74
      foreach ($arguments as $key => $value) {
75
        $stmt->bindParam($key, $arguments[$key]);
76
      }
77
    }
78

    
79
    // PostgreSQL requires the table name to be specified explicitly
80
    // when requesting the last insert ID, so we pass that in via
81
    // the options array.
82
    $options = $this->queryOptions;
83

    
84
    if (!empty($table_information->sequences)) {
85
      $options['sequence_name'] = $table_information->sequences[0];
86
    }
87
    // If there are no sequences then we can't get a last insert id.
88
    elseif ($options['return'] == Database::RETURN_INSERT_ID) {
89
      $options['return'] = Database::RETURN_NULL;
90
    }
91
    // Only use the returned last_insert_id if it is not already set.
92
    if (!empty($last_insert_id)) {
93
      $this->connection->query($stmt, array(), $options);
94
    }
95
    else {
96
      $last_insert_id = $this->connection->query($stmt, array(), $options);
97
    }
98

    
99
    // Re-initialize the values array so that we can re-use this query.
100
    $this->insertValues = array();
101

    
102
    return $last_insert_id;
103
  }
104

    
105
  public function __toString() {
106
    // Create a sanitized comment string to prepend to the query.
107
    $comments = $this->connection->makeComment($this->comments);
108

    
109
    // Default fields are always placed first for consistency.
110
    $insert_fields = array_merge($this->defaultFields, $this->insertFields);
111

    
112
    // If we're selecting from a SelectQuery, finish building the query and
113
    // pass it back, as any remaining options are irrelevant.
114
    if (!empty($this->fromQuery)) {
115
      $insert_fields_string = $insert_fields ? ' (' . implode(', ', $insert_fields) . ') ' : ' ';
116
      return $comments . 'INSERT INTO {' . $this->table . '}' . $insert_fields_string . $this->fromQuery;
117
    }
118

    
119
    $query = $comments . 'INSERT INTO {' . $this->table . '} (' . implode(', ', $insert_fields) . ') VALUES ';
120

    
121
    $max_placeholder = 0;
122
    $values = array();
123
      if (count($this->insertValues)) {
124
      foreach ($this->insertValues as $insert_values) {
125
        $placeholders = array();
126

    
127
        // Default fields aren't really placeholders, but this is the most convenient
128
        // way to handle them.
129
        $placeholders = array_pad($placeholders, count($this->defaultFields), 'default');
130

    
131
        $new_placeholder = $max_placeholder + count($insert_values);
132
        for ($i = $max_placeholder; $i < $new_placeholder; ++$i) {
133
          $placeholders[] = ':db_insert_placeholder_' . $i;
134
        }
135
        $max_placeholder = $new_placeholder;
136
        $values[] = '(' . implode(', ', $placeholders) . ')';
137
      }
138
    }
139
    else {
140
      // If there are no values, then this is a default-only query. We still need to handle that.
141
      $placeholders = array_fill(0, count($this->defaultFields), 'default');
142
      $values[] = '(' . implode(', ', $placeholders) . ')';
143
    }
144

    
145
    $query .= implode(', ', $values);
146

    
147
    return $query;
148
  }
149
}
150

    
151
class UpdateQuery_pgsql extends UpdateQuery {
152
  public function execute() {
153
    $max_placeholder = 0;
154
    $blobs = array();
155
    $blob_count = 0;
156

    
157
    // Because we filter $fields the same way here and in __toString(), the
158
    // placeholders will all match up properly.
159
    $stmt = $this->connection->prepareQuery((string) $this);
160

    
161
    // Fetch the list of blobs and sequences used on that table.
162
    $table_information = $this->connection->schema()->queryTableInformation($this->table);
163

    
164
    // Expressions take priority over literal fields, so we process those first
165
    // and remove any literal fields that conflict.
166
    $fields = $this->fields;
167
    $expression_fields = array();
168
    foreach ($this->expressionFields as $field => $data) {
169
      if (!empty($data['arguments'])) {
170
        foreach ($data['arguments'] as $placeholder => $argument) {
171
          // We assume that an expression will never happen on a BLOB field,
172
          // which is a fairly safe assumption to make since in most cases
173
          // it would be an invalid query anyway.
174
          $stmt->bindParam($placeholder, $data['arguments'][$placeholder]);
175
        }
176
      }
177
      unset($fields[$field]);
178
    }
179

    
180
    foreach ($fields as $field => $value) {
181
      $placeholder = ':db_update_placeholder_' . ($max_placeholder++);
182

    
183
      if (isset($table_information->blob_fields[$field])) {
184
        $blobs[$blob_count] = fopen('php://memory', 'a');
185
        fwrite($blobs[$blob_count], $value);
186
        rewind($blobs[$blob_count]);
187
        $stmt->bindParam($placeholder, $blobs[$blob_count], PDO::PARAM_LOB);
188
        ++$blob_count;
189
      }
190
      else {
191
        $stmt->bindParam($placeholder, $fields[$field]);
192
      }
193
    }
194

    
195
    if (count($this->condition)) {
196
      $this->condition->compile($this->connection, $this);
197

    
198
      $arguments = $this->condition->arguments();
199
      foreach ($arguments as $placeholder => $value) {
200
        $stmt->bindParam($placeholder, $arguments[$placeholder]);
201
      }
202
    }
203

    
204
    $options = $this->queryOptions;
205
    $options['already_prepared'] = TRUE;
206
    $this->connection->query($stmt, $options);
207

    
208
    return $stmt->rowCount();
209
  }
210
}