diff options
Diffstat (limited to 'vendor/doctrine/dbal/src/Driver/PgSQL/Statement.php')
| -rw-r--r-- | vendor/doctrine/dbal/src/Driver/PgSQL/Statement.php | 91 |
1 files changed, 91 insertions, 0 deletions
diff --git a/vendor/doctrine/dbal/src/Driver/PgSQL/Statement.php b/vendor/doctrine/dbal/src/Driver/PgSQL/Statement.php new file mode 100644 index 0000000..b48ab5d --- /dev/null +++ b/vendor/doctrine/dbal/src/Driver/PgSQL/Statement.php | |||
| @@ -0,0 +1,91 @@ | |||
| 1 | <?php | ||
| 2 | |||
| 3 | declare(strict_types=1); | ||
| 4 | |||
| 5 | namespace Doctrine\DBAL\Driver\PgSQL; | ||
| 6 | |||
| 7 | use Doctrine\DBAL\Driver\PgSQL\Exception\UnknownParameter; | ||
| 8 | use Doctrine\DBAL\Driver\Statement as StatementInterface; | ||
| 9 | use Doctrine\DBAL\ParameterType; | ||
| 10 | use PgSql\Connection as PgSqlConnection; | ||
| 11 | |||
| 12 | use function assert; | ||
| 13 | use function is_resource; | ||
| 14 | use function ksort; | ||
| 15 | use function pg_escape_bytea; | ||
| 16 | use function pg_escape_identifier; | ||
| 17 | use function pg_get_result; | ||
| 18 | use function pg_last_error; | ||
| 19 | use function pg_query; | ||
| 20 | use function pg_result_error; | ||
| 21 | use function pg_send_execute; | ||
| 22 | use function stream_get_contents; | ||
| 23 | |||
| 24 | final class Statement implements StatementInterface | ||
| 25 | { | ||
| 26 | /** @var array<int, mixed> */ | ||
| 27 | private array $parameters = []; | ||
| 28 | |||
| 29 | /** @psalm-var array<int, ParameterType> */ | ||
| 30 | private array $parameterTypes = []; | ||
| 31 | |||
| 32 | /** @param array<array-key, int> $parameterMap */ | ||
| 33 | public function __construct( | ||
| 34 | private readonly PgSqlConnection $connection, | ||
| 35 | private readonly string $name, | ||
| 36 | private readonly array $parameterMap, | ||
| 37 | ) { | ||
| 38 | } | ||
| 39 | |||
| 40 | public function __destruct() | ||
| 41 | { | ||
| 42 | if (! isset($this->connection)) { | ||
| 43 | return; | ||
| 44 | } | ||
| 45 | |||
| 46 | @pg_query( | ||
| 47 | $this->connection, | ||
| 48 | 'DEALLOCATE ' . pg_escape_identifier($this->connection, $this->name), | ||
| 49 | ); | ||
| 50 | } | ||
| 51 | |||
| 52 | /** {@inheritDoc} */ | ||
| 53 | public function bindValue(int|string $param, mixed $value, ParameterType $type = ParameterType::STRING): void | ||
| 54 | { | ||
| 55 | if (! isset($this->parameterMap[$param])) { | ||
| 56 | throw UnknownParameter::new((string) $param); | ||
| 57 | } | ||
| 58 | |||
| 59 | $this->parameters[$this->parameterMap[$param]] = $value; | ||
| 60 | $this->parameterTypes[$this->parameterMap[$param]] = $type; | ||
| 61 | } | ||
| 62 | |||
| 63 | /** {@inheritDoc} */ | ||
| 64 | public function execute(): Result | ||
| 65 | { | ||
| 66 | ksort($this->parameters); | ||
| 67 | |||
| 68 | $escapedParameters = []; | ||
| 69 | foreach ($this->parameters as $parameter => $value) { | ||
| 70 | $escapedParameters[] = match ($this->parameterTypes[$parameter]) { | ||
| 71 | ParameterType::BINARY, ParameterType::LARGE_OBJECT => $value === null | ||
| 72 | ? null | ||
| 73 | : pg_escape_bytea($this->connection, is_resource($value) ? stream_get_contents($value) : $value), | ||
| 74 | default => $value, | ||
| 75 | }; | ||
| 76 | } | ||
| 77 | |||
| 78 | if (@pg_send_execute($this->connection, $this->name, $escapedParameters) !== true) { | ||
| 79 | throw new Exception(pg_last_error($this->connection)); | ||
| 80 | } | ||
| 81 | |||
| 82 | $result = @pg_get_result($this->connection); | ||
| 83 | assert($result !== false); | ||
| 84 | |||
| 85 | if ((bool) pg_result_error($result)) { | ||
| 86 | throw Exception::fromResult($result); | ||
| 87 | } | ||
| 88 | |||
| 89 | return new Result($result); | ||
| 90 | } | ||
| 91 | } | ||
