1 | <?php |
||
2 | |||
3 | declare(strict_types=1); |
||
4 | |||
5 | namespace Cozy\Database\Relational; |
||
6 | |||
7 | /** |
||
8 | * Represents a connection to a relational database server. |
||
9 | * It encapsulates a PDO instance to simplify and improve its functionality, in addition to |
||
10 | * allowing good security practices. |
||
11 | */ |
||
12 | class Connection |
||
13 | { |
||
14 | /** @var \PDO */ |
||
15 | protected $pdo; |
||
16 | protected $statements = []; |
||
17 | |||
18 | /** |
||
19 | * Wraps a PDO instance representing a connection to a database. |
||
20 | * |
||
21 | * @param \PDO $pdo Instance of PDO. |
||
22 | */ |
||
23 | public function __construct(\PDO $pdo) |
||
24 | { |
||
25 | $pdo->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION); |
||
26 | $pdo->setAttribute(\PDO::ATTR_EMULATE_PREPARES, false); |
||
27 | $this->pdo = $pdo; |
||
28 | } |
||
29 | |||
30 | /** |
||
31 | * Returns the wrapped PDO object. |
||
32 | * |
||
33 | * @return \PDO |
||
34 | */ |
||
35 | public function getPdo() |
||
36 | { |
||
37 | return $this->pdo; |
||
38 | } |
||
39 | |||
40 | public function isAlive(): bool |
||
41 | { |
||
42 | try { |
||
43 | if (@$this->pdo->query('SELECT 1') == false) { |
||
44 | return false; |
||
45 | } |
||
46 | |||
47 | return true; |
||
48 | } catch (\PDOException $e) { |
||
49 | return false; |
||
50 | } |
||
51 | } |
||
52 | |||
53 | /** |
||
54 | * Returns error information about the last operation on the database handle. |
||
55 | * |
||
56 | * @return array |
||
57 | */ |
||
58 | public function getErrorInfo() |
||
59 | { |
||
60 | return $this->pdo->errorInfo(); |
||
61 | } |
||
62 | |||
63 | /** |
||
64 | * Prepares a statement for execution. |
||
65 | * |
||
66 | * @param string $sentence A valid and properly escaped SQL sentence. |
||
67 | * @param array $driver_options Attribute values for the PDOStatement object. |
||
68 | * @return Statement|bool Returns a Statement object or false in case of failure. |
||
69 | */ |
||
70 | public function prepare(string $sentence, array $driver_options = []) |
||
71 | { |
||
72 | try { |
||
73 | $statement = $this->pdo->prepare($sentence, $driver_options); |
||
74 | |||
75 | if ($statement === false) { |
||
76 | return false; |
||
77 | } |
||
78 | |||
79 | return new Statement($statement); |
||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||
80 | } catch (\PDOException $e) { |
||
81 | throw new Exception($e->getMessage(), $e->getCode(), $this->pdo->errorInfo(), $sentence); |
||
82 | } |
||
83 | } |
||
84 | |||
85 | /** |
||
86 | * Retrieve a database connection attribute from the wrapped PDO. |
||
87 | * |
||
88 | * @param int $attribute One of the PDO::ATTR_* constants |
||
89 | * @return mixed A successful call returns the value of the requested PDO attribute, otherwise returns null. |
||
90 | */ |
||
91 | public function getAttribute($attribute) |
||
92 | { |
||
93 | return $this->pdo->getAttribute($attribute); |
||
94 | } |
||
95 | |||
96 | /** |
||
97 | * Sets an attribute in the wrapped PDO. |
||
98 | * |
||
99 | * @param int $attribute One of the PDO::ATTR_* constants. |
||
100 | * @param mixed $value The value to pass. |
||
101 | * @return bool TRUE on success or FALSE on failure. |
||
102 | * @throws Exception |
||
103 | */ |
||
104 | public function setAttribute($attribute, $value) |
||
105 | { |
||
106 | if ($attribute === \PDO::ATTR_EMULATE_PREPARES && $value !== false) { |
||
107 | throw new Exception( |
||
108 | 'Cozy Database does not allow the use of emulated prepared statements, ' . |
||
109 | 'which would be a security downgrade.', |
||
110 | 'CZ099' |
||
111 | ); |
||
112 | } elseif ($attribute === \PDO::ATTR_ERRMODE && $value !== \PDO::ERRMODE_EXCEPTION) { |
||
113 | throw new Exception( |
||
114 | 'Cozy Database only allows the safest-by-default error mode (exceptions).', |
||
115 | 'CZ099' |
||
116 | ); |
||
117 | } |
||
118 | |||
119 | return $this->pdo->setAttribute($attribute, $value); |
||
120 | } |
||
121 | |||
122 | /** |
||
123 | * Quotes a string for use in a query. |
||
124 | * |
||
125 | * @param string $string The string to be quoted. |
||
126 | * @param int $parameter_type Provides a data type hint for drivers that have alternate quoting styles. |
||
127 | * @return string|bool A quoted string that is theoretically safe to pass into an SQL statement. |
||
128 | * Returns FALSE if the driver does not support quoting in this way. |
||
129 | */ |
||
130 | public function quote($string, $parameter_type = \PDO::PARAM_STR) |
||
131 | { |
||
132 | return $this->pdo->quote($string, $parameter_type); |
||
133 | } |
||
134 | |||
135 | /** |
||
136 | * Returns the ID of the last inserted row or sequence value. |
||
137 | * |
||
138 | * @param string|null $name Name of the sequence object from which the ID should be returned. |
||
139 | * @return string |
||
140 | */ |
||
141 | public function getLastInsertId(string $name = null) |
||
142 | { |
||
143 | return $this->pdo->lastInsertId($name); |
||
144 | } |
||
145 | |||
146 | /** |
||
147 | * Initiates a transaction. |
||
148 | * |
||
149 | * @return bool TRUE on success or FALSE on failure. |
||
150 | */ |
||
151 | public function beginTransaction() |
||
152 | { |
||
153 | try { |
||
154 | return $this->pdo->beginTransaction(); |
||
155 | } catch (\PDOException $e) { |
||
156 | throw new Exception($e->getMessage(), $e->getCode(), $this->pdo->errorInfo()); |
||
157 | } |
||
158 | } |
||
159 | |||
160 | /** |
||
161 | * Commits the current transaction. |
||
162 | * |
||
163 | * @return bool TRUE on success or FALSE on failure. |
||
164 | */ |
||
165 | public function commitTransaction() |
||
166 | { |
||
167 | try { |
||
168 | return $this->pdo->commit(); |
||
169 | } catch (\PDOException $e) { |
||
170 | throw new Exception($e->getMessage(), $e->getCode(), $this->pdo->errorInfo()); |
||
171 | } |
||
172 | } |
||
173 | |||
174 | /** |
||
175 | * Rolls back the current transaction. |
||
176 | * |
||
177 | * @return bool TRUE on success or FALSE on failure. |
||
178 | */ |
||
179 | public function rollBackTransaction() |
||
180 | { |
||
181 | try { |
||
182 | return $this->pdo->rollBack(); |
||
183 | } catch (\PDOException $e) { |
||
184 | throw new Exception($e->getMessage(), $e->getCode(), $this->pdo->errorInfo()); |
||
185 | } |
||
186 | } |
||
187 | |||
188 | /** |
||
189 | * Checks if inside a transaction. |
||
190 | * |
||
191 | * @return bool TRUE if a transaction is currently active, and FALSE if not. |
||
192 | */ |
||
193 | public function inTransaction() |
||
194 | { |
||
195 | return $this->pdo->inTransaction(); |
||
196 | } |
||
197 | } |
||
198 |