123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304 |
- <?php
- /**
- * Copyright © Magento, Inc. All rights reserved.
- * See COPYING.txt for license details.
- */
- namespace Magento\Analytics\ReportXml\DB;
- use Magento\Framework\App\ResourceConnection;
- use Magento\Framework\DB\Select;
- /**
- * Responsible for Select object creation, works as a builder. Returns Select as result;
- *
- * Used in SQL assemblers.
- */
- class SelectBuilder
- {
- /**
- * @var ResourceConnection
- */
- private $resourceConnection;
- /**
- * @var string
- */
- private $connectionName;
- /**
- * @var array
- */
- private $from;
- /**
- * @var array
- */
- private $group = [];
- /**
- * @var array
- */
- private $columns = [];
- /**
- * @var array
- */
- private $filters = [];
- /**
- * @var array
- */
- private $joins = [];
- /**
- * @var array
- */
- private $params = [];
- /**
- * @var array
- */
- private $having = [];
- /**
- * SelectBuilder constructor.
- *
- * @param ResourceConnection $resourceConnection
- */
- public function __construct(
- ResourceConnection $resourceConnection
- ) {
- $this->resourceConnection = $resourceConnection;
- }
- /**
- * Get join condition
- *
- * @return array
- */
- public function getJoins()
- {
- return $this->joins;
- }
- /**
- * Set joins conditions
- *
- * @param array $joins
- * @return $this
- */
- public function setJoins($joins)
- {
- $this->joins = $joins;
- return $this;
- }
- /**
- * Get connection name
- *
- * @return string
- */
- public function getConnectionName()
- {
- return $this->connectionName;
- }
- /**
- * Set connection name
- *
- * @param string $connectionName
- * @return $this
- */
- public function setConnectionName($connectionName)
- {
- $this->connectionName = $connectionName;
- return $this;
- }
- /**
- * Get columns
- *
- * @return array
- */
- public function getColumns()
- {
- return $this->columns;
- }
- /**
- * Set columns
- *
- * @param array $columns
- * @return $this
- */
- public function setColumns($columns)
- {
- $this->columns = $columns;
- return $this;
- }
- /**
- * Get filters
- *
- * @return array
- */
- public function getFilters()
- {
- return $this->filters;
- }
- /**
- * Set filters
- *
- * @param array $filters
- * @return $this
- */
- public function setFilters($filters)
- {
- $this->filters = $filters;
- return $this;
- }
- /**
- * Get from condition
- *
- * @return array
- */
- public function getFrom()
- {
- return $this->from;
- }
- /**
- * Set from condition
- *
- * @param array $from
- * @return $this
- */
- public function setFrom($from)
- {
- $this->from = $from;
- return $this;
- }
- /**
- * Process JOIN conditions
- *
- * @param Select $select
- * @param array $joinConfig
- * @return Select
- */
- private function processJoin(Select $select, $joinConfig)
- {
- switch ($joinConfig['link-type']) {
- case 'left':
- $select->joinLeft($joinConfig['table'], $joinConfig['condition'], []);
- break;
- case 'inner':
- $select->joinInner($joinConfig['table'], $joinConfig['condition'], []);
- break;
- case 'right':
- $select->joinRight($joinConfig['table'], $joinConfig['condition'], []);
- break;
- }
- return $select;
- }
- /**
- * Creates Select object
- *
- * @return Select
- */
- public function create()
- {
- $connection = $this->resourceConnection->getConnection($this->getConnectionName());
- $select = $connection->select();
- $select->from($this->getFrom(), []);
- $select->columns($this->getColumns());
- foreach ($this->getFilters() as $filter) {
- $select->where($filter);
- }
- foreach ($this->getJoins() as $joinConfig) {
- $select = $this->processJoin($select, $joinConfig);
- }
- if (!empty($this->getGroup())) {
- $select->group(implode(', ', $this->getGroup()));
- }
- return $select;
- }
- /**
- * Returns group
- *
- * @return array
- */
- public function getGroup()
- {
- return $this->group;
- }
- /**
- * Set group
- *
- * @param array $group
- * @return $this
- */
- public function setGroup($group)
- {
- $this->group = $group;
- return $this;
- }
- /**
- * Get parameters
- *
- * @return array
- */
- public function getParams()
- {
- return $this->params;
- }
- /**
- * Set parameters
- *
- * @param array $params
- * @return $this
- */
- public function setParams($params)
- {
- $this->params = $params;
- return $this;
- }
- /**
- * Get having condition
- *
- * @return array
- */
- public function getHaving()
- {
- return $this->having;
- }
- /**
- * Set having condition
- *
- * @param array $having
- * @return $this
- */
- public function setHaving($having)
- {
- $this->having = $having;
- return $this;
- }
- }
|