class Upsert

Same name in this branch
  1. 8.9.x core/modules/system/tests/modules/driver_test/src/Driver/Database/DrivertestMysql/Upsert.php \Drupal\driver_test\Driver\Database\DrivertestMysql\Upsert
  2. 8.9.x core/modules/system/tests/modules/driver_test/src/Driver/Database/DrivertestPgsql/Upsert.php \Drupal\driver_test\Driver\Database\DrivertestPgsql\Upsert
  3. 8.9.x core/lib/Drupal/Core/Database/Driver/sqlite/Upsert.php \Drupal\Core\Database\Driver\sqlite\Upsert
  4. 8.9.x core/lib/Drupal/Core/Database/Driver/mysql/Upsert.php \Drupal\Core\Database\Driver\mysql\Upsert
  5. 8.9.x core/lib/Drupal/Core/Database/Query/Upsert.php \Drupal\Core\Database\Query\Upsert
Same name and namespace in other branches
  1. 11.x core/modules/sqlite/src/Driver/Database/sqlite/Upsert.php \Drupal\sqlite\Driver\Database\sqlite\Upsert
  2. 11.x core/modules/mysql/src/Driver/Database/mysql/Upsert.php \Drupal\mysql\Driver\Database\mysql\Upsert
  3. 11.x core/modules/pgsql/src/Driver/Database/pgsql/Upsert.php \Drupal\pgsql\Driver\Database\pgsql\Upsert
  4. 11.x core/tests/fixtures/database_drivers/module/core_fake/src/Driver/Database/CoreFakeWithAllCustomClasses/Upsert.php \Drupal\core_fake\Driver\Database\CoreFakeWithAllCustomClasses\Upsert
  5. 11.x core/lib/Drupal/Core/Database/Query/Upsert.php \Drupal\Core\Database\Query\Upsert
  6. 10 core/modules/sqlite/src/Driver/Database/sqlite/Upsert.php \Drupal\sqlite\Driver\Database\sqlite\Upsert
  7. 10 core/modules/mysql/src/Driver/Database/mysql/Upsert.php \Drupal\mysql\Driver\Database\mysql\Upsert
  8. 10 core/modules/pgsql/src/Driver/Database/pgsql/Upsert.php \Drupal\pgsql\Driver\Database\pgsql\Upsert
  9. 10 core/tests/fixtures/database_drivers/module/core_fake/src/Driver/Database/CoreFakeWithAllCustomClasses/Upsert.php \Drupal\core_fake\Driver\Database\CoreFakeWithAllCustomClasses\Upsert
  10. 10 core/lib/Drupal/Core/Database/Driver/sqlite/Upsert.php \Drupal\Core\Database\Driver\sqlite\Upsert
  11. 10 core/lib/Drupal/Core/Database/Driver/mysql/Upsert.php \Drupal\Core\Database\Driver\mysql\Upsert
  12. 10 core/lib/Drupal/Core/Database/Driver/pgsql/Upsert.php \Drupal\Core\Database\Driver\pgsql\Upsert
  13. 10 core/lib/Drupal/Core/Database/Query/Upsert.php \Drupal\Core\Database\Query\Upsert
  14. 9 core/modules/sqlite/src/Driver/Database/sqlite/Upsert.php \Drupal\sqlite\Driver\Database\sqlite\Upsert
  15. 9 core/modules/mysql/src/Driver/Database/mysql/Upsert.php \Drupal\mysql\Driver\Database\mysql\Upsert
  16. 9 core/modules/system/tests/modules/driver_test/src/Driver/Database/DrivertestMysql/Upsert.php \Drupal\driver_test\Driver\Database\DrivertestMysql\Upsert
  17. 9 core/modules/system/tests/modules/driver_test/src/Driver/Database/DrivertestMysqlDeprecatedVersion/Upsert.php \Drupal\driver_test\Driver\Database\DrivertestMysqlDeprecatedVersion\Upsert
  18. 9 core/modules/system/tests/modules/driver_test/src/Driver/Database/DrivertestPgsql/Upsert.php \Drupal\driver_test\Driver\Database\DrivertestPgsql\Upsert
  19. 9 core/modules/pgsql/src/Driver/Database/pgsql/Upsert.php \Drupal\pgsql\Driver\Database\pgsql\Upsert
  20. 9 core/tests/fixtures/database_drivers/module/corefake/src/Driver/Database/corefakeWithAllCustomClasses/Upsert.php \Drupal\corefake\Driver\Database\corefakeWithAllCustomClasses\Upsert
  21. 9 core/lib/Drupal/Core/Database/Driver/sqlite/Upsert.php \Drupal\Core\Database\Driver\sqlite\Upsert
  22. 9 core/lib/Drupal/Core/Database/Driver/mysql/Upsert.php \Drupal\Core\Database\Driver\mysql\Upsert
  23. 9 core/lib/Drupal/Core/Database/Driver/pgsql/Upsert.php \Drupal\Core\Database\Driver\pgsql\Upsert
  24. 9 core/lib/Drupal/Core/Database/Query/Upsert.php \Drupal\Core\Database\Query\Upsert

PostgreSQL implementation of \Drupal\Core\Database\Query\Upsert.

Hierarchy

Expanded class hierarchy of Upsert

1 file declares its use of Upsert
Upsert.php in core/modules/system/tests/modules/driver_test/src/Driver/Database/DrivertestPgsql/Upsert.php
3 string references to 'Upsert'
Connection::upsert in core/lib/Drupal/Core/Database/Driver/pgsql/Connection.php
Connection::upsert in core/lib/Drupal/Core/Database/Connection.php
Prepares and returns an UPSERT query object.
Connection::__construct in core/modules/system/tests/modules/driver_test/src/Driver/Database/DrivertestMysqlDeprecatedVersion/Connection.php
Constructs a Connection object.

File

core/lib/Drupal/Core/Database/Driver/pgsql/Upsert.php, line 10

Namespace

Drupal\Core\Database\Driver\pgsql
View source
class Upsert extends QueryUpsert {
  
  /**
   * {@inheritdoc}
   */
  public function execute() {
    if (!$this->preExecute()) {
      return NULL;
    }
    // Default options for upsert queries.
    $this->queryOptions += [
      'throw_exception' => TRUE,
    ];
    // Default fields are always placed first for consistency.
    $insert_fields = array_merge($this->defaultFields, $this->insertFields);
    $table = $this->connection
      ->escapeTable($this->table);
    // We have to execute multiple queries, therefore we wrap everything in a
    // transaction so that it is atomic where possible.
    $transaction = $this->connection
      ->startTransaction();
    try {
      // First, lock the table we're upserting into.
      $this->connection
        ->query('LOCK TABLE {' . $table . '} IN SHARE ROW EXCLUSIVE MODE', [], $this->queryOptions);
      // Second, delete all items first so we can do one insert.
      $unique_key_position = array_search($this->key, $insert_fields);
      $delete_ids = [];
      foreach ($this->insertValues as $insert_values) {
        $delete_ids[] = $insert_values[$unique_key_position];
      }
      // Delete in chunks when a large array is passed.
      foreach (array_chunk($delete_ids, 1000) as $delete_ids_chunk) {
        $this->connection
          ->delete($this->table, $this->queryOptions)
          ->condition($this->key, $delete_ids_chunk, 'IN')
          ->execute();
      }
      // Third, insert all the values.
      $insert = $this->connection
        ->insert($this->table, $this->queryOptions)
        ->fields($insert_fields);
      foreach ($this->insertValues as $insert_values) {
        $insert->values($insert_values);
      }
      $insert->execute();
    } catch (\Exception $e) {
      // One of the queries failed, rollback the whole batch.
      $transaction->rollBack();
      // Rethrow the exception for the calling code.
      throw $e;
    }
    // Re-initialize the values array so that we can re-use this query.
    $this->insertValues = [];
    // Transaction commits here where $transaction looses scope.
    return TRUE;
  }
  
  /**
   * {@inheritdoc}
   */
  public function __toString() {
    // Nothing to do.
  }

}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.