1 | <?php |
||
2 | |||
3 | declare(strict_types=1); |
||
4 | |||
5 | namespace Doctrine\DBAL\Driver; |
||
6 | |||
7 | use PDO; |
||
8 | use function assert; |
||
9 | |||
10 | /** |
||
11 | * PDO implementation of the Connection interface. |
||
12 | * |
||
13 | * Used by all PDO-based drivers. |
||
14 | */ |
||
15 | class PDOConnection implements ServerInfoAwareConnection |
||
16 | { |
||
17 | /** @var PDO */ |
||
18 | private $connection; |
||
19 | |||
20 | /** |
||
21 | * @param array<int, mixed> $options |
||
22 | * |
||
23 | * @throws PDOException In case of an error. |
||
24 | */ |
||
25 | 707 | public function __construct(string $dsn, string $username = '', string $password = '', array $options = []) |
|
26 | { |
||
27 | try { |
||
28 | 707 | $this->connection = new PDO($dsn, $username, $password, $options); |
|
29 | 667 | $this->connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); |
|
30 | 40 | } catch (\PDOException $exception) { |
|
31 | 40 | throw new PDOException($exception); |
|
32 | } |
||
33 | 667 | } |
|
34 | |||
35 | 2513 | public function exec(string $statement) : int |
|
36 | { |
||
37 | try { |
||
38 | 2513 | return $this->connection->exec($statement); |
|
39 | 1141 | } catch (\PDOException $exception) { |
|
40 | 1141 | throw new PDOException($exception); |
|
41 | } |
||
42 | } |
||
43 | |||
44 | 257 | public function getServerVersion() : string |
|
45 | { |
||
46 | 257 | return $this->connection->getAttribute(PDO::ATTR_SERVER_VERSION); |
|
47 | } |
||
48 | |||
49 | 2611 | public function prepare(string $sql) : Statement |
|
50 | { |
||
51 | try { |
||
52 | 2611 | return $this->createStatement( |
|
53 | 2611 | $this->connection->prepare($sql) |
|
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||
54 | ); |
||
55 | 30 | } catch (\PDOException $exception) { |
|
56 | 30 | throw new PDOException($exception); |
|
57 | } |
||
58 | } |
||
59 | |||
60 | 3160 | public function query(string $sql) : ResultStatement |
|
61 | { |
||
62 | try { |
||
63 | 3160 | $stmt = $this->connection->query($sql); |
|
64 | 3067 | assert($stmt instanceof \PDOStatement); |
|
65 | |||
66 | 3067 | return $this->createStatement($stmt); |
|
67 | 93 | } catch (\PDOException $exception) { |
|
68 | 93 | throw new PDOException($exception); |
|
69 | } |
||
70 | } |
||
71 | |||
72 | 136 | public function quote(string $input) : string |
|
73 | { |
||
74 | 136 | return $this->connection->quote($input); |
|
75 | } |
||
76 | |||
77 | 71 | public function lastInsertId(?string $name = null) : string |
|
78 | { |
||
79 | try { |
||
80 | 71 | if ($name === null) { |
|
81 | 16 | return $this->connection->lastInsertId(); |
|
82 | } |
||
83 | |||
84 | 55 | return $this->connection->lastInsertId($name); |
|
85 | } catch (\PDOException $exception) { |
||
86 | throw new PDOException($exception); |
||
87 | } |
||
88 | } |
||
89 | |||
90 | /** |
||
91 | * Creates a wrapped statement |
||
92 | */ |
||
93 | 4300 | protected function createStatement(\PDOStatement $stmt) : PDOStatement |
|
94 | { |
||
95 | 4300 | return new PDOStatement($stmt); |
|
96 | } |
||
97 | |||
98 | 212 | public function beginTransaction() : void |
|
99 | { |
||
100 | 212 | $this->connection->beginTransaction(); |
|
101 | 212 | } |
|
102 | |||
103 | 82 | public function commit() : void |
|
104 | { |
||
105 | 82 | $this->connection->commit(); |
|
106 | 82 | } |
|
107 | |||
108 | 142 | public function rollBack() : void |
|
109 | { |
||
110 | 142 | $this->connection->rollBack(); |
|
111 | 142 | } |
|
112 | |||
113 | 486 | public function getWrappedConnection() : PDO |
|
114 | { |
||
115 | 486 | return $this->connection; |
|
116 | } |
||
117 | } |
||
118 |