|
7 | 7 |
|
8 | 8 | use Magento\Framework\App\ResourceConnection;
|
9 | 9 | use Magento\Framework\DB\Ddl\Table;
|
| 10 | +use Magento\Framework\DB\Select; |
10 | 11 | use Magento\Framework\Search\Adapter\Mysql\Aggregation\Builder as AggregationBuilder;
|
11 | 12 | use Magento\Framework\Search\AdapterInterface;
|
12 | 13 | use Magento\Framework\Search\RequestInterface;
|
@@ -49,6 +50,16 @@ class Adapter implements AdapterInterface
|
49 | 50 | */
|
50 | 51 | private $temporaryStorageFactory;
|
51 | 52 |
|
| 53 | + /** |
| 54 | + * Query Select Parts to be skipped when prepare query for count |
| 55 | + * |
| 56 | + * @var array |
| 57 | + */ |
| 58 | + private $countSqlSkipParts = [ |
| 59 | + \Magento\Framework\DB\Select::LIMIT_COUNT => true, |
| 60 | + \Magento\Framework\DB\Select::LIMIT_OFFSET => true, |
| 61 | + ]; |
| 62 | + |
52 | 63 | /**
|
53 | 64 | * @param Mapper $mapper
|
54 | 65 | * @param ResponseFactory $responseFactory
|
@@ -86,7 +97,7 @@ public function query(RequestInterface $request)
|
86 | 97 | $response = [
|
87 | 98 | 'documents' => $documents,
|
88 | 99 | 'aggregations' => $aggregations,
|
89 |
| - 'total' => count($documents) |
| 100 | + 'total' => $this->getSize($query) |
90 | 101 | ];
|
91 | 102 | return $this->responseFactory->create($response);
|
92 | 103 | }
|
@@ -115,4 +126,39 @@ private function getConnection()
|
115 | 126 | {
|
116 | 127 | return $this->resource->getConnection();
|
117 | 128 | }
|
| 129 | + |
| 130 | + /** |
| 131 | + * Get rows size |
| 132 | + * |
| 133 | + * @param Select $query |
| 134 | + * @return int |
| 135 | + */ |
| 136 | + private function getSize(Select $query): int |
| 137 | + { |
| 138 | + $sql = $this->getSelectCountSql($query); |
| 139 | + $parentSelect = $this->getConnection()->select(); |
| 140 | + $parentSelect->from(['core_select' => $sql]); |
| 141 | + $parentSelect->reset(\Magento\Framework\DB\Select::COLUMNS); |
| 142 | + $parentSelect->columns('COUNT(*)'); |
| 143 | + $totalRecords = $this->getConnection()->fetchOne($parentSelect); |
| 144 | + |
| 145 | + return intval($totalRecords); |
| 146 | + } |
| 147 | + |
| 148 | + /** |
| 149 | + * Reset limit and offset |
| 150 | + * |
| 151 | + * @param Select $query |
| 152 | + * @return Select |
| 153 | + */ |
| 154 | + private function getSelectCountSql(Select $query): Select |
| 155 | + { |
| 156 | + foreach ($this->countSqlSkipParts as $part => $toSkip) { |
| 157 | + if ($toSkip) { |
| 158 | + $query->reset($part); |
| 159 | + } |
| 160 | + } |
| 161 | + |
| 162 | + return $query; |
| 163 | + } |
118 | 164 | }
|
0 commit comments