Version 1
[yaffs-website] / web / core / lib / Drupal / Core / Database / Driver / pgsql / Update.php
1 <?php
2
3 namespace Drupal\Core\Database\Driver\pgsql;
4
5 use Drupal\Core\Database\Database;
6 use Drupal\Core\Database\Query\Update as QueryUpdate;
7 use Drupal\Core\Database\Query\SelectInterface;
8
9 /**
10  * PostgreSQL implementation of \Drupal\Core\Database\Query\Update.
11  */
12 class Update extends QueryUpdate {
13
14   public function execute() {
15     $max_placeholder = 0;
16     $blobs = [];
17     $blob_count = 0;
18
19     // Because we filter $fields the same way here and in __toString(), the
20     // placeholders will all match up properly.
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     // Expressions take priority over literal fields, so we process those first
27     // and remove any literal fields that conflict.
28     $fields = $this->fields;
29     foreach ($this->expressionFields as $field => $data) {
30       if (!empty($data['arguments'])) {
31         foreach ($data['arguments'] as $placeholder => $argument) {
32           // We assume that an expression will never happen on a BLOB field,
33           // which is a fairly safe assumption to make since in most cases
34           // it would be an invalid query anyway.
35           $stmt->bindParam($placeholder, $data['arguments'][$placeholder]);
36         }
37       }
38       if ($data['expression'] instanceof SelectInterface) {
39         $data['expression']->compile($this->connection, $this);
40         $select_query_arguments = $data['expression']->arguments();
41         foreach ($select_query_arguments as $placeholder => $argument) {
42           $stmt->bindParam($placeholder, $select_query_arguments[$placeholder]);
43         }
44       }
45       unset($fields[$field]);
46     }
47
48     foreach ($fields as $field => $value) {
49       $placeholder = ':db_update_placeholder_' . ($max_placeholder++);
50
51       if (isset($table_information->blob_fields[$field])) {
52         $blobs[$blob_count] = fopen('php://memory', 'a');
53         fwrite($blobs[$blob_count], $value);
54         rewind($blobs[$blob_count]);
55         $stmt->bindParam($placeholder, $blobs[$blob_count], \PDO::PARAM_LOB);
56         ++$blob_count;
57       }
58       else {
59         $stmt->bindParam($placeholder, $fields[$field]);
60       }
61     }
62
63     if (count($this->condition)) {
64       $this->condition->compile($this->connection, $this);
65
66       $arguments = $this->condition->arguments();
67       foreach ($arguments as $placeholder => $value) {
68         $stmt->bindParam($placeholder, $arguments[$placeholder]);
69       }
70     }
71
72     $options = $this->queryOptions;
73     $options['already_prepared'] = TRUE;
74     $options['return'] = Database::RETURN_AFFECTED;
75
76     $this->connection->addSavepoint();
77     try {
78       $result = $this->connection->query($stmt, [], $options);
79       $this->connection->releaseSavepoint();
80       return $result;
81     }
82     catch (\Exception $e) {
83       $this->connection->rollbackSavepoint();
84       throw $e;
85     }
86   }
87
88 }