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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Framework\DB;
use Psr\Log\LoggerInterface as Logger;
/**
* Class Query
*/
class Query implements QueryInterface
{
/**
* Select object
*
* @var \Magento\Framework\DB\Select
*/
protected $select;
/**
* @var \Magento\Framework\Api\CriteriaInterface
*/
protected $criteria;
/**
* Resource instance
*
* @var \Magento\Framework\Model\ResourceModel\Db\AbstractDb
*/
protected $resource;
/**
* Database's statement for fetch item one by one
*
* @var \Zend_Db_Statement_Pdo
*/
protected $fetchStmt = null;
/**
* @var Logger
*/
protected $logger;
/**
* @var \Magento\Framework\Data\Collection\Db\FetchStrategyInterface
*/
private $fetchStrategy;
/**
* @var array
*/
protected $bindParams = [];
/**
* @var int
*/
protected $totalRecords;
/**
* @var mixed
*/
protected $data;
/**
* Query Select Parts to be skipped when prepare query for count
*
* @var array
*/
protected $countSqlSkipParts = [
\Magento\Framework\DB\Select::ORDER => true,
\Magento\Framework\DB\Select::LIMIT_COUNT => true,
\Magento\Framework\DB\Select::LIMIT_OFFSET => true,
\Magento\Framework\DB\Select::COLUMNS => true,
];
/**
* @param \Magento\Framework\DB\Select $select
* @param \Magento\Framework\Api\CriteriaInterface $criteria
* @param \Magento\Framework\Model\ResourceModel\Db\AbstractDb $resource
* @param \Magento\Framework\Data\Collection\Db\FetchStrategyInterface $fetchStrategy
*/
public function __construct(
\Magento\Framework\DB\Select $select,
\Magento\Framework\Api\CriteriaInterface $criteria,
\Magento\Framework\Model\ResourceModel\Db\AbstractDb $resource,
\Magento\Framework\Data\Collection\Db\FetchStrategyInterface $fetchStrategy
) {
$this->select = $select;
$this->criteria = $criteria;
$this->resource = $resource;
$this->fetchStrategy = $fetchStrategy;
}
/**
* Retrieve source Criteria object
*
* @return \Magento\Framework\Api\CriteriaInterface
*/
public function getCriteria()
{
return $this->criteria;
}
/**
* Retrieve all ids for query
*
* @return array
*/
public function getAllIds()
{
$idsSelect = clone $this->getSelect();
$idsSelect->reset(\Magento\Framework\DB\Select::ORDER);
$idsSelect->reset(\Magento\Framework\DB\Select::LIMIT_COUNT);
$idsSelect->reset(\Magento\Framework\DB\Select::LIMIT_OFFSET);
$idsSelect->reset(\Magento\Framework\DB\Select::COLUMNS);
$idsSelect->columns($this->getResource()->getIdFieldName(), 'main_table');
return $this->getConnection()->fetchCol($idsSelect, $this->bindParams);
}
/**
* Add variable to bind list
*
* @param string $name
* @param mixed $value
* @return void
*/
public function addBindParam($name, $value)
{
$this->bindParams[$name] = $value;
}
/**
* Get collection size
*
* @return int
*/
public function getSize()
{
if ($this->totalRecords === null) {
$sql = $this->getSelectCountSql();
$this->totalRecords = $this->getConnection()->fetchOne($sql, $this->bindParams);
}
return (int)$this->totalRecords;
}
/**
* Get sql select string or object
*
* @param bool $stringMode
* @return string || Select
*/
public function getSelectSql($stringMode = false)
{
if ($stringMode) {
return $this->select->__toString();
}
return $this->select;
}
/**
* Reset Statement object
*
* @return void
*/
public function reset()
{
$this->fetchStmt = null;
$this->data = null;
}
/**
* Fetch all statement
*
* @return array
*/
public function fetchAll()
{
if ($this->data === null) {
$select = $this->getSelect();
$this->data = $this->fetchStrategy->fetchAll($select, $this->bindParams);
}
return $this->data;
}
/**
* Fetch statement
*
* @return mixed
*/
public function fetchItem()
{
if (null === $this->fetchStmt) {
$this->fetchStmt = $this->getConnection()->query($this->getSelect(), $this->bindParams);
}
$data = $this->fetchStmt->fetch();
if (!$data) {
$data = [];
}
return $data;
}
/**
* Get Identity Field Name
*
* @return string
*/
public function getIdFieldName()
{
return $this->getResource()->getIdFieldName();
}
/**
* Retrieve connection object
*
* @return \Magento\Framework\DB\Adapter\AdapterInterface
*/
public function getConnection()
{
return $this->getSelect()->getConnection();
}
/**
* Get resource instance
*
* @return \Magento\Framework\Model\ResourceModel\Db\AbstractDb
*/
public function getResource()
{
return $this->resource;
}
/**
* Add Select Part to skip from count query
*
* @param string $name
* @param bool $toSkip
* @return void
*/
public function addCountSqlSkipPart($name, $toSkip = true)
{
$this->countSqlSkipParts[$name] = $toSkip;
}
/**
* Get SQL for get record count
*
* @return Select
*/
protected function getSelectCountSql()
{
$countSelect = clone $this->getSelect();
foreach ($this->getCountSqlSkipParts() as $part => $toSkip) {
if ($toSkip) {
$countSelect->reset($part);
}
}
$countSelect->columns('COUNT(*)');
return $countSelect;
}
/**
* Returned count SQL skip parts
*
* @return array
*/
protected function getCountSqlSkipParts()
{
return $this->countSqlSkipParts;
}
/**
* Get \Magento\Framework\DB\Select object instance
*
* @return Select
*/
protected function getSelect()
{
return $this->select;
}
}