You are here

class MySql in Search API 8

Represents a MySQL-based database.

Hierarchy

Expanded class hierarchy of MySql

1 string reference to 'MySql'
search_api_db.services.yml in modules/search_api_db/search_api_db.services.yml
modules/search_api_db/search_api_db.services.yml
1 service uses MySql
mysql.search_api_db.database_compatibility in modules/search_api_db/search_api_db.services.yml
Drupal\search_api_db\DatabaseCompatibility\MySql

File

modules/search_api_db/src/DatabaseCompatibility/MySql.php, line 12

Namespace

Drupal\search_api_db\DatabaseCompatibility
View source
class MySql extends GenericDatabase {

  /**
   * {@inheritdoc}
   */
  public function alterNewTable($table, $type = 'text') {

    // The Drupal MySQL integration defaults to using a 4-byte-per-character
    // encoding, which would make it impossible to use our normal 255 characters
    // long varchar fields in a primary key (since that would exceed the key's
    // maximum size). Therefore, we have to convert all tables to the "utf8"
    // character set – but we only want to make fulltext tables case-sensitive.
    $charset = $type === 'text' ? 'utf8mb4' : 'utf8';
    $collation = $type === 'text' ? 'utf8mb4_bin' : 'utf8_general_ci';
    try {
      $this->database
        ->query("ALTER TABLE {{$table}} CONVERT TO CHARACTER SET '{$charset}' COLLATE '{$collation}'");
    } catch (\PDOException $e) {
      $class = get_class($e);
      $message = $e
        ->getMessage();
      throw new SearchApiException("{$class} while trying to change collation of {$type} search data table '{$table}': {$message}", 0, $e);
    } catch (DatabaseException $e) {
      $class = get_class($e);
      $message = $e
        ->getMessage();
      throw new SearchApiException("{$class} while trying to change collation of {$type} search data table '{$table}': {$message}", 0, $e);
    }
  }

  /**
   * {@inheritdoc}
   */
  public function preprocessIndexValue($value, $type = 'text') {
    $value = parent::preprocessIndexValue($value, $type);

    // As MySQL removes trailing whitespace when computing primary keys, we need
    // to do the same or pseudo-duplicates could cause an exception ("Integrity
    // constraint violation: Duplicate entry") during indexing.
    if ($type !== 'text') {
      $value = rtrim($value);
    }
    return $value;
  }

  /**
   * {@inheritdoc}
   */
  public function orderByRandom(SelectInterface $query) {
    $alias = $query
      ->addExpression('rand()', 'random_order_field');
    $query
      ->orderBy($alias);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
GenericDatabase::$database protected property The connection to the database.
GenericDatabase::$transliterator protected property The transliteration service to use.
GenericDatabase::getCloneForDatabase public function Creates a clone of this service for the given database. Overrides DatabaseCompatibilityHandlerInterface::getCloneForDatabase
GenericDatabase::getDatabase public function Retrieves the database connection this compatibility handler is based upon. Overrides DatabaseCompatibilityHandlerInterface::getDatabase
GenericDatabase::__construct public function Constructs a GenericDatabase object.
MySql::alterNewTable public function Reacts to a new table being created. Overrides GenericDatabase::alterNewTable
MySql::orderByRandom public function Applies a random sort to the query. Overrides GenericDatabase::orderByRandom
MySql::preprocessIndexValue public function Determines the canonical base form of a value. Overrides GenericDatabase::preprocessIndexValue