Currently, the only way to issue a UNION query is to call $dbr->unionQueries(), which takes an array of SQL strings (that you hopefully got from $dbr->selectSQLText() or $selectQueryBuilder->getSQL(), rather than writing them by hand), and passing the resulting SQL string into $dbr->query(). If we want to phase out SQL strings in favor of Query objects (compare the end of the T326181 task description), we need some better way to make these queries.
Description
Details
| Subject | Repo | Branch | Lines +/- | |
|---|---|---|---|---|
| EntityUsageTable: Use UnionQueryBuilder | mediawiki/extensions/Wikibase | master | +15 -13 | |
| rdbms: Introduce UnionQueryBuilder | mediawiki/core | master | +148 -41 |
| Status | Subtype | Assigned | Task | ||
|---|---|---|---|---|---|
| Resolved | Ladsgroup | T343098 [epic] Data Persistence Hypothesis WE 3.2.1 | |||
| Resolved | Ladsgroup | T326181 Stop doing regex on SQL in Database | |||
| Open | None | T210206 Deprecate raw SQL conditions for IDatabase methods (select, insert, etc.) | |||
| Resolved | Lucas_Werkmeister_WMDE | T333690 rdbms: Add way to issue UNION queries without raw SQL |
Event Timeline
One option might be a new UnionQueryBuilder class, with very few methods.
$uqb = $dbr->newUnionQueryBuilder(); $uqb->add( $dbr->newSelectQueryBuilder() ->select( 'field1' ) ->from( 'table1' ) ->where( [ 'field2' => 'value' ] ) ); $res = $uqb->fetchResultSet();
It would probably just start out with a method to add a SelectQueryBuilder (maybe it could also accept strings for backwards compatibility?), and fetchResultSet(). Additional fetch*() methods could maybe be added later if needed, but I wouldn’t include them to begin with.
Lucas and I iterated on its design a bit before making this ticket and we came up with the mentioned design. i.e. I support this.
Change 906101 had a related patch set uploaded (by Ladsgroup; author: Amir Sarabadani):
[mediawiki/core@master] rdbms: Introduce UnionQueryBuilder
Change 906603 had a related patch set uploaded (by Lucas Werkmeister (WMDE); author: Lucas Werkmeister (WMDE)):
[mediawiki/extensions/Wikibase@master] EntityUsageTable: Use UnionQueryBuilder
Change 906101 merged by jenkins-bot:
[mediawiki/core@master] rdbms: Introduce UnionQueryBuilder
Change 906603 merged by jenkins-bot:
[mediawiki/extensions/Wikibase@master] EntityUsageTable: Use UnionQueryBuilder