DBConnRef.php 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800
  1. <?php
  2. namespace Wikimedia\Rdbms;
  3. use InvalidArgumentException;
  4. /**
  5. * Helper class used for automatically marking an IDatabase connection as reusable (once it no
  6. * longer matters which DB domain is selected) and for deferring the actual network connection
  7. *
  8. * This uses an RAII-style pattern where calling code is expected to keep the returned reference
  9. * handle as a function variable that falls out of scope when no longer needed. This avoids the
  10. * need for matching reuseConnection() calls for every "return" statement as well as the tedious
  11. * use of try/finally.
  12. *
  13. * @par Example:
  14. * @code
  15. * function getRowData() {
  16. * $conn = $this->lb->getConnectedRef( DB_REPLICA );
  17. * $row = $conn->select( ... );
  18. * return $row ? (array)$row : false;
  19. * // $conn falls out of scope and $this->lb->reuseConnection() gets called
  20. * }
  21. * @endcode
  22. *
  23. * @ingroup Database
  24. * @since 1.22
  25. */
  26. class DBConnRef implements IDatabase {
  27. /** @var ILoadBalancer */
  28. private $lb;
  29. /** @var Database|null Live connection handle */
  30. private $conn;
  31. /** @var array|null N-tuple of (server index, group, DatabaseDomain|string) */
  32. private $params;
  33. /** @var int One of DB_MASTER/DB_REPLICA */
  34. private $role;
  35. const FLD_INDEX = 0;
  36. const FLD_GROUP = 1;
  37. const FLD_DOMAIN = 2;
  38. const FLD_FLAGS = 3;
  39. /**
  40. * @param ILoadBalancer $lb Connection manager for $conn
  41. * @param IDatabase|array $conn Database or (server index, query groups, domain, flags)
  42. * @param int $role The type of connection asked for; one of DB_MASTER/DB_REPLICA
  43. * @internal This method should not be called outside of LoadBalancer
  44. */
  45. public function __construct( ILoadBalancer $lb, $conn, $role ) {
  46. $this->lb = $lb;
  47. $this->role = $role;
  48. if ( $conn instanceof IDatabase && !( $conn instanceof DBConnRef ) ) {
  49. $this->conn = $conn; // live handle
  50. } elseif ( is_array( $conn ) && count( $conn ) >= 4 && $conn[self::FLD_DOMAIN] !== false ) {
  51. $this->params = $conn;
  52. } else {
  53. throw new InvalidArgumentException( "Missing lazy connection arguments." );
  54. }
  55. }
  56. function __call( $name, array $arguments ) {
  57. if ( $this->conn === null ) {
  58. list( $index, $groups, $wiki, $flags ) = $this->params;
  59. $this->conn = $this->lb->getConnection( $index, $groups, $wiki, $flags );
  60. }
  61. return $this->conn->$name( ...$arguments );
  62. }
  63. /**
  64. * @return int DB_MASTER when this *requires* the master DB, otherwise DB_REPLICA
  65. * @since 1.33
  66. */
  67. public function getReferenceRole() {
  68. return $this->role;
  69. }
  70. public function getServerInfo() {
  71. return $this->__call( __FUNCTION__, func_get_args() );
  72. }
  73. /**
  74. * @param bool|null $buffer
  75. * @return bool
  76. * @deprecated Since 1.34 Use query batching
  77. */
  78. public function bufferResults( $buffer = null ) {
  79. return $this->__call( __FUNCTION__, func_get_args() );
  80. }
  81. public function trxLevel() {
  82. return $this->__call( __FUNCTION__, func_get_args() );
  83. }
  84. public function trxTimestamp() {
  85. return $this->__call( __FUNCTION__, func_get_args() );
  86. }
  87. public function explicitTrxActive() {
  88. return $this->__call( __FUNCTION__, func_get_args() );
  89. }
  90. public function assertNoOpenTransactions() {
  91. return $this->__call( __FUNCTION__, func_get_args() );
  92. }
  93. public function tablePrefix( $prefix = null ) {
  94. if ( $this->conn === null && $prefix === null ) {
  95. $domain = DatabaseDomain::newFromId( $this->params[self::FLD_DOMAIN] );
  96. // Avoid triggering a database connection
  97. return $domain->getTablePrefix();
  98. } elseif ( $this->conn !== null && $prefix === null ) {
  99. // This will just return the prefix
  100. return $this->__call( __FUNCTION__, func_get_args() );
  101. }
  102. // Disallow things that might confuse the LoadBalancer tracking
  103. throw new DBUnexpectedError( $this, "Database selection is disallowed to enable reuse." );
  104. }
  105. public function dbSchema( $schema = null ) {
  106. if ( $this->conn === null && $schema === null ) {
  107. $domain = DatabaseDomain::newFromId( $this->params[self::FLD_DOMAIN] );
  108. // Avoid triggering a database connection
  109. return $domain->getSchema();
  110. } elseif ( $this->conn !== null && $schema === null ) {
  111. // This will just return the schema
  112. return $this->__call( __FUNCTION__, func_get_args() );
  113. }
  114. // Disallow things that might confuse the LoadBalancer tracking
  115. throw new DBUnexpectedError( $this, "Database selection is disallowed to enable reuse." );
  116. }
  117. public function getLBInfo( $name = null ) {
  118. return $this->__call( __FUNCTION__, func_get_args() );
  119. }
  120. public function setLBInfo( $nameOrArray, $value = null ) {
  121. // Disallow things that might confuse the LoadBalancer tracking
  122. throw new DBUnexpectedError( $this, "Changing LB info is disallowed to enable reuse." );
  123. }
  124. public function setLazyMasterHandle( IDatabase $conn ) {
  125. // Disallow things that might confuse the LoadBalancer tracking
  126. throw new DBUnexpectedError( $this, "Database injection is disallowed to enable reuse." );
  127. }
  128. public function implicitOrderby() {
  129. return $this->__call( __FUNCTION__, func_get_args() );
  130. }
  131. public function lastQuery() {
  132. return $this->__call( __FUNCTION__, func_get_args() );
  133. }
  134. public function lastDoneWrites() {
  135. return $this->__call( __FUNCTION__, func_get_args() );
  136. }
  137. public function writesPending() {
  138. return $this->__call( __FUNCTION__, func_get_args() );
  139. }
  140. public function preCommitCallbacksPending() {
  141. return $this->__call( __FUNCTION__, func_get_args() );
  142. }
  143. public function writesOrCallbacksPending() {
  144. return $this->__call( __FUNCTION__, func_get_args() );
  145. }
  146. public function pendingWriteQueryDuration( $type = self::ESTIMATE_TOTAL ) {
  147. return $this->__call( __FUNCTION__, func_get_args() );
  148. }
  149. public function pendingWriteCallers() {
  150. return $this->__call( __FUNCTION__, func_get_args() );
  151. }
  152. public function pendingWriteRowsAffected() {
  153. return $this->__call( __FUNCTION__, func_get_args() );
  154. }
  155. public function isOpen() {
  156. return $this->__call( __FUNCTION__, func_get_args() );
  157. }
  158. public function setFlag( $flag, $remember = self::REMEMBER_NOTHING ) {
  159. return $this->__call( __FUNCTION__, func_get_args() );
  160. }
  161. public function clearFlag( $flag, $remember = self::REMEMBER_NOTHING ) {
  162. return $this->__call( __FUNCTION__, func_get_args() );
  163. }
  164. public function restoreFlags( $state = self::RESTORE_PRIOR ) {
  165. return $this->__call( __FUNCTION__, func_get_args() );
  166. }
  167. public function getFlag( $flag ) {
  168. return $this->__call( __FUNCTION__, func_get_args() );
  169. }
  170. public function getProperty( $name ) {
  171. return $this->__call( __FUNCTION__, func_get_args() );
  172. }
  173. public function getDomainID() {
  174. if ( $this->conn === null ) {
  175. $domain = $this->params[self::FLD_DOMAIN];
  176. // Avoid triggering a database connection
  177. return $domain instanceof DatabaseDomain ? $domain->getId() : $domain;
  178. }
  179. return $this->__call( __FUNCTION__, func_get_args() );
  180. }
  181. public function getType() {
  182. if ( $this->conn === null ) {
  183. // Avoid triggering a database connection
  184. if ( $this->params[self::FLD_INDEX] === ILoadBalancer::DB_MASTER ) {
  185. $index = $this->lb->getWriterIndex();
  186. } else {
  187. $index = $this->params[self::FLD_INDEX];
  188. }
  189. if ( $index >= 0 ) {
  190. // In theory, if $index is DB_REPLICA, the type could vary
  191. return $this->lb->getServerType( $index );
  192. }
  193. }
  194. return $this->__call( __FUNCTION__, func_get_args() );
  195. }
  196. public function fetchObject( $res ) {
  197. return $this->__call( __FUNCTION__, func_get_args() );
  198. }
  199. public function fetchRow( $res ) {
  200. return $this->__call( __FUNCTION__, func_get_args() );
  201. }
  202. public function numRows( $res ) {
  203. return $this->__call( __FUNCTION__, func_get_args() );
  204. }
  205. public function numFields( $res ) {
  206. return $this->__call( __FUNCTION__, func_get_args() );
  207. }
  208. public function fieldName( $res, $n ) {
  209. return $this->__call( __FUNCTION__, func_get_args() );
  210. }
  211. public function insertId() {
  212. return $this->__call( __FUNCTION__, func_get_args() );
  213. }
  214. public function dataSeek( $res, $row ) {
  215. return $this->__call( __FUNCTION__, func_get_args() );
  216. }
  217. public function lastErrno() {
  218. return $this->__call( __FUNCTION__, func_get_args() );
  219. }
  220. public function lastError() {
  221. return $this->__call( __FUNCTION__, func_get_args() );
  222. }
  223. public function affectedRows() {
  224. return $this->__call( __FUNCTION__, func_get_args() );
  225. }
  226. public function getSoftwareLink() {
  227. return $this->__call( __FUNCTION__, func_get_args() );
  228. }
  229. public function getServerVersion() {
  230. return $this->__call( __FUNCTION__, func_get_args() );
  231. }
  232. public function close( $fname = __METHOD__, $owner = null ) {
  233. throw new DBUnexpectedError( $this->conn, 'Cannot close shared connection.' );
  234. }
  235. public function query( $sql, $fname = __METHOD__, $flags = 0 ) {
  236. if ( $this->role !== ILoadBalancer::DB_MASTER ) {
  237. $flags |= IDatabase::QUERY_REPLICA_ROLE;
  238. }
  239. return $this->__call( __FUNCTION__, [ $sql, $fname, $flags ] );
  240. }
  241. public function freeResult( $res ) {
  242. return $this->__call( __FUNCTION__, func_get_args() );
  243. }
  244. public function selectField(
  245. $table, $var, $cond = '', $fname = __METHOD__, $options = [], $join_conds = []
  246. ) {
  247. return $this->__call( __FUNCTION__, func_get_args() );
  248. }
  249. public function selectFieldValues(
  250. $table, $var, $cond = '', $fname = __METHOD__, $options = [], $join_conds = []
  251. ) {
  252. return $this->__call( __FUNCTION__, func_get_args() );
  253. }
  254. public function select(
  255. $table, $vars, $conds = '', $fname = __METHOD__,
  256. $options = [], $join_conds = []
  257. ) {
  258. return $this->__call( __FUNCTION__, func_get_args() );
  259. }
  260. public function selectSQLText(
  261. $table, $vars, $conds = '', $fname = __METHOD__,
  262. $options = [], $join_conds = []
  263. ) {
  264. return $this->__call( __FUNCTION__, func_get_args() );
  265. }
  266. public function limitResult( $sql, $limit, $offset = false ) {
  267. return $this->__call( __FUNCTION__, func_get_args() );
  268. }
  269. public function selectRow(
  270. $table, $vars, $conds, $fname = __METHOD__,
  271. $options = [], $join_conds = []
  272. ) {
  273. return $this->__call( __FUNCTION__, func_get_args() );
  274. }
  275. public function estimateRowCount(
  276. $table, $vars = '*', $conds = '', $fname = __METHOD__, $options = [], $join_conds = []
  277. ) {
  278. return $this->__call( __FUNCTION__, func_get_args() );
  279. }
  280. public function selectRowCount(
  281. $tables, $vars = '*', $conds = '', $fname = __METHOD__, $options = [], $join_conds = []
  282. ) {
  283. return $this->__call( __FUNCTION__, func_get_args() );
  284. }
  285. public function lockForUpdate(
  286. $table, $conds = '', $fname = __METHOD__, $options = [], $join_conds = []
  287. ) {
  288. $this->assertRoleAllowsWrites();
  289. return $this->__call( __FUNCTION__, func_get_args() );
  290. }
  291. public function fieldExists( $table, $field, $fname = __METHOD__ ) {
  292. return $this->__call( __FUNCTION__, func_get_args() );
  293. }
  294. public function indexExists( $table, $index, $fname = __METHOD__ ) {
  295. return $this->__call( __FUNCTION__, func_get_args() );
  296. }
  297. public function tableExists( $table, $fname = __METHOD__ ) {
  298. return $this->__call( __FUNCTION__, func_get_args() );
  299. }
  300. public function insert( $table, $a, $fname = __METHOD__, $options = [] ) {
  301. $this->assertRoleAllowsWrites();
  302. return $this->__call( __FUNCTION__, func_get_args() );
  303. }
  304. public function update( $table, $values, $conds, $fname = __METHOD__, $options = [] ) {
  305. $this->assertRoleAllowsWrites();
  306. return $this->__call( __FUNCTION__, func_get_args() );
  307. }
  308. public function makeList( $a, $mode = self::LIST_COMMA ) {
  309. return $this->__call( __FUNCTION__, func_get_args() );
  310. }
  311. public function makeWhereFrom2d( $data, $baseKey, $subKey ) {
  312. return $this->__call( __FUNCTION__, func_get_args() );
  313. }
  314. public function aggregateValue( $valuedata, $valuename = 'value' ) {
  315. return $this->__call( __FUNCTION__, func_get_args() );
  316. }
  317. public function bitNot( $field ) {
  318. return $this->__call( __FUNCTION__, func_get_args() );
  319. }
  320. public function bitAnd( $fieldLeft, $fieldRight ) {
  321. return $this->__call( __FUNCTION__, func_get_args() );
  322. }
  323. public function bitOr( $fieldLeft, $fieldRight ) {
  324. return $this->__call( __FUNCTION__, func_get_args() );
  325. }
  326. public function buildConcat( $stringList ) {
  327. return $this->__call( __FUNCTION__, func_get_args() );
  328. }
  329. public function buildGroupConcatField(
  330. $delim, $table, $field, $conds = '', $join_conds = []
  331. ) {
  332. return $this->__call( __FUNCTION__, func_get_args() );
  333. }
  334. public function buildSubstring( $input, $startPosition, $length = null ) {
  335. return $this->__call( __FUNCTION__, func_get_args() );
  336. }
  337. public function buildStringCast( $field ) {
  338. return $this->__call( __FUNCTION__, func_get_args() );
  339. }
  340. public function buildIntegerCast( $field ) {
  341. return $this->__call( __FUNCTION__, func_get_args() );
  342. }
  343. public function buildSelectSubquery(
  344. $table, $vars, $conds = '', $fname = __METHOD__,
  345. $options = [], $join_conds = []
  346. ) {
  347. return $this->__call( __FUNCTION__, func_get_args() );
  348. }
  349. public function databasesAreIndependent() {
  350. return $this->__call( __FUNCTION__, func_get_args() );
  351. }
  352. public function selectDB( $db ) {
  353. // Disallow things that might confuse the LoadBalancer tracking
  354. throw new DBUnexpectedError( $this, "Database selection is disallowed to enable reuse." );
  355. }
  356. public function selectDomain( $domain ) {
  357. // Disallow things that might confuse the LoadBalancer tracking
  358. throw new DBUnexpectedError( $this, "Database selection is disallowed to enable reuse." );
  359. }
  360. public function getDBname() {
  361. if ( $this->conn === null ) {
  362. $domain = DatabaseDomain::newFromId( $this->params[self::FLD_DOMAIN] );
  363. // Avoid triggering a database connection
  364. return $domain->getDatabase();
  365. }
  366. return $this->__call( __FUNCTION__, func_get_args() );
  367. }
  368. public function getServer() {
  369. return $this->__call( __FUNCTION__, func_get_args() );
  370. }
  371. public function addQuotes( $s ) {
  372. return $this->__call( __FUNCTION__, func_get_args() );
  373. }
  374. public function addIdentifierQuotes( $s ) {
  375. return $this->__call( __FUNCTION__, func_get_args() );
  376. }
  377. public function buildLike( $param ) {
  378. return $this->__call( __FUNCTION__, func_get_args() );
  379. }
  380. public function anyChar() {
  381. return $this->__call( __FUNCTION__, func_get_args() );
  382. }
  383. public function anyString() {
  384. return $this->__call( __FUNCTION__, func_get_args() );
  385. }
  386. public function nextSequenceValue( $seqName ) {
  387. $this->assertRoleAllowsWrites();
  388. return $this->__call( __FUNCTION__, func_get_args() );
  389. }
  390. public function replace( $table, $uniqueIndexes, $rows, $fname = __METHOD__ ) {
  391. $this->assertRoleAllowsWrites();
  392. return $this->__call( __FUNCTION__, func_get_args() );
  393. }
  394. public function upsert(
  395. $table, array $rows, $uniqueIndexes, array $set, $fname = __METHOD__
  396. ) {
  397. $this->assertRoleAllowsWrites();
  398. return $this->__call( __FUNCTION__, func_get_args() );
  399. }
  400. public function deleteJoin(
  401. $delTable, $joinTable, $delVar, $joinVar, $conds, $fname = __METHOD__
  402. ) {
  403. $this->assertRoleAllowsWrites();
  404. return $this->__call( __FUNCTION__, func_get_args() );
  405. }
  406. public function delete( $table, $conds, $fname = __METHOD__ ) {
  407. $this->assertRoleAllowsWrites();
  408. return $this->__call( __FUNCTION__, func_get_args() );
  409. }
  410. public function insertSelect(
  411. $destTable, $srcTable, $varMap, $conds,
  412. $fname = __METHOD__, $insertOptions = [], $selectOptions = [], $selectJoinConds = []
  413. ) {
  414. $this->assertRoleAllowsWrites();
  415. return $this->__call( __FUNCTION__, func_get_args() );
  416. }
  417. public function unionSupportsOrderAndLimit() {
  418. return $this->__call( __FUNCTION__, func_get_args() );
  419. }
  420. public function unionQueries( $sqls, $all ) {
  421. return $this->__call( __FUNCTION__, func_get_args() );
  422. }
  423. public function unionConditionPermutations(
  424. $table, $vars, array $permute_conds, $extra_conds = '', $fname = __METHOD__,
  425. $options = [], $join_conds = []
  426. ) {
  427. return $this->__call( __FUNCTION__, func_get_args() );
  428. }
  429. public function conditional( $cond, $trueVal, $falseVal ) {
  430. return $this->__call( __FUNCTION__, func_get_args() );
  431. }
  432. public function strreplace( $orig, $old, $new ) {
  433. return $this->__call( __FUNCTION__, func_get_args() );
  434. }
  435. public function getServerUptime() {
  436. return $this->__call( __FUNCTION__, func_get_args() );
  437. }
  438. public function wasDeadlock() {
  439. return $this->__call( __FUNCTION__, func_get_args() );
  440. }
  441. public function wasLockTimeout() {
  442. return $this->__call( __FUNCTION__, func_get_args() );
  443. }
  444. public function wasConnectionLoss() {
  445. return $this->__call( __FUNCTION__, func_get_args() );
  446. }
  447. public function wasReadOnlyError() {
  448. return $this->__call( __FUNCTION__, func_get_args() );
  449. }
  450. public function wasErrorReissuable() {
  451. return $this->__call( __FUNCTION__, func_get_args() );
  452. }
  453. public function masterPosWait( DBMasterPos $pos, $timeout ) {
  454. return $this->__call( __FUNCTION__, func_get_args() );
  455. }
  456. public function getReplicaPos() {
  457. return $this->__call( __FUNCTION__, func_get_args() );
  458. }
  459. public function getMasterPos() {
  460. return $this->__call( __FUNCTION__, func_get_args() );
  461. }
  462. public function serverIsReadOnly() {
  463. return $this->__call( __FUNCTION__, func_get_args() );
  464. }
  465. public function onTransactionResolution( callable $callback, $fname = __METHOD__ ) {
  466. // DB_REPLICA role: caller might want to refresh cache after a REPEATABLE-READ snapshot
  467. return $this->__call( __FUNCTION__, func_get_args() );
  468. }
  469. public function onTransactionCommitOrIdle( callable $callback, $fname = __METHOD__ ) {
  470. // DB_REPLICA role: caller might want to refresh cache after a REPEATABLE-READ snapshot
  471. return $this->__call( __FUNCTION__, func_get_args() );
  472. }
  473. public function onTransactionIdle( callable $callback, $fname = __METHOD__ ) {
  474. return $this->onTransactionCommitOrIdle( $callback, $fname );
  475. }
  476. public function onTransactionPreCommitOrIdle( callable $callback, $fname = __METHOD__ ) {
  477. // DB_REPLICA role: caller might want to refresh cache after a cache mutex is released
  478. return $this->__call( __FUNCTION__, func_get_args() );
  479. }
  480. public function onAtomicSectionCancel( callable $callback, $fname = __METHOD__ ) {
  481. return $this->__call( __FUNCTION__, func_get_args() );
  482. }
  483. public function setTransactionListener( $name, callable $callback = null ) {
  484. return $this->__call( __FUNCTION__, func_get_args() );
  485. }
  486. public function startAtomic(
  487. $fname = __METHOD__, $cancelable = IDatabase::ATOMIC_NOT_CANCELABLE
  488. ) {
  489. // Don't call assertRoleAllowsWrites(); caller might want a REPEATABLE-READ snapshot
  490. return $this->__call( __FUNCTION__, func_get_args() );
  491. }
  492. public function endAtomic( $fname = __METHOD__ ) {
  493. // Don't call assertRoleAllowsWrites(); caller might want a REPEATABLE-READ snapshot
  494. return $this->__call( __FUNCTION__, func_get_args() );
  495. }
  496. public function cancelAtomic( $fname = __METHOD__, AtomicSectionIdentifier $sectionId = null ) {
  497. // Don't call assertRoleAllowsWrites(); caller might want a REPEATABLE-READ snapshot
  498. return $this->__call( __FUNCTION__, func_get_args() );
  499. }
  500. public function doAtomicSection(
  501. $fname, callable $callback, $cancelable = self::ATOMIC_NOT_CANCELABLE
  502. ) {
  503. // Don't call assertRoleAllowsWrites(); caller might want a REPEATABLE-READ snapshot
  504. return $this->__call( __FUNCTION__, func_get_args() );
  505. }
  506. public function begin( $fname = __METHOD__, $mode = IDatabase::TRANSACTION_EXPLICIT ) {
  507. return $this->__call( __FUNCTION__, func_get_args() );
  508. }
  509. public function commit( $fname = __METHOD__, $flush = self::FLUSHING_ONE ) {
  510. return $this->__call( __FUNCTION__, func_get_args() );
  511. }
  512. public function rollback( $fname = __METHOD__, $flush = self::FLUSHING_ONE ) {
  513. return $this->__call( __FUNCTION__, func_get_args() );
  514. }
  515. public function flushSnapshot( $fname = __METHOD__, $flush = self::FLUSHING_ONE ) {
  516. return $this->__call( __FUNCTION__, func_get_args() );
  517. }
  518. public function timestamp( $ts = 0 ) {
  519. return $this->__call( __FUNCTION__, func_get_args() );
  520. }
  521. public function timestampOrNull( $ts = null ) {
  522. return $this->__call( __FUNCTION__, func_get_args() );
  523. }
  524. public function ping( &$rtt = null ) {
  525. return func_num_args()
  526. ? $this->__call( __FUNCTION__, [ &$rtt ] )
  527. : $this->__call( __FUNCTION__, [] ); // method cares about null vs missing
  528. }
  529. public function getLag() {
  530. return $this->__call( __FUNCTION__, func_get_args() );
  531. }
  532. public function getSessionLagStatus() {
  533. return $this->__call( __FUNCTION__, func_get_args() );
  534. }
  535. public function maxListLen() {
  536. return $this->__call( __FUNCTION__, func_get_args() );
  537. }
  538. public function encodeBlob( $b ) {
  539. return $this->__call( __FUNCTION__, func_get_args() );
  540. }
  541. public function decodeBlob( $b ) {
  542. return $this->__call( __FUNCTION__, func_get_args() );
  543. }
  544. public function setSessionOptions( array $options ) {
  545. return $this->__call( __FUNCTION__, func_get_args() );
  546. }
  547. public function setSchemaVars( $vars ) {
  548. return $this->__call( __FUNCTION__, func_get_args() );
  549. }
  550. public function lockIsFree( $lockName, $method ) {
  551. $this->assertRoleAllowsWrites();
  552. return $this->__call( __FUNCTION__, func_get_args() );
  553. }
  554. public function lock( $lockName, $method, $timeout = 5 ) {
  555. $this->assertRoleAllowsWrites();
  556. return $this->__call( __FUNCTION__, func_get_args() );
  557. }
  558. public function unlock( $lockName, $method ) {
  559. $this->assertRoleAllowsWrites();
  560. return $this->__call( __FUNCTION__, func_get_args() );
  561. }
  562. public function getScopedLockAndFlush( $lockKey, $fname, $timeout ) {
  563. $this->assertRoleAllowsWrites();
  564. return $this->__call( __FUNCTION__, func_get_args() );
  565. }
  566. public function namedLocksEnqueue() {
  567. return $this->__call( __FUNCTION__, func_get_args() );
  568. }
  569. public function getInfinity() {
  570. return $this->__call( __FUNCTION__, func_get_args() );
  571. }
  572. public function encodeExpiry( $expiry ) {
  573. return $this->__call( __FUNCTION__, func_get_args() );
  574. }
  575. public function decodeExpiry( $expiry, $format = TS_MW ) {
  576. return $this->__call( __FUNCTION__, func_get_args() );
  577. }
  578. public function setBigSelects( $value = true ) {
  579. return $this->__call( __FUNCTION__, func_get_args() );
  580. }
  581. public function isReadOnly() {
  582. return $this->__call( __FUNCTION__, func_get_args() );
  583. }
  584. public function setTableAliases( array $aliases ) {
  585. return $this->__call( __FUNCTION__, func_get_args() );
  586. }
  587. public function setIndexAliases( array $aliases ) {
  588. return $this->__call( __FUNCTION__, func_get_args() );
  589. }
  590. public function __toString() {
  591. if ( $this->conn === null ) {
  592. // spl_object_id is PHP >= 7.2
  593. $id = function_exists( 'spl_object_id' )
  594. ? spl_object_id( $this )
  595. : spl_object_hash( $this );
  596. return $this->getType() . ' object #' . $id;
  597. }
  598. return $this->__call( __FUNCTION__, func_get_args() );
  599. }
  600. /**
  601. * Error out if the role is not DB_MASTER
  602. *
  603. * Note that the underlying connection may or may not itself be read-only.
  604. * It could even be to a writable master (both server-side and to the application).
  605. * This error is meant for the case when a DB_REPLICA handle was requested but a
  606. * a write was attempted on that handle regardless.
  607. *
  608. * In configurations where the master DB has some generic read load or is the only server,
  609. * DB_MASTER/DB_REPLICA will sometimes (or always) use the same connection to the master DB.
  610. * This does not effect the role of DBConnRef instances.
  611. * @throws DBReadOnlyRoleError
  612. */
  613. protected function assertRoleAllowsWrites() {
  614. // DB_MASTER is "prima facie" writable
  615. if ( $this->role !== ILoadBalancer::DB_MASTER ) {
  616. throw new DBReadOnlyRoleError( $this->conn, "Cannot write with role DB_REPLICA" );
  617. }
  618. }
  619. /**
  620. * Clean up the connection when out of scope
  621. */
  622. function __destruct() {
  623. if ( $this->conn ) {
  624. $this->lb->reuseConnection( $this->conn );
  625. }
  626. }
  627. }
  628. /**
  629. * @since 1.22
  630. * @deprecated since 1.29
  631. */
  632. class_alias( DBConnRef::class, 'DBConnRef' );