Database.php 150 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948
  1. <?php
  2. /**
  3. * @defgroup Database Database
  4. *
  5. * This file deals with database interface functions
  6. * and query specifics/optimisations.
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License along
  19. * with this program; if not, write to the Free Software Foundation, Inc.,
  20. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  21. * http://www.gnu.org/copyleft/gpl.html
  22. *
  23. * @file
  24. * @ingroup Database
  25. */
  26. namespace Wikimedia\Rdbms;
  27. use Psr\Log\LoggerAwareInterface;
  28. use Psr\Log\LoggerInterface;
  29. use Psr\Log\NullLogger;
  30. use Wikimedia\ScopedCallback;
  31. use Wikimedia\Timestamp\ConvertibleTimestamp;
  32. use Wikimedia\AtEase\AtEase;
  33. use BagOStuff;
  34. use HashBagOStuff;
  35. use LogicException;
  36. use InvalidArgumentException;
  37. use UnexpectedValueException;
  38. use Exception;
  39. use RuntimeException;
  40. use Throwable;
  41. /**
  42. * Relational database abstraction object
  43. *
  44. * @ingroup Database
  45. * @since 1.28
  46. */
  47. abstract class Database implements IDatabase, IMaintainableDatabase, LoggerAwareInterface {
  48. /** @var BagOStuff APC cache */
  49. protected $srvCache;
  50. /** @var LoggerInterface */
  51. protected $connLogger;
  52. /** @var LoggerInterface */
  53. protected $queryLogger;
  54. /** @var callable Error logging callback */
  55. protected $errorLogger;
  56. /** @var callable Deprecation logging callback */
  57. protected $deprecationLogger;
  58. /** @var callable|null */
  59. protected $profiler;
  60. /** @var TransactionProfiler */
  61. protected $trxProfiler;
  62. /** @var DatabaseDomain */
  63. protected $currentDomain;
  64. /** @var object|resource|null Database connection */
  65. protected $conn;
  66. /** @var IDatabase|null Lazy handle to the master DB this server replicates from */
  67. private $lazyMasterHandle;
  68. /** @var string Server that this instance is currently connected to */
  69. protected $server;
  70. /** @var string User that this instance is currently connected under the name of */
  71. protected $user;
  72. /** @var string Password used to establish the current connection */
  73. protected $password;
  74. /** @var bool Whether this PHP instance is for a CLI script */
  75. protected $cliMode;
  76. /** @var string Agent name for query profiling */
  77. protected $agent;
  78. /** @var array Parameters used by initConnection() to establish a connection */
  79. protected $connectionParams;
  80. /** @var string[]|int[]|float[] SQL variables values to use for all new connections */
  81. protected $connectionVariables;
  82. /** @var int Row batch size to use for emulated INSERT SELECT queries */
  83. protected $nonNativeInsertSelectBatchSize;
  84. /** @var int Current bit field of class DBO_* constants */
  85. protected $flags;
  86. /** @var array Current LoadBalancer tracking information */
  87. protected $lbInfo = [];
  88. /** @var string Current SQL query delimiter */
  89. protected $delimiter = ';';
  90. /** @var array[] Current map of (table => (dbname, schema, prefix) map) */
  91. protected $tableAliases = [];
  92. /** @var string[] Current map of (index alias => index) */
  93. protected $indexAliases = [];
  94. /** @var array|null Current variables use for schema element placeholders */
  95. protected $schemaVars;
  96. /** @var string|bool|null Stashed value of html_errors INI setting */
  97. private $htmlErrors;
  98. /** @var int[] Prior flags member variable values */
  99. private $priorFlags = [];
  100. /** @var array Map of (name => 1) for locks obtained via lock() */
  101. protected $sessionNamedLocks = [];
  102. /** @var array Map of (table name => 1) for TEMPORARY tables */
  103. protected $sessionTempTables = [];
  104. /** @var string ID of the active transaction or the empty string otherwise */
  105. private $trxShortId = '';
  106. /** @var int Transaction status */
  107. private $trxStatus = self::STATUS_TRX_NONE;
  108. /** @var Exception|null The last error that caused the status to become STATUS_TRX_ERROR */
  109. private $trxStatusCause;
  110. /** @var array|null Error details of the last statement-only rollback */
  111. private $trxStatusIgnoredCause;
  112. /** @var float|null UNIX timestamp at the time of BEGIN for the last transaction */
  113. private $trxTimestamp = null;
  114. /** @var float Replication lag estimate at the time of BEGIN for the last transaction */
  115. private $trxReplicaLag = null;
  116. /** @var string Name of the function that start the last transaction */
  117. private $trxFname = null;
  118. /** @var bool Whether possible write queries were done in the last transaction started */
  119. private $trxDoneWrites = false;
  120. /** @var bool Whether the current transaction was started implicitly due to DBO_TRX */
  121. private $trxAutomatic = false;
  122. /** @var int Counter for atomic savepoint identifiers (reset with each transaction) */
  123. private $trxAtomicCounter = 0;
  124. /** @var array List of (name, unique ID, savepoint ID) for each active atomic section level */
  125. private $trxAtomicLevels = [];
  126. /** @var bool Whether the current transaction was started implicitly by startAtomic() */
  127. private $trxAutomaticAtomic = false;
  128. /** @var string[] Write query callers of the current transaction */
  129. private $trxWriteCallers = [];
  130. /** @var float Seconds spent in write queries for the current transaction */
  131. private $trxWriteDuration = 0.0;
  132. /** @var int Number of write queries for the current transaction */
  133. private $trxWriteQueryCount = 0;
  134. /** @var int Number of rows affected by write queries for the current transaction */
  135. private $trxWriteAffectedRows = 0;
  136. /** @var float Like trxWriteQueryCount but excludes lock-bound, easy to replicate, queries */
  137. private $trxWriteAdjDuration = 0.0;
  138. /** @var int Number of write queries counted in trxWriteAdjDuration */
  139. private $trxWriteAdjQueryCount = 0;
  140. /** @var array[] List of (callable, method name, atomic section id) */
  141. private $trxIdleCallbacks = [];
  142. /** @var array[] List of (callable, method name, atomic section id) */
  143. private $trxPreCommitCallbacks = [];
  144. /** @var array[] List of (callable, method name, atomic section id) */
  145. private $trxEndCallbacks = [];
  146. /** @var array[] List of (callable, method name, atomic section id) */
  147. private $trxSectionCancelCallbacks = [];
  148. /** @var callable[] Map of (name => callable) */
  149. private $trxRecurringCallbacks = [];
  150. /** @var bool Whether to suppress triggering of transaction end callbacks */
  151. private $trxEndCallbacksSuppressed = false;
  152. /** @var integer|null Rows affected by the last query to query() or its CRUD wrappers */
  153. protected $affectedRowCount;
  154. /** @var float UNIX timestamp */
  155. private $lastPing = 0.0;
  156. /** @var string The last SQL query attempted */
  157. private $lastQuery = '';
  158. /** @var float|bool UNIX timestamp of last write query */
  159. private $lastWriteTime = false;
  160. /** @var string|bool */
  161. private $lastPhpError = false;
  162. /** @var float Query rount trip time estimate */
  163. private $lastRoundTripEstimate = 0.0;
  164. /** @var int|null Integer ID of the managing LBFactory instance or null if none */
  165. private $ownerId;
  166. /** @var string Lock granularity is on the level of the entire database */
  167. const ATTR_DB_LEVEL_LOCKING = 'db-level-locking';
  168. /** @var string The SCHEMA keyword refers to a grouping of tables in a database */
  169. const ATTR_SCHEMAS_AS_TABLE_GROUPS = 'supports-schemas';
  170. /** @var int New Database instance will not be connected yet when returned */
  171. const NEW_UNCONNECTED = 0;
  172. /** @var int New Database instance will already be connected when returned */
  173. const NEW_CONNECTED = 1;
  174. /** @var int Transaction is in a error state requiring a full or savepoint rollback */
  175. const STATUS_TRX_ERROR = 1;
  176. /** @var int Transaction is active and in a normal state */
  177. const STATUS_TRX_OK = 2;
  178. /** @var int No transaction is active */
  179. const STATUS_TRX_NONE = 3;
  180. /** @var string Idiom used when a cancelable atomic section started the transaction */
  181. private static $NOT_APPLICABLE = 'n/a';
  182. /** @var string Prefix to the atomic section counter used to make savepoint IDs */
  183. private static $SAVEPOINT_PREFIX = 'wikimedia_rdbms_atomic';
  184. /** @var int Writes to this temporary table do not affect lastDoneWrites() */
  185. private static $TEMP_NORMAL = 1;
  186. /** @var int Writes to this temporary table effect lastDoneWrites() */
  187. private static $TEMP_PSEUDO_PERMANENT = 2;
  188. /** @var int Number of times to re-try an operation in case of deadlock */
  189. private static $DEADLOCK_TRIES = 4;
  190. /** @var int Minimum time to wait before retry, in microseconds */
  191. private static $DEADLOCK_DELAY_MIN = 500000;
  192. /** @var int Maximum time to wait before retry */
  193. private static $DEADLOCK_DELAY_MAX = 1500000;
  194. /** @var int How long before it is worth doing a dummy query to test the connection */
  195. private static $PING_TTL = 1.0;
  196. /** @var string Dummy SQL query */
  197. private static $PING_QUERY = 'SELECT 1 AS ping';
  198. /** @var float Guess of how many seconds it takes to replicate a small insert */
  199. private static $TINY_WRITE_SEC = 0.010;
  200. /** @var float Consider a write slow if it took more than this many seconds */
  201. private static $SLOW_WRITE_SEC = 0.500;
  202. /** @var float Assume an insert of this many rows or less should be fast to replicate */
  203. private static $SMALL_WRITE_ROWS = 100;
  204. /** @var string[] List of DBO_* flags that can be changed after connection */
  205. protected static $MUTABLE_FLAGS = [
  206. 'DBO_DEBUG',
  207. 'DBO_NOBUFFER',
  208. 'DBO_TRX',
  209. 'DBO_DDLMODE',
  210. ];
  211. /** @var int Bit field of all DBO_* flags that can be changed after connection */
  212. protected static $DBO_MUTABLE = (
  213. self::DBO_DEBUG | self::DBO_NOBUFFER | self::DBO_TRX | self::DBO_DDLMODE
  214. );
  215. /**
  216. * @note exceptions for missing libraries/drivers should be thrown in initConnection()
  217. * @param array $params Parameters passed from Database::factory()
  218. */
  219. public function __construct( array $params ) {
  220. $this->connectionParams = [];
  221. foreach ( [ 'host', 'user', 'password', 'dbname', 'schema', 'tablePrefix' ] as $name ) {
  222. $this->connectionParams[$name] = $params[$name];
  223. }
  224. $this->connectionVariables = $params['variables'] ?? [];
  225. $this->cliMode = $params['cliMode'];
  226. $this->agent = $params['agent'];
  227. $this->flags = $params['flags'];
  228. if ( $this->flags & self::DBO_DEFAULT ) {
  229. if ( $this->cliMode ) {
  230. $this->flags &= ~self::DBO_TRX;
  231. } else {
  232. $this->flags |= self::DBO_TRX;
  233. }
  234. }
  235. $this->nonNativeInsertSelectBatchSize = $params['nonNativeInsertSelectBatchSize'] ?? 10000;
  236. $this->srvCache = $params['srvCache'] ?? new HashBagOStuff();
  237. $this->profiler = is_callable( $params['profiler'] ) ? $params['profiler'] : null;
  238. $this->trxProfiler = $params['trxProfiler'];
  239. $this->connLogger = $params['connLogger'];
  240. $this->queryLogger = $params['queryLogger'];
  241. $this->errorLogger = $params['errorLogger'];
  242. $this->deprecationLogger = $params['deprecationLogger'];
  243. // Set initial dummy domain until open() sets the final DB/prefix
  244. $this->currentDomain = new DatabaseDomain(
  245. $params['dbname'] != '' ? $params['dbname'] : null,
  246. $params['schema'] != '' ? $params['schema'] : null,
  247. $params['tablePrefix']
  248. );
  249. $this->ownerId = $params['ownerId'] ?? null;
  250. }
  251. /**
  252. * Initialize the connection to the database over the wire (or to local files)
  253. *
  254. * @throws LogicException
  255. * @throws InvalidArgumentException
  256. * @throws DBConnectionError
  257. * @since 1.31
  258. */
  259. final public function initConnection() {
  260. if ( $this->isOpen() ) {
  261. throw new LogicException( __METHOD__ . ': already connected' );
  262. }
  263. // Establish the connection
  264. $this->doInitConnection();
  265. }
  266. /**
  267. * Actually connect to the database over the wire (or to local files)
  268. *
  269. * @throws DBConnectionError
  270. * @since 1.31
  271. */
  272. protected function doInitConnection() {
  273. $this->open(
  274. $this->connectionParams['host'],
  275. $this->connectionParams['user'],
  276. $this->connectionParams['password'],
  277. $this->connectionParams['dbname'],
  278. $this->connectionParams['schema'],
  279. $this->connectionParams['tablePrefix']
  280. );
  281. }
  282. /**
  283. * Open a new connection to the database (closing any existing one)
  284. *
  285. * @param string|null $server Database server host
  286. * @param string|null $user Database user name
  287. * @param string|null $password Database user password
  288. * @param string|null $dbName Database name
  289. * @param string|null $schema Database schema name
  290. * @param string $tablePrefix Table prefix
  291. * @throws DBConnectionError
  292. */
  293. abstract protected function open( $server, $user, $password, $dbName, $schema, $tablePrefix );
  294. /**
  295. * Construct a Database subclass instance given a database type and parameters
  296. *
  297. * This also connects to the database immediately upon object construction
  298. *
  299. * @param string $type A possible DB type (sqlite, mysql, postgres,...)
  300. * @param array $params Parameter map with keys:
  301. * - host : The hostname of the DB server
  302. * - user : The name of the database user the client operates under
  303. * - password : The password for the database user
  304. * - dbname : The name of the database to use where queries do not specify one.
  305. * The database must exist or an error might be thrown. Setting this to the empty string
  306. * will avoid any such errors and make the handle have no implicit database scope. This is
  307. * useful for queries like SHOW STATUS, CREATE DATABASE, or DROP DATABASE. Note that a
  308. * "database" in Postgres is rougly equivalent to an entire MySQL server. This the domain
  309. * in which user names and such are defined, e.g. users are database-specific in Postgres.
  310. * - schema : The database schema to use (if supported). A "schema" in Postgres is roughly
  311. * equivalent to a "database" in MySQL. Note that MySQL and SQLite do not use schemas.
  312. * - tablePrefix : Optional table prefix that is implicitly added on to all table names
  313. * recognized in queries. This can be used in place of schemas for handle site farms.
  314. * - flags : Optional bit field of DBO_* constants that define connection, protocol,
  315. * buffering, and transaction behavior. It is STRONGLY adviced to leave the DBO_DEFAULT
  316. * flag in place UNLESS this this database simply acts as a key/value store.
  317. * - driver: Optional name of a specific DB client driver. For MySQL, there is only the
  318. * 'mysqli' driver; the old one 'mysql' has been removed.
  319. * - variables: Optional map of session variables to set after connecting. This can be
  320. * used to adjust lock timeouts or encoding modes and the like.
  321. * - connLogger: Optional PSR-3 logger interface instance.
  322. * - queryLogger: Optional PSR-3 logger interface instance.
  323. * - profiler : Optional callback that takes a section name argument and returns
  324. * a ScopedCallback instance that ends the profile section in its destructor.
  325. * These will be called in query(), using a simplified version of the SQL that
  326. * also includes the agent as a SQL comment.
  327. * - trxProfiler: Optional TransactionProfiler instance.
  328. * - errorLogger: Optional callback that takes an Exception and logs it.
  329. * - deprecationLogger: Optional callback that takes a string and logs it.
  330. * - cliMode: Whether to consider the execution context that of a CLI script.
  331. * - agent: Optional name used to identify the end-user in query profiling/logging.
  332. * - srvCache: Optional BagOStuff instance to an APC-style cache.
  333. * - nonNativeInsertSelectBatchSize: Optional batch size for non-native INSERT SELECT.
  334. * - ownerId: Optional integer ID of a LoadBalancer instance that manages this instance.
  335. * @param int $connect One of the class constants (NEW_CONNECTED, NEW_UNCONNECTED) [optional]
  336. * @return Database|null If the database driver or extension cannot be found
  337. * @throws InvalidArgumentException If the database driver or extension cannot be found
  338. * @since 1.18
  339. */
  340. final public static function factory( $type, $params = [], $connect = self::NEW_CONNECTED ) {
  341. $class = self::getClass( $type, $params['driver'] ?? null );
  342. if ( class_exists( $class ) && is_subclass_of( $class, IDatabase::class ) ) {
  343. $params += [
  344. 'host' => null,
  345. 'user' => null,
  346. 'password' => null,
  347. 'dbname' => null,
  348. 'schema' => null,
  349. 'tablePrefix' => '',
  350. 'flags' => 0,
  351. 'variables' => [],
  352. 'cliMode' => ( PHP_SAPI === 'cli' || PHP_SAPI === 'phpdbg' ),
  353. 'agent' => basename( $_SERVER['SCRIPT_NAME'] ) . '@' . gethostname(),
  354. 'ownerId' => null
  355. ];
  356. $normalizedParams = [
  357. // Configuration
  358. 'host' => strlen( $params['host'] ) ? $params['host'] : null,
  359. 'user' => strlen( $params['user'] ) ? $params['user'] : null,
  360. 'password' => is_string( $params['password'] ) ? $params['password'] : null,
  361. 'dbname' => strlen( $params['dbname'] ) ? $params['dbname'] : null,
  362. 'schema' => strlen( $params['schema'] ) ? $params['schema'] : null,
  363. 'tablePrefix' => (string)$params['tablePrefix'],
  364. 'flags' => (int)$params['flags'],
  365. 'variables' => $params['variables'],
  366. 'cliMode' => (bool)$params['cliMode'],
  367. 'agent' => (string)$params['agent'],
  368. // Objects and callbacks
  369. 'srvCache' => $params['srvCache'] ?? new HashBagOStuff(),
  370. 'profiler' => $params['profiler'] ?? null,
  371. 'trxProfiler' => $params['trxProfiler'] ?? new TransactionProfiler(),
  372. 'connLogger' => $params['connLogger'] ?? new NullLogger(),
  373. 'queryLogger' => $params['queryLogger'] ?? new NullLogger(),
  374. 'errorLogger' => $params['errorLogger'] ?? function ( Exception $e ) {
  375. trigger_error( get_class( $e ) . ': ' . $e->getMessage(), E_USER_WARNING );
  376. },
  377. 'deprecationLogger' => $params['deprecationLogger'] ?? function ( $msg ) {
  378. trigger_error( $msg, E_USER_DEPRECATED );
  379. }
  380. ] + $params;
  381. /** @var Database $conn */
  382. $conn = new $class( $normalizedParams );
  383. if ( $connect === self::NEW_CONNECTED ) {
  384. $conn->initConnection();
  385. }
  386. } else {
  387. $conn = null;
  388. }
  389. return $conn;
  390. }
  391. /**
  392. * @param string $dbType A possible DB type (sqlite, mysql, postgres,...)
  393. * @param string|null $driver Optional name of a specific DB client driver
  394. * @return array Map of (Database::ATTR_* constant => value) for all such constants
  395. * @throws InvalidArgumentException
  396. * @since 1.31
  397. */
  398. final public static function attributesFromType( $dbType, $driver = null ) {
  399. static $defaults = [
  400. self::ATTR_DB_LEVEL_LOCKING => false,
  401. self::ATTR_SCHEMAS_AS_TABLE_GROUPS => false
  402. ];
  403. $class = self::getClass( $dbType, $driver );
  404. return call_user_func( [ $class, 'getAttributes' ] ) + $defaults;
  405. }
  406. /**
  407. * @param string $dbType A possible DB type (sqlite, mysql, postgres,...)
  408. * @param string|null $driver Optional name of a specific DB client driver
  409. * @return string Database subclass name to use
  410. * @throws InvalidArgumentException
  411. */
  412. private static function getClass( $dbType, $driver = null ) {
  413. // For database types with built-in support, the below maps type to IDatabase
  414. // implementations. For types with multipe driver implementations (PHP extensions),
  415. // an array can be used, keyed by extension name. In case of an array, the
  416. // optional 'driver' parameter can be used to force a specific driver. Otherwise,
  417. // we auto-detect the first available driver. For types without built-in support,
  418. // an class named "Database<Type>" us used, eg. DatabaseFoo for type 'foo'.
  419. static $builtinTypes = [
  420. 'mysql' => [ 'mysqli' => DatabaseMysqli::class ],
  421. 'sqlite' => DatabaseSqlite::class,
  422. 'postgres' => DatabasePostgres::class,
  423. ];
  424. $dbType = strtolower( $dbType );
  425. $class = false;
  426. if ( isset( $builtinTypes[$dbType] ) ) {
  427. $possibleDrivers = $builtinTypes[$dbType];
  428. if ( is_string( $possibleDrivers ) ) {
  429. $class = $possibleDrivers;
  430. } elseif ( (string)$driver !== '' ) {
  431. if ( !isset( $possibleDrivers[$driver] ) ) {
  432. throw new InvalidArgumentException( __METHOD__ .
  433. " type '$dbType' does not support driver '{$driver}'" );
  434. }
  435. $class = $possibleDrivers[$driver];
  436. } else {
  437. foreach ( $possibleDrivers as $posDriver => $possibleClass ) {
  438. if ( extension_loaded( $posDriver ) ) {
  439. $class = $possibleClass;
  440. break;
  441. }
  442. }
  443. }
  444. } else {
  445. $class = 'Database' . ucfirst( $dbType );
  446. }
  447. if ( $class === false ) {
  448. throw new InvalidArgumentException( __METHOD__ .
  449. " no viable database extension found for type '$dbType'" );
  450. }
  451. return $class;
  452. }
  453. /**
  454. * @return array Map of (Database::ATTR_* constant => value)
  455. * @since 1.31
  456. */
  457. protected static function getAttributes() {
  458. return [];
  459. }
  460. /**
  461. * Set the PSR-3 logger interface to use for query logging. (The logger
  462. * interfaces for connection logging and error logging can be set with the
  463. * constructor.)
  464. *
  465. * @param LoggerInterface $logger
  466. */
  467. public function setLogger( LoggerInterface $logger ) {
  468. $this->queryLogger = $logger;
  469. }
  470. public function getServerInfo() {
  471. return $this->getServerVersion();
  472. }
  473. /**
  474. * Backwards-compatibility no-op method for disabling query buffering
  475. *
  476. * @param null|bool $buffer Whether to buffer queries (ignored)
  477. * @return bool Whether buffering was already enabled (always true)
  478. * @deprecated Since 1.34 Use query batching; this no longer does anything
  479. */
  480. public function bufferResults( $buffer = null ) {
  481. return true;
  482. }
  483. final public function trxLevel() {
  484. return ( $this->trxShortId != '' ) ? 1 : 0;
  485. }
  486. public function trxTimestamp() {
  487. return $this->trxLevel() ? $this->trxTimestamp : null;
  488. }
  489. /**
  490. * @return int One of the STATUS_TRX_* class constants
  491. * @since 1.31
  492. */
  493. public function trxStatus() {
  494. return $this->trxStatus;
  495. }
  496. public function tablePrefix( $prefix = null ) {
  497. $old = $this->currentDomain->getTablePrefix();
  498. if ( $prefix !== null ) {
  499. $this->currentDomain = new DatabaseDomain(
  500. $this->currentDomain->getDatabase(),
  501. $this->currentDomain->getSchema(),
  502. $prefix
  503. );
  504. }
  505. return $old;
  506. }
  507. public function dbSchema( $schema = null ) {
  508. if ( strlen( $schema ) && $this->getDBname() === null ) {
  509. throw new DBUnexpectedError( $this, "Cannot set schema to '$schema'; no database set" );
  510. }
  511. $old = $this->currentDomain->getSchema();
  512. if ( $schema !== null ) {
  513. $this->currentDomain = new DatabaseDomain(
  514. $this->currentDomain->getDatabase(),
  515. // DatabaseDomain uses null for unspecified schemas
  516. strlen( $schema ) ? $schema : null,
  517. $this->currentDomain->getTablePrefix()
  518. );
  519. }
  520. return (string)$old;
  521. }
  522. /**
  523. * @return string Schema to use to qualify relations in queries
  524. */
  525. protected function relationSchemaQualifier() {
  526. return $this->dbSchema();
  527. }
  528. public function getLBInfo( $name = null ) {
  529. if ( is_null( $name ) ) {
  530. return $this->lbInfo;
  531. }
  532. if ( array_key_exists( $name, $this->lbInfo ) ) {
  533. return $this->lbInfo[$name];
  534. }
  535. return null;
  536. }
  537. public function setLBInfo( $nameOrArray, $value = null ) {
  538. if ( is_array( $nameOrArray ) ) {
  539. $this->lbInfo = $nameOrArray;
  540. } elseif ( is_string( $nameOrArray ) ) {
  541. if ( $value !== null ) {
  542. $this->lbInfo[$nameOrArray] = $value;
  543. } else {
  544. unset( $this->lbInfo[$nameOrArray] );
  545. }
  546. } else {
  547. throw new InvalidArgumentException( "Got non-string key" );
  548. }
  549. }
  550. public function setLazyMasterHandle( IDatabase $conn ) {
  551. $this->lazyMasterHandle = $conn;
  552. }
  553. /**
  554. * @return IDatabase|null
  555. * @see setLazyMasterHandle()
  556. * @since 1.27
  557. */
  558. protected function getLazyMasterHandle() {
  559. return $this->lazyMasterHandle;
  560. }
  561. public function implicitOrderby() {
  562. return true;
  563. }
  564. public function lastQuery() {
  565. return $this->lastQuery;
  566. }
  567. public function lastDoneWrites() {
  568. return $this->lastWriteTime ?: false;
  569. }
  570. public function writesPending() {
  571. return $this->trxLevel() && $this->trxDoneWrites;
  572. }
  573. public function writesOrCallbacksPending() {
  574. return $this->trxLevel() && (
  575. $this->trxDoneWrites ||
  576. $this->trxIdleCallbacks ||
  577. $this->trxPreCommitCallbacks ||
  578. $this->trxEndCallbacks ||
  579. $this->trxSectionCancelCallbacks
  580. );
  581. }
  582. public function preCommitCallbacksPending() {
  583. return $this->trxLevel() && $this->trxPreCommitCallbacks;
  584. }
  585. /**
  586. * @return string|null
  587. */
  588. final protected function getTransactionRoundId() {
  589. // If transaction round participation is enabled, see if one is active
  590. if ( $this->getFlag( self::DBO_TRX ) ) {
  591. $id = $this->getLBInfo( 'trxRoundId' );
  592. return is_string( $id ) ? $id : null;
  593. }
  594. return null;
  595. }
  596. public function pendingWriteQueryDuration( $type = self::ESTIMATE_TOTAL ) {
  597. if ( !$this->trxLevel() ) {
  598. return false;
  599. } elseif ( !$this->trxDoneWrites ) {
  600. return 0.0;
  601. }
  602. switch ( $type ) {
  603. case self::ESTIMATE_DB_APPLY:
  604. return $this->pingAndCalculateLastTrxApplyTime();
  605. default: // everything
  606. return $this->trxWriteDuration;
  607. }
  608. }
  609. /**
  610. * @return float Time to apply writes to replicas based on trxWrite* fields
  611. */
  612. private function pingAndCalculateLastTrxApplyTime() {
  613. $this->ping( $rtt );
  614. $rttAdjTotal = $this->trxWriteAdjQueryCount * $rtt;
  615. $applyTime = max( $this->trxWriteAdjDuration - $rttAdjTotal, 0 );
  616. // For omitted queries, make them count as something at least
  617. $omitted = $this->trxWriteQueryCount - $this->trxWriteAdjQueryCount;
  618. $applyTime += self::$TINY_WRITE_SEC * $omitted;
  619. return $applyTime;
  620. }
  621. public function pendingWriteCallers() {
  622. return $this->trxLevel() ? $this->trxWriteCallers : [];
  623. }
  624. public function pendingWriteRowsAffected() {
  625. return $this->trxWriteAffectedRows;
  626. }
  627. /**
  628. * List the methods that have write queries or callbacks for the current transaction
  629. *
  630. * This method should not be used outside of Database/LoadBalancer
  631. *
  632. * @return string[]
  633. * @since 1.32
  634. */
  635. public function pendingWriteAndCallbackCallers() {
  636. $fnames = $this->pendingWriteCallers();
  637. foreach ( [
  638. $this->trxIdleCallbacks,
  639. $this->trxPreCommitCallbacks,
  640. $this->trxEndCallbacks,
  641. $this->trxSectionCancelCallbacks
  642. ] as $callbacks ) {
  643. foreach ( $callbacks as $callback ) {
  644. $fnames[] = $callback[1];
  645. }
  646. }
  647. return $fnames;
  648. }
  649. /**
  650. * @return string
  651. */
  652. private function flatAtomicSectionList() {
  653. return array_reduce( $this->trxAtomicLevels, function ( $accum, $v ) {
  654. return $accum === null ? $v[0] : "$accum, " . $v[0];
  655. } );
  656. }
  657. public function isOpen() {
  658. return (bool)$this->conn;
  659. }
  660. public function setFlag( $flag, $remember = self::REMEMBER_NOTHING ) {
  661. if ( $flag & ~static::$DBO_MUTABLE ) {
  662. throw new DBUnexpectedError(
  663. $this,
  664. "Got $flag (allowed: " . implode( ', ', static::$MUTABLE_FLAGS ) . ')'
  665. );
  666. }
  667. if ( $remember === self::REMEMBER_PRIOR ) {
  668. array_push( $this->priorFlags, $this->flags );
  669. }
  670. $this->flags |= $flag;
  671. }
  672. public function clearFlag( $flag, $remember = self::REMEMBER_NOTHING ) {
  673. if ( $flag & ~static::$DBO_MUTABLE ) {
  674. throw new DBUnexpectedError(
  675. $this,
  676. "Got $flag (allowed: " . implode( ', ', static::$MUTABLE_FLAGS ) . ')'
  677. );
  678. }
  679. if ( $remember === self::REMEMBER_PRIOR ) {
  680. array_push( $this->priorFlags, $this->flags );
  681. }
  682. $this->flags &= ~$flag;
  683. }
  684. public function restoreFlags( $state = self::RESTORE_PRIOR ) {
  685. if ( !$this->priorFlags ) {
  686. return;
  687. }
  688. if ( $state === self::RESTORE_INITIAL ) {
  689. $this->flags = reset( $this->priorFlags );
  690. $this->priorFlags = [];
  691. } else {
  692. $this->flags = array_pop( $this->priorFlags );
  693. }
  694. }
  695. public function getFlag( $flag ) {
  696. return ( ( $this->flags & $flag ) === $flag );
  697. }
  698. public function getDomainID() {
  699. return $this->currentDomain->getId();
  700. }
  701. /**
  702. * Get information about an index into an object
  703. * @param string $table Table name
  704. * @param string $index Index name
  705. * @param string $fname Calling function name
  706. * @return mixed Database-specific index description class or false if the index does not exist
  707. */
  708. abstract function indexInfo( $table, $index, $fname = __METHOD__ );
  709. /**
  710. * Wrapper for addslashes()
  711. *
  712. * @param string $s String to be slashed.
  713. * @return string Slashed string.
  714. */
  715. abstract function strencode( $s );
  716. /**
  717. * Set a custom error handler for logging errors during database connection
  718. */
  719. protected function installErrorHandler() {
  720. $this->lastPhpError = false;
  721. $this->htmlErrors = ini_set( 'html_errors', '0' );
  722. set_error_handler( [ $this, 'connectionErrorLogger' ] );
  723. }
  724. /**
  725. * Restore the previous error handler and return the last PHP error for this DB
  726. *
  727. * @return bool|string
  728. */
  729. protected function restoreErrorHandler() {
  730. restore_error_handler();
  731. if ( $this->htmlErrors !== false ) {
  732. ini_set( 'html_errors', $this->htmlErrors );
  733. }
  734. return $this->getLastPHPError();
  735. }
  736. /**
  737. * @return string|bool Last PHP error for this DB (typically connection errors)
  738. */
  739. protected function getLastPHPError() {
  740. if ( $this->lastPhpError ) {
  741. $error = preg_replace( '!\[<a.*</a>\]!', '', $this->lastPhpError );
  742. $error = preg_replace( '!^.*?:\s?(.*)$!', '$1', $error );
  743. return $error;
  744. }
  745. return false;
  746. }
  747. /**
  748. * Error handler for logging errors during database connection
  749. * This method should not be used outside of Database classes
  750. *
  751. * @param int $errno
  752. * @param string $errstr
  753. */
  754. public function connectionErrorLogger( $errno, $errstr ) {
  755. $this->lastPhpError = $errstr;
  756. }
  757. /**
  758. * Create a log context to pass to PSR-3 logger functions.
  759. *
  760. * @param array $extras Additional data to add to context
  761. * @return array
  762. */
  763. protected function getLogContext( array $extras = [] ) {
  764. return array_merge(
  765. [
  766. 'db_server' => $this->server,
  767. 'db_name' => $this->getDBname(),
  768. 'db_user' => $this->user,
  769. ],
  770. $extras
  771. );
  772. }
  773. final public function close( $fname = __METHOD__, $owner = null ) {
  774. $error = null; // error to throw after disconnecting
  775. $wasOpen = (bool)$this->conn;
  776. // This should mostly do nothing if the connection is already closed
  777. if ( $this->conn ) {
  778. // Roll back any dangling transaction first
  779. if ( $this->trxLevel() ) {
  780. if ( $this->trxAtomicLevels ) {
  781. // Cannot let incomplete atomic sections be committed
  782. $levels = $this->flatAtomicSectionList();
  783. $error = "$fname: atomic sections $levels are still open";
  784. } elseif ( $this->trxAutomatic ) {
  785. // Only the connection manager can commit non-empty DBO_TRX transactions
  786. // (empty ones we can silently roll back)
  787. if ( $this->writesOrCallbacksPending() ) {
  788. $error = "$fname: " .
  789. "expected mass rollback of all peer transactions (DBO_TRX set)";
  790. }
  791. } else {
  792. // Manual transactions should have been committed or rolled
  793. // back, even if empty.
  794. $error = "$fname: transaction is still open (from {$this->trxFname})";
  795. }
  796. if ( $this->trxEndCallbacksSuppressed && $error === null ) {
  797. $error = "$fname: callbacks are suppressed; cannot properly commit";
  798. }
  799. // Rollback the changes and run any callbacks as needed
  800. $this->rollback( __METHOD__, self::FLUSHING_INTERNAL );
  801. }
  802. // Close the actual connection in the binding handle
  803. $closed = $this->closeConnection();
  804. } else {
  805. $closed = true; // already closed; nothing to do
  806. }
  807. $this->conn = null;
  808. // Log or throw any unexpected errors after having disconnected
  809. if ( $error !== null ) {
  810. // T217819, T231443: if this is probably just LoadBalancer trying to recover from
  811. // errors and shutdown, then log any problems and move on since the request has to
  812. // end one way or another. Throwing errors is not very useful at some point.
  813. if ( $this->ownerId !== null && $owner === $this->ownerId ) {
  814. $this->queryLogger->error( $error );
  815. } else {
  816. throw new DBUnexpectedError( $this, $error );
  817. }
  818. }
  819. // Note that various subclasses call close() at the start of open(), which itself is
  820. // called by replaceLostConnection(). In that case, just because onTransactionResolution()
  821. // callbacks are pending does not mean that an exception should be thrown. Rather, they
  822. // will be executed after the reconnection step.
  823. if ( $wasOpen ) {
  824. // Sanity check that no callbacks are dangling
  825. $fnames = $this->pendingWriteAndCallbackCallers();
  826. if ( $fnames ) {
  827. throw new RuntimeException(
  828. "Transaction callbacks are still pending: " . implode( ', ', $fnames )
  829. );
  830. }
  831. }
  832. return $closed;
  833. }
  834. /**
  835. * Make sure there is an open connection handle (alive or not) as a sanity check
  836. *
  837. * This guards against fatal errors to the binding handle not being defined
  838. * in cases where open() was never called or close() was already called
  839. *
  840. * @throws DBUnexpectedError
  841. */
  842. final protected function assertHasConnectionHandle() {
  843. if ( !$this->isOpen() ) {
  844. throw new DBUnexpectedError( $this, "DB connection was already closed" );
  845. }
  846. }
  847. /**
  848. * Make sure that this server is not marked as a replica nor read-only as a sanity check
  849. *
  850. * @throws DBReadOnlyRoleError
  851. * @throws DBReadOnlyError
  852. */
  853. protected function assertIsWritableMaster() {
  854. if ( $this->getLBInfo( 'replica' ) ) {
  855. throw new DBReadOnlyRoleError(
  856. $this,
  857. 'Write operations are not allowed on replica database connections'
  858. );
  859. }
  860. $reason = $this->getReadOnlyReason();
  861. if ( $reason !== false ) {
  862. throw new DBReadOnlyError( $this, "Database is read-only: $reason" );
  863. }
  864. }
  865. /**
  866. * Closes underlying database connection
  867. * @since 1.20
  868. * @return bool Whether connection was closed successfully
  869. */
  870. abstract protected function closeConnection();
  871. /**
  872. * Run a query and return a DBMS-dependent wrapper or boolean
  873. *
  874. * This is meant to handle the basic command of actually sending a query to the
  875. * server via the driver. No implicit transaction, reconnection, nor retry logic
  876. * should happen here. The higher level query() method is designed to handle those
  877. * sorts of concerns. This method should not trigger such higher level methods.
  878. *
  879. * The lastError() and lastErrno() methods should meaningfully reflect what error,
  880. * if any, occured during the last call to this method. Methods like executeQuery(),
  881. * query(), select(), insert(), update(), delete(), and upsert() implement their calls
  882. * to doQuery() such that an immediately subsequent call to lastError()/lastErrno()
  883. * meaningfully reflects any error that occured during that public query method call.
  884. *
  885. * For SELECT queries, this returns either:
  886. * - a) A driver-specific value/resource, only on success. This can be iterated
  887. * over by calling fetchObject()/fetchRow() until there are no more rows.
  888. * Alternatively, the result can be passed to resultObject() to obtain an
  889. * IResultWrapper instance which can then be iterated over via "foreach".
  890. * - b) False, on any query failure
  891. *
  892. * For non-SELECT queries, this returns either:
  893. * - a) A driver-specific value/resource, only on success
  894. * - b) True, only on success (e.g. no meaningful result other than "OK")
  895. * - c) False, on any query failure
  896. *
  897. * @param string $sql SQL query
  898. * @return mixed|bool An object, resource, or true on success; false on failure
  899. */
  900. abstract protected function doQuery( $sql );
  901. /**
  902. * Determine whether a query writes to the DB. When in doubt, this returns true.
  903. *
  904. * Main use cases:
  905. *
  906. * - Subsequent web requests should not need to wait for replication from
  907. * the master position seen by this web request, unless this request made
  908. * changes to the master. This is handled by ChronologyProtector by checking
  909. * doneWrites() at the end of the request. doneWrites() returns true if any
  910. * query set lastWriteTime; which query() does based on isWriteQuery().
  911. *
  912. * - Reject write queries to replica DBs, in query().
  913. *
  914. * @param string $sql
  915. * @return bool
  916. */
  917. protected function isWriteQuery( $sql ) {
  918. // BEGIN and COMMIT queries are considered read queries here.
  919. // Database backends and drivers (MySQL, MariaDB, php-mysqli) generally
  920. // treat these as write queries, in that their results have "affected rows"
  921. // as meta data as from writes, instead of "num rows" as from reads.
  922. // But, we treat them as read queries because when reading data (from
  923. // either replica or master) we use transactions to enable repeatable-read
  924. // snapshots, which ensures we get consistent results from the same snapshot
  925. // for all queries within a request. Use cases:
  926. // - Treating these as writes would trigger ChronologyProtector (see method doc).
  927. // - We use this method to reject writes to replicas, but we need to allow
  928. // use of transactions on replicas for read snapshots. This is fine given
  929. // that transactions by themselves don't make changes, only actual writes
  930. // within the transaction matter, which we still detect.
  931. return !preg_match(
  932. '/^(?:SELECT|BEGIN|ROLLBACK|COMMIT|SAVEPOINT|RELEASE|SET|SHOW|EXPLAIN|USE|\(SELECT)\b/i',
  933. $sql
  934. );
  935. }
  936. /**
  937. * @param string $sql
  938. * @return string|null
  939. */
  940. protected function getQueryVerb( $sql ) {
  941. return preg_match( '/^\s*([a-z]+)/i', $sql, $m ) ? strtoupper( $m[1] ) : null;
  942. }
  943. /**
  944. * Determine whether a SQL statement is sensitive to isolation level.
  945. *
  946. * A SQL statement is considered transactable if its result could vary
  947. * depending on the transaction isolation level. Operational commands
  948. * such as 'SET' and 'SHOW' are not considered to be transactable.
  949. *
  950. * Main purpose: Used by query() to decide whether to begin a transaction
  951. * before the current query (in DBO_TRX mode, on by default).
  952. *
  953. * @param string $sql
  954. * @return bool
  955. */
  956. protected function isTransactableQuery( $sql ) {
  957. return !in_array(
  958. $this->getQueryVerb( $sql ),
  959. [ 'BEGIN', 'ROLLBACK', 'COMMIT', 'SET', 'SHOW', 'CREATE', 'ALTER', 'USE', 'SHOW' ],
  960. true
  961. );
  962. }
  963. /**
  964. * @param string $sql A SQL query
  965. * @param bool $pseudoPermanent Treat any table from CREATE TEMPORARY as pseudo-permanent
  966. * @return array A n-tuple of:
  967. * - int|null: A self::TEMP_* constant for temp table operations or null otherwise
  968. * - string|null: The name of the new temporary table $sql creates, or null
  969. * - string|null: The name of the temporary table that $sql drops, or null
  970. */
  971. protected function getTempWrites( $sql, $pseudoPermanent ) {
  972. static $qt = '[`"\']?(\w+)[`"\']?'; // quoted table
  973. if ( preg_match(
  974. '/^CREATE\s+TEMPORARY\s+TABLE\s+(?:IF\s+NOT\s+EXISTS\s+)?' . $qt . '/i',
  975. $sql,
  976. $matches
  977. ) ) {
  978. $type = $pseudoPermanent ? self::$TEMP_PSEUDO_PERMANENT : self::$TEMP_NORMAL;
  979. return [ $type, $matches[1], null ];
  980. } elseif ( preg_match(
  981. '/^DROP\s+(?:TEMPORARY\s+)?TABLE\s+(?:IF\s+EXISTS\s+)?' . $qt . '/i',
  982. $sql,
  983. $matches
  984. ) ) {
  985. return [ $this->sessionTempTables[$matches[1]] ?? null, null, $matches[1] ];
  986. } elseif ( preg_match(
  987. '/^TRUNCATE\s+(?:TEMPORARY\s+)?TABLE\s+(?:IF\s+EXISTS\s+)?' . $qt . '/i',
  988. $sql,
  989. $matches
  990. ) ) {
  991. return [ $this->sessionTempTables[$matches[1]] ?? null, null, null ];
  992. } elseif ( preg_match(
  993. '/^(?:(?:INSERT|REPLACE)\s+(?:\w+\s+)?INTO|UPDATE|DELETE\s+FROM)\s+' . $qt . '/i',
  994. $sql,
  995. $matches
  996. ) ) {
  997. return [ $this->sessionTempTables[$matches[1]] ?? null, null, null ];
  998. }
  999. return [ null, null, null ];
  1000. }
  1001. /**
  1002. * @param IResultWrapper|bool $ret
  1003. * @param int|null $tmpType TEMP_NORMAL or TEMP_PSEUDO_PERMANENT
  1004. * @param string|null $tmpNew Name of created temp table
  1005. * @param string|null $tmpDel Name of dropped temp table
  1006. */
  1007. protected function registerTempWrites( $ret, $tmpType, $tmpNew, $tmpDel ) {
  1008. if ( $ret !== false ) {
  1009. if ( $tmpNew !== null ) {
  1010. $this->sessionTempTables[$tmpNew] = $tmpType;
  1011. }
  1012. if ( $tmpDel !== null ) {
  1013. unset( $this->sessionTempTables[$tmpDel] );
  1014. }
  1015. }
  1016. }
  1017. public function query( $sql, $fname = __METHOD__, $flags = 0 ) {
  1018. $flags = (int)$flags; // b/c; this field used to be a bool
  1019. // Sanity check that the SQL query is appropriate in the current context and is
  1020. // allowed for an outside caller (e.g. does not break transaction/session tracking).
  1021. $this->assertQueryIsCurrentlyAllowed( $sql, $fname );
  1022. // Send the query to the server and fetch any corresponding errors
  1023. list( $ret, $err, $errno, $unignorable ) = $this->executeQuery( $sql, $fname, $flags );
  1024. if ( $ret === false ) {
  1025. $ignoreErrors = $this->fieldHasBit( $flags, self::QUERY_SILENCE_ERRORS );
  1026. // Throw an error unless both the ignore flag was set and a rollback is not needed
  1027. $this->reportQueryError( $err, $errno, $sql, $fname, $ignoreErrors && !$unignorable );
  1028. }
  1029. return $this->resultObject( $ret );
  1030. }
  1031. /**
  1032. * Execute a query, retrying it if there is a recoverable connection loss
  1033. *
  1034. * This is similar to query() except:
  1035. * - It does not prevent all non-ROLLBACK queries if there is a corrupted transaction
  1036. * - It does not disallow raw queries that are supposed to use dedicated IDatabase methods
  1037. * - It does not throw exceptions for common error cases
  1038. *
  1039. * This is meant for internal use with Database subclasses.
  1040. *
  1041. * @param string $sql Original SQL query
  1042. * @param string $fname Name of the calling function
  1043. * @param int $flags Bit field of class QUERY_* constants
  1044. * @return array An n-tuple of:
  1045. * - mixed|bool: An object, resource, or true on success; false on failure
  1046. * - string: The result of calling lastError()
  1047. * - int: The result of calling lastErrno()
  1048. * - bool: Whether a rollback is needed to allow future non-rollback queries
  1049. * @throws DBUnexpectedError
  1050. */
  1051. final protected function executeQuery( $sql, $fname, $flags ) {
  1052. $this->assertHasConnectionHandle();
  1053. $priorTransaction = $this->trxLevel();
  1054. if ( $this->isWriteQuery( $sql ) ) {
  1055. // In theory, non-persistent writes are allowed in read-only mode, but due to things
  1056. // like https://bugs.mysql.com/bug.php?id=33669 that might not work anyway...
  1057. $this->assertIsWritableMaster();
  1058. // Do not treat temporary table writes as "meaningful writes" since they are only
  1059. // visible to one session and are not permanent. Profile them as reads. Integration
  1060. // tests can override this behavior via $flags.
  1061. $pseudoPermanent = $this->fieldHasBit( $flags, self::QUERY_PSEUDO_PERMANENT );
  1062. list( $tmpType, $tmpNew, $tmpDel ) = $this->getTempWrites( $sql, $pseudoPermanent );
  1063. $isPermWrite = ( $tmpType !== self::$TEMP_NORMAL );
  1064. // DBConnRef uses QUERY_REPLICA_ROLE to enforce the replica role for raw SQL queries
  1065. if ( $isPermWrite && $this->fieldHasBit( $flags, self::QUERY_REPLICA_ROLE ) ) {
  1066. throw new DBReadOnlyRoleError( $this, "Cannot write; target role is DB_REPLICA" );
  1067. }
  1068. } else {
  1069. // No permanent writes in this query
  1070. $isPermWrite = false;
  1071. // No temporary tables written to either
  1072. list( $tmpType, $tmpNew, $tmpDel ) = [ null, null, null ];
  1073. }
  1074. // Add trace comment to the begin of the sql string, right after the operator.
  1075. // Or, for one-word queries (like "BEGIN" or COMMIT") add it to the end (T44598).
  1076. $encAgent = str_replace( '/', '-', $this->agent );
  1077. $commentedSql = preg_replace( '/\s|$/', " /* $fname $encAgent */ ", $sql, 1 );
  1078. // Send the query to the server and fetch any corresponding errors.
  1079. // This also doubles as a "ping" to see if the connection was dropped.
  1080. list( $ret, $err, $errno, $recoverableSR, $recoverableCL, $reconnected ) =
  1081. $this->executeQueryAttempt( $sql, $commentedSql, $isPermWrite, $fname, $flags );
  1082. // Check if the query failed due to a recoverable connection loss
  1083. $allowRetry = !$this->fieldHasBit( $flags, self::QUERY_NO_RETRY );
  1084. if ( $ret === false && $recoverableCL && $reconnected && $allowRetry ) {
  1085. // Silently resend the query to the server since it is safe and possible
  1086. list( $ret, $err, $errno, $recoverableSR, $recoverableCL ) =
  1087. $this->executeQueryAttempt( $sql, $commentedSql, $isPermWrite, $fname, $flags );
  1088. }
  1089. // Register creation and dropping of temporary tables
  1090. $this->registerTempWrites( $ret, $tmpType, $tmpNew, $tmpDel );
  1091. $corruptedTrx = false;
  1092. if ( $ret === false ) {
  1093. if ( $priorTransaction ) {
  1094. if ( $recoverableSR ) {
  1095. # We're ignoring an error that caused just the current query to be aborted.
  1096. # But log the cause so we can log a deprecation notice if a caller actually
  1097. # does ignore it.
  1098. $this->trxStatusIgnoredCause = [ $err, $errno, $fname ];
  1099. } elseif ( !$recoverableCL ) {
  1100. # Either the query was aborted or all queries after BEGIN where aborted.
  1101. # In the first case, the only options going forward are (a) ROLLBACK, or
  1102. # (b) ROLLBACK TO SAVEPOINT (if one was set). If the later case, the only
  1103. # option is ROLLBACK, since the snapshots would have been released.
  1104. $corruptedTrx = true; // cannot recover
  1105. $this->trxStatus = self::STATUS_TRX_ERROR;
  1106. $this->trxStatusCause =
  1107. $this->getQueryExceptionAndLog( $err, $errno, $sql, $fname );
  1108. $this->trxStatusIgnoredCause = null;
  1109. }
  1110. }
  1111. }
  1112. return [ $ret, $err, $errno, $corruptedTrx ];
  1113. }
  1114. /**
  1115. * Wrapper for doQuery() that handles DBO_TRX, profiling, logging, affected row count
  1116. * tracking, and reconnects (without retry) on query failure due to connection loss
  1117. *
  1118. * @param string $sql Original SQL query
  1119. * @param string $commentedSql SQL query with debugging/trace comment
  1120. * @param bool $isPermWrite Whether the query is a (non-temporary table) write
  1121. * @param string $fname Name of the calling function
  1122. * @param int $flags Bit field of class QUERY_* constants
  1123. * @return array An n-tuple of:
  1124. * - mixed|bool: An object, resource, or true on success; false on failure
  1125. * - string: The result of calling lastError()
  1126. * - int: The result of calling lastErrno()
  1127. * - bool: Whether a statement rollback error occured
  1128. * - bool: Whether a disconnect *both* happened *and* was recoverable
  1129. * - bool: Whether a reconnection attempt was *both* made *and* succeeded
  1130. * @throws DBUnexpectedError
  1131. */
  1132. private function executeQueryAttempt( $sql, $commentedSql, $isPermWrite, $fname, $flags ) {
  1133. $priorWritesPending = $this->writesOrCallbacksPending();
  1134. if ( ( $flags & self::QUERY_IGNORE_DBO_TRX ) == 0 ) {
  1135. $this->beginIfImplied( $sql, $fname );
  1136. }
  1137. // Keep track of whether the transaction has write queries pending
  1138. if ( $isPermWrite ) {
  1139. $this->lastWriteTime = microtime( true );
  1140. if ( $this->trxLevel() && !$this->trxDoneWrites ) {
  1141. $this->trxDoneWrites = true;
  1142. $this->trxProfiler->transactionWritingIn(
  1143. $this->server, $this->getDomainID(), $this->trxShortId );
  1144. }
  1145. }
  1146. $prefix = $this->getLBInfo( 'master' ) ? 'query-m: ' : 'query: ';
  1147. $generalizedSql = new GeneralizedSql( $sql, $this->trxShortId, $prefix );
  1148. $startTime = microtime( true );
  1149. $ps = $this->profiler
  1150. ? ( $this->profiler )( $generalizedSql->stringify() )
  1151. : null;
  1152. $this->affectedRowCount = null;
  1153. $this->lastQuery = $sql;
  1154. $ret = $this->doQuery( $commentedSql );
  1155. $lastError = $this->lastError();
  1156. $lastErrno = $this->lastErrno();
  1157. $this->affectedRowCount = $this->affectedRows();
  1158. unset( $ps ); // profile out (if set)
  1159. $queryRuntime = max( microtime( true ) - $startTime, 0.0 );
  1160. $recoverableSR = false; // recoverable statement rollback?
  1161. $recoverableCL = false; // recoverable connection loss?
  1162. $reconnected = false; // reconnection both attempted and succeeded?
  1163. if ( $ret !== false ) {
  1164. $this->lastPing = $startTime;
  1165. if ( $isPermWrite && $this->trxLevel() ) {
  1166. $this->updateTrxWriteQueryTime( $sql, $queryRuntime, $this->affectedRows() );
  1167. $this->trxWriteCallers[] = $fname;
  1168. }
  1169. } elseif ( $this->wasConnectionError( $lastErrno ) ) {
  1170. # Check if no meaningful session state was lost
  1171. $recoverableCL = $this->canRecoverFromDisconnect( $sql, $priorWritesPending );
  1172. # Update session state tracking and try to restore the connection
  1173. $reconnected = $this->replaceLostConnection( __METHOD__ );
  1174. } else {
  1175. # Check if only the last query was rolled back
  1176. $recoverableSR = $this->wasKnownStatementRollbackError();
  1177. }
  1178. if ( $sql === self::$PING_QUERY ) {
  1179. $this->lastRoundTripEstimate = $queryRuntime;
  1180. }
  1181. $this->trxProfiler->recordQueryCompletion(
  1182. $generalizedSql,
  1183. $startTime,
  1184. $isPermWrite,
  1185. $isPermWrite ? $this->affectedRows() : $this->numRows( $ret )
  1186. );
  1187. // Avoid the overhead of logging calls unless debug mode is enabled
  1188. if ( $this->getFlag( self::DBO_DEBUG ) ) {
  1189. $this->queryLogger->debug(
  1190. "{method} [{runtime}s] {db_host}: {sql}",
  1191. [
  1192. 'method' => $fname,
  1193. 'db_host' => $this->getServer(),
  1194. 'sql' => $sql,
  1195. 'domain' => $this->getDomainID(),
  1196. 'runtime' => round( $queryRuntime, 3 )
  1197. ]
  1198. );
  1199. }
  1200. return [ $ret, $lastError, $lastErrno, $recoverableSR, $recoverableCL, $reconnected ];
  1201. }
  1202. /**
  1203. * Start an implicit transaction if DBO_TRX is enabled and no transaction is active
  1204. *
  1205. * @param string $sql
  1206. * @param string $fname
  1207. */
  1208. private function beginIfImplied( $sql, $fname ) {
  1209. if (
  1210. !$this->trxLevel() &&
  1211. $this->getFlag( self::DBO_TRX ) &&
  1212. $this->isTransactableQuery( $sql )
  1213. ) {
  1214. $this->begin( __METHOD__ . " ($fname)", self::TRANSACTION_INTERNAL );
  1215. $this->trxAutomatic = true;
  1216. }
  1217. }
  1218. /**
  1219. * Update the estimated run-time of a query, not counting large row lock times
  1220. *
  1221. * LoadBalancer can be set to rollback transactions that will create huge replication
  1222. * lag. It bases this estimate off of pendingWriteQueryDuration(). Certain simple
  1223. * queries, like inserting a row can take a long time due to row locking. This method
  1224. * uses some simple heuristics to discount those cases.
  1225. *
  1226. * @param string $sql A SQL write query
  1227. * @param float $runtime Total runtime, including RTT
  1228. * @param int $affected Affected row count
  1229. */
  1230. private function updateTrxWriteQueryTime( $sql, $runtime, $affected ) {
  1231. // Whether this is indicative of replica DB runtime (except for RBR or ws_repl)
  1232. $indicativeOfReplicaRuntime = true;
  1233. if ( $runtime > self::$SLOW_WRITE_SEC ) {
  1234. $verb = $this->getQueryVerb( $sql );
  1235. // insert(), upsert(), replace() are fast unless bulky in size or blocked on locks
  1236. if ( $verb === 'INSERT' ) {
  1237. $indicativeOfReplicaRuntime = $this->affectedRows() > self::$SMALL_WRITE_ROWS;
  1238. } elseif ( $verb === 'REPLACE' ) {
  1239. $indicativeOfReplicaRuntime = $this->affectedRows() > self::$SMALL_WRITE_ROWS / 2;
  1240. }
  1241. }
  1242. $this->trxWriteDuration += $runtime;
  1243. $this->trxWriteQueryCount += 1;
  1244. $this->trxWriteAffectedRows += $affected;
  1245. if ( $indicativeOfReplicaRuntime ) {
  1246. $this->trxWriteAdjDuration += $runtime;
  1247. $this->trxWriteAdjQueryCount += 1;
  1248. }
  1249. }
  1250. /**
  1251. * Error out if the DB is not in a valid state for a query via query()
  1252. *
  1253. * @param string $sql
  1254. * @param string $fname
  1255. * @throws DBTransactionStateError
  1256. */
  1257. private function assertQueryIsCurrentlyAllowed( $sql, $fname ) {
  1258. $verb = $this->getQueryVerb( $sql );
  1259. if ( $verb === 'USE' ) {
  1260. throw new DBUnexpectedError( $this, "Got USE query; use selectDomain() instead" );
  1261. }
  1262. if ( $verb === 'ROLLBACK' ) { // transaction/savepoint
  1263. return;
  1264. }
  1265. if ( $this->trxStatus < self::STATUS_TRX_OK ) {
  1266. throw new DBTransactionStateError(
  1267. $this,
  1268. "Cannot execute query from $fname while transaction status is ERROR",
  1269. [],
  1270. $this->trxStatusCause
  1271. );
  1272. } elseif ( $this->trxStatus === self::STATUS_TRX_OK && $this->trxStatusIgnoredCause ) {
  1273. list( $iLastError, $iLastErrno, $iFname ) = $this->trxStatusIgnoredCause;
  1274. call_user_func( $this->deprecationLogger,
  1275. "Caller from $fname ignored an error originally raised from $iFname: " .
  1276. "[$iLastErrno] $iLastError"
  1277. );
  1278. $this->trxStatusIgnoredCause = null;
  1279. }
  1280. }
  1281. public function assertNoOpenTransactions() {
  1282. if ( $this->explicitTrxActive() ) {
  1283. throw new DBTransactionError(
  1284. $this,
  1285. "Explicit transaction still active. A caller may have caught an error. "
  1286. . "Open transactions: " . $this->flatAtomicSectionList()
  1287. );
  1288. }
  1289. }
  1290. /**
  1291. * Determine whether it is safe to retry queries after a database connection is lost
  1292. *
  1293. * @param string $sql SQL query
  1294. * @param bool $priorWritesPending Whether there is a transaction open with
  1295. * possible write queries or transaction pre-commit/idle callbacks
  1296. * waiting on it to finish.
  1297. * @return bool True if it is safe to retry the query, false otherwise
  1298. */
  1299. private function canRecoverFromDisconnect( $sql, $priorWritesPending ) {
  1300. # Transaction dropped; this can mean lost writes, or REPEATABLE-READ snapshots.
  1301. # Dropped connections also mean that named locks are automatically released.
  1302. # Only allow error suppression in autocommit mode or when the lost transaction
  1303. # didn't matter anyway (aside from DBO_TRX snapshot loss).
  1304. if ( $this->sessionNamedLocks ) {
  1305. return false; // possible critical section violation
  1306. } elseif ( $this->sessionTempTables ) {
  1307. return false; // tables might be queried latter
  1308. } elseif ( $sql === 'COMMIT' ) {
  1309. return !$priorWritesPending; // nothing written anyway? (T127428)
  1310. } elseif ( $sql === 'ROLLBACK' ) {
  1311. return true; // transaction lost...which is also what was requested :)
  1312. } elseif ( $this->explicitTrxActive() ) {
  1313. return false; // don't drop atomocity and explicit snapshots
  1314. } elseif ( $priorWritesPending ) {
  1315. return false; // prior writes lost from implicit transaction
  1316. }
  1317. return true;
  1318. }
  1319. /**
  1320. * Clean things up after session (and thus transaction) loss before reconnect
  1321. */
  1322. private function handleSessionLossPreconnect() {
  1323. // Clean up tracking of session-level things...
  1324. // https://dev.mysql.com/doc/refman/5.7/en/implicit-commit.html
  1325. // https://www.postgresql.org/docs/9.2/static/sql-createtable.html (ignoring ON COMMIT)
  1326. $this->sessionTempTables = [];
  1327. // https://dev.mysql.com/doc/refman/5.7/en/miscellaneous-functions.html#function_get-lock
  1328. // https://www.postgresql.org/docs/9.4/static/functions-admin.html#FUNCTIONS-ADVISORY-LOCKS
  1329. $this->sessionNamedLocks = [];
  1330. // Session loss implies transaction loss
  1331. $oldTrxShortId = $this->consumeTrxShortId();
  1332. $this->trxAtomicCounter = 0;
  1333. $this->trxIdleCallbacks = []; // T67263; transaction already lost
  1334. $this->trxPreCommitCallbacks = []; // T67263; transaction already lost
  1335. // Clear additional subclass fields
  1336. $this->doHandleSessionLossPreconnect();
  1337. // @note: leave trxRecurringCallbacks in place
  1338. if ( $this->trxDoneWrites ) {
  1339. $this->trxProfiler->transactionWritingOut(
  1340. $this->server,
  1341. $this->getDomainID(),
  1342. $oldTrxShortId,
  1343. $this->pendingWriteQueryDuration( self::ESTIMATE_TOTAL ),
  1344. $this->trxWriteAffectedRows
  1345. );
  1346. }
  1347. }
  1348. /**
  1349. * Reset any additional subclass trx* and session* fields
  1350. */
  1351. protected function doHandleSessionLossPreconnect() {
  1352. // no-op
  1353. }
  1354. /**
  1355. * Clean things up after session (and thus transaction) loss after reconnect
  1356. */
  1357. private function handleSessionLossPostconnect() {
  1358. try {
  1359. // Handle callbacks in trxEndCallbacks, e.g. onTransactionResolution().
  1360. // If callback suppression is set then the array will remain unhandled.
  1361. $this->runOnTransactionIdleCallbacks( self::TRIGGER_ROLLBACK );
  1362. } catch ( Exception $ex ) {
  1363. // Already logged; move on...
  1364. }
  1365. try {
  1366. // Handle callbacks in trxRecurringCallbacks, e.g. setTransactionListener()
  1367. $this->runTransactionListenerCallbacks( self::TRIGGER_ROLLBACK );
  1368. } catch ( Exception $ex ) {
  1369. // Already logged; move on...
  1370. }
  1371. }
  1372. /**
  1373. * Reset the transaction ID and return the old one
  1374. *
  1375. * @return string The old transaction ID or the empty string if there wasn't one
  1376. */
  1377. private function consumeTrxShortId() {
  1378. $old = $this->trxShortId;
  1379. $this->trxShortId = '';
  1380. return $old;
  1381. }
  1382. /**
  1383. * Checks whether the cause of the error is detected to be a timeout.
  1384. *
  1385. * It returns false by default, and not all engines support detecting this yet.
  1386. * If this returns false, it will be treated as a generic query error.
  1387. *
  1388. * @param string $error Error text
  1389. * @param int $errno Error number
  1390. * @return bool
  1391. */
  1392. protected function wasQueryTimeout( $error, $errno ) {
  1393. return false;
  1394. }
  1395. /**
  1396. * Report a query error. Log the error, and if neither the object ignore
  1397. * flag nor the $ignoreErrors flag is set, throw a DBQueryError.
  1398. *
  1399. * @param string $error
  1400. * @param int $errno
  1401. * @param string $sql
  1402. * @param string $fname
  1403. * @param bool $ignore
  1404. * @throws DBQueryError
  1405. */
  1406. public function reportQueryError( $error, $errno, $sql, $fname, $ignore = false ) {
  1407. if ( $ignore ) {
  1408. $this->queryLogger->debug( "SQL ERROR (ignored): $error" );
  1409. } else {
  1410. throw $this->getQueryExceptionAndLog( $error, $errno, $sql, $fname );
  1411. }
  1412. }
  1413. /**
  1414. * @param string $error
  1415. * @param string|int $errno
  1416. * @param string $sql
  1417. * @param string $fname
  1418. * @return DBError
  1419. */
  1420. private function getQueryExceptionAndLog( $error, $errno, $sql, $fname ) {
  1421. $this->queryLogger->error(
  1422. "{fname}\t{db_server}\t{errno}\t{error}\t{sql1line}",
  1423. $this->getLogContext( [
  1424. 'method' => __METHOD__,
  1425. 'errno' => $errno,
  1426. 'error' => $error,
  1427. 'sql1line' => mb_substr( str_replace( "\n", "\\n", $sql ), 0, 5 * 1024 ),
  1428. 'fname' => $fname,
  1429. 'exception' => new RuntimeException()
  1430. ] )
  1431. );
  1432. if ( $this->wasQueryTimeout( $error, $errno ) ) {
  1433. $e = new DBQueryTimeoutError( $this, $error, $errno, $sql, $fname );
  1434. } elseif ( $this->wasConnectionError( $errno ) ) {
  1435. $e = new DBQueryDisconnectedError( $this, $error, $errno, $sql, $fname );
  1436. } else {
  1437. $e = new DBQueryError( $this, $error, $errno, $sql, $fname );
  1438. }
  1439. return $e;
  1440. }
  1441. /**
  1442. * @param string $error
  1443. * @return DBConnectionError
  1444. */
  1445. final protected function newExceptionAfterConnectError( $error ) {
  1446. // Connection was not fully initialized and is not safe for use
  1447. $this->conn = null;
  1448. $this->connLogger->error(
  1449. "Error connecting to {db_server} as user {db_user}: {error}",
  1450. $this->getLogContext( [
  1451. 'error' => $error,
  1452. 'exception' => new RuntimeException()
  1453. ] )
  1454. );
  1455. return new DBConnectionError( $this, $error );
  1456. }
  1457. public function freeResult( $res ) {
  1458. }
  1459. public function selectField(
  1460. $table, $var, $cond = '', $fname = __METHOD__, $options = [], $join_conds = []
  1461. ) {
  1462. if ( $var === '*' ) { // sanity
  1463. throw new DBUnexpectedError( $this, "Cannot use a * field: got '$var'" );
  1464. }
  1465. if ( !is_array( $options ) ) {
  1466. $options = [ $options ];
  1467. }
  1468. $options['LIMIT'] = 1;
  1469. $res = $this->select( $table, $var, $cond, $fname, $options, $join_conds );
  1470. if ( $res === false ) {
  1471. throw new DBUnexpectedError( $this, "Got false from select()" );
  1472. }
  1473. $row = $this->fetchRow( $res );
  1474. if ( $row === false ) {
  1475. return false;
  1476. }
  1477. return reset( $row );
  1478. }
  1479. public function selectFieldValues(
  1480. $table, $var, $cond = '', $fname = __METHOD__, $options = [], $join_conds = []
  1481. ) {
  1482. if ( $var === '*' ) { // sanity
  1483. throw new DBUnexpectedError( $this, "Cannot use a * field" );
  1484. } elseif ( !is_string( $var ) ) { // sanity
  1485. throw new DBUnexpectedError( $this, "Cannot use an array of fields" );
  1486. }
  1487. if ( !is_array( $options ) ) {
  1488. $options = [ $options ];
  1489. }
  1490. $res = $this->select( $table, [ 'value' => $var ], $cond, $fname, $options, $join_conds );
  1491. if ( $res === false ) {
  1492. throw new DBUnexpectedError( $this, "Got false from select()" );
  1493. }
  1494. $values = [];
  1495. foreach ( $res as $row ) {
  1496. $values[] = $row->value;
  1497. }
  1498. return $values;
  1499. }
  1500. /**
  1501. * Returns an optional USE INDEX clause to go after the table, and a
  1502. * string to go at the end of the query.
  1503. *
  1504. * @see Database::select()
  1505. *
  1506. * @param array $options Associative array of options to be turned into
  1507. * an SQL query, valid keys are listed in the function.
  1508. * @return array
  1509. */
  1510. protected function makeSelectOptions( array $options ) {
  1511. $preLimitTail = $postLimitTail = '';
  1512. $startOpts = '';
  1513. $noKeyOptions = [];
  1514. foreach ( $options as $key => $option ) {
  1515. if ( is_numeric( $key ) ) {
  1516. $noKeyOptions[$option] = true;
  1517. }
  1518. }
  1519. $preLimitTail .= $this->makeGroupByWithHaving( $options );
  1520. $preLimitTail .= $this->makeOrderBy( $options );
  1521. if ( isset( $noKeyOptions['FOR UPDATE'] ) ) {
  1522. $postLimitTail .= ' FOR UPDATE';
  1523. }
  1524. if ( isset( $noKeyOptions['LOCK IN SHARE MODE'] ) ) {
  1525. $postLimitTail .= ' LOCK IN SHARE MODE';
  1526. }
  1527. if ( isset( $noKeyOptions['DISTINCT'] ) || isset( $noKeyOptions['DISTINCTROW'] ) ) {
  1528. $startOpts .= 'DISTINCT';
  1529. }
  1530. # Various MySQL extensions
  1531. if ( isset( $noKeyOptions['STRAIGHT_JOIN'] ) ) {
  1532. $startOpts .= ' /*! STRAIGHT_JOIN */';
  1533. }
  1534. if ( isset( $noKeyOptions['SQL_BIG_RESULT'] ) ) {
  1535. $startOpts .= ' SQL_BIG_RESULT';
  1536. }
  1537. if ( isset( $noKeyOptions['SQL_BUFFER_RESULT'] ) ) {
  1538. $startOpts .= ' SQL_BUFFER_RESULT';
  1539. }
  1540. if ( isset( $noKeyOptions['SQL_SMALL_RESULT'] ) ) {
  1541. $startOpts .= ' SQL_SMALL_RESULT';
  1542. }
  1543. if ( isset( $noKeyOptions['SQL_CALC_FOUND_ROWS'] ) ) {
  1544. $startOpts .= ' SQL_CALC_FOUND_ROWS';
  1545. }
  1546. if ( isset( $options['USE INDEX'] ) && is_string( $options['USE INDEX'] ) ) {
  1547. $useIndex = $this->useIndexClause( $options['USE INDEX'] );
  1548. } else {
  1549. $useIndex = '';
  1550. }
  1551. if ( isset( $options['IGNORE INDEX'] ) && is_string( $options['IGNORE INDEX'] ) ) {
  1552. $ignoreIndex = $this->ignoreIndexClause( $options['IGNORE INDEX'] );
  1553. } else {
  1554. $ignoreIndex = '';
  1555. }
  1556. return [ $startOpts, $useIndex, $preLimitTail, $postLimitTail, $ignoreIndex ];
  1557. }
  1558. /**
  1559. * Returns an optional GROUP BY with an optional HAVING
  1560. *
  1561. * @param array $options Associative array of options
  1562. * @return string
  1563. * @see Database::select()
  1564. * @since 1.21
  1565. */
  1566. protected function makeGroupByWithHaving( $options ) {
  1567. $sql = '';
  1568. if ( isset( $options['GROUP BY'] ) ) {
  1569. $gb = is_array( $options['GROUP BY'] )
  1570. ? implode( ',', $options['GROUP BY'] )
  1571. : $options['GROUP BY'];
  1572. $sql .= ' GROUP BY ' . $gb;
  1573. }
  1574. if ( isset( $options['HAVING'] ) ) {
  1575. $having = is_array( $options['HAVING'] )
  1576. ? $this->makeList( $options['HAVING'], self::LIST_AND )
  1577. : $options['HAVING'];
  1578. $sql .= ' HAVING ' . $having;
  1579. }
  1580. return $sql;
  1581. }
  1582. /**
  1583. * Returns an optional ORDER BY
  1584. *
  1585. * @param array $options Associative array of options
  1586. * @return string
  1587. * @see Database::select()
  1588. * @since 1.21
  1589. */
  1590. protected function makeOrderBy( $options ) {
  1591. if ( isset( $options['ORDER BY'] ) ) {
  1592. $ob = is_array( $options['ORDER BY'] )
  1593. ? implode( ',', $options['ORDER BY'] )
  1594. : $options['ORDER BY'];
  1595. return ' ORDER BY ' . $ob;
  1596. }
  1597. return '';
  1598. }
  1599. public function select(
  1600. $table, $vars, $conds = '', $fname = __METHOD__, $options = [], $join_conds = []
  1601. ) {
  1602. $sql = $this->selectSQLText( $table, $vars, $conds, $fname, $options, $join_conds );
  1603. return $this->query( $sql, $fname );
  1604. }
  1605. public function selectSQLText( $table, $vars, $conds = '', $fname = __METHOD__,
  1606. $options = [], $join_conds = []
  1607. ) {
  1608. if ( is_array( $vars ) ) {
  1609. $fields = implode( ',', $this->fieldNamesWithAlias( $vars ) );
  1610. } else {
  1611. $fields = $vars;
  1612. }
  1613. $options = (array)$options;
  1614. $useIndexes = ( isset( $options['USE INDEX'] ) && is_array( $options['USE INDEX'] ) )
  1615. ? $options['USE INDEX']
  1616. : [];
  1617. $ignoreIndexes = (
  1618. isset( $options['IGNORE INDEX'] ) &&
  1619. is_array( $options['IGNORE INDEX'] )
  1620. )
  1621. ? $options['IGNORE INDEX']
  1622. : [];
  1623. if (
  1624. $this->selectOptionsIncludeLocking( $options ) &&
  1625. $this->selectFieldsOrOptionsAggregate( $vars, $options )
  1626. ) {
  1627. // Some DB types (e.g. postgres) disallow FOR UPDATE with aggregate
  1628. // functions. Discourage use of such queries to encourage compatibility.
  1629. call_user_func(
  1630. $this->deprecationLogger,
  1631. __METHOD__ . ": aggregation used with a locking SELECT ($fname)"
  1632. );
  1633. }
  1634. if ( is_array( $table ) ) {
  1635. $from = ' FROM ' .
  1636. $this->tableNamesWithIndexClauseOrJOIN(
  1637. $table, $useIndexes, $ignoreIndexes, $join_conds );
  1638. } elseif ( $table != '' ) {
  1639. $from = ' FROM ' .
  1640. $this->tableNamesWithIndexClauseOrJOIN(
  1641. [ $table ], $useIndexes, $ignoreIndexes, [] );
  1642. } else {
  1643. $from = '';
  1644. }
  1645. list( $startOpts, $useIndex, $preLimitTail, $postLimitTail, $ignoreIndex ) =
  1646. $this->makeSelectOptions( $options );
  1647. if ( is_array( $conds ) ) {
  1648. $conds = $this->makeList( $conds, self::LIST_AND );
  1649. }
  1650. if ( $conds === null || $conds === false ) {
  1651. $this->queryLogger->warning(
  1652. __METHOD__
  1653. . ' called from '
  1654. . $fname
  1655. . ' with incorrect parameters: $conds must be a string or an array'
  1656. );
  1657. $conds = '';
  1658. }
  1659. if ( $conds === '' || $conds === '*' ) {
  1660. $sql = "SELECT $startOpts $fields $from $useIndex $ignoreIndex $preLimitTail";
  1661. } elseif ( is_string( $conds ) ) {
  1662. $sql = "SELECT $startOpts $fields $from $useIndex $ignoreIndex " .
  1663. "WHERE $conds $preLimitTail";
  1664. } else {
  1665. throw new DBUnexpectedError( $this, __METHOD__ . ' called with incorrect parameters' );
  1666. }
  1667. if ( isset( $options['LIMIT'] ) ) {
  1668. $sql = $this->limitResult( $sql, $options['LIMIT'],
  1669. $options['OFFSET'] ?? false );
  1670. }
  1671. $sql = "$sql $postLimitTail";
  1672. if ( isset( $options['EXPLAIN'] ) ) {
  1673. $sql = 'EXPLAIN ' . $sql;
  1674. }
  1675. return $sql;
  1676. }
  1677. public function selectRow( $table, $vars, $conds, $fname = __METHOD__,
  1678. $options = [], $join_conds = []
  1679. ) {
  1680. $options = (array)$options;
  1681. $options['LIMIT'] = 1;
  1682. $res = $this->select( $table, $vars, $conds, $fname, $options, $join_conds );
  1683. if ( $res === false ) {
  1684. throw new DBUnexpectedError( $this, "Got false from select()" );
  1685. }
  1686. if ( !$this->numRows( $res ) ) {
  1687. return false;
  1688. }
  1689. return $this->fetchObject( $res );
  1690. }
  1691. public function estimateRowCount(
  1692. $table, $var = '*', $conds = '', $fname = __METHOD__, $options = [], $join_conds = []
  1693. ) {
  1694. $conds = $this->normalizeConditions( $conds, $fname );
  1695. $column = $this->extractSingleFieldFromList( $var );
  1696. if ( is_string( $column ) && !in_array( $column, [ '*', '1' ] ) ) {
  1697. $conds[] = "$column IS NOT NULL";
  1698. }
  1699. $res = $this->select(
  1700. $table, [ 'rowcount' => 'COUNT(*)' ], $conds, $fname, $options, $join_conds
  1701. );
  1702. $row = $res ? $this->fetchRow( $res ) : [];
  1703. return isset( $row['rowcount'] ) ? (int)$row['rowcount'] : 0;
  1704. }
  1705. public function selectRowCount(
  1706. $tables, $var = '*', $conds = '', $fname = __METHOD__, $options = [], $join_conds = []
  1707. ) {
  1708. $conds = $this->normalizeConditions( $conds, $fname );
  1709. $column = $this->extractSingleFieldFromList( $var );
  1710. if ( is_string( $column ) && !in_array( $column, [ '*', '1' ] ) ) {
  1711. $conds[] = "$column IS NOT NULL";
  1712. }
  1713. $res = $this->select(
  1714. [
  1715. 'tmp_count' => $this->buildSelectSubquery(
  1716. $tables,
  1717. '1',
  1718. $conds,
  1719. $fname,
  1720. $options,
  1721. $join_conds
  1722. )
  1723. ],
  1724. [ 'rowcount' => 'COUNT(*)' ],
  1725. [],
  1726. $fname
  1727. );
  1728. $row = $res ? $this->fetchRow( $res ) : [];
  1729. return isset( $row['rowcount'] ) ? (int)$row['rowcount'] : 0;
  1730. }
  1731. /**
  1732. * @param string|array $options
  1733. * @return bool
  1734. */
  1735. private function selectOptionsIncludeLocking( $options ) {
  1736. $options = (array)$options;
  1737. foreach ( [ 'FOR UPDATE', 'LOCK IN SHARE MODE' ] as $lock ) {
  1738. if ( in_array( $lock, $options, true ) ) {
  1739. return true;
  1740. }
  1741. }
  1742. return false;
  1743. }
  1744. /**
  1745. * @param array|string $fields
  1746. * @param array|string $options
  1747. * @return bool
  1748. */
  1749. private function selectFieldsOrOptionsAggregate( $fields, $options ) {
  1750. foreach ( (array)$options as $key => $value ) {
  1751. if ( is_string( $key ) ) {
  1752. if ( preg_match( '/^(?:GROUP BY|HAVING)$/i', $key ) ) {
  1753. return true;
  1754. }
  1755. } elseif ( is_string( $value ) ) {
  1756. if ( preg_match( '/^(?:DISTINCT|DISTINCTROW)$/i', $value ) ) {
  1757. return true;
  1758. }
  1759. }
  1760. }
  1761. $regex = '/^(?:COUNT|MIN|MAX|SUM|GROUP_CONCAT|LISTAGG|ARRAY_AGG)\s*\\(/i';
  1762. foreach ( (array)$fields as $field ) {
  1763. if ( is_string( $field ) && preg_match( $regex, $field ) ) {
  1764. return true;
  1765. }
  1766. }
  1767. return false;
  1768. }
  1769. /**
  1770. * @param array|string $conds
  1771. * @param string $fname
  1772. * @return array
  1773. */
  1774. final protected function normalizeConditions( $conds, $fname ) {
  1775. if ( $conds === null || $conds === false ) {
  1776. $this->queryLogger->warning(
  1777. __METHOD__
  1778. . ' called from '
  1779. . $fname
  1780. . ' with incorrect parameters: $conds must be a string or an array'
  1781. );
  1782. $conds = '';
  1783. }
  1784. if ( !is_array( $conds ) ) {
  1785. $conds = ( $conds === '' ) ? [] : [ $conds ];
  1786. }
  1787. return $conds;
  1788. }
  1789. /**
  1790. * @param array|string $var Field parameter in the style of select()
  1791. * @return string|null Column name or null; ignores aliases
  1792. * @throws DBUnexpectedError Errors out if multiple columns are given
  1793. */
  1794. final protected function extractSingleFieldFromList( $var ) {
  1795. if ( is_array( $var ) ) {
  1796. if ( !$var ) {
  1797. $column = null;
  1798. } elseif ( count( $var ) == 1 ) {
  1799. $column = $var[0] ?? reset( $var );
  1800. } else {
  1801. throw new DBUnexpectedError( $this, __METHOD__ . ': got multiple columns' );
  1802. }
  1803. } else {
  1804. $column = $var;
  1805. }
  1806. return $column;
  1807. }
  1808. public function lockForUpdate(
  1809. $table, $conds = '', $fname = __METHOD__, $options = [], $join_conds = []
  1810. ) {
  1811. if ( !$this->trxLevel() && !$this->getFlag( self::DBO_TRX ) ) {
  1812. throw new DBUnexpectedError(
  1813. $this,
  1814. __METHOD__ . ': no transaction is active nor is DBO_TRX set'
  1815. );
  1816. }
  1817. $options = (array)$options;
  1818. $options[] = 'FOR UPDATE';
  1819. return $this->selectRowCount( $table, '*', $conds, $fname, $options, $join_conds );
  1820. }
  1821. public function fieldExists( $table, $field, $fname = __METHOD__ ) {
  1822. $info = $this->fieldInfo( $table, $field );
  1823. return (bool)$info;
  1824. }
  1825. public function indexExists( $table, $index, $fname = __METHOD__ ) {
  1826. if ( !$this->tableExists( $table ) ) {
  1827. return null;
  1828. }
  1829. $info = $this->indexInfo( $table, $index, $fname );
  1830. if ( is_null( $info ) ) {
  1831. return null;
  1832. } else {
  1833. return $info !== false;
  1834. }
  1835. }
  1836. abstract public function tableExists( $table, $fname = __METHOD__ );
  1837. public function indexUnique( $table, $index ) {
  1838. $indexInfo = $this->indexInfo( $table, $index );
  1839. if ( !$indexInfo ) {
  1840. return null;
  1841. }
  1842. return !$indexInfo[0]->Non_unique;
  1843. }
  1844. /**
  1845. * Helper for Database::insert().
  1846. *
  1847. * @param array $options
  1848. * @return string
  1849. */
  1850. protected function makeInsertOptions( $options ) {
  1851. return implode( ' ', $options );
  1852. }
  1853. public function insert( $table, $a, $fname = __METHOD__, $options = [] ) {
  1854. # No rows to insert, easy just return now
  1855. if ( !count( $a ) ) {
  1856. return true;
  1857. }
  1858. $table = $this->tableName( $table );
  1859. if ( !is_array( $options ) ) {
  1860. $options = [ $options ];
  1861. }
  1862. $options = $this->makeInsertOptions( $options );
  1863. if ( isset( $a[0] ) && is_array( $a[0] ) ) {
  1864. $multi = true;
  1865. $keys = array_keys( $a[0] );
  1866. } else {
  1867. $multi = false;
  1868. $keys = array_keys( $a );
  1869. }
  1870. $sql = 'INSERT ' . $options .
  1871. " INTO $table (" . implode( ',', $keys ) . ') VALUES ';
  1872. if ( $multi ) {
  1873. $first = true;
  1874. foreach ( $a as $row ) {
  1875. if ( $first ) {
  1876. $first = false;
  1877. } else {
  1878. $sql .= ',';
  1879. }
  1880. $sql .= '(' . $this->makeList( $row ) . ')';
  1881. }
  1882. } else {
  1883. $sql .= '(' . $this->makeList( $a ) . ')';
  1884. }
  1885. $this->query( $sql, $fname );
  1886. return true;
  1887. }
  1888. /**
  1889. * Make UPDATE options array for Database::makeUpdateOptions
  1890. *
  1891. * @param array $options
  1892. * @return array
  1893. */
  1894. protected function makeUpdateOptionsArray( $options ) {
  1895. if ( !is_array( $options ) ) {
  1896. $options = [ $options ];
  1897. }
  1898. $opts = [];
  1899. if ( in_array( 'IGNORE', $options ) ) {
  1900. $opts[] = 'IGNORE';
  1901. }
  1902. return $opts;
  1903. }
  1904. /**
  1905. * Make UPDATE options for the Database::update function
  1906. *
  1907. * @param array $options The options passed to Database::update
  1908. * @return string
  1909. */
  1910. protected function makeUpdateOptions( $options ) {
  1911. $opts = $this->makeUpdateOptionsArray( $options );
  1912. return implode( ' ', $opts );
  1913. }
  1914. public function update( $table, $values, $conds, $fname = __METHOD__, $options = [] ) {
  1915. $table = $this->tableName( $table );
  1916. $opts = $this->makeUpdateOptions( $options );
  1917. $sql = "UPDATE $opts $table SET " . $this->makeList( $values, self::LIST_SET );
  1918. if ( $conds !== [] && $conds !== '*' ) {
  1919. $sql .= " WHERE " . $this->makeList( $conds, self::LIST_AND );
  1920. }
  1921. $this->query( $sql, $fname );
  1922. return true;
  1923. }
  1924. public function makeList( $a, $mode = self::LIST_COMMA ) {
  1925. if ( !is_array( $a ) ) {
  1926. throw new DBUnexpectedError( $this, __METHOD__ . ' called with incorrect parameters' );
  1927. }
  1928. $first = true;
  1929. $list = '';
  1930. foreach ( $a as $field => $value ) {
  1931. if ( !$first ) {
  1932. if ( $mode == self::LIST_AND ) {
  1933. $list .= ' AND ';
  1934. } elseif ( $mode == self::LIST_OR ) {
  1935. $list .= ' OR ';
  1936. } else {
  1937. $list .= ',';
  1938. }
  1939. } else {
  1940. $first = false;
  1941. }
  1942. if ( ( $mode == self::LIST_AND || $mode == self::LIST_OR ) && is_numeric( $field ) ) {
  1943. $list .= "($value)";
  1944. } elseif ( $mode == self::LIST_SET && is_numeric( $field ) ) {
  1945. $list .= "$value";
  1946. } elseif (
  1947. ( $mode == self::LIST_AND || $mode == self::LIST_OR ) && is_array( $value )
  1948. ) {
  1949. // Remove null from array to be handled separately if found
  1950. $includeNull = false;
  1951. foreach ( array_keys( $value, null, true ) as $nullKey ) {
  1952. $includeNull = true;
  1953. unset( $value[$nullKey] );
  1954. }
  1955. if ( count( $value ) == 0 && !$includeNull ) {
  1956. throw new InvalidArgumentException(
  1957. __METHOD__ . ": empty input for field $field" );
  1958. } elseif ( count( $value ) == 0 ) {
  1959. // only check if $field is null
  1960. $list .= "$field IS NULL";
  1961. } else {
  1962. // IN clause contains at least one valid element
  1963. if ( $includeNull ) {
  1964. // Group subconditions to ensure correct precedence
  1965. $list .= '(';
  1966. }
  1967. if ( count( $value ) == 1 ) {
  1968. // Special-case single values, as IN isn't terribly efficient
  1969. // Don't necessarily assume the single key is 0; we don't
  1970. // enforce linear numeric ordering on other arrays here.
  1971. $value = array_values( $value )[0];
  1972. $list .= $field . " = " . $this->addQuotes( $value );
  1973. } else {
  1974. $list .= $field . " IN (" . $this->makeList( $value ) . ") ";
  1975. }
  1976. // if null present in array, append IS NULL
  1977. if ( $includeNull ) {
  1978. $list .= " OR $field IS NULL)";
  1979. }
  1980. }
  1981. } elseif ( $value === null ) {
  1982. if ( $mode == self::LIST_AND || $mode == self::LIST_OR ) {
  1983. $list .= "$field IS ";
  1984. } elseif ( $mode == self::LIST_SET ) {
  1985. $list .= "$field = ";
  1986. }
  1987. $list .= 'NULL';
  1988. } else {
  1989. if (
  1990. $mode == self::LIST_AND || $mode == self::LIST_OR || $mode == self::LIST_SET
  1991. ) {
  1992. $list .= "$field = ";
  1993. }
  1994. $list .= $mode == self::LIST_NAMES ? $value : $this->addQuotes( $value );
  1995. }
  1996. }
  1997. return $list;
  1998. }
  1999. public function makeWhereFrom2d( $data, $baseKey, $subKey ) {
  2000. $conds = [];
  2001. foreach ( $data as $base => $sub ) {
  2002. if ( count( $sub ) ) {
  2003. $conds[] = $this->makeList(
  2004. [ $baseKey => $base, $subKey => array_keys( $sub ) ],
  2005. self::LIST_AND );
  2006. }
  2007. }
  2008. if ( $conds ) {
  2009. return $this->makeList( $conds, self::LIST_OR );
  2010. } else {
  2011. // Nothing to search for...
  2012. return false;
  2013. }
  2014. }
  2015. public function aggregateValue( $valuedata, $valuename = 'value' ) {
  2016. return $valuename;
  2017. }
  2018. public function bitNot( $field ) {
  2019. return "(~$field)";
  2020. }
  2021. public function bitAnd( $fieldLeft, $fieldRight ) {
  2022. return "($fieldLeft & $fieldRight)";
  2023. }
  2024. public function bitOr( $fieldLeft, $fieldRight ) {
  2025. return "($fieldLeft | $fieldRight)";
  2026. }
  2027. public function buildConcat( $stringList ) {
  2028. return 'CONCAT(' . implode( ',', $stringList ) . ')';
  2029. }
  2030. public function buildGroupConcatField(
  2031. $delim, $table, $field, $conds = '', $join_conds = []
  2032. ) {
  2033. $fld = "GROUP_CONCAT($field SEPARATOR " . $this->addQuotes( $delim ) . ')';
  2034. return '(' . $this->selectSQLText( $table, $fld, $conds, null, [], $join_conds ) . ')';
  2035. }
  2036. public function buildSubstring( $input, $startPosition, $length = null ) {
  2037. $this->assertBuildSubstringParams( $startPosition, $length );
  2038. $functionBody = "$input FROM $startPosition";
  2039. if ( $length !== null ) {
  2040. $functionBody .= " FOR $length";
  2041. }
  2042. return 'SUBSTRING(' . $functionBody . ')';
  2043. }
  2044. /**
  2045. * Check type and bounds for parameters to self::buildSubstring()
  2046. *
  2047. * All supported databases have substring functions that behave the same for
  2048. * positive $startPosition and non-negative $length, but behaviors differ when
  2049. * given 0 or negative $startPosition or negative $length. The simplest
  2050. * solution to that is to just forbid those values.
  2051. *
  2052. * @param int $startPosition
  2053. * @param int|null $length
  2054. * @since 1.31
  2055. */
  2056. protected function assertBuildSubstringParams( $startPosition, $length ) {
  2057. if ( !is_int( $startPosition ) || $startPosition <= 0 ) {
  2058. throw new InvalidArgumentException(
  2059. '$startPosition must be a positive integer'
  2060. );
  2061. }
  2062. if ( !( is_int( $length ) && $length >= 0 || $length === null ) ) {
  2063. throw new InvalidArgumentException(
  2064. '$length must be null or an integer greater than or equal to 0'
  2065. );
  2066. }
  2067. }
  2068. public function buildStringCast( $field ) {
  2069. // In theory this should work for any standards-compliant
  2070. // SQL implementation, although it may not be the best way to do it.
  2071. return "CAST( $field AS CHARACTER )";
  2072. }
  2073. public function buildIntegerCast( $field ) {
  2074. return 'CAST( ' . $field . ' AS INTEGER )';
  2075. }
  2076. public function buildSelectSubquery(
  2077. $table, $vars, $conds = '', $fname = __METHOD__,
  2078. $options = [], $join_conds = []
  2079. ) {
  2080. return new Subquery(
  2081. $this->selectSQLText( $table, $vars, $conds, $fname, $options, $join_conds )
  2082. );
  2083. }
  2084. public function databasesAreIndependent() {
  2085. return false;
  2086. }
  2087. final public function selectDB( $db ) {
  2088. $this->selectDomain( new DatabaseDomain(
  2089. $db,
  2090. $this->currentDomain->getSchema(),
  2091. $this->currentDomain->getTablePrefix()
  2092. ) );
  2093. return true;
  2094. }
  2095. final public function selectDomain( $domain ) {
  2096. $this->doSelectDomain( DatabaseDomain::newFromId( $domain ) );
  2097. }
  2098. /**
  2099. * @param DatabaseDomain $domain
  2100. * @throws DBConnectionError
  2101. * @throws DBError
  2102. * @since 1.32
  2103. */
  2104. protected function doSelectDomain( DatabaseDomain $domain ) {
  2105. $this->currentDomain = $domain;
  2106. }
  2107. public function getDBname() {
  2108. return $this->currentDomain->getDatabase();
  2109. }
  2110. public function getServer() {
  2111. return $this->server;
  2112. }
  2113. public function tableName( $name, $format = 'quoted' ) {
  2114. if ( $name instanceof Subquery ) {
  2115. throw new DBUnexpectedError(
  2116. $this,
  2117. __METHOD__ . ': got Subquery instance when expecting a string'
  2118. );
  2119. }
  2120. # Skip the entire process when we have a string quoted on both ends.
  2121. # Note that we check the end so that we will still quote any use of
  2122. # use of `database`.table. But won't break things if someone wants
  2123. # to query a database table with a dot in the name.
  2124. if ( $this->isQuotedIdentifier( $name ) ) {
  2125. return $name;
  2126. }
  2127. # Lets test for any bits of text that should never show up in a table
  2128. # name. Basically anything like JOIN or ON which are actually part of
  2129. # SQL queries, but may end up inside of the table value to combine
  2130. # sql. Such as how the API is doing.
  2131. # Note that we use a whitespace test rather than a \b test to avoid
  2132. # any remote case where a word like on may be inside of a table name
  2133. # surrounded by symbols which may be considered word breaks.
  2134. if ( preg_match( '/(^|\s)(DISTINCT|JOIN|ON|AS)(\s|$)/i', $name ) !== 0 ) {
  2135. $this->queryLogger->warning(
  2136. __METHOD__ . ": use of subqueries is not supported this way",
  2137. [ 'exception' => new RuntimeException() ]
  2138. );
  2139. return $name;
  2140. }
  2141. # Split database and table into proper variables.
  2142. list( $database, $schema, $prefix, $table ) = $this->qualifiedTableComponents( $name );
  2143. # Quote $table and apply the prefix if not quoted.
  2144. # $tableName might be empty if this is called from Database::replaceVars()
  2145. $tableName = "{$prefix}{$table}";
  2146. if ( $format === 'quoted'
  2147. && !$this->isQuotedIdentifier( $tableName )
  2148. && $tableName !== ''
  2149. ) {
  2150. $tableName = $this->addIdentifierQuotes( $tableName );
  2151. }
  2152. # Quote $schema and $database and merge them with the table name if needed
  2153. $tableName = $this->prependDatabaseOrSchema( $schema, $tableName, $format );
  2154. $tableName = $this->prependDatabaseOrSchema( $database, $tableName, $format );
  2155. return $tableName;
  2156. }
  2157. /**
  2158. * Get the table components needed for a query given the currently selected database
  2159. *
  2160. * @param string $name Table name in the form of db.schema.table, db.table, or table
  2161. * @return array (DB name or "" for default, schema name, table prefix, table name)
  2162. */
  2163. protected function qualifiedTableComponents( $name ) {
  2164. # We reverse the explode so that database.table and table both output the correct table.
  2165. $dbDetails = explode( '.', $name, 3 );
  2166. if ( count( $dbDetails ) == 3 ) {
  2167. list( $database, $schema, $table ) = $dbDetails;
  2168. # We don't want any prefix added in this case
  2169. $prefix = '';
  2170. } elseif ( count( $dbDetails ) == 2 ) {
  2171. list( $database, $table ) = $dbDetails;
  2172. # We don't want any prefix added in this case
  2173. $prefix = '';
  2174. # In dbs that support it, $database may actually be the schema
  2175. # but that doesn't affect any of the functionality here
  2176. $schema = '';
  2177. } else {
  2178. list( $table ) = $dbDetails;
  2179. if ( isset( $this->tableAliases[$table] ) ) {
  2180. $database = $this->tableAliases[$table]['dbname'];
  2181. $schema = is_string( $this->tableAliases[$table]['schema'] )
  2182. ? $this->tableAliases[$table]['schema']
  2183. : $this->relationSchemaQualifier();
  2184. $prefix = is_string( $this->tableAliases[$table]['prefix'] )
  2185. ? $this->tableAliases[$table]['prefix']
  2186. : $this->tablePrefix();
  2187. } else {
  2188. $database = '';
  2189. $schema = $this->relationSchemaQualifier(); # Default schema
  2190. $prefix = $this->tablePrefix(); # Default prefix
  2191. }
  2192. }
  2193. return [ $database, $schema, $prefix, $table ];
  2194. }
  2195. /**
  2196. * @param string|null $namespace Database or schema
  2197. * @param string $relation Name of table, view, sequence, etc...
  2198. * @param string $format One of (raw, quoted)
  2199. * @return string Relation name with quoted and merged $namespace as needed
  2200. */
  2201. private function prependDatabaseOrSchema( $namespace, $relation, $format ) {
  2202. if ( strlen( $namespace ) ) {
  2203. if ( $format === 'quoted' && !$this->isQuotedIdentifier( $namespace ) ) {
  2204. $namespace = $this->addIdentifierQuotes( $namespace );
  2205. }
  2206. $relation = $namespace . '.' . $relation;
  2207. }
  2208. return $relation;
  2209. }
  2210. public function tableNames() {
  2211. $inArray = func_get_args();
  2212. $retVal = [];
  2213. foreach ( $inArray as $name ) {
  2214. $retVal[$name] = $this->tableName( $name );
  2215. }
  2216. return $retVal;
  2217. }
  2218. public function tableNamesN() {
  2219. $inArray = func_get_args();
  2220. $retVal = [];
  2221. foreach ( $inArray as $name ) {
  2222. $retVal[] = $this->tableName( $name );
  2223. }
  2224. return $retVal;
  2225. }
  2226. /**
  2227. * Get an aliased table name
  2228. *
  2229. * This returns strings like "tableName AS newTableName" for aliased tables
  2230. * and "(SELECT * from tableA) newTablename" for subqueries (e.g. derived tables)
  2231. *
  2232. * @see Database::tableName()
  2233. * @param string|Subquery $table Table name or object with a 'sql' field
  2234. * @param string|bool $alias Table alias (optional)
  2235. * @return string SQL name for aliased table. Will not alias a table to its own name
  2236. */
  2237. protected function tableNameWithAlias( $table, $alias = false ) {
  2238. if ( is_string( $table ) ) {
  2239. $quotedTable = $this->tableName( $table );
  2240. } elseif ( $table instanceof Subquery ) {
  2241. $quotedTable = (string)$table;
  2242. } else {
  2243. throw new InvalidArgumentException( "Table must be a string or Subquery" );
  2244. }
  2245. if ( $alias === false || $alias === $table ) {
  2246. if ( $table instanceof Subquery ) {
  2247. throw new InvalidArgumentException( "Subquery table missing alias" );
  2248. }
  2249. return $quotedTable;
  2250. } else {
  2251. return $quotedTable . ' ' . $this->addIdentifierQuotes( $alias );
  2252. }
  2253. }
  2254. /**
  2255. * Gets an array of aliased table names
  2256. *
  2257. * @param array $tables [ [alias] => table ]
  2258. * @return string[] See tableNameWithAlias()
  2259. */
  2260. protected function tableNamesWithAlias( $tables ) {
  2261. $retval = [];
  2262. foreach ( $tables as $alias => $table ) {
  2263. if ( is_numeric( $alias ) ) {
  2264. $alias = $table;
  2265. }
  2266. $retval[] = $this->tableNameWithAlias( $table, $alias );
  2267. }
  2268. return $retval;
  2269. }
  2270. /**
  2271. * Get an aliased field name
  2272. * e.g. fieldName AS newFieldName
  2273. *
  2274. * @param string $name Field name
  2275. * @param string|bool $alias Alias (optional)
  2276. * @return string SQL name for aliased field. Will not alias a field to its own name
  2277. */
  2278. protected function fieldNameWithAlias( $name, $alias = false ) {
  2279. if ( !$alias || (string)$alias === (string)$name ) {
  2280. return $name;
  2281. } else {
  2282. return $name . ' AS ' . $this->addIdentifierQuotes( $alias ); // PostgreSQL needs AS
  2283. }
  2284. }
  2285. /**
  2286. * Gets an array of aliased field names
  2287. *
  2288. * @param array $fields [ [alias] => field ]
  2289. * @return string[] See fieldNameWithAlias()
  2290. */
  2291. protected function fieldNamesWithAlias( $fields ) {
  2292. $retval = [];
  2293. foreach ( $fields as $alias => $field ) {
  2294. if ( is_numeric( $alias ) ) {
  2295. $alias = $field;
  2296. }
  2297. $retval[] = $this->fieldNameWithAlias( $field, $alias );
  2298. }
  2299. return $retval;
  2300. }
  2301. /**
  2302. * Get the aliased table name clause for a FROM clause
  2303. * which might have a JOIN and/or USE INDEX or IGNORE INDEX clause
  2304. *
  2305. * @param array $tables ( [alias] => table )
  2306. * @param array $use_index Same as for select()
  2307. * @param array $ignore_index Same as for select()
  2308. * @param array $join_conds Same as for select()
  2309. * @return string
  2310. */
  2311. protected function tableNamesWithIndexClauseOrJOIN(
  2312. $tables, $use_index = [], $ignore_index = [], $join_conds = []
  2313. ) {
  2314. $ret = [];
  2315. $retJOIN = [];
  2316. $use_index = (array)$use_index;
  2317. $ignore_index = (array)$ignore_index;
  2318. $join_conds = (array)$join_conds;
  2319. foreach ( $tables as $alias => $table ) {
  2320. if ( !is_string( $alias ) ) {
  2321. // No alias? Set it equal to the table name
  2322. $alias = $table;
  2323. }
  2324. if ( is_array( $table ) ) {
  2325. // A parenthesized group
  2326. if ( count( $table ) > 1 ) {
  2327. $joinedTable = '(' .
  2328. $this->tableNamesWithIndexClauseOrJOIN(
  2329. $table, $use_index, $ignore_index, $join_conds ) . ')';
  2330. } else {
  2331. // Degenerate case
  2332. $innerTable = reset( $table );
  2333. $innerAlias = key( $table );
  2334. $joinedTable = $this->tableNameWithAlias(
  2335. $innerTable,
  2336. is_string( $innerAlias ) ? $innerAlias : $innerTable
  2337. );
  2338. }
  2339. } else {
  2340. $joinedTable = $this->tableNameWithAlias( $table, $alias );
  2341. }
  2342. // Is there a JOIN clause for this table?
  2343. if ( isset( $join_conds[$alias] ) ) {
  2344. list( $joinType, $conds ) = $join_conds[$alias];
  2345. $tableClause = $joinType;
  2346. $tableClause .= ' ' . $joinedTable;
  2347. if ( isset( $use_index[$alias] ) ) { // has USE INDEX?
  2348. $use = $this->useIndexClause( implode( ',', (array)$use_index[$alias] ) );
  2349. if ( $use != '' ) {
  2350. $tableClause .= ' ' . $use;
  2351. }
  2352. }
  2353. if ( isset( $ignore_index[$alias] ) ) { // has IGNORE INDEX?
  2354. $ignore = $this->ignoreIndexClause(
  2355. implode( ',', (array)$ignore_index[$alias] ) );
  2356. if ( $ignore != '' ) {
  2357. $tableClause .= ' ' . $ignore;
  2358. }
  2359. }
  2360. $on = $this->makeList( (array)$conds, self::LIST_AND );
  2361. if ( $on != '' ) {
  2362. $tableClause .= ' ON (' . $on . ')';
  2363. }
  2364. $retJOIN[] = $tableClause;
  2365. } elseif ( isset( $use_index[$alias] ) ) {
  2366. // Is there an INDEX clause for this table?
  2367. $tableClause = $joinedTable;
  2368. $tableClause .= ' ' . $this->useIndexClause(
  2369. implode( ',', (array)$use_index[$alias] )
  2370. );
  2371. $ret[] = $tableClause;
  2372. } elseif ( isset( $ignore_index[$alias] ) ) {
  2373. // Is there an INDEX clause for this table?
  2374. $tableClause = $joinedTable;
  2375. $tableClause .= ' ' . $this->ignoreIndexClause(
  2376. implode( ',', (array)$ignore_index[$alias] )
  2377. );
  2378. $ret[] = $tableClause;
  2379. } else {
  2380. $tableClause = $joinedTable;
  2381. $ret[] = $tableClause;
  2382. }
  2383. }
  2384. // We can't separate explicit JOIN clauses with ',', use ' ' for those
  2385. $implicitJoins = implode( ',', $ret );
  2386. $explicitJoins = implode( ' ', $retJOIN );
  2387. // Compile our final table clause
  2388. return implode( ' ', [ $implicitJoins, $explicitJoins ] );
  2389. }
  2390. /**
  2391. * Allows for index remapping in queries where this is not consistent across DBMS
  2392. *
  2393. * @param string $index
  2394. * @return string
  2395. */
  2396. protected function indexName( $index ) {
  2397. return $this->indexAliases[$index] ?? $index;
  2398. }
  2399. public function addQuotes( $s ) {
  2400. if ( $s instanceof Blob ) {
  2401. $s = $s->fetch();
  2402. }
  2403. if ( $s === null ) {
  2404. return 'NULL';
  2405. } elseif ( is_bool( $s ) ) {
  2406. return (int)$s;
  2407. } else {
  2408. # This will also quote numeric values. This should be harmless,
  2409. # and protects against weird problems that occur when they really
  2410. # _are_ strings such as article titles and string->number->string
  2411. # conversion is not 1:1.
  2412. return "'" . $this->strencode( $s ) . "'";
  2413. }
  2414. }
  2415. public function addIdentifierQuotes( $s ) {
  2416. return '"' . str_replace( '"', '""', $s ) . '"';
  2417. }
  2418. /**
  2419. * Returns if the given identifier looks quoted or not according to
  2420. * the database convention for quoting identifiers .
  2421. *
  2422. * @note Do not use this to determine if untrusted input is safe.
  2423. * A malicious user can trick this function.
  2424. * @param string $name
  2425. * @return bool
  2426. */
  2427. public function isQuotedIdentifier( $name ) {
  2428. return $name[0] == '"' && substr( $name, -1, 1 ) == '"';
  2429. }
  2430. /**
  2431. * @param string $s
  2432. * @param string $escapeChar
  2433. * @return string
  2434. */
  2435. protected function escapeLikeInternal( $s, $escapeChar = '`' ) {
  2436. return str_replace( [ $escapeChar, '%', '_' ],
  2437. [ "{$escapeChar}{$escapeChar}", "{$escapeChar}%", "{$escapeChar}_" ],
  2438. $s );
  2439. }
  2440. public function buildLike( $param, ...$params ) {
  2441. if ( is_array( $param ) ) {
  2442. $params = $param;
  2443. } else {
  2444. $params = func_get_args();
  2445. }
  2446. $s = '';
  2447. // We use ` instead of \ as the default LIKE escape character, since addQuotes()
  2448. // may escape backslashes, creating problems of double escaping. The `
  2449. // character has good cross-DBMS compatibility, avoiding special operators
  2450. // in MS SQL like ^ and %
  2451. $escapeChar = '`';
  2452. foreach ( $params as $value ) {
  2453. if ( $value instanceof LikeMatch ) {
  2454. $s .= $value->toString();
  2455. } else {
  2456. $s .= $this->escapeLikeInternal( $value, $escapeChar );
  2457. }
  2458. }
  2459. return ' LIKE ' .
  2460. $this->addQuotes( $s ) . ' ESCAPE ' . $this->addQuotes( $escapeChar ) . ' ';
  2461. }
  2462. public function anyChar() {
  2463. return new LikeMatch( '_' );
  2464. }
  2465. public function anyString() {
  2466. return new LikeMatch( '%' );
  2467. }
  2468. public function nextSequenceValue( $seqName ) {
  2469. return null;
  2470. }
  2471. /**
  2472. * USE INDEX clause. Unlikely to be useful for anything but MySQL. This
  2473. * is only needed because a) MySQL must be as efficient as possible due to
  2474. * its use on Wikipedia, and b) MySQL 4.0 is kind of dumb sometimes about
  2475. * which index to pick. Anyway, other databases might have different
  2476. * indexes on a given table. So don't bother overriding this unless you're
  2477. * MySQL.
  2478. * @param string $index
  2479. * @return string
  2480. */
  2481. public function useIndexClause( $index ) {
  2482. return '';
  2483. }
  2484. /**
  2485. * IGNORE INDEX clause. Unlikely to be useful for anything but MySQL. This
  2486. * is only needed because a) MySQL must be as efficient as possible due to
  2487. * its use on Wikipedia, and b) MySQL 4.0 is kind of dumb sometimes about
  2488. * which index to pick. Anyway, other databases might have different
  2489. * indexes on a given table. So don't bother overriding this unless you're
  2490. * MySQL.
  2491. * @param string $index
  2492. * @return string
  2493. */
  2494. public function ignoreIndexClause( $index ) {
  2495. return '';
  2496. }
  2497. public function replace( $table, $uniqueIndexes, $rows, $fname = __METHOD__ ) {
  2498. if ( count( $rows ) == 0 ) {
  2499. return;
  2500. }
  2501. $uniqueIndexes = (array)$uniqueIndexes;
  2502. // Single row case
  2503. if ( !is_array( reset( $rows ) ) ) {
  2504. $rows = [ $rows ];
  2505. }
  2506. try {
  2507. $this->startAtomic( $fname, self::ATOMIC_CANCELABLE );
  2508. $affectedRowCount = 0;
  2509. foreach ( $rows as $row ) {
  2510. // Delete rows which collide with this one
  2511. $indexWhereClauses = [];
  2512. foreach ( $uniqueIndexes as $index ) {
  2513. $indexColumns = (array)$index;
  2514. $indexRowValues = array_intersect_key( $row, array_flip( $indexColumns ) );
  2515. if ( count( $indexRowValues ) != count( $indexColumns ) ) {
  2516. throw new DBUnexpectedError(
  2517. $this,
  2518. 'New record does not provide all values for unique key (' .
  2519. implode( ', ', $indexColumns ) . ')'
  2520. );
  2521. } elseif ( in_array( null, $indexRowValues, true ) ) {
  2522. throw new DBUnexpectedError(
  2523. $this,
  2524. 'New record has a null value for unique key (' .
  2525. implode( ', ', $indexColumns ) . ')'
  2526. );
  2527. }
  2528. $indexWhereClauses[] = $this->makeList( $indexRowValues, LIST_AND );
  2529. }
  2530. if ( $indexWhereClauses ) {
  2531. $this->delete( $table, $this->makeList( $indexWhereClauses, LIST_OR ), $fname );
  2532. $affectedRowCount += $this->affectedRows();
  2533. }
  2534. // Now insert the row
  2535. $this->insert( $table, $row, $fname );
  2536. $affectedRowCount += $this->affectedRows();
  2537. }
  2538. $this->endAtomic( $fname );
  2539. $this->affectedRowCount = $affectedRowCount;
  2540. } catch ( Exception $e ) {
  2541. $this->cancelAtomic( $fname );
  2542. throw $e;
  2543. }
  2544. }
  2545. /**
  2546. * REPLACE query wrapper for MySQL and SQLite, which have a native REPLACE
  2547. * statement.
  2548. *
  2549. * @param string $table Table name
  2550. * @param array|string $rows Row(s) to insert
  2551. * @param string $fname Caller function name
  2552. */
  2553. protected function nativeReplace( $table, $rows, $fname ) {
  2554. $table = $this->tableName( $table );
  2555. # Single row case
  2556. if ( !is_array( reset( $rows ) ) ) {
  2557. $rows = [ $rows ];
  2558. }
  2559. $sql = "REPLACE INTO $table (" . implode( ',', array_keys( $rows[0] ) ) . ') VALUES ';
  2560. $first = true;
  2561. foreach ( $rows as $row ) {
  2562. if ( $first ) {
  2563. $first = false;
  2564. } else {
  2565. $sql .= ',';
  2566. }
  2567. $sql .= '(' . $this->makeList( $row ) . ')';
  2568. }
  2569. $this->query( $sql, $fname );
  2570. }
  2571. public function upsert( $table, array $rows, $uniqueIndexes, array $set,
  2572. $fname = __METHOD__
  2573. ) {
  2574. if ( $rows === [] ) {
  2575. return true; // nothing to do
  2576. }
  2577. $uniqueIndexes = (array)$uniqueIndexes;
  2578. if ( !is_array( reset( $rows ) ) ) {
  2579. $rows = [ $rows ];
  2580. }
  2581. if ( count( $uniqueIndexes ) ) {
  2582. $clauses = []; // list WHERE clauses that each identify a single row
  2583. foreach ( $rows as $row ) {
  2584. foreach ( $uniqueIndexes as $index ) {
  2585. $index = is_array( $index ) ? $index : [ $index ]; // columns
  2586. $rowKey = []; // unique key to this row
  2587. foreach ( $index as $column ) {
  2588. $rowKey[$column] = $row[$column];
  2589. }
  2590. $clauses[] = $this->makeList( $rowKey, self::LIST_AND );
  2591. }
  2592. }
  2593. $where = [ $this->makeList( $clauses, self::LIST_OR ) ];
  2594. } else {
  2595. $where = false;
  2596. }
  2597. $affectedRowCount = 0;
  2598. try {
  2599. $this->startAtomic( $fname, self::ATOMIC_CANCELABLE );
  2600. # Update any existing conflicting row(s)
  2601. if ( $where !== false ) {
  2602. $this->update( $table, $set, $where, $fname );
  2603. $affectedRowCount += $this->affectedRows();
  2604. }
  2605. # Now insert any non-conflicting row(s)
  2606. $this->insert( $table, $rows, $fname, [ 'IGNORE' ] );
  2607. $affectedRowCount += $this->affectedRows();
  2608. $this->endAtomic( $fname );
  2609. $this->affectedRowCount = $affectedRowCount;
  2610. } catch ( Exception $e ) {
  2611. $this->cancelAtomic( $fname );
  2612. throw $e;
  2613. }
  2614. return true;
  2615. }
  2616. public function deleteJoin( $delTable, $joinTable, $delVar, $joinVar, $conds,
  2617. $fname = __METHOD__
  2618. ) {
  2619. if ( !$conds ) {
  2620. throw new DBUnexpectedError( $this, __METHOD__ . ' called with empty $conds' );
  2621. }
  2622. $delTable = $this->tableName( $delTable );
  2623. $joinTable = $this->tableName( $joinTable );
  2624. $sql = "DELETE FROM $delTable WHERE $delVar IN (SELECT $joinVar FROM $joinTable ";
  2625. if ( $conds != '*' ) {
  2626. $sql .= 'WHERE ' . $this->makeList( $conds, self::LIST_AND );
  2627. }
  2628. $sql .= ')';
  2629. $this->query( $sql, $fname );
  2630. }
  2631. public function textFieldSize( $table, $field ) {
  2632. $table = $this->tableName( $table );
  2633. $sql = "SHOW COLUMNS FROM $table LIKE \"$field\"";
  2634. $res = $this->query( $sql, __METHOD__ );
  2635. $row = $this->fetchObject( $res );
  2636. $m = [];
  2637. if ( preg_match( '/\((.*)\)/', $row->Type, $m ) ) {
  2638. $size = $m[1];
  2639. } else {
  2640. $size = -1;
  2641. }
  2642. return $size;
  2643. }
  2644. public function delete( $table, $conds, $fname = __METHOD__ ) {
  2645. if ( !$conds ) {
  2646. throw new DBUnexpectedError( $this, __METHOD__ . ' called with no conditions' );
  2647. }
  2648. $table = $this->tableName( $table );
  2649. $sql = "DELETE FROM $table";
  2650. if ( $conds != '*' ) {
  2651. if ( is_array( $conds ) ) {
  2652. $conds = $this->makeList( $conds, self::LIST_AND );
  2653. }
  2654. $sql .= ' WHERE ' . $conds;
  2655. }
  2656. $this->query( $sql, $fname );
  2657. return true;
  2658. }
  2659. final public function insertSelect(
  2660. $destTable, $srcTable, $varMap, $conds,
  2661. $fname = __METHOD__, $insertOptions = [], $selectOptions = [], $selectJoinConds = []
  2662. ) {
  2663. static $hints = [ 'NO_AUTO_COLUMNS' ];
  2664. $insertOptions = (array)$insertOptions;
  2665. $selectOptions = (array)$selectOptions;
  2666. if ( $this->cliMode && $this->isInsertSelectSafe( $insertOptions, $selectOptions ) ) {
  2667. // For massive migrations with downtime, we don't want to select everything
  2668. // into memory and OOM, so do all this native on the server side if possible.
  2669. $this->nativeInsertSelect(
  2670. $destTable,
  2671. $srcTable,
  2672. $varMap,
  2673. $conds,
  2674. $fname,
  2675. array_diff( $insertOptions, $hints ),
  2676. $selectOptions,
  2677. $selectJoinConds
  2678. );
  2679. } else {
  2680. $this->nonNativeInsertSelect(
  2681. $destTable,
  2682. $srcTable,
  2683. $varMap,
  2684. $conds,
  2685. $fname,
  2686. array_diff( $insertOptions, $hints ),
  2687. $selectOptions,
  2688. $selectJoinConds
  2689. );
  2690. }
  2691. return true;
  2692. }
  2693. /**
  2694. * @param array $insertOptions INSERT options
  2695. * @param array $selectOptions SELECT options
  2696. * @return bool Whether an INSERT SELECT with these options will be replication safe
  2697. * @since 1.31
  2698. */
  2699. protected function isInsertSelectSafe( array $insertOptions, array $selectOptions ) {
  2700. return true;
  2701. }
  2702. /**
  2703. * Implementation of insertSelect() based on select() and insert()
  2704. *
  2705. * @see IDatabase::insertSelect()
  2706. * @since 1.30
  2707. * @param string $destTable
  2708. * @param string|array $srcTable
  2709. * @param array $varMap
  2710. * @param array $conds
  2711. * @param string $fname
  2712. * @param array $insertOptions
  2713. * @param array $selectOptions
  2714. * @param array $selectJoinConds
  2715. */
  2716. protected function nonNativeInsertSelect( $destTable, $srcTable, $varMap, $conds,
  2717. $fname = __METHOD__,
  2718. $insertOptions = [], $selectOptions = [], $selectJoinConds = []
  2719. ) {
  2720. // For web requests, do a locking SELECT and then INSERT. This puts the SELECT burden
  2721. // on only the master (without needing row-based-replication). It also makes it easy to
  2722. // know how big the INSERT is going to be.
  2723. $fields = [];
  2724. foreach ( $varMap as $dstColumn => $sourceColumnOrSql ) {
  2725. $fields[] = $this->fieldNameWithAlias( $sourceColumnOrSql, $dstColumn );
  2726. }
  2727. $selectOptions[] = 'FOR UPDATE';
  2728. $res = $this->select(
  2729. $srcTable, implode( ',', $fields ), $conds, $fname, $selectOptions, $selectJoinConds
  2730. );
  2731. if ( !$res ) {
  2732. return;
  2733. }
  2734. try {
  2735. $affectedRowCount = 0;
  2736. $this->startAtomic( $fname, self::ATOMIC_CANCELABLE );
  2737. $rows = [];
  2738. $ok = true;
  2739. foreach ( $res as $row ) {
  2740. $rows[] = (array)$row;
  2741. // Avoid inserts that are too huge
  2742. if ( count( $rows ) >= $this->nonNativeInsertSelectBatchSize ) {
  2743. $ok = $this->insert( $destTable, $rows, $fname, $insertOptions );
  2744. if ( !$ok ) {
  2745. break;
  2746. }
  2747. $affectedRowCount += $this->affectedRows();
  2748. $rows = [];
  2749. }
  2750. }
  2751. if ( $rows && $ok ) {
  2752. $ok = $this->insert( $destTable, $rows, $fname, $insertOptions );
  2753. if ( $ok ) {
  2754. $affectedRowCount += $this->affectedRows();
  2755. }
  2756. }
  2757. if ( $ok ) {
  2758. $this->endAtomic( $fname );
  2759. $this->affectedRowCount = $affectedRowCount;
  2760. } else {
  2761. $this->cancelAtomic( $fname );
  2762. }
  2763. } catch ( Exception $e ) {
  2764. $this->cancelAtomic( $fname );
  2765. throw $e;
  2766. }
  2767. }
  2768. /**
  2769. * Native server-side implementation of insertSelect() for situations where
  2770. * we don't want to select everything into memory
  2771. *
  2772. * @see IDatabase::insertSelect()
  2773. * @param string $destTable
  2774. * @param string|array $srcTable
  2775. * @param array $varMap
  2776. * @param array $conds
  2777. * @param string $fname
  2778. * @param array $insertOptions
  2779. * @param array $selectOptions
  2780. * @param array $selectJoinConds
  2781. */
  2782. protected function nativeInsertSelect( $destTable, $srcTable, $varMap, $conds,
  2783. $fname = __METHOD__,
  2784. $insertOptions = [], $selectOptions = [], $selectJoinConds = []
  2785. ) {
  2786. $destTable = $this->tableName( $destTable );
  2787. if ( !is_array( $insertOptions ) ) {
  2788. $insertOptions = [ $insertOptions ];
  2789. }
  2790. $insertOptions = $this->makeInsertOptions( $insertOptions );
  2791. $selectSql = $this->selectSQLText(
  2792. $srcTable,
  2793. array_values( $varMap ),
  2794. $conds,
  2795. $fname,
  2796. $selectOptions,
  2797. $selectJoinConds
  2798. );
  2799. $sql = "INSERT $insertOptions" .
  2800. " INTO $destTable (" . implode( ',', array_keys( $varMap ) ) . ') ' .
  2801. $selectSql;
  2802. $this->query( $sql, $fname );
  2803. }
  2804. public function limitResult( $sql, $limit, $offset = false ) {
  2805. if ( !is_numeric( $limit ) ) {
  2806. throw new DBUnexpectedError(
  2807. $this,
  2808. "Invalid non-numeric limit passed to " . __METHOD__
  2809. );
  2810. }
  2811. // This version works in MySQL and SQLite. It will very likely need to be
  2812. // overridden for most other RDBMS subclasses.
  2813. return "$sql LIMIT "
  2814. . ( ( is_numeric( $offset ) && $offset != 0 ) ? "{$offset}," : "" )
  2815. . "{$limit} ";
  2816. }
  2817. public function unionSupportsOrderAndLimit() {
  2818. return true; // True for almost every DB supported
  2819. }
  2820. public function unionQueries( $sqls, $all ) {
  2821. $glue = $all ? ') UNION ALL (' : ') UNION (';
  2822. return '(' . implode( $glue, $sqls ) . ')';
  2823. }
  2824. public function unionConditionPermutations(
  2825. $table, $vars, array $permute_conds, $extra_conds = '', $fname = __METHOD__,
  2826. $options = [], $join_conds = []
  2827. ) {
  2828. // First, build the Cartesian product of $permute_conds
  2829. $conds = [ [] ];
  2830. foreach ( $permute_conds as $field => $values ) {
  2831. if ( !$values ) {
  2832. // Skip empty $values
  2833. continue;
  2834. }
  2835. $values = array_unique( $values ); // For sanity
  2836. $newConds = [];
  2837. foreach ( $conds as $cond ) {
  2838. foreach ( $values as $value ) {
  2839. $cond[$field] = $value;
  2840. $newConds[] = $cond; // Arrays are by-value, not by-reference, so this works
  2841. }
  2842. }
  2843. $conds = $newConds;
  2844. }
  2845. $extra_conds = $extra_conds === '' ? [] : (array)$extra_conds;
  2846. // If there's just one condition and no subordering, hand off to
  2847. // selectSQLText directly.
  2848. if ( count( $conds ) === 1 &&
  2849. ( !isset( $options['INNER ORDER BY'] ) || !$this->unionSupportsOrderAndLimit() )
  2850. ) {
  2851. return $this->selectSQLText(
  2852. $table, $vars, $conds[0] + $extra_conds, $fname, $options, $join_conds
  2853. );
  2854. }
  2855. // Otherwise, we need to pull out the order and limit to apply after
  2856. // the union. Then build the SQL queries for each set of conditions in
  2857. // $conds. Then union them together (using UNION ALL, because the
  2858. // product *should* already be distinct).
  2859. $orderBy = $this->makeOrderBy( $options );
  2860. $limit = $options['LIMIT'] ?? null;
  2861. $offset = $options['OFFSET'] ?? false;
  2862. $all = empty( $options['NOTALL'] ) && !in_array( 'NOTALL', $options );
  2863. if ( !$this->unionSupportsOrderAndLimit() ) {
  2864. unset( $options['ORDER BY'], $options['LIMIT'], $options['OFFSET'] );
  2865. } else {
  2866. if ( array_key_exists( 'INNER ORDER BY', $options ) ) {
  2867. $options['ORDER BY'] = $options['INNER ORDER BY'];
  2868. }
  2869. if ( $limit !== null && is_numeric( $offset ) && $offset != 0 ) {
  2870. // We need to increase the limit by the offset rather than
  2871. // using the offset directly, otherwise it'll skip incorrectly
  2872. // in the subqueries.
  2873. $options['LIMIT'] = $limit + $offset;
  2874. unset( $options['OFFSET'] );
  2875. }
  2876. }
  2877. $sqls = [];
  2878. foreach ( $conds as $cond ) {
  2879. $sqls[] = $this->selectSQLText(
  2880. $table, $vars, $cond + $extra_conds, $fname, $options, $join_conds
  2881. );
  2882. }
  2883. $sql = $this->unionQueries( $sqls, $all ) . $orderBy;
  2884. if ( $limit !== null ) {
  2885. $sql = $this->limitResult( $sql, $limit, $offset );
  2886. }
  2887. return $sql;
  2888. }
  2889. public function conditional( $cond, $trueVal, $falseVal ) {
  2890. if ( is_array( $cond ) ) {
  2891. $cond = $this->makeList( $cond, self::LIST_AND );
  2892. }
  2893. return " (CASE WHEN $cond THEN $trueVal ELSE $falseVal END) ";
  2894. }
  2895. public function strreplace( $orig, $old, $new ) {
  2896. return "REPLACE({$orig}, {$old}, {$new})";
  2897. }
  2898. public function getServerUptime() {
  2899. return 0;
  2900. }
  2901. public function wasDeadlock() {
  2902. return false;
  2903. }
  2904. public function wasLockTimeout() {
  2905. return false;
  2906. }
  2907. public function wasConnectionLoss() {
  2908. return $this->wasConnectionError( $this->lastErrno() );
  2909. }
  2910. public function wasReadOnlyError() {
  2911. return false;
  2912. }
  2913. public function wasErrorReissuable() {
  2914. return (
  2915. $this->wasDeadlock() ||
  2916. $this->wasLockTimeout() ||
  2917. $this->wasConnectionLoss()
  2918. );
  2919. }
  2920. /**
  2921. * Do not use this method outside of Database/DBError classes
  2922. *
  2923. * @param int|string $errno
  2924. * @return bool Whether the given query error was a connection drop
  2925. */
  2926. public function wasConnectionError( $errno ) {
  2927. return false;
  2928. }
  2929. /**
  2930. * @return bool Whether it is known that the last query error only caused statement rollback
  2931. * @note This is for backwards compatibility for callers catching DBError exceptions in
  2932. * order to ignore problems like duplicate key errors or foriegn key violations
  2933. * @since 1.31
  2934. */
  2935. protected function wasKnownStatementRollbackError() {
  2936. return false; // don't know; it could have caused a transaction rollback
  2937. }
  2938. public function deadlockLoop() {
  2939. $args = func_get_args();
  2940. $function = array_shift( $args );
  2941. $tries = self::$DEADLOCK_TRIES;
  2942. $this->begin( __METHOD__ );
  2943. $retVal = null;
  2944. /** @var Exception $e */
  2945. $e = null;
  2946. do {
  2947. try {
  2948. $retVal = $function( ...$args );
  2949. break;
  2950. } catch ( DBQueryError $e ) {
  2951. if ( $this->wasDeadlock() ) {
  2952. // Retry after a randomized delay
  2953. usleep( mt_rand( self::$DEADLOCK_DELAY_MIN, self::$DEADLOCK_DELAY_MAX ) );
  2954. } else {
  2955. // Throw the error back up
  2956. throw $e;
  2957. }
  2958. }
  2959. } while ( --$tries > 0 );
  2960. if ( $tries <= 0 ) {
  2961. // Too many deadlocks; give up
  2962. $this->rollback( __METHOD__ );
  2963. throw $e;
  2964. } else {
  2965. $this->commit( __METHOD__ );
  2966. return $retVal;
  2967. }
  2968. }
  2969. public function masterPosWait( DBMasterPos $pos, $timeout ) {
  2970. # Real waits are implemented in the subclass.
  2971. return 0;
  2972. }
  2973. public function getReplicaPos() {
  2974. # Stub
  2975. return false;
  2976. }
  2977. public function getMasterPos() {
  2978. # Stub
  2979. return false;
  2980. }
  2981. public function serverIsReadOnly() {
  2982. return false;
  2983. }
  2984. final public function onTransactionResolution( callable $callback, $fname = __METHOD__ ) {
  2985. if ( !$this->trxLevel() ) {
  2986. throw new DBUnexpectedError( $this, "No transaction is active" );
  2987. }
  2988. $this->trxEndCallbacks[] = [ $callback, $fname, $this->currentAtomicSectionId() ];
  2989. }
  2990. final public function onTransactionCommitOrIdle( callable $callback, $fname = __METHOD__ ) {
  2991. if ( !$this->trxLevel() && $this->getTransactionRoundId() ) {
  2992. // Start an implicit transaction similar to how query() does
  2993. $this->begin( __METHOD__, self::TRANSACTION_INTERNAL );
  2994. $this->trxAutomatic = true;
  2995. }
  2996. $this->trxIdleCallbacks[] = [ $callback, $fname, $this->currentAtomicSectionId() ];
  2997. if ( !$this->trxLevel() ) {
  2998. $this->runOnTransactionIdleCallbacks( self::TRIGGER_IDLE );
  2999. }
  3000. }
  3001. final public function onTransactionIdle( callable $callback, $fname = __METHOD__ ) {
  3002. $this->onTransactionCommitOrIdle( $callback, $fname );
  3003. }
  3004. final public function onTransactionPreCommitOrIdle( callable $callback, $fname = __METHOD__ ) {
  3005. if ( !$this->trxLevel() && $this->getTransactionRoundId() ) {
  3006. // Start an implicit transaction similar to how query() does
  3007. $this->begin( __METHOD__, self::TRANSACTION_INTERNAL );
  3008. $this->trxAutomatic = true;
  3009. }
  3010. if ( $this->trxLevel() ) {
  3011. $this->trxPreCommitCallbacks[] = [ $callback, $fname, $this->currentAtomicSectionId() ];
  3012. } else {
  3013. // No transaction is active nor will start implicitly, so make one for this callback
  3014. $this->startAtomic( __METHOD__, self::ATOMIC_CANCELABLE );
  3015. try {
  3016. $callback( $this );
  3017. $this->endAtomic( __METHOD__ );
  3018. } catch ( Exception $e ) {
  3019. $this->cancelAtomic( __METHOD__ );
  3020. throw $e;
  3021. }
  3022. }
  3023. }
  3024. final public function onAtomicSectionCancel( callable $callback, $fname = __METHOD__ ) {
  3025. if ( !$this->trxLevel() || !$this->trxAtomicLevels ) {
  3026. throw new DBUnexpectedError( $this, "No atomic section is open (got $fname)" );
  3027. }
  3028. $this->trxSectionCancelCallbacks[] = [ $callback, $fname, $this->currentAtomicSectionId() ];
  3029. }
  3030. /**
  3031. * @return AtomicSectionIdentifier|null ID of the topmost atomic section level
  3032. */
  3033. private function currentAtomicSectionId() {
  3034. if ( $this->trxLevel() && $this->trxAtomicLevels ) {
  3035. $levelInfo = end( $this->trxAtomicLevels );
  3036. return $levelInfo[1];
  3037. }
  3038. return null;
  3039. }
  3040. /**
  3041. * Hoist callback ownership for callbacks in a section to a parent section.
  3042. * All callbacks should have an owner that is present in trxAtomicLevels.
  3043. * @param AtomicSectionIdentifier $old
  3044. * @param AtomicSectionIdentifier $new
  3045. */
  3046. private function reassignCallbacksForSection(
  3047. AtomicSectionIdentifier $old, AtomicSectionIdentifier $new
  3048. ) {
  3049. foreach ( $this->trxPreCommitCallbacks as $key => $info ) {
  3050. if ( $info[2] === $old ) {
  3051. $this->trxPreCommitCallbacks[$key][2] = $new;
  3052. }
  3053. }
  3054. foreach ( $this->trxIdleCallbacks as $key => $info ) {
  3055. if ( $info[2] === $old ) {
  3056. $this->trxIdleCallbacks[$key][2] = $new;
  3057. }
  3058. }
  3059. foreach ( $this->trxEndCallbacks as $key => $info ) {
  3060. if ( $info[2] === $old ) {
  3061. $this->trxEndCallbacks[$key][2] = $new;
  3062. }
  3063. }
  3064. foreach ( $this->trxSectionCancelCallbacks as $key => $info ) {
  3065. if ( $info[2] === $old ) {
  3066. $this->trxSectionCancelCallbacks[$key][2] = $new;
  3067. }
  3068. }
  3069. }
  3070. /**
  3071. * Update callbacks that were owned by cancelled atomic sections.
  3072. *
  3073. * Callbacks for "on commit" should never be run if they're owned by a
  3074. * section that won't be committed.
  3075. *
  3076. * Callbacks for "on resolution" need to reflect that the section was
  3077. * rolled back, even if the transaction as a whole commits successfully.
  3078. *
  3079. * Callbacks for "on section cancel" should already have been consumed,
  3080. * but errors during the cancellation itself can prevent that while still
  3081. * destroying the section. Hoist any such callbacks to the new top section,
  3082. * which we assume will itself have to be cancelled or rolled back to
  3083. * resolve the error.
  3084. *
  3085. * @param AtomicSectionIdentifier[] $sectionIds ID of an actual savepoint
  3086. * @param AtomicSectionIdentifier|null $newSectionId New top section ID.
  3087. * @throws UnexpectedValueException
  3088. */
  3089. private function modifyCallbacksForCancel(
  3090. array $sectionIds, AtomicSectionIdentifier $newSectionId = null
  3091. ) {
  3092. // Cancel the "on commit" callbacks owned by this savepoint
  3093. $this->trxIdleCallbacks = array_filter(
  3094. $this->trxIdleCallbacks,
  3095. function ( $entry ) use ( $sectionIds ) {
  3096. return !in_array( $entry[2], $sectionIds, true );
  3097. }
  3098. );
  3099. $this->trxPreCommitCallbacks = array_filter(
  3100. $this->trxPreCommitCallbacks,
  3101. function ( $entry ) use ( $sectionIds ) {
  3102. return !in_array( $entry[2], $sectionIds, true );
  3103. }
  3104. );
  3105. // Make "on resolution" callbacks owned by this savepoint to perceive a rollback
  3106. foreach ( $this->trxEndCallbacks as $key => $entry ) {
  3107. if ( in_array( $entry[2], $sectionIds, true ) ) {
  3108. $callback = $entry[0];
  3109. $this->trxEndCallbacks[$key][0] = function () use ( $callback ) {
  3110. // @phan-suppress-next-line PhanInfiniteRecursion, PhanUndeclaredInvokeInCallable
  3111. return $callback( self::TRIGGER_ROLLBACK, $this );
  3112. };
  3113. // This "on resolution" callback no longer belongs to a section.
  3114. $this->trxEndCallbacks[$key][2] = null;
  3115. }
  3116. }
  3117. // Hoist callback ownership for section cancel callbacks to the new top section
  3118. foreach ( $this->trxSectionCancelCallbacks as $key => $entry ) {
  3119. if ( in_array( $entry[2], $sectionIds, true ) ) {
  3120. $this->trxSectionCancelCallbacks[$key][2] = $newSectionId;
  3121. }
  3122. }
  3123. }
  3124. final public function setTransactionListener( $name, callable $callback = null ) {
  3125. if ( $callback ) {
  3126. $this->trxRecurringCallbacks[$name] = $callback;
  3127. } else {
  3128. unset( $this->trxRecurringCallbacks[$name] );
  3129. }
  3130. }
  3131. /**
  3132. * Whether to disable running of post-COMMIT/ROLLBACK callbacks
  3133. *
  3134. * This method should not be used outside of Database/LoadBalancer
  3135. *
  3136. * @param bool $suppress
  3137. * @since 1.28
  3138. */
  3139. final public function setTrxEndCallbackSuppression( $suppress ) {
  3140. $this->trxEndCallbacksSuppressed = $suppress;
  3141. }
  3142. /**
  3143. * Actually consume and run any "on transaction idle/resolution" callbacks.
  3144. *
  3145. * This method should not be used outside of Database/LoadBalancer
  3146. *
  3147. * @param int $trigger IDatabase::TRIGGER_* constant
  3148. * @return int Number of callbacks attempted
  3149. * @since 1.20
  3150. * @throws Exception
  3151. */
  3152. public function runOnTransactionIdleCallbacks( $trigger ) {
  3153. if ( $this->trxLevel() ) { // sanity
  3154. throw new DBUnexpectedError( $this, __METHOD__ . ': a transaction is still open' );
  3155. }
  3156. if ( $this->trxEndCallbacksSuppressed ) {
  3157. return 0;
  3158. }
  3159. $count = 0;
  3160. $autoTrx = $this->getFlag( self::DBO_TRX ); // automatic begin() enabled?
  3161. /** @var Exception $e */
  3162. $e = null; // first exception
  3163. do { // callbacks may add callbacks :)
  3164. $callbacks = array_merge(
  3165. $this->trxIdleCallbacks,
  3166. $this->trxEndCallbacks // include "transaction resolution" callbacks
  3167. );
  3168. $this->trxIdleCallbacks = []; // consumed (and recursion guard)
  3169. $this->trxEndCallbacks = []; // consumed (recursion guard)
  3170. // Only run trxSectionCancelCallbacks on rollback, not commit.
  3171. // But always consume them.
  3172. if ( $trigger === self::TRIGGER_ROLLBACK ) {
  3173. $callbacks = array_merge( $callbacks, $this->trxSectionCancelCallbacks );
  3174. }
  3175. $this->trxSectionCancelCallbacks = []; // consumed (recursion guard)
  3176. foreach ( $callbacks as $callback ) {
  3177. ++$count;
  3178. list( $phpCallback ) = $callback;
  3179. $this->clearFlag( self::DBO_TRX ); // make each query its own transaction
  3180. try {
  3181. // @phan-suppress-next-line PhanParamTooManyCallable
  3182. call_user_func( $phpCallback, $trigger, $this );
  3183. } catch ( Exception $ex ) {
  3184. call_user_func( $this->errorLogger, $ex );
  3185. $e = $e ?: $ex;
  3186. // Some callbacks may use startAtomic/endAtomic, so make sure
  3187. // their transactions are ended so other callbacks don't fail
  3188. if ( $this->trxLevel() ) {
  3189. $this->rollback( __METHOD__, self::FLUSHING_INTERNAL );
  3190. }
  3191. } finally {
  3192. if ( $autoTrx ) {
  3193. $this->setFlag( self::DBO_TRX ); // restore automatic begin()
  3194. } else {
  3195. $this->clearFlag( self::DBO_TRX ); // restore auto-commit
  3196. }
  3197. }
  3198. }
  3199. } while ( count( $this->trxIdleCallbacks ) );
  3200. if ( $e instanceof Exception ) {
  3201. throw $e; // re-throw any first exception
  3202. }
  3203. return $count;
  3204. }
  3205. /**
  3206. * Actually consume and run any "on transaction pre-commit" callbacks.
  3207. *
  3208. * This method should not be used outside of Database/LoadBalancer
  3209. *
  3210. * @since 1.22
  3211. * @return int Number of callbacks attempted
  3212. * @throws Exception
  3213. */
  3214. public function runOnTransactionPreCommitCallbacks() {
  3215. $count = 0;
  3216. $e = null; // first exception
  3217. do { // callbacks may add callbacks :)
  3218. $callbacks = $this->trxPreCommitCallbacks;
  3219. $this->trxPreCommitCallbacks = []; // consumed (and recursion guard)
  3220. foreach ( $callbacks as $callback ) {
  3221. try {
  3222. ++$count;
  3223. list( $phpCallback ) = $callback;
  3224. // @phan-suppress-next-line PhanUndeclaredInvokeInCallable
  3225. $phpCallback( $this );
  3226. } catch ( Exception $ex ) {
  3227. ( $this->errorLogger )( $ex );
  3228. $e = $e ?: $ex;
  3229. }
  3230. }
  3231. } while ( count( $this->trxPreCommitCallbacks ) );
  3232. if ( $e instanceof Exception ) {
  3233. throw $e; // re-throw any first exception
  3234. }
  3235. return $count;
  3236. }
  3237. /**
  3238. * Actually run any "atomic section cancel" callbacks.
  3239. *
  3240. * @param int $trigger IDatabase::TRIGGER_* constant
  3241. * @param AtomicSectionIdentifier[]|null $sectionIds Section IDs to cancel,
  3242. * null on transaction rollback
  3243. */
  3244. private function runOnAtomicSectionCancelCallbacks(
  3245. $trigger, array $sectionIds = null
  3246. ) {
  3247. /** @var Exception|Throwable $e */
  3248. $e = null; // first exception
  3249. $notCancelled = [];
  3250. do {
  3251. $callbacks = $this->trxSectionCancelCallbacks;
  3252. $this->trxSectionCancelCallbacks = []; // consumed (recursion guard)
  3253. foreach ( $callbacks as $entry ) {
  3254. if ( $sectionIds === null || in_array( $entry[2], $sectionIds, true ) ) {
  3255. try {
  3256. // @phan-suppress-next-line PhanUndeclaredInvokeInCallable
  3257. $entry[0]( $trigger, $this );
  3258. } catch ( Exception $ex ) {
  3259. ( $this->errorLogger )( $ex );
  3260. $e = $e ?: $ex;
  3261. } catch ( Throwable $ex ) {
  3262. // @todo: Log?
  3263. $e = $e ?: $ex;
  3264. }
  3265. } else {
  3266. $notCancelled[] = $entry;
  3267. }
  3268. }
  3269. } while ( count( $this->trxSectionCancelCallbacks ) );
  3270. $this->trxSectionCancelCallbacks = $notCancelled;
  3271. if ( $e !== null ) {
  3272. throw $e; // re-throw any first Exception/Throwable
  3273. }
  3274. }
  3275. /**
  3276. * Actually run any "transaction listener" callbacks.
  3277. *
  3278. * This method should not be used outside of Database/LoadBalancer
  3279. *
  3280. * @param int $trigger IDatabase::TRIGGER_* constant
  3281. * @throws Exception
  3282. * @since 1.20
  3283. */
  3284. public function runTransactionListenerCallbacks( $trigger ) {
  3285. if ( $this->trxEndCallbacksSuppressed ) {
  3286. return;
  3287. }
  3288. /** @var Exception $e */
  3289. $e = null; // first exception
  3290. foreach ( $this->trxRecurringCallbacks as $phpCallback ) {
  3291. try {
  3292. $phpCallback( $trigger, $this );
  3293. } catch ( Exception $ex ) {
  3294. ( $this->errorLogger )( $ex );
  3295. $e = $e ?: $ex;
  3296. }
  3297. }
  3298. if ( $e instanceof Exception ) {
  3299. throw $e; // re-throw any first exception
  3300. }
  3301. }
  3302. /**
  3303. * Create a savepoint
  3304. *
  3305. * This is used internally to implement atomic sections. It should not be
  3306. * used otherwise.
  3307. *
  3308. * @since 1.31
  3309. * @param string $identifier Identifier for the savepoint
  3310. * @param string $fname Calling function name
  3311. */
  3312. protected function doSavepoint( $identifier, $fname ) {
  3313. $this->query( 'SAVEPOINT ' . $this->addIdentifierQuotes( $identifier ), $fname );
  3314. }
  3315. /**
  3316. * Release a savepoint
  3317. *
  3318. * This is used internally to implement atomic sections. It should not be
  3319. * used otherwise.
  3320. *
  3321. * @since 1.31
  3322. * @param string $identifier Identifier for the savepoint
  3323. * @param string $fname Calling function name
  3324. */
  3325. protected function doReleaseSavepoint( $identifier, $fname ) {
  3326. $this->query( 'RELEASE SAVEPOINT ' . $this->addIdentifierQuotes( $identifier ), $fname );
  3327. }
  3328. /**
  3329. * Rollback to a savepoint
  3330. *
  3331. * This is used internally to implement atomic sections. It should not be
  3332. * used otherwise.
  3333. *
  3334. * @since 1.31
  3335. * @param string $identifier Identifier for the savepoint
  3336. * @param string $fname Calling function name
  3337. */
  3338. protected function doRollbackToSavepoint( $identifier, $fname ) {
  3339. $this->query( 'ROLLBACK TO SAVEPOINT ' . $this->addIdentifierQuotes( $identifier ), $fname );
  3340. }
  3341. /**
  3342. * @param string $fname
  3343. * @return string
  3344. */
  3345. private function nextSavepointId( $fname ) {
  3346. $savepointId = self::$SAVEPOINT_PREFIX . ++$this->trxAtomicCounter;
  3347. if ( strlen( $savepointId ) > 30 ) {
  3348. // 30 == Oracle's identifier length limit (pre 12c)
  3349. // With a 22 character prefix, that puts the highest number at 99999999.
  3350. throw new DBUnexpectedError(
  3351. $this,
  3352. 'There have been an excessively large number of atomic sections in a transaction'
  3353. . " started by $this->trxFname (at $fname)"
  3354. );
  3355. }
  3356. return $savepointId;
  3357. }
  3358. final public function startAtomic(
  3359. $fname = __METHOD__, $cancelable = self::ATOMIC_NOT_CANCELABLE
  3360. ) {
  3361. $savepointId = $cancelable === self::ATOMIC_CANCELABLE ? self::$NOT_APPLICABLE : null;
  3362. if ( !$this->trxLevel() ) {
  3363. $this->begin( $fname, self::TRANSACTION_INTERNAL ); // sets trxAutomatic
  3364. // If DBO_TRX is set, a series of startAtomic/endAtomic pairs will result
  3365. // in all changes being in one transaction to keep requests transactional.
  3366. if ( $this->getFlag( self::DBO_TRX ) ) {
  3367. // Since writes could happen in between the topmost atomic sections as part
  3368. // of the transaction, those sections will need savepoints.
  3369. $savepointId = $this->nextSavepointId( $fname );
  3370. $this->doSavepoint( $savepointId, $fname );
  3371. } else {
  3372. $this->trxAutomaticAtomic = true;
  3373. }
  3374. } elseif ( $cancelable === self::ATOMIC_CANCELABLE ) {
  3375. $savepointId = $this->nextSavepointId( $fname );
  3376. $this->doSavepoint( $savepointId, $fname );
  3377. }
  3378. $sectionId = new AtomicSectionIdentifier;
  3379. $this->trxAtomicLevels[] = [ $fname, $sectionId, $savepointId ];
  3380. $this->queryLogger->debug( 'startAtomic: entering level ' .
  3381. ( count( $this->trxAtomicLevels ) - 1 ) . " ($fname)" );
  3382. return $sectionId;
  3383. }
  3384. final public function endAtomic( $fname = __METHOD__ ) {
  3385. if ( !$this->trxLevel() || !$this->trxAtomicLevels ) {
  3386. throw new DBUnexpectedError( $this, "No atomic section is open (got $fname)" );
  3387. }
  3388. // Check if the current section matches $fname
  3389. $pos = count( $this->trxAtomicLevels ) - 1;
  3390. list( $savedFname, $sectionId, $savepointId ) = $this->trxAtomicLevels[$pos];
  3391. $this->queryLogger->debug( "endAtomic: leaving level $pos ($fname)" );
  3392. if ( $savedFname !== $fname ) {
  3393. throw new DBUnexpectedError(
  3394. $this,
  3395. "Invalid atomic section ended (got $fname but expected $savedFname)"
  3396. );
  3397. }
  3398. // Remove the last section (no need to re-index the array)
  3399. array_pop( $this->trxAtomicLevels );
  3400. if ( !$this->trxAtomicLevels && $this->trxAutomaticAtomic ) {
  3401. $this->commit( $fname, self::FLUSHING_INTERNAL );
  3402. } elseif ( $savepointId !== null && $savepointId !== self::$NOT_APPLICABLE ) {
  3403. $this->doReleaseSavepoint( $savepointId, $fname );
  3404. }
  3405. // Hoist callback ownership for callbacks in the section that just ended;
  3406. // all callbacks should have an owner that is present in trxAtomicLevels.
  3407. $currentSectionId = $this->currentAtomicSectionId();
  3408. if ( $currentSectionId ) {
  3409. $this->reassignCallbacksForSection( $sectionId, $currentSectionId );
  3410. }
  3411. }
  3412. final public function cancelAtomic(
  3413. $fname = __METHOD__, AtomicSectionIdentifier $sectionId = null
  3414. ) {
  3415. if ( !$this->trxLevel() || !$this->trxAtomicLevels ) {
  3416. throw new DBUnexpectedError( $this, "No atomic section is open (got $fname)" );
  3417. }
  3418. $excisedIds = [];
  3419. $newTopSection = $this->currentAtomicSectionId();
  3420. try {
  3421. $excisedFnames = [];
  3422. if ( $sectionId !== null ) {
  3423. // Find the (last) section with the given $sectionId
  3424. $pos = -1;
  3425. foreach ( $this->trxAtomicLevels as $i => list( $asFname, $asId, $spId ) ) {
  3426. if ( $asId === $sectionId ) {
  3427. $pos = $i;
  3428. }
  3429. }
  3430. if ( $pos < 0 ) {
  3431. throw new DBUnexpectedError( $this, "Atomic section not found (for $fname)" );
  3432. }
  3433. // Remove all descendant sections and re-index the array
  3434. $len = count( $this->trxAtomicLevels );
  3435. for ( $i = $pos + 1; $i < $len; ++$i ) {
  3436. $excisedFnames[] = $this->trxAtomicLevels[$i][0];
  3437. $excisedIds[] = $this->trxAtomicLevels[$i][1];
  3438. }
  3439. $this->trxAtomicLevels = array_slice( $this->trxAtomicLevels, 0, $pos + 1 );
  3440. $newTopSection = $this->currentAtomicSectionId();
  3441. }
  3442. // Check if the current section matches $fname
  3443. $pos = count( $this->trxAtomicLevels ) - 1;
  3444. list( $savedFname, $savedSectionId, $savepointId ) = $this->trxAtomicLevels[$pos];
  3445. if ( $excisedFnames ) {
  3446. $this->queryLogger->debug( "cancelAtomic: canceling level $pos ($savedFname) " .
  3447. "and descendants " . implode( ', ', $excisedFnames ) );
  3448. } else {
  3449. $this->queryLogger->debug( "cancelAtomic: canceling level $pos ($savedFname)" );
  3450. }
  3451. if ( $savedFname !== $fname ) {
  3452. throw new DBUnexpectedError(
  3453. $this,
  3454. "Invalid atomic section ended (got $fname but expected $savedFname)"
  3455. );
  3456. }
  3457. // Remove the last section (no need to re-index the array)
  3458. array_pop( $this->trxAtomicLevels );
  3459. $excisedIds[] = $savedSectionId;
  3460. $newTopSection = $this->currentAtomicSectionId();
  3461. if ( $savepointId !== null ) {
  3462. // Rollback the transaction to the state just before this atomic section
  3463. if ( $savepointId === self::$NOT_APPLICABLE ) {
  3464. $this->rollback( $fname, self::FLUSHING_INTERNAL );
  3465. // Note: rollback() will run trxSectionCancelCallbacks
  3466. } else {
  3467. $this->doRollbackToSavepoint( $savepointId, $fname );
  3468. $this->trxStatus = self::STATUS_TRX_OK; // no exception; recovered
  3469. $this->trxStatusIgnoredCause = null;
  3470. // Run trxSectionCancelCallbacks now.
  3471. $this->runOnAtomicSectionCancelCallbacks( self::TRIGGER_CANCEL, $excisedIds );
  3472. }
  3473. } elseif ( $this->trxStatus > self::STATUS_TRX_ERROR ) {
  3474. // Put the transaction into an error state if it's not already in one
  3475. $this->trxStatus = self::STATUS_TRX_ERROR;
  3476. $this->trxStatusCause = new DBUnexpectedError(
  3477. $this,
  3478. "Uncancelable atomic section canceled (got $fname)"
  3479. );
  3480. }
  3481. } finally {
  3482. // Fix up callbacks owned by the sections that were just cancelled.
  3483. // All callbacks should have an owner that is present in trxAtomicLevels.
  3484. $this->modifyCallbacksForCancel( $excisedIds, $newTopSection );
  3485. }
  3486. $this->affectedRowCount = 0; // for the sake of consistency
  3487. }
  3488. final public function doAtomicSection(
  3489. $fname, callable $callback, $cancelable = self::ATOMIC_NOT_CANCELABLE
  3490. ) {
  3491. $sectionId = $this->startAtomic( $fname, $cancelable );
  3492. try {
  3493. $res = $callback( $this, $fname );
  3494. } catch ( Exception $e ) {
  3495. $this->cancelAtomic( $fname, $sectionId );
  3496. throw $e;
  3497. }
  3498. $this->endAtomic( $fname );
  3499. return $res;
  3500. }
  3501. final public function begin( $fname = __METHOD__, $mode = self::TRANSACTION_EXPLICIT ) {
  3502. static $modes = [ self::TRANSACTION_EXPLICIT, self::TRANSACTION_INTERNAL ];
  3503. if ( !in_array( $mode, $modes, true ) ) {
  3504. throw new DBUnexpectedError( $this, "$fname: invalid mode parameter '$mode'" );
  3505. }
  3506. // Protect against mismatched atomic section, transaction nesting, and snapshot loss
  3507. if ( $this->trxLevel() ) {
  3508. if ( $this->trxAtomicLevels ) {
  3509. $levels = $this->flatAtomicSectionList();
  3510. $msg = "$fname: got explicit BEGIN while atomic section(s) $levels are open";
  3511. throw new DBUnexpectedError( $this, $msg );
  3512. } elseif ( !$this->trxAutomatic ) {
  3513. $msg = "$fname: explicit transaction already active (from {$this->trxFname})";
  3514. throw new DBUnexpectedError( $this, $msg );
  3515. } else {
  3516. $msg = "$fname: implicit transaction already active (from {$this->trxFname})";
  3517. throw new DBUnexpectedError( $this, $msg );
  3518. }
  3519. } elseif ( $this->getFlag( self::DBO_TRX ) && $mode !== self::TRANSACTION_INTERNAL ) {
  3520. $msg = "$fname: implicit transaction expected (DBO_TRX set)";
  3521. throw new DBUnexpectedError( $this, $msg );
  3522. }
  3523. $this->assertHasConnectionHandle();
  3524. $this->doBegin( $fname );
  3525. $this->trxShortId = sprintf( '%06x', mt_rand( 0, 0xffffff ) );
  3526. $this->trxStatus = self::STATUS_TRX_OK;
  3527. $this->trxStatusIgnoredCause = null;
  3528. $this->trxAtomicCounter = 0;
  3529. $this->trxTimestamp = microtime( true );
  3530. $this->trxFname = $fname;
  3531. $this->trxDoneWrites = false;
  3532. $this->trxAutomaticAtomic = false;
  3533. $this->trxAtomicLevels = [];
  3534. $this->trxWriteDuration = 0.0;
  3535. $this->trxWriteQueryCount = 0;
  3536. $this->trxWriteAffectedRows = 0;
  3537. $this->trxWriteAdjDuration = 0.0;
  3538. $this->trxWriteAdjQueryCount = 0;
  3539. $this->trxWriteCallers = [];
  3540. // First SELECT after BEGIN will establish the snapshot in REPEATABLE-READ.
  3541. // Get an estimate of the replication lag before any such queries.
  3542. $this->trxReplicaLag = null; // clear cached value first
  3543. $this->trxReplicaLag = $this->getApproximateLagStatus()['lag'];
  3544. // T147697: make explicitTrxActive() return true until begin() finishes. This way, no
  3545. // caller will think its OK to muck around with the transaction just because startAtomic()
  3546. // has not yet completed (e.g. setting trxAtomicLevels).
  3547. $this->trxAutomatic = ( $mode === self::TRANSACTION_INTERNAL );
  3548. }
  3549. /**
  3550. * Issues the BEGIN command to the database server.
  3551. *
  3552. * @see Database::begin()
  3553. * @param string $fname
  3554. * @throws DBError
  3555. */
  3556. protected function doBegin( $fname ) {
  3557. $this->query( 'BEGIN', $fname );
  3558. }
  3559. final public function commit( $fname = __METHOD__, $flush = self::FLUSHING_ONE ) {
  3560. static $modes = [ self::FLUSHING_ONE, self::FLUSHING_ALL_PEERS, self::FLUSHING_INTERNAL ];
  3561. if ( !in_array( $flush, $modes, true ) ) {
  3562. throw new DBUnexpectedError( $this, "$fname: invalid flush parameter '$flush'" );
  3563. }
  3564. if ( $this->trxLevel() && $this->trxAtomicLevels ) {
  3565. // There are still atomic sections open; this cannot be ignored
  3566. $levels = $this->flatAtomicSectionList();
  3567. throw new DBUnexpectedError(
  3568. $this,
  3569. "$fname: got COMMIT while atomic sections $levels are still open"
  3570. );
  3571. }
  3572. if ( $flush === self::FLUSHING_INTERNAL || $flush === self::FLUSHING_ALL_PEERS ) {
  3573. if ( !$this->trxLevel() ) {
  3574. return; // nothing to do
  3575. } elseif ( !$this->trxAutomatic ) {
  3576. throw new DBUnexpectedError(
  3577. $this,
  3578. "$fname: flushing an explicit transaction, getting out of sync"
  3579. );
  3580. }
  3581. } elseif ( !$this->trxLevel() ) {
  3582. $this->queryLogger->error(
  3583. "$fname: no transaction to commit, something got out of sync" );
  3584. return; // nothing to do
  3585. } elseif ( $this->trxAutomatic ) {
  3586. throw new DBUnexpectedError(
  3587. $this,
  3588. "$fname: expected mass commit of all peer transactions (DBO_TRX set)"
  3589. );
  3590. }
  3591. $this->assertHasConnectionHandle();
  3592. $this->runOnTransactionPreCommitCallbacks();
  3593. $writeTime = $this->pendingWriteQueryDuration( self::ESTIMATE_DB_APPLY );
  3594. $this->doCommit( $fname );
  3595. $oldTrxShortId = $this->consumeTrxShortId();
  3596. $this->trxStatus = self::STATUS_TRX_NONE;
  3597. if ( $this->trxDoneWrites ) {
  3598. $this->lastWriteTime = microtime( true );
  3599. $this->trxProfiler->transactionWritingOut(
  3600. $this->server,
  3601. $this->getDomainID(),
  3602. $oldTrxShortId,
  3603. $writeTime,
  3604. $this->trxWriteAffectedRows
  3605. );
  3606. }
  3607. // With FLUSHING_ALL_PEERS, callbacks will be explicitly run later
  3608. if ( $flush !== self::FLUSHING_ALL_PEERS ) {
  3609. $this->runOnTransactionIdleCallbacks( self::TRIGGER_COMMIT );
  3610. $this->runTransactionListenerCallbacks( self::TRIGGER_COMMIT );
  3611. }
  3612. }
  3613. /**
  3614. * Issues the COMMIT command to the database server.
  3615. *
  3616. * @see Database::commit()
  3617. * @param string $fname
  3618. * @throws DBError
  3619. */
  3620. protected function doCommit( $fname ) {
  3621. if ( $this->trxLevel() ) {
  3622. $this->query( 'COMMIT', $fname );
  3623. }
  3624. }
  3625. final public function rollback( $fname = __METHOD__, $flush = self::FLUSHING_ONE ) {
  3626. $trxActive = $this->trxLevel();
  3627. if ( $flush !== self::FLUSHING_INTERNAL
  3628. && $flush !== self::FLUSHING_ALL_PEERS
  3629. && $this->getFlag( self::DBO_TRX )
  3630. ) {
  3631. throw new DBUnexpectedError(
  3632. $this,
  3633. "$fname: Expected mass rollback of all peer transactions (DBO_TRX set)"
  3634. );
  3635. }
  3636. if ( $trxActive ) {
  3637. $this->assertHasConnectionHandle();
  3638. $this->doRollback( $fname );
  3639. $oldTrxShortId = $this->consumeTrxShortId();
  3640. $this->trxStatus = self::STATUS_TRX_NONE;
  3641. $this->trxAtomicLevels = [];
  3642. // Estimate the RTT via a query now that trxStatus is OK
  3643. $writeTime = $this->pingAndCalculateLastTrxApplyTime();
  3644. if ( $this->trxDoneWrites ) {
  3645. $this->trxProfiler->transactionWritingOut(
  3646. $this->server,
  3647. $this->getDomainID(),
  3648. $oldTrxShortId,
  3649. $writeTime,
  3650. $this->trxWriteAffectedRows
  3651. );
  3652. }
  3653. }
  3654. // Clear any commit-dependant callbacks. They might even be present
  3655. // only due to transaction rounds, with no SQL transaction being active
  3656. $this->trxIdleCallbacks = [];
  3657. $this->trxPreCommitCallbacks = [];
  3658. // With FLUSHING_ALL_PEERS, callbacks will be explicitly run later
  3659. if ( $trxActive && $flush !== self::FLUSHING_ALL_PEERS ) {
  3660. try {
  3661. $this->runOnTransactionIdleCallbacks( self::TRIGGER_ROLLBACK );
  3662. } catch ( Exception $e ) {
  3663. // already logged; finish and let LoadBalancer move on during mass-rollback
  3664. }
  3665. try {
  3666. $this->runTransactionListenerCallbacks( self::TRIGGER_ROLLBACK );
  3667. } catch ( Exception $e ) {
  3668. // already logged; let LoadBalancer move on during mass-rollback
  3669. }
  3670. $this->affectedRowCount = 0; // for the sake of consistency
  3671. }
  3672. }
  3673. /**
  3674. * Issues the ROLLBACK command to the database server.
  3675. *
  3676. * @see Database::rollback()
  3677. * @param string $fname
  3678. * @throws DBError
  3679. */
  3680. protected function doRollback( $fname ) {
  3681. if ( $this->trxLevel() ) {
  3682. # Disconnects cause rollback anyway, so ignore those errors
  3683. $ignoreErrors = true;
  3684. $this->query( 'ROLLBACK', $fname, $ignoreErrors );
  3685. }
  3686. }
  3687. public function flushSnapshot( $fname = __METHOD__, $flush = self::FLUSHING_ONE ) {
  3688. if ( $this->explicitTrxActive() ) {
  3689. // Committing this transaction would break callers that assume it is still open
  3690. throw new DBUnexpectedError(
  3691. $this,
  3692. "$fname: Cannot flush snapshot; " .
  3693. "explicit transaction '{$this->trxFname}' is still open"
  3694. );
  3695. } elseif ( $this->writesOrCallbacksPending() ) {
  3696. // This only flushes transactions to clear snapshots, not to write data
  3697. $fnames = implode( ', ', $this->pendingWriteAndCallbackCallers() );
  3698. throw new DBUnexpectedError(
  3699. $this,
  3700. "$fname: Cannot flush snapshot; " .
  3701. "writes from transaction {$this->trxFname} are still pending ($fnames)"
  3702. );
  3703. } elseif (
  3704. $this->trxLevel() &&
  3705. $this->getTransactionRoundId() &&
  3706. $flush !== self::FLUSHING_INTERNAL &&
  3707. $flush !== self::FLUSHING_ALL_PEERS
  3708. ) {
  3709. $this->queryLogger->warning(
  3710. "$fname: Expected mass snapshot flush of all peer transactions " .
  3711. "in the explicit transactions round '{$this->getTransactionRoundId()}'",
  3712. [ 'exception' => new RuntimeException() ]
  3713. );
  3714. }
  3715. $this->commit( $fname, self::FLUSHING_INTERNAL );
  3716. }
  3717. public function explicitTrxActive() {
  3718. return $this->trxLevel() && ( $this->trxAtomicLevels || !$this->trxAutomatic );
  3719. }
  3720. public function duplicateTableStructure(
  3721. $oldName, $newName, $temporary = false, $fname = __METHOD__
  3722. ) {
  3723. throw new RuntimeException( __METHOD__ . ' is not implemented in descendant class' );
  3724. }
  3725. public function listTables( $prefix = null, $fname = __METHOD__ ) {
  3726. throw new RuntimeException( __METHOD__ . ' is not implemented in descendant class' );
  3727. }
  3728. public function listViews( $prefix = null, $fname = __METHOD__ ) {
  3729. throw new RuntimeException( __METHOD__ . ' is not implemented in descendant class' );
  3730. }
  3731. public function timestamp( $ts = 0 ) {
  3732. $t = new ConvertibleTimestamp( $ts );
  3733. // Let errors bubble up to avoid putting garbage in the DB
  3734. return $t->getTimestamp( TS_MW );
  3735. }
  3736. public function timestampOrNull( $ts = null ) {
  3737. if ( is_null( $ts ) ) {
  3738. return null;
  3739. } else {
  3740. return $this->timestamp( $ts );
  3741. }
  3742. }
  3743. public function affectedRows() {
  3744. return ( $this->affectedRowCount === null )
  3745. ? $this->fetchAffectedRowCount() // default to driver value
  3746. : $this->affectedRowCount;
  3747. }
  3748. /**
  3749. * @return int Number of retrieved rows according to the driver
  3750. */
  3751. abstract protected function fetchAffectedRowCount();
  3752. /**
  3753. * Take a query result and wrap it in an iterable result wrapper if necessary.
  3754. * Booleans are passed through as-is to indicate success/failure of write queries.
  3755. *
  3756. * Once upon a time, Database::query() returned a bare MySQL result
  3757. * resource, and it was necessary to call this function to convert it to
  3758. * a wrapper. Nowadays, raw database objects are never exposed to external
  3759. * callers, so this is unnecessary in external code.
  3760. *
  3761. * @param bool|IResultWrapper|resource $result
  3762. * @return bool|IResultWrapper
  3763. */
  3764. protected function resultObject( $result ) {
  3765. if ( !$result ) {
  3766. return false; // failed query
  3767. } elseif ( $result instanceof IResultWrapper ) {
  3768. return $result;
  3769. } elseif ( $result === true ) {
  3770. return $result; // succesful write query
  3771. } else {
  3772. return new ResultWrapper( $this, $result );
  3773. }
  3774. }
  3775. public function ping( &$rtt = null ) {
  3776. // Avoid hitting the server if it was hit recently
  3777. if ( $this->isOpen() && ( microtime( true ) - $this->lastPing ) < self::$PING_TTL ) {
  3778. if ( !func_num_args() || $this->lastRoundTripEstimate > 0 ) {
  3779. $rtt = $this->lastRoundTripEstimate;
  3780. return true; // don't care about $rtt
  3781. }
  3782. }
  3783. // This will reconnect if possible or return false if not
  3784. $flags = self::QUERY_IGNORE_DBO_TRX | self::QUERY_SILENCE_ERRORS;
  3785. $ok = ( $this->query( self::$PING_QUERY, __METHOD__, $flags ) !== false );
  3786. if ( $ok ) {
  3787. $rtt = $this->lastRoundTripEstimate;
  3788. }
  3789. return $ok;
  3790. }
  3791. /**
  3792. * Close any existing (dead) database connection and open a new connection
  3793. *
  3794. * @param string $fname
  3795. * @return bool True if new connection is opened successfully, false if error
  3796. */
  3797. protected function replaceLostConnection( $fname ) {
  3798. $this->closeConnection();
  3799. $this->conn = null;
  3800. $this->handleSessionLossPreconnect();
  3801. try {
  3802. $this->open(
  3803. $this->server,
  3804. $this->user,
  3805. $this->password,
  3806. $this->currentDomain->getDatabase(),
  3807. $this->currentDomain->getSchema(),
  3808. $this->tablePrefix()
  3809. );
  3810. $this->lastPing = microtime( true );
  3811. $ok = true;
  3812. $this->connLogger->warning(
  3813. $fname . ': lost connection to {dbserver}; reconnected',
  3814. [
  3815. 'dbserver' => $this->getServer(),
  3816. 'exception' => new RuntimeException()
  3817. ]
  3818. );
  3819. } catch ( DBConnectionError $e ) {
  3820. $ok = false;
  3821. $this->connLogger->error(
  3822. $fname . ': lost connection to {dbserver} permanently',
  3823. [ 'dbserver' => $this->getServer() ]
  3824. );
  3825. }
  3826. $this->handleSessionLossPostconnect();
  3827. return $ok;
  3828. }
  3829. public function getSessionLagStatus() {
  3830. return $this->getRecordedTransactionLagStatus() ?: $this->getApproximateLagStatus();
  3831. }
  3832. /**
  3833. * Get the replica DB lag when the current transaction started
  3834. *
  3835. * This is useful when transactions might use snapshot isolation
  3836. * (e.g. REPEATABLE-READ in innodb), so the "real" lag of that data
  3837. * is this lag plus transaction duration. If they don't, it is still
  3838. * safe to be pessimistic. This returns null if there is no transaction.
  3839. *
  3840. * This returns null if the lag status for this transaction was not yet recorded.
  3841. *
  3842. * @return array|null ('lag': seconds or false on error, 'since': UNIX timestamp of BEGIN)
  3843. * @since 1.27
  3844. */
  3845. final protected function getRecordedTransactionLagStatus() {
  3846. return ( $this->trxLevel() && $this->trxReplicaLag !== null )
  3847. ? [ 'lag' => $this->trxReplicaLag, 'since' => $this->trxTimestamp() ]
  3848. : null;
  3849. }
  3850. /**
  3851. * Get a replica DB lag estimate for this server
  3852. *
  3853. * @return array ('lag': seconds or false on error, 'since': UNIX timestamp of estimate)
  3854. * @since 1.27
  3855. */
  3856. protected function getApproximateLagStatus() {
  3857. return [
  3858. 'lag' => $this->getLBInfo( 'replica' ) ? $this->getLag() : 0,
  3859. 'since' => microtime( true )
  3860. ];
  3861. }
  3862. /**
  3863. * Merge the result of getSessionLagStatus() for several DBs
  3864. * using the most pessimistic values to estimate the lag of
  3865. * any data derived from them in combination
  3866. *
  3867. * This is information is useful for caching modules
  3868. *
  3869. * @see WANObjectCache::set()
  3870. * @see WANObjectCache::getWithSetCallback()
  3871. *
  3872. * @param IDatabase $db1
  3873. * @param IDatabase|null $db2 [optional]
  3874. * @return array Map of values:
  3875. * - lag: highest lag of any of the DBs or false on error (e.g. replication stopped)
  3876. * - since: oldest UNIX timestamp of any of the DB lag estimates
  3877. * - pending: whether any of the DBs have uncommitted changes
  3878. * @throws DBError
  3879. * @since 1.27
  3880. */
  3881. public static function getCacheSetOptions( IDatabase $db1, IDatabase $db2 = null ) {
  3882. $res = [ 'lag' => 0, 'since' => INF, 'pending' => false ];
  3883. foreach ( func_get_args() as $db ) {
  3884. /** @var IDatabase $db */
  3885. $status = $db->getSessionLagStatus();
  3886. if ( $status['lag'] === false ) {
  3887. $res['lag'] = false;
  3888. } elseif ( $res['lag'] !== false ) {
  3889. $res['lag'] = max( $res['lag'], $status['lag'] );
  3890. }
  3891. $res['since'] = min( $res['since'], $status['since'] );
  3892. $res['pending'] = $res['pending'] ?: $db->writesPending();
  3893. }
  3894. return $res;
  3895. }
  3896. public function getLag() {
  3897. if ( $this->getLBInfo( 'master' ) ) {
  3898. return 0; // this is the master
  3899. } elseif ( $this->getLBInfo( 'is static' ) ) {
  3900. return 0; // static dataset
  3901. }
  3902. return $this->doGetLag();
  3903. }
  3904. protected function doGetLag() {
  3905. return 0;
  3906. }
  3907. public function maxListLen() {
  3908. return 0;
  3909. }
  3910. public function encodeBlob( $b ) {
  3911. return $b;
  3912. }
  3913. public function decodeBlob( $b ) {
  3914. if ( $b instanceof Blob ) {
  3915. $b = $b->fetch();
  3916. }
  3917. return $b;
  3918. }
  3919. public function setSessionOptions( array $options ) {
  3920. }
  3921. public function sourceFile(
  3922. $filename,
  3923. callable $lineCallback = null,
  3924. callable $resultCallback = null,
  3925. $fname = false,
  3926. callable $inputCallback = null
  3927. ) {
  3928. AtEase::suppressWarnings();
  3929. $fp = fopen( $filename, 'r' );
  3930. AtEase::restoreWarnings();
  3931. if ( $fp === false ) {
  3932. throw new RuntimeException( "Could not open \"{$filename}\"" );
  3933. }
  3934. if ( !$fname ) {
  3935. $fname = __METHOD__ . "( $filename )";
  3936. }
  3937. try {
  3938. $error = $this->sourceStream(
  3939. $fp, $lineCallback, $resultCallback, $fname, $inputCallback );
  3940. } catch ( Exception $e ) {
  3941. fclose( $fp );
  3942. throw $e;
  3943. }
  3944. fclose( $fp );
  3945. return $error;
  3946. }
  3947. public function setSchemaVars( $vars ) {
  3948. $this->schemaVars = is_array( $vars ) ? $vars : null;
  3949. }
  3950. public function sourceStream(
  3951. $fp,
  3952. callable $lineCallback = null,
  3953. callable $resultCallback = null,
  3954. $fname = __METHOD__,
  3955. callable $inputCallback = null
  3956. ) {
  3957. $delimiterReset = new ScopedCallback(
  3958. function ( $delimiter ) {
  3959. $this->delimiter = $delimiter;
  3960. },
  3961. [ $this->delimiter ]
  3962. );
  3963. $cmd = '';
  3964. while ( !feof( $fp ) ) {
  3965. if ( $lineCallback ) {
  3966. call_user_func( $lineCallback );
  3967. }
  3968. $line = trim( fgets( $fp ) );
  3969. if ( $line == '' ) {
  3970. continue;
  3971. }
  3972. if ( $line[0] == '-' && $line[1] == '-' ) {
  3973. continue;
  3974. }
  3975. if ( $cmd != '' ) {
  3976. $cmd .= ' ';
  3977. }
  3978. $done = $this->streamStatementEnd( $cmd, $line );
  3979. $cmd .= "$line\n";
  3980. if ( $done || feof( $fp ) ) {
  3981. $cmd = $this->replaceVars( $cmd );
  3982. if ( $inputCallback ) {
  3983. $callbackResult = $inputCallback( $cmd );
  3984. if ( is_string( $callbackResult ) || !$callbackResult ) {
  3985. $cmd = $callbackResult;
  3986. }
  3987. }
  3988. if ( $cmd ) {
  3989. $res = $this->query( $cmd, $fname );
  3990. if ( $resultCallback ) {
  3991. $resultCallback( $res, $this );
  3992. }
  3993. if ( $res === false ) {
  3994. $err = $this->lastError();
  3995. return "Query \"{$cmd}\" failed with error code \"$err\".\n";
  3996. }
  3997. }
  3998. $cmd = '';
  3999. }
  4000. }
  4001. ScopedCallback::consume( $delimiterReset );
  4002. return true;
  4003. }
  4004. /**
  4005. * Called by sourceStream() to check if we've reached a statement end
  4006. *
  4007. * @param string &$sql SQL assembled so far
  4008. * @param string &$newLine New line about to be added to $sql
  4009. * @return bool Whether $newLine contains end of the statement
  4010. */
  4011. public function streamStatementEnd( &$sql, &$newLine ) {
  4012. if ( $this->delimiter ) {
  4013. $prev = $newLine;
  4014. $newLine = preg_replace(
  4015. '/' . preg_quote( $this->delimiter, '/' ) . '$/', '', $newLine );
  4016. if ( $newLine != $prev ) {
  4017. return true;
  4018. }
  4019. }
  4020. return false;
  4021. }
  4022. /**
  4023. * Database independent variable replacement. Replaces a set of variables
  4024. * in an SQL statement with their contents as given by $this->getSchemaVars().
  4025. *
  4026. * Supports '{$var}' `{$var}` and / *$var* / (without the spaces) style variables.
  4027. *
  4028. * - '{$var}' should be used for text and is passed through the database's
  4029. * addQuotes method.
  4030. * - `{$var}` should be used for identifiers (e.g. table and database names).
  4031. * It is passed through the database's addIdentifierQuotes method which
  4032. * can be overridden if the database uses something other than backticks.
  4033. * - / *_* / or / *$wgDBprefix* / passes the name that follows through the
  4034. * database's tableName method.
  4035. * - / *i* / passes the name that follows through the database's indexName method.
  4036. * - In all other cases, / *$var* / is left unencoded. Except for table options,
  4037. * its use should be avoided. In 1.24 and older, string encoding was applied.
  4038. *
  4039. * @param string $ins SQL statement to replace variables in
  4040. * @return string The new SQL statement with variables replaced
  4041. */
  4042. protected function replaceVars( $ins ) {
  4043. $vars = $this->getSchemaVars();
  4044. return preg_replace_callback(
  4045. '!
  4046. /\* (\$wgDBprefix|[_i]) \*/ (\w*) | # 1-2. tableName, indexName
  4047. \'\{\$ (\w+) }\' | # 3. addQuotes
  4048. `\{\$ (\w+) }` | # 4. addIdentifierQuotes
  4049. /\*\$ (\w+) \*/ # 5. leave unencoded
  4050. !x',
  4051. function ( $m ) use ( $vars ) {
  4052. // Note: Because of <https://bugs.php.net/bug.php?id=51881>,
  4053. // check for both nonexistent keys *and* the empty string.
  4054. if ( isset( $m[1] ) && $m[1] !== '' ) {
  4055. if ( $m[1] === 'i' ) {
  4056. return $this->indexName( $m[2] );
  4057. } else {
  4058. return $this->tableName( $m[2] );
  4059. }
  4060. } elseif ( isset( $m[3] ) && $m[3] !== '' && array_key_exists( $m[3], $vars ) ) {
  4061. return $this->addQuotes( $vars[$m[3]] );
  4062. } elseif ( isset( $m[4] ) && $m[4] !== '' && array_key_exists( $m[4], $vars ) ) {
  4063. return $this->addIdentifierQuotes( $vars[$m[4]] );
  4064. } elseif ( isset( $m[5] ) && $m[5] !== '' && array_key_exists( $m[5], $vars ) ) {
  4065. return $vars[$m[5]];
  4066. } else {
  4067. return $m[0];
  4068. }
  4069. },
  4070. $ins
  4071. );
  4072. }
  4073. /**
  4074. * Get schema variables. If none have been set via setSchemaVars(), then
  4075. * use some defaults from the current object.
  4076. *
  4077. * @return array
  4078. */
  4079. protected function getSchemaVars() {
  4080. return $this->schemaVars ?? $this->getDefaultSchemaVars();
  4081. }
  4082. /**
  4083. * Get schema variables to use if none have been set via setSchemaVars().
  4084. *
  4085. * Override this in derived classes to provide variables for tables.sql
  4086. * and SQL patch files.
  4087. *
  4088. * @return array
  4089. */
  4090. protected function getDefaultSchemaVars() {
  4091. return [];
  4092. }
  4093. public function lockIsFree( $lockName, $method ) {
  4094. // RDBMs methods for checking named locks may or may not count this thread itself.
  4095. // In MySQL, IS_FREE_LOCK() returns 0 if the thread already has the lock. This is
  4096. // the behavior choosen by the interface for this method.
  4097. return !isset( $this->sessionNamedLocks[$lockName] );
  4098. }
  4099. public function lock( $lockName, $method, $timeout = 5 ) {
  4100. $this->sessionNamedLocks[$lockName] = 1;
  4101. return true;
  4102. }
  4103. public function unlock( $lockName, $method ) {
  4104. unset( $this->sessionNamedLocks[$lockName] );
  4105. return true;
  4106. }
  4107. public function getScopedLockAndFlush( $lockKey, $fname, $timeout ) {
  4108. if ( $this->writesOrCallbacksPending() ) {
  4109. // This only flushes transactions to clear snapshots, not to write data
  4110. $fnames = implode( ', ', $this->pendingWriteAndCallbackCallers() );
  4111. throw new DBUnexpectedError(
  4112. $this,
  4113. "$fname: Cannot flush pre-lock snapshot; " .
  4114. "writes from transaction {$this->trxFname} are still pending ($fnames)"
  4115. );
  4116. }
  4117. if ( !$this->lock( $lockKey, $fname, $timeout ) ) {
  4118. return null;
  4119. }
  4120. $unlocker = new ScopedCallback( function () use ( $lockKey, $fname ) {
  4121. if ( $this->trxLevel() ) {
  4122. // There is a good chance an exception was thrown, causing any early return
  4123. // from the caller. Let any error handler get a chance to issue rollback().
  4124. // If there isn't one, let the error bubble up and trigger server-side rollback.
  4125. $this->onTransactionResolution(
  4126. function () use ( $lockKey, $fname ) {
  4127. $this->unlock( $lockKey, $fname );
  4128. },
  4129. $fname
  4130. );
  4131. } else {
  4132. $this->unlock( $lockKey, $fname );
  4133. }
  4134. } );
  4135. $this->commit( $fname, self::FLUSHING_INTERNAL );
  4136. return $unlocker;
  4137. }
  4138. public function namedLocksEnqueue() {
  4139. return false;
  4140. }
  4141. public function tableLocksHaveTransactionScope() {
  4142. return true;
  4143. }
  4144. final public function lockTables( array $read, array $write, $method ) {
  4145. if ( $this->writesOrCallbacksPending() ) {
  4146. throw new DBUnexpectedError( $this, "Transaction writes or callbacks still pending" );
  4147. }
  4148. if ( $this->tableLocksHaveTransactionScope() ) {
  4149. $this->startAtomic( $method );
  4150. }
  4151. return $this->doLockTables( $read, $write, $method );
  4152. }
  4153. /**
  4154. * Helper function for lockTables() that handles the actual table locking
  4155. *
  4156. * @param array $read Array of tables to lock for read access
  4157. * @param array $write Array of tables to lock for write access
  4158. * @param string $method Name of caller
  4159. * @return true
  4160. */
  4161. protected function doLockTables( array $read, array $write, $method ) {
  4162. return true;
  4163. }
  4164. final public function unlockTables( $method ) {
  4165. if ( $this->tableLocksHaveTransactionScope() ) {
  4166. $this->endAtomic( $method );
  4167. return true; // locks released on COMMIT/ROLLBACK
  4168. }
  4169. return $this->doUnlockTables( $method );
  4170. }
  4171. /**
  4172. * Helper function for unlockTables() that handles the actual table unlocking
  4173. *
  4174. * @param string $method Name of caller
  4175. * @return true
  4176. */
  4177. protected function doUnlockTables( $method ) {
  4178. return true;
  4179. }
  4180. /**
  4181. * Delete a table
  4182. * @param string $tableName
  4183. * @param string $fName
  4184. * @return bool|IResultWrapper
  4185. * @since 1.18
  4186. */
  4187. public function dropTable( $tableName, $fName = __METHOD__ ) {
  4188. if ( !$this->tableExists( $tableName, $fName ) ) {
  4189. return false;
  4190. }
  4191. $sql = "DROP TABLE " . $this->tableName( $tableName ) . " CASCADE";
  4192. return $this->query( $sql, $fName );
  4193. }
  4194. public function getInfinity() {
  4195. return 'infinity';
  4196. }
  4197. public function encodeExpiry( $expiry ) {
  4198. return ( $expiry == '' || $expiry == 'infinity' || $expiry == $this->getInfinity() )
  4199. ? $this->getInfinity()
  4200. : $this->timestamp( $expiry );
  4201. }
  4202. public function decodeExpiry( $expiry, $format = TS_MW ) {
  4203. if ( $expiry == '' || $expiry == 'infinity' || $expiry == $this->getInfinity() ) {
  4204. return 'infinity';
  4205. }
  4206. return ConvertibleTimestamp::convert( $format, $expiry );
  4207. }
  4208. public function setBigSelects( $value = true ) {
  4209. // no-op
  4210. }
  4211. public function isReadOnly() {
  4212. return ( $this->getReadOnlyReason() !== false );
  4213. }
  4214. /**
  4215. * @return string|bool Reason this DB is read-only or false if it is not
  4216. */
  4217. protected function getReadOnlyReason() {
  4218. $reason = $this->getLBInfo( 'readOnlyReason' );
  4219. if ( is_string( $reason ) ) {
  4220. return $reason;
  4221. } elseif ( $this->getLBInfo( 'replica' ) ) {
  4222. return "Server is configured in the role of a read-only replica database.";
  4223. }
  4224. return false;
  4225. }
  4226. public function setTableAliases( array $aliases ) {
  4227. $this->tableAliases = $aliases;
  4228. }
  4229. public function setIndexAliases( array $aliases ) {
  4230. $this->indexAliases = $aliases;
  4231. }
  4232. /**
  4233. * @param int $field
  4234. * @param int $flags
  4235. * @return bool
  4236. * @since 1.34
  4237. */
  4238. final protected function fieldHasBit( $field, $flags ) {
  4239. return ( ( $field & $flags ) === $flags );
  4240. }
  4241. /**
  4242. * Get the underlying binding connection handle
  4243. *
  4244. * Makes sure the connection resource is set (disconnects and ping() failure can unset it).
  4245. * This catches broken callers than catch and ignore disconnection exceptions.
  4246. * Unlike checking isOpen(), this is safe to call inside of open().
  4247. *
  4248. * @return mixed
  4249. * @throws DBUnexpectedError
  4250. * @since 1.26
  4251. */
  4252. protected function getBindingHandle() {
  4253. if ( !$this->conn ) {
  4254. throw new DBUnexpectedError(
  4255. $this,
  4256. 'DB connection was already closed or the connection dropped'
  4257. );
  4258. }
  4259. return $this->conn;
  4260. }
  4261. public function __toString() {
  4262. // spl_object_id is PHP >= 7.2
  4263. $id = function_exists( 'spl_object_id' )
  4264. ? spl_object_id( $this )
  4265. : spl_object_hash( $this );
  4266. $description = $this->getType() . ' object #' . $id;
  4267. if ( is_resource( $this->conn ) ) {
  4268. $description .= ' (' . (string)$this->conn . ')'; // "resource id #<ID>"
  4269. } elseif ( is_object( $this->conn ) ) {
  4270. // spl_object_id is PHP >= 7.2
  4271. $handleId = function_exists( 'spl_object_id' )
  4272. ? spl_object_id( $this->conn )
  4273. : spl_object_hash( $this->conn );
  4274. $description .= " (handle id #$handleId)";
  4275. }
  4276. return $description;
  4277. }
  4278. /**
  4279. * Make sure that copies do not share the same client binding handle
  4280. * @throws DBConnectionError
  4281. */
  4282. public function __clone() {
  4283. $this->connLogger->warning(
  4284. "Cloning " . static::class . " is not recommended; forking connection",
  4285. [ 'exception' => new RuntimeException() ]
  4286. );
  4287. if ( $this->isOpen() ) {
  4288. // Open a new connection resource without messing with the old one
  4289. $this->conn = null;
  4290. $this->trxEndCallbacks = []; // don't copy
  4291. $this->trxSectionCancelCallbacks = []; // don't copy
  4292. $this->handleSessionLossPreconnect(); // no trx or locks anymore
  4293. $this->open(
  4294. $this->server,
  4295. $this->user,
  4296. $this->password,
  4297. $this->currentDomain->getDatabase(),
  4298. $this->currentDomain->getSchema(),
  4299. $this->tablePrefix()
  4300. );
  4301. $this->lastPing = microtime( true );
  4302. }
  4303. }
  4304. /**
  4305. * Called by serialize. Throw an exception when DB connection is serialized.
  4306. * This causes problems on some database engines because the connection is
  4307. * not restored on unserialize.
  4308. */
  4309. public function __sleep() {
  4310. throw new RuntimeException( 'Database serialization may cause problems, since ' .
  4311. 'the connection is not restored on wakeup' );
  4312. }
  4313. /**
  4314. * Run a few simple sanity checks and close dangling connections
  4315. */
  4316. public function __destruct() {
  4317. if ( $this->trxLevel() && $this->trxDoneWrites ) {
  4318. trigger_error( "Uncommitted DB writes (transaction from {$this->trxFname})" );
  4319. }
  4320. $danglingWriters = $this->pendingWriteAndCallbackCallers();
  4321. if ( $danglingWriters ) {
  4322. $fnames = implode( ', ', $danglingWriters );
  4323. trigger_error( "DB transaction writes or callbacks still pending ($fnames)" );
  4324. }
  4325. if ( $this->conn ) {
  4326. // Avoid connection leaks for sanity. Normally, resources close at script completion.
  4327. // The connection might already be closed in zend/hhvm by now, so suppress warnings.
  4328. AtEase::suppressWarnings();
  4329. $this->closeConnection();
  4330. AtEase::restoreWarnings();
  4331. $this->conn = null;
  4332. }
  4333. }
  4334. }
  4335. /**
  4336. * @deprecated since 1.28
  4337. */
  4338. class_alias( Database::class, 'DatabaseBase' );
  4339. /**
  4340. * @deprecated since 1.29
  4341. */
  4342. class_alias( Database::class, 'Database' );