diff options
Diffstat (limited to 'vendor/doctrine/dbal/src/Driver/OCI8/Connection.php')
-rw-r--r-- | vendor/doctrine/dbal/src/Driver/OCI8/Connection.php | 119 |
1 files changed, 119 insertions, 0 deletions
diff --git a/vendor/doctrine/dbal/src/Driver/OCI8/Connection.php b/vendor/doctrine/dbal/src/Driver/OCI8/Connection.php new file mode 100644 index 0000000..3652ca0 --- /dev/null +++ b/vendor/doctrine/dbal/src/Driver/OCI8/Connection.php | |||
@@ -0,0 +1,119 @@ | |||
1 | <?php | ||
2 | |||
3 | declare(strict_types=1); | ||
4 | |||
5 | namespace Doctrine\DBAL\Driver\OCI8; | ||
6 | |||
7 | use Doctrine\DBAL\Driver\Connection as ConnectionInterface; | ||
8 | use Doctrine\DBAL\Driver\Exception; | ||
9 | use Doctrine\DBAL\Driver\Exception\IdentityColumnsNotSupported; | ||
10 | use Doctrine\DBAL\Driver\OCI8\Exception\Error; | ||
11 | use Doctrine\DBAL\SQL\Parser; | ||
12 | |||
13 | use function addcslashes; | ||
14 | use function assert; | ||
15 | use function is_resource; | ||
16 | use function oci_commit; | ||
17 | use function oci_parse; | ||
18 | use function oci_rollback; | ||
19 | use function oci_server_version; | ||
20 | use function preg_match; | ||
21 | use function str_replace; | ||
22 | |||
23 | final class Connection implements ConnectionInterface | ||
24 | { | ||
25 | private readonly Parser $parser; | ||
26 | private readonly ExecutionMode $executionMode; | ||
27 | |||
28 | /** | ||
29 | * @internal The connection can be only instantiated by its driver. | ||
30 | * | ||
31 | * @param resource $connection | ||
32 | */ | ||
33 | public function __construct(private readonly mixed $connection) | ||
34 | { | ||
35 | $this->parser = new Parser(false); | ||
36 | $this->executionMode = new ExecutionMode(); | ||
37 | } | ||
38 | |||
39 | public function getServerVersion(): string | ||
40 | { | ||
41 | $version = oci_server_version($this->connection); | ||
42 | assert($version !== false); | ||
43 | |||
44 | $result = preg_match('/\s+(\d+\.\d+\.\d+\.\d+\.\d+)\s+/', $version, $matches); | ||
45 | assert($result === 1); | ||
46 | |||
47 | return $matches[1]; | ||
48 | } | ||
49 | |||
50 | /** @throws Parser\Exception */ | ||
51 | public function prepare(string $sql): Statement | ||
52 | { | ||
53 | $visitor = new ConvertPositionalToNamedPlaceholders(); | ||
54 | |||
55 | $this->parser->parse($sql, $visitor); | ||
56 | |||
57 | $statement = oci_parse($this->connection, $visitor->getSQL()); | ||
58 | assert(is_resource($statement)); | ||
59 | |||
60 | return new Statement($this->connection, $statement, $visitor->getParameterMap(), $this->executionMode); | ||
61 | } | ||
62 | |||
63 | /** | ||
64 | * @throws Exception | ||
65 | * @throws Parser\Exception | ||
66 | */ | ||
67 | public function query(string $sql): Result | ||
68 | { | ||
69 | return $this->prepare($sql)->execute(); | ||
70 | } | ||
71 | |||
72 | public function quote(string $value): string | ||
73 | { | ||
74 | return "'" . addcslashes(str_replace("'", "''", $value), "\000\n\r\\\032") . "'"; | ||
75 | } | ||
76 | |||
77 | /** | ||
78 | * @throws Exception | ||
79 | * @throws Parser\Exception | ||
80 | */ | ||
81 | public function exec(string $sql): int|string | ||
82 | { | ||
83 | return $this->prepare($sql)->execute()->rowCount(); | ||
84 | } | ||
85 | |||
86 | public function lastInsertId(): int|string | ||
87 | { | ||
88 | throw IdentityColumnsNotSupported::new(); | ||
89 | } | ||
90 | |||
91 | public function beginTransaction(): void | ||
92 | { | ||
93 | $this->executionMode->disableAutoCommit(); | ||
94 | } | ||
95 | |||
96 | public function commit(): void | ||
97 | { | ||
98 | if (! oci_commit($this->connection)) { | ||
99 | throw Error::new($this->connection); | ||
100 | } | ||
101 | |||
102 | $this->executionMode->enableAutoCommit(); | ||
103 | } | ||
104 | |||
105 | public function rollBack(): void | ||
106 | { | ||
107 | if (! oci_rollback($this->connection)) { | ||
108 | throw Error::new($this->connection); | ||
109 | } | ||
110 | |||
111 | $this->executionMode->enableAutoCommit(); | ||
112 | } | ||
113 | |||
114 | /** @return resource */ | ||
115 | public function getNativeConnection() | ||
116 | { | ||
117 | return $this->connection; | ||
118 | } | ||
119 | } | ||