Update.php 2.9 KB

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