diff options
Diffstat (limited to 'vendor/doctrine/dbal/src/Driver/Mysqli/Connection.php')
-rw-r--r-- | vendor/doctrine/dbal/src/Driver/Mysqli/Connection.php | 112 |
1 files changed, 112 insertions, 0 deletions
diff --git a/vendor/doctrine/dbal/src/Driver/Mysqli/Connection.php b/vendor/doctrine/dbal/src/Driver/Mysqli/Connection.php new file mode 100644 index 0000000..cc00fb6 --- /dev/null +++ b/vendor/doctrine/dbal/src/Driver/Mysqli/Connection.php | |||
@@ -0,0 +1,112 @@ | |||
1 | <?php | ||
2 | |||
3 | declare(strict_types=1); | ||
4 | |||
5 | namespace Doctrine\DBAL\Driver\Mysqli; | ||
6 | |||
7 | use Doctrine\DBAL\Driver\Connection as ConnectionInterface; | ||
8 | use Doctrine\DBAL\Driver\Exception; | ||
9 | use Doctrine\DBAL\Driver\Mysqli\Exception\ConnectionError; | ||
10 | use mysqli; | ||
11 | use mysqli_sql_exception; | ||
12 | |||
13 | final class Connection implements ConnectionInterface | ||
14 | { | ||
15 | /** | ||
16 | * Name of the option to set connection flags | ||
17 | */ | ||
18 | public const OPTION_FLAGS = 'flags'; | ||
19 | |||
20 | /** @internal The connection can be only instantiated by its driver. */ | ||
21 | public function __construct(private readonly mysqli $connection) | ||
22 | { | ||
23 | } | ||
24 | |||
25 | public function getServerVersion(): string | ||
26 | { | ||
27 | return $this->connection->get_server_info(); | ||
28 | } | ||
29 | |||
30 | public function prepare(string $sql): Statement | ||
31 | { | ||
32 | try { | ||
33 | $stmt = $this->connection->prepare($sql); | ||
34 | } catch (mysqli_sql_exception $e) { | ||
35 | throw ConnectionError::upcast($e); | ||
36 | } | ||
37 | |||
38 | if ($stmt === false) { | ||
39 | throw ConnectionError::new($this->connection); | ||
40 | } | ||
41 | |||
42 | return new Statement($stmt); | ||
43 | } | ||
44 | |||
45 | public function query(string $sql): Result | ||
46 | { | ||
47 | return $this->prepare($sql)->execute(); | ||
48 | } | ||
49 | |||
50 | public function quote(string $value): string | ||
51 | { | ||
52 | return "'" . $this->connection->escape_string($value) . "'"; | ||
53 | } | ||
54 | |||
55 | public function exec(string $sql): int|string | ||
56 | { | ||
57 | try { | ||
58 | $result = $this->connection->query($sql); | ||
59 | } catch (mysqli_sql_exception $e) { | ||
60 | throw ConnectionError::upcast($e); | ||
61 | } | ||
62 | |||
63 | if ($result === false) { | ||
64 | throw ConnectionError::new($this->connection); | ||
65 | } | ||
66 | |||
67 | return $this->connection->affected_rows; | ||
68 | } | ||
69 | |||
70 | public function lastInsertId(): int|string | ||
71 | { | ||
72 | $lastInsertId = $this->connection->insert_id; | ||
73 | |||
74 | if ($lastInsertId === 0) { | ||
75 | throw Exception\NoIdentityValue::new(); | ||
76 | } | ||
77 | |||
78 | return $this->connection->insert_id; | ||
79 | } | ||
80 | |||
81 | public function beginTransaction(): void | ||
82 | { | ||
83 | $this->connection->begin_transaction(); | ||
84 | } | ||
85 | |||
86 | public function commit(): void | ||
87 | { | ||
88 | try { | ||
89 | if (! $this->connection->commit()) { | ||
90 | throw ConnectionError::new($this->connection); | ||
91 | } | ||
92 | } catch (mysqli_sql_exception $e) { | ||
93 | throw ConnectionError::upcast($e); | ||
94 | } | ||
95 | } | ||
96 | |||
97 | public function rollBack(): void | ||
98 | { | ||
99 | try { | ||
100 | if (! $this->connection->rollback()) { | ||
101 | throw ConnectionError::new($this->connection); | ||
102 | } | ||
103 | } catch (mysqli_sql_exception $e) { | ||
104 | throw ConnectionError::upcast($e); | ||
105 | } | ||
106 | } | ||
107 | |||
108 | public function getNativeConnection(): mysqli | ||
109 | { | ||
110 | return $this->connection; | ||
111 | } | ||
112 | } | ||