You are here

public function Schema::findTables in Drupal 9

Same name in this branch
  1. 9 core/lib/Drupal/Core/Database/Schema.php \Drupal\Core\Database\Schema::findTables()
  2. 9 core/lib/Drupal/Core/Database/Driver/sqlite/Schema.php \Drupal\Core\Database\Driver\sqlite\Schema::findTables()
  3. 9 core/lib/Drupal/Core/Database/Driver/pgsql/Schema.php \Drupal\Core\Database\Driver\pgsql\Schema::findTables()
Same name and namespace in other branches
  1. 8 core/lib/Drupal/Core/Database/Driver/pgsql/Schema.php \Drupal\Core\Database\Driver\pgsql\Schema::findTables()

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

Parameters

string $table_expression: An SQL expression, for example "cache_%" (without the quotes).

Return value

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

Overrides Schema::findTables

File

core/lib/Drupal/Core/Database/Driver/pgsql/Schema.php, line 513

Class

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

Namespace

Drupal\Core\Database\Driver\pgsql

Code

public function findTables($table_expression) {
  $individually_prefixed_tables = $this->connection
    ->getUnprefixedTablesMap();
  $default_prefix = $this->connection
    ->tablePrefix();
  $default_prefix_length = strlen($default_prefix);
  $tables = [];

  // 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.
  $results = $this->connection
    ->query("SELECT tablename FROM pg_tables WHERE schemaname = :schema", [
    ':schema' => $this->defaultSchema,
  ]);
  foreach ($results as $table) {

    // Take into account tables that have an individual prefix.
    if (isset($individually_prefixed_tables[$table->tablename])) {
      $prefix_length = strlen($this->connection
        ->tablePrefix($individually_prefixed_tables[$table->tablename]));
    }
    elseif ($default_prefix && substr($table->tablename, 0, $default_prefix_length) !== $default_prefix) {

      // This table name does not start the default prefix, which means that
      // it is not managed by Drupal so it should be excluded from the result.
      continue;
    }
    else {
      $prefix_length = $default_prefix_length;
    }

    // Remove the prefix from the returned tables.
    $unprefixed_table_name = substr($table->tablename, $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;
}