/** * Return array of static columns * * @return array * @SuppressWarnings(PHPMD.CyclomaticComplexity) */ protected function getStaticColumns() { $columns = []; $columnsToSkip = ['entity_type_id', 'attribute_set_id']; $describe = $this->connection->describeTable($this->connection->getTableName($this->getTableName('catalog_category_entity'))); foreach ($describe as $column) { if (in_array($column['COLUMN_NAME'], $columnsToSkip)) { continue; } $isUnsigned = ''; $options = null; $ddlType = $this->resourceHelper->getDdlTypeByColumnType($column['DATA_TYPE']); $column['DEFAULT'] = trim($column['DEFAULT'], "' "); switch ($ddlType) { case \Magento\Framework\DB\Ddl\Table::TYPE_SMALLINT: case \Magento\Framework\DB\Ddl\Table::TYPE_INTEGER: case \Magento\Framework\DB\Ddl\Table::TYPE_BIGINT: $isUnsigned = (bool) $column['UNSIGNED']; if ($column['DEFAULT'] === '') { $column['DEFAULT'] = null; } $options = null; if ($column['SCALE'] > 0) { $ddlType = \Magento\Framework\DB\Ddl\Table::TYPE_DECIMAL; } else { break; } // fall-through intentional // fall-through intentional case \Magento\Framework\DB\Ddl\Table::TYPE_DECIMAL: $options = $column['PRECISION'] . ',' . $column['SCALE']; $isUnsigned = null; if ($column['DEFAULT'] === '') { $column['DEFAULT'] = null; } break; case \Magento\Framework\DB\Ddl\Table::TYPE_TEXT: $options = $column['LENGTH']; $isUnsigned = null; break; case \Magento\Framework\DB\Ddl\Table::TYPE_TIMESTAMP: $options = null; $isUnsigned = null; break; case \Magento\Framework\DB\Ddl\Table::TYPE_DATETIME: $isUnsigned = null; break; } $columns[$column['COLUMN_NAME']] = ['type' => [$ddlType, $options], 'unsigned' => $isUnsigned, 'nullable' => $column['NULLABLE'], 'default' => $column['DEFAULT'] === null ? false : $column['DEFAULT'], 'comment' => $column['COLUMN_NAME']]; } $columns['store_id'] = ['type' => [\Magento\Framework\DB\Ddl\Table::TYPE_SMALLINT, 5], 'unsigned' => true, 'nullable' => false, 'default' => '0', 'comment' => 'Store Id']; return $columns; }
/** * Copy data from source table to destination * * @param string $sourceTable * @param string $destTable * @param null|string $where * @return void */ protected function _insertFromTable($sourceTable, $destTable, $where = null) { $sourceColumns = array_keys($this->_connection->describeTable($sourceTable)); $targetColumns = array_keys($this->_connection->describeTable($destTable)); $select = $this->_connection->select()->from($sourceTable, $sourceColumns); if ($where) { $select->where($where); } $query = $this->_connection->insertFromSelect($select, $destTable, $targetColumns, \Magento\Framework\DB\Adapter\AdapterInterface::INSERT_ON_DUPLICATE); $this->_connection->query($query); }
/** * Synchronize data between index storage and original storage * * @return $this */ protected function _syncData() { $idxTableName = $this->_getIdxTable(); $tableName = $this->_getTable('cataloginventory_stock_status'); $this->_deleteOldRelations($tableName); $columns = array_keys($this->_connection->describeTable($idxTableName)); $select = $this->_connection->select()->from($idxTableName, $columns); $query = $select->insertFromSelect($tableName, $columns); $this->_connection->query($query); return $this; }
/** * Text columns * * @param string $table Database table name * @return array list of columns with text type */ protected function getTextColumns($table) { $result = []; $allowedTypes = ['text', 'varchar', 'mediumtext', 'longtext']; $columns = $this->connection->describeTable($table); foreach ($columns as $column => $info) { if (in_array($info['DATA_TYPE'], $allowedTypes)) { $result[] = $column; } } return $result; }
/** * Collect fields for the entity with composite type. * * @param array $fixture * @return array */ protected function compositeCollectFields(array $fixture) { $entityTypes = $fixture['entities']; $fields = []; foreach ($entityTypes as $entityType) { $fields = array_merge($fields, $this->connection->describeTable($entityType)); } $attributes = []; foreach ($fields as $code => $field) { $attributes[$code] = ['attribute_code' => $code, 'backend_type' => $field['DATA_TYPE'], 'is_required' => $field['PRIMARY'] || $field['IDENTITY'], 'default_value' => $field['DEFAULT'], 'input' => '']; } return $attributes; }
/** * @param EntityMetadataInterface $metadata * @param AdapterInterface $connection * @param array $data * @return array */ protected function prepareData(EntityMetadataInterface $metadata, AdapterInterface $connection, $data) { $output = []; foreach ($connection->describeTable($metadata->getEntityTable()) as $column) { if ($column['DEFAULT'] == 'CURRENT_TIMESTAMP' || $column['IDENTITY']) { continue; } if (isset($data[strtolower($column['COLUMN_NAME'])])) { $output[strtolower($column['COLUMN_NAME'])] = $data[strtolower($column['COLUMN_NAME'])]; } } if (empty($data[$metadata->getIdentifierField()])) { $output[$metadata->getIdentifierField()] = $metadata->generateIdentifier(); } return $output; }
/** * @inheritdoc */ public function getDocumentStructure($documentName) { return $this->resourceAdapter->describeTable($documentName); }
/** * Write single product into flat product table * * @param int $storeId * @param int $productId * @param string $valueFieldSuffix * @return \Magento\Catalog\Model\Indexer\Product\Flat */ public function write($storeId, $productId, $valueFieldSuffix = '') { $flatTable = $this->_productIndexerHelper->getFlatTableName($storeId); $attributes = $this->_productIndexerHelper->getAttributes(); $eavAttributes = $this->_productIndexerHelper->getTablesStructure($attributes); $updateData = array(); $describe = $this->_connection->describeTable($flatTable); foreach ($eavAttributes as $tableName => $tableColumns) { $columnsChunks = array_chunk($tableColumns, self::ATTRIBUTES_CHUNK_SIZE, true); foreach ($columnsChunks as $columns) { $select = $this->_connection->select(); $selectValue = $this->_connection->select(); $keyColumns = array('entity_id' => 'e.entity_id', 'attribute_id' => 't.attribute_id', 'value' => $this->_connection->getIfNullSql('`t2`.`value`', '`t`.`value`')); if ($tableName != $this->_productIndexerHelper->getTable('catalog_product_entity')) { $valueColumns = array(); $ids = array(); $select->from(array('e' => $this->_productIndexerHelper->getTable('catalog_product_entity')), $keyColumns); $selectValue->from(array('e' => $this->_productIndexerHelper->getTable('catalog_product_entity')), $keyColumns); /** @var $attribute \Magento\Catalog\Model\Resource\Eav\Attribute */ foreach ($columns as $columnName => $attribute) { if (isset($describe[$columnName])) { $ids[$attribute->getId()] = $columnName; } } $select->joinLeft(array('t' => $tableName), 'e.entity_id = t.entity_id ' . $this->_connection->quoteInto(' AND t.attribute_id IN (?)', array_keys($ids)) . ' AND t.store_id = 0', array())->joinLeft(array('t2' => $tableName), 't.entity_id = t2.entity_id ' . ' AND t.attribute_id = t2.attribute_id ' . $this->_connection->quoteInto(' AND t2.store_id = ?', $storeId), array())->where('e.entity_id = ' . $productId)->where('t.attribute_id IS NOT NULL'); $cursor = $this->_connection->query($select); while ($row = $cursor->fetch(\Zend_Db::FETCH_ASSOC)) { $updateData[$ids[$row['attribute_id']]] = $row['value']; $valueColumnName = $ids[$row['attribute_id']] . $valueFieldSuffix; if (isset($describe[$valueColumnName])) { $valueColumns[$row['value']] = $valueColumnName; } } //Update not simple attributes (eg. dropdown) if (!empty($valueColumns)) { $valueIds = array_keys($valueColumns); $select = $this->_connection->select()->from(array('t' => $this->_productIndexerHelper->getTable('eav_attribute_option_value')), array('t.option_id', 't.value'))->where($this->_connection->quoteInto('t.option_id IN (?)', $valueIds)); $cursor = $this->_connection->query($select); while ($row = $cursor->fetch(\Zend_Db::FETCH_ASSOC)) { $valueColumnName = $valueColumns[$row['option_id']]; if (isset($describe[$valueColumnName])) { $updateData[$valueColumnName] = $row['value']; } } } } else { $columnNames = array_keys($columns); $columnNames[] = 'attribute_set_id'; $columnNames[] = 'type_id'; $select->from(array('e' => $this->_productIndexerHelper->getTable('catalog_product_entity')), $columnNames)->where('e.entity_id = ' . $productId); $cursor = $this->_connection->query($select); $row = $cursor->fetch(\Zend_Db::FETCH_ASSOC); if (!empty($row)) { foreach ($row as $columnName => $value) { $updateData[$columnName] = $value; } } } } } if (!empty($updateData)) { $updateData += array('entity_id' => $productId); $updateFields = array(); foreach ($updateData as $key => $value) { $updateFields[$key] = $key; } $this->_connection->insertOnDuplicate($flatTable, $updateData, $updateFields); } return $this; }