You are here

public function Connection::nextId in Drupal driver for SQL Server and SQL Azure 8.2

Same name in this branch
  1. 8.2 drivers/lib/Drupal/Driver/Database/sqlsrv/Connection.php \Drupal\Driver\Database\sqlsrv\Connection::nextId()
  2. 8.2 drivers/lib/Drupal/Driver/Database/sqlsrv/PDO/Connection.php \Drupal\Driver\Database\sqlsrv\PDO\Connection::nextId()
Same name and namespace in other branches
  1. 8 drivers/lib/Drupal/Driver/Database/sqlsrv/Connection.php \Drupal\Driver\Database\sqlsrv\Connection::nextId()
  2. 3.0.x drivers/lib/Drupal/Driver/Database/sqlsrv/Connection.php \Drupal\Driver\Database\sqlsrv\Connection::nextId()

{@inhertidoc}

Overrides Connection::nextId

File

drivers/lib/Drupal/Driver/Database/sqlsrv/Connection.php, line 801
Definition of Drupal\Driver\Database\sqlsrv\Connection

Class

Connection
Temporary tables: temporary table support is done by means of global temporary tables (#) to avoid the use of DIRECT QUERIES. You can enable and disable the use of direct queries with $this->driver_settings->defaultDirectQuery =…

Namespace

Drupal\Driver\Database\sqlsrv

Code

public function nextId($existing = 0, $name = 'drupal') {
  if (version_compare($this
    ->Scheme()
    ->EngineVersion()
    ->Version(), '11', '>')) {

    // Native sequence support is only available for SLQ Server 2012 and beyound
    return $this->connection
      ->nextId($existing, $this
      ->prefixTable($name));
  }
  else {

    // If an exiting value is passed, for its insertion into the sequence table.
    if ($existing > 0) {
      $exists = $this
        ->query_direct("SELECT COUNT(*) FROM {sequences} WHERE value = :existing", [
        ':existing' => $existing,
      ])
        ->fetchField();
      if (!$exists) {
        $this
          ->query_direct('SET IDENTITY_INSERT {sequences} ON; INSERT INTO {sequences} (value) VALUES(:existing); SET IDENTITY_INSERT {sequences} OFF', [
          ':existing' => $existing,
        ]);
      }
    }

    // Refactored to use OUTPUT because under high concurrency LAST_INSERTED_ID does not work properly.
    return $this
      ->query_direct('INSERT INTO {sequences} OUTPUT (Inserted.[value]) DEFAULT VALUES')
      ->fetchField();
  }
}