You are here

public function Schema::findTables in Drupal 10

Same name in this branch
  1. 10 core/lib/Drupal/Core/Database/Schema.php \Drupal\Core\Database\Schema::findTables()
  2. 10 core/modules/sqlite/src/Driver/Database/sqlite/Schema.php \Drupal\sqlite\Driver\Database\sqlite\Schema::findTables()
  3. 10 core/modules/pgsql/src/Driver/Database/pgsql/Schema.php \Drupal\pgsql\Driver\Database\pgsql\Schema::findTables()

Finds all tables that are like the specified base table name.

Parameters

string $table_expression: A case-insensitive pattern against which table names are compared. Both '_' and '%' are treated like wildcards in MySQL 'LIKE' expressions, where '_' matches any single character and '%' matches an arbitrary number of characters (including zero characters). So 'foo%bar' matches table names like 'foobar', 'fooXBar', 'fooXBaR', or 'fooXxBar'; whereas 'foo_bar' matches 'fooXBar' and 'fooXBaR' but not 'fooBar' or 'fooXxxBar'.

Return value

array Both the keys and the values are the matching tables.

Overrides Schema::findTables

File

core/modules/sqlite/src/Driver/Database/sqlite/Schema.php, line 815

Class

Schema
SQLite implementation of \Drupal\Core\Database\Schema.

Namespace

Drupal\sqlite\Driver\Database\sqlite

Code

public function findTables($table_expression) {
  $tables = [];

  // The SQLite implementation doesn't need to use the same filtering strategy
  // as the parent one because individually prefixed tables live in their own
  // schema (database), which means that neither the main database nor any
  // attached one will contain a prefixed table name, so we just need to loop
  // over all known schemas and filter by the user-supplied table expression.
  $attached_dbs = $this->connection
    ->getAttachedDatabases();
  foreach ($attached_dbs as $schema) {

    // Can't use query placeholders for the schema because the query would
    // have to be :prefixsqlite_master, which does not work. We also need to
    // ignore the internal SQLite tables.
    $result = $this->connection
      ->query("SELECT name FROM " . $schema . ".sqlite_master WHERE type = :type AND name LIKE :table_name AND name NOT LIKE :pattern", [
      ':type' => 'table',
      ':table_name' => $table_expression,
      ':pattern' => 'sqlite_%',
    ]);
    $tables += $result
      ->fetchAllKeyed(0, 0);
  }
  return $tables;
}