You are here

public function Upsert::execute in Drupal driver for SQL Server and SQL Azure 8.2

Same name and namespace in other branches
  1. 8 drivers/lib/Drupal/Driver/Database/sqlsrv/Upsert.php \Drupal\Driver\Database\sqlsrv\Upsert::execute()
  2. 3.0.x drivers/lib/Drupal/Driver/Database/sqlsrv/Upsert.php \Drupal\Driver\Database\sqlsrv\Upsert::execute()

Runs the query against the database.

Return value

\Drupal\Core\Database\StatementInterface|null A prepared statement, or NULL if the query is not valid.

Overrides Upsert::execute

File

drivers/lib/Drupal/Driver/Database/sqlsrv/Upsert.php, line 33
Contains \Drupal\Core\Database\Driver\sqlsrv\Upsert

Class

Upsert
Implements Native Upsert queries for MSSQL.

Namespace

Drupal\Driver\Database\sqlsrv

Code

public function execute() {

  // Check that the table does exist.
  if (!$this->connection
    ->schema()
    ->tableExists($this->table)) {
    throw new \Drupal\Core\Database\SchemaObjectDoesNotExistException("Table {$this->table} does not exist.");
  }

  // Retrieve query options.
  $options = $this->queryOptions;

  // Initialize result array.
  $this->result = [];

  // Keep a reference to the blobs.
  $blobs = [];

  // Fetch the list of blobs and sequences used on that table.
  $columnInformation = $this->connection
    ->schema()
    ->getTableIntrospection($this->table);

  // Initialize placeholder count.
  $max_placeholder = 0;

  // Build the query, ensure that we have retries for concurrency control
  $options['integrityretry'] = true;
  $options['prefix_tables'] = false;
  if (count($this->insertValues) >= 2000) {
    $options['insecure'] = true;
  }
  $stmt = $this->connection
    ->prepareQuery((string) $this, $options);

  // 3. Bind the dataset.
  foreach ($this->insertValues as $insert_values) {
    $fields = array_combine($this->insertFields, $insert_values);
    $stmt
      ->BindValues($fields, $blobs, ':db_insert_placeholder_', $columnInformation, $max_placeholder);
  }

  // 4. Run the query, this will return UPDATE or INSERT
  $this->connection
    ->query($stmt, []);

  // Captura the results.
  foreach ($stmt as $value) {
    $this->result[] = $value->{'$action'};
  }

  // Re-initialize the values array so that we can re-use this query.
  $this->insertValues = [];
  return true;
}