diff --git a/core/modules/statistics/src/StatisticsDatabaseStorage.php b/core/modules/statistics/src/StatisticsDatabaseStorage.php index 76f988b2f2..7aa57460a2 100644 --- a/core/modules/statistics/src/StatisticsDatabaseStorage.php +++ b/core/modules/statistics/src/StatisticsDatabaseStorage.php @@ -73,16 +73,19 @@ public function recordView($entity_type_id, $key, $id) { * {@inheritdoc} */ public function fetchView(EntityTypeInterface $entity_type, $id) { - $entity_type_id = $entity_type->id(); - $table = $entity_type_id . '_counter'; - $view = $this->connection - ->select($table, 'c') - ->fields('c', ['totalcount', 'daycount', 'timestamp']) - ->condition($entity_type->getKey('id'), $id) - ->execute() - ->fetchObject(); - if ($view) { - return new StatisticsViewsResult($view->totalcount, $view->daycount, $view->timestamp); + try { + $view = $this->connection + ->select($this->tableName($entity_type), 'c') + ->fields('c', ['totalcount', 'daycount', 'timestamp']) + ->condition($entity_type->getKey('id'), $id) + ->execute() + ->fetchObject(); + if ($view) { + return new StatisticsViewsResult($view->totalcount, $view->daycount, $view->timestamp); + } + } + catch (\Exception $e) { + $this->catchException($entity_type, $e); } return NULL; } @@ -92,36 +95,48 @@ public function fetchView(EntityTypeInterface $entity_type, $id) { */ public function fetchAll(EntityTypeInterface $entity_type, $order = 'totalcount', $limit = 5) { assert(in_array($order, ['totalcount', 'daycount', 'timestamp']), "Invalid order argument."); - $table = $entity_type->id() . '_counter'; - return $this->connection - ->select($table, 'nc') - ->fields('nc', [$entity_type->getKey('id')]) - ->orderBy($order, 'DESC') - ->range(0, $limit) - ->execute() - ->fetchCol(); + try { + return $this->connection + ->select($this->tableName($entity_type), 'nc') + ->fields('nc', [$entity_type->getKey('id')]) + ->orderBy($order, 'DESC') + ->range(0, $limit) + ->execute() + ->fetchCol(); + } + catch (\Exception $e) { + $this->catchException($entity_type, $e); + } } /** * {@inheritdoc} */ public function deleteViews(EntityTypeInterface $entity_type, $id) { - $table = $entity_type->id() . '_counter'; - return (bool) $this->connection - ->delete($table) - ->condition($entity_type->getKey('id'), $id) - ->execute(); + try { + return (bool) $this->connection + ->delete($this->tableName($entity_type)) + ->condition($entity_type->getKey('id'), $id) + ->execute(); + } + catch (\Exception $e) { + $this->catchException($entity_type, $e); + } } /** * {@inheritdoc} */ public function maxTotalCount(EntityTypeInterface $entity_type) { - $table = $entity_type->id() . '_counter'; - $query = $this->connection->select($table, 'nc'); - $query->addExpression('MAX(totalcount)'); - $max_total_count = (int) $query->execute()->fetchField(); - return $max_total_count; + try { + $query = $this->connection->select($this->tableName($entity_type), 'nc'); + $query->addExpression('MAX(totalcount)'); + $max_total_count = (int) $query->execute()->fetchField(); + return $max_total_count; + } + catch (\Exception $e) { + $this->catchException($entity_type, $e); + } } /** @@ -188,11 +203,38 @@ public function createTable(EntityTypeInterface $entity_type) { * {@inheritdoc} */ public function dropTable(EntityTypeInterface $entity_type) { - $entity_type_id = $entity_type->id(); - $table = $entity_type_id . '_counter'; - if ($this->connection->schema()->tableExists($table)) { - $this->connection->schema()->dropTable($table); + if ($this->connection->schema()->tableExists($this->tableName($entity_type))) { + $this->connection->schema()->dropTable($this->tableName($entity_type)); + } + } + + /** + * Act on an exception when the table might not have been created. + * + * If the table does not yet exist, that's fine, but if the table exists and + * something else caused the exception, then propagate it. + * + * @param \Exception $e + * The exception. + * + * @throws \Exception + */ + protected function catchException(EntityTypeInterface $entity_type,\Exception $e) { + if ($this->connection->schema()->tableExists($this->tableName($entity_type))) { + throw $e; } } + /** + * Generates the table name from the entity type. + * + * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type + * + * @return string + */ + protected function tableName(EntityTypeInterface $entity_type) { + $entity_type_id = $entity_type->id(); + return $entity_type_id . '_counter'; + } + } diff --git a/core/modules/statistics/src/StatisticsStorageInterface.php b/core/modules/statistics/src/StatisticsStorageInterface.php index 8b4c5dd70c..cbe7af0d3f 100644 --- a/core/modules/statistics/src/StatisticsStorageInterface.php +++ b/core/modules/statistics/src/StatisticsStorageInterface.php @@ -17,7 +17,7 @@ * * @param string $entity_type_id * The entity type ID. - * @param int $key + * @param string $key * The ID key of the entity to count. * @param int $id * The ID of the entity to count.