You are here

protected function SqlContentEntityStorageSchema::getSelectQueryForFieldStorageDeletion in Drupal 9

Same name and namespace in other branches
  1. 8 core/lib/Drupal/Core/Entity/Sql/SqlContentEntityStorageSchema.php \Drupal\Core\Entity\Sql\SqlContentEntityStorageSchema::getSelectQueryForFieldStorageDeletion()
  2. 10 core/lib/Drupal/Core/Entity/Sql/SqlContentEntityStorageSchema.php \Drupal\Core\Entity\Sql\SqlContentEntityStorageSchema::getSelectQueryForFieldStorageDeletion()

Returns a SELECT query suitable for inserting data into a dedicated table.

Parameters

string $table_name: The entity table name to select from.

array $shared_table_field_columns: An array of field column names for a shared table schema.

array $dedicated_table_field_columns: An array of field column names for a dedicated table schema.

string $base_table: (optional) The name of the base entity table. Defaults to NULL.

Return value

\Drupal\Core\Database\Query\SelectInterface A database select query.

File

core/lib/Drupal/Core/Entity/Sql/SqlContentEntityStorageSchema.php, line 831

Class

SqlContentEntityStorageSchema
Defines a schema handler that supports revisionable, translatable entities.

Namespace

Drupal\Core\Entity\Sql

Code

protected function getSelectQueryForFieldStorageDeletion($table_name, array $shared_table_field_columns, array $dedicated_table_field_columns, $base_table = NULL) {

  // Create a SELECT query that generates a result suitable for writing into
  // a dedicated field table.
  $select = $this->database
    ->select($table_name, 'entity_table');

  // Add the bundle column.
  if ($bundle = $this->entityType
    ->getKey('bundle')) {

    // The bundle field is not stored in the revision table, so we need to
    // join the data (or base) table and retrieve it from there.
    if ($base_table && $base_table !== $table_name) {
      $join_condition = "[entity_table].[{$this->entityType->getKey('id')}] = [%alias].[{$this->entityType->getKey('id')}]";

      // If the entity type is translatable, we also need to add the langcode
      // to the join, otherwise we'll get duplicate rows for each language.
      if ($this->entityType
        ->isTranslatable()) {
        $langcode = $this->entityType
          ->getKey('langcode');
        $join_condition .= " AND [entity_table].[{$langcode}] = [%alias].[{$langcode}]";
      }
      $select
        ->join($base_table, 'base_table', $join_condition);
      $select
        ->addField('base_table', $bundle, 'bundle');
    }
    else {
      $select
        ->addField('entity_table', $bundle, 'bundle');
    }
  }
  else {
    $select
      ->addExpression(':bundle', 'bundle', [
      ':bundle' => $this->entityType
        ->id(),
    ]);
  }

  // Add the deleted column.
  $select
    ->addExpression(':deleted', 'deleted', [
    ':deleted' => 1,
  ]);

  // Add the entity_id column.
  $select
    ->addField('entity_table', $this->entityType
    ->getKey('id'), 'entity_id');

  // Add the revision_id column.
  if ($this->entityType
    ->isRevisionable()) {
    $select
      ->addField('entity_table', $this->entityType
      ->getKey('revision'), 'revision_id');
  }
  else {
    $select
      ->addField('entity_table', $this->entityType
      ->getKey('id'), 'revision_id');
  }

  // Add the langcode column.
  if ($langcode = $this->entityType
    ->getKey('langcode')) {
    $select
      ->addField('entity_table', $langcode, 'langcode');
  }
  else {
    $select
      ->addExpression(':langcode', 'langcode', [
      ':langcode' => LanguageInterface::LANGCODE_NOT_SPECIFIED,
    ]);
  }

  // Add the delta column and set it to 0 because we are only dealing with
  // single cardinality fields.
  $select
    ->addExpression(':delta', 'delta', [
    ':delta' => 0,
  ]);

  // Add all the dynamic field columns.
  $or = $select
    ->orConditionGroup();
  foreach ($shared_table_field_columns as $field_column_name => $schema_column_name) {
    $select
      ->addField('entity_table', $schema_column_name, $dedicated_table_field_columns[$field_column_name]);
    $or
      ->isNotNull('entity_table.' . $schema_column_name);
  }
  $select
    ->condition($or);

  // Lock the table rows.
  $select
    ->forUpdate(TRUE);
  return $select;
}