TYPO3 CMS  TYPO3_8-7
QueryHelper.php
Go to the documentation of this file.
1 <?php
2 declare(strict_types = 1);
4 
5 /*
6  * This file is part of the TYPO3 CMS project.
7  *
8  * It is free software; you can redistribute it and/or modify it under
9  * the terms of the GNU General Public License, either version 2
10  * of the License, or any later version.
11  *
12  * For the full copyright and license information, please read the
13  * LICENSE.txt file that was distributed with this source code.
14  *
15  * The TYPO3 project - inspiring people to share!
16  */
17 
20 
30 {
42  public static function parseOrderBy(string $input): array
43  {
44  $input = preg_replace('/^(?:ORDER[[:space:]]*BY[[:space:]]*)+/i', '', trim($input)) ?: '';
45  $orderExpressions = GeneralUtility::trimExplode(',', $input, true);
46 
47  return array_map(
48  function ($expression) {
49  list($fieldName, $order) = GeneralUtility::trimExplode(' ', $expression, true);
50 
51  return [$fieldName, $order];
52  },
53  $orderExpressions
54  );
55  }
56 
68  public static function parseTableList(string $input): array
69  {
70  $input = preg_replace('/^(?:FROM[[:space:]]+)+/i', '', trim($input)) ?: '';
71  $tableExpressions = GeneralUtility::trimExplode(',', $input, true);
72 
73  return array_map(
74  function ($expression) {
75  list($tableName, $as, $alias) = GeneralUtility::trimExplode(' ', $expression, true);
76 
77  if (!empty($as) && strtolower($as) === 'as' && !empty($alias)) {
78  return [$tableName, $alias];
79  }
80  if (!empty($as) && empty($alias)) {
81  return [$tableName, $as];
82  }
83  return [$tableName, null];
84  },
85  $tableExpressions
86  );
87  }
88 
98  public static function parseGroupBy(string $input): array
99  {
100  $input = preg_replace('/^(?:GROUP[[:space:]]*BY[[:space:]]*)+/i', '', trim($input)) ?: '';
101 
102  return GeneralUtility::trimExplode(',', $input, true);
103  }
104 
111  public static function parseJoin(string $input): array
112  {
113  $input = trim($input);
114  $quoteCharacter = ' ';
115  // Check if the tableName is quoted
116  if ($input[0] === '`' || $input[0] === '"') {
117  $quoteCharacter .= $input[0];
118  $input = substr($input, 1);
119  $tableName = strtok($input, $quoteCharacter);
120  } else {
121  $tableName = strtok($input, $quoteCharacter);
122  }
123 
124  $tableAlias = strtok($quoteCharacter);
125  if (strtolower($tableAlias) === 'as') {
126  $tableAlias = strtok($quoteCharacter);
127  // Skip the next token which must be ON
128  strtok(' ');
129  $joinCondition = strtok('');
130  } elseif (strtolower($tableAlias) === 'on') {
131  $tableAlias = null;
132  $joinCondition = strtok('');
133  } else {
134  // Skip the next token which must be ON
135  strtok(' ');
136  $joinCondition = strtok('');
137  }
138 
139  // Catch the edge case that the table name is unquoted and the
140  // table alias is actually quoted. This will not work in the case
141  // that the quoted table alias contains whitespace.
142  $firstCharacterOfTableAlias = $tableAlias[0] ?? null;
143  if ($firstCharacterOfTableAlias === '`' || $firstCharacterOfTableAlias === '"') {
144  $tableAlias = substr($tableAlias, 1, -1);
145  }
146 
147  $tableAlias = $tableAlias ?: $tableName;
148 
149  return ['tableName' => $tableName, 'tableAlias' => $tableAlias, 'joinCondition' => $joinCondition];
150  }
151 
161  public static function stripLogicalOperatorPrefix(string $constraint): string
162  {
163  return preg_replace('/^(?:(AND|OR)[[:space:]]*)+/i', '', trim($constraint)) ?: '';
164  }
165 
173  public static function getDateTimeFormats()
174  {
175  return [
176  'date' => [
177  'empty' => '0000-00-00',
178  'format' => 'Y-m-d'
179  ],
180  'datetime' => [
181  'empty' => '0000-00-00 00:00:00',
182  'format' => 'Y-m-d H:i:s'
183  ]
184  ];
185  }
186 
195  public static function quoteDatabaseIdentifiers(Connection $connection, string $sql): string
196  {
197  if (strpos($sql, '{#') !== false) {
198  $sql = preg_replace_callback(
199  '/{#(?P<identifier>[^}]+)}/',
200  function (array $matches) use ($connection) {
201  return $connection->quoteIdentifier($matches['identifier']);
202  },
203  $sql
204  );
205  }
206 
207  return $sql;
208  }
209 }
static trimExplode($delim, $string, $removeEmptyValues=false, $limit=0)
static stripLogicalOperatorPrefix(string $constraint)
static quoteDatabaseIdentifiers(Connection $connection, string $sql)