You are here

public function Scheme::TableExists in Drupal driver for SQL Server and SQL Azure 8.2

Check if a table already exists.

Parameters

string $table: Name of the table.

Return value

boolean True if the table exists, false otherwise.

2 calls to Scheme::TableExists()
Scheme::TableDetailsGet in drivers/lib/Drupal/Driver/Database/sqlsrv/Scheme.php
Database introspection: fetch technical information about a table.
Scheme::TableDrop in drivers/lib/Drupal/Driver/Database/sqlsrv/Scheme.php
Drop a table.

File

drivers/lib/Drupal/Driver/Database/sqlsrv/Scheme.php, line 227

Class

Scheme

Namespace

Drupal\Driver\Database\sqlsrv

Code

public function TableExists($table, $refresh = false) {

  // Account for empty table names..
  if (empty($table)) {
    return false;
  }
  $bin = $this->cnn
    ->Cache('sqlsrv-table-exists');
  if (!$bin
    ->Get('@@preloaded')) {
    foreach ($this->cnn
      ->query_execute("SELECT table_name FROM INFORMATION_SCHEMA.tables") as $t) {
      $bin
        ->Set($t->table_name, true);
    }
    $bin
      ->Set('@@preloaded', true);
  }
  if (!$refresh && ($cache = $bin
    ->Get($table))) {
    return $cache->data;
  }

  // Temporary tables and regular tables cannot be verified in the same way.
  $query = null;
  if ($table[0] == '#') {
    $table .= '%';
    $query = "SELECT 1 FROM tempdb.sys.tables WHERE name like :table";
  }
  else {
    $query = "SELECT 1 FROM INFORMATION_SCHEMA.tables WHERE table_name = :table";
  }
  $exists = $this->cnn
    ->query_execute($query, [
    ':table' => $table,
  ])
    ->fetchField() !== false;
  if ($exists) {
    $bin
      ->Set($table, $exists);
  }
  return $exists;
}