SelectBuilder.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Analytics\ReportXml\DB;
  7. use Magento\Framework\App\ResourceConnection;
  8. use Magento\Framework\DB\Select;
  9. /**
  10. * Responsible for Select object creation, works as a builder. Returns Select as result;
  11. *
  12. * Used in SQL assemblers.
  13. */
  14. class SelectBuilder
  15. {
  16. /**
  17. * @var ResourceConnection
  18. */
  19. private $resourceConnection;
  20. /**
  21. * @var string
  22. */
  23. private $connectionName;
  24. /**
  25. * @var array
  26. */
  27. private $from;
  28. /**
  29. * @var array
  30. */
  31. private $group = [];
  32. /**
  33. * @var array
  34. */
  35. private $columns = [];
  36. /**
  37. * @var array
  38. */
  39. private $filters = [];
  40. /**
  41. * @var array
  42. */
  43. private $joins = [];
  44. /**
  45. * @var array
  46. */
  47. private $params = [];
  48. /**
  49. * @var array
  50. */
  51. private $having = [];
  52. /**
  53. * SelectBuilder constructor.
  54. *
  55. * @param ResourceConnection $resourceConnection
  56. */
  57. public function __construct(
  58. ResourceConnection $resourceConnection
  59. ) {
  60. $this->resourceConnection = $resourceConnection;
  61. }
  62. /**
  63. * Get join condition
  64. *
  65. * @return array
  66. */
  67. public function getJoins()
  68. {
  69. return $this->joins;
  70. }
  71. /**
  72. * Set joins conditions
  73. *
  74. * @param array $joins
  75. * @return $this
  76. */
  77. public function setJoins($joins)
  78. {
  79. $this->joins = $joins;
  80. return $this;
  81. }
  82. /**
  83. * Get connection name
  84. *
  85. * @return string
  86. */
  87. public function getConnectionName()
  88. {
  89. return $this->connectionName;
  90. }
  91. /**
  92. * Set connection name
  93. *
  94. * @param string $connectionName
  95. * @return $this
  96. */
  97. public function setConnectionName($connectionName)
  98. {
  99. $this->connectionName = $connectionName;
  100. return $this;
  101. }
  102. /**
  103. * Get columns
  104. *
  105. * @return array
  106. */
  107. public function getColumns()
  108. {
  109. return $this->columns;
  110. }
  111. /**
  112. * Set columns
  113. *
  114. * @param array $columns
  115. * @return $this
  116. */
  117. public function setColumns($columns)
  118. {
  119. $this->columns = $columns;
  120. return $this;
  121. }
  122. /**
  123. * Get filters
  124. *
  125. * @return array
  126. */
  127. public function getFilters()
  128. {
  129. return $this->filters;
  130. }
  131. /**
  132. * Set filters
  133. *
  134. * @param array $filters
  135. * @return $this
  136. */
  137. public function setFilters($filters)
  138. {
  139. $this->filters = $filters;
  140. return $this;
  141. }
  142. /**
  143. * Get from condition
  144. *
  145. * @return array
  146. */
  147. public function getFrom()
  148. {
  149. return $this->from;
  150. }
  151. /**
  152. * Set from condition
  153. *
  154. * @param array $from
  155. * @return $this
  156. */
  157. public function setFrom($from)
  158. {
  159. $this->from = $from;
  160. return $this;
  161. }
  162. /**
  163. * Process JOIN conditions
  164. *
  165. * @param Select $select
  166. * @param array $joinConfig
  167. * @return Select
  168. */
  169. private function processJoin(Select $select, $joinConfig)
  170. {
  171. switch ($joinConfig['link-type']) {
  172. case 'left':
  173. $select->joinLeft($joinConfig['table'], $joinConfig['condition'], []);
  174. break;
  175. case 'inner':
  176. $select->joinInner($joinConfig['table'], $joinConfig['condition'], []);
  177. break;
  178. case 'right':
  179. $select->joinRight($joinConfig['table'], $joinConfig['condition'], []);
  180. break;
  181. }
  182. return $select;
  183. }
  184. /**
  185. * Creates Select object
  186. *
  187. * @return Select
  188. */
  189. public function create()
  190. {
  191. $connection = $this->resourceConnection->getConnection($this->getConnectionName());
  192. $select = $connection->select();
  193. $select->from($this->getFrom(), []);
  194. $select->columns($this->getColumns());
  195. foreach ($this->getFilters() as $filter) {
  196. $select->where($filter);
  197. }
  198. foreach ($this->getJoins() as $joinConfig) {
  199. $select = $this->processJoin($select, $joinConfig);
  200. }
  201. if (!empty($this->getGroup())) {
  202. $select->group(implode(', ', $this->getGroup()));
  203. }
  204. return $select;
  205. }
  206. /**
  207. * Returns group
  208. *
  209. * @return array
  210. */
  211. public function getGroup()
  212. {
  213. return $this->group;
  214. }
  215. /**
  216. * Set group
  217. *
  218. * @param array $group
  219. * @return $this
  220. */
  221. public function setGroup($group)
  222. {
  223. $this->group = $group;
  224. return $this;
  225. }
  226. /**
  227. * Get parameters
  228. *
  229. * @return array
  230. */
  231. public function getParams()
  232. {
  233. return $this->params;
  234. }
  235. /**
  236. * Set parameters
  237. *
  238. * @param array $params
  239. * @return $this
  240. */
  241. public function setParams($params)
  242. {
  243. $this->params = $params;
  244. return $this;
  245. }
  246. /**
  247. * Get having condition
  248. *
  249. * @return array
  250. */
  251. public function getHaving()
  252. {
  253. return $this->having;
  254. }
  255. /**
  256. * Set having condition
  257. *
  258. * @param array $having
  259. * @return $this
  260. */
  261. public function setHaving($having)
  262. {
  263. $this->having = $having;
  264. return $this;
  265. }
  266. }