Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
51.06% |
24 / 47 |
|
47.06% |
8 / 17 |
CRAP | |
0.00% |
0 / 1 |
| common_persistence_sql_dbal_Driver | |
51.06% |
24 / 47 |
|
47.06% |
8 / 17 |
127.56 | |
0.00% |
0 / 1 |
| connect | |
70.00% |
7 / 10 |
|
0.00% |
0 / 1 |
4.43 | |||
| persistentConnect | |
53.85% |
7 / 13 |
|
0.00% |
0 / 1 |
9.54 | |||
| getConnection | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getDriverManagerClass | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
3 | |||
| setDriverManagerClass | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getPlatForm | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getSchemaManager | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| exec | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| query | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| quote | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| insert | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
6 | |||
| lastInsertId | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getDbalConnection | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getDataBase | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| transactional | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| quoteColumnsMap | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
6 | |||
| filterNotNumericParamTypes | |
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) 2013-2021 (original work) Open Assessment Technologies SA (under the project TAO-PRODUCT); |
| 19 | * |
| 20 | * @author Lionel Lecaque <lionel@taotesting.com> |
| 21 | * @author Jerome Bogaerts, <jerome@taotesting.com> |
| 22 | * |
| 23 | */ |
| 24 | |
| 25 | declare(strict_types=1); |
| 26 | |
| 27 | use Doctrine\DBAL\Configuration; |
| 28 | use Doctrine\DBAL\Connection; |
| 29 | use Doctrine\DBAL\DBALException; |
| 30 | use Doctrine\DBAL\Driver\ResultStatement; |
| 31 | use Doctrine\DBAL\Driver\Statement; |
| 32 | use Doctrine\DBAL\DriverManager; |
| 33 | use Doctrine\DBAL\Logging\SQLLogger; |
| 34 | |
| 35 | /** |
| 36 | * Dbal Driver |
| 37 | */ |
| 38 | class common_persistence_sql_dbal_Driver implements common_persistence_sql_Driver |
| 39 | { |
| 40 | use common_persistence_sql_MultipleOperations; |
| 41 | |
| 42 | /** |
| 43 | * @var Connection |
| 44 | */ |
| 45 | protected $connection; |
| 46 | |
| 47 | /** |
| 48 | * @var string |
| 49 | */ |
| 50 | private $driverManagerClass; |
| 51 | |
| 52 | /** |
| 53 | * Connect to Dbal |
| 54 | * |
| 55 | * @param string $id |
| 56 | * @param array $params |
| 57 | * @return common_persistence_Persistence|common_persistence_SqlPersistence |
| 58 | * @throws DBALException |
| 59 | */ |
| 60 | public function connect($id, array $params) |
| 61 | { |
| 62 | $isMysqlDbal = false; |
| 63 | if (isset($params['connection'])) { |
| 64 | $connectionParams = $params['connection']; |
| 65 | $isMysqlDbal = isset($connectionParams['driver']) && $connectionParams['driver'] === 'pdo_mysql'; |
| 66 | } else { |
| 67 | $connectionParams = $params; |
| 68 | $connectionParams['driver'] = str_replace('dbal_', '', $connectionParams['driver']); |
| 69 | } |
| 70 | |
| 71 | $this->persistentConnect($connectionParams, $params['sqlLoggerClass'] ?? null); |
| 72 | |
| 73 | if ($isMysqlDbal) { |
| 74 | $this->exec('SET SESSION SQL_MODE=\'ANSI_QUOTES\';'); |
| 75 | } |
| 76 | |
| 77 | return new common_persistence_SqlPersistence($params, $this); |
| 78 | } |
| 79 | |
| 80 | /** |
| 81 | * Endless connection |
| 82 | * |
| 83 | * @param $connectionParams |
| 84 | * @throws DBALException |
| 85 | */ |
| 86 | protected function persistentConnect($connectionParams, string $sqlClassLogger = null) |
| 87 | { |
| 88 | $config = new Configuration(); |
| 89 | if ($sqlClassLogger !== null && is_a($sqlClassLogger, SQLLogger::class, true)) { |
| 90 | $config->setSQLLogger(new $sqlClassLogger()); |
| 91 | } |
| 92 | |
| 93 | $connLimit = 3; // Max connection attempts. |
| 94 | $counter = 0; // Connection attempts counter. |
| 95 | |
| 96 | while (true) { |
| 97 | try { |
| 98 | /** @var Connection connection */ |
| 99 | $this->connection = $this->getConnection($connectionParams, $config); |
| 100 | break; |
| 101 | } catch (DBALException $e) { |
| 102 | $this->connection = null; |
| 103 | $counter++; |
| 104 | |
| 105 | if ($counter === $connLimit) { |
| 106 | // Connection attempts exceeded. |
| 107 | throw $e; |
| 108 | } |
| 109 | } |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | /** |
| 114 | * @param $params |
| 115 | * @param $config |
| 116 | * @return Connection |
| 117 | * |
| 118 | * @throws DBALException |
| 119 | * |
| 120 | */ |
| 121 | private function getConnection($params, $config) |
| 122 | { |
| 123 | return call_user_func($this->getDriverManagerClass() . '::getConnection', $params, $config); |
| 124 | } |
| 125 | |
| 126 | /** |
| 127 | * @return string |
| 128 | */ |
| 129 | private function getDriverManagerClass() |
| 130 | { |
| 131 | if (!$this->driverManagerClass || !class_exists($this->driverManagerClass)) { |
| 132 | $this->driverManagerClass = DriverManager::class; |
| 133 | } |
| 134 | return $this->driverManagerClass; |
| 135 | } |
| 136 | |
| 137 | /** |
| 138 | * @param string $driverManagerClass |
| 139 | */ |
| 140 | protected function setDriverManagerClass($driverManagerClass) |
| 141 | { |
| 142 | $this->driverManagerClass = $driverManagerClass; |
| 143 | } |
| 144 | |
| 145 | /** |
| 146 | * (non-PHPdoc) |
| 147 | * @see common_persistence_sql_Driver::getPlatForm() |
| 148 | */ |
| 149 | public function getPlatForm() |
| 150 | { |
| 151 | return new common_persistence_sql_Platform($this->getDbalConnection()); |
| 152 | } |
| 153 | |
| 154 | /** |
| 155 | * (non-PHPdoc) |
| 156 | * @see common_persistence_sql_Driver::getSchemaManager() |
| 157 | */ |
| 158 | public function getSchemaManager() |
| 159 | { |
| 160 | return new common_persistence_sql_dbal_SchemaManager($this->connection->getSchemaManager()); |
| 161 | } |
| 162 | |
| 163 | /** |
| 164 | * Execute the statement with provided params |
| 165 | * |
| 166 | * @param mixed $statement |
| 167 | * @param array $params |
| 168 | * @param array $types |
| 169 | * @return integer number of affected row |
| 170 | * @throws DBALException |
| 171 | */ |
| 172 | public function exec($statement, $params = [], array $types = []) |
| 173 | { |
| 174 | return $this->connection->executeUpdate($statement, $params, $types); |
| 175 | } |
| 176 | |
| 177 | |
| 178 | /** |
| 179 | * Query the statement with provided params |
| 180 | * |
| 181 | * @param Statement $statement |
| 182 | * @param array $params |
| 183 | * @param array $types |
| 184 | * @return ResultStatement |
| 185 | * @throws DBALException |
| 186 | */ |
| 187 | public function query($statement, $params = [], array $types = []) |
| 188 | { |
| 189 | return $this->connection->executeQuery($statement, $params, $types); |
| 190 | } |
| 191 | |
| 192 | /** |
| 193 | * Convenience access to PDO::quote. |
| 194 | * |
| 195 | * @param string $parameter The parameter to quote. |
| 196 | * @param int $parameter_type A PDO PARAM_XX constant. |
| 197 | * @return string The quoted string. |
| 198 | */ |
| 199 | public function quote($parameter, $parameter_type = PDO::PARAM_STR) |
| 200 | { |
| 201 | return $this->connection->quote($parameter, $parameter_type); |
| 202 | } |
| 203 | |
| 204 | /** |
| 205 | * Insert a single row into the database. |
| 206 | * |
| 207 | * column names and values will be encoded |
| 208 | * |
| 209 | * @param string $tableName name of the table |
| 210 | * @param array $data An associative array containing column-value pairs. |
| 211 | * @param array $types |
| 212 | * @return integer The number of affected rows. |
| 213 | * |
| 214 | * @throws DBALException |
| 215 | */ |
| 216 | public function insert($tableName, array $data, array $types = []) |
| 217 | { |
| 218 | $cleanColumns = $this->quoteColumnsMap($data); |
| 219 | |
| 220 | if (is_string(key($types))) { |
| 221 | $types = $this->filterNotNumericParamTypes($types); |
| 222 | $types = $this->quoteColumnsMap($types); |
| 223 | } |
| 224 | |
| 225 | return $this->connection->insert($tableName, $cleanColumns, $types); |
| 226 | } |
| 227 | |
| 228 | /** |
| 229 | * Convenience access to PDO::lastInsertId. |
| 230 | * |
| 231 | * @param string $name |
| 232 | * @return string The quoted string. |
| 233 | */ |
| 234 | public function lastInsertId($name = null) |
| 235 | { |
| 236 | return $this->connection->lastInsertId($name); |
| 237 | } |
| 238 | |
| 239 | /** |
| 240 | * @return Connection |
| 241 | */ |
| 242 | public function getDbalConnection() |
| 243 | { |
| 244 | return $this->connection; |
| 245 | } |
| 246 | |
| 247 | /** |
| 248 | * Returns the name of the connections database |
| 249 | * @return string |
| 250 | */ |
| 251 | public function getDataBase() |
| 252 | { |
| 253 | return $this->connection->getDatabase(); |
| 254 | } |
| 255 | |
| 256 | /** |
| 257 | * Execute a function within a transaction. |
| 258 | * |
| 259 | * @param Closure $func The function to execute in a transactional way. |
| 260 | * |
| 261 | * @return mixed The value returned by $func |
| 262 | * |
| 263 | * @throws Exception |
| 264 | * @throws Throwable |
| 265 | */ |
| 266 | public function transactional(Closure $func) |
| 267 | { |
| 268 | return $this->connection->transactional($func); |
| 269 | } |
| 270 | |
| 271 | /** |
| 272 | * add quotes to column names in column associated array |
| 273 | */ |
| 274 | private function quoteColumnsMap(array $columnsMap): array |
| 275 | { |
| 276 | $quotedColumnsMap = []; |
| 277 | foreach ($columnsMap as $column => $associatedValue) { |
| 278 | $quotedColumnsMap[$this->getPlatForm()->quoteIdentifier($column)] = $associatedValue; |
| 279 | } |
| 280 | |
| 281 | return $quotedColumnsMap; |
| 282 | } |
| 283 | |
| 284 | private function filterNotNumericParamTypes(array $types): array |
| 285 | { |
| 286 | return array_filter($types, 'is_int'); |
| 287 | } |
| 288 | } |