Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
TableMaintainer.php
Go to the documentation of this file.
1 <?php
6 declare(strict_types=1);
7 
9 
14 
19 {
23  const MAIN_INDEX_TABLE = 'catalog_product_index_price';
24 
28  private $resource;
29 
33  private $tableResolver;
34 
38  private $connection;
39 
43  private $tmpTableSuffix = '_temp';
44 
48  private $additionalTableSuffix = '_replica';
49 
53  private $mainTmpTable;
54 
58  private $connectionName;
59 
65  public function __construct(
66  ResourceConnection $resource,
67  TableResolver $tableResolver,
68  $connectionName = null
69  ) {
70  $this->resource = $resource;
71  $this->tableResolver = $tableResolver;
72  $this->connectionName = $connectionName;
73  }
74 
80  public function getConnection(): AdapterInterface
81  {
82  if (null === $this->connection) {
83  $this->connection = $this->resource->getConnection($this->connectionName);
84  }
85  return $this->connection;
86  }
87 
94  private function getTable(string $table): string
95  {
96  return $this->resource->getTableName($table);
97  }
98 
109  private function createTable(string $mainTableName, string $newTableName)
110  {
111  if (!$this->getConnection()->isTableExists($newTableName)) {
112  $this->getConnection()->createTable(
113  $this->getConnection()->createTableByDdl($mainTableName, $newTableName)
114  );
115  }
116  }
117 
125  private function dropTable(string $tableName)
126  {
127  if ($this->getConnection()->isTableExists($tableName)) {
128  $this->getConnection()->dropTable($tableName);
129  }
130  }
131 
139  private function truncateTable(string $tableName)
140  {
141  if ($this->getConnection()->isTableExists($tableName)) {
142  $this->getConnection()->truncateTable($tableName);
143  }
144  }
145 
153  private function getArrayKeyForTmpTable(array $dimensions): string
154  {
155  $key = 'temp';
156  foreach ($dimensions as $dimension) {
157  $key .= $dimension->getName() . '_' . $dimension->getValue();
158  }
159  return $key;
160  }
161 
169  public function getMainTable(array $dimensions): string
170  {
171  return $this->tableResolver->resolve(self::MAIN_INDEX_TABLE, $dimensions);
172  }
173 
183  public function createTablesForDimensions(array $dimensions)
184  {
185  $mainTableName = $this->getMainTable($dimensions);
186  //Create index table for dimensions based on main replica table
187  //Using main replica table is necessary for backward capability and TableResolver plugin work
188  $this->createTable(
189  $this->getTable(self::MAIN_INDEX_TABLE . $this->additionalTableSuffix),
190  $mainTableName
191  );
192 
193  $mainReplicaTableName = $this->getMainTable($dimensions) . $this->additionalTableSuffix;
194  //Create replica table for dimensions based on main replica table
195  $this->createTable(
196  $this->getTable(self::MAIN_INDEX_TABLE . $this->additionalTableSuffix),
197  $mainReplicaTableName
198  );
199  }
200 
208  public function dropTablesForDimensions(array $dimensions)
209  {
210  $mainTableName = $this->getMainTable($dimensions);
211  $this->dropTable($mainTableName);
212 
213  $mainReplicaTableName = $this->getMainTable($dimensions) . $this->additionalTableSuffix;
214  $this->dropTable($mainReplicaTableName);
215  }
216 
224  public function truncateTablesForDimensions(array $dimensions)
225  {
226  $mainTableName = $this->getMainTable($dimensions);
227  $this->truncateTable($mainTableName);
228 
229  $mainReplicaTableName = $this->getMainTable($dimensions) . $this->additionalTableSuffix;
230  $this->truncateTable($mainReplicaTableName);
231  }
232 
240  public function getMainReplicaTable(array $dimensions): string
241  {
242  return $this->getMainTable($dimensions) . $this->additionalTableSuffix;
243  }
244 
252  public function createMainTmpTable(array $dimensions)
253  {
254  // Create temporary table based on template table catalog_product_index_price_tmp without indexes
255  $templateTableName = $this->resource->getTableName(self::MAIN_INDEX_TABLE . '_tmp');
256  $temporaryTableName = $this->getMainTable($dimensions) . $this->tmpTableSuffix;
257  $this->getConnection()->createTemporaryTableLike($temporaryTableName, $templateTableName, true);
258  $this->mainTmpTable[$this->getArrayKeyForTmpTable($dimensions)] = $temporaryTableName;
259  }
260 
270  public function getMainTmpTable(array $dimensions): string
271  {
272  $cacheKey = $this->getArrayKeyForTmpTable($dimensions);
273  if (!isset($this->mainTmpTable[$cacheKey])) {
274  throw new \LogicException(
275  sprintf('Temporary table for provided dimensions "%s" does not exist', $cacheKey)
276  );
277  }
278  return $this->mainTmpTable[$cacheKey];
279  }
280 }
$tableName
Definition: trigger.php:13
$resource
Definition: bulk.php:12
__construct(ResourceConnection $resource, TableResolver $tableResolver, $connectionName=null)
$table
Definition: trigger.php:14