@@ -24,368 +24,368 @@ |
||
| 24 | 24 | * @since 6.0.0 |
| 25 | 25 | */ |
| 26 | 26 | interface IDBConnection { |
| 27 | - /** |
|
| 28 | - * @since 28.0.0 |
|
| 29 | - */ |
|
| 30 | - public const PLATFORM_MYSQL = 'mysql'; |
|
| 31 | - |
|
| 32 | - /** |
|
| 33 | - * @since 28.0.0 |
|
| 34 | - */ |
|
| 35 | - public const PLATFORM_ORACLE = 'oracle'; |
|
| 36 | - |
|
| 37 | - /** |
|
| 38 | - * @since 28.0.0 |
|
| 39 | - */ |
|
| 40 | - public const PLATFORM_POSTGRES = 'postgres'; |
|
| 41 | - |
|
| 42 | - /** |
|
| 43 | - * @since 28.0.0 |
|
| 44 | - */ |
|
| 45 | - public const PLATFORM_SQLITE = 'sqlite'; |
|
| 46 | - |
|
| 47 | - /** |
|
| 48 | - * @since 32.0.0 |
|
| 49 | - */ |
|
| 50 | - public const PLATFORM_MARIADB = 'mariadb'; |
|
| 51 | - |
|
| 52 | - /** |
|
| 53 | - * Gets the QueryBuilder for the connection. |
|
| 54 | - * |
|
| 55 | - * @return \OCP\DB\QueryBuilder\IQueryBuilder |
|
| 56 | - * @since 8.2.0 |
|
| 57 | - */ |
|
| 58 | - public function getQueryBuilder(); |
|
| 59 | - |
|
| 60 | - /** |
|
| 61 | - * Used to abstract the Nextcloud database access away |
|
| 62 | - * @param string $sql the sql query with ? placeholder for params |
|
| 63 | - * @param int|null $limit the maximum number of rows |
|
| 64 | - * @param int|null $offset from which row we want to start |
|
| 65 | - * @return IPreparedStatement The prepared statement. |
|
| 66 | - * @since 6.0.0 |
|
| 67 | - * @throws Exception since 21.0.0 |
|
| 68 | - * |
|
| 69 | - * @psalm-taint-sink sql $sql |
|
| 70 | - */ |
|
| 71 | - public function prepare($sql, $limit = null, $offset = null): IPreparedStatement; |
|
| 72 | - |
|
| 73 | - /** |
|
| 74 | - * Executes an, optionally parameterized, SQL query. |
|
| 75 | - * |
|
| 76 | - * If the query is parameterized, a prepared statement is used. |
|
| 77 | - * If an SQLLogger is configured, the execution is logged. |
|
| 78 | - * |
|
| 79 | - * @param string $sql The SQL query to execute. |
|
| 80 | - * @param string[] $params The parameters to bind to the query, if any. |
|
| 81 | - * @param array $types The types the previous parameters are in. |
|
| 82 | - * @return IResult The executed statement. |
|
| 83 | - * @since 8.0.0 |
|
| 84 | - * @throws Exception since 21.0.0 |
|
| 85 | - * |
|
| 86 | - * @psalm-taint-sink sql $sql |
|
| 87 | - */ |
|
| 88 | - public function executeQuery(string $sql, array $params = [], $types = []): IResult; |
|
| 89 | - |
|
| 90 | - /** |
|
| 91 | - * Executes an SQL INSERT/UPDATE/DELETE query with the given parameters |
|
| 92 | - * and returns the number of affected rows. |
|
| 93 | - * |
|
| 94 | - * This method supports PDO binding types as well as DBAL mapping types. |
|
| 95 | - * |
|
| 96 | - * @param string $sql The SQL query. |
|
| 97 | - * @param array $params The query parameters. |
|
| 98 | - * @param array $types The parameter types. |
|
| 99 | - * @return int The number of affected rows. |
|
| 100 | - * @since 8.0.0 |
|
| 101 | - * @throws Exception since 21.0.0 |
|
| 102 | - * |
|
| 103 | - * @deprecated 21.0.0 use executeStatement |
|
| 104 | - * |
|
| 105 | - * @psalm-taint-sink sql $sql |
|
| 106 | - */ |
|
| 107 | - public function executeUpdate(string $sql, array $params = [], array $types = []): int; |
|
| 108 | - |
|
| 109 | - /** |
|
| 110 | - * Executes an SQL INSERT/UPDATE/DELETE query with the given parameters |
|
| 111 | - * and returns the number of affected rows. |
|
| 112 | - * |
|
| 113 | - * This method supports PDO binding types as well as DBAL mapping types. |
|
| 114 | - * |
|
| 115 | - * @param string $sql The SQL query. |
|
| 116 | - * @param array $params The query parameters. |
|
| 117 | - * @param array $types The parameter types. |
|
| 118 | - * @return int The number of affected rows. |
|
| 119 | - * @since 21.0.0 |
|
| 120 | - * @throws Exception since 21.0.0 |
|
| 121 | - * |
|
| 122 | - * @psalm-taint-sink sql $sql |
|
| 123 | - */ |
|
| 124 | - public function executeStatement($sql, array $params = [], array $types = []): int; |
|
| 125 | - |
|
| 126 | - /** |
|
| 127 | - * Used to get the id of the just inserted element |
|
| 128 | - * @param string $table the name of the table where we inserted the item |
|
| 129 | - * @return int the id of the inserted element |
|
| 130 | - * @since 6.0.0 |
|
| 131 | - * @throws Exception since 21.0.0 |
|
| 132 | - * @deprecated 21.0.0 use \OCP\DB\QueryBuilder\IQueryBuilder::getLastInsertId |
|
| 133 | - */ |
|
| 134 | - public function lastInsertId(string $table): int; |
|
| 135 | - |
|
| 136 | - /** |
|
| 137 | - * Insert a row if the matching row does not exists. To accomplish proper race condition avoidance |
|
| 138 | - * it is needed that there is also a unique constraint on the values. Then this method will |
|
| 139 | - * catch the exception and return 0. |
|
| 140 | - * |
|
| 141 | - * @param string $table The table name (will replace *PREFIX* with the actual prefix) |
|
| 142 | - * @param array $input data that should be inserted into the table (column name => value) |
|
| 143 | - * @param array|null $compare List of values that should be checked for "if not exists" |
|
| 144 | - * If this is null or an empty array, all keys of $input will be compared |
|
| 145 | - * Please note: text fields (clob) must not be used in the compare array |
|
| 146 | - * @return int number of inserted rows |
|
| 147 | - * @throws Exception used to be the removed dbal exception, since 21.0.0 it's \OCP\DB\Exception |
|
| 148 | - * @since 6.0.0 - parameter $compare was added in 8.1.0, return type changed from boolean in 8.1.0 |
|
| 149 | - * @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 |
|
| 150 | - */ |
|
| 151 | - public function insertIfNotExist(string $table, array $input, ?array $compare = null); |
|
| 152 | - |
|
| 153 | - |
|
| 154 | - /** |
|
| 155 | - * |
|
| 156 | - * Insert a row if the row does not exist. Eventual conflicts during insert will be ignored. |
|
| 157 | - * |
|
| 158 | - * Implementation is not fully finished and should not be used! |
|
| 159 | - * |
|
| 160 | - * @param string $table The table name (will replace *PREFIX* with the actual prefix) |
|
| 161 | - * @param array $values data that should be inserted into the table (column name => value) |
|
| 162 | - * @return int number of inserted rows |
|
| 163 | - * @since 16.0.0 |
|
| 164 | - */ |
|
| 165 | - public function insertIgnoreConflict(string $table, array $values) : int; |
|
| 166 | - |
|
| 167 | - /** |
|
| 168 | - * Insert or update a row value |
|
| 169 | - * |
|
| 170 | - * @param string $table |
|
| 171 | - * @param array $keys (column name => value) |
|
| 172 | - * @param array $values (column name => value) |
|
| 173 | - * @param array $updatePreconditionValues ensure values match preconditions (column name => value) |
|
| 174 | - * @return int number of new rows |
|
| 175 | - * @throws Exception used to be the removed dbal exception, since 21.0.0 it's \OCP\DB\Exception |
|
| 176 | - * @throws PreConditionNotMetException |
|
| 177 | - * @since 9.0.0 |
|
| 178 | - */ |
|
| 179 | - public function setValues($table, array $keys, array $values, array $updatePreconditionValues = []): int; |
|
| 180 | - |
|
| 181 | - /** |
|
| 182 | - * Create an exclusive read+write lock on a table |
|
| 183 | - * |
|
| 184 | - * Important Note: Due to the nature how locks work on different DBs, it is |
|
| 185 | - * only possible to lock one table at a time. You should also NOT start a |
|
| 186 | - * transaction while holding a lock. |
|
| 187 | - * |
|
| 188 | - * @param string $tableName |
|
| 189 | - * @throws Exception since 21.0.0 |
|
| 190 | - * @since 9.1.0 |
|
| 191 | - */ |
|
| 192 | - public function lockTable($tableName): void; |
|
| 193 | - |
|
| 194 | - /** |
|
| 195 | - * Release a previous acquired lock again |
|
| 196 | - * |
|
| 197 | - * @throws Exception since 21.0.0 |
|
| 198 | - * @since 9.1.0 |
|
| 199 | - */ |
|
| 200 | - public function unlockTable(): void; |
|
| 201 | - |
|
| 202 | - /** |
|
| 203 | - * Start a transaction |
|
| 204 | - * @since 6.0.0 |
|
| 205 | - * @throws Exception since 21.0.0 |
|
| 206 | - */ |
|
| 207 | - public function beginTransaction(): void; |
|
| 208 | - |
|
| 209 | - /** |
|
| 210 | - * Check if a transaction is active |
|
| 211 | - * |
|
| 212 | - * @return bool |
|
| 213 | - * @since 8.2.0 |
|
| 214 | - */ |
|
| 215 | - public function inTransaction(): bool; |
|
| 216 | - |
|
| 217 | - /** |
|
| 218 | - * Commit the database changes done during a transaction that is in progress |
|
| 219 | - * @since 6.0.0 |
|
| 220 | - * @throws Exception since 21.0.0 |
|
| 221 | - */ |
|
| 222 | - public function commit(): void; |
|
| 223 | - |
|
| 224 | - /** |
|
| 225 | - * Rollback the database changes done during a transaction that is in progress |
|
| 226 | - * @since 6.0.0 |
|
| 227 | - * @throws Exception since 21.0.0 |
|
| 228 | - */ |
|
| 229 | - public function rollBack(): void; |
|
| 230 | - |
|
| 231 | - /** |
|
| 232 | - * Gets the error code and message as a string for logging |
|
| 233 | - * @return string |
|
| 234 | - * @since 6.0.0 |
|
| 235 | - * @deprecated 21.0.0 doesn't return anything meaningful |
|
| 236 | - */ |
|
| 237 | - public function getError(): string; |
|
| 238 | - |
|
| 239 | - /** |
|
| 240 | - * Fetch the SQLSTATE associated with the last database operation. |
|
| 241 | - * |
|
| 242 | - * @return integer The last error code. |
|
| 243 | - * @since 8.0.0 |
|
| 244 | - * @deprecated 21.0.0 doesn't return anything anymore |
|
| 245 | - */ |
|
| 246 | - public function errorCode(); |
|
| 247 | - |
|
| 248 | - /** |
|
| 249 | - * Fetch extended error information associated with the last database operation. |
|
| 250 | - * |
|
| 251 | - * @return array The last error information. |
|
| 252 | - * @since 8.0.0 |
|
| 253 | - * @deprecated 21.0.0 doesn't return anything anymore |
|
| 254 | - */ |
|
| 255 | - public function errorInfo(); |
|
| 256 | - |
|
| 257 | - /** |
|
| 258 | - * Establishes the connection with the database. |
|
| 259 | - * |
|
| 260 | - * @return bool |
|
| 261 | - * @throws Exception since 21.0.0 |
|
| 262 | - * @since 8.0.0 |
|
| 263 | - */ |
|
| 264 | - public function connect(): bool; |
|
| 265 | - |
|
| 266 | - /** |
|
| 267 | - * Close the database connection |
|
| 268 | - * @since 8.0.0 |
|
| 269 | - */ |
|
| 270 | - public function close(): void; |
|
| 271 | - |
|
| 272 | - /** |
|
| 273 | - * Quotes a given input parameter. |
|
| 274 | - * |
|
| 275 | - * @param mixed $input Parameter to be quoted. |
|
| 276 | - * @param int $type Type of the parameter. |
|
| 277 | - * @return mixed The quoted parameter. |
|
| 278 | - * @since 8.0.0 |
|
| 279 | - */ |
|
| 280 | - public function quote($input, $type = IQueryBuilder::PARAM_STR); |
|
| 281 | - |
|
| 282 | - /** |
|
| 283 | - * Gets the DatabasePlatform instance that provides all the metadata about |
|
| 284 | - * the platform this driver connects to. |
|
| 285 | - * |
|
| 286 | - * @return \Doctrine\DBAL\Platforms\AbstractPlatform The database platform. |
|
| 287 | - * @since 8.0.0 |
|
| 288 | - * @deprecated 30.0.0 Please use {@see self::getDatabaseProvider()} and compare to self::PLATFORM_* constants |
|
| 289 | - */ |
|
| 290 | - public function getDatabasePlatform(); |
|
| 291 | - |
|
| 292 | - /** |
|
| 293 | - * Drop a table from the database if it exists |
|
| 294 | - * |
|
| 295 | - * @param string $table table name without the prefix |
|
| 296 | - * @throws Exception since 21.0.0 |
|
| 297 | - * @since 8.0.0 |
|
| 298 | - * |
|
| 299 | - * @psalm-taint-sink sql $table |
|
| 300 | - */ |
|
| 301 | - public function dropTable(string $table): void; |
|
| 302 | - |
|
| 303 | - /** |
|
| 304 | - * Truncate a table data if it exists |
|
| 305 | - * |
|
| 306 | - * Cascade is not supported on many platforms but would optionally cascade the truncate by |
|
| 307 | - * following the foreign keys. |
|
| 308 | - * |
|
| 309 | - * @param string $table table name without the prefix |
|
| 310 | - * @param bool $cascade whether to truncate cascading |
|
| 311 | - * @throws Exception |
|
| 312 | - * @since 32.0.0 |
|
| 313 | - * |
|
| 314 | - * @psalm-taint-sink sql $table |
|
| 315 | - */ |
|
| 316 | - public function truncateTable(string $table, bool $cascade): void; |
|
| 317 | - |
|
| 318 | - /** |
|
| 319 | - * Check if a table exists |
|
| 320 | - * |
|
| 321 | - * @param string $table table name without the prefix |
|
| 322 | - * @return bool |
|
| 323 | - * @throws Exception since 21.0.0 |
|
| 324 | - * @since 8.0.0 |
|
| 325 | - */ |
|
| 326 | - public function tableExists(string $table): bool; |
|
| 327 | - |
|
| 328 | - /** |
|
| 329 | - * Escape a parameter to be used in a LIKE query |
|
| 330 | - * |
|
| 331 | - * @param string $param |
|
| 332 | - * @return string |
|
| 333 | - * @since 9.0.0 |
|
| 334 | - */ |
|
| 335 | - public function escapeLikeParameter(string $param): string; |
|
| 336 | - |
|
| 337 | - /** |
|
| 338 | - * Check whether or not the current database support 4byte wide unicode |
|
| 339 | - * |
|
| 340 | - * @return bool |
|
| 341 | - * @since 11.0.0 |
|
| 342 | - */ |
|
| 343 | - public function supports4ByteText(): bool; |
|
| 344 | - |
|
| 345 | - /** |
|
| 346 | - * Create the schema of the connected database |
|
| 347 | - * |
|
| 348 | - * @return Schema |
|
| 349 | - * @throws Exception since 21.0.0 |
|
| 350 | - * @since 13.0.0 |
|
| 351 | - */ |
|
| 352 | - public function createSchema(): Schema; |
|
| 353 | - |
|
| 354 | - /** |
|
| 355 | - * Migrate the database to the given schema |
|
| 356 | - * |
|
| 357 | - * @param Schema $toSchema |
|
| 358 | - * @throws Exception since 21.0.0 |
|
| 359 | - * @since 13.0.0 |
|
| 360 | - */ |
|
| 361 | - public function migrateToSchema(Schema $toSchema): void; |
|
| 362 | - |
|
| 363 | - /** |
|
| 364 | - * Returns the database provider name |
|
| 365 | - * |
|
| 366 | - * @link https://github.com/nextcloud/server/issues/30877 |
|
| 367 | - * |
|
| 368 | - * @param bool $strict differentiate between database flavors, e.g. MySQL vs MariaDB |
|
| 369 | - * @return self::PLATFORM_MYSQL|self::PLATFORM_ORACLE|self::PLATFORM_POSTGRES|self::PLATFORM_SQLITE|self::PLATFORM_MARIADB |
|
| 370 | - * @since 32.0.0 Optional parameter $strict was added |
|
| 371 | - * @since 28.0.0 |
|
| 372 | - */ |
|
| 373 | - public function getDatabaseProvider(bool $strict = false): string; |
|
| 374 | - |
|
| 375 | - /** |
|
| 376 | - * Get the shard definition by name, if configured |
|
| 377 | - * |
|
| 378 | - * @param string $name |
|
| 379 | - * @return ShardDefinition|null |
|
| 380 | - * @since 30.0.0 |
|
| 381 | - */ |
|
| 382 | - public function getShardDefinition(string $name): ?ShardDefinition; |
|
| 383 | - |
|
| 384 | - /** |
|
| 385 | - * Get a helper class for implementing cross-shard moves |
|
| 386 | - * |
|
| 387 | - * @return CrossShardMoveHelper |
|
| 388 | - * @since 30.0.0 |
|
| 389 | - */ |
|
| 390 | - public function getCrossShardMoveHelper(): CrossShardMoveHelper; |
|
| 27 | + /** |
|
| 28 | + * @since 28.0.0 |
|
| 29 | + */ |
|
| 30 | + public const PLATFORM_MYSQL = 'mysql'; |
|
| 31 | + |
|
| 32 | + /** |
|
| 33 | + * @since 28.0.0 |
|
| 34 | + */ |
|
| 35 | + public const PLATFORM_ORACLE = 'oracle'; |
|
| 36 | + |
|
| 37 | + /** |
|
| 38 | + * @since 28.0.0 |
|
| 39 | + */ |
|
| 40 | + public const PLATFORM_POSTGRES = 'postgres'; |
|
| 41 | + |
|
| 42 | + /** |
|
| 43 | + * @since 28.0.0 |
|
| 44 | + */ |
|
| 45 | + public const PLATFORM_SQLITE = 'sqlite'; |
|
| 46 | + |
|
| 47 | + /** |
|
| 48 | + * @since 32.0.0 |
|
| 49 | + */ |
|
| 50 | + public const PLATFORM_MARIADB = 'mariadb'; |
|
| 51 | + |
|
| 52 | + /** |
|
| 53 | + * Gets the QueryBuilder for the connection. |
|
| 54 | + * |
|
| 55 | + * @return \OCP\DB\QueryBuilder\IQueryBuilder |
|
| 56 | + * @since 8.2.0 |
|
| 57 | + */ |
|
| 58 | + public function getQueryBuilder(); |
|
| 59 | + |
|
| 60 | + /** |
|
| 61 | + * Used to abstract the Nextcloud database access away |
|
| 62 | + * @param string $sql the sql query with ? placeholder for params |
|
| 63 | + * @param int|null $limit the maximum number of rows |
|
| 64 | + * @param int|null $offset from which row we want to start |
|
| 65 | + * @return IPreparedStatement The prepared statement. |
|
| 66 | + * @since 6.0.0 |
|
| 67 | + * @throws Exception since 21.0.0 |
|
| 68 | + * |
|
| 69 | + * @psalm-taint-sink sql $sql |
|
| 70 | + */ |
|
| 71 | + public function prepare($sql, $limit = null, $offset = null): IPreparedStatement; |
|
| 72 | + |
|
| 73 | + /** |
|
| 74 | + * Executes an, optionally parameterized, SQL query. |
|
| 75 | + * |
|
| 76 | + * If the query is parameterized, a prepared statement is used. |
|
| 77 | + * If an SQLLogger is configured, the execution is logged. |
|
| 78 | + * |
|
| 79 | + * @param string $sql The SQL query to execute. |
|
| 80 | + * @param string[] $params The parameters to bind to the query, if any. |
|
| 81 | + * @param array $types The types the previous parameters are in. |
|
| 82 | + * @return IResult The executed statement. |
|
| 83 | + * @since 8.0.0 |
|
| 84 | + * @throws Exception since 21.0.0 |
|
| 85 | + * |
|
| 86 | + * @psalm-taint-sink sql $sql |
|
| 87 | + */ |
|
| 88 | + public function executeQuery(string $sql, array $params = [], $types = []): IResult; |
|
| 89 | + |
|
| 90 | + /** |
|
| 91 | + * Executes an SQL INSERT/UPDATE/DELETE query with the given parameters |
|
| 92 | + * and returns the number of affected rows. |
|
| 93 | + * |
|
| 94 | + * This method supports PDO binding types as well as DBAL mapping types. |
|
| 95 | + * |
|
| 96 | + * @param string $sql The SQL query. |
|
| 97 | + * @param array $params The query parameters. |
|
| 98 | + * @param array $types The parameter types. |
|
| 99 | + * @return int The number of affected rows. |
|
| 100 | + * @since 8.0.0 |
|
| 101 | + * @throws Exception since 21.0.0 |
|
| 102 | + * |
|
| 103 | + * @deprecated 21.0.0 use executeStatement |
|
| 104 | + * |
|
| 105 | + * @psalm-taint-sink sql $sql |
|
| 106 | + */ |
|
| 107 | + public function executeUpdate(string $sql, array $params = [], array $types = []): int; |
|
| 108 | + |
|
| 109 | + /** |
|
| 110 | + * Executes an SQL INSERT/UPDATE/DELETE query with the given parameters |
|
| 111 | + * and returns the number of affected rows. |
|
| 112 | + * |
|
| 113 | + * This method supports PDO binding types as well as DBAL mapping types. |
|
| 114 | + * |
|
| 115 | + * @param string $sql The SQL query. |
|
| 116 | + * @param array $params The query parameters. |
|
| 117 | + * @param array $types The parameter types. |
|
| 118 | + * @return int The number of affected rows. |
|
| 119 | + * @since 21.0.0 |
|
| 120 | + * @throws Exception since 21.0.0 |
|
| 121 | + * |
|
| 122 | + * @psalm-taint-sink sql $sql |
|
| 123 | + */ |
|
| 124 | + public function executeStatement($sql, array $params = [], array $types = []): int; |
|
| 125 | + |
|
| 126 | + /** |
|
| 127 | + * Used to get the id of the just inserted element |
|
| 128 | + * @param string $table the name of the table where we inserted the item |
|
| 129 | + * @return int the id of the inserted element |
|
| 130 | + * @since 6.0.0 |
|
| 131 | + * @throws Exception since 21.0.0 |
|
| 132 | + * @deprecated 21.0.0 use \OCP\DB\QueryBuilder\IQueryBuilder::getLastInsertId |
|
| 133 | + */ |
|
| 134 | + public function lastInsertId(string $table): int; |
|
| 135 | + |
|
| 136 | + /** |
|
| 137 | + * Insert a row if the matching row does not exists. To accomplish proper race condition avoidance |
|
| 138 | + * it is needed that there is also a unique constraint on the values. Then this method will |
|
| 139 | + * catch the exception and return 0. |
|
| 140 | + * |
|
| 141 | + * @param string $table The table name (will replace *PREFIX* with the actual prefix) |
|
| 142 | + * @param array $input data that should be inserted into the table (column name => value) |
|
| 143 | + * @param array|null $compare List of values that should be checked for "if not exists" |
|
| 144 | + * If this is null or an empty array, all keys of $input will be compared |
|
| 145 | + * Please note: text fields (clob) must not be used in the compare array |
|
| 146 | + * @return int number of inserted rows |
|
| 147 | + * @throws Exception used to be the removed dbal exception, since 21.0.0 it's \OCP\DB\Exception |
|
| 148 | + * @since 6.0.0 - parameter $compare was added in 8.1.0, return type changed from boolean in 8.1.0 |
|
| 149 | + * @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 |
|
| 150 | + */ |
|
| 151 | + public function insertIfNotExist(string $table, array $input, ?array $compare = null); |
|
| 152 | + |
|
| 153 | + |
|
| 154 | + /** |
|
| 155 | + * |
|
| 156 | + * Insert a row if the row does not exist. Eventual conflicts during insert will be ignored. |
|
| 157 | + * |
|
| 158 | + * Implementation is not fully finished and should not be used! |
|
| 159 | + * |
|
| 160 | + * @param string $table The table name (will replace *PREFIX* with the actual prefix) |
|
| 161 | + * @param array $values data that should be inserted into the table (column name => value) |
|
| 162 | + * @return int number of inserted rows |
|
| 163 | + * @since 16.0.0 |
|
| 164 | + */ |
|
| 165 | + public function insertIgnoreConflict(string $table, array $values) : int; |
|
| 166 | + |
|
| 167 | + /** |
|
| 168 | + * Insert or update a row value |
|
| 169 | + * |
|
| 170 | + * @param string $table |
|
| 171 | + * @param array $keys (column name => value) |
|
| 172 | + * @param array $values (column name => value) |
|
| 173 | + * @param array $updatePreconditionValues ensure values match preconditions (column name => value) |
|
| 174 | + * @return int number of new rows |
|
| 175 | + * @throws Exception used to be the removed dbal exception, since 21.0.0 it's \OCP\DB\Exception |
|
| 176 | + * @throws PreConditionNotMetException |
|
| 177 | + * @since 9.0.0 |
|
| 178 | + */ |
|
| 179 | + public function setValues($table, array $keys, array $values, array $updatePreconditionValues = []): int; |
|
| 180 | + |
|
| 181 | + /** |
|
| 182 | + * Create an exclusive read+write lock on a table |
|
| 183 | + * |
|
| 184 | + * Important Note: Due to the nature how locks work on different DBs, it is |
|
| 185 | + * only possible to lock one table at a time. You should also NOT start a |
|
| 186 | + * transaction while holding a lock. |
|
| 187 | + * |
|
| 188 | + * @param string $tableName |
|
| 189 | + * @throws Exception since 21.0.0 |
|
| 190 | + * @since 9.1.0 |
|
| 191 | + */ |
|
| 192 | + public function lockTable($tableName): void; |
|
| 193 | + |
|
| 194 | + /** |
|
| 195 | + * Release a previous acquired lock again |
|
| 196 | + * |
|
| 197 | + * @throws Exception since 21.0.0 |
|
| 198 | + * @since 9.1.0 |
|
| 199 | + */ |
|
| 200 | + public function unlockTable(): void; |
|
| 201 | + |
|
| 202 | + /** |
|
| 203 | + * Start a transaction |
|
| 204 | + * @since 6.0.0 |
|
| 205 | + * @throws Exception since 21.0.0 |
|
| 206 | + */ |
|
| 207 | + public function beginTransaction(): void; |
|
| 208 | + |
|
| 209 | + /** |
|
| 210 | + * Check if a transaction is active |
|
| 211 | + * |
|
| 212 | + * @return bool |
|
| 213 | + * @since 8.2.0 |
|
| 214 | + */ |
|
| 215 | + public function inTransaction(): bool; |
|
| 216 | + |
|
| 217 | + /** |
|
| 218 | + * Commit the database changes done during a transaction that is in progress |
|
| 219 | + * @since 6.0.0 |
|
| 220 | + * @throws Exception since 21.0.0 |
|
| 221 | + */ |
|
| 222 | + public function commit(): void; |
|
| 223 | + |
|
| 224 | + /** |
|
| 225 | + * Rollback the database changes done during a transaction that is in progress |
|
| 226 | + * @since 6.0.0 |
|
| 227 | + * @throws Exception since 21.0.0 |
|
| 228 | + */ |
|
| 229 | + public function rollBack(): void; |
|
| 230 | + |
|
| 231 | + /** |
|
| 232 | + * Gets the error code and message as a string for logging |
|
| 233 | + * @return string |
|
| 234 | + * @since 6.0.0 |
|
| 235 | + * @deprecated 21.0.0 doesn't return anything meaningful |
|
| 236 | + */ |
|
| 237 | + public function getError(): string; |
|
| 238 | + |
|
| 239 | + /** |
|
| 240 | + * Fetch the SQLSTATE associated with the last database operation. |
|
| 241 | + * |
|
| 242 | + * @return integer The last error code. |
|
| 243 | + * @since 8.0.0 |
|
| 244 | + * @deprecated 21.0.0 doesn't return anything anymore |
|
| 245 | + */ |
|
| 246 | + public function errorCode(); |
|
| 247 | + |
|
| 248 | + /** |
|
| 249 | + * Fetch extended error information associated with the last database operation. |
|
| 250 | + * |
|
| 251 | + * @return array The last error information. |
|
| 252 | + * @since 8.0.0 |
|
| 253 | + * @deprecated 21.0.0 doesn't return anything anymore |
|
| 254 | + */ |
|
| 255 | + public function errorInfo(); |
|
| 256 | + |
|
| 257 | + /** |
|
| 258 | + * Establishes the connection with the database. |
|
| 259 | + * |
|
| 260 | + * @return bool |
|
| 261 | + * @throws Exception since 21.0.0 |
|
| 262 | + * @since 8.0.0 |
|
| 263 | + */ |
|
| 264 | + public function connect(): bool; |
|
| 265 | + |
|
| 266 | + /** |
|
| 267 | + * Close the database connection |
|
| 268 | + * @since 8.0.0 |
|
| 269 | + */ |
|
| 270 | + public function close(): void; |
|
| 271 | + |
|
| 272 | + /** |
|
| 273 | + * Quotes a given input parameter. |
|
| 274 | + * |
|
| 275 | + * @param mixed $input Parameter to be quoted. |
|
| 276 | + * @param int $type Type of the parameter. |
|
| 277 | + * @return mixed The quoted parameter. |
|
| 278 | + * @since 8.0.0 |
|
| 279 | + */ |
|
| 280 | + public function quote($input, $type = IQueryBuilder::PARAM_STR); |
|
| 281 | + |
|
| 282 | + /** |
|
| 283 | + * Gets the DatabasePlatform instance that provides all the metadata about |
|
| 284 | + * the platform this driver connects to. |
|
| 285 | + * |
|
| 286 | + * @return \Doctrine\DBAL\Platforms\AbstractPlatform The database platform. |
|
| 287 | + * @since 8.0.0 |
|
| 288 | + * @deprecated 30.0.0 Please use {@see self::getDatabaseProvider()} and compare to self::PLATFORM_* constants |
|
| 289 | + */ |
|
| 290 | + public function getDatabasePlatform(); |
|
| 291 | + |
|
| 292 | + /** |
|
| 293 | + * Drop a table from the database if it exists |
|
| 294 | + * |
|
| 295 | + * @param string $table table name without the prefix |
|
| 296 | + * @throws Exception since 21.0.0 |
|
| 297 | + * @since 8.0.0 |
|
| 298 | + * |
|
| 299 | + * @psalm-taint-sink sql $table |
|
| 300 | + */ |
|
| 301 | + public function dropTable(string $table): void; |
|
| 302 | + |
|
| 303 | + /** |
|
| 304 | + * Truncate a table data if it exists |
|
| 305 | + * |
|
| 306 | + * Cascade is not supported on many platforms but would optionally cascade the truncate by |
|
| 307 | + * following the foreign keys. |
|
| 308 | + * |
|
| 309 | + * @param string $table table name without the prefix |
|
| 310 | + * @param bool $cascade whether to truncate cascading |
|
| 311 | + * @throws Exception |
|
| 312 | + * @since 32.0.0 |
|
| 313 | + * |
|
| 314 | + * @psalm-taint-sink sql $table |
|
| 315 | + */ |
|
| 316 | + public function truncateTable(string $table, bool $cascade): void; |
|
| 317 | + |
|
| 318 | + /** |
|
| 319 | + * Check if a table exists |
|
| 320 | + * |
|
| 321 | + * @param string $table table name without the prefix |
|
| 322 | + * @return bool |
|
| 323 | + * @throws Exception since 21.0.0 |
|
| 324 | + * @since 8.0.0 |
|
| 325 | + */ |
|
| 326 | + public function tableExists(string $table): bool; |
|
| 327 | + |
|
| 328 | + /** |
|
| 329 | + * Escape a parameter to be used in a LIKE query |
|
| 330 | + * |
|
| 331 | + * @param string $param |
|
| 332 | + * @return string |
|
| 333 | + * @since 9.0.0 |
|
| 334 | + */ |
|
| 335 | + public function escapeLikeParameter(string $param): string; |
|
| 336 | + |
|
| 337 | + /** |
|
| 338 | + * Check whether or not the current database support 4byte wide unicode |
|
| 339 | + * |
|
| 340 | + * @return bool |
|
| 341 | + * @since 11.0.0 |
|
| 342 | + */ |
|
| 343 | + public function supports4ByteText(): bool; |
|
| 344 | + |
|
| 345 | + /** |
|
| 346 | + * Create the schema of the connected database |
|
| 347 | + * |
|
| 348 | + * @return Schema |
|
| 349 | + * @throws Exception since 21.0.0 |
|
| 350 | + * @since 13.0.0 |
|
| 351 | + */ |
|
| 352 | + public function createSchema(): Schema; |
|
| 353 | + |
|
| 354 | + /** |
|
| 355 | + * Migrate the database to the given schema |
|
| 356 | + * |
|
| 357 | + * @param Schema $toSchema |
|
| 358 | + * @throws Exception since 21.0.0 |
|
| 359 | + * @since 13.0.0 |
|
| 360 | + */ |
|
| 361 | + public function migrateToSchema(Schema $toSchema): void; |
|
| 362 | + |
|
| 363 | + /** |
|
| 364 | + * Returns the database provider name |
|
| 365 | + * |
|
| 366 | + * @link https://github.com/nextcloud/server/issues/30877 |
|
| 367 | + * |
|
| 368 | + * @param bool $strict differentiate between database flavors, e.g. MySQL vs MariaDB |
|
| 369 | + * @return self::PLATFORM_MYSQL|self::PLATFORM_ORACLE|self::PLATFORM_POSTGRES|self::PLATFORM_SQLITE|self::PLATFORM_MARIADB |
|
| 370 | + * @since 32.0.0 Optional parameter $strict was added |
|
| 371 | + * @since 28.0.0 |
|
| 372 | + */ |
|
| 373 | + public function getDatabaseProvider(bool $strict = false): string; |
|
| 374 | + |
|
| 375 | + /** |
|
| 376 | + * Get the shard definition by name, if configured |
|
| 377 | + * |
|
| 378 | + * @param string $name |
|
| 379 | + * @return ShardDefinition|null |
|
| 380 | + * @since 30.0.0 |
|
| 381 | + */ |
|
| 382 | + public function getShardDefinition(string $name): ?ShardDefinition; |
|
| 383 | + |
|
| 384 | + /** |
|
| 385 | + * Get a helper class for implementing cross-shard moves |
|
| 386 | + * |
|
| 387 | + * @return CrossShardMoveHelper |
|
| 388 | + * @since 30.0.0 |
|
| 389 | + */ |
|
| 390 | + public function getCrossShardMoveHelper(): CrossShardMoveHelper; |
|
| 391 | 391 | } |
@@ -23,243 +23,243 @@ |
||
| 23 | 23 | * Adapts the public API to our internal DBAL connection wrapper |
| 24 | 24 | */ |
| 25 | 25 | class ConnectionAdapter implements IDBConnection { |
| 26 | - /** @var Connection */ |
|
| 27 | - private $inner; |
|
| 28 | - |
|
| 29 | - public function __construct(Connection $inner) { |
|
| 30 | - $this->inner = $inner; |
|
| 31 | - } |
|
| 32 | - |
|
| 33 | - public function getQueryBuilder(): IQueryBuilder { |
|
| 34 | - return $this->inner->getQueryBuilder(); |
|
| 35 | - } |
|
| 36 | - |
|
| 37 | - public function prepare($sql, $limit = null, $offset = null): IPreparedStatement { |
|
| 38 | - try { |
|
| 39 | - return new PreparedStatement( |
|
| 40 | - $this->inner->prepare($sql, $limit, $offset) |
|
| 41 | - ); |
|
| 42 | - } catch (Exception $e) { |
|
| 43 | - throw DbalException::wrap($e); |
|
| 44 | - } |
|
| 45 | - } |
|
| 46 | - |
|
| 47 | - public function executeQuery(string $sql, array $params = [], $types = []): IResult { |
|
| 48 | - try { |
|
| 49 | - return new ResultAdapter( |
|
| 50 | - $this->inner->executeQuery($sql, $params, $types) |
|
| 51 | - ); |
|
| 52 | - } catch (Exception $e) { |
|
| 53 | - throw DbalException::wrap($e, '', $sql); |
|
| 54 | - } |
|
| 55 | - } |
|
| 56 | - |
|
| 57 | - public function executeUpdate(string $sql, array $params = [], array $types = []): int { |
|
| 58 | - try { |
|
| 59 | - return $this->inner->executeUpdate($sql, $params, $types); |
|
| 60 | - } catch (Exception $e) { |
|
| 61 | - throw DbalException::wrap($e, '', $sql); |
|
| 62 | - } |
|
| 63 | - } |
|
| 64 | - |
|
| 65 | - public function executeStatement($sql, array $params = [], array $types = []): int { |
|
| 66 | - try { |
|
| 67 | - return $this->inner->executeStatement($sql, $params, $types); |
|
| 68 | - } catch (Exception $e) { |
|
| 69 | - throw DbalException::wrap($e, '', $sql); |
|
| 70 | - } |
|
| 71 | - } |
|
| 72 | - |
|
| 73 | - public function lastInsertId(string $table): int { |
|
| 74 | - try { |
|
| 75 | - return $this->inner->lastInsertId($table); |
|
| 76 | - } catch (Exception $e) { |
|
| 77 | - throw DbalException::wrap($e); |
|
| 78 | - } |
|
| 79 | - } |
|
| 80 | - |
|
| 81 | - public function insertIfNotExist(string $table, array $input, ?array $compare = null) { |
|
| 82 | - try { |
|
| 83 | - return $this->inner->insertIfNotExist($table, $input, $compare); |
|
| 84 | - } catch (Exception $e) { |
|
| 85 | - throw DbalException::wrap($e); |
|
| 86 | - } |
|
| 87 | - } |
|
| 88 | - |
|
| 89 | - public function insertIgnoreConflict(string $table, array $values): int { |
|
| 90 | - try { |
|
| 91 | - return $this->inner->insertIgnoreConflict($table, $values); |
|
| 92 | - } catch (Exception $e) { |
|
| 93 | - throw DbalException::wrap($e); |
|
| 94 | - } |
|
| 95 | - } |
|
| 96 | - |
|
| 97 | - public function setValues($table, array $keys, array $values, array $updatePreconditionValues = []): int { |
|
| 98 | - try { |
|
| 99 | - return $this->inner->setValues($table, $keys, $values, $updatePreconditionValues); |
|
| 100 | - } catch (Exception $e) { |
|
| 101 | - throw DbalException::wrap($e); |
|
| 102 | - } |
|
| 103 | - } |
|
| 104 | - |
|
| 105 | - public function lockTable($tableName): void { |
|
| 106 | - try { |
|
| 107 | - $this->inner->lockTable($tableName); |
|
| 108 | - } catch (Exception $e) { |
|
| 109 | - throw DbalException::wrap($e); |
|
| 110 | - } |
|
| 111 | - } |
|
| 112 | - |
|
| 113 | - public function unlockTable(): void { |
|
| 114 | - try { |
|
| 115 | - $this->inner->unlockTable(); |
|
| 116 | - } catch (Exception $e) { |
|
| 117 | - throw DbalException::wrap($e); |
|
| 118 | - } |
|
| 119 | - } |
|
| 120 | - |
|
| 121 | - public function beginTransaction(): void { |
|
| 122 | - try { |
|
| 123 | - $this->inner->beginTransaction(); |
|
| 124 | - } catch (Exception $e) { |
|
| 125 | - throw DbalException::wrap($e); |
|
| 126 | - } |
|
| 127 | - } |
|
| 128 | - |
|
| 129 | - public function inTransaction(): bool { |
|
| 130 | - return $this->inner->inTransaction(); |
|
| 131 | - } |
|
| 132 | - |
|
| 133 | - public function commit(): void { |
|
| 134 | - try { |
|
| 135 | - $this->inner->commit(); |
|
| 136 | - } catch (Exception $e) { |
|
| 137 | - throw DbalException::wrap($e); |
|
| 138 | - } |
|
| 139 | - } |
|
| 140 | - |
|
| 141 | - public function rollBack(): void { |
|
| 142 | - try { |
|
| 143 | - $this->inner->rollBack(); |
|
| 144 | - } catch (Exception $e) { |
|
| 145 | - throw DbalException::wrap($e); |
|
| 146 | - } |
|
| 147 | - } |
|
| 148 | - |
|
| 149 | - public function getError(): string { |
|
| 150 | - return $this->inner->getError(); |
|
| 151 | - } |
|
| 152 | - |
|
| 153 | - public function errorCode() { |
|
| 154 | - return $this->inner->errorCode(); |
|
| 155 | - } |
|
| 156 | - |
|
| 157 | - public function errorInfo() { |
|
| 158 | - return $this->inner->errorInfo(); |
|
| 159 | - } |
|
| 160 | - |
|
| 161 | - public function connect(): bool { |
|
| 162 | - try { |
|
| 163 | - return $this->inner->connect(); |
|
| 164 | - } catch (Exception $e) { |
|
| 165 | - throw DbalException::wrap($e); |
|
| 166 | - } |
|
| 167 | - } |
|
| 168 | - |
|
| 169 | - public function close(): void { |
|
| 170 | - $this->inner->close(); |
|
| 171 | - } |
|
| 172 | - |
|
| 173 | - public function quote($input, $type = IQueryBuilder::PARAM_STR) { |
|
| 174 | - return $this->inner->quote($input, $type); |
|
| 175 | - } |
|
| 176 | - |
|
| 177 | - /** |
|
| 178 | - * @todo we are leaking a 3rdparty type here |
|
| 179 | - */ |
|
| 180 | - public function getDatabasePlatform(): AbstractPlatform { |
|
| 181 | - return $this->inner->getDatabasePlatform(); |
|
| 182 | - } |
|
| 183 | - |
|
| 184 | - public function dropTable(string $table): void { |
|
| 185 | - try { |
|
| 186 | - $this->inner->dropTable($table); |
|
| 187 | - } catch (Exception $e) { |
|
| 188 | - throw DbalException::wrap($e); |
|
| 189 | - } |
|
| 190 | - } |
|
| 191 | - |
|
| 192 | - public function truncateTable(string $table, bool $cascade): void { |
|
| 193 | - try { |
|
| 194 | - $this->inner->truncateTable($table, $cascade); |
|
| 195 | - } catch (Exception $e) { |
|
| 196 | - throw DbalException::wrap($e); |
|
| 197 | - } |
|
| 198 | - } |
|
| 199 | - |
|
| 200 | - public function tableExists(string $table): bool { |
|
| 201 | - try { |
|
| 202 | - return $this->inner->tableExists($table); |
|
| 203 | - } catch (Exception $e) { |
|
| 204 | - throw DbalException::wrap($e); |
|
| 205 | - } |
|
| 206 | - } |
|
| 207 | - |
|
| 208 | - public function escapeLikeParameter(string $param): string { |
|
| 209 | - return $this->inner->escapeLikeParameter($param); |
|
| 210 | - } |
|
| 211 | - |
|
| 212 | - public function supports4ByteText(): bool { |
|
| 213 | - return $this->inner->supports4ByteText(); |
|
| 214 | - } |
|
| 215 | - |
|
| 216 | - /** |
|
| 217 | - * @todo leaks a 3rdparty type |
|
| 218 | - */ |
|
| 219 | - public function createSchema(): Schema { |
|
| 220 | - try { |
|
| 221 | - return $this->inner->createSchema(); |
|
| 222 | - } catch (Exception $e) { |
|
| 223 | - throw DbalException::wrap($e); |
|
| 224 | - } |
|
| 225 | - } |
|
| 226 | - |
|
| 227 | - public function migrateToSchema(Schema $toSchema): void { |
|
| 228 | - try { |
|
| 229 | - $this->inner->migrateToSchema($toSchema); |
|
| 230 | - } catch (Exception $e) { |
|
| 231 | - throw DbalException::wrap($e); |
|
| 232 | - } |
|
| 233 | - } |
|
| 234 | - |
|
| 235 | - public function getInner(): Connection { |
|
| 236 | - return $this->inner; |
|
| 237 | - } |
|
| 238 | - |
|
| 239 | - /** |
|
| 240 | - * @return self::PLATFORM_MYSQL|self::PLATFORM_ORACLE|self::PLATFORM_POSTGRES|self::PLATFORM_SQLITE|self::PLATFORM_MARIADB |
|
| 241 | - */ |
|
| 242 | - public function getDatabaseProvider(bool $strict = false): string { |
|
| 243 | - return $this->inner->getDatabaseProvider($strict); |
|
| 244 | - } |
|
| 245 | - |
|
| 246 | - /** |
|
| 247 | - * @internal Should only be used inside the QueryBuilder, ExpressionBuilder and FunctionBuilder |
|
| 248 | - * All apps and API code should not need this and instead use provided functionality from the above. |
|
| 249 | - */ |
|
| 250 | - public function getServerVersion(): string { |
|
| 251 | - return $this->inner->getServerVersion(); |
|
| 252 | - } |
|
| 253 | - |
|
| 254 | - public function logDatabaseException(\Exception $exception) { |
|
| 255 | - $this->inner->logDatabaseException($exception); |
|
| 256 | - } |
|
| 257 | - |
|
| 258 | - public function getShardDefinition(string $name): ?ShardDefinition { |
|
| 259 | - return $this->inner->getShardDefinition($name); |
|
| 260 | - } |
|
| 261 | - |
|
| 262 | - public function getCrossShardMoveHelper(): CrossShardMoveHelper { |
|
| 263 | - return $this->inner->getCrossShardMoveHelper(); |
|
| 264 | - } |
|
| 26 | + /** @var Connection */ |
|
| 27 | + private $inner; |
|
| 28 | + |
|
| 29 | + public function __construct(Connection $inner) { |
|
| 30 | + $this->inner = $inner; |
|
| 31 | + } |
|
| 32 | + |
|
| 33 | + public function getQueryBuilder(): IQueryBuilder { |
|
| 34 | + return $this->inner->getQueryBuilder(); |
|
| 35 | + } |
|
| 36 | + |
|
| 37 | + public function prepare($sql, $limit = null, $offset = null): IPreparedStatement { |
|
| 38 | + try { |
|
| 39 | + return new PreparedStatement( |
|
| 40 | + $this->inner->prepare($sql, $limit, $offset) |
|
| 41 | + ); |
|
| 42 | + } catch (Exception $e) { |
|
| 43 | + throw DbalException::wrap($e); |
|
| 44 | + } |
|
| 45 | + } |
|
| 46 | + |
|
| 47 | + public function executeQuery(string $sql, array $params = [], $types = []): IResult { |
|
| 48 | + try { |
|
| 49 | + return new ResultAdapter( |
|
| 50 | + $this->inner->executeQuery($sql, $params, $types) |
|
| 51 | + ); |
|
| 52 | + } catch (Exception $e) { |
|
| 53 | + throw DbalException::wrap($e, '', $sql); |
|
| 54 | + } |
|
| 55 | + } |
|
| 56 | + |
|
| 57 | + public function executeUpdate(string $sql, array $params = [], array $types = []): int { |
|
| 58 | + try { |
|
| 59 | + return $this->inner->executeUpdate($sql, $params, $types); |
|
| 60 | + } catch (Exception $e) { |
|
| 61 | + throw DbalException::wrap($e, '', $sql); |
|
| 62 | + } |
|
| 63 | + } |
|
| 64 | + |
|
| 65 | + public function executeStatement($sql, array $params = [], array $types = []): int { |
|
| 66 | + try { |
|
| 67 | + return $this->inner->executeStatement($sql, $params, $types); |
|
| 68 | + } catch (Exception $e) { |
|
| 69 | + throw DbalException::wrap($e, '', $sql); |
|
| 70 | + } |
|
| 71 | + } |
|
| 72 | + |
|
| 73 | + public function lastInsertId(string $table): int { |
|
| 74 | + try { |
|
| 75 | + return $this->inner->lastInsertId($table); |
|
| 76 | + } catch (Exception $e) { |
|
| 77 | + throw DbalException::wrap($e); |
|
| 78 | + } |
|
| 79 | + } |
|
| 80 | + |
|
| 81 | + public function insertIfNotExist(string $table, array $input, ?array $compare = null) { |
|
| 82 | + try { |
|
| 83 | + return $this->inner->insertIfNotExist($table, $input, $compare); |
|
| 84 | + } catch (Exception $e) { |
|
| 85 | + throw DbalException::wrap($e); |
|
| 86 | + } |
|
| 87 | + } |
|
| 88 | + |
|
| 89 | + public function insertIgnoreConflict(string $table, array $values): int { |
|
| 90 | + try { |
|
| 91 | + return $this->inner->insertIgnoreConflict($table, $values); |
|
| 92 | + } catch (Exception $e) { |
|
| 93 | + throw DbalException::wrap($e); |
|
| 94 | + } |
|
| 95 | + } |
|
| 96 | + |
|
| 97 | + public function setValues($table, array $keys, array $values, array $updatePreconditionValues = []): int { |
|
| 98 | + try { |
|
| 99 | + return $this->inner->setValues($table, $keys, $values, $updatePreconditionValues); |
|
| 100 | + } catch (Exception $e) { |
|
| 101 | + throw DbalException::wrap($e); |
|
| 102 | + } |
|
| 103 | + } |
|
| 104 | + |
|
| 105 | + public function lockTable($tableName): void { |
|
| 106 | + try { |
|
| 107 | + $this->inner->lockTable($tableName); |
|
| 108 | + } catch (Exception $e) { |
|
| 109 | + throw DbalException::wrap($e); |
|
| 110 | + } |
|
| 111 | + } |
|
| 112 | + |
|
| 113 | + public function unlockTable(): void { |
|
| 114 | + try { |
|
| 115 | + $this->inner->unlockTable(); |
|
| 116 | + } catch (Exception $e) { |
|
| 117 | + throw DbalException::wrap($e); |
|
| 118 | + } |
|
| 119 | + } |
|
| 120 | + |
|
| 121 | + public function beginTransaction(): void { |
|
| 122 | + try { |
|
| 123 | + $this->inner->beginTransaction(); |
|
| 124 | + } catch (Exception $e) { |
|
| 125 | + throw DbalException::wrap($e); |
|
| 126 | + } |
|
| 127 | + } |
|
| 128 | + |
|
| 129 | + public function inTransaction(): bool { |
|
| 130 | + return $this->inner->inTransaction(); |
|
| 131 | + } |
|
| 132 | + |
|
| 133 | + public function commit(): void { |
|
| 134 | + try { |
|
| 135 | + $this->inner->commit(); |
|
| 136 | + } catch (Exception $e) { |
|
| 137 | + throw DbalException::wrap($e); |
|
| 138 | + } |
|
| 139 | + } |
|
| 140 | + |
|
| 141 | + public function rollBack(): void { |
|
| 142 | + try { |
|
| 143 | + $this->inner->rollBack(); |
|
| 144 | + } catch (Exception $e) { |
|
| 145 | + throw DbalException::wrap($e); |
|
| 146 | + } |
|
| 147 | + } |
|
| 148 | + |
|
| 149 | + public function getError(): string { |
|
| 150 | + return $this->inner->getError(); |
|
| 151 | + } |
|
| 152 | + |
|
| 153 | + public function errorCode() { |
|
| 154 | + return $this->inner->errorCode(); |
|
| 155 | + } |
|
| 156 | + |
|
| 157 | + public function errorInfo() { |
|
| 158 | + return $this->inner->errorInfo(); |
|
| 159 | + } |
|
| 160 | + |
|
| 161 | + public function connect(): bool { |
|
| 162 | + try { |
|
| 163 | + return $this->inner->connect(); |
|
| 164 | + } catch (Exception $e) { |
|
| 165 | + throw DbalException::wrap($e); |
|
| 166 | + } |
|
| 167 | + } |
|
| 168 | + |
|
| 169 | + public function close(): void { |
|
| 170 | + $this->inner->close(); |
|
| 171 | + } |
|
| 172 | + |
|
| 173 | + public function quote($input, $type = IQueryBuilder::PARAM_STR) { |
|
| 174 | + return $this->inner->quote($input, $type); |
|
| 175 | + } |
|
| 176 | + |
|
| 177 | + /** |
|
| 178 | + * @todo we are leaking a 3rdparty type here |
|
| 179 | + */ |
|
| 180 | + public function getDatabasePlatform(): AbstractPlatform { |
|
| 181 | + return $this->inner->getDatabasePlatform(); |
|
| 182 | + } |
|
| 183 | + |
|
| 184 | + public function dropTable(string $table): void { |
|
| 185 | + try { |
|
| 186 | + $this->inner->dropTable($table); |
|
| 187 | + } catch (Exception $e) { |
|
| 188 | + throw DbalException::wrap($e); |
|
| 189 | + } |
|
| 190 | + } |
|
| 191 | + |
|
| 192 | + public function truncateTable(string $table, bool $cascade): void { |
|
| 193 | + try { |
|
| 194 | + $this->inner->truncateTable($table, $cascade); |
|
| 195 | + } catch (Exception $e) { |
|
| 196 | + throw DbalException::wrap($e); |
|
| 197 | + } |
|
| 198 | + } |
|
| 199 | + |
|
| 200 | + public function tableExists(string $table): bool { |
|
| 201 | + try { |
|
| 202 | + return $this->inner->tableExists($table); |
|
| 203 | + } catch (Exception $e) { |
|
| 204 | + throw DbalException::wrap($e); |
|
| 205 | + } |
|
| 206 | + } |
|
| 207 | + |
|
| 208 | + public function escapeLikeParameter(string $param): string { |
|
| 209 | + return $this->inner->escapeLikeParameter($param); |
|
| 210 | + } |
|
| 211 | + |
|
| 212 | + public function supports4ByteText(): bool { |
|
| 213 | + return $this->inner->supports4ByteText(); |
|
| 214 | + } |
|
| 215 | + |
|
| 216 | + /** |
|
| 217 | + * @todo leaks a 3rdparty type |
|
| 218 | + */ |
|
| 219 | + public function createSchema(): Schema { |
|
| 220 | + try { |
|
| 221 | + return $this->inner->createSchema(); |
|
| 222 | + } catch (Exception $e) { |
|
| 223 | + throw DbalException::wrap($e); |
|
| 224 | + } |
|
| 225 | + } |
|
| 226 | + |
|
| 227 | + public function migrateToSchema(Schema $toSchema): void { |
|
| 228 | + try { |
|
| 229 | + $this->inner->migrateToSchema($toSchema); |
|
| 230 | + } catch (Exception $e) { |
|
| 231 | + throw DbalException::wrap($e); |
|
| 232 | + } |
|
| 233 | + } |
|
| 234 | + |
|
| 235 | + public function getInner(): Connection { |
|
| 236 | + return $this->inner; |
|
| 237 | + } |
|
| 238 | + |
|
| 239 | + /** |
|
| 240 | + * @return self::PLATFORM_MYSQL|self::PLATFORM_ORACLE|self::PLATFORM_POSTGRES|self::PLATFORM_SQLITE|self::PLATFORM_MARIADB |
|
| 241 | + */ |
|
| 242 | + public function getDatabaseProvider(bool $strict = false): string { |
|
| 243 | + return $this->inner->getDatabaseProvider($strict); |
|
| 244 | + } |
|
| 245 | + |
|
| 246 | + /** |
|
| 247 | + * @internal Should only be used inside the QueryBuilder, ExpressionBuilder and FunctionBuilder |
|
| 248 | + * All apps and API code should not need this and instead use provided functionality from the above. |
|
| 249 | + */ |
|
| 250 | + public function getServerVersion(): string { |
|
| 251 | + return $this->inner->getServerVersion(); |
|
| 252 | + } |
|
| 253 | + |
|
| 254 | + public function logDatabaseException(\Exception $exception) { |
|
| 255 | + $this->inner->logDatabaseException($exception); |
|
| 256 | + } |
|
| 257 | + |
|
| 258 | + public function getShardDefinition(string $name): ?ShardDefinition { |
|
| 259 | + return $this->inner->getShardDefinition($name); |
|
| 260 | + } |
|
| 261 | + |
|
| 262 | + public function getCrossShardMoveHelper(): CrossShardMoveHelper { |
|
| 263 | + return $this->inner->getCrossShardMoveHelper(); |
|
| 264 | + } |
|
| 265 | 265 | } |
@@ -50,921 +50,921 @@ |
||
| 50 | 50 | use function in_array; |
| 51 | 51 | |
| 52 | 52 | class Connection extends PrimaryReadReplicaConnection { |
| 53 | - /** @var string */ |
|
| 54 | - protected $tablePrefix; |
|
| 55 | - |
|
| 56 | - /** @var \OC\DB\Adapter $adapter */ |
|
| 57 | - protected $adapter; |
|
| 58 | - |
|
| 59 | - /** @var SystemConfig */ |
|
| 60 | - private $systemConfig; |
|
| 61 | - |
|
| 62 | - private ClockInterface $clock; |
|
| 63 | - |
|
| 64 | - private LoggerInterface $logger; |
|
| 65 | - |
|
| 66 | - protected $lockedTable = null; |
|
| 67 | - |
|
| 68 | - /** @var int */ |
|
| 69 | - protected $queriesBuilt = 0; |
|
| 70 | - |
|
| 71 | - /** @var int */ |
|
| 72 | - protected $queriesExecuted = 0; |
|
| 73 | - |
|
| 74 | - /** @var DbDataCollector|null */ |
|
| 75 | - protected $dbDataCollector = null; |
|
| 76 | - private array $lastConnectionCheck = []; |
|
| 77 | - |
|
| 78 | - protected ?float $transactionActiveSince = null; |
|
| 79 | - |
|
| 80 | - /** @var array<string, int> */ |
|
| 81 | - protected $tableDirtyWrites = []; |
|
| 82 | - |
|
| 83 | - protected bool $logDbException = false; |
|
| 84 | - private ?array $transactionBacktrace = null; |
|
| 85 | - |
|
| 86 | - protected bool $logRequestId; |
|
| 87 | - protected string $requestId; |
|
| 88 | - |
|
| 89 | - /** @var array<string, list<string>> */ |
|
| 90 | - protected array $partitions; |
|
| 91 | - /** @var ShardDefinition[] */ |
|
| 92 | - protected array $shards = []; |
|
| 93 | - protected ShardConnectionManager $shardConnectionManager; |
|
| 94 | - protected AutoIncrementHandler $autoIncrementHandler; |
|
| 95 | - protected bool $isShardingEnabled; |
|
| 96 | - |
|
| 97 | - public const SHARD_PRESETS = [ |
|
| 98 | - 'filecache' => [ |
|
| 99 | - 'companion_keys' => [ |
|
| 100 | - 'file_id', |
|
| 101 | - ], |
|
| 102 | - 'companion_tables' => [ |
|
| 103 | - 'filecache_extended', |
|
| 104 | - 'files_metadata', |
|
| 105 | - ], |
|
| 106 | - 'primary_key' => 'fileid', |
|
| 107 | - 'shard_key' => 'storage', |
|
| 108 | - 'table' => 'filecache', |
|
| 109 | - ], |
|
| 110 | - ]; |
|
| 111 | - |
|
| 112 | - /** |
|
| 113 | - * Initializes a new instance of the Connection class. |
|
| 114 | - * |
|
| 115 | - * @throws \Exception |
|
| 116 | - */ |
|
| 117 | - public function __construct( |
|
| 118 | - private array $params, |
|
| 119 | - Driver $driver, |
|
| 120 | - ?Configuration $config = null, |
|
| 121 | - ?EventManager $eventManager = null, |
|
| 122 | - ) { |
|
| 123 | - if (!isset($params['adapter'])) { |
|
| 124 | - throw new \Exception('adapter not set'); |
|
| 125 | - } |
|
| 126 | - if (!isset($params['tablePrefix'])) { |
|
| 127 | - throw new \Exception('tablePrefix not set'); |
|
| 128 | - } |
|
| 129 | - /** |
|
| 130 | - * @psalm-suppress InternalMethod |
|
| 131 | - */ |
|
| 132 | - parent::__construct($params, $driver, $config, $eventManager); |
|
| 133 | - $this->adapter = new $params['adapter']($this); |
|
| 134 | - $this->tablePrefix = $params['tablePrefix']; |
|
| 135 | - $this->isShardingEnabled = isset($this->params['sharding']) && !empty($this->params['sharding']); |
|
| 136 | - |
|
| 137 | - if ($this->isShardingEnabled) { |
|
| 138 | - /** @psalm-suppress InvalidArrayOffset */ |
|
| 139 | - $this->shardConnectionManager = $this->params['shard_connection_manager'] ?? Server::get(ShardConnectionManager::class); |
|
| 140 | - /** @psalm-suppress InvalidArrayOffset */ |
|
| 141 | - $this->autoIncrementHandler = $this->params['auto_increment_handler'] ?? new AutoIncrementHandler( |
|
| 142 | - Server::get(ICacheFactory::class), |
|
| 143 | - $this->shardConnectionManager, |
|
| 144 | - ); |
|
| 145 | - } |
|
| 146 | - $this->systemConfig = \OC::$server->getSystemConfig(); |
|
| 147 | - $this->clock = Server::get(ClockInterface::class); |
|
| 148 | - $this->logger = Server::get(LoggerInterface::class); |
|
| 149 | - |
|
| 150 | - $this->logRequestId = $this->systemConfig->getValue('db.log_request_id', false); |
|
| 151 | - $this->logDbException = $this->systemConfig->getValue('db.log_exceptions', false); |
|
| 152 | - $this->requestId = Server::get(IRequestId::class)->getId(); |
|
| 153 | - |
|
| 154 | - /** @var \OCP\Profiler\IProfiler */ |
|
| 155 | - $profiler = Server::get(IProfiler::class); |
|
| 156 | - if ($profiler->isEnabled()) { |
|
| 157 | - $this->dbDataCollector = new DbDataCollector($this); |
|
| 158 | - $profiler->add($this->dbDataCollector); |
|
| 159 | - $debugStack = new BacktraceDebugStack(); |
|
| 160 | - $this->dbDataCollector->setDebugStack($debugStack); |
|
| 161 | - $this->_config->setSQLLogger($debugStack); |
|
| 162 | - } |
|
| 163 | - |
|
| 164 | - /** @var array<string, array{shards: array[], mapper: ?string, from_primary_key: ?int, from_shard_key: ?int}> $shardConfig */ |
|
| 165 | - $shardConfig = $this->params['sharding'] ?? []; |
|
| 166 | - $shardNames = array_keys($shardConfig); |
|
| 167 | - $this->shards = array_map(function (array $config, string $name) { |
|
| 168 | - if (!isset(self::SHARD_PRESETS[$name])) { |
|
| 169 | - throw new \Exception("Shard preset $name not found"); |
|
| 170 | - } |
|
| 171 | - |
|
| 172 | - $shardMapperClass = $config['mapper'] ?? RoundRobinShardMapper::class; |
|
| 173 | - $shardMapper = Server::get($shardMapperClass); |
|
| 174 | - if (!$shardMapper instanceof IShardMapper) { |
|
| 175 | - throw new \Exception("Invalid shard mapper: $shardMapperClass"); |
|
| 176 | - } |
|
| 177 | - return new ShardDefinition( |
|
| 178 | - self::SHARD_PRESETS[$name]['table'], |
|
| 179 | - self::SHARD_PRESETS[$name]['primary_key'], |
|
| 180 | - self::SHARD_PRESETS[$name]['companion_keys'], |
|
| 181 | - self::SHARD_PRESETS[$name]['shard_key'], |
|
| 182 | - $shardMapper, |
|
| 183 | - self::SHARD_PRESETS[$name]['companion_tables'], |
|
| 184 | - $config['shards'], |
|
| 185 | - $config['from_primary_key'] ?? 0, |
|
| 186 | - $config['from_shard_key'] ?? 0, |
|
| 187 | - ); |
|
| 188 | - }, $shardConfig, $shardNames); |
|
| 189 | - $this->shards = array_combine($shardNames, $this->shards); |
|
| 190 | - $this->partitions = array_map(function (ShardDefinition $shard) { |
|
| 191 | - return array_merge([$shard->table], $shard->companionTables); |
|
| 192 | - }, $this->shards); |
|
| 193 | - |
|
| 194 | - $this->setNestTransactionsWithSavepoints(true); |
|
| 195 | - } |
|
| 196 | - |
|
| 197 | - /** |
|
| 198 | - * @return IDBConnection[] |
|
| 199 | - */ |
|
| 200 | - public function getShardConnections(): array { |
|
| 201 | - $connections = []; |
|
| 202 | - if ($this->isShardingEnabled) { |
|
| 203 | - foreach ($this->shards as $shardDefinition) { |
|
| 204 | - foreach ($shardDefinition->getAllShards() as $shard) { |
|
| 205 | - if ($shard !== ShardDefinition::MIGRATION_SHARD) { |
|
| 206 | - /** @var ConnectionAdapter $connection */ |
|
| 207 | - $connections[] = $this->shardConnectionManager->getConnection($shardDefinition, $shard); |
|
| 208 | - } |
|
| 209 | - } |
|
| 210 | - } |
|
| 211 | - } |
|
| 212 | - return $connections; |
|
| 213 | - } |
|
| 214 | - |
|
| 215 | - /** |
|
| 216 | - * @throws Exception |
|
| 217 | - */ |
|
| 218 | - public function connect($connectionName = null) { |
|
| 219 | - try { |
|
| 220 | - if ($this->_conn) { |
|
| 221 | - $this->reconnectIfNeeded(); |
|
| 222 | - /** @psalm-suppress InternalMethod */ |
|
| 223 | - return parent::connect(); |
|
| 224 | - } |
|
| 225 | - |
|
| 226 | - // Only trigger the event logger for the initial connect call |
|
| 227 | - $eventLogger = Server::get(IEventLogger::class); |
|
| 228 | - $eventLogger->start('connect:db', 'db connection opened'); |
|
| 229 | - /** @psalm-suppress InternalMethod */ |
|
| 230 | - $status = parent::connect(); |
|
| 231 | - $eventLogger->end('connect:db'); |
|
| 232 | - |
|
| 233 | - $this->lastConnectionCheck[$this->getConnectionName()] = time(); |
|
| 234 | - |
|
| 235 | - return $status; |
|
| 236 | - } catch (Exception $e) { |
|
| 237 | - // throw a new exception to prevent leaking info from the stacktrace |
|
| 238 | - throw new Exception('Failed to connect to the database: ' . $e->getMessage(), $e->getCode()); |
|
| 239 | - } |
|
| 240 | - } |
|
| 241 | - |
|
| 242 | - protected function performConnect(?string $connectionName = null): bool { |
|
| 243 | - if (($connectionName ?? 'replica') === 'replica' |
|
| 244 | - && count($this->params['replica']) === 1 |
|
| 245 | - && $this->params['primary'] === $this->params['replica'][0]) { |
|
| 246 | - return parent::performConnect('primary'); |
|
| 247 | - } |
|
| 248 | - return parent::performConnect($connectionName); |
|
| 249 | - } |
|
| 250 | - |
|
| 251 | - public function getStats(): array { |
|
| 252 | - return [ |
|
| 253 | - 'built' => $this->queriesBuilt, |
|
| 254 | - 'executed' => $this->queriesExecuted, |
|
| 255 | - ]; |
|
| 256 | - } |
|
| 257 | - |
|
| 258 | - /** |
|
| 259 | - * Returns a QueryBuilder for the connection. |
|
| 260 | - */ |
|
| 261 | - public function getQueryBuilder(): IQueryBuilder { |
|
| 262 | - $this->queriesBuilt++; |
|
| 263 | - |
|
| 264 | - $builder = new QueryBuilder( |
|
| 265 | - new ConnectionAdapter($this), |
|
| 266 | - $this->systemConfig, |
|
| 267 | - $this->logger |
|
| 268 | - ); |
|
| 269 | - if ($this->isShardingEnabled && count($this->partitions) > 0) { |
|
| 270 | - $builder = new PartitionedQueryBuilder( |
|
| 271 | - $builder, |
|
| 272 | - $this->shards, |
|
| 273 | - $this->shardConnectionManager, |
|
| 274 | - $this->autoIncrementHandler, |
|
| 275 | - ); |
|
| 276 | - foreach ($this->partitions as $name => $tables) { |
|
| 277 | - $partition = new PartitionSplit($name, $tables); |
|
| 278 | - $builder->addPartition($partition); |
|
| 279 | - } |
|
| 280 | - return $builder; |
|
| 281 | - } else { |
|
| 282 | - return $builder; |
|
| 283 | - } |
|
| 284 | - } |
|
| 285 | - |
|
| 286 | - /** |
|
| 287 | - * Gets the QueryBuilder for the connection. |
|
| 288 | - * |
|
| 289 | - * @return \Doctrine\DBAL\Query\QueryBuilder |
|
| 290 | - * @deprecated 8.0.0 please use $this->getQueryBuilder() instead |
|
| 291 | - */ |
|
| 292 | - public function createQueryBuilder() { |
|
| 293 | - $backtrace = $this->getCallerBacktrace(); |
|
| 294 | - $this->logger->debug('Doctrine QueryBuilder retrieved in {backtrace}', ['app' => 'core', 'backtrace' => $backtrace]); |
|
| 295 | - $this->queriesBuilt++; |
|
| 296 | - return parent::createQueryBuilder(); |
|
| 297 | - } |
|
| 298 | - |
|
| 299 | - /** |
|
| 300 | - * Gets the ExpressionBuilder for the connection. |
|
| 301 | - * |
|
| 302 | - * @return \Doctrine\DBAL\Query\Expression\ExpressionBuilder |
|
| 303 | - * @deprecated 8.0.0 please use $this->getQueryBuilder()->expr() instead |
|
| 304 | - */ |
|
| 305 | - public function getExpressionBuilder() { |
|
| 306 | - $backtrace = $this->getCallerBacktrace(); |
|
| 307 | - $this->logger->debug('Doctrine ExpressionBuilder retrieved in {backtrace}', ['app' => 'core', 'backtrace' => $backtrace]); |
|
| 308 | - $this->queriesBuilt++; |
|
| 309 | - return parent::getExpressionBuilder(); |
|
| 310 | - } |
|
| 311 | - |
|
| 312 | - /** |
|
| 313 | - * Get the file and line that called the method where `getCallerBacktrace()` was used |
|
| 314 | - * |
|
| 315 | - * @return string |
|
| 316 | - */ |
|
| 317 | - protected function getCallerBacktrace() { |
|
| 318 | - $traces = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 2); |
|
| 319 | - |
|
| 320 | - // 0 is the method where we use `getCallerBacktrace` |
|
| 321 | - // 1 is the target method which uses the method we want to log |
|
| 322 | - if (isset($traces[1])) { |
|
| 323 | - return $traces[1]['file'] . ':' . $traces[1]['line']; |
|
| 324 | - } |
|
| 325 | - |
|
| 326 | - return ''; |
|
| 327 | - } |
|
| 328 | - |
|
| 329 | - /** |
|
| 330 | - * @return string |
|
| 331 | - */ |
|
| 332 | - public function getPrefix() { |
|
| 333 | - return $this->tablePrefix; |
|
| 334 | - } |
|
| 335 | - |
|
| 336 | - /** |
|
| 337 | - * Prepares an SQL statement. |
|
| 338 | - * |
|
| 339 | - * @param string $statement The SQL statement to prepare. |
|
| 340 | - * @param int|null $limit |
|
| 341 | - * @param int|null $offset |
|
| 342 | - * |
|
| 343 | - * @return Statement The prepared statement. |
|
| 344 | - * @throws Exception |
|
| 345 | - */ |
|
| 346 | - public function prepare($sql, $limit = null, $offset = null): Statement { |
|
| 347 | - if ($limit === -1 || $limit === null) { |
|
| 348 | - $limit = null; |
|
| 349 | - } else { |
|
| 350 | - $limit = (int)$limit; |
|
| 351 | - } |
|
| 352 | - if ($offset !== null) { |
|
| 353 | - $offset = (int)$offset; |
|
| 354 | - } |
|
| 355 | - if (!is_null($limit)) { |
|
| 356 | - $platform = $this->getDatabasePlatform(); |
|
| 357 | - $sql = $platform->modifyLimitQuery($sql, $limit, $offset); |
|
| 358 | - } |
|
| 359 | - $statement = $this->finishQuery($sql); |
|
| 360 | - |
|
| 361 | - return parent::prepare($statement); |
|
| 362 | - } |
|
| 363 | - |
|
| 364 | - /** |
|
| 365 | - * Executes an, optionally parametrized, SQL query. |
|
| 366 | - * |
|
| 367 | - * If the query is parametrized, a prepared statement is used. |
|
| 368 | - * If an SQLLogger is configured, the execution is logged. |
|
| 369 | - * |
|
| 370 | - * @param string $sql The SQL query to execute. |
|
| 371 | - * @param array $params The parameters to bind to the query, if any. |
|
| 372 | - * @param array $types The types the previous parameters are in. |
|
| 373 | - * @param \Doctrine\DBAL\Cache\QueryCacheProfile|null $qcp The query cache profile, optional. |
|
| 374 | - * |
|
| 375 | - * @return Result The executed statement. |
|
| 376 | - * |
|
| 377 | - * @throws \Doctrine\DBAL\Exception |
|
| 378 | - */ |
|
| 379 | - public function executeQuery(string $sql, array $params = [], $types = [], ?QueryCacheProfile $qcp = null): Result { |
|
| 380 | - $tables = $this->getQueriedTables($sql); |
|
| 381 | - $now = $this->clock->now()->getTimestamp(); |
|
| 382 | - $dirtyTableWrites = []; |
|
| 383 | - foreach ($tables as $table) { |
|
| 384 | - $lastAccess = $this->tableDirtyWrites[$table] ?? 0; |
|
| 385 | - // Only very recent writes are considered dirty |
|
| 386 | - if ($lastAccess >= ($now - 3)) { |
|
| 387 | - $dirtyTableWrites[] = $table; |
|
| 388 | - } |
|
| 389 | - } |
|
| 390 | - if ($this->isTransactionActive()) { |
|
| 391 | - // Transacted queries go to the primary. The consistency of the primary guarantees that we can not run |
|
| 392 | - // into a dirty read. |
|
| 393 | - } elseif (count($dirtyTableWrites) === 0) { |
|
| 394 | - // No tables read that could have been written already in the same request and no transaction active |
|
| 395 | - // so we can switch back to the replica for reading as long as no writes happen that switch back to the primary |
|
| 396 | - // We cannot log here as this would log too early in the server boot process |
|
| 397 | - $this->ensureConnectedToReplica(); |
|
| 398 | - } else { |
|
| 399 | - // Read to a table that has been written to previously |
|
| 400 | - // While this might not necessarily mean that we did a read after write it is an indication for a code path to check |
|
| 401 | - $this->logger->log( |
|
| 402 | - (int)($this->systemConfig->getValue('loglevel_dirty_database_queries', null) ?? 0), |
|
| 403 | - 'dirty table reads: ' . $sql, |
|
| 404 | - [ |
|
| 405 | - 'tables' => array_keys($this->tableDirtyWrites), |
|
| 406 | - 'reads' => $tables, |
|
| 407 | - 'exception' => new \Exception('dirty table reads: ' . $sql), |
|
| 408 | - ], |
|
| 409 | - ); |
|
| 410 | - // To prevent a dirty read on a replica that is slightly out of sync, we |
|
| 411 | - // switch back to the primary. This is detrimental for performance but |
|
| 412 | - // safer for consistency. |
|
| 413 | - $this->ensureConnectedToPrimary(); |
|
| 414 | - } |
|
| 415 | - |
|
| 416 | - $sql = $this->finishQuery($sql); |
|
| 417 | - $this->queriesExecuted++; |
|
| 418 | - $this->logQueryToFile($sql, $params); |
|
| 419 | - try { |
|
| 420 | - return parent::executeQuery($sql, $params, $types, $qcp); |
|
| 421 | - } catch (\Exception $e) { |
|
| 422 | - $this->logDatabaseException($e); |
|
| 423 | - throw $e; |
|
| 424 | - } |
|
| 425 | - } |
|
| 426 | - |
|
| 427 | - /** |
|
| 428 | - * Helper function to get the list of tables affected by a given query |
|
| 429 | - * used to track dirty tables that received a write with the current request |
|
| 430 | - */ |
|
| 431 | - private function getQueriedTables(string $sql): array { |
|
| 432 | - $re = '/(\*PREFIX\*\w+)/mi'; |
|
| 433 | - preg_match_all($re, $sql, $matches); |
|
| 434 | - return array_map([$this, 'replaceTablePrefix'], $matches[0] ?? []); |
|
| 435 | - } |
|
| 436 | - |
|
| 437 | - /** |
|
| 438 | - * @throws Exception |
|
| 439 | - */ |
|
| 440 | - public function executeUpdate(string $sql, array $params = [], array $types = []): int { |
|
| 441 | - return $this->executeStatement($sql, $params, $types); |
|
| 442 | - } |
|
| 443 | - |
|
| 444 | - /** |
|
| 445 | - * Executes an SQL INSERT/UPDATE/DELETE query with the given parameters |
|
| 446 | - * and returns the number of affected rows. |
|
| 447 | - * |
|
| 448 | - * This method supports PDO binding types as well as DBAL mapping types. |
|
| 449 | - * |
|
| 450 | - * @param string $sql The SQL query. |
|
| 451 | - * @param array $params The query parameters. |
|
| 452 | - * @param array $types The parameter types. |
|
| 453 | - * |
|
| 454 | - * @return int The number of affected rows. |
|
| 455 | - * |
|
| 456 | - * @throws \Doctrine\DBAL\Exception |
|
| 457 | - */ |
|
| 458 | - public function executeStatement($sql, array $params = [], array $types = []): int { |
|
| 459 | - $tables = $this->getQueriedTables($sql); |
|
| 460 | - foreach ($tables as $table) { |
|
| 461 | - $this->tableDirtyWrites[$table] = $this->clock->now()->getTimestamp(); |
|
| 462 | - } |
|
| 463 | - $sql = $this->finishQuery($sql); |
|
| 464 | - $this->queriesExecuted++; |
|
| 465 | - $this->logQueryToFile($sql, $params); |
|
| 466 | - try { |
|
| 467 | - return (int)parent::executeStatement($sql, $params, $types); |
|
| 468 | - } catch (\Exception $e) { |
|
| 469 | - $this->logDatabaseException($e); |
|
| 470 | - throw $e; |
|
| 471 | - } |
|
| 472 | - } |
|
| 473 | - |
|
| 474 | - protected function logQueryToFile(string $sql, array $params): void { |
|
| 475 | - $logFile = $this->systemConfig->getValue('query_log_file'); |
|
| 476 | - if ($logFile !== '' && is_writable(dirname($logFile)) && (!file_exists($logFile) || is_writable($logFile))) { |
|
| 477 | - $prefix = ''; |
|
| 478 | - if ($this->systemConfig->getValue('query_log_file_requestid') === 'yes') { |
|
| 479 | - $prefix .= Server::get(IRequestId::class)->getId() . "\t"; |
|
| 480 | - } |
|
| 481 | - |
|
| 482 | - $postfix = ''; |
|
| 483 | - if ($this->systemConfig->getValue('query_log_file_parameters') === 'yes') { |
|
| 484 | - $postfix .= '; ' . json_encode($params); |
|
| 485 | - } |
|
| 486 | - |
|
| 487 | - if ($this->systemConfig->getValue('query_log_file_backtrace') === 'yes') { |
|
| 488 | - $trace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS); |
|
| 489 | - array_pop($trace); |
|
| 490 | - $postfix .= '; ' . json_encode($trace); |
|
| 491 | - } |
|
| 492 | - |
|
| 493 | - // FIXME: Improve to log the actual target db host |
|
| 494 | - $isPrimary = $this->connections['primary'] === $this->_conn; |
|
| 495 | - $prefix .= ' ' . ($isPrimary === true ? 'primary' : 'replica') . ' '; |
|
| 496 | - $prefix .= ' ' . $this->getTransactionNestingLevel() . ' '; |
|
| 497 | - |
|
| 498 | - file_put_contents( |
|
| 499 | - $this->systemConfig->getValue('query_log_file', ''), |
|
| 500 | - $prefix . $sql . $postfix . "\n", |
|
| 501 | - FILE_APPEND |
|
| 502 | - ); |
|
| 503 | - } |
|
| 504 | - } |
|
| 505 | - |
|
| 506 | - /** |
|
| 507 | - * Returns the ID of the last inserted row, or the last value from a sequence object, |
|
| 508 | - * depending on the underlying driver. |
|
| 509 | - * |
|
| 510 | - * Note: This method may not return a meaningful or consistent result across different drivers, |
|
| 511 | - * because the underlying database may not even support the notion of AUTO_INCREMENT/IDENTITY |
|
| 512 | - * columns or sequences. |
|
| 513 | - * |
|
| 514 | - * @param string $seqName Name of the sequence object from which the ID should be returned. |
|
| 515 | - * |
|
| 516 | - * @return int the last inserted ID. |
|
| 517 | - * @throws Exception |
|
| 518 | - */ |
|
| 519 | - public function lastInsertId($name = null): int { |
|
| 520 | - if ($name) { |
|
| 521 | - $name = $this->replaceTablePrefix($name); |
|
| 522 | - } |
|
| 523 | - return $this->adapter->lastInsertId($name); |
|
| 524 | - } |
|
| 525 | - |
|
| 526 | - /** |
|
| 527 | - * @internal |
|
| 528 | - * @throws Exception |
|
| 529 | - */ |
|
| 530 | - public function realLastInsertId($seqName = null) { |
|
| 531 | - return parent::lastInsertId($seqName); |
|
| 532 | - } |
|
| 533 | - |
|
| 534 | - /** |
|
| 535 | - * Insert a row if the matching row does not exists. To accomplish proper race condition avoidance |
|
| 536 | - * it is needed that there is also a unique constraint on the values. Then this method will |
|
| 537 | - * catch the exception and return 0. |
|
| 538 | - * |
|
| 539 | - * @param string $table The table name (will replace *PREFIX* with the actual prefix) |
|
| 540 | - * @param array $input data that should be inserted into the table (column name => value) |
|
| 541 | - * @param array|null $compare List of values that should be checked for "if not exists" |
|
| 542 | - * If this is null or an empty array, all keys of $input will be compared |
|
| 543 | - * Please note: text fields (clob) must not be used in the compare array |
|
| 544 | - * @return int number of inserted rows |
|
| 545 | - * @throws \Doctrine\DBAL\Exception |
|
| 546 | - * @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 |
|
| 547 | - */ |
|
| 548 | - public function insertIfNotExist($table, $input, ?array $compare = null) { |
|
| 549 | - try { |
|
| 550 | - return $this->adapter->insertIfNotExist($table, $input, $compare); |
|
| 551 | - } catch (\Exception $e) { |
|
| 552 | - $this->logDatabaseException($e); |
|
| 553 | - throw $e; |
|
| 554 | - } |
|
| 555 | - } |
|
| 556 | - |
|
| 557 | - public function insertIgnoreConflict(string $table, array $values) : int { |
|
| 558 | - try { |
|
| 559 | - return $this->adapter->insertIgnoreConflict($table, $values); |
|
| 560 | - } catch (\Exception $e) { |
|
| 561 | - $this->logDatabaseException($e); |
|
| 562 | - throw $e; |
|
| 563 | - } |
|
| 564 | - } |
|
| 565 | - |
|
| 566 | - private function getType($value) { |
|
| 567 | - if (is_bool($value)) { |
|
| 568 | - return IQueryBuilder::PARAM_BOOL; |
|
| 569 | - } elseif (is_int($value)) { |
|
| 570 | - return IQueryBuilder::PARAM_INT; |
|
| 571 | - } else { |
|
| 572 | - return IQueryBuilder::PARAM_STR; |
|
| 573 | - } |
|
| 574 | - } |
|
| 575 | - |
|
| 576 | - /** |
|
| 577 | - * Insert or update a row value |
|
| 578 | - * |
|
| 579 | - * @param string $table |
|
| 580 | - * @param array $keys (column name => value) |
|
| 581 | - * @param array $values (column name => value) |
|
| 582 | - * @param array $updatePreconditionValues ensure values match preconditions (column name => value) |
|
| 583 | - * @return int number of new rows |
|
| 584 | - * @throws \OCP\DB\Exception |
|
| 585 | - * @throws PreConditionNotMetException |
|
| 586 | - */ |
|
| 587 | - public function setValues(string $table, array $keys, array $values, array $updatePreconditionValues = []): int { |
|
| 588 | - try { |
|
| 589 | - $insertQb = $this->getQueryBuilder(); |
|
| 590 | - $insertQb->insert($table) |
|
| 591 | - ->values( |
|
| 592 | - array_map(function ($value) use ($insertQb) { |
|
| 593 | - return $insertQb->createNamedParameter($value, $this->getType($value)); |
|
| 594 | - }, array_merge($keys, $values)) |
|
| 595 | - ); |
|
| 596 | - return $insertQb->executeStatement(); |
|
| 597 | - } catch (\OCP\DB\Exception $e) { |
|
| 598 | - if (!in_array($e->getReason(), [ |
|
| 599 | - \OCP\DB\Exception::REASON_CONSTRAINT_VIOLATION, |
|
| 600 | - \OCP\DB\Exception::REASON_UNIQUE_CONSTRAINT_VIOLATION, |
|
| 601 | - ]) |
|
| 602 | - ) { |
|
| 603 | - throw $e; |
|
| 604 | - } |
|
| 605 | - |
|
| 606 | - // value already exists, try update |
|
| 607 | - $updateQb = $this->getQueryBuilder(); |
|
| 608 | - $updateQb->update($table); |
|
| 609 | - foreach ($values as $name => $value) { |
|
| 610 | - $updateQb->set($name, $updateQb->createNamedParameter($value, $this->getType($value))); |
|
| 611 | - } |
|
| 612 | - $where = []; |
|
| 613 | - $whereValues = array_merge($keys, $updatePreconditionValues); |
|
| 614 | - foreach ($whereValues as $name => $value) { |
|
| 615 | - if ($value === '') { |
|
| 616 | - $where[] = $updateQb->expr()->emptyString( |
|
| 617 | - $name |
|
| 618 | - ); |
|
| 619 | - } else { |
|
| 620 | - $where[] = $updateQb->expr()->eq( |
|
| 621 | - $name, |
|
| 622 | - $updateQb->createNamedParameter($value, $this->getType($value)), |
|
| 623 | - $this->getType($value) |
|
| 624 | - ); |
|
| 625 | - } |
|
| 626 | - } |
|
| 627 | - $updateQb->where($updateQb->expr()->andX(...$where)); |
|
| 628 | - $affected = $updateQb->executeStatement(); |
|
| 629 | - |
|
| 630 | - if ($affected === 0 && !empty($updatePreconditionValues)) { |
|
| 631 | - throw new PreConditionNotMetException(); |
|
| 632 | - } |
|
| 633 | - |
|
| 634 | - return 0; |
|
| 635 | - } |
|
| 636 | - } |
|
| 637 | - |
|
| 638 | - /** |
|
| 639 | - * Create an exclusive read+write lock on a table |
|
| 640 | - * |
|
| 641 | - * @param string $tableName |
|
| 642 | - * |
|
| 643 | - * @throws \BadMethodCallException When trying to acquire a second lock |
|
| 644 | - * @throws Exception |
|
| 645 | - * @since 9.1.0 |
|
| 646 | - */ |
|
| 647 | - public function lockTable($tableName) { |
|
| 648 | - if ($this->lockedTable !== null) { |
|
| 649 | - throw new \BadMethodCallException('Can not lock a new table until the previous lock is released.'); |
|
| 650 | - } |
|
| 651 | - |
|
| 652 | - $tableName = $this->tablePrefix . $tableName; |
|
| 653 | - $this->lockedTable = $tableName; |
|
| 654 | - $this->adapter->lockTable($tableName); |
|
| 655 | - } |
|
| 656 | - |
|
| 657 | - /** |
|
| 658 | - * Release a previous acquired lock again |
|
| 659 | - * |
|
| 660 | - * @throws Exception |
|
| 661 | - * @since 9.1.0 |
|
| 662 | - */ |
|
| 663 | - public function unlockTable() { |
|
| 664 | - $this->adapter->unlockTable(); |
|
| 665 | - $this->lockedTable = null; |
|
| 666 | - } |
|
| 667 | - |
|
| 668 | - /** |
|
| 669 | - * returns the error code and message as a string for logging |
|
| 670 | - * works with DoctrineException |
|
| 671 | - * @return string |
|
| 672 | - */ |
|
| 673 | - public function getError() { |
|
| 674 | - $msg = $this->errorCode() . ': '; |
|
| 675 | - $errorInfo = $this->errorInfo(); |
|
| 676 | - if (!empty($errorInfo)) { |
|
| 677 | - $msg .= 'SQLSTATE = ' . $errorInfo[0] . ', '; |
|
| 678 | - $msg .= 'Driver Code = ' . $errorInfo[1] . ', '; |
|
| 679 | - $msg .= 'Driver Message = ' . $errorInfo[2]; |
|
| 680 | - } |
|
| 681 | - return $msg; |
|
| 682 | - } |
|
| 683 | - |
|
| 684 | - public function errorCode() { |
|
| 685 | - return -1; |
|
| 686 | - } |
|
| 687 | - |
|
| 688 | - public function errorInfo() { |
|
| 689 | - return []; |
|
| 690 | - } |
|
| 691 | - |
|
| 692 | - /** |
|
| 693 | - * Drop a table from the database if it exists |
|
| 694 | - * |
|
| 695 | - * @param string $table table name without the prefix |
|
| 696 | - * |
|
| 697 | - * @throws Exception |
|
| 698 | - */ |
|
| 699 | - public function dropTable($table) { |
|
| 700 | - $table = $this->tablePrefix . trim($table); |
|
| 701 | - $schema = $this->createSchemaManager(); |
|
| 702 | - if ($schema->tablesExist([$table])) { |
|
| 703 | - $schema->dropTable($table); |
|
| 704 | - } |
|
| 705 | - } |
|
| 706 | - |
|
| 707 | - /** |
|
| 708 | - * Truncate a table data if it exists |
|
| 709 | - * |
|
| 710 | - * @param string $table table name without the prefix |
|
| 711 | - * @param bool $cascade whether to truncate cascading |
|
| 712 | - * |
|
| 713 | - * @throws Exception |
|
| 714 | - */ |
|
| 715 | - public function truncateTable(string $table, bool $cascade) { |
|
| 716 | - $this->executeStatement($this->getDatabasePlatform() |
|
| 717 | - ->getTruncateTableSQL($this->tablePrefix . trim($table), $cascade)); |
|
| 718 | - } |
|
| 719 | - |
|
| 720 | - /** |
|
| 721 | - * Check if a table exists |
|
| 722 | - * |
|
| 723 | - * @param string $table table name without the prefix |
|
| 724 | - * |
|
| 725 | - * @return bool |
|
| 726 | - * @throws Exception |
|
| 727 | - */ |
|
| 728 | - public function tableExists($table) { |
|
| 729 | - $table = $this->tablePrefix . trim($table); |
|
| 730 | - $schema = $this->createSchemaManager(); |
|
| 731 | - return $schema->tablesExist([$table]); |
|
| 732 | - } |
|
| 733 | - |
|
| 734 | - protected function finishQuery(string $statement): string { |
|
| 735 | - $statement = $this->replaceTablePrefix($statement); |
|
| 736 | - $statement = $this->adapter->fixupStatement($statement); |
|
| 737 | - if ($this->logRequestId) { |
|
| 738 | - return $statement . ' /* reqid: ' . $this->requestId . ' */'; |
|
| 739 | - } else { |
|
| 740 | - return $statement; |
|
| 741 | - } |
|
| 742 | - } |
|
| 743 | - |
|
| 744 | - // internal use |
|
| 745 | - /** |
|
| 746 | - * @param string $statement |
|
| 747 | - * @return string |
|
| 748 | - */ |
|
| 749 | - protected function replaceTablePrefix($statement) { |
|
| 750 | - return str_replace('*PREFIX*', $this->tablePrefix, $statement); |
|
| 751 | - } |
|
| 752 | - |
|
| 753 | - /** |
|
| 754 | - * Check if a transaction is active |
|
| 755 | - * |
|
| 756 | - * @return bool |
|
| 757 | - * @since 8.2.0 |
|
| 758 | - */ |
|
| 759 | - public function inTransaction() { |
|
| 760 | - return $this->getTransactionNestingLevel() > 0; |
|
| 761 | - } |
|
| 762 | - |
|
| 763 | - /** |
|
| 764 | - * Escape a parameter to be used in a LIKE query |
|
| 765 | - * |
|
| 766 | - * @param string $param |
|
| 767 | - * @return string |
|
| 768 | - */ |
|
| 769 | - public function escapeLikeParameter($param) { |
|
| 770 | - return addcslashes($param, '\\_%'); |
|
| 771 | - } |
|
| 772 | - |
|
| 773 | - /** |
|
| 774 | - * Check whether or not the current database support 4byte wide unicode |
|
| 775 | - * |
|
| 776 | - * @return bool |
|
| 777 | - * @since 11.0.0 |
|
| 778 | - */ |
|
| 779 | - public function supports4ByteText() { |
|
| 780 | - if (!$this->getDatabasePlatform() instanceof MySQLPlatform) { |
|
| 781 | - return true; |
|
| 782 | - } |
|
| 783 | - return $this->getParams()['charset'] === 'utf8mb4'; |
|
| 784 | - } |
|
| 785 | - |
|
| 786 | - |
|
| 787 | - /** |
|
| 788 | - * Create the schema of the connected database |
|
| 789 | - * |
|
| 790 | - * @return Schema |
|
| 791 | - * @throws Exception |
|
| 792 | - */ |
|
| 793 | - public function createSchema() { |
|
| 794 | - $migrator = $this->getMigrator(); |
|
| 795 | - return $migrator->createSchema(); |
|
| 796 | - } |
|
| 797 | - |
|
| 798 | - /** |
|
| 799 | - * Migrate the database to the given schema |
|
| 800 | - * |
|
| 801 | - * @param Schema $toSchema |
|
| 802 | - * @param bool $dryRun If true, will return the sql queries instead of running them. |
|
| 803 | - * |
|
| 804 | - * @throws Exception |
|
| 805 | - * |
|
| 806 | - * @return string|null Returns a string only if $dryRun is true. |
|
| 807 | - */ |
|
| 808 | - public function migrateToSchema(Schema $toSchema, bool $dryRun = false) { |
|
| 809 | - $migrator = $this->getMigrator(); |
|
| 810 | - |
|
| 811 | - if ($dryRun) { |
|
| 812 | - return $migrator->generateChangeScript($toSchema); |
|
| 813 | - } else { |
|
| 814 | - $migrator->migrate($toSchema); |
|
| 815 | - foreach ($this->getShardConnections() as $shardConnection) { |
|
| 816 | - $shardConnection->migrateToSchema($toSchema); |
|
| 817 | - } |
|
| 818 | - } |
|
| 819 | - } |
|
| 820 | - |
|
| 821 | - private function getMigrator() { |
|
| 822 | - // TODO properly inject those dependencies |
|
| 823 | - $random = \OC::$server->get(ISecureRandom::class); |
|
| 824 | - $platform = $this->getDatabasePlatform(); |
|
| 825 | - $config = \OC::$server->getConfig(); |
|
| 826 | - $dispatcher = Server::get(\OCP\EventDispatcher\IEventDispatcher::class); |
|
| 827 | - if ($platform instanceof SqlitePlatform) { |
|
| 828 | - return new SQLiteMigrator($this, $config, $dispatcher); |
|
| 829 | - } elseif ($platform instanceof OraclePlatform) { |
|
| 830 | - return new OracleMigrator($this, $config, $dispatcher); |
|
| 831 | - } else { |
|
| 832 | - return new Migrator($this, $config, $dispatcher); |
|
| 833 | - } |
|
| 834 | - } |
|
| 835 | - |
|
| 836 | - public function beginTransaction() { |
|
| 837 | - if (!$this->inTransaction()) { |
|
| 838 | - $this->transactionBacktrace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS); |
|
| 839 | - $this->transactionActiveSince = microtime(true); |
|
| 840 | - } |
|
| 841 | - return parent::beginTransaction(); |
|
| 842 | - } |
|
| 843 | - |
|
| 844 | - public function commit() { |
|
| 845 | - $result = parent::commit(); |
|
| 846 | - if ($this->getTransactionNestingLevel() === 0) { |
|
| 847 | - $timeTook = microtime(true) - $this->transactionActiveSince; |
|
| 848 | - $this->transactionBacktrace = null; |
|
| 849 | - $this->transactionActiveSince = null; |
|
| 850 | - if ($timeTook > 1) { |
|
| 851 | - $logLevel = match (true) { |
|
| 852 | - $timeTook > 20 * 60 => ILogger::ERROR, |
|
| 853 | - $timeTook > 5 * 60 => ILogger::WARN, |
|
| 854 | - $timeTook > 10 => ILogger::INFO, |
|
| 855 | - default => ILogger::DEBUG, |
|
| 856 | - }; |
|
| 857 | - $this->logger->log( |
|
| 858 | - $logLevel, |
|
| 859 | - 'Transaction took ' . $timeTook . 's', |
|
| 860 | - [ |
|
| 861 | - 'exception' => new \Exception('Transaction took ' . $timeTook . 's'), |
|
| 862 | - 'timeSpent' => $timeTook, |
|
| 863 | - ] |
|
| 864 | - ); |
|
| 865 | - } |
|
| 866 | - } |
|
| 867 | - return $result; |
|
| 868 | - } |
|
| 869 | - |
|
| 870 | - public function rollBack() { |
|
| 871 | - $result = parent::rollBack(); |
|
| 872 | - if ($this->getTransactionNestingLevel() === 0) { |
|
| 873 | - $timeTook = microtime(true) - $this->transactionActiveSince; |
|
| 874 | - $this->transactionBacktrace = null; |
|
| 875 | - $this->transactionActiveSince = null; |
|
| 876 | - if ($timeTook > 1) { |
|
| 877 | - $logLevel = match (true) { |
|
| 878 | - $timeTook > 20 * 60 => ILogger::ERROR, |
|
| 879 | - $timeTook > 5 * 60 => ILogger::WARN, |
|
| 880 | - $timeTook > 10 => ILogger::INFO, |
|
| 881 | - default => ILogger::DEBUG, |
|
| 882 | - }; |
|
| 883 | - $this->logger->log( |
|
| 884 | - $logLevel, |
|
| 885 | - 'Transaction rollback took longer than 1s: ' . $timeTook, |
|
| 886 | - [ |
|
| 887 | - 'exception' => new \Exception('Long running transaction rollback'), |
|
| 888 | - 'timeSpent' => $timeTook, |
|
| 889 | - ] |
|
| 890 | - ); |
|
| 891 | - } |
|
| 892 | - } |
|
| 893 | - return $result; |
|
| 894 | - } |
|
| 895 | - |
|
| 896 | - private function reconnectIfNeeded(): void { |
|
| 897 | - if ( |
|
| 898 | - !isset($this->lastConnectionCheck[$this->getConnectionName()]) |
|
| 899 | - || time() <= $this->lastConnectionCheck[$this->getConnectionName()] + 30 |
|
| 900 | - || $this->isTransactionActive() |
|
| 901 | - ) { |
|
| 902 | - return; |
|
| 903 | - } |
|
| 904 | - |
|
| 905 | - try { |
|
| 906 | - $this->_conn->query($this->getDriver()->getDatabasePlatform()->getDummySelectSQL()); |
|
| 907 | - $this->lastConnectionCheck[$this->getConnectionName()] = time(); |
|
| 908 | - } catch (ConnectionLost|\Exception $e) { |
|
| 909 | - $this->logger->warning('Exception during connectivity check, closing and reconnecting', ['exception' => $e]); |
|
| 910 | - $this->close(); |
|
| 911 | - } |
|
| 912 | - } |
|
| 913 | - |
|
| 914 | - private function getConnectionName(): string { |
|
| 915 | - return $this->isConnectedToPrimary() ? 'primary' : 'replica'; |
|
| 916 | - } |
|
| 917 | - |
|
| 918 | - /** |
|
| 919 | - * @return IDBConnection::PLATFORM_MYSQL|IDBConnection::PLATFORM_ORACLE|IDBConnection::PLATFORM_POSTGRES|IDBConnection::PLATFORM_SQLITE|IDBConnection::PLATFORM_MARIADB |
|
| 920 | - */ |
|
| 921 | - public function getDatabaseProvider(bool $strict = false): string { |
|
| 922 | - $platform = $this->getDatabasePlatform(); |
|
| 923 | - if ($strict && $platform instanceof MariaDBPlatform) { |
|
| 924 | - return IDBConnection::PLATFORM_MARIADB; |
|
| 925 | - } elseif ($platform instanceof MySQLPlatform) { |
|
| 926 | - return IDBConnection::PLATFORM_MYSQL; |
|
| 927 | - } elseif ($platform instanceof OraclePlatform) { |
|
| 928 | - return IDBConnection::PLATFORM_ORACLE; |
|
| 929 | - } elseif ($platform instanceof PostgreSQLPlatform) { |
|
| 930 | - return IDBConnection::PLATFORM_POSTGRES; |
|
| 931 | - } elseif ($platform instanceof SqlitePlatform) { |
|
| 932 | - return IDBConnection::PLATFORM_SQLITE; |
|
| 933 | - } else { |
|
| 934 | - throw new \Exception('Database ' . $platform::class . ' not supported'); |
|
| 935 | - } |
|
| 936 | - } |
|
| 937 | - |
|
| 938 | - /** |
|
| 939 | - * @internal Should only be used inside the QueryBuilder, ExpressionBuilder and FunctionBuilder |
|
| 940 | - * All apps and API code should not need this and instead use provided functionality from the above. |
|
| 941 | - */ |
|
| 942 | - public function getServerVersion(): string { |
|
| 943 | - /** @var ServerInfoAwareConnection $this->_conn */ |
|
| 944 | - return $this->_conn->getServerVersion(); |
|
| 945 | - } |
|
| 946 | - |
|
| 947 | - /** |
|
| 948 | - * Log a database exception if enabled |
|
| 949 | - * |
|
| 950 | - * @param \Exception $exception |
|
| 951 | - * @return void |
|
| 952 | - */ |
|
| 953 | - public function logDatabaseException(\Exception $exception): void { |
|
| 954 | - if ($this->logDbException) { |
|
| 955 | - if ($exception instanceof Exception\UniqueConstraintViolationException) { |
|
| 956 | - $this->logger->info($exception->getMessage(), ['exception' => $exception, 'transaction' => $this->transactionBacktrace]); |
|
| 957 | - } else { |
|
| 958 | - $this->logger->error($exception->getMessage(), ['exception' => $exception, 'transaction' => $this->transactionBacktrace]); |
|
| 959 | - } |
|
| 960 | - } |
|
| 961 | - } |
|
| 962 | - |
|
| 963 | - public function getShardDefinition(string $name): ?ShardDefinition { |
|
| 964 | - return $this->shards[$name] ?? null; |
|
| 965 | - } |
|
| 966 | - |
|
| 967 | - public function getCrossShardMoveHelper(): CrossShardMoveHelper { |
|
| 968 | - return new CrossShardMoveHelper($this->shardConnectionManager); |
|
| 969 | - } |
|
| 53 | + /** @var string */ |
|
| 54 | + protected $tablePrefix; |
|
| 55 | + |
|
| 56 | + /** @var \OC\DB\Adapter $adapter */ |
|
| 57 | + protected $adapter; |
|
| 58 | + |
|
| 59 | + /** @var SystemConfig */ |
|
| 60 | + private $systemConfig; |
|
| 61 | + |
|
| 62 | + private ClockInterface $clock; |
|
| 63 | + |
|
| 64 | + private LoggerInterface $logger; |
|
| 65 | + |
|
| 66 | + protected $lockedTable = null; |
|
| 67 | + |
|
| 68 | + /** @var int */ |
|
| 69 | + protected $queriesBuilt = 0; |
|
| 70 | + |
|
| 71 | + /** @var int */ |
|
| 72 | + protected $queriesExecuted = 0; |
|
| 73 | + |
|
| 74 | + /** @var DbDataCollector|null */ |
|
| 75 | + protected $dbDataCollector = null; |
|
| 76 | + private array $lastConnectionCheck = []; |
|
| 77 | + |
|
| 78 | + protected ?float $transactionActiveSince = null; |
|
| 79 | + |
|
| 80 | + /** @var array<string, int> */ |
|
| 81 | + protected $tableDirtyWrites = []; |
|
| 82 | + |
|
| 83 | + protected bool $logDbException = false; |
|
| 84 | + private ?array $transactionBacktrace = null; |
|
| 85 | + |
|
| 86 | + protected bool $logRequestId; |
|
| 87 | + protected string $requestId; |
|
| 88 | + |
|
| 89 | + /** @var array<string, list<string>> */ |
|
| 90 | + protected array $partitions; |
|
| 91 | + /** @var ShardDefinition[] */ |
|
| 92 | + protected array $shards = []; |
|
| 93 | + protected ShardConnectionManager $shardConnectionManager; |
|
| 94 | + protected AutoIncrementHandler $autoIncrementHandler; |
|
| 95 | + protected bool $isShardingEnabled; |
|
| 96 | + |
|
| 97 | + public const SHARD_PRESETS = [ |
|
| 98 | + 'filecache' => [ |
|
| 99 | + 'companion_keys' => [ |
|
| 100 | + 'file_id', |
|
| 101 | + ], |
|
| 102 | + 'companion_tables' => [ |
|
| 103 | + 'filecache_extended', |
|
| 104 | + 'files_metadata', |
|
| 105 | + ], |
|
| 106 | + 'primary_key' => 'fileid', |
|
| 107 | + 'shard_key' => 'storage', |
|
| 108 | + 'table' => 'filecache', |
|
| 109 | + ], |
|
| 110 | + ]; |
|
| 111 | + |
|
| 112 | + /** |
|
| 113 | + * Initializes a new instance of the Connection class. |
|
| 114 | + * |
|
| 115 | + * @throws \Exception |
|
| 116 | + */ |
|
| 117 | + public function __construct( |
|
| 118 | + private array $params, |
|
| 119 | + Driver $driver, |
|
| 120 | + ?Configuration $config = null, |
|
| 121 | + ?EventManager $eventManager = null, |
|
| 122 | + ) { |
|
| 123 | + if (!isset($params['adapter'])) { |
|
| 124 | + throw new \Exception('adapter not set'); |
|
| 125 | + } |
|
| 126 | + if (!isset($params['tablePrefix'])) { |
|
| 127 | + throw new \Exception('tablePrefix not set'); |
|
| 128 | + } |
|
| 129 | + /** |
|
| 130 | + * @psalm-suppress InternalMethod |
|
| 131 | + */ |
|
| 132 | + parent::__construct($params, $driver, $config, $eventManager); |
|
| 133 | + $this->adapter = new $params['adapter']($this); |
|
| 134 | + $this->tablePrefix = $params['tablePrefix']; |
|
| 135 | + $this->isShardingEnabled = isset($this->params['sharding']) && !empty($this->params['sharding']); |
|
| 136 | + |
|
| 137 | + if ($this->isShardingEnabled) { |
|
| 138 | + /** @psalm-suppress InvalidArrayOffset */ |
|
| 139 | + $this->shardConnectionManager = $this->params['shard_connection_manager'] ?? Server::get(ShardConnectionManager::class); |
|
| 140 | + /** @psalm-suppress InvalidArrayOffset */ |
|
| 141 | + $this->autoIncrementHandler = $this->params['auto_increment_handler'] ?? new AutoIncrementHandler( |
|
| 142 | + Server::get(ICacheFactory::class), |
|
| 143 | + $this->shardConnectionManager, |
|
| 144 | + ); |
|
| 145 | + } |
|
| 146 | + $this->systemConfig = \OC::$server->getSystemConfig(); |
|
| 147 | + $this->clock = Server::get(ClockInterface::class); |
|
| 148 | + $this->logger = Server::get(LoggerInterface::class); |
|
| 149 | + |
|
| 150 | + $this->logRequestId = $this->systemConfig->getValue('db.log_request_id', false); |
|
| 151 | + $this->logDbException = $this->systemConfig->getValue('db.log_exceptions', false); |
|
| 152 | + $this->requestId = Server::get(IRequestId::class)->getId(); |
|
| 153 | + |
|
| 154 | + /** @var \OCP\Profiler\IProfiler */ |
|
| 155 | + $profiler = Server::get(IProfiler::class); |
|
| 156 | + if ($profiler->isEnabled()) { |
|
| 157 | + $this->dbDataCollector = new DbDataCollector($this); |
|
| 158 | + $profiler->add($this->dbDataCollector); |
|
| 159 | + $debugStack = new BacktraceDebugStack(); |
|
| 160 | + $this->dbDataCollector->setDebugStack($debugStack); |
|
| 161 | + $this->_config->setSQLLogger($debugStack); |
|
| 162 | + } |
|
| 163 | + |
|
| 164 | + /** @var array<string, array{shards: array[], mapper: ?string, from_primary_key: ?int, from_shard_key: ?int}> $shardConfig */ |
|
| 165 | + $shardConfig = $this->params['sharding'] ?? []; |
|
| 166 | + $shardNames = array_keys($shardConfig); |
|
| 167 | + $this->shards = array_map(function (array $config, string $name) { |
|
| 168 | + if (!isset(self::SHARD_PRESETS[$name])) { |
|
| 169 | + throw new \Exception("Shard preset $name not found"); |
|
| 170 | + } |
|
| 171 | + |
|
| 172 | + $shardMapperClass = $config['mapper'] ?? RoundRobinShardMapper::class; |
|
| 173 | + $shardMapper = Server::get($shardMapperClass); |
|
| 174 | + if (!$shardMapper instanceof IShardMapper) { |
|
| 175 | + throw new \Exception("Invalid shard mapper: $shardMapperClass"); |
|
| 176 | + } |
|
| 177 | + return new ShardDefinition( |
|
| 178 | + self::SHARD_PRESETS[$name]['table'], |
|
| 179 | + self::SHARD_PRESETS[$name]['primary_key'], |
|
| 180 | + self::SHARD_PRESETS[$name]['companion_keys'], |
|
| 181 | + self::SHARD_PRESETS[$name]['shard_key'], |
|
| 182 | + $shardMapper, |
|
| 183 | + self::SHARD_PRESETS[$name]['companion_tables'], |
|
| 184 | + $config['shards'], |
|
| 185 | + $config['from_primary_key'] ?? 0, |
|
| 186 | + $config['from_shard_key'] ?? 0, |
|
| 187 | + ); |
|
| 188 | + }, $shardConfig, $shardNames); |
|
| 189 | + $this->shards = array_combine($shardNames, $this->shards); |
|
| 190 | + $this->partitions = array_map(function (ShardDefinition $shard) { |
|
| 191 | + return array_merge([$shard->table], $shard->companionTables); |
|
| 192 | + }, $this->shards); |
|
| 193 | + |
|
| 194 | + $this->setNestTransactionsWithSavepoints(true); |
|
| 195 | + } |
|
| 196 | + |
|
| 197 | + /** |
|
| 198 | + * @return IDBConnection[] |
|
| 199 | + */ |
|
| 200 | + public function getShardConnections(): array { |
|
| 201 | + $connections = []; |
|
| 202 | + if ($this->isShardingEnabled) { |
|
| 203 | + foreach ($this->shards as $shardDefinition) { |
|
| 204 | + foreach ($shardDefinition->getAllShards() as $shard) { |
|
| 205 | + if ($shard !== ShardDefinition::MIGRATION_SHARD) { |
|
| 206 | + /** @var ConnectionAdapter $connection */ |
|
| 207 | + $connections[] = $this->shardConnectionManager->getConnection($shardDefinition, $shard); |
|
| 208 | + } |
|
| 209 | + } |
|
| 210 | + } |
|
| 211 | + } |
|
| 212 | + return $connections; |
|
| 213 | + } |
|
| 214 | + |
|
| 215 | + /** |
|
| 216 | + * @throws Exception |
|
| 217 | + */ |
|
| 218 | + public function connect($connectionName = null) { |
|
| 219 | + try { |
|
| 220 | + if ($this->_conn) { |
|
| 221 | + $this->reconnectIfNeeded(); |
|
| 222 | + /** @psalm-suppress InternalMethod */ |
|
| 223 | + return parent::connect(); |
|
| 224 | + } |
|
| 225 | + |
|
| 226 | + // Only trigger the event logger for the initial connect call |
|
| 227 | + $eventLogger = Server::get(IEventLogger::class); |
|
| 228 | + $eventLogger->start('connect:db', 'db connection opened'); |
|
| 229 | + /** @psalm-suppress InternalMethod */ |
|
| 230 | + $status = parent::connect(); |
|
| 231 | + $eventLogger->end('connect:db'); |
|
| 232 | + |
|
| 233 | + $this->lastConnectionCheck[$this->getConnectionName()] = time(); |
|
| 234 | + |
|
| 235 | + return $status; |
|
| 236 | + } catch (Exception $e) { |
|
| 237 | + // throw a new exception to prevent leaking info from the stacktrace |
|
| 238 | + throw new Exception('Failed to connect to the database: ' . $e->getMessage(), $e->getCode()); |
|
| 239 | + } |
|
| 240 | + } |
|
| 241 | + |
|
| 242 | + protected function performConnect(?string $connectionName = null): bool { |
|
| 243 | + if (($connectionName ?? 'replica') === 'replica' |
|
| 244 | + && count($this->params['replica']) === 1 |
|
| 245 | + && $this->params['primary'] === $this->params['replica'][0]) { |
|
| 246 | + return parent::performConnect('primary'); |
|
| 247 | + } |
|
| 248 | + return parent::performConnect($connectionName); |
|
| 249 | + } |
|
| 250 | + |
|
| 251 | + public function getStats(): array { |
|
| 252 | + return [ |
|
| 253 | + 'built' => $this->queriesBuilt, |
|
| 254 | + 'executed' => $this->queriesExecuted, |
|
| 255 | + ]; |
|
| 256 | + } |
|
| 257 | + |
|
| 258 | + /** |
|
| 259 | + * Returns a QueryBuilder for the connection. |
|
| 260 | + */ |
|
| 261 | + public function getQueryBuilder(): IQueryBuilder { |
|
| 262 | + $this->queriesBuilt++; |
|
| 263 | + |
|
| 264 | + $builder = new QueryBuilder( |
|
| 265 | + new ConnectionAdapter($this), |
|
| 266 | + $this->systemConfig, |
|
| 267 | + $this->logger |
|
| 268 | + ); |
|
| 269 | + if ($this->isShardingEnabled && count($this->partitions) > 0) { |
|
| 270 | + $builder = new PartitionedQueryBuilder( |
|
| 271 | + $builder, |
|
| 272 | + $this->shards, |
|
| 273 | + $this->shardConnectionManager, |
|
| 274 | + $this->autoIncrementHandler, |
|
| 275 | + ); |
|
| 276 | + foreach ($this->partitions as $name => $tables) { |
|
| 277 | + $partition = new PartitionSplit($name, $tables); |
|
| 278 | + $builder->addPartition($partition); |
|
| 279 | + } |
|
| 280 | + return $builder; |
|
| 281 | + } else { |
|
| 282 | + return $builder; |
|
| 283 | + } |
|
| 284 | + } |
|
| 285 | + |
|
| 286 | + /** |
|
| 287 | + * Gets the QueryBuilder for the connection. |
|
| 288 | + * |
|
| 289 | + * @return \Doctrine\DBAL\Query\QueryBuilder |
|
| 290 | + * @deprecated 8.0.0 please use $this->getQueryBuilder() instead |
|
| 291 | + */ |
|
| 292 | + public function createQueryBuilder() { |
|
| 293 | + $backtrace = $this->getCallerBacktrace(); |
|
| 294 | + $this->logger->debug('Doctrine QueryBuilder retrieved in {backtrace}', ['app' => 'core', 'backtrace' => $backtrace]); |
|
| 295 | + $this->queriesBuilt++; |
|
| 296 | + return parent::createQueryBuilder(); |
|
| 297 | + } |
|
| 298 | + |
|
| 299 | + /** |
|
| 300 | + * Gets the ExpressionBuilder for the connection. |
|
| 301 | + * |
|
| 302 | + * @return \Doctrine\DBAL\Query\Expression\ExpressionBuilder |
|
| 303 | + * @deprecated 8.0.0 please use $this->getQueryBuilder()->expr() instead |
|
| 304 | + */ |
|
| 305 | + public function getExpressionBuilder() { |
|
| 306 | + $backtrace = $this->getCallerBacktrace(); |
|
| 307 | + $this->logger->debug('Doctrine ExpressionBuilder retrieved in {backtrace}', ['app' => 'core', 'backtrace' => $backtrace]); |
|
| 308 | + $this->queriesBuilt++; |
|
| 309 | + return parent::getExpressionBuilder(); |
|
| 310 | + } |
|
| 311 | + |
|
| 312 | + /** |
|
| 313 | + * Get the file and line that called the method where `getCallerBacktrace()` was used |
|
| 314 | + * |
|
| 315 | + * @return string |
|
| 316 | + */ |
|
| 317 | + protected function getCallerBacktrace() { |
|
| 318 | + $traces = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 2); |
|
| 319 | + |
|
| 320 | + // 0 is the method where we use `getCallerBacktrace` |
|
| 321 | + // 1 is the target method which uses the method we want to log |
|
| 322 | + if (isset($traces[1])) { |
|
| 323 | + return $traces[1]['file'] . ':' . $traces[1]['line']; |
|
| 324 | + } |
|
| 325 | + |
|
| 326 | + return ''; |
|
| 327 | + } |
|
| 328 | + |
|
| 329 | + /** |
|
| 330 | + * @return string |
|
| 331 | + */ |
|
| 332 | + public function getPrefix() { |
|
| 333 | + return $this->tablePrefix; |
|
| 334 | + } |
|
| 335 | + |
|
| 336 | + /** |
|
| 337 | + * Prepares an SQL statement. |
|
| 338 | + * |
|
| 339 | + * @param string $statement The SQL statement to prepare. |
|
| 340 | + * @param int|null $limit |
|
| 341 | + * @param int|null $offset |
|
| 342 | + * |
|
| 343 | + * @return Statement The prepared statement. |
|
| 344 | + * @throws Exception |
|
| 345 | + */ |
|
| 346 | + public function prepare($sql, $limit = null, $offset = null): Statement { |
|
| 347 | + if ($limit === -1 || $limit === null) { |
|
| 348 | + $limit = null; |
|
| 349 | + } else { |
|
| 350 | + $limit = (int)$limit; |
|
| 351 | + } |
|
| 352 | + if ($offset !== null) { |
|
| 353 | + $offset = (int)$offset; |
|
| 354 | + } |
|
| 355 | + if (!is_null($limit)) { |
|
| 356 | + $platform = $this->getDatabasePlatform(); |
|
| 357 | + $sql = $platform->modifyLimitQuery($sql, $limit, $offset); |
|
| 358 | + } |
|
| 359 | + $statement = $this->finishQuery($sql); |
|
| 360 | + |
|
| 361 | + return parent::prepare($statement); |
|
| 362 | + } |
|
| 363 | + |
|
| 364 | + /** |
|
| 365 | + * Executes an, optionally parametrized, SQL query. |
|
| 366 | + * |
|
| 367 | + * If the query is parametrized, a prepared statement is used. |
|
| 368 | + * If an SQLLogger is configured, the execution is logged. |
|
| 369 | + * |
|
| 370 | + * @param string $sql The SQL query to execute. |
|
| 371 | + * @param array $params The parameters to bind to the query, if any. |
|
| 372 | + * @param array $types The types the previous parameters are in. |
|
| 373 | + * @param \Doctrine\DBAL\Cache\QueryCacheProfile|null $qcp The query cache profile, optional. |
|
| 374 | + * |
|
| 375 | + * @return Result The executed statement. |
|
| 376 | + * |
|
| 377 | + * @throws \Doctrine\DBAL\Exception |
|
| 378 | + */ |
|
| 379 | + public function executeQuery(string $sql, array $params = [], $types = [], ?QueryCacheProfile $qcp = null): Result { |
|
| 380 | + $tables = $this->getQueriedTables($sql); |
|
| 381 | + $now = $this->clock->now()->getTimestamp(); |
|
| 382 | + $dirtyTableWrites = []; |
|
| 383 | + foreach ($tables as $table) { |
|
| 384 | + $lastAccess = $this->tableDirtyWrites[$table] ?? 0; |
|
| 385 | + // Only very recent writes are considered dirty |
|
| 386 | + if ($lastAccess >= ($now - 3)) { |
|
| 387 | + $dirtyTableWrites[] = $table; |
|
| 388 | + } |
|
| 389 | + } |
|
| 390 | + if ($this->isTransactionActive()) { |
|
| 391 | + // Transacted queries go to the primary. The consistency of the primary guarantees that we can not run |
|
| 392 | + // into a dirty read. |
|
| 393 | + } elseif (count($dirtyTableWrites) === 0) { |
|
| 394 | + // No tables read that could have been written already in the same request and no transaction active |
|
| 395 | + // so we can switch back to the replica for reading as long as no writes happen that switch back to the primary |
|
| 396 | + // We cannot log here as this would log too early in the server boot process |
|
| 397 | + $this->ensureConnectedToReplica(); |
|
| 398 | + } else { |
|
| 399 | + // Read to a table that has been written to previously |
|
| 400 | + // While this might not necessarily mean that we did a read after write it is an indication for a code path to check |
|
| 401 | + $this->logger->log( |
|
| 402 | + (int)($this->systemConfig->getValue('loglevel_dirty_database_queries', null) ?? 0), |
|
| 403 | + 'dirty table reads: ' . $sql, |
|
| 404 | + [ |
|
| 405 | + 'tables' => array_keys($this->tableDirtyWrites), |
|
| 406 | + 'reads' => $tables, |
|
| 407 | + 'exception' => new \Exception('dirty table reads: ' . $sql), |
|
| 408 | + ], |
|
| 409 | + ); |
|
| 410 | + // To prevent a dirty read on a replica that is slightly out of sync, we |
|
| 411 | + // switch back to the primary. This is detrimental for performance but |
|
| 412 | + // safer for consistency. |
|
| 413 | + $this->ensureConnectedToPrimary(); |
|
| 414 | + } |
|
| 415 | + |
|
| 416 | + $sql = $this->finishQuery($sql); |
|
| 417 | + $this->queriesExecuted++; |
|
| 418 | + $this->logQueryToFile($sql, $params); |
|
| 419 | + try { |
|
| 420 | + return parent::executeQuery($sql, $params, $types, $qcp); |
|
| 421 | + } catch (\Exception $e) { |
|
| 422 | + $this->logDatabaseException($e); |
|
| 423 | + throw $e; |
|
| 424 | + } |
|
| 425 | + } |
|
| 426 | + |
|
| 427 | + /** |
|
| 428 | + * Helper function to get the list of tables affected by a given query |
|
| 429 | + * used to track dirty tables that received a write with the current request |
|
| 430 | + */ |
|
| 431 | + private function getQueriedTables(string $sql): array { |
|
| 432 | + $re = '/(\*PREFIX\*\w+)/mi'; |
|
| 433 | + preg_match_all($re, $sql, $matches); |
|
| 434 | + return array_map([$this, 'replaceTablePrefix'], $matches[0] ?? []); |
|
| 435 | + } |
|
| 436 | + |
|
| 437 | + /** |
|
| 438 | + * @throws Exception |
|
| 439 | + */ |
|
| 440 | + public function executeUpdate(string $sql, array $params = [], array $types = []): int { |
|
| 441 | + return $this->executeStatement($sql, $params, $types); |
|
| 442 | + } |
|
| 443 | + |
|
| 444 | + /** |
|
| 445 | + * Executes an SQL INSERT/UPDATE/DELETE query with the given parameters |
|
| 446 | + * and returns the number of affected rows. |
|
| 447 | + * |
|
| 448 | + * This method supports PDO binding types as well as DBAL mapping types. |
|
| 449 | + * |
|
| 450 | + * @param string $sql The SQL query. |
|
| 451 | + * @param array $params The query parameters. |
|
| 452 | + * @param array $types The parameter types. |
|
| 453 | + * |
|
| 454 | + * @return int The number of affected rows. |
|
| 455 | + * |
|
| 456 | + * @throws \Doctrine\DBAL\Exception |
|
| 457 | + */ |
|
| 458 | + public function executeStatement($sql, array $params = [], array $types = []): int { |
|
| 459 | + $tables = $this->getQueriedTables($sql); |
|
| 460 | + foreach ($tables as $table) { |
|
| 461 | + $this->tableDirtyWrites[$table] = $this->clock->now()->getTimestamp(); |
|
| 462 | + } |
|
| 463 | + $sql = $this->finishQuery($sql); |
|
| 464 | + $this->queriesExecuted++; |
|
| 465 | + $this->logQueryToFile($sql, $params); |
|
| 466 | + try { |
|
| 467 | + return (int)parent::executeStatement($sql, $params, $types); |
|
| 468 | + } catch (\Exception $e) { |
|
| 469 | + $this->logDatabaseException($e); |
|
| 470 | + throw $e; |
|
| 471 | + } |
|
| 472 | + } |
|
| 473 | + |
|
| 474 | + protected function logQueryToFile(string $sql, array $params): void { |
|
| 475 | + $logFile = $this->systemConfig->getValue('query_log_file'); |
|
| 476 | + if ($logFile !== '' && is_writable(dirname($logFile)) && (!file_exists($logFile) || is_writable($logFile))) { |
|
| 477 | + $prefix = ''; |
|
| 478 | + if ($this->systemConfig->getValue('query_log_file_requestid') === 'yes') { |
|
| 479 | + $prefix .= Server::get(IRequestId::class)->getId() . "\t"; |
|
| 480 | + } |
|
| 481 | + |
|
| 482 | + $postfix = ''; |
|
| 483 | + if ($this->systemConfig->getValue('query_log_file_parameters') === 'yes') { |
|
| 484 | + $postfix .= '; ' . json_encode($params); |
|
| 485 | + } |
|
| 486 | + |
|
| 487 | + if ($this->systemConfig->getValue('query_log_file_backtrace') === 'yes') { |
|
| 488 | + $trace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS); |
|
| 489 | + array_pop($trace); |
|
| 490 | + $postfix .= '; ' . json_encode($trace); |
|
| 491 | + } |
|
| 492 | + |
|
| 493 | + // FIXME: Improve to log the actual target db host |
|
| 494 | + $isPrimary = $this->connections['primary'] === $this->_conn; |
|
| 495 | + $prefix .= ' ' . ($isPrimary === true ? 'primary' : 'replica') . ' '; |
|
| 496 | + $prefix .= ' ' . $this->getTransactionNestingLevel() . ' '; |
|
| 497 | + |
|
| 498 | + file_put_contents( |
|
| 499 | + $this->systemConfig->getValue('query_log_file', ''), |
|
| 500 | + $prefix . $sql . $postfix . "\n", |
|
| 501 | + FILE_APPEND |
|
| 502 | + ); |
|
| 503 | + } |
|
| 504 | + } |
|
| 505 | + |
|
| 506 | + /** |
|
| 507 | + * Returns the ID of the last inserted row, or the last value from a sequence object, |
|
| 508 | + * depending on the underlying driver. |
|
| 509 | + * |
|
| 510 | + * Note: This method may not return a meaningful or consistent result across different drivers, |
|
| 511 | + * because the underlying database may not even support the notion of AUTO_INCREMENT/IDENTITY |
|
| 512 | + * columns or sequences. |
|
| 513 | + * |
|
| 514 | + * @param string $seqName Name of the sequence object from which the ID should be returned. |
|
| 515 | + * |
|
| 516 | + * @return int the last inserted ID. |
|
| 517 | + * @throws Exception |
|
| 518 | + */ |
|
| 519 | + public function lastInsertId($name = null): int { |
|
| 520 | + if ($name) { |
|
| 521 | + $name = $this->replaceTablePrefix($name); |
|
| 522 | + } |
|
| 523 | + return $this->adapter->lastInsertId($name); |
|
| 524 | + } |
|
| 525 | + |
|
| 526 | + /** |
|
| 527 | + * @internal |
|
| 528 | + * @throws Exception |
|
| 529 | + */ |
|
| 530 | + public function realLastInsertId($seqName = null) { |
|
| 531 | + return parent::lastInsertId($seqName); |
|
| 532 | + } |
|
| 533 | + |
|
| 534 | + /** |
|
| 535 | + * Insert a row if the matching row does not exists. To accomplish proper race condition avoidance |
|
| 536 | + * it is needed that there is also a unique constraint on the values. Then this method will |
|
| 537 | + * catch the exception and return 0. |
|
| 538 | + * |
|
| 539 | + * @param string $table The table name (will replace *PREFIX* with the actual prefix) |
|
| 540 | + * @param array $input data that should be inserted into the table (column name => value) |
|
| 541 | + * @param array|null $compare List of values that should be checked for "if not exists" |
|
| 542 | + * If this is null or an empty array, all keys of $input will be compared |
|
| 543 | + * Please note: text fields (clob) must not be used in the compare array |
|
| 544 | + * @return int number of inserted rows |
|
| 545 | + * @throws \Doctrine\DBAL\Exception |
|
| 546 | + * @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 |
|
| 547 | + */ |
|
| 548 | + public function insertIfNotExist($table, $input, ?array $compare = null) { |
|
| 549 | + try { |
|
| 550 | + return $this->adapter->insertIfNotExist($table, $input, $compare); |
|
| 551 | + } catch (\Exception $e) { |
|
| 552 | + $this->logDatabaseException($e); |
|
| 553 | + throw $e; |
|
| 554 | + } |
|
| 555 | + } |
|
| 556 | + |
|
| 557 | + public function insertIgnoreConflict(string $table, array $values) : int { |
|
| 558 | + try { |
|
| 559 | + return $this->adapter->insertIgnoreConflict($table, $values); |
|
| 560 | + } catch (\Exception $e) { |
|
| 561 | + $this->logDatabaseException($e); |
|
| 562 | + throw $e; |
|
| 563 | + } |
|
| 564 | + } |
|
| 565 | + |
|
| 566 | + private function getType($value) { |
|
| 567 | + if (is_bool($value)) { |
|
| 568 | + return IQueryBuilder::PARAM_BOOL; |
|
| 569 | + } elseif (is_int($value)) { |
|
| 570 | + return IQueryBuilder::PARAM_INT; |
|
| 571 | + } else { |
|
| 572 | + return IQueryBuilder::PARAM_STR; |
|
| 573 | + } |
|
| 574 | + } |
|
| 575 | + |
|
| 576 | + /** |
|
| 577 | + * Insert or update a row value |
|
| 578 | + * |
|
| 579 | + * @param string $table |
|
| 580 | + * @param array $keys (column name => value) |
|
| 581 | + * @param array $values (column name => value) |
|
| 582 | + * @param array $updatePreconditionValues ensure values match preconditions (column name => value) |
|
| 583 | + * @return int number of new rows |
|
| 584 | + * @throws \OCP\DB\Exception |
|
| 585 | + * @throws PreConditionNotMetException |
|
| 586 | + */ |
|
| 587 | + public function setValues(string $table, array $keys, array $values, array $updatePreconditionValues = []): int { |
|
| 588 | + try { |
|
| 589 | + $insertQb = $this->getQueryBuilder(); |
|
| 590 | + $insertQb->insert($table) |
|
| 591 | + ->values( |
|
| 592 | + array_map(function ($value) use ($insertQb) { |
|
| 593 | + return $insertQb->createNamedParameter($value, $this->getType($value)); |
|
| 594 | + }, array_merge($keys, $values)) |
|
| 595 | + ); |
|
| 596 | + return $insertQb->executeStatement(); |
|
| 597 | + } catch (\OCP\DB\Exception $e) { |
|
| 598 | + if (!in_array($e->getReason(), [ |
|
| 599 | + \OCP\DB\Exception::REASON_CONSTRAINT_VIOLATION, |
|
| 600 | + \OCP\DB\Exception::REASON_UNIQUE_CONSTRAINT_VIOLATION, |
|
| 601 | + ]) |
|
| 602 | + ) { |
|
| 603 | + throw $e; |
|
| 604 | + } |
|
| 605 | + |
|
| 606 | + // value already exists, try update |
|
| 607 | + $updateQb = $this->getQueryBuilder(); |
|
| 608 | + $updateQb->update($table); |
|
| 609 | + foreach ($values as $name => $value) { |
|
| 610 | + $updateQb->set($name, $updateQb->createNamedParameter($value, $this->getType($value))); |
|
| 611 | + } |
|
| 612 | + $where = []; |
|
| 613 | + $whereValues = array_merge($keys, $updatePreconditionValues); |
|
| 614 | + foreach ($whereValues as $name => $value) { |
|
| 615 | + if ($value === '') { |
|
| 616 | + $where[] = $updateQb->expr()->emptyString( |
|
| 617 | + $name |
|
| 618 | + ); |
|
| 619 | + } else { |
|
| 620 | + $where[] = $updateQb->expr()->eq( |
|
| 621 | + $name, |
|
| 622 | + $updateQb->createNamedParameter($value, $this->getType($value)), |
|
| 623 | + $this->getType($value) |
|
| 624 | + ); |
|
| 625 | + } |
|
| 626 | + } |
|
| 627 | + $updateQb->where($updateQb->expr()->andX(...$where)); |
|
| 628 | + $affected = $updateQb->executeStatement(); |
|
| 629 | + |
|
| 630 | + if ($affected === 0 && !empty($updatePreconditionValues)) { |
|
| 631 | + throw new PreConditionNotMetException(); |
|
| 632 | + } |
|
| 633 | + |
|
| 634 | + return 0; |
|
| 635 | + } |
|
| 636 | + } |
|
| 637 | + |
|
| 638 | + /** |
|
| 639 | + * Create an exclusive read+write lock on a table |
|
| 640 | + * |
|
| 641 | + * @param string $tableName |
|
| 642 | + * |
|
| 643 | + * @throws \BadMethodCallException When trying to acquire a second lock |
|
| 644 | + * @throws Exception |
|
| 645 | + * @since 9.1.0 |
|
| 646 | + */ |
|
| 647 | + public function lockTable($tableName) { |
|
| 648 | + if ($this->lockedTable !== null) { |
|
| 649 | + throw new \BadMethodCallException('Can not lock a new table until the previous lock is released.'); |
|
| 650 | + } |
|
| 651 | + |
|
| 652 | + $tableName = $this->tablePrefix . $tableName; |
|
| 653 | + $this->lockedTable = $tableName; |
|
| 654 | + $this->adapter->lockTable($tableName); |
|
| 655 | + } |
|
| 656 | + |
|
| 657 | + /** |
|
| 658 | + * Release a previous acquired lock again |
|
| 659 | + * |
|
| 660 | + * @throws Exception |
|
| 661 | + * @since 9.1.0 |
|
| 662 | + */ |
|
| 663 | + public function unlockTable() { |
|
| 664 | + $this->adapter->unlockTable(); |
|
| 665 | + $this->lockedTable = null; |
|
| 666 | + } |
|
| 667 | + |
|
| 668 | + /** |
|
| 669 | + * returns the error code and message as a string for logging |
|
| 670 | + * works with DoctrineException |
|
| 671 | + * @return string |
|
| 672 | + */ |
|
| 673 | + public function getError() { |
|
| 674 | + $msg = $this->errorCode() . ': '; |
|
| 675 | + $errorInfo = $this->errorInfo(); |
|
| 676 | + if (!empty($errorInfo)) { |
|
| 677 | + $msg .= 'SQLSTATE = ' . $errorInfo[0] . ', '; |
|
| 678 | + $msg .= 'Driver Code = ' . $errorInfo[1] . ', '; |
|
| 679 | + $msg .= 'Driver Message = ' . $errorInfo[2]; |
|
| 680 | + } |
|
| 681 | + return $msg; |
|
| 682 | + } |
|
| 683 | + |
|
| 684 | + public function errorCode() { |
|
| 685 | + return -1; |
|
| 686 | + } |
|
| 687 | + |
|
| 688 | + public function errorInfo() { |
|
| 689 | + return []; |
|
| 690 | + } |
|
| 691 | + |
|
| 692 | + /** |
|
| 693 | + * Drop a table from the database if it exists |
|
| 694 | + * |
|
| 695 | + * @param string $table table name without the prefix |
|
| 696 | + * |
|
| 697 | + * @throws Exception |
|
| 698 | + */ |
|
| 699 | + public function dropTable($table) { |
|
| 700 | + $table = $this->tablePrefix . trim($table); |
|
| 701 | + $schema = $this->createSchemaManager(); |
|
| 702 | + if ($schema->tablesExist([$table])) { |
|
| 703 | + $schema->dropTable($table); |
|
| 704 | + } |
|
| 705 | + } |
|
| 706 | + |
|
| 707 | + /** |
|
| 708 | + * Truncate a table data if it exists |
|
| 709 | + * |
|
| 710 | + * @param string $table table name without the prefix |
|
| 711 | + * @param bool $cascade whether to truncate cascading |
|
| 712 | + * |
|
| 713 | + * @throws Exception |
|
| 714 | + */ |
|
| 715 | + public function truncateTable(string $table, bool $cascade) { |
|
| 716 | + $this->executeStatement($this->getDatabasePlatform() |
|
| 717 | + ->getTruncateTableSQL($this->tablePrefix . trim($table), $cascade)); |
|
| 718 | + } |
|
| 719 | + |
|
| 720 | + /** |
|
| 721 | + * Check if a table exists |
|
| 722 | + * |
|
| 723 | + * @param string $table table name without the prefix |
|
| 724 | + * |
|
| 725 | + * @return bool |
|
| 726 | + * @throws Exception |
|
| 727 | + */ |
|
| 728 | + public function tableExists($table) { |
|
| 729 | + $table = $this->tablePrefix . trim($table); |
|
| 730 | + $schema = $this->createSchemaManager(); |
|
| 731 | + return $schema->tablesExist([$table]); |
|
| 732 | + } |
|
| 733 | + |
|
| 734 | + protected function finishQuery(string $statement): string { |
|
| 735 | + $statement = $this->replaceTablePrefix($statement); |
|
| 736 | + $statement = $this->adapter->fixupStatement($statement); |
|
| 737 | + if ($this->logRequestId) { |
|
| 738 | + return $statement . ' /* reqid: ' . $this->requestId . ' */'; |
|
| 739 | + } else { |
|
| 740 | + return $statement; |
|
| 741 | + } |
|
| 742 | + } |
|
| 743 | + |
|
| 744 | + // internal use |
|
| 745 | + /** |
|
| 746 | + * @param string $statement |
|
| 747 | + * @return string |
|
| 748 | + */ |
|
| 749 | + protected function replaceTablePrefix($statement) { |
|
| 750 | + return str_replace('*PREFIX*', $this->tablePrefix, $statement); |
|
| 751 | + } |
|
| 752 | + |
|
| 753 | + /** |
|
| 754 | + * Check if a transaction is active |
|
| 755 | + * |
|
| 756 | + * @return bool |
|
| 757 | + * @since 8.2.0 |
|
| 758 | + */ |
|
| 759 | + public function inTransaction() { |
|
| 760 | + return $this->getTransactionNestingLevel() > 0; |
|
| 761 | + } |
|
| 762 | + |
|
| 763 | + /** |
|
| 764 | + * Escape a parameter to be used in a LIKE query |
|
| 765 | + * |
|
| 766 | + * @param string $param |
|
| 767 | + * @return string |
|
| 768 | + */ |
|
| 769 | + public function escapeLikeParameter($param) { |
|
| 770 | + return addcslashes($param, '\\_%'); |
|
| 771 | + } |
|
| 772 | + |
|
| 773 | + /** |
|
| 774 | + * Check whether or not the current database support 4byte wide unicode |
|
| 775 | + * |
|
| 776 | + * @return bool |
|
| 777 | + * @since 11.0.0 |
|
| 778 | + */ |
|
| 779 | + public function supports4ByteText() { |
|
| 780 | + if (!$this->getDatabasePlatform() instanceof MySQLPlatform) { |
|
| 781 | + return true; |
|
| 782 | + } |
|
| 783 | + return $this->getParams()['charset'] === 'utf8mb4'; |
|
| 784 | + } |
|
| 785 | + |
|
| 786 | + |
|
| 787 | + /** |
|
| 788 | + * Create the schema of the connected database |
|
| 789 | + * |
|
| 790 | + * @return Schema |
|
| 791 | + * @throws Exception |
|
| 792 | + */ |
|
| 793 | + public function createSchema() { |
|
| 794 | + $migrator = $this->getMigrator(); |
|
| 795 | + return $migrator->createSchema(); |
|
| 796 | + } |
|
| 797 | + |
|
| 798 | + /** |
|
| 799 | + * Migrate the database to the given schema |
|
| 800 | + * |
|
| 801 | + * @param Schema $toSchema |
|
| 802 | + * @param bool $dryRun If true, will return the sql queries instead of running them. |
|
| 803 | + * |
|
| 804 | + * @throws Exception |
|
| 805 | + * |
|
| 806 | + * @return string|null Returns a string only if $dryRun is true. |
|
| 807 | + */ |
|
| 808 | + public function migrateToSchema(Schema $toSchema, bool $dryRun = false) { |
|
| 809 | + $migrator = $this->getMigrator(); |
|
| 810 | + |
|
| 811 | + if ($dryRun) { |
|
| 812 | + return $migrator->generateChangeScript($toSchema); |
|
| 813 | + } else { |
|
| 814 | + $migrator->migrate($toSchema); |
|
| 815 | + foreach ($this->getShardConnections() as $shardConnection) { |
|
| 816 | + $shardConnection->migrateToSchema($toSchema); |
|
| 817 | + } |
|
| 818 | + } |
|
| 819 | + } |
|
| 820 | + |
|
| 821 | + private function getMigrator() { |
|
| 822 | + // TODO properly inject those dependencies |
|
| 823 | + $random = \OC::$server->get(ISecureRandom::class); |
|
| 824 | + $platform = $this->getDatabasePlatform(); |
|
| 825 | + $config = \OC::$server->getConfig(); |
|
| 826 | + $dispatcher = Server::get(\OCP\EventDispatcher\IEventDispatcher::class); |
|
| 827 | + if ($platform instanceof SqlitePlatform) { |
|
| 828 | + return new SQLiteMigrator($this, $config, $dispatcher); |
|
| 829 | + } elseif ($platform instanceof OraclePlatform) { |
|
| 830 | + return new OracleMigrator($this, $config, $dispatcher); |
|
| 831 | + } else { |
|
| 832 | + return new Migrator($this, $config, $dispatcher); |
|
| 833 | + } |
|
| 834 | + } |
|
| 835 | + |
|
| 836 | + public function beginTransaction() { |
|
| 837 | + if (!$this->inTransaction()) { |
|
| 838 | + $this->transactionBacktrace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS); |
|
| 839 | + $this->transactionActiveSince = microtime(true); |
|
| 840 | + } |
|
| 841 | + return parent::beginTransaction(); |
|
| 842 | + } |
|
| 843 | + |
|
| 844 | + public function commit() { |
|
| 845 | + $result = parent::commit(); |
|
| 846 | + if ($this->getTransactionNestingLevel() === 0) { |
|
| 847 | + $timeTook = microtime(true) - $this->transactionActiveSince; |
|
| 848 | + $this->transactionBacktrace = null; |
|
| 849 | + $this->transactionActiveSince = null; |
|
| 850 | + if ($timeTook > 1) { |
|
| 851 | + $logLevel = match (true) { |
|
| 852 | + $timeTook > 20 * 60 => ILogger::ERROR, |
|
| 853 | + $timeTook > 5 * 60 => ILogger::WARN, |
|
| 854 | + $timeTook > 10 => ILogger::INFO, |
|
| 855 | + default => ILogger::DEBUG, |
|
| 856 | + }; |
|
| 857 | + $this->logger->log( |
|
| 858 | + $logLevel, |
|
| 859 | + 'Transaction took ' . $timeTook . 's', |
|
| 860 | + [ |
|
| 861 | + 'exception' => new \Exception('Transaction took ' . $timeTook . 's'), |
|
| 862 | + 'timeSpent' => $timeTook, |
|
| 863 | + ] |
|
| 864 | + ); |
|
| 865 | + } |
|
| 866 | + } |
|
| 867 | + return $result; |
|
| 868 | + } |
|
| 869 | + |
|
| 870 | + public function rollBack() { |
|
| 871 | + $result = parent::rollBack(); |
|
| 872 | + if ($this->getTransactionNestingLevel() === 0) { |
|
| 873 | + $timeTook = microtime(true) - $this->transactionActiveSince; |
|
| 874 | + $this->transactionBacktrace = null; |
|
| 875 | + $this->transactionActiveSince = null; |
|
| 876 | + if ($timeTook > 1) { |
|
| 877 | + $logLevel = match (true) { |
|
| 878 | + $timeTook > 20 * 60 => ILogger::ERROR, |
|
| 879 | + $timeTook > 5 * 60 => ILogger::WARN, |
|
| 880 | + $timeTook > 10 => ILogger::INFO, |
|
| 881 | + default => ILogger::DEBUG, |
|
| 882 | + }; |
|
| 883 | + $this->logger->log( |
|
| 884 | + $logLevel, |
|
| 885 | + 'Transaction rollback took longer than 1s: ' . $timeTook, |
|
| 886 | + [ |
|
| 887 | + 'exception' => new \Exception('Long running transaction rollback'), |
|
| 888 | + 'timeSpent' => $timeTook, |
|
| 889 | + ] |
|
| 890 | + ); |
|
| 891 | + } |
|
| 892 | + } |
|
| 893 | + return $result; |
|
| 894 | + } |
|
| 895 | + |
|
| 896 | + private function reconnectIfNeeded(): void { |
|
| 897 | + if ( |
|
| 898 | + !isset($this->lastConnectionCheck[$this->getConnectionName()]) |
|
| 899 | + || time() <= $this->lastConnectionCheck[$this->getConnectionName()] + 30 |
|
| 900 | + || $this->isTransactionActive() |
|
| 901 | + ) { |
|
| 902 | + return; |
|
| 903 | + } |
|
| 904 | + |
|
| 905 | + try { |
|
| 906 | + $this->_conn->query($this->getDriver()->getDatabasePlatform()->getDummySelectSQL()); |
|
| 907 | + $this->lastConnectionCheck[$this->getConnectionName()] = time(); |
|
| 908 | + } catch (ConnectionLost|\Exception $e) { |
|
| 909 | + $this->logger->warning('Exception during connectivity check, closing and reconnecting', ['exception' => $e]); |
|
| 910 | + $this->close(); |
|
| 911 | + } |
|
| 912 | + } |
|
| 913 | + |
|
| 914 | + private function getConnectionName(): string { |
|
| 915 | + return $this->isConnectedToPrimary() ? 'primary' : 'replica'; |
|
| 916 | + } |
|
| 917 | + |
|
| 918 | + /** |
|
| 919 | + * @return IDBConnection::PLATFORM_MYSQL|IDBConnection::PLATFORM_ORACLE|IDBConnection::PLATFORM_POSTGRES|IDBConnection::PLATFORM_SQLITE|IDBConnection::PLATFORM_MARIADB |
|
| 920 | + */ |
|
| 921 | + public function getDatabaseProvider(bool $strict = false): string { |
|
| 922 | + $platform = $this->getDatabasePlatform(); |
|
| 923 | + if ($strict && $platform instanceof MariaDBPlatform) { |
|
| 924 | + return IDBConnection::PLATFORM_MARIADB; |
|
| 925 | + } elseif ($platform instanceof MySQLPlatform) { |
|
| 926 | + return IDBConnection::PLATFORM_MYSQL; |
|
| 927 | + } elseif ($platform instanceof OraclePlatform) { |
|
| 928 | + return IDBConnection::PLATFORM_ORACLE; |
|
| 929 | + } elseif ($platform instanceof PostgreSQLPlatform) { |
|
| 930 | + return IDBConnection::PLATFORM_POSTGRES; |
|
| 931 | + } elseif ($platform instanceof SqlitePlatform) { |
|
| 932 | + return IDBConnection::PLATFORM_SQLITE; |
|
| 933 | + } else { |
|
| 934 | + throw new \Exception('Database ' . $platform::class . ' not supported'); |
|
| 935 | + } |
|
| 936 | + } |
|
| 937 | + |
|
| 938 | + /** |
|
| 939 | + * @internal Should only be used inside the QueryBuilder, ExpressionBuilder and FunctionBuilder |
|
| 940 | + * All apps and API code should not need this and instead use provided functionality from the above. |
|
| 941 | + */ |
|
| 942 | + public function getServerVersion(): string { |
|
| 943 | + /** @var ServerInfoAwareConnection $this->_conn */ |
|
| 944 | + return $this->_conn->getServerVersion(); |
|
| 945 | + } |
|
| 946 | + |
|
| 947 | + /** |
|
| 948 | + * Log a database exception if enabled |
|
| 949 | + * |
|
| 950 | + * @param \Exception $exception |
|
| 951 | + * @return void |
|
| 952 | + */ |
|
| 953 | + public function logDatabaseException(\Exception $exception): void { |
|
| 954 | + if ($this->logDbException) { |
|
| 955 | + if ($exception instanceof Exception\UniqueConstraintViolationException) { |
|
| 956 | + $this->logger->info($exception->getMessage(), ['exception' => $exception, 'transaction' => $this->transactionBacktrace]); |
|
| 957 | + } else { |
|
| 958 | + $this->logger->error($exception->getMessage(), ['exception' => $exception, 'transaction' => $this->transactionBacktrace]); |
|
| 959 | + } |
|
| 960 | + } |
|
| 961 | + } |
|
| 962 | + |
|
| 963 | + public function getShardDefinition(string $name): ?ShardDefinition { |
|
| 964 | + return $this->shards[$name] ?? null; |
|
| 965 | + } |
|
| 966 | + |
|
| 967 | + public function getCrossShardMoveHelper(): CrossShardMoveHelper { |
|
| 968 | + return new CrossShardMoveHelper($this->shardConnectionManager); |
|
| 969 | + } |
|
| 970 | 970 | } |
@@ -29,1371 +29,1371 @@ |
||
| 29 | 29 | use Psr\Log\LoggerInterface; |
| 30 | 30 | |
| 31 | 31 | class QueryBuilder implements IQueryBuilder { |
| 32 | - /** @var ConnectionAdapter */ |
|
| 33 | - private $connection; |
|
| 34 | - |
|
| 35 | - /** @var SystemConfig */ |
|
| 36 | - private $systemConfig; |
|
| 37 | - |
|
| 38 | - private LoggerInterface $logger; |
|
| 39 | - |
|
| 40 | - /** @var \Doctrine\DBAL\Query\QueryBuilder */ |
|
| 41 | - private $queryBuilder; |
|
| 42 | - |
|
| 43 | - /** @var QuoteHelper */ |
|
| 44 | - private $helper; |
|
| 45 | - |
|
| 46 | - /** @var bool */ |
|
| 47 | - private $automaticTablePrefix = true; |
|
| 48 | - private bool $nonEmptyWhere = false; |
|
| 49 | - |
|
| 50 | - /** @var string */ |
|
| 51 | - protected $lastInsertedTable; |
|
| 52 | - private array $selectedColumns = []; |
|
| 53 | - |
|
| 54 | - /** |
|
| 55 | - * Initializes a new QueryBuilder. |
|
| 56 | - * |
|
| 57 | - * @param ConnectionAdapter $connection |
|
| 58 | - * @param SystemConfig $systemConfig |
|
| 59 | - */ |
|
| 60 | - public function __construct(ConnectionAdapter $connection, SystemConfig $systemConfig, LoggerInterface $logger) { |
|
| 61 | - $this->connection = $connection; |
|
| 62 | - $this->systemConfig = $systemConfig; |
|
| 63 | - $this->logger = $logger; |
|
| 64 | - $this->queryBuilder = new \Doctrine\DBAL\Query\QueryBuilder($this->connection->getInner()); |
|
| 65 | - $this->helper = new QuoteHelper(); |
|
| 66 | - } |
|
| 67 | - |
|
| 68 | - /** |
|
| 69 | - * Enable/disable automatic prefixing of table names with the oc_ prefix |
|
| 70 | - * |
|
| 71 | - * @param bool $enabled If set to true table names will be prefixed with the |
|
| 72 | - * owncloud database prefix automatically. |
|
| 73 | - * @since 8.2.0 |
|
| 74 | - */ |
|
| 75 | - public function automaticTablePrefix($enabled) { |
|
| 76 | - $this->automaticTablePrefix = (bool)$enabled; |
|
| 77 | - } |
|
| 78 | - |
|
| 79 | - /** |
|
| 80 | - * Gets an ExpressionBuilder used for object-oriented construction of query expressions. |
|
| 81 | - * This producer method is intended for convenient inline usage. Example: |
|
| 82 | - * |
|
| 83 | - * <code> |
|
| 84 | - * $qb = $conn->getQueryBuilder() |
|
| 85 | - * ->select('u') |
|
| 86 | - * ->from('users', 'u') |
|
| 87 | - * ->where($qb->expr()->eq('u.id', 1)); |
|
| 88 | - * </code> |
|
| 89 | - * |
|
| 90 | - * For more complex expression construction, consider storing the expression |
|
| 91 | - * builder object in a local variable. |
|
| 92 | - * |
|
| 93 | - * @return \OCP\DB\QueryBuilder\IExpressionBuilder |
|
| 94 | - */ |
|
| 95 | - public function expr() { |
|
| 96 | - return match($this->connection->getDatabaseProvider()) { |
|
| 97 | - IDBConnection::PLATFORM_ORACLE => new OCIExpressionBuilder($this->connection, $this, $this->logger), |
|
| 98 | - IDBConnection::PLATFORM_POSTGRES => new PgSqlExpressionBuilder($this->connection, $this, $this->logger), |
|
| 99 | - IDBConnection::PLATFORM_MARIADB, |
|
| 100 | - IDBConnection::PLATFORM_MYSQL => new MySqlExpressionBuilder($this->connection, $this, $this->logger), |
|
| 101 | - IDBConnection::PLATFORM_SQLITE => new SqliteExpressionBuilder($this->connection, $this, $this->logger), |
|
| 102 | - }; |
|
| 103 | - } |
|
| 104 | - |
|
| 105 | - /** |
|
| 106 | - * Gets an FunctionBuilder used for object-oriented construction of query functions. |
|
| 107 | - * This producer method is intended for convenient inline usage. Example: |
|
| 108 | - * |
|
| 109 | - * <code> |
|
| 110 | - * $qb = $conn->getQueryBuilder() |
|
| 111 | - * ->select('u') |
|
| 112 | - * ->from('users', 'u') |
|
| 113 | - * ->where($qb->fun()->md5('u.id')); |
|
| 114 | - * </code> |
|
| 115 | - * |
|
| 116 | - * For more complex function construction, consider storing the function |
|
| 117 | - * builder object in a local variable. |
|
| 118 | - * |
|
| 119 | - * @return \OCP\DB\QueryBuilder\IFunctionBuilder |
|
| 120 | - */ |
|
| 121 | - public function func() { |
|
| 122 | - return match($this->connection->getDatabaseProvider()) { |
|
| 123 | - IDBConnection::PLATFORM_ORACLE => new OCIFunctionBuilder($this->connection, $this, $this->helper), |
|
| 124 | - IDBConnection::PLATFORM_POSTGRES => new PgSqlFunctionBuilder($this->connection, $this, $this->helper), |
|
| 125 | - IDBConnection::PLATFORM_MARIADB, |
|
| 126 | - IDBConnection::PLATFORM_MYSQL => new FunctionBuilder($this->connection, $this, $this->helper), |
|
| 127 | - IDBConnection::PLATFORM_SQLITE => new SqliteFunctionBuilder($this->connection, $this, $this->helper), |
|
| 128 | - }; |
|
| 129 | - } |
|
| 130 | - |
|
| 131 | - /** |
|
| 132 | - * Gets the type of the currently built query. |
|
| 133 | - * |
|
| 134 | - * @return integer |
|
| 135 | - */ |
|
| 136 | - public function getType() { |
|
| 137 | - return $this->queryBuilder->getType(); |
|
| 138 | - } |
|
| 139 | - |
|
| 140 | - /** |
|
| 141 | - * Gets the associated DBAL Connection for this query builder. |
|
| 142 | - * |
|
| 143 | - * @return \OCP\IDBConnection |
|
| 144 | - */ |
|
| 145 | - public function getConnection() { |
|
| 146 | - return $this->connection; |
|
| 147 | - } |
|
| 148 | - |
|
| 149 | - /** |
|
| 150 | - * Gets the state of this query builder instance. |
|
| 151 | - * |
|
| 152 | - * @return int Always returns 0 which is former `QueryBuilder::STATE_DIRTY` |
|
| 153 | - * @deprecated 30.0.0 This function is going to be removed with the next Doctrine/DBAL update |
|
| 154 | - * and we can not fix this in our wrapper. |
|
| 155 | - */ |
|
| 156 | - public function getState() { |
|
| 157 | - $this->logger->debug(IQueryBuilder::class . '::' . __FUNCTION__ . ' is deprecated and will be removed soon.', ['exception' => new \Exception('Deprecated call to ' . __METHOD__)]); |
|
| 158 | - return $this->queryBuilder->getState(); |
|
| 159 | - } |
|
| 160 | - |
|
| 161 | - private function prepareForExecute() { |
|
| 162 | - if ($this->systemConfig->getValue('log_query', false)) { |
|
| 163 | - try { |
|
| 164 | - $params = []; |
|
| 165 | - foreach ($this->getParameters() as $placeholder => $value) { |
|
| 166 | - if ($value instanceof \DateTimeInterface) { |
|
| 167 | - $params[] = $placeholder . ' => DateTime:\'' . $value->format('c') . '\''; |
|
| 168 | - } elseif (is_array($value)) { |
|
| 169 | - $params[] = $placeholder . ' => (\'' . implode('\', \'', $value) . '\')'; |
|
| 170 | - } else { |
|
| 171 | - $params[] = $placeholder . ' => \'' . $value . '\''; |
|
| 172 | - } |
|
| 173 | - } |
|
| 174 | - if (empty($params)) { |
|
| 175 | - $this->logger->debug('DB QueryBuilder: \'{query}\'', [ |
|
| 176 | - 'query' => $this->getSQL(), |
|
| 177 | - 'app' => 'core', |
|
| 178 | - ]); |
|
| 179 | - } else { |
|
| 180 | - $this->logger->debug('DB QueryBuilder: \'{query}\' with parameters: {params}', [ |
|
| 181 | - 'query' => $this->getSQL(), |
|
| 182 | - 'params' => implode(', ', $params), |
|
| 183 | - 'app' => 'core', |
|
| 184 | - ]); |
|
| 185 | - } |
|
| 186 | - } catch (\Error $e) { |
|
| 187 | - // likely an error during conversion of $value to string |
|
| 188 | - $this->logger->error('DB QueryBuilder: error trying to log SQL query', ['exception' => $e]); |
|
| 189 | - } |
|
| 190 | - } |
|
| 191 | - |
|
| 192 | - // if (!empty($this->getQueryPart('select'))) { |
|
| 193 | - // $select = $this->getQueryPart('select'); |
|
| 194 | - // $hasSelectAll = array_filter($select, static function ($s) { |
|
| 195 | - // return $s === '*'; |
|
| 196 | - // }); |
|
| 197 | - // $hasSelectSpecific = array_filter($select, static function ($s) { |
|
| 198 | - // return $s !== '*'; |
|
| 199 | - // }); |
|
| 200 | - |
|
| 201 | - // if (empty($hasSelectAll) === empty($hasSelectSpecific)) { |
|
| 202 | - // $exception = new QueryException('Query is selecting * and specific values in the same query. This is not supported in Oracle.'); |
|
| 203 | - // $this->logger->error($exception->getMessage(), [ |
|
| 204 | - // 'query' => $this->getSQL(), |
|
| 205 | - // 'app' => 'core', |
|
| 206 | - // 'exception' => $exception, |
|
| 207 | - // ]); |
|
| 208 | - // } |
|
| 209 | - // } |
|
| 210 | - |
|
| 211 | - $tooLongOutputColumns = []; |
|
| 212 | - foreach ($this->getOutputColumns() as $column) { |
|
| 213 | - if (strlen($column) > 30) { |
|
| 214 | - $tooLongOutputColumns[] = $column; |
|
| 215 | - } |
|
| 216 | - } |
|
| 217 | - |
|
| 218 | - if (!empty($tooLongOutputColumns)) { |
|
| 219 | - $exception = new QueryException('More than 30 characters for an output column name are not allowed on Oracle.'); |
|
| 220 | - $this->logger->error($exception->getMessage(), [ |
|
| 221 | - 'query' => $this->getSQL(), |
|
| 222 | - 'columns' => $tooLongOutputColumns, |
|
| 223 | - 'app' => 'core', |
|
| 224 | - 'exception' => $exception, |
|
| 225 | - ]); |
|
| 226 | - } |
|
| 227 | - |
|
| 228 | - $numberOfParameters = 0; |
|
| 229 | - $hasTooLargeArrayParameter = false; |
|
| 230 | - foreach ($this->getParameters() as $parameter) { |
|
| 231 | - if (is_array($parameter)) { |
|
| 232 | - $count = count($parameter); |
|
| 233 | - $numberOfParameters += $count; |
|
| 234 | - $hasTooLargeArrayParameter = $hasTooLargeArrayParameter || ($count > 1000); |
|
| 235 | - } |
|
| 236 | - } |
|
| 237 | - |
|
| 238 | - if ($hasTooLargeArrayParameter) { |
|
| 239 | - $exception = new QueryException('More than 1000 expressions in a list are not allowed on Oracle.'); |
|
| 240 | - $this->logger->error($exception->getMessage(), [ |
|
| 241 | - 'query' => $this->getSQL(), |
|
| 242 | - 'app' => 'core', |
|
| 243 | - 'exception' => $exception, |
|
| 244 | - ]); |
|
| 245 | - } |
|
| 246 | - |
|
| 247 | - if ($numberOfParameters > 65535) { |
|
| 248 | - $exception = new QueryException('The number of parameters must not exceed 65535. Restriction by PostgreSQL.'); |
|
| 249 | - $this->logger->error($exception->getMessage(), [ |
|
| 250 | - 'query' => $this->getSQL(), |
|
| 251 | - 'app' => 'core', |
|
| 252 | - 'exception' => $exception, |
|
| 253 | - ]); |
|
| 254 | - } |
|
| 255 | - } |
|
| 256 | - |
|
| 257 | - /** |
|
| 258 | - * Executes this query using the bound parameters and their types. |
|
| 259 | - * |
|
| 260 | - * Uses {@see Connection::executeQuery} for select statements and {@see Connection::executeUpdate} |
|
| 261 | - * for insert, update and delete statements. |
|
| 262 | - * |
|
| 263 | - * @return IResult|int |
|
| 264 | - */ |
|
| 265 | - public function execute(?IDBConnection $connection = null) { |
|
| 266 | - try { |
|
| 267 | - if ($this->getType() === \Doctrine\DBAL\Query\QueryBuilder::SELECT) { |
|
| 268 | - return $this->executeQuery($connection); |
|
| 269 | - } else { |
|
| 270 | - return $this->executeStatement($connection); |
|
| 271 | - } |
|
| 272 | - } catch (DBALException $e) { |
|
| 273 | - // `IQueryBuilder->execute` never wrapped the exception, but `executeQuery` and `executeStatement` do |
|
| 274 | - /** @var \Doctrine\DBAL\Exception $previous */ |
|
| 275 | - $previous = $e->getPrevious(); |
|
| 276 | - |
|
| 277 | - throw $previous; |
|
| 278 | - } |
|
| 279 | - } |
|
| 280 | - |
|
| 281 | - public function executeQuery(?IDBConnection $connection = null): IResult { |
|
| 282 | - if ($this->getType() !== \Doctrine\DBAL\Query\QueryBuilder::SELECT) { |
|
| 283 | - throw new \RuntimeException('Invalid query type, expected SELECT query'); |
|
| 284 | - } |
|
| 285 | - |
|
| 286 | - $this->prepareForExecute(); |
|
| 287 | - if (!$connection) { |
|
| 288 | - $connection = $this->connection; |
|
| 289 | - } |
|
| 290 | - |
|
| 291 | - return $connection->executeQuery( |
|
| 292 | - $this->getSQL(), |
|
| 293 | - $this->getParameters(), |
|
| 294 | - $this->getParameterTypes(), |
|
| 295 | - ); |
|
| 296 | - } |
|
| 297 | - |
|
| 298 | - public function executeStatement(?IDBConnection $connection = null): int { |
|
| 299 | - if ($this->getType() === \Doctrine\DBAL\Query\QueryBuilder::SELECT) { |
|
| 300 | - throw new \RuntimeException('Invalid query type, expected INSERT, DELETE or UPDATE statement'); |
|
| 301 | - } |
|
| 302 | - |
|
| 303 | - $this->prepareForExecute(); |
|
| 304 | - if (!$connection) { |
|
| 305 | - $connection = $this->connection; |
|
| 306 | - } |
|
| 307 | - |
|
| 308 | - return $connection->executeStatement( |
|
| 309 | - $this->getSQL(), |
|
| 310 | - $this->getParameters(), |
|
| 311 | - $this->getParameterTypes(), |
|
| 312 | - ); |
|
| 313 | - } |
|
| 314 | - |
|
| 315 | - |
|
| 316 | - /** |
|
| 317 | - * Gets the complete SQL string formed by the current specifications of this QueryBuilder. |
|
| 318 | - * |
|
| 319 | - * <code> |
|
| 320 | - * $qb = $conn->getQueryBuilder() |
|
| 321 | - * ->select('u') |
|
| 322 | - * ->from('User', 'u') |
|
| 323 | - * echo $qb->getSQL(); // SELECT u FROM User u |
|
| 324 | - * </code> |
|
| 325 | - * |
|
| 326 | - * @return string The SQL query string. |
|
| 327 | - */ |
|
| 328 | - public function getSQL() { |
|
| 329 | - return $this->queryBuilder->getSQL(); |
|
| 330 | - } |
|
| 331 | - |
|
| 332 | - /** |
|
| 333 | - * Sets a query parameter for the query being constructed. |
|
| 334 | - * |
|
| 335 | - * <code> |
|
| 336 | - * $qb = $conn->getQueryBuilder() |
|
| 337 | - * ->select('u') |
|
| 338 | - * ->from('users', 'u') |
|
| 339 | - * ->where('u.id = :user_id') |
|
| 340 | - * ->setParameter(':user_id', 1); |
|
| 341 | - * </code> |
|
| 342 | - * |
|
| 343 | - * @param string|integer $key The parameter position or name. |
|
| 344 | - * @param mixed $value The parameter value. |
|
| 345 | - * @param string|null|int $type One of the IQueryBuilder::PARAM_* constants. |
|
| 346 | - * |
|
| 347 | - * @return $this This QueryBuilder instance. |
|
| 348 | - */ |
|
| 349 | - public function setParameter($key, $value, $type = null) { |
|
| 350 | - $this->queryBuilder->setParameter($key, $value, $type); |
|
| 351 | - |
|
| 352 | - return $this; |
|
| 353 | - } |
|
| 354 | - |
|
| 355 | - /** |
|
| 356 | - * Sets a collection of query parameters for the query being constructed. |
|
| 357 | - * |
|
| 358 | - * <code> |
|
| 359 | - * $qb = $conn->getQueryBuilder() |
|
| 360 | - * ->select('u') |
|
| 361 | - * ->from('users', 'u') |
|
| 362 | - * ->where('u.id = :user_id1 OR u.id = :user_id2') |
|
| 363 | - * ->setParameters(array( |
|
| 364 | - * ':user_id1' => 1, |
|
| 365 | - * ':user_id2' => 2 |
|
| 366 | - * )); |
|
| 367 | - * </code> |
|
| 368 | - * |
|
| 369 | - * @param array $params The query parameters to set. |
|
| 370 | - * @param array $types The query parameters types to set. |
|
| 371 | - * |
|
| 372 | - * @return $this This QueryBuilder instance. |
|
| 373 | - */ |
|
| 374 | - public function setParameters(array $params, array $types = []) { |
|
| 375 | - $this->queryBuilder->setParameters($params, $types); |
|
| 376 | - |
|
| 377 | - return $this; |
|
| 378 | - } |
|
| 379 | - |
|
| 380 | - /** |
|
| 381 | - * Gets all defined query parameters for the query being constructed indexed by parameter index or name. |
|
| 382 | - * |
|
| 383 | - * @return array The currently defined query parameters indexed by parameter index or name. |
|
| 384 | - */ |
|
| 385 | - public function getParameters() { |
|
| 386 | - return $this->queryBuilder->getParameters(); |
|
| 387 | - } |
|
| 388 | - |
|
| 389 | - /** |
|
| 390 | - * Gets a (previously set) query parameter of the query being constructed. |
|
| 391 | - * |
|
| 392 | - * @param mixed $key The key (index or name) of the bound parameter. |
|
| 393 | - * |
|
| 394 | - * @return mixed The value of the bound parameter. |
|
| 395 | - */ |
|
| 396 | - public function getParameter($key) { |
|
| 397 | - return $this->queryBuilder->getParameter($key); |
|
| 398 | - } |
|
| 399 | - |
|
| 400 | - /** |
|
| 401 | - * Gets all defined query parameter types for the query being constructed indexed by parameter index or name. |
|
| 402 | - * |
|
| 403 | - * @return array The currently defined query parameter types indexed by parameter index or name. |
|
| 404 | - */ |
|
| 405 | - public function getParameterTypes() { |
|
| 406 | - return $this->queryBuilder->getParameterTypes(); |
|
| 407 | - } |
|
| 408 | - |
|
| 409 | - /** |
|
| 410 | - * Gets a (previously set) query parameter type of the query being constructed. |
|
| 411 | - * |
|
| 412 | - * @param mixed $key The key (index or name) of the bound parameter type. |
|
| 413 | - * |
|
| 414 | - * @return mixed The value of the bound parameter type. |
|
| 415 | - */ |
|
| 416 | - public function getParameterType($key) { |
|
| 417 | - return $this->queryBuilder->getParameterType($key); |
|
| 418 | - } |
|
| 419 | - |
|
| 420 | - /** |
|
| 421 | - * Sets the position of the first result to retrieve (the "offset"). |
|
| 422 | - * |
|
| 423 | - * @param int $firstResult The first result to return. |
|
| 424 | - * |
|
| 425 | - * @return $this This QueryBuilder instance. |
|
| 426 | - */ |
|
| 427 | - public function setFirstResult($firstResult) { |
|
| 428 | - $this->queryBuilder->setFirstResult((int)$firstResult); |
|
| 429 | - |
|
| 430 | - return $this; |
|
| 431 | - } |
|
| 432 | - |
|
| 433 | - /** |
|
| 434 | - * Gets the position of the first result the query object was set to retrieve (the "offset"). |
|
| 435 | - * Returns 0 if {@link setFirstResult} was not applied to this QueryBuilder. |
|
| 436 | - * |
|
| 437 | - * @return int The position of the first result. |
|
| 438 | - */ |
|
| 439 | - public function getFirstResult() { |
|
| 440 | - return $this->queryBuilder->getFirstResult(); |
|
| 441 | - } |
|
| 442 | - |
|
| 443 | - /** |
|
| 444 | - * Sets the maximum number of results to retrieve (the "limit"). |
|
| 445 | - * |
|
| 446 | - * NOTE: Setting max results to "0" will cause mixed behaviour. While most |
|
| 447 | - * of the databases will just return an empty result set, Oracle will return |
|
| 448 | - * all entries. |
|
| 449 | - * |
|
| 450 | - * @param int|null $maxResults The maximum number of results to retrieve. |
|
| 451 | - * |
|
| 452 | - * @return $this This QueryBuilder instance. |
|
| 453 | - */ |
|
| 454 | - public function setMaxResults($maxResults) { |
|
| 455 | - if ($maxResults === null) { |
|
| 456 | - $this->queryBuilder->setMaxResults($maxResults); |
|
| 457 | - } else { |
|
| 458 | - $this->queryBuilder->setMaxResults((int)$maxResults); |
|
| 459 | - } |
|
| 460 | - |
|
| 461 | - return $this; |
|
| 462 | - } |
|
| 463 | - |
|
| 464 | - /** |
|
| 465 | - * Gets the maximum number of results the query object was set to retrieve (the "limit"). |
|
| 466 | - * Returns NULL if {@link setMaxResults} was not applied to this query builder. |
|
| 467 | - * |
|
| 468 | - * @return int|null The maximum number of results. |
|
| 469 | - */ |
|
| 470 | - public function getMaxResults() { |
|
| 471 | - return $this->queryBuilder->getMaxResults(); |
|
| 472 | - } |
|
| 473 | - |
|
| 474 | - /** |
|
| 475 | - * Specifies an item that is to be returned in the query result. |
|
| 476 | - * Replaces any previously specified selections, if any. |
|
| 477 | - * |
|
| 478 | - * <code> |
|
| 479 | - * $qb = $conn->getQueryBuilder() |
|
| 480 | - * ->select('u.id', 'p.id') |
|
| 481 | - * ->from('users', 'u') |
|
| 482 | - * ->leftJoin('u', 'phonenumbers', 'p', 'u.id = p.user_id'); |
|
| 483 | - * </code> |
|
| 484 | - * |
|
| 485 | - * @param mixed ...$selects The selection expressions. |
|
| 486 | - * |
|
| 487 | - * '@return $this This QueryBuilder instance. |
|
| 488 | - */ |
|
| 489 | - public function select(...$selects) { |
|
| 490 | - if (count($selects) === 1 && is_array($selects[0])) { |
|
| 491 | - $selects = $selects[0]; |
|
| 492 | - } |
|
| 493 | - $this->addOutputColumns($selects); |
|
| 494 | - |
|
| 495 | - $this->queryBuilder->select( |
|
| 496 | - $this->helper->quoteColumnNames($selects) |
|
| 497 | - ); |
|
| 498 | - |
|
| 499 | - return $this; |
|
| 500 | - } |
|
| 501 | - |
|
| 502 | - /** |
|
| 503 | - * Specifies an item that is to be returned with a different name in the query result. |
|
| 504 | - * |
|
| 505 | - * <code> |
|
| 506 | - * $qb = $conn->getQueryBuilder() |
|
| 507 | - * ->selectAlias('u.id', 'user_id') |
|
| 508 | - * ->from('users', 'u') |
|
| 509 | - * ->leftJoin('u', 'phonenumbers', 'p', 'u.id = p.user_id'); |
|
| 510 | - * </code> |
|
| 511 | - * |
|
| 512 | - * @param mixed $select The selection expressions. |
|
| 513 | - * @param string $alias The column alias used in the constructed query. |
|
| 514 | - * |
|
| 515 | - * @return $this This QueryBuilder instance. |
|
| 516 | - */ |
|
| 517 | - public function selectAlias($select, $alias) { |
|
| 518 | - $this->queryBuilder->addSelect( |
|
| 519 | - $this->helper->quoteColumnName($select) . ' AS ' . $this->helper->quoteColumnName($alias) |
|
| 520 | - ); |
|
| 521 | - $this->addOutputColumns([$alias]); |
|
| 522 | - |
|
| 523 | - return $this; |
|
| 524 | - } |
|
| 525 | - |
|
| 526 | - /** |
|
| 527 | - * Specifies an item that is to be returned uniquely in the query result. |
|
| 528 | - * |
|
| 529 | - * <code> |
|
| 530 | - * $qb = $conn->getQueryBuilder() |
|
| 531 | - * ->selectDistinct('type') |
|
| 532 | - * ->from('users'); |
|
| 533 | - * </code> |
|
| 534 | - * |
|
| 535 | - * @param mixed $select The selection expressions. |
|
| 536 | - * |
|
| 537 | - * @return $this This QueryBuilder instance. |
|
| 538 | - */ |
|
| 539 | - public function selectDistinct($select) { |
|
| 540 | - if (!is_array($select)) { |
|
| 541 | - $select = [$select]; |
|
| 542 | - } |
|
| 543 | - $this->addOutputColumns($select); |
|
| 544 | - |
|
| 545 | - $quotedSelect = $this->helper->quoteColumnNames($select); |
|
| 546 | - |
|
| 547 | - $this->queryBuilder->addSelect( |
|
| 548 | - 'DISTINCT ' . implode(', ', $quotedSelect) |
|
| 549 | - ); |
|
| 550 | - |
|
| 551 | - return $this; |
|
| 552 | - } |
|
| 553 | - |
|
| 554 | - /** |
|
| 555 | - * Adds an item that is to be returned in the query result. |
|
| 556 | - * |
|
| 557 | - * <code> |
|
| 558 | - * $qb = $conn->getQueryBuilder() |
|
| 559 | - * ->select('u.id') |
|
| 560 | - * ->addSelect('p.id') |
|
| 561 | - * ->from('users', 'u') |
|
| 562 | - * ->leftJoin('u', 'phonenumbers', 'u.id = p.user_id'); |
|
| 563 | - * </code> |
|
| 564 | - * |
|
| 565 | - * @param mixed ...$selects The selection expression. |
|
| 566 | - * |
|
| 567 | - * @return $this This QueryBuilder instance. |
|
| 568 | - */ |
|
| 569 | - public function addSelect(...$selects) { |
|
| 570 | - if (count($selects) === 1 && is_array($selects[0])) { |
|
| 571 | - $selects = $selects[0]; |
|
| 572 | - } |
|
| 573 | - $this->addOutputColumns($selects); |
|
| 574 | - |
|
| 575 | - $this->queryBuilder->addSelect( |
|
| 576 | - $this->helper->quoteColumnNames($selects) |
|
| 577 | - ); |
|
| 578 | - |
|
| 579 | - return $this; |
|
| 580 | - } |
|
| 581 | - |
|
| 582 | - private function addOutputColumns(array $columns): void { |
|
| 583 | - foreach ($columns as $column) { |
|
| 584 | - if (is_array($column)) { |
|
| 585 | - $this->addOutputColumns($column); |
|
| 586 | - } elseif (is_string($column) && !str_contains($column, '*')) { |
|
| 587 | - if (str_contains(strtolower($column), ' as ')) { |
|
| 588 | - [, $column] = preg_split('/ as /i', $column); |
|
| 589 | - } |
|
| 590 | - if (str_contains($column, '.')) { |
|
| 591 | - [, $column] = explode('.', $column); |
|
| 592 | - } |
|
| 593 | - $this->selectedColumns[] = $column; |
|
| 594 | - } |
|
| 595 | - } |
|
| 596 | - } |
|
| 597 | - |
|
| 598 | - public function getOutputColumns(): array { |
|
| 599 | - return array_unique($this->selectedColumns); |
|
| 600 | - } |
|
| 601 | - |
|
| 602 | - /** |
|
| 603 | - * Turns the query being built into a bulk delete query that ranges over |
|
| 604 | - * a certain table. |
|
| 605 | - * |
|
| 606 | - * <code> |
|
| 607 | - * $qb = $conn->getQueryBuilder() |
|
| 608 | - * ->delete('users', 'u') |
|
| 609 | - * ->where('u.id = :user_id'); |
|
| 610 | - * ->setParameter(':user_id', 1); |
|
| 611 | - * </code> |
|
| 612 | - * |
|
| 613 | - * @param string $delete The table whose rows are subject to the deletion. |
|
| 614 | - * @param string $alias The table alias used in the constructed query. |
|
| 615 | - * |
|
| 616 | - * @return $this This QueryBuilder instance. |
|
| 617 | - * @since 30.0.0 Alias is deprecated and will no longer be used with the next Doctrine/DBAL update |
|
| 618 | - */ |
|
| 619 | - public function delete($delete = null, $alias = null) { |
|
| 620 | - if ($alias !== null) { |
|
| 621 | - $this->logger->debug('DELETE queries with alias are no longer supported and the provided alias is ignored', ['exception' => new \InvalidArgumentException('Table alias provided for DELETE query')]); |
|
| 622 | - } |
|
| 623 | - |
|
| 624 | - $this->queryBuilder->delete( |
|
| 625 | - $this->getTableName($delete), |
|
| 626 | - $alias |
|
| 627 | - ); |
|
| 628 | - |
|
| 629 | - return $this; |
|
| 630 | - } |
|
| 631 | - |
|
| 632 | - /** |
|
| 633 | - * Turns the query being built into a bulk update query that ranges over |
|
| 634 | - * a certain table |
|
| 635 | - * |
|
| 636 | - * <code> |
|
| 637 | - * $qb = $conn->getQueryBuilder() |
|
| 638 | - * ->update('users', 'u') |
|
| 639 | - * ->set('u.password', md5('password')) |
|
| 640 | - * ->where('u.id = ?'); |
|
| 641 | - * </code> |
|
| 642 | - * |
|
| 643 | - * @param string $update The table whose rows are subject to the update. |
|
| 644 | - * @param string $alias The table alias used in the constructed query. |
|
| 645 | - * |
|
| 646 | - * @return $this This QueryBuilder instance. |
|
| 647 | - * @since 30.0.0 Alias is deprecated and will no longer be used with the next Doctrine/DBAL update |
|
| 648 | - */ |
|
| 649 | - public function update($update = null, $alias = null) { |
|
| 650 | - if ($alias !== null) { |
|
| 651 | - $this->logger->debug('UPDATE queries with alias are no longer supported and the provided alias is ignored', ['exception' => new \InvalidArgumentException('Table alias provided for UPDATE query')]); |
|
| 652 | - } |
|
| 653 | - |
|
| 654 | - $this->queryBuilder->update( |
|
| 655 | - $this->getTableName($update), |
|
| 656 | - $alias |
|
| 657 | - ); |
|
| 658 | - |
|
| 659 | - return $this; |
|
| 660 | - } |
|
| 661 | - |
|
| 662 | - /** |
|
| 663 | - * Turns the query being built into an insert query that inserts into |
|
| 664 | - * a certain table |
|
| 665 | - * |
|
| 666 | - * <code> |
|
| 667 | - * $qb = $conn->getQueryBuilder() |
|
| 668 | - * ->insert('users') |
|
| 669 | - * ->values( |
|
| 670 | - * array( |
|
| 671 | - * 'name' => '?', |
|
| 672 | - * 'password' => '?' |
|
| 673 | - * ) |
|
| 674 | - * ); |
|
| 675 | - * </code> |
|
| 676 | - * |
|
| 677 | - * @param string $insert The table into which the rows should be inserted. |
|
| 678 | - * |
|
| 679 | - * @return $this This QueryBuilder instance. |
|
| 680 | - */ |
|
| 681 | - public function insert($insert = null) { |
|
| 682 | - $this->queryBuilder->insert( |
|
| 683 | - $this->getTableName($insert) |
|
| 684 | - ); |
|
| 685 | - |
|
| 686 | - $this->lastInsertedTable = $insert; |
|
| 687 | - |
|
| 688 | - return $this; |
|
| 689 | - } |
|
| 690 | - |
|
| 691 | - /** |
|
| 692 | - * Creates and adds a query root corresponding to the table identified by the |
|
| 693 | - * given alias, forming a cartesian product with any existing query roots. |
|
| 694 | - * |
|
| 695 | - * <code> |
|
| 696 | - * $qb = $conn->getQueryBuilder() |
|
| 697 | - * ->select('u.id') |
|
| 698 | - * ->from('users', 'u') |
|
| 699 | - * </code> |
|
| 700 | - * |
|
| 701 | - * @param string|IQueryFunction $from The table. |
|
| 702 | - * @param string|null $alias The alias of the table. |
|
| 703 | - * |
|
| 704 | - * @return $this This QueryBuilder instance. |
|
| 705 | - */ |
|
| 706 | - public function from($from, $alias = null) { |
|
| 707 | - $this->queryBuilder->from( |
|
| 708 | - $this->getTableName($from), |
|
| 709 | - $this->quoteAlias($alias) |
|
| 710 | - ); |
|
| 711 | - |
|
| 712 | - return $this; |
|
| 713 | - } |
|
| 714 | - |
|
| 715 | - /** |
|
| 716 | - * Creates and adds a join to the query. |
|
| 717 | - * |
|
| 718 | - * <code> |
|
| 719 | - * $qb = $conn->getQueryBuilder() |
|
| 720 | - * ->select('u.name') |
|
| 721 | - * ->from('users', 'u') |
|
| 722 | - * ->join('u', 'phonenumbers', 'p', 'p.is_primary = 1'); |
|
| 723 | - * </code> |
|
| 724 | - * |
|
| 725 | - * @param string $fromAlias The alias that points to a from clause. |
|
| 726 | - * @param string $join The table name to join. |
|
| 727 | - * @param string $alias The alias of the join table. |
|
| 728 | - * @param string|ICompositeExpression|null $condition The condition for the join. |
|
| 729 | - * |
|
| 730 | - * @return $this This QueryBuilder instance. |
|
| 731 | - */ |
|
| 732 | - public function join($fromAlias, $join, $alias, $condition = null) { |
|
| 733 | - $this->queryBuilder->join( |
|
| 734 | - $this->quoteAlias($fromAlias), |
|
| 735 | - $this->getTableName($join), |
|
| 736 | - $this->quoteAlias($alias), |
|
| 737 | - $condition |
|
| 738 | - ); |
|
| 739 | - |
|
| 740 | - return $this; |
|
| 741 | - } |
|
| 742 | - |
|
| 743 | - /** |
|
| 744 | - * Creates and adds a join to the query. |
|
| 745 | - * |
|
| 746 | - * <code> |
|
| 747 | - * $qb = $conn->getQueryBuilder() |
|
| 748 | - * ->select('u.name') |
|
| 749 | - * ->from('users', 'u') |
|
| 750 | - * ->innerJoin('u', 'phonenumbers', 'p', 'p.is_primary = 1'); |
|
| 751 | - * </code> |
|
| 752 | - * |
|
| 753 | - * @param string $fromAlias The alias that points to a from clause. |
|
| 754 | - * @param string $join The table name to join. |
|
| 755 | - * @param string $alias The alias of the join table. |
|
| 756 | - * @param string|ICompositeExpression|null $condition The condition for the join. |
|
| 757 | - * |
|
| 758 | - * @return $this This QueryBuilder instance. |
|
| 759 | - */ |
|
| 760 | - public function innerJoin($fromAlias, $join, $alias, $condition = null) { |
|
| 761 | - $this->queryBuilder->innerJoin( |
|
| 762 | - $this->quoteAlias($fromAlias), |
|
| 763 | - $this->getTableName($join), |
|
| 764 | - $this->quoteAlias($alias), |
|
| 765 | - $condition |
|
| 766 | - ); |
|
| 767 | - |
|
| 768 | - return $this; |
|
| 769 | - } |
|
| 770 | - |
|
| 771 | - /** |
|
| 772 | - * Creates and adds a left join to the query. |
|
| 773 | - * |
|
| 774 | - * <code> |
|
| 775 | - * $qb = $conn->getQueryBuilder() |
|
| 776 | - * ->select('u.name') |
|
| 777 | - * ->from('users', 'u') |
|
| 778 | - * ->leftJoin('u', 'phonenumbers', 'p', 'p.is_primary = 1'); |
|
| 779 | - * </code> |
|
| 780 | - * |
|
| 781 | - * @param string $fromAlias The alias that points to a from clause. |
|
| 782 | - * @param string|IQueryFunction $join The table name or sub-query to join. |
|
| 783 | - * @param string $alias The alias of the join table. |
|
| 784 | - * @param string|ICompositeExpression|null $condition The condition for the join. |
|
| 785 | - * |
|
| 786 | - * @return $this This QueryBuilder instance. |
|
| 787 | - */ |
|
| 788 | - public function leftJoin($fromAlias, $join, $alias, $condition = null) { |
|
| 789 | - $this->queryBuilder->leftJoin( |
|
| 790 | - $this->quoteAlias($fromAlias), |
|
| 791 | - $this->getTableName($join), |
|
| 792 | - $this->quoteAlias($alias), |
|
| 793 | - $condition |
|
| 794 | - ); |
|
| 795 | - |
|
| 796 | - return $this; |
|
| 797 | - } |
|
| 798 | - |
|
| 799 | - /** |
|
| 800 | - * Creates and adds a right join to the query. |
|
| 801 | - * |
|
| 802 | - * <code> |
|
| 803 | - * $qb = $conn->getQueryBuilder() |
|
| 804 | - * ->select('u.name') |
|
| 805 | - * ->from('users', 'u') |
|
| 806 | - * ->rightJoin('u', 'phonenumbers', 'p', 'p.is_primary = 1'); |
|
| 807 | - * </code> |
|
| 808 | - * |
|
| 809 | - * @param string $fromAlias The alias that points to a from clause. |
|
| 810 | - * @param string $join The table name to join. |
|
| 811 | - * @param string $alias The alias of the join table. |
|
| 812 | - * @param string|ICompositeExpression|null $condition The condition for the join. |
|
| 813 | - * |
|
| 814 | - * @return $this This QueryBuilder instance. |
|
| 815 | - */ |
|
| 816 | - public function rightJoin($fromAlias, $join, $alias, $condition = null) { |
|
| 817 | - $this->queryBuilder->rightJoin( |
|
| 818 | - $this->quoteAlias($fromAlias), |
|
| 819 | - $this->getTableName($join), |
|
| 820 | - $this->quoteAlias($alias), |
|
| 821 | - $condition |
|
| 822 | - ); |
|
| 823 | - |
|
| 824 | - return $this; |
|
| 825 | - } |
|
| 826 | - |
|
| 827 | - /** |
|
| 828 | - * Sets a new value for a column in a bulk update query. |
|
| 829 | - * |
|
| 830 | - * <code> |
|
| 831 | - * $qb = $conn->getQueryBuilder() |
|
| 832 | - * ->update('users', 'u') |
|
| 833 | - * ->set('u.password', md5('password')) |
|
| 834 | - * ->where('u.id = ?'); |
|
| 835 | - * </code> |
|
| 836 | - * |
|
| 837 | - * @param string $key The column to set. |
|
| 838 | - * @param ILiteral|IParameter|IQueryFunction|string $value The value, expression, placeholder, etc. |
|
| 839 | - * |
|
| 840 | - * @return $this This QueryBuilder instance. |
|
| 841 | - */ |
|
| 842 | - public function set($key, $value) { |
|
| 843 | - $this->queryBuilder->set( |
|
| 844 | - $this->helper->quoteColumnName($key), |
|
| 845 | - $this->helper->quoteColumnName($value) |
|
| 846 | - ); |
|
| 847 | - |
|
| 848 | - return $this; |
|
| 849 | - } |
|
| 850 | - |
|
| 851 | - /** |
|
| 852 | - * Specifies one or more restrictions to the query result. |
|
| 853 | - * Replaces any previously specified restrictions, if any. |
|
| 854 | - * |
|
| 855 | - * <code> |
|
| 856 | - * $qb = $conn->getQueryBuilder() |
|
| 857 | - * ->select('u.name') |
|
| 858 | - * ->from('users', 'u') |
|
| 859 | - * ->where('u.id = ?'); |
|
| 860 | - * |
|
| 861 | - * // You can optionally programmatically build and/or expressions |
|
| 862 | - * $qb = $conn->getQueryBuilder(); |
|
| 863 | - * |
|
| 864 | - * $or = $qb->expr()->orx( |
|
| 865 | - * $qb->expr()->eq('u.id', 1), |
|
| 866 | - * $qb->expr()->eq('u.id', 2), |
|
| 867 | - * ); |
|
| 868 | - * |
|
| 869 | - * $qb->update('users', 'u') |
|
| 870 | - * ->set('u.password', md5('password')) |
|
| 871 | - * ->where($or); |
|
| 872 | - * </code> |
|
| 873 | - * |
|
| 874 | - * @param mixed ...$predicates The restriction predicates. |
|
| 875 | - * |
|
| 876 | - * @return $this This QueryBuilder instance. |
|
| 877 | - */ |
|
| 878 | - public function where(...$predicates) { |
|
| 879 | - if ($this->nonEmptyWhere && $this->systemConfig->getValue('debug', false)) { |
|
| 880 | - // Only logging a warning, not throwing for now. |
|
| 881 | - $e = new QueryException('Using where() on non-empty WHERE part, please verify it is intentional to not call andWhere() or orWhere() instead. Otherwise consider creating a new query builder object or call resetQueryPart(\'where\') first.'); |
|
| 882 | - $this->logger->warning($e->getMessage(), ['exception' => $e]); |
|
| 883 | - } |
|
| 884 | - |
|
| 885 | - $this->nonEmptyWhere = true; |
|
| 886 | - |
|
| 887 | - call_user_func_array( |
|
| 888 | - [$this->queryBuilder, 'where'], |
|
| 889 | - $predicates |
|
| 890 | - ); |
|
| 891 | - |
|
| 892 | - return $this; |
|
| 893 | - } |
|
| 894 | - |
|
| 895 | - /** |
|
| 896 | - * Adds one or more restrictions to the query results, forming a logical |
|
| 897 | - * conjunction with any previously specified restrictions. |
|
| 898 | - * |
|
| 899 | - * <code> |
|
| 900 | - * $qb = $conn->getQueryBuilder() |
|
| 901 | - * ->select('u') |
|
| 902 | - * ->from('users', 'u') |
|
| 903 | - * ->where('u.username LIKE ?') |
|
| 904 | - * ->andWhere('u.is_active = 1'); |
|
| 905 | - * </code> |
|
| 906 | - * |
|
| 907 | - * @param mixed ...$where The query restrictions. |
|
| 908 | - * |
|
| 909 | - * @return $this This QueryBuilder instance. |
|
| 910 | - * |
|
| 911 | - * @see where() |
|
| 912 | - */ |
|
| 913 | - public function andWhere(...$where) { |
|
| 914 | - $this->nonEmptyWhere = true; |
|
| 915 | - call_user_func_array( |
|
| 916 | - [$this->queryBuilder, 'andWhere'], |
|
| 917 | - $where |
|
| 918 | - ); |
|
| 919 | - |
|
| 920 | - return $this; |
|
| 921 | - } |
|
| 922 | - |
|
| 923 | - /** |
|
| 924 | - * Adds one or more restrictions to the query results, forming a logical |
|
| 925 | - * disjunction with any previously specified restrictions. |
|
| 926 | - * |
|
| 927 | - * <code> |
|
| 928 | - * $qb = $conn->getQueryBuilder() |
|
| 929 | - * ->select('u.name') |
|
| 930 | - * ->from('users', 'u') |
|
| 931 | - * ->where('u.id = 1') |
|
| 932 | - * ->orWhere('u.id = 2'); |
|
| 933 | - * </code> |
|
| 934 | - * |
|
| 935 | - * @param mixed ...$where The WHERE statement. |
|
| 936 | - * |
|
| 937 | - * @return $this This QueryBuilder instance. |
|
| 938 | - * |
|
| 939 | - * @see where() |
|
| 940 | - */ |
|
| 941 | - public function orWhere(...$where) { |
|
| 942 | - $this->nonEmptyWhere = true; |
|
| 943 | - call_user_func_array( |
|
| 944 | - [$this->queryBuilder, 'orWhere'], |
|
| 945 | - $where |
|
| 946 | - ); |
|
| 947 | - |
|
| 948 | - return $this; |
|
| 949 | - } |
|
| 950 | - |
|
| 951 | - /** |
|
| 952 | - * Specifies a grouping over the results of the query. |
|
| 953 | - * Replaces any previously specified groupings, if any. |
|
| 954 | - * |
|
| 955 | - * <code> |
|
| 956 | - * $qb = $conn->getQueryBuilder() |
|
| 957 | - * ->select('u.name') |
|
| 958 | - * ->from('users', 'u') |
|
| 959 | - * ->groupBy('u.id'); |
|
| 960 | - * </code> |
|
| 961 | - * |
|
| 962 | - * @param mixed ...$groupBys The grouping expression. |
|
| 963 | - * |
|
| 964 | - * @return $this This QueryBuilder instance. |
|
| 965 | - */ |
|
| 966 | - public function groupBy(...$groupBys) { |
|
| 967 | - if (count($groupBys) === 1 && is_array($groupBys[0])) { |
|
| 968 | - $groupBys = $groupBys[0]; |
|
| 969 | - } |
|
| 970 | - |
|
| 971 | - call_user_func_array( |
|
| 972 | - [$this->queryBuilder, 'groupBy'], |
|
| 973 | - $this->helper->quoteColumnNames($groupBys) |
|
| 974 | - ); |
|
| 975 | - |
|
| 976 | - return $this; |
|
| 977 | - } |
|
| 978 | - |
|
| 979 | - /** |
|
| 980 | - * Adds a grouping expression to the query. |
|
| 981 | - * |
|
| 982 | - * <code> |
|
| 983 | - * $qb = $conn->getQueryBuilder() |
|
| 984 | - * ->select('u.name') |
|
| 985 | - * ->from('users', 'u') |
|
| 986 | - * ->groupBy('u.lastLogin'); |
|
| 987 | - * ->addGroupBy('u.createdAt') |
|
| 988 | - * </code> |
|
| 989 | - * |
|
| 990 | - * @param mixed ...$groupBy The grouping expression. |
|
| 991 | - * |
|
| 992 | - * @return $this This QueryBuilder instance. |
|
| 993 | - */ |
|
| 994 | - public function addGroupBy(...$groupBy) { |
|
| 995 | - call_user_func_array( |
|
| 996 | - [$this->queryBuilder, 'addGroupBy'], |
|
| 997 | - $this->helper->quoteColumnNames($groupBy) |
|
| 998 | - ); |
|
| 999 | - |
|
| 1000 | - return $this; |
|
| 1001 | - } |
|
| 1002 | - |
|
| 1003 | - /** |
|
| 1004 | - * Sets a value for a column in an insert query. |
|
| 1005 | - * |
|
| 1006 | - * <code> |
|
| 1007 | - * $qb = $conn->getQueryBuilder() |
|
| 1008 | - * ->insert('users') |
|
| 1009 | - * ->values( |
|
| 1010 | - * array( |
|
| 1011 | - * 'name' => '?' |
|
| 1012 | - * ) |
|
| 1013 | - * ) |
|
| 1014 | - * ->setValue('password', '?'); |
|
| 1015 | - * </code> |
|
| 1016 | - * |
|
| 1017 | - * @param string $column The column into which the value should be inserted. |
|
| 1018 | - * @param IParameter|string $value The value that should be inserted into the column. |
|
| 1019 | - * |
|
| 1020 | - * @return $this This QueryBuilder instance. |
|
| 1021 | - */ |
|
| 1022 | - public function setValue($column, $value) { |
|
| 1023 | - $this->queryBuilder->setValue( |
|
| 1024 | - $this->helper->quoteColumnName($column), |
|
| 1025 | - (string)$value |
|
| 1026 | - ); |
|
| 1027 | - |
|
| 1028 | - return $this; |
|
| 1029 | - } |
|
| 1030 | - |
|
| 1031 | - /** |
|
| 1032 | - * Specifies values for an insert query indexed by column names. |
|
| 1033 | - * Replaces any previous values, if any. |
|
| 1034 | - * |
|
| 1035 | - * <code> |
|
| 1036 | - * $qb = $conn->getQueryBuilder() |
|
| 1037 | - * ->insert('users') |
|
| 1038 | - * ->values( |
|
| 1039 | - * array( |
|
| 1040 | - * 'name' => '?', |
|
| 1041 | - * 'password' => '?' |
|
| 1042 | - * ) |
|
| 1043 | - * ); |
|
| 1044 | - * </code> |
|
| 1045 | - * |
|
| 1046 | - * @param array $values The values to specify for the insert query indexed by column names. |
|
| 1047 | - * |
|
| 1048 | - * @return $this This QueryBuilder instance. |
|
| 1049 | - */ |
|
| 1050 | - public function values(array $values) { |
|
| 1051 | - $quotedValues = []; |
|
| 1052 | - foreach ($values as $key => $value) { |
|
| 1053 | - $quotedValues[$this->helper->quoteColumnName($key)] = $value; |
|
| 1054 | - } |
|
| 1055 | - |
|
| 1056 | - $this->queryBuilder->values($quotedValues); |
|
| 1057 | - |
|
| 1058 | - return $this; |
|
| 1059 | - } |
|
| 1060 | - |
|
| 1061 | - /** |
|
| 1062 | - * Specifies a restriction over the groups of the query. |
|
| 1063 | - * Replaces any previous having restrictions, if any. |
|
| 1064 | - * |
|
| 1065 | - * @param mixed ...$having The restriction over the groups. |
|
| 1066 | - * |
|
| 1067 | - * @return $this This QueryBuilder instance. |
|
| 1068 | - */ |
|
| 1069 | - public function having(...$having) { |
|
| 1070 | - call_user_func_array( |
|
| 1071 | - [$this->queryBuilder, 'having'], |
|
| 1072 | - $having |
|
| 1073 | - ); |
|
| 1074 | - |
|
| 1075 | - return $this; |
|
| 1076 | - } |
|
| 1077 | - |
|
| 1078 | - /** |
|
| 1079 | - * Adds a restriction over the groups of the query, forming a logical |
|
| 1080 | - * conjunction with any existing having restrictions. |
|
| 1081 | - * |
|
| 1082 | - * @param mixed ...$having The restriction to append. |
|
| 1083 | - * |
|
| 1084 | - * @return $this This QueryBuilder instance. |
|
| 1085 | - */ |
|
| 1086 | - public function andHaving(...$having) { |
|
| 1087 | - call_user_func_array( |
|
| 1088 | - [$this->queryBuilder, 'andHaving'], |
|
| 1089 | - $having |
|
| 1090 | - ); |
|
| 1091 | - |
|
| 1092 | - return $this; |
|
| 1093 | - } |
|
| 1094 | - |
|
| 1095 | - /** |
|
| 1096 | - * Adds a restriction over the groups of the query, forming a logical |
|
| 1097 | - * disjunction with any existing having restrictions. |
|
| 1098 | - * |
|
| 1099 | - * @param mixed ...$having The restriction to add. |
|
| 1100 | - * |
|
| 1101 | - * @return $this This QueryBuilder instance. |
|
| 1102 | - */ |
|
| 1103 | - public function orHaving(...$having) { |
|
| 1104 | - call_user_func_array( |
|
| 1105 | - [$this->queryBuilder, 'orHaving'], |
|
| 1106 | - $having |
|
| 1107 | - ); |
|
| 1108 | - |
|
| 1109 | - return $this; |
|
| 1110 | - } |
|
| 1111 | - |
|
| 1112 | - /** |
|
| 1113 | - * Specifies an ordering for the query results. |
|
| 1114 | - * Replaces any previously specified orderings, if any. |
|
| 1115 | - * |
|
| 1116 | - * @param string|IQueryFunction|ILiteral|IParameter $sort The ordering expression. |
|
| 1117 | - * @param string $order The ordering direction. |
|
| 1118 | - * |
|
| 1119 | - * @return $this This QueryBuilder instance. |
|
| 1120 | - */ |
|
| 1121 | - public function orderBy($sort, $order = null) { |
|
| 1122 | - $this->queryBuilder->orderBy( |
|
| 1123 | - $this->helper->quoteColumnName($sort), |
|
| 1124 | - $order |
|
| 1125 | - ); |
|
| 1126 | - |
|
| 1127 | - return $this; |
|
| 1128 | - } |
|
| 1129 | - |
|
| 1130 | - /** |
|
| 1131 | - * Adds an ordering to the query results. |
|
| 1132 | - * |
|
| 1133 | - * @param string|ILiteral|IParameter|IQueryFunction $sort The ordering expression. |
|
| 1134 | - * @param string $order The ordering direction. |
|
| 1135 | - * |
|
| 1136 | - * @return $this This QueryBuilder instance. |
|
| 1137 | - */ |
|
| 1138 | - public function addOrderBy($sort, $order = null) { |
|
| 1139 | - $this->queryBuilder->addOrderBy( |
|
| 1140 | - $this->helper->quoteColumnName($sort), |
|
| 1141 | - $order |
|
| 1142 | - ); |
|
| 1143 | - |
|
| 1144 | - return $this; |
|
| 1145 | - } |
|
| 1146 | - |
|
| 1147 | - /** |
|
| 1148 | - * Gets a query part by its name. |
|
| 1149 | - * |
|
| 1150 | - * @param string $queryPartName |
|
| 1151 | - * |
|
| 1152 | - * @return mixed |
|
| 1153 | - * @deprecated 30.0.0 This function is going to be removed with the next Doctrine/DBAL update |
|
| 1154 | - * and we can not fix this in our wrapper. Please track the details you need, outside the object. |
|
| 1155 | - */ |
|
| 1156 | - public function getQueryPart($queryPartName) { |
|
| 1157 | - $this->logger->debug(IQueryBuilder::class . '::' . __FUNCTION__ . ' is deprecated and will be removed soon.', ['exception' => new \Exception('Deprecated call to ' . __METHOD__)]); |
|
| 1158 | - return $this->queryBuilder->getQueryPart($queryPartName); |
|
| 1159 | - } |
|
| 1160 | - |
|
| 1161 | - /** |
|
| 1162 | - * Gets all query parts. |
|
| 1163 | - * |
|
| 1164 | - * @return array |
|
| 1165 | - * @deprecated 30.0.0 This function is going to be removed with the next Doctrine/DBAL update |
|
| 1166 | - * and we can not fix this in our wrapper. Please track the details you need, outside the object. |
|
| 1167 | - */ |
|
| 1168 | - public function getQueryParts() { |
|
| 1169 | - $this->logger->debug(IQueryBuilder::class . '::' . __FUNCTION__ . ' is deprecated and will be removed soon.', ['exception' => new \Exception('Deprecated call to ' . __METHOD__)]); |
|
| 1170 | - return $this->queryBuilder->getQueryParts(); |
|
| 1171 | - } |
|
| 1172 | - |
|
| 1173 | - /** |
|
| 1174 | - * Resets SQL parts. |
|
| 1175 | - * |
|
| 1176 | - * @param array|null $queryPartNames |
|
| 1177 | - * |
|
| 1178 | - * @return $this This QueryBuilder instance. |
|
| 1179 | - * @deprecated 30.0.0 This function is going to be removed with the next Doctrine/DBAL update |
|
| 1180 | - * and we can not fix this in our wrapper. Please create a new IQueryBuilder instead. |
|
| 1181 | - */ |
|
| 1182 | - public function resetQueryParts($queryPartNames = null) { |
|
| 1183 | - $this->logger->debug(IQueryBuilder::class . '::' . __FUNCTION__ . ' is deprecated and will be removed soon.', ['exception' => new \Exception('Deprecated call to ' . __METHOD__)]); |
|
| 1184 | - $this->queryBuilder->resetQueryParts($queryPartNames); |
|
| 1185 | - |
|
| 1186 | - return $this; |
|
| 1187 | - } |
|
| 1188 | - |
|
| 1189 | - /** |
|
| 1190 | - * Resets a single SQL part. |
|
| 1191 | - * |
|
| 1192 | - * @param string $queryPartName |
|
| 1193 | - * |
|
| 1194 | - * @return $this This QueryBuilder instance. |
|
| 1195 | - * @deprecated 30.0.0 This function is going to be removed with the next Doctrine/DBAL update |
|
| 1196 | - * and we can not fix this in our wrapper. Please create a new IQueryBuilder instead. |
|
| 1197 | - */ |
|
| 1198 | - public function resetQueryPart($queryPartName) { |
|
| 1199 | - $this->logger->debug(IQueryBuilder::class . '::' . __FUNCTION__ . ' is deprecated and will be removed soon.', ['exception' => new \Exception('Deprecated call to ' . __METHOD__)]); |
|
| 1200 | - $this->queryBuilder->resetQueryPart($queryPartName); |
|
| 1201 | - |
|
| 1202 | - return $this; |
|
| 1203 | - } |
|
| 1204 | - |
|
| 1205 | - /** |
|
| 1206 | - * Creates a new named parameter and bind the value $value to it. |
|
| 1207 | - * |
|
| 1208 | - * This method provides a shortcut for PDOStatement::bindValue |
|
| 1209 | - * when using prepared statements. |
|
| 1210 | - * |
|
| 1211 | - * The parameter $value specifies the value that you want to bind. If |
|
| 1212 | - * $placeholder is not provided bindValue() will automatically create a |
|
| 1213 | - * placeholder for you. An automatic placeholder will be of the name |
|
| 1214 | - * ':dcValue1', ':dcValue2' etc. |
|
| 1215 | - * |
|
| 1216 | - * For more information see {@link https://www.php.net/pdostatement-bindparam} |
|
| 1217 | - * |
|
| 1218 | - * Example: |
|
| 1219 | - * <code> |
|
| 1220 | - * $value = 2; |
|
| 1221 | - * $q->eq( 'id', $q->bindValue( $value ) ); |
|
| 1222 | - * $stmt = $q->executeQuery(); // executed with 'id = 2' |
|
| 1223 | - * </code> |
|
| 1224 | - * |
|
| 1225 | - * @license New BSD License |
|
| 1226 | - * @link http://www.zetacomponents.org |
|
| 1227 | - * |
|
| 1228 | - * @param mixed $value |
|
| 1229 | - * @param IQueryBuilder::PARAM_* $type |
|
| 1230 | - * @param string $placeHolder The name to bind with. The string must start with a colon ':'. |
|
| 1231 | - * |
|
| 1232 | - * @return IParameter the placeholder name used. |
|
| 1233 | - */ |
|
| 1234 | - public function createNamedParameter($value, $type = IQueryBuilder::PARAM_STR, $placeHolder = null) { |
|
| 1235 | - return new Parameter($this->queryBuilder->createNamedParameter($value, $type, $placeHolder)); |
|
| 1236 | - } |
|
| 1237 | - |
|
| 1238 | - /** |
|
| 1239 | - * Creates a new positional parameter and bind the given value to it. |
|
| 1240 | - * |
|
| 1241 | - * Attention: If you are using positional parameters with the query builder you have |
|
| 1242 | - * to be very careful to bind all parameters in the order they appear in the SQL |
|
| 1243 | - * statement , otherwise they get bound in the wrong order which can lead to serious |
|
| 1244 | - * bugs in your code. |
|
| 1245 | - * |
|
| 1246 | - * Example: |
|
| 1247 | - * <code> |
|
| 1248 | - * $qb = $conn->getQueryBuilder(); |
|
| 1249 | - * $qb->select('u.*') |
|
| 1250 | - * ->from('users', 'u') |
|
| 1251 | - * ->where('u.username = ' . $qb->createPositionalParameter('Foo', IQueryBuilder::PARAM_STR)) |
|
| 1252 | - * ->orWhere('u.username = ' . $qb->createPositionalParameter('Bar', IQueryBuilder::PARAM_STR)) |
|
| 1253 | - * </code> |
|
| 1254 | - * |
|
| 1255 | - * @param mixed $value |
|
| 1256 | - * @param IQueryBuilder::PARAM_* $type |
|
| 1257 | - * |
|
| 1258 | - * @return IParameter |
|
| 1259 | - */ |
|
| 1260 | - public function createPositionalParameter($value, $type = IQueryBuilder::PARAM_STR) { |
|
| 1261 | - return new Parameter($this->queryBuilder->createPositionalParameter($value, $type)); |
|
| 1262 | - } |
|
| 1263 | - |
|
| 1264 | - /** |
|
| 1265 | - * Creates a new parameter |
|
| 1266 | - * |
|
| 1267 | - * Example: |
|
| 1268 | - * <code> |
|
| 1269 | - * $qb = $conn->getQueryBuilder(); |
|
| 1270 | - * $qb->select('u.*') |
|
| 1271 | - * ->from('users', 'u') |
|
| 1272 | - * ->where('u.username = ' . $qb->createParameter('name')) |
|
| 1273 | - * ->setParameter('name', 'Bar', IQueryBuilder::PARAM_STR)) |
|
| 1274 | - * </code> |
|
| 1275 | - * |
|
| 1276 | - * @param string $name |
|
| 1277 | - * |
|
| 1278 | - * @return IParameter |
|
| 1279 | - */ |
|
| 1280 | - public function createParameter($name) { |
|
| 1281 | - return new Parameter(':' . $name); |
|
| 1282 | - } |
|
| 1283 | - |
|
| 1284 | - /** |
|
| 1285 | - * Creates a new function |
|
| 1286 | - * |
|
| 1287 | - * Attention: Column names inside the call have to be quoted before hand |
|
| 1288 | - * |
|
| 1289 | - * Example: |
|
| 1290 | - * <code> |
|
| 1291 | - * $qb = $conn->getQueryBuilder(); |
|
| 1292 | - * $qb->select($qb->createFunction('COUNT(*)')) |
|
| 1293 | - * ->from('users', 'u') |
|
| 1294 | - * echo $qb->getSQL(); // SELECT COUNT(*) FROM `users` u |
|
| 1295 | - * </code> |
|
| 1296 | - * <code> |
|
| 1297 | - * $qb = $conn->getQueryBuilder(); |
|
| 1298 | - * $qb->select($qb->createFunction('COUNT(`column`)')) |
|
| 1299 | - * ->from('users', 'u') |
|
| 1300 | - * echo $qb->getSQL(); // SELECT COUNT(`column`) FROM `users` u |
|
| 1301 | - * </code> |
|
| 1302 | - * |
|
| 1303 | - * @param string $call |
|
| 1304 | - * |
|
| 1305 | - * @return IQueryFunction |
|
| 1306 | - */ |
|
| 1307 | - public function createFunction($call) { |
|
| 1308 | - return new QueryFunction($call); |
|
| 1309 | - } |
|
| 1310 | - |
|
| 1311 | - /** |
|
| 1312 | - * Used to get the id of the last inserted element |
|
| 1313 | - * @return int |
|
| 1314 | - * @throws \BadMethodCallException When being called before an insert query has been run. |
|
| 1315 | - */ |
|
| 1316 | - public function getLastInsertId(): int { |
|
| 1317 | - if ($this->getType() === \Doctrine\DBAL\Query\QueryBuilder::INSERT && $this->lastInsertedTable) { |
|
| 1318 | - // lastInsertId() needs the prefix but no quotes |
|
| 1319 | - $table = $this->prefixTableName($this->lastInsertedTable); |
|
| 1320 | - return $this->connection->lastInsertId($table); |
|
| 1321 | - } |
|
| 1322 | - |
|
| 1323 | - throw new \BadMethodCallException('Invalid call to getLastInsertId without using insert() before.'); |
|
| 1324 | - } |
|
| 1325 | - |
|
| 1326 | - /** |
|
| 1327 | - * Returns the table name quoted and with database prefix as needed by the implementation |
|
| 1328 | - * |
|
| 1329 | - * @param string|IQueryFunction $table |
|
| 1330 | - * @return string |
|
| 1331 | - */ |
|
| 1332 | - public function getTableName($table) { |
|
| 1333 | - if ($table instanceof IQueryFunction) { |
|
| 1334 | - return (string)$table; |
|
| 1335 | - } |
|
| 1336 | - |
|
| 1337 | - $table = $this->prefixTableName($table); |
|
| 1338 | - return $this->helper->quoteColumnName($table); |
|
| 1339 | - } |
|
| 1340 | - |
|
| 1341 | - /** |
|
| 1342 | - * Returns the table name with database prefix as needed by the implementation |
|
| 1343 | - * |
|
| 1344 | - * Was protected until version 30. |
|
| 1345 | - * |
|
| 1346 | - * @param string $table |
|
| 1347 | - * @return string |
|
| 1348 | - */ |
|
| 1349 | - public function prefixTableName(string $table): string { |
|
| 1350 | - if ($this->automaticTablePrefix === false || str_starts_with($table, '*PREFIX*')) { |
|
| 1351 | - return $table; |
|
| 1352 | - } |
|
| 1353 | - |
|
| 1354 | - return '*PREFIX*' . $table; |
|
| 1355 | - } |
|
| 1356 | - |
|
| 1357 | - /** |
|
| 1358 | - * Returns the column name quoted and with table alias prefix as needed by the implementation |
|
| 1359 | - * |
|
| 1360 | - * @param string $column |
|
| 1361 | - * @param string $tableAlias |
|
| 1362 | - * @return string |
|
| 1363 | - */ |
|
| 1364 | - public function getColumnName($column, $tableAlias = '') { |
|
| 1365 | - if ($tableAlias !== '') { |
|
| 1366 | - $tableAlias .= '.'; |
|
| 1367 | - } |
|
| 1368 | - |
|
| 1369 | - return $this->helper->quoteColumnName($tableAlias . $column); |
|
| 1370 | - } |
|
| 1371 | - |
|
| 1372 | - /** |
|
| 1373 | - * Returns the column name quoted and with table alias prefix as needed by the implementation |
|
| 1374 | - * |
|
| 1375 | - * @param string $alias |
|
| 1376 | - * @return string |
|
| 1377 | - */ |
|
| 1378 | - public function quoteAlias($alias) { |
|
| 1379 | - if ($alias === '' || $alias === null) { |
|
| 1380 | - return $alias; |
|
| 1381 | - } |
|
| 1382 | - |
|
| 1383 | - return $this->helper->quoteColumnName($alias); |
|
| 1384 | - } |
|
| 1385 | - |
|
| 1386 | - public function escapeLikeParameter(string $parameter): string { |
|
| 1387 | - return $this->connection->escapeLikeParameter($parameter); |
|
| 1388 | - } |
|
| 1389 | - |
|
| 1390 | - public function hintShardKey(string $column, mixed $value, bool $overwrite = false): self { |
|
| 1391 | - return $this; |
|
| 1392 | - } |
|
| 1393 | - |
|
| 1394 | - public function runAcrossAllShards(): self { |
|
| 1395 | - // noop |
|
| 1396 | - return $this; |
|
| 1397 | - } |
|
| 32 | + /** @var ConnectionAdapter */ |
|
| 33 | + private $connection; |
|
| 34 | + |
|
| 35 | + /** @var SystemConfig */ |
|
| 36 | + private $systemConfig; |
|
| 37 | + |
|
| 38 | + private LoggerInterface $logger; |
|
| 39 | + |
|
| 40 | + /** @var \Doctrine\DBAL\Query\QueryBuilder */ |
|
| 41 | + private $queryBuilder; |
|
| 42 | + |
|
| 43 | + /** @var QuoteHelper */ |
|
| 44 | + private $helper; |
|
| 45 | + |
|
| 46 | + /** @var bool */ |
|
| 47 | + private $automaticTablePrefix = true; |
|
| 48 | + private bool $nonEmptyWhere = false; |
|
| 49 | + |
|
| 50 | + /** @var string */ |
|
| 51 | + protected $lastInsertedTable; |
|
| 52 | + private array $selectedColumns = []; |
|
| 53 | + |
|
| 54 | + /** |
|
| 55 | + * Initializes a new QueryBuilder. |
|
| 56 | + * |
|
| 57 | + * @param ConnectionAdapter $connection |
|
| 58 | + * @param SystemConfig $systemConfig |
|
| 59 | + */ |
|
| 60 | + public function __construct(ConnectionAdapter $connection, SystemConfig $systemConfig, LoggerInterface $logger) { |
|
| 61 | + $this->connection = $connection; |
|
| 62 | + $this->systemConfig = $systemConfig; |
|
| 63 | + $this->logger = $logger; |
|
| 64 | + $this->queryBuilder = new \Doctrine\DBAL\Query\QueryBuilder($this->connection->getInner()); |
|
| 65 | + $this->helper = new QuoteHelper(); |
|
| 66 | + } |
|
| 67 | + |
|
| 68 | + /** |
|
| 69 | + * Enable/disable automatic prefixing of table names with the oc_ prefix |
|
| 70 | + * |
|
| 71 | + * @param bool $enabled If set to true table names will be prefixed with the |
|
| 72 | + * owncloud database prefix automatically. |
|
| 73 | + * @since 8.2.0 |
|
| 74 | + */ |
|
| 75 | + public function automaticTablePrefix($enabled) { |
|
| 76 | + $this->automaticTablePrefix = (bool)$enabled; |
|
| 77 | + } |
|
| 78 | + |
|
| 79 | + /** |
|
| 80 | + * Gets an ExpressionBuilder used for object-oriented construction of query expressions. |
|
| 81 | + * This producer method is intended for convenient inline usage. Example: |
|
| 82 | + * |
|
| 83 | + * <code> |
|
| 84 | + * $qb = $conn->getQueryBuilder() |
|
| 85 | + * ->select('u') |
|
| 86 | + * ->from('users', 'u') |
|
| 87 | + * ->where($qb->expr()->eq('u.id', 1)); |
|
| 88 | + * </code> |
|
| 89 | + * |
|
| 90 | + * For more complex expression construction, consider storing the expression |
|
| 91 | + * builder object in a local variable. |
|
| 92 | + * |
|
| 93 | + * @return \OCP\DB\QueryBuilder\IExpressionBuilder |
|
| 94 | + */ |
|
| 95 | + public function expr() { |
|
| 96 | + return match($this->connection->getDatabaseProvider()) { |
|
| 97 | + IDBConnection::PLATFORM_ORACLE => new OCIExpressionBuilder($this->connection, $this, $this->logger), |
|
| 98 | + IDBConnection::PLATFORM_POSTGRES => new PgSqlExpressionBuilder($this->connection, $this, $this->logger), |
|
| 99 | + IDBConnection::PLATFORM_MARIADB, |
|
| 100 | + IDBConnection::PLATFORM_MYSQL => new MySqlExpressionBuilder($this->connection, $this, $this->logger), |
|
| 101 | + IDBConnection::PLATFORM_SQLITE => new SqliteExpressionBuilder($this->connection, $this, $this->logger), |
|
| 102 | + }; |
|
| 103 | + } |
|
| 104 | + |
|
| 105 | + /** |
|
| 106 | + * Gets an FunctionBuilder used for object-oriented construction of query functions. |
|
| 107 | + * This producer method is intended for convenient inline usage. Example: |
|
| 108 | + * |
|
| 109 | + * <code> |
|
| 110 | + * $qb = $conn->getQueryBuilder() |
|
| 111 | + * ->select('u') |
|
| 112 | + * ->from('users', 'u') |
|
| 113 | + * ->where($qb->fun()->md5('u.id')); |
|
| 114 | + * </code> |
|
| 115 | + * |
|
| 116 | + * For more complex function construction, consider storing the function |
|
| 117 | + * builder object in a local variable. |
|
| 118 | + * |
|
| 119 | + * @return \OCP\DB\QueryBuilder\IFunctionBuilder |
|
| 120 | + */ |
|
| 121 | + public function func() { |
|
| 122 | + return match($this->connection->getDatabaseProvider()) { |
|
| 123 | + IDBConnection::PLATFORM_ORACLE => new OCIFunctionBuilder($this->connection, $this, $this->helper), |
|
| 124 | + IDBConnection::PLATFORM_POSTGRES => new PgSqlFunctionBuilder($this->connection, $this, $this->helper), |
|
| 125 | + IDBConnection::PLATFORM_MARIADB, |
|
| 126 | + IDBConnection::PLATFORM_MYSQL => new FunctionBuilder($this->connection, $this, $this->helper), |
|
| 127 | + IDBConnection::PLATFORM_SQLITE => new SqliteFunctionBuilder($this->connection, $this, $this->helper), |
|
| 128 | + }; |
|
| 129 | + } |
|
| 130 | + |
|
| 131 | + /** |
|
| 132 | + * Gets the type of the currently built query. |
|
| 133 | + * |
|
| 134 | + * @return integer |
|
| 135 | + */ |
|
| 136 | + public function getType() { |
|
| 137 | + return $this->queryBuilder->getType(); |
|
| 138 | + } |
|
| 139 | + |
|
| 140 | + /** |
|
| 141 | + * Gets the associated DBAL Connection for this query builder. |
|
| 142 | + * |
|
| 143 | + * @return \OCP\IDBConnection |
|
| 144 | + */ |
|
| 145 | + public function getConnection() { |
|
| 146 | + return $this->connection; |
|
| 147 | + } |
|
| 148 | + |
|
| 149 | + /** |
|
| 150 | + * Gets the state of this query builder instance. |
|
| 151 | + * |
|
| 152 | + * @return int Always returns 0 which is former `QueryBuilder::STATE_DIRTY` |
|
| 153 | + * @deprecated 30.0.0 This function is going to be removed with the next Doctrine/DBAL update |
|
| 154 | + * and we can not fix this in our wrapper. |
|
| 155 | + */ |
|
| 156 | + public function getState() { |
|
| 157 | + $this->logger->debug(IQueryBuilder::class . '::' . __FUNCTION__ . ' is deprecated and will be removed soon.', ['exception' => new \Exception('Deprecated call to ' . __METHOD__)]); |
|
| 158 | + return $this->queryBuilder->getState(); |
|
| 159 | + } |
|
| 160 | + |
|
| 161 | + private function prepareForExecute() { |
|
| 162 | + if ($this->systemConfig->getValue('log_query', false)) { |
|
| 163 | + try { |
|
| 164 | + $params = []; |
|
| 165 | + foreach ($this->getParameters() as $placeholder => $value) { |
|
| 166 | + if ($value instanceof \DateTimeInterface) { |
|
| 167 | + $params[] = $placeholder . ' => DateTime:\'' . $value->format('c') . '\''; |
|
| 168 | + } elseif (is_array($value)) { |
|
| 169 | + $params[] = $placeholder . ' => (\'' . implode('\', \'', $value) . '\')'; |
|
| 170 | + } else { |
|
| 171 | + $params[] = $placeholder . ' => \'' . $value . '\''; |
|
| 172 | + } |
|
| 173 | + } |
|
| 174 | + if (empty($params)) { |
|
| 175 | + $this->logger->debug('DB QueryBuilder: \'{query}\'', [ |
|
| 176 | + 'query' => $this->getSQL(), |
|
| 177 | + 'app' => 'core', |
|
| 178 | + ]); |
|
| 179 | + } else { |
|
| 180 | + $this->logger->debug('DB QueryBuilder: \'{query}\' with parameters: {params}', [ |
|
| 181 | + 'query' => $this->getSQL(), |
|
| 182 | + 'params' => implode(', ', $params), |
|
| 183 | + 'app' => 'core', |
|
| 184 | + ]); |
|
| 185 | + } |
|
| 186 | + } catch (\Error $e) { |
|
| 187 | + // likely an error during conversion of $value to string |
|
| 188 | + $this->logger->error('DB QueryBuilder: error trying to log SQL query', ['exception' => $e]); |
|
| 189 | + } |
|
| 190 | + } |
|
| 191 | + |
|
| 192 | + // if (!empty($this->getQueryPart('select'))) { |
|
| 193 | + // $select = $this->getQueryPart('select'); |
|
| 194 | + // $hasSelectAll = array_filter($select, static function ($s) { |
|
| 195 | + // return $s === '*'; |
|
| 196 | + // }); |
|
| 197 | + // $hasSelectSpecific = array_filter($select, static function ($s) { |
|
| 198 | + // return $s !== '*'; |
|
| 199 | + // }); |
|
| 200 | + |
|
| 201 | + // if (empty($hasSelectAll) === empty($hasSelectSpecific)) { |
|
| 202 | + // $exception = new QueryException('Query is selecting * and specific values in the same query. This is not supported in Oracle.'); |
|
| 203 | + // $this->logger->error($exception->getMessage(), [ |
|
| 204 | + // 'query' => $this->getSQL(), |
|
| 205 | + // 'app' => 'core', |
|
| 206 | + // 'exception' => $exception, |
|
| 207 | + // ]); |
|
| 208 | + // } |
|
| 209 | + // } |
|
| 210 | + |
|
| 211 | + $tooLongOutputColumns = []; |
|
| 212 | + foreach ($this->getOutputColumns() as $column) { |
|
| 213 | + if (strlen($column) > 30) { |
|
| 214 | + $tooLongOutputColumns[] = $column; |
|
| 215 | + } |
|
| 216 | + } |
|
| 217 | + |
|
| 218 | + if (!empty($tooLongOutputColumns)) { |
|
| 219 | + $exception = new QueryException('More than 30 characters for an output column name are not allowed on Oracle.'); |
|
| 220 | + $this->logger->error($exception->getMessage(), [ |
|
| 221 | + 'query' => $this->getSQL(), |
|
| 222 | + 'columns' => $tooLongOutputColumns, |
|
| 223 | + 'app' => 'core', |
|
| 224 | + 'exception' => $exception, |
|
| 225 | + ]); |
|
| 226 | + } |
|
| 227 | + |
|
| 228 | + $numberOfParameters = 0; |
|
| 229 | + $hasTooLargeArrayParameter = false; |
|
| 230 | + foreach ($this->getParameters() as $parameter) { |
|
| 231 | + if (is_array($parameter)) { |
|
| 232 | + $count = count($parameter); |
|
| 233 | + $numberOfParameters += $count; |
|
| 234 | + $hasTooLargeArrayParameter = $hasTooLargeArrayParameter || ($count > 1000); |
|
| 235 | + } |
|
| 236 | + } |
|
| 237 | + |
|
| 238 | + if ($hasTooLargeArrayParameter) { |
|
| 239 | + $exception = new QueryException('More than 1000 expressions in a list are not allowed on Oracle.'); |
|
| 240 | + $this->logger->error($exception->getMessage(), [ |
|
| 241 | + 'query' => $this->getSQL(), |
|
| 242 | + 'app' => 'core', |
|
| 243 | + 'exception' => $exception, |
|
| 244 | + ]); |
|
| 245 | + } |
|
| 246 | + |
|
| 247 | + if ($numberOfParameters > 65535) { |
|
| 248 | + $exception = new QueryException('The number of parameters must not exceed 65535. Restriction by PostgreSQL.'); |
|
| 249 | + $this->logger->error($exception->getMessage(), [ |
|
| 250 | + 'query' => $this->getSQL(), |
|
| 251 | + 'app' => 'core', |
|
| 252 | + 'exception' => $exception, |
|
| 253 | + ]); |
|
| 254 | + } |
|
| 255 | + } |
|
| 256 | + |
|
| 257 | + /** |
|
| 258 | + * Executes this query using the bound parameters and their types. |
|
| 259 | + * |
|
| 260 | + * Uses {@see Connection::executeQuery} for select statements and {@see Connection::executeUpdate} |
|
| 261 | + * for insert, update and delete statements. |
|
| 262 | + * |
|
| 263 | + * @return IResult|int |
|
| 264 | + */ |
|
| 265 | + public function execute(?IDBConnection $connection = null) { |
|
| 266 | + try { |
|
| 267 | + if ($this->getType() === \Doctrine\DBAL\Query\QueryBuilder::SELECT) { |
|
| 268 | + return $this->executeQuery($connection); |
|
| 269 | + } else { |
|
| 270 | + return $this->executeStatement($connection); |
|
| 271 | + } |
|
| 272 | + } catch (DBALException $e) { |
|
| 273 | + // `IQueryBuilder->execute` never wrapped the exception, but `executeQuery` and `executeStatement` do |
|
| 274 | + /** @var \Doctrine\DBAL\Exception $previous */ |
|
| 275 | + $previous = $e->getPrevious(); |
|
| 276 | + |
|
| 277 | + throw $previous; |
|
| 278 | + } |
|
| 279 | + } |
|
| 280 | + |
|
| 281 | + public function executeQuery(?IDBConnection $connection = null): IResult { |
|
| 282 | + if ($this->getType() !== \Doctrine\DBAL\Query\QueryBuilder::SELECT) { |
|
| 283 | + throw new \RuntimeException('Invalid query type, expected SELECT query'); |
|
| 284 | + } |
|
| 285 | + |
|
| 286 | + $this->prepareForExecute(); |
|
| 287 | + if (!$connection) { |
|
| 288 | + $connection = $this->connection; |
|
| 289 | + } |
|
| 290 | + |
|
| 291 | + return $connection->executeQuery( |
|
| 292 | + $this->getSQL(), |
|
| 293 | + $this->getParameters(), |
|
| 294 | + $this->getParameterTypes(), |
|
| 295 | + ); |
|
| 296 | + } |
|
| 297 | + |
|
| 298 | + public function executeStatement(?IDBConnection $connection = null): int { |
|
| 299 | + if ($this->getType() === \Doctrine\DBAL\Query\QueryBuilder::SELECT) { |
|
| 300 | + throw new \RuntimeException('Invalid query type, expected INSERT, DELETE or UPDATE statement'); |
|
| 301 | + } |
|
| 302 | + |
|
| 303 | + $this->prepareForExecute(); |
|
| 304 | + if (!$connection) { |
|
| 305 | + $connection = $this->connection; |
|
| 306 | + } |
|
| 307 | + |
|
| 308 | + return $connection->executeStatement( |
|
| 309 | + $this->getSQL(), |
|
| 310 | + $this->getParameters(), |
|
| 311 | + $this->getParameterTypes(), |
|
| 312 | + ); |
|
| 313 | + } |
|
| 314 | + |
|
| 315 | + |
|
| 316 | + /** |
|
| 317 | + * Gets the complete SQL string formed by the current specifications of this QueryBuilder. |
|
| 318 | + * |
|
| 319 | + * <code> |
|
| 320 | + * $qb = $conn->getQueryBuilder() |
|
| 321 | + * ->select('u') |
|
| 322 | + * ->from('User', 'u') |
|
| 323 | + * echo $qb->getSQL(); // SELECT u FROM User u |
|
| 324 | + * </code> |
|
| 325 | + * |
|
| 326 | + * @return string The SQL query string. |
|
| 327 | + */ |
|
| 328 | + public function getSQL() { |
|
| 329 | + return $this->queryBuilder->getSQL(); |
|
| 330 | + } |
|
| 331 | + |
|
| 332 | + /** |
|
| 333 | + * Sets a query parameter for the query being constructed. |
|
| 334 | + * |
|
| 335 | + * <code> |
|
| 336 | + * $qb = $conn->getQueryBuilder() |
|
| 337 | + * ->select('u') |
|
| 338 | + * ->from('users', 'u') |
|
| 339 | + * ->where('u.id = :user_id') |
|
| 340 | + * ->setParameter(':user_id', 1); |
|
| 341 | + * </code> |
|
| 342 | + * |
|
| 343 | + * @param string|integer $key The parameter position or name. |
|
| 344 | + * @param mixed $value The parameter value. |
|
| 345 | + * @param string|null|int $type One of the IQueryBuilder::PARAM_* constants. |
|
| 346 | + * |
|
| 347 | + * @return $this This QueryBuilder instance. |
|
| 348 | + */ |
|
| 349 | + public function setParameter($key, $value, $type = null) { |
|
| 350 | + $this->queryBuilder->setParameter($key, $value, $type); |
|
| 351 | + |
|
| 352 | + return $this; |
|
| 353 | + } |
|
| 354 | + |
|
| 355 | + /** |
|
| 356 | + * Sets a collection of query parameters for the query being constructed. |
|
| 357 | + * |
|
| 358 | + * <code> |
|
| 359 | + * $qb = $conn->getQueryBuilder() |
|
| 360 | + * ->select('u') |
|
| 361 | + * ->from('users', 'u') |
|
| 362 | + * ->where('u.id = :user_id1 OR u.id = :user_id2') |
|
| 363 | + * ->setParameters(array( |
|
| 364 | + * ':user_id1' => 1, |
|
| 365 | + * ':user_id2' => 2 |
|
| 366 | + * )); |
|
| 367 | + * </code> |
|
| 368 | + * |
|
| 369 | + * @param array $params The query parameters to set. |
|
| 370 | + * @param array $types The query parameters types to set. |
|
| 371 | + * |
|
| 372 | + * @return $this This QueryBuilder instance. |
|
| 373 | + */ |
|
| 374 | + public function setParameters(array $params, array $types = []) { |
|
| 375 | + $this->queryBuilder->setParameters($params, $types); |
|
| 376 | + |
|
| 377 | + return $this; |
|
| 378 | + } |
|
| 379 | + |
|
| 380 | + /** |
|
| 381 | + * Gets all defined query parameters for the query being constructed indexed by parameter index or name. |
|
| 382 | + * |
|
| 383 | + * @return array The currently defined query parameters indexed by parameter index or name. |
|
| 384 | + */ |
|
| 385 | + public function getParameters() { |
|
| 386 | + return $this->queryBuilder->getParameters(); |
|
| 387 | + } |
|
| 388 | + |
|
| 389 | + /** |
|
| 390 | + * Gets a (previously set) query parameter of the query being constructed. |
|
| 391 | + * |
|
| 392 | + * @param mixed $key The key (index or name) of the bound parameter. |
|
| 393 | + * |
|
| 394 | + * @return mixed The value of the bound parameter. |
|
| 395 | + */ |
|
| 396 | + public function getParameter($key) { |
|
| 397 | + return $this->queryBuilder->getParameter($key); |
|
| 398 | + } |
|
| 399 | + |
|
| 400 | + /** |
|
| 401 | + * Gets all defined query parameter types for the query being constructed indexed by parameter index or name. |
|
| 402 | + * |
|
| 403 | + * @return array The currently defined query parameter types indexed by parameter index or name. |
|
| 404 | + */ |
|
| 405 | + public function getParameterTypes() { |
|
| 406 | + return $this->queryBuilder->getParameterTypes(); |
|
| 407 | + } |
|
| 408 | + |
|
| 409 | + /** |
|
| 410 | + * Gets a (previously set) query parameter type of the query being constructed. |
|
| 411 | + * |
|
| 412 | + * @param mixed $key The key (index or name) of the bound parameter type. |
|
| 413 | + * |
|
| 414 | + * @return mixed The value of the bound parameter type. |
|
| 415 | + */ |
|
| 416 | + public function getParameterType($key) { |
|
| 417 | + return $this->queryBuilder->getParameterType($key); |
|
| 418 | + } |
|
| 419 | + |
|
| 420 | + /** |
|
| 421 | + * Sets the position of the first result to retrieve (the "offset"). |
|
| 422 | + * |
|
| 423 | + * @param int $firstResult The first result to return. |
|
| 424 | + * |
|
| 425 | + * @return $this This QueryBuilder instance. |
|
| 426 | + */ |
|
| 427 | + public function setFirstResult($firstResult) { |
|
| 428 | + $this->queryBuilder->setFirstResult((int)$firstResult); |
|
| 429 | + |
|
| 430 | + return $this; |
|
| 431 | + } |
|
| 432 | + |
|
| 433 | + /** |
|
| 434 | + * Gets the position of the first result the query object was set to retrieve (the "offset"). |
|
| 435 | + * Returns 0 if {@link setFirstResult} was not applied to this QueryBuilder. |
|
| 436 | + * |
|
| 437 | + * @return int The position of the first result. |
|
| 438 | + */ |
|
| 439 | + public function getFirstResult() { |
|
| 440 | + return $this->queryBuilder->getFirstResult(); |
|
| 441 | + } |
|
| 442 | + |
|
| 443 | + /** |
|
| 444 | + * Sets the maximum number of results to retrieve (the "limit"). |
|
| 445 | + * |
|
| 446 | + * NOTE: Setting max results to "0" will cause mixed behaviour. While most |
|
| 447 | + * of the databases will just return an empty result set, Oracle will return |
|
| 448 | + * all entries. |
|
| 449 | + * |
|
| 450 | + * @param int|null $maxResults The maximum number of results to retrieve. |
|
| 451 | + * |
|
| 452 | + * @return $this This QueryBuilder instance. |
|
| 453 | + */ |
|
| 454 | + public function setMaxResults($maxResults) { |
|
| 455 | + if ($maxResults === null) { |
|
| 456 | + $this->queryBuilder->setMaxResults($maxResults); |
|
| 457 | + } else { |
|
| 458 | + $this->queryBuilder->setMaxResults((int)$maxResults); |
|
| 459 | + } |
|
| 460 | + |
|
| 461 | + return $this; |
|
| 462 | + } |
|
| 463 | + |
|
| 464 | + /** |
|
| 465 | + * Gets the maximum number of results the query object was set to retrieve (the "limit"). |
|
| 466 | + * Returns NULL if {@link setMaxResults} was not applied to this query builder. |
|
| 467 | + * |
|
| 468 | + * @return int|null The maximum number of results. |
|
| 469 | + */ |
|
| 470 | + public function getMaxResults() { |
|
| 471 | + return $this->queryBuilder->getMaxResults(); |
|
| 472 | + } |
|
| 473 | + |
|
| 474 | + /** |
|
| 475 | + * Specifies an item that is to be returned in the query result. |
|
| 476 | + * Replaces any previously specified selections, if any. |
|
| 477 | + * |
|
| 478 | + * <code> |
|
| 479 | + * $qb = $conn->getQueryBuilder() |
|
| 480 | + * ->select('u.id', 'p.id') |
|
| 481 | + * ->from('users', 'u') |
|
| 482 | + * ->leftJoin('u', 'phonenumbers', 'p', 'u.id = p.user_id'); |
|
| 483 | + * </code> |
|
| 484 | + * |
|
| 485 | + * @param mixed ...$selects The selection expressions. |
|
| 486 | + * |
|
| 487 | + * '@return $this This QueryBuilder instance. |
|
| 488 | + */ |
|
| 489 | + public function select(...$selects) { |
|
| 490 | + if (count($selects) === 1 && is_array($selects[0])) { |
|
| 491 | + $selects = $selects[0]; |
|
| 492 | + } |
|
| 493 | + $this->addOutputColumns($selects); |
|
| 494 | + |
|
| 495 | + $this->queryBuilder->select( |
|
| 496 | + $this->helper->quoteColumnNames($selects) |
|
| 497 | + ); |
|
| 498 | + |
|
| 499 | + return $this; |
|
| 500 | + } |
|
| 501 | + |
|
| 502 | + /** |
|
| 503 | + * Specifies an item that is to be returned with a different name in the query result. |
|
| 504 | + * |
|
| 505 | + * <code> |
|
| 506 | + * $qb = $conn->getQueryBuilder() |
|
| 507 | + * ->selectAlias('u.id', 'user_id') |
|
| 508 | + * ->from('users', 'u') |
|
| 509 | + * ->leftJoin('u', 'phonenumbers', 'p', 'u.id = p.user_id'); |
|
| 510 | + * </code> |
|
| 511 | + * |
|
| 512 | + * @param mixed $select The selection expressions. |
|
| 513 | + * @param string $alias The column alias used in the constructed query. |
|
| 514 | + * |
|
| 515 | + * @return $this This QueryBuilder instance. |
|
| 516 | + */ |
|
| 517 | + public function selectAlias($select, $alias) { |
|
| 518 | + $this->queryBuilder->addSelect( |
|
| 519 | + $this->helper->quoteColumnName($select) . ' AS ' . $this->helper->quoteColumnName($alias) |
|
| 520 | + ); |
|
| 521 | + $this->addOutputColumns([$alias]); |
|
| 522 | + |
|
| 523 | + return $this; |
|
| 524 | + } |
|
| 525 | + |
|
| 526 | + /** |
|
| 527 | + * Specifies an item that is to be returned uniquely in the query result. |
|
| 528 | + * |
|
| 529 | + * <code> |
|
| 530 | + * $qb = $conn->getQueryBuilder() |
|
| 531 | + * ->selectDistinct('type') |
|
| 532 | + * ->from('users'); |
|
| 533 | + * </code> |
|
| 534 | + * |
|
| 535 | + * @param mixed $select The selection expressions. |
|
| 536 | + * |
|
| 537 | + * @return $this This QueryBuilder instance. |
|
| 538 | + */ |
|
| 539 | + public function selectDistinct($select) { |
|
| 540 | + if (!is_array($select)) { |
|
| 541 | + $select = [$select]; |
|
| 542 | + } |
|
| 543 | + $this->addOutputColumns($select); |
|
| 544 | + |
|
| 545 | + $quotedSelect = $this->helper->quoteColumnNames($select); |
|
| 546 | + |
|
| 547 | + $this->queryBuilder->addSelect( |
|
| 548 | + 'DISTINCT ' . implode(', ', $quotedSelect) |
|
| 549 | + ); |
|
| 550 | + |
|
| 551 | + return $this; |
|
| 552 | + } |
|
| 553 | + |
|
| 554 | + /** |
|
| 555 | + * Adds an item that is to be returned in the query result. |
|
| 556 | + * |
|
| 557 | + * <code> |
|
| 558 | + * $qb = $conn->getQueryBuilder() |
|
| 559 | + * ->select('u.id') |
|
| 560 | + * ->addSelect('p.id') |
|
| 561 | + * ->from('users', 'u') |
|
| 562 | + * ->leftJoin('u', 'phonenumbers', 'u.id = p.user_id'); |
|
| 563 | + * </code> |
|
| 564 | + * |
|
| 565 | + * @param mixed ...$selects The selection expression. |
|
| 566 | + * |
|
| 567 | + * @return $this This QueryBuilder instance. |
|
| 568 | + */ |
|
| 569 | + public function addSelect(...$selects) { |
|
| 570 | + if (count($selects) === 1 && is_array($selects[0])) { |
|
| 571 | + $selects = $selects[0]; |
|
| 572 | + } |
|
| 573 | + $this->addOutputColumns($selects); |
|
| 574 | + |
|
| 575 | + $this->queryBuilder->addSelect( |
|
| 576 | + $this->helper->quoteColumnNames($selects) |
|
| 577 | + ); |
|
| 578 | + |
|
| 579 | + return $this; |
|
| 580 | + } |
|
| 581 | + |
|
| 582 | + private function addOutputColumns(array $columns): void { |
|
| 583 | + foreach ($columns as $column) { |
|
| 584 | + if (is_array($column)) { |
|
| 585 | + $this->addOutputColumns($column); |
|
| 586 | + } elseif (is_string($column) && !str_contains($column, '*')) { |
|
| 587 | + if (str_contains(strtolower($column), ' as ')) { |
|
| 588 | + [, $column] = preg_split('/ as /i', $column); |
|
| 589 | + } |
|
| 590 | + if (str_contains($column, '.')) { |
|
| 591 | + [, $column] = explode('.', $column); |
|
| 592 | + } |
|
| 593 | + $this->selectedColumns[] = $column; |
|
| 594 | + } |
|
| 595 | + } |
|
| 596 | + } |
|
| 597 | + |
|
| 598 | + public function getOutputColumns(): array { |
|
| 599 | + return array_unique($this->selectedColumns); |
|
| 600 | + } |
|
| 601 | + |
|
| 602 | + /** |
|
| 603 | + * Turns the query being built into a bulk delete query that ranges over |
|
| 604 | + * a certain table. |
|
| 605 | + * |
|
| 606 | + * <code> |
|
| 607 | + * $qb = $conn->getQueryBuilder() |
|
| 608 | + * ->delete('users', 'u') |
|
| 609 | + * ->where('u.id = :user_id'); |
|
| 610 | + * ->setParameter(':user_id', 1); |
|
| 611 | + * </code> |
|
| 612 | + * |
|
| 613 | + * @param string $delete The table whose rows are subject to the deletion. |
|
| 614 | + * @param string $alias The table alias used in the constructed query. |
|
| 615 | + * |
|
| 616 | + * @return $this This QueryBuilder instance. |
|
| 617 | + * @since 30.0.0 Alias is deprecated and will no longer be used with the next Doctrine/DBAL update |
|
| 618 | + */ |
|
| 619 | + public function delete($delete = null, $alias = null) { |
|
| 620 | + if ($alias !== null) { |
|
| 621 | + $this->logger->debug('DELETE queries with alias are no longer supported and the provided alias is ignored', ['exception' => new \InvalidArgumentException('Table alias provided for DELETE query')]); |
|
| 622 | + } |
|
| 623 | + |
|
| 624 | + $this->queryBuilder->delete( |
|
| 625 | + $this->getTableName($delete), |
|
| 626 | + $alias |
|
| 627 | + ); |
|
| 628 | + |
|
| 629 | + return $this; |
|
| 630 | + } |
|
| 631 | + |
|
| 632 | + /** |
|
| 633 | + * Turns the query being built into a bulk update query that ranges over |
|
| 634 | + * a certain table |
|
| 635 | + * |
|
| 636 | + * <code> |
|
| 637 | + * $qb = $conn->getQueryBuilder() |
|
| 638 | + * ->update('users', 'u') |
|
| 639 | + * ->set('u.password', md5('password')) |
|
| 640 | + * ->where('u.id = ?'); |
|
| 641 | + * </code> |
|
| 642 | + * |
|
| 643 | + * @param string $update The table whose rows are subject to the update. |
|
| 644 | + * @param string $alias The table alias used in the constructed query. |
|
| 645 | + * |
|
| 646 | + * @return $this This QueryBuilder instance. |
|
| 647 | + * @since 30.0.0 Alias is deprecated and will no longer be used with the next Doctrine/DBAL update |
|
| 648 | + */ |
|
| 649 | + public function update($update = null, $alias = null) { |
|
| 650 | + if ($alias !== null) { |
|
| 651 | + $this->logger->debug('UPDATE queries with alias are no longer supported and the provided alias is ignored', ['exception' => new \InvalidArgumentException('Table alias provided for UPDATE query')]); |
|
| 652 | + } |
|
| 653 | + |
|
| 654 | + $this->queryBuilder->update( |
|
| 655 | + $this->getTableName($update), |
|
| 656 | + $alias |
|
| 657 | + ); |
|
| 658 | + |
|
| 659 | + return $this; |
|
| 660 | + } |
|
| 661 | + |
|
| 662 | + /** |
|
| 663 | + * Turns the query being built into an insert query that inserts into |
|
| 664 | + * a certain table |
|
| 665 | + * |
|
| 666 | + * <code> |
|
| 667 | + * $qb = $conn->getQueryBuilder() |
|
| 668 | + * ->insert('users') |
|
| 669 | + * ->values( |
|
| 670 | + * array( |
|
| 671 | + * 'name' => '?', |
|
| 672 | + * 'password' => '?' |
|
| 673 | + * ) |
|
| 674 | + * ); |
|
| 675 | + * </code> |
|
| 676 | + * |
|
| 677 | + * @param string $insert The table into which the rows should be inserted. |
|
| 678 | + * |
|
| 679 | + * @return $this This QueryBuilder instance. |
|
| 680 | + */ |
|
| 681 | + public function insert($insert = null) { |
|
| 682 | + $this->queryBuilder->insert( |
|
| 683 | + $this->getTableName($insert) |
|
| 684 | + ); |
|
| 685 | + |
|
| 686 | + $this->lastInsertedTable = $insert; |
|
| 687 | + |
|
| 688 | + return $this; |
|
| 689 | + } |
|
| 690 | + |
|
| 691 | + /** |
|
| 692 | + * Creates and adds a query root corresponding to the table identified by the |
|
| 693 | + * given alias, forming a cartesian product with any existing query roots. |
|
| 694 | + * |
|
| 695 | + * <code> |
|
| 696 | + * $qb = $conn->getQueryBuilder() |
|
| 697 | + * ->select('u.id') |
|
| 698 | + * ->from('users', 'u') |
|
| 699 | + * </code> |
|
| 700 | + * |
|
| 701 | + * @param string|IQueryFunction $from The table. |
|
| 702 | + * @param string|null $alias The alias of the table. |
|
| 703 | + * |
|
| 704 | + * @return $this This QueryBuilder instance. |
|
| 705 | + */ |
|
| 706 | + public function from($from, $alias = null) { |
|
| 707 | + $this->queryBuilder->from( |
|
| 708 | + $this->getTableName($from), |
|
| 709 | + $this->quoteAlias($alias) |
|
| 710 | + ); |
|
| 711 | + |
|
| 712 | + return $this; |
|
| 713 | + } |
|
| 714 | + |
|
| 715 | + /** |
|
| 716 | + * Creates and adds a join to the query. |
|
| 717 | + * |
|
| 718 | + * <code> |
|
| 719 | + * $qb = $conn->getQueryBuilder() |
|
| 720 | + * ->select('u.name') |
|
| 721 | + * ->from('users', 'u') |
|
| 722 | + * ->join('u', 'phonenumbers', 'p', 'p.is_primary = 1'); |
|
| 723 | + * </code> |
|
| 724 | + * |
|
| 725 | + * @param string $fromAlias The alias that points to a from clause. |
|
| 726 | + * @param string $join The table name to join. |
|
| 727 | + * @param string $alias The alias of the join table. |
|
| 728 | + * @param string|ICompositeExpression|null $condition The condition for the join. |
|
| 729 | + * |
|
| 730 | + * @return $this This QueryBuilder instance. |
|
| 731 | + */ |
|
| 732 | + public function join($fromAlias, $join, $alias, $condition = null) { |
|
| 733 | + $this->queryBuilder->join( |
|
| 734 | + $this->quoteAlias($fromAlias), |
|
| 735 | + $this->getTableName($join), |
|
| 736 | + $this->quoteAlias($alias), |
|
| 737 | + $condition |
|
| 738 | + ); |
|
| 739 | + |
|
| 740 | + return $this; |
|
| 741 | + } |
|
| 742 | + |
|
| 743 | + /** |
|
| 744 | + * Creates and adds a join to the query. |
|
| 745 | + * |
|
| 746 | + * <code> |
|
| 747 | + * $qb = $conn->getQueryBuilder() |
|
| 748 | + * ->select('u.name') |
|
| 749 | + * ->from('users', 'u') |
|
| 750 | + * ->innerJoin('u', 'phonenumbers', 'p', 'p.is_primary = 1'); |
|
| 751 | + * </code> |
|
| 752 | + * |
|
| 753 | + * @param string $fromAlias The alias that points to a from clause. |
|
| 754 | + * @param string $join The table name to join. |
|
| 755 | + * @param string $alias The alias of the join table. |
|
| 756 | + * @param string|ICompositeExpression|null $condition The condition for the join. |
|
| 757 | + * |
|
| 758 | + * @return $this This QueryBuilder instance. |
|
| 759 | + */ |
|
| 760 | + public function innerJoin($fromAlias, $join, $alias, $condition = null) { |
|
| 761 | + $this->queryBuilder->innerJoin( |
|
| 762 | + $this->quoteAlias($fromAlias), |
|
| 763 | + $this->getTableName($join), |
|
| 764 | + $this->quoteAlias($alias), |
|
| 765 | + $condition |
|
| 766 | + ); |
|
| 767 | + |
|
| 768 | + return $this; |
|
| 769 | + } |
|
| 770 | + |
|
| 771 | + /** |
|
| 772 | + * Creates and adds a left join to the query. |
|
| 773 | + * |
|
| 774 | + * <code> |
|
| 775 | + * $qb = $conn->getQueryBuilder() |
|
| 776 | + * ->select('u.name') |
|
| 777 | + * ->from('users', 'u') |
|
| 778 | + * ->leftJoin('u', 'phonenumbers', 'p', 'p.is_primary = 1'); |
|
| 779 | + * </code> |
|
| 780 | + * |
|
| 781 | + * @param string $fromAlias The alias that points to a from clause. |
|
| 782 | + * @param string|IQueryFunction $join The table name or sub-query to join. |
|
| 783 | + * @param string $alias The alias of the join table. |
|
| 784 | + * @param string|ICompositeExpression|null $condition The condition for the join. |
|
| 785 | + * |
|
| 786 | + * @return $this This QueryBuilder instance. |
|
| 787 | + */ |
|
| 788 | + public function leftJoin($fromAlias, $join, $alias, $condition = null) { |
|
| 789 | + $this->queryBuilder->leftJoin( |
|
| 790 | + $this->quoteAlias($fromAlias), |
|
| 791 | + $this->getTableName($join), |
|
| 792 | + $this->quoteAlias($alias), |
|
| 793 | + $condition |
|
| 794 | + ); |
|
| 795 | + |
|
| 796 | + return $this; |
|
| 797 | + } |
|
| 798 | + |
|
| 799 | + /** |
|
| 800 | + * Creates and adds a right join to the query. |
|
| 801 | + * |
|
| 802 | + * <code> |
|
| 803 | + * $qb = $conn->getQueryBuilder() |
|
| 804 | + * ->select('u.name') |
|
| 805 | + * ->from('users', 'u') |
|
| 806 | + * ->rightJoin('u', 'phonenumbers', 'p', 'p.is_primary = 1'); |
|
| 807 | + * </code> |
|
| 808 | + * |
|
| 809 | + * @param string $fromAlias The alias that points to a from clause. |
|
| 810 | + * @param string $join The table name to join. |
|
| 811 | + * @param string $alias The alias of the join table. |
|
| 812 | + * @param string|ICompositeExpression|null $condition The condition for the join. |
|
| 813 | + * |
|
| 814 | + * @return $this This QueryBuilder instance. |
|
| 815 | + */ |
|
| 816 | + public function rightJoin($fromAlias, $join, $alias, $condition = null) { |
|
| 817 | + $this->queryBuilder->rightJoin( |
|
| 818 | + $this->quoteAlias($fromAlias), |
|
| 819 | + $this->getTableName($join), |
|
| 820 | + $this->quoteAlias($alias), |
|
| 821 | + $condition |
|
| 822 | + ); |
|
| 823 | + |
|
| 824 | + return $this; |
|
| 825 | + } |
|
| 826 | + |
|
| 827 | + /** |
|
| 828 | + * Sets a new value for a column in a bulk update query. |
|
| 829 | + * |
|
| 830 | + * <code> |
|
| 831 | + * $qb = $conn->getQueryBuilder() |
|
| 832 | + * ->update('users', 'u') |
|
| 833 | + * ->set('u.password', md5('password')) |
|
| 834 | + * ->where('u.id = ?'); |
|
| 835 | + * </code> |
|
| 836 | + * |
|
| 837 | + * @param string $key The column to set. |
|
| 838 | + * @param ILiteral|IParameter|IQueryFunction|string $value The value, expression, placeholder, etc. |
|
| 839 | + * |
|
| 840 | + * @return $this This QueryBuilder instance. |
|
| 841 | + */ |
|
| 842 | + public function set($key, $value) { |
|
| 843 | + $this->queryBuilder->set( |
|
| 844 | + $this->helper->quoteColumnName($key), |
|
| 845 | + $this->helper->quoteColumnName($value) |
|
| 846 | + ); |
|
| 847 | + |
|
| 848 | + return $this; |
|
| 849 | + } |
|
| 850 | + |
|
| 851 | + /** |
|
| 852 | + * Specifies one or more restrictions to the query result. |
|
| 853 | + * Replaces any previously specified restrictions, if any. |
|
| 854 | + * |
|
| 855 | + * <code> |
|
| 856 | + * $qb = $conn->getQueryBuilder() |
|
| 857 | + * ->select('u.name') |
|
| 858 | + * ->from('users', 'u') |
|
| 859 | + * ->where('u.id = ?'); |
|
| 860 | + * |
|
| 861 | + * // You can optionally programmatically build and/or expressions |
|
| 862 | + * $qb = $conn->getQueryBuilder(); |
|
| 863 | + * |
|
| 864 | + * $or = $qb->expr()->orx( |
|
| 865 | + * $qb->expr()->eq('u.id', 1), |
|
| 866 | + * $qb->expr()->eq('u.id', 2), |
|
| 867 | + * ); |
|
| 868 | + * |
|
| 869 | + * $qb->update('users', 'u') |
|
| 870 | + * ->set('u.password', md5('password')) |
|
| 871 | + * ->where($or); |
|
| 872 | + * </code> |
|
| 873 | + * |
|
| 874 | + * @param mixed ...$predicates The restriction predicates. |
|
| 875 | + * |
|
| 876 | + * @return $this This QueryBuilder instance. |
|
| 877 | + */ |
|
| 878 | + public function where(...$predicates) { |
|
| 879 | + if ($this->nonEmptyWhere && $this->systemConfig->getValue('debug', false)) { |
|
| 880 | + // Only logging a warning, not throwing for now. |
|
| 881 | + $e = new QueryException('Using where() on non-empty WHERE part, please verify it is intentional to not call andWhere() or orWhere() instead. Otherwise consider creating a new query builder object or call resetQueryPart(\'where\') first.'); |
|
| 882 | + $this->logger->warning($e->getMessage(), ['exception' => $e]); |
|
| 883 | + } |
|
| 884 | + |
|
| 885 | + $this->nonEmptyWhere = true; |
|
| 886 | + |
|
| 887 | + call_user_func_array( |
|
| 888 | + [$this->queryBuilder, 'where'], |
|
| 889 | + $predicates |
|
| 890 | + ); |
|
| 891 | + |
|
| 892 | + return $this; |
|
| 893 | + } |
|
| 894 | + |
|
| 895 | + /** |
|
| 896 | + * Adds one or more restrictions to the query results, forming a logical |
|
| 897 | + * conjunction with any previously specified restrictions. |
|
| 898 | + * |
|
| 899 | + * <code> |
|
| 900 | + * $qb = $conn->getQueryBuilder() |
|
| 901 | + * ->select('u') |
|
| 902 | + * ->from('users', 'u') |
|
| 903 | + * ->where('u.username LIKE ?') |
|
| 904 | + * ->andWhere('u.is_active = 1'); |
|
| 905 | + * </code> |
|
| 906 | + * |
|
| 907 | + * @param mixed ...$where The query restrictions. |
|
| 908 | + * |
|
| 909 | + * @return $this This QueryBuilder instance. |
|
| 910 | + * |
|
| 911 | + * @see where() |
|
| 912 | + */ |
|
| 913 | + public function andWhere(...$where) { |
|
| 914 | + $this->nonEmptyWhere = true; |
|
| 915 | + call_user_func_array( |
|
| 916 | + [$this->queryBuilder, 'andWhere'], |
|
| 917 | + $where |
|
| 918 | + ); |
|
| 919 | + |
|
| 920 | + return $this; |
|
| 921 | + } |
|
| 922 | + |
|
| 923 | + /** |
|
| 924 | + * Adds one or more restrictions to the query results, forming a logical |
|
| 925 | + * disjunction with any previously specified restrictions. |
|
| 926 | + * |
|
| 927 | + * <code> |
|
| 928 | + * $qb = $conn->getQueryBuilder() |
|
| 929 | + * ->select('u.name') |
|
| 930 | + * ->from('users', 'u') |
|
| 931 | + * ->where('u.id = 1') |
|
| 932 | + * ->orWhere('u.id = 2'); |
|
| 933 | + * </code> |
|
| 934 | + * |
|
| 935 | + * @param mixed ...$where The WHERE statement. |
|
| 936 | + * |
|
| 937 | + * @return $this This QueryBuilder instance. |
|
| 938 | + * |
|
| 939 | + * @see where() |
|
| 940 | + */ |
|
| 941 | + public function orWhere(...$where) { |
|
| 942 | + $this->nonEmptyWhere = true; |
|
| 943 | + call_user_func_array( |
|
| 944 | + [$this->queryBuilder, 'orWhere'], |
|
| 945 | + $where |
|
| 946 | + ); |
|
| 947 | + |
|
| 948 | + return $this; |
|
| 949 | + } |
|
| 950 | + |
|
| 951 | + /** |
|
| 952 | + * Specifies a grouping over the results of the query. |
|
| 953 | + * Replaces any previously specified groupings, if any. |
|
| 954 | + * |
|
| 955 | + * <code> |
|
| 956 | + * $qb = $conn->getQueryBuilder() |
|
| 957 | + * ->select('u.name') |
|
| 958 | + * ->from('users', 'u') |
|
| 959 | + * ->groupBy('u.id'); |
|
| 960 | + * </code> |
|
| 961 | + * |
|
| 962 | + * @param mixed ...$groupBys The grouping expression. |
|
| 963 | + * |
|
| 964 | + * @return $this This QueryBuilder instance. |
|
| 965 | + */ |
|
| 966 | + public function groupBy(...$groupBys) { |
|
| 967 | + if (count($groupBys) === 1 && is_array($groupBys[0])) { |
|
| 968 | + $groupBys = $groupBys[0]; |
|
| 969 | + } |
|
| 970 | + |
|
| 971 | + call_user_func_array( |
|
| 972 | + [$this->queryBuilder, 'groupBy'], |
|
| 973 | + $this->helper->quoteColumnNames($groupBys) |
|
| 974 | + ); |
|
| 975 | + |
|
| 976 | + return $this; |
|
| 977 | + } |
|
| 978 | + |
|
| 979 | + /** |
|
| 980 | + * Adds a grouping expression to the query. |
|
| 981 | + * |
|
| 982 | + * <code> |
|
| 983 | + * $qb = $conn->getQueryBuilder() |
|
| 984 | + * ->select('u.name') |
|
| 985 | + * ->from('users', 'u') |
|
| 986 | + * ->groupBy('u.lastLogin'); |
|
| 987 | + * ->addGroupBy('u.createdAt') |
|
| 988 | + * </code> |
|
| 989 | + * |
|
| 990 | + * @param mixed ...$groupBy The grouping expression. |
|
| 991 | + * |
|
| 992 | + * @return $this This QueryBuilder instance. |
|
| 993 | + */ |
|
| 994 | + public function addGroupBy(...$groupBy) { |
|
| 995 | + call_user_func_array( |
|
| 996 | + [$this->queryBuilder, 'addGroupBy'], |
|
| 997 | + $this->helper->quoteColumnNames($groupBy) |
|
| 998 | + ); |
|
| 999 | + |
|
| 1000 | + return $this; |
|
| 1001 | + } |
|
| 1002 | + |
|
| 1003 | + /** |
|
| 1004 | + * Sets a value for a column in an insert query. |
|
| 1005 | + * |
|
| 1006 | + * <code> |
|
| 1007 | + * $qb = $conn->getQueryBuilder() |
|
| 1008 | + * ->insert('users') |
|
| 1009 | + * ->values( |
|
| 1010 | + * array( |
|
| 1011 | + * 'name' => '?' |
|
| 1012 | + * ) |
|
| 1013 | + * ) |
|
| 1014 | + * ->setValue('password', '?'); |
|
| 1015 | + * </code> |
|
| 1016 | + * |
|
| 1017 | + * @param string $column The column into which the value should be inserted. |
|
| 1018 | + * @param IParameter|string $value The value that should be inserted into the column. |
|
| 1019 | + * |
|
| 1020 | + * @return $this This QueryBuilder instance. |
|
| 1021 | + */ |
|
| 1022 | + public function setValue($column, $value) { |
|
| 1023 | + $this->queryBuilder->setValue( |
|
| 1024 | + $this->helper->quoteColumnName($column), |
|
| 1025 | + (string)$value |
|
| 1026 | + ); |
|
| 1027 | + |
|
| 1028 | + return $this; |
|
| 1029 | + } |
|
| 1030 | + |
|
| 1031 | + /** |
|
| 1032 | + * Specifies values for an insert query indexed by column names. |
|
| 1033 | + * Replaces any previous values, if any. |
|
| 1034 | + * |
|
| 1035 | + * <code> |
|
| 1036 | + * $qb = $conn->getQueryBuilder() |
|
| 1037 | + * ->insert('users') |
|
| 1038 | + * ->values( |
|
| 1039 | + * array( |
|
| 1040 | + * 'name' => '?', |
|
| 1041 | + * 'password' => '?' |
|
| 1042 | + * ) |
|
| 1043 | + * ); |
|
| 1044 | + * </code> |
|
| 1045 | + * |
|
| 1046 | + * @param array $values The values to specify for the insert query indexed by column names. |
|
| 1047 | + * |
|
| 1048 | + * @return $this This QueryBuilder instance. |
|
| 1049 | + */ |
|
| 1050 | + public function values(array $values) { |
|
| 1051 | + $quotedValues = []; |
|
| 1052 | + foreach ($values as $key => $value) { |
|
| 1053 | + $quotedValues[$this->helper->quoteColumnName($key)] = $value; |
|
| 1054 | + } |
|
| 1055 | + |
|
| 1056 | + $this->queryBuilder->values($quotedValues); |
|
| 1057 | + |
|
| 1058 | + return $this; |
|
| 1059 | + } |
|
| 1060 | + |
|
| 1061 | + /** |
|
| 1062 | + * Specifies a restriction over the groups of the query. |
|
| 1063 | + * Replaces any previous having restrictions, if any. |
|
| 1064 | + * |
|
| 1065 | + * @param mixed ...$having The restriction over the groups. |
|
| 1066 | + * |
|
| 1067 | + * @return $this This QueryBuilder instance. |
|
| 1068 | + */ |
|
| 1069 | + public function having(...$having) { |
|
| 1070 | + call_user_func_array( |
|
| 1071 | + [$this->queryBuilder, 'having'], |
|
| 1072 | + $having |
|
| 1073 | + ); |
|
| 1074 | + |
|
| 1075 | + return $this; |
|
| 1076 | + } |
|
| 1077 | + |
|
| 1078 | + /** |
|
| 1079 | + * Adds a restriction over the groups of the query, forming a logical |
|
| 1080 | + * conjunction with any existing having restrictions. |
|
| 1081 | + * |
|
| 1082 | + * @param mixed ...$having The restriction to append. |
|
| 1083 | + * |
|
| 1084 | + * @return $this This QueryBuilder instance. |
|
| 1085 | + */ |
|
| 1086 | + public function andHaving(...$having) { |
|
| 1087 | + call_user_func_array( |
|
| 1088 | + [$this->queryBuilder, 'andHaving'], |
|
| 1089 | + $having |
|
| 1090 | + ); |
|
| 1091 | + |
|
| 1092 | + return $this; |
|
| 1093 | + } |
|
| 1094 | + |
|
| 1095 | + /** |
|
| 1096 | + * Adds a restriction over the groups of the query, forming a logical |
|
| 1097 | + * disjunction with any existing having restrictions. |
|
| 1098 | + * |
|
| 1099 | + * @param mixed ...$having The restriction to add. |
|
| 1100 | + * |
|
| 1101 | + * @return $this This QueryBuilder instance. |
|
| 1102 | + */ |
|
| 1103 | + public function orHaving(...$having) { |
|
| 1104 | + call_user_func_array( |
|
| 1105 | + [$this->queryBuilder, 'orHaving'], |
|
| 1106 | + $having |
|
| 1107 | + ); |
|
| 1108 | + |
|
| 1109 | + return $this; |
|
| 1110 | + } |
|
| 1111 | + |
|
| 1112 | + /** |
|
| 1113 | + * Specifies an ordering for the query results. |
|
| 1114 | + * Replaces any previously specified orderings, if any. |
|
| 1115 | + * |
|
| 1116 | + * @param string|IQueryFunction|ILiteral|IParameter $sort The ordering expression. |
|
| 1117 | + * @param string $order The ordering direction. |
|
| 1118 | + * |
|
| 1119 | + * @return $this This QueryBuilder instance. |
|
| 1120 | + */ |
|
| 1121 | + public function orderBy($sort, $order = null) { |
|
| 1122 | + $this->queryBuilder->orderBy( |
|
| 1123 | + $this->helper->quoteColumnName($sort), |
|
| 1124 | + $order |
|
| 1125 | + ); |
|
| 1126 | + |
|
| 1127 | + return $this; |
|
| 1128 | + } |
|
| 1129 | + |
|
| 1130 | + /** |
|
| 1131 | + * Adds an ordering to the query results. |
|
| 1132 | + * |
|
| 1133 | + * @param string|ILiteral|IParameter|IQueryFunction $sort The ordering expression. |
|
| 1134 | + * @param string $order The ordering direction. |
|
| 1135 | + * |
|
| 1136 | + * @return $this This QueryBuilder instance. |
|
| 1137 | + */ |
|
| 1138 | + public function addOrderBy($sort, $order = null) { |
|
| 1139 | + $this->queryBuilder->addOrderBy( |
|
| 1140 | + $this->helper->quoteColumnName($sort), |
|
| 1141 | + $order |
|
| 1142 | + ); |
|
| 1143 | + |
|
| 1144 | + return $this; |
|
| 1145 | + } |
|
| 1146 | + |
|
| 1147 | + /** |
|
| 1148 | + * Gets a query part by its name. |
|
| 1149 | + * |
|
| 1150 | + * @param string $queryPartName |
|
| 1151 | + * |
|
| 1152 | + * @return mixed |
|
| 1153 | + * @deprecated 30.0.0 This function is going to be removed with the next Doctrine/DBAL update |
|
| 1154 | + * and we can not fix this in our wrapper. Please track the details you need, outside the object. |
|
| 1155 | + */ |
|
| 1156 | + public function getQueryPart($queryPartName) { |
|
| 1157 | + $this->logger->debug(IQueryBuilder::class . '::' . __FUNCTION__ . ' is deprecated and will be removed soon.', ['exception' => new \Exception('Deprecated call to ' . __METHOD__)]); |
|
| 1158 | + return $this->queryBuilder->getQueryPart($queryPartName); |
|
| 1159 | + } |
|
| 1160 | + |
|
| 1161 | + /** |
|
| 1162 | + * Gets all query parts. |
|
| 1163 | + * |
|
| 1164 | + * @return array |
|
| 1165 | + * @deprecated 30.0.0 This function is going to be removed with the next Doctrine/DBAL update |
|
| 1166 | + * and we can not fix this in our wrapper. Please track the details you need, outside the object. |
|
| 1167 | + */ |
|
| 1168 | + public function getQueryParts() { |
|
| 1169 | + $this->logger->debug(IQueryBuilder::class . '::' . __FUNCTION__ . ' is deprecated and will be removed soon.', ['exception' => new \Exception('Deprecated call to ' . __METHOD__)]); |
|
| 1170 | + return $this->queryBuilder->getQueryParts(); |
|
| 1171 | + } |
|
| 1172 | + |
|
| 1173 | + /** |
|
| 1174 | + * Resets SQL parts. |
|
| 1175 | + * |
|
| 1176 | + * @param array|null $queryPartNames |
|
| 1177 | + * |
|
| 1178 | + * @return $this This QueryBuilder instance. |
|
| 1179 | + * @deprecated 30.0.0 This function is going to be removed with the next Doctrine/DBAL update |
|
| 1180 | + * and we can not fix this in our wrapper. Please create a new IQueryBuilder instead. |
|
| 1181 | + */ |
|
| 1182 | + public function resetQueryParts($queryPartNames = null) { |
|
| 1183 | + $this->logger->debug(IQueryBuilder::class . '::' . __FUNCTION__ . ' is deprecated and will be removed soon.', ['exception' => new \Exception('Deprecated call to ' . __METHOD__)]); |
|
| 1184 | + $this->queryBuilder->resetQueryParts($queryPartNames); |
|
| 1185 | + |
|
| 1186 | + return $this; |
|
| 1187 | + } |
|
| 1188 | + |
|
| 1189 | + /** |
|
| 1190 | + * Resets a single SQL part. |
|
| 1191 | + * |
|
| 1192 | + * @param string $queryPartName |
|
| 1193 | + * |
|
| 1194 | + * @return $this This QueryBuilder instance. |
|
| 1195 | + * @deprecated 30.0.0 This function is going to be removed with the next Doctrine/DBAL update |
|
| 1196 | + * and we can not fix this in our wrapper. Please create a new IQueryBuilder instead. |
|
| 1197 | + */ |
|
| 1198 | + public function resetQueryPart($queryPartName) { |
|
| 1199 | + $this->logger->debug(IQueryBuilder::class . '::' . __FUNCTION__ . ' is deprecated and will be removed soon.', ['exception' => new \Exception('Deprecated call to ' . __METHOD__)]); |
|
| 1200 | + $this->queryBuilder->resetQueryPart($queryPartName); |
|
| 1201 | + |
|
| 1202 | + return $this; |
|
| 1203 | + } |
|
| 1204 | + |
|
| 1205 | + /** |
|
| 1206 | + * Creates a new named parameter and bind the value $value to it. |
|
| 1207 | + * |
|
| 1208 | + * This method provides a shortcut for PDOStatement::bindValue |
|
| 1209 | + * when using prepared statements. |
|
| 1210 | + * |
|
| 1211 | + * The parameter $value specifies the value that you want to bind. If |
|
| 1212 | + * $placeholder is not provided bindValue() will automatically create a |
|
| 1213 | + * placeholder for you. An automatic placeholder will be of the name |
|
| 1214 | + * ':dcValue1', ':dcValue2' etc. |
|
| 1215 | + * |
|
| 1216 | + * For more information see {@link https://www.php.net/pdostatement-bindparam} |
|
| 1217 | + * |
|
| 1218 | + * Example: |
|
| 1219 | + * <code> |
|
| 1220 | + * $value = 2; |
|
| 1221 | + * $q->eq( 'id', $q->bindValue( $value ) ); |
|
| 1222 | + * $stmt = $q->executeQuery(); // executed with 'id = 2' |
|
| 1223 | + * </code> |
|
| 1224 | + * |
|
| 1225 | + * @license New BSD License |
|
| 1226 | + * @link http://www.zetacomponents.org |
|
| 1227 | + * |
|
| 1228 | + * @param mixed $value |
|
| 1229 | + * @param IQueryBuilder::PARAM_* $type |
|
| 1230 | + * @param string $placeHolder The name to bind with. The string must start with a colon ':'. |
|
| 1231 | + * |
|
| 1232 | + * @return IParameter the placeholder name used. |
|
| 1233 | + */ |
|
| 1234 | + public function createNamedParameter($value, $type = IQueryBuilder::PARAM_STR, $placeHolder = null) { |
|
| 1235 | + return new Parameter($this->queryBuilder->createNamedParameter($value, $type, $placeHolder)); |
|
| 1236 | + } |
|
| 1237 | + |
|
| 1238 | + /** |
|
| 1239 | + * Creates a new positional parameter and bind the given value to it. |
|
| 1240 | + * |
|
| 1241 | + * Attention: If you are using positional parameters with the query builder you have |
|
| 1242 | + * to be very careful to bind all parameters in the order they appear in the SQL |
|
| 1243 | + * statement , otherwise they get bound in the wrong order which can lead to serious |
|
| 1244 | + * bugs in your code. |
|
| 1245 | + * |
|
| 1246 | + * Example: |
|
| 1247 | + * <code> |
|
| 1248 | + * $qb = $conn->getQueryBuilder(); |
|
| 1249 | + * $qb->select('u.*') |
|
| 1250 | + * ->from('users', 'u') |
|
| 1251 | + * ->where('u.username = ' . $qb->createPositionalParameter('Foo', IQueryBuilder::PARAM_STR)) |
|
| 1252 | + * ->orWhere('u.username = ' . $qb->createPositionalParameter('Bar', IQueryBuilder::PARAM_STR)) |
|
| 1253 | + * </code> |
|
| 1254 | + * |
|
| 1255 | + * @param mixed $value |
|
| 1256 | + * @param IQueryBuilder::PARAM_* $type |
|
| 1257 | + * |
|
| 1258 | + * @return IParameter |
|
| 1259 | + */ |
|
| 1260 | + public function createPositionalParameter($value, $type = IQueryBuilder::PARAM_STR) { |
|
| 1261 | + return new Parameter($this->queryBuilder->createPositionalParameter($value, $type)); |
|
| 1262 | + } |
|
| 1263 | + |
|
| 1264 | + /** |
|
| 1265 | + * Creates a new parameter |
|
| 1266 | + * |
|
| 1267 | + * Example: |
|
| 1268 | + * <code> |
|
| 1269 | + * $qb = $conn->getQueryBuilder(); |
|
| 1270 | + * $qb->select('u.*') |
|
| 1271 | + * ->from('users', 'u') |
|
| 1272 | + * ->where('u.username = ' . $qb->createParameter('name')) |
|
| 1273 | + * ->setParameter('name', 'Bar', IQueryBuilder::PARAM_STR)) |
|
| 1274 | + * </code> |
|
| 1275 | + * |
|
| 1276 | + * @param string $name |
|
| 1277 | + * |
|
| 1278 | + * @return IParameter |
|
| 1279 | + */ |
|
| 1280 | + public function createParameter($name) { |
|
| 1281 | + return new Parameter(':' . $name); |
|
| 1282 | + } |
|
| 1283 | + |
|
| 1284 | + /** |
|
| 1285 | + * Creates a new function |
|
| 1286 | + * |
|
| 1287 | + * Attention: Column names inside the call have to be quoted before hand |
|
| 1288 | + * |
|
| 1289 | + * Example: |
|
| 1290 | + * <code> |
|
| 1291 | + * $qb = $conn->getQueryBuilder(); |
|
| 1292 | + * $qb->select($qb->createFunction('COUNT(*)')) |
|
| 1293 | + * ->from('users', 'u') |
|
| 1294 | + * echo $qb->getSQL(); // SELECT COUNT(*) FROM `users` u |
|
| 1295 | + * </code> |
|
| 1296 | + * <code> |
|
| 1297 | + * $qb = $conn->getQueryBuilder(); |
|
| 1298 | + * $qb->select($qb->createFunction('COUNT(`column`)')) |
|
| 1299 | + * ->from('users', 'u') |
|
| 1300 | + * echo $qb->getSQL(); // SELECT COUNT(`column`) FROM `users` u |
|
| 1301 | + * </code> |
|
| 1302 | + * |
|
| 1303 | + * @param string $call |
|
| 1304 | + * |
|
| 1305 | + * @return IQueryFunction |
|
| 1306 | + */ |
|
| 1307 | + public function createFunction($call) { |
|
| 1308 | + return new QueryFunction($call); |
|
| 1309 | + } |
|
| 1310 | + |
|
| 1311 | + /** |
|
| 1312 | + * Used to get the id of the last inserted element |
|
| 1313 | + * @return int |
|
| 1314 | + * @throws \BadMethodCallException When being called before an insert query has been run. |
|
| 1315 | + */ |
|
| 1316 | + public function getLastInsertId(): int { |
|
| 1317 | + if ($this->getType() === \Doctrine\DBAL\Query\QueryBuilder::INSERT && $this->lastInsertedTable) { |
|
| 1318 | + // lastInsertId() needs the prefix but no quotes |
|
| 1319 | + $table = $this->prefixTableName($this->lastInsertedTable); |
|
| 1320 | + return $this->connection->lastInsertId($table); |
|
| 1321 | + } |
|
| 1322 | + |
|
| 1323 | + throw new \BadMethodCallException('Invalid call to getLastInsertId without using insert() before.'); |
|
| 1324 | + } |
|
| 1325 | + |
|
| 1326 | + /** |
|
| 1327 | + * Returns the table name quoted and with database prefix as needed by the implementation |
|
| 1328 | + * |
|
| 1329 | + * @param string|IQueryFunction $table |
|
| 1330 | + * @return string |
|
| 1331 | + */ |
|
| 1332 | + public function getTableName($table) { |
|
| 1333 | + if ($table instanceof IQueryFunction) { |
|
| 1334 | + return (string)$table; |
|
| 1335 | + } |
|
| 1336 | + |
|
| 1337 | + $table = $this->prefixTableName($table); |
|
| 1338 | + return $this->helper->quoteColumnName($table); |
|
| 1339 | + } |
|
| 1340 | + |
|
| 1341 | + /** |
|
| 1342 | + * Returns the table name with database prefix as needed by the implementation |
|
| 1343 | + * |
|
| 1344 | + * Was protected until version 30. |
|
| 1345 | + * |
|
| 1346 | + * @param string $table |
|
| 1347 | + * @return string |
|
| 1348 | + */ |
|
| 1349 | + public function prefixTableName(string $table): string { |
|
| 1350 | + if ($this->automaticTablePrefix === false || str_starts_with($table, '*PREFIX*')) { |
|
| 1351 | + return $table; |
|
| 1352 | + } |
|
| 1353 | + |
|
| 1354 | + return '*PREFIX*' . $table; |
|
| 1355 | + } |
|
| 1356 | + |
|
| 1357 | + /** |
|
| 1358 | + * Returns the column name quoted and with table alias prefix as needed by the implementation |
|
| 1359 | + * |
|
| 1360 | + * @param string $column |
|
| 1361 | + * @param string $tableAlias |
|
| 1362 | + * @return string |
|
| 1363 | + */ |
|
| 1364 | + public function getColumnName($column, $tableAlias = '') { |
|
| 1365 | + if ($tableAlias !== '') { |
|
| 1366 | + $tableAlias .= '.'; |
|
| 1367 | + } |
|
| 1368 | + |
|
| 1369 | + return $this->helper->quoteColumnName($tableAlias . $column); |
|
| 1370 | + } |
|
| 1371 | + |
|
| 1372 | + /** |
|
| 1373 | + * Returns the column name quoted and with table alias prefix as needed by the implementation |
|
| 1374 | + * |
|
| 1375 | + * @param string $alias |
|
| 1376 | + * @return string |
|
| 1377 | + */ |
|
| 1378 | + public function quoteAlias($alias) { |
|
| 1379 | + if ($alias === '' || $alias === null) { |
|
| 1380 | + return $alias; |
|
| 1381 | + } |
|
| 1382 | + |
|
| 1383 | + return $this->helper->quoteColumnName($alias); |
|
| 1384 | + } |
|
| 1385 | + |
|
| 1386 | + public function escapeLikeParameter(string $parameter): string { |
|
| 1387 | + return $this->connection->escapeLikeParameter($parameter); |
|
| 1388 | + } |
|
| 1389 | + |
|
| 1390 | + public function hintShardKey(string $column, mixed $value, bool $overwrite = false): self { |
|
| 1391 | + return $this; |
|
| 1392 | + } |
|
| 1393 | + |
|
| 1394 | + public function runAcrossAllShards(): self { |
|
| 1395 | + // noop |
|
| 1396 | + return $this; |
|
| 1397 | + } |
|
| 1398 | 1398 | |
| 1399 | 1399 | } |