Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Helper.php
Go to the documentation of this file.
1 <?php
9 namespace Magento\Framework\DB;
10 
11 class Helper extends \Magento\Framework\DB\Helper\AbstractHelper
12 {
20  protected function _prepareOrder(\Magento\Framework\DB\Select $select, $autoReset = false)
21  {
22  $selectOrders = $select->getPart(\Magento\Framework\DB\Select::ORDER);
23  if (!$selectOrders) {
24  return [];
25  }
26 
27  $orders = [];
28  foreach ($selectOrders as $term) {
29  if (is_array($term)) {
30  if (!is_numeric($term[0])) {
31  $orders[] = sprintf('%s %s', $this->getConnection()->quoteIdentifier($term[0], true), $term[1]);
32  }
33  } else {
34  if (!is_numeric($term)) {
35  $orders[] = $this->getConnection()->quoteIdentifier($term, true);
36  }
37  }
38  }
39 
40  if ($autoReset) {
41  $select->reset(\Magento\Framework\DB\Select::ORDER);
42  }
43 
44  return $orders;
45  }
46 
58  protected function _truncateAliasName($field, $reverse = false)
59  {
60  $string = $field;
61  if (!is_numeric($field) && (strpos($field, '.') !== false)) {
62  $size = strpos($field, '.');
63  if ($reverse) {
64  $string = substr($field, 0, $size);
65  } else {
66  $string = substr($field, $size + 1);
67  }
68  }
69 
70  return $string;
71  }
72 
80  protected function _prepareGroup(\Magento\Framework\DB\Select $select, $autoReset = false)
81  {
82  $selectGroups = $select->getPart(\Magento\Framework\DB\Select::GROUP);
83  if (!$selectGroups) {
84  return [];
85  }
86 
87  $groups = [];
88  foreach ($selectGroups as $term) {
89  $groups[] = $this->getConnection()->quoteIdentifier($term, true);
90  }
91 
92  if ($autoReset) {
93  $select->reset(\Magento\Framework\DB\Select::GROUP);
94  }
95 
96  return $groups;
97  }
98 
107  protected function _prepareHaving(\Magento\Framework\DB\Select $select, $autoReset = false)
108  {
109  $selectHavings = $select->getPart(\Magento\Framework\DB\Select::HAVING);
110  if (!$selectHavings) {
111  return [];
112  }
113 
114  $havings = [];
115  $columns = $select->getPart(\Magento\Framework\DB\Select::COLUMNS);
116  foreach ($columns as $columnEntry) {
117  $correlationName = (string)$columnEntry[1];
118  $column = $columnEntry[2];
119  foreach ($selectHavings as $having) {
123  if (strpos($having, $correlationName) !== false) {
124  if (is_string($column)) {
128  $havings[] = str_replace($correlationName, $column, $having);
129  } else {
130  throw new \Zend_Db_Exception(
131  sprintf("Can't prepare expression without column alias: '%s'", $correlationName)
132  );
133  }
134  }
135  }
136  }
137 
138  if ($autoReset) {
139  $select->reset(\Magento\Framework\DB\Select::HAVING);
140  }
141 
142  return $havings;
143  }
144 
153  protected function _assembleLimit($query, $limitCount, $limitOffset, $columnList = [])
154  {
155  if ($limitCount !== null) {
156  $limitCount = intval($limitCount);
157  if ($limitCount <= 0) {
158  //throw new \Exception("LIMIT argument count={$limitCount} is not valid");
159  }
160 
161  $limitOffset = intval($limitOffset);
162  if ($limitOffset < 0) {
163  //throw new \Exception("LIMIT argument offset={$limitOffset} is not valid");
164  }
165 
166  if ($limitOffset + $limitCount != $limitOffset + 1) {
167  $columns = [];
168  foreach ($columnList as $columnEntry) {
169  $columns[] = $columnEntry[2] ? $columnEntry[2] : $columnEntry[1];
170  }
171  $query = sprintf('%s LIMIT %s, %s', $query, $limitCount, $limitOffset);
172  }
173  }
174 
175  return $query;
176  }
177 
188  public function prepareColumnsList(\Magento\Framework\DB\Select $select, $groupByCondition = null)
189  {
190  if (!count($select->getPart(\Magento\Framework\DB\Select::FROM))) {
191  return $select->getPart(\Magento\Framework\DB\Select::COLUMNS);
192  }
193 
194  $columns = $select->getPart(\Magento\Framework\DB\Select::COLUMNS);
195  $tables = $select->getPart(\Magento\Framework\DB\Select::FROM);
196  $preparedColumns = [];
197 
198  foreach ($columns as $columnEntry) {
199  list($correlationName, $column, $alias) = $columnEntry;
200  if ($column instanceof \Zend_Db_Expr) {
201  if ($alias !== null) {
202  if (preg_match('/(^|[^a-zA-Z_])^(SELECT)?(SUM|MIN|MAX|AVG|COUNT)\s*\(/i', $column)) {
203  $column = new \Zend_Db_Expr($column);
204  }
205  $preparedColumns[strtoupper($alias)] = [null, $column, $alias];
206  } else {
207  throw new \Zend_Db_Exception("Can't prepare expression without alias");
208  }
209  } else {
210  if ($column == \Magento\Framework\DB\Select::SQL_WILDCARD) {
211  if ($tables[$correlationName]['tableName'] instanceof \Zend_Db_Expr) {
212  throw new \Zend_Db_Exception(
213  "Can't prepare expression when tableName is instance of \Zend_Db_Expr"
214  );
215  }
216  $tableColumns = $this->getConnection()->describeTable($tables[$correlationName]['tableName']);
217  foreach (array_keys($tableColumns) as $col) {
218  $preparedColumns[strtoupper($col)] = [$correlationName, $col, null];
219  }
220  } else {
221  $columnKey = $alias === null ? $column : $alias;
222  $preparedColumns[strtoupper($columnKey)] = [$correlationName, $column, $alias];
223  }
224  }
225  }
226 
227  return $preparedColumns;
228  }
229 
241  public function addGroupConcatColumn(
242  $select,
243  $fieldAlias,
244  $fields,
245  $groupConcatDelimiter = ',',
246  $fieldsDelimiter = '',
247  $additionalWhere = ''
248  ) {
249  if (is_array($fields)) {
250  $fieldExpr = $this->getConnection()->getConcatSql($fields, $fieldsDelimiter);
251  } else {
252  $fieldExpr = $fields;
253  }
254  if ($additionalWhere) {
255  $fieldExpr = $this->getConnection()->getCheckSql($additionalWhere, $fieldExpr, "''");
256  }
257  $separator = '';
258  if ($groupConcatDelimiter) {
259  $separator = sprintf(" SEPARATOR '%s'", $groupConcatDelimiter);
260  }
261  $select->columns([$fieldAlias => new \Zend_Db_Expr(sprintf('GROUP_CONCAT(%s%s)', $fieldExpr, $separator))]);
262  return $select;
263  }
264 
272  public function getDateDiff($startDate, $endDate)
273  {
274  $dateDiff = '(TO_DAYS(' . $endDate . ') - TO_DAYS(' . $startDate . '))';
275  return new \Zend_Db_Expr($dateDiff);
276  }
277 
289  public function addLikeEscape($value, $options = [])
290  {
292  return new \Zend_Db_Expr($this->getConnection()->quote($value));
293  }
294 }
const HAVING
Definition: Select.php:53
const ORDER
Definition: Select.php:54
prepareColumnsList(\Magento\Framework\DB\Select $select, $groupByCondition=null)
Definition: Helper.php:188
$fields
Definition: details.phtml:14
_prepareOrder(\Magento\Framework\DB\Select $select, $autoReset=false)
Definition: Helper.php:20
_truncateAliasName($field, $reverse=false)
Definition: Helper.php:58
addLikeEscape($value, $options=[])
Definition: Helper.php:289
_assembleLimit($query, $limitCount, $limitOffset, $columnList=[])
Definition: Helper.php:153
const FROM
Definition: Select.php:49
_prepareHaving(\Magento\Framework\DB\Select $select, $autoReset=false)
Definition: Helper.php:107
$columns
Definition: default.phtml:15
const SQL_WILDCARD
Definition: Select.php:66
$value
Definition: gender.phtml:16
const COLUMNS
Definition: Select.php:48
const GROUP
Definition: Select.php:52
addGroupConcatColumn( $select, $fieldAlias, $fields, $groupConcatDelimiter=',', $fieldsDelimiter='', $additionalWhere='')
Definition: Helper.php:241
if(!trim($html)) $alias
Definition: details.phtml:20
getDateDiff($startDate, $endDate)
Definition: Helper.php:272
_prepareGroup(\Magento\Framework\DB\Select $select, $autoReset=false)
Definition: Helper.php:80