|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Tpetry\PostgresqlEnhanced\Backports; |
| 6 | + |
| 7 | +use Closure; |
| 8 | +use RuntimeException; |
| 9 | +use Tpetry\PostgresqlEnhanced\Query\Grammar as QueryGrammar; |
| 10 | +use Tpetry\PostgresqlEnhanced\Schema\Grammars\Grammar as SchemaGrammar; |
| 11 | + |
| 12 | +/** |
| 13 | + * To support some features these commits from laravel needed to be backported for older versions: |
| 14 | + * - [8.x] Adds new RefreshDatabaseLazily testing trait (https://github.com/laravel/framework/commit/3d1ead403e05ee0b9aa93a6ff720704970aec9c8). |
| 15 | + * - [10.x] Escaping functionality within the Grammar (https://github.com/laravel/framework/commit/e953137280cdf6e0fe3c3e4c49d7209ad86c92c0). |
| 16 | + */ |
| 17 | +trait ConnectionBackport |
| 18 | +{ |
| 19 | + /** |
| 20 | + * All of the callbacks that should be invoked before a query is executed. |
| 21 | + * |
| 22 | + * @var Closure[] |
| 23 | + */ |
| 24 | + protected $beforeExecutingCallbacks = []; |
| 25 | + |
| 26 | + /** |
| 27 | + * Register a hook to be run just before a database query is executed. |
| 28 | + */ |
| 29 | + public function beforeExecuting(Closure $callback): static |
| 30 | + { |
| 31 | + $this->beforeExecutingCallbacks[] = $callback; |
| 32 | + |
| 33 | + return $this; |
| 34 | + } |
| 35 | + |
| 36 | + /** |
| 37 | + * Escape a value for safe SQL embedding. |
| 38 | + * |
| 39 | + * @param string|float|int|bool|null $value |
| 40 | + * @param bool $binary |
| 41 | + */ |
| 42 | + public function escape($value, $binary = false): string |
| 43 | + { |
| 44 | + if (null === $value) { |
| 45 | + return 'null'; |
| 46 | + } elseif ($binary) { |
| 47 | + return $this->escapeBinary($value); |
| 48 | + } elseif (\is_int($value) || \is_float($value)) { |
| 49 | + return (string) $value; |
| 50 | + } elseif (\is_bool($value)) { |
| 51 | + return $this->escapeBool($value); |
| 52 | + } else { |
| 53 | + if (str_contains($value, "\00")) { |
| 54 | + throw new RuntimeException('Strings with null bytes cannot be escaped. Use the binary escape option.'); |
| 55 | + } |
| 56 | + if (false === preg_match('//u', $value)) { |
| 57 | + throw new RuntimeException('Strings with invalid UTF-8 byte sequences cannot be escaped.'); |
| 58 | + } |
| 59 | + |
| 60 | + return $this->escapeString($value); |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + /** |
| 65 | + * Escape a binary value for safe SQL embedding. |
| 66 | + * |
| 67 | + * @param string $value |
| 68 | + */ |
| 69 | + protected function escapeBinary($value): string |
| 70 | + { |
| 71 | + $hex = bin2hex($value); |
| 72 | + |
| 73 | + return "'\x{$hex}'::bytea"; |
| 74 | + } |
| 75 | + |
| 76 | + /** |
| 77 | + * Escape a boolean value for safe SQL embedding. |
| 78 | + * |
| 79 | + * @param bool $value |
| 80 | + */ |
| 81 | + protected function escapeBool($value): string |
| 82 | + { |
| 83 | + return $value ? 'true' : 'false'; |
| 84 | + } |
| 85 | + |
| 86 | + /** |
| 87 | + * Escape a string value for safe SQL embedding. |
| 88 | + * |
| 89 | + * @param string $value |
| 90 | + */ |
| 91 | + protected function escapeString($value): string |
| 92 | + { |
| 93 | + return $this->getPdo()->quote($value); |
| 94 | + } |
| 95 | + |
| 96 | + /** |
| 97 | + * Get the default query grammar instance. |
| 98 | + */ |
| 99 | + protected function getDefaultQueryGrammar(): QueryGrammar |
| 100 | + { |
| 101 | + ($grammar = new QueryGrammar())->setConnection($this); |
| 102 | + |
| 103 | + return $this->withTablePrefix($grammar); |
| 104 | + } |
| 105 | + |
| 106 | + /** |
| 107 | + * Get the default schema grammar instance. |
| 108 | + */ |
| 109 | + protected function getDefaultSchemaGrammar(): SchemaGrammar |
| 110 | + { |
| 111 | + ($grammar = new SchemaGrammar())->setConnection($this); |
| 112 | + |
| 113 | + return $this->withTablePrefix($grammar); |
| 114 | + } |
| 115 | + |
| 116 | + /** |
| 117 | + * Run a SQL statement and log its execution context. |
| 118 | + * |
| 119 | + * @param string $query |
| 120 | + * @param array $bindings |
| 121 | + */ |
| 122 | + protected function run($query, $bindings, Closure $callback): mixed |
| 123 | + { |
| 124 | + foreach ($this->beforeExecutingCallbacks as $beforeExecutingCallback) { |
| 125 | + $beforeExecutingCallback($query, $bindings, $this); |
| 126 | + } |
| 127 | + |
| 128 | + return parent::run($query, $bindings, $callback); |
| 129 | + } |
| 130 | +} |
0 commit comments