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