Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like QueryBuilder often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use QueryBuilder, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 53 | class QueryBuilder |
||
| 54 | { |
||
| 55 | /* |
||
| 56 | * The query types. |
||
| 57 | */ |
||
| 58 | const SELECT = 0; |
||
| 59 | const DELETE = 1; |
||
| 60 | const UPDATE = 2; |
||
| 61 | const INSERT = 3; |
||
| 62 | |||
| 63 | /* |
||
| 64 | * The builder states. |
||
| 65 | */ |
||
| 66 | const STATE_DIRTY = 0; |
||
| 67 | const STATE_CLEAN = 1; |
||
| 68 | |||
| 69 | /** |
||
| 70 | * The DBAL Connection. |
||
| 71 | * |
||
| 72 | * @var \Doctrine\DBAL\Connection |
||
| 73 | */ |
||
| 74 | private $connection; |
||
| 75 | |||
| 76 | /** |
||
| 77 | * @var array The array of SQL parts collected. |
||
| 78 | */ |
||
| 79 | private $sqlParts = [ |
||
| 80 | 'select' => [], |
||
| 81 | 'from' => [], |
||
| 82 | 'join' => [], |
||
| 83 | 'set' => [], |
||
| 84 | 'where' => null, |
||
| 85 | 'groupBy' => [], |
||
| 86 | 'having' => null, |
||
| 87 | 'orderBy' => [], |
||
| 88 | 'values' => [], |
||
| 89 | ]; |
||
| 90 | |||
| 91 | /** |
||
| 92 | * The complete SQL string for this query. |
||
| 93 | * |
||
| 94 | * @var string |
||
| 95 | */ |
||
| 96 | private $sql; |
||
| 97 | |||
| 98 | /** |
||
| 99 | * The query parameters. |
||
| 100 | * |
||
| 101 | * @var array |
||
| 102 | */ |
||
| 103 | private $params = []; |
||
| 104 | |||
| 105 | /** |
||
| 106 | * The parameter type map of this query. |
||
| 107 | * |
||
| 108 | * @var array |
||
| 109 | */ |
||
| 110 | private $paramTypes = []; |
||
| 111 | |||
| 112 | /** |
||
| 113 | * The type of query this is. Can be select, update or delete. |
||
| 114 | * |
||
| 115 | * @var int |
||
| 116 | */ |
||
| 117 | private $type = self::SELECT; |
||
| 118 | |||
| 119 | /** |
||
| 120 | * The state of the query object. Can be dirty or clean. |
||
| 121 | * |
||
| 122 | * @var int |
||
| 123 | */ |
||
| 124 | private $state = self::STATE_CLEAN; |
||
| 125 | |||
| 126 | /** |
||
| 127 | * The index of the first result to retrieve. |
||
| 128 | * |
||
| 129 | * @var int |
||
| 130 | */ |
||
| 131 | private $firstResult = null; |
||
| 132 | |||
| 133 | /** |
||
| 134 | * The maximum number of results to retrieve. |
||
| 135 | * |
||
| 136 | * @var int |
||
| 137 | */ |
||
| 138 | private $maxResults = null; |
||
| 139 | |||
| 140 | /** |
||
| 141 | * The counter of bound parameters used with {@see bindValue). |
||
| 142 | * |
||
| 143 | * @var int |
||
| 144 | */ |
||
| 145 | private $boundCounter = 0; |
||
| 146 | |||
| 147 | /** |
||
| 148 | * Initializes a new <tt>QueryBuilder</tt>. |
||
| 149 | * |
||
| 150 | * @param \Doctrine\DBAL\Connection $connection The DBAL Connection. |
||
| 151 | */ |
||
| 152 | public function __construct(Connection $connection) |
||
| 156 | |||
| 157 | /** |
||
| 158 | * Gets an ExpressionBuilder used for object-oriented construction of query expressions. |
||
| 159 | * This producer method is intended for convenient inline usage. Example: |
||
| 160 | * |
||
| 161 | * <code> |
||
| 162 | * $qb = $conn->createQueryBuilder() |
||
| 163 | * ->select('u') |
||
| 164 | * ->from('users', 'u') |
||
| 165 | * ->where($qb->expr()->eq('u.id', 1)); |
||
| 166 | * </code> |
||
| 167 | * |
||
| 168 | * For more complex expression construction, consider storing the expression |
||
| 169 | * builder object in a local variable. |
||
| 170 | * |
||
| 171 | * @return \Doctrine\DBAL\Query\Expression\ExpressionBuilder |
||
| 172 | */ |
||
| 173 | public function expr() |
||
| 177 | |||
| 178 | /** |
||
| 179 | * Gets the type of the currently built query. |
||
| 180 | * |
||
| 181 | * @return int |
||
| 182 | */ |
||
| 183 | public function getType() |
||
| 187 | |||
| 188 | /** |
||
| 189 | * Gets the associated DBAL Connection for this query builder. |
||
| 190 | * |
||
| 191 | * @return \Doctrine\DBAL\Connection |
||
| 192 | */ |
||
| 193 | public function getConnection() |
||
| 197 | |||
| 198 | /** |
||
| 199 | * Gets the state of this query builder instance. |
||
| 200 | * |
||
| 201 | * @return int Either QueryBuilder::STATE_DIRTY or QueryBuilder::STATE_CLEAN. |
||
| 202 | */ |
||
| 203 | public function getState() |
||
| 207 | |||
| 208 | /** |
||
| 209 | * Executes this query using the bound parameters and their types. |
||
| 210 | * |
||
| 211 | * Uses {@see Connection::executeQuery} for select statements and {@see Connection::executeUpdate} |
||
| 212 | * for insert, update and delete statements. |
||
| 213 | * |
||
| 214 | * @return \Doctrine\DBAL\Driver\Statement|int |
||
|
|
|||
| 215 | */ |
||
| 216 | public function execute() |
||
| 224 | |||
| 225 | /** |
||
| 226 | * Gets the complete SQL string formed by the current specifications of this QueryBuilder. |
||
| 227 | * |
||
| 228 | * <code> |
||
| 229 | * $qb = $em->createQueryBuilder() |
||
| 230 | * ->select('u') |
||
| 231 | * ->from('User', 'u') |
||
| 232 | * echo $qb->getSQL(); // SELECT u FROM User u |
||
| 233 | * </code> |
||
| 234 | * |
||
| 235 | * @return string The SQL query string. |
||
| 236 | */ |
||
| 237 | public function getSQL() |
||
| 266 | |||
| 267 | /** |
||
| 268 | * Sets a query parameter for the query being constructed. |
||
| 269 | * |
||
| 270 | * <code> |
||
| 271 | * $qb = $conn->createQueryBuilder() |
||
| 272 | * ->select('u') |
||
| 273 | * ->from('users', 'u') |
||
| 274 | * ->where('u.id = :user_id') |
||
| 275 | * ->setParameter(':user_id', 1); |
||
| 276 | * </code> |
||
| 277 | * |
||
| 278 | * @param string|int $key The parameter position or name. |
||
| 279 | * @param mixed $value The parameter value. |
||
| 280 | * @param string|int|null $type One of the {@link \Doctrine\DBAL\ParameterType} constants. |
||
| 281 | * |
||
| 282 | * @return $this This QueryBuilder instance. |
||
| 283 | */ |
||
| 284 | public function setParameter($key, $value, $type = null) |
||
| 294 | |||
| 295 | /** |
||
| 296 | * Sets a collection of query parameters for the query being constructed. |
||
| 297 | * |
||
| 298 | * <code> |
||
| 299 | * $qb = $conn->createQueryBuilder() |
||
| 300 | * ->select('u') |
||
| 301 | * ->from('users', 'u') |
||
| 302 | * ->where('u.id = :user_id1 OR u.id = :user_id2') |
||
| 303 | * ->setParameters(array( |
||
| 304 | * ':user_id1' => 1, |
||
| 305 | * ':user_id2' => 2 |
||
| 306 | * )); |
||
| 307 | * </code> |
||
| 308 | * |
||
| 309 | * @param array $params The query parameters to set. |
||
| 310 | * @param array $types The query parameters types to set. |
||
| 311 | * |
||
| 312 | * @return $this This QueryBuilder instance. |
||
| 313 | */ |
||
| 314 | public function setParameters(array $params, array $types = []) |
||
| 321 | |||
| 322 | /** |
||
| 323 | * Gets all defined query parameters for the query being constructed indexed by parameter index or name. |
||
| 324 | * |
||
| 325 | * @return array The currently defined query parameters indexed by parameter index or name. |
||
| 326 | */ |
||
| 327 | public function getParameters() |
||
| 331 | |||
| 332 | /** |
||
| 333 | * Gets a (previously set) query parameter of the query being constructed. |
||
| 334 | * |
||
| 335 | * @param mixed $key The key (index or name) of the bound parameter. |
||
| 336 | * |
||
| 337 | * @return mixed The value of the bound parameter. |
||
| 338 | */ |
||
| 339 | public function getParameter($key) |
||
| 343 | |||
| 344 | /** |
||
| 345 | * Gets all defined query parameter types for the query being constructed indexed by parameter index or name. |
||
| 346 | * |
||
| 347 | * @return array The currently defined query parameter types indexed by parameter index or name. |
||
| 348 | */ |
||
| 349 | public function getParameterTypes() |
||
| 353 | |||
| 354 | /** |
||
| 355 | * Gets a (previously set) query parameter type of the query being constructed. |
||
| 356 | * |
||
| 357 | * @param mixed $key The key (index or name) of the bound parameter type. |
||
| 358 | * |
||
| 359 | * @return mixed The value of the bound parameter type. |
||
| 360 | */ |
||
| 361 | public function getParameterType($key) |
||
| 365 | |||
| 366 | /** |
||
| 367 | * Sets the position of the first result to retrieve (the "offset"). |
||
| 368 | * |
||
| 369 | * @param int $firstResult The first result to return. |
||
| 370 | * |
||
| 371 | * @return $this This QueryBuilder instance. |
||
| 372 | */ |
||
| 373 | public function setFirstResult($firstResult) |
||
| 380 | |||
| 381 | /** |
||
| 382 | * Gets the position of the first result the query object was set to retrieve (the "offset"). |
||
| 383 | * Returns NULL if {@link setFirstResult} was not applied to this QueryBuilder. |
||
| 384 | * |
||
| 385 | * @return int The position of the first result. |
||
| 386 | */ |
||
| 387 | public function getFirstResult() |
||
| 391 | |||
| 392 | /** |
||
| 393 | * Sets the maximum number of results to retrieve (the "limit"). |
||
| 394 | * |
||
| 395 | * @param int $maxResults The maximum number of results to retrieve. |
||
| 396 | * |
||
| 397 | * @return $this This QueryBuilder instance. |
||
| 398 | */ |
||
| 399 | public function setMaxResults($maxResults) |
||
| 406 | |||
| 407 | /** |
||
| 408 | * Gets the maximum number of results the query object was set to retrieve (the "limit"). |
||
| 409 | * Returns NULL if {@link setMaxResults} was not applied to this query builder. |
||
| 410 | * |
||
| 411 | * @return int The maximum number of results. |
||
| 412 | */ |
||
| 413 | public function getMaxResults() |
||
| 417 | |||
| 418 | /** |
||
| 419 | * Either appends to or replaces a single, generic query part. |
||
| 420 | * |
||
| 421 | * The available parts are: 'select', 'from', 'set', 'where', |
||
| 422 | * 'groupBy', 'having' and 'orderBy'. |
||
| 423 | * |
||
| 424 | * @param string $sqlPartName |
||
| 425 | * @param string $sqlPart |
||
| 426 | * @param bool $append |
||
| 427 | * |
||
| 428 | * @return $this This QueryBuilder instance. |
||
| 429 | */ |
||
| 430 | public function add($sqlPartName, $sqlPart, $append = false) |
||
| 462 | |||
| 463 | /** |
||
| 464 | * Specifies an item that is to be returned in the query result. |
||
| 465 | * Replaces any previously specified selections, if any. |
||
| 466 | * |
||
| 467 | * <code> |
||
| 468 | * $qb = $conn->createQueryBuilder() |
||
| 469 | * ->select('u.id', 'p.id') |
||
| 470 | * ->from('users', 'u') |
||
| 471 | * ->leftJoin('u', 'phonenumbers', 'p', 'u.id = p.user_id'); |
||
| 472 | * </code> |
||
| 473 | * |
||
| 474 | * @param mixed $select The selection expressions. |
||
| 475 | * |
||
| 476 | * @return $this This QueryBuilder instance. |
||
| 477 | */ |
||
| 478 | View Code Duplication | public function select($select = null) |
|
| 490 | |||
| 491 | /** |
||
| 492 | * Adds an item that is to be returned in the query result. |
||
| 493 | * |
||
| 494 | * <code> |
||
| 495 | * $qb = $conn->createQueryBuilder() |
||
| 496 | * ->select('u.id') |
||
| 497 | * ->addSelect('p.id') |
||
| 498 | * ->from('users', 'u') |
||
| 499 | * ->leftJoin('u', 'phonenumbers', 'u.id = p.user_id'); |
||
| 500 | * </code> |
||
| 501 | * |
||
| 502 | * @param mixed $select The selection expression. |
||
| 503 | * |
||
| 504 | * @return $this This QueryBuilder instance. |
||
| 505 | */ |
||
| 506 | View Code Duplication | public function addSelect($select = null) |
|
| 518 | |||
| 519 | /** |
||
| 520 | * Turns the query being built into a bulk delete query that ranges over |
||
| 521 | * a certain table. |
||
| 522 | * |
||
| 523 | * <code> |
||
| 524 | * $qb = $conn->createQueryBuilder() |
||
| 525 | * ->delete('users', 'u') |
||
| 526 | * ->where('u.id = :user_id'); |
||
| 527 | * ->setParameter(':user_id', 1); |
||
| 528 | * </code> |
||
| 529 | * |
||
| 530 | * @param string $delete The table whose rows are subject to the deletion. |
||
| 531 | * @param string $alias The table alias used in the constructed query. |
||
| 532 | * |
||
| 533 | * @return $this This QueryBuilder instance. |
||
| 534 | */ |
||
| 535 | View Code Duplication | public function delete($delete = null, $alias = null) |
|
| 548 | |||
| 549 | /** |
||
| 550 | * Turns the query being built into a bulk update query that ranges over |
||
| 551 | * a certain table |
||
| 552 | * |
||
| 553 | * <code> |
||
| 554 | * $qb = $conn->createQueryBuilder() |
||
| 555 | * ->update('users', 'u') |
||
| 556 | * ->set('u.last_login', 'NOW()') |
||
| 557 | * ->where('u.id = ?'); |
||
| 558 | * </code> |
||
| 559 | * |
||
| 560 | * @param string $update The table whose rows are subject to the update. |
||
| 561 | * @param string $alias The table alias used in the constructed query. |
||
| 562 | * |
||
| 563 | * @return $this This QueryBuilder instance. |
||
| 564 | */ |
||
| 565 | View Code Duplication | public function update($update = null, $alias = null) |
|
| 578 | |||
| 579 | /** |
||
| 580 | * Turns the query being built into an insert query that inserts into |
||
| 581 | * a certain table |
||
| 582 | * |
||
| 583 | * <code> |
||
| 584 | * $qb = $conn->createQueryBuilder() |
||
| 585 | * ->insert('users') |
||
| 586 | * ->values( |
||
| 587 | * array( |
||
| 588 | * 'name' => '?', |
||
| 589 | * 'password' => '?' |
||
| 590 | * ) |
||
| 591 | * ); |
||
| 592 | * </code> |
||
| 593 | * |
||
| 594 | * @param string $insert The table into which the rows should be inserted. |
||
| 595 | * |
||
| 596 | * @return $this This QueryBuilder instance. |
||
| 597 | */ |
||
| 598 | public function insert($insert = null) |
||
| 610 | |||
| 611 | /** |
||
| 612 | * Creates and adds a query root corresponding to the table identified by the |
||
| 613 | * given alias, forming a cartesian product with any existing query roots. |
||
| 614 | * |
||
| 615 | * <code> |
||
| 616 | * $qb = $conn->createQueryBuilder() |
||
| 617 | * ->select('u.id') |
||
| 618 | * ->from('users', 'u') |
||
| 619 | * </code> |
||
| 620 | * |
||
| 621 | * @param string $from The table. |
||
| 622 | * @param string|null $alias The alias of the table. |
||
| 623 | * |
||
| 624 | * @return $this This QueryBuilder instance. |
||
| 625 | */ |
||
| 626 | public function from($from, $alias = null) |
||
| 633 | |||
| 634 | /** |
||
| 635 | * Creates and adds a join to the query. |
||
| 636 | * |
||
| 637 | * <code> |
||
| 638 | * $qb = $conn->createQueryBuilder() |
||
| 639 | * ->select('u.name') |
||
| 640 | * ->from('users', 'u') |
||
| 641 | * ->join('u', 'phonenumbers', 'p', 'p.is_primary = 1'); |
||
| 642 | * </code> |
||
| 643 | * |
||
| 644 | * @param string $fromAlias The alias that points to a from clause. |
||
| 645 | * @param string $join The table name to join. |
||
| 646 | * @param string $alias The alias of the join table. |
||
| 647 | * @param string $condition The condition for the join. |
||
| 648 | * |
||
| 649 | * @return $this This QueryBuilder instance. |
||
| 650 | */ |
||
| 651 | public function join($fromAlias, $join, $alias, $condition = null) |
||
| 655 | |||
| 656 | /** |
||
| 657 | * Creates and adds a join to the query. |
||
| 658 | * |
||
| 659 | * <code> |
||
| 660 | * $qb = $conn->createQueryBuilder() |
||
| 661 | * ->select('u.name') |
||
| 662 | * ->from('users', 'u') |
||
| 663 | * ->innerJoin('u', 'phonenumbers', 'p', 'p.is_primary = 1'); |
||
| 664 | * </code> |
||
| 665 | * |
||
| 666 | * @param string $fromAlias The alias that points to a from clause. |
||
| 667 | * @param string $join The table name to join. |
||
| 668 | * @param string $alias The alias of the join table. |
||
| 669 | * @param string $condition The condition for the join. |
||
| 670 | * |
||
| 671 | * @return $this This QueryBuilder instance. |
||
| 672 | */ |
||
| 673 | View Code Duplication | public function innerJoin($fromAlias, $join, $alias, $condition = null) |
|
| 684 | |||
| 685 | /** |
||
| 686 | * Creates and adds a left join to the query. |
||
| 687 | * |
||
| 688 | * <code> |
||
| 689 | * $qb = $conn->createQueryBuilder() |
||
| 690 | * ->select('u.name') |
||
| 691 | * ->from('users', 'u') |
||
| 692 | * ->leftJoin('u', 'phonenumbers', 'p', 'p.is_primary = 1'); |
||
| 693 | * </code> |
||
| 694 | * |
||
| 695 | * @param string $fromAlias The alias that points to a from clause. |
||
| 696 | * @param string $join The table name to join. |
||
| 697 | * @param string $alias The alias of the join table. |
||
| 698 | * @param string $condition The condition for the join. |
||
| 699 | * |
||
| 700 | * @return $this This QueryBuilder instance. |
||
| 701 | */ |
||
| 702 | View Code Duplication | public function leftJoin($fromAlias, $join, $alias, $condition = null) |
|
| 713 | |||
| 714 | /** |
||
| 715 | * Creates and adds a right join to the query. |
||
| 716 | * |
||
| 717 | * <code> |
||
| 718 | * $qb = $conn->createQueryBuilder() |
||
| 719 | * ->select('u.name') |
||
| 720 | * ->from('users', 'u') |
||
| 721 | * ->rightJoin('u', 'phonenumbers', 'p', 'p.is_primary = 1'); |
||
| 722 | * </code> |
||
| 723 | * |
||
| 724 | * @param string $fromAlias The alias that points to a from clause. |
||
| 725 | * @param string $join The table name to join. |
||
| 726 | * @param string $alias The alias of the join table. |
||
| 727 | * @param string $condition The condition for the join. |
||
| 728 | * |
||
| 729 | * @return $this This QueryBuilder instance. |
||
| 730 | */ |
||
| 731 | View Code Duplication | public function rightJoin($fromAlias, $join, $alias, $condition = null) |
|
| 742 | |||
| 743 | /** |
||
| 744 | * Sets a new value for a column in a bulk update query. |
||
| 745 | * |
||
| 746 | * <code> |
||
| 747 | * $qb = $conn->createQueryBuilder() |
||
| 748 | * ->update('users', 'u') |
||
| 749 | * ->set('u.last_login', 'NOW()') |
||
| 750 | * ->where('u.id = ?'); |
||
| 751 | * </code> |
||
| 752 | * |
||
| 753 | * @param string $key The column to set. |
||
| 754 | * @param string $value The value, expression, placeholder, etc. |
||
| 755 | * |
||
| 756 | * @return $this This QueryBuilder instance. |
||
| 757 | */ |
||
| 758 | public function set($key, $value) |
||
| 762 | |||
| 763 | /** |
||
| 764 | * Specifies one or more restrictions to the query result. |
||
| 765 | * Replaces any previously specified restrictions, if any. |
||
| 766 | * |
||
| 767 | * <code> |
||
| 768 | * $qb = $conn->createQueryBuilder() |
||
| 769 | * ->select('u.name') |
||
| 770 | * ->from('users', 'u') |
||
| 771 | * ->where('u.id = ?'); |
||
| 772 | * |
||
| 773 | * // You can optionally programatically build and/or expressions |
||
| 774 | * $qb = $conn->createQueryBuilder(); |
||
| 775 | * |
||
| 776 | * $or = $qb->expr()->orx(); |
||
| 777 | * $or->add($qb->expr()->eq('u.id', 1)); |
||
| 778 | * $or->add($qb->expr()->eq('u.id', 2)); |
||
| 779 | * |
||
| 780 | * $qb->update('users', 'u') |
||
| 781 | * ->set('u.last_login', 'NOW()') |
||
| 782 | * ->where($or); |
||
| 783 | * </code> |
||
| 784 | * |
||
| 785 | * @param mixed $predicates The restriction predicates. |
||
| 786 | * |
||
| 787 | * @return $this This QueryBuilder instance. |
||
| 788 | */ |
||
| 789 | View Code Duplication | public function where($predicates) |
|
| 797 | |||
| 798 | /** |
||
| 799 | * Adds one or more restrictions to the query results, forming a logical |
||
| 800 | * conjunction with any previously specified restrictions. |
||
| 801 | * |
||
| 802 | * <code> |
||
| 803 | * $qb = $conn->createQueryBuilder() |
||
| 804 | * ->select('u') |
||
| 805 | * ->from('users', 'u') |
||
| 806 | * ->where('u.username LIKE ?') |
||
| 807 | * ->andWhere('u.is_active = 1'); |
||
| 808 | * </code> |
||
| 809 | * |
||
| 810 | * @param mixed $where The query restrictions. |
||
| 811 | * |
||
| 812 | * @return $this This QueryBuilder instance. |
||
| 813 | * |
||
| 814 | * @see where() |
||
| 815 | */ |
||
| 816 | View Code Duplication | public function andWhere($where) |
|
| 830 | |||
| 831 | /** |
||
| 832 | * Adds one or more restrictions to the query results, forming a logical |
||
| 833 | * disjunction with any previously specified restrictions. |
||
| 834 | * |
||
| 835 | * <code> |
||
| 836 | * $qb = $em->createQueryBuilder() |
||
| 837 | * ->select('u.name') |
||
| 838 | * ->from('users', 'u') |
||
| 839 | * ->where('u.id = 1') |
||
| 840 | * ->orWhere('u.id = 2'); |
||
| 841 | * </code> |
||
| 842 | * |
||
| 843 | * @param mixed $where The WHERE statement. |
||
| 844 | * |
||
| 845 | * @return $this This QueryBuilder instance. |
||
| 846 | * |
||
| 847 | * @see where() |
||
| 848 | */ |
||
| 849 | View Code Duplication | public function orWhere($where) |
|
| 863 | |||
| 864 | /** |
||
| 865 | * Specifies a grouping over the results of the query. |
||
| 866 | * Replaces any previously specified groupings, if any. |
||
| 867 | * |
||
| 868 | * <code> |
||
| 869 | * $qb = $conn->createQueryBuilder() |
||
| 870 | * ->select('u.name') |
||
| 871 | * ->from('users', 'u') |
||
| 872 | * ->groupBy('u.id'); |
||
| 873 | * </code> |
||
| 874 | * |
||
| 875 | * @param mixed $groupBy The grouping expression. |
||
| 876 | * |
||
| 877 | * @return $this This QueryBuilder instance. |
||
| 878 | */ |
||
| 879 | View Code Duplication | public function groupBy($groupBy) |
|
| 889 | |||
| 890 | |||
| 891 | /** |
||
| 892 | * Adds a grouping expression to the query. |
||
| 893 | * |
||
| 894 | * <code> |
||
| 895 | * $qb = $conn->createQueryBuilder() |
||
| 896 | * ->select('u.name') |
||
| 897 | * ->from('users', 'u') |
||
| 898 | * ->groupBy('u.lastLogin'); |
||
| 899 | * ->addGroupBy('u.createdAt') |
||
| 900 | * </code> |
||
| 901 | * |
||
| 902 | * @param mixed $groupBy The grouping expression. |
||
| 903 | * |
||
| 904 | * @return $this This QueryBuilder instance. |
||
| 905 | */ |
||
| 906 | View Code Duplication | public function addGroupBy($groupBy) |
|
| 916 | |||
| 917 | /** |
||
| 918 | * Sets a value for a column in an insert query. |
||
| 919 | * |
||
| 920 | * <code> |
||
| 921 | * $qb = $conn->createQueryBuilder() |
||
| 922 | * ->insert('users') |
||
| 923 | * ->values( |
||
| 924 | * array( |
||
| 925 | * 'name' => '?' |
||
| 926 | * ) |
||
| 927 | * ) |
||
| 928 | * ->setValue('password', '?'); |
||
| 929 | * </code> |
||
| 930 | * |
||
| 931 | * @param string $column The column into which the value should be inserted. |
||
| 932 | * @param string $value The value that should be inserted into the column. |
||
| 933 | * |
||
| 934 | * @return $this This QueryBuilder instance. |
||
| 935 | */ |
||
| 936 | public function setValue($column, $value) |
||
| 942 | |||
| 943 | /** |
||
| 944 | * Specifies values for an insert query indexed by column names. |
||
| 945 | * Replaces any previous values, if any. |
||
| 946 | * |
||
| 947 | * <code> |
||
| 948 | * $qb = $conn->createQueryBuilder() |
||
| 949 | * ->insert('users') |
||
| 950 | * ->values( |
||
| 951 | * array( |
||
| 952 | * 'name' => '?', |
||
| 953 | * 'password' => '?' |
||
| 954 | * ) |
||
| 955 | * ); |
||
| 956 | * </code> |
||
| 957 | * |
||
| 958 | * @param array $values The values to specify for the insert query indexed by column names. |
||
| 959 | * |
||
| 960 | * @return $this This QueryBuilder instance. |
||
| 961 | */ |
||
| 962 | public function values(array $values) |
||
| 966 | |||
| 967 | /** |
||
| 968 | * Specifies a restriction over the groups of the query. |
||
| 969 | * Replaces any previous having restrictions, if any. |
||
| 970 | * |
||
| 971 | * @param mixed $having The restriction over the groups. |
||
| 972 | * |
||
| 973 | * @return $this This QueryBuilder instance. |
||
| 974 | */ |
||
| 975 | View Code Duplication | public function having($having) |
|
| 983 | |||
| 984 | /** |
||
| 985 | * Adds a restriction over the groups of the query, forming a logical |
||
| 986 | * conjunction with any existing having restrictions. |
||
| 987 | * |
||
| 988 | * @param mixed $having The restriction to append. |
||
| 989 | * |
||
| 990 | * @return $this This QueryBuilder instance. |
||
| 991 | */ |
||
| 992 | View Code Duplication | public function andHaving($having) |
|
| 1006 | |||
| 1007 | /** |
||
| 1008 | * Adds a restriction over the groups of the query, forming a logical |
||
| 1009 | * disjunction with any existing having restrictions. |
||
| 1010 | * |
||
| 1011 | * @param mixed $having The restriction to add. |
||
| 1012 | * |
||
| 1013 | * @return $this This QueryBuilder instance. |
||
| 1014 | */ |
||
| 1015 | View Code Duplication | public function orHaving($having) |
|
| 1029 | |||
| 1030 | /** |
||
| 1031 | * Specifies an ordering for the query results. |
||
| 1032 | * Replaces any previously specified orderings, if any. |
||
| 1033 | * |
||
| 1034 | * @param string $sort The ordering expression. |
||
| 1035 | * @param string $order The ordering direction. |
||
| 1036 | * |
||
| 1037 | * @return $this This QueryBuilder instance. |
||
| 1038 | */ |
||
| 1039 | public function orderBy($sort, $order = null) |
||
| 1043 | |||
| 1044 | /** |
||
| 1045 | * Adds an ordering to the query results. |
||
| 1046 | * |
||
| 1047 | * @param string $sort The ordering expression. |
||
| 1048 | * @param string $order The ordering direction. |
||
| 1049 | * |
||
| 1050 | * @return $this This QueryBuilder instance. |
||
| 1051 | */ |
||
| 1052 | public function addOrderBy($sort, $order = null) |
||
| 1056 | |||
| 1057 | /** |
||
| 1058 | * Gets a query part by its name. |
||
| 1059 | * |
||
| 1060 | * @param string $queryPartName |
||
| 1061 | * |
||
| 1062 | * @return mixed |
||
| 1063 | */ |
||
| 1064 | public function getQueryPart($queryPartName) |
||
| 1068 | |||
| 1069 | /** |
||
| 1070 | * Gets all query parts. |
||
| 1071 | * |
||
| 1072 | * @return array |
||
| 1073 | */ |
||
| 1074 | public function getQueryParts() |
||
| 1078 | |||
| 1079 | /** |
||
| 1080 | * Resets SQL parts. |
||
| 1081 | * |
||
| 1082 | * @param array|null $queryPartNames |
||
| 1083 | * |
||
| 1084 | * @return $this This QueryBuilder instance. |
||
| 1085 | */ |
||
| 1086 | public function resetQueryParts($queryPartNames = null) |
||
| 1098 | |||
| 1099 | /** |
||
| 1100 | * Resets a single SQL part. |
||
| 1101 | * |
||
| 1102 | * @param string $queryPartName |
||
| 1103 | * |
||
| 1104 | * @return $this This QueryBuilder instance. |
||
| 1105 | */ |
||
| 1106 | public function resetQueryPart($queryPartName) |
||
| 1115 | |||
| 1116 | /** |
||
| 1117 | * @return string |
||
| 1118 | * |
||
| 1119 | * @throws \Doctrine\DBAL\Query\QueryException |
||
| 1120 | */ |
||
| 1121 | private function getSQLForSelect() |
||
| 1141 | |||
| 1142 | /** |
||
| 1143 | * @return string[] |
||
| 1144 | */ |
||
| 1145 | private function getFromClauses() |
||
| 1169 | |||
| 1170 | /** |
||
| 1171 | * @param array $knownAliases |
||
| 1172 | * |
||
| 1173 | * @throws QueryException |
||
| 1174 | */ |
||
| 1175 | private function verifyAllAliasesAreKnown(array $knownAliases) |
||
| 1183 | |||
| 1184 | /** |
||
| 1185 | * @return bool |
||
| 1186 | */ |
||
| 1187 | private function isLimitQuery() |
||
| 1191 | |||
| 1192 | /** |
||
| 1193 | * Converts this instance into an INSERT string in SQL. |
||
| 1194 | * |
||
| 1195 | * @return string |
||
| 1196 | */ |
||
| 1197 | private function getSQLForInsert() |
||
| 1203 | |||
| 1204 | /** |
||
| 1205 | * Converts this instance into an UPDATE string in SQL. |
||
| 1206 | * |
||
| 1207 | * @return string |
||
| 1208 | */ |
||
| 1209 | private function getSQLForUpdate() |
||
| 1218 | |||
| 1219 | /** |
||
| 1220 | * Converts this instance into a DELETE string in SQL. |
||
| 1221 | * |
||
| 1222 | * @return string |
||
| 1223 | */ |
||
| 1224 | private function getSQLForDelete() |
||
| 1231 | |||
| 1232 | /** |
||
| 1233 | * Gets a string representation of this QueryBuilder which corresponds to |
||
| 1234 | * the final SQL query being constructed. |
||
| 1235 | * |
||
| 1236 | * @return string The string representation of this QueryBuilder. |
||
| 1237 | */ |
||
| 1238 | public function __toString() |
||
| 1242 | |||
| 1243 | /** |
||
| 1244 | * Creates a new named parameter and bind the value $value to it. |
||
| 1245 | * |
||
| 1246 | * This method provides a shortcut for PDOStatement::bindValue |
||
| 1247 | * when using prepared statements. |
||
| 1248 | * |
||
| 1249 | * The parameter $value specifies the value that you want to bind. If |
||
| 1250 | * $placeholder is not provided bindValue() will automatically create a |
||
| 1251 | * placeholder for you. An automatic placeholder will be of the name |
||
| 1252 | * ':dcValue1', ':dcValue2' etc. |
||
| 1253 | * |
||
| 1254 | * For more information see {@link http://php.net/pdostatement-bindparam} |
||
| 1255 | * |
||
| 1256 | * Example: |
||
| 1257 | * <code> |
||
| 1258 | * $value = 2; |
||
| 1259 | * $q->eq( 'id', $q->bindValue( $value ) ); |
||
| 1260 | * $stmt = $q->executeQuery(); // executed with 'id = 2' |
||
| 1261 | * </code> |
||
| 1262 | * |
||
| 1263 | * @license New BSD License |
||
| 1264 | * @link http://www.zetacomponents.org |
||
| 1265 | * |
||
| 1266 | * @param mixed $value |
||
| 1267 | * @param mixed $type |
||
| 1268 | * @param string $placeHolder The name to bind with. The string must start with a colon ':'. |
||
| 1269 | * |
||
| 1270 | * @return string the placeholder name used. |
||
| 1271 | */ |
||
| 1272 | public function createNamedParameter($value, $type = ParameterType::STRING, $placeHolder = null) |
||
| 1282 | |||
| 1283 | /** |
||
| 1284 | * Creates a new positional parameter and bind the given value to it. |
||
| 1285 | * |
||
| 1286 | * Attention: If you are using positional parameters with the query builder you have |
||
| 1287 | * to be very careful to bind all parameters in the order they appear in the SQL |
||
| 1288 | * statement , otherwise they get bound in the wrong order which can lead to serious |
||
| 1289 | * bugs in your code. |
||
| 1290 | * |
||
| 1291 | * Example: |
||
| 1292 | * <code> |
||
| 1293 | * $qb = $conn->createQueryBuilder(); |
||
| 1294 | * $qb->select('u.*') |
||
| 1295 | * ->from('users', 'u') |
||
| 1296 | * ->where('u.username = ' . $qb->createPositionalParameter('Foo', ParameterType::STRING)) |
||
| 1297 | * ->orWhere('u.username = ' . $qb->createPositionalParameter('Bar', ParameterType::STRING)) |
||
| 1298 | * </code> |
||
| 1299 | * |
||
| 1300 | * @param mixed $value |
||
| 1301 | * @param int $type |
||
| 1302 | * |
||
| 1303 | * @return string |
||
| 1304 | */ |
||
| 1305 | public function createPositionalParameter($value, $type = ParameterType::STRING) |
||
| 1312 | |||
| 1313 | /** |
||
| 1314 | * @param string $fromAlias |
||
| 1315 | * @param array $knownAliases |
||
| 1316 | * |
||
| 1317 | * @return string |
||
| 1318 | * |
||
| 1319 | * @throws QueryException |
||
| 1320 | */ |
||
| 1321 | private function getSQLForJoins($fromAlias, array &$knownAliases) |
||
| 1343 | |||
| 1344 | /** |
||
| 1345 | * Deep clone of all expression objects in the SQL parts. |
||
| 1346 | * |
||
| 1347 | * @return void |
||
| 1348 | */ |
||
| 1349 | public function __clone() |
||
| 1369 | } |
||
| 1370 |
This check compares the return type specified in the
@returnannotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.