You are here

protected function Database::findFreeColumn in Search API 8

Finds a free column name within a database table.

Used as a helper method in fieldsUpdated().

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

Parameters

string $table: The name of the table.

string $column: The name to base the column name on.

Return value

string A column name that isn't in use in the specified table yet.

1 call to Database::findFreeColumn()
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 750

Class

Database
Indexes and searches items using the database.

Namespace

Drupal\search_api_db\Plugin\search_api\backend

Code

protected function findFreeColumn($table, $column) {
  $maxbytes = 62;
  $base = $name = Unicode::truncateBytes(mb_strtolower(preg_replace('/[^a-z0-9]/i', '_', $column)), $maxbytes);

  // If the table does not exist yet, the initial name is not taken.
  if ($this->database
    ->schema()
    ->tableExists($table)) {
    $i = 0;
    while ($this->database
      ->schema()
      ->fieldExists($table, $name)) {
      $suffix = '_' . ++$i;
      $name = Unicode::truncateBytes($base, $maxbytes - strlen($suffix)) . $suffix;
    }
  }
  return $name;
}