Query.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\DB;
  7. use Psr\Log\LoggerInterface as Logger;
  8. /**
  9. * Class Query
  10. */
  11. class Query implements QueryInterface
  12. {
  13. /**
  14. * Select object
  15. *
  16. * @var \Magento\Framework\DB\Select
  17. */
  18. protected $select;
  19. /**
  20. * @var \Magento\Framework\Api\CriteriaInterface
  21. */
  22. protected $criteria;
  23. /**
  24. * Resource instance
  25. *
  26. * @var \Magento\Framework\Model\ResourceModel\Db\AbstractDb
  27. */
  28. protected $resource;
  29. /**
  30. * Database's statement for fetch item one by one
  31. *
  32. * @var \Zend_Db_Statement_Pdo
  33. */
  34. protected $fetchStmt = null;
  35. /**
  36. * @var Logger
  37. */
  38. protected $logger;
  39. /**
  40. * @var \Magento\Framework\Data\Collection\Db\FetchStrategyInterface
  41. */
  42. private $fetchStrategy;
  43. /**
  44. * @var array
  45. */
  46. protected $bindParams = [];
  47. /**
  48. * @var int
  49. */
  50. protected $totalRecords;
  51. /**
  52. * @var mixed
  53. */
  54. protected $data;
  55. /**
  56. * Query Select Parts to be skipped when prepare query for count
  57. *
  58. * @var array
  59. */
  60. protected $countSqlSkipParts = [
  61. \Magento\Framework\DB\Select::ORDER => true,
  62. \Magento\Framework\DB\Select::LIMIT_COUNT => true,
  63. \Magento\Framework\DB\Select::LIMIT_OFFSET => true,
  64. \Magento\Framework\DB\Select::COLUMNS => true,
  65. ];
  66. /**
  67. * @param \Magento\Framework\DB\Select $select
  68. * @param \Magento\Framework\Api\CriteriaInterface $criteria
  69. * @param \Magento\Framework\Model\ResourceModel\Db\AbstractDb $resource
  70. * @param \Magento\Framework\Data\Collection\Db\FetchStrategyInterface $fetchStrategy
  71. */
  72. public function __construct(
  73. \Magento\Framework\DB\Select $select,
  74. \Magento\Framework\Api\CriteriaInterface $criteria,
  75. \Magento\Framework\Model\ResourceModel\Db\AbstractDb $resource,
  76. \Magento\Framework\Data\Collection\Db\FetchStrategyInterface $fetchStrategy
  77. ) {
  78. $this->select = $select;
  79. $this->criteria = $criteria;
  80. $this->resource = $resource;
  81. $this->fetchStrategy = $fetchStrategy;
  82. }
  83. /**
  84. * Retrieve source Criteria object
  85. *
  86. * @return \Magento\Framework\Api\CriteriaInterface
  87. */
  88. public function getCriteria()
  89. {
  90. return $this->criteria;
  91. }
  92. /**
  93. * Retrieve all ids for query
  94. *
  95. * @return array
  96. */
  97. public function getAllIds()
  98. {
  99. $idsSelect = clone $this->getSelect();
  100. $idsSelect->reset(\Magento\Framework\DB\Select::ORDER);
  101. $idsSelect->reset(\Magento\Framework\DB\Select::LIMIT_COUNT);
  102. $idsSelect->reset(\Magento\Framework\DB\Select::LIMIT_OFFSET);
  103. $idsSelect->reset(\Magento\Framework\DB\Select::COLUMNS);
  104. $idsSelect->columns($this->getResource()->getIdFieldName(), 'main_table');
  105. return $this->getConnection()->fetchCol($idsSelect, $this->bindParams);
  106. }
  107. /**
  108. * Add variable to bind list
  109. *
  110. * @param string $name
  111. * @param mixed $value
  112. * @return void
  113. */
  114. public function addBindParam($name, $value)
  115. {
  116. $this->bindParams[$name] = $value;
  117. }
  118. /**
  119. * Get collection size
  120. *
  121. * @return int
  122. */
  123. public function getSize()
  124. {
  125. if ($this->totalRecords === null) {
  126. $sql = $this->getSelectCountSql();
  127. $this->totalRecords = $this->getConnection()->fetchOne($sql, $this->bindParams);
  128. }
  129. return (int)$this->totalRecords;
  130. }
  131. /**
  132. * Get sql select string or object
  133. *
  134. * @param bool $stringMode
  135. * @return string || Select
  136. */
  137. public function getSelectSql($stringMode = false)
  138. {
  139. if ($stringMode) {
  140. return $this->select->__toString();
  141. }
  142. return $this->select;
  143. }
  144. /**
  145. * Reset Statement object
  146. *
  147. * @return void
  148. */
  149. public function reset()
  150. {
  151. $this->fetchStmt = null;
  152. $this->data = null;
  153. }
  154. /**
  155. * Fetch all statement
  156. *
  157. * @return array
  158. */
  159. public function fetchAll()
  160. {
  161. if ($this->data === null) {
  162. $select = $this->getSelect();
  163. $this->data = $this->fetchStrategy->fetchAll($select, $this->bindParams);
  164. }
  165. return $this->data;
  166. }
  167. /**
  168. * Fetch statement
  169. *
  170. * @return mixed
  171. */
  172. public function fetchItem()
  173. {
  174. if (null === $this->fetchStmt) {
  175. $this->fetchStmt = $this->getConnection()->query($this->getSelect(), $this->bindParams);
  176. }
  177. $data = $this->fetchStmt->fetch();
  178. if (!$data) {
  179. $data = [];
  180. }
  181. return $data;
  182. }
  183. /**
  184. * Get Identity Field Name
  185. *
  186. * @return string
  187. */
  188. public function getIdFieldName()
  189. {
  190. return $this->getResource()->getIdFieldName();
  191. }
  192. /**
  193. * Retrieve connection object
  194. *
  195. * @return \Magento\Framework\DB\Adapter\AdapterInterface
  196. */
  197. public function getConnection()
  198. {
  199. return $this->getSelect()->getConnection();
  200. }
  201. /**
  202. * Get resource instance
  203. *
  204. * @return \Magento\Framework\Model\ResourceModel\Db\AbstractDb
  205. */
  206. public function getResource()
  207. {
  208. return $this->resource;
  209. }
  210. /**
  211. * Add Select Part to skip from count query
  212. *
  213. * @param string $name
  214. * @param bool $toSkip
  215. * @return void
  216. */
  217. public function addCountSqlSkipPart($name, $toSkip = true)
  218. {
  219. $this->countSqlSkipParts[$name] = $toSkip;
  220. }
  221. /**
  222. * Get SQL for get record count
  223. *
  224. * @return Select
  225. */
  226. protected function getSelectCountSql()
  227. {
  228. $countSelect = clone $this->getSelect();
  229. foreach ($this->getCountSqlSkipParts() as $part => $toSkip) {
  230. if ($toSkip) {
  231. $countSelect->reset($part);
  232. }
  233. }
  234. $countSelect->columns('COUNT(*)');
  235. return $countSelect;
  236. }
  237. /**
  238. * Returned count SQL skip parts
  239. *
  240. * @return array
  241. */
  242. protected function getCountSqlSkipParts()
  243. {
  244. return $this->countSqlSkipParts;
  245. }
  246. /**
  247. * Get \Magento\Framework\DB\Select object instance
  248. *
  249. * @return Select
  250. */
  251. protected function getSelect()
  252. {
  253. return $this->select;
  254. }
  255. }