protected function setUp() { /** @var \Magento\Framework\Setup\ModuleDataSetupInterface $installer */ $installer = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create('\\Magento\\Framework\\Setup\\ModuleDataSetupInterface'); $this->_connection = $installer->getConnection(); $this->_tableName = $this->_connection->getTableName('table_two_column_idx'); $this->_oneColumnIdxName = $this->_connection->getIndexName($this->_tableName, ['column1']); $this->_twoColumnIdxName = $this->_connection->getIndexName($this->_tableName, ['column1', 'column2']); $table = $this->_connection->newTable($this->_tableName)->addColumn('id', \Magento\Framework\DB\Ddl\Table::TYPE_INTEGER, null, ['identity' => true, 'unsigned' => true, 'nullable' => false, 'primary' => true], 'Id')->addColumn('column1', \Magento\Framework\DB\Ddl\Table::TYPE_INTEGER)->addColumn('column2', \Magento\Framework\DB\Ddl\Table::TYPE_INTEGER)->addIndex($this->_oneColumnIdxName, ['column1'])->addIndex($this->_twoColumnIdxName, ['column1', 'column2']); $this->_connection->createTable($table); }
/** * Clean unused relation products * * @param int $storeId * @return \Magento\Catalog\Model\Indexer\Product\Flat\AbstractAction */ protected function _cleanRelationProducts($storeId) { if (!$this->_productIndexerHelper->isAddChildData()) { return $this; } $metadata = $this->metadataPool->getMetadata(ProductInterface::class); foreach ($this->_getProductTypeInstances() as $typeInstance) { /** @var $typeInstance \Magento\Catalog\Model\Product\Type\AbstractType */ if (!$typeInstance->isComposite(null)) { continue; } $relation = $typeInstance->getRelationInfo(); if ($relation && $relation->getTable() && $relation->getParentFieldName() && $relation->getChildFieldName()) { $select = $this->_connection->select()->distinct(true)->from(['t' => $this->_productIndexerHelper->getTable($relation->getTable())], [])->join(['entity_table' => $this->_connection->getTableName('catalog_product_entity')], 'entity_table.' . $metadata->getLinkField() . 't.' . $relation->getParentFieldName(), [$relation->getParentFieldName() => 'entity_table.entity_id']); $joinLeftCond = ["e.entity_id = entity_table.entity_id", "e.child_id = t.{$relation->getChildFieldName()}"]; if ($relation->getWhere() !== null) { $select->where($relation->getWhere()); $joinLeftCond[] = $relation->getWhere(); } $entitySelect = new \Zend_Db_Expr($select->__toString()); /** @var $select \Magento\Framework\DB\Select */ $select = $this->_connection->select()->from(['e' => $this->_productIndexerHelper->getFlatTableName($storeId)], null)->joinLeft(['t' => $this->_productIndexerHelper->getTable($relation->getTable())], implode(' AND ', $joinLeftCond), [])->where('e.is_child = ?', 1)->where('e.entity_id IN(?)', $entitySelect)->where("t.{$relation->getChildFieldName()} IS NULL"); $sql = $select->deleteFromSelect('e'); $this->_connection->query($sql); } } return $this; }
/** * Create delta for specified table * * @param string $documentName * @param string $deltaLogName * @param string $idKey * @return void */ public function createDelta($documentName, $deltaLogName, $idKey) { if (!$this->resourceAdapter->isTableExists($deltaLogName)) { $triggerTable = $this->resourceAdapter->newTable($deltaLogName)->addColumn($idKey, \Magento\Framework\DB\Ddl\Table::TYPE_INTEGER, null, ['nullable' => false, 'primary' => true])->addColumn('operation', \Magento\Framework\DB\Ddl\Table::TYPE_TEXT)->addColumn('processed', \Magento\Framework\DB\Ddl\Table::TYPE_BOOLEAN, null, ['nullable' => false, 'default' => 0]); $this->resourceAdapter->createTable($triggerTable); } else { $this->deleteAllRecords($deltaLogName); } foreach (Trigger::getListOfEvents() as $event) { $triggerName = $this->resourceAdapter->getTableName('trg_' . $documentName . '_after_' . strtolower($event)); $statement = $this->buildStatement($event, $idKey, $deltaLogName); $trigger = $this->triggerFactory->create()->setTime(Trigger::TIME_AFTER)->setEvent($event)->setTable($documentName); $triggerKey = $documentName . $event . Trigger::TIME_AFTER; $triggerExists = $this->isTriggerExist($triggerKey); if ($triggerExists) { $triggerName = $this->triggers[$triggerKey]['trigger_name']; $oldTriggerStatement = $this->triggers[$triggerKey]['action_statement']; if (strpos($oldTriggerStatement, $statement) !== false) { unset($trigger); continue; } $trigger->addStatement($oldTriggerStatement); $this->resourceAdapter->dropTrigger($triggerName); } $trigger->addStatement($statement)->setName($triggerName); $this->resourceAdapter->createTrigger($trigger); if (!$triggerExists) { $this->triggers[$triggerKey] = 1; } unset($trigger); } }
/** * Get temporary table name for concurrent indexing in persistent connection * Temp table name is NOT shared between action instances and each action has it's own temp tree index * * @return string */ protected function getTemporaryTreeIndexTableName() { if (empty($this->tempTreeIndexTableName)) { $this->tempTreeIndexTableName = $this->connection->getTableName('temp_catalog_category_tree_index') . '_' . substr(md5(time() . rand(0, 999999999)), 0, 8); } return $this->tempTreeIndexTableName; }
/** * Get table name (validated by db adapter) by table placeholder * * @param string|array $tableName * @return string */ public function getTable($tableName) { $tablePrefix = (string) $this->tablePrefix; if ($tablePrefix && strpos($tableName, $tablePrefix) !== 0) { $tableName = $tablePrefix . $tableName; } $cacheKey = $this->getTableCacheName($tableName); if (!isset($this->tables[$cacheKey])) { $this->tables[$cacheKey] = $this->connection->getTableName($tableName); } return $this->tables[$cacheKey]; }
/** * Return attribute values for given entities and store of specific attribute type * * @param string $type * @param array $entityIds * @param integer $storeId * @return array */ protected function getAttributeTypeValues($type, $entityIds, $storeId) { $linkField = $this->getCategoryMetadata()->getLinkField(); $select = $this->connection->select()->from(['def' => $this->connection->getTableName($this->getTableName('catalog_category_entity_' . $type))], [$linkField, 'attribute_id'])->joinLeft(['e' => $this->connection->getTableName($this->getTableName('catalog_category_entity'))], "def.{$linkField} = e.{$linkField}")->joinLeft(['store' => $this->connection->getTableName($this->getTableName('catalog_category_entity_' . $type))], "store.{$linkField} = def.{$linkField} AND store.attribute_id = def.attribute_id " . 'AND store.store_id = ' . $storeId, ['value' => $this->connection->getCheckSql('store.value_id > 0', $this->connection->quoteIdentifier('store.value'), $this->connection->quoteIdentifier('def.value'))])->where("e.entity_id IN (?)", $entityIds)->where('def.store_id IN (?)', [\Magento\Store\Model\Store::DEFAULT_STORE_ID, $storeId]); return $this->connection->fetchAll($select); }