Add rudimentary UNION
support to the QueryBuilder
#6369
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Summary
The
UNION
operator is used to combine the result-setof two or more
SELECT
statements, which all databasevendors supports with usual specialities for each.
Still, there is a common shared subset which works for
all of them:
with shared common requirements:
Each
SELECT
must return the same fieldsin number, naming and order.
Each
SELECT
must not haveORDER BY
,expect MySQL allowing it to be used as sub
query expression encapsulated in parentheses.
Taking the shared requirements and working behaviour,
it is possible to provide a generic support to the
QueryBuilder with a minimalistic surface addition now,
and following methods are added:
union(string|QueryBuilder ...$unionParts)
andaddUnion(string|QueryBuilder ...$unionParts)
tocreate a
UNION
query retrieving unique rowsunionAll(string|QueryBuilder ...$unionParts)
andaddUnionAll(string|QueryBuilder ...$unionParts)
tocreate a
UNION ALL
query retrieving eventuallyduplicated rows.
This follows the generic logic as
select(...)
andaddSelect(...)
along with introducing new internalQueryType::UNION_DISTINCT
andQueryType::UNION_ALL
enum cases.
Additional to the consideration to allow SQL strings
and QueryBuilder for
union()
,unionAll()
,addUnion()
and
addUnionAll()
and minimize the direct handlingof miss-configuration to the number of provided parts
and let other issues like the field (order, count, naming)
or not allowed order by handling to the database itself.
With that, vendor specific implementation can be done if
required.
Technically, the SQL build process is dispatched to a
DefaultUnionSQLBuilder
along with anUnionSQLBuilder
interface, which also allows application to implement
custom behaviour if required.
Example:
Unit and functional tests are added to demonstrate the
implementation and cover it for future changes.
Resolves: #6368