You are here

protected function Database::findFreeTable in Search API 8

Finds a free table name using a certain prefix and name base.

Used as a helper method in fieldsUpdated().

MySQL 5.0 imposes a 64 characters length limit for table names, PostgreSQL 8.3 only allows 62 bytes. Therefore, always return a name at most 62 bytes long.

Parameters

string $prefix: Prefix for the table name. Must only consist of characters valid for SQL identifiers.

string $name: Name to base the table name on.

Return value

string A database table name that isn't in use yet.

2 calls to Database::findFreeTable()
Database::addIndex in modules/search_api_db/src/Plugin/search_api/backend/Database.php
Adds a new index to this server.
Database::fieldsUpdated in modules/search_api_db/src/Plugin/search_api/backend/Database.php
Updates the storage tables when the field configuration changes.

File

modules/search_api_db/src/Plugin/search_api/backend/Database.php, line 715

Class

Database
Indexes and searches items using the database.

Namespace

Drupal\search_api_db\Plugin\search_api\backend

Code

protected function findFreeTable($prefix, $name) {

  // A DB prefix might further reduce the maximum length of the table name.
  $max_bytes = 62;
  if ($db_prefix = $this->database
    ->tablePrefix()) {

    // Use strlen() instead of mb_strlen() since we want to measure bytes, not
    // characters.
    $max_bytes -= strlen($db_prefix);
  }
  $base = $table = Unicode::truncateBytes($prefix . mb_strtolower(preg_replace('/[^a-z0-9]/i', '_', $name)), $max_bytes);
  $i = 0;
  while ($this->database
    ->schema()
    ->tableExists($table)) {
    $suffix = '_' . ++$i;
    $table = Unicode::truncateBytes($base, $max_bytes - strlen($suffix)) . $suffix;
  }
  return $table;
}