summaryrefslogtreecommitdiff
path: root/vendor/doctrine/dbal/src/Driver/PgSQL/Statement.php
diff options
context:
space:
mode:
Diffstat (limited to 'vendor/doctrine/dbal/src/Driver/PgSQL/Statement.php')
-rw-r--r--vendor/doctrine/dbal/src/Driver/PgSQL/Statement.php91
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
3declare(strict_types=1);
4
5namespace Doctrine\DBAL\Driver\PgSQL;
6
7use Doctrine\DBAL\Driver\PgSQL\Exception\UnknownParameter;
8use Doctrine\DBAL\Driver\Statement as StatementInterface;
9use Doctrine\DBAL\ParameterType;
10use PgSql\Connection as PgSqlConnection;
11
12use function assert;
13use function is_resource;
14use function ksort;
15use function pg_escape_bytea;
16use function pg_escape_identifier;
17use function pg_get_result;
18use function pg_last_error;
19use function pg_query;
20use function pg_result_error;
21use function pg_send_execute;
22use function stream_get_contents;
23
24final 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}