|
1 | 1 | <?php |
2 | 2 | namespace Kir\MySQL\Databases; |
3 | 3 |
|
4 | | -use Exception; |
5 | | -use Kir\MySQL\Builder\Select; |
6 | | -use Kir\MySQL\Databases\MySQL\MySQLRunnableSelect; |
7 | | -use PDO; |
8 | | -use PDOException; |
9 | | -use RuntimeException; |
10 | | -use Throwable; |
11 | | -use UnexpectedValueException; |
12 | 4 | use Kir\MySQL\Builder; |
| 5 | +use Kir\MySQL\Builder\DBExpr; |
13 | 6 | use Kir\MySQL\Builder\QueryStatement; |
| 7 | +use Kir\MySQL\Builder\Select; |
14 | 8 | use Kir\MySQL\Database; |
15 | 9 | use Kir\MySQL\Databases\MySQL\MySQLExceptionInterpreter; |
| 10 | +use Kir\MySQL\Databases\MySQL\MySQLExpressionQuoter; |
| 11 | +use Kir\MySQL\Databases\MySQL\MySQLFieldQuoter; |
| 12 | +use Kir\MySQL\Databases\MySQL\MySQLRunnableSelect; |
| 13 | +use Kir\MySQL\Databases\MySQL\MySQLUUIDGenerator; |
| 14 | +use Kir\MySQL\Databases\MySQL\MySQLValueQuoter; |
16 | 15 | use Kir\MySQL\QueryLogger\QueryLoggers; |
17 | 16 | use Kir\MySQL\Tools\AliasRegistry; |
18 | 17 | use Kir\MySQL\Tools\VirtualTables; |
| 18 | +use PDO; |
| 19 | +use PDOException; |
| 20 | +use RuntimeException; |
| 21 | +use Throwable; |
| 22 | +use phpDocumentor\Reflection\Types\Scalar; |
19 | 23 |
|
20 | 24 | /** |
21 | 25 | */ |
@@ -150,63 +154,27 @@ public function getTableFields(string $table): array { |
150 | 154 |
|
151 | 155 | /** |
152 | 156 | * @param string $expression |
153 | | - * @param array<int, null|int|float|string|array<int, string>|Builder\DBExpr|Builder\Select> $arguments |
| 157 | + * @param array<int, null|scalar|array<int, string>|DBExpr|Select> $arguments |
154 | 158 | * @return string |
155 | 159 | */ |
156 | 160 | public function quoteExpression(string $expression, array $arguments = []): string { |
157 | | - $index = -1; |
158 | | - $func = function () use ($arguments, &$index) { |
159 | | - $index++; |
160 | | - if(array_key_exists($index, $arguments)) { |
161 | | - $argument = $arguments[$index]; |
162 | | - $value = $this->quote($argument); |
163 | | - } elseif(count($arguments) > 0) { |
164 | | - $args = $arguments; |
165 | | - $value = array_pop($args); |
166 | | - $value = $this->quote($value); |
167 | | - } else { |
168 | | - $value = 'NULL'; |
169 | | - } |
170 | | - return $value; |
171 | | - }; |
172 | | - $result = preg_replace_callback('/(\\?)/', $func, $expression); |
173 | | - return (string) $result; |
| 161 | + return MySQLExpressionQuoter::quoteExpression($this->pdo, $expression, $arguments); |
174 | 162 | } |
175 | 163 |
|
176 | 164 | /** |
177 | | - * @param null|int|float|string|array<int, string>|Builder\DBExpr|Select $value |
| 165 | + * @param null|scalar|array<int, string>|DBExpr|Select $value |
178 | 166 | * @return string |
179 | 167 | */ |
180 | 168 | public function quote($value): string { |
181 | | - if(is_null($value)) { |
182 | | - $result = 'NULL'; |
183 | | - } elseif($value instanceof Builder\DBExpr) { |
184 | | - $result = $value->getExpression(); |
185 | | - } elseif($value instanceof Builder\Select) { |
186 | | - $result = sprintf('(%s)', (string) $value); |
187 | | - } elseif(is_array($value)) { |
188 | | - $result = implode(', ', array_map(function ($value) { return $this->quote($value); }, $value)); |
189 | | - } elseif(is_int($value) || is_float($value)) { |
190 | | - $result = (string) $value; |
191 | | - } else { |
192 | | - $result = $this->pdo->quote($value); |
193 | | - } |
194 | | - return $result; |
| 169 | + return MySQLValueQuoter::quote($this->pdo, $value); |
195 | 170 | } |
196 | 171 |
|
197 | 172 | /** |
198 | 173 | * @param string $field |
199 | 174 | * @return string |
200 | 175 | */ |
201 | 176 | public function quoteField(string $field): string { |
202 | | - if (is_numeric($field) || !is_string($field)) { |
203 | | - throw new UnexpectedValueException('Field name is invalid'); |
204 | | - } |
205 | | - if(strpos($field, '`') !== false) { |
206 | | - return $field; |
207 | | - } |
208 | | - $parts = explode('.', $field); |
209 | | - return '`'.implode('`.`', $parts).'`'; |
| 177 | + return MySQLFieldQuoter::quoteField($field); |
210 | 178 | } |
211 | 179 |
|
212 | 180 | /** |
@@ -307,7 +275,7 @@ public function dryRun(callable $callback) { |
307 | 275 | $this->transactionRollback(); |
308 | 276 | } |
309 | 277 | } else { |
310 | | - $uniqueId = $this->genUniqueId(); |
| 278 | + $uniqueId = MySQLUUIDGenerator::genUUIDv4(); |
311 | 279 | $this->exec("SAVEPOINT {$uniqueId}"); |
312 | 280 | try { |
313 | 281 | return $callback($this); |
@@ -337,7 +305,7 @@ public function transaction(callable $callback) { |
337 | 305 | throw $e; |
338 | 306 | } |
339 | 307 | } |
340 | | - $uniqueId = $this->genUniqueId(); |
| 308 | + $uniqueId = MySQLUUIDGenerator::genUUIDv4(); |
341 | 309 | $this->exec("SAVEPOINT {$uniqueId}"); |
342 | 310 | try { |
343 | 311 | $result = $callback($this); |
@@ -394,26 +362,4 @@ private function exceptionHandler(callable $fn) { |
394 | 362 | } |
395 | 363 | return null; |
396 | 364 | } |
397 | | - |
398 | | - /** |
399 | | - * @return string |
400 | | - */ |
401 | | - private function genUniqueId(): string { |
402 | | - // Generate a unique id from a former random-uuid-generator |
403 | | - try { |
404 | | - return sprintf('ID%04x%04x%04x%04x%04x%04x%04x%04x', |
405 | | - random_int(0, 0xffff), |
406 | | - random_int(0, 0xffff), |
407 | | - random_int(0, 0xffff), |
408 | | - random_int(0, 0x0fff) | 0x4000, |
409 | | - random_int(0, 0x3fff) | 0x8000, |
410 | | - random_int(0, 0xffff), |
411 | | - random_int(0, 0xffff), |
412 | | - random_int(0, 0xffff) |
413 | | - ); |
414 | | - } catch (Exception $e) { |
415 | | - // Should not throw an excepion under normal conditions |
416 | | - throw new RuntimeException($e->getMessage(), $e->getCode(), $e); |
417 | | - } |
418 | | - } |
419 | 365 | } |
0 commit comments