[TASK] Remove ext:dbal from installation steps
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Classes / Database / Query / QueryHelper.php
1 <?php
2 declare (strict_types = 1);
3 namespace TYPO3\CMS\Core\Database\Query;
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
18 use TYPO3\CMS\Core\Utility\GeneralUtility;
19
20 /**
21 * Contains misc helper methods to build syntactically valid SQL queries.
22 * Most helper functions are required to deal with legacy data where the
23 * format of the input is not strict enough to reliably use the SQL parts
24 * in queries directly.
25 *
26 * @internal
27 */
28 class QueryHelper
29 {
30 /**
31 * Takes an input, possibly prefixed with ORDER BY, and explodes it into
32 * and array of arrays where each item consists of a fieldName and a order
33 * direction.
34 *
35 * Each of the resulting fieldName/direction pairs can be used passed into
36 * QueryBuilder::orderBy() so sort a query result set.
37 *
38 * @param string $input eg . "ORDER BY title, uid
39 * @return array|array[] Array of arrays containing fieldName/direction pairs
40 */
41 public static function parseOrderBy(string $input): array
42 {
43 $input = preg_replace('/^(?:ORDER[[:space:]]*BY[[:space:]]*)+/i', '', trim($input)) ?: '';
44 $orderExpressions = GeneralUtility::trimExplode(',', $input, true);
45
46 return array_map(
47 function ($expression) {
48 list($fieldName, $order) = GeneralUtility::trimExplode(' ', $expression, true);
49
50 return [$fieldName, $order];
51 },
52 $orderExpressions
53 );
54 }
55
56 /**
57 * Takes an input, possibly prefixed with FROM, and explodes it into
58 * and array of arrays where each item consists of a tableName and an
59 * optional alias name.
60 *
61 * Each of the resulting pairs can be used with QueryBuilder::from()
62 * to select from one or more tables.
63 *
64 * @param string $input eg . "FROM aTable, anotherTable AS b, aThirdTable c"
65 * @return array|array[] Array of arrays containing tableName/alias pairs
66 */
67 public static function parseTableList(string $input): array
68 {
69 $input = preg_replace('/^(?:FROM[[:space:]]+)+/i', '', trim($input)) ?: '';
70 $tableExpressions = GeneralUtility::trimExplode(',', $input, true);
71
72 return array_map(
73 function ($expression) {
74 list($tableName, $as, $alias) = GeneralUtility::trimExplode(' ', $expression, true);
75
76 if (!empty($as) && strtolower($as) === 'as' && !empty($alias)) {
77 return [$tableName, $alias];
78 } elseif (!empty($as) && empty($alias)) {
79 return [$tableName, $as];
80 } else {
81 return [$tableName, null];
82 }
83 },
84 $tableExpressions
85 );
86 }
87
88 /**
89 * Removes the prefix "GROUP BY" from the input string.
90 *
91 * This function should be used when you can't guarantee that the string
92 * that you want to use as a GROUP BY fragment is not prefixed.
93 *
94 * @param string $input eg. "GROUP BY title, uid
95 * @return array|string[] column names to group by
96 */
97 public static function parseGroupBy(string $input): array
98 {
99 $input = preg_replace('/^(?:GROUP[[:space:]]*BY[[:space:]]*)+/i', '', trim($input)) ?: '';
100
101 return GeneralUtility::trimExplode(',', $input, true);
102 }
103
104 /**
105 * Removes the prefixes AND/OR from the input string.
106 *
107 * This function should be used when you can't guarantee that the string
108 * that you want to use as a WHERE fragment is not prefixed.
109 *
110 * @param string $constraint The where part fragment with a possible leading AND or OR operator
111 * @return string The modified where part without leading operator
112 */
113 public static function stripLogicalOperatorPrefix(string $constraint): string
114 {
115 return preg_replace('/^(?:(AND|OR)[[:space:]]*)+/i', '', trim($constraint)) ?: '';
116 }
117
118 /**
119 * Returns the date and time formats compatible with the given database.
120 *
121 * This simple method should probably be deprecated and removed later.
122 *
123 * @return array
124 */
125 public static function getDateTimeFormats()
126 {
127 return [
128 'date' => [
129 'empty' => '0000-00-00',
130 'format' => 'Y-m-d'
131 ],
132 'datetime' => [
133 'empty' => '0000-00-00 00:00:00',
134 'format' => 'Y-m-d H:i:s'
135 ]
136 ];
137 }
138 }