Commit c2b77b0c authored by catch's avatar catch

Issue #2988562 by jeqq, amateescu, timmillwood: Classes that extend...

Issue #2988562 by jeqq, amateescu, timmillwood: Classes that extend SqlContentEntityStorageSchema should not hardcode table names and use the ones provided by the storage instead
parent f51f707a
......@@ -16,7 +16,7 @@ class FeedStorage extends SqlContentEntityStorage implements FeedStorageInterfac
* {@inheritdoc}
*/
public function getFeedIdsToRefresh() {
return $this->database->query('SELECT fid FROM {aggregator_feed} WHERE queued = 0 AND checked + refresh < :time AND refresh <> :never', [
return $this->database->query('SELECT fid FROM {' . $this->getBaseTable() . '} WHERE queued = 0 AND checked + refresh < :time AND refresh <> :never', [
':time' => REQUEST_TIME,
':never' => AGGREGATOR_CLEAR_NEVER,
])->fetchCol();
......
......@@ -17,7 +17,7 @@ protected function getSharedTableFieldSchema(FieldStorageDefinitionInterface $st
$schema = parent::getSharedTableFieldSchema($storage_definition, $table_name, $column_mapping);
$field_name = $storage_definition->getName();
if ($table_name == 'aggregator_feed') {
if ($table_name == $this->storage->getBaseTable()) {
switch ($field_name) {
case 'url':
$this->addSharedTableFieldIndex($storage_definition, $schema, TRUE, 255);
......
......@@ -17,7 +17,7 @@ protected function getSharedTableFieldSchema(FieldStorageDefinitionInterface $st
$schema = parent::getSharedTableFieldSchema($storage_definition, $table_name, $column_mapping);
$field_name = $storage_definition->getName();
if ($table_name == 'aggregator_item') {
if ($table_name == $this->storage->getBaseTable()) {
switch ($field_name) {
case 'timestamp':
$this->addSharedTableFieldIndex($storage_definition, $schema, TRUE);
......
......@@ -71,7 +71,7 @@ public static function createInstance(ContainerInterface $container, EntityTypeI
* {@inheritdoc}
*/
public function getMaxThread(CommentInterface $comment) {
$query = $this->database->select('comment_field_data', 'c')
$query = $this->database->select($this->getDataTable(), 'c')
->condition('entity_id', $comment->getCommentedEntityId())
->condition('field_name', $comment->getFieldName())
->condition('entity_type', $comment->getCommentedEntityTypeId())
......@@ -85,7 +85,7 @@ public function getMaxThread(CommentInterface $comment) {
* {@inheritdoc}
*/
public function getMaxThreadPerThread(CommentInterface $comment) {
$query = $this->database->select('comment_field_data', 'c')
$query = $this->database->select($this->getDataTable(), 'c')
->condition('entity_id', $comment->getCommentedEntityId())
->condition('field_name', $comment->getFieldName())
->condition('entity_type', $comment->getCommentedEntityTypeId())
......@@ -102,8 +102,9 @@ public function getMaxThreadPerThread(CommentInterface $comment) {
public function getDisplayOrdinal(CommentInterface $comment, $comment_mode, $divisor = 1) {
// Count how many comments (c1) are before $comment (c2) in display order.
// This is the 0-based display ordinal.
$query = $this->database->select('comment_field_data', 'c1');
$query->innerJoin('comment_field_data', 'c2', 'c2.entity_id = c1.entity_id AND c2.entity_type = c1.entity_type AND c2.field_name = c1.field_name');
$data_table = $this->getDataTable();
$query = $this->database->select($data_table, 'c1');
$query->innerJoin($data_table, 'c2', 'c2.entity_id = c1.entity_id AND c2.entity_type = c1.entity_type AND c2.field_name = c1.field_name');
$query->addExpression('COUNT(*)', 'count');
$query->condition('c2.cid', $comment->id());
if (!$this->currentUser->hasPermission('administer comments')) {
......@@ -137,6 +138,7 @@ public function getDisplayOrdinal(CommentInterface $comment, $comment_mode, $div
public function getNewCommentPageNumber($total_comments, $new_comments, FieldableEntityInterface $entity, $field_name) {
$field = $entity->getFieldDefinition($field_name);
$comments_per_page = $field->getSetting('per_page');
$data_table = $this->getDataTable();
if ($total_comments <= $comments_per_page) {
// Only one page of comments.
......@@ -150,7 +152,7 @@ public function getNewCommentPageNumber($total_comments, $new_comments, Fieldabl
// Threaded comments.
// 1. Find all the threads with a new comment.
$unread_threads_query = $this->database->select('comment_field_data', 'comment')
$unread_threads_query = $this->database->select($data_table, 'comment')
->fields('comment', ['thread'])
->condition('entity_id', $entity->id())
->condition('entity_type', $entity->getEntityTypeId())
......@@ -175,7 +177,7 @@ public function getNewCommentPageNumber($total_comments, $new_comments, Fieldabl
$first_thread = substr($first_thread, 0, -1);
// Find the number of the first comment of the first unread thread.
$count = $this->database->query('SELECT COUNT(*) FROM {comment_field_data} WHERE entity_id = :entity_id
$count = $this->database->query('SELECT COUNT(*) FROM {' . $data_table . '} WHERE entity_id = :entity_id
AND entity_type = :entity_type
AND field_name = :field_name
AND status = :status
......@@ -196,7 +198,7 @@ public function getNewCommentPageNumber($total_comments, $new_comments, Fieldabl
* {@inheritdoc}
*/
public function getChildCids(array $comments) {
return $this->database->select('comment_field_data', 'c')
return $this->database->select($this->getDataTable(), 'c')
->fields('c', ['cid'])
->condition('pid', array_keys($comments), 'IN')
->condition('default_langcode', 1)
......@@ -262,7 +264,8 @@ public function getChildCids(array $comments) {
* to consider the trailing "/" so we use a substring only.
*/
public function loadThread(EntityInterface $entity, $field_name, $mode, $comments_per_page = 0, $pager_id = 0) {
$query = $this->database->select('comment_field_data', 'c');
$data_table = $this->getDataTable();
$query = $this->database->select($data_table, 'c');
$query->addField('c', 'cid');
$query
->condition('c.entity_id', $entity->id())
......@@ -282,7 +285,7 @@ public function loadThread(EntityInterface $entity, $field_name, $mode, $comment
$query->element($pager_id);
}
$count_query = $this->database->select('comment_field_data', 'c');
$count_query = $this->database->select($data_table, 'c');
$count_query->addExpression('COUNT(*)');
$count_query
->condition('c.entity_id', $entity->id())
......@@ -328,7 +331,7 @@ public function loadThread(EntityInterface $entity, $field_name, $mode, $comment
* {@inheritdoc}
*/
public function getUnapprovedCount() {
return $this->database->select('comment_field_data', 'c')
return $this->database->select($this->getDataTable(), 'c')
->condition('status', CommentInterface::NOT_PUBLISHED, '=')
->condition('default_langcode', 1)
->countQuery()
......
......@@ -17,24 +17,26 @@ class CommentStorageSchema extends SqlContentEntityStorageSchema {
protected function getEntitySchema(ContentEntityTypeInterface $entity_type, $reset = FALSE) {
$schema = parent::getEntitySchema($entity_type, $reset);
$schema['comment_field_data']['indexes'] += [
'comment__status_pid' => ['pid', 'status'],
'comment__num_new' => [
'entity_id',
'entity_type',
'comment_type',
'status',
'created',
'cid',
'thread',
],
'comment__entity_langcode' => [
'entity_id',
'entity_type',
'comment_type',
'default_langcode',
],
];
if ($data_table = $this->storage->getDataTable()) {
$schema[$data_table]['indexes'] += [
'comment__status_pid' => ['pid', 'status'],
'comment__num_new' => [
'entity_id',
'entity_type',
'comment_type',
'status',
'created',
'cid',
'thread',
],
'comment__entity_langcode' => [
'entity_id',
'entity_type',
'comment_type',
'default_langcode',
],
];
}
return $schema;
}
......
......@@ -25,12 +25,16 @@ protected function getEntitySchema(ContentEntityTypeInterface $entity_type, $res
'workflow',
'langcode',
];
$schema['content_moderation_state_field_data']['unique keys'] += [
'content_moderation_state__lookup' => $unique_keys,
];
$schema['content_moderation_state_field_revision']['unique keys'] += [
'content_moderation_state__lookup' => $unique_keys,
];
if ($data_table = $this->storage->getDataTable()) {
$schema[$data_table]['unique keys'] += [
'content_moderation_state__lookup' => $unique_keys,
];
}
if ($revision_data_table = $this->storage->getRevisionDataTable()) {
$schema[$revision_data_table]['unique keys'] += [
'content_moderation_state__lookup' => $unique_keys,
];
}
return $schema;
}
......
......@@ -17,7 +17,7 @@ protected function getSharedTableFieldSchema(FieldStorageDefinitionInterface $st
$schema = parent::getSharedTableFieldSchema($storage_definition, $table_name, $column_mapping);
$field_name = $storage_definition->getName();
if ($table_name == 'file_managed') {
if ($table_name == $this->storage->getBaseTable()) {
switch ($field_name) {
case 'status':
case 'changed':
......
......@@ -17,7 +17,7 @@ protected function getSharedTableFieldSchema(FieldStorageDefinitionInterface $st
$schema = parent::getSharedTableFieldSchema($storage_definition, $table_name, $column_mapping);
$field_name = $storage_definition->getName();
if ($table_name == 'menu_link_content') {
if ($table_name == $this->storage->getBaseTable()) {
switch ($field_name) {
case 'rediscover':
$this->addSharedTableFieldIndex($storage_definition, $schema, TRUE);
......
......@@ -19,7 +19,7 @@ class NodeStorage extends SqlContentEntityStorage implements NodeStorageInterfac
*/
public function revisionIds(NodeInterface $node) {
return $this->database->query(
'SELECT vid FROM {node_revision} WHERE nid=:nid ORDER BY vid',
'SELECT vid FROM {' . $this->getRevisionTable() . '} WHERE nid=:nid ORDER BY vid',
[':nid' => $node->id()]
)->fetchCol();
}
......@@ -29,7 +29,7 @@ public function revisionIds(NodeInterface $node) {
*/
public function userRevisionIds(AccountInterface $account) {
return $this->database->query(
'SELECT vid FROM {node_field_revision} WHERE uid = :uid ORDER BY vid',
'SELECT vid FROM {' . $this->getRevisionDataTable() . '} WHERE uid = :uid ORDER BY vid',
[':uid' => $account->id()]
)->fetchCol();
}
......@@ -38,14 +38,14 @@ public function userRevisionIds(AccountInterface $account) {
* {@inheritdoc}
*/
public function countDefaultLanguageRevisions(NodeInterface $node) {
return $this->database->query('SELECT COUNT(*) FROM {node_field_revision} WHERE nid = :nid AND default_langcode = 1', [':nid' => $node->id()])->fetchField();
return $this->database->query('SELECT COUNT(*) FROM {' . $this->getRevisionDataTable() . '} WHERE nid = :nid AND default_langcode = 1', [':nid' => $node->id()])->fetchField();
}
/**
* {@inheritdoc}
*/
public function updateType($old_type, $new_type) {
return $this->database->update('node')
return $this->database->update($this->getBaseTable())
->fields(['type' => $new_type])
->condition('type', $old_type)
->execute();
......@@ -55,7 +55,7 @@ public function updateType($old_type, $new_type) {
* {@inheritdoc}
*/
public function clearRevisionsLanguage(LanguageInterface $language) {
return $this->database->update('node_revision')
return $this->database->update($this->getRevisionTable())
->fields(['langcode' => LanguageInterface::LANGCODE_NOT_SPECIFIED])
->condition('langcode', $language->getId())
->execute();
......
......@@ -17,10 +17,12 @@ class NodeStorageSchema extends SqlContentEntityStorageSchema {
protected function getEntitySchema(ContentEntityTypeInterface $entity_type, $reset = FALSE) {
$schema = parent::getEntitySchema($entity_type, $reset);
$schema['node_field_data']['indexes'] += [
'node__frontpage' => ['promote', 'status', 'sticky', 'created'],
'node__title_type' => ['title', ['type', 4]],
];
if ($data_table = $this->storage->getDataTable()) {
$schema[$data_table]['indexes'] += [
'node__frontpage' => ['promote', 'status', 'sticky', 'created'],
'node__title_type' => ['title', ['type', 4]],
];
}
return $schema;
}
......
......@@ -18,7 +18,7 @@ protected function getEntitySchema(ContentEntityTypeInterface $entity_type, $res
$schema = parent::getEntitySchema($entity_type, $reset);
if ($entity_type->id() == 'entity_test_update') {
$schema[$entity_type->getBaseTable()]['indexes'] += \Drupal::state()->get('entity_test_update.additional_entity_indexes', []);
$schema[$this->storage->getBaseTable()]['indexes'] += \Drupal::state()->get('entity_test_update.additional_entity_indexes', []);
}
return $schema;
}
......
......@@ -211,7 +211,7 @@ public function loadTree($vid, $parent = 0, $max_depth = NULL, $load_entities =
$this->treeChildren[$vid] = [];
$this->treeParents[$vid] = [];
$this->treeTerms[$vid] = [];
$query = $this->database->select('taxonomy_term_field_data', 't');
$query = $this->database->select($this->getDataTable(), 't');
$query->join('taxonomy_term__parent', 'p', 't.tid = p.entity_id');
$query->addExpression('parent_target_id', 'parent');
$result = $query
......@@ -305,7 +305,7 @@ public function loadTree($vid, $parent = 0, $max_depth = NULL, $load_entities =
public function nodeCount($vid) {
$query = $this->database->select('taxonomy_index', 'ti');
$query->addExpression('COUNT(DISTINCT ti.nid)');
$query->leftJoin('taxonomy_term_data', 'td', 'ti.tid = td.tid');
$query->leftJoin($this->getBaseTable(), 'td', 'ti.tid = td.tid');
$query->condition('td.vid', $vid);
$query->addTag('vocabulary_node_count');
return $query->execute()->fetchField();
......@@ -315,7 +315,7 @@ public function nodeCount($vid) {
* {@inheritdoc}
*/
public function resetWeights($vid) {
$this->database->update('taxonomy_term_field_data')
$this->database->update($this->getDataTable())
->fields(['weight' => 0])
->condition('vid', $vid)
->execute();
......@@ -325,7 +325,7 @@ public function resetWeights($vid) {
* {@inheritdoc}
*/
public function getNodeTerms(array $nids, array $vocabs = [], $langcode = NULL) {
$query = db_select('taxonomy_term_field_data', 'td');
$query = db_select($this->getDataTable(), 'td');
$query->innerJoin('taxonomy_index', 'tn', 'td.tid = tn.tid');
$query->fields('td', ['tid']);
$query->addField('tn', 'nid', 'node_nid');
......
......@@ -17,10 +17,12 @@ class TermStorageSchema extends SqlContentEntityStorageSchema {
protected function getEntitySchema(ContentEntityTypeInterface $entity_type, $reset = FALSE) {
$schema = parent::getEntitySchema($entity_type, $reset = FALSE);
$schema['taxonomy_term_field_data']['indexes'] += [
'taxonomy_term__tree' => ['vid', 'weight', 'name'],
'taxonomy_term__vid_name' => ['vid', 'name'],
];
if ($data_table = $this->storage->getDataTable()) {
$schema[$data_table]['indexes'] += [
'taxonomy_term__tree' => ['vid', 'weight', 'name'],
'taxonomy_term__vid_name' => ['vid', 'name'],
];
}
$schema['taxonomy_index'] = [
'description' => 'Maintains denormalized information about node/term relationships.',
......
......@@ -21,7 +21,7 @@ protected function doSaveFieldItems(ContentEntityInterface $entity, array $names
// The anonymous user account is saved with the fixed user ID of 0.
// Therefore we need to check for NULL explicitly.
if ($entity->id() === NULL) {
$entity->uid->value = $this->database->nextId($this->database->query('SELECT MAX(uid) FROM {users}')->fetchField());
$entity->uid->value = $this->database->nextId($this->database->query('SELECT MAX(uid) FROM {' . $this->getBaseTable() . '}')->fetchField());
$entity->enforceIsNew();
}
return parent::doSaveFieldItems($entity, $names);
......@@ -39,7 +39,7 @@ protected function isColumnSerial($table_name, $schema_name) {
* {@inheritdoc}
*/
public function updateLastLoginTimestamp(UserInterface $account) {
$this->database->update('users_field_data')
$this->database->update($this->getDataTable())
->fields(['login' => $account->getLastLoginTime()])
->condition('uid', $account->id())
->execute();
......@@ -51,7 +51,7 @@ public function updateLastLoginTimestamp(UserInterface $account) {
* {@inheritdoc}
*/
public function updateLastAccessTimestamp(AccountInterface $account, $timestamp) {
$this->database->update('users_field_data')
$this->database->update($this->getDataTable())
->fields([
'access' => $timestamp,
])
......
......@@ -17,9 +17,11 @@ class UserStorageSchema extends SqlContentEntityStorageSchema {
protected function getEntitySchema(ContentEntityTypeInterface $entity_type, $reset = FALSE) {
$schema = parent::getEntitySchema($entity_type, $reset);
$schema['users_field_data']['unique keys'] += [
'user__name' => ['name', 'langcode'],
];
if ($data_table = $this->storage->getDataTable()) {
$schema[$data_table]['unique keys'] += [
'user__name' => ['name', 'langcode'],
];
}
return $schema;
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment