Page MenuHomePhabricator

rdbms: Add way to issue UNION queries without raw SQL
Closed, ResolvedPublic

Description

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.

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

https://gerrit.wikimedia.org/r/906101

Change 906603 had a related patch set uploaded (by Lucas Werkmeister (WMDE); author: Lucas Werkmeister (WMDE)):

[mediawiki/extensions/Wikibase@master] EntityUsageTable: Use UnionQueryBuilder

https://gerrit.wikimedia.org/r/906603

Change 906101 merged by jenkins-bot:

[mediawiki/core@master] rdbms: Introduce UnionQueryBuilder

https://gerrit.wikimedia.org/r/906101

Change 906603 merged by jenkins-bot:

[mediawiki/extensions/Wikibase@master] EntityUsageTable: Use UnionQueryBuilder

https://gerrit.wikimedia.org/r/906603

Ladsgroup claimed this task.
Ladsgroup removed a project: Patch-For-Review.