summaryrefslogtreecommitdiff
path: root/vendor/doctrine/dbal/src/Driver/PgSQL/Connection.php
diff options
context:
space:
mode:
Diffstat (limited to 'vendor/doctrine/dbal/src/Driver/PgSQL/Connection.php')
-rw-r--r--vendor/doctrine/dbal/src/Driver/PgSQL/Connection.php129
1 files changed, 129 insertions, 0 deletions
diff --git a/vendor/doctrine/dbal/src/Driver/PgSQL/Connection.php b/vendor/doctrine/dbal/src/Driver/PgSQL/Connection.php
new file mode 100644
index 0000000..4f280b0
--- /dev/null
+++ b/vendor/doctrine/dbal/src/Driver/PgSQL/Connection.php
@@ -0,0 +1,129 @@
1<?php
2
3declare(strict_types=1);
4
5namespace Doctrine\DBAL\Driver\PgSQL;
6
7use Doctrine\DBAL\Driver\Connection as ConnectionInterface;
8use Doctrine\DBAL\Driver\Exception\NoIdentityValue;
9use Doctrine\DBAL\SQL\Parser;
10use PgSql\Connection as PgSqlConnection;
11
12use function assert;
13use function pg_close;
14use function pg_escape_literal;
15use function pg_get_result;
16use function pg_last_error;
17use function pg_result_error;
18use function pg_send_prepare;
19use function pg_send_query;
20use function pg_version;
21use function uniqid;
22
23final class Connection implements ConnectionInterface
24{
25 private readonly Parser $parser;
26
27 public function __construct(private readonly PgSqlConnection $connection)
28 {
29 $this->parser = new Parser(false);
30 }
31
32 public function __destruct()
33 {
34 if (! isset($this->connection)) {
35 return;
36 }
37
38 @pg_close($this->connection);
39 }
40
41 public function prepare(string $sql): Statement
42 {
43 $visitor = new ConvertParameters();
44 $this->parser->parse($sql, $visitor);
45
46 $statementName = uniqid('dbal', true);
47 if (@pg_send_prepare($this->connection, $statementName, $visitor->getSQL()) !== true) {
48 throw new Exception(pg_last_error($this->connection));
49 }
50
51 $result = @pg_get_result($this->connection);
52 assert($result !== false);
53
54 if ((bool) pg_result_error($result)) {
55 throw Exception::fromResult($result);
56 }
57
58 return new Statement($this->connection, $statementName, $visitor->getParameterMap());
59 }
60
61 public function query(string $sql): Result
62 {
63 if (@pg_send_query($this->connection, $sql) !== true) {
64 throw new Exception(pg_last_error($this->connection));
65 }
66
67 $result = @pg_get_result($this->connection);
68 assert($result !== false);
69
70 if ((bool) pg_result_error($result)) {
71 throw Exception::fromResult($result);
72 }
73
74 return new Result($result);
75 }
76
77 /** {@inheritDoc} */
78 public function quote(string $value): string
79 {
80 $quotedValue = pg_escape_literal($this->connection, $value);
81 assert($quotedValue !== false);
82
83 return $quotedValue;
84 }
85
86 public function exec(string $sql): int
87 {
88 return $this->query($sql)->rowCount();
89 }
90
91 /** {@inheritDoc} */
92 public function lastInsertId(): int|string
93 {
94 try {
95 return $this->query('SELECT LASTVAL()')->fetchOne();
96 } catch (Exception $exception) {
97 if ($exception->getSQLState() === '55000') {
98 throw NoIdentityValue::new($exception);
99 }
100
101 throw $exception;
102 }
103 }
104
105 public function beginTransaction(): void
106 {
107 $this->exec('BEGIN');
108 }
109
110 public function commit(): void
111 {
112 $this->exec('COMMIT');
113 }
114
115 public function rollBack(): void
116 {
117 $this->exec('ROLLBACK');
118 }
119
120 public function getServerVersion(): string
121 {
122 return (string) pg_version($this->connection)['server'];
123 }
124
125 public function getNativeConnection(): PgSqlConnection
126 {
127 return $this->connection;
128 }
129}