1 | <?php |
||
16 | abstract class Driver { |
||
17 | |||
18 | /** |
||
19 | * The internal PDO connection that is wrapped by this driver. |
||
20 | * @var \PDO |
||
21 | */ |
||
22 | private $pdo; |
||
23 | |||
24 | /** |
||
25 | * The default schema used in the connection. |
||
26 | * @var string |
||
27 | */ |
||
28 | protected $defaultSchema; |
||
29 | |||
30 | /** |
||
31 | * The connection parameters with which this connection was established. |
||
32 | * @var array |
||
33 | */ |
||
34 | protected $config; |
||
35 | |||
36 | /** |
||
37 | * An instance of the descriptor used internally. |
||
38 | * @var \ntentan\atiaa\Descriptor |
||
39 | */ |
||
40 | private $descriptor; |
||
41 | private static $transactionCount = 0; |
||
42 | |||
43 | /** |
||
44 | * Creates a new instance of the Atiaa driver. This class is usually initiated |
||
45 | * through the \ntentan\atiaa\Atiaa::getConnection() method. For example |
||
46 | * to create a new instance of a connection to a mysql database. |
||
47 | * |
||
48 | * ````php |
||
49 | * use ntentan\atiaa\Driver; |
||
50 | * |
||
51 | * \\ This automatically insitatiates the driver class |
||
52 | * $driver = Driver::getConnection( |
||
53 | * array( |
||
54 | * 'driver' => 'mysql', |
||
55 | * 'user' => 'root', |
||
56 | * 'password' => 'rootpassy', |
||
57 | * 'host' => 'localhost', |
||
58 | * 'dbname' => 'somedb' |
||
59 | * ) |
||
60 | * ); |
||
61 | * |
||
62 | * var_dump($driver->query("SELECT * FROM some_table"); |
||
63 | * var_dump($driver->describe()); |
||
64 | * ```` |
||
65 | * |
||
66 | * @param array<string> $config The configuration with which to connect to the database. |
||
67 | */ |
||
68 | 33 | public function __construct($config = null) { |
|
84 | |||
85 | 20 | public function __destruct() { |
|
88 | |||
89 | /** |
||
90 | * Close a connection to the database server. |
||
91 | */ |
||
92 | 20 | public function disconnect() { |
|
96 | |||
97 | /** |
||
98 | * Get the default schema of the current connection. |
||
99 | * @return string |
||
100 | */ |
||
101 | 11 | public function getDefaultSchema() { |
|
104 | |||
105 | /** |
||
106 | * Use the PDO driver to quote a string. |
||
107 | * @param type $string |
||
108 | * @return string |
||
109 | */ |
||
110 | 3 | public function quote($string) { |
|
113 | |||
114 | /** |
||
115 | * |
||
116 | * @param boolean $status |
||
117 | * @param \PDOStatement $result |
||
118 | */ |
||
119 | 25 | private function fetchRows($statement) { |
|
127 | |||
128 | /** |
||
129 | * Pepare and execute a query, while binding data at the same time. Prevents |
||
130 | * the writing of repetitive prepare and execute statements. This method |
||
131 | * returns an array which contains the results of the query that was |
||
132 | * executed. For queries which do not return any results a null is returned. |
||
133 | * |
||
134 | * @todo Add a parameter to cache prepared statements so they can be reused easily. |
||
135 | * |
||
136 | * @param string $query The query to be executed quoted in PDO style |
||
137 | * @param false|array<mixed> $bindData The data to be bound to the query object. |
||
138 | * @return array<mixed> |
||
139 | */ |
||
140 | 29 | public function query($query, $bindData = false) { |
|
156 | |||
157 | /** |
||
158 | * Runs a query but ensures that all identifiers are properly quoted by calling |
||
159 | * the Driver::quoteQueryIdentifiers method on the query before executing it. |
||
160 | * |
||
161 | * @param string $query |
||
162 | * @param false|array<mixed> $bindData |
||
163 | * @return array<mixed> |
||
164 | */ |
||
165 | 16 | public function quotedQuery($query, $bindData = false) { |
|
168 | |||
169 | /** |
||
170 | * Expands the configuration array into a format that can easily be passed |
||
171 | * to PDO. |
||
172 | * |
||
173 | * @param array $params The query parameters |
||
174 | * @return string |
||
175 | */ |
||
176 | 33 | private function expand($params) { |
|
194 | |||
195 | /** |
||
196 | * This method provides a system independent way of quoting identifiers in |
||
197 | * queries. By default all identifiers can be quoted with double quotes ("). |
||
198 | * When a query quoted with double quotes is passed through this method the |
||
199 | * output generated has the double quotes replaced with the quoting character |
||
200 | * of the target database platform. |
||
201 | * |
||
202 | * @param string $query |
||
203 | * @return string |
||
204 | */ |
||
205 | 19 | public function quoteQueryIdentifiers($query) { |
|
212 | |||
213 | /** |
||
214 | * Returns an array description of the schema represented by the connection. |
||
215 | * The description returns contains information about `tables`, `columns`, `keys`, |
||
216 | * `constraints`, `views` and `indices`. |
||
217 | * |
||
218 | * @return array<mixed> |
||
219 | */ |
||
220 | 6 | public function describe() { |
|
223 | |||
224 | /** |
||
225 | * Returns the description of a database table as an associative array. |
||
226 | * |
||
227 | * @param string $table |
||
228 | * @return array<mixed> |
||
229 | */ |
||
230 | 7 | public function describeTable($table) { |
|
241 | |||
242 | /** |
||
243 | * A wrapper arround PDO's beginTransaction method which uses a static reference |
||
244 | * counter to implement nested transactions. |
||
245 | */ |
||
246 | 6 | public function beginTransaction() { |
|
251 | |||
252 | /** |
||
253 | * A wrapper around PDO's commit transaction method which uses a static reference |
||
254 | * counter to implement nested transactions. |
||
255 | */ |
||
256 | 3 | public function commit() { |
|
261 | |||
262 | /** |
||
263 | * A wrapper around PDO's rollback transaction methd which rolls back all |
||
264 | * activities performed since the first call to begin transaction. |
||
265 | * Unfortunately, transactions cannot be rolled back in a nested fashion. |
||
266 | */ |
||
267 | 3 | public function rollback() { |
|
271 | |||
272 | /** |
||
273 | * Return the underlying PDO object. |
||
274 | * @return \PDO |
||
275 | */ |
||
276 | 3 | public function getPDO() { |
|
279 | |||
280 | /** |
||
281 | * Returns an instance of a descriptor for a given driver. |
||
282 | * @return \atiaa\Descriptor |
||
283 | */ |
||
284 | 13 | private function getDescriptor() { |
|
291 | |||
292 | /** |
||
293 | * A wrapper around PDO's lastInsertId() method. |
||
294 | * @return mixed |
||
295 | */ |
||
296 | public function getLastInsertId() { |
||
299 | |||
300 | /** |
||
301 | * Specify the default schema to use in cases where a schema is not provided |
||
302 | * as part of the table reference. |
||
303 | * @param string $defaultSchema |
||
304 | */ |
||
305 | public function setDefaultSchema($defaultSchema) { |
||
308 | |||
309 | abstract protected function getDriverName(); |
||
310 | |||
311 | abstract public function quoteIdentifier($identifier); |
||
312 | |||
313 | /** |
||
314 | * Returns a new instance of a driver based on the connection parameters |
||
315 | * passed to the method. The connection parameters are passed through an |
||
316 | * associative array with the following keys. |
||
317 | * |
||
318 | * driver |
||
319 | * : The name of the driver to use for the database connection. Supported |
||
320 | * drivers are `mysql` and `postgresql`. This parameter is required for |
||
321 | * all connections. |
||
322 | * |
||
323 | * user |
||
324 | * : The username to use for the database connection on platforms that |
||
325 | * support it. |
||
326 | * |
||
327 | * password |
||
328 | * : The password associated to the user specified in the connection. |
||
329 | * |
||
330 | * host |
||
331 | * : The host name of the database server. |
||
332 | * |
||
333 | * dbname |
||
334 | * : The name of the default database to use after the connection to the |
||
335 | * database is established. |
||
336 | * |
||
337 | * @param array $config |
||
338 | * @return \ntentan\atiaa\Driver |
||
339 | */ |
||
340 | /* public static function getConnection($config) |
||
341 | { |
||
342 | if (is_string($config) && file_exists($config)) { |
||
343 | require $config; |
||
344 | } else if ($config['driver'] == '') { |
||
345 | throw new DatabaseDriverException("Please specify a name for your database driver."); |
||
346 | } |
||
347 | try { |
||
348 | $class = "\\ntentan\\atiaa\\drivers\\" . ucfirst($config['driver']) . "Driver"; |
||
349 | return new $class($config); |
||
350 | } catch (\PDOException $e) { |
||
351 | throw new DatabaseDriverException("PDO failed to connect: {$e->getMessage()}", $e); |
||
352 | } |
||
353 | } */ |
||
354 | |||
355 | 3 | public function setCleanDefaults($cleanDefaults) { |
|
358 | |||
359 | } |
||
360 |
Our type inference engine has found an assignment of a scalar value (like a string, an integer or null) to a property which is an array.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property.
To type hint that a parameter can be either an array or null, you can set a type hint of array and a default value of null. The PHP interpreter will then accept both an array or null for that parameter.
The function can be called with either null or an array for the parameter
$needle
but will only accept an array as$haystack
.