You are here

private function InsertQuery_sqlsrv::__toString2 in Drupal driver for SQL Server and SQL Azure 7.2

Same name and namespace in other branches
  1. 7.3 sqlsrv/query.inc \InsertQuery_sqlsrv::__toString2()

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

Parameters

mixed $batch_size:

Return value

string

Throws

Exception

2 calls to InsertQuery_sqlsrv::__toString2()
InsertQuery_sqlsrv::execute in sqlsrv/query.inc
Executes the insert query.
InsertQuery_sqlsrv::__toString in sqlsrv/query.inc
Implements PHP magic __toString method to convert the query to a string.

File

sqlsrv/query.inc, line 185

Class

InsertQuery_sqlsrv
SQL Server-specific implementation of INSERT.

Code

private function __toString2($batch_size) {

  // Make sure we don't go crazy with this numbers.
  if ($batch_size > 250) {
    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 {

    // Empty is the default for
    // a missing lastInsertId()
    $output = "OUTPUT ('')";
  }
  if ($this->use_output === FALSE) {
    $output = '';
  }

  // 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;
}