public function Schema::findTables in Drupal 10
Same name in this branch
- 10 core/lib/Drupal/Core/Database/Schema.php \Drupal\Core\Database\Schema::findTables()
- 10 core/modules/sqlite/src/Driver/Database/sqlite/Schema.php \Drupal\sqlite\Driver\Database\sqlite\Schema::findTables()
- 10 core/modules/pgsql/src/Driver/Database/pgsql/Schema.php \Drupal\pgsql\Driver\Database\pgsql\Schema::findTables()
Same name and namespace in other branches
- 8 core/lib/Drupal/Core/Database/Schema.php \Drupal\Core\Database\Schema::findTables()
- 9 core/lib/Drupal/Core/Database/Schema.php \Drupal\Core\Database\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.
2 methods override Schema::findTables()
- Schema::findTables in core/
modules/ sqlite/ src/ Driver/ Database/ sqlite/ Schema.php - Finds all tables that are like the specified base table name.
- Schema::findTables in core/
modules/ pgsql/ src/ Driver/ Database/ pgsql/ Schema.php - Finds all tables that are like the specified base table name.
File
- core/
lib/ Drupal/ Core/ Database/ Schema.php, line 193
Class
- Schema
- Provides a base implementation for Database Schema.
Namespace
Drupal\Core\DatabaseCode
public function findTables($table_expression) {
// Load all the tables up front in order to take into account per-table
// prefixes. The actual matching is done at the bottom of the method.
$condition = $this
->buildTableNameCondition('%', 'LIKE');
$condition
->compile($this->connection, $this);
$prefix = $this->connection
->tablePrefix();
$prefix_length = strlen($prefix);
$tables = [];
// Normally, we would heartily discourage the use of string
// concatenation for conditionals like this however, we
// couldn't use \Drupal::database()->select() here because it would prefix
// information_schema.tables and the query would fail.
// Don't use {} around information_schema.tables table.
$results = $this->connection
->query("SELECT table_name AS table_name FROM information_schema.tables WHERE " . (string) $condition, $condition
->arguments());
foreach ($results as $table) {
if ($prefix && substr($table->table_name, 0, $prefix_length) !== $prefix) {
// This table name does not start the prefix, which means that it is
// not managed by Drupal so it should be excluded from the result.
continue;
}
// Remove the prefix from the returned tables.
$unprefixed_table_name = substr($table->table_name, $prefix_length);
// The pattern can match a table which is the same as the prefix. That
// will become an empty string when we remove the prefix, which will
// probably surprise the caller, besides not being a prefixed table. So
// remove it.
if (!empty($unprefixed_table_name)) {
$tables[$unprefixed_table_name] = $unprefixed_table_name;
}
}
// Convert the table expression from its SQL LIKE syntax to a regular
// expression and escape the delimiter that will be used for matching.
$table_expression = str_replace([
'%',
'_',
], [
'.*?',
'.',
], preg_quote($table_expression, '/'));
$tables = preg_grep('/^' . $table_expression . '$/i', $tables);
return $tables;
}