Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add QueryBuilder support for UNION clause #6369

Merged
merged 1 commit into from
Jun 14, 2024

Commits on Jun 14, 2024

  1. Add QueryBuilder support for UNION clause

    The `UNION` operator is used to combine the result-set
    of two or more `SELECT` statements, which all database
    vendors supports with usual specialities for each.
    
    Still, there is a common shared subset which works for
    all of them:
    
    ```
        SELECT column_name(s) FROM table1
        WHERE ...
    
        UNION <ALL | DISTINCT>
    
        SELECT column_name(s) FROM table2
        WHERE ...
    
        ORDER BY ...
        LIMIT x OFFSET y
    ```
    
    with shared common requirements:
    
    * Each `SELECT` must return the same fields
      in number, naming and order.
    
    * Each `SELECT` **must not** have `ORDER BY`,
      expect MySQL allowing it to be used as sub
      query expression encapsulated in parenthesis.
    
    It is now possible to build `UNION` queries using
    following additional QueryBuilder API methods:
    
    * `union(string|QueryBuilder $part)` to create a `UNION`
       query retrieving unique rows
    * `addUnion(string|QueryBuilder $part, UnionType $type)`
       to add `UNION (ALL|DISTINCT)` query with the selected
       union query type.
    
    This follows the generic logic of `select(...)` and
    `addSelect(...)` along with introducing new UnionType
    enum and internal QueryType::UNION enum case.
    
    Technically, the SQL build process is dispatched to a
    `DefaultUnionSQLBuilder` along with an `UnionSQLBuilder`
    interface, which also allows application to implement
    custom behaviour if required. Union SQL keyword and part
    SQL generation is handled through added methods on the
    Platforms to allow adjustment for furture versions if
    needed - or throw a Exception if a Platform does not
    support it anymore.
    
    Example:
    
    ```php
    $platform = $connection->getDatabasePlatform();
    $qb       = $>connection->createQueryBuilder();
    $select10 = $platform->getDummySelectSQL('2 as field_one');
    $select20 = $platform->getDummySelectSQL('1 as field_one');
    $qb->union($select10)
       ->addUnion($select20, UnionType::ALL)
       ->setMaxResults(1)
       ->setFirstResult(1)
       ->orderBy('field_one', 'ASC');
    $rows = $qb->executeQuery()->fetchAllAssociative();
    ```
    
    Unit and functional tests are added to demonstrate the
    implementation and cover it for future changes.
    
    Resolves: doctrine#6368
    sbuerk committed Jun 14, 2024
    Configuration menu
    Copy the full SHA
    bc0fd01 View commit details
    Browse the repository at this point in the history