SelectQueryBuilder is a new fluent interface for constructing database
queries, which has been merged to master for release in MediaWiki
1.35. Please consider using it in new code.
SELECT page_id FROM page
WHERE page_namespace=$namespace AND page_title=$title
becomes
$id = $db->newSelectQueryBuilder()
->select( 'page_id' )
->from( 'page' )
->where( [
'page_namespace' => $namespace,
'page_title' => $title,
] )
->fetchField();
As explained on the design task T243051, SelectQueryBuilder was
loosely based on the query builder in Doctrine, but I made an effort
to respect existing MediaWiki conventions, to make migration easy.
SelectQueryBuilder is easy to use for simple cases, but has the most
impact on readability when it is used for complex queries. That's why
I chose to migrate the showIndirectLinks query in
Special:WhatLinksHere as a pilot -- it was one of the gnarliest
queries in core.
SelectQueryBuilder excels at building joins, including parenthesized
(nested) joins and joins on subqueries.
SelectQueryBuilder can be used as a structured alternative to the
"query info" pattern, in which the parameters to Database::select()
are stored in an associative array. It can convert to and from such
arrays. As a pilot of this functionality, I converted ApiQueryBase to
use a SelectQueryBuilder to store accumulated query info.
Check it out!
-- Tim Starling