You are here

protected function SearchApiDbService::findFreeTable in Search API Database Search 7

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 to start the table name.

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

Return value

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

1 call to SearchApiDbService::findFreeTable()
SearchApiDbService::fieldsUpdated in ./service.inc
Overrides SearchApiAbstractService::fieldsUpdated().

File

./service.inc, line 326
Contains SearchApiDbService.

Class

SearchApiDbService
Indexes and searches items using the database.

Code

protected function findFreeTable($prefix, $name) {

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

    // Use strlen instead of drupal_strlen since we want to measure bytes
    // instead of characters.
    $maxbytes -= strlen($db_prefix);
  }
  $base = $table = self::mbStrcut($prefix . drupal_strtolower(preg_replace('/[^a-z0-9]/i', '_', $name)), 0, $maxbytes);
  $i = 0;
  while ($this->connection
    ->schema()
    ->tableExists($table)) {
    $suffix = '_' . ++$i;
    $table = self::mbStrcut($base, 0, $maxbytes - strlen($suffix)) . $suffix;
  }
  return $table;
}