Skip to content

Commit 1e21672

Browse files
author
olysenko
committed
Merge remote-tracking branch 'origin/MAGETWO-65420' into bugfixes
2 parents 319ac53 + 6b25f65 commit 1e21672

File tree

18 files changed

+343
-72
lines changed

18 files changed

+343
-72
lines changed

app/code/Magento/Analytics/ReportXml/DB/ColumnsResolver.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66

77
namespace Magento\Analytics\ReportXml\DB;
88

9+
use Magento\Framework\DB\Sql\ColumnValueExpression;
10+
911
/**
1012
* Class ColumnsResolver
1113
*
@@ -51,7 +53,7 @@ public function getColumns(SelectBuilder $selectBuilder, $entityConfig)
5153
if (isset($attributeData['distinct']) && $attributeData['distinct'] == true) {
5254
$prefix = ' DISTINCT ';
5355
}
54-
$expression = new \Zend_Db_Expr(
56+
$expression = new ColumnValueExpression(
5557
strtoupper($attributeData['function']) . '(' . $prefix . $tableAlias . '.' . $columnName . ')'
5658
);
5759
} else {

app/code/Magento/Analytics/ReportXml/DB/ConditionResolver.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
namespace Magento\Analytics\ReportXml\DB;
88

99
use Magento\Framework\App\ResourceConnection;
10+
use Magento\Framework\DB\Sql\Expression;
1011

1112
/**
1213
* Class ConditionResolver
@@ -87,7 +88,7 @@ private function getValue($condition, $referencedEntity)
8788
$value = $this->getConnection()->quote($argument);
8889
break;
8990
case "variable":
90-
$value = new \Zend_Db_Expr($argument);
91+
$value = new Expression($argument);
9192
break;
9293
case "identifier":
9394
$value = $this->getConnection()->quoteIdentifier(
@@ -110,7 +111,9 @@ private function getValue($condition, $referencedEntity)
110111
private function getCondition(SelectBuilder $selectBuilder, $tableName, $condition, $referencedEntity = null)
111112
{
112113
$columns = $selectBuilder->getColumns();
113-
if (isset($columns[$condition['attribute']]) && $columns[$condition['attribute']] instanceof \Zend_Db_Expr) {
114+
if (isset($columns[$condition['attribute']])
115+
&& $columns[$condition['attribute']] instanceof Expression
116+
) {
114117
$expression = $columns[$condition['attribute']];
115118
} else {
116119
$expression = $tableName . '.' . $condition['attribute'];

app/code/Magento/Analytics/ReportXml/SelectHydrator.php

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
use Magento\Framework\DB\Select;
1010
use Magento\Framework\App\ResourceConnection;
11+
use Magento\Framework\ObjectManagerInterface;
1112

1213
/**
1314
* Class SelectHydrator
@@ -45,15 +46,22 @@ class SelectHydrator
4546
private $resourceConnection;
4647

4748
/**
48-
* SelectHydrator constructor.
49+
* @var ObjectManagerInterface
50+
*/
51+
private $objectManager;
52+
53+
/**
4954
* @param ResourceConnection $resourceConnection
55+
* @param ObjectManagerInterface $objectManager
5056
* @param array $selectParts
5157
*/
5258
public function __construct(
5359
ResourceConnection $resourceConnection,
60+
ObjectManagerInterface $objectManager,
5461
$selectParts = []
5562
) {
5663
$this->resourceConnection = $resourceConnection;
64+
$this->objectManager = $objectManager;
5765
$this->selectParts = $selectParts;
5866
}
5967

@@ -88,9 +96,48 @@ public function extract(Select $select)
8896
public function recreate(array $selectParts)
8997
{
9098
$select = $this->resourceConnection->getConnection()->select();
99+
100+
$select = $this->processColumns($select, $selectParts);
101+
91102
foreach ($selectParts as $partName => $partValue) {
92103
$select->setPart($partName, $partValue);
93104
}
105+
106+
return $select;
107+
}
108+
109+
/**
110+
* Process COLUMNS part values and add this part into select.
111+
*
112+
* If each column contains information about select expression
113+
* an object with the type of this expression going to be created and assigned to this column.
114+
*
115+
* @param Select $select
116+
* @param array $selectParts
117+
* @return Select
118+
*/
119+
private function processColumns(Select $select, array &$selectParts)
120+
{
121+
if (!empty($selectParts[Select::COLUMNS]) && is_array($selectParts[Select::COLUMNS])) {
122+
$part = [];
123+
124+
foreach ($selectParts[Select::COLUMNS] as $columnEntry) {
125+
list($correlationName, $column, $alias) = $columnEntry;
126+
if (is_array($column) && !empty($column['class'])) {
127+
$expression = $this->objectManager->create(
128+
$column['class'],
129+
isset($column['arguments']) ? $column['arguments'] : []
130+
);
131+
$part[] = [$correlationName, $expression, $alias];
132+
} else {
133+
$part[] = $columnEntry;
134+
}
135+
}
136+
137+
$select->setPart(Select::COLUMNS, $part);
138+
unset($selectParts[Select::COLUMNS]);
139+
}
140+
94141
return $select;
95142
}
96143
}

app/code/Magento/Analytics/Test/Unit/ReportXml/DB/ColumnsResolverTest.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Magento\Analytics\ReportXml\DB\NameResolver;
99
use Magento\Analytics\ReportXml\DB\ColumnsResolver;
1010
use Magento\Analytics\ReportXml\DB\SelectBuilder;
11+
use Magento\Framework\DB\Sql\ColumnValueExpression;
1112

1213
/**
1314
* Class ColumnsResolverTest
@@ -51,12 +52,12 @@ public function testGetColumnsWithoutAttributes()
5152
}
5253

5354
/**
54-
* @dataProvider dataProvider
55+
* @dataProvider getColumnsDataProvider
5556
*/
5657
public function testGetColumns($expression, $attributeData)
5758
{
5859
$columnAlias = 'fn';
59-
$expr = new \Zend_Db_Expr($expression);
60+
$expr = new ColumnValueExpression($expression);
6061
$expectedResult = [$columnAlias => $expr];
6162
$columns = [$columnAlias => 'name'];
6263
$entityConfig['attribute'] = ['attribute1' => $attributeData];
@@ -91,7 +92,10 @@ public function testGetColumns($expression, $attributeData)
9192
);
9293
}
9394

94-
public function dataProvider()
95+
/**
96+
* @return array
97+
*/
98+
public function getColumnsDataProvider()
9599
{
96100
return [
97101
'TestWithFunction' =>

app/code/Magento/Analytics/Test/Unit/ReportXml/DB/ConditionResolverTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use Magento\Framework\App\ResourceConnection;
1010
use Magento\Analytics\ReportXml\DB\SelectBuilder;
1111
use Magento\Framework\DB\Adapter\AdapterInterface;
12+
use Magento\Framework\DB\Sql\Expression;
1213

1314
/**
1415
* Class ConditionResolverTest
@@ -80,7 +81,7 @@ public function testGetFilter()
8081

8182
$this->selectBuilderMock->expects($this->any())
8283
->method('getColumns')
83-
->willReturn(['price' => new \Zend_Db_Expr("(n.price = 400)")]);
84+
->willReturn(['price' => new Expression("(n.price = 400)")]);
8485

8586
$this->resourceConnectionMock->expects($this->once())
8687
->method('getConnection')

0 commit comments

Comments
 (0)