diff options
Diffstat (limited to 'vendor/doctrine/dbal/src/Query/Expression/ExpressionBuilder.php')
-rw-r--r-- | vendor/doctrine/dbal/src/Query/Expression/ExpressionBuilder.php | 244 |
1 files changed, 244 insertions, 0 deletions
diff --git a/vendor/doctrine/dbal/src/Query/Expression/ExpressionBuilder.php b/vendor/doctrine/dbal/src/Query/Expression/ExpressionBuilder.php new file mode 100644 index 0000000..14942b2 --- /dev/null +++ b/vendor/doctrine/dbal/src/Query/Expression/ExpressionBuilder.php | |||
@@ -0,0 +1,244 @@ | |||
1 | <?php | ||
2 | |||
3 | declare(strict_types=1); | ||
4 | |||
5 | namespace Doctrine\DBAL\Query\Expression; | ||
6 | |||
7 | use Doctrine\DBAL\Connection; | ||
8 | |||
9 | use function implode; | ||
10 | use function sprintf; | ||
11 | |||
12 | /** | ||
13 | * ExpressionBuilder class is responsible to dynamically create SQL query parts. | ||
14 | */ | ||
15 | class ExpressionBuilder | ||
16 | { | ||
17 | final public const EQ = '='; | ||
18 | final public const NEQ = '<>'; | ||
19 | final public const LT = '<'; | ||
20 | final public const LTE = '<='; | ||
21 | final public const GT = '>'; | ||
22 | final public const GTE = '>='; | ||
23 | |||
24 | /** | ||
25 | * Initializes a new <tt>ExpressionBuilder</tt>. | ||
26 | * | ||
27 | * @param Connection $connection The DBAL Connection. | ||
28 | */ | ||
29 | public function __construct(private readonly Connection $connection) | ||
30 | { | ||
31 | } | ||
32 | |||
33 | /** | ||
34 | * Creates a conjunction of the given expressions. | ||
35 | */ | ||
36 | public function and( | ||
37 | string|CompositeExpression $expression, | ||
38 | string|CompositeExpression ...$expressions, | ||
39 | ): CompositeExpression { | ||
40 | return CompositeExpression::and($expression, ...$expressions); | ||
41 | } | ||
42 | |||
43 | /** | ||
44 | * Creates a disjunction of the given expressions. | ||
45 | */ | ||
46 | public function or( | ||
47 | string|CompositeExpression $expression, | ||
48 | string|CompositeExpression ...$expressions, | ||
49 | ): CompositeExpression { | ||
50 | return CompositeExpression::or($expression, ...$expressions); | ||
51 | } | ||
52 | |||
53 | /** | ||
54 | * Creates a comparison expression. | ||
55 | * | ||
56 | * @param string $x The left expression. | ||
57 | * @param string $operator The comparison operator. | ||
58 | * @param string $y The right expression. | ||
59 | */ | ||
60 | public function comparison(string $x, string $operator, string $y): string | ||
61 | { | ||
62 | return $x . ' ' . $operator . ' ' . $y; | ||
63 | } | ||
64 | |||
65 | /** | ||
66 | * Creates an equality comparison expression with the given arguments. | ||
67 | * | ||
68 | * First argument is considered the left expression and the second is the right expression. | ||
69 | * When converted to string, it will generated a <left expr> = <right expr>. Example: | ||
70 | * | ||
71 | * [php] | ||
72 | * // u.id = ? | ||
73 | * $expr->eq('u.id', '?'); | ||
74 | * | ||
75 | * @param string $x The left expression. | ||
76 | * @param string $y The right expression. | ||
77 | */ | ||
78 | public function eq(string $x, string $y): string | ||
79 | { | ||
80 | return $this->comparison($x, self::EQ, $y); | ||
81 | } | ||
82 | |||
83 | /** | ||
84 | * Creates a non equality comparison expression with the given arguments. | ||
85 | * First argument is considered the left expression and the second is the right expression. | ||
86 | * When converted to string, it will generated a <left expr> <> <right expr>. Example: | ||
87 | * | ||
88 | * [php] | ||
89 | * // u.id <> 1 | ||
90 | * $q->where($q->expr()->neq('u.id', '1')); | ||
91 | * | ||
92 | * @param string $x The left expression. | ||
93 | * @param string $y The right expression. | ||
94 | */ | ||
95 | public function neq(string $x, string $y): string | ||
96 | { | ||
97 | return $this->comparison($x, self::NEQ, $y); | ||
98 | } | ||
99 | |||
100 | /** | ||
101 | * Creates a lower-than comparison expression with the given arguments. | ||
102 | * First argument is considered the left expression and the second is the right expression. | ||
103 | * When converted to string, it will generated a <left expr> < <right expr>. Example: | ||
104 | * | ||
105 | * [php] | ||
106 | * // u.id < ? | ||
107 | * $q->where($q->expr()->lt('u.id', '?')); | ||
108 | * | ||
109 | * @param string $x The left expression. | ||
110 | * @param string $y The right expression. | ||
111 | */ | ||
112 | public function lt(string $x, string $y): string | ||
113 | { | ||
114 | return $this->comparison($x, self::LT, $y); | ||
115 | } | ||
116 | |||
117 | /** | ||
118 | * Creates a lower-than-equal comparison expression with the given arguments. | ||
119 | * First argument is considered the left expression and the second is the right expression. | ||
120 | * When converted to string, it will generated a <left expr> <= <right expr>. Example: | ||
121 | * | ||
122 | * [php] | ||
123 | * // u.id <= ? | ||
124 | * $q->where($q->expr()->lte('u.id', '?')); | ||
125 | * | ||
126 | * @param string $x The left expression. | ||
127 | * @param string $y The right expression. | ||
128 | */ | ||
129 | public function lte(string $x, string $y): string | ||
130 | { | ||
131 | return $this->comparison($x, self::LTE, $y); | ||
132 | } | ||
133 | |||
134 | /** | ||
135 | * Creates a greater-than comparison expression with the given arguments. | ||
136 | * First argument is considered the left expression and the second is the right expression. | ||
137 | * When converted to string, it will generated a <left expr> > <right expr>. Example: | ||
138 | * | ||
139 | * [php] | ||
140 | * // u.id > ? | ||
141 | * $q->where($q->expr()->gt('u.id', '?')); | ||
142 | * | ||
143 | * @param string $x The left expression. | ||
144 | * @param string $y The right expression. | ||
145 | */ | ||
146 | public function gt(string $x, string $y): string | ||
147 | { | ||
148 | return $this->comparison($x, self::GT, $y); | ||
149 | } | ||
150 | |||
151 | /** | ||
152 | * Creates a greater-than-equal comparison expression with the given arguments. | ||
153 | * First argument is considered the left expression and the second is the right expression. | ||
154 | * When converted to string, it will generated a <left expr> >= <right expr>. Example: | ||
155 | * | ||
156 | * [php] | ||
157 | * // u.id >= ? | ||
158 | * $q->where($q->expr()->gte('u.id', '?')); | ||
159 | * | ||
160 | * @param string $x The left expression. | ||
161 | * @param string $y The right expression. | ||
162 | */ | ||
163 | public function gte(string $x, string $y): string | ||
164 | { | ||
165 | return $this->comparison($x, self::GTE, $y); | ||
166 | } | ||
167 | |||
168 | /** | ||
169 | * Creates an IS NULL expression with the given arguments. | ||
170 | * | ||
171 | * @param string $x The expression to be restricted by IS NULL. | ||
172 | */ | ||
173 | public function isNull(string $x): string | ||
174 | { | ||
175 | return $x . ' IS NULL'; | ||
176 | } | ||
177 | |||
178 | /** | ||
179 | * Creates an IS NOT NULL expression with the given arguments. | ||
180 | * | ||
181 | * @param string $x The expression to be restricted by IS NOT NULL. | ||
182 | */ | ||
183 | public function isNotNull(string $x): string | ||
184 | { | ||
185 | return $x . ' IS NOT NULL'; | ||
186 | } | ||
187 | |||
188 | /** | ||
189 | * Creates a LIKE comparison expression. | ||
190 | * | ||
191 | * @param string $expression The expression to be inspected by the LIKE comparison | ||
192 | * @param string $pattern The pattern to compare against | ||
193 | */ | ||
194 | public function like(string $expression, string $pattern, ?string $escapeChar = null): string | ||
195 | { | ||
196 | return $this->comparison($expression, 'LIKE', $pattern) . | ||
197 | ($escapeChar !== null ? sprintf(' ESCAPE %s', $escapeChar) : ''); | ||
198 | } | ||
199 | |||
200 | /** | ||
201 | * Creates a NOT LIKE comparison expression | ||
202 | * | ||
203 | * @param string $expression The expression to be inspected by the NOT LIKE comparison | ||
204 | * @param string $pattern The pattern to compare against | ||
205 | */ | ||
206 | public function notLike(string $expression, string $pattern, ?string $escapeChar = null): string | ||
207 | { | ||
208 | return $this->comparison($expression, 'NOT LIKE', $pattern) . | ||
209 | ($escapeChar !== null ? sprintf(' ESCAPE %s', $escapeChar) : ''); | ||
210 | } | ||
211 | |||
212 | /** | ||
213 | * Creates an IN () comparison expression with the given arguments. | ||
214 | * | ||
215 | * @param string $x The SQL expression to be matched against the set. | ||
216 | * @param string|string[] $y The SQL expression or an array of SQL expressions representing the set. | ||
217 | */ | ||
218 | public function in(string $x, string|array $y): string | ||
219 | { | ||
220 | return $this->comparison($x, 'IN', '(' . implode(', ', (array) $y) . ')'); | ||
221 | } | ||
222 | |||
223 | /** | ||
224 | * Creates a NOT IN () comparison expression with the given arguments. | ||
225 | * | ||
226 | * @param string $x The SQL expression to be matched against the set. | ||
227 | * @param string|string[] $y The SQL expression or an array of SQL expressions representing the set. | ||
228 | */ | ||
229 | public function notIn(string $x, string|array $y): string | ||
230 | { | ||
231 | return $this->comparison($x, 'NOT IN', '(' . implode(', ', (array) $y) . ')'); | ||
232 | } | ||
233 | |||
234 | /** | ||
235 | * Creates an SQL literal expression from the string. | ||
236 | * | ||
237 | * The usage of this method is discouraged. Use prepared statements | ||
238 | * or {@see AbstractPlatform::quoteStringLiteral()} instead. | ||
239 | */ | ||
240 | public function literal(string $input): string | ||
241 | { | ||
242 | return $this->connection->quote($input); | ||
243 | } | ||
244 | } | ||