Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 55 |
|
0.00% |
0 / 27 |
CRAP | |
0.00% |
0 / 1 |
core_kernel_classes_DbWrapper | |
0.00% |
0 / 54 |
|
0.00% |
0 / 27 |
1056 | |
0.00% |
0 / 1 |
singleton | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
6 | |||
__construct | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
__clone | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
2 | |||
getNrOfQueries | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
query | |
0.00% |
0 / 7 |
|
0.00% |
0 / 1 |
6 | |||
exec | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
2 | |||
insert | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
getTables | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
getColumnNames | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
incrementNrOfQueries | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
getNrOfHits | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
incrementNrOfHits | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
getNrOfMisses | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
incrementNrOfMisses | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
debug | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
6 | |||
limitStatement | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
getPlatForm | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
6 | |||
getSchemaManager | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
6 | |||
getIndexAlreadyExistsErrorCode | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
getColumnNotFoundErrorCode | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
createIndex | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
rebuildIndexes | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
flush | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
getRowCount | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
2 | |||
lastInsertId | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
quote | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
quoteIdentifier | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 |
1 | <?php |
2 | |
3 | /* |
4 | * This program is free software; you can redistribute it and/or |
5 | * modify it under the terms of the GNU General Public License |
6 | * as published by the Free Software Foundation; under version 2 |
7 | * of the License (non-upgradable). |
8 | * |
9 | * This program is distributed in the hope that it will be useful, |
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
12 | * GNU General Public License for more details. |
13 | * |
14 | * You should have received a copy of the GNU General Public License |
15 | * along with this program; if not, write to the Free Software |
16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
17 | * |
18 | * Copyright (c) 2002-2008 (original work) Public Research Centre Henri Tudor & University of Luxembourg |
19 | * (under the project TAO & TAO2); |
20 | * 2008-2010 (update and modification) Deutsche Institut für Internationale Pädagogische Forschung |
21 | * (under the project TAO-TRANSFER); |
22 | * 2009-2012 (update and modification) Public Research Centre Henri Tudor |
23 | * (under the project TAO-SUSTAIN & TAO-DEV); |
24 | * |
25 | */ |
26 | |
27 | // phpcs:disable PSR1.Files.SideEffects |
28 | error_reporting(E_ALL); |
29 | // phpcs:enable PSR1.Files.SideEffects |
30 | |
31 | |
32 | /** |
33 | * Simple utility class that allow you to wrap the database connector. |
34 | * You can retrieve an instance evreywhere using the singleton method. |
35 | * |
36 | * This database wrapper uses PDO. |
37 | * |
38 | * @abstract |
39 | * @access public |
40 | * @author Jerome Bogaerts, <jerome@taotesting.com> |
41 | * @package generis |
42 | |
43 | */ |
44 | class core_kernel_classes_DbWrapper |
45 | { |
46 | // --- ASSOCIATIONS --- |
47 | |
48 | |
49 | // --- ATTRIBUTES --- |
50 | |
51 | /** |
52 | * singleton |
53 | * |
54 | * @access private |
55 | * @var DbWrapper |
56 | */ |
57 | private static $instance = null; |
58 | |
59 | |
60 | /** |
61 | * The number of queries executed by the wrapper since its instantiation. |
62 | * |
63 | * @access private |
64 | * @var int |
65 | */ |
66 | private $nrQueries = 0; |
67 | |
68 | |
69 | |
70 | /** |
71 | * The very last PDOStatement instance that was prepared by the wrapper. |
72 | * |
73 | * @access public |
74 | * @var PDOStatement |
75 | */ |
76 | public $lastPreparedExecStatement = null; |
77 | |
78 | |
79 | /** |
80 | * The number of statement reused in the statement store since the |
81 | * of the wrapper. |
82 | * |
83 | * @access private |
84 | * @var int |
85 | */ |
86 | private $nrHits = 0; |
87 | |
88 | /** |
89 | * The number of statements that could not be reused since the instantiation |
90 | * the wrapper. |
91 | * |
92 | * @access private |
93 | * @var int |
94 | */ |
95 | private $nrMisses = 0; |
96 | |
97 | /** |
98 | * debug mode |
99 | * |
100 | * @access public |
101 | * @var boolean |
102 | */ |
103 | public $debug = false; |
104 | |
105 | /** |
106 | * |
107 | * @var common_persistence_SqlPersistence |
108 | */ |
109 | private $persistence = null; |
110 | |
111 | /** |
112 | * |
113 | * @var common_persistence_sql_Platform |
114 | */ |
115 | private $platform = null; |
116 | /** |
117 | * |
118 | * @var common_persistence_sql_SchemaManager |
119 | */ |
120 | private $schemaManager = null; |
121 | /** |
122 | * Entry point. |
123 | * Enables you to retrieve staticly the DbWrapper instance. |
124 | * |
125 | * @access public |
126 | * @author Joel Bout, <joel.bout@tudor.lu> |
127 | * @throws core_kernel_persistence_Exception |
128 | * @return core_kernel_classes_DbWrapper |
129 | */ |
130 | public static function singleton() |
131 | { |
132 | $returnValue = null; |
133 | |
134 | if (!isset(self::$instance)) { |
135 | $c = __CLASS__; |
136 | self::$instance = new $c(); |
137 | } |
138 | $returnValue = self::$instance; |
139 | |
140 | |
141 | |
142 | return $returnValue; |
143 | } |
144 | |
145 | /** |
146 | * Initialize the storage engine connection |
147 | * |
148 | * @access private |
149 | * @author Joel Bout, <joel.bout@tudor.lu> |
150 | * @return core_kernel_classes_DbWrapper |
151 | */ |
152 | private function __construct() |
153 | { |
154 | $this->persistence = common_persistence_SqlPersistence::getPersistence('default'); |
155 | //file_put_contents(ROOT_PATH.'sql.php', file_get_contents(ROOT_PATH.'sql.php.master')); |
156 | } |
157 | |
158 | /** |
159 | * Will throw an exception. Singleton instances must not be cloned. |
160 | * |
161 | * @access public |
162 | * @author C�dric Alfonsi, <cedric.alfonsi@tudor.lu> |
163 | * @return core_kernel_classes_DbWrapper |
164 | */ |
165 | public function __clone() |
166 | { |
167 | $returnValue = null; |
168 | trigger_error('You cannot clone a singleton', E_USER_ERROR); |
169 | return $returnValue; |
170 | } |
171 | |
172 | /** |
173 | * Returns the ammount of queries executed so far. |
174 | * |
175 | * @access public |
176 | * @author Jerome Bogaerts, <jerome@taotesting.com> |
177 | * @return int |
178 | */ |
179 | public function getNrOfQueries() |
180 | { |
181 | return $this->nrQueries; |
182 | } |
183 | |
184 | /** |
185 | * Executes an SQL query on the storage engine. Should be used for SELECT |
186 | * only. |
187 | * |
188 | * @access public |
189 | * @author Jerome Bogaerts, <jerome@taotesting.com> |
190 | * @param string statement |
191 | * @param array params |
192 | * @return PDOStatement |
193 | */ |
194 | public function query($statement, $params = []) |
195 | { |
196 | $returnValue = null; |
197 | |
198 | |
199 | // $trace=debug_backtrace(); |
200 | // $caller=array_shift($trace); |
201 | // $caller=array_shift($trace); |
202 | // common_Logger::d('trace : '. $caller['function'] .$caller['class'] ); |
203 | // common_Logger::d($statement . implode('|', $params)); |
204 | $sth = $this->persistence->query($statement, $params); |
205 | |
206 | if (!empty($sth)) { |
207 | $returnValue = $sth; |
208 | } |
209 | |
210 | $this->incrementNrOfQueries(); |
211 | return $returnValue; |
212 | } |
213 | |
214 | /** |
215 | * Executes a query on the storage engine. Should be only used for INSERT, |
216 | * DELETE statements. |
217 | * |
218 | * @access public |
219 | * @author Jerome Bogaerts, <jerome@taotesting.com> |
220 | * @param string statement |
221 | * @param array params |
222 | * @return int |
223 | */ |
224 | public function exec($statement, $params = []) |
225 | { |
226 | $this->debug($statement); |
227 | |
228 | $returnValue = $this->persistence->exec($statement, $params); |
229 | |
230 | $this->incrementNrOfQueries(); |
231 | return (int) $returnValue; |
232 | } |
233 | |
234 | /** |
235 | * @author "Lionel Lecaque, <lionel@taotesting.com>" |
236 | * @param string $tableName |
237 | * @param array $data |
238 | */ |
239 | public function insert($tableName, array $data) |
240 | { |
241 | $this->incrementNrOfQueries(); |
242 | return $this->persistence->insert($tableName, $data); |
243 | } |
244 | |
245 | /** |
246 | * Returns an array of string containting the names of the tables contained |
247 | * the currently selected database in the storage engine. |
248 | * |
249 | * @abstract |
250 | * @access public |
251 | * @author Jerome Bogaerts, <jerome@taotesting.com> |
252 | * @return array |
253 | */ |
254 | public function getTables() |
255 | { |
256 | return $this->getSchemaManager()->getTables(); |
257 | } |
258 | |
259 | |
260 | |
261 | |
262 | /** |
263 | * Returns the column names of a given table |
264 | * |
265 | * @abstract |
266 | * @access public |
267 | * @author Jerome Bogaerts, <jerome@taotesting.com> |
268 | * @param string table |
269 | * @return array |
270 | */ |
271 | public function getColumnNames($table) |
272 | { |
273 | return $this->getSchemaManager()->getColumnNames($table); |
274 | } |
275 | |
276 | /** |
277 | * Increments the number of queries executed so far. |
278 | * |
279 | * @access protected |
280 | * @author Jerome Bogaerts, <jerome@taotesting.com> |
281 | * @return void |
282 | */ |
283 | protected function incrementNrOfQueries() |
284 | { |
285 | $this->nrQueries++; |
286 | } |
287 | |
288 | /** |
289 | * Returns the number of hits in the statements store. |
290 | * |
291 | * @access public |
292 | * @author Jerome Bogaerts, <jerome@taotesting.com> |
293 | * @return int |
294 | */ |
295 | public function getNrOfHits() |
296 | { |
297 | return $this->nrHits; |
298 | } |
299 | |
300 | /** |
301 | * Increment the number of hits in the statements store. |
302 | * |
303 | * @access protected |
304 | * @author Jerome Bogaerts, <jerome@taotesting.com> |
305 | * |
306 | */ |
307 | protected function incrementNrOfHits() |
308 | { |
309 | $this->nrHits++; |
310 | } |
311 | |
312 | /** |
313 | * Returns the number of misses in the statements store. |
314 | * |
315 | * @access public |
316 | * @author Jerome Bogaerts, <jerome@taotesting.com> |
317 | * @return int |
318 | */ |
319 | public function getNrOfMisses() |
320 | { |
321 | return $this->nrMisses; |
322 | } |
323 | |
324 | /** |
325 | * Increment the number of misses in the statements store. |
326 | * |
327 | * @access protected |
328 | * @author Jerome Bogaerts, <jerome@taotesting.com> |
329 | * |
330 | */ |
331 | protected function incrementNrOfMisses() |
332 | { |
333 | $this->nrMisses++; |
334 | } |
335 | |
336 | /** |
337 | * outputs a given statement in the logger. |
338 | * |
339 | * @access protected |
340 | * @author Jerome Bogaerts, <jerome@taotesting.com> |
341 | * @param string statement |
342 | * |
343 | */ |
344 | protected function debug($statement) |
345 | { |
346 | if ($this->debug) { |
347 | common_Logger::w($statement); |
348 | } |
349 | } |
350 | |
351 | /** |
352 | * Appends the correct LIMIT statement depending on the implementation of |
353 | * wrapper. For instance, limiting results in SQL statements are different |
354 | * mySQL and postgres. |
355 | * |
356 | * @abstract |
357 | * @access public |
358 | * @author Jerome Bogaerts, <jerome@taotesting.com> |
359 | * @param string statement The statement to limit |
360 | * @param int limit Limit lower bound. |
361 | * @param int offset Limit upper bound. |
362 | * @return string |
363 | */ |
364 | public function limitStatement($statement, $limit, $offset = 0) |
365 | { |
366 | return $this->getPlatform()->limitStatement($statement, $limit, $offset); |
367 | } |
368 | |
369 | |
370 | |
371 | /** |
372 | * @author "Lionel Lecaque, <lionel@taotesting.com>" |
373 | * return common_persistence_sql_Platform |
374 | */ |
375 | public function getPlatForm() |
376 | { |
377 | if ($this->platform == null) { |
378 | $this->platform = $this->persistence->getPlatForm(); |
379 | } |
380 | return $this->platform; |
381 | } |
382 | |
383 | /** |
384 | * @author "Lionel Lecaque, <lionel@taotesting.com>" |
385 | * return common_persistence_sql_SchemaManager |
386 | */ |
387 | public function getSchemaManager() |
388 | { |
389 | if ($this->schemaManager == null) { |
390 | $this->schemaManager = $this->persistence->getSchemaManager(); |
391 | } |
392 | return $this->schemaManager; |
393 | } |
394 | |
395 | /** |
396 | * The error code returned by PDO in when an Index already exists in a table |
397 | * a given DBMS implementation. |
398 | * |
399 | * |
400 | * @access public |
401 | * @author Jerome Bogaerts, <jerome@taotesting.com> |
402 | * @return string |
403 | */ |
404 | public function getIndexAlreadyExistsErrorCode() |
405 | { |
406 | return $this->getSchemaManager()->getIndexAlreadyExistsErrorCode(); |
407 | } |
408 | |
409 | /** |
410 | * |
411 | * @author Lionel Lecaque, lionel@taotesting.com |
412 | */ |
413 | public function getColumnNotFoundErrorCode() |
414 | { |
415 | return $this->getSchemaManager()->getColumnNotFoundErrorCode(); |
416 | } |
417 | |
418 | /** |
419 | * Create an index on a given table and selected columns. This method throws |
420 | * in case of error. |
421 | * |
422 | * @abstract |
423 | * @access public |
424 | * @author Jerome Bogaerts, <jerome@taotesting.com> |
425 | * @param string $indexName The name of the index to create. |
426 | * @param string $tableName A table name |
427 | * @param array $columns An associative array that represents the columns on which the index applies. The keys of |
428 | * the array are the name of the columns, the values are the length of the data to index in |
429 | * the column. If there is no length limitation, set the value of the array cell to null. |
430 | * @return void |
431 | */ |
432 | public function createIndex($indexName, $tableName, $columns) |
433 | { |
434 | return $this->getSchemaManager()->createIndex($indexName, $tableName, $columns); |
435 | } |
436 | |
437 | /** |
438 | * Rebuild the indexes of a given table. This method throws DBALException in case of error. |
439 | * |
440 | * @abstract |
441 | * @access public |
442 | * @author Jerome Bogaerts, <jerome@taotesting.com> |
443 | * @param string tableName |
444 | * @return void |
445 | * @throws \Doctrine\DBAL\DBALException |
446 | */ |
447 | public function rebuildIndexes($tableName) |
448 | { |
449 | return $this->getSchemaManager()->rebuildIndexes($tableName); |
450 | } |
451 | |
452 | /** |
453 | * Flush a particular table (query cache, ...). This method throws |
454 | * in case of error. |
455 | * |
456 | * @abstract |
457 | * @access public |
458 | * @author Jerome Bogaerts, <jerome@taotesting.com> |
459 | * @param string tableName |
460 | * @return void |
461 | */ |
462 | public function flush($tableName) |
463 | { |
464 | return $this->getSchemaManager()->flush($tableName); |
465 | } |
466 | |
467 | /** |
468 | * Get the row count of a given table. The column to count is specified for |
469 | * performance reasons. |
470 | * |
471 | * @access public |
472 | * @author Jerome Bogaerts, <jerome@taotesting.com> |
473 | * @param string tableName The name of the table. |
474 | * @param string column The column name on wich the COUNT sql statement must be performed. |
475 | * @return int |
476 | */ |
477 | public function getRowCount($tableName, $column = 'id') |
478 | { |
479 | $sql = 'SELECT count("' . $column . '") FROM "' . $tableName . '"'; |
480 | $result = $this->persistence->query($sql); |
481 | $returnValue = intval($result->fetchColumn(0)); |
482 | $result->closeCursor(); |
483 | return (int) $returnValue; |
484 | } |
485 | |
486 | /** |
487 | * Convenience access to lastInsertId. |
488 | * |
489 | * @author Jerome Bogaerts, <jerome@taotesting.com> |
490 | * @param string $name |
491 | * @return string The quoted string. |
492 | */ |
493 | public function lastInsertId($name = null) |
494 | { |
495 | return $this->persistence->lastInsertId($name); |
496 | } |
497 | |
498 | /** |
499 | * Convenience access to platForm quote. |
500 | * |
501 | * @author Jerome Bogaerts, <jerome@taotesting.com> |
502 | * @param string $parameter The parameter to quote. |
503 | * @param int $parameter_type A PDO PARAM_XX constant. |
504 | * @return string The quoted string. |
505 | */ |
506 | public function quote($parameter) |
507 | { |
508 | return $this->persistence->quote($parameter); |
509 | } |
510 | |
511 | public function quoteIdentifier($parameter) |
512 | { |
513 | return $this->persistence->getPlatForm()->quoteIdentifier($parameter); |
514 | } |
515 | } |