1 | <?php declare(strict_types=1); |
||
14 | class Database implements DatabaseInterface |
||
15 | { |
||
16 | /** Table name prefix */ |
||
17 | public static $prefix = ''; |
||
18 | |||
19 | /** @var \PDO Database driver */ |
||
20 | protected $driver; |
||
21 | |||
22 | /** @var string Database name */ |
||
23 | protected $database; |
||
24 | |||
25 | /** @var int Amount of milliseconds spent on queries */ |
||
26 | protected $elapsed; |
||
27 | |||
28 | /** @var int Amount queries executed */ |
||
29 | protected $count; |
||
30 | |||
31 | /** Do not serialize anything */ |
||
32 | public function __sleep() |
||
36 | |||
37 | /** |
||
38 | * Connect to a database using driver with parameters |
||
39 | * @param string $database Database name |
||
40 | * @param string $username Database username |
||
41 | * @param string $password Database password |
||
42 | * @param string $host Database host(localhost by default) |
||
43 | * @param int $port Database port(3306 by default) |
||
44 | * @param string $driver Database driver for interaction(MySQL by default) |
||
45 | * @param string $charset Database character set |
||
46 | * @return bool True if connection to database was successful |
||
47 | */ |
||
48 | public function connect( |
||
72 | |||
73 | /** |
||
74 | * Get database name |
||
75 | * @return string |
||
76 | * @deprecated |
||
77 | */ |
||
78 | public function database() |
||
82 | |||
83 | /** |
||
84 | * High-level database query executor |
||
85 | * @param string $sql SQL statement |
||
86 | * @return mixed Database query result |
||
87 | * @deprecated Use execute() |
||
88 | */ |
||
89 | public function query($sql) |
||
93 | |||
94 | /** |
||
95 | * Internal error beautifier. |
||
96 | * |
||
97 | * @param \Exception $exception |
||
98 | * @param $sql |
||
99 | * @param string $text |
||
100 | * |
||
101 | * @throws \Exception |
||
102 | */ |
||
103 | private function outputError(\Exception $exception, $sql, $text = 'Error executing database query:') |
||
112 | |||
113 | /** |
||
114 | * Proxy function for executing database fetching logic with exception, |
||
115 | * error, profile handling |
||
116 | * @param callback $fetcher Callback for fetching |
||
117 | * @return mixed Fetching function result |
||
118 | */ |
||
119 | private function executeFetcher($fetcher, $sql) |
||
147 | |||
148 | /** |
||
149 | * High-level database query executor |
||
150 | * @param string $sql SQL statement |
||
151 | * @return mixed Database query result |
||
152 | */ |
||
153 | private function innerQuery($sql) |
||
164 | |||
165 | /** |
||
166 | * Retrieve array of records from a database, if $className is passed method |
||
167 | * will try to create an object of that type. If request has failed than |
||
168 | * method will return empty array of stdClass all arrays regarding to $className is |
||
169 | * passed or not. |
||
170 | * |
||
171 | * @param string $sql SQL statement |
||
172 | * @return array Collection of arrays or objects |
||
173 | */ |
||
174 | private function innerFetch($sql, $className = null) |
||
189 | |||
190 | /** |
||
191 | * Special accelerated function to retrieve db record fields instead of objects |
||
192 | * |
||
193 | * @param string $sql SQL statement |
||
194 | * @param int $columnIndex Needed column index |
||
195 | * |
||
196 | * @return array Database records column value collection |
||
197 | */ |
||
198 | private function innerFetchColumn($sql, $columnIndex) |
||
209 | |||
210 | /** |
||
211 | * {@inheritdoc} |
||
212 | */ |
||
213 | public function execute(string $sql) |
||
217 | |||
218 | /** |
||
219 | * {@inheritdoc} |
||
220 | */ |
||
221 | public function fetch(string $sql) |
||
225 | |||
226 | /** |
||
227 | * Special accelerated function to retrieve db record fields instead of objects |
||
228 | * TODO: Change to be independent of query and class name, just SQL, this SQL |
||
229 | * should only have one column in SELECT part and then we do not need parameter |
||
230 | * for this as we can always take 0. |
||
231 | * |
||
232 | * @param string $entity Entity identifier |
||
233 | * @param QueryInterface Query object |
||
234 | * @param string $field Entity field identifier |
||
235 | * |
||
236 | * @return array Collection of rows with field value |
||
237 | * @deprecated |
||
238 | */ |
||
239 | public function fetchColumn($entity, QueryInterface $query, $field) |
||
249 | |||
250 | /** |
||
251 | * Count resulting rows. |
||
252 | * |
||
253 | * @param string Entity identifier |
||
254 | * @param QueryInterface Query object |
||
255 | * |
||
256 | * @return int Amount of rows |
||
257 | */ |
||
258 | public function count($entity, QueryInterface $query) |
||
265 | |||
266 | /** |
||
267 | * Quote variable for security reasons. |
||
268 | * |
||
269 | * @param string $value |
||
270 | * @return string Quoted value |
||
271 | */ |
||
272 | protected function quote($value) |
||
276 | |||
277 | /** |
||
278 | * Convert QueryInterface into SQL statement. |
||
279 | * |
||
280 | * @param string Entity identifier |
||
281 | * @param QueryInterface Query object |
||
282 | * |
||
283 | * @return string SQL statement |
||
284 | */ |
||
285 | protected function prepareSQL($entity, QueryInterface $query) |
||
289 | |||
290 | /** |
||
291 | * {@inheritdoc} |
||
292 | */ |
||
293 | public function fetchArray(string $sql) : array |
||
297 | |||
298 | /** |
||
299 | * {@inheritdoc} |
||
300 | */ |
||
301 | public function fetchObjects(string $sql, string $className) : array |
||
310 | |||
311 | /** |
||
312 | * {@inheritdoc} |
||
313 | */ |
||
314 | public function fetchColumns(string $sql, int $columnIndex) : array |
||
318 | |||
319 | /** |
||
320 | * Regroup database rows by primary field value. |
||
321 | * |
||
322 | * @param array $rows Collection of records received from database |
||
323 | * @param string $primaryField Primary field name for grouping |
||
324 | * |
||
325 | * @return array Grouped rows by primary field value |
||
326 | */ |
||
327 | protected function groupResults(array $rows, string $primaryField) : array |
||
342 | |||
343 | /** |
||
344 | * Fill entity instance fields from row column values according to entity metadata attributes. |
||
345 | * |
||
346 | * @param mixed $instance Entity instance |
||
347 | * @param array $attributes Metadata entity attributes |
||
348 | * @param array $row Database results row |
||
349 | */ |
||
350 | protected function fillEntityFieldValues($instance, array $attributes, array $row) |
||
366 | |||
367 | /** |
||
368 | * Create entity instances and its joined entities. |
||
369 | * |
||
370 | * @param array $rows |
||
371 | * @param string $primaryField |
||
372 | * @param string $className |
||
373 | * @param array $joinedClassNames |
||
374 | * |
||
375 | * @return array |
||
376 | */ |
||
377 | protected function createEntities(array $rows, string $primaryField, string $className, array $joinedClassNames) |
||
404 | } |
||
405 |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..