Page MenuHomePhabricator

A query builder for MediaWiki core
Closed, ResolvedPublic

Description

As part of my review of data access abstractions in MediaWiki core, I'm introducing a query builder module. The idea is to encapsulate the parameters to IDatabase::select(), as seen in the getQueryInfo() pattern, into an object.

The class has chainable mutator methods, similar to those in Doctrine's QueryBuilder. Unlike in Doctrine, to help with migration from existing code, it is necessary to allow callers to directly set the underlying arrays. Certain implementation details are exposed.

The query builder object does not require an IDatabase for its construction, only for its execution. This gives it a similar state to the getQueryInfo() arrays, which eases migration. The disadvantage of this approach is that it is difficult to add an expression builder. Ideally, the arrays passed to IDatabase::select() would be DBMS-independent, including not having any quoted strings in them. For example, instead of IDatabase::buildLike(), which returns a DBMS-dependent string, you would have a LikeExpression value object which would be resolved during the execution of IDatabase::select(). But I think that's a project for another day. For now, the syntax will be $queryBuilder->where( 'user_name' . $db->buildLike(...) ).

The query builder's join methods, which are aimed at new code, require a modern style in which all joins have aliases and join conditions. Doctrine's join interface is very similar. Instead of

$tables[] = 'user';
$join_conds['user'] = [ 'JOIN', 'rev_user=user_id' ];

We would have

$builder->join( 'user', 'user1', 'rev_user=user_id' );

I'm adding structured support for parenthesized join expressions and joining on a subquery.

Doctrine uses the same QueryBuilder class for select, update and delete queries, which I don't think is advisable since the allowed methods are different. I'm adding a newable SelectQueryBuilder; we could later add UpdateQueryBuilder and DeleteQueryBuilder.

Instead of a single execute() function, I'm planning to have wrappers for select(), selectField(), selectFieldValues(), selectRow() and selectSQLText(), potentially called fetchResultSet(), fetchField(), fetchFieldValues(), fetchRow() and getSQL() respectively.

Related Objects

Mentioned In
T357956: Migrate usage of Database::select to SelectQueryBuilder in Genealogy
T354773: Migrate usage of Database::delete to DeleteQueryBuilder in ReadingLists
T354772: Migrate usage of Database::update to UpdateQueryBuilder in ReadingLists
T335377: Create QueryBuilders for common database queries
T316435: Migrate usage of Database::select to SelectQueryBuilder in CheckUser's PreliminaryCheckService
T316434: Migrate usage of Database::select to SelectQueryBuilder in CheckUser's TimelineRowFormatter
T316433: Migrate usage of Database::select to SelectQueryBuilder in CheckUser's CompareService
T312515: Migrate usage of Database::select to SelectQueryBuilder in Wikibase
T312510: Migrate usage of Database::select to SelectQueryBuilder in Wikibase
T312505: Migrate usage of Database::select to SelectQueryBuilder in FileExporter
T312504: Migrate usage of Database::select to SelectQueryBuilder in TocTree
T312503: Migrate usage of Database::select to SelectQueryBuilder in WikiEditor
T312502: Migrate usage of Database::select to SelectQueryBuilder in UploadWizard
T312501: Migrate usage of Database::select to SelectQueryBuilder in PageImages
T312500: Migrate usage of Database::select to SelectQueryBuilder in Kartographer
T312499: Migrate usage of Database::select to SelectQueryBuilder in Calendar
T312498: Migrate usage of Database::select to SelectQueryBuilder in ContentTranslation
T312497: Migrate usage of Database::select to SelectQueryBuilder in MapSources
T312496: Migrate usage of Database::select to SelectQueryBuilder in ExtensionDistributor
T312495: Migrate usage of Database::select to SelectQueryBuilder in TitleBlacklist
T312494: Migrate usage of Database::select to SelectQueryBuilder in ProofreadPage
T312493: Migrate usage of Database::select to SelectQueryBuilder in OpenStackManager
T312492: Migrate usage of Database::select to SelectQueryBuilder in Score
T312491: Migrate usage of Database::select to SelectQueryBuilder in ParserFunctions
T312490: Migrate usage of Database::select to SelectQueryBuilder in WikimediaEvents
T312489: Migrate usage of Database::select to SelectQueryBuilder in SiteMatrix
T312488: Migrate usage of Database::select to SelectQueryBuilder in TrustedXFF
T312487: Migrate usage of Database::select to SelectQueryBuilder in InterwikiSorting
T312486: Migrate usage of Database::select to SelectQueryBuilder in ImageMap
T312485: Migrate usage of Database::select to SelectQueryBuilder in MachineVision
T312484: Migrate usage of Database::select to SelectQueryBuilder in Renameuser
T312483: Migrate usage of Database::select to SelectQueryBuilder in InputBox
T312482: Migrate usage of Database::select to SelectQueryBuilder in ReadingLists
T312481: Migrate usage of Database::select to SelectQueryBuilder in Thanks
T312480: Migrate usage of Database::select to SelectQueryBuilder in PageTriage
T312479: Migrate usage of Database::select to SelectQueryBuilder in CheckUser
T312478: Migrate usage of Database::select to SelectQueryBuilder in FundraiserLandingPage
T312477: Migrate usage of Database::select to SelectQueryBuilder in WikimediaIncubator
T312476: Migrate usage of Database::select to SelectQueryBuilder in OAuth
T312475: Migrate usage of Database::select to SelectQueryBuilder in RelatedArticles
T312474: Migrate usage of Database::select to SelectQueryBuilder in DoubleWiki
T312473: Migrate usage of Database::select to SelectQueryBuilder in NewUserMessage
T312472: Migrate usage of Database::select to SelectQueryBuilder in VisualEditor
T312471: Migrate usage of Database::select to SelectQueryBuilder in SubpageSortkey
T312470: Migrate usage of Database::select to SelectQueryBuilder in WikibaseMediaInfo
T312469: Migrate usage of Database::select to SelectQueryBuilder in SpamBlacklist
T312468: Migrate usage of Database::select to SelectQueryBuilder in Elastica
T312467: Migrate usage of Database::select to SelectQueryBuilder in TimedMediaHandler
T312466: Migrate usage of Database::select to SelectQueryBuilder in DiscussionTools
T312465: Migrate usage of Database::select to SelectQueryBuilder in SubPageList3
T312464: Migrate usage of Database::select to SelectQueryBuilder in ExternalGuidance
T312463: Migrate usage of Database::select to SelectQueryBuilder in GWToolset
T312462: Migrate usage of Database::select to SelectQueryBuilder in ArticlePlaceholder
T312461: Migrate usage of Database::select to SelectQueryBuilder in WikiLove
T312460: Migrate usage of Database::select to SelectQueryBuilder in GuidedTour
T312459: Migrate usage of Database::select to SelectQueryBuilder in Dashiki
T312458: Migrate usage of Database::select to SelectQueryBuilder in AdvancedSearch
T312457: Migrate usage of Database::select to SelectQueryBuilder in RSS
T312456: Migrate usage of Database::select to SelectQueryBuilder in Wikidata.org
T312455: Migrate usage of Database::select to SelectQueryBuilder in PropertySuggester
T312454: Migrate usage of Database::select to SelectQueryBuilder in ORES
T312453: Migrate usage of Database::select to SelectQueryBuilder in VipsScaler
T312452: Migrate usage of Database::select to SelectQueryBuilder in GlobalWatchlist
T312451: Migrate usage of Database::select to SelectQueryBuilder in Collection
T312450: Migrate usage of Database::select to SelectQueryBuilder in MobileFrontend
T312449: Migrate usage of Database::select to SelectQueryBuilder in SecurePoll
T312448: Migrate usage of Database::select to SelectQueryBuilder in PagedTiffHandler
T312447: Migrate usage of Database::select to SelectQueryBuilder in PageAssessments
T312446: Migrate usage of Database::select to SelectQueryBuilder in CodeEditor
T312445: Migrate usage of Database::select to SelectQueryBuilder in ShortUrl
T312444: Migrate usage of Database::select to SelectQueryBuilder in FundraisingTranslateWorkflow
T312443: Migrate usage of Database::select to SelectQueryBuilder in Capiunto
T312442: Migrate usage of Database::select to SelectQueryBuilder in LdapAuthentication
T312441: Migrate usage of Database::select to SelectQueryBuilder in Scribunto
T312440: Migrate usage of Database::select to SelectQueryBuilder in Gadgets
T312439: Migrate usage of Database::select to SelectQueryBuilder in Math
T312438: Migrate usage of Database::select to SelectQueryBuilder in SimilarEditors
T312437: Migrate usage of Database::select to SelectQueryBuilder in CodeMirror
T312436: Migrate usage of Database::select to SelectQueryBuilder in RevisionSlider
T312435: Migrate usage of Database::select to SelectQueryBuilder in MultimediaViewer
T312434: Migrate usage of Database::select to SelectQueryBuilder in Linter
T312433: Migrate usage of Database::select to SelectQueryBuilder in ConfirmEdit
T312432: Migrate usage of Database::select to SelectQueryBuilder in LabeledSectionTransclusion
T312431: Migrate usage of Database::select to SelectQueryBuilder in SandboxLink
T312430: Migrate usage of Database::select to SelectQueryBuilder in LiquidThreads
T312429: Migrate usage of Database::select to SelectQueryBuilder in WikimediaApiPortalOAuth
T312428: Migrate usage of Database::select to SelectQueryBuilder in Listings
T312427: Migrate usage of Database::select to SelectQueryBuilder in Poem
T312426: Migrate usage of Database::select to SelectQueryBuilder in IPInfo
T312425: Migrate usage of Database::select to SelectQueryBuilder in Interwiki
T312424: Migrate usage of Database::select to SelectQueryBuilder in WikimediaEditorTasks
T312423: Migrate usage of Database::select to SelectQueryBuilder in GlobalUserPage
T312422: Migrate usage of Database::select to SelectQueryBuilder in Translate
T312421: Migrate usage of Database::select to SelectQueryBuilder in CirrusSearch
T312420: Migrate usage of Database::select to SelectQueryBuilder in AbuseFilter
T312419: Migrate usage of Database::select to SelectQueryBuilder in OATHAuth
T312418: Migrate usage of Database::select to SelectQueryBuilder in Flow
T312417: Migrate usage of Database::select to SelectQueryBuilder in WebAuthn
T312416: Migrate usage of Database::select to SelectQueryBuilder in Cite
T312415: Migrate usage of Database::select to SelectQueryBuilder in UrlShortener
T312414: Migrate usage of Database::select to SelectQueryBuilder in Graph
T312413: Migrate usage of Database::select to SelectQueryBuilder in Citoid
T312412: Migrate usage of Database::select to SelectQueryBuilder in UploadsLink
T312411: Migrate usage of Database::select to SelectQueryBuilder in TorBlock
T312410: Migrate usage of Database::select to SelectQueryBuilder in FileImporter
T312409: Migrate usage of Database::select to SelectQueryBuilder in Disambiguator
T312408: Migrate usage of Database::select to SelectQueryBuilder in TemplateWizard
T312407: Migrate usage of Database::select to SelectQueryBuilder in GlobalCssJs
T312406: Migrate usage of Database::select to SelectQueryBuilder in 3D
T312405: Migrate usage of Database::select to SelectQueryBuilder in DonationInterface
T312404: Migrate usage of Database::select to SelectQueryBuilder in XAnalytics
T312403: Migrate usage of Database::select to SelectQueryBuilder in PoolCounter
T312402: Migrate usage of Database::select to SelectQueryBuilder in SearchExtraNS
T312401: Migrate usage of Database::select to SelectQueryBuilder in WikibaseCirrusSearch
T312400: Migrate usage of Database::select to SelectQueryBuilder in PdfHandler
T312399: Migrate usage of Database::select to SelectQueryBuilder in LoginNotify
T312398: Migrate usage of Database::select to SelectQueryBuilder in EventStreamConfig
T312397: Migrate usage of Database::select to SelectQueryBuilder in NearbyPages
T312396: Migrate usage of Database::select to SelectQueryBuilder in Insider
T312395: Migrate usage of Database::select to SelectQueryBuilder in CategoryTree
T312394: Migrate usage of Database::select to SelectQueryBuilder in CentralAuth
T312393: Migrate usage of Database::select to SelectQueryBuilder in MassMessage
T312392: Migrate usage of Database::select to SelectQueryBuilder in JsonConfig
T312391: Migrate usage of Database::select to SelectQueryBuilder in WikimediaMessages
T312390: Migrate usage of Database::select to SelectQueryBuilder in GoogleNewsSitemap
T312389: Migrate usage of Database::select to SelectQueryBuilder in GlobalPreferences
T312388: Migrate usage of Database::select to SelectQueryBuilder in GeoData
T312387: Migrate usage of Database::select to SelectQueryBuilder in ElectronPdfService
T312386: Migrate usage of Database::select to SelectQueryBuilder in ArticleCreationWorkflow
T312385: Migrate usage of Database::select to SelectQueryBuilder in WikibaseLexemeCirrusSearch
T312384: Migrate usage of Database::select to SelectQueryBuilder in Josa
T312383: Migrate usage of Database::select to SelectQueryBuilder in GeoCrumbs
T312382: Migrate usage of Database::select to SelectQueryBuilder in ApiFeatureUsage
T312381: Migrate usage of Database::select to SelectQueryBuilder in WikidataPageBanner
T312380: Migrate usage of Database::select to SelectQueryBuilder in GlobalUsage
T312379: Migrate usage of Database::select to SelectQueryBuilder in Cognate
T312378: Migrate usage of Database::select to SelectQueryBuilder in FlaggedRevs
T312377: Migrate usage of Database::select to SelectQueryBuilder in WikiLambda
T312376: Migrate usage of Database::select to SelectQueryBuilder in TemplateSandbox
T312375: Migrate usage of Database::select to SelectQueryBuilder in SecureLinkFixer
T312374: Migrate usage of Database::select to SelectQueryBuilder in FeaturedFeeds
T312373: Migrate usage of Database::select to SelectQueryBuilder in ContactPage
T312372: Migrate usage of Database::select to SelectQueryBuilder in ChessBrowser
T312371: Migrate usage of Database::select to SelectQueryBuilder in MediaModeration
T312370: Migrate usage of Database::select to SelectQueryBuilder in timeline
T312369: Migrate usage of Database::select to SelectQueryBuilder in CentralNotice
T312368: Migrate usage of Database::select to SelectQueryBuilder in TheWikipediaLibrary
T312367: Migrate usage of Database::select to SelectQueryBuilder in Popups
T312366: Migrate usage of Database::select to SelectQueryBuilder in CharInsert
T312365: Migrate usage of Database::select to SelectQueryBuilder in PageViewInfo
T312364: Migrate usage of Database::select to SelectQueryBuilder in Wikistories
T312363: Migrate usage of Database::select to SelectQueryBuilder in WikibaseQualityConstraints
T312362: Migrate usage of Database::select to SelectQueryBuilder in WikimediaBadges
T312361: Migrate usage of Database::select to SelectQueryBuilder in Nuke
T312360: Migrate usage of Database::select to SelectQueryBuilder in StopForumSpam
T312359: [ES-M1] Migrate usage of Database::select to SelectQueryBuilder in EntitySchema
T312358: Migrate usage of Database::select to SelectQueryBuilder in DismissableSiteNotice
T312357: Migrate usage of Database::select to SelectQueryBuilder in CommonsMetadata
T312356: Migrate usage of Database::select to SelectQueryBuilder in BounceHandler
T312355: Migrate usage of Database::select to SelectQueryBuilder in LandingCheck
T312354: Migrate usage of Database::select to SelectQueryBuilder in EventBus
T312353: Migrate usage of Database::select to SelectQueryBuilder in TemplateStyles
T312352: Migrate usage of Database::select to SelectQueryBuilder in ImageSuggestions
T312351: Migrate usage of Database::select to SelectQueryBuilder in CongressLookup
T312350: Migrate usage of Database::select to SelectQueryBuilder in TextExtracts
T312349: Migrate usage of Database::select to SelectQueryBuilder in WikibaseLexeme
T312348: Migrate usage of Database::select to SelectQueryBuilder in TemplateData
T312347: Migrate usage of Database::select to SelectQueryBuilder in AntiSpoof
T312346: Migrate usage of Database::select to SelectQueryBuilder in MediaSearch
T312345: Migrate usage of Database::select to SelectQueryBuilder in CiteThisPage
T312344: Migrate usage of Database::select to SelectQueryBuilder in SyntaxHighlight_GeSHi
T312342: Migrate usage of Database::select to SelectQueryBuilder in Wikisource
T312343: Migrate usage of Database::select to SelectQueryBuilder in wikihiero
T312341: Migrate usage of Database::select to SelectQueryBuilder in MobileApp
T312340: Migrate usage of Database::select to SelectQueryBuilder in UniversalLanguageSelector
T312339: Migrate usage of Database::select to SelectQueryBuilder in intersection
T312338: Migrate usage of Database::select to SelectQueryBuilder in Babel
T312337: Migrate usage of Database::select to SelectQueryBuilder in TranslationNotifications
T312336: Migrate usage of Database::select to SelectQueryBuilder in Campaigns
T312335: Migrate usage of Database::select to SelectQueryBuilder in EventLogging
T312334: Migrate usage of Database::select to SelectQueryBuilder in Quiz
T312333: Migrate usage of Database::select to SelectQueryBuilder in Echo
T312332: Migrate usage of Database::select to SelectQueryBuilder in cldr
T312331: Migrate usage of Database::select to SelectQueryBuilder in TwoColConflict
T312330: Migrate usage of Database::select to SelectQueryBuilder in GrowthExperiments
T312329: Migrate usage of Database::select to SelectQueryBuilder in GlobalBlocking
T312328: Migrate usage of Database::select to SelectQueryBuilder in NavigationTiming
T312327: Migrate usage of Database::select to SelectQueryBuilder in QuickSurveys
T312326: Migrate usage of Database::select to SelectQueryBuilder in CreditsSource
T312325: Migrate usage of Database::select to SelectQueryBuilder in Newsletter
T312324: Migrate usage of Database::select to SelectQueryBuilder in OAuthRateLimiter
T312323: Migrate usage of Database::select to SelectQueryBuilder in BetaFeatures
T312322: Migrate usage of Database::select to SelectQueryBuilder in ApiFeatureUsage
T311866: Migrate Database::select usages to SelectQueryBuilder (in WMF-deployed extensions)
T297633: <Tech Initiative> Improving Databases in MediaWiki
T289046: Use core QueryBuilder for DB queries in Wikibase
T259518: Decide how JOIN tables should be mentioned in IDatabase::select()
Mentioned Here
T190396: Consider splitting the IDatabase interface
T210206: Deprecate raw SQL conditions for IDatabase methods (select, insert, etc.)
rMW9e871e05b739: MessageCache: Add STRAIGHT_JOIN to avoid planner oddness
T203712: Formalize getQueryInfo() usage

Event Timeline

There are a very large number of changes, so older changes are hidden. Show Older Changes

Change 811936 had a related patch set uploaded (by Ladsgroup; author: Amir Sarabadani):

[mediawiki/core@master] rdbms: Add a note encouraging use of SelectQueryBuilder in IDatabase::select

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

Change 811936 merged by jenkins-bot:

[mediawiki/core@master] rdbms: Add a note encouraging use of SelectQueryBuilder in IDatabase::select

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

Change 810856 abandoned by Ladsgroup:

[mediawiki/core@master] rdbms: Soft-deprecate IDatabase::select() and similar methods

Reason:

These methods are now @internal making this patch moot

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