‪TYPO3CMS  10.4
QueryHelper.php
Go to the documentation of this file.
1 <?php
2 
3 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 
19 
22 
32 {
44  public static function ‪parseOrderBy(string $input): array
45  {
46  $input = preg_replace('/^(?:ORDER[[:space:]]*BY[[:space:]]*)+/i', '', trim($input)) ?: '';
47  $orderExpressions = ‪GeneralUtility::trimExplode(',', $input, true);
48 
49  return array_map(
50  function ($expression) {
51  $fieldNameOrderArray = ‪GeneralUtility::trimExplode(' ', $expression, true);
52  $fieldName = $fieldNameOrderArray[0] ?? null;
53  $order = $fieldNameOrderArray[1] ?? null;
54 
55  return [$fieldName, $order];
56  },
57  $orderExpressions
58  );
59  }
60 
72  public static function ‪parseTableList(string $input): array
73  {
74  $input = preg_replace('/^(?:FROM[[:space:]]+)+/i', '', trim($input)) ?: '';
75  $tableExpressions = ‪GeneralUtility::trimExplode(',', $input, true);
76 
77  return array_map(
78  function ($expression) {
79  [$tableName, $as, $alias] = array_pad(‪GeneralUtility::trimExplode(' ', $expression, true), 3, null);
80 
81  if (!empty($as) && strtolower($as) === 'as' && !empty($alias)) {
82  return [$tableName, $alias];
83  }
84  if (!empty($as) && empty($alias)) {
85  return [$tableName, $as];
86  }
87  return [$tableName, null];
88  },
89  $tableExpressions
90  );
91  }
92 
102  public static function ‪parseGroupBy(string $input): array
103  {
104  $input = preg_replace('/^(?:GROUP[[:space:]]*BY[[:space:]]*)+/i', '', trim($input)) ?: '';
105 
106  return ‪GeneralUtility::trimExplode(',', $input, true);
107  }
108 
115  public static function ‪parseJoin(string $input): array
116  {
117  $input = trim($input);
118  $quoteCharacter = ' ';
119  // Check if the tableName is quoted
120  if ($input[0] === '`' || $input[0] === '"') {
121  $quoteCharacter .= $input[0];
122  $input = substr($input, 1);
123  $tableName = strtok($input, $quoteCharacter);
124  } else {
125  $tableName = strtok($input, $quoteCharacter);
126  }
127 
128  $tableAlias = (string)strtok($quoteCharacter);
129  if (strtolower($tableAlias) === 'as') {
130  $tableAlias = (string)strtok($quoteCharacter);
131  // Skip the next token which must be ON
132  strtok(' ');
133  $joinCondition = strtok('');
134  } elseif (strtolower($tableAlias) === 'on') {
135  $tableAlias = null;
136  $joinCondition = strtok('');
137  } else {
138  // Skip the next token which must be ON
139  strtok(' ');
140  $joinCondition = strtok('');
141  }
142 
143  // Catch the edge case that the table name is unquoted and the
144  // table alias is actually quoted. This will not work in the case
145  // that the quoted table alias contains whitespace.
146  $firstCharacterOfTableAlias = $tableAlias[0] ?? null;
147  if ($firstCharacterOfTableAlias === '`' || $firstCharacterOfTableAlias === '"') {
148  $tableAlias = substr((string)$tableAlias, 1, -1);
149  }
150 
151  $tableAlias = $tableAlias ?: $tableName;
152 
153  return ['tableName' => $tableName, 'tableAlias' => $tableAlias, 'joinCondition' => $joinCondition];
154  }
155 
165  public static function ‪stripLogicalOperatorPrefix(string $constraint): string
166  {
167  return preg_replace('/^(?:(AND|OR)[[:space:]]*)+/i', '', trim($constraint)) ?: '';
168  }
169 
177  public static function ‪getDateTimeFormats()
178  {
179  return [
180  'date' => [
181  'empty' => '0000-00-00',
182  'format' => 'Y-m-d'
183  ],
184  'datetime' => [
185  'empty' => '0000-00-00 00:00:00',
186  'format' => 'Y-m-d H:i:s'
187  ],
188  'time' => [
189  'empty' => '00:00:00',
190  'format' => 'H:i:s'
191  ]
192  ];
193  }
194 
202  public static function ‪getDateTimeTypes()
203  {
204  return [
205  'date',
206  'datetime',
207  'time'
208  ];
209  }
210 
219  public static function ‪quoteDatabaseIdentifiers(‪Connection $connection, string $sql): string
220  {
221  if (strpos($sql, '{#') !== false) {
222  $sql = preg_replace_callback(
223  '/{#(?P<identifier>[^}]+)}/',
224  function (array $matches) use ($connection) {
225  return $connection->‪quoteIdentifier($matches['identifier']);
226  },
227  $sql
228  );
229  }
230 
231  return $sql;
232  }
233 }
‪TYPO3\CMS\Core\Database\Query\QueryHelper\parseOrderBy
‪static array array[] parseOrderBy(string $input)
Definition: QueryHelper.php:44
‪TYPO3\CMS\Core\Database\Query\QueryHelper\getDateTimeFormats
‪static array getDateTimeFormats()
Definition: QueryHelper.php:177
‪TYPO3\CMS\Core\Database\Query\QueryHelper\parseTableList
‪static array array[] parseTableList(string $input)
Definition: QueryHelper.php:72
‪TYPO3\CMS\Core\Database\Query\QueryHelper\parseJoin
‪static array parseJoin(string $input)
Definition: QueryHelper.php:115
‪TYPO3\CMS\Core\Database\Query\QueryHelper\parseGroupBy
‪static array string[] parseGroupBy(string $input)
Definition: QueryHelper.php:102
‪TYPO3\CMS\Core\Database\Connection\quoteIdentifier
‪string quoteIdentifier($identifier)
Definition: Connection.php:133
‪TYPO3\CMS\Core\Database\Query\QueryHelper\quoteDatabaseIdentifiers
‪static string quoteDatabaseIdentifiers(Connection $connection, string $sql)
Definition: QueryHelper.php:219
‪TYPO3\CMS\Core\Database\Query\QueryHelper
Definition: QueryHelper.php:32
‪TYPO3\CMS\Core\Database\Query\QueryHelper\getDateTimeTypes
‪static array getDateTimeTypes()
Definition: QueryHelper.php:202
‪TYPO3\CMS\Core\Utility\GeneralUtility\trimExplode
‪static string[] trimExplode($delim, $string, $removeEmptyValues=false, $limit=0)
Definition: GeneralUtility.php:1059
‪TYPO3\CMS\Core\Database\Connection
Definition: Connection.php:36
‪TYPO3\CMS\Core\Database\Query\QueryHelper\stripLogicalOperatorPrefix
‪static string stripLogicalOperatorPrefix(string $constraint)
Definition: QueryHelper.php:165
‪TYPO3\CMS\Core\Database\Query
Definition: BulkInsertQuery.php:18
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:46