@@ -31,871 +31,871 @@ |
||
| 31 | 31 | */ |
| 32 | 32 | interface IQueryBuilder { |
| 33 | 33 | |
| 34 | - /** |
|
| 35 | - * @since 9.0.0 |
|
| 36 | - */ |
|
| 37 | - const PARAM_NULL = \PDO::PARAM_NULL; |
|
| 38 | - /** |
|
| 39 | - * @since 9.0.0 |
|
| 40 | - */ |
|
| 41 | - const PARAM_BOOL = \PDO::PARAM_BOOL; |
|
| 42 | - /** |
|
| 43 | - * @since 9.0.0 |
|
| 44 | - */ |
|
| 45 | - const PARAM_INT = \PDO::PARAM_INT; |
|
| 46 | - /** |
|
| 47 | - * @since 9.0.0 |
|
| 48 | - */ |
|
| 49 | - const PARAM_STR = \PDO::PARAM_STR; |
|
| 50 | - /** |
|
| 51 | - * @since 9.0.0 |
|
| 52 | - */ |
|
| 53 | - const PARAM_LOB = \PDO::PARAM_LOB; |
|
| 54 | - /** |
|
| 55 | - * @since 9.0.0 |
|
| 56 | - */ |
|
| 57 | - const PARAM_DATE = 'datetime'; |
|
| 58 | - |
|
| 59 | - /** |
|
| 60 | - * @since 9.0.0 |
|
| 61 | - */ |
|
| 62 | - const PARAM_INT_ARRAY = Connection::PARAM_INT_ARRAY; |
|
| 63 | - /** |
|
| 64 | - * @since 9.0.0 |
|
| 65 | - */ |
|
| 66 | - const PARAM_STR_ARRAY = Connection::PARAM_STR_ARRAY; |
|
| 67 | - |
|
| 68 | - |
|
| 69 | - /** |
|
| 70 | - * Enable/disable automatic prefixing of table names with the oc_ prefix |
|
| 71 | - * |
|
| 72 | - * @param bool $enabled If set to true table names will be prefixed with the |
|
| 73 | - * owncloud database prefix automatically. |
|
| 74 | - * @since 8.2.0 |
|
| 75 | - */ |
|
| 76 | - public function automaticTablePrefix($enabled); |
|
| 77 | - |
|
| 78 | - /** |
|
| 79 | - * Gets an ExpressionBuilder used for object-oriented construction of query expressions. |
|
| 80 | - * This producer method is intended for convenient inline usage. Example: |
|
| 81 | - * |
|
| 82 | - * <code> |
|
| 83 | - * $qb = $conn->getQueryBuilder() |
|
| 84 | - * ->select('u') |
|
| 85 | - * ->from('users', 'u') |
|
| 86 | - * ->where($qb->expr()->eq('u.id', 1)); |
|
| 87 | - * </code> |
|
| 88 | - * |
|
| 89 | - * For more complex expression construction, consider storing the expression |
|
| 90 | - * builder object in a local variable. |
|
| 91 | - * |
|
| 92 | - * @return \OCP\DB\QueryBuilder\IExpressionBuilder |
|
| 93 | - * @since 8.2.0 |
|
| 94 | - */ |
|
| 95 | - public function expr(); |
|
| 96 | - |
|
| 97 | - /** |
|
| 98 | - * Gets an FunctionBuilder used for object-oriented construction of query functions. |
|
| 99 | - * This producer method is intended for convenient inline usage. Example: |
|
| 100 | - * |
|
| 101 | - * <code> |
|
| 102 | - * $qb = $conn->getQueryBuilder() |
|
| 103 | - * ->select('u') |
|
| 104 | - * ->from('users', 'u') |
|
| 105 | - * ->where($qb->fun()->md5('u.id')); |
|
| 106 | - * </code> |
|
| 107 | - * |
|
| 108 | - * For more complex function construction, consider storing the function |
|
| 109 | - * builder object in a local variable. |
|
| 110 | - * |
|
| 111 | - * @return \OCP\DB\QueryBuilder\IFunctionBuilder |
|
| 112 | - * @since 12.0.0 |
|
| 113 | - */ |
|
| 114 | - public function func(); |
|
| 115 | - |
|
| 116 | - /** |
|
| 117 | - * Gets the type of the currently built query. |
|
| 118 | - * |
|
| 119 | - * @return integer |
|
| 120 | - * @since 8.2.0 |
|
| 121 | - */ |
|
| 122 | - public function getType(); |
|
| 123 | - |
|
| 124 | - /** |
|
| 125 | - * Gets the associated DBAL Connection for this query builder. |
|
| 126 | - * |
|
| 127 | - * @return \OCP\IDBConnection |
|
| 128 | - * @since 8.2.0 |
|
| 129 | - */ |
|
| 130 | - public function getConnection(); |
|
| 131 | - |
|
| 132 | - /** |
|
| 133 | - * Gets the state of this query builder instance. |
|
| 134 | - * |
|
| 135 | - * @return integer Either QueryBuilder::STATE_DIRTY or QueryBuilder::STATE_CLEAN. |
|
| 136 | - * @since 8.2.0 |
|
| 137 | - */ |
|
| 138 | - public function getState(); |
|
| 139 | - |
|
| 140 | - /** |
|
| 141 | - * Executes this query using the bound parameters and their types. |
|
| 142 | - * |
|
| 143 | - * Uses {@see Connection::executeQuery} for select statements and {@see Connection::executeUpdate} |
|
| 144 | - * for insert, update and delete statements. |
|
| 145 | - * |
|
| 146 | - * @return \Doctrine\DBAL\Driver\Statement|int |
|
| 147 | - * @since 8.2.0 |
|
| 148 | - */ |
|
| 149 | - public function execute(); |
|
| 150 | - |
|
| 151 | - /** |
|
| 152 | - * Gets the complete SQL string formed by the current specifications of this QueryBuilder. |
|
| 153 | - * |
|
| 154 | - * <code> |
|
| 155 | - * $qb = $conn->getQueryBuilder() |
|
| 156 | - * ->select('u') |
|
| 157 | - * ->from('User', 'u') |
|
| 158 | - * echo $qb->getSQL(); // SELECT u FROM User u |
|
| 159 | - * </code> |
|
| 160 | - * |
|
| 161 | - * @return string The SQL query string. |
|
| 162 | - * @since 8.2.0 |
|
| 163 | - */ |
|
| 164 | - public function getSQL(); |
|
| 165 | - |
|
| 166 | - /** |
|
| 167 | - * Sets a query parameter for the query being constructed. |
|
| 168 | - * |
|
| 169 | - * <code> |
|
| 170 | - * $qb = $conn->getQueryBuilder() |
|
| 171 | - * ->select('u') |
|
| 172 | - * ->from('users', 'u') |
|
| 173 | - * ->where('u.id = :user_id') |
|
| 174 | - * ->setParameter(':user_id', 1); |
|
| 175 | - * </code> |
|
| 176 | - * |
|
| 177 | - * @param string|integer $key The parameter position or name. |
|
| 178 | - * @param mixed $value The parameter value. |
|
| 179 | - * @param string|null $type One of the IQueryBuilder::PARAM_* constants. |
|
| 180 | - * |
|
| 181 | - * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance. |
|
| 182 | - * @since 8.2.0 |
|
| 183 | - */ |
|
| 184 | - public function setParameter($key, $value, $type = null); |
|
| 185 | - |
|
| 186 | - /** |
|
| 187 | - * Sets a collection of query parameters for the query being constructed. |
|
| 188 | - * |
|
| 189 | - * <code> |
|
| 190 | - * $qb = $conn->getQueryBuilder() |
|
| 191 | - * ->select('u') |
|
| 192 | - * ->from('users', 'u') |
|
| 193 | - * ->where('u.id = :user_id1 OR u.id = :user_id2') |
|
| 194 | - * ->setParameters(array( |
|
| 195 | - * ':user_id1' => 1, |
|
| 196 | - * ':user_id2' => 2 |
|
| 197 | - * )); |
|
| 198 | - * </code> |
|
| 199 | - * |
|
| 200 | - * @param array $params The query parameters to set. |
|
| 201 | - * @param array $types The query parameters types to set. |
|
| 202 | - * |
|
| 203 | - * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance. |
|
| 204 | - * @since 8.2.0 |
|
| 205 | - */ |
|
| 206 | - public function setParameters(array $params, array $types = array()); |
|
| 207 | - |
|
| 208 | - /** |
|
| 209 | - * Gets all defined query parameters for the query being constructed indexed by parameter index or name. |
|
| 210 | - * |
|
| 211 | - * @return array The currently defined query parameters indexed by parameter index or name. |
|
| 212 | - * @since 8.2.0 |
|
| 213 | - */ |
|
| 214 | - public function getParameters(); |
|
| 215 | - |
|
| 216 | - /** |
|
| 217 | - * Gets a (previously set) query parameter of the query being constructed. |
|
| 218 | - * |
|
| 219 | - * @param mixed $key The key (index or name) of the bound parameter. |
|
| 220 | - * |
|
| 221 | - * @return mixed The value of the bound parameter. |
|
| 222 | - * @since 8.2.0 |
|
| 223 | - */ |
|
| 224 | - public function getParameter($key); |
|
| 225 | - |
|
| 226 | - /** |
|
| 227 | - * Gets all defined query parameter types for the query being constructed indexed by parameter index or name. |
|
| 228 | - * |
|
| 229 | - * @return array The currently defined query parameter types indexed by parameter index or name. |
|
| 230 | - * @since 8.2.0 |
|
| 231 | - */ |
|
| 232 | - public function getParameterTypes(); |
|
| 233 | - |
|
| 234 | - /** |
|
| 235 | - * Gets a (previously set) query parameter type of the query being constructed. |
|
| 236 | - * |
|
| 237 | - * @param mixed $key The key (index or name) of the bound parameter type. |
|
| 238 | - * |
|
| 239 | - * @return mixed The value of the bound parameter type. |
|
| 240 | - * @since 8.2.0 |
|
| 241 | - */ |
|
| 242 | - public function getParameterType($key); |
|
| 243 | - |
|
| 244 | - /** |
|
| 245 | - * Sets the position of the first result to retrieve (the "offset"). |
|
| 246 | - * |
|
| 247 | - * @param integer $firstResult The first result to return. |
|
| 248 | - * |
|
| 249 | - * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance. |
|
| 250 | - * @since 8.2.0 |
|
| 251 | - */ |
|
| 252 | - public function setFirstResult($firstResult); |
|
| 253 | - |
|
| 254 | - /** |
|
| 255 | - * Gets the position of the first result the query object was set to retrieve (the "offset"). |
|
| 256 | - * Returns NULL if {@link setFirstResult} was not applied to this QueryBuilder. |
|
| 257 | - * |
|
| 258 | - * @return integer The position of the first result. |
|
| 259 | - * @since 8.2.0 |
|
| 260 | - */ |
|
| 261 | - public function getFirstResult(); |
|
| 262 | - |
|
| 263 | - /** |
|
| 264 | - * Sets the maximum number of results to retrieve (the "limit"). |
|
| 265 | - * |
|
| 266 | - * @param integer $maxResults The maximum number of results to retrieve. |
|
| 267 | - * |
|
| 268 | - * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance. |
|
| 269 | - * @since 8.2.0 |
|
| 270 | - */ |
|
| 271 | - public function setMaxResults($maxResults); |
|
| 272 | - |
|
| 273 | - /** |
|
| 274 | - * Gets the maximum number of results the query object was set to retrieve (the "limit"). |
|
| 275 | - * Returns NULL if {@link setMaxResults} was not applied to this query builder. |
|
| 276 | - * |
|
| 277 | - * @return integer The maximum number of results. |
|
| 278 | - * @since 8.2.0 |
|
| 279 | - */ |
|
| 280 | - public function getMaxResults(); |
|
| 281 | - |
|
| 282 | - /** |
|
| 283 | - * Specifies an item that is to be returned in the query result. |
|
| 284 | - * Replaces any previously specified selections, if any. |
|
| 285 | - * |
|
| 286 | - * <code> |
|
| 287 | - * $qb = $conn->getQueryBuilder() |
|
| 288 | - * ->select('u.id', 'p.id') |
|
| 289 | - * ->from('users', 'u') |
|
| 290 | - * ->leftJoin('u', 'phonenumbers', 'p', 'u.id = p.user_id'); |
|
| 291 | - * </code> |
|
| 292 | - * |
|
| 293 | - * @param mixed $select The selection expressions. |
|
| 294 | - * |
|
| 295 | - * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance. |
|
| 296 | - * @since 8.2.0 |
|
| 297 | - */ |
|
| 298 | - public function select($select = null); |
|
| 299 | - |
|
| 300 | - /** |
|
| 301 | - * Specifies an item that is to be returned with a different name in the query result. |
|
| 302 | - * |
|
| 303 | - * <code> |
|
| 304 | - * $qb = $conn->getQueryBuilder() |
|
| 305 | - * ->selectAlias('u.id', 'user_id') |
|
| 306 | - * ->from('users', 'u') |
|
| 307 | - * ->leftJoin('u', 'phonenumbers', 'p', 'u.id = p.user_id'); |
|
| 308 | - * </code> |
|
| 309 | - * |
|
| 310 | - * @param mixed $select The selection expressions. |
|
| 311 | - * @param string $alias The column alias used in the constructed query. |
|
| 312 | - * |
|
| 313 | - * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance. |
|
| 314 | - * @since 8.2.1 |
|
| 315 | - */ |
|
| 316 | - public function selectAlias($select, $alias); |
|
| 317 | - |
|
| 318 | - /** |
|
| 319 | - * Specifies an item that is to be returned uniquely in the query result. |
|
| 320 | - * |
|
| 321 | - * <code> |
|
| 322 | - * $qb = $conn->getQueryBuilder() |
|
| 323 | - * ->selectDistinct('type') |
|
| 324 | - * ->from('users'); |
|
| 325 | - * </code> |
|
| 326 | - * |
|
| 327 | - * @param mixed $select The selection expressions. |
|
| 328 | - * |
|
| 329 | - * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance. |
|
| 330 | - * @since 9.0.0 |
|
| 331 | - */ |
|
| 332 | - public function selectDistinct($select); |
|
| 333 | - |
|
| 334 | - /** |
|
| 335 | - * Adds an item that is to be returned in the query result. |
|
| 336 | - * |
|
| 337 | - * <code> |
|
| 338 | - * $qb = $conn->getQueryBuilder() |
|
| 339 | - * ->select('u.id') |
|
| 340 | - * ->addSelect('p.id') |
|
| 341 | - * ->from('users', 'u') |
|
| 342 | - * ->leftJoin('u', 'phonenumbers', 'u.id = p.user_id'); |
|
| 343 | - * </code> |
|
| 344 | - * |
|
| 345 | - * @param mixed $select The selection expression. |
|
| 346 | - * |
|
| 347 | - * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance. |
|
| 348 | - * @since 8.2.0 |
|
| 349 | - */ |
|
| 350 | - public function addSelect($select = null); |
|
| 351 | - |
|
| 352 | - /** |
|
| 353 | - * Turns the query being built into a bulk delete query that ranges over |
|
| 354 | - * a certain table. |
|
| 355 | - * |
|
| 356 | - * <code> |
|
| 357 | - * $qb = $conn->getQueryBuilder() |
|
| 358 | - * ->delete('users', 'u') |
|
| 359 | - * ->where('u.id = :user_id'); |
|
| 360 | - * ->setParameter(':user_id', 1); |
|
| 361 | - * </code> |
|
| 362 | - * |
|
| 363 | - * @param string $delete The table whose rows are subject to the deletion. |
|
| 364 | - * @param string $alias The table alias used in the constructed query. |
|
| 365 | - * |
|
| 366 | - * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance. |
|
| 367 | - * @since 8.2.0 |
|
| 368 | - */ |
|
| 369 | - public function delete($delete = null, $alias = null); |
|
| 370 | - |
|
| 371 | - /** |
|
| 372 | - * Turns the query being built into a bulk update query that ranges over |
|
| 373 | - * a certain table |
|
| 374 | - * |
|
| 375 | - * <code> |
|
| 376 | - * $qb = $conn->getQueryBuilder() |
|
| 377 | - * ->update('users', 'u') |
|
| 378 | - * ->set('u.password', md5('password')) |
|
| 379 | - * ->where('u.id = ?'); |
|
| 380 | - * </code> |
|
| 381 | - * |
|
| 382 | - * @param string $update The table whose rows are subject to the update. |
|
| 383 | - * @param string $alias The table alias used in the constructed query. |
|
| 384 | - * |
|
| 385 | - * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance. |
|
| 386 | - * @since 8.2.0 |
|
| 387 | - */ |
|
| 388 | - public function update($update = null, $alias = null); |
|
| 389 | - |
|
| 390 | - /** |
|
| 391 | - * Turns the query being built into an insert query that inserts into |
|
| 392 | - * a certain table |
|
| 393 | - * |
|
| 394 | - * <code> |
|
| 395 | - * $qb = $conn->getQueryBuilder() |
|
| 396 | - * ->insert('users') |
|
| 397 | - * ->values( |
|
| 398 | - * array( |
|
| 399 | - * 'name' => '?', |
|
| 400 | - * 'password' => '?' |
|
| 401 | - * ) |
|
| 402 | - * ); |
|
| 403 | - * </code> |
|
| 404 | - * |
|
| 405 | - * @param string $insert The table into which the rows should be inserted. |
|
| 406 | - * |
|
| 407 | - * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance. |
|
| 408 | - * @since 8.2.0 |
|
| 409 | - */ |
|
| 410 | - public function insert($insert = null); |
|
| 411 | - |
|
| 412 | - /** |
|
| 413 | - * Creates and adds a query root corresponding to the table identified by the |
|
| 414 | - * given alias, forming a cartesian product with any existing query roots. |
|
| 415 | - * |
|
| 416 | - * <code> |
|
| 417 | - * $qb = $conn->getQueryBuilder() |
|
| 418 | - * ->select('u.id') |
|
| 419 | - * ->from('users', 'u') |
|
| 420 | - * </code> |
|
| 421 | - * |
|
| 422 | - * @param string $from The table. |
|
| 423 | - * @param string|null $alias The alias of the table. |
|
| 424 | - * |
|
| 425 | - * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance. |
|
| 426 | - * @since 8.2.0 |
|
| 427 | - */ |
|
| 428 | - public function from($from, $alias = null); |
|
| 429 | - |
|
| 430 | - /** |
|
| 431 | - * Creates and adds a join to the query. |
|
| 432 | - * |
|
| 433 | - * <code> |
|
| 434 | - * $qb = $conn->getQueryBuilder() |
|
| 435 | - * ->select('u.name') |
|
| 436 | - * ->from('users', 'u') |
|
| 437 | - * ->join('u', 'phonenumbers', 'p', 'p.is_primary = 1'); |
|
| 438 | - * </code> |
|
| 439 | - * |
|
| 440 | - * @param string $fromAlias The alias that points to a from clause. |
|
| 441 | - * @param string $join The table name to join. |
|
| 442 | - * @param string $alias The alias of the join table. |
|
| 443 | - * @param string $condition The condition for the join. |
|
| 444 | - * |
|
| 445 | - * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance. |
|
| 446 | - * @since 8.2.0 |
|
| 447 | - */ |
|
| 448 | - public function join($fromAlias, $join, $alias, $condition = null); |
|
| 449 | - |
|
| 450 | - /** |
|
| 451 | - * Creates and adds a join to the query. |
|
| 452 | - * |
|
| 453 | - * <code> |
|
| 454 | - * $qb = $conn->getQueryBuilder() |
|
| 455 | - * ->select('u.name') |
|
| 456 | - * ->from('users', 'u') |
|
| 457 | - * ->innerJoin('u', 'phonenumbers', 'p', 'p.is_primary = 1'); |
|
| 458 | - * </code> |
|
| 459 | - * |
|
| 460 | - * @param string $fromAlias The alias that points to a from clause. |
|
| 461 | - * @param string $join The table name to join. |
|
| 462 | - * @param string $alias The alias of the join table. |
|
| 463 | - * @param string $condition The condition for the join. |
|
| 464 | - * |
|
| 465 | - * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance. |
|
| 466 | - * @since 8.2.0 |
|
| 467 | - */ |
|
| 468 | - public function innerJoin($fromAlias, $join, $alias, $condition = null); |
|
| 469 | - |
|
| 470 | - /** |
|
| 471 | - * Creates and adds a left join to the query. |
|
| 472 | - * |
|
| 473 | - * <code> |
|
| 474 | - * $qb = $conn->getQueryBuilder() |
|
| 475 | - * ->select('u.name') |
|
| 476 | - * ->from('users', 'u') |
|
| 477 | - * ->leftJoin('u', 'phonenumbers', 'p', 'p.is_primary = 1'); |
|
| 478 | - * </code> |
|
| 479 | - * |
|
| 480 | - * @param string $fromAlias The alias that points to a from clause. |
|
| 481 | - * @param string $join The table name to join. |
|
| 482 | - * @param string $alias The alias of the join table. |
|
| 483 | - * @param string $condition The condition for the join. |
|
| 484 | - * |
|
| 485 | - * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance. |
|
| 486 | - * @since 8.2.0 |
|
| 487 | - */ |
|
| 488 | - public function leftJoin($fromAlias, $join, $alias, $condition = null); |
|
| 489 | - |
|
| 490 | - /** |
|
| 491 | - * Creates and adds a right join to the query. |
|
| 492 | - * |
|
| 493 | - * <code> |
|
| 494 | - * $qb = $conn->getQueryBuilder() |
|
| 495 | - * ->select('u.name') |
|
| 496 | - * ->from('users', 'u') |
|
| 497 | - * ->rightJoin('u', 'phonenumbers', 'p', 'p.is_primary = 1'); |
|
| 498 | - * </code> |
|
| 499 | - * |
|
| 500 | - * @param string $fromAlias The alias that points to a from clause. |
|
| 501 | - * @param string $join The table name to join. |
|
| 502 | - * @param string $alias The alias of the join table. |
|
| 503 | - * @param string $condition The condition for the join. |
|
| 504 | - * |
|
| 505 | - * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance. |
|
| 506 | - * @since 8.2.0 |
|
| 507 | - */ |
|
| 508 | - public function rightJoin($fromAlias, $join, $alias, $condition = null); |
|
| 509 | - |
|
| 510 | - /** |
|
| 511 | - * Sets a new value for a column in a bulk update query. |
|
| 512 | - * |
|
| 513 | - * <code> |
|
| 514 | - * $qb = $conn->getQueryBuilder() |
|
| 515 | - * ->update('users', 'u') |
|
| 516 | - * ->set('u.password', md5('password')) |
|
| 517 | - * ->where('u.id = ?'); |
|
| 518 | - * </code> |
|
| 519 | - * |
|
| 520 | - * @param string $key The column to set. |
|
| 521 | - * @param string $value The value, expression, placeholder, etc. |
|
| 522 | - * |
|
| 523 | - * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance. |
|
| 524 | - * @since 8.2.0 |
|
| 525 | - */ |
|
| 526 | - public function set($key, $value); |
|
| 527 | - |
|
| 528 | - /** |
|
| 529 | - * Specifies one or more restrictions to the query result. |
|
| 530 | - * Replaces any previously specified restrictions, if any. |
|
| 531 | - * |
|
| 532 | - * <code> |
|
| 533 | - * $qb = $conn->getQueryBuilder() |
|
| 534 | - * ->select('u.name') |
|
| 535 | - * ->from('users', 'u') |
|
| 536 | - * ->where('u.id = ?'); |
|
| 537 | - * |
|
| 538 | - * // You can optionally programatically build and/or expressions |
|
| 539 | - * $qb = $conn->getQueryBuilder(); |
|
| 540 | - * |
|
| 541 | - * $or = $qb->expr()->orx(); |
|
| 542 | - * $or->add($qb->expr()->eq('u.id', 1)); |
|
| 543 | - * $or->add($qb->expr()->eq('u.id', 2)); |
|
| 544 | - * |
|
| 545 | - * $qb->update('users', 'u') |
|
| 546 | - * ->set('u.password', md5('password')) |
|
| 547 | - * ->where($or); |
|
| 548 | - * </code> |
|
| 549 | - * |
|
| 550 | - * @param mixed $predicates The restriction predicates. |
|
| 551 | - * |
|
| 552 | - * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance. |
|
| 553 | - * @since 8.2.0 |
|
| 554 | - */ |
|
| 555 | - public function where($predicates); |
|
| 556 | - |
|
| 557 | - /** |
|
| 558 | - * Adds one or more restrictions to the query results, forming a logical |
|
| 559 | - * conjunction with any previously specified restrictions. |
|
| 560 | - * |
|
| 561 | - * <code> |
|
| 562 | - * $qb = $conn->getQueryBuilder() |
|
| 563 | - * ->select('u') |
|
| 564 | - * ->from('users', 'u') |
|
| 565 | - * ->where('u.username LIKE ?') |
|
| 566 | - * ->andWhere('u.is_active = 1'); |
|
| 567 | - * </code> |
|
| 568 | - * |
|
| 569 | - * @param mixed $where The query restrictions. |
|
| 570 | - * |
|
| 571 | - * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance. |
|
| 572 | - * |
|
| 573 | - * @see where() |
|
| 574 | - * @since 8.2.0 |
|
| 575 | - */ |
|
| 576 | - public function andWhere($where); |
|
| 577 | - |
|
| 578 | - /** |
|
| 579 | - * Adds one or more restrictions to the query results, forming a logical |
|
| 580 | - * disjunction with any previously specified restrictions. |
|
| 581 | - * |
|
| 582 | - * <code> |
|
| 583 | - * $qb = $conn->getQueryBuilder() |
|
| 584 | - * ->select('u.name') |
|
| 585 | - * ->from('users', 'u') |
|
| 586 | - * ->where('u.id = 1') |
|
| 587 | - * ->orWhere('u.id = 2'); |
|
| 588 | - * </code> |
|
| 589 | - * |
|
| 590 | - * @param mixed $where The WHERE statement. |
|
| 591 | - * |
|
| 592 | - * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance. |
|
| 593 | - * |
|
| 594 | - * @see where() |
|
| 595 | - * @since 8.2.0 |
|
| 596 | - */ |
|
| 597 | - public function orWhere($where); |
|
| 598 | - |
|
| 599 | - /** |
|
| 600 | - * Specifies a grouping over the results of the query. |
|
| 601 | - * Replaces any previously specified groupings, if any. |
|
| 602 | - * |
|
| 603 | - * <code> |
|
| 604 | - * $qb = $conn->getQueryBuilder() |
|
| 605 | - * ->select('u.name') |
|
| 606 | - * ->from('users', 'u') |
|
| 607 | - * ->groupBy('u.id'); |
|
| 608 | - * </code> |
|
| 609 | - * |
|
| 610 | - * @param mixed $groupBy The grouping expression. |
|
| 611 | - * |
|
| 612 | - * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance. |
|
| 613 | - * @since 8.2.0 |
|
| 614 | - */ |
|
| 615 | - public function groupBy($groupBy); |
|
| 616 | - |
|
| 617 | - /** |
|
| 618 | - * Adds a grouping expression to the query. |
|
| 619 | - * |
|
| 620 | - * <code> |
|
| 621 | - * $qb = $conn->getQueryBuilder() |
|
| 622 | - * ->select('u.name') |
|
| 623 | - * ->from('users', 'u') |
|
| 624 | - * ->groupBy('u.lastLogin'); |
|
| 625 | - * ->addGroupBy('u.createdAt') |
|
| 626 | - * </code> |
|
| 627 | - * |
|
| 628 | - * @param mixed $groupBy The grouping expression. |
|
| 629 | - * |
|
| 630 | - * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance. |
|
| 631 | - * @since 8.2.0 |
|
| 632 | - */ |
|
| 633 | - public function addGroupBy($groupBy); |
|
| 634 | - |
|
| 635 | - /** |
|
| 636 | - * Sets a value for a column in an insert query. |
|
| 637 | - * |
|
| 638 | - * <code> |
|
| 639 | - * $qb = $conn->getQueryBuilder() |
|
| 640 | - * ->insert('users') |
|
| 641 | - * ->values( |
|
| 642 | - * array( |
|
| 643 | - * 'name' => '?' |
|
| 644 | - * ) |
|
| 645 | - * ) |
|
| 646 | - * ->setValue('password', '?'); |
|
| 647 | - * </code> |
|
| 648 | - * |
|
| 649 | - * @param string $column The column into which the value should be inserted. |
|
| 650 | - * @param string $value The value that should be inserted into the column. |
|
| 651 | - * |
|
| 652 | - * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance. |
|
| 653 | - * @since 8.2.0 |
|
| 654 | - */ |
|
| 655 | - public function setValue($column, $value); |
|
| 656 | - |
|
| 657 | - /** |
|
| 658 | - * Specifies values for an insert query indexed by column names. |
|
| 659 | - * Replaces any previous values, if any. |
|
| 660 | - * |
|
| 661 | - * <code> |
|
| 662 | - * $qb = $conn->getQueryBuilder() |
|
| 663 | - * ->insert('users') |
|
| 664 | - * ->values( |
|
| 665 | - * array( |
|
| 666 | - * 'name' => '?', |
|
| 667 | - * 'password' => '?' |
|
| 668 | - * ) |
|
| 669 | - * ); |
|
| 670 | - * </code> |
|
| 671 | - * |
|
| 672 | - * @param array $values The values to specify for the insert query indexed by column names. |
|
| 673 | - * |
|
| 674 | - * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance. |
|
| 675 | - * @since 8.2.0 |
|
| 676 | - */ |
|
| 677 | - public function values(array $values); |
|
| 678 | - |
|
| 679 | - /** |
|
| 680 | - * Specifies a restriction over the groups of the query. |
|
| 681 | - * Replaces any previous having restrictions, if any. |
|
| 682 | - * |
|
| 683 | - * @param mixed $having The restriction over the groups. |
|
| 684 | - * |
|
| 685 | - * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance. |
|
| 686 | - * @since 8.2.0 |
|
| 687 | - */ |
|
| 688 | - public function having($having); |
|
| 689 | - |
|
| 690 | - /** |
|
| 691 | - * Adds a restriction over the groups of the query, forming a logical |
|
| 692 | - * conjunction with any existing having restrictions. |
|
| 693 | - * |
|
| 694 | - * @param mixed $having The restriction to append. |
|
| 695 | - * |
|
| 696 | - * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance. |
|
| 697 | - * @since 8.2.0 |
|
| 698 | - */ |
|
| 699 | - public function andHaving($having); |
|
| 700 | - |
|
| 701 | - /** |
|
| 702 | - * Adds a restriction over the groups of the query, forming a logical |
|
| 703 | - * disjunction with any existing having restrictions. |
|
| 704 | - * |
|
| 705 | - * @param mixed $having The restriction to add. |
|
| 706 | - * |
|
| 707 | - * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance. |
|
| 708 | - * @since 8.2.0 |
|
| 709 | - */ |
|
| 710 | - public function orHaving($having); |
|
| 711 | - |
|
| 712 | - /** |
|
| 713 | - * Specifies an ordering for the query results. |
|
| 714 | - * Replaces any previously specified orderings, if any. |
|
| 715 | - * |
|
| 716 | - * @param string $sort The ordering expression. |
|
| 717 | - * @param string $order The ordering direction. |
|
| 718 | - * |
|
| 719 | - * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance. |
|
| 720 | - * @since 8.2.0 |
|
| 721 | - */ |
|
| 722 | - public function orderBy($sort, $order = null); |
|
| 723 | - |
|
| 724 | - /** |
|
| 725 | - * Adds an ordering to the query results. |
|
| 726 | - * |
|
| 727 | - * @param string $sort The ordering expression. |
|
| 728 | - * @param string $order The ordering direction. |
|
| 729 | - * |
|
| 730 | - * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance. |
|
| 731 | - * @since 8.2.0 |
|
| 732 | - */ |
|
| 733 | - public function addOrderBy($sort, $order = null); |
|
| 734 | - |
|
| 735 | - /** |
|
| 736 | - * Gets a query part by its name. |
|
| 737 | - * |
|
| 738 | - * @param string $queryPartName |
|
| 739 | - * |
|
| 740 | - * @return mixed |
|
| 741 | - * @since 8.2.0 |
|
| 742 | - */ |
|
| 743 | - public function getQueryPart($queryPartName); |
|
| 744 | - |
|
| 745 | - /** |
|
| 746 | - * Gets all query parts. |
|
| 747 | - * |
|
| 748 | - * @return array |
|
| 749 | - * @since 8.2.0 |
|
| 750 | - */ |
|
| 751 | - public function getQueryParts(); |
|
| 752 | - |
|
| 753 | - /** |
|
| 754 | - * Resets SQL parts. |
|
| 755 | - * |
|
| 756 | - * @param array|null $queryPartNames |
|
| 757 | - * |
|
| 758 | - * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance. |
|
| 759 | - * @since 8.2.0 |
|
| 760 | - */ |
|
| 761 | - public function resetQueryParts($queryPartNames = null); |
|
| 762 | - |
|
| 763 | - /** |
|
| 764 | - * Resets a single SQL part. |
|
| 765 | - * |
|
| 766 | - * @param string $queryPartName |
|
| 767 | - * |
|
| 768 | - * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance. |
|
| 769 | - * @since 8.2.0 |
|
| 770 | - */ |
|
| 771 | - public function resetQueryPart($queryPartName); |
|
| 772 | - |
|
| 773 | - /** |
|
| 774 | - * Creates a new named parameter and bind the value $value to it. |
|
| 775 | - * |
|
| 776 | - * This method provides a shortcut for PDOStatement::bindValue |
|
| 777 | - * when using prepared statements. |
|
| 778 | - * |
|
| 779 | - * The parameter $value specifies the value that you want to bind. If |
|
| 780 | - * $placeholder is not provided bindValue() will automatically create a |
|
| 781 | - * placeholder for you. An automatic placeholder will be of the name |
|
| 782 | - * ':dcValue1', ':dcValue2' etc. |
|
| 783 | - * |
|
| 784 | - * For more information see {@link http://php.net/pdostatement-bindparam} |
|
| 785 | - * |
|
| 786 | - * Example: |
|
| 787 | - * <code> |
|
| 788 | - * $value = 2; |
|
| 789 | - * $q->eq( 'id', $q->bindValue( $value ) ); |
|
| 790 | - * $stmt = $q->executeQuery(); // executed with 'id = 2' |
|
| 791 | - * </code> |
|
| 792 | - * |
|
| 793 | - * @license New BSD License |
|
| 794 | - * @link http://www.zetacomponents.org |
|
| 795 | - * |
|
| 796 | - * @param mixed $value |
|
| 797 | - * @param mixed $type |
|
| 798 | - * @param string $placeHolder The name to bind with. The string must start with a colon ':'. |
|
| 799 | - * |
|
| 800 | - * @return IParameter |
|
| 801 | - * @since 8.2.0 |
|
| 802 | - */ |
|
| 803 | - public function createNamedParameter($value, $type = self::PARAM_STR, $placeHolder = null); |
|
| 804 | - |
|
| 805 | - /** |
|
| 806 | - * Creates a new positional parameter and bind the given value to it. |
|
| 807 | - * |
|
| 808 | - * Attention: If you are using positional parameters with the query builder you have |
|
| 809 | - * to be very careful to bind all parameters in the order they appear in the SQL |
|
| 810 | - * statement , otherwise they get bound in the wrong order which can lead to serious |
|
| 811 | - * bugs in your code. |
|
| 812 | - * |
|
| 813 | - * Example: |
|
| 814 | - * <code> |
|
| 815 | - * $qb = $conn->getQueryBuilder(); |
|
| 816 | - * $qb->select('u.*') |
|
| 817 | - * ->from('users', 'u') |
|
| 818 | - * ->where('u.username = ' . $qb->createPositionalParameter('Foo', IQueryBuilder::PARAM_STR)) |
|
| 819 | - * ->orWhere('u.username = ' . $qb->createPositionalParameter('Bar', IQueryBuilder::PARAM_STR)) |
|
| 820 | - * </code> |
|
| 821 | - * |
|
| 822 | - * @param mixed $value |
|
| 823 | - * @param integer $type |
|
| 824 | - * |
|
| 825 | - * @return IParameter |
|
| 826 | - * @since 8.2.0 |
|
| 827 | - */ |
|
| 828 | - public function createPositionalParameter($value, $type = self::PARAM_STR); |
|
| 829 | - |
|
| 830 | - /** |
|
| 831 | - * Creates a new parameter |
|
| 832 | - * |
|
| 833 | - * Example: |
|
| 834 | - * <code> |
|
| 835 | - * $qb = $conn->getQueryBuilder(); |
|
| 836 | - * $qb->select('u.*') |
|
| 837 | - * ->from('users', 'u') |
|
| 838 | - * ->where('u.username = ' . $qb->createParameter('name')) |
|
| 839 | - * ->setParameter('name', 'Bar', IQueryBuilder::PARAM_STR)) |
|
| 840 | - * </code> |
|
| 841 | - * |
|
| 842 | - * @param string $name |
|
| 843 | - * |
|
| 844 | - * @return IParameter |
|
| 845 | - * @since 8.2.0 |
|
| 846 | - */ |
|
| 847 | - public function createParameter($name); |
|
| 848 | - |
|
| 849 | - /** |
|
| 850 | - * Creates a new function |
|
| 851 | - * |
|
| 852 | - * Attention: Column names inside the call have to be quoted before hand |
|
| 853 | - * |
|
| 854 | - * Example: |
|
| 855 | - * <code> |
|
| 856 | - * $qb = $conn->getQueryBuilder(); |
|
| 857 | - * $qb->select($qb->createFunction('COUNT(*)')) |
|
| 858 | - * ->from('users', 'u') |
|
| 859 | - * echo $qb->getSQL(); // SELECT COUNT(*) FROM `users` u |
|
| 860 | - * </code> |
|
| 861 | - * <code> |
|
| 862 | - * $qb = $conn->getQueryBuilder(); |
|
| 863 | - * $qb->select($qb->createFunction('COUNT(`column`)')) |
|
| 864 | - * ->from('users', 'u') |
|
| 865 | - * echo $qb->getSQL(); // SELECT COUNT(`column`) FROM `users` u |
|
| 866 | - * </code> |
|
| 867 | - * |
|
| 868 | - * @param string $call |
|
| 869 | - * |
|
| 870 | - * @return IQueryFunction |
|
| 871 | - * @since 8.2.0 |
|
| 872 | - */ |
|
| 873 | - public function createFunction($call); |
|
| 874 | - |
|
| 875 | - /** |
|
| 876 | - * Used to get the id of the last inserted element |
|
| 877 | - * @return int |
|
| 878 | - * @throws \BadMethodCallException When being called before an insert query has been run. |
|
| 879 | - * @since 9.0.0 |
|
| 880 | - */ |
|
| 881 | - public function getLastInsertId(); |
|
| 882 | - |
|
| 883 | - /** |
|
| 884 | - * Returns the table name quoted and with database prefix as needed by the implementation |
|
| 885 | - * |
|
| 886 | - * @param string $table |
|
| 887 | - * @return string |
|
| 888 | - * @since 9.0.0 |
|
| 889 | - */ |
|
| 890 | - public function getTableName($table); |
|
| 891 | - |
|
| 892 | - /** |
|
| 893 | - * Returns the column name quoted and with table alias prefix as needed by the implementation |
|
| 894 | - * |
|
| 895 | - * @param string $column |
|
| 896 | - * @param string $tableAlias |
|
| 897 | - * @return string |
|
| 898 | - * @since 9.0.0 |
|
| 899 | - */ |
|
| 900 | - public function getColumnName($column, $tableAlias = ''); |
|
| 34 | + /** |
|
| 35 | + * @since 9.0.0 |
|
| 36 | + */ |
|
| 37 | + const PARAM_NULL = \PDO::PARAM_NULL; |
|
| 38 | + /** |
|
| 39 | + * @since 9.0.0 |
|
| 40 | + */ |
|
| 41 | + const PARAM_BOOL = \PDO::PARAM_BOOL; |
|
| 42 | + /** |
|
| 43 | + * @since 9.0.0 |
|
| 44 | + */ |
|
| 45 | + const PARAM_INT = \PDO::PARAM_INT; |
|
| 46 | + /** |
|
| 47 | + * @since 9.0.0 |
|
| 48 | + */ |
|
| 49 | + const PARAM_STR = \PDO::PARAM_STR; |
|
| 50 | + /** |
|
| 51 | + * @since 9.0.0 |
|
| 52 | + */ |
|
| 53 | + const PARAM_LOB = \PDO::PARAM_LOB; |
|
| 54 | + /** |
|
| 55 | + * @since 9.0.0 |
|
| 56 | + */ |
|
| 57 | + const PARAM_DATE = 'datetime'; |
|
| 58 | + |
|
| 59 | + /** |
|
| 60 | + * @since 9.0.0 |
|
| 61 | + */ |
|
| 62 | + const PARAM_INT_ARRAY = Connection::PARAM_INT_ARRAY; |
|
| 63 | + /** |
|
| 64 | + * @since 9.0.0 |
|
| 65 | + */ |
|
| 66 | + const PARAM_STR_ARRAY = Connection::PARAM_STR_ARRAY; |
|
| 67 | + |
|
| 68 | + |
|
| 69 | + /** |
|
| 70 | + * Enable/disable automatic prefixing of table names with the oc_ prefix |
|
| 71 | + * |
|
| 72 | + * @param bool $enabled If set to true table names will be prefixed with the |
|
| 73 | + * owncloud database prefix automatically. |
|
| 74 | + * @since 8.2.0 |
|
| 75 | + */ |
|
| 76 | + public function automaticTablePrefix($enabled); |
|
| 77 | + |
|
| 78 | + /** |
|
| 79 | + * Gets an ExpressionBuilder used for object-oriented construction of query expressions. |
|
| 80 | + * This producer method is intended for convenient inline usage. Example: |
|
| 81 | + * |
|
| 82 | + * <code> |
|
| 83 | + * $qb = $conn->getQueryBuilder() |
|
| 84 | + * ->select('u') |
|
| 85 | + * ->from('users', 'u') |
|
| 86 | + * ->where($qb->expr()->eq('u.id', 1)); |
|
| 87 | + * </code> |
|
| 88 | + * |
|
| 89 | + * For more complex expression construction, consider storing the expression |
|
| 90 | + * builder object in a local variable. |
|
| 91 | + * |
|
| 92 | + * @return \OCP\DB\QueryBuilder\IExpressionBuilder |
|
| 93 | + * @since 8.2.0 |
|
| 94 | + */ |
|
| 95 | + public function expr(); |
|
| 96 | + |
|
| 97 | + /** |
|
| 98 | + * Gets an FunctionBuilder used for object-oriented construction of query functions. |
|
| 99 | + * This producer method is intended for convenient inline usage. Example: |
|
| 100 | + * |
|
| 101 | + * <code> |
|
| 102 | + * $qb = $conn->getQueryBuilder() |
|
| 103 | + * ->select('u') |
|
| 104 | + * ->from('users', 'u') |
|
| 105 | + * ->where($qb->fun()->md5('u.id')); |
|
| 106 | + * </code> |
|
| 107 | + * |
|
| 108 | + * For more complex function construction, consider storing the function |
|
| 109 | + * builder object in a local variable. |
|
| 110 | + * |
|
| 111 | + * @return \OCP\DB\QueryBuilder\IFunctionBuilder |
|
| 112 | + * @since 12.0.0 |
|
| 113 | + */ |
|
| 114 | + public function func(); |
|
| 115 | + |
|
| 116 | + /** |
|
| 117 | + * Gets the type of the currently built query. |
|
| 118 | + * |
|
| 119 | + * @return integer |
|
| 120 | + * @since 8.2.0 |
|
| 121 | + */ |
|
| 122 | + public function getType(); |
|
| 123 | + |
|
| 124 | + /** |
|
| 125 | + * Gets the associated DBAL Connection for this query builder. |
|
| 126 | + * |
|
| 127 | + * @return \OCP\IDBConnection |
|
| 128 | + * @since 8.2.0 |
|
| 129 | + */ |
|
| 130 | + public function getConnection(); |
|
| 131 | + |
|
| 132 | + /** |
|
| 133 | + * Gets the state of this query builder instance. |
|
| 134 | + * |
|
| 135 | + * @return integer Either QueryBuilder::STATE_DIRTY or QueryBuilder::STATE_CLEAN. |
|
| 136 | + * @since 8.2.0 |
|
| 137 | + */ |
|
| 138 | + public function getState(); |
|
| 139 | + |
|
| 140 | + /** |
|
| 141 | + * Executes this query using the bound parameters and their types. |
|
| 142 | + * |
|
| 143 | + * Uses {@see Connection::executeQuery} for select statements and {@see Connection::executeUpdate} |
|
| 144 | + * for insert, update and delete statements. |
|
| 145 | + * |
|
| 146 | + * @return \Doctrine\DBAL\Driver\Statement|int |
|
| 147 | + * @since 8.2.0 |
|
| 148 | + */ |
|
| 149 | + public function execute(); |
|
| 150 | + |
|
| 151 | + /** |
|
| 152 | + * Gets the complete SQL string formed by the current specifications of this QueryBuilder. |
|
| 153 | + * |
|
| 154 | + * <code> |
|
| 155 | + * $qb = $conn->getQueryBuilder() |
|
| 156 | + * ->select('u') |
|
| 157 | + * ->from('User', 'u') |
|
| 158 | + * echo $qb->getSQL(); // SELECT u FROM User u |
|
| 159 | + * </code> |
|
| 160 | + * |
|
| 161 | + * @return string The SQL query string. |
|
| 162 | + * @since 8.2.0 |
|
| 163 | + */ |
|
| 164 | + public function getSQL(); |
|
| 165 | + |
|
| 166 | + /** |
|
| 167 | + * Sets a query parameter for the query being constructed. |
|
| 168 | + * |
|
| 169 | + * <code> |
|
| 170 | + * $qb = $conn->getQueryBuilder() |
|
| 171 | + * ->select('u') |
|
| 172 | + * ->from('users', 'u') |
|
| 173 | + * ->where('u.id = :user_id') |
|
| 174 | + * ->setParameter(':user_id', 1); |
|
| 175 | + * </code> |
|
| 176 | + * |
|
| 177 | + * @param string|integer $key The parameter position or name. |
|
| 178 | + * @param mixed $value The parameter value. |
|
| 179 | + * @param string|null $type One of the IQueryBuilder::PARAM_* constants. |
|
| 180 | + * |
|
| 181 | + * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance. |
|
| 182 | + * @since 8.2.0 |
|
| 183 | + */ |
|
| 184 | + public function setParameter($key, $value, $type = null); |
|
| 185 | + |
|
| 186 | + /** |
|
| 187 | + * Sets a collection of query parameters for the query being constructed. |
|
| 188 | + * |
|
| 189 | + * <code> |
|
| 190 | + * $qb = $conn->getQueryBuilder() |
|
| 191 | + * ->select('u') |
|
| 192 | + * ->from('users', 'u') |
|
| 193 | + * ->where('u.id = :user_id1 OR u.id = :user_id2') |
|
| 194 | + * ->setParameters(array( |
|
| 195 | + * ':user_id1' => 1, |
|
| 196 | + * ':user_id2' => 2 |
|
| 197 | + * )); |
|
| 198 | + * </code> |
|
| 199 | + * |
|
| 200 | + * @param array $params The query parameters to set. |
|
| 201 | + * @param array $types The query parameters types to set. |
|
| 202 | + * |
|
| 203 | + * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance. |
|
| 204 | + * @since 8.2.0 |
|
| 205 | + */ |
|
| 206 | + public function setParameters(array $params, array $types = array()); |
|
| 207 | + |
|
| 208 | + /** |
|
| 209 | + * Gets all defined query parameters for the query being constructed indexed by parameter index or name. |
|
| 210 | + * |
|
| 211 | + * @return array The currently defined query parameters indexed by parameter index or name. |
|
| 212 | + * @since 8.2.0 |
|
| 213 | + */ |
|
| 214 | + public function getParameters(); |
|
| 215 | + |
|
| 216 | + /** |
|
| 217 | + * Gets a (previously set) query parameter of the query being constructed. |
|
| 218 | + * |
|
| 219 | + * @param mixed $key The key (index or name) of the bound parameter. |
|
| 220 | + * |
|
| 221 | + * @return mixed The value of the bound parameter. |
|
| 222 | + * @since 8.2.0 |
|
| 223 | + */ |
|
| 224 | + public function getParameter($key); |
|
| 225 | + |
|
| 226 | + /** |
|
| 227 | + * Gets all defined query parameter types for the query being constructed indexed by parameter index or name. |
|
| 228 | + * |
|
| 229 | + * @return array The currently defined query parameter types indexed by parameter index or name. |
|
| 230 | + * @since 8.2.0 |
|
| 231 | + */ |
|
| 232 | + public function getParameterTypes(); |
|
| 233 | + |
|
| 234 | + /** |
|
| 235 | + * Gets a (previously set) query parameter type of the query being constructed. |
|
| 236 | + * |
|
| 237 | + * @param mixed $key The key (index or name) of the bound parameter type. |
|
| 238 | + * |
|
| 239 | + * @return mixed The value of the bound parameter type. |
|
| 240 | + * @since 8.2.0 |
|
| 241 | + */ |
|
| 242 | + public function getParameterType($key); |
|
| 243 | + |
|
| 244 | + /** |
|
| 245 | + * Sets the position of the first result to retrieve (the "offset"). |
|
| 246 | + * |
|
| 247 | + * @param integer $firstResult The first result to return. |
|
| 248 | + * |
|
| 249 | + * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance. |
|
| 250 | + * @since 8.2.0 |
|
| 251 | + */ |
|
| 252 | + public function setFirstResult($firstResult); |
|
| 253 | + |
|
| 254 | + /** |
|
| 255 | + * Gets the position of the first result the query object was set to retrieve (the "offset"). |
|
| 256 | + * Returns NULL if {@link setFirstResult} was not applied to this QueryBuilder. |
|
| 257 | + * |
|
| 258 | + * @return integer The position of the first result. |
|
| 259 | + * @since 8.2.0 |
|
| 260 | + */ |
|
| 261 | + public function getFirstResult(); |
|
| 262 | + |
|
| 263 | + /** |
|
| 264 | + * Sets the maximum number of results to retrieve (the "limit"). |
|
| 265 | + * |
|
| 266 | + * @param integer $maxResults The maximum number of results to retrieve. |
|
| 267 | + * |
|
| 268 | + * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance. |
|
| 269 | + * @since 8.2.0 |
|
| 270 | + */ |
|
| 271 | + public function setMaxResults($maxResults); |
|
| 272 | + |
|
| 273 | + /** |
|
| 274 | + * Gets the maximum number of results the query object was set to retrieve (the "limit"). |
|
| 275 | + * Returns NULL if {@link setMaxResults} was not applied to this query builder. |
|
| 276 | + * |
|
| 277 | + * @return integer The maximum number of results. |
|
| 278 | + * @since 8.2.0 |
|
| 279 | + */ |
|
| 280 | + public function getMaxResults(); |
|
| 281 | + |
|
| 282 | + /** |
|
| 283 | + * Specifies an item that is to be returned in the query result. |
|
| 284 | + * Replaces any previously specified selections, if any. |
|
| 285 | + * |
|
| 286 | + * <code> |
|
| 287 | + * $qb = $conn->getQueryBuilder() |
|
| 288 | + * ->select('u.id', 'p.id') |
|
| 289 | + * ->from('users', 'u') |
|
| 290 | + * ->leftJoin('u', 'phonenumbers', 'p', 'u.id = p.user_id'); |
|
| 291 | + * </code> |
|
| 292 | + * |
|
| 293 | + * @param mixed $select The selection expressions. |
|
| 294 | + * |
|
| 295 | + * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance. |
|
| 296 | + * @since 8.2.0 |
|
| 297 | + */ |
|
| 298 | + public function select($select = null); |
|
| 299 | + |
|
| 300 | + /** |
|
| 301 | + * Specifies an item that is to be returned with a different name in the query result. |
|
| 302 | + * |
|
| 303 | + * <code> |
|
| 304 | + * $qb = $conn->getQueryBuilder() |
|
| 305 | + * ->selectAlias('u.id', 'user_id') |
|
| 306 | + * ->from('users', 'u') |
|
| 307 | + * ->leftJoin('u', 'phonenumbers', 'p', 'u.id = p.user_id'); |
|
| 308 | + * </code> |
|
| 309 | + * |
|
| 310 | + * @param mixed $select The selection expressions. |
|
| 311 | + * @param string $alias The column alias used in the constructed query. |
|
| 312 | + * |
|
| 313 | + * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance. |
|
| 314 | + * @since 8.2.1 |
|
| 315 | + */ |
|
| 316 | + public function selectAlias($select, $alias); |
|
| 317 | + |
|
| 318 | + /** |
|
| 319 | + * Specifies an item that is to be returned uniquely in the query result. |
|
| 320 | + * |
|
| 321 | + * <code> |
|
| 322 | + * $qb = $conn->getQueryBuilder() |
|
| 323 | + * ->selectDistinct('type') |
|
| 324 | + * ->from('users'); |
|
| 325 | + * </code> |
|
| 326 | + * |
|
| 327 | + * @param mixed $select The selection expressions. |
|
| 328 | + * |
|
| 329 | + * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance. |
|
| 330 | + * @since 9.0.0 |
|
| 331 | + */ |
|
| 332 | + public function selectDistinct($select); |
|
| 333 | + |
|
| 334 | + /** |
|
| 335 | + * Adds an item that is to be returned in the query result. |
|
| 336 | + * |
|
| 337 | + * <code> |
|
| 338 | + * $qb = $conn->getQueryBuilder() |
|
| 339 | + * ->select('u.id') |
|
| 340 | + * ->addSelect('p.id') |
|
| 341 | + * ->from('users', 'u') |
|
| 342 | + * ->leftJoin('u', 'phonenumbers', 'u.id = p.user_id'); |
|
| 343 | + * </code> |
|
| 344 | + * |
|
| 345 | + * @param mixed $select The selection expression. |
|
| 346 | + * |
|
| 347 | + * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance. |
|
| 348 | + * @since 8.2.0 |
|
| 349 | + */ |
|
| 350 | + public function addSelect($select = null); |
|
| 351 | + |
|
| 352 | + /** |
|
| 353 | + * Turns the query being built into a bulk delete query that ranges over |
|
| 354 | + * a certain table. |
|
| 355 | + * |
|
| 356 | + * <code> |
|
| 357 | + * $qb = $conn->getQueryBuilder() |
|
| 358 | + * ->delete('users', 'u') |
|
| 359 | + * ->where('u.id = :user_id'); |
|
| 360 | + * ->setParameter(':user_id', 1); |
|
| 361 | + * </code> |
|
| 362 | + * |
|
| 363 | + * @param string $delete The table whose rows are subject to the deletion. |
|
| 364 | + * @param string $alias The table alias used in the constructed query. |
|
| 365 | + * |
|
| 366 | + * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance. |
|
| 367 | + * @since 8.2.0 |
|
| 368 | + */ |
|
| 369 | + public function delete($delete = null, $alias = null); |
|
| 370 | + |
|
| 371 | + /** |
|
| 372 | + * Turns the query being built into a bulk update query that ranges over |
|
| 373 | + * a certain table |
|
| 374 | + * |
|
| 375 | + * <code> |
|
| 376 | + * $qb = $conn->getQueryBuilder() |
|
| 377 | + * ->update('users', 'u') |
|
| 378 | + * ->set('u.password', md5('password')) |
|
| 379 | + * ->where('u.id = ?'); |
|
| 380 | + * </code> |
|
| 381 | + * |
|
| 382 | + * @param string $update The table whose rows are subject to the update. |
|
| 383 | + * @param string $alias The table alias used in the constructed query. |
|
| 384 | + * |
|
| 385 | + * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance. |
|
| 386 | + * @since 8.2.0 |
|
| 387 | + */ |
|
| 388 | + public function update($update = null, $alias = null); |
|
| 389 | + |
|
| 390 | + /** |
|
| 391 | + * Turns the query being built into an insert query that inserts into |
|
| 392 | + * a certain table |
|
| 393 | + * |
|
| 394 | + * <code> |
|
| 395 | + * $qb = $conn->getQueryBuilder() |
|
| 396 | + * ->insert('users') |
|
| 397 | + * ->values( |
|
| 398 | + * array( |
|
| 399 | + * 'name' => '?', |
|
| 400 | + * 'password' => '?' |
|
| 401 | + * ) |
|
| 402 | + * ); |
|
| 403 | + * </code> |
|
| 404 | + * |
|
| 405 | + * @param string $insert The table into which the rows should be inserted. |
|
| 406 | + * |
|
| 407 | + * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance. |
|
| 408 | + * @since 8.2.0 |
|
| 409 | + */ |
|
| 410 | + public function insert($insert = null); |
|
| 411 | + |
|
| 412 | + /** |
|
| 413 | + * Creates and adds a query root corresponding to the table identified by the |
|
| 414 | + * given alias, forming a cartesian product with any existing query roots. |
|
| 415 | + * |
|
| 416 | + * <code> |
|
| 417 | + * $qb = $conn->getQueryBuilder() |
|
| 418 | + * ->select('u.id') |
|
| 419 | + * ->from('users', 'u') |
|
| 420 | + * </code> |
|
| 421 | + * |
|
| 422 | + * @param string $from The table. |
|
| 423 | + * @param string|null $alias The alias of the table. |
|
| 424 | + * |
|
| 425 | + * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance. |
|
| 426 | + * @since 8.2.0 |
|
| 427 | + */ |
|
| 428 | + public function from($from, $alias = null); |
|
| 429 | + |
|
| 430 | + /** |
|
| 431 | + * Creates and adds a join to the query. |
|
| 432 | + * |
|
| 433 | + * <code> |
|
| 434 | + * $qb = $conn->getQueryBuilder() |
|
| 435 | + * ->select('u.name') |
|
| 436 | + * ->from('users', 'u') |
|
| 437 | + * ->join('u', 'phonenumbers', 'p', 'p.is_primary = 1'); |
|
| 438 | + * </code> |
|
| 439 | + * |
|
| 440 | + * @param string $fromAlias The alias that points to a from clause. |
|
| 441 | + * @param string $join The table name to join. |
|
| 442 | + * @param string $alias The alias of the join table. |
|
| 443 | + * @param string $condition The condition for the join. |
|
| 444 | + * |
|
| 445 | + * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance. |
|
| 446 | + * @since 8.2.0 |
|
| 447 | + */ |
|
| 448 | + public function join($fromAlias, $join, $alias, $condition = null); |
|
| 449 | + |
|
| 450 | + /** |
|
| 451 | + * Creates and adds a join to the query. |
|
| 452 | + * |
|
| 453 | + * <code> |
|
| 454 | + * $qb = $conn->getQueryBuilder() |
|
| 455 | + * ->select('u.name') |
|
| 456 | + * ->from('users', 'u') |
|
| 457 | + * ->innerJoin('u', 'phonenumbers', 'p', 'p.is_primary = 1'); |
|
| 458 | + * </code> |
|
| 459 | + * |
|
| 460 | + * @param string $fromAlias The alias that points to a from clause. |
|
| 461 | + * @param string $join The table name to join. |
|
| 462 | + * @param string $alias The alias of the join table. |
|
| 463 | + * @param string $condition The condition for the join. |
|
| 464 | + * |
|
| 465 | + * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance. |
|
| 466 | + * @since 8.2.0 |
|
| 467 | + */ |
|
| 468 | + public function innerJoin($fromAlias, $join, $alias, $condition = null); |
|
| 469 | + |
|
| 470 | + /** |
|
| 471 | + * Creates and adds a left join to the query. |
|
| 472 | + * |
|
| 473 | + * <code> |
|
| 474 | + * $qb = $conn->getQueryBuilder() |
|
| 475 | + * ->select('u.name') |
|
| 476 | + * ->from('users', 'u') |
|
| 477 | + * ->leftJoin('u', 'phonenumbers', 'p', 'p.is_primary = 1'); |
|
| 478 | + * </code> |
|
| 479 | + * |
|
| 480 | + * @param string $fromAlias The alias that points to a from clause. |
|
| 481 | + * @param string $join The table name to join. |
|
| 482 | + * @param string $alias The alias of the join table. |
|
| 483 | + * @param string $condition The condition for the join. |
|
| 484 | + * |
|
| 485 | + * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance. |
|
| 486 | + * @since 8.2.0 |
|
| 487 | + */ |
|
| 488 | + public function leftJoin($fromAlias, $join, $alias, $condition = null); |
|
| 489 | + |
|
| 490 | + /** |
|
| 491 | + * Creates and adds a right join to the query. |
|
| 492 | + * |
|
| 493 | + * <code> |
|
| 494 | + * $qb = $conn->getQueryBuilder() |
|
| 495 | + * ->select('u.name') |
|
| 496 | + * ->from('users', 'u') |
|
| 497 | + * ->rightJoin('u', 'phonenumbers', 'p', 'p.is_primary = 1'); |
|
| 498 | + * </code> |
|
| 499 | + * |
|
| 500 | + * @param string $fromAlias The alias that points to a from clause. |
|
| 501 | + * @param string $join The table name to join. |
|
| 502 | + * @param string $alias The alias of the join table. |
|
| 503 | + * @param string $condition The condition for the join. |
|
| 504 | + * |
|
| 505 | + * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance. |
|
| 506 | + * @since 8.2.0 |
|
| 507 | + */ |
|
| 508 | + public function rightJoin($fromAlias, $join, $alias, $condition = null); |
|
| 509 | + |
|
| 510 | + /** |
|
| 511 | + * Sets a new value for a column in a bulk update query. |
|
| 512 | + * |
|
| 513 | + * <code> |
|
| 514 | + * $qb = $conn->getQueryBuilder() |
|
| 515 | + * ->update('users', 'u') |
|
| 516 | + * ->set('u.password', md5('password')) |
|
| 517 | + * ->where('u.id = ?'); |
|
| 518 | + * </code> |
|
| 519 | + * |
|
| 520 | + * @param string $key The column to set. |
|
| 521 | + * @param string $value The value, expression, placeholder, etc. |
|
| 522 | + * |
|
| 523 | + * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance. |
|
| 524 | + * @since 8.2.0 |
|
| 525 | + */ |
|
| 526 | + public function set($key, $value); |
|
| 527 | + |
|
| 528 | + /** |
|
| 529 | + * Specifies one or more restrictions to the query result. |
|
| 530 | + * Replaces any previously specified restrictions, if any. |
|
| 531 | + * |
|
| 532 | + * <code> |
|
| 533 | + * $qb = $conn->getQueryBuilder() |
|
| 534 | + * ->select('u.name') |
|
| 535 | + * ->from('users', 'u') |
|
| 536 | + * ->where('u.id = ?'); |
|
| 537 | + * |
|
| 538 | + * // You can optionally programatically build and/or expressions |
|
| 539 | + * $qb = $conn->getQueryBuilder(); |
|
| 540 | + * |
|
| 541 | + * $or = $qb->expr()->orx(); |
|
| 542 | + * $or->add($qb->expr()->eq('u.id', 1)); |
|
| 543 | + * $or->add($qb->expr()->eq('u.id', 2)); |
|
| 544 | + * |
|
| 545 | + * $qb->update('users', 'u') |
|
| 546 | + * ->set('u.password', md5('password')) |
|
| 547 | + * ->where($or); |
|
| 548 | + * </code> |
|
| 549 | + * |
|
| 550 | + * @param mixed $predicates The restriction predicates. |
|
| 551 | + * |
|
| 552 | + * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance. |
|
| 553 | + * @since 8.2.0 |
|
| 554 | + */ |
|
| 555 | + public function where($predicates); |
|
| 556 | + |
|
| 557 | + /** |
|
| 558 | + * Adds one or more restrictions to the query results, forming a logical |
|
| 559 | + * conjunction with any previously specified restrictions. |
|
| 560 | + * |
|
| 561 | + * <code> |
|
| 562 | + * $qb = $conn->getQueryBuilder() |
|
| 563 | + * ->select('u') |
|
| 564 | + * ->from('users', 'u') |
|
| 565 | + * ->where('u.username LIKE ?') |
|
| 566 | + * ->andWhere('u.is_active = 1'); |
|
| 567 | + * </code> |
|
| 568 | + * |
|
| 569 | + * @param mixed $where The query restrictions. |
|
| 570 | + * |
|
| 571 | + * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance. |
|
| 572 | + * |
|
| 573 | + * @see where() |
|
| 574 | + * @since 8.2.0 |
|
| 575 | + */ |
|
| 576 | + public function andWhere($where); |
|
| 577 | + |
|
| 578 | + /** |
|
| 579 | + * Adds one or more restrictions to the query results, forming a logical |
|
| 580 | + * disjunction with any previously specified restrictions. |
|
| 581 | + * |
|
| 582 | + * <code> |
|
| 583 | + * $qb = $conn->getQueryBuilder() |
|
| 584 | + * ->select('u.name') |
|
| 585 | + * ->from('users', 'u') |
|
| 586 | + * ->where('u.id = 1') |
|
| 587 | + * ->orWhere('u.id = 2'); |
|
| 588 | + * </code> |
|
| 589 | + * |
|
| 590 | + * @param mixed $where The WHERE statement. |
|
| 591 | + * |
|
| 592 | + * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance. |
|
| 593 | + * |
|
| 594 | + * @see where() |
|
| 595 | + * @since 8.2.0 |
|
| 596 | + */ |
|
| 597 | + public function orWhere($where); |
|
| 598 | + |
|
| 599 | + /** |
|
| 600 | + * Specifies a grouping over the results of the query. |
|
| 601 | + * Replaces any previously specified groupings, if any. |
|
| 602 | + * |
|
| 603 | + * <code> |
|
| 604 | + * $qb = $conn->getQueryBuilder() |
|
| 605 | + * ->select('u.name') |
|
| 606 | + * ->from('users', 'u') |
|
| 607 | + * ->groupBy('u.id'); |
|
| 608 | + * </code> |
|
| 609 | + * |
|
| 610 | + * @param mixed $groupBy The grouping expression. |
|
| 611 | + * |
|
| 612 | + * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance. |
|
| 613 | + * @since 8.2.0 |
|
| 614 | + */ |
|
| 615 | + public function groupBy($groupBy); |
|
| 616 | + |
|
| 617 | + /** |
|
| 618 | + * Adds a grouping expression to the query. |
|
| 619 | + * |
|
| 620 | + * <code> |
|
| 621 | + * $qb = $conn->getQueryBuilder() |
|
| 622 | + * ->select('u.name') |
|
| 623 | + * ->from('users', 'u') |
|
| 624 | + * ->groupBy('u.lastLogin'); |
|
| 625 | + * ->addGroupBy('u.createdAt') |
|
| 626 | + * </code> |
|
| 627 | + * |
|
| 628 | + * @param mixed $groupBy The grouping expression. |
|
| 629 | + * |
|
| 630 | + * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance. |
|
| 631 | + * @since 8.2.0 |
|
| 632 | + */ |
|
| 633 | + public function addGroupBy($groupBy); |
|
| 634 | + |
|
| 635 | + /** |
|
| 636 | + * Sets a value for a column in an insert query. |
|
| 637 | + * |
|
| 638 | + * <code> |
|
| 639 | + * $qb = $conn->getQueryBuilder() |
|
| 640 | + * ->insert('users') |
|
| 641 | + * ->values( |
|
| 642 | + * array( |
|
| 643 | + * 'name' => '?' |
|
| 644 | + * ) |
|
| 645 | + * ) |
|
| 646 | + * ->setValue('password', '?'); |
|
| 647 | + * </code> |
|
| 648 | + * |
|
| 649 | + * @param string $column The column into which the value should be inserted. |
|
| 650 | + * @param string $value The value that should be inserted into the column. |
|
| 651 | + * |
|
| 652 | + * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance. |
|
| 653 | + * @since 8.2.0 |
|
| 654 | + */ |
|
| 655 | + public function setValue($column, $value); |
|
| 656 | + |
|
| 657 | + /** |
|
| 658 | + * Specifies values for an insert query indexed by column names. |
|
| 659 | + * Replaces any previous values, if any. |
|
| 660 | + * |
|
| 661 | + * <code> |
|
| 662 | + * $qb = $conn->getQueryBuilder() |
|
| 663 | + * ->insert('users') |
|
| 664 | + * ->values( |
|
| 665 | + * array( |
|
| 666 | + * 'name' => '?', |
|
| 667 | + * 'password' => '?' |
|
| 668 | + * ) |
|
| 669 | + * ); |
|
| 670 | + * </code> |
|
| 671 | + * |
|
| 672 | + * @param array $values The values to specify for the insert query indexed by column names. |
|
| 673 | + * |
|
| 674 | + * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance. |
|
| 675 | + * @since 8.2.0 |
|
| 676 | + */ |
|
| 677 | + public function values(array $values); |
|
| 678 | + |
|
| 679 | + /** |
|
| 680 | + * Specifies a restriction over the groups of the query. |
|
| 681 | + * Replaces any previous having restrictions, if any. |
|
| 682 | + * |
|
| 683 | + * @param mixed $having The restriction over the groups. |
|
| 684 | + * |
|
| 685 | + * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance. |
|
| 686 | + * @since 8.2.0 |
|
| 687 | + */ |
|
| 688 | + public function having($having); |
|
| 689 | + |
|
| 690 | + /** |
|
| 691 | + * Adds a restriction over the groups of the query, forming a logical |
|
| 692 | + * conjunction with any existing having restrictions. |
|
| 693 | + * |
|
| 694 | + * @param mixed $having The restriction to append. |
|
| 695 | + * |
|
| 696 | + * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance. |
|
| 697 | + * @since 8.2.0 |
|
| 698 | + */ |
|
| 699 | + public function andHaving($having); |
|
| 700 | + |
|
| 701 | + /** |
|
| 702 | + * Adds a restriction over the groups of the query, forming a logical |
|
| 703 | + * disjunction with any existing having restrictions. |
|
| 704 | + * |
|
| 705 | + * @param mixed $having The restriction to add. |
|
| 706 | + * |
|
| 707 | + * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance. |
|
| 708 | + * @since 8.2.0 |
|
| 709 | + */ |
|
| 710 | + public function orHaving($having); |
|
| 711 | + |
|
| 712 | + /** |
|
| 713 | + * Specifies an ordering for the query results. |
|
| 714 | + * Replaces any previously specified orderings, if any. |
|
| 715 | + * |
|
| 716 | + * @param string $sort The ordering expression. |
|
| 717 | + * @param string $order The ordering direction. |
|
| 718 | + * |
|
| 719 | + * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance. |
|
| 720 | + * @since 8.2.0 |
|
| 721 | + */ |
|
| 722 | + public function orderBy($sort, $order = null); |
|
| 723 | + |
|
| 724 | + /** |
|
| 725 | + * Adds an ordering to the query results. |
|
| 726 | + * |
|
| 727 | + * @param string $sort The ordering expression. |
|
| 728 | + * @param string $order The ordering direction. |
|
| 729 | + * |
|
| 730 | + * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance. |
|
| 731 | + * @since 8.2.0 |
|
| 732 | + */ |
|
| 733 | + public function addOrderBy($sort, $order = null); |
|
| 734 | + |
|
| 735 | + /** |
|
| 736 | + * Gets a query part by its name. |
|
| 737 | + * |
|
| 738 | + * @param string $queryPartName |
|
| 739 | + * |
|
| 740 | + * @return mixed |
|
| 741 | + * @since 8.2.0 |
|
| 742 | + */ |
|
| 743 | + public function getQueryPart($queryPartName); |
|
| 744 | + |
|
| 745 | + /** |
|
| 746 | + * Gets all query parts. |
|
| 747 | + * |
|
| 748 | + * @return array |
|
| 749 | + * @since 8.2.0 |
|
| 750 | + */ |
|
| 751 | + public function getQueryParts(); |
|
| 752 | + |
|
| 753 | + /** |
|
| 754 | + * Resets SQL parts. |
|
| 755 | + * |
|
| 756 | + * @param array|null $queryPartNames |
|
| 757 | + * |
|
| 758 | + * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance. |
|
| 759 | + * @since 8.2.0 |
|
| 760 | + */ |
|
| 761 | + public function resetQueryParts($queryPartNames = null); |
|
| 762 | + |
|
| 763 | + /** |
|
| 764 | + * Resets a single SQL part. |
|
| 765 | + * |
|
| 766 | + * @param string $queryPartName |
|
| 767 | + * |
|
| 768 | + * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance. |
|
| 769 | + * @since 8.2.0 |
|
| 770 | + */ |
|
| 771 | + public function resetQueryPart($queryPartName); |
|
| 772 | + |
|
| 773 | + /** |
|
| 774 | + * Creates a new named parameter and bind the value $value to it. |
|
| 775 | + * |
|
| 776 | + * This method provides a shortcut for PDOStatement::bindValue |
|
| 777 | + * when using prepared statements. |
|
| 778 | + * |
|
| 779 | + * The parameter $value specifies the value that you want to bind. If |
|
| 780 | + * $placeholder is not provided bindValue() will automatically create a |
|
| 781 | + * placeholder for you. An automatic placeholder will be of the name |
|
| 782 | + * ':dcValue1', ':dcValue2' etc. |
|
| 783 | + * |
|
| 784 | + * For more information see {@link http://php.net/pdostatement-bindparam} |
|
| 785 | + * |
|
| 786 | + * Example: |
|
| 787 | + * <code> |
|
| 788 | + * $value = 2; |
|
| 789 | + * $q->eq( 'id', $q->bindValue( $value ) ); |
|
| 790 | + * $stmt = $q->executeQuery(); // executed with 'id = 2' |
|
| 791 | + * </code> |
|
| 792 | + * |
|
| 793 | + * @license New BSD License |
|
| 794 | + * @link http://www.zetacomponents.org |
|
| 795 | + * |
|
| 796 | + * @param mixed $value |
|
| 797 | + * @param mixed $type |
|
| 798 | + * @param string $placeHolder The name to bind with. The string must start with a colon ':'. |
|
| 799 | + * |
|
| 800 | + * @return IParameter |
|
| 801 | + * @since 8.2.0 |
|
| 802 | + */ |
|
| 803 | + public function createNamedParameter($value, $type = self::PARAM_STR, $placeHolder = null); |
|
| 804 | + |
|
| 805 | + /** |
|
| 806 | + * Creates a new positional parameter and bind the given value to it. |
|
| 807 | + * |
|
| 808 | + * Attention: If you are using positional parameters with the query builder you have |
|
| 809 | + * to be very careful to bind all parameters in the order they appear in the SQL |
|
| 810 | + * statement , otherwise they get bound in the wrong order which can lead to serious |
|
| 811 | + * bugs in your code. |
|
| 812 | + * |
|
| 813 | + * Example: |
|
| 814 | + * <code> |
|
| 815 | + * $qb = $conn->getQueryBuilder(); |
|
| 816 | + * $qb->select('u.*') |
|
| 817 | + * ->from('users', 'u') |
|
| 818 | + * ->where('u.username = ' . $qb->createPositionalParameter('Foo', IQueryBuilder::PARAM_STR)) |
|
| 819 | + * ->orWhere('u.username = ' . $qb->createPositionalParameter('Bar', IQueryBuilder::PARAM_STR)) |
|
| 820 | + * </code> |
|
| 821 | + * |
|
| 822 | + * @param mixed $value |
|
| 823 | + * @param integer $type |
|
| 824 | + * |
|
| 825 | + * @return IParameter |
|
| 826 | + * @since 8.2.0 |
|
| 827 | + */ |
|
| 828 | + public function createPositionalParameter($value, $type = self::PARAM_STR); |
|
| 829 | + |
|
| 830 | + /** |
|
| 831 | + * Creates a new parameter |
|
| 832 | + * |
|
| 833 | + * Example: |
|
| 834 | + * <code> |
|
| 835 | + * $qb = $conn->getQueryBuilder(); |
|
| 836 | + * $qb->select('u.*') |
|
| 837 | + * ->from('users', 'u') |
|
| 838 | + * ->where('u.username = ' . $qb->createParameter('name')) |
|
| 839 | + * ->setParameter('name', 'Bar', IQueryBuilder::PARAM_STR)) |
|
| 840 | + * </code> |
|
| 841 | + * |
|
| 842 | + * @param string $name |
|
| 843 | + * |
|
| 844 | + * @return IParameter |
|
| 845 | + * @since 8.2.0 |
|
| 846 | + */ |
|
| 847 | + public function createParameter($name); |
|
| 848 | + |
|
| 849 | + /** |
|
| 850 | + * Creates a new function |
|
| 851 | + * |
|
| 852 | + * Attention: Column names inside the call have to be quoted before hand |
|
| 853 | + * |
|
| 854 | + * Example: |
|
| 855 | + * <code> |
|
| 856 | + * $qb = $conn->getQueryBuilder(); |
|
| 857 | + * $qb->select($qb->createFunction('COUNT(*)')) |
|
| 858 | + * ->from('users', 'u') |
|
| 859 | + * echo $qb->getSQL(); // SELECT COUNT(*) FROM `users` u |
|
| 860 | + * </code> |
|
| 861 | + * <code> |
|
| 862 | + * $qb = $conn->getQueryBuilder(); |
|
| 863 | + * $qb->select($qb->createFunction('COUNT(`column`)')) |
|
| 864 | + * ->from('users', 'u') |
|
| 865 | + * echo $qb->getSQL(); // SELECT COUNT(`column`) FROM `users` u |
|
| 866 | + * </code> |
|
| 867 | + * |
|
| 868 | + * @param string $call |
|
| 869 | + * |
|
| 870 | + * @return IQueryFunction |
|
| 871 | + * @since 8.2.0 |
|
| 872 | + */ |
|
| 873 | + public function createFunction($call); |
|
| 874 | + |
|
| 875 | + /** |
|
| 876 | + * Used to get the id of the last inserted element |
|
| 877 | + * @return int |
|
| 878 | + * @throws \BadMethodCallException When being called before an insert query has been run. |
|
| 879 | + * @since 9.0.0 |
|
| 880 | + */ |
|
| 881 | + public function getLastInsertId(); |
|
| 882 | + |
|
| 883 | + /** |
|
| 884 | + * Returns the table name quoted and with database prefix as needed by the implementation |
|
| 885 | + * |
|
| 886 | + * @param string $table |
|
| 887 | + * @return string |
|
| 888 | + * @since 9.0.0 |
|
| 889 | + */ |
|
| 890 | + public function getTableName($table); |
|
| 891 | + |
|
| 892 | + /** |
|
| 893 | + * Returns the column name quoted and with table alias prefix as needed by the implementation |
|
| 894 | + * |
|
| 895 | + * @param string $column |
|
| 896 | + * @param string $tableAlias |
|
| 897 | + * @return string |
|
| 898 | + * @since 9.0.0 |
|
| 899 | + */ |
|
| 900 | + public function getColumnName($column, $tableAlias = ''); |
|
| 901 | 901 | } |
@@ -46,1151 +46,1151 @@ |
||
| 46 | 46 | |
| 47 | 47 | class QueryBuilder implements IQueryBuilder { |
| 48 | 48 | |
| 49 | - /** @var \OCP\IDBConnection */ |
|
| 50 | - private $connection; |
|
| 51 | - |
|
| 52 | - /** @var SystemConfig */ |
|
| 53 | - private $systemConfig; |
|
| 54 | - |
|
| 55 | - /** @var ILogger */ |
|
| 56 | - private $logger; |
|
| 57 | - |
|
| 58 | - /** @var \Doctrine\DBAL\Query\QueryBuilder */ |
|
| 59 | - private $queryBuilder; |
|
| 60 | - |
|
| 61 | - /** @var QuoteHelper */ |
|
| 62 | - private $helper; |
|
| 63 | - |
|
| 64 | - /** @var bool */ |
|
| 65 | - private $automaticTablePrefix = true; |
|
| 66 | - |
|
| 67 | - /** @var string */ |
|
| 68 | - protected $lastInsertedTable; |
|
| 69 | - |
|
| 70 | - /** |
|
| 71 | - * Initializes a new QueryBuilder. |
|
| 72 | - * |
|
| 73 | - * @param IDBConnection $connection |
|
| 74 | - * @param SystemConfig $systemConfig |
|
| 75 | - * @param ILogger $logger |
|
| 76 | - */ |
|
| 77 | - public function __construct(IDBConnection $connection, SystemConfig $systemConfig, ILogger $logger) { |
|
| 78 | - $this->connection = $connection; |
|
| 79 | - $this->systemConfig = $systemConfig; |
|
| 80 | - $this->logger = $logger; |
|
| 81 | - $this->queryBuilder = new \Doctrine\DBAL\Query\QueryBuilder($this->connection); |
|
| 82 | - $this->helper = new QuoteHelper(); |
|
| 83 | - } |
|
| 84 | - |
|
| 85 | - /** |
|
| 86 | - * Enable/disable automatic prefixing of table names with the oc_ prefix |
|
| 87 | - * |
|
| 88 | - * @param bool $enabled If set to true table names will be prefixed with the |
|
| 89 | - * owncloud database prefix automatically. |
|
| 90 | - * @since 8.2.0 |
|
| 91 | - */ |
|
| 92 | - public function automaticTablePrefix($enabled) { |
|
| 93 | - $this->automaticTablePrefix = (bool) $enabled; |
|
| 94 | - } |
|
| 95 | - |
|
| 96 | - /** |
|
| 97 | - * Gets an ExpressionBuilder used for object-oriented construction of query expressions. |
|
| 98 | - * This producer method is intended for convenient inline usage. Example: |
|
| 99 | - * |
|
| 100 | - * <code> |
|
| 101 | - * $qb = $conn->getQueryBuilder() |
|
| 102 | - * ->select('u') |
|
| 103 | - * ->from('users', 'u') |
|
| 104 | - * ->where($qb->expr()->eq('u.id', 1)); |
|
| 105 | - * </code> |
|
| 106 | - * |
|
| 107 | - * For more complex expression construction, consider storing the expression |
|
| 108 | - * builder object in a local variable. |
|
| 109 | - * |
|
| 110 | - * @return \OCP\DB\QueryBuilder\IExpressionBuilder |
|
| 111 | - */ |
|
| 112 | - public function expr() { |
|
| 113 | - if ($this->connection instanceof OracleConnection) { |
|
| 114 | - return new OCIExpressionBuilder($this->connection); |
|
| 115 | - } else if ($this->connection->getDatabasePlatform() instanceof PostgreSqlPlatform) { |
|
| 116 | - return new PgSqlExpressionBuilder($this->connection); |
|
| 117 | - } else if ($this->connection->getDatabasePlatform() instanceof MySqlPlatform) { |
|
| 118 | - return new MySqlExpressionBuilder($this->connection); |
|
| 119 | - } else if ($this->connection->getDatabasePlatform() instanceof SqlitePlatform) { |
|
| 120 | - return new SqliteExpressionBuilder($this->connection); |
|
| 121 | - } else { |
|
| 122 | - return new ExpressionBuilder($this->connection); |
|
| 123 | - } |
|
| 124 | - } |
|
| 125 | - |
|
| 126 | - /** |
|
| 127 | - * Gets an FunctionBuilder used for object-oriented construction of query functions. |
|
| 128 | - * This producer method is intended for convenient inline usage. Example: |
|
| 129 | - * |
|
| 130 | - * <code> |
|
| 131 | - * $qb = $conn->getQueryBuilder() |
|
| 132 | - * ->select('u') |
|
| 133 | - * ->from('users', 'u') |
|
| 134 | - * ->where($qb->fun()->md5('u.id')); |
|
| 135 | - * </code> |
|
| 136 | - * |
|
| 137 | - * For more complex function construction, consider storing the function |
|
| 138 | - * builder object in a local variable. |
|
| 139 | - * |
|
| 140 | - * @return \OCP\DB\QueryBuilder\IFunctionBuilder |
|
| 141 | - */ |
|
| 142 | - public function func() { |
|
| 143 | - if ($this->connection instanceof OracleConnection) { |
|
| 144 | - return new OCIFunctionBuilder($this->helper); |
|
| 145 | - } else if ($this->connection->getDatabasePlatform() instanceof SqlitePlatform) { |
|
| 146 | - return new SqliteFunctionBuilder($this->helper); |
|
| 147 | - } else if ($this->connection->getDatabasePlatform() instanceof PostgreSqlPlatform) { |
|
| 148 | - return new PgSqlFunctionBuilder($this->helper); |
|
| 149 | - } else { |
|
| 150 | - return new FunctionBuilder($this->helper); |
|
| 151 | - } |
|
| 152 | - } |
|
| 153 | - |
|
| 154 | - /** |
|
| 155 | - * Gets the type of the currently built query. |
|
| 156 | - * |
|
| 157 | - * @return integer |
|
| 158 | - */ |
|
| 159 | - public function getType() { |
|
| 160 | - return $this->queryBuilder->getType(); |
|
| 161 | - } |
|
| 162 | - |
|
| 163 | - /** |
|
| 164 | - * Gets the associated DBAL Connection for this query builder. |
|
| 165 | - * |
|
| 166 | - * @return \OCP\IDBConnection |
|
| 167 | - */ |
|
| 168 | - public function getConnection() { |
|
| 169 | - return $this->connection; |
|
| 170 | - } |
|
| 171 | - |
|
| 172 | - /** |
|
| 173 | - * Gets the state of this query builder instance. |
|
| 174 | - * |
|
| 175 | - * @return integer Either QueryBuilder::STATE_DIRTY or QueryBuilder::STATE_CLEAN. |
|
| 176 | - */ |
|
| 177 | - public function getState() { |
|
| 178 | - return $this->queryBuilder->getState(); |
|
| 179 | - } |
|
| 180 | - |
|
| 181 | - /** |
|
| 182 | - * Executes this query using the bound parameters and their types. |
|
| 183 | - * |
|
| 184 | - * Uses {@see Connection::executeQuery} for select statements and {@see Connection::executeUpdate} |
|
| 185 | - * for insert, update and delete statements. |
|
| 186 | - * |
|
| 187 | - * @return \Doctrine\DBAL\Driver\Statement|int |
|
| 188 | - */ |
|
| 189 | - public function execute() { |
|
| 190 | - if ($this->systemConfig->getValue('log_query', false)) { |
|
| 191 | - $params = []; |
|
| 192 | - foreach ($this->getParameters() as $placeholder => $value) { |
|
| 193 | - if (is_array($value)) { |
|
| 194 | - $params[] = $placeholder . ' => (\'' . implode('\', \'', $value) . '\')'; |
|
| 195 | - } else { |
|
| 196 | - $params[] = $placeholder . ' => \'' . $value . '\''; |
|
| 197 | - } |
|
| 198 | - } |
|
| 199 | - if (empty($params)) { |
|
| 200 | - $this->logger->debug('DB QueryBuilder: \'{query}\'', [ |
|
| 201 | - 'query' => $this->getSQL(), |
|
| 202 | - 'app' => 'core', |
|
| 203 | - ]); |
|
| 204 | - } else { |
|
| 205 | - $this->logger->debug('DB QueryBuilder: \'{query}\' with parameters: {params}', [ |
|
| 206 | - 'query' => $this->getSQL(), |
|
| 207 | - 'params' => implode(', ', $params), |
|
| 208 | - 'app' => 'core', |
|
| 209 | - ]); |
|
| 210 | - } |
|
| 211 | - } |
|
| 212 | - |
|
| 213 | - return $this->queryBuilder->execute(); |
|
| 214 | - } |
|
| 215 | - |
|
| 216 | - /** |
|
| 217 | - * Gets the complete SQL string formed by the current specifications of this QueryBuilder. |
|
| 218 | - * |
|
| 219 | - * <code> |
|
| 220 | - * $qb = $conn->getQueryBuilder() |
|
| 221 | - * ->select('u') |
|
| 222 | - * ->from('User', 'u') |
|
| 223 | - * echo $qb->getSQL(); // SELECT u FROM User u |
|
| 224 | - * </code> |
|
| 225 | - * |
|
| 226 | - * @return string The SQL query string. |
|
| 227 | - */ |
|
| 228 | - public function getSQL() { |
|
| 229 | - return $this->queryBuilder->getSQL(); |
|
| 230 | - } |
|
| 231 | - |
|
| 232 | - /** |
|
| 233 | - * Sets a query parameter for the query being constructed. |
|
| 234 | - * |
|
| 235 | - * <code> |
|
| 236 | - * $qb = $conn->getQueryBuilder() |
|
| 237 | - * ->select('u') |
|
| 238 | - * ->from('users', 'u') |
|
| 239 | - * ->where('u.id = :user_id') |
|
| 240 | - * ->setParameter(':user_id', 1); |
|
| 241 | - * </code> |
|
| 242 | - * |
|
| 243 | - * @param string|integer $key The parameter position or name. |
|
| 244 | - * @param mixed $value The parameter value. |
|
| 245 | - * @param string|null $type One of the IQueryBuilder::PARAM_* constants. |
|
| 246 | - * |
|
| 247 | - * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance. |
|
| 248 | - */ |
|
| 249 | - public function setParameter($key, $value, $type = null) { |
|
| 250 | - $this->queryBuilder->setParameter($key, $value, $type); |
|
| 251 | - |
|
| 252 | - return $this; |
|
| 253 | - } |
|
| 254 | - |
|
| 255 | - /** |
|
| 256 | - * Sets a collection of query parameters for the query being constructed. |
|
| 257 | - * |
|
| 258 | - * <code> |
|
| 259 | - * $qb = $conn->getQueryBuilder() |
|
| 260 | - * ->select('u') |
|
| 261 | - * ->from('users', 'u') |
|
| 262 | - * ->where('u.id = :user_id1 OR u.id = :user_id2') |
|
| 263 | - * ->setParameters(array( |
|
| 264 | - * ':user_id1' => 1, |
|
| 265 | - * ':user_id2' => 2 |
|
| 266 | - * )); |
|
| 267 | - * </code> |
|
| 268 | - * |
|
| 269 | - * @param array $params The query parameters to set. |
|
| 270 | - * @param array $types The query parameters types to set. |
|
| 271 | - * |
|
| 272 | - * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance. |
|
| 273 | - */ |
|
| 274 | - public function setParameters(array $params, array $types = array()) { |
|
| 275 | - $this->queryBuilder->setParameters($params, $types); |
|
| 276 | - |
|
| 277 | - return $this; |
|
| 278 | - } |
|
| 279 | - |
|
| 280 | - /** |
|
| 281 | - * Gets all defined query parameters for the query being constructed indexed by parameter index or name. |
|
| 282 | - * |
|
| 283 | - * @return array The currently defined query parameters indexed by parameter index or name. |
|
| 284 | - */ |
|
| 285 | - public function getParameters() { |
|
| 286 | - return $this->queryBuilder->getParameters(); |
|
| 287 | - } |
|
| 288 | - |
|
| 289 | - /** |
|
| 290 | - * Gets a (previously set) query parameter of the query being constructed. |
|
| 291 | - * |
|
| 292 | - * @param mixed $key The key (index or name) of the bound parameter. |
|
| 293 | - * |
|
| 294 | - * @return mixed The value of the bound parameter. |
|
| 295 | - */ |
|
| 296 | - public function getParameter($key) { |
|
| 297 | - return $this->queryBuilder->getParameter($key); |
|
| 298 | - } |
|
| 299 | - |
|
| 300 | - /** |
|
| 301 | - * Gets all defined query parameter types for the query being constructed indexed by parameter index or name. |
|
| 302 | - * |
|
| 303 | - * @return array The currently defined query parameter types indexed by parameter index or name. |
|
| 304 | - */ |
|
| 305 | - public function getParameterTypes() { |
|
| 306 | - return $this->queryBuilder->getParameterTypes(); |
|
| 307 | - } |
|
| 308 | - |
|
| 309 | - /** |
|
| 310 | - * Gets a (previously set) query parameter type of the query being constructed. |
|
| 311 | - * |
|
| 312 | - * @param mixed $key The key (index or name) of the bound parameter type. |
|
| 313 | - * |
|
| 314 | - * @return mixed The value of the bound parameter type. |
|
| 315 | - */ |
|
| 316 | - public function getParameterType($key) { |
|
| 317 | - return $this->queryBuilder->getParameterType($key); |
|
| 318 | - } |
|
| 319 | - |
|
| 320 | - /** |
|
| 321 | - * Sets the position of the first result to retrieve (the "offset"). |
|
| 322 | - * |
|
| 323 | - * @param integer $firstResult The first result to return. |
|
| 324 | - * |
|
| 325 | - * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance. |
|
| 326 | - */ |
|
| 327 | - public function setFirstResult($firstResult) { |
|
| 328 | - $this->queryBuilder->setFirstResult($firstResult); |
|
| 329 | - |
|
| 330 | - return $this; |
|
| 331 | - } |
|
| 332 | - |
|
| 333 | - /** |
|
| 334 | - * Gets the position of the first result the query object was set to retrieve (the "offset"). |
|
| 335 | - * Returns NULL if {@link setFirstResult} was not applied to this QueryBuilder. |
|
| 336 | - * |
|
| 337 | - * @return integer The position of the first result. |
|
| 338 | - */ |
|
| 339 | - public function getFirstResult() { |
|
| 340 | - return $this->queryBuilder->getFirstResult(); |
|
| 341 | - } |
|
| 342 | - |
|
| 343 | - /** |
|
| 344 | - * Sets the maximum number of results to retrieve (the "limit"). |
|
| 345 | - * |
|
| 346 | - * NOTE: Setting max results to "0" will cause mixed behaviour. While most |
|
| 347 | - * of the databases will just return an empty result set, Oracle will return |
|
| 348 | - * all entries. |
|
| 349 | - * |
|
| 350 | - * @param integer $maxResults The maximum number of results to retrieve. |
|
| 351 | - * |
|
| 352 | - * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance. |
|
| 353 | - */ |
|
| 354 | - public function setMaxResults($maxResults) { |
|
| 355 | - $this->queryBuilder->setMaxResults($maxResults); |
|
| 356 | - |
|
| 357 | - return $this; |
|
| 358 | - } |
|
| 359 | - |
|
| 360 | - /** |
|
| 361 | - * Gets the maximum number of results the query object was set to retrieve (the "limit"). |
|
| 362 | - * Returns NULL if {@link setMaxResults} was not applied to this query builder. |
|
| 363 | - * |
|
| 364 | - * @return integer The maximum number of results. |
|
| 365 | - */ |
|
| 366 | - public function getMaxResults() { |
|
| 367 | - return $this->queryBuilder->getMaxResults(); |
|
| 368 | - } |
|
| 369 | - |
|
| 370 | - /** |
|
| 371 | - * Specifies an item that is to be returned in the query result. |
|
| 372 | - * Replaces any previously specified selections, if any. |
|
| 373 | - * |
|
| 374 | - * <code> |
|
| 375 | - * $qb = $conn->getQueryBuilder() |
|
| 376 | - * ->select('u.id', 'p.id') |
|
| 377 | - * ->from('users', 'u') |
|
| 378 | - * ->leftJoin('u', 'phonenumbers', 'p', 'u.id = p.user_id'); |
|
| 379 | - * </code> |
|
| 380 | - * |
|
| 381 | - * @param mixed $select The selection expressions. |
|
| 382 | - * |
|
| 383 | - * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance. |
|
| 384 | - */ |
|
| 385 | - public function select($select = null) { |
|
| 386 | - $selects = is_array($select) ? $select : func_get_args(); |
|
| 387 | - |
|
| 388 | - $this->queryBuilder->select( |
|
| 389 | - $this->helper->quoteColumnNames($selects) |
|
| 390 | - ); |
|
| 391 | - |
|
| 392 | - return $this; |
|
| 393 | - } |
|
| 394 | - |
|
| 395 | - /** |
|
| 396 | - * Specifies an item that is to be returned with a different name in the query result. |
|
| 397 | - * |
|
| 398 | - * <code> |
|
| 399 | - * $qb = $conn->getQueryBuilder() |
|
| 400 | - * ->selectAlias('u.id', 'user_id') |
|
| 401 | - * ->from('users', 'u') |
|
| 402 | - * ->leftJoin('u', 'phonenumbers', 'p', 'u.id = p.user_id'); |
|
| 403 | - * </code> |
|
| 404 | - * |
|
| 405 | - * @param mixed $select The selection expressions. |
|
| 406 | - * @param string $alias The column alias used in the constructed query. |
|
| 407 | - * |
|
| 408 | - * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance. |
|
| 409 | - */ |
|
| 410 | - public function selectAlias($select, $alias) { |
|
| 411 | - |
|
| 412 | - $this->queryBuilder->addSelect( |
|
| 413 | - $this->helper->quoteColumnName($select) . ' AS ' . $this->helper->quoteColumnName($alias) |
|
| 414 | - ); |
|
| 415 | - |
|
| 416 | - return $this; |
|
| 417 | - } |
|
| 418 | - |
|
| 419 | - /** |
|
| 420 | - * Specifies an item that is to be returned uniquely in the query result. |
|
| 421 | - * |
|
| 422 | - * <code> |
|
| 423 | - * $qb = $conn->getQueryBuilder() |
|
| 424 | - * ->selectDistinct('type') |
|
| 425 | - * ->from('users'); |
|
| 426 | - * </code> |
|
| 427 | - * |
|
| 428 | - * @param mixed $select The selection expressions. |
|
| 429 | - * |
|
| 430 | - * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance. |
|
| 431 | - */ |
|
| 432 | - public function selectDistinct($select) { |
|
| 433 | - |
|
| 434 | - $this->queryBuilder->addSelect( |
|
| 435 | - 'DISTINCT ' . $this->helper->quoteColumnName($select) |
|
| 436 | - ); |
|
| 437 | - |
|
| 438 | - return $this; |
|
| 439 | - } |
|
| 440 | - |
|
| 441 | - /** |
|
| 442 | - * Adds an item that is to be returned in the query result. |
|
| 443 | - * |
|
| 444 | - * <code> |
|
| 445 | - * $qb = $conn->getQueryBuilder() |
|
| 446 | - * ->select('u.id') |
|
| 447 | - * ->addSelect('p.id') |
|
| 448 | - * ->from('users', 'u') |
|
| 449 | - * ->leftJoin('u', 'phonenumbers', 'u.id = p.user_id'); |
|
| 450 | - * </code> |
|
| 451 | - * |
|
| 452 | - * @param mixed $select The selection expression. |
|
| 453 | - * |
|
| 454 | - * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance. |
|
| 455 | - */ |
|
| 456 | - public function addSelect($select = null) { |
|
| 457 | - $selects = is_array($select) ? $select : func_get_args(); |
|
| 458 | - |
|
| 459 | - $this->queryBuilder->addSelect( |
|
| 460 | - $this->helper->quoteColumnNames($selects) |
|
| 461 | - ); |
|
| 462 | - |
|
| 463 | - return $this; |
|
| 464 | - } |
|
| 465 | - |
|
| 466 | - /** |
|
| 467 | - * Turns the query being built into a bulk delete query that ranges over |
|
| 468 | - * a certain table. |
|
| 469 | - * |
|
| 470 | - * <code> |
|
| 471 | - * $qb = $conn->getQueryBuilder() |
|
| 472 | - * ->delete('users', 'u') |
|
| 473 | - * ->where('u.id = :user_id'); |
|
| 474 | - * ->setParameter(':user_id', 1); |
|
| 475 | - * </code> |
|
| 476 | - * |
|
| 477 | - * @param string $delete The table whose rows are subject to the deletion. |
|
| 478 | - * @param string $alias The table alias used in the constructed query. |
|
| 479 | - * |
|
| 480 | - * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance. |
|
| 481 | - */ |
|
| 482 | - public function delete($delete = null, $alias = null) { |
|
| 483 | - $this->queryBuilder->delete( |
|
| 484 | - $this->getTableName($delete), |
|
| 485 | - $alias |
|
| 486 | - ); |
|
| 487 | - |
|
| 488 | - return $this; |
|
| 489 | - } |
|
| 490 | - |
|
| 491 | - /** |
|
| 492 | - * Turns the query being built into a bulk update query that ranges over |
|
| 493 | - * a certain table |
|
| 494 | - * |
|
| 495 | - * <code> |
|
| 496 | - * $qb = $conn->getQueryBuilder() |
|
| 497 | - * ->update('users', 'u') |
|
| 498 | - * ->set('u.password', md5('password')) |
|
| 499 | - * ->where('u.id = ?'); |
|
| 500 | - * </code> |
|
| 501 | - * |
|
| 502 | - * @param string $update The table whose rows are subject to the update. |
|
| 503 | - * @param string $alias The table alias used in the constructed query. |
|
| 504 | - * |
|
| 505 | - * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance. |
|
| 506 | - */ |
|
| 507 | - public function update($update = null, $alias = null) { |
|
| 508 | - $this->queryBuilder->update( |
|
| 509 | - $this->getTableName($update), |
|
| 510 | - $alias |
|
| 511 | - ); |
|
| 512 | - |
|
| 513 | - return $this; |
|
| 514 | - } |
|
| 515 | - |
|
| 516 | - /** |
|
| 517 | - * Turns the query being built into an insert query that inserts into |
|
| 518 | - * a certain table |
|
| 519 | - * |
|
| 520 | - * <code> |
|
| 521 | - * $qb = $conn->getQueryBuilder() |
|
| 522 | - * ->insert('users') |
|
| 523 | - * ->values( |
|
| 524 | - * array( |
|
| 525 | - * 'name' => '?', |
|
| 526 | - * 'password' => '?' |
|
| 527 | - * ) |
|
| 528 | - * ); |
|
| 529 | - * </code> |
|
| 530 | - * |
|
| 531 | - * @param string $insert The table into which the rows should be inserted. |
|
| 532 | - * |
|
| 533 | - * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance. |
|
| 534 | - */ |
|
| 535 | - public function insert($insert = null) { |
|
| 536 | - $this->queryBuilder->insert( |
|
| 537 | - $this->getTableName($insert) |
|
| 538 | - ); |
|
| 539 | - |
|
| 540 | - $this->lastInsertedTable = $insert; |
|
| 541 | - |
|
| 542 | - return $this; |
|
| 543 | - } |
|
| 544 | - |
|
| 545 | - /** |
|
| 546 | - * Creates and adds a query root corresponding to the table identified by the |
|
| 547 | - * given alias, forming a cartesian product with any existing query roots. |
|
| 548 | - * |
|
| 549 | - * <code> |
|
| 550 | - * $qb = $conn->getQueryBuilder() |
|
| 551 | - * ->select('u.id') |
|
| 552 | - * ->from('users', 'u') |
|
| 553 | - * </code> |
|
| 554 | - * |
|
| 555 | - * @param string $from The table. |
|
| 556 | - * @param string|null $alias The alias of the table. |
|
| 557 | - * |
|
| 558 | - * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance. |
|
| 559 | - */ |
|
| 560 | - public function from($from, $alias = null) { |
|
| 561 | - $this->queryBuilder->from( |
|
| 562 | - $this->getTableName($from), |
|
| 563 | - $alias |
|
| 564 | - ); |
|
| 565 | - |
|
| 566 | - return $this; |
|
| 567 | - } |
|
| 568 | - |
|
| 569 | - /** |
|
| 570 | - * Creates and adds a join to the query. |
|
| 571 | - * |
|
| 572 | - * <code> |
|
| 573 | - * $qb = $conn->getQueryBuilder() |
|
| 574 | - * ->select('u.name') |
|
| 575 | - * ->from('users', 'u') |
|
| 576 | - * ->join('u', 'phonenumbers', 'p', 'p.is_primary = 1'); |
|
| 577 | - * </code> |
|
| 578 | - * |
|
| 579 | - * @param string $fromAlias The alias that points to a from clause. |
|
| 580 | - * @param string $join The table name to join. |
|
| 581 | - * @param string $alias The alias of the join table. |
|
| 582 | - * @param string $condition The condition for the join. |
|
| 583 | - * |
|
| 584 | - * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance. |
|
| 585 | - */ |
|
| 586 | - public function join($fromAlias, $join, $alias, $condition = null) { |
|
| 587 | - $this->queryBuilder->join( |
|
| 588 | - $fromAlias, |
|
| 589 | - $this->getTableName($join), |
|
| 590 | - $alias, |
|
| 591 | - $condition |
|
| 592 | - ); |
|
| 593 | - |
|
| 594 | - return $this; |
|
| 595 | - } |
|
| 596 | - |
|
| 597 | - /** |
|
| 598 | - * Creates and adds a join to the query. |
|
| 599 | - * |
|
| 600 | - * <code> |
|
| 601 | - * $qb = $conn->getQueryBuilder() |
|
| 602 | - * ->select('u.name') |
|
| 603 | - * ->from('users', 'u') |
|
| 604 | - * ->innerJoin('u', 'phonenumbers', 'p', 'p.is_primary = 1'); |
|
| 605 | - * </code> |
|
| 606 | - * |
|
| 607 | - * @param string $fromAlias The alias that points to a from clause. |
|
| 608 | - * @param string $join The table name to join. |
|
| 609 | - * @param string $alias The alias of the join table. |
|
| 610 | - * @param string $condition The condition for the join. |
|
| 611 | - * |
|
| 612 | - * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance. |
|
| 613 | - */ |
|
| 614 | - public function innerJoin($fromAlias, $join, $alias, $condition = null) { |
|
| 615 | - $this->queryBuilder->innerJoin( |
|
| 616 | - $fromAlias, |
|
| 617 | - $this->getTableName($join), |
|
| 618 | - $alias, |
|
| 619 | - $condition |
|
| 620 | - ); |
|
| 621 | - |
|
| 622 | - return $this; |
|
| 623 | - } |
|
| 624 | - |
|
| 625 | - /** |
|
| 626 | - * Creates and adds a left join to the query. |
|
| 627 | - * |
|
| 628 | - * <code> |
|
| 629 | - * $qb = $conn->getQueryBuilder() |
|
| 630 | - * ->select('u.name') |
|
| 631 | - * ->from('users', 'u') |
|
| 632 | - * ->leftJoin('u', 'phonenumbers', 'p', 'p.is_primary = 1'); |
|
| 633 | - * </code> |
|
| 634 | - * |
|
| 635 | - * @param string $fromAlias The alias that points to a from clause. |
|
| 636 | - * @param string $join The table name to join. |
|
| 637 | - * @param string $alias The alias of the join table. |
|
| 638 | - * @param string $condition The condition for the join. |
|
| 639 | - * |
|
| 640 | - * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance. |
|
| 641 | - */ |
|
| 642 | - public function leftJoin($fromAlias, $join, $alias, $condition = null) { |
|
| 643 | - $this->queryBuilder->leftJoin( |
|
| 644 | - $fromAlias, |
|
| 645 | - $this->getTableName($join), |
|
| 646 | - $alias, |
|
| 647 | - $condition |
|
| 648 | - ); |
|
| 649 | - |
|
| 650 | - return $this; |
|
| 651 | - } |
|
| 652 | - |
|
| 653 | - /** |
|
| 654 | - * Creates and adds a right join to the query. |
|
| 655 | - * |
|
| 656 | - * <code> |
|
| 657 | - * $qb = $conn->getQueryBuilder() |
|
| 658 | - * ->select('u.name') |
|
| 659 | - * ->from('users', 'u') |
|
| 660 | - * ->rightJoin('u', 'phonenumbers', 'p', 'p.is_primary = 1'); |
|
| 661 | - * </code> |
|
| 662 | - * |
|
| 663 | - * @param string $fromAlias The alias that points to a from clause. |
|
| 664 | - * @param string $join The table name to join. |
|
| 665 | - * @param string $alias The alias of the join table. |
|
| 666 | - * @param string $condition The condition for the join. |
|
| 667 | - * |
|
| 668 | - * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance. |
|
| 669 | - */ |
|
| 670 | - public function rightJoin($fromAlias, $join, $alias, $condition = null) { |
|
| 671 | - $this->queryBuilder->rightJoin( |
|
| 672 | - $fromAlias, |
|
| 673 | - $this->getTableName($join), |
|
| 674 | - $alias, |
|
| 675 | - $condition |
|
| 676 | - ); |
|
| 677 | - |
|
| 678 | - return $this; |
|
| 679 | - } |
|
| 680 | - |
|
| 681 | - /** |
|
| 682 | - * Sets a new value for a column in a bulk update query. |
|
| 683 | - * |
|
| 684 | - * <code> |
|
| 685 | - * $qb = $conn->getQueryBuilder() |
|
| 686 | - * ->update('users', 'u') |
|
| 687 | - * ->set('u.password', md5('password')) |
|
| 688 | - * ->where('u.id = ?'); |
|
| 689 | - * </code> |
|
| 690 | - * |
|
| 691 | - * @param string $key The column to set. |
|
| 692 | - * @param string $value The value, expression, placeholder, etc. |
|
| 693 | - * |
|
| 694 | - * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance. |
|
| 695 | - */ |
|
| 696 | - public function set($key, $value) { |
|
| 697 | - $this->queryBuilder->set( |
|
| 698 | - $this->helper->quoteColumnName($key), |
|
| 699 | - $this->helper->quoteColumnName($value) |
|
| 700 | - ); |
|
| 701 | - |
|
| 702 | - return $this; |
|
| 703 | - } |
|
| 704 | - |
|
| 705 | - /** |
|
| 706 | - * Specifies one or more restrictions to the query result. |
|
| 707 | - * Replaces any previously specified restrictions, if any. |
|
| 708 | - * |
|
| 709 | - * <code> |
|
| 710 | - * $qb = $conn->getQueryBuilder() |
|
| 711 | - * ->select('u.name') |
|
| 712 | - * ->from('users', 'u') |
|
| 713 | - * ->where('u.id = ?'); |
|
| 714 | - * |
|
| 715 | - * // You can optionally programatically build and/or expressions |
|
| 716 | - * $qb = $conn->getQueryBuilder(); |
|
| 717 | - * |
|
| 718 | - * $or = $qb->expr()->orx(); |
|
| 719 | - * $or->add($qb->expr()->eq('u.id', 1)); |
|
| 720 | - * $or->add($qb->expr()->eq('u.id', 2)); |
|
| 721 | - * |
|
| 722 | - * $qb->update('users', 'u') |
|
| 723 | - * ->set('u.password', md5('password')) |
|
| 724 | - * ->where($or); |
|
| 725 | - * </code> |
|
| 726 | - * |
|
| 727 | - * @param mixed $predicates The restriction predicates. |
|
| 728 | - * |
|
| 729 | - * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance. |
|
| 730 | - */ |
|
| 731 | - public function where($predicates) { |
|
| 732 | - call_user_func_array( |
|
| 733 | - [$this->queryBuilder, 'where'], |
|
| 734 | - func_get_args() |
|
| 735 | - ); |
|
| 736 | - |
|
| 737 | - return $this; |
|
| 738 | - } |
|
| 739 | - |
|
| 740 | - /** |
|
| 741 | - * Adds one or more restrictions to the query results, forming a logical |
|
| 742 | - * conjunction with any previously specified restrictions. |
|
| 743 | - * |
|
| 744 | - * <code> |
|
| 745 | - * $qb = $conn->getQueryBuilder() |
|
| 746 | - * ->select('u') |
|
| 747 | - * ->from('users', 'u') |
|
| 748 | - * ->where('u.username LIKE ?') |
|
| 749 | - * ->andWhere('u.is_active = 1'); |
|
| 750 | - * </code> |
|
| 751 | - * |
|
| 752 | - * @param mixed $where The query restrictions. |
|
| 753 | - * |
|
| 754 | - * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance. |
|
| 755 | - * |
|
| 756 | - * @see where() |
|
| 757 | - */ |
|
| 758 | - public function andWhere($where) { |
|
| 759 | - call_user_func_array( |
|
| 760 | - [$this->queryBuilder, 'andWhere'], |
|
| 761 | - func_get_args() |
|
| 762 | - ); |
|
| 763 | - |
|
| 764 | - return $this; |
|
| 765 | - } |
|
| 766 | - |
|
| 767 | - /** |
|
| 768 | - * Adds one or more restrictions to the query results, forming a logical |
|
| 769 | - * disjunction with any previously specified restrictions. |
|
| 770 | - * |
|
| 771 | - * <code> |
|
| 772 | - * $qb = $conn->getQueryBuilder() |
|
| 773 | - * ->select('u.name') |
|
| 774 | - * ->from('users', 'u') |
|
| 775 | - * ->where('u.id = 1') |
|
| 776 | - * ->orWhere('u.id = 2'); |
|
| 777 | - * </code> |
|
| 778 | - * |
|
| 779 | - * @param mixed $where The WHERE statement. |
|
| 780 | - * |
|
| 781 | - * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance. |
|
| 782 | - * |
|
| 783 | - * @see where() |
|
| 784 | - */ |
|
| 785 | - public function orWhere($where) { |
|
| 786 | - call_user_func_array( |
|
| 787 | - [$this->queryBuilder, 'orWhere'], |
|
| 788 | - func_get_args() |
|
| 789 | - ); |
|
| 790 | - |
|
| 791 | - return $this; |
|
| 792 | - } |
|
| 793 | - |
|
| 794 | - /** |
|
| 795 | - * Specifies a grouping over the results of the query. |
|
| 796 | - * Replaces any previously specified groupings, if any. |
|
| 797 | - * |
|
| 798 | - * <code> |
|
| 799 | - * $qb = $conn->getQueryBuilder() |
|
| 800 | - * ->select('u.name') |
|
| 801 | - * ->from('users', 'u') |
|
| 802 | - * ->groupBy('u.id'); |
|
| 803 | - * </code> |
|
| 804 | - * |
|
| 805 | - * @param mixed $groupBy The grouping expression. |
|
| 806 | - * |
|
| 807 | - * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance. |
|
| 808 | - */ |
|
| 809 | - public function groupBy($groupBy) { |
|
| 810 | - $groupBys = is_array($groupBy) ? $groupBy : func_get_args(); |
|
| 811 | - |
|
| 812 | - call_user_func_array( |
|
| 813 | - [$this->queryBuilder, 'groupBy'], |
|
| 814 | - $this->helper->quoteColumnNames($groupBys) |
|
| 815 | - ); |
|
| 816 | - |
|
| 817 | - return $this; |
|
| 818 | - } |
|
| 819 | - |
|
| 820 | - /** |
|
| 821 | - * Adds a grouping expression to the query. |
|
| 822 | - * |
|
| 823 | - * <code> |
|
| 824 | - * $qb = $conn->getQueryBuilder() |
|
| 825 | - * ->select('u.name') |
|
| 826 | - * ->from('users', 'u') |
|
| 827 | - * ->groupBy('u.lastLogin'); |
|
| 828 | - * ->addGroupBy('u.createdAt') |
|
| 829 | - * </code> |
|
| 830 | - * |
|
| 831 | - * @param mixed $groupBy The grouping expression. |
|
| 832 | - * |
|
| 833 | - * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance. |
|
| 834 | - */ |
|
| 835 | - public function addGroupBy($groupBy) { |
|
| 836 | - $groupBys = is_array($groupBy) ? $groupBy : func_get_args(); |
|
| 837 | - |
|
| 838 | - call_user_func_array( |
|
| 839 | - [$this->queryBuilder, 'addGroupBy'], |
|
| 840 | - $this->helper->quoteColumnNames($groupBys) |
|
| 841 | - ); |
|
| 842 | - |
|
| 843 | - return $this; |
|
| 844 | - } |
|
| 845 | - |
|
| 846 | - /** |
|
| 847 | - * Sets a value for a column in an insert query. |
|
| 848 | - * |
|
| 849 | - * <code> |
|
| 850 | - * $qb = $conn->getQueryBuilder() |
|
| 851 | - * ->insert('users') |
|
| 852 | - * ->values( |
|
| 853 | - * array( |
|
| 854 | - * 'name' => '?' |
|
| 855 | - * ) |
|
| 856 | - * ) |
|
| 857 | - * ->setValue('password', '?'); |
|
| 858 | - * </code> |
|
| 859 | - * |
|
| 860 | - * @param string $column The column into which the value should be inserted. |
|
| 861 | - * @param string $value The value that should be inserted into the column. |
|
| 862 | - * |
|
| 863 | - * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance. |
|
| 864 | - */ |
|
| 865 | - public function setValue($column, $value) { |
|
| 866 | - $this->queryBuilder->setValue( |
|
| 867 | - $this->helper->quoteColumnName($column), |
|
| 868 | - $value |
|
| 869 | - ); |
|
| 870 | - |
|
| 871 | - return $this; |
|
| 872 | - } |
|
| 873 | - |
|
| 874 | - /** |
|
| 875 | - * Specifies values for an insert query indexed by column names. |
|
| 876 | - * Replaces any previous values, if any. |
|
| 877 | - * |
|
| 878 | - * <code> |
|
| 879 | - * $qb = $conn->getQueryBuilder() |
|
| 880 | - * ->insert('users') |
|
| 881 | - * ->values( |
|
| 882 | - * array( |
|
| 883 | - * 'name' => '?', |
|
| 884 | - * 'password' => '?' |
|
| 885 | - * ) |
|
| 886 | - * ); |
|
| 887 | - * </code> |
|
| 888 | - * |
|
| 889 | - * @param array $values The values to specify for the insert query indexed by column names. |
|
| 890 | - * |
|
| 891 | - * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance. |
|
| 892 | - */ |
|
| 893 | - public function values(array $values) { |
|
| 894 | - $quotedValues = []; |
|
| 895 | - foreach ($values as $key => $value) { |
|
| 896 | - $quotedValues[$this->helper->quoteColumnName($key)] = $value; |
|
| 897 | - } |
|
| 898 | - |
|
| 899 | - $this->queryBuilder->values($quotedValues); |
|
| 900 | - |
|
| 901 | - return $this; |
|
| 902 | - } |
|
| 903 | - |
|
| 904 | - /** |
|
| 905 | - * Specifies a restriction over the groups of the query. |
|
| 906 | - * Replaces any previous having restrictions, if any. |
|
| 907 | - * |
|
| 908 | - * @param mixed $having The restriction over the groups. |
|
| 909 | - * |
|
| 910 | - * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance. |
|
| 911 | - */ |
|
| 912 | - public function having($having) { |
|
| 913 | - call_user_func_array( |
|
| 914 | - [$this->queryBuilder, 'having'], |
|
| 915 | - func_get_args() |
|
| 916 | - ); |
|
| 917 | - |
|
| 918 | - return $this; |
|
| 919 | - } |
|
| 920 | - |
|
| 921 | - /** |
|
| 922 | - * Adds a restriction over the groups of the query, forming a logical |
|
| 923 | - * conjunction with any existing having restrictions. |
|
| 924 | - * |
|
| 925 | - * @param mixed $having The restriction to append. |
|
| 926 | - * |
|
| 927 | - * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance. |
|
| 928 | - */ |
|
| 929 | - public function andHaving($having) { |
|
| 930 | - call_user_func_array( |
|
| 931 | - [$this->queryBuilder, 'andHaving'], |
|
| 932 | - func_get_args() |
|
| 933 | - ); |
|
| 934 | - |
|
| 935 | - return $this; |
|
| 936 | - } |
|
| 937 | - |
|
| 938 | - /** |
|
| 939 | - * Adds a restriction over the groups of the query, forming a logical |
|
| 940 | - * disjunction with any existing having restrictions. |
|
| 941 | - * |
|
| 942 | - * @param mixed $having The restriction to add. |
|
| 943 | - * |
|
| 944 | - * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance. |
|
| 945 | - */ |
|
| 946 | - public function orHaving($having) { |
|
| 947 | - call_user_func_array( |
|
| 948 | - [$this->queryBuilder, 'orHaving'], |
|
| 949 | - func_get_args() |
|
| 950 | - ); |
|
| 951 | - |
|
| 952 | - return $this; |
|
| 953 | - } |
|
| 954 | - |
|
| 955 | - /** |
|
| 956 | - * Specifies an ordering for the query results. |
|
| 957 | - * Replaces any previously specified orderings, if any. |
|
| 958 | - * |
|
| 959 | - * @param string $sort The ordering expression. |
|
| 960 | - * @param string $order The ordering direction. |
|
| 961 | - * |
|
| 962 | - * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance. |
|
| 963 | - */ |
|
| 964 | - public function orderBy($sort, $order = null) { |
|
| 965 | - $this->queryBuilder->orderBy( |
|
| 966 | - $this->helper->quoteColumnName($sort), |
|
| 967 | - $order |
|
| 968 | - ); |
|
| 969 | - |
|
| 970 | - return $this; |
|
| 971 | - } |
|
| 972 | - |
|
| 973 | - /** |
|
| 974 | - * Adds an ordering to the query results. |
|
| 975 | - * |
|
| 976 | - * @param string $sort The ordering expression. |
|
| 977 | - * @param string $order The ordering direction. |
|
| 978 | - * |
|
| 979 | - * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance. |
|
| 980 | - */ |
|
| 981 | - public function addOrderBy($sort, $order = null) { |
|
| 982 | - $this->queryBuilder->addOrderBy( |
|
| 983 | - $this->helper->quoteColumnName($sort), |
|
| 984 | - $order |
|
| 985 | - ); |
|
| 986 | - |
|
| 987 | - return $this; |
|
| 988 | - } |
|
| 989 | - |
|
| 990 | - /** |
|
| 991 | - * Gets a query part by its name. |
|
| 992 | - * |
|
| 993 | - * @param string $queryPartName |
|
| 994 | - * |
|
| 995 | - * @return mixed |
|
| 996 | - */ |
|
| 997 | - public function getQueryPart($queryPartName) { |
|
| 998 | - return $this->queryBuilder->getQueryPart($queryPartName); |
|
| 999 | - } |
|
| 1000 | - |
|
| 1001 | - /** |
|
| 1002 | - * Gets all query parts. |
|
| 1003 | - * |
|
| 1004 | - * @return array |
|
| 1005 | - */ |
|
| 1006 | - public function getQueryParts() { |
|
| 1007 | - return $this->queryBuilder->getQueryParts(); |
|
| 1008 | - } |
|
| 1009 | - |
|
| 1010 | - /** |
|
| 1011 | - * Resets SQL parts. |
|
| 1012 | - * |
|
| 1013 | - * @param array|null $queryPartNames |
|
| 1014 | - * |
|
| 1015 | - * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance. |
|
| 1016 | - */ |
|
| 1017 | - public function resetQueryParts($queryPartNames = null) { |
|
| 1018 | - $this->queryBuilder->resetQueryParts($queryPartNames); |
|
| 1019 | - |
|
| 1020 | - return $this; |
|
| 1021 | - } |
|
| 1022 | - |
|
| 1023 | - /** |
|
| 1024 | - * Resets a single SQL part. |
|
| 1025 | - * |
|
| 1026 | - * @param string $queryPartName |
|
| 1027 | - * |
|
| 1028 | - * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance. |
|
| 1029 | - */ |
|
| 1030 | - public function resetQueryPart($queryPartName) { |
|
| 1031 | - $this->queryBuilder->resetQueryPart($queryPartName); |
|
| 1032 | - |
|
| 1033 | - return $this; |
|
| 1034 | - } |
|
| 1035 | - |
|
| 1036 | - /** |
|
| 1037 | - * Creates a new named parameter and bind the value $value to it. |
|
| 1038 | - * |
|
| 1039 | - * This method provides a shortcut for PDOStatement::bindValue |
|
| 1040 | - * when using prepared statements. |
|
| 1041 | - * |
|
| 1042 | - * The parameter $value specifies the value that you want to bind. If |
|
| 1043 | - * $placeholder is not provided bindValue() will automatically create a |
|
| 1044 | - * placeholder for you. An automatic placeholder will be of the name |
|
| 1045 | - * ':dcValue1', ':dcValue2' etc. |
|
| 1046 | - * |
|
| 1047 | - * For more information see {@link http://php.net/pdostatement-bindparam} |
|
| 1048 | - * |
|
| 1049 | - * Example: |
|
| 1050 | - * <code> |
|
| 1051 | - * $value = 2; |
|
| 1052 | - * $q->eq( 'id', $q->bindValue( $value ) ); |
|
| 1053 | - * $stmt = $q->executeQuery(); // executed with 'id = 2' |
|
| 1054 | - * </code> |
|
| 1055 | - * |
|
| 1056 | - * @license New BSD License |
|
| 1057 | - * @link http://www.zetacomponents.org |
|
| 1058 | - * |
|
| 1059 | - * @param mixed $value |
|
| 1060 | - * @param mixed $type |
|
| 1061 | - * @param string $placeHolder The name to bind with. The string must start with a colon ':'. |
|
| 1062 | - * |
|
| 1063 | - * @return IParameter the placeholder name used. |
|
| 1064 | - */ |
|
| 1065 | - public function createNamedParameter($value, $type = IQueryBuilder::PARAM_STR, $placeHolder = null) { |
|
| 1066 | - return new Parameter($this->queryBuilder->createNamedParameter($value, $type, $placeHolder)); |
|
| 1067 | - } |
|
| 1068 | - |
|
| 1069 | - /** |
|
| 1070 | - * Creates a new positional parameter and bind the given value to it. |
|
| 1071 | - * |
|
| 1072 | - * Attention: If you are using positional parameters with the query builder you have |
|
| 1073 | - * to be very careful to bind all parameters in the order they appear in the SQL |
|
| 1074 | - * statement , otherwise they get bound in the wrong order which can lead to serious |
|
| 1075 | - * bugs in your code. |
|
| 1076 | - * |
|
| 1077 | - * Example: |
|
| 1078 | - * <code> |
|
| 1079 | - * $qb = $conn->getQueryBuilder(); |
|
| 1080 | - * $qb->select('u.*') |
|
| 1081 | - * ->from('users', 'u') |
|
| 1082 | - * ->where('u.username = ' . $qb->createPositionalParameter('Foo', IQueryBuilder::PARAM_STR)) |
|
| 1083 | - * ->orWhere('u.username = ' . $qb->createPositionalParameter('Bar', IQueryBuilder::PARAM_STR)) |
|
| 1084 | - * </code> |
|
| 1085 | - * |
|
| 1086 | - * @param mixed $value |
|
| 1087 | - * @param integer $type |
|
| 1088 | - * |
|
| 1089 | - * @return IParameter |
|
| 1090 | - */ |
|
| 1091 | - public function createPositionalParameter($value, $type = IQueryBuilder::PARAM_STR) { |
|
| 1092 | - return new Parameter($this->queryBuilder->createPositionalParameter($value, $type)); |
|
| 1093 | - } |
|
| 1094 | - |
|
| 1095 | - /** |
|
| 1096 | - * Creates a new parameter |
|
| 1097 | - * |
|
| 1098 | - * Example: |
|
| 1099 | - * <code> |
|
| 1100 | - * $qb = $conn->getQueryBuilder(); |
|
| 1101 | - * $qb->select('u.*') |
|
| 1102 | - * ->from('users', 'u') |
|
| 1103 | - * ->where('u.username = ' . $qb->createParameter('name')) |
|
| 1104 | - * ->setParameter('name', 'Bar', IQueryBuilder::PARAM_STR)) |
|
| 1105 | - * </code> |
|
| 1106 | - * |
|
| 1107 | - * @param string $name |
|
| 1108 | - * |
|
| 1109 | - * @return IParameter |
|
| 1110 | - */ |
|
| 1111 | - public function createParameter($name) { |
|
| 1112 | - return new Parameter(':' . $name); |
|
| 1113 | - } |
|
| 1114 | - |
|
| 1115 | - /** |
|
| 1116 | - * Creates a new function |
|
| 1117 | - * |
|
| 1118 | - * Attention: Column names inside the call have to be quoted before hand |
|
| 1119 | - * |
|
| 1120 | - * Example: |
|
| 1121 | - * <code> |
|
| 1122 | - * $qb = $conn->getQueryBuilder(); |
|
| 1123 | - * $qb->select($qb->createFunction('COUNT(*)')) |
|
| 1124 | - * ->from('users', 'u') |
|
| 1125 | - * echo $qb->getSQL(); // SELECT COUNT(*) FROM `users` u |
|
| 1126 | - * </code> |
|
| 1127 | - * <code> |
|
| 1128 | - * $qb = $conn->getQueryBuilder(); |
|
| 1129 | - * $qb->select($qb->createFunction('COUNT(`column`)')) |
|
| 1130 | - * ->from('users', 'u') |
|
| 1131 | - * echo $qb->getSQL(); // SELECT COUNT(`column`) FROM `users` u |
|
| 1132 | - * </code> |
|
| 1133 | - * |
|
| 1134 | - * @param string $call |
|
| 1135 | - * |
|
| 1136 | - * @return IQueryFunction |
|
| 1137 | - */ |
|
| 1138 | - public function createFunction($call) { |
|
| 1139 | - return new QueryFunction($call); |
|
| 1140 | - } |
|
| 1141 | - |
|
| 1142 | - /** |
|
| 1143 | - * Used to get the id of the last inserted element |
|
| 1144 | - * @return int |
|
| 1145 | - * @throws \BadMethodCallException When being called before an insert query has been run. |
|
| 1146 | - */ |
|
| 1147 | - public function getLastInsertId() { |
|
| 1148 | - if ($this->getType() === \Doctrine\DBAL\Query\QueryBuilder::INSERT && $this->lastInsertedTable) { |
|
| 1149 | - // lastInsertId() needs the prefix but no quotes |
|
| 1150 | - $table = $this->prefixTableName($this->lastInsertedTable); |
|
| 1151 | - return (int) $this->connection->lastInsertId($table); |
|
| 1152 | - } |
|
| 1153 | - |
|
| 1154 | - throw new \BadMethodCallException('Invalid call to getLastInsertId without using insert() before.'); |
|
| 1155 | - } |
|
| 1156 | - |
|
| 1157 | - /** |
|
| 1158 | - * Returns the table name quoted and with database prefix as needed by the implementation |
|
| 1159 | - * |
|
| 1160 | - * @param string $table |
|
| 1161 | - * @return string |
|
| 1162 | - */ |
|
| 1163 | - public function getTableName($table) { |
|
| 1164 | - $table = $this->prefixTableName($table); |
|
| 1165 | - return $this->helper->quoteColumnName($table); |
|
| 1166 | - } |
|
| 1167 | - |
|
| 1168 | - /** |
|
| 1169 | - * Returns the table name with database prefix as needed by the implementation |
|
| 1170 | - * |
|
| 1171 | - * @param string $table |
|
| 1172 | - * @return string |
|
| 1173 | - */ |
|
| 1174 | - protected function prefixTableName($table) { |
|
| 1175 | - if ($this->automaticTablePrefix === false || strpos($table, '*PREFIX*') === 0) { |
|
| 1176 | - return $table; |
|
| 1177 | - } |
|
| 1178 | - |
|
| 1179 | - return '*PREFIX*' . $table; |
|
| 1180 | - } |
|
| 1181 | - |
|
| 1182 | - /** |
|
| 1183 | - * Returns the column name quoted and with table alias prefix as needed by the implementation |
|
| 1184 | - * |
|
| 1185 | - * @param string $column |
|
| 1186 | - * @param string $tableAlias |
|
| 1187 | - * @return string |
|
| 1188 | - */ |
|
| 1189 | - public function getColumnName($column, $tableAlias = '') { |
|
| 1190 | - if ($tableAlias !== '') { |
|
| 1191 | - $tableAlias .= '.'; |
|
| 1192 | - } |
|
| 1193 | - |
|
| 1194 | - return $this->helper->quoteColumnName($tableAlias . $column); |
|
| 1195 | - } |
|
| 49 | + /** @var \OCP\IDBConnection */ |
|
| 50 | + private $connection; |
|
| 51 | + |
|
| 52 | + /** @var SystemConfig */ |
|
| 53 | + private $systemConfig; |
|
| 54 | + |
|
| 55 | + /** @var ILogger */ |
|
| 56 | + private $logger; |
|
| 57 | + |
|
| 58 | + /** @var \Doctrine\DBAL\Query\QueryBuilder */ |
|
| 59 | + private $queryBuilder; |
|
| 60 | + |
|
| 61 | + /** @var QuoteHelper */ |
|
| 62 | + private $helper; |
|
| 63 | + |
|
| 64 | + /** @var bool */ |
|
| 65 | + private $automaticTablePrefix = true; |
|
| 66 | + |
|
| 67 | + /** @var string */ |
|
| 68 | + protected $lastInsertedTable; |
|
| 69 | + |
|
| 70 | + /** |
|
| 71 | + * Initializes a new QueryBuilder. |
|
| 72 | + * |
|
| 73 | + * @param IDBConnection $connection |
|
| 74 | + * @param SystemConfig $systemConfig |
|
| 75 | + * @param ILogger $logger |
|
| 76 | + */ |
|
| 77 | + public function __construct(IDBConnection $connection, SystemConfig $systemConfig, ILogger $logger) { |
|
| 78 | + $this->connection = $connection; |
|
| 79 | + $this->systemConfig = $systemConfig; |
|
| 80 | + $this->logger = $logger; |
|
| 81 | + $this->queryBuilder = new \Doctrine\DBAL\Query\QueryBuilder($this->connection); |
|
| 82 | + $this->helper = new QuoteHelper(); |
|
| 83 | + } |
|
| 84 | + |
|
| 85 | + /** |
|
| 86 | + * Enable/disable automatic prefixing of table names with the oc_ prefix |
|
| 87 | + * |
|
| 88 | + * @param bool $enabled If set to true table names will be prefixed with the |
|
| 89 | + * owncloud database prefix automatically. |
|
| 90 | + * @since 8.2.0 |
|
| 91 | + */ |
|
| 92 | + public function automaticTablePrefix($enabled) { |
|
| 93 | + $this->automaticTablePrefix = (bool) $enabled; |
|
| 94 | + } |
|
| 95 | + |
|
| 96 | + /** |
|
| 97 | + * Gets an ExpressionBuilder used for object-oriented construction of query expressions. |
|
| 98 | + * This producer method is intended for convenient inline usage. Example: |
|
| 99 | + * |
|
| 100 | + * <code> |
|
| 101 | + * $qb = $conn->getQueryBuilder() |
|
| 102 | + * ->select('u') |
|
| 103 | + * ->from('users', 'u') |
|
| 104 | + * ->where($qb->expr()->eq('u.id', 1)); |
|
| 105 | + * </code> |
|
| 106 | + * |
|
| 107 | + * For more complex expression construction, consider storing the expression |
|
| 108 | + * builder object in a local variable. |
|
| 109 | + * |
|
| 110 | + * @return \OCP\DB\QueryBuilder\IExpressionBuilder |
|
| 111 | + */ |
|
| 112 | + public function expr() { |
|
| 113 | + if ($this->connection instanceof OracleConnection) { |
|
| 114 | + return new OCIExpressionBuilder($this->connection); |
|
| 115 | + } else if ($this->connection->getDatabasePlatform() instanceof PostgreSqlPlatform) { |
|
| 116 | + return new PgSqlExpressionBuilder($this->connection); |
|
| 117 | + } else if ($this->connection->getDatabasePlatform() instanceof MySqlPlatform) { |
|
| 118 | + return new MySqlExpressionBuilder($this->connection); |
|
| 119 | + } else if ($this->connection->getDatabasePlatform() instanceof SqlitePlatform) { |
|
| 120 | + return new SqliteExpressionBuilder($this->connection); |
|
| 121 | + } else { |
|
| 122 | + return new ExpressionBuilder($this->connection); |
|
| 123 | + } |
|
| 124 | + } |
|
| 125 | + |
|
| 126 | + /** |
|
| 127 | + * Gets an FunctionBuilder used for object-oriented construction of query functions. |
|
| 128 | + * This producer method is intended for convenient inline usage. Example: |
|
| 129 | + * |
|
| 130 | + * <code> |
|
| 131 | + * $qb = $conn->getQueryBuilder() |
|
| 132 | + * ->select('u') |
|
| 133 | + * ->from('users', 'u') |
|
| 134 | + * ->where($qb->fun()->md5('u.id')); |
|
| 135 | + * </code> |
|
| 136 | + * |
|
| 137 | + * For more complex function construction, consider storing the function |
|
| 138 | + * builder object in a local variable. |
|
| 139 | + * |
|
| 140 | + * @return \OCP\DB\QueryBuilder\IFunctionBuilder |
|
| 141 | + */ |
|
| 142 | + public function func() { |
|
| 143 | + if ($this->connection instanceof OracleConnection) { |
|
| 144 | + return new OCIFunctionBuilder($this->helper); |
|
| 145 | + } else if ($this->connection->getDatabasePlatform() instanceof SqlitePlatform) { |
|
| 146 | + return new SqliteFunctionBuilder($this->helper); |
|
| 147 | + } else if ($this->connection->getDatabasePlatform() instanceof PostgreSqlPlatform) { |
|
| 148 | + return new PgSqlFunctionBuilder($this->helper); |
|
| 149 | + } else { |
|
| 150 | + return new FunctionBuilder($this->helper); |
|
| 151 | + } |
|
| 152 | + } |
|
| 153 | + |
|
| 154 | + /** |
|
| 155 | + * Gets the type of the currently built query. |
|
| 156 | + * |
|
| 157 | + * @return integer |
|
| 158 | + */ |
|
| 159 | + public function getType() { |
|
| 160 | + return $this->queryBuilder->getType(); |
|
| 161 | + } |
|
| 162 | + |
|
| 163 | + /** |
|
| 164 | + * Gets the associated DBAL Connection for this query builder. |
|
| 165 | + * |
|
| 166 | + * @return \OCP\IDBConnection |
|
| 167 | + */ |
|
| 168 | + public function getConnection() { |
|
| 169 | + return $this->connection; |
|
| 170 | + } |
|
| 171 | + |
|
| 172 | + /** |
|
| 173 | + * Gets the state of this query builder instance. |
|
| 174 | + * |
|
| 175 | + * @return integer Either QueryBuilder::STATE_DIRTY or QueryBuilder::STATE_CLEAN. |
|
| 176 | + */ |
|
| 177 | + public function getState() { |
|
| 178 | + return $this->queryBuilder->getState(); |
|
| 179 | + } |
|
| 180 | + |
|
| 181 | + /** |
|
| 182 | + * Executes this query using the bound parameters and their types. |
|
| 183 | + * |
|
| 184 | + * Uses {@see Connection::executeQuery} for select statements and {@see Connection::executeUpdate} |
|
| 185 | + * for insert, update and delete statements. |
|
| 186 | + * |
|
| 187 | + * @return \Doctrine\DBAL\Driver\Statement|int |
|
| 188 | + */ |
|
| 189 | + public function execute() { |
|
| 190 | + if ($this->systemConfig->getValue('log_query', false)) { |
|
| 191 | + $params = []; |
|
| 192 | + foreach ($this->getParameters() as $placeholder => $value) { |
|
| 193 | + if (is_array($value)) { |
|
| 194 | + $params[] = $placeholder . ' => (\'' . implode('\', \'', $value) . '\')'; |
|
| 195 | + } else { |
|
| 196 | + $params[] = $placeholder . ' => \'' . $value . '\''; |
|
| 197 | + } |
|
| 198 | + } |
|
| 199 | + if (empty($params)) { |
|
| 200 | + $this->logger->debug('DB QueryBuilder: \'{query}\'', [ |
|
| 201 | + 'query' => $this->getSQL(), |
|
| 202 | + 'app' => 'core', |
|
| 203 | + ]); |
|
| 204 | + } else { |
|
| 205 | + $this->logger->debug('DB QueryBuilder: \'{query}\' with parameters: {params}', [ |
|
| 206 | + 'query' => $this->getSQL(), |
|
| 207 | + 'params' => implode(', ', $params), |
|
| 208 | + 'app' => 'core', |
|
| 209 | + ]); |
|
| 210 | + } |
|
| 211 | + } |
|
| 212 | + |
|
| 213 | + return $this->queryBuilder->execute(); |
|
| 214 | + } |
|
| 215 | + |
|
| 216 | + /** |
|
| 217 | + * Gets the complete SQL string formed by the current specifications of this QueryBuilder. |
|
| 218 | + * |
|
| 219 | + * <code> |
|
| 220 | + * $qb = $conn->getQueryBuilder() |
|
| 221 | + * ->select('u') |
|
| 222 | + * ->from('User', 'u') |
|
| 223 | + * echo $qb->getSQL(); // SELECT u FROM User u |
|
| 224 | + * </code> |
|
| 225 | + * |
|
| 226 | + * @return string The SQL query string. |
|
| 227 | + */ |
|
| 228 | + public function getSQL() { |
|
| 229 | + return $this->queryBuilder->getSQL(); |
|
| 230 | + } |
|
| 231 | + |
|
| 232 | + /** |
|
| 233 | + * Sets a query parameter for the query being constructed. |
|
| 234 | + * |
|
| 235 | + * <code> |
|
| 236 | + * $qb = $conn->getQueryBuilder() |
|
| 237 | + * ->select('u') |
|
| 238 | + * ->from('users', 'u') |
|
| 239 | + * ->where('u.id = :user_id') |
|
| 240 | + * ->setParameter(':user_id', 1); |
|
| 241 | + * </code> |
|
| 242 | + * |
|
| 243 | + * @param string|integer $key The parameter position or name. |
|
| 244 | + * @param mixed $value The parameter value. |
|
| 245 | + * @param string|null $type One of the IQueryBuilder::PARAM_* constants. |
|
| 246 | + * |
|
| 247 | + * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance. |
|
| 248 | + */ |
|
| 249 | + public function setParameter($key, $value, $type = null) { |
|
| 250 | + $this->queryBuilder->setParameter($key, $value, $type); |
|
| 251 | + |
|
| 252 | + return $this; |
|
| 253 | + } |
|
| 254 | + |
|
| 255 | + /** |
|
| 256 | + * Sets a collection of query parameters for the query being constructed. |
|
| 257 | + * |
|
| 258 | + * <code> |
|
| 259 | + * $qb = $conn->getQueryBuilder() |
|
| 260 | + * ->select('u') |
|
| 261 | + * ->from('users', 'u') |
|
| 262 | + * ->where('u.id = :user_id1 OR u.id = :user_id2') |
|
| 263 | + * ->setParameters(array( |
|
| 264 | + * ':user_id1' => 1, |
|
| 265 | + * ':user_id2' => 2 |
|
| 266 | + * )); |
|
| 267 | + * </code> |
|
| 268 | + * |
|
| 269 | + * @param array $params The query parameters to set. |
|
| 270 | + * @param array $types The query parameters types to set. |
|
| 271 | + * |
|
| 272 | + * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance. |
|
| 273 | + */ |
|
| 274 | + public function setParameters(array $params, array $types = array()) { |
|
| 275 | + $this->queryBuilder->setParameters($params, $types); |
|
| 276 | + |
|
| 277 | + return $this; |
|
| 278 | + } |
|
| 279 | + |
|
| 280 | + /** |
|
| 281 | + * Gets all defined query parameters for the query being constructed indexed by parameter index or name. |
|
| 282 | + * |
|
| 283 | + * @return array The currently defined query parameters indexed by parameter index or name. |
|
| 284 | + */ |
|
| 285 | + public function getParameters() { |
|
| 286 | + return $this->queryBuilder->getParameters(); |
|
| 287 | + } |
|
| 288 | + |
|
| 289 | + /** |
|
| 290 | + * Gets a (previously set) query parameter of the query being constructed. |
|
| 291 | + * |
|
| 292 | + * @param mixed $key The key (index or name) of the bound parameter. |
|
| 293 | + * |
|
| 294 | + * @return mixed The value of the bound parameter. |
|
| 295 | + */ |
|
| 296 | + public function getParameter($key) { |
|
| 297 | + return $this->queryBuilder->getParameter($key); |
|
| 298 | + } |
|
| 299 | + |
|
| 300 | + /** |
|
| 301 | + * Gets all defined query parameter types for the query being constructed indexed by parameter index or name. |
|
| 302 | + * |
|
| 303 | + * @return array The currently defined query parameter types indexed by parameter index or name. |
|
| 304 | + */ |
|
| 305 | + public function getParameterTypes() { |
|
| 306 | + return $this->queryBuilder->getParameterTypes(); |
|
| 307 | + } |
|
| 308 | + |
|
| 309 | + /** |
|
| 310 | + * Gets a (previously set) query parameter type of the query being constructed. |
|
| 311 | + * |
|
| 312 | + * @param mixed $key The key (index or name) of the bound parameter type. |
|
| 313 | + * |
|
| 314 | + * @return mixed The value of the bound parameter type. |
|
| 315 | + */ |
|
| 316 | + public function getParameterType($key) { |
|
| 317 | + return $this->queryBuilder->getParameterType($key); |
|
| 318 | + } |
|
| 319 | + |
|
| 320 | + /** |
|
| 321 | + * Sets the position of the first result to retrieve (the "offset"). |
|
| 322 | + * |
|
| 323 | + * @param integer $firstResult The first result to return. |
|
| 324 | + * |
|
| 325 | + * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance. |
|
| 326 | + */ |
|
| 327 | + public function setFirstResult($firstResult) { |
|
| 328 | + $this->queryBuilder->setFirstResult($firstResult); |
|
| 329 | + |
|
| 330 | + return $this; |
|
| 331 | + } |
|
| 332 | + |
|
| 333 | + /** |
|
| 334 | + * Gets the position of the first result the query object was set to retrieve (the "offset"). |
|
| 335 | + * Returns NULL if {@link setFirstResult} was not applied to this QueryBuilder. |
|
| 336 | + * |
|
| 337 | + * @return integer The position of the first result. |
|
| 338 | + */ |
|
| 339 | + public function getFirstResult() { |
|
| 340 | + return $this->queryBuilder->getFirstResult(); |
|
| 341 | + } |
|
| 342 | + |
|
| 343 | + /** |
|
| 344 | + * Sets the maximum number of results to retrieve (the "limit"). |
|
| 345 | + * |
|
| 346 | + * NOTE: Setting max results to "0" will cause mixed behaviour. While most |
|
| 347 | + * of the databases will just return an empty result set, Oracle will return |
|
| 348 | + * all entries. |
|
| 349 | + * |
|
| 350 | + * @param integer $maxResults The maximum number of results to retrieve. |
|
| 351 | + * |
|
| 352 | + * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance. |
|
| 353 | + */ |
|
| 354 | + public function setMaxResults($maxResults) { |
|
| 355 | + $this->queryBuilder->setMaxResults($maxResults); |
|
| 356 | + |
|
| 357 | + return $this; |
|
| 358 | + } |
|
| 359 | + |
|
| 360 | + /** |
|
| 361 | + * Gets the maximum number of results the query object was set to retrieve (the "limit"). |
|
| 362 | + * Returns NULL if {@link setMaxResults} was not applied to this query builder. |
|
| 363 | + * |
|
| 364 | + * @return integer The maximum number of results. |
|
| 365 | + */ |
|
| 366 | + public function getMaxResults() { |
|
| 367 | + return $this->queryBuilder->getMaxResults(); |
|
| 368 | + } |
|
| 369 | + |
|
| 370 | + /** |
|
| 371 | + * Specifies an item that is to be returned in the query result. |
|
| 372 | + * Replaces any previously specified selections, if any. |
|
| 373 | + * |
|
| 374 | + * <code> |
|
| 375 | + * $qb = $conn->getQueryBuilder() |
|
| 376 | + * ->select('u.id', 'p.id') |
|
| 377 | + * ->from('users', 'u') |
|
| 378 | + * ->leftJoin('u', 'phonenumbers', 'p', 'u.id = p.user_id'); |
|
| 379 | + * </code> |
|
| 380 | + * |
|
| 381 | + * @param mixed $select The selection expressions. |
|
| 382 | + * |
|
| 383 | + * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance. |
|
| 384 | + */ |
|
| 385 | + public function select($select = null) { |
|
| 386 | + $selects = is_array($select) ? $select : func_get_args(); |
|
| 387 | + |
|
| 388 | + $this->queryBuilder->select( |
|
| 389 | + $this->helper->quoteColumnNames($selects) |
|
| 390 | + ); |
|
| 391 | + |
|
| 392 | + return $this; |
|
| 393 | + } |
|
| 394 | + |
|
| 395 | + /** |
|
| 396 | + * Specifies an item that is to be returned with a different name in the query result. |
|
| 397 | + * |
|
| 398 | + * <code> |
|
| 399 | + * $qb = $conn->getQueryBuilder() |
|
| 400 | + * ->selectAlias('u.id', 'user_id') |
|
| 401 | + * ->from('users', 'u') |
|
| 402 | + * ->leftJoin('u', 'phonenumbers', 'p', 'u.id = p.user_id'); |
|
| 403 | + * </code> |
|
| 404 | + * |
|
| 405 | + * @param mixed $select The selection expressions. |
|
| 406 | + * @param string $alias The column alias used in the constructed query. |
|
| 407 | + * |
|
| 408 | + * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance. |
|
| 409 | + */ |
|
| 410 | + public function selectAlias($select, $alias) { |
|
| 411 | + |
|
| 412 | + $this->queryBuilder->addSelect( |
|
| 413 | + $this->helper->quoteColumnName($select) . ' AS ' . $this->helper->quoteColumnName($alias) |
|
| 414 | + ); |
|
| 415 | + |
|
| 416 | + return $this; |
|
| 417 | + } |
|
| 418 | + |
|
| 419 | + /** |
|
| 420 | + * Specifies an item that is to be returned uniquely in the query result. |
|
| 421 | + * |
|
| 422 | + * <code> |
|
| 423 | + * $qb = $conn->getQueryBuilder() |
|
| 424 | + * ->selectDistinct('type') |
|
| 425 | + * ->from('users'); |
|
| 426 | + * </code> |
|
| 427 | + * |
|
| 428 | + * @param mixed $select The selection expressions. |
|
| 429 | + * |
|
| 430 | + * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance. |
|
| 431 | + */ |
|
| 432 | + public function selectDistinct($select) { |
|
| 433 | + |
|
| 434 | + $this->queryBuilder->addSelect( |
|
| 435 | + 'DISTINCT ' . $this->helper->quoteColumnName($select) |
|
| 436 | + ); |
|
| 437 | + |
|
| 438 | + return $this; |
|
| 439 | + } |
|
| 440 | + |
|
| 441 | + /** |
|
| 442 | + * Adds an item that is to be returned in the query result. |
|
| 443 | + * |
|
| 444 | + * <code> |
|
| 445 | + * $qb = $conn->getQueryBuilder() |
|
| 446 | + * ->select('u.id') |
|
| 447 | + * ->addSelect('p.id') |
|
| 448 | + * ->from('users', 'u') |
|
| 449 | + * ->leftJoin('u', 'phonenumbers', 'u.id = p.user_id'); |
|
| 450 | + * </code> |
|
| 451 | + * |
|
| 452 | + * @param mixed $select The selection expression. |
|
| 453 | + * |
|
| 454 | + * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance. |
|
| 455 | + */ |
|
| 456 | + public function addSelect($select = null) { |
|
| 457 | + $selects = is_array($select) ? $select : func_get_args(); |
|
| 458 | + |
|
| 459 | + $this->queryBuilder->addSelect( |
|
| 460 | + $this->helper->quoteColumnNames($selects) |
|
| 461 | + ); |
|
| 462 | + |
|
| 463 | + return $this; |
|
| 464 | + } |
|
| 465 | + |
|
| 466 | + /** |
|
| 467 | + * Turns the query being built into a bulk delete query that ranges over |
|
| 468 | + * a certain table. |
|
| 469 | + * |
|
| 470 | + * <code> |
|
| 471 | + * $qb = $conn->getQueryBuilder() |
|
| 472 | + * ->delete('users', 'u') |
|
| 473 | + * ->where('u.id = :user_id'); |
|
| 474 | + * ->setParameter(':user_id', 1); |
|
| 475 | + * </code> |
|
| 476 | + * |
|
| 477 | + * @param string $delete The table whose rows are subject to the deletion. |
|
| 478 | + * @param string $alias The table alias used in the constructed query. |
|
| 479 | + * |
|
| 480 | + * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance. |
|
| 481 | + */ |
|
| 482 | + public function delete($delete = null, $alias = null) { |
|
| 483 | + $this->queryBuilder->delete( |
|
| 484 | + $this->getTableName($delete), |
|
| 485 | + $alias |
|
| 486 | + ); |
|
| 487 | + |
|
| 488 | + return $this; |
|
| 489 | + } |
|
| 490 | + |
|
| 491 | + /** |
|
| 492 | + * Turns the query being built into a bulk update query that ranges over |
|
| 493 | + * a certain table |
|
| 494 | + * |
|
| 495 | + * <code> |
|
| 496 | + * $qb = $conn->getQueryBuilder() |
|
| 497 | + * ->update('users', 'u') |
|
| 498 | + * ->set('u.password', md5('password')) |
|
| 499 | + * ->where('u.id = ?'); |
|
| 500 | + * </code> |
|
| 501 | + * |
|
| 502 | + * @param string $update The table whose rows are subject to the update. |
|
| 503 | + * @param string $alias The table alias used in the constructed query. |
|
| 504 | + * |
|
| 505 | + * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance. |
|
| 506 | + */ |
|
| 507 | + public function update($update = null, $alias = null) { |
|
| 508 | + $this->queryBuilder->update( |
|
| 509 | + $this->getTableName($update), |
|
| 510 | + $alias |
|
| 511 | + ); |
|
| 512 | + |
|
| 513 | + return $this; |
|
| 514 | + } |
|
| 515 | + |
|
| 516 | + /** |
|
| 517 | + * Turns the query being built into an insert query that inserts into |
|
| 518 | + * a certain table |
|
| 519 | + * |
|
| 520 | + * <code> |
|
| 521 | + * $qb = $conn->getQueryBuilder() |
|
| 522 | + * ->insert('users') |
|
| 523 | + * ->values( |
|
| 524 | + * array( |
|
| 525 | + * 'name' => '?', |
|
| 526 | + * 'password' => '?' |
|
| 527 | + * ) |
|
| 528 | + * ); |
|
| 529 | + * </code> |
|
| 530 | + * |
|
| 531 | + * @param string $insert The table into which the rows should be inserted. |
|
| 532 | + * |
|
| 533 | + * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance. |
|
| 534 | + */ |
|
| 535 | + public function insert($insert = null) { |
|
| 536 | + $this->queryBuilder->insert( |
|
| 537 | + $this->getTableName($insert) |
|
| 538 | + ); |
|
| 539 | + |
|
| 540 | + $this->lastInsertedTable = $insert; |
|
| 541 | + |
|
| 542 | + return $this; |
|
| 543 | + } |
|
| 544 | + |
|
| 545 | + /** |
|
| 546 | + * Creates and adds a query root corresponding to the table identified by the |
|
| 547 | + * given alias, forming a cartesian product with any existing query roots. |
|
| 548 | + * |
|
| 549 | + * <code> |
|
| 550 | + * $qb = $conn->getQueryBuilder() |
|
| 551 | + * ->select('u.id') |
|
| 552 | + * ->from('users', 'u') |
|
| 553 | + * </code> |
|
| 554 | + * |
|
| 555 | + * @param string $from The table. |
|
| 556 | + * @param string|null $alias The alias of the table. |
|
| 557 | + * |
|
| 558 | + * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance. |
|
| 559 | + */ |
|
| 560 | + public function from($from, $alias = null) { |
|
| 561 | + $this->queryBuilder->from( |
|
| 562 | + $this->getTableName($from), |
|
| 563 | + $alias |
|
| 564 | + ); |
|
| 565 | + |
|
| 566 | + return $this; |
|
| 567 | + } |
|
| 568 | + |
|
| 569 | + /** |
|
| 570 | + * Creates and adds a join to the query. |
|
| 571 | + * |
|
| 572 | + * <code> |
|
| 573 | + * $qb = $conn->getQueryBuilder() |
|
| 574 | + * ->select('u.name') |
|
| 575 | + * ->from('users', 'u') |
|
| 576 | + * ->join('u', 'phonenumbers', 'p', 'p.is_primary = 1'); |
|
| 577 | + * </code> |
|
| 578 | + * |
|
| 579 | + * @param string $fromAlias The alias that points to a from clause. |
|
| 580 | + * @param string $join The table name to join. |
|
| 581 | + * @param string $alias The alias of the join table. |
|
| 582 | + * @param string $condition The condition for the join. |
|
| 583 | + * |
|
| 584 | + * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance. |
|
| 585 | + */ |
|
| 586 | + public function join($fromAlias, $join, $alias, $condition = null) { |
|
| 587 | + $this->queryBuilder->join( |
|
| 588 | + $fromAlias, |
|
| 589 | + $this->getTableName($join), |
|
| 590 | + $alias, |
|
| 591 | + $condition |
|
| 592 | + ); |
|
| 593 | + |
|
| 594 | + return $this; |
|
| 595 | + } |
|
| 596 | + |
|
| 597 | + /** |
|
| 598 | + * Creates and adds a join to the query. |
|
| 599 | + * |
|
| 600 | + * <code> |
|
| 601 | + * $qb = $conn->getQueryBuilder() |
|
| 602 | + * ->select('u.name') |
|
| 603 | + * ->from('users', 'u') |
|
| 604 | + * ->innerJoin('u', 'phonenumbers', 'p', 'p.is_primary = 1'); |
|
| 605 | + * </code> |
|
| 606 | + * |
|
| 607 | + * @param string $fromAlias The alias that points to a from clause. |
|
| 608 | + * @param string $join The table name to join. |
|
| 609 | + * @param string $alias The alias of the join table. |
|
| 610 | + * @param string $condition The condition for the join. |
|
| 611 | + * |
|
| 612 | + * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance. |
|
| 613 | + */ |
|
| 614 | + public function innerJoin($fromAlias, $join, $alias, $condition = null) { |
|
| 615 | + $this->queryBuilder->innerJoin( |
|
| 616 | + $fromAlias, |
|
| 617 | + $this->getTableName($join), |
|
| 618 | + $alias, |
|
| 619 | + $condition |
|
| 620 | + ); |
|
| 621 | + |
|
| 622 | + return $this; |
|
| 623 | + } |
|
| 624 | + |
|
| 625 | + /** |
|
| 626 | + * Creates and adds a left join to the query. |
|
| 627 | + * |
|
| 628 | + * <code> |
|
| 629 | + * $qb = $conn->getQueryBuilder() |
|
| 630 | + * ->select('u.name') |
|
| 631 | + * ->from('users', 'u') |
|
| 632 | + * ->leftJoin('u', 'phonenumbers', 'p', 'p.is_primary = 1'); |
|
| 633 | + * </code> |
|
| 634 | + * |
|
| 635 | + * @param string $fromAlias The alias that points to a from clause. |
|
| 636 | + * @param string $join The table name to join. |
|
| 637 | + * @param string $alias The alias of the join table. |
|
| 638 | + * @param string $condition The condition for the join. |
|
| 639 | + * |
|
| 640 | + * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance. |
|
| 641 | + */ |
|
| 642 | + public function leftJoin($fromAlias, $join, $alias, $condition = null) { |
|
| 643 | + $this->queryBuilder->leftJoin( |
|
| 644 | + $fromAlias, |
|
| 645 | + $this->getTableName($join), |
|
| 646 | + $alias, |
|
| 647 | + $condition |
|
| 648 | + ); |
|
| 649 | + |
|
| 650 | + return $this; |
|
| 651 | + } |
|
| 652 | + |
|
| 653 | + /** |
|
| 654 | + * Creates and adds a right join to the query. |
|
| 655 | + * |
|
| 656 | + * <code> |
|
| 657 | + * $qb = $conn->getQueryBuilder() |
|
| 658 | + * ->select('u.name') |
|
| 659 | + * ->from('users', 'u') |
|
| 660 | + * ->rightJoin('u', 'phonenumbers', 'p', 'p.is_primary = 1'); |
|
| 661 | + * </code> |
|
| 662 | + * |
|
| 663 | + * @param string $fromAlias The alias that points to a from clause. |
|
| 664 | + * @param string $join The table name to join. |
|
| 665 | + * @param string $alias The alias of the join table. |
|
| 666 | + * @param string $condition The condition for the join. |
|
| 667 | + * |
|
| 668 | + * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance. |
|
| 669 | + */ |
|
| 670 | + public function rightJoin($fromAlias, $join, $alias, $condition = null) { |
|
| 671 | + $this->queryBuilder->rightJoin( |
|
| 672 | + $fromAlias, |
|
| 673 | + $this->getTableName($join), |
|
| 674 | + $alias, |
|
| 675 | + $condition |
|
| 676 | + ); |
|
| 677 | + |
|
| 678 | + return $this; |
|
| 679 | + } |
|
| 680 | + |
|
| 681 | + /** |
|
| 682 | + * Sets a new value for a column in a bulk update query. |
|
| 683 | + * |
|
| 684 | + * <code> |
|
| 685 | + * $qb = $conn->getQueryBuilder() |
|
| 686 | + * ->update('users', 'u') |
|
| 687 | + * ->set('u.password', md5('password')) |
|
| 688 | + * ->where('u.id = ?'); |
|
| 689 | + * </code> |
|
| 690 | + * |
|
| 691 | + * @param string $key The column to set. |
|
| 692 | + * @param string $value The value, expression, placeholder, etc. |
|
| 693 | + * |
|
| 694 | + * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance. |
|
| 695 | + */ |
|
| 696 | + public function set($key, $value) { |
|
| 697 | + $this->queryBuilder->set( |
|
| 698 | + $this->helper->quoteColumnName($key), |
|
| 699 | + $this->helper->quoteColumnName($value) |
|
| 700 | + ); |
|
| 701 | + |
|
| 702 | + return $this; |
|
| 703 | + } |
|
| 704 | + |
|
| 705 | + /** |
|
| 706 | + * Specifies one or more restrictions to the query result. |
|
| 707 | + * Replaces any previously specified restrictions, if any. |
|
| 708 | + * |
|
| 709 | + * <code> |
|
| 710 | + * $qb = $conn->getQueryBuilder() |
|
| 711 | + * ->select('u.name') |
|
| 712 | + * ->from('users', 'u') |
|
| 713 | + * ->where('u.id = ?'); |
|
| 714 | + * |
|
| 715 | + * // You can optionally programatically build and/or expressions |
|
| 716 | + * $qb = $conn->getQueryBuilder(); |
|
| 717 | + * |
|
| 718 | + * $or = $qb->expr()->orx(); |
|
| 719 | + * $or->add($qb->expr()->eq('u.id', 1)); |
|
| 720 | + * $or->add($qb->expr()->eq('u.id', 2)); |
|
| 721 | + * |
|
| 722 | + * $qb->update('users', 'u') |
|
| 723 | + * ->set('u.password', md5('password')) |
|
| 724 | + * ->where($or); |
|
| 725 | + * </code> |
|
| 726 | + * |
|
| 727 | + * @param mixed $predicates The restriction predicates. |
|
| 728 | + * |
|
| 729 | + * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance. |
|
| 730 | + */ |
|
| 731 | + public function where($predicates) { |
|
| 732 | + call_user_func_array( |
|
| 733 | + [$this->queryBuilder, 'where'], |
|
| 734 | + func_get_args() |
|
| 735 | + ); |
|
| 736 | + |
|
| 737 | + return $this; |
|
| 738 | + } |
|
| 739 | + |
|
| 740 | + /** |
|
| 741 | + * Adds one or more restrictions to the query results, forming a logical |
|
| 742 | + * conjunction with any previously specified restrictions. |
|
| 743 | + * |
|
| 744 | + * <code> |
|
| 745 | + * $qb = $conn->getQueryBuilder() |
|
| 746 | + * ->select('u') |
|
| 747 | + * ->from('users', 'u') |
|
| 748 | + * ->where('u.username LIKE ?') |
|
| 749 | + * ->andWhere('u.is_active = 1'); |
|
| 750 | + * </code> |
|
| 751 | + * |
|
| 752 | + * @param mixed $where The query restrictions. |
|
| 753 | + * |
|
| 754 | + * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance. |
|
| 755 | + * |
|
| 756 | + * @see where() |
|
| 757 | + */ |
|
| 758 | + public function andWhere($where) { |
|
| 759 | + call_user_func_array( |
|
| 760 | + [$this->queryBuilder, 'andWhere'], |
|
| 761 | + func_get_args() |
|
| 762 | + ); |
|
| 763 | + |
|
| 764 | + return $this; |
|
| 765 | + } |
|
| 766 | + |
|
| 767 | + /** |
|
| 768 | + * Adds one or more restrictions to the query results, forming a logical |
|
| 769 | + * disjunction with any previously specified restrictions. |
|
| 770 | + * |
|
| 771 | + * <code> |
|
| 772 | + * $qb = $conn->getQueryBuilder() |
|
| 773 | + * ->select('u.name') |
|
| 774 | + * ->from('users', 'u') |
|
| 775 | + * ->where('u.id = 1') |
|
| 776 | + * ->orWhere('u.id = 2'); |
|
| 777 | + * </code> |
|
| 778 | + * |
|
| 779 | + * @param mixed $where The WHERE statement. |
|
| 780 | + * |
|
| 781 | + * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance. |
|
| 782 | + * |
|
| 783 | + * @see where() |
|
| 784 | + */ |
|
| 785 | + public function orWhere($where) { |
|
| 786 | + call_user_func_array( |
|
| 787 | + [$this->queryBuilder, 'orWhere'], |
|
| 788 | + func_get_args() |
|
| 789 | + ); |
|
| 790 | + |
|
| 791 | + return $this; |
|
| 792 | + } |
|
| 793 | + |
|
| 794 | + /** |
|
| 795 | + * Specifies a grouping over the results of the query. |
|
| 796 | + * Replaces any previously specified groupings, if any. |
|
| 797 | + * |
|
| 798 | + * <code> |
|
| 799 | + * $qb = $conn->getQueryBuilder() |
|
| 800 | + * ->select('u.name') |
|
| 801 | + * ->from('users', 'u') |
|
| 802 | + * ->groupBy('u.id'); |
|
| 803 | + * </code> |
|
| 804 | + * |
|
| 805 | + * @param mixed $groupBy The grouping expression. |
|
| 806 | + * |
|
| 807 | + * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance. |
|
| 808 | + */ |
|
| 809 | + public function groupBy($groupBy) { |
|
| 810 | + $groupBys = is_array($groupBy) ? $groupBy : func_get_args(); |
|
| 811 | + |
|
| 812 | + call_user_func_array( |
|
| 813 | + [$this->queryBuilder, 'groupBy'], |
|
| 814 | + $this->helper->quoteColumnNames($groupBys) |
|
| 815 | + ); |
|
| 816 | + |
|
| 817 | + return $this; |
|
| 818 | + } |
|
| 819 | + |
|
| 820 | + /** |
|
| 821 | + * Adds a grouping expression to the query. |
|
| 822 | + * |
|
| 823 | + * <code> |
|
| 824 | + * $qb = $conn->getQueryBuilder() |
|
| 825 | + * ->select('u.name') |
|
| 826 | + * ->from('users', 'u') |
|
| 827 | + * ->groupBy('u.lastLogin'); |
|
| 828 | + * ->addGroupBy('u.createdAt') |
|
| 829 | + * </code> |
|
| 830 | + * |
|
| 831 | + * @param mixed $groupBy The grouping expression. |
|
| 832 | + * |
|
| 833 | + * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance. |
|
| 834 | + */ |
|
| 835 | + public function addGroupBy($groupBy) { |
|
| 836 | + $groupBys = is_array($groupBy) ? $groupBy : func_get_args(); |
|
| 837 | + |
|
| 838 | + call_user_func_array( |
|
| 839 | + [$this->queryBuilder, 'addGroupBy'], |
|
| 840 | + $this->helper->quoteColumnNames($groupBys) |
|
| 841 | + ); |
|
| 842 | + |
|
| 843 | + return $this; |
|
| 844 | + } |
|
| 845 | + |
|
| 846 | + /** |
|
| 847 | + * Sets a value for a column in an insert query. |
|
| 848 | + * |
|
| 849 | + * <code> |
|
| 850 | + * $qb = $conn->getQueryBuilder() |
|
| 851 | + * ->insert('users') |
|
| 852 | + * ->values( |
|
| 853 | + * array( |
|
| 854 | + * 'name' => '?' |
|
| 855 | + * ) |
|
| 856 | + * ) |
|
| 857 | + * ->setValue('password', '?'); |
|
| 858 | + * </code> |
|
| 859 | + * |
|
| 860 | + * @param string $column The column into which the value should be inserted. |
|
| 861 | + * @param string $value The value that should be inserted into the column. |
|
| 862 | + * |
|
| 863 | + * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance. |
|
| 864 | + */ |
|
| 865 | + public function setValue($column, $value) { |
|
| 866 | + $this->queryBuilder->setValue( |
|
| 867 | + $this->helper->quoteColumnName($column), |
|
| 868 | + $value |
|
| 869 | + ); |
|
| 870 | + |
|
| 871 | + return $this; |
|
| 872 | + } |
|
| 873 | + |
|
| 874 | + /** |
|
| 875 | + * Specifies values for an insert query indexed by column names. |
|
| 876 | + * Replaces any previous values, if any. |
|
| 877 | + * |
|
| 878 | + * <code> |
|
| 879 | + * $qb = $conn->getQueryBuilder() |
|
| 880 | + * ->insert('users') |
|
| 881 | + * ->values( |
|
| 882 | + * array( |
|
| 883 | + * 'name' => '?', |
|
| 884 | + * 'password' => '?' |
|
| 885 | + * ) |
|
| 886 | + * ); |
|
| 887 | + * </code> |
|
| 888 | + * |
|
| 889 | + * @param array $values The values to specify for the insert query indexed by column names. |
|
| 890 | + * |
|
| 891 | + * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance. |
|
| 892 | + */ |
|
| 893 | + public function values(array $values) { |
|
| 894 | + $quotedValues = []; |
|
| 895 | + foreach ($values as $key => $value) { |
|
| 896 | + $quotedValues[$this->helper->quoteColumnName($key)] = $value; |
|
| 897 | + } |
|
| 898 | + |
|
| 899 | + $this->queryBuilder->values($quotedValues); |
|
| 900 | + |
|
| 901 | + return $this; |
|
| 902 | + } |
|
| 903 | + |
|
| 904 | + /** |
|
| 905 | + * Specifies a restriction over the groups of the query. |
|
| 906 | + * Replaces any previous having restrictions, if any. |
|
| 907 | + * |
|
| 908 | + * @param mixed $having The restriction over the groups. |
|
| 909 | + * |
|
| 910 | + * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance. |
|
| 911 | + */ |
|
| 912 | + public function having($having) { |
|
| 913 | + call_user_func_array( |
|
| 914 | + [$this->queryBuilder, 'having'], |
|
| 915 | + func_get_args() |
|
| 916 | + ); |
|
| 917 | + |
|
| 918 | + return $this; |
|
| 919 | + } |
|
| 920 | + |
|
| 921 | + /** |
|
| 922 | + * Adds a restriction over the groups of the query, forming a logical |
|
| 923 | + * conjunction with any existing having restrictions. |
|
| 924 | + * |
|
| 925 | + * @param mixed $having The restriction to append. |
|
| 926 | + * |
|
| 927 | + * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance. |
|
| 928 | + */ |
|
| 929 | + public function andHaving($having) { |
|
| 930 | + call_user_func_array( |
|
| 931 | + [$this->queryBuilder, 'andHaving'], |
|
| 932 | + func_get_args() |
|
| 933 | + ); |
|
| 934 | + |
|
| 935 | + return $this; |
|
| 936 | + } |
|
| 937 | + |
|
| 938 | + /** |
|
| 939 | + * Adds a restriction over the groups of the query, forming a logical |
|
| 940 | + * disjunction with any existing having restrictions. |
|
| 941 | + * |
|
| 942 | + * @param mixed $having The restriction to add. |
|
| 943 | + * |
|
| 944 | + * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance. |
|
| 945 | + */ |
|
| 946 | + public function orHaving($having) { |
|
| 947 | + call_user_func_array( |
|
| 948 | + [$this->queryBuilder, 'orHaving'], |
|
| 949 | + func_get_args() |
|
| 950 | + ); |
|
| 951 | + |
|
| 952 | + return $this; |
|
| 953 | + } |
|
| 954 | + |
|
| 955 | + /** |
|
| 956 | + * Specifies an ordering for the query results. |
|
| 957 | + * Replaces any previously specified orderings, if any. |
|
| 958 | + * |
|
| 959 | + * @param string $sort The ordering expression. |
|
| 960 | + * @param string $order The ordering direction. |
|
| 961 | + * |
|
| 962 | + * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance. |
|
| 963 | + */ |
|
| 964 | + public function orderBy($sort, $order = null) { |
|
| 965 | + $this->queryBuilder->orderBy( |
|
| 966 | + $this->helper->quoteColumnName($sort), |
|
| 967 | + $order |
|
| 968 | + ); |
|
| 969 | + |
|
| 970 | + return $this; |
|
| 971 | + } |
|
| 972 | + |
|
| 973 | + /** |
|
| 974 | + * Adds an ordering to the query results. |
|
| 975 | + * |
|
| 976 | + * @param string $sort The ordering expression. |
|
| 977 | + * @param string $order The ordering direction. |
|
| 978 | + * |
|
| 979 | + * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance. |
|
| 980 | + */ |
|
| 981 | + public function addOrderBy($sort, $order = null) { |
|
| 982 | + $this->queryBuilder->addOrderBy( |
|
| 983 | + $this->helper->quoteColumnName($sort), |
|
| 984 | + $order |
|
| 985 | + ); |
|
| 986 | + |
|
| 987 | + return $this; |
|
| 988 | + } |
|
| 989 | + |
|
| 990 | + /** |
|
| 991 | + * Gets a query part by its name. |
|
| 992 | + * |
|
| 993 | + * @param string $queryPartName |
|
| 994 | + * |
|
| 995 | + * @return mixed |
|
| 996 | + */ |
|
| 997 | + public function getQueryPart($queryPartName) { |
|
| 998 | + return $this->queryBuilder->getQueryPart($queryPartName); |
|
| 999 | + } |
|
| 1000 | + |
|
| 1001 | + /** |
|
| 1002 | + * Gets all query parts. |
|
| 1003 | + * |
|
| 1004 | + * @return array |
|
| 1005 | + */ |
|
| 1006 | + public function getQueryParts() { |
|
| 1007 | + return $this->queryBuilder->getQueryParts(); |
|
| 1008 | + } |
|
| 1009 | + |
|
| 1010 | + /** |
|
| 1011 | + * Resets SQL parts. |
|
| 1012 | + * |
|
| 1013 | + * @param array|null $queryPartNames |
|
| 1014 | + * |
|
| 1015 | + * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance. |
|
| 1016 | + */ |
|
| 1017 | + public function resetQueryParts($queryPartNames = null) { |
|
| 1018 | + $this->queryBuilder->resetQueryParts($queryPartNames); |
|
| 1019 | + |
|
| 1020 | + return $this; |
|
| 1021 | + } |
|
| 1022 | + |
|
| 1023 | + /** |
|
| 1024 | + * Resets a single SQL part. |
|
| 1025 | + * |
|
| 1026 | + * @param string $queryPartName |
|
| 1027 | + * |
|
| 1028 | + * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance. |
|
| 1029 | + */ |
|
| 1030 | + public function resetQueryPart($queryPartName) { |
|
| 1031 | + $this->queryBuilder->resetQueryPart($queryPartName); |
|
| 1032 | + |
|
| 1033 | + return $this; |
|
| 1034 | + } |
|
| 1035 | + |
|
| 1036 | + /** |
|
| 1037 | + * Creates a new named parameter and bind the value $value to it. |
|
| 1038 | + * |
|
| 1039 | + * This method provides a shortcut for PDOStatement::bindValue |
|
| 1040 | + * when using prepared statements. |
|
| 1041 | + * |
|
| 1042 | + * The parameter $value specifies the value that you want to bind. If |
|
| 1043 | + * $placeholder is not provided bindValue() will automatically create a |
|
| 1044 | + * placeholder for you. An automatic placeholder will be of the name |
|
| 1045 | + * ':dcValue1', ':dcValue2' etc. |
|
| 1046 | + * |
|
| 1047 | + * For more information see {@link http://php.net/pdostatement-bindparam} |
|
| 1048 | + * |
|
| 1049 | + * Example: |
|
| 1050 | + * <code> |
|
| 1051 | + * $value = 2; |
|
| 1052 | + * $q->eq( 'id', $q->bindValue( $value ) ); |
|
| 1053 | + * $stmt = $q->executeQuery(); // executed with 'id = 2' |
|
| 1054 | + * </code> |
|
| 1055 | + * |
|
| 1056 | + * @license New BSD License |
|
| 1057 | + * @link http://www.zetacomponents.org |
|
| 1058 | + * |
|
| 1059 | + * @param mixed $value |
|
| 1060 | + * @param mixed $type |
|
| 1061 | + * @param string $placeHolder The name to bind with. The string must start with a colon ':'. |
|
| 1062 | + * |
|
| 1063 | + * @return IParameter the placeholder name used. |
|
| 1064 | + */ |
|
| 1065 | + public function createNamedParameter($value, $type = IQueryBuilder::PARAM_STR, $placeHolder = null) { |
|
| 1066 | + return new Parameter($this->queryBuilder->createNamedParameter($value, $type, $placeHolder)); |
|
| 1067 | + } |
|
| 1068 | + |
|
| 1069 | + /** |
|
| 1070 | + * Creates a new positional parameter and bind the given value to it. |
|
| 1071 | + * |
|
| 1072 | + * Attention: If you are using positional parameters with the query builder you have |
|
| 1073 | + * to be very careful to bind all parameters in the order they appear in the SQL |
|
| 1074 | + * statement , otherwise they get bound in the wrong order which can lead to serious |
|
| 1075 | + * bugs in your code. |
|
| 1076 | + * |
|
| 1077 | + * Example: |
|
| 1078 | + * <code> |
|
| 1079 | + * $qb = $conn->getQueryBuilder(); |
|
| 1080 | + * $qb->select('u.*') |
|
| 1081 | + * ->from('users', 'u') |
|
| 1082 | + * ->where('u.username = ' . $qb->createPositionalParameter('Foo', IQueryBuilder::PARAM_STR)) |
|
| 1083 | + * ->orWhere('u.username = ' . $qb->createPositionalParameter('Bar', IQueryBuilder::PARAM_STR)) |
|
| 1084 | + * </code> |
|
| 1085 | + * |
|
| 1086 | + * @param mixed $value |
|
| 1087 | + * @param integer $type |
|
| 1088 | + * |
|
| 1089 | + * @return IParameter |
|
| 1090 | + */ |
|
| 1091 | + public function createPositionalParameter($value, $type = IQueryBuilder::PARAM_STR) { |
|
| 1092 | + return new Parameter($this->queryBuilder->createPositionalParameter($value, $type)); |
|
| 1093 | + } |
|
| 1094 | + |
|
| 1095 | + /** |
|
| 1096 | + * Creates a new parameter |
|
| 1097 | + * |
|
| 1098 | + * Example: |
|
| 1099 | + * <code> |
|
| 1100 | + * $qb = $conn->getQueryBuilder(); |
|
| 1101 | + * $qb->select('u.*') |
|
| 1102 | + * ->from('users', 'u') |
|
| 1103 | + * ->where('u.username = ' . $qb->createParameter('name')) |
|
| 1104 | + * ->setParameter('name', 'Bar', IQueryBuilder::PARAM_STR)) |
|
| 1105 | + * </code> |
|
| 1106 | + * |
|
| 1107 | + * @param string $name |
|
| 1108 | + * |
|
| 1109 | + * @return IParameter |
|
| 1110 | + */ |
|
| 1111 | + public function createParameter($name) { |
|
| 1112 | + return new Parameter(':' . $name); |
|
| 1113 | + } |
|
| 1114 | + |
|
| 1115 | + /** |
|
| 1116 | + * Creates a new function |
|
| 1117 | + * |
|
| 1118 | + * Attention: Column names inside the call have to be quoted before hand |
|
| 1119 | + * |
|
| 1120 | + * Example: |
|
| 1121 | + * <code> |
|
| 1122 | + * $qb = $conn->getQueryBuilder(); |
|
| 1123 | + * $qb->select($qb->createFunction('COUNT(*)')) |
|
| 1124 | + * ->from('users', 'u') |
|
| 1125 | + * echo $qb->getSQL(); // SELECT COUNT(*) FROM `users` u |
|
| 1126 | + * </code> |
|
| 1127 | + * <code> |
|
| 1128 | + * $qb = $conn->getQueryBuilder(); |
|
| 1129 | + * $qb->select($qb->createFunction('COUNT(`column`)')) |
|
| 1130 | + * ->from('users', 'u') |
|
| 1131 | + * echo $qb->getSQL(); // SELECT COUNT(`column`) FROM `users` u |
|
| 1132 | + * </code> |
|
| 1133 | + * |
|
| 1134 | + * @param string $call |
|
| 1135 | + * |
|
| 1136 | + * @return IQueryFunction |
|
| 1137 | + */ |
|
| 1138 | + public function createFunction($call) { |
|
| 1139 | + return new QueryFunction($call); |
|
| 1140 | + } |
|
| 1141 | + |
|
| 1142 | + /** |
|
| 1143 | + * Used to get the id of the last inserted element |
|
| 1144 | + * @return int |
|
| 1145 | + * @throws \BadMethodCallException When being called before an insert query has been run. |
|
| 1146 | + */ |
|
| 1147 | + public function getLastInsertId() { |
|
| 1148 | + if ($this->getType() === \Doctrine\DBAL\Query\QueryBuilder::INSERT && $this->lastInsertedTable) { |
|
| 1149 | + // lastInsertId() needs the prefix but no quotes |
|
| 1150 | + $table = $this->prefixTableName($this->lastInsertedTable); |
|
| 1151 | + return (int) $this->connection->lastInsertId($table); |
|
| 1152 | + } |
|
| 1153 | + |
|
| 1154 | + throw new \BadMethodCallException('Invalid call to getLastInsertId without using insert() before.'); |
|
| 1155 | + } |
|
| 1156 | + |
|
| 1157 | + /** |
|
| 1158 | + * Returns the table name quoted and with database prefix as needed by the implementation |
|
| 1159 | + * |
|
| 1160 | + * @param string $table |
|
| 1161 | + * @return string |
|
| 1162 | + */ |
|
| 1163 | + public function getTableName($table) { |
|
| 1164 | + $table = $this->prefixTableName($table); |
|
| 1165 | + return $this->helper->quoteColumnName($table); |
|
| 1166 | + } |
|
| 1167 | + |
|
| 1168 | + /** |
|
| 1169 | + * Returns the table name with database prefix as needed by the implementation |
|
| 1170 | + * |
|
| 1171 | + * @param string $table |
|
| 1172 | + * @return string |
|
| 1173 | + */ |
|
| 1174 | + protected function prefixTableName($table) { |
|
| 1175 | + if ($this->automaticTablePrefix === false || strpos($table, '*PREFIX*') === 0) { |
|
| 1176 | + return $table; |
|
| 1177 | + } |
|
| 1178 | + |
|
| 1179 | + return '*PREFIX*' . $table; |
|
| 1180 | + } |
|
| 1181 | + |
|
| 1182 | + /** |
|
| 1183 | + * Returns the column name quoted and with table alias prefix as needed by the implementation |
|
| 1184 | + * |
|
| 1185 | + * @param string $column |
|
| 1186 | + * @param string $tableAlias |
|
| 1187 | + * @return string |
|
| 1188 | + */ |
|
| 1189 | + public function getColumnName($column, $tableAlias = '') { |
|
| 1190 | + if ($tableAlias !== '') { |
|
| 1191 | + $tableAlias .= '.'; |
|
| 1192 | + } |
|
| 1193 | + |
|
| 1194 | + return $this->helper->quoteColumnName($tableAlias . $column); |
|
| 1195 | + } |
|
| 1196 | 1196 | } |
@@ -56,842 +56,842 @@ |
||
| 56 | 56 | * - ChangePropagator: updates the mtime and etags of parent folders whenever a change to the cache is made to the cache by the updater |
| 57 | 57 | */ |
| 58 | 58 | class Cache implements ICache { |
| 59 | - use MoveFromCacheTrait { |
|
| 60 | - MoveFromCacheTrait::moveFromCache as moveFromCacheFallback; |
|
| 61 | - } |
|
| 62 | - |
|
| 63 | - /** |
|
| 64 | - * @var array partial data for the cache |
|
| 65 | - */ |
|
| 66 | - protected $partial = array(); |
|
| 67 | - |
|
| 68 | - /** |
|
| 69 | - * @var string |
|
| 70 | - */ |
|
| 71 | - protected $storageId; |
|
| 72 | - |
|
| 73 | - /** |
|
| 74 | - * @var Storage $storageCache |
|
| 75 | - */ |
|
| 76 | - protected $storageCache; |
|
| 77 | - |
|
| 78 | - /** @var IMimeTypeLoader */ |
|
| 79 | - protected $mimetypeLoader; |
|
| 80 | - |
|
| 81 | - /** |
|
| 82 | - * @var IDBConnection |
|
| 83 | - */ |
|
| 84 | - protected $connection; |
|
| 85 | - |
|
| 86 | - /** @var QuerySearchHelper */ |
|
| 87 | - protected $querySearchHelper; |
|
| 88 | - |
|
| 89 | - /** |
|
| 90 | - * @param \OC\Files\Storage\Storage|string $storage |
|
| 91 | - */ |
|
| 92 | - public function __construct($storage) { |
|
| 93 | - if ($storage instanceof \OC\Files\Storage\Storage) { |
|
| 94 | - $this->storageId = $storage->getId(); |
|
| 95 | - } else { |
|
| 96 | - $this->storageId = $storage; |
|
| 97 | - } |
|
| 98 | - if (strlen($this->storageId) > 64) { |
|
| 99 | - $this->storageId = md5($this->storageId); |
|
| 100 | - } |
|
| 101 | - |
|
| 102 | - $this->storageCache = new Storage($storage); |
|
| 103 | - $this->mimetypeLoader = \OC::$server->getMimeTypeLoader(); |
|
| 104 | - $this->connection = \OC::$server->getDatabaseConnection(); |
|
| 105 | - $this->querySearchHelper = new QuerySearchHelper($this->mimetypeLoader); |
|
| 106 | - } |
|
| 107 | - |
|
| 108 | - /** |
|
| 109 | - * Get the numeric storage id for this cache's storage |
|
| 110 | - * |
|
| 111 | - * @return int |
|
| 112 | - */ |
|
| 113 | - public function getNumericStorageId() { |
|
| 114 | - return $this->storageCache->getNumericId(); |
|
| 115 | - } |
|
| 116 | - |
|
| 117 | - /** |
|
| 118 | - * get the stored metadata of a file or folder |
|
| 119 | - * |
|
| 120 | - * @param string | int $file either the path of a file or folder or the file id for a file or folder |
|
| 121 | - * @return ICacheEntry|false the cache entry as array of false if the file is not found in the cache |
|
| 122 | - */ |
|
| 123 | - public function get($file) { |
|
| 124 | - if (is_string($file) or $file == '') { |
|
| 125 | - // normalize file |
|
| 126 | - $file = $this->normalize($file); |
|
| 127 | - |
|
| 128 | - $where = 'WHERE `storage` = ? AND `path_hash` = ?'; |
|
| 129 | - $params = array($this->getNumericStorageId(), md5($file)); |
|
| 130 | - } else { //file id |
|
| 131 | - $where = 'WHERE `fileid` = ?'; |
|
| 132 | - $params = array($file); |
|
| 133 | - } |
|
| 134 | - $sql = 'SELECT `fileid`, `storage`, `path`, `path_hash`, `parent`, `name`, `mimetype`, `mimepart`, `size`, `mtime`, |
|
| 59 | + use MoveFromCacheTrait { |
|
| 60 | + MoveFromCacheTrait::moveFromCache as moveFromCacheFallback; |
|
| 61 | + } |
|
| 62 | + |
|
| 63 | + /** |
|
| 64 | + * @var array partial data for the cache |
|
| 65 | + */ |
|
| 66 | + protected $partial = array(); |
|
| 67 | + |
|
| 68 | + /** |
|
| 69 | + * @var string |
|
| 70 | + */ |
|
| 71 | + protected $storageId; |
|
| 72 | + |
|
| 73 | + /** |
|
| 74 | + * @var Storage $storageCache |
|
| 75 | + */ |
|
| 76 | + protected $storageCache; |
|
| 77 | + |
|
| 78 | + /** @var IMimeTypeLoader */ |
|
| 79 | + protected $mimetypeLoader; |
|
| 80 | + |
|
| 81 | + /** |
|
| 82 | + * @var IDBConnection |
|
| 83 | + */ |
|
| 84 | + protected $connection; |
|
| 85 | + |
|
| 86 | + /** @var QuerySearchHelper */ |
|
| 87 | + protected $querySearchHelper; |
|
| 88 | + |
|
| 89 | + /** |
|
| 90 | + * @param \OC\Files\Storage\Storage|string $storage |
|
| 91 | + */ |
|
| 92 | + public function __construct($storage) { |
|
| 93 | + if ($storage instanceof \OC\Files\Storage\Storage) { |
|
| 94 | + $this->storageId = $storage->getId(); |
|
| 95 | + } else { |
|
| 96 | + $this->storageId = $storage; |
|
| 97 | + } |
|
| 98 | + if (strlen($this->storageId) > 64) { |
|
| 99 | + $this->storageId = md5($this->storageId); |
|
| 100 | + } |
|
| 101 | + |
|
| 102 | + $this->storageCache = new Storage($storage); |
|
| 103 | + $this->mimetypeLoader = \OC::$server->getMimeTypeLoader(); |
|
| 104 | + $this->connection = \OC::$server->getDatabaseConnection(); |
|
| 105 | + $this->querySearchHelper = new QuerySearchHelper($this->mimetypeLoader); |
|
| 106 | + } |
|
| 107 | + |
|
| 108 | + /** |
|
| 109 | + * Get the numeric storage id for this cache's storage |
|
| 110 | + * |
|
| 111 | + * @return int |
|
| 112 | + */ |
|
| 113 | + public function getNumericStorageId() { |
|
| 114 | + return $this->storageCache->getNumericId(); |
|
| 115 | + } |
|
| 116 | + |
|
| 117 | + /** |
|
| 118 | + * get the stored metadata of a file or folder |
|
| 119 | + * |
|
| 120 | + * @param string | int $file either the path of a file or folder or the file id for a file or folder |
|
| 121 | + * @return ICacheEntry|false the cache entry as array of false if the file is not found in the cache |
|
| 122 | + */ |
|
| 123 | + public function get($file) { |
|
| 124 | + if (is_string($file) or $file == '') { |
|
| 125 | + // normalize file |
|
| 126 | + $file = $this->normalize($file); |
|
| 127 | + |
|
| 128 | + $where = 'WHERE `storage` = ? AND `path_hash` = ?'; |
|
| 129 | + $params = array($this->getNumericStorageId(), md5($file)); |
|
| 130 | + } else { //file id |
|
| 131 | + $where = 'WHERE `fileid` = ?'; |
|
| 132 | + $params = array($file); |
|
| 133 | + } |
|
| 134 | + $sql = 'SELECT `fileid`, `storage`, `path`, `path_hash`, `parent`, `name`, `mimetype`, `mimepart`, `size`, `mtime`, |
|
| 135 | 135 | `storage_mtime`, `encrypted`, `etag`, `permissions`, `checksum` |
| 136 | 136 | FROM `*PREFIX*filecache` ' . $where; |
| 137 | - $result = $this->connection->executeQuery($sql, $params); |
|
| 138 | - $data = $result->fetch(); |
|
| 139 | - |
|
| 140 | - //FIXME hide this HACK in the next database layer, or just use doctrine and get rid of MDB2 and PDO |
|
| 141 | - //PDO returns false, MDB2 returns null, oracle always uses MDB2, so convert null to false |
|
| 142 | - if ($data === null) { |
|
| 143 | - $data = false; |
|
| 144 | - } |
|
| 145 | - |
|
| 146 | - //merge partial data |
|
| 147 | - if (!$data and is_string($file)) { |
|
| 148 | - if (isset($this->partial[$file])) { |
|
| 149 | - $data = $this->partial[$file]; |
|
| 150 | - } |
|
| 151 | - return $data; |
|
| 152 | - } else { |
|
| 153 | - return self::cacheEntryFromData($data, $this->mimetypeLoader); |
|
| 154 | - } |
|
| 155 | - } |
|
| 156 | - |
|
| 157 | - /** |
|
| 158 | - * Create a CacheEntry from database row |
|
| 159 | - * |
|
| 160 | - * @param array $data |
|
| 161 | - * @param IMimeTypeLoader $mimetypeLoader |
|
| 162 | - * @return CacheEntry |
|
| 163 | - */ |
|
| 164 | - public static function cacheEntryFromData($data, IMimeTypeLoader $mimetypeLoader) { |
|
| 165 | - //fix types |
|
| 166 | - $data['fileid'] = (int)$data['fileid']; |
|
| 167 | - $data['parent'] = (int)$data['parent']; |
|
| 168 | - $data['size'] = 0 + $data['size']; |
|
| 169 | - $data['mtime'] = (int)$data['mtime']; |
|
| 170 | - $data['storage_mtime'] = (int)$data['storage_mtime']; |
|
| 171 | - $data['encryptedVersion'] = (int)$data['encrypted']; |
|
| 172 | - $data['encrypted'] = (bool)$data['encrypted']; |
|
| 173 | - $data['storage_id'] = $data['storage']; |
|
| 174 | - $data['storage'] = (int)$data['storage']; |
|
| 175 | - $data['mimetype'] = $mimetypeLoader->getMimetypeById($data['mimetype']); |
|
| 176 | - $data['mimepart'] = $mimetypeLoader->getMimetypeById($data['mimepart']); |
|
| 177 | - if ($data['storage_mtime'] == 0) { |
|
| 178 | - $data['storage_mtime'] = $data['mtime']; |
|
| 179 | - } |
|
| 180 | - $data['permissions'] = (int)$data['permissions']; |
|
| 181 | - return new CacheEntry($data); |
|
| 182 | - } |
|
| 183 | - |
|
| 184 | - /** |
|
| 185 | - * get the metadata of all files stored in $folder |
|
| 186 | - * |
|
| 187 | - * @param string $folder |
|
| 188 | - * @return ICacheEntry[] |
|
| 189 | - */ |
|
| 190 | - public function getFolderContents($folder) { |
|
| 191 | - $fileId = $this->getId($folder); |
|
| 192 | - return $this->getFolderContentsById($fileId); |
|
| 193 | - } |
|
| 194 | - |
|
| 195 | - /** |
|
| 196 | - * get the metadata of all files stored in $folder |
|
| 197 | - * |
|
| 198 | - * @param int $fileId the file id of the folder |
|
| 199 | - * @return ICacheEntry[] |
|
| 200 | - */ |
|
| 201 | - public function getFolderContentsById($fileId) { |
|
| 202 | - if ($fileId > -1) { |
|
| 203 | - $sql = 'SELECT `fileid`, `storage`, `path`, `parent`, `name`, `mimetype`, `mimepart`, `size`, `mtime`, |
|
| 137 | + $result = $this->connection->executeQuery($sql, $params); |
|
| 138 | + $data = $result->fetch(); |
|
| 139 | + |
|
| 140 | + //FIXME hide this HACK in the next database layer, or just use doctrine and get rid of MDB2 and PDO |
|
| 141 | + //PDO returns false, MDB2 returns null, oracle always uses MDB2, so convert null to false |
|
| 142 | + if ($data === null) { |
|
| 143 | + $data = false; |
|
| 144 | + } |
|
| 145 | + |
|
| 146 | + //merge partial data |
|
| 147 | + if (!$data and is_string($file)) { |
|
| 148 | + if (isset($this->partial[$file])) { |
|
| 149 | + $data = $this->partial[$file]; |
|
| 150 | + } |
|
| 151 | + return $data; |
|
| 152 | + } else { |
|
| 153 | + return self::cacheEntryFromData($data, $this->mimetypeLoader); |
|
| 154 | + } |
|
| 155 | + } |
|
| 156 | + |
|
| 157 | + /** |
|
| 158 | + * Create a CacheEntry from database row |
|
| 159 | + * |
|
| 160 | + * @param array $data |
|
| 161 | + * @param IMimeTypeLoader $mimetypeLoader |
|
| 162 | + * @return CacheEntry |
|
| 163 | + */ |
|
| 164 | + public static function cacheEntryFromData($data, IMimeTypeLoader $mimetypeLoader) { |
|
| 165 | + //fix types |
|
| 166 | + $data['fileid'] = (int)$data['fileid']; |
|
| 167 | + $data['parent'] = (int)$data['parent']; |
|
| 168 | + $data['size'] = 0 + $data['size']; |
|
| 169 | + $data['mtime'] = (int)$data['mtime']; |
|
| 170 | + $data['storage_mtime'] = (int)$data['storage_mtime']; |
|
| 171 | + $data['encryptedVersion'] = (int)$data['encrypted']; |
|
| 172 | + $data['encrypted'] = (bool)$data['encrypted']; |
|
| 173 | + $data['storage_id'] = $data['storage']; |
|
| 174 | + $data['storage'] = (int)$data['storage']; |
|
| 175 | + $data['mimetype'] = $mimetypeLoader->getMimetypeById($data['mimetype']); |
|
| 176 | + $data['mimepart'] = $mimetypeLoader->getMimetypeById($data['mimepart']); |
|
| 177 | + if ($data['storage_mtime'] == 0) { |
|
| 178 | + $data['storage_mtime'] = $data['mtime']; |
|
| 179 | + } |
|
| 180 | + $data['permissions'] = (int)$data['permissions']; |
|
| 181 | + return new CacheEntry($data); |
|
| 182 | + } |
|
| 183 | + |
|
| 184 | + /** |
|
| 185 | + * get the metadata of all files stored in $folder |
|
| 186 | + * |
|
| 187 | + * @param string $folder |
|
| 188 | + * @return ICacheEntry[] |
|
| 189 | + */ |
|
| 190 | + public function getFolderContents($folder) { |
|
| 191 | + $fileId = $this->getId($folder); |
|
| 192 | + return $this->getFolderContentsById($fileId); |
|
| 193 | + } |
|
| 194 | + |
|
| 195 | + /** |
|
| 196 | + * get the metadata of all files stored in $folder |
|
| 197 | + * |
|
| 198 | + * @param int $fileId the file id of the folder |
|
| 199 | + * @return ICacheEntry[] |
|
| 200 | + */ |
|
| 201 | + public function getFolderContentsById($fileId) { |
|
| 202 | + if ($fileId > -1) { |
|
| 203 | + $sql = 'SELECT `fileid`, `storage`, `path`, `parent`, `name`, `mimetype`, `mimepart`, `size`, `mtime`, |
|
| 204 | 204 | `storage_mtime`, `encrypted`, `etag`, `permissions`, `checksum` |
| 205 | 205 | FROM `*PREFIX*filecache` WHERE `parent` = ? ORDER BY `name` ASC'; |
| 206 | - $result = $this->connection->executeQuery($sql, [$fileId]); |
|
| 207 | - $files = $result->fetchAll(); |
|
| 208 | - return array_map(function (array $data) { |
|
| 209 | - return self::cacheEntryFromData($data, $this->mimetypeLoader);; |
|
| 210 | - }, $files); |
|
| 211 | - } else { |
|
| 212 | - return array(); |
|
| 213 | - } |
|
| 214 | - } |
|
| 215 | - |
|
| 216 | - /** |
|
| 217 | - * insert or update meta data for a file or folder |
|
| 218 | - * |
|
| 219 | - * @param string $file |
|
| 220 | - * @param array $data |
|
| 221 | - * |
|
| 222 | - * @return int file id |
|
| 223 | - * @throws \RuntimeException |
|
| 224 | - */ |
|
| 225 | - public function put($file, array $data) { |
|
| 226 | - if (($id = $this->getId($file)) > -1) { |
|
| 227 | - $this->update($id, $data); |
|
| 228 | - return $id; |
|
| 229 | - } else { |
|
| 230 | - return $this->insert($file, $data); |
|
| 231 | - } |
|
| 232 | - } |
|
| 233 | - |
|
| 234 | - /** |
|
| 235 | - * insert meta data for a new file or folder |
|
| 236 | - * |
|
| 237 | - * @param string $file |
|
| 238 | - * @param array $data |
|
| 239 | - * |
|
| 240 | - * @return int file id |
|
| 241 | - * @throws \RuntimeException |
|
| 242 | - */ |
|
| 243 | - public function insert($file, array $data) { |
|
| 244 | - // normalize file |
|
| 245 | - $file = $this->normalize($file); |
|
| 246 | - |
|
| 247 | - if (isset($this->partial[$file])) { //add any saved partial data |
|
| 248 | - $data = array_merge($this->partial[$file], $data); |
|
| 249 | - unset($this->partial[$file]); |
|
| 250 | - } |
|
| 251 | - |
|
| 252 | - $requiredFields = array('size', 'mtime', 'mimetype'); |
|
| 253 | - foreach ($requiredFields as $field) { |
|
| 254 | - if (!isset($data[$field])) { //data not complete save as partial and return |
|
| 255 | - $this->partial[$file] = $data; |
|
| 256 | - return -1; |
|
| 257 | - } |
|
| 258 | - } |
|
| 259 | - |
|
| 260 | - $data['path'] = $file; |
|
| 261 | - $data['parent'] = $this->getParentId($file); |
|
| 262 | - $data['name'] = \OC_Util::basename($file); |
|
| 263 | - |
|
| 264 | - list($queryParts, $params) = $this->buildParts($data); |
|
| 265 | - $queryParts[] = '`storage`'; |
|
| 266 | - $params[] = $this->getNumericStorageId(); |
|
| 267 | - |
|
| 268 | - $queryParts = array_map(function ($item) { |
|
| 269 | - return trim($item, "`"); |
|
| 270 | - }, $queryParts); |
|
| 271 | - $values = array_combine($queryParts, $params); |
|
| 272 | - if (\OC::$server->getDatabaseConnection()->insertIfNotExist('*PREFIX*filecache', $values, [ |
|
| 273 | - 'storage', |
|
| 274 | - 'path_hash', |
|
| 275 | - ]) |
|
| 276 | - ) { |
|
| 277 | - return (int)$this->connection->lastInsertId('*PREFIX*filecache'); |
|
| 278 | - } |
|
| 279 | - |
|
| 280 | - // The file was created in the mean time |
|
| 281 | - if (($id = $this->getId($file)) > -1) { |
|
| 282 | - $this->update($id, $data); |
|
| 283 | - return $id; |
|
| 284 | - } else { |
|
| 285 | - throw new \RuntimeException('File entry could not be inserted with insertIfNotExist() but could also not be selected with getId() in order to perform an update. Please try again.'); |
|
| 286 | - } |
|
| 287 | - } |
|
| 288 | - |
|
| 289 | - /** |
|
| 290 | - * update the metadata of an existing file or folder in the cache |
|
| 291 | - * |
|
| 292 | - * @param int $id the fileid of the existing file or folder |
|
| 293 | - * @param array $data [$key => $value] the metadata to update, only the fields provided in the array will be updated, non-provided values will remain unchanged |
|
| 294 | - */ |
|
| 295 | - public function update($id, array $data) { |
|
| 296 | - |
|
| 297 | - if (isset($data['path'])) { |
|
| 298 | - // normalize path |
|
| 299 | - $data['path'] = $this->normalize($data['path']); |
|
| 300 | - } |
|
| 301 | - |
|
| 302 | - if (isset($data['name'])) { |
|
| 303 | - // normalize path |
|
| 304 | - $data['name'] = $this->normalize($data['name']); |
|
| 305 | - } |
|
| 306 | - |
|
| 307 | - list($queryParts, $params) = $this->buildParts($data); |
|
| 308 | - // duplicate $params because we need the parts twice in the SQL statement |
|
| 309 | - // once for the SET part, once in the WHERE clause |
|
| 310 | - $params = array_merge($params, $params); |
|
| 311 | - $params[] = $id; |
|
| 312 | - |
|
| 313 | - // don't update if the data we try to set is the same as the one in the record |
|
| 314 | - // some databases (Postgres) don't like superfluous updates |
|
| 315 | - $sql = 'UPDATE `*PREFIX*filecache` SET ' . implode(' = ?, ', $queryParts) . '=? ' . |
|
| 316 | - 'WHERE (' . |
|
| 317 | - implode(' <> ? OR ', $queryParts) . ' <> ? OR ' . |
|
| 318 | - implode(' IS NULL OR ', $queryParts) . ' IS NULL' . |
|
| 319 | - ') AND `fileid` = ? '; |
|
| 320 | - $this->connection->executeQuery($sql, $params); |
|
| 321 | - |
|
| 322 | - } |
|
| 323 | - |
|
| 324 | - /** |
|
| 325 | - * extract query parts and params array from data array |
|
| 326 | - * |
|
| 327 | - * @param array $data |
|
| 328 | - * @return array [$queryParts, $params] |
|
| 329 | - * $queryParts: string[], the (escaped) column names to be set in the query |
|
| 330 | - * $params: mixed[], the new values for the columns, to be passed as params to the query |
|
| 331 | - */ |
|
| 332 | - protected function buildParts(array $data) { |
|
| 333 | - $fields = array( |
|
| 334 | - 'path', 'parent', 'name', 'mimetype', 'size', 'mtime', 'storage_mtime', 'encrypted', |
|
| 335 | - 'etag', 'permissions', 'checksum'); |
|
| 336 | - |
|
| 337 | - $doNotCopyStorageMTime = false; |
|
| 338 | - if (array_key_exists('mtime', $data) && $data['mtime'] === null) { |
|
| 339 | - // this horrific magic tells it to not copy storage_mtime to mtime |
|
| 340 | - unset($data['mtime']); |
|
| 341 | - $doNotCopyStorageMTime = true; |
|
| 342 | - } |
|
| 343 | - |
|
| 344 | - $params = array(); |
|
| 345 | - $queryParts = array(); |
|
| 346 | - foreach ($data as $name => $value) { |
|
| 347 | - if (array_search($name, $fields) !== false) { |
|
| 348 | - if ($name === 'path') { |
|
| 349 | - $params[] = md5($value); |
|
| 350 | - $queryParts[] = '`path_hash`'; |
|
| 351 | - } elseif ($name === 'mimetype') { |
|
| 352 | - $params[] = $this->mimetypeLoader->getId(substr($value, 0, strpos($value, '/'))); |
|
| 353 | - $queryParts[] = '`mimepart`'; |
|
| 354 | - $value = $this->mimetypeLoader->getId($value); |
|
| 355 | - } elseif ($name === 'storage_mtime') { |
|
| 356 | - if (!$doNotCopyStorageMTime && !isset($data['mtime'])) { |
|
| 357 | - $params[] = $value; |
|
| 358 | - $queryParts[] = '`mtime`'; |
|
| 359 | - } |
|
| 360 | - } elseif ($name === 'encrypted') { |
|
| 361 | - if (isset($data['encryptedVersion'])) { |
|
| 362 | - $value = $data['encryptedVersion']; |
|
| 363 | - } else { |
|
| 364 | - // Boolean to integer conversion |
|
| 365 | - $value = $value ? 1 : 0; |
|
| 366 | - } |
|
| 367 | - } |
|
| 368 | - $params[] = $value; |
|
| 369 | - $queryParts[] = '`' . $name . '`'; |
|
| 370 | - } |
|
| 371 | - } |
|
| 372 | - return array($queryParts, $params); |
|
| 373 | - } |
|
| 374 | - |
|
| 375 | - /** |
|
| 376 | - * get the file id for a file |
|
| 377 | - * |
|
| 378 | - * A file id is a numeric id for a file or folder that's unique within an owncloud instance which stays the same for the lifetime of a file |
|
| 379 | - * |
|
| 380 | - * File ids are easiest way for apps to store references to a file since unlike paths they are not affected by renames or sharing |
|
| 381 | - * |
|
| 382 | - * @param string $file |
|
| 383 | - * @return int |
|
| 384 | - */ |
|
| 385 | - public function getId($file) { |
|
| 386 | - // normalize file |
|
| 387 | - $file = $this->normalize($file); |
|
| 388 | - |
|
| 389 | - $pathHash = md5($file); |
|
| 390 | - |
|
| 391 | - $sql = 'SELECT `fileid` FROM `*PREFIX*filecache` WHERE `storage` = ? AND `path_hash` = ?'; |
|
| 392 | - $result = $this->connection->executeQuery($sql, array($this->getNumericStorageId(), $pathHash)); |
|
| 393 | - if ($row = $result->fetch()) { |
|
| 394 | - return $row['fileid']; |
|
| 395 | - } else { |
|
| 396 | - return -1; |
|
| 397 | - } |
|
| 398 | - } |
|
| 399 | - |
|
| 400 | - /** |
|
| 401 | - * get the id of the parent folder of a file |
|
| 402 | - * |
|
| 403 | - * @param string $file |
|
| 404 | - * @return int |
|
| 405 | - */ |
|
| 406 | - public function getParentId($file) { |
|
| 407 | - if ($file === '') { |
|
| 408 | - return -1; |
|
| 409 | - } else { |
|
| 410 | - $parent = $this->getParentPath($file); |
|
| 411 | - return (int)$this->getId($parent); |
|
| 412 | - } |
|
| 413 | - } |
|
| 414 | - |
|
| 415 | - private function getParentPath($path) { |
|
| 416 | - $parent = dirname($path); |
|
| 417 | - if ($parent === '.') { |
|
| 418 | - $parent = ''; |
|
| 419 | - } |
|
| 420 | - return $parent; |
|
| 421 | - } |
|
| 422 | - |
|
| 423 | - /** |
|
| 424 | - * check if a file is available in the cache |
|
| 425 | - * |
|
| 426 | - * @param string $file |
|
| 427 | - * @return bool |
|
| 428 | - */ |
|
| 429 | - public function inCache($file) { |
|
| 430 | - return $this->getId($file) != -1; |
|
| 431 | - } |
|
| 432 | - |
|
| 433 | - /** |
|
| 434 | - * remove a file or folder from the cache |
|
| 435 | - * |
|
| 436 | - * when removing a folder from the cache all files and folders inside the folder will be removed as well |
|
| 437 | - * |
|
| 438 | - * @param string $file |
|
| 439 | - */ |
|
| 440 | - public function remove($file) { |
|
| 441 | - $entry = $this->get($file); |
|
| 442 | - $sql = 'DELETE FROM `*PREFIX*filecache` WHERE `fileid` = ?'; |
|
| 443 | - $this->connection->executeQuery($sql, array($entry['fileid'])); |
|
| 444 | - if ($entry['mimetype'] === 'httpd/unix-directory') { |
|
| 445 | - $this->removeChildren($entry); |
|
| 446 | - } |
|
| 447 | - } |
|
| 448 | - |
|
| 449 | - /** |
|
| 450 | - * Get all sub folders of a folder |
|
| 451 | - * |
|
| 452 | - * @param array $entry the cache entry of the folder to get the subfolders for |
|
| 453 | - * @return array[] the cache entries for the subfolders |
|
| 454 | - */ |
|
| 455 | - private function getSubFolders($entry) { |
|
| 456 | - $children = $this->getFolderContentsById($entry['fileid']); |
|
| 457 | - return array_filter($children, function ($child) { |
|
| 458 | - return $child['mimetype'] === 'httpd/unix-directory'; |
|
| 459 | - }); |
|
| 460 | - } |
|
| 461 | - |
|
| 462 | - /** |
|
| 463 | - * Recursively remove all children of a folder |
|
| 464 | - * |
|
| 465 | - * @param array $entry the cache entry of the folder to remove the children of |
|
| 466 | - * @throws \OC\DatabaseException |
|
| 467 | - */ |
|
| 468 | - private function removeChildren($entry) { |
|
| 469 | - $subFolders = $this->getSubFolders($entry); |
|
| 470 | - foreach ($subFolders as $folder) { |
|
| 471 | - $this->removeChildren($folder); |
|
| 472 | - } |
|
| 473 | - $sql = 'DELETE FROM `*PREFIX*filecache` WHERE `parent` = ?'; |
|
| 474 | - $this->connection->executeQuery($sql, array($entry['fileid'])); |
|
| 475 | - } |
|
| 476 | - |
|
| 477 | - /** |
|
| 478 | - * Move a file or folder in the cache |
|
| 479 | - * |
|
| 480 | - * @param string $source |
|
| 481 | - * @param string $target |
|
| 482 | - */ |
|
| 483 | - public function move($source, $target) { |
|
| 484 | - $this->moveFromCache($this, $source, $target); |
|
| 485 | - } |
|
| 486 | - |
|
| 487 | - /** |
|
| 488 | - * Get the storage id and path needed for a move |
|
| 489 | - * |
|
| 490 | - * @param string $path |
|
| 491 | - * @return array [$storageId, $internalPath] |
|
| 492 | - */ |
|
| 493 | - protected function getMoveInfo($path) { |
|
| 494 | - return [$this->getNumericStorageId(), $path]; |
|
| 495 | - } |
|
| 496 | - |
|
| 497 | - /** |
|
| 498 | - * Move a file or folder in the cache |
|
| 499 | - * |
|
| 500 | - * @param \OCP\Files\Cache\ICache $sourceCache |
|
| 501 | - * @param string $sourcePath |
|
| 502 | - * @param string $targetPath |
|
| 503 | - * @throws \OC\DatabaseException |
|
| 504 | - * @throws \Exception if the given storages have an invalid id |
|
| 505 | - */ |
|
| 506 | - public function moveFromCache(ICache $sourceCache, $sourcePath, $targetPath) { |
|
| 507 | - if ($sourceCache instanceof Cache) { |
|
| 508 | - // normalize source and target |
|
| 509 | - $sourcePath = $this->normalize($sourcePath); |
|
| 510 | - $targetPath = $this->normalize($targetPath); |
|
| 511 | - |
|
| 512 | - $sourceData = $sourceCache->get($sourcePath); |
|
| 513 | - $sourceId = $sourceData['fileid']; |
|
| 514 | - $newParentId = $this->getParentId($targetPath); |
|
| 515 | - |
|
| 516 | - list($sourceStorageId, $sourcePath) = $sourceCache->getMoveInfo($sourcePath); |
|
| 517 | - list($targetStorageId, $targetPath) = $this->getMoveInfo($targetPath); |
|
| 518 | - |
|
| 519 | - if (is_null($sourceStorageId) || $sourceStorageId === false) { |
|
| 520 | - throw new \Exception('Invalid source storage id: ' . $sourceStorageId); |
|
| 521 | - } |
|
| 522 | - if (is_null($targetStorageId) || $targetStorageId === false) { |
|
| 523 | - throw new \Exception('Invalid target storage id: ' . $targetStorageId); |
|
| 524 | - } |
|
| 525 | - |
|
| 526 | - $this->connection->beginTransaction(); |
|
| 527 | - if ($sourceData['mimetype'] === 'httpd/unix-directory') { |
|
| 528 | - //update all child entries |
|
| 529 | - $sourceLength = strlen($sourcePath); |
|
| 530 | - $query = $this->connection->getQueryBuilder(); |
|
| 531 | - |
|
| 532 | - $fun = $query->func(); |
|
| 533 | - $newPathFunction = $fun->concat( |
|
| 534 | - $query->createNamedParameter($targetPath), |
|
| 535 | - $fun->substring('path', $query->createNamedParameter($sourceLength + 1, IQueryBuilder::PARAM_INT))// +1 for the leading slash |
|
| 536 | - ); |
|
| 537 | - $query->update('filecache') |
|
| 538 | - ->set('storage', $query->createNamedParameter($targetStorageId, IQueryBuilder::PARAM_INT)) |
|
| 539 | - ->set('path_hash', $fun->md5($newPathFunction)) |
|
| 540 | - ->set('path', $newPathFunction) |
|
| 541 | - ->where($query->expr()->eq('storage', $query->createNamedParameter($sourceStorageId, IQueryBuilder::PARAM_INT))) |
|
| 542 | - ->andWhere($query->expr()->like('path', $query->createNamedParameter($this->connection->escapeLikeParameter($sourcePath) . '/%'))); |
|
| 543 | - |
|
| 544 | - try { |
|
| 545 | - $query->execute(); |
|
| 546 | - } catch (\OC\DatabaseException $e) { |
|
| 547 | - $this->connection->rollBack(); |
|
| 548 | - throw $e; |
|
| 549 | - } |
|
| 550 | - } |
|
| 551 | - |
|
| 552 | - $sql = 'UPDATE `*PREFIX*filecache` SET `storage` = ?, `path` = ?, `path_hash` = ?, `name` = ?, `parent` = ? WHERE `fileid` = ?'; |
|
| 553 | - $this->connection->executeQuery($sql, array($targetStorageId, $targetPath, md5($targetPath), \OC_Util::basename($targetPath), $newParentId, $sourceId)); |
|
| 554 | - $this->connection->commit(); |
|
| 555 | - } else { |
|
| 556 | - $this->moveFromCacheFallback($sourceCache, $sourcePath, $targetPath); |
|
| 557 | - } |
|
| 558 | - } |
|
| 559 | - |
|
| 560 | - /** |
|
| 561 | - * remove all entries for files that are stored on the storage from the cache |
|
| 562 | - */ |
|
| 563 | - public function clear() { |
|
| 564 | - $sql = 'DELETE FROM `*PREFIX*filecache` WHERE `storage` = ?'; |
|
| 565 | - $this->connection->executeQuery($sql, array($this->getNumericStorageId())); |
|
| 566 | - |
|
| 567 | - $sql = 'DELETE FROM `*PREFIX*storages` WHERE `id` = ?'; |
|
| 568 | - $this->connection->executeQuery($sql, array($this->storageId)); |
|
| 569 | - } |
|
| 570 | - |
|
| 571 | - /** |
|
| 572 | - * Get the scan status of a file |
|
| 573 | - * |
|
| 574 | - * - Cache::NOT_FOUND: File is not in the cache |
|
| 575 | - * - Cache::PARTIAL: File is not stored in the cache but some incomplete data is known |
|
| 576 | - * - Cache::SHALLOW: The folder and it's direct children are in the cache but not all sub folders are fully scanned |
|
| 577 | - * - Cache::COMPLETE: The file or folder, with all it's children) are fully scanned |
|
| 578 | - * |
|
| 579 | - * @param string $file |
|
| 580 | - * |
|
| 581 | - * @return int Cache::NOT_FOUND, Cache::PARTIAL, Cache::SHALLOW or Cache::COMPLETE |
|
| 582 | - */ |
|
| 583 | - public function getStatus($file) { |
|
| 584 | - // normalize file |
|
| 585 | - $file = $this->normalize($file); |
|
| 586 | - |
|
| 587 | - $pathHash = md5($file); |
|
| 588 | - $sql = 'SELECT `size` FROM `*PREFIX*filecache` WHERE `storage` = ? AND `path_hash` = ?'; |
|
| 589 | - $result = $this->connection->executeQuery($sql, array($this->getNumericStorageId(), $pathHash)); |
|
| 590 | - if ($row = $result->fetch()) { |
|
| 591 | - if ((int)$row['size'] === -1) { |
|
| 592 | - return self::SHALLOW; |
|
| 593 | - } else { |
|
| 594 | - return self::COMPLETE; |
|
| 595 | - } |
|
| 596 | - } else { |
|
| 597 | - if (isset($this->partial[$file])) { |
|
| 598 | - return self::PARTIAL; |
|
| 599 | - } else { |
|
| 600 | - return self::NOT_FOUND; |
|
| 601 | - } |
|
| 602 | - } |
|
| 603 | - } |
|
| 604 | - |
|
| 605 | - /** |
|
| 606 | - * search for files matching $pattern |
|
| 607 | - * |
|
| 608 | - * @param string $pattern the search pattern using SQL search syntax (e.g. '%searchstring%') |
|
| 609 | - * @return ICacheEntry[] an array of cache entries where the name matches the search pattern |
|
| 610 | - */ |
|
| 611 | - public function search($pattern) { |
|
| 612 | - // normalize pattern |
|
| 613 | - $pattern = $this->normalize($pattern); |
|
| 614 | - |
|
| 615 | - if ($pattern === '%%') { |
|
| 616 | - return []; |
|
| 617 | - } |
|
| 618 | - |
|
| 619 | - |
|
| 620 | - $sql = ' |
|
| 206 | + $result = $this->connection->executeQuery($sql, [$fileId]); |
|
| 207 | + $files = $result->fetchAll(); |
|
| 208 | + return array_map(function (array $data) { |
|
| 209 | + return self::cacheEntryFromData($data, $this->mimetypeLoader);; |
|
| 210 | + }, $files); |
|
| 211 | + } else { |
|
| 212 | + return array(); |
|
| 213 | + } |
|
| 214 | + } |
|
| 215 | + |
|
| 216 | + /** |
|
| 217 | + * insert or update meta data for a file or folder |
|
| 218 | + * |
|
| 219 | + * @param string $file |
|
| 220 | + * @param array $data |
|
| 221 | + * |
|
| 222 | + * @return int file id |
|
| 223 | + * @throws \RuntimeException |
|
| 224 | + */ |
|
| 225 | + public function put($file, array $data) { |
|
| 226 | + if (($id = $this->getId($file)) > -1) { |
|
| 227 | + $this->update($id, $data); |
|
| 228 | + return $id; |
|
| 229 | + } else { |
|
| 230 | + return $this->insert($file, $data); |
|
| 231 | + } |
|
| 232 | + } |
|
| 233 | + |
|
| 234 | + /** |
|
| 235 | + * insert meta data for a new file or folder |
|
| 236 | + * |
|
| 237 | + * @param string $file |
|
| 238 | + * @param array $data |
|
| 239 | + * |
|
| 240 | + * @return int file id |
|
| 241 | + * @throws \RuntimeException |
|
| 242 | + */ |
|
| 243 | + public function insert($file, array $data) { |
|
| 244 | + // normalize file |
|
| 245 | + $file = $this->normalize($file); |
|
| 246 | + |
|
| 247 | + if (isset($this->partial[$file])) { //add any saved partial data |
|
| 248 | + $data = array_merge($this->partial[$file], $data); |
|
| 249 | + unset($this->partial[$file]); |
|
| 250 | + } |
|
| 251 | + |
|
| 252 | + $requiredFields = array('size', 'mtime', 'mimetype'); |
|
| 253 | + foreach ($requiredFields as $field) { |
|
| 254 | + if (!isset($data[$field])) { //data not complete save as partial and return |
|
| 255 | + $this->partial[$file] = $data; |
|
| 256 | + return -1; |
|
| 257 | + } |
|
| 258 | + } |
|
| 259 | + |
|
| 260 | + $data['path'] = $file; |
|
| 261 | + $data['parent'] = $this->getParentId($file); |
|
| 262 | + $data['name'] = \OC_Util::basename($file); |
|
| 263 | + |
|
| 264 | + list($queryParts, $params) = $this->buildParts($data); |
|
| 265 | + $queryParts[] = '`storage`'; |
|
| 266 | + $params[] = $this->getNumericStorageId(); |
|
| 267 | + |
|
| 268 | + $queryParts = array_map(function ($item) { |
|
| 269 | + return trim($item, "`"); |
|
| 270 | + }, $queryParts); |
|
| 271 | + $values = array_combine($queryParts, $params); |
|
| 272 | + if (\OC::$server->getDatabaseConnection()->insertIfNotExist('*PREFIX*filecache', $values, [ |
|
| 273 | + 'storage', |
|
| 274 | + 'path_hash', |
|
| 275 | + ]) |
|
| 276 | + ) { |
|
| 277 | + return (int)$this->connection->lastInsertId('*PREFIX*filecache'); |
|
| 278 | + } |
|
| 279 | + |
|
| 280 | + // The file was created in the mean time |
|
| 281 | + if (($id = $this->getId($file)) > -1) { |
|
| 282 | + $this->update($id, $data); |
|
| 283 | + return $id; |
|
| 284 | + } else { |
|
| 285 | + throw new \RuntimeException('File entry could not be inserted with insertIfNotExist() but could also not be selected with getId() in order to perform an update. Please try again.'); |
|
| 286 | + } |
|
| 287 | + } |
|
| 288 | + |
|
| 289 | + /** |
|
| 290 | + * update the metadata of an existing file or folder in the cache |
|
| 291 | + * |
|
| 292 | + * @param int $id the fileid of the existing file or folder |
|
| 293 | + * @param array $data [$key => $value] the metadata to update, only the fields provided in the array will be updated, non-provided values will remain unchanged |
|
| 294 | + */ |
|
| 295 | + public function update($id, array $data) { |
|
| 296 | + |
|
| 297 | + if (isset($data['path'])) { |
|
| 298 | + // normalize path |
|
| 299 | + $data['path'] = $this->normalize($data['path']); |
|
| 300 | + } |
|
| 301 | + |
|
| 302 | + if (isset($data['name'])) { |
|
| 303 | + // normalize path |
|
| 304 | + $data['name'] = $this->normalize($data['name']); |
|
| 305 | + } |
|
| 306 | + |
|
| 307 | + list($queryParts, $params) = $this->buildParts($data); |
|
| 308 | + // duplicate $params because we need the parts twice in the SQL statement |
|
| 309 | + // once for the SET part, once in the WHERE clause |
|
| 310 | + $params = array_merge($params, $params); |
|
| 311 | + $params[] = $id; |
|
| 312 | + |
|
| 313 | + // don't update if the data we try to set is the same as the one in the record |
|
| 314 | + // some databases (Postgres) don't like superfluous updates |
|
| 315 | + $sql = 'UPDATE `*PREFIX*filecache` SET ' . implode(' = ?, ', $queryParts) . '=? ' . |
|
| 316 | + 'WHERE (' . |
|
| 317 | + implode(' <> ? OR ', $queryParts) . ' <> ? OR ' . |
|
| 318 | + implode(' IS NULL OR ', $queryParts) . ' IS NULL' . |
|
| 319 | + ') AND `fileid` = ? '; |
|
| 320 | + $this->connection->executeQuery($sql, $params); |
|
| 321 | + |
|
| 322 | + } |
|
| 323 | + |
|
| 324 | + /** |
|
| 325 | + * extract query parts and params array from data array |
|
| 326 | + * |
|
| 327 | + * @param array $data |
|
| 328 | + * @return array [$queryParts, $params] |
|
| 329 | + * $queryParts: string[], the (escaped) column names to be set in the query |
|
| 330 | + * $params: mixed[], the new values for the columns, to be passed as params to the query |
|
| 331 | + */ |
|
| 332 | + protected function buildParts(array $data) { |
|
| 333 | + $fields = array( |
|
| 334 | + 'path', 'parent', 'name', 'mimetype', 'size', 'mtime', 'storage_mtime', 'encrypted', |
|
| 335 | + 'etag', 'permissions', 'checksum'); |
|
| 336 | + |
|
| 337 | + $doNotCopyStorageMTime = false; |
|
| 338 | + if (array_key_exists('mtime', $data) && $data['mtime'] === null) { |
|
| 339 | + // this horrific magic tells it to not copy storage_mtime to mtime |
|
| 340 | + unset($data['mtime']); |
|
| 341 | + $doNotCopyStorageMTime = true; |
|
| 342 | + } |
|
| 343 | + |
|
| 344 | + $params = array(); |
|
| 345 | + $queryParts = array(); |
|
| 346 | + foreach ($data as $name => $value) { |
|
| 347 | + if (array_search($name, $fields) !== false) { |
|
| 348 | + if ($name === 'path') { |
|
| 349 | + $params[] = md5($value); |
|
| 350 | + $queryParts[] = '`path_hash`'; |
|
| 351 | + } elseif ($name === 'mimetype') { |
|
| 352 | + $params[] = $this->mimetypeLoader->getId(substr($value, 0, strpos($value, '/'))); |
|
| 353 | + $queryParts[] = '`mimepart`'; |
|
| 354 | + $value = $this->mimetypeLoader->getId($value); |
|
| 355 | + } elseif ($name === 'storage_mtime') { |
|
| 356 | + if (!$doNotCopyStorageMTime && !isset($data['mtime'])) { |
|
| 357 | + $params[] = $value; |
|
| 358 | + $queryParts[] = '`mtime`'; |
|
| 359 | + } |
|
| 360 | + } elseif ($name === 'encrypted') { |
|
| 361 | + if (isset($data['encryptedVersion'])) { |
|
| 362 | + $value = $data['encryptedVersion']; |
|
| 363 | + } else { |
|
| 364 | + // Boolean to integer conversion |
|
| 365 | + $value = $value ? 1 : 0; |
|
| 366 | + } |
|
| 367 | + } |
|
| 368 | + $params[] = $value; |
|
| 369 | + $queryParts[] = '`' . $name . '`'; |
|
| 370 | + } |
|
| 371 | + } |
|
| 372 | + return array($queryParts, $params); |
|
| 373 | + } |
|
| 374 | + |
|
| 375 | + /** |
|
| 376 | + * get the file id for a file |
|
| 377 | + * |
|
| 378 | + * A file id is a numeric id for a file or folder that's unique within an owncloud instance which stays the same for the lifetime of a file |
|
| 379 | + * |
|
| 380 | + * File ids are easiest way for apps to store references to a file since unlike paths they are not affected by renames or sharing |
|
| 381 | + * |
|
| 382 | + * @param string $file |
|
| 383 | + * @return int |
|
| 384 | + */ |
|
| 385 | + public function getId($file) { |
|
| 386 | + // normalize file |
|
| 387 | + $file = $this->normalize($file); |
|
| 388 | + |
|
| 389 | + $pathHash = md5($file); |
|
| 390 | + |
|
| 391 | + $sql = 'SELECT `fileid` FROM `*PREFIX*filecache` WHERE `storage` = ? AND `path_hash` = ?'; |
|
| 392 | + $result = $this->connection->executeQuery($sql, array($this->getNumericStorageId(), $pathHash)); |
|
| 393 | + if ($row = $result->fetch()) { |
|
| 394 | + return $row['fileid']; |
|
| 395 | + } else { |
|
| 396 | + return -1; |
|
| 397 | + } |
|
| 398 | + } |
|
| 399 | + |
|
| 400 | + /** |
|
| 401 | + * get the id of the parent folder of a file |
|
| 402 | + * |
|
| 403 | + * @param string $file |
|
| 404 | + * @return int |
|
| 405 | + */ |
|
| 406 | + public function getParentId($file) { |
|
| 407 | + if ($file === '') { |
|
| 408 | + return -1; |
|
| 409 | + } else { |
|
| 410 | + $parent = $this->getParentPath($file); |
|
| 411 | + return (int)$this->getId($parent); |
|
| 412 | + } |
|
| 413 | + } |
|
| 414 | + |
|
| 415 | + private function getParentPath($path) { |
|
| 416 | + $parent = dirname($path); |
|
| 417 | + if ($parent === '.') { |
|
| 418 | + $parent = ''; |
|
| 419 | + } |
|
| 420 | + return $parent; |
|
| 421 | + } |
|
| 422 | + |
|
| 423 | + /** |
|
| 424 | + * check if a file is available in the cache |
|
| 425 | + * |
|
| 426 | + * @param string $file |
|
| 427 | + * @return bool |
|
| 428 | + */ |
|
| 429 | + public function inCache($file) { |
|
| 430 | + return $this->getId($file) != -1; |
|
| 431 | + } |
|
| 432 | + |
|
| 433 | + /** |
|
| 434 | + * remove a file or folder from the cache |
|
| 435 | + * |
|
| 436 | + * when removing a folder from the cache all files and folders inside the folder will be removed as well |
|
| 437 | + * |
|
| 438 | + * @param string $file |
|
| 439 | + */ |
|
| 440 | + public function remove($file) { |
|
| 441 | + $entry = $this->get($file); |
|
| 442 | + $sql = 'DELETE FROM `*PREFIX*filecache` WHERE `fileid` = ?'; |
|
| 443 | + $this->connection->executeQuery($sql, array($entry['fileid'])); |
|
| 444 | + if ($entry['mimetype'] === 'httpd/unix-directory') { |
|
| 445 | + $this->removeChildren($entry); |
|
| 446 | + } |
|
| 447 | + } |
|
| 448 | + |
|
| 449 | + /** |
|
| 450 | + * Get all sub folders of a folder |
|
| 451 | + * |
|
| 452 | + * @param array $entry the cache entry of the folder to get the subfolders for |
|
| 453 | + * @return array[] the cache entries for the subfolders |
|
| 454 | + */ |
|
| 455 | + private function getSubFolders($entry) { |
|
| 456 | + $children = $this->getFolderContentsById($entry['fileid']); |
|
| 457 | + return array_filter($children, function ($child) { |
|
| 458 | + return $child['mimetype'] === 'httpd/unix-directory'; |
|
| 459 | + }); |
|
| 460 | + } |
|
| 461 | + |
|
| 462 | + /** |
|
| 463 | + * Recursively remove all children of a folder |
|
| 464 | + * |
|
| 465 | + * @param array $entry the cache entry of the folder to remove the children of |
|
| 466 | + * @throws \OC\DatabaseException |
|
| 467 | + */ |
|
| 468 | + private function removeChildren($entry) { |
|
| 469 | + $subFolders = $this->getSubFolders($entry); |
|
| 470 | + foreach ($subFolders as $folder) { |
|
| 471 | + $this->removeChildren($folder); |
|
| 472 | + } |
|
| 473 | + $sql = 'DELETE FROM `*PREFIX*filecache` WHERE `parent` = ?'; |
|
| 474 | + $this->connection->executeQuery($sql, array($entry['fileid'])); |
|
| 475 | + } |
|
| 476 | + |
|
| 477 | + /** |
|
| 478 | + * Move a file or folder in the cache |
|
| 479 | + * |
|
| 480 | + * @param string $source |
|
| 481 | + * @param string $target |
|
| 482 | + */ |
|
| 483 | + public function move($source, $target) { |
|
| 484 | + $this->moveFromCache($this, $source, $target); |
|
| 485 | + } |
|
| 486 | + |
|
| 487 | + /** |
|
| 488 | + * Get the storage id and path needed for a move |
|
| 489 | + * |
|
| 490 | + * @param string $path |
|
| 491 | + * @return array [$storageId, $internalPath] |
|
| 492 | + */ |
|
| 493 | + protected function getMoveInfo($path) { |
|
| 494 | + return [$this->getNumericStorageId(), $path]; |
|
| 495 | + } |
|
| 496 | + |
|
| 497 | + /** |
|
| 498 | + * Move a file or folder in the cache |
|
| 499 | + * |
|
| 500 | + * @param \OCP\Files\Cache\ICache $sourceCache |
|
| 501 | + * @param string $sourcePath |
|
| 502 | + * @param string $targetPath |
|
| 503 | + * @throws \OC\DatabaseException |
|
| 504 | + * @throws \Exception if the given storages have an invalid id |
|
| 505 | + */ |
|
| 506 | + public function moveFromCache(ICache $sourceCache, $sourcePath, $targetPath) { |
|
| 507 | + if ($sourceCache instanceof Cache) { |
|
| 508 | + // normalize source and target |
|
| 509 | + $sourcePath = $this->normalize($sourcePath); |
|
| 510 | + $targetPath = $this->normalize($targetPath); |
|
| 511 | + |
|
| 512 | + $sourceData = $sourceCache->get($sourcePath); |
|
| 513 | + $sourceId = $sourceData['fileid']; |
|
| 514 | + $newParentId = $this->getParentId($targetPath); |
|
| 515 | + |
|
| 516 | + list($sourceStorageId, $sourcePath) = $sourceCache->getMoveInfo($sourcePath); |
|
| 517 | + list($targetStorageId, $targetPath) = $this->getMoveInfo($targetPath); |
|
| 518 | + |
|
| 519 | + if (is_null($sourceStorageId) || $sourceStorageId === false) { |
|
| 520 | + throw new \Exception('Invalid source storage id: ' . $sourceStorageId); |
|
| 521 | + } |
|
| 522 | + if (is_null($targetStorageId) || $targetStorageId === false) { |
|
| 523 | + throw new \Exception('Invalid target storage id: ' . $targetStorageId); |
|
| 524 | + } |
|
| 525 | + |
|
| 526 | + $this->connection->beginTransaction(); |
|
| 527 | + if ($sourceData['mimetype'] === 'httpd/unix-directory') { |
|
| 528 | + //update all child entries |
|
| 529 | + $sourceLength = strlen($sourcePath); |
|
| 530 | + $query = $this->connection->getQueryBuilder(); |
|
| 531 | + |
|
| 532 | + $fun = $query->func(); |
|
| 533 | + $newPathFunction = $fun->concat( |
|
| 534 | + $query->createNamedParameter($targetPath), |
|
| 535 | + $fun->substring('path', $query->createNamedParameter($sourceLength + 1, IQueryBuilder::PARAM_INT))// +1 for the leading slash |
|
| 536 | + ); |
|
| 537 | + $query->update('filecache') |
|
| 538 | + ->set('storage', $query->createNamedParameter($targetStorageId, IQueryBuilder::PARAM_INT)) |
|
| 539 | + ->set('path_hash', $fun->md5($newPathFunction)) |
|
| 540 | + ->set('path', $newPathFunction) |
|
| 541 | + ->where($query->expr()->eq('storage', $query->createNamedParameter($sourceStorageId, IQueryBuilder::PARAM_INT))) |
|
| 542 | + ->andWhere($query->expr()->like('path', $query->createNamedParameter($this->connection->escapeLikeParameter($sourcePath) . '/%'))); |
|
| 543 | + |
|
| 544 | + try { |
|
| 545 | + $query->execute(); |
|
| 546 | + } catch (\OC\DatabaseException $e) { |
|
| 547 | + $this->connection->rollBack(); |
|
| 548 | + throw $e; |
|
| 549 | + } |
|
| 550 | + } |
|
| 551 | + |
|
| 552 | + $sql = 'UPDATE `*PREFIX*filecache` SET `storage` = ?, `path` = ?, `path_hash` = ?, `name` = ?, `parent` = ? WHERE `fileid` = ?'; |
|
| 553 | + $this->connection->executeQuery($sql, array($targetStorageId, $targetPath, md5($targetPath), \OC_Util::basename($targetPath), $newParentId, $sourceId)); |
|
| 554 | + $this->connection->commit(); |
|
| 555 | + } else { |
|
| 556 | + $this->moveFromCacheFallback($sourceCache, $sourcePath, $targetPath); |
|
| 557 | + } |
|
| 558 | + } |
|
| 559 | + |
|
| 560 | + /** |
|
| 561 | + * remove all entries for files that are stored on the storage from the cache |
|
| 562 | + */ |
|
| 563 | + public function clear() { |
|
| 564 | + $sql = 'DELETE FROM `*PREFIX*filecache` WHERE `storage` = ?'; |
|
| 565 | + $this->connection->executeQuery($sql, array($this->getNumericStorageId())); |
|
| 566 | + |
|
| 567 | + $sql = 'DELETE FROM `*PREFIX*storages` WHERE `id` = ?'; |
|
| 568 | + $this->connection->executeQuery($sql, array($this->storageId)); |
|
| 569 | + } |
|
| 570 | + |
|
| 571 | + /** |
|
| 572 | + * Get the scan status of a file |
|
| 573 | + * |
|
| 574 | + * - Cache::NOT_FOUND: File is not in the cache |
|
| 575 | + * - Cache::PARTIAL: File is not stored in the cache but some incomplete data is known |
|
| 576 | + * - Cache::SHALLOW: The folder and it's direct children are in the cache but not all sub folders are fully scanned |
|
| 577 | + * - Cache::COMPLETE: The file or folder, with all it's children) are fully scanned |
|
| 578 | + * |
|
| 579 | + * @param string $file |
|
| 580 | + * |
|
| 581 | + * @return int Cache::NOT_FOUND, Cache::PARTIAL, Cache::SHALLOW or Cache::COMPLETE |
|
| 582 | + */ |
|
| 583 | + public function getStatus($file) { |
|
| 584 | + // normalize file |
|
| 585 | + $file = $this->normalize($file); |
|
| 586 | + |
|
| 587 | + $pathHash = md5($file); |
|
| 588 | + $sql = 'SELECT `size` FROM `*PREFIX*filecache` WHERE `storage` = ? AND `path_hash` = ?'; |
|
| 589 | + $result = $this->connection->executeQuery($sql, array($this->getNumericStorageId(), $pathHash)); |
|
| 590 | + if ($row = $result->fetch()) { |
|
| 591 | + if ((int)$row['size'] === -1) { |
|
| 592 | + return self::SHALLOW; |
|
| 593 | + } else { |
|
| 594 | + return self::COMPLETE; |
|
| 595 | + } |
|
| 596 | + } else { |
|
| 597 | + if (isset($this->partial[$file])) { |
|
| 598 | + return self::PARTIAL; |
|
| 599 | + } else { |
|
| 600 | + return self::NOT_FOUND; |
|
| 601 | + } |
|
| 602 | + } |
|
| 603 | + } |
|
| 604 | + |
|
| 605 | + /** |
|
| 606 | + * search for files matching $pattern |
|
| 607 | + * |
|
| 608 | + * @param string $pattern the search pattern using SQL search syntax (e.g. '%searchstring%') |
|
| 609 | + * @return ICacheEntry[] an array of cache entries where the name matches the search pattern |
|
| 610 | + */ |
|
| 611 | + public function search($pattern) { |
|
| 612 | + // normalize pattern |
|
| 613 | + $pattern = $this->normalize($pattern); |
|
| 614 | + |
|
| 615 | + if ($pattern === '%%') { |
|
| 616 | + return []; |
|
| 617 | + } |
|
| 618 | + |
|
| 619 | + |
|
| 620 | + $sql = ' |
|
| 621 | 621 | SELECT `fileid`, `storage`, `path`, `parent`, `name`, |
| 622 | 622 | `mimetype`, `storage_mtime`, `mimepart`, `size`, `mtime`, |
| 623 | 623 | `encrypted`, `etag`, `permissions`, `checksum` |
| 624 | 624 | FROM `*PREFIX*filecache` |
| 625 | 625 | WHERE `storage` = ? AND `name` ILIKE ?'; |
| 626 | - $result = $this->connection->executeQuery($sql, |
|
| 627 | - [$this->getNumericStorageId(), $pattern] |
|
| 628 | - ); |
|
| 629 | - |
|
| 630 | - return $this->searchResultToCacheEntries($result); |
|
| 631 | - } |
|
| 632 | - |
|
| 633 | - /** |
|
| 634 | - * @param Statement $result |
|
| 635 | - * @return CacheEntry[] |
|
| 636 | - */ |
|
| 637 | - private function searchResultToCacheEntries(Statement $result) { |
|
| 638 | - $files = $result->fetchAll(); |
|
| 639 | - |
|
| 640 | - return array_map(function (array $data) { |
|
| 641 | - return self::cacheEntryFromData($data, $this->mimetypeLoader); |
|
| 642 | - }, $files); |
|
| 643 | - } |
|
| 644 | - |
|
| 645 | - /** |
|
| 646 | - * search for files by mimetype |
|
| 647 | - * |
|
| 648 | - * @param string $mimetype either a full mimetype to search ('text/plain') or only the first part of a mimetype ('image') |
|
| 649 | - * where it will search for all mimetypes in the group ('image/*') |
|
| 650 | - * @return ICacheEntry[] an array of cache entries where the mimetype matches the search |
|
| 651 | - */ |
|
| 652 | - public function searchByMime($mimetype) { |
|
| 653 | - if (strpos($mimetype, '/')) { |
|
| 654 | - $where = '`mimetype` = ?'; |
|
| 655 | - } else { |
|
| 656 | - $where = '`mimepart` = ?'; |
|
| 657 | - } |
|
| 658 | - $sql = 'SELECT `fileid`, `storage`, `path`, `parent`, `name`, `mimetype`, `mimepart`, `size`, `storage_mtime`, `mtime`, `encrypted`, `etag`, `permissions`, `checksum` |
|
| 626 | + $result = $this->connection->executeQuery($sql, |
|
| 627 | + [$this->getNumericStorageId(), $pattern] |
|
| 628 | + ); |
|
| 629 | + |
|
| 630 | + return $this->searchResultToCacheEntries($result); |
|
| 631 | + } |
|
| 632 | + |
|
| 633 | + /** |
|
| 634 | + * @param Statement $result |
|
| 635 | + * @return CacheEntry[] |
|
| 636 | + */ |
|
| 637 | + private function searchResultToCacheEntries(Statement $result) { |
|
| 638 | + $files = $result->fetchAll(); |
|
| 639 | + |
|
| 640 | + return array_map(function (array $data) { |
|
| 641 | + return self::cacheEntryFromData($data, $this->mimetypeLoader); |
|
| 642 | + }, $files); |
|
| 643 | + } |
|
| 644 | + |
|
| 645 | + /** |
|
| 646 | + * search for files by mimetype |
|
| 647 | + * |
|
| 648 | + * @param string $mimetype either a full mimetype to search ('text/plain') or only the first part of a mimetype ('image') |
|
| 649 | + * where it will search for all mimetypes in the group ('image/*') |
|
| 650 | + * @return ICacheEntry[] an array of cache entries where the mimetype matches the search |
|
| 651 | + */ |
|
| 652 | + public function searchByMime($mimetype) { |
|
| 653 | + if (strpos($mimetype, '/')) { |
|
| 654 | + $where = '`mimetype` = ?'; |
|
| 655 | + } else { |
|
| 656 | + $where = '`mimepart` = ?'; |
|
| 657 | + } |
|
| 658 | + $sql = 'SELECT `fileid`, `storage`, `path`, `parent`, `name`, `mimetype`, `mimepart`, `size`, `storage_mtime`, `mtime`, `encrypted`, `etag`, `permissions`, `checksum` |
|
| 659 | 659 | FROM `*PREFIX*filecache` WHERE ' . $where . ' AND `storage` = ?'; |
| 660 | - $mimetype = $this->mimetypeLoader->getId($mimetype); |
|
| 661 | - $result = $this->connection->executeQuery($sql, array($mimetype, $this->getNumericStorageId())); |
|
| 662 | - |
|
| 663 | - return $this->searchResultToCacheEntries($result); |
|
| 664 | - } |
|
| 665 | - |
|
| 666 | - public function searchQuery(ISearchQuery $searchQuery) { |
|
| 667 | - $builder = \OC::$server->getDatabaseConnection()->getQueryBuilder(); |
|
| 668 | - |
|
| 669 | - $query = $builder->select(['fileid', 'storage', 'path', 'parent', 'name', 'mimetype', 'mimepart', 'size', 'mtime', 'storage_mtime', 'encrypted', 'etag', 'permissions', 'checksum']) |
|
| 670 | - ->from('filecache', 'file'); |
|
| 671 | - |
|
| 672 | - $query->where($builder->expr()->eq('storage', $builder->createNamedParameter($this->getNumericStorageId()))); |
|
| 673 | - |
|
| 674 | - if ($this->querySearchHelper->shouldJoinTags($searchQuery->getSearchOperation())) { |
|
| 675 | - $query |
|
| 676 | - ->innerJoin('file', 'vcategory_to_object', 'tagmap', $builder->expr()->eq('file.fileid', 'tagmap.objid')) |
|
| 677 | - ->innerJoin('tagmap', 'vcategory', 'tag', $builder->expr()->andX( |
|
| 678 | - $builder->expr()->eq('tagmap.type', 'tag.type'), |
|
| 679 | - $builder->expr()->eq('tagmap.categoryid', 'tag.id') |
|
| 680 | - )) |
|
| 681 | - ->andWhere($builder->expr()->eq('tag.type', $builder->createNamedParameter('files'))) |
|
| 682 | - ->andWhere($builder->expr()->eq('tag.uid', $builder->createNamedParameter($searchQuery->getUser()->getUID()))); |
|
| 683 | - } |
|
| 684 | - |
|
| 685 | - $query->andWhere($this->querySearchHelper->searchOperatorToDBExpr($builder, $searchQuery->getSearchOperation())); |
|
| 686 | - |
|
| 687 | - $this->querySearchHelper->addSearchOrdersToQuery($query, $searchQuery->getOrder()); |
|
| 688 | - |
|
| 689 | - if ($searchQuery->getLimit()) { |
|
| 690 | - $query->setMaxResults($searchQuery->getLimit()); |
|
| 691 | - } |
|
| 692 | - if ($searchQuery->getOffset()) { |
|
| 693 | - $query->setFirstResult($searchQuery->getOffset()); |
|
| 694 | - } |
|
| 695 | - |
|
| 696 | - $result = $query->execute(); |
|
| 697 | - return $this->searchResultToCacheEntries($result); |
|
| 698 | - } |
|
| 699 | - |
|
| 700 | - /** |
|
| 701 | - * Search for files by tag of a given users. |
|
| 702 | - * |
|
| 703 | - * Note that every user can tag files differently. |
|
| 704 | - * |
|
| 705 | - * @param string|int $tag name or tag id |
|
| 706 | - * @param string $userId owner of the tags |
|
| 707 | - * @return ICacheEntry[] file data |
|
| 708 | - */ |
|
| 709 | - public function searchByTag($tag, $userId) { |
|
| 710 | - $sql = 'SELECT `fileid`, `storage`, `path`, `parent`, `name`, ' . |
|
| 711 | - '`mimetype`, `mimepart`, `size`, `mtime`, `storage_mtime`, ' . |
|
| 712 | - '`encrypted`, `etag`, `permissions`, `checksum` ' . |
|
| 713 | - 'FROM `*PREFIX*filecache` `file`, ' . |
|
| 714 | - '`*PREFIX*vcategory_to_object` `tagmap`, ' . |
|
| 715 | - '`*PREFIX*vcategory` `tag` ' . |
|
| 716 | - // JOIN filecache to vcategory_to_object |
|
| 717 | - 'WHERE `file`.`fileid` = `tagmap`.`objid` ' . |
|
| 718 | - // JOIN vcategory_to_object to vcategory |
|
| 719 | - 'AND `tagmap`.`type` = `tag`.`type` ' . |
|
| 720 | - 'AND `tagmap`.`categoryid` = `tag`.`id` ' . |
|
| 721 | - // conditions |
|
| 722 | - 'AND `file`.`storage` = ? ' . |
|
| 723 | - 'AND `tag`.`type` = \'files\' ' . |
|
| 724 | - 'AND `tag`.`uid` = ? '; |
|
| 725 | - if (is_int($tag)) { |
|
| 726 | - $sql .= 'AND `tag`.`id` = ? '; |
|
| 727 | - } else { |
|
| 728 | - $sql .= 'AND `tag`.`category` = ? '; |
|
| 729 | - } |
|
| 730 | - $result = $this->connection->executeQuery( |
|
| 731 | - $sql, |
|
| 732 | - [ |
|
| 733 | - $this->getNumericStorageId(), |
|
| 734 | - $userId, |
|
| 735 | - $tag |
|
| 736 | - ] |
|
| 737 | - ); |
|
| 738 | - |
|
| 739 | - $files = $result->fetchAll(); |
|
| 740 | - |
|
| 741 | - return array_map(function (array $data) { |
|
| 742 | - return self::cacheEntryFromData($data, $this->mimetypeLoader); |
|
| 743 | - }, $files); |
|
| 744 | - } |
|
| 745 | - |
|
| 746 | - /** |
|
| 747 | - * Re-calculate the folder size and the size of all parent folders |
|
| 748 | - * |
|
| 749 | - * @param string|boolean $path |
|
| 750 | - * @param array $data (optional) meta data of the folder |
|
| 751 | - */ |
|
| 752 | - public function correctFolderSize($path, $data = null) { |
|
| 753 | - $this->calculateFolderSize($path, $data); |
|
| 754 | - if ($path !== '') { |
|
| 755 | - $parent = dirname($path); |
|
| 756 | - if ($parent === '.' or $parent === '/') { |
|
| 757 | - $parent = ''; |
|
| 758 | - } |
|
| 759 | - $this->correctFolderSize($parent); |
|
| 760 | - } |
|
| 761 | - } |
|
| 762 | - |
|
| 763 | - /** |
|
| 764 | - * calculate the size of a folder and set it in the cache |
|
| 765 | - * |
|
| 766 | - * @param string $path |
|
| 767 | - * @param array $entry (optional) meta data of the folder |
|
| 768 | - * @return int |
|
| 769 | - */ |
|
| 770 | - public function calculateFolderSize($path, $entry = null) { |
|
| 771 | - $totalSize = 0; |
|
| 772 | - if (is_null($entry) or !isset($entry['fileid'])) { |
|
| 773 | - $entry = $this->get($path); |
|
| 774 | - } |
|
| 775 | - if (isset($entry['mimetype']) && $entry['mimetype'] === 'httpd/unix-directory') { |
|
| 776 | - $id = $entry['fileid']; |
|
| 777 | - $sql = 'SELECT SUM(`size`) AS f1, MIN(`size`) AS f2 ' . |
|
| 778 | - 'FROM `*PREFIX*filecache` ' . |
|
| 779 | - 'WHERE `parent` = ? AND `storage` = ?'; |
|
| 780 | - $result = $this->connection->executeQuery($sql, array($id, $this->getNumericStorageId())); |
|
| 781 | - if ($row = $result->fetch()) { |
|
| 782 | - $result->closeCursor(); |
|
| 783 | - list($sum, $min) = array_values($row); |
|
| 784 | - $sum = 0 + $sum; |
|
| 785 | - $min = 0 + $min; |
|
| 786 | - if ($min === -1) { |
|
| 787 | - $totalSize = $min; |
|
| 788 | - } else { |
|
| 789 | - $totalSize = $sum; |
|
| 790 | - } |
|
| 791 | - $update = array(); |
|
| 792 | - if ($entry['size'] !== $totalSize) { |
|
| 793 | - $update['size'] = $totalSize; |
|
| 794 | - } |
|
| 795 | - if (count($update) > 0) { |
|
| 796 | - $this->update($id, $update); |
|
| 797 | - } |
|
| 798 | - } else { |
|
| 799 | - $result->closeCursor(); |
|
| 800 | - } |
|
| 801 | - } |
|
| 802 | - return $totalSize; |
|
| 803 | - } |
|
| 804 | - |
|
| 805 | - /** |
|
| 806 | - * get all file ids on the files on the storage |
|
| 807 | - * |
|
| 808 | - * @return int[] |
|
| 809 | - */ |
|
| 810 | - public function getAll() { |
|
| 811 | - $sql = 'SELECT `fileid` FROM `*PREFIX*filecache` WHERE `storage` = ?'; |
|
| 812 | - $result = $this->connection->executeQuery($sql, array($this->getNumericStorageId())); |
|
| 813 | - $ids = array(); |
|
| 814 | - while ($row = $result->fetch()) { |
|
| 815 | - $ids[] = $row['fileid']; |
|
| 816 | - } |
|
| 817 | - return $ids; |
|
| 818 | - } |
|
| 819 | - |
|
| 820 | - /** |
|
| 821 | - * find a folder in the cache which has not been fully scanned |
|
| 822 | - * |
|
| 823 | - * If multiple incomplete folders are in the cache, the one with the highest id will be returned, |
|
| 824 | - * use the one with the highest id gives the best result with the background scanner, since that is most |
|
| 825 | - * likely the folder where we stopped scanning previously |
|
| 826 | - * |
|
| 827 | - * @return string|bool the path of the folder or false when no folder matched |
|
| 828 | - */ |
|
| 829 | - public function getIncomplete() { |
|
| 830 | - $query = $this->connection->prepare('SELECT `path` FROM `*PREFIX*filecache`' |
|
| 831 | - . ' WHERE `storage` = ? AND `size` = -1 ORDER BY `fileid` DESC', 1); |
|
| 832 | - $query->execute([$this->getNumericStorageId()]); |
|
| 833 | - if ($row = $query->fetch()) { |
|
| 834 | - return $row['path']; |
|
| 835 | - } else { |
|
| 836 | - return false; |
|
| 837 | - } |
|
| 838 | - } |
|
| 839 | - |
|
| 840 | - /** |
|
| 841 | - * get the path of a file on this storage by it's file id |
|
| 842 | - * |
|
| 843 | - * @param int $id the file id of the file or folder to search |
|
| 844 | - * @return string|null the path of the file (relative to the storage) or null if a file with the given id does not exists within this cache |
|
| 845 | - */ |
|
| 846 | - public function getPathById($id) { |
|
| 847 | - $sql = 'SELECT `path` FROM `*PREFIX*filecache` WHERE `fileid` = ? AND `storage` = ?'; |
|
| 848 | - $result = $this->connection->executeQuery($sql, array($id, $this->getNumericStorageId())); |
|
| 849 | - if ($row = $result->fetch()) { |
|
| 850 | - // Oracle stores empty strings as null... |
|
| 851 | - if ($row['path'] === null) { |
|
| 852 | - return ''; |
|
| 853 | - } |
|
| 854 | - return $row['path']; |
|
| 855 | - } else { |
|
| 856 | - return null; |
|
| 857 | - } |
|
| 858 | - } |
|
| 859 | - |
|
| 860 | - /** |
|
| 861 | - * get the storage id of the storage for a file and the internal path of the file |
|
| 862 | - * unlike getPathById this does not limit the search to files on this storage and |
|
| 863 | - * instead does a global search in the cache table |
|
| 864 | - * |
|
| 865 | - * @param int $id |
|
| 866 | - * @deprecated use getPathById() instead |
|
| 867 | - * @return array first element holding the storage id, second the path |
|
| 868 | - */ |
|
| 869 | - static public function getById($id) { |
|
| 870 | - $connection = \OC::$server->getDatabaseConnection(); |
|
| 871 | - $sql = 'SELECT `storage`, `path` FROM `*PREFIX*filecache` WHERE `fileid` = ?'; |
|
| 872 | - $result = $connection->executeQuery($sql, array($id)); |
|
| 873 | - if ($row = $result->fetch()) { |
|
| 874 | - $numericId = $row['storage']; |
|
| 875 | - $path = $row['path']; |
|
| 876 | - } else { |
|
| 877 | - return null; |
|
| 878 | - } |
|
| 879 | - |
|
| 880 | - if ($id = Storage::getStorageId($numericId)) { |
|
| 881 | - return array($id, $path); |
|
| 882 | - } else { |
|
| 883 | - return null; |
|
| 884 | - } |
|
| 885 | - } |
|
| 886 | - |
|
| 887 | - /** |
|
| 888 | - * normalize the given path |
|
| 889 | - * |
|
| 890 | - * @param string $path |
|
| 891 | - * @return string |
|
| 892 | - */ |
|
| 893 | - public function normalize($path) { |
|
| 894 | - |
|
| 895 | - return trim(\OC_Util::normalizeUnicode($path), '/'); |
|
| 896 | - } |
|
| 660 | + $mimetype = $this->mimetypeLoader->getId($mimetype); |
|
| 661 | + $result = $this->connection->executeQuery($sql, array($mimetype, $this->getNumericStorageId())); |
|
| 662 | + |
|
| 663 | + return $this->searchResultToCacheEntries($result); |
|
| 664 | + } |
|
| 665 | + |
|
| 666 | + public function searchQuery(ISearchQuery $searchQuery) { |
|
| 667 | + $builder = \OC::$server->getDatabaseConnection()->getQueryBuilder(); |
|
| 668 | + |
|
| 669 | + $query = $builder->select(['fileid', 'storage', 'path', 'parent', 'name', 'mimetype', 'mimepart', 'size', 'mtime', 'storage_mtime', 'encrypted', 'etag', 'permissions', 'checksum']) |
|
| 670 | + ->from('filecache', 'file'); |
|
| 671 | + |
|
| 672 | + $query->where($builder->expr()->eq('storage', $builder->createNamedParameter($this->getNumericStorageId()))); |
|
| 673 | + |
|
| 674 | + if ($this->querySearchHelper->shouldJoinTags($searchQuery->getSearchOperation())) { |
|
| 675 | + $query |
|
| 676 | + ->innerJoin('file', 'vcategory_to_object', 'tagmap', $builder->expr()->eq('file.fileid', 'tagmap.objid')) |
|
| 677 | + ->innerJoin('tagmap', 'vcategory', 'tag', $builder->expr()->andX( |
|
| 678 | + $builder->expr()->eq('tagmap.type', 'tag.type'), |
|
| 679 | + $builder->expr()->eq('tagmap.categoryid', 'tag.id') |
|
| 680 | + )) |
|
| 681 | + ->andWhere($builder->expr()->eq('tag.type', $builder->createNamedParameter('files'))) |
|
| 682 | + ->andWhere($builder->expr()->eq('tag.uid', $builder->createNamedParameter($searchQuery->getUser()->getUID()))); |
|
| 683 | + } |
|
| 684 | + |
|
| 685 | + $query->andWhere($this->querySearchHelper->searchOperatorToDBExpr($builder, $searchQuery->getSearchOperation())); |
|
| 686 | + |
|
| 687 | + $this->querySearchHelper->addSearchOrdersToQuery($query, $searchQuery->getOrder()); |
|
| 688 | + |
|
| 689 | + if ($searchQuery->getLimit()) { |
|
| 690 | + $query->setMaxResults($searchQuery->getLimit()); |
|
| 691 | + } |
|
| 692 | + if ($searchQuery->getOffset()) { |
|
| 693 | + $query->setFirstResult($searchQuery->getOffset()); |
|
| 694 | + } |
|
| 695 | + |
|
| 696 | + $result = $query->execute(); |
|
| 697 | + return $this->searchResultToCacheEntries($result); |
|
| 698 | + } |
|
| 699 | + |
|
| 700 | + /** |
|
| 701 | + * Search for files by tag of a given users. |
|
| 702 | + * |
|
| 703 | + * Note that every user can tag files differently. |
|
| 704 | + * |
|
| 705 | + * @param string|int $tag name or tag id |
|
| 706 | + * @param string $userId owner of the tags |
|
| 707 | + * @return ICacheEntry[] file data |
|
| 708 | + */ |
|
| 709 | + public function searchByTag($tag, $userId) { |
|
| 710 | + $sql = 'SELECT `fileid`, `storage`, `path`, `parent`, `name`, ' . |
|
| 711 | + '`mimetype`, `mimepart`, `size`, `mtime`, `storage_mtime`, ' . |
|
| 712 | + '`encrypted`, `etag`, `permissions`, `checksum` ' . |
|
| 713 | + 'FROM `*PREFIX*filecache` `file`, ' . |
|
| 714 | + '`*PREFIX*vcategory_to_object` `tagmap`, ' . |
|
| 715 | + '`*PREFIX*vcategory` `tag` ' . |
|
| 716 | + // JOIN filecache to vcategory_to_object |
|
| 717 | + 'WHERE `file`.`fileid` = `tagmap`.`objid` ' . |
|
| 718 | + // JOIN vcategory_to_object to vcategory |
|
| 719 | + 'AND `tagmap`.`type` = `tag`.`type` ' . |
|
| 720 | + 'AND `tagmap`.`categoryid` = `tag`.`id` ' . |
|
| 721 | + // conditions |
|
| 722 | + 'AND `file`.`storage` = ? ' . |
|
| 723 | + 'AND `tag`.`type` = \'files\' ' . |
|
| 724 | + 'AND `tag`.`uid` = ? '; |
|
| 725 | + if (is_int($tag)) { |
|
| 726 | + $sql .= 'AND `tag`.`id` = ? '; |
|
| 727 | + } else { |
|
| 728 | + $sql .= 'AND `tag`.`category` = ? '; |
|
| 729 | + } |
|
| 730 | + $result = $this->connection->executeQuery( |
|
| 731 | + $sql, |
|
| 732 | + [ |
|
| 733 | + $this->getNumericStorageId(), |
|
| 734 | + $userId, |
|
| 735 | + $tag |
|
| 736 | + ] |
|
| 737 | + ); |
|
| 738 | + |
|
| 739 | + $files = $result->fetchAll(); |
|
| 740 | + |
|
| 741 | + return array_map(function (array $data) { |
|
| 742 | + return self::cacheEntryFromData($data, $this->mimetypeLoader); |
|
| 743 | + }, $files); |
|
| 744 | + } |
|
| 745 | + |
|
| 746 | + /** |
|
| 747 | + * Re-calculate the folder size and the size of all parent folders |
|
| 748 | + * |
|
| 749 | + * @param string|boolean $path |
|
| 750 | + * @param array $data (optional) meta data of the folder |
|
| 751 | + */ |
|
| 752 | + public function correctFolderSize($path, $data = null) { |
|
| 753 | + $this->calculateFolderSize($path, $data); |
|
| 754 | + if ($path !== '') { |
|
| 755 | + $parent = dirname($path); |
|
| 756 | + if ($parent === '.' or $parent === '/') { |
|
| 757 | + $parent = ''; |
|
| 758 | + } |
|
| 759 | + $this->correctFolderSize($parent); |
|
| 760 | + } |
|
| 761 | + } |
|
| 762 | + |
|
| 763 | + /** |
|
| 764 | + * calculate the size of a folder and set it in the cache |
|
| 765 | + * |
|
| 766 | + * @param string $path |
|
| 767 | + * @param array $entry (optional) meta data of the folder |
|
| 768 | + * @return int |
|
| 769 | + */ |
|
| 770 | + public function calculateFolderSize($path, $entry = null) { |
|
| 771 | + $totalSize = 0; |
|
| 772 | + if (is_null($entry) or !isset($entry['fileid'])) { |
|
| 773 | + $entry = $this->get($path); |
|
| 774 | + } |
|
| 775 | + if (isset($entry['mimetype']) && $entry['mimetype'] === 'httpd/unix-directory') { |
|
| 776 | + $id = $entry['fileid']; |
|
| 777 | + $sql = 'SELECT SUM(`size`) AS f1, MIN(`size`) AS f2 ' . |
|
| 778 | + 'FROM `*PREFIX*filecache` ' . |
|
| 779 | + 'WHERE `parent` = ? AND `storage` = ?'; |
|
| 780 | + $result = $this->connection->executeQuery($sql, array($id, $this->getNumericStorageId())); |
|
| 781 | + if ($row = $result->fetch()) { |
|
| 782 | + $result->closeCursor(); |
|
| 783 | + list($sum, $min) = array_values($row); |
|
| 784 | + $sum = 0 + $sum; |
|
| 785 | + $min = 0 + $min; |
|
| 786 | + if ($min === -1) { |
|
| 787 | + $totalSize = $min; |
|
| 788 | + } else { |
|
| 789 | + $totalSize = $sum; |
|
| 790 | + } |
|
| 791 | + $update = array(); |
|
| 792 | + if ($entry['size'] !== $totalSize) { |
|
| 793 | + $update['size'] = $totalSize; |
|
| 794 | + } |
|
| 795 | + if (count($update) > 0) { |
|
| 796 | + $this->update($id, $update); |
|
| 797 | + } |
|
| 798 | + } else { |
|
| 799 | + $result->closeCursor(); |
|
| 800 | + } |
|
| 801 | + } |
|
| 802 | + return $totalSize; |
|
| 803 | + } |
|
| 804 | + |
|
| 805 | + /** |
|
| 806 | + * get all file ids on the files on the storage |
|
| 807 | + * |
|
| 808 | + * @return int[] |
|
| 809 | + */ |
|
| 810 | + public function getAll() { |
|
| 811 | + $sql = 'SELECT `fileid` FROM `*PREFIX*filecache` WHERE `storage` = ?'; |
|
| 812 | + $result = $this->connection->executeQuery($sql, array($this->getNumericStorageId())); |
|
| 813 | + $ids = array(); |
|
| 814 | + while ($row = $result->fetch()) { |
|
| 815 | + $ids[] = $row['fileid']; |
|
| 816 | + } |
|
| 817 | + return $ids; |
|
| 818 | + } |
|
| 819 | + |
|
| 820 | + /** |
|
| 821 | + * find a folder in the cache which has not been fully scanned |
|
| 822 | + * |
|
| 823 | + * If multiple incomplete folders are in the cache, the one with the highest id will be returned, |
|
| 824 | + * use the one with the highest id gives the best result with the background scanner, since that is most |
|
| 825 | + * likely the folder where we stopped scanning previously |
|
| 826 | + * |
|
| 827 | + * @return string|bool the path of the folder or false when no folder matched |
|
| 828 | + */ |
|
| 829 | + public function getIncomplete() { |
|
| 830 | + $query = $this->connection->prepare('SELECT `path` FROM `*PREFIX*filecache`' |
|
| 831 | + . ' WHERE `storage` = ? AND `size` = -1 ORDER BY `fileid` DESC', 1); |
|
| 832 | + $query->execute([$this->getNumericStorageId()]); |
|
| 833 | + if ($row = $query->fetch()) { |
|
| 834 | + return $row['path']; |
|
| 835 | + } else { |
|
| 836 | + return false; |
|
| 837 | + } |
|
| 838 | + } |
|
| 839 | + |
|
| 840 | + /** |
|
| 841 | + * get the path of a file on this storage by it's file id |
|
| 842 | + * |
|
| 843 | + * @param int $id the file id of the file or folder to search |
|
| 844 | + * @return string|null the path of the file (relative to the storage) or null if a file with the given id does not exists within this cache |
|
| 845 | + */ |
|
| 846 | + public function getPathById($id) { |
|
| 847 | + $sql = 'SELECT `path` FROM `*PREFIX*filecache` WHERE `fileid` = ? AND `storage` = ?'; |
|
| 848 | + $result = $this->connection->executeQuery($sql, array($id, $this->getNumericStorageId())); |
|
| 849 | + if ($row = $result->fetch()) { |
|
| 850 | + // Oracle stores empty strings as null... |
|
| 851 | + if ($row['path'] === null) { |
|
| 852 | + return ''; |
|
| 853 | + } |
|
| 854 | + return $row['path']; |
|
| 855 | + } else { |
|
| 856 | + return null; |
|
| 857 | + } |
|
| 858 | + } |
|
| 859 | + |
|
| 860 | + /** |
|
| 861 | + * get the storage id of the storage for a file and the internal path of the file |
|
| 862 | + * unlike getPathById this does not limit the search to files on this storage and |
|
| 863 | + * instead does a global search in the cache table |
|
| 864 | + * |
|
| 865 | + * @param int $id |
|
| 866 | + * @deprecated use getPathById() instead |
|
| 867 | + * @return array first element holding the storage id, second the path |
|
| 868 | + */ |
|
| 869 | + static public function getById($id) { |
|
| 870 | + $connection = \OC::$server->getDatabaseConnection(); |
|
| 871 | + $sql = 'SELECT `storage`, `path` FROM `*PREFIX*filecache` WHERE `fileid` = ?'; |
|
| 872 | + $result = $connection->executeQuery($sql, array($id)); |
|
| 873 | + if ($row = $result->fetch()) { |
|
| 874 | + $numericId = $row['storage']; |
|
| 875 | + $path = $row['path']; |
|
| 876 | + } else { |
|
| 877 | + return null; |
|
| 878 | + } |
|
| 879 | + |
|
| 880 | + if ($id = Storage::getStorageId($numericId)) { |
|
| 881 | + return array($id, $path); |
|
| 882 | + } else { |
|
| 883 | + return null; |
|
| 884 | + } |
|
| 885 | + } |
|
| 886 | + |
|
| 887 | + /** |
|
| 888 | + * normalize the given path |
|
| 889 | + * |
|
| 890 | + * @param string $path |
|
| 891 | + * @return string |
|
| 892 | + */ |
|
| 893 | + public function normalize($path) { |
|
| 894 | + |
|
| 895 | + return trim(\OC_Util::normalizeUnicode($path), '/'); |
|
| 896 | + } |
|
| 897 | 897 | } |
@@ -163,21 +163,21 @@ discard block |
||
| 163 | 163 | */ |
| 164 | 164 | public static function cacheEntryFromData($data, IMimeTypeLoader $mimetypeLoader) { |
| 165 | 165 | //fix types |
| 166 | - $data['fileid'] = (int)$data['fileid']; |
|
| 167 | - $data['parent'] = (int)$data['parent']; |
|
| 166 | + $data['fileid'] = (int) $data['fileid']; |
|
| 167 | + $data['parent'] = (int) $data['parent']; |
|
| 168 | 168 | $data['size'] = 0 + $data['size']; |
| 169 | - $data['mtime'] = (int)$data['mtime']; |
|
| 170 | - $data['storage_mtime'] = (int)$data['storage_mtime']; |
|
| 171 | - $data['encryptedVersion'] = (int)$data['encrypted']; |
|
| 172 | - $data['encrypted'] = (bool)$data['encrypted']; |
|
| 169 | + $data['mtime'] = (int) $data['mtime']; |
|
| 170 | + $data['storage_mtime'] = (int) $data['storage_mtime']; |
|
| 171 | + $data['encryptedVersion'] = (int) $data['encrypted']; |
|
| 172 | + $data['encrypted'] = (bool) $data['encrypted']; |
|
| 173 | 173 | $data['storage_id'] = $data['storage']; |
| 174 | - $data['storage'] = (int)$data['storage']; |
|
| 174 | + $data['storage'] = (int) $data['storage']; |
|
| 175 | 175 | $data['mimetype'] = $mimetypeLoader->getMimetypeById($data['mimetype']); |
| 176 | 176 | $data['mimepart'] = $mimetypeLoader->getMimetypeById($data['mimepart']); |
| 177 | 177 | if ($data['storage_mtime'] == 0) { |
| 178 | 178 | $data['storage_mtime'] = $data['mtime']; |
| 179 | 179 | } |
| 180 | - $data['permissions'] = (int)$data['permissions']; |
|
| 180 | + $data['permissions'] = (int) $data['permissions']; |
|
| 181 | 181 | return new CacheEntry($data); |
| 182 | 182 | } |
| 183 | 183 | |
@@ -205,8 +205,8 @@ discard block |
||
| 205 | 205 | FROM `*PREFIX*filecache` WHERE `parent` = ? ORDER BY `name` ASC'; |
| 206 | 206 | $result = $this->connection->executeQuery($sql, [$fileId]); |
| 207 | 207 | $files = $result->fetchAll(); |
| 208 | - return array_map(function (array $data) { |
|
| 209 | - return self::cacheEntryFromData($data, $this->mimetypeLoader);; |
|
| 208 | + return array_map(function(array $data) { |
|
| 209 | + return self::cacheEntryFromData($data, $this->mimetypeLoader); ; |
|
| 210 | 210 | }, $files); |
| 211 | 211 | } else { |
| 212 | 212 | return array(); |
@@ -265,7 +265,7 @@ discard block |
||
| 265 | 265 | $queryParts[] = '`storage`'; |
| 266 | 266 | $params[] = $this->getNumericStorageId(); |
| 267 | 267 | |
| 268 | - $queryParts = array_map(function ($item) { |
|
| 268 | + $queryParts = array_map(function($item) { |
|
| 269 | 269 | return trim($item, "`"); |
| 270 | 270 | }, $queryParts); |
| 271 | 271 | $values = array_combine($queryParts, $params); |
@@ -274,7 +274,7 @@ discard block |
||
| 274 | 274 | 'path_hash', |
| 275 | 275 | ]) |
| 276 | 276 | ) { |
| 277 | - return (int)$this->connection->lastInsertId('*PREFIX*filecache'); |
|
| 277 | + return (int) $this->connection->lastInsertId('*PREFIX*filecache'); |
|
| 278 | 278 | } |
| 279 | 279 | |
| 280 | 280 | // The file was created in the mean time |
@@ -312,10 +312,10 @@ discard block |
||
| 312 | 312 | |
| 313 | 313 | // don't update if the data we try to set is the same as the one in the record |
| 314 | 314 | // some databases (Postgres) don't like superfluous updates |
| 315 | - $sql = 'UPDATE `*PREFIX*filecache` SET ' . implode(' = ?, ', $queryParts) . '=? ' . |
|
| 316 | - 'WHERE (' . |
|
| 317 | - implode(' <> ? OR ', $queryParts) . ' <> ? OR ' . |
|
| 318 | - implode(' IS NULL OR ', $queryParts) . ' IS NULL' . |
|
| 315 | + $sql = 'UPDATE `*PREFIX*filecache` SET '.implode(' = ?, ', $queryParts).'=? '. |
|
| 316 | + 'WHERE ('. |
|
| 317 | + implode(' <> ? OR ', $queryParts).' <> ? OR '. |
|
| 318 | + implode(' IS NULL OR ', $queryParts).' IS NULL'. |
|
| 319 | 319 | ') AND `fileid` = ? '; |
| 320 | 320 | $this->connection->executeQuery($sql, $params); |
| 321 | 321 | |
@@ -366,7 +366,7 @@ discard block |
||
| 366 | 366 | } |
| 367 | 367 | } |
| 368 | 368 | $params[] = $value; |
| 369 | - $queryParts[] = '`' . $name . '`'; |
|
| 369 | + $queryParts[] = '`'.$name.'`'; |
|
| 370 | 370 | } |
| 371 | 371 | } |
| 372 | 372 | return array($queryParts, $params); |
@@ -408,7 +408,7 @@ discard block |
||
| 408 | 408 | return -1; |
| 409 | 409 | } else { |
| 410 | 410 | $parent = $this->getParentPath($file); |
| 411 | - return (int)$this->getId($parent); |
|
| 411 | + return (int) $this->getId($parent); |
|
| 412 | 412 | } |
| 413 | 413 | } |
| 414 | 414 | |
@@ -454,7 +454,7 @@ discard block |
||
| 454 | 454 | */ |
| 455 | 455 | private function getSubFolders($entry) { |
| 456 | 456 | $children = $this->getFolderContentsById($entry['fileid']); |
| 457 | - return array_filter($children, function ($child) { |
|
| 457 | + return array_filter($children, function($child) { |
|
| 458 | 458 | return $child['mimetype'] === 'httpd/unix-directory'; |
| 459 | 459 | }); |
| 460 | 460 | } |
@@ -517,10 +517,10 @@ discard block |
||
| 517 | 517 | list($targetStorageId, $targetPath) = $this->getMoveInfo($targetPath); |
| 518 | 518 | |
| 519 | 519 | if (is_null($sourceStorageId) || $sourceStorageId === false) { |
| 520 | - throw new \Exception('Invalid source storage id: ' . $sourceStorageId); |
|
| 520 | + throw new \Exception('Invalid source storage id: '.$sourceStorageId); |
|
| 521 | 521 | } |
| 522 | 522 | if (is_null($targetStorageId) || $targetStorageId === false) { |
| 523 | - throw new \Exception('Invalid target storage id: ' . $targetStorageId); |
|
| 523 | + throw new \Exception('Invalid target storage id: '.$targetStorageId); |
|
| 524 | 524 | } |
| 525 | 525 | |
| 526 | 526 | $this->connection->beginTransaction(); |
@@ -539,7 +539,7 @@ discard block |
||
| 539 | 539 | ->set('path_hash', $fun->md5($newPathFunction)) |
| 540 | 540 | ->set('path', $newPathFunction) |
| 541 | 541 | ->where($query->expr()->eq('storage', $query->createNamedParameter($sourceStorageId, IQueryBuilder::PARAM_INT))) |
| 542 | - ->andWhere($query->expr()->like('path', $query->createNamedParameter($this->connection->escapeLikeParameter($sourcePath) . '/%'))); |
|
| 542 | + ->andWhere($query->expr()->like('path', $query->createNamedParameter($this->connection->escapeLikeParameter($sourcePath).'/%'))); |
|
| 543 | 543 | |
| 544 | 544 | try { |
| 545 | 545 | $query->execute(); |
@@ -588,7 +588,7 @@ discard block |
||
| 588 | 588 | $sql = 'SELECT `size` FROM `*PREFIX*filecache` WHERE `storage` = ? AND `path_hash` = ?'; |
| 589 | 589 | $result = $this->connection->executeQuery($sql, array($this->getNumericStorageId(), $pathHash)); |
| 590 | 590 | if ($row = $result->fetch()) { |
| 591 | - if ((int)$row['size'] === -1) { |
|
| 591 | + if ((int) $row['size'] === -1) { |
|
| 592 | 592 | return self::SHALLOW; |
| 593 | 593 | } else { |
| 594 | 594 | return self::COMPLETE; |
@@ -637,7 +637,7 @@ discard block |
||
| 637 | 637 | private function searchResultToCacheEntries(Statement $result) { |
| 638 | 638 | $files = $result->fetchAll(); |
| 639 | 639 | |
| 640 | - return array_map(function (array $data) { |
|
| 640 | + return array_map(function(array $data) { |
|
| 641 | 641 | return self::cacheEntryFromData($data, $this->mimetypeLoader); |
| 642 | 642 | }, $files); |
| 643 | 643 | } |
@@ -656,7 +656,7 @@ discard block |
||
| 656 | 656 | $where = '`mimepart` = ?'; |
| 657 | 657 | } |
| 658 | 658 | $sql = 'SELECT `fileid`, `storage`, `path`, `parent`, `name`, `mimetype`, `mimepart`, `size`, `storage_mtime`, `mtime`, `encrypted`, `etag`, `permissions`, `checksum` |
| 659 | - FROM `*PREFIX*filecache` WHERE ' . $where . ' AND `storage` = ?'; |
|
| 659 | + FROM `*PREFIX*filecache` WHERE ' . $where.' AND `storage` = ?'; |
|
| 660 | 660 | $mimetype = $this->mimetypeLoader->getId($mimetype); |
| 661 | 661 | $result = $this->connection->executeQuery($sql, array($mimetype, $this->getNumericStorageId())); |
| 662 | 662 | |
@@ -707,20 +707,20 @@ discard block |
||
| 707 | 707 | * @return ICacheEntry[] file data |
| 708 | 708 | */ |
| 709 | 709 | public function searchByTag($tag, $userId) { |
| 710 | - $sql = 'SELECT `fileid`, `storage`, `path`, `parent`, `name`, ' . |
|
| 711 | - '`mimetype`, `mimepart`, `size`, `mtime`, `storage_mtime`, ' . |
|
| 712 | - '`encrypted`, `etag`, `permissions`, `checksum` ' . |
|
| 713 | - 'FROM `*PREFIX*filecache` `file`, ' . |
|
| 714 | - '`*PREFIX*vcategory_to_object` `tagmap`, ' . |
|
| 715 | - '`*PREFIX*vcategory` `tag` ' . |
|
| 710 | + $sql = 'SELECT `fileid`, `storage`, `path`, `parent`, `name`, '. |
|
| 711 | + '`mimetype`, `mimepart`, `size`, `mtime`, `storage_mtime`, '. |
|
| 712 | + '`encrypted`, `etag`, `permissions`, `checksum` '. |
|
| 713 | + 'FROM `*PREFIX*filecache` `file`, '. |
|
| 714 | + '`*PREFIX*vcategory_to_object` `tagmap`, '. |
|
| 715 | + '`*PREFIX*vcategory` `tag` '. |
|
| 716 | 716 | // JOIN filecache to vcategory_to_object |
| 717 | - 'WHERE `file`.`fileid` = `tagmap`.`objid` ' . |
|
| 717 | + 'WHERE `file`.`fileid` = `tagmap`.`objid` '. |
|
| 718 | 718 | // JOIN vcategory_to_object to vcategory |
| 719 | - 'AND `tagmap`.`type` = `tag`.`type` ' . |
|
| 720 | - 'AND `tagmap`.`categoryid` = `tag`.`id` ' . |
|
| 719 | + 'AND `tagmap`.`type` = `tag`.`type` '. |
|
| 720 | + 'AND `tagmap`.`categoryid` = `tag`.`id` '. |
|
| 721 | 721 | // conditions |
| 722 | - 'AND `file`.`storage` = ? ' . |
|
| 723 | - 'AND `tag`.`type` = \'files\' ' . |
|
| 722 | + 'AND `file`.`storage` = ? '. |
|
| 723 | + 'AND `tag`.`type` = \'files\' '. |
|
| 724 | 724 | 'AND `tag`.`uid` = ? '; |
| 725 | 725 | if (is_int($tag)) { |
| 726 | 726 | $sql .= 'AND `tag`.`id` = ? '; |
@@ -738,7 +738,7 @@ discard block |
||
| 738 | 738 | |
| 739 | 739 | $files = $result->fetchAll(); |
| 740 | 740 | |
| 741 | - return array_map(function (array $data) { |
|
| 741 | + return array_map(function(array $data) { |
|
| 742 | 742 | return self::cacheEntryFromData($data, $this->mimetypeLoader); |
| 743 | 743 | }, $files); |
| 744 | 744 | } |
@@ -774,8 +774,8 @@ discard block |
||
| 774 | 774 | } |
| 775 | 775 | if (isset($entry['mimetype']) && $entry['mimetype'] === 'httpd/unix-directory') { |
| 776 | 776 | $id = $entry['fileid']; |
| 777 | - $sql = 'SELECT SUM(`size`) AS f1, MIN(`size`) AS f2 ' . |
|
| 778 | - 'FROM `*PREFIX*filecache` ' . |
|
| 777 | + $sql = 'SELECT SUM(`size`) AS f1, MIN(`size`) AS f2 '. |
|
| 778 | + 'FROM `*PREFIX*filecache` '. |
|
| 779 | 779 | 'WHERE `parent` = ? AND `storage` = ?'; |
| 780 | 780 | $result = $this->connection->executeQuery($sql, array($id, $this->getNumericStorageId())); |
| 781 | 781 | if ($row = $result->fetch()) { |