You are here

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

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: Name of the table.

string $column: If adding a column to $name, 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 SearchApiDbService::findFreeColumn()
SearchApiDbService::fieldsUpdated in ./service.inc
Overrides SearchApiAbstractService::fieldsUpdated().

File

./service.inc, line 361
Contains SearchApiDbService.

Class

SearchApiDbService
Indexes and searches items using the database.

Code

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

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