Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,11 @@ public function getBulksByUser($userId)
OperationInterface::STATUS_TYPE_COMPLETE
];
$select = $collection->getSelect();
$select->columns(['status' => $this->calculatedStatusSql->get($operationTableName)])
->order(new \Zend_Db_Expr('FIELD(status, ' . implode(',', $statusesArray) . ')'));
$statusExpr = $this->calculatedStatusSql->get($operationTableName);
$select->columns(['status' => $statusExpr])
->order(
$this->resourceConnection->getConnection()->getFieldSql((string) $statusExpr, $statusesArray)
);
$collection->addFieldToFilter('user_id', $userId)
->addOrder('start_time');

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,10 @@ public function testGetBulksByUser(): void
$bulkCollection->expects($this->once())->method('getSelect')->willReturn($selectMock);
$selectMock->expects($this->once())->method('columns')->willReturnSelf();
$selectMock->expects($this->once())->method('order')->willReturnSelf();
$this->resourceConnectionMock->expects($this->once())
->method('getConnection')
->willReturn($this->connectionMock);
$this->connectionMock->expects($this->once())->method('getFieldSql')->willReturn('FIELD(status, 1, 2)');
$this->bulkCollectionFactory->expects($this->once())->method('create')->willReturn($bulkCollection);
$bulkCollection->expects($this->once())->method('addFieldToFilter')->with('user_id', $userId)->willReturnSelf();
$bulkCollection->expects($this->once())->method('getItems')->willReturn([$this->bulkMock]);
Expand Down
2 changes: 1 addition & 1 deletion app/code/Magento/Backup/Model/ResourceModel/Helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ public function getHeader()
{
$dbConfig = $this->getConnection()->getConfig();

$versionRow = $this->getConnection()->fetchRow('SHOW VARIABLES LIKE \'version\'');
$versionRow = ['Value' => $this->getConnection()->getServerVersion()];
$hostName = !empty($dbConfig['unix_socket'])
? $dbConfig['unix_socket']
: (!empty($dbConfig['host']) ? $dbConfig['host'] : 'localhost');
Expand Down
47 changes: 19 additions & 28 deletions app/code/Magento/Bundle/Model/ResourceModel/Indexer/Price.php
Original file line number Diff line number Diff line change
Expand Up @@ -485,7 +485,7 @@ private function calculateBundleOptionPrice($priceTable, $dimensions)
[
'min_price' => new \Zend_Db_Expr('MIN(' . $minPrice . ')'),
'alt_price' => new \Zend_Db_Expr('MIN(price)'),
'max_price' => $connection->getCheckSql('group_type = 0', 'MAX(price)', 'SUM(price)'),
'max_price' => $connection->getCheckSql('MIN(group_type) = 0', 'MAX(price)', 'SUM(price)'),
'tier_price' => new \Zend_Db_Expr('MIN(' . $tierPrice . ')'),
'alt_tier_price' => new \Zend_Db_Expr('MIN(tier_price)'),
]
Expand Down Expand Up @@ -734,33 +734,24 @@ private function calculateDynamicBundleSelectionPrice(array $dimensions): void
]
);
$select = $this->stockStatusQueryProcessor->execute($select);
$query = str_replace('AS `idx`', 'AS `idx` USE INDEX (PRIMARY)', (string) $select);

$insertColumns = [
'entity_id',
'customer_group_id',
'website_id',
'option_id',
'selection_id',
'group_type',
'is_required',
'price',
'tier_price'
];
$insertColumns = array_map(function ($item) use ($connection) {
return $connection->quoteIdentifier($item);
}, $insertColumns);
$updateValues = [];
foreach ($insertColumns as $column) {
$updateValues[] = sprintf("%s = VALUES(%s)", $column, $column);
}

$connection->query(sprintf(
"INSERT INTO `" . $this->getBundleSelectionTable() . "` (%s) %s ON DUPLICATE KEY UPDATE %s",
implode(",", $insertColumns),
$query,
implode(",", $updateValues)
));
$connection->query(
$connection->insertFromSelect(
$select,
$this->getBundleSelectionTable(),
[
'entity_id',
'customer_group_id',
'website_id',
'option_id',
'selection_id',
'group_type',
'is_required',
'price',
'tier_price',
],
\Magento\Framework\DB\Adapter\AdapterInterface::INSERT_ON_DUPLICATE
)
);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,8 @@ public function execute(int $entityId, int $storeId): bool
);
$isOptionSalableExpr = new \Zend_Db_Expr(
sprintf(
'MAX(IFNULL(child_status_store.value, child_status_global.value) != %s)',
'MAX(CASE WHEN %s != %s THEN 1 ELSE 0 END)',
$connection->getIfNullSql('child_status_store.value', 'child_status_global.value'),
ProductStatus::STATUS_DISABLED
)
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,9 @@ public function addPriceFilter($product, $searchMin, $useRegularPrice = false)
$minimalPriceExpression = self::INDEX_TABLE_ALIAS . '.price';
} else {
$this->getCatalogRuleProcessor()->addPriceData($this, 'selection.product_id');
$minimalPriceExpression = 'LEAST(minimal_price, IFNULL(catalog_rule_price, minimal_price))';
$minimalPriceExpression = 'LEAST(minimal_price, '
. $this->getConnection()->getIfNullSql('catalog_rule_price', 'minimal_price')
. ')';
}
$orderByValue = new \Zend_Db_Expr(
'(' .
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -154,40 +154,6 @@ public function testCalculateDynamicBundleSelectionPrice(): void
`is_required` = VALUES(`is_required`),
`price` = VALUES(`price`),
`tier_price` = VALUES(`tier_price`)";
$processedQuery = "INSERT INTO `catalog_product_index_price_bundle_sel_temp` (,,,,,,,,) SELECT `i`.`entity_id`,
`i`.`customer_group_id`,
`i`.`website_id`,
`bo`.`option_id`,
`bs`.`selection_id`,
IF(bo.type = 'select' OR bo.type = 'radio', 0, 1) AS `group_type`,
`bo`.`required` AS `is_required`,
LEAST(IF(i.special_price > 0 AND i.special_price < 100,
ROUND(idx.min_price * bs.selection_qty * (i.special_price / 100), 4), idx.min_price * bs.selection_qty),
IFNULL((IF(i.tier_percent IS NOT NULL,
ROUND((1 - i.tier_percent / 100) * idx.min_price * bs.selection_qty, 4), NULL)), idx.min_price *
bs.selection_qty)) AS `price`,
IF(i.tier_percent IS NOT NULL, ROUND((1 - i.tier_percent / 100) * idx.min_price * bs.selection_qty, 4),
NULL) AS `tier_price`
FROM `catalog_product_index_price_bundle_temp` AS `i`
INNER JOIN `catalog_product_entity` AS `parent_product` ON parent_product.entity_id = i.entity_id AND
(parent_product.created_in <= 1 AND parent_product.updated_in > 1)
INNER JOIN `catalog_product_bundle_option` AS `bo` ON bo.parent_id = parent_product.row_id
INNER JOIN `catalog_product_bundle_selection` AS `bs` ON bs.option_id = bo.option_id
INNER JOIN `catalog_product_index_price_replica` AS `idx` USE INDEX (PRIMARY)
ON bs.product_id = idx.entity_id AND i.customer_group_id = idx.customer_group_id AND
i.website_id = idx.website_id
INNER JOIN `cataloginventory_stock_status` AS `si` ON si.product_id = bs.product_id
WHERE (i.price_type = 0)
AND (si.stock_status = 1)
ON DUPLICATE KEY UPDATE `entity_id` = VALUES(`entity_id`),
`customer_group_id` = VALUES(`customer_group_id`),
`website_id` = VALUES(`website_id`),
`option_id` = VALUES(`option_id`),
`selection_id` = VALUES(`selection_id`),
`group_type` = VALUES(`group_type`),
`is_required` = VALUES(`is_required`),
`price` = VALUES(`price`),
`tier_price` = VALUES(`tier_price`) ON DUPLICATE KEY UPDATE = VALUES(), = VALUES(), = VALUES(), = VALUES(), = VALUES(), = VALUES(), = VALUES(), = VALUES(), = VALUES()";
//@codingStandardsIgnoreEnd
$this->connectionMock->expects($this->exactly(3))
->method('getCheckSql')
Expand Down Expand Up @@ -232,8 +198,17 @@ public function testCalculateDynamicBundleSelectionPrice(): void
$this->connectionMock->expects($this->once())->method('getIfNullSql');
$this->connectionMock->expects($this->once())->method('getLeastSql');
$this->connectionMock->method('select')->willReturn($select);
$this->connectionMock->expects($this->exactly(9))->method('quoteIdentifier');
$this->connectionMock->expects($this->once())->method('query')->with($processedQuery);
$insertSql = 'INSERT INTO catalog_product_index_price_bundle_sel_temp ... ON DUPLICATE KEY UPDATE';
$this->connectionMock->expects($this->once())
->method('insertFromSelect')
->with(
$select,
$this->isType('string'),
$this->isType('array'),
AdapterInterface::INSERT_ON_DUPLICATE
)
->willReturn($insertSql);
$this->connectionMock->expects($this->once())->method('query')->with($insertSql);

$pool = $this->createMock(EntityMetadataInterface::class);
$pool->expects($this->once())->method('getLinkField')->willReturn($entity);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,10 @@ private function initAttributeValues($entityType, $entity, $storeId)
$selects = [];
foreach ($attributeTables as $attributeTable => $attributeCodes) {
$select = $metadata->getEntityConnection()->select()
->from(['t' => $attributeTable], ['value' => 't.value', 'store_id' => 't.store_id'])
->from(
['t' => $attributeTable],
['value' => $metadata->getEntityConnection()->castToText('t.value'), 'store_id' => 't.store_id']
)
->join(
['a' => $this->resourceConnection->getTableName('eav_attribute')],
'a.attribute_id = t.attribute_id',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -388,7 +388,7 @@ protected function getNonAnchorCategoriesSelect(Store $store)
[
'category_id' => 'cc.entity_id',
'product_id' => 'ccp.product_id',
'position' => 'ccp.position',
'position' => new \Zend_Db_Expr('MIN(ccp.position)'),
'is_parent' => new \Zend_Db_Expr('1'),
'store_id' => new \Zend_Db_Expr($store->getId()),
'visibility' => new \Zend_Db_Expr(
Expand Down Expand Up @@ -640,7 +640,7 @@ protected function createAnchorSelect(Store $store)
'category_id' => 'cc.entity_id',
'product_id' => 'ccp.product_id',
'position' => new \Zend_Db_Expr(
$this->connection->getIfNullSql('ccp2.position', 'MIN(ccp.position) + 10000')
$this->connection->getIfNullSql('MAX(ccp2.position)', 'MIN(ccp.position) + 10000')
),
'is_parent' => new \Zend_Db_Expr('0'),
'store_id' => new \Zend_Db_Expr($store->getId()),
Expand Down Expand Up @@ -874,16 +874,16 @@ protected function getAllProducts(Store $store)
$this->connection->getIfNullSql('cpvs.value', 'cpvd.value') . ' IN (?)',
$this->visibility->getVisibleInSiteIds()
)->group(
'cp.entity_id'
['cp.entity_id', new \Zend_Db_Expr($this->connection->getIfNullSql('cpvs.value', 'cpvd.value'))]
)->columns(
[
'category_id' => new \Zend_Db_Expr($store->getRootCategoryId()),
'product_id' => 'cp.entity_id',
'position' => new \Zend_Db_Expr(
$this->connection->getCheckSql('ccp.product_id IS NOT NULL', 'MIN(ccp.position)', '10000')
$this->connection->getCheckSql('COUNT(ccp.product_id) > 0', 'MIN(ccp.position)', '10000')
),
'is_parent' => new \Zend_Db_Expr(
$this->connection->getCheckSql('ccp.product_id IS NOT NULL', '1', '0')
$this->connection->getCheckSql('COUNT(ccp.product_id) > 0', '1', '0')
),
'store_id' => new \Zend_Db_Expr($store->getId()),
'visibility' => new \Zend_Db_Expr(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,10 @@ public function removeDisabledProducts(array &$ids, $storeId)
. 'product_table.' . $metadata->getLinkField(),
[]
);
$select->where('IFNULL(status_attr.value, status_global_attr.value) = ?', Status::STATUS_DISABLED);
$select->where(
$this->connection->getIfNullSql('status_attr.value', 'status_global_attr.value') . ' = ?',
Status::STATUS_DISABLED
);

$result = $this->connection->query($select);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -723,9 +723,7 @@ private function getProductsCountQuery(array $categoryIds, $addVisibilityFilter
if (true === $addVisibilityFilter) {
$select->where('cat_index.visibility in (?)', $this->catalogProductVisibility->getVisibleInSiteIds());
}
if (count($categoryIds) > 1) {
$select->group('cat_index.category_id');
}
$select->group('cat_index.category_id');

return $select;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -239,10 +239,18 @@ protected function _addLoadAttributesSelectValues($select, $table, $type)
$storeId = $this->getStoreId();
if ($storeId) {
$connection = $this->getConnection();
$valueExpr = $connection->getCheckSql('t_s.value_id IS NULL', 't_d.value', 't_s.value');
$valueExpr = $connection->getCheckSql(
't_s.value_id IS NULL',
$connection->castToText('t_d.value'),
$connection->castToText('t_s.value')
);

$select->columns(
['default_value' => 't_d.value', 'store_value' => 't_s.value', 'value' => $valueExpr]
[
'default_value' => $connection->castToText('t_d.value'),
'store_value' => $connection->castToText('t_s.value'),
'value' => $valueExpr,
]
);
} else {
$select = parent::_addLoadAttributesSelectValues($select, $table, $type);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,8 @@ public function execute(Collection $collection, array $categoryIds): void
[]
);
}
$positions[] = $connection->getIfNullSql($table . '.position', '~0');
// Portable max bigint for missing position.
$positions[] = $connection->getIfNullSql($table . '.position', '9223372036854775807');
}

// Ensures that position attribute is registered in _joinFields
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -224,9 +224,9 @@ public function createBatchBaseSelect($storeId, $attributeId)
),
[]
)->columns([
'label' => $this->getConnection()->getIfNullSql('`value`.`label`', '`default_value`.`label`'),
'position' => $this->getConnection()->getIfNullSql('`value`.`position`', '`default_value`.`position`'),
'disabled' => $this->getConnection()->getIfNullSql('`value`.`disabled`', '`default_value`.`disabled`'),
'label' => $this->getConnection()->getIfNullSql('value.label', 'default_value.label'),
'position' => $this->getConnection()->getIfNullSql('value.position', 'default_value.position'),
'disabled' => $this->getConnection()->getIfNullSql('value.disabled', 'default_value.disabled'),
'label_default' => 'default_value.label',
'position_default' => 'default_value.position',
'disabled_default' => 'default_value.disabled'
Expand All @@ -236,7 +236,7 @@ public function createBatchBaseSelect($storeId, $attributeId)
)->where(
$mainTableAlias . '.disabled = 0'
)->order(
$positionCheckSql . ' ' . \Magento\Framework\DB\Select::SQL_ASC
new \Zend_Db_Expr($positionCheckSql . ' ' . \Magento\Framework\DB\Select::SQL_ASC)
);

return $select;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ protected function _prepareRelationIndexSelect(?array $parentIds = null)
'i.entity_id = cpw.product_id AND sw.website_id = cpw.website_id',
[]
)->group(
['parent_id', 'i.attribute_id', 'i.store_id', 'i.value', 'l.child_id']
['e.entity_id', 'i.attribute_id', 'i.store_id', 'i.value', 'l.child_id']
)->columns(
[
'parent_id' => 'e.entity_id',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -454,7 +454,8 @@ protected function getSelect($entityIds = null, $type = null)
);
$currentDate = 'cwd.website_date';

$maxUnsignedBigint = '~0';
// Portable max bigint (not MySQL ~0).
$maxUnsignedBigint = '9223372036854775807';
$specialFromDate = $connection->getDatePartSql($specialFrom);
$specialToDate = $connection->getDatePartSql($specialTo);
$specialFromExpr = "{$specialFrom} IS NULL OR {$specialFromDate} <= {$currentDate}";
Expand Down Expand Up @@ -838,7 +839,7 @@ protected function hasEntity()
*/
private function getTotalTierPriceExpression(\Zend_Db_Expr $priceExpression)
{
$maxUnsignedBigint = '~0';
$maxUnsignedBigint = '9223372036854775807';

return $this->getConnection()->getCheckSql(
implode(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ public function getQuery(array $dimensions, string $productType, array $entityId
$specialTo = $this->joinAttributeProcessor->process($select, 'special_to_date');
$currentDate = 'cwd.website_date';

$maxUnsignedBigint = '~0';
$maxUnsignedBigint = '9223372036854775807';
$specialFromDate = $connection->getDatePartSql($specialFrom);
$specialToDate = $connection->getDatePartSql($specialTo);
$specialFromExpr = "{$specialFrom} IS NULL OR {$specialFromDate} <= {$currentDate}";
Expand Down Expand Up @@ -257,7 +257,7 @@ public function getQuery(array $dimensions, string $productType, array $entityId
*/
private function getTotalTierPriceExpression(\Zend_Db_Expr $priceExpression)
{
$maxUnsignedBigint = '~0';
$maxUnsignedBigint = '9223372036854775807';

return $this->getConnection()->getCheckSql(
implode(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,10 @@ public function process(Select $select)
[]
);

$select->where('IFNULL(status_attr.value, status_global_attr.value) = ?', Status::STATUS_ENABLED);
$select->where(
$select->getConnection()->getIfNullSql('status_attr.value', 'status_global_attr.value') . ' = ?',
Status::STATUS_ENABLED
);

return $select;
}
Expand Down
Loading
Loading