diff options
author | polo <ordipolo@gmx.fr> | 2024-08-13 23:45:21 +0200 |
---|---|---|
committer | polo <ordipolo@gmx.fr> | 2024-08-13 23:45:21 +0200 |
commit | bf6655a534a6775d30cafa67bd801276bda1d98d (patch) | |
tree | c6381e3f6c81c33eab72508f410b165ba05f7e9c /vendor/doctrine/dbal/src/Query/Expression | |
parent | 94d67a4b51f8e62e7d518cce26a526ae1ec48278 (diff) | |
download | AppliGestionPHP-bf6655a534a6775d30cafa67bd801276bda1d98d.zip |
VERSION 0.2 doctrine ORM et entités
Diffstat (limited to 'vendor/doctrine/dbal/src/Query/Expression')
-rw-r--r-- | vendor/doctrine/dbal/src/Query/Expression/CompositeExpression.php | 98 | ||||
-rw-r--r-- | vendor/doctrine/dbal/src/Query/Expression/ExpressionBuilder.php | 244 |
2 files changed, 342 insertions, 0 deletions
diff --git a/vendor/doctrine/dbal/src/Query/Expression/CompositeExpression.php b/vendor/doctrine/dbal/src/Query/Expression/CompositeExpression.php new file mode 100644 index 0000000..efe307a --- /dev/null +++ b/vendor/doctrine/dbal/src/Query/Expression/CompositeExpression.php | |||
@@ -0,0 +1,98 @@ | |||
1 | <?php | ||
2 | |||
3 | declare(strict_types=1); | ||
4 | |||
5 | namespace Doctrine\DBAL\Query\Expression; | ||
6 | |||
7 | use Countable; | ||
8 | |||
9 | use function array_merge; | ||
10 | use function array_values; | ||
11 | use function count; | ||
12 | use function implode; | ||
13 | |||
14 | /** | ||
15 | * Composite expression is responsible to build a group of similar expression. | ||
16 | * | ||
17 | * This class is immutable. | ||
18 | */ | ||
19 | class CompositeExpression implements Countable | ||
20 | { | ||
21 | /** | ||
22 | * Constant that represents an AND composite expression. | ||
23 | */ | ||
24 | final public const TYPE_AND = 'AND'; | ||
25 | |||
26 | /** | ||
27 | * Constant that represents an OR composite expression. | ||
28 | */ | ||
29 | final public const TYPE_OR = 'OR'; | ||
30 | |||
31 | /** | ||
32 | * Each expression part of the composite expression. | ||
33 | * | ||
34 | * @var array<int, self|string> | ||
35 | */ | ||
36 | private array $parts; | ||
37 | |||
38 | /** @internal Use the and() / or() factory methods. */ | ||
39 | public function __construct( | ||
40 | private readonly string $type, | ||
41 | self|string $part, | ||
42 | self|string ...$parts, | ||
43 | ) { | ||
44 | $this->parts = array_merge([$part], array_values($parts)); | ||
45 | } | ||
46 | |||
47 | public static function and(self|string $part, self|string ...$parts): self | ||
48 | { | ||
49 | return new self(self::TYPE_AND, $part, ...$parts); | ||
50 | } | ||
51 | |||
52 | public static function or(self|string $part, self|string ...$parts): self | ||
53 | { | ||
54 | return new self(self::TYPE_OR, $part, ...$parts); | ||
55 | } | ||
56 | |||
57 | /** | ||
58 | * Returns a new CompositeExpression with the given parts added. | ||
59 | */ | ||
60 | public function with(self|string $part, self|string ...$parts): self | ||
61 | { | ||
62 | $that = clone $this; | ||
63 | |||
64 | $that->parts = array_merge($that->parts, [$part], array_values($parts)); | ||
65 | |||
66 | return $that; | ||
67 | } | ||
68 | |||
69 | /** | ||
70 | * Retrieves the amount of expressions on composite expression. | ||
71 | * | ||
72 | * @psalm-return int<0, max> | ||
73 | */ | ||
74 | public function count(): int | ||
75 | { | ||
76 | return count($this->parts); | ||
77 | } | ||
78 | |||
79 | /** | ||
80 | * Retrieves the string representation of this composite expression. | ||
81 | */ | ||
82 | public function __toString(): string | ||
83 | { | ||
84 | if ($this->count() === 1) { | ||
85 | return (string) $this->parts[0]; | ||
86 | } | ||
87 | |||
88 | return '(' . implode(') ' . $this->type . ' (', $this->parts) . ')'; | ||
89 | } | ||
90 | |||
91 | /** | ||
92 | * Returns the type of this composite expression (AND/OR). | ||
93 | */ | ||
94 | public function getType(): string | ||
95 | { | ||
96 | return $this->type; | ||
97 | } | ||
98 | } | ||
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 | } | ||