Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like DatabaseBase often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use DatabaseBase, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 32 | abstract class DatabaseBase implements IDatabase { |
||
| 33 | /** Number of times to re-try an operation in case of deadlock */ |
||
| 34 | const DEADLOCK_TRIES = 4; |
||
| 35 | |||
| 36 | /** Minimum time to wait before retry, in microseconds */ |
||
| 37 | const DEADLOCK_DELAY_MIN = 500000; |
||
| 38 | |||
| 39 | /** Maximum time to wait before retry */ |
||
| 40 | const DEADLOCK_DELAY_MAX = 1500000; |
||
| 41 | |||
| 42 | protected $mLastQuery = ''; |
||
| 43 | protected $mDoneWrites = false; |
||
| 44 | protected $mPHPError = false; |
||
| 45 | |||
| 46 | protected $mServer, $mUser, $mPassword, $mDBname; |
||
|
|
|||
| 47 | |||
| 48 | /** @var BagOStuff APC cache */ |
||
| 49 | protected $srvCache; |
||
| 50 | |||
| 51 | /** @var resource Database connection */ |
||
| 52 | protected $mConn = null; |
||
| 53 | protected $mOpened = false; |
||
| 54 | |||
| 55 | /** @var array[] List of (callable, method name) */ |
||
| 56 | protected $mTrxIdleCallbacks = []; |
||
| 57 | /** @var array[] List of (callable, method name) */ |
||
| 58 | protected $mTrxPreCommitCallbacks = []; |
||
| 59 | /** @var array[] List of (callable, method name) */ |
||
| 60 | protected $mTrxEndCallbacks = []; |
||
| 61 | /** @var bool Whether to suppress triggering of post-commit callbacks */ |
||
| 62 | protected $suppressPostCommitCallbacks = false; |
||
| 63 | |||
| 64 | protected $mTablePrefix; |
||
| 65 | protected $mSchema; |
||
| 66 | protected $mFlags; |
||
| 67 | protected $mForeign; |
||
| 68 | protected $mLBInfo = []; |
||
| 69 | protected $mDefaultBigSelects = null; |
||
| 70 | protected $mSchemaVars = false; |
||
| 71 | /** @var array */ |
||
| 72 | protected $mSessionVars = []; |
||
| 73 | |||
| 74 | protected $preparedArgs; |
||
| 75 | |||
| 76 | protected $htmlErrors; |
||
| 77 | |||
| 78 | protected $delimiter = ';'; |
||
| 79 | |||
| 80 | /** |
||
| 81 | * Either 1 if a transaction is active or 0 otherwise. |
||
| 82 | * The other Trx fields may not be meaningfull if this is 0. |
||
| 83 | * |
||
| 84 | * @var int |
||
| 85 | */ |
||
| 86 | protected $mTrxLevel = 0; |
||
| 87 | |||
| 88 | /** |
||
| 89 | * Either a short hexidecimal string if a transaction is active or "" |
||
| 90 | * |
||
| 91 | * @var string |
||
| 92 | * @see DatabaseBase::mTrxLevel |
||
| 93 | */ |
||
| 94 | protected $mTrxShortId = ''; |
||
| 95 | |||
| 96 | /** |
||
| 97 | * The UNIX time that the transaction started. Callers can assume that if |
||
| 98 | * snapshot isolation is used, then the data is *at least* up to date to that |
||
| 99 | * point (possibly more up-to-date since the first SELECT defines the snapshot). |
||
| 100 | * |
||
| 101 | * @var float|null |
||
| 102 | * @see DatabaseBase::mTrxLevel |
||
| 103 | */ |
||
| 104 | private $mTrxTimestamp = null; |
||
| 105 | |||
| 106 | /** @var float Lag estimate at the time of BEGIN */ |
||
| 107 | private $mTrxSlaveLag = null; |
||
| 108 | |||
| 109 | /** |
||
| 110 | * Remembers the function name given for starting the most recent transaction via begin(). |
||
| 111 | * Used to provide additional context for error reporting. |
||
| 112 | * |
||
| 113 | * @var string |
||
| 114 | * @see DatabaseBase::mTrxLevel |
||
| 115 | */ |
||
| 116 | private $mTrxFname = null; |
||
| 117 | |||
| 118 | /** |
||
| 119 | * Record if possible write queries were done in the last transaction started |
||
| 120 | * |
||
| 121 | * @var bool |
||
| 122 | * @see DatabaseBase::mTrxLevel |
||
| 123 | */ |
||
| 124 | private $mTrxDoneWrites = false; |
||
| 125 | |||
| 126 | /** |
||
| 127 | * Record if the current transaction was started implicitly due to DBO_TRX being set. |
||
| 128 | * |
||
| 129 | * @var bool |
||
| 130 | * @see DatabaseBase::mTrxLevel |
||
| 131 | */ |
||
| 132 | private $mTrxAutomatic = false; |
||
| 133 | |||
| 134 | /** |
||
| 135 | * Array of levels of atomicity within transactions |
||
| 136 | * |
||
| 137 | * @var array |
||
| 138 | */ |
||
| 139 | private $mTrxAtomicLevels = []; |
||
| 140 | |||
| 141 | /** |
||
| 142 | * Record if the current transaction was started implicitly by DatabaseBase::startAtomic |
||
| 143 | * |
||
| 144 | * @var bool |
||
| 145 | */ |
||
| 146 | private $mTrxAutomaticAtomic = false; |
||
| 147 | |||
| 148 | /** |
||
| 149 | * Track the write query callers of the current transaction |
||
| 150 | * |
||
| 151 | * @var string[] |
||
| 152 | */ |
||
| 153 | private $mTrxWriteCallers = []; |
||
| 154 | |||
| 155 | /** |
||
| 156 | * Track the seconds spent in write queries for the current transaction |
||
| 157 | * |
||
| 158 | * @var float |
||
| 159 | */ |
||
| 160 | private $mTrxWriteDuration = 0.0; |
||
| 161 | |||
| 162 | /** @var array Map of (name => 1) for locks obtained via lock() */ |
||
| 163 | private $mNamedLocksHeld = []; |
||
| 164 | |||
| 165 | /** @var IDatabase|null Lazy handle to the master DB this server replicates from */ |
||
| 166 | private $lazyMasterHandle; |
||
| 167 | |||
| 168 | /** |
||
| 169 | * @since 1.21 |
||
| 170 | * @var resource File handle for upgrade |
||
| 171 | */ |
||
| 172 | protected $fileHandle = null; |
||
| 173 | |||
| 174 | /** |
||
| 175 | * @since 1.22 |
||
| 176 | * @var string[] Process cache of VIEWs names in the database |
||
| 177 | */ |
||
| 178 | protected $allViews = null; |
||
| 179 | |||
| 180 | /** @var TransactionProfiler */ |
||
| 181 | protected $trxProfiler; |
||
| 182 | |||
| 183 | public function getServerInfo() { |
||
| 186 | |||
| 187 | /** |
||
| 188 | * @return string Command delimiter used by this database engine |
||
| 189 | */ |
||
| 190 | public function getDelimiter() { |
||
| 193 | |||
| 194 | /** |
||
| 195 | * Boolean, controls output of large amounts of debug information. |
||
| 196 | * @param bool|null $debug |
||
| 197 | * - true to enable debugging |
||
| 198 | * - false to disable debugging |
||
| 199 | * - omitted or null to do nothing |
||
| 200 | * |
||
| 201 | * @return bool|null Previous value of the flag |
||
| 202 | */ |
||
| 203 | public function debug( $debug = null ) { |
||
| 206 | |||
| 207 | public function bufferResults( $buffer = null ) { |
||
| 214 | |||
| 215 | /** |
||
| 216 | * Turns on (false) or off (true) the automatic generation and sending |
||
| 217 | * of a "we're sorry, but there has been a database error" page on |
||
| 218 | * database errors. Default is on (false). When turned off, the |
||
| 219 | * code should use lastErrno() and lastError() to handle the |
||
| 220 | * situation as appropriate. |
||
| 221 | * |
||
| 222 | * Do not use this function outside of the Database classes. |
||
| 223 | * |
||
| 224 | * @param null|bool $ignoreErrors |
||
| 225 | * @return bool The previous value of the flag. |
||
| 226 | */ |
||
| 227 | protected function ignoreErrors( $ignoreErrors = null ) { |
||
| 230 | |||
| 231 | public function trxLevel() { |
||
| 234 | |||
| 235 | public function trxTimestamp() { |
||
| 238 | |||
| 239 | public function tablePrefix( $prefix = null ) { |
||
| 242 | |||
| 243 | public function dbSchema( $schema = null ) { |
||
| 246 | |||
| 247 | /** |
||
| 248 | * Set the filehandle to copy write statements to. |
||
| 249 | * |
||
| 250 | * @param resource $fh File handle |
||
| 251 | */ |
||
| 252 | public function setFileHandle( $fh ) { |
||
| 255 | |||
| 256 | public function getLBInfo( $name = null ) { |
||
| 267 | |||
| 268 | public function setLBInfo( $name, $value = null ) { |
||
| 275 | |||
| 276 | /** |
||
| 277 | * Set a lazy-connecting DB handle to the master DB (for replication status purposes) |
||
| 278 | * |
||
| 279 | * @param IDatabase $conn |
||
| 280 | * @since 1.27 |
||
| 281 | */ |
||
| 282 | public function setLazyMasterHandle( IDatabase $conn ) { |
||
| 285 | |||
| 286 | /** |
||
| 287 | * @return IDatabase|null |
||
| 288 | * @see setLazyMasterHandle() |
||
| 289 | * @since 1.27 |
||
| 290 | */ |
||
| 291 | public function getLazyMasterHandle() { |
||
| 294 | |||
| 295 | /** |
||
| 296 | * @return TransactionProfiler |
||
| 297 | */ |
||
| 298 | protected function getTransactionProfiler() { |
||
| 305 | |||
| 306 | /** |
||
| 307 | * @param TransactionProfiler $profiler |
||
| 308 | * @since 1.27 |
||
| 309 | */ |
||
| 310 | public function setTransactionProfiler( TransactionProfiler $profiler ) { |
||
| 313 | |||
| 314 | /** |
||
| 315 | * Returns true if this database supports (and uses) cascading deletes |
||
| 316 | * |
||
| 317 | * @return bool |
||
| 318 | */ |
||
| 319 | public function cascadingDeletes() { |
||
| 322 | |||
| 323 | /** |
||
| 324 | * Returns true if this database supports (and uses) triggers (e.g. on the page table) |
||
| 325 | * |
||
| 326 | * @return bool |
||
| 327 | */ |
||
| 328 | public function cleanupTriggers() { |
||
| 331 | |||
| 332 | /** |
||
| 333 | * Returns true if this database is strict about what can be put into an IP field. |
||
| 334 | * Specifically, it uses a NULL value instead of an empty string. |
||
| 335 | * |
||
| 336 | * @return bool |
||
| 337 | */ |
||
| 338 | public function strictIPs() { |
||
| 341 | |||
| 342 | /** |
||
| 343 | * Returns true if this database uses timestamps rather than integers |
||
| 344 | * |
||
| 345 | * @return bool |
||
| 346 | */ |
||
| 347 | public function realTimestamps() { |
||
| 350 | |||
| 351 | public function implicitGroupby() { |
||
| 354 | |||
| 355 | public function implicitOrderby() { |
||
| 358 | |||
| 359 | /** |
||
| 360 | * Returns true if this database can do a native search on IP columns |
||
| 361 | * e.g. this works as expected: .. WHERE rc_ip = '127.42.12.102/32'; |
||
| 362 | * |
||
| 363 | * @return bool |
||
| 364 | */ |
||
| 365 | public function searchableIPs() { |
||
| 368 | |||
| 369 | /** |
||
| 370 | * Returns true if this database can use functional indexes |
||
| 371 | * |
||
| 372 | * @return bool |
||
| 373 | */ |
||
| 374 | public function functionalIndexes() { |
||
| 377 | |||
| 378 | public function lastQuery() { |
||
| 381 | |||
| 382 | public function doneWrites() { |
||
| 385 | |||
| 386 | public function lastDoneWrites() { |
||
| 389 | |||
| 390 | public function writesPending() { |
||
| 393 | |||
| 394 | public function writesOrCallbacksPending() { |
||
| 399 | |||
| 400 | public function pendingWriteQueryDuration() { |
||
| 403 | |||
| 404 | public function pendingWriteCallers() { |
||
| 407 | |||
| 408 | public function isOpen() { |
||
| 411 | |||
| 412 | public function setFlag( $flag ) { |
||
| 415 | |||
| 416 | public function clearFlag( $flag ) { |
||
| 419 | |||
| 420 | public function getFlag( $flag ) { |
||
| 423 | |||
| 424 | public function getProperty( $name ) { |
||
| 427 | |||
| 428 | public function getWikiID() { |
||
| 435 | |||
| 436 | /** |
||
| 437 | * Return a path to the DBMS-specific SQL file if it exists, |
||
| 438 | * otherwise default SQL file |
||
| 439 | * |
||
| 440 | * @param string $filename |
||
| 441 | * @return string |
||
| 442 | */ |
||
| 443 | View Code Duplication | private function getSqlFilePath( $filename ) { |
|
| 452 | |||
| 453 | /** |
||
| 454 | * Return a path to the DBMS-specific schema file, |
||
| 455 | * otherwise default to tables.sql |
||
| 456 | * |
||
| 457 | * @return string |
||
| 458 | */ |
||
| 459 | public function getSchemaPath() { |
||
| 462 | |||
| 463 | /** |
||
| 464 | * Return a path to the DBMS-specific update key file, |
||
| 465 | * otherwise default to update-keys.sql |
||
| 466 | * |
||
| 467 | * @return string |
||
| 468 | */ |
||
| 469 | public function getUpdateKeysPath() { |
||
| 472 | |||
| 473 | /** |
||
| 474 | * Get information about an index into an object |
||
| 475 | * @param string $table Table name |
||
| 476 | * @param string $index Index name |
||
| 477 | * @param string $fname Calling function name |
||
| 478 | * @return mixed Database-specific index description class or false if the index does not exist |
||
| 479 | */ |
||
| 480 | abstract function indexInfo( $table, $index, $fname = __METHOD__ ); |
||
| 481 | |||
| 482 | /** |
||
| 483 | * Wrapper for addslashes() |
||
| 484 | * |
||
| 485 | * @param string $s String to be slashed. |
||
| 486 | * @return string Slashed string. |
||
| 487 | */ |
||
| 488 | abstract function strencode( $s ); |
||
| 489 | |||
| 490 | /** |
||
| 491 | * Constructor. |
||
| 492 | * |
||
| 493 | * FIXME: It is possible to construct a Database object with no associated |
||
| 494 | * connection object, by specifying no parameters to __construct(). This |
||
| 495 | * feature is deprecated and should be removed. |
||
| 496 | * |
||
| 497 | * DatabaseBase subclasses should not be constructed directly in external |
||
| 498 | * code. DatabaseBase::factory() should be used instead. |
||
| 499 | * |
||
| 500 | * @param array $params Parameters passed from DatabaseBase::factory() |
||
| 501 | */ |
||
| 502 | function __construct( array $params ) { |
||
| 551 | |||
| 552 | /** |
||
| 553 | * Called by serialize. Throw an exception when DB connection is serialized. |
||
| 554 | * This causes problems on some database engines because the connection is |
||
| 555 | * not restored on unserialize. |
||
| 556 | */ |
||
| 557 | public function __sleep() { |
||
| 561 | |||
| 562 | /** |
||
| 563 | * Given a DB type, construct the name of the appropriate child class of |
||
| 564 | * DatabaseBase. This is designed to replace all of the manual stuff like: |
||
| 565 | * $class = 'Database' . ucfirst( strtolower( $dbType ) ); |
||
| 566 | * as well as validate against the canonical list of DB types we have |
||
| 567 | * |
||
| 568 | * This factory function is mostly useful for when you need to connect to a |
||
| 569 | * database other than the MediaWiki default (such as for external auth, |
||
| 570 | * an extension, et cetera). Do not use this to connect to the MediaWiki |
||
| 571 | * database. Example uses in core: |
||
| 572 | * @see LoadBalancer::reallyOpenConnection() |
||
| 573 | * @see ForeignDBRepo::getMasterDB() |
||
| 574 | * @see WebInstallerDBConnect::execute() |
||
| 575 | * |
||
| 576 | * @since 1.18 |
||
| 577 | * |
||
| 578 | * @param string $dbType A possible DB type |
||
| 579 | * @param array $p An array of options to pass to the constructor. |
||
| 580 | * Valid options are: host, user, password, dbname, flags, tablePrefix, schema, driver |
||
| 581 | * @throws MWException If the database driver or extension cannot be found |
||
| 582 | * @return DatabaseBase|null DatabaseBase subclass or null |
||
| 583 | */ |
||
| 584 | final public static function factory( $dbType, $p = [] ) { |
||
| 648 | |||
| 649 | protected function installErrorHandler() { |
||
| 654 | |||
| 655 | /** |
||
| 656 | * @return bool|string |
||
| 657 | */ |
||
| 658 | protected function restoreErrorHandler() { |
||
| 672 | |||
| 673 | /** |
||
| 674 | * @param int $errno |
||
| 675 | * @param string $errstr |
||
| 676 | */ |
||
| 677 | public function connectionErrorHandler( $errno, $errstr ) { |
||
| 680 | |||
| 681 | /** |
||
| 682 | * Create a log context to pass to wfLogDBError or other logging functions. |
||
| 683 | * |
||
| 684 | * @param array $extras Additional data to add to context |
||
| 685 | * @return array |
||
| 686 | */ |
||
| 687 | protected function getLogContext( array $extras = [] ) { |
||
| 697 | |||
| 698 | public function close() { |
||
| 720 | |||
| 721 | /** |
||
| 722 | * Make sure isOpen() returns true as a sanity check |
||
| 723 | * |
||
| 724 | * @throws DBUnexpectedError |
||
| 725 | */ |
||
| 726 | protected function assertOpen() { |
||
| 731 | |||
| 732 | /** |
||
| 733 | * Closes underlying database connection |
||
| 734 | * @since 1.20 |
||
| 735 | * @return bool Whether connection was closed successfully |
||
| 736 | */ |
||
| 737 | abstract protected function closeConnection(); |
||
| 738 | |||
| 739 | function reportConnectionError( $error = 'Unknown error' ) { |
||
| 748 | |||
| 749 | /** |
||
| 750 | * The DBMS-dependent part of query() |
||
| 751 | * |
||
| 752 | * @param string $sql SQL query. |
||
| 753 | * @return ResultWrapper|bool Result object to feed to fetchObject, |
||
| 754 | * fetchRow, ...; or false on failure |
||
| 755 | */ |
||
| 756 | abstract protected function doQuery( $sql ); |
||
| 757 | |||
| 758 | /** |
||
| 759 | * Determine whether a query writes to the DB. |
||
| 760 | * Should return true if unsure. |
||
| 761 | * |
||
| 762 | * @param string $sql |
||
| 763 | * @return bool |
||
| 764 | */ |
||
| 765 | protected function isWriteQuery( $sql ) { |
||
| 768 | |||
| 769 | /** |
||
| 770 | * Determine whether a SQL statement is sensitive to isolation level. |
||
| 771 | * A SQL statement is considered transactable if its result could vary |
||
| 772 | * depending on the transaction isolation level. Operational commands |
||
| 773 | * such as 'SET' and 'SHOW' are not considered to be transactable. |
||
| 774 | * |
||
| 775 | * @param string $sql |
||
| 776 | * @return bool |
||
| 777 | */ |
||
| 778 | protected function isTransactableQuery( $sql ) { |
||
| 782 | |||
| 783 | public function query( $sql, $fname = __METHOD__, $tempIgnore = false ) { |
||
| 920 | |||
| 921 | public function reportQueryError( $error, $errno, $sql, $fname, $tempIgnore = false ) { |
||
| 940 | |||
| 941 | /** |
||
| 942 | * Intended to be compatible with the PEAR::DB wrapper functions. |
||
| 943 | * http://pear.php.net/manual/en/package.database.db.intro-execute.php |
||
| 944 | * |
||
| 945 | * ? = scalar value, quoted as necessary |
||
| 946 | * ! = raw SQL bit (a function for instance) |
||
| 947 | * & = filename; reads the file and inserts as a blob |
||
| 948 | * (we don't use this though...) |
||
| 949 | * |
||
| 950 | * @param string $sql |
||
| 951 | * @param string $func |
||
| 952 | * |
||
| 953 | * @return array |
||
| 954 | */ |
||
| 955 | protected function prepare( $sql, $func = 'DatabaseBase::prepare' ) { |
||
| 962 | |||
| 963 | /** |
||
| 964 | * Free a prepared query, generated by prepare(). |
||
| 965 | * @param string $prepared |
||
| 966 | */ |
||
| 967 | protected function freePrepared( $prepared ) { |
||
| 970 | |||
| 971 | /** |
||
| 972 | * Execute a prepared query with the various arguments |
||
| 973 | * @param string $prepared The prepared sql |
||
| 974 | * @param mixed $args Either an array here, or put scalars as varargs |
||
| 975 | * |
||
| 976 | * @return ResultWrapper |
||
| 977 | */ |
||
| 978 | public function execute( $prepared, $args = null ) { |
||
| 989 | |||
| 990 | /** |
||
| 991 | * For faking prepared SQL statements on DBs that don't support it directly. |
||
| 992 | * |
||
| 993 | * @param string $preparedQuery A 'preparable' SQL statement |
||
| 994 | * @param array $args Array of Arguments to fill it with |
||
| 995 | * @return string Executable SQL |
||
| 996 | */ |
||
| 997 | public function fillPrepared( $preparedQuery, $args ) { |
||
| 1004 | |||
| 1005 | /** |
||
| 1006 | * preg_callback func for fillPrepared() |
||
| 1007 | * The arguments should be in $this->preparedArgs and must not be touched |
||
| 1008 | * while we're doing this. |
||
| 1009 | * |
||
| 1010 | * @param array $matches |
||
| 1011 | * @throws DBUnexpectedError |
||
| 1012 | * @return string |
||
| 1013 | */ |
||
| 1014 | protected function fillPreparedArg( $matches ) { |
||
| 1044 | |||
| 1045 | public function freeResult( $res ) { |
||
| 1047 | |||
| 1048 | public function selectField( |
||
| 1074 | |||
| 1075 | public function selectFieldValues( |
||
| 1100 | |||
| 1101 | /** |
||
| 1102 | * Returns an optional USE INDEX clause to go after the table, and a |
||
| 1103 | * string to go at the end of the query. |
||
| 1104 | * |
||
| 1105 | * @param array $options Associative array of options to be turned into |
||
| 1106 | * an SQL query, valid keys are listed in the function. |
||
| 1107 | * @return array |
||
| 1108 | * @see DatabaseBase::select() |
||
| 1109 | */ |
||
| 1110 | public function makeSelectOptions( $options ) { |
||
| 1185 | |||
| 1186 | /** |
||
| 1187 | * Returns an optional GROUP BY with an optional HAVING |
||
| 1188 | * |
||
| 1189 | * @param array $options Associative array of options |
||
| 1190 | * @return string |
||
| 1191 | * @see DatabaseBase::select() |
||
| 1192 | * @since 1.21 |
||
| 1193 | */ |
||
| 1194 | public function makeGroupByWithHaving( $options ) { |
||
| 1211 | |||
| 1212 | /** |
||
| 1213 | * Returns an optional ORDER BY |
||
| 1214 | * |
||
| 1215 | * @param array $options Associative array of options |
||
| 1216 | * @return string |
||
| 1217 | * @see DatabaseBase::select() |
||
| 1218 | * @since 1.21 |
||
| 1219 | */ |
||
| 1220 | public function makeOrderBy( $options ) { |
||
| 1231 | |||
| 1232 | // See IDatabase::select for the docs for this function |
||
| 1233 | public function select( $table, $vars, $conds = '', $fname = __METHOD__, |
||
| 1239 | |||
| 1240 | public function selectSQLText( $table, $vars, $conds = '', $fname = __METHOD__, |
||
| 1290 | |||
| 1291 | public function selectRow( $table, $vars, $conds, $fname = __METHOD__, |
||
| 1310 | |||
| 1311 | public function estimateRowCount( |
||
| 1324 | |||
| 1325 | public function selectRowCount( |
||
| 1339 | |||
| 1340 | /** |
||
| 1341 | * Removes most variables from an SQL query and replaces them with X or N for numbers. |
||
| 1342 | * It's only slightly flawed. Don't use for anything important. |
||
| 1343 | * |
||
| 1344 | * @param string $sql A SQL Query |
||
| 1345 | * |
||
| 1346 | * @return string |
||
| 1347 | */ |
||
| 1348 | protected static function generalizeSQL( $sql ) { |
||
| 1369 | |||
| 1370 | public function fieldExists( $table, $field, $fname = __METHOD__ ) { |
||
| 1375 | |||
| 1376 | public function indexExists( $table, $index, $fname = __METHOD__ ) { |
||
| 1388 | |||
| 1389 | public function tableExists( $table, $fname = __METHOD__ ) { |
||
| 1397 | |||
| 1398 | public function indexUnique( $table, $index ) { |
||
| 1407 | |||
| 1408 | /** |
||
| 1409 | * Helper for DatabaseBase::insert(). |
||
| 1410 | * |
||
| 1411 | * @param array $options |
||
| 1412 | * @return string |
||
| 1413 | */ |
||
| 1414 | protected function makeInsertOptions( $options ) { |
||
| 1417 | |||
| 1418 | public function insert( $table, $a, $fname = __METHOD__, $options = [] ) { |
||
| 1469 | |||
| 1470 | /** |
||
| 1471 | * Make UPDATE options array for DatabaseBase::makeUpdateOptions |
||
| 1472 | * |
||
| 1473 | * @param array $options |
||
| 1474 | * @return array |
||
| 1475 | */ |
||
| 1476 | protected function makeUpdateOptionsArray( $options ) { |
||
| 1493 | |||
| 1494 | /** |
||
| 1495 | * Make UPDATE options for the DatabaseBase::update function |
||
| 1496 | * |
||
| 1497 | * @param array $options The options passed to DatabaseBase::update |
||
| 1498 | * @return string |
||
| 1499 | */ |
||
| 1500 | protected function makeUpdateOptions( $options ) { |
||
| 1505 | |||
| 1506 | function update( $table, $values, $conds, $fname = __METHOD__, $options = [] ) { |
||
| 1517 | |||
| 1518 | public function makeList( $a, $mode = LIST_COMMA ) { |
||
| 1592 | |||
| 1593 | public function makeWhereFrom2d( $data, $baseKey, $subKey ) { |
||
| 1611 | |||
| 1612 | /** |
||
| 1613 | * Return aggregated value alias |
||
| 1614 | * |
||
| 1615 | * @param array $valuedata |
||
| 1616 | * @param string $valuename |
||
| 1617 | * |
||
| 1618 | * @return string |
||
| 1619 | */ |
||
| 1620 | public function aggregateValue( $valuedata, $valuename = 'value' ) { |
||
| 1623 | |||
| 1624 | public function bitNot( $field ) { |
||
| 1627 | |||
| 1628 | public function bitAnd( $fieldLeft, $fieldRight ) { |
||
| 1631 | |||
| 1632 | public function bitOr( $fieldLeft, $fieldRight ) { |
||
| 1635 | |||
| 1636 | public function buildConcat( $stringList ) { |
||
| 1639 | |||
| 1640 | View Code Duplication | public function buildGroupConcatField( |
|
| 1647 | |||
| 1648 | public function selectDB( $db ) { |
||
| 1656 | |||
| 1657 | public function getDBname() { |
||
| 1660 | |||
| 1661 | public function getServer() { |
||
| 1664 | |||
| 1665 | /** |
||
| 1666 | * Format a table name ready for use in constructing an SQL query |
||
| 1667 | * |
||
| 1668 | * This does two important things: it quotes the table names to clean them up, |
||
| 1669 | * and it adds a table prefix if only given a table name with no quotes. |
||
| 1670 | * |
||
| 1671 | * All functions of this object which require a table name call this function |
||
| 1672 | * themselves. Pass the canonical name to such functions. This is only needed |
||
| 1673 | * when calling query() directly. |
||
| 1674 | * |
||
| 1675 | * @note This function does not sanitize user input. It is not safe to use |
||
| 1676 | * this function to escape user input. |
||
| 1677 | * @param string $name Database table name |
||
| 1678 | * @param string $format One of: |
||
| 1679 | * quoted - Automatically pass the table name through addIdentifierQuotes() |
||
| 1680 | * so that it can be used in a query. |
||
| 1681 | * raw - Do not add identifier quotes to the table name |
||
| 1682 | * @return string Full database name |
||
| 1683 | */ |
||
| 1684 | public function tableName( $name, $format = 'quoted' ) { |
||
| 1762 | |||
| 1763 | /** |
||
| 1764 | * Fetch a number of table names into an array |
||
| 1765 | * This is handy when you need to construct SQL for joins |
||
| 1766 | * |
||
| 1767 | * Example: |
||
| 1768 | * extract( $dbr->tableNames( 'user', 'watchlist' ) ); |
||
| 1769 | * $sql = "SELECT wl_namespace,wl_title FROM $watchlist,$user |
||
| 1770 | * WHERE wl_user=user_id AND wl_user=$nameWithQuotes"; |
||
| 1771 | * |
||
| 1772 | * @return array |
||
| 1773 | */ |
||
| 1774 | View Code Duplication | public function tableNames() { |
|
| 1784 | |||
| 1785 | /** |
||
| 1786 | * Fetch a number of table names into an zero-indexed numerical array |
||
| 1787 | * This is handy when you need to construct SQL for joins |
||
| 1788 | * |
||
| 1789 | * Example: |
||
| 1790 | * list( $user, $watchlist ) = $dbr->tableNamesN( 'user', 'watchlist' ); |
||
| 1791 | * $sql = "SELECT wl_namespace,wl_title FROM $watchlist,$user |
||
| 1792 | * WHERE wl_user=user_id AND wl_user=$nameWithQuotes"; |
||
| 1793 | * |
||
| 1794 | * @return array |
||
| 1795 | */ |
||
| 1796 | View Code Duplication | public function tableNamesN() { |
|
| 1806 | |||
| 1807 | /** |
||
| 1808 | * Get an aliased table name |
||
| 1809 | * e.g. tableName AS newTableName |
||
| 1810 | * |
||
| 1811 | * @param string $name Table name, see tableName() |
||
| 1812 | * @param string|bool $alias Alias (optional) |
||
| 1813 | * @return string SQL name for aliased table. Will not alias a table to its own name |
||
| 1814 | */ |
||
| 1815 | public function tableNameWithAlias( $name, $alias = false ) { |
||
| 1822 | |||
| 1823 | /** |
||
| 1824 | * Gets an array of aliased table names |
||
| 1825 | * |
||
| 1826 | * @param array $tables Array( [alias] => table ) |
||
| 1827 | * @return string[] See tableNameWithAlias() |
||
| 1828 | */ |
||
| 1829 | public function tableNamesWithAlias( $tables ) { |
||
| 1840 | |||
| 1841 | /** |
||
| 1842 | * Get an aliased field name |
||
| 1843 | * e.g. fieldName AS newFieldName |
||
| 1844 | * |
||
| 1845 | * @param string $name Field name |
||
| 1846 | * @param string|bool $alias Alias (optional) |
||
| 1847 | * @return string SQL name for aliased field. Will not alias a field to its own name |
||
| 1848 | */ |
||
| 1849 | public function fieldNameWithAlias( $name, $alias = false ) { |
||
| 1856 | |||
| 1857 | /** |
||
| 1858 | * Gets an array of aliased field names |
||
| 1859 | * |
||
| 1860 | * @param array $fields Array( [alias] => field ) |
||
| 1861 | * @return string[] See fieldNameWithAlias() |
||
| 1862 | */ |
||
| 1863 | public function fieldNamesWithAlias( $fields ) { |
||
| 1874 | |||
| 1875 | /** |
||
| 1876 | * Get the aliased table name clause for a FROM clause |
||
| 1877 | * which might have a JOIN and/or USE INDEX clause |
||
| 1878 | * |
||
| 1879 | * @param array $tables ( [alias] => table ) |
||
| 1880 | * @param array $use_index Same as for select() |
||
| 1881 | * @param array $join_conds Same as for select() |
||
| 1882 | * @return string |
||
| 1883 | */ |
||
| 1884 | protected function tableNamesWithUseIndexOrJOIN( |
||
| 1936 | |||
| 1937 | /** |
||
| 1938 | * Get the name of an index in a given table. |
||
| 1939 | * |
||
| 1940 | * @param string $index |
||
| 1941 | * @return string |
||
| 1942 | */ |
||
| 1943 | protected function indexName( $index ) { |
||
| 1957 | |||
| 1958 | public function addQuotes( $s ) { |
||
| 1972 | |||
| 1973 | /** |
||
| 1974 | * Quotes an identifier using `backticks` or "double quotes" depending on the database type. |
||
| 1975 | * MySQL uses `backticks` while basically everything else uses double quotes. |
||
| 1976 | * Since MySQL is the odd one out here the double quotes are our generic |
||
| 1977 | * and we implement backticks in DatabaseMysql. |
||
| 1978 | * |
||
| 1979 | * @param string $s |
||
| 1980 | * @return string |
||
| 1981 | */ |
||
| 1982 | public function addIdentifierQuotes( $s ) { |
||
| 1985 | |||
| 1986 | /** |
||
| 1987 | * Returns if the given identifier looks quoted or not according to |
||
| 1988 | * the database convention for quoting identifiers . |
||
| 1989 | * |
||
| 1990 | * @note Do not use this to determine if untrusted input is safe. |
||
| 1991 | * A malicious user can trick this function. |
||
| 1992 | * @param string $name |
||
| 1993 | * @return bool |
||
| 1994 | */ |
||
| 1995 | public function isQuotedIdentifier( $name ) { |
||
| 1998 | |||
| 1999 | /** |
||
| 2000 | * @param string $s |
||
| 2001 | * @return string |
||
| 2002 | */ |
||
| 2003 | protected function escapeLikeInternal( $s ) { |
||
| 2006 | |||
| 2007 | public function buildLike() { |
||
| 2026 | |||
| 2027 | public function anyChar() { |
||
| 2030 | |||
| 2031 | public function anyString() { |
||
| 2034 | |||
| 2035 | public function nextSequenceValue( $seqName ) { |
||
| 2038 | |||
| 2039 | /** |
||
| 2040 | * USE INDEX clause. Unlikely to be useful for anything but MySQL. This |
||
| 2041 | * is only needed because a) MySQL must be as efficient as possible due to |
||
| 2042 | * its use on Wikipedia, and b) MySQL 4.0 is kind of dumb sometimes about |
||
| 2043 | * which index to pick. Anyway, other databases might have different |
||
| 2044 | * indexes on a given table. So don't bother overriding this unless you're |
||
| 2045 | * MySQL. |
||
| 2046 | * @param string $index |
||
| 2047 | * @return string |
||
| 2048 | */ |
||
| 2049 | public function useIndexClause( $index ) { |
||
| 2052 | |||
| 2053 | public function replace( $table, $uniqueIndexes, $rows, $fname = __METHOD__ ) { |
||
| 2100 | |||
| 2101 | /** |
||
| 2102 | * REPLACE query wrapper for MySQL and SQLite, which have a native REPLACE |
||
| 2103 | * statement. |
||
| 2104 | * |
||
| 2105 | * @param string $table Table name |
||
| 2106 | * @param array|string $rows Row(s) to insert |
||
| 2107 | * @param string $fname Caller function name |
||
| 2108 | * |
||
| 2109 | * @return ResultWrapper |
||
| 2110 | */ |
||
| 2111 | protected function nativeReplace( $table, $rows, $fname ) { |
||
| 2134 | |||
| 2135 | public function upsert( $table, array $rows, array $uniqueIndexes, array $set, |
||
| 2188 | |||
| 2189 | View Code Duplication | public function deleteJoin( $delTable, $joinTable, $delVar, $joinVar, $conds, |
|
| 2207 | |||
| 2208 | /** |
||
| 2209 | * Returns the size of a text field, or -1 for "unlimited" |
||
| 2210 | * |
||
| 2211 | * @param string $table |
||
| 2212 | * @param string $field |
||
| 2213 | * @return int |
||
| 2214 | */ |
||
| 2215 | public function textFieldSize( $table, $field ) { |
||
| 2231 | |||
| 2232 | /** |
||
| 2233 | * A string to insert into queries to show that they're low-priority, like |
||
| 2234 | * MySQL's LOW_PRIORITY. If no such feature exists, return an empty |
||
| 2235 | * string and nothing bad should happen. |
||
| 2236 | * |
||
| 2237 | * @return string Returns the text of the low priority option if it is |
||
| 2238 | * supported, or a blank string otherwise |
||
| 2239 | */ |
||
| 2240 | public function lowPriorityOption() { |
||
| 2243 | |||
| 2244 | public function delete( $table, $conds, $fname = __METHOD__ ) { |
||
| 2261 | |||
| 2262 | public function insertSelect( $destTable, $srcTable, $varMap, $conds, |
||
| 2301 | |||
| 2302 | /** |
||
| 2303 | * Construct a LIMIT query with optional offset. This is used for query |
||
| 2304 | * pages. The SQL should be adjusted so that only the first $limit rows |
||
| 2305 | * are returned. If $offset is provided as well, then the first $offset |
||
| 2306 | * rows should be discarded, and the next $limit rows should be returned. |
||
| 2307 | * If the result of the query is not ordered, then the rows to be returned |
||
| 2308 | * are theoretically arbitrary. |
||
| 2309 | * |
||
| 2310 | * $sql is expected to be a SELECT, if that makes a difference. |
||
| 2311 | * |
||
| 2312 | * The version provided by default works in MySQL and SQLite. It will very |
||
| 2313 | * likely need to be overridden for most other DBMSes. |
||
| 2314 | * |
||
| 2315 | * @param string $sql SQL query we will append the limit too |
||
| 2316 | * @param int $limit The SQL limit |
||
| 2317 | * @param int|bool $offset The SQL offset (default false) |
||
| 2318 | * @throws DBUnexpectedError |
||
| 2319 | * @return string |
||
| 2320 | */ |
||
| 2321 | public function limitResult( $sql, $limit, $offset = false ) { |
||
| 2330 | |||
| 2331 | public function unionSupportsOrderAndLimit() { |
||
| 2334 | |||
| 2335 | public function unionQueries( $sqls, $all ) { |
||
| 2340 | |||
| 2341 | public function conditional( $cond, $trueVal, $falseVal ) { |
||
| 2348 | |||
| 2349 | public function strreplace( $orig, $old, $new ) { |
||
| 2352 | |||
| 2353 | public function getServerUptime() { |
||
| 2356 | |||
| 2357 | public function wasDeadlock() { |
||
| 2360 | |||
| 2361 | public function wasLockTimeout() { |
||
| 2364 | |||
| 2365 | public function wasErrorReissuable() { |
||
| 2368 | |||
| 2369 | public function wasReadOnlyError() { |
||
| 2372 | |||
| 2373 | /** |
||
| 2374 | * Determines if the given query error was a connection drop |
||
| 2375 | * STUB |
||
| 2376 | * |
||
| 2377 | * @param integer|string $errno |
||
| 2378 | * @return bool |
||
| 2379 | */ |
||
| 2380 | public function wasConnectionError( $errno ) { |
||
| 2383 | |||
| 2384 | /** |
||
| 2385 | * Perform a deadlock-prone transaction. |
||
| 2386 | * |
||
| 2387 | * This function invokes a callback function to perform a set of write |
||
| 2388 | * queries. If a deadlock occurs during the processing, the transaction |
||
| 2389 | * will be rolled back and the callback function will be called again. |
||
| 2390 | * |
||
| 2391 | * Avoid using this method outside of Job or Maintenance classes. |
||
| 2392 | * |
||
| 2393 | * Usage: |
||
| 2394 | * $dbw->deadlockLoop( callback, ... ); |
||
| 2395 | * |
||
| 2396 | * Extra arguments are passed through to the specified callback function. |
||
| 2397 | * This method requires that no transactions are already active to avoid |
||
| 2398 | * causing premature commits or exceptions. |
||
| 2399 | * |
||
| 2400 | * Returns whatever the callback function returned on its successful, |
||
| 2401 | * iteration, or false on error, for example if the retry limit was |
||
| 2402 | * reached. |
||
| 2403 | * |
||
| 2404 | * @return mixed |
||
| 2405 | * @throws DBUnexpectedError |
||
| 2406 | * @throws Exception |
||
| 2407 | */ |
||
| 2408 | public function deadlockLoop() { |
||
| 2443 | |||
| 2444 | public function masterPosWait( DBMasterPos $pos, $timeout ) { |
||
| 2448 | |||
| 2449 | public function getSlavePos() { |
||
| 2453 | |||
| 2454 | public function getMasterPos() { |
||
| 2458 | |||
| 2459 | public function serverIsReadOnly() { |
||
| 2462 | |||
| 2463 | final public function onTransactionResolution( callable $callback ) { |
||
| 2469 | |||
| 2470 | final public function onTransactionIdle( callable $callback ) { |
||
| 2476 | |||
| 2477 | final public function onTransactionPreCommitOrIdle( callable $callback ) { |
||
| 2492 | |||
| 2493 | /** |
||
| 2494 | * Whether to disable running of post-commit callbacks |
||
| 2495 | * |
||
| 2496 | * This method should not be used outside of Database/LoadBalancer |
||
| 2497 | * |
||
| 2498 | * @param bool $suppress |
||
| 2499 | * @since 1.28 |
||
| 2500 | */ |
||
| 2501 | final public function setPostCommitCallbackSupression( $suppress ) { |
||
| 2504 | |||
| 2505 | /** |
||
| 2506 | * Actually run and consume any "on transaction idle/resolution" callbacks. |
||
| 2507 | * |
||
| 2508 | * This method should not be used outside of Database/LoadBalancer |
||
| 2509 | * |
||
| 2510 | * @param integer $trigger IDatabase::TRIGGER_* constant |
||
| 2511 | * @since 1.20 |
||
| 2512 | */ |
||
| 2513 | public function runOnTransactionIdleCallbacks( $trigger ) { |
||
| 2556 | |||
| 2557 | /** |
||
| 2558 | * Actually run and consume any "on transaction pre-commit" callbacks. |
||
| 2559 | * |
||
| 2560 | * This method should not be used outside of Database/LoadBalancer |
||
| 2561 | * |
||
| 2562 | * @since 1.22 |
||
| 2563 | */ |
||
| 2564 | public function runOnTransactionPreCommitCallbacks() { |
||
| 2586 | |||
| 2587 | final public function startAtomic( $fname = __METHOD__ ) { |
||
| 2600 | |||
| 2601 | final public function endAtomic( $fname = __METHOD__ ) { |
||
| 2615 | |||
| 2616 | final public function doAtomicSection( $fname, callable $callback ) { |
||
| 2626 | |||
| 2627 | final public function begin( $fname = __METHOD__ ) { |
||
| 2685 | |||
| 2686 | /** |
||
| 2687 | * Issues the BEGIN command to the database server. |
||
| 2688 | * |
||
| 2689 | * @see DatabaseBase::begin() |
||
| 2690 | * @param string $fname |
||
| 2691 | */ |
||
| 2692 | protected function doBegin( $fname ) { |
||
| 2696 | |||
| 2697 | final public function commit( $fname = __METHOD__, $flush = '' ) { |
||
| 2739 | |||
| 2740 | /** |
||
| 2741 | * Issues the COMMIT command to the database server. |
||
| 2742 | * |
||
| 2743 | * @see DatabaseBase::commit() |
||
| 2744 | * @param string $fname |
||
| 2745 | */ |
||
| 2746 | protected function doCommit( $fname ) { |
||
| 2752 | |||
| 2753 | final public function rollback( $fname = __METHOD__, $flush = '' ) { |
||
| 2779 | |||
| 2780 | /** |
||
| 2781 | * Issues the ROLLBACK command to the database server. |
||
| 2782 | * |
||
| 2783 | * @see DatabaseBase::rollback() |
||
| 2784 | * @param string $fname |
||
| 2785 | */ |
||
| 2786 | protected function doRollback( $fname ) { |
||
| 2792 | |||
| 2793 | /** |
||
| 2794 | * Creates a new table with structure copied from existing table |
||
| 2795 | * Note that unlike most database abstraction functions, this function does not |
||
| 2796 | * automatically append database prefix, because it works at a lower |
||
| 2797 | * abstraction level. |
||
| 2798 | * The table names passed to this function shall not be quoted (this |
||
| 2799 | * function calls addIdentifierQuotes when needed). |
||
| 2800 | * |
||
| 2801 | * @param string $oldName Name of table whose structure should be copied |
||
| 2802 | * @param string $newName Name of table to be created |
||
| 2803 | * @param bool $temporary Whether the new table should be temporary |
||
| 2804 | * @param string $fname Calling function name |
||
| 2805 | * @throws MWException |
||
| 2806 | * @return bool True if operation was successful |
||
| 2807 | */ |
||
| 2808 | public function duplicateTableStructure( $oldName, $newName, $temporary = false, |
||
| 2814 | |||
| 2815 | function listTables( $prefix = null, $fname = __METHOD__ ) { |
||
| 2818 | |||
| 2819 | /** |
||
| 2820 | * Reset the views process cache set by listViews() |
||
| 2821 | * @since 1.22 |
||
| 2822 | */ |
||
| 2823 | final public function clearViewsCache() { |
||
| 2826 | |||
| 2827 | /** |
||
| 2828 | * Lists all the VIEWs in the database |
||
| 2829 | * |
||
| 2830 | * For caching purposes the list of all views should be stored in |
||
| 2831 | * $this->allViews. The process cache can be cleared with clearViewsCache() |
||
| 2832 | * |
||
| 2833 | * @param string $prefix Only show VIEWs with this prefix, eg. unit_test_ |
||
| 2834 | * @param string $fname Name of calling function |
||
| 2835 | * @throws MWException |
||
| 2836 | * @return array |
||
| 2837 | * @since 1.22 |
||
| 2838 | */ |
||
| 2839 | public function listViews( $prefix = null, $fname = __METHOD__ ) { |
||
| 2842 | |||
| 2843 | /** |
||
| 2844 | * Differentiates between a TABLE and a VIEW |
||
| 2845 | * |
||
| 2846 | * @param string $name Name of the database-structure to test. |
||
| 2847 | * @throws MWException |
||
| 2848 | * @return bool |
||
| 2849 | * @since 1.22 |
||
| 2850 | */ |
||
| 2851 | public function isView( $name ) { |
||
| 2854 | |||
| 2855 | public function timestamp( $ts = 0 ) { |
||
| 2858 | |||
| 2859 | public function timestampOrNull( $ts = null ) { |
||
| 2866 | |||
| 2867 | /** |
||
| 2868 | * Take the result from a query, and wrap it in a ResultWrapper if |
||
| 2869 | * necessary. Boolean values are passed through as is, to indicate success |
||
| 2870 | * of write queries or failure. |
||
| 2871 | * |
||
| 2872 | * Once upon a time, DatabaseBase::query() returned a bare MySQL result |
||
| 2873 | * resource, and it was necessary to call this function to convert it to |
||
| 2874 | * a wrapper. Nowadays, raw database objects are never exposed to external |
||
| 2875 | * callers, so this is unnecessary in external code. |
||
| 2876 | * |
||
| 2877 | * @param bool|ResultWrapper|resource|object $result |
||
| 2878 | * @return bool|ResultWrapper |
||
| 2879 | */ |
||
| 2880 | protected function resultObject( $result ) { |
||
| 2892 | |||
| 2893 | public function ping() { |
||
| 2897 | |||
| 2898 | public function getSessionLagStatus() { |
||
| 2901 | |||
| 2902 | /** |
||
| 2903 | * Get the slave lag when the current transaction started |
||
| 2904 | * |
||
| 2905 | * This is useful when transactions might use snapshot isolation |
||
| 2906 | * (e.g. REPEATABLE-READ in innodb), so the "real" lag of that data |
||
| 2907 | * is this lag plus transaction duration. If they don't, it is still |
||
| 2908 | * safe to be pessimistic. This returns null if there is no transaction. |
||
| 2909 | * |
||
| 2910 | * @return array|null ('lag': seconds or false on error, 'since': UNIX timestamp of BEGIN) |
||
| 2911 | * @since 1.27 |
||
| 2912 | */ |
||
| 2913 | public function getTransactionLagStatus() { |
||
| 2918 | |||
| 2919 | /** |
||
| 2920 | * Get a slave lag estimate for this server |
||
| 2921 | * |
||
| 2922 | * @return array ('lag': seconds or false on error, 'since': UNIX timestamp of estimate) |
||
| 2923 | * @since 1.27 |
||
| 2924 | */ |
||
| 2925 | public function getApproximateLagStatus() { |
||
| 2931 | |||
| 2932 | /** |
||
| 2933 | * Merge the result of getSessionLagStatus() for several DBs |
||
| 2934 | * using the most pessimistic values to estimate the lag of |
||
| 2935 | * any data derived from them in combination |
||
| 2936 | * |
||
| 2937 | * This is information is useful for caching modules |
||
| 2938 | * |
||
| 2939 | * @see WANObjectCache::set() |
||
| 2940 | * @see WANObjectCache::getWithSetCallback() |
||
| 2941 | * |
||
| 2942 | * @param IDatabase $db1 |
||
| 2943 | * @param IDatabase ... |
||
| 2944 | * @return array Map of values: |
||
| 2945 | * - lag: highest lag of any of the DBs or false on error (e.g. replication stopped) |
||
| 2946 | * - since: oldest UNIX timestamp of any of the DB lag estimates |
||
| 2947 | * - pending: whether any of the DBs have uncommitted changes |
||
| 2948 | * @since 1.27 |
||
| 2949 | */ |
||
| 2950 | public static function getCacheSetOptions( IDatabase $db1 ) { |
||
| 2966 | |||
| 2967 | public function getLag() { |
||
| 2970 | |||
| 2971 | function maxListLen() { |
||
| 2974 | |||
| 2975 | public function encodeBlob( $b ) { |
||
| 2978 | |||
| 2979 | public function decodeBlob( $b ) { |
||
| 2985 | |||
| 2986 | public function setSessionOptions( array $options ) { |
||
| 2988 | |||
| 2989 | /** |
||
| 2990 | * Read and execute SQL commands from a file. |
||
| 2991 | * |
||
| 2992 | * Returns true on success, error string or exception on failure (depending |
||
| 2993 | * on object's error ignore settings). |
||
| 2994 | * |
||
| 2995 | * @param string $filename File name to open |
||
| 2996 | * @param bool|callable $lineCallback Optional function called before reading each line |
||
| 2997 | * @param bool|callable $resultCallback Optional function called for each MySQL result |
||
| 2998 | * @param bool|string $fname Calling function name or false if name should be |
||
| 2999 | * generated dynamically using $filename |
||
| 3000 | * @param bool|callable $inputCallback Optional function called for each |
||
| 3001 | * complete line sent |
||
| 3002 | * @throws Exception|MWException |
||
| 3003 | * @return bool|string |
||
| 3004 | */ |
||
| 3005 | public function sourceFile( |
||
| 3031 | |||
| 3032 | /** |
||
| 3033 | * Get the full path of a patch file. Originally based on archive() |
||
| 3034 | * from updaters.inc. Keep in mind this always returns a patch, as |
||
| 3035 | * it fails back to MySQL if no DB-specific patch can be found |
||
| 3036 | * |
||
| 3037 | * @param string $patch The name of the patch, like patch-something.sql |
||
| 3038 | * @return string Full path to patch file |
||
| 3039 | */ |
||
| 3040 | View Code Duplication | public function patchPath( $patch ) { |
|
| 3050 | |||
| 3051 | public function setSchemaVars( $vars ) { |
||
| 3054 | |||
| 3055 | /** |
||
| 3056 | * Read and execute commands from an open file handle. |
||
| 3057 | * |
||
| 3058 | * Returns true on success, error string or exception on failure (depending |
||
| 3059 | * on object's error ignore settings). |
||
| 3060 | * |
||
| 3061 | * @param resource $fp File handle |
||
| 3062 | * @param bool|callable $lineCallback Optional function called before reading each query |
||
| 3063 | * @param bool|callable $resultCallback Optional function called for each MySQL result |
||
| 3064 | * @param string $fname Calling function name |
||
| 3065 | * @param bool|callable $inputCallback Optional function called for each complete query sent |
||
| 3066 | * @return bool|string |
||
| 3067 | */ |
||
| 3068 | public function sourceStream( $fp, $lineCallback = false, $resultCallback = false, |
||
| 3118 | |||
| 3119 | /** |
||
| 3120 | * Called by sourceStream() to check if we've reached a statement end |
||
| 3121 | * |
||
| 3122 | * @param string $sql SQL assembled so far |
||
| 3123 | * @param string $newLine New line about to be added to $sql |
||
| 3124 | * @return bool Whether $newLine contains end of the statement |
||
| 3125 | */ |
||
| 3126 | public function streamStatementEnd( &$sql, &$newLine ) { |
||
| 3137 | |||
| 3138 | /** |
||
| 3139 | * Database independent variable replacement. Replaces a set of variables |
||
| 3140 | * in an SQL statement with their contents as given by $this->getSchemaVars(). |
||
| 3141 | * |
||
| 3142 | * Supports '{$var}' `{$var}` and / *$var* / (without the spaces) style variables. |
||
| 3143 | * |
||
| 3144 | * - '{$var}' should be used for text and is passed through the database's |
||
| 3145 | * addQuotes method. |
||
| 3146 | * - `{$var}` should be used for identifiers (e.g. table and database names). |
||
| 3147 | * It is passed through the database's addIdentifierQuotes method which |
||
| 3148 | * can be overridden if the database uses something other than backticks. |
||
| 3149 | * - / *_* / or / *$wgDBprefix* / passes the name that follows through the |
||
| 3150 | * database's tableName method. |
||
| 3151 | * - / *i* / passes the name that follows through the database's indexName method. |
||
| 3152 | * - In all other cases, / *$var* / is left unencoded. Except for table options, |
||
| 3153 | * its use should be avoided. In 1.24 and older, string encoding was applied. |
||
| 3154 | * |
||
| 3155 | * @param string $ins SQL statement to replace variables in |
||
| 3156 | * @return string The new SQL statement with variables replaced |
||
| 3157 | */ |
||
| 3158 | protected function replaceVars( $ins ) { |
||
| 3189 | |||
| 3190 | /** |
||
| 3191 | * Get schema variables. If none have been set via setSchemaVars(), then |
||
| 3192 | * use some defaults from the current object. |
||
| 3193 | * |
||
| 3194 | * @return array |
||
| 3195 | */ |
||
| 3196 | protected function getSchemaVars() { |
||
| 3203 | |||
| 3204 | /** |
||
| 3205 | * Get schema variables to use if none have been set via setSchemaVars(). |
||
| 3206 | * |
||
| 3207 | * Override this in derived classes to provide variables for tables.sql |
||
| 3208 | * and SQL patch files. |
||
| 3209 | * |
||
| 3210 | * @return array |
||
| 3211 | */ |
||
| 3212 | protected function getDefaultSchemaVars() { |
||
| 3215 | |||
| 3216 | public function lockIsFree( $lockName, $method ) { |
||
| 3219 | |||
| 3220 | public function lock( $lockName, $method, $timeout = 5 ) { |
||
| 3225 | |||
| 3226 | public function unlock( $lockName, $method ) { |
||
| 3231 | |||
| 3232 | public function getScopedLockAndFlush( $lockKey, $fname, $timeout ) { |
||
| 3246 | |||
| 3247 | public function namedLocksEnqueue() { |
||
| 3250 | |||
| 3251 | /** |
||
| 3252 | * Lock specific tables |
||
| 3253 | * |
||
| 3254 | * @param array $read Array of tables to lock for read access |
||
| 3255 | * @param array $write Array of tables to lock for write access |
||
| 3256 | * @param string $method Name of caller |
||
| 3257 | * @param bool $lowPriority Whether to indicate writes to be LOW PRIORITY |
||
| 3258 | * @return bool |
||
| 3259 | */ |
||
| 3260 | public function lockTables( $read, $write, $method, $lowPriority = true ) { |
||
| 3263 | |||
| 3264 | /** |
||
| 3265 | * Unlock specific tables |
||
| 3266 | * |
||
| 3267 | * @param string $method The caller |
||
| 3268 | * @return bool |
||
| 3269 | */ |
||
| 3270 | public function unlockTables( $method ) { |
||
| 3273 | |||
| 3274 | /** |
||
| 3275 | * Delete a table |
||
| 3276 | * @param string $tableName |
||
| 3277 | * @param string $fName |
||
| 3278 | * @return bool|ResultWrapper |
||
| 3279 | * @since 1.18 |
||
| 3280 | */ |
||
| 3281 | View Code Duplication | public function dropTable( $tableName, $fName = __METHOD__ ) { |
|
| 3292 | |||
| 3293 | /** |
||
| 3294 | * Get search engine class. All subclasses of this need to implement this |
||
| 3295 | * if they wish to use searching. |
||
| 3296 | * |
||
| 3297 | * @return string |
||
| 3298 | */ |
||
| 3299 | public function getSearchEngine() { |
||
| 3302 | |||
| 3303 | public function getInfinity() { |
||
| 3306 | |||
| 3307 | public function encodeExpiry( $expiry ) { |
||
| 3312 | |||
| 3313 | public function decodeExpiry( $expiry, $format = TS_MW ) { |
||
| 3318 | |||
| 3319 | public function setBigSelects( $value = true ) { |
||
| 3322 | |||
| 3323 | public function isReadOnly() { |
||
| 3326 | |||
| 3327 | /** |
||
| 3328 | * @return string|bool Reason this DB is read-only or false if it is not |
||
| 3329 | */ |
||
| 3330 | protected function getReadOnlyReason() { |
||
| 3335 | |||
| 3336 | /** |
||
| 3337 | * @since 1.19 |
||
| 3338 | * @return string |
||
| 3339 | */ |
||
| 3340 | public function __toString() { |
||
| 3343 | |||
| 3344 | /** |
||
| 3345 | * Run a few simple sanity checks |
||
| 3346 | */ |
||
| 3347 | public function __destruct() { |
||
| 3365 | } |
||
| 3366 | |||
| 3374 |
Only declaring a single property per statement allows you to later on add doc comments more easily.
It is also recommended by PSR2, so it is a common style that many people expect.