You are here

protected function FuzzySearchService::findFreeTable in Fuzzy Search 7

Helper method for finding free table names for fields.

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

2 calls to FuzzySearchService::findFreeTable()
FuzzySearchService::addIndex in includes/service.inc
Implements SearchApiServiceInterface::__construct().
FuzzySearchService::fieldsUpdated in includes/service.inc
Implements SearchApiServiceInterface::__construct().

File

includes/service.inc, line 140

Class

FuzzySearchService
Search service class using the database for storing index information.

Code

protected function findFreeTable($prefix, $name) {

  // A DB prefix might further reduce the maximum length of the table name.
  $maxlen = 63;
  list($key, $target) = explode(':', $this->options['database'], 2);
  if ($db_prefix = Database::getConnection($target, $key)
    ->tablePrefix()) {
    $maxlen -= drupal_strlen($db_prefix);
  }
  $base = $table = drupal_substr($prefix . drupal_strtolower(preg_replace('/[^a-z0-9]/i', '_', $name)), 0, $maxlen);
  $i = 0;
  while (db_table_exists($table)) {
    $suffix = '_' . ++$i;
    $table = drupal_substr($base, 0, $maxlen - drupal_strlen($suffix)) . $suffix;
  }
  return $table;
}