/** * @param RequestFilterInterface|\Magento\Framework\Search\Request\Filter\Bool $filter * @param bool $isNegation * @return string */ private function processBoolFilter(RequestFilterInterface $filter, $isNegation) { $must = $this->buildFilters($filter->getMust(), Select::SQL_AND, $isNegation); $should = $this->buildFilters($filter->getShould(), Select::SQL_OR, $isNegation); $mustNot = $this->buildFilters($filter->getMustNot(), Select::SQL_AND, !$isNegation); $queries = [$must, $this->conditionManager->wrapBrackets($should), $this->conditionManager->wrapBrackets($mustNot)]; return $this->conditionManager->combineQueries($queries, Select::SQL_AND); }
/** * @param FilterInterface $filter * @param bool $isNegation * @param string $query * @return string * @SuppressWarnings(PHPMD.UnusedFormalParameter) */ private function processQueryWithField(FilterInterface $filter, $isNegation, $query) { $currentStoreId = $this->scopeResolver->getScope()->getId(); $attribute = $this->config->getAttribute(\Magento\Catalog\Model\Product::ENTITY, $filter->getField()); $select = $this->getSelect(); $table = $attribute->getBackendTable(); if ($filter->getField() == 'price') { $query = str_replace('price', 'min_price', $query); $select->from(['main_table' => $this->resource->getTableName('catalog_product_index_price')], 'entity_id')->where($query); } elseif ($filter->getField() == 'category_ids') { return 'category_index.category_id = ' . $filter->getValue(); } else { if ($attribute->isStatic()) { $select->from(['main_table' => $table], 'entity_id')->where($query); } else { if ($filter->getType() == FilterInterface::TYPE_TERM) { $field = $filter->getField(); $mapper = function ($value) use($field, $isNegation) { return ($isNegation ? '-' : '') . $this->attributePrefix . $field . '_' . $value; }; if (is_array($filter->getValue())) { $value = implode(' ', array_map($mapper, $filter->getValue())); } else { $value = $mapper($filter->getValue()); } return 'MATCH (data_index) AGAINST (' . $this->getConnection()->quote($value) . ' IN BOOLEAN MODE)'; } $ifNullCondition = $this->getConnection()->getIfNullSql('current_store.value', 'main_table.value'); $select->from(['main_table' => $table], 'entity_id')->joinLeft(['current_store' => $table], 'current_store.attribute_id = main_table.attribute_id AND current_store.store_id = ' . $currentStoreId, null)->columns([$filter->getField() => $ifNullCondition])->where('main_table.attribute_id = ?', $attribute->getAttributeId())->where('main_table.store_id = ?', \Magento\Store\Model\Store::DEFAULT_STORE_ID)->having($query); } } return 'search_index.product_id IN ( select entity_id from ' . $this->conditionManager->wrapBrackets($select) . ' as filter )'; }
/** * @param FilterInterface $filter * @param bool $isNegation * @param string $query * @param QueryContainer $queryContainer * @return string */ private function processQueryWithField(FilterInterface $filter, $isNegation, $query, QueryContainer $queryContainer) { $currentStoreId = $this->scopeResolver->getScope()->getId(); $attribute = $this->config->getAttribute(\Magento\Catalog\Model\Product::ENTITY, $filter->getField()); $select = $this->getConnection()->select(); $table = $attribute->getBackendTable(); if ($filter->getField() == 'price') { $query = str_replace('price', 'min_price', $query); $select->from(['main_table' => $this->resource->getTableName('catalog_product_index_price')], 'entity_id')->where($query); } elseif ($filter->getField() == 'category_ids') { return 'category_index.category_id = ' . $filter->getValue(); } else { if ($attribute->isStatic()) { $select->from(['main_table' => $table], 'entity_id')->where($query); } else { if ($filter->getType() == FilterInterface::TYPE_TERM) { if (is_array($filter->getValue())) { $value = sprintf('%s IN (%s)', $isNegation ? 'NOT' : '', implode(',', $filter->getValue())); } else { $value = ($isNegation ? '!' : '') . '= ' . $filter->getValue(); } $filterQuery = sprintf('cpie.store_id = %d AND cpie.attribute_id = %d AND cpie.value %s', $this->scopeResolver->getScope()->getId(), $attribute->getId(), $value); $queryContainer->addFilter($filterQuery); return ''; } $ifNullCondition = $this->getConnection()->getIfNullSql('current_store.value', 'main_table.value'); $select->from(['main_table' => $table], 'entity_id')->joinLeft(['current_store' => $table], 'current_store.attribute_id = main_table.attribute_id AND current_store.store_id = ' . $currentStoreId, null)->columns([$filter->getField() => $ifNullCondition])->where('main_table.attribute_id = ?', $attribute->getAttributeId())->where('main_table.store_id = ?', \Magento\Store\Model\Store::DEFAULT_STORE_ID)->having($query); } } return 'search_index.entity_id IN ( select entity_id from ' . $this->conditionManager->wrapBrackets($select) . ' as filter )'; }
/** * @param FilterInterface $filter * @param bool $isNegation * @param string $query * @return string */ private function processQueryWithField(FilterInterface $filter, $isNegation, $query) { /** @var \Magento\Catalog\Model\ResourceModel\Eav\Attribute $attribute */ $attribute = $this->config->getAttribute(Product::ENTITY, $filter->getField()); if ($filter->getField() === 'price') { $resultQuery = str_replace($this->connection->quoteIdentifier('price'), $this->connection->quoteIdentifier('price_index.min_price'), $query); } elseif ($filter->getField() === 'category_ids') { return 'category_ids_index.category_id = ' . $filter->getValue(); } elseif ($attribute->isStatic()) { $alias = $this->tableMapper->getMappingAlias($filter); $resultQuery = str_replace($this->connection->quoteIdentifier($attribute->getAttributeCode()), $this->connection->quoteIdentifier($alias . '.' . $attribute->getAttributeCode()), $query); } elseif ($filter->getType() === FilterInterface::TYPE_TERM && in_array($attribute->getFrontendInput(), ['select', 'multiselect'], true)) { $alias = $this->tableMapper->getMappingAlias($filter); if (is_array($filter->getValue())) { $value = sprintf('%s IN (%s)', $isNegation ? 'NOT' : '', implode(',', $filter->getValue())); } else { $value = ($isNegation ? '!' : '') . '= ' . $filter->getValue(); } $resultQuery = sprintf('%1$s.value %2$s', $alias, $value); } else { $table = $attribute->getBackendTable(); $select = $this->connection->select(); $ifNullCondition = $this->connection->getIfNullSql('current_store.value', 'main_table.value'); $currentStoreId = $this->scopeResolver->getScope()->getId(); $select->from(['main_table' => $table], 'entity_id')->joinLeft(['current_store' => $table], 'current_store.attribute_id = main_table.attribute_id AND current_store.store_id = ' . $currentStoreId, null)->columns([$filter->getField() => $ifNullCondition])->where('main_table.attribute_id = ?', $attribute->getAttributeId())->where('main_table.store_id = ?', Store::DEFAULT_STORE_ID)->having($query); $resultQuery = 'search_index.entity_id IN ( select entity_id from ' . $this->conditionManager->wrapBrackets($select) . ' as filter )'; } return $resultQuery; }
/** * Add filtering by dimensions * * @param RequestInterface $request * @param Select $select * @return \Magento\Framework\DB\Select */ private function processDimensions(RequestInterface $request, Select $select) { $dimensions = $this->prepareDimensions($request->getDimensions()); $query = $this->conditionManager->combineQueries($dimensions, Select::SQL_OR); if (!empty($query)) { $select->where($this->conditionManager->wrapBrackets($query)); } return $select; }
/** * @param \Magento\Framework\Search\Request\FilterInterface[] $filters * @param string $unionOperator * @param bool $isNegation * @return string */ private function buildFilters(array $filters, $unionOperator, $isNegation) { $queries = []; foreach ($filters as $filter) { $filterQuery = $this->processFilter($filter, $isNegation); $queries[] = $this->conditionManager->wrapBrackets($filterQuery); } return $this->conditionManager->combineQueries($queries, $unionOperator); }
/** * @SuppressWarnings(PHPMD.NPathComplexity) * @SuppressWarnings(PHPMD.ExcessiveMethodLength) */ protected function setUp() { $objectManager = new ObjectManager($this); $this->conditionManager = $this->getMockBuilder('Magento\\Framework\\Search\\Adapter\\Mysql\\ConditionManager')->disableOriginalConstructor()->setMethods(['generateCondition', 'combineQueries', 'wrapBrackets'])->getMock(); $this->conditionManager->expects($this->any())->method('generateCondition')->will($this->returnCallback(function ($field, $operator, $value) { return sprintf('%s %s %s', $field, $operator, $value); })); $this->conditionManager->expects($this->any())->method('combineQueries')->will($this->returnCallback(function ($queries, $operator) { return implode(' ' . $operator . ' ', array_filter($queries, 'strlen')); })); $this->conditionManager->expects($this->any())->method('wrapBrackets')->will($this->returnCallback(function ($query) { return !empty($query) ? sprintf('(%s)', $query) : ''; })); $rangeBuilder = $this->getMockBuilder('\\Magento\\Framework\\Search\\Adapter\\Mysql\\Filter\\Builder\\Range')->setMethods(['buildFilter'])->disableOriginalConstructor()->getMock(); $rangeBuilder->expects($this->any())->method('buildFilter')->will($this->returnCallback(function (FilterInterface $filter, $isNegation) { /** * @var \Magento\Framework\Search\Request\Filter\Range $filter * @var \Magento\Framework\DB\Adapter\AdapterInterface $adapter */ $fromCondition = ''; if ($filter->getFrom() !== null) { $fromCondition = $this->conditionManager->generateCondition($filter->getField(), $isNegation ? '<' : '>=', $filter->getFrom()); } $toCondition = ''; if ($filter->getTo() !== null) { $toCondition = $this->conditionManager->generateCondition($filter->getField(), $isNegation ? '>=' : '<', $filter->getTo()); } $unionOperator = $isNegation ? Select::SQL_OR : Select::SQL_AND; return $this->conditionManager->combineQueries([$fromCondition, $toCondition], $unionOperator); })); $termBuilder = $this->getMockBuilder('\\Magento\\Framework\\Search\\Adapter\\Mysql\\Filter\\Builder\\Term')->setMethods(['buildFilter'])->disableOriginalConstructor()->getMock(); $termBuilder->expects($this->any())->method('buildFilter')->will($this->returnCallback(function (FilterInterface $filter, $isNegation) { /** * @var \Magento\Framework\Search\Request\Filter\Term $filter * @var \Magento\Framework\DB\Adapter\AdapterInterface $adapter */ return $this->conditionManager->generateCondition($filter->getField(), $isNegation ? '!=' : '=', $filter->getValue()); })); $this->preprocessor = $this->getMockBuilder('Magento\\Framework\\Search\\Adapter\\Mysql\\Filter\\PreprocessorInterface')->setMethods(['process'])->disableOriginalConstructor()->getMockForAbstractClass(); $this->preprocessor->expects($this->any())->method('process')->willReturnCallback(function ($filter, $isNegation, $queryString) { return $this->conditionManager->wrapBrackets($queryString); }); $this->builder = $objectManager->getObject('Magento\\Framework\\Search\\Adapter\\Mysql\\Filter\\Builder', ['range' => $rangeBuilder, 'term' => $termBuilder, 'conditionManager' => $this->conditionManager, 'preprocessor' => $this->preprocessor]); }
/** * @param FilterInterface $filter * @param string $query * @param Attribute $attribute * @return string */ private function processRangeNumeric(FilterInterface $filter, $query, $attribute) { $tableSuffix = $attribute->getBackendType() === 'decimal' ? '_decimal' : ''; $table = $this->resource->getTableName("catalog_product_index_eav{$tableSuffix}"); $select = $this->connection->select(); $currentStoreId = $this->scopeResolver->getScope()->getId(); $select->from(['main_table' => $table], 'entity_id')->columns([$filter->getField() => 'main_table.value'])->where('main_table.attribute_id = ?', $attribute->getAttributeId())->where('main_table.store_id = ?', $currentStoreId)->having($query); $resultQuery = 'search_index.entity_id IN ( select entity_id from ' . $this->conditionManager->wrapBrackets($select) . ' as filter )'; return $resultQuery; }
/** * Add filtering by dimensions * * @param RequestInterface $request * @param Select $select * @return \Magento\Framework\DB\Select */ private function processDimensions(RequestInterface $request, Select $select) { $dimensions = []; foreach ($request->getDimensions() as $dimension) { $dimensions[] = $this->dimensionsBuilder->build($dimension); } $query = $this->conditionManager->combineQueries($dimensions, Select::SQL_OR); if (!empty($query)) { $select->where($this->conditionManager->wrapBrackets($query)); } return $select; }
/** * @dataProvider wrapBracketsDataProvider * @param $query * @param $expectedResult */ public function testWrapBrackets($query, $expectedResult) { $actualResult = $this->conditionManager->wrapBrackets($query); $this->assertEquals($expectedResult, $actualResult); }