-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathSelectHydrator.php
152 lines (135 loc) · 4.08 KB
/
SelectHydrator.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
<?php
/**
* Copyright 2021 Adobe
* All Rights Reserved.
*
* NOTICE: All information contained herein is, and remains
* the property of Adobe and its suppliers, if any. The intellectual
* and technical concepts contained herein are proprietary to Adobe
* and its suppliers and are protected by all applicable intellectual
* property laws, including trade secret and copyright laws.
* Dissemination of this information or reproduction of this material
* is strictly forbidden unless prior written permission is obtained
* from Adobe.
*/
namespace Magento\QueryXml\Model;
use Magento\Framework\App\ResourceConnection;
use Magento\Framework\DB\Select;
use Magento\Framework\ObjectManagerInterface;
/**
* Hydrator for report select parts
*/
class SelectHydrator
{
/**
* Array of supported Select parts
*
* @var array
*/
private $predefinedSelectParts =
[
Select::DISTINCT,
Select::COLUMNS,
Select::UNION,
Select::FROM,
Select::WHERE,
Select::GROUP,
Select::HAVING,
Select::ORDER,
Select::LIMIT_COUNT,
Select::LIMIT_OFFSET,
Select::FOR_UPDATE
];
/**
* @var array
*/
private $selectParts;
/**
* @var ResourceConnection
*/
private $resourceConnection;
/**
* @var ObjectManagerInterface
*/
private $objectManager;
/**
* @param ResourceConnection $resourceConnection
* @param ObjectManagerInterface $objectManager
* @param array $selectParts
*/
public function __construct(
ResourceConnection $resourceConnection,
ObjectManagerInterface $objectManager,
$selectParts = []
) {
$this->resourceConnection = $resourceConnection;
$this->objectManager = $objectManager;
$this->selectParts = $selectParts;
}
/**
* @return array
*/
private function getSelectParts()
{
return array_merge($this->predefinedSelectParts, $this->selectParts);
}
/**
* Extracts Select metadata parts
*
* @param Select $select
* @return array
* @throws \Zend_Db_Select_Exception
*/
public function extract(Select $select)
{
$parts = [];
foreach ($this->getSelectParts() as $partName) {
$parts[$partName] = $select->getPart($partName);
}
return $parts;
}
/**
* @param array $selectParts
* @return Select
*/
public function recreate(array $selectParts)
{
$select = $this->resourceConnection->getConnection()->select();
$select = $this->processColumns($select, $selectParts);
foreach ($selectParts as $partName => $partValue) {
$select->setPart($partName, $partValue);
}
return $select;
}
/**
* Process COLUMNS part values and add this part into select.
*
* If each column contains information about select expression
* an object with the type of this expression going to be created and assigned to this column.
*
* @param Select $select
* @param array $selectParts
* @return Select
*/
private function processColumns(Select $select, array &$selectParts)
{
if (!empty($selectParts[Select::COLUMNS]) && is_array($selectParts[Select::COLUMNS])) {
$part = [];
foreach ($selectParts[Select::COLUMNS] as $columnEntry) {
list($correlationName, $column, $alias) = $columnEntry;
if (is_array($column) && !empty($column['class'])) {
$expression = $this->objectManager->create(
$column['class'],
isset($column['arguments']) ? $column['arguments'] : []
);
$part[] = [$correlationName, $expression, $alias];
} else {
$part[] = $columnEntry;
}
}
$select->setPart(Select::COLUMNS, $part);
unset($selectParts[Select::COLUMNS]);
}
return $select;
}
}