@@ -48,410 +48,410 @@ |
||
| 48 | 48 | use OCP\PreConditionNotMetException; |
| 49 | 49 | |
| 50 | 50 | class Connection extends ReconnectWrapper implements IDBConnection { |
| 51 | - /** |
|
| 52 | - * @var string $tablePrefix |
|
| 53 | - */ |
|
| 54 | - protected $tablePrefix; |
|
| 55 | - |
|
| 56 | - /** |
|
| 57 | - * @var \OC\DB\Adapter $adapter |
|
| 58 | - */ |
|
| 59 | - protected $adapter; |
|
| 60 | - |
|
| 61 | - protected $lockedTable = null; |
|
| 62 | - |
|
| 63 | - public function connect() { |
|
| 64 | - try { |
|
| 65 | - return parent::connect(); |
|
| 66 | - } catch (DBALException $e) { |
|
| 67 | - // throw a new exception to prevent leaking info from the stacktrace |
|
| 68 | - throw new DBALException('Failed to connect to the database: ' . $e->getMessage(), $e->getCode()); |
|
| 69 | - } |
|
| 70 | - } |
|
| 71 | - |
|
| 72 | - /** |
|
| 73 | - * Returns a QueryBuilder for the connection. |
|
| 74 | - * |
|
| 75 | - * @return \OCP\DB\QueryBuilder\IQueryBuilder |
|
| 76 | - */ |
|
| 77 | - public function getQueryBuilder() { |
|
| 78 | - return new QueryBuilder( |
|
| 79 | - $this, |
|
| 80 | - \OC::$server->getSystemConfig(), |
|
| 81 | - \OC::$server->getLogger() |
|
| 82 | - ); |
|
| 83 | - } |
|
| 84 | - |
|
| 85 | - /** |
|
| 86 | - * Gets the QueryBuilder for the connection. |
|
| 87 | - * |
|
| 88 | - * @return \Doctrine\DBAL\Query\QueryBuilder |
|
| 89 | - * @deprecated please use $this->getQueryBuilder() instead |
|
| 90 | - */ |
|
| 91 | - public function createQueryBuilder() { |
|
| 92 | - $backtrace = $this->getCallerBacktrace(); |
|
| 93 | - \OC::$server->getLogger()->debug('Doctrine QueryBuilder retrieved in {backtrace}', ['app' => 'core', 'backtrace' => $backtrace]); |
|
| 94 | - return parent::createQueryBuilder(); |
|
| 95 | - } |
|
| 96 | - |
|
| 97 | - /** |
|
| 98 | - * Gets the ExpressionBuilder for the connection. |
|
| 99 | - * |
|
| 100 | - * @return \Doctrine\DBAL\Query\Expression\ExpressionBuilder |
|
| 101 | - * @deprecated please use $this->getQueryBuilder()->expr() instead |
|
| 102 | - */ |
|
| 103 | - public function getExpressionBuilder() { |
|
| 104 | - $backtrace = $this->getCallerBacktrace(); |
|
| 105 | - \OC::$server->getLogger()->debug('Doctrine ExpressionBuilder retrieved in {backtrace}', ['app' => 'core', 'backtrace' => $backtrace]); |
|
| 106 | - return parent::getExpressionBuilder(); |
|
| 107 | - } |
|
| 108 | - |
|
| 109 | - /** |
|
| 110 | - * Get the file and line that called the method where `getCallerBacktrace()` was used |
|
| 111 | - * |
|
| 112 | - * @return string |
|
| 113 | - */ |
|
| 114 | - protected function getCallerBacktrace() { |
|
| 115 | - $traces = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 2); |
|
| 116 | - |
|
| 117 | - // 0 is the method where we use `getCallerBacktrace` |
|
| 118 | - // 1 is the target method which uses the method we want to log |
|
| 119 | - if (isset($traces[1])) { |
|
| 120 | - return $traces[1]['file'] . ':' . $traces[1]['line']; |
|
| 121 | - } |
|
| 122 | - |
|
| 123 | - return ''; |
|
| 124 | - } |
|
| 125 | - |
|
| 126 | - /** |
|
| 127 | - * @return string |
|
| 128 | - */ |
|
| 129 | - public function getPrefix() { |
|
| 130 | - return $this->tablePrefix; |
|
| 131 | - } |
|
| 132 | - |
|
| 133 | - /** |
|
| 134 | - * Initializes a new instance of the Connection class. |
|
| 135 | - * |
|
| 136 | - * @param array $params The connection parameters. |
|
| 137 | - * @param \Doctrine\DBAL\Driver $driver |
|
| 138 | - * @param \Doctrine\DBAL\Configuration $config |
|
| 139 | - * @param \Doctrine\Common\EventManager $eventManager |
|
| 140 | - * @throws \Exception |
|
| 141 | - */ |
|
| 142 | - public function __construct(array $params, Driver $driver, Configuration $config = null, |
|
| 143 | - EventManager $eventManager = null) |
|
| 144 | - { |
|
| 145 | - if (!isset($params['adapter'])) { |
|
| 146 | - throw new \Exception('adapter not set'); |
|
| 147 | - } |
|
| 148 | - if (!isset($params['tablePrefix'])) { |
|
| 149 | - throw new \Exception('tablePrefix not set'); |
|
| 150 | - } |
|
| 151 | - parent::__construct($params, $driver, $config, $eventManager); |
|
| 152 | - $this->adapter = new $params['adapter']($this); |
|
| 153 | - $this->tablePrefix = $params['tablePrefix']; |
|
| 154 | - } |
|
| 155 | - |
|
| 156 | - /** |
|
| 157 | - * Prepares an SQL statement. |
|
| 158 | - * |
|
| 159 | - * @param string $statement The SQL statement to prepare. |
|
| 160 | - * @param int $limit |
|
| 161 | - * @param int $offset |
|
| 162 | - * @return \Doctrine\DBAL\Driver\Statement The prepared statement. |
|
| 163 | - */ |
|
| 164 | - public function prepare( $statement, $limit=null, $offset=null ) { |
|
| 165 | - if ($limit === -1) { |
|
| 166 | - $limit = null; |
|
| 167 | - } |
|
| 168 | - if (!is_null($limit)) { |
|
| 169 | - $platform = $this->getDatabasePlatform(); |
|
| 170 | - $statement = $platform->modifyLimitQuery($statement, $limit, $offset); |
|
| 171 | - } |
|
| 172 | - $statement = $this->replaceTablePrefix($statement); |
|
| 173 | - $statement = $this->adapter->fixupStatement($statement); |
|
| 174 | - |
|
| 175 | - return parent::prepare($statement); |
|
| 176 | - } |
|
| 177 | - |
|
| 178 | - /** |
|
| 179 | - * Executes an, optionally parametrized, SQL query. |
|
| 180 | - * |
|
| 181 | - * If the query is parametrized, a prepared statement is used. |
|
| 182 | - * If an SQLLogger is configured, the execution is logged. |
|
| 183 | - * |
|
| 184 | - * @param string $query The SQL query to execute. |
|
| 185 | - * @param array $params The parameters to bind to the query, if any. |
|
| 186 | - * @param array $types The types the previous parameters are in. |
|
| 187 | - * @param \Doctrine\DBAL\Cache\QueryCacheProfile|null $qcp The query cache profile, optional. |
|
| 188 | - * |
|
| 189 | - * @return \Doctrine\DBAL\Driver\Statement The executed statement. |
|
| 190 | - * |
|
| 191 | - * @throws \Doctrine\DBAL\DBALException |
|
| 192 | - */ |
|
| 193 | - public function executeQuery($query, array $params = array(), $types = array(), QueryCacheProfile $qcp = null) |
|
| 194 | - { |
|
| 195 | - $query = $this->replaceTablePrefix($query); |
|
| 196 | - $query = $this->adapter->fixupStatement($query); |
|
| 197 | - return parent::executeQuery($query, $params, $types, $qcp); |
|
| 198 | - } |
|
| 199 | - |
|
| 200 | - /** |
|
| 201 | - * Executes an SQL INSERT/UPDATE/DELETE query with the given parameters |
|
| 202 | - * and returns the number of affected rows. |
|
| 203 | - * |
|
| 204 | - * This method supports PDO binding types as well as DBAL mapping types. |
|
| 205 | - * |
|
| 206 | - * @param string $query The SQL query. |
|
| 207 | - * @param array $params The query parameters. |
|
| 208 | - * @param array $types The parameter types. |
|
| 209 | - * |
|
| 210 | - * @return integer The number of affected rows. |
|
| 211 | - * |
|
| 212 | - * @throws \Doctrine\DBAL\DBALException |
|
| 213 | - */ |
|
| 214 | - public function executeUpdate($query, array $params = array(), array $types = array()) |
|
| 215 | - { |
|
| 216 | - $query = $this->replaceTablePrefix($query); |
|
| 217 | - $query = $this->adapter->fixupStatement($query); |
|
| 218 | - return parent::executeUpdate($query, $params, $types); |
|
| 219 | - } |
|
| 220 | - |
|
| 221 | - /** |
|
| 222 | - * Returns the ID of the last inserted row, or the last value from a sequence object, |
|
| 223 | - * depending on the underlying driver. |
|
| 224 | - * |
|
| 225 | - * Note: This method may not return a meaningful or consistent result across different drivers, |
|
| 226 | - * because the underlying database may not even support the notion of AUTO_INCREMENT/IDENTITY |
|
| 227 | - * columns or sequences. |
|
| 228 | - * |
|
| 229 | - * @param string $seqName Name of the sequence object from which the ID should be returned. |
|
| 230 | - * @return string A string representation of the last inserted ID. |
|
| 231 | - */ |
|
| 232 | - public function lastInsertId($seqName = null) { |
|
| 233 | - if ($seqName) { |
|
| 234 | - $seqName = $this->replaceTablePrefix($seqName); |
|
| 235 | - } |
|
| 236 | - return $this->adapter->lastInsertId($seqName); |
|
| 237 | - } |
|
| 238 | - |
|
| 239 | - // internal use |
|
| 240 | - public function realLastInsertId($seqName = null) { |
|
| 241 | - return parent::lastInsertId($seqName); |
|
| 242 | - } |
|
| 243 | - |
|
| 244 | - /** |
|
| 245 | - * Insert a row if the matching row does not exists. To accomplish proper race condition avoidance |
|
| 246 | - * it is needed that there is also a unique constraint on the values. Then this method will |
|
| 247 | - * catch the exception and return 0. |
|
| 248 | - * |
|
| 249 | - * @param string $table The table name (will replace *PREFIX* with the actual prefix) |
|
| 250 | - * @param array $input data that should be inserted into the table (column name => value) |
|
| 251 | - * @param array|null $compare List of values that should be checked for "if not exists" |
|
| 252 | - * If this is null or an empty array, all keys of $input will be compared |
|
| 253 | - * Please note: text fields (clob) must not be used in the compare array |
|
| 254 | - * @return int number of inserted rows |
|
| 255 | - * @throws \Doctrine\DBAL\DBALException |
|
| 256 | - * @deprecated 15.0.0 - use unique index and "try { $db->insert() } catch (UniqueConstraintViolationException $e) {}" instead, because it is more reliable and does not have the risk for deadlocks - see https://github.com/nextcloud/server/pull/12371 |
|
| 257 | - */ |
|
| 258 | - public function insertIfNotExist($table, $input, array $compare = null) { |
|
| 259 | - return $this->adapter->insertIfNotExist($table, $input, $compare); |
|
| 260 | - } |
|
| 261 | - |
|
| 262 | - public function insertIgnoreConflict(string $table, array $values) : int { |
|
| 263 | - return $this->adapter->insertIgnoreConflict($table, $values); |
|
| 264 | - } |
|
| 265 | - |
|
| 266 | - private function getType($value) { |
|
| 267 | - if (is_bool($value)) { |
|
| 268 | - return IQueryBuilder::PARAM_BOOL; |
|
| 269 | - } else if (is_int($value)) { |
|
| 270 | - return IQueryBuilder::PARAM_INT; |
|
| 271 | - } else { |
|
| 272 | - return IQueryBuilder::PARAM_STR; |
|
| 273 | - } |
|
| 274 | - } |
|
| 275 | - |
|
| 276 | - /** |
|
| 277 | - * Insert or update a row value |
|
| 278 | - * |
|
| 279 | - * @param string $table |
|
| 280 | - * @param array $keys (column name => value) |
|
| 281 | - * @param array $values (column name => value) |
|
| 282 | - * @param array $updatePreconditionValues ensure values match preconditions (column name => value) |
|
| 283 | - * @return int number of new rows |
|
| 284 | - * @throws \Doctrine\DBAL\DBALException |
|
| 285 | - * @throws PreConditionNotMetException |
|
| 286 | - * @suppress SqlInjectionChecker |
|
| 287 | - */ |
|
| 288 | - public function setValues($table, array $keys, array $values, array $updatePreconditionValues = []) { |
|
| 289 | - try { |
|
| 290 | - $insertQb = $this->getQueryBuilder(); |
|
| 291 | - $insertQb->insert($table) |
|
| 292 | - ->values( |
|
| 293 | - array_map(function($value) use ($insertQb) { |
|
| 294 | - return $insertQb->createNamedParameter($value, $this->getType($value)); |
|
| 295 | - }, array_merge($keys, $values)) |
|
| 296 | - ); |
|
| 297 | - return $insertQb->execute(); |
|
| 298 | - } catch (ConstraintViolationException $e) { |
|
| 299 | - // value already exists, try update |
|
| 300 | - $updateQb = $this->getQueryBuilder(); |
|
| 301 | - $updateQb->update($table); |
|
| 302 | - foreach ($values as $name => $value) { |
|
| 303 | - $updateQb->set($name, $updateQb->createNamedParameter($value, $this->getType($value))); |
|
| 304 | - } |
|
| 305 | - $where = $updateQb->expr()->andX(); |
|
| 306 | - $whereValues = array_merge($keys, $updatePreconditionValues); |
|
| 307 | - foreach ($whereValues as $name => $value) { |
|
| 308 | - $where->add($updateQb->expr()->eq( |
|
| 309 | - $name, |
|
| 310 | - $updateQb->createNamedParameter($value, $this->getType($value)), |
|
| 311 | - $this->getType($value) |
|
| 312 | - )); |
|
| 313 | - } |
|
| 314 | - $updateQb->where($where); |
|
| 315 | - $affected = $updateQb->execute(); |
|
| 316 | - |
|
| 317 | - if ($affected === 0 && !empty($updatePreconditionValues)) { |
|
| 318 | - throw new PreConditionNotMetException(); |
|
| 319 | - } |
|
| 320 | - |
|
| 321 | - return 0; |
|
| 322 | - } |
|
| 323 | - } |
|
| 324 | - |
|
| 325 | - /** |
|
| 326 | - * Create an exclusive read+write lock on a table |
|
| 327 | - * |
|
| 328 | - * @param string $tableName |
|
| 329 | - * @throws \BadMethodCallException When trying to acquire a second lock |
|
| 330 | - * @since 9.1.0 |
|
| 331 | - */ |
|
| 332 | - public function lockTable($tableName) { |
|
| 333 | - if ($this->lockedTable !== null) { |
|
| 334 | - throw new \BadMethodCallException('Can not lock a new table until the previous lock is released.'); |
|
| 335 | - } |
|
| 336 | - |
|
| 337 | - $tableName = $this->tablePrefix . $tableName; |
|
| 338 | - $this->lockedTable = $tableName; |
|
| 339 | - $this->adapter->lockTable($tableName); |
|
| 340 | - } |
|
| 341 | - |
|
| 342 | - /** |
|
| 343 | - * Release a previous acquired lock again |
|
| 344 | - * |
|
| 345 | - * @since 9.1.0 |
|
| 346 | - */ |
|
| 347 | - public function unlockTable() { |
|
| 348 | - $this->adapter->unlockTable(); |
|
| 349 | - $this->lockedTable = null; |
|
| 350 | - } |
|
| 351 | - |
|
| 352 | - /** |
|
| 353 | - * returns the error code and message as a string for logging |
|
| 354 | - * works with DoctrineException |
|
| 355 | - * @return string |
|
| 356 | - */ |
|
| 357 | - public function getError() { |
|
| 358 | - $msg = $this->errorCode() . ': '; |
|
| 359 | - $errorInfo = $this->errorInfo(); |
|
| 360 | - if (is_array($errorInfo)) { |
|
| 361 | - $msg .= 'SQLSTATE = '.$errorInfo[0] . ', '; |
|
| 362 | - $msg .= 'Driver Code = '.$errorInfo[1] . ', '; |
|
| 363 | - $msg .= 'Driver Message = '.$errorInfo[2]; |
|
| 364 | - } |
|
| 365 | - return $msg; |
|
| 366 | - } |
|
| 367 | - |
|
| 368 | - /** |
|
| 369 | - * Drop a table from the database if it exists |
|
| 370 | - * |
|
| 371 | - * @param string $table table name without the prefix |
|
| 372 | - */ |
|
| 373 | - public function dropTable($table) { |
|
| 374 | - $table = $this->tablePrefix . trim($table); |
|
| 375 | - $schema = $this->getSchemaManager(); |
|
| 376 | - if($schema->tablesExist(array($table))) { |
|
| 377 | - $schema->dropTable($table); |
|
| 378 | - } |
|
| 379 | - } |
|
| 380 | - |
|
| 381 | - /** |
|
| 382 | - * Check if a table exists |
|
| 383 | - * |
|
| 384 | - * @param string $table table name without the prefix |
|
| 385 | - * @return bool |
|
| 386 | - */ |
|
| 387 | - public function tableExists($table){ |
|
| 388 | - $table = $this->tablePrefix . trim($table); |
|
| 389 | - $schema = $this->getSchemaManager(); |
|
| 390 | - return $schema->tablesExist(array($table)); |
|
| 391 | - } |
|
| 392 | - |
|
| 393 | - // internal use |
|
| 394 | - /** |
|
| 395 | - * @param string $statement |
|
| 396 | - * @return string |
|
| 397 | - */ |
|
| 398 | - protected function replaceTablePrefix($statement) { |
|
| 399 | - return str_replace( '*PREFIX*', $this->tablePrefix, $statement ); |
|
| 400 | - } |
|
| 401 | - |
|
| 402 | - /** |
|
| 403 | - * Check if a transaction is active |
|
| 404 | - * |
|
| 405 | - * @return bool |
|
| 406 | - * @since 8.2.0 |
|
| 407 | - */ |
|
| 408 | - public function inTransaction() { |
|
| 409 | - return $this->getTransactionNestingLevel() > 0; |
|
| 410 | - } |
|
| 411 | - |
|
| 412 | - /** |
|
| 413 | - * Escape a parameter to be used in a LIKE query |
|
| 414 | - * |
|
| 415 | - * @param string $param |
|
| 416 | - * @return string |
|
| 417 | - */ |
|
| 418 | - public function escapeLikeParameter($param) { |
|
| 419 | - return addcslashes($param, '\\_%'); |
|
| 420 | - } |
|
| 421 | - |
|
| 422 | - /** |
|
| 423 | - * Check whether or not the current database support 4byte wide unicode |
|
| 424 | - * |
|
| 425 | - * @return bool |
|
| 426 | - * @since 11.0.0 |
|
| 427 | - */ |
|
| 428 | - public function supports4ByteText() { |
|
| 429 | - if (!$this->getDatabasePlatform() instanceof MySqlPlatform) { |
|
| 430 | - return true; |
|
| 431 | - } |
|
| 432 | - return $this->getParams()['charset'] === 'utf8mb4'; |
|
| 433 | - } |
|
| 434 | - |
|
| 435 | - |
|
| 436 | - /** |
|
| 437 | - * Create the schema of the connected database |
|
| 438 | - * |
|
| 439 | - * @return Schema |
|
| 440 | - */ |
|
| 441 | - public function createSchema() { |
|
| 442 | - $schemaManager = new MDB2SchemaManager($this); |
|
| 443 | - $migrator = $schemaManager->getMigrator(); |
|
| 444 | - return $migrator->createSchema(); |
|
| 445 | - } |
|
| 446 | - |
|
| 447 | - /** |
|
| 448 | - * Migrate the database to the given schema |
|
| 449 | - * |
|
| 450 | - * @param Schema $toSchema |
|
| 451 | - */ |
|
| 452 | - public function migrateToSchema(Schema $toSchema) { |
|
| 453 | - $schemaManager = new MDB2SchemaManager($this); |
|
| 454 | - $migrator = $schemaManager->getMigrator(); |
|
| 455 | - $migrator->migrate($toSchema); |
|
| 456 | - } |
|
| 51 | + /** |
|
| 52 | + * @var string $tablePrefix |
|
| 53 | + */ |
|
| 54 | + protected $tablePrefix; |
|
| 55 | + |
|
| 56 | + /** |
|
| 57 | + * @var \OC\DB\Adapter $adapter |
|
| 58 | + */ |
|
| 59 | + protected $adapter; |
|
| 60 | + |
|
| 61 | + protected $lockedTable = null; |
|
| 62 | + |
|
| 63 | + public function connect() { |
|
| 64 | + try { |
|
| 65 | + return parent::connect(); |
|
| 66 | + } catch (DBALException $e) { |
|
| 67 | + // throw a new exception to prevent leaking info from the stacktrace |
|
| 68 | + throw new DBALException('Failed to connect to the database: ' . $e->getMessage(), $e->getCode()); |
|
| 69 | + } |
|
| 70 | + } |
|
| 71 | + |
|
| 72 | + /** |
|
| 73 | + * Returns a QueryBuilder for the connection. |
|
| 74 | + * |
|
| 75 | + * @return \OCP\DB\QueryBuilder\IQueryBuilder |
|
| 76 | + */ |
|
| 77 | + public function getQueryBuilder() { |
|
| 78 | + return new QueryBuilder( |
|
| 79 | + $this, |
|
| 80 | + \OC::$server->getSystemConfig(), |
|
| 81 | + \OC::$server->getLogger() |
|
| 82 | + ); |
|
| 83 | + } |
|
| 84 | + |
|
| 85 | + /** |
|
| 86 | + * Gets the QueryBuilder for the connection. |
|
| 87 | + * |
|
| 88 | + * @return \Doctrine\DBAL\Query\QueryBuilder |
|
| 89 | + * @deprecated please use $this->getQueryBuilder() instead |
|
| 90 | + */ |
|
| 91 | + public function createQueryBuilder() { |
|
| 92 | + $backtrace = $this->getCallerBacktrace(); |
|
| 93 | + \OC::$server->getLogger()->debug('Doctrine QueryBuilder retrieved in {backtrace}', ['app' => 'core', 'backtrace' => $backtrace]); |
|
| 94 | + return parent::createQueryBuilder(); |
|
| 95 | + } |
|
| 96 | + |
|
| 97 | + /** |
|
| 98 | + * Gets the ExpressionBuilder for the connection. |
|
| 99 | + * |
|
| 100 | + * @return \Doctrine\DBAL\Query\Expression\ExpressionBuilder |
|
| 101 | + * @deprecated please use $this->getQueryBuilder()->expr() instead |
|
| 102 | + */ |
|
| 103 | + public function getExpressionBuilder() { |
|
| 104 | + $backtrace = $this->getCallerBacktrace(); |
|
| 105 | + \OC::$server->getLogger()->debug('Doctrine ExpressionBuilder retrieved in {backtrace}', ['app' => 'core', 'backtrace' => $backtrace]); |
|
| 106 | + return parent::getExpressionBuilder(); |
|
| 107 | + } |
|
| 108 | + |
|
| 109 | + /** |
|
| 110 | + * Get the file and line that called the method where `getCallerBacktrace()` was used |
|
| 111 | + * |
|
| 112 | + * @return string |
|
| 113 | + */ |
|
| 114 | + protected function getCallerBacktrace() { |
|
| 115 | + $traces = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 2); |
|
| 116 | + |
|
| 117 | + // 0 is the method where we use `getCallerBacktrace` |
|
| 118 | + // 1 is the target method which uses the method we want to log |
|
| 119 | + if (isset($traces[1])) { |
|
| 120 | + return $traces[1]['file'] . ':' . $traces[1]['line']; |
|
| 121 | + } |
|
| 122 | + |
|
| 123 | + return ''; |
|
| 124 | + } |
|
| 125 | + |
|
| 126 | + /** |
|
| 127 | + * @return string |
|
| 128 | + */ |
|
| 129 | + public function getPrefix() { |
|
| 130 | + return $this->tablePrefix; |
|
| 131 | + } |
|
| 132 | + |
|
| 133 | + /** |
|
| 134 | + * Initializes a new instance of the Connection class. |
|
| 135 | + * |
|
| 136 | + * @param array $params The connection parameters. |
|
| 137 | + * @param \Doctrine\DBAL\Driver $driver |
|
| 138 | + * @param \Doctrine\DBAL\Configuration $config |
|
| 139 | + * @param \Doctrine\Common\EventManager $eventManager |
|
| 140 | + * @throws \Exception |
|
| 141 | + */ |
|
| 142 | + public function __construct(array $params, Driver $driver, Configuration $config = null, |
|
| 143 | + EventManager $eventManager = null) |
|
| 144 | + { |
|
| 145 | + if (!isset($params['adapter'])) { |
|
| 146 | + throw new \Exception('adapter not set'); |
|
| 147 | + } |
|
| 148 | + if (!isset($params['tablePrefix'])) { |
|
| 149 | + throw new \Exception('tablePrefix not set'); |
|
| 150 | + } |
|
| 151 | + parent::__construct($params, $driver, $config, $eventManager); |
|
| 152 | + $this->adapter = new $params['adapter']($this); |
|
| 153 | + $this->tablePrefix = $params['tablePrefix']; |
|
| 154 | + } |
|
| 155 | + |
|
| 156 | + /** |
|
| 157 | + * Prepares an SQL statement. |
|
| 158 | + * |
|
| 159 | + * @param string $statement The SQL statement to prepare. |
|
| 160 | + * @param int $limit |
|
| 161 | + * @param int $offset |
|
| 162 | + * @return \Doctrine\DBAL\Driver\Statement The prepared statement. |
|
| 163 | + */ |
|
| 164 | + public function prepare( $statement, $limit=null, $offset=null ) { |
|
| 165 | + if ($limit === -1) { |
|
| 166 | + $limit = null; |
|
| 167 | + } |
|
| 168 | + if (!is_null($limit)) { |
|
| 169 | + $platform = $this->getDatabasePlatform(); |
|
| 170 | + $statement = $platform->modifyLimitQuery($statement, $limit, $offset); |
|
| 171 | + } |
|
| 172 | + $statement = $this->replaceTablePrefix($statement); |
|
| 173 | + $statement = $this->adapter->fixupStatement($statement); |
|
| 174 | + |
|
| 175 | + return parent::prepare($statement); |
|
| 176 | + } |
|
| 177 | + |
|
| 178 | + /** |
|
| 179 | + * Executes an, optionally parametrized, SQL query. |
|
| 180 | + * |
|
| 181 | + * If the query is parametrized, a prepared statement is used. |
|
| 182 | + * If an SQLLogger is configured, the execution is logged. |
|
| 183 | + * |
|
| 184 | + * @param string $query The SQL query to execute. |
|
| 185 | + * @param array $params The parameters to bind to the query, if any. |
|
| 186 | + * @param array $types The types the previous parameters are in. |
|
| 187 | + * @param \Doctrine\DBAL\Cache\QueryCacheProfile|null $qcp The query cache profile, optional. |
|
| 188 | + * |
|
| 189 | + * @return \Doctrine\DBAL\Driver\Statement The executed statement. |
|
| 190 | + * |
|
| 191 | + * @throws \Doctrine\DBAL\DBALException |
|
| 192 | + */ |
|
| 193 | + public function executeQuery($query, array $params = array(), $types = array(), QueryCacheProfile $qcp = null) |
|
| 194 | + { |
|
| 195 | + $query = $this->replaceTablePrefix($query); |
|
| 196 | + $query = $this->adapter->fixupStatement($query); |
|
| 197 | + return parent::executeQuery($query, $params, $types, $qcp); |
|
| 198 | + } |
|
| 199 | + |
|
| 200 | + /** |
|
| 201 | + * Executes an SQL INSERT/UPDATE/DELETE query with the given parameters |
|
| 202 | + * and returns the number of affected rows. |
|
| 203 | + * |
|
| 204 | + * This method supports PDO binding types as well as DBAL mapping types. |
|
| 205 | + * |
|
| 206 | + * @param string $query The SQL query. |
|
| 207 | + * @param array $params The query parameters. |
|
| 208 | + * @param array $types The parameter types. |
|
| 209 | + * |
|
| 210 | + * @return integer The number of affected rows. |
|
| 211 | + * |
|
| 212 | + * @throws \Doctrine\DBAL\DBALException |
|
| 213 | + */ |
|
| 214 | + public function executeUpdate($query, array $params = array(), array $types = array()) |
|
| 215 | + { |
|
| 216 | + $query = $this->replaceTablePrefix($query); |
|
| 217 | + $query = $this->adapter->fixupStatement($query); |
|
| 218 | + return parent::executeUpdate($query, $params, $types); |
|
| 219 | + } |
|
| 220 | + |
|
| 221 | + /** |
|
| 222 | + * Returns the ID of the last inserted row, or the last value from a sequence object, |
|
| 223 | + * depending on the underlying driver. |
|
| 224 | + * |
|
| 225 | + * Note: This method may not return a meaningful or consistent result across different drivers, |
|
| 226 | + * because the underlying database may not even support the notion of AUTO_INCREMENT/IDENTITY |
|
| 227 | + * columns or sequences. |
|
| 228 | + * |
|
| 229 | + * @param string $seqName Name of the sequence object from which the ID should be returned. |
|
| 230 | + * @return string A string representation of the last inserted ID. |
|
| 231 | + */ |
|
| 232 | + public function lastInsertId($seqName = null) { |
|
| 233 | + if ($seqName) { |
|
| 234 | + $seqName = $this->replaceTablePrefix($seqName); |
|
| 235 | + } |
|
| 236 | + return $this->adapter->lastInsertId($seqName); |
|
| 237 | + } |
|
| 238 | + |
|
| 239 | + // internal use |
|
| 240 | + public function realLastInsertId($seqName = null) { |
|
| 241 | + return parent::lastInsertId($seqName); |
|
| 242 | + } |
|
| 243 | + |
|
| 244 | + /** |
|
| 245 | + * Insert a row if the matching row does not exists. To accomplish proper race condition avoidance |
|
| 246 | + * it is needed that there is also a unique constraint on the values. Then this method will |
|
| 247 | + * catch the exception and return 0. |
|
| 248 | + * |
|
| 249 | + * @param string $table The table name (will replace *PREFIX* with the actual prefix) |
|
| 250 | + * @param array $input data that should be inserted into the table (column name => value) |
|
| 251 | + * @param array|null $compare List of values that should be checked for "if not exists" |
|
| 252 | + * If this is null or an empty array, all keys of $input will be compared |
|
| 253 | + * Please note: text fields (clob) must not be used in the compare array |
|
| 254 | + * @return int number of inserted rows |
|
| 255 | + * @throws \Doctrine\DBAL\DBALException |
|
| 256 | + * @deprecated 15.0.0 - use unique index and "try { $db->insert() } catch (UniqueConstraintViolationException $e) {}" instead, because it is more reliable and does not have the risk for deadlocks - see https://github.com/nextcloud/server/pull/12371 |
|
| 257 | + */ |
|
| 258 | + public function insertIfNotExist($table, $input, array $compare = null) { |
|
| 259 | + return $this->adapter->insertIfNotExist($table, $input, $compare); |
|
| 260 | + } |
|
| 261 | + |
|
| 262 | + public function insertIgnoreConflict(string $table, array $values) : int { |
|
| 263 | + return $this->adapter->insertIgnoreConflict($table, $values); |
|
| 264 | + } |
|
| 265 | + |
|
| 266 | + private function getType($value) { |
|
| 267 | + if (is_bool($value)) { |
|
| 268 | + return IQueryBuilder::PARAM_BOOL; |
|
| 269 | + } else if (is_int($value)) { |
|
| 270 | + return IQueryBuilder::PARAM_INT; |
|
| 271 | + } else { |
|
| 272 | + return IQueryBuilder::PARAM_STR; |
|
| 273 | + } |
|
| 274 | + } |
|
| 275 | + |
|
| 276 | + /** |
|
| 277 | + * Insert or update a row value |
|
| 278 | + * |
|
| 279 | + * @param string $table |
|
| 280 | + * @param array $keys (column name => value) |
|
| 281 | + * @param array $values (column name => value) |
|
| 282 | + * @param array $updatePreconditionValues ensure values match preconditions (column name => value) |
|
| 283 | + * @return int number of new rows |
|
| 284 | + * @throws \Doctrine\DBAL\DBALException |
|
| 285 | + * @throws PreConditionNotMetException |
|
| 286 | + * @suppress SqlInjectionChecker |
|
| 287 | + */ |
|
| 288 | + public function setValues($table, array $keys, array $values, array $updatePreconditionValues = []) { |
|
| 289 | + try { |
|
| 290 | + $insertQb = $this->getQueryBuilder(); |
|
| 291 | + $insertQb->insert($table) |
|
| 292 | + ->values( |
|
| 293 | + array_map(function($value) use ($insertQb) { |
|
| 294 | + return $insertQb->createNamedParameter($value, $this->getType($value)); |
|
| 295 | + }, array_merge($keys, $values)) |
|
| 296 | + ); |
|
| 297 | + return $insertQb->execute(); |
|
| 298 | + } catch (ConstraintViolationException $e) { |
|
| 299 | + // value already exists, try update |
|
| 300 | + $updateQb = $this->getQueryBuilder(); |
|
| 301 | + $updateQb->update($table); |
|
| 302 | + foreach ($values as $name => $value) { |
|
| 303 | + $updateQb->set($name, $updateQb->createNamedParameter($value, $this->getType($value))); |
|
| 304 | + } |
|
| 305 | + $where = $updateQb->expr()->andX(); |
|
| 306 | + $whereValues = array_merge($keys, $updatePreconditionValues); |
|
| 307 | + foreach ($whereValues as $name => $value) { |
|
| 308 | + $where->add($updateQb->expr()->eq( |
|
| 309 | + $name, |
|
| 310 | + $updateQb->createNamedParameter($value, $this->getType($value)), |
|
| 311 | + $this->getType($value) |
|
| 312 | + )); |
|
| 313 | + } |
|
| 314 | + $updateQb->where($where); |
|
| 315 | + $affected = $updateQb->execute(); |
|
| 316 | + |
|
| 317 | + if ($affected === 0 && !empty($updatePreconditionValues)) { |
|
| 318 | + throw new PreConditionNotMetException(); |
|
| 319 | + } |
|
| 320 | + |
|
| 321 | + return 0; |
|
| 322 | + } |
|
| 323 | + } |
|
| 324 | + |
|
| 325 | + /** |
|
| 326 | + * Create an exclusive read+write lock on a table |
|
| 327 | + * |
|
| 328 | + * @param string $tableName |
|
| 329 | + * @throws \BadMethodCallException When trying to acquire a second lock |
|
| 330 | + * @since 9.1.0 |
|
| 331 | + */ |
|
| 332 | + public function lockTable($tableName) { |
|
| 333 | + if ($this->lockedTable !== null) { |
|
| 334 | + throw new \BadMethodCallException('Can not lock a new table until the previous lock is released.'); |
|
| 335 | + } |
|
| 336 | + |
|
| 337 | + $tableName = $this->tablePrefix . $tableName; |
|
| 338 | + $this->lockedTable = $tableName; |
|
| 339 | + $this->adapter->lockTable($tableName); |
|
| 340 | + } |
|
| 341 | + |
|
| 342 | + /** |
|
| 343 | + * Release a previous acquired lock again |
|
| 344 | + * |
|
| 345 | + * @since 9.1.0 |
|
| 346 | + */ |
|
| 347 | + public function unlockTable() { |
|
| 348 | + $this->adapter->unlockTable(); |
|
| 349 | + $this->lockedTable = null; |
|
| 350 | + } |
|
| 351 | + |
|
| 352 | + /** |
|
| 353 | + * returns the error code and message as a string for logging |
|
| 354 | + * works with DoctrineException |
|
| 355 | + * @return string |
|
| 356 | + */ |
|
| 357 | + public function getError() { |
|
| 358 | + $msg = $this->errorCode() . ': '; |
|
| 359 | + $errorInfo = $this->errorInfo(); |
|
| 360 | + if (is_array($errorInfo)) { |
|
| 361 | + $msg .= 'SQLSTATE = '.$errorInfo[0] . ', '; |
|
| 362 | + $msg .= 'Driver Code = '.$errorInfo[1] . ', '; |
|
| 363 | + $msg .= 'Driver Message = '.$errorInfo[2]; |
|
| 364 | + } |
|
| 365 | + return $msg; |
|
| 366 | + } |
|
| 367 | + |
|
| 368 | + /** |
|
| 369 | + * Drop a table from the database if it exists |
|
| 370 | + * |
|
| 371 | + * @param string $table table name without the prefix |
|
| 372 | + */ |
|
| 373 | + public function dropTable($table) { |
|
| 374 | + $table = $this->tablePrefix . trim($table); |
|
| 375 | + $schema = $this->getSchemaManager(); |
|
| 376 | + if($schema->tablesExist(array($table))) { |
|
| 377 | + $schema->dropTable($table); |
|
| 378 | + } |
|
| 379 | + } |
|
| 380 | + |
|
| 381 | + /** |
|
| 382 | + * Check if a table exists |
|
| 383 | + * |
|
| 384 | + * @param string $table table name without the prefix |
|
| 385 | + * @return bool |
|
| 386 | + */ |
|
| 387 | + public function tableExists($table){ |
|
| 388 | + $table = $this->tablePrefix . trim($table); |
|
| 389 | + $schema = $this->getSchemaManager(); |
|
| 390 | + return $schema->tablesExist(array($table)); |
|
| 391 | + } |
|
| 392 | + |
|
| 393 | + // internal use |
|
| 394 | + /** |
|
| 395 | + * @param string $statement |
|
| 396 | + * @return string |
|
| 397 | + */ |
|
| 398 | + protected function replaceTablePrefix($statement) { |
|
| 399 | + return str_replace( '*PREFIX*', $this->tablePrefix, $statement ); |
|
| 400 | + } |
|
| 401 | + |
|
| 402 | + /** |
|
| 403 | + * Check if a transaction is active |
|
| 404 | + * |
|
| 405 | + * @return bool |
|
| 406 | + * @since 8.2.0 |
|
| 407 | + */ |
|
| 408 | + public function inTransaction() { |
|
| 409 | + return $this->getTransactionNestingLevel() > 0; |
|
| 410 | + } |
|
| 411 | + |
|
| 412 | + /** |
|
| 413 | + * Escape a parameter to be used in a LIKE query |
|
| 414 | + * |
|
| 415 | + * @param string $param |
|
| 416 | + * @return string |
|
| 417 | + */ |
|
| 418 | + public function escapeLikeParameter($param) { |
|
| 419 | + return addcslashes($param, '\\_%'); |
|
| 420 | + } |
|
| 421 | + |
|
| 422 | + /** |
|
| 423 | + * Check whether or not the current database support 4byte wide unicode |
|
| 424 | + * |
|
| 425 | + * @return bool |
|
| 426 | + * @since 11.0.0 |
|
| 427 | + */ |
|
| 428 | + public function supports4ByteText() { |
|
| 429 | + if (!$this->getDatabasePlatform() instanceof MySqlPlatform) { |
|
| 430 | + return true; |
|
| 431 | + } |
|
| 432 | + return $this->getParams()['charset'] === 'utf8mb4'; |
|
| 433 | + } |
|
| 434 | + |
|
| 435 | + |
|
| 436 | + /** |
|
| 437 | + * Create the schema of the connected database |
|
| 438 | + * |
|
| 439 | + * @return Schema |
|
| 440 | + */ |
|
| 441 | + public function createSchema() { |
|
| 442 | + $schemaManager = new MDB2SchemaManager($this); |
|
| 443 | + $migrator = $schemaManager->getMigrator(); |
|
| 444 | + return $migrator->createSchema(); |
|
| 445 | + } |
|
| 446 | + |
|
| 447 | + /** |
|
| 448 | + * Migrate the database to the given schema |
|
| 449 | + * |
|
| 450 | + * @param Schema $toSchema |
|
| 451 | + */ |
|
| 452 | + public function migrateToSchema(Schema $toSchema) { |
|
| 453 | + $schemaManager = new MDB2SchemaManager($this); |
|
| 454 | + $migrator = $schemaManager->getMigrator(); |
|
| 455 | + $migrator->migrate($toSchema); |
|
| 456 | + } |
|
| 457 | 457 | } |
@@ -39,219 +39,219 @@ |
||
| 39 | 39 | * Takes care of creating and configuring Doctrine connections. |
| 40 | 40 | */ |
| 41 | 41 | class ConnectionFactory { |
| 42 | - /** @var string default database name */ |
|
| 43 | - const DEFAULT_DBNAME = 'owncloud'; |
|
| 42 | + /** @var string default database name */ |
|
| 43 | + const DEFAULT_DBNAME = 'owncloud'; |
|
| 44 | 44 | |
| 45 | - /** @var string default database table prefix */ |
|
| 46 | - const DEFAULT_DBTABLEPREFIX = 'oc_'; |
|
| 45 | + /** @var string default database table prefix */ |
|
| 46 | + const DEFAULT_DBTABLEPREFIX = 'oc_'; |
|
| 47 | 47 | |
| 48 | - /** |
|
| 49 | - * @var array |
|
| 50 | - * |
|
| 51 | - * Array mapping DBMS type to default connection parameters passed to |
|
| 52 | - * \Doctrine\DBAL\DriverManager::getConnection(). |
|
| 53 | - */ |
|
| 54 | - protected $defaultConnectionParams = [ |
|
| 55 | - 'mysql' => [ |
|
| 56 | - 'adapter' => AdapterMySQL::class, |
|
| 57 | - 'charset' => 'UTF8', |
|
| 58 | - 'driver' => 'pdo_mysql', |
|
| 59 | - 'wrapperClass' => Connection::class, |
|
| 60 | - ], |
|
| 61 | - 'oci' => [ |
|
| 62 | - 'adapter' => AdapterOCI8::class, |
|
| 63 | - 'charset' => 'AL32UTF8', |
|
| 64 | - 'driver' => 'oci8', |
|
| 65 | - 'wrapperClass' => OracleConnection::class, |
|
| 66 | - ], |
|
| 67 | - 'pgsql' => [ |
|
| 68 | - 'adapter' => AdapterPgSql::class, |
|
| 69 | - 'driver' => 'pdo_pgsql', |
|
| 70 | - 'wrapperClass' => Connection::class, |
|
| 71 | - ], |
|
| 72 | - 'sqlite3' => [ |
|
| 73 | - 'adapter' => AdapterSqlite::class, |
|
| 74 | - 'driver' => 'pdo_sqlite', |
|
| 75 | - 'wrapperClass' => Connection::class, |
|
| 76 | - ], |
|
| 77 | - ]; |
|
| 48 | + /** |
|
| 49 | + * @var array |
|
| 50 | + * |
|
| 51 | + * Array mapping DBMS type to default connection parameters passed to |
|
| 52 | + * \Doctrine\DBAL\DriverManager::getConnection(). |
|
| 53 | + */ |
|
| 54 | + protected $defaultConnectionParams = [ |
|
| 55 | + 'mysql' => [ |
|
| 56 | + 'adapter' => AdapterMySQL::class, |
|
| 57 | + 'charset' => 'UTF8', |
|
| 58 | + 'driver' => 'pdo_mysql', |
|
| 59 | + 'wrapperClass' => Connection::class, |
|
| 60 | + ], |
|
| 61 | + 'oci' => [ |
|
| 62 | + 'adapter' => AdapterOCI8::class, |
|
| 63 | + 'charset' => 'AL32UTF8', |
|
| 64 | + 'driver' => 'oci8', |
|
| 65 | + 'wrapperClass' => OracleConnection::class, |
|
| 66 | + ], |
|
| 67 | + 'pgsql' => [ |
|
| 68 | + 'adapter' => AdapterPgSql::class, |
|
| 69 | + 'driver' => 'pdo_pgsql', |
|
| 70 | + 'wrapperClass' => Connection::class, |
|
| 71 | + ], |
|
| 72 | + 'sqlite3' => [ |
|
| 73 | + 'adapter' => AdapterSqlite::class, |
|
| 74 | + 'driver' => 'pdo_sqlite', |
|
| 75 | + 'wrapperClass' => Connection::class, |
|
| 76 | + ], |
|
| 77 | + ]; |
|
| 78 | 78 | |
| 79 | - /** @var SystemConfig */ |
|
| 80 | - private $config; |
|
| 79 | + /** @var SystemConfig */ |
|
| 80 | + private $config; |
|
| 81 | 81 | |
| 82 | - /** |
|
| 83 | - * ConnectionFactory constructor. |
|
| 84 | - * |
|
| 85 | - * @param SystemConfig $systemConfig |
|
| 86 | - */ |
|
| 87 | - public function __construct(SystemConfig $systemConfig) { |
|
| 88 | - $this->config = $systemConfig; |
|
| 89 | - if ($this->config->getValue('mysql.utf8mb4', false)) { |
|
| 90 | - $this->defaultConnectionParams['mysql']['charset'] = 'utf8mb4'; |
|
| 91 | - } |
|
| 92 | - } |
|
| 82 | + /** |
|
| 83 | + * ConnectionFactory constructor. |
|
| 84 | + * |
|
| 85 | + * @param SystemConfig $systemConfig |
|
| 86 | + */ |
|
| 87 | + public function __construct(SystemConfig $systemConfig) { |
|
| 88 | + $this->config = $systemConfig; |
|
| 89 | + if ($this->config->getValue('mysql.utf8mb4', false)) { |
|
| 90 | + $this->defaultConnectionParams['mysql']['charset'] = 'utf8mb4'; |
|
| 91 | + } |
|
| 92 | + } |
|
| 93 | 93 | |
| 94 | - /** |
|
| 95 | - * @brief Get default connection parameters for a given DBMS. |
|
| 96 | - * @param string $type DBMS type |
|
| 97 | - * @throws \InvalidArgumentException If $type is invalid |
|
| 98 | - * @return array Default connection parameters. |
|
| 99 | - */ |
|
| 100 | - public function getDefaultConnectionParams($type) { |
|
| 101 | - $normalizedType = $this->normalizeType($type); |
|
| 102 | - if (!isset($this->defaultConnectionParams[$normalizedType])) { |
|
| 103 | - throw new \InvalidArgumentException("Unsupported type: $type"); |
|
| 104 | - } |
|
| 105 | - $result = $this->defaultConnectionParams[$normalizedType]; |
|
| 106 | - // \PDO::MYSQL_ATTR_FOUND_ROWS may not be defined, e.g. when the MySQL |
|
| 107 | - // driver is missing. In this case, we won't be able to connect anyway. |
|
| 108 | - if ($normalizedType === 'mysql' && defined('\PDO::MYSQL_ATTR_FOUND_ROWS')) { |
|
| 109 | - $result['driverOptions'] = array( |
|
| 110 | - \PDO::MYSQL_ATTR_FOUND_ROWS => true, |
|
| 111 | - ); |
|
| 112 | - } |
|
| 113 | - return $result; |
|
| 114 | - } |
|
| 94 | + /** |
|
| 95 | + * @brief Get default connection parameters for a given DBMS. |
|
| 96 | + * @param string $type DBMS type |
|
| 97 | + * @throws \InvalidArgumentException If $type is invalid |
|
| 98 | + * @return array Default connection parameters. |
|
| 99 | + */ |
|
| 100 | + public function getDefaultConnectionParams($type) { |
|
| 101 | + $normalizedType = $this->normalizeType($type); |
|
| 102 | + if (!isset($this->defaultConnectionParams[$normalizedType])) { |
|
| 103 | + throw new \InvalidArgumentException("Unsupported type: $type"); |
|
| 104 | + } |
|
| 105 | + $result = $this->defaultConnectionParams[$normalizedType]; |
|
| 106 | + // \PDO::MYSQL_ATTR_FOUND_ROWS may not be defined, e.g. when the MySQL |
|
| 107 | + // driver is missing. In this case, we won't be able to connect anyway. |
|
| 108 | + if ($normalizedType === 'mysql' && defined('\PDO::MYSQL_ATTR_FOUND_ROWS')) { |
|
| 109 | + $result['driverOptions'] = array( |
|
| 110 | + \PDO::MYSQL_ATTR_FOUND_ROWS => true, |
|
| 111 | + ); |
|
| 112 | + } |
|
| 113 | + return $result; |
|
| 114 | + } |
|
| 115 | 115 | |
| 116 | - /** |
|
| 117 | - * @brief Get default connection parameters for a given DBMS. |
|
| 118 | - * @param string $type DBMS type |
|
| 119 | - * @param array $additionalConnectionParams Additional connection parameters |
|
| 120 | - * @return \OC\DB\Connection |
|
| 121 | - */ |
|
| 122 | - public function getConnection($type, $additionalConnectionParams) { |
|
| 123 | - $normalizedType = $this->normalizeType($type); |
|
| 124 | - $eventManager = new EventManager(); |
|
| 125 | - $eventManager->addEventSubscriber(new SetTransactionIsolationLevel()); |
|
| 126 | - switch ($normalizedType) { |
|
| 127 | - case 'mysql': |
|
| 128 | - $eventManager->addEventSubscriber( |
|
| 129 | - new SQLSessionInit("SET SESSION AUTOCOMMIT=1")); |
|
| 130 | - break; |
|
| 131 | - case 'oci': |
|
| 132 | - $eventManager->addEventSubscriber(new OracleSessionInit); |
|
| 133 | - // the driverOptions are unused in dbal and need to be mapped to the parameters |
|
| 134 | - if (isset($additionalConnectionParams['driverOptions'])) { |
|
| 135 | - $additionalConnectionParams = array_merge($additionalConnectionParams, $additionalConnectionParams['driverOptions']); |
|
| 136 | - } |
|
| 137 | - $host = $additionalConnectionParams['host']; |
|
| 138 | - $port = isset($additionalConnectionParams['port']) ? $additionalConnectionParams['port'] : null; |
|
| 139 | - $dbName = $additionalConnectionParams['dbname']; |
|
| 116 | + /** |
|
| 117 | + * @brief Get default connection parameters for a given DBMS. |
|
| 118 | + * @param string $type DBMS type |
|
| 119 | + * @param array $additionalConnectionParams Additional connection parameters |
|
| 120 | + * @return \OC\DB\Connection |
|
| 121 | + */ |
|
| 122 | + public function getConnection($type, $additionalConnectionParams) { |
|
| 123 | + $normalizedType = $this->normalizeType($type); |
|
| 124 | + $eventManager = new EventManager(); |
|
| 125 | + $eventManager->addEventSubscriber(new SetTransactionIsolationLevel()); |
|
| 126 | + switch ($normalizedType) { |
|
| 127 | + case 'mysql': |
|
| 128 | + $eventManager->addEventSubscriber( |
|
| 129 | + new SQLSessionInit("SET SESSION AUTOCOMMIT=1")); |
|
| 130 | + break; |
|
| 131 | + case 'oci': |
|
| 132 | + $eventManager->addEventSubscriber(new OracleSessionInit); |
|
| 133 | + // the driverOptions are unused in dbal and need to be mapped to the parameters |
|
| 134 | + if (isset($additionalConnectionParams['driverOptions'])) { |
|
| 135 | + $additionalConnectionParams = array_merge($additionalConnectionParams, $additionalConnectionParams['driverOptions']); |
|
| 136 | + } |
|
| 137 | + $host = $additionalConnectionParams['host']; |
|
| 138 | + $port = isset($additionalConnectionParams['port']) ? $additionalConnectionParams['port'] : null; |
|
| 139 | + $dbName = $additionalConnectionParams['dbname']; |
|
| 140 | 140 | |
| 141 | - // we set the connect string as dbname and unset the host to coerce doctrine into using it as connect string |
|
| 142 | - if ($host === '') { |
|
| 143 | - $additionalConnectionParams['dbname'] = $dbName; // use dbname as easy connect name |
|
| 144 | - } else { |
|
| 145 | - $additionalConnectionParams['dbname'] = '//' . $host . (!empty($port) ? ":{$port}" : "") . '/' . $dbName; |
|
| 146 | - } |
|
| 147 | - unset($additionalConnectionParams['host']); |
|
| 148 | - break; |
|
| 141 | + // we set the connect string as dbname and unset the host to coerce doctrine into using it as connect string |
|
| 142 | + if ($host === '') { |
|
| 143 | + $additionalConnectionParams['dbname'] = $dbName; // use dbname as easy connect name |
|
| 144 | + } else { |
|
| 145 | + $additionalConnectionParams['dbname'] = '//' . $host . (!empty($port) ? ":{$port}" : "") . '/' . $dbName; |
|
| 146 | + } |
|
| 147 | + unset($additionalConnectionParams['host']); |
|
| 148 | + break; |
|
| 149 | 149 | |
| 150 | - case 'sqlite3': |
|
| 151 | - $journalMode = $additionalConnectionParams['sqlite.journal_mode']; |
|
| 152 | - $additionalConnectionParams['platform'] = new OCSqlitePlatform(); |
|
| 153 | - $eventManager->addEventSubscriber(new SQLiteSessionInit(true, $journalMode)); |
|
| 154 | - break; |
|
| 155 | - } |
|
| 156 | - /** @var Connection $connection */ |
|
| 157 | - $connection = DriverManager::getConnection( |
|
| 158 | - array_merge($this->getDefaultConnectionParams($type), $additionalConnectionParams), |
|
| 159 | - new Configuration(), |
|
| 160 | - $eventManager |
|
| 161 | - ); |
|
| 162 | - return $connection; |
|
| 163 | - } |
|
| 150 | + case 'sqlite3': |
|
| 151 | + $journalMode = $additionalConnectionParams['sqlite.journal_mode']; |
|
| 152 | + $additionalConnectionParams['platform'] = new OCSqlitePlatform(); |
|
| 153 | + $eventManager->addEventSubscriber(new SQLiteSessionInit(true, $journalMode)); |
|
| 154 | + break; |
|
| 155 | + } |
|
| 156 | + /** @var Connection $connection */ |
|
| 157 | + $connection = DriverManager::getConnection( |
|
| 158 | + array_merge($this->getDefaultConnectionParams($type), $additionalConnectionParams), |
|
| 159 | + new Configuration(), |
|
| 160 | + $eventManager |
|
| 161 | + ); |
|
| 162 | + return $connection; |
|
| 163 | + } |
|
| 164 | 164 | |
| 165 | - /** |
|
| 166 | - * @brief Normalize DBMS type |
|
| 167 | - * @param string $type DBMS type |
|
| 168 | - * @return string Normalized DBMS type |
|
| 169 | - */ |
|
| 170 | - public function normalizeType($type) { |
|
| 171 | - return $type === 'sqlite' ? 'sqlite3' : $type; |
|
| 172 | - } |
|
| 165 | + /** |
|
| 166 | + * @brief Normalize DBMS type |
|
| 167 | + * @param string $type DBMS type |
|
| 168 | + * @return string Normalized DBMS type |
|
| 169 | + */ |
|
| 170 | + public function normalizeType($type) { |
|
| 171 | + return $type === 'sqlite' ? 'sqlite3' : $type; |
|
| 172 | + } |
|
| 173 | 173 | |
| 174 | - /** |
|
| 175 | - * Checks whether the specified DBMS type is valid. |
|
| 176 | - * |
|
| 177 | - * @param string $type |
|
| 178 | - * @return bool |
|
| 179 | - */ |
|
| 180 | - public function isValidType($type) { |
|
| 181 | - $normalizedType = $this->normalizeType($type); |
|
| 182 | - return isset($this->defaultConnectionParams[$normalizedType]); |
|
| 183 | - } |
|
| 174 | + /** |
|
| 175 | + * Checks whether the specified DBMS type is valid. |
|
| 176 | + * |
|
| 177 | + * @param string $type |
|
| 178 | + * @return bool |
|
| 179 | + */ |
|
| 180 | + public function isValidType($type) { |
|
| 181 | + $normalizedType = $this->normalizeType($type); |
|
| 182 | + return isset($this->defaultConnectionParams[$normalizedType]); |
|
| 183 | + } |
|
| 184 | 184 | |
| 185 | - /** |
|
| 186 | - * Create the connection parameters for the config |
|
| 187 | - * |
|
| 188 | - * @return array |
|
| 189 | - */ |
|
| 190 | - public function createConnectionParams() { |
|
| 191 | - $type = $this->config->getValue('dbtype', 'sqlite'); |
|
| 185 | + /** |
|
| 186 | + * Create the connection parameters for the config |
|
| 187 | + * |
|
| 188 | + * @return array |
|
| 189 | + */ |
|
| 190 | + public function createConnectionParams() { |
|
| 191 | + $type = $this->config->getValue('dbtype', 'sqlite'); |
|
| 192 | 192 | |
| 193 | - $connectionParams = [ |
|
| 194 | - 'user' => $this->config->getValue('dbuser', ''), |
|
| 195 | - 'password' => $this->config->getValue('dbpassword', ''), |
|
| 196 | - ]; |
|
| 197 | - $name = $this->config->getValue('dbname', self::DEFAULT_DBNAME); |
|
| 193 | + $connectionParams = [ |
|
| 194 | + 'user' => $this->config->getValue('dbuser', ''), |
|
| 195 | + 'password' => $this->config->getValue('dbpassword', ''), |
|
| 196 | + ]; |
|
| 197 | + $name = $this->config->getValue('dbname', self::DEFAULT_DBNAME); |
|
| 198 | 198 | |
| 199 | - if ($this->normalizeType($type) === 'sqlite3') { |
|
| 200 | - $dataDir = $this->config->getValue("datadirectory", \OC::$SERVERROOT . '/data'); |
|
| 201 | - $connectionParams['path'] = $dataDir . '/' . $name . '.db'; |
|
| 202 | - } else { |
|
| 203 | - $host = $this->config->getValue('dbhost', ''); |
|
| 204 | - $connectionParams = array_merge($connectionParams, $this->splitHostFromPortAndSocket($host)); |
|
| 205 | - $connectionParams['dbname'] = $name; |
|
| 206 | - } |
|
| 199 | + if ($this->normalizeType($type) === 'sqlite3') { |
|
| 200 | + $dataDir = $this->config->getValue("datadirectory", \OC::$SERVERROOT . '/data'); |
|
| 201 | + $connectionParams['path'] = $dataDir . '/' . $name . '.db'; |
|
| 202 | + } else { |
|
| 203 | + $host = $this->config->getValue('dbhost', ''); |
|
| 204 | + $connectionParams = array_merge($connectionParams, $this->splitHostFromPortAndSocket($host)); |
|
| 205 | + $connectionParams['dbname'] = $name; |
|
| 206 | + } |
|
| 207 | 207 | |
| 208 | - $connectionParams['tablePrefix'] = $this->config->getValue('dbtableprefix', self::DEFAULT_DBTABLEPREFIX); |
|
| 209 | - $connectionParams['sqlite.journal_mode'] = $this->config->getValue('sqlite.journal_mode', 'WAL'); |
|
| 208 | + $connectionParams['tablePrefix'] = $this->config->getValue('dbtableprefix', self::DEFAULT_DBTABLEPREFIX); |
|
| 209 | + $connectionParams['sqlite.journal_mode'] = $this->config->getValue('sqlite.journal_mode', 'WAL'); |
|
| 210 | 210 | |
| 211 | - //additional driver options, eg. for mysql ssl |
|
| 212 | - $driverOptions = $this->config->getValue('dbdriveroptions', null); |
|
| 213 | - if ($driverOptions) { |
|
| 214 | - $connectionParams['driverOptions'] = $driverOptions; |
|
| 215 | - } |
|
| 211 | + //additional driver options, eg. for mysql ssl |
|
| 212 | + $driverOptions = $this->config->getValue('dbdriveroptions', null); |
|
| 213 | + if ($driverOptions) { |
|
| 214 | + $connectionParams['driverOptions'] = $driverOptions; |
|
| 215 | + } |
|
| 216 | 216 | |
| 217 | - // set default table creation options |
|
| 218 | - $connectionParams['defaultTableOptions'] = [ |
|
| 219 | - 'collate' => 'utf8_bin', |
|
| 220 | - 'tablePrefix' => $connectionParams['tablePrefix'] |
|
| 221 | - ]; |
|
| 217 | + // set default table creation options |
|
| 218 | + $connectionParams['defaultTableOptions'] = [ |
|
| 219 | + 'collate' => 'utf8_bin', |
|
| 220 | + 'tablePrefix' => $connectionParams['tablePrefix'] |
|
| 221 | + ]; |
|
| 222 | 222 | |
| 223 | - if ($this->config->getValue('mysql.utf8mb4', false)) { |
|
| 224 | - $connectionParams['defaultTableOptions'] = [ |
|
| 225 | - 'collate' => 'utf8mb4_bin', |
|
| 226 | - 'charset' => 'utf8mb4', |
|
| 227 | - 'row_format' => 'compressed', |
|
| 228 | - 'tablePrefix' => $connectionParams['tablePrefix'] |
|
| 229 | - ]; |
|
| 230 | - } |
|
| 223 | + if ($this->config->getValue('mysql.utf8mb4', false)) { |
|
| 224 | + $connectionParams['defaultTableOptions'] = [ |
|
| 225 | + 'collate' => 'utf8mb4_bin', |
|
| 226 | + 'charset' => 'utf8mb4', |
|
| 227 | + 'row_format' => 'compressed', |
|
| 228 | + 'tablePrefix' => $connectionParams['tablePrefix'] |
|
| 229 | + ]; |
|
| 230 | + } |
|
| 231 | 231 | |
| 232 | - return $connectionParams; |
|
| 233 | - } |
|
| 232 | + return $connectionParams; |
|
| 233 | + } |
|
| 234 | 234 | |
| 235 | - /** |
|
| 236 | - * @param string $host |
|
| 237 | - * @return array |
|
| 238 | - */ |
|
| 239 | - protected function splitHostFromPortAndSocket($host): array { |
|
| 240 | - $params = [ |
|
| 241 | - 'host' => $host, |
|
| 242 | - ]; |
|
| 235 | + /** |
|
| 236 | + * @param string $host |
|
| 237 | + * @return array |
|
| 238 | + */ |
|
| 239 | + protected function splitHostFromPortAndSocket($host): array { |
|
| 240 | + $params = [ |
|
| 241 | + 'host' => $host, |
|
| 242 | + ]; |
|
| 243 | 243 | |
| 244 | - $matches = []; |
|
| 245 | - if (preg_match('/^(.*):([^\]:]+)$/', $host, $matches)) { |
|
| 246 | - // Host variable carries a port or socket. |
|
| 247 | - $params['host'] = $matches[1]; |
|
| 248 | - if (is_numeric($matches[2])) { |
|
| 249 | - $params['port'] = (int) $matches[2]; |
|
| 250 | - } else { |
|
| 251 | - $params['unix_socket'] = $matches[2]; |
|
| 252 | - } |
|
| 253 | - } |
|
| 244 | + $matches = []; |
|
| 245 | + if (preg_match('/^(.*):([^\]:]+)$/', $host, $matches)) { |
|
| 246 | + // Host variable carries a port or socket. |
|
| 247 | + $params['host'] = $matches[1]; |
|
| 248 | + if (is_numeric($matches[2])) { |
|
| 249 | + $params['port'] = (int) $matches[2]; |
|
| 250 | + } else { |
|
| 251 | + $params['unix_socket'] = $matches[2]; |
|
| 252 | + } |
|
| 253 | + } |
|
| 254 | 254 | |
| 255 | - return $params; |
|
| 256 | - } |
|
| 255 | + return $params; |
|
| 256 | + } |
|
| 257 | 257 | } |
@@ -32,15 +32,15 @@ |
||
| 32 | 32 | use Doctrine\DBAL\TransactionIsolationLevel; |
| 33 | 33 | |
| 34 | 34 | class SetTransactionIsolationLevel implements EventSubscriber { |
| 35 | - /** |
|
| 36 | - * @param ConnectionEventArgs $args |
|
| 37 | - * @return void |
|
| 38 | - */ |
|
| 39 | - public function postConnect(ConnectionEventArgs $args) { |
|
| 40 | - $args->getConnection()->setTransactionIsolation(TransactionIsolationLevel::READ_COMMITTED); |
|
| 41 | - } |
|
| 35 | + /** |
|
| 36 | + * @param ConnectionEventArgs $args |
|
| 37 | + * @return void |
|
| 38 | + */ |
|
| 39 | + public function postConnect(ConnectionEventArgs $args) { |
|
| 40 | + $args->getConnection()->setTransactionIsolation(TransactionIsolationLevel::READ_COMMITTED); |
|
| 41 | + } |
|
| 42 | 42 | |
| 43 | - public function getSubscribedEvents() { |
|
| 44 | - return [Events::postConnect]; |
|
| 45 | - } |
|
| 43 | + public function getSubscribedEvents() { |
|
| 44 | + return [Events::postConnect]; |
|
| 45 | + } |
|
| 46 | 46 | } |