You are here

private function Insert::BuildQuery in Drupal driver for SQL Server and SQL Azure 8

Same name and namespace in other branches
  1. 8.2 drivers/lib/Drupal/Driver/Database/sqlsrv/Insert.php \Drupal\Driver\Database\sqlsrv\Insert::BuildQuery()
  2. 3.0.x drivers/lib/Drupal/Driver/Database/sqlsrv/Insert.php \Drupal\Driver\Database\sqlsrv\Insert::buildQuery()

The aspect of the query depends on the batch size...

Parameters

int $batch_size: The number of inserts to perform on a single statement.

Return value

string

Throws

Exception

2 calls to Insert::BuildQuery()
Insert::execute in drivers/lib/Drupal/Driver/Database/sqlsrv/Insert.php
Executes the insert query.
Insert::__toString in drivers/lib/Drupal/Driver/Database/sqlsrv/Insert.php
Implements PHP magic __toString method to convert the query to a string.

File

drivers/lib/Drupal/Driver/Database/sqlsrv/Insert.php, line 196
Definition of Drupal\Driver\Database\sqlsrv\Insert

Class

Insert

Namespace

Drupal\Driver\Database\sqlsrv

Code

private function BuildQuery($batch_size) {

  // Make sure we don't go crazy with this numbers.
  if ($batch_size > Insert::MAX_BATCH_SIZE) {
    throw new \Exception("MSSQL Native Batch Insert limited to 250.");
  }

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

  // Create a sanitized comment string to prepend to the query.
  $prefix = $this->connection
    ->makeComment($this->comments);
  $output = NULL;

  // Enable direct insertion to identity columns if necessary.
  if (!empty($this->setIdentity)) {
    $prefix .= 'SET IDENTITY_INSERT {' . $this->table . '} ON;';
  }

  // Using PDO->lastInsertId() is not reliable on highly concurrent scenarios.
  // It is much better to use the OUTPUT option of SQL Server.
  if (isset($columnInformation['identities']) && !empty($columnInformation['identities'])) {
    $identities = array_keys($columnInformation['identities']);
    $identity = reset($identities);
    $output = "OUTPUT (Inserted.{$identity})";
  }
  else {
    $output = "OUTPUT (1)";
  }

  // If we're selecting from a SelectQuery, finish building the query and
  // pass it back, as any remaining options are irrelevant.
  if (!empty($this->fromQuery)) {
    if (empty($this->insertFields)) {
      return $prefix . "INSERT INTO {{$this->table}} {$output}" . $this->fromQuery;
    }
    else {
      $fields_csv = implode(', ', $this->connection
        ->quoteIdentifiers($this->insertFields));
      return $prefix . "INSERT INTO {{$this->table}} ({$fields_csv}) {$output} " . $this->fromQuery;
    }
  }

  // Full default insert
  if (empty($this->insertFields)) {
    return $prefix . "INSERT INTO {{$this->table}} {$output} DEFAULT VALUES";
  }

  // Build the list of placeholders, a set of placeholders
  // for each element in the batch.
  $placeholders = array();
  $field_count = count($this->insertFields);
  for ($j = 0; $j < $batch_size; $j++) {
    $batch_placeholders = array();
    for ($i = 0; $i < $field_count; ++$i) {
      $batch_placeholders[] = ':db_insert' . ($field_count * $j + $i);
    }
    $placeholders[] = '(' . implode(', ', $batch_placeholders) . ')';
  }
  $sql = $prefix . 'INSERT INTO {' . $this->table . '} (' . implode(', ', $this->connection
    ->quoteIdentifiers($this->insertFields)) . ') ' . $output . ' VALUES ' . PHP_EOL;
  $sql .= implode(', ', $placeholders) . PHP_EOL;
  return $sql;
}