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() { |
||
| 699 | if ( $this->mConn ) { |
||
| 700 | if ( $this->trxLevel() ) { |
||
| 701 | if ( !$this->mTrxAutomatic ) { |
||
| 702 | wfWarn( "Transaction still in progress (from {$this->mTrxFname}), " . |
||
| 703 | " performing implicit commit before closing connection!" ); |
||
| 704 | } |
||
| 705 | |||
| 706 | $this->commit( __METHOD__, 'flush' ); |
||
| 707 | } |
||
| 708 | |||
| 709 | $closed = $this->closeConnection(); |
||
| 710 | $this->mConn = false; |
||
| 711 | } elseif ( $this->mTrxIdleCallbacks || $this->mTrxEndCallbacks ) { // sanity |
||
| 712 | throw new MWException( "Transaction callbacks still pending." ); |
||
| 713 | } else { |
||
| 714 | $closed = true; |
||
| 715 | } |
||
| 716 | $this->mOpened = false; |
||
| 717 | |||
| 718 | return $closed; |
||
| 719 | } |
||
| 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 ) { |
||
| 927 | |||
| 928 | private function canRecoverFromDisconnect( $sql, $priorWritesPending ) { |
||
| 947 | |||
| 948 | private function handleTransactionLoss() { |
||
| 961 | |||
| 962 | public function reportQueryError( $error, $errno, $sql, $fname, $tempIgnore = false ) { |
||
| 981 | |||
| 982 | /** |
||
| 983 | * Intended to be compatible with the PEAR::DB wrapper functions. |
||
| 984 | * http://pear.php.net/manual/en/package.database.db.intro-execute.php |
||
| 985 | * |
||
| 986 | * ? = scalar value, quoted as necessary |
||
| 987 | * ! = raw SQL bit (a function for instance) |
||
| 988 | * & = filename; reads the file and inserts as a blob |
||
| 989 | * (we don't use this though...) |
||
| 990 | * |
||
| 991 | * @param string $sql |
||
| 992 | * @param string $func |
||
| 993 | * |
||
| 994 | * @return array |
||
| 995 | */ |
||
| 996 | protected function prepare( $sql, $func = 'DatabaseBase::prepare' ) { |
||
| 1003 | |||
| 1004 | /** |
||
| 1005 | * Free a prepared query, generated by prepare(). |
||
| 1006 | * @param string $prepared |
||
| 1007 | */ |
||
| 1008 | protected function freePrepared( $prepared ) { |
||
| 1011 | |||
| 1012 | /** |
||
| 1013 | * Execute a prepared query with the various arguments |
||
| 1014 | * @param string $prepared The prepared sql |
||
| 1015 | * @param mixed $args Either an array here, or put scalars as varargs |
||
| 1016 | * |
||
| 1017 | * @return ResultWrapper |
||
| 1018 | */ |
||
| 1019 | public function execute( $prepared, $args = null ) { |
||
| 1030 | |||
| 1031 | /** |
||
| 1032 | * For faking prepared SQL statements on DBs that don't support it directly. |
||
| 1033 | * |
||
| 1034 | * @param string $preparedQuery A 'preparable' SQL statement |
||
| 1035 | * @param array $args Array of Arguments to fill it with |
||
| 1036 | * @return string Executable SQL |
||
| 1037 | */ |
||
| 1038 | public function fillPrepared( $preparedQuery, $args ) { |
||
| 1045 | |||
| 1046 | /** |
||
| 1047 | * preg_callback func for fillPrepared() |
||
| 1048 | * The arguments should be in $this->preparedArgs and must not be touched |
||
| 1049 | * while we're doing this. |
||
| 1050 | * |
||
| 1051 | * @param array $matches |
||
| 1052 | * @throws DBUnexpectedError |
||
| 1053 | * @return string |
||
| 1054 | */ |
||
| 1055 | protected function fillPreparedArg( $matches ) { |
||
| 1085 | |||
| 1086 | public function freeResult( $res ) { |
||
| 1088 | |||
| 1089 | public function selectField( |
||
| 1115 | |||
| 1116 | public function selectFieldValues( |
||
| 1141 | |||
| 1142 | /** |
||
| 1143 | * Returns an optional USE INDEX clause to go after the table, and a |
||
| 1144 | * string to go at the end of the query. |
||
| 1145 | * |
||
| 1146 | * @param array $options Associative array of options to be turned into |
||
| 1147 | * an SQL query, valid keys are listed in the function. |
||
| 1148 | * @return array |
||
| 1149 | * @see DatabaseBase::select() |
||
| 1150 | */ |
||
| 1151 | public function makeSelectOptions( $options ) { |
||
| 1226 | |||
| 1227 | /** |
||
| 1228 | * Returns an optional GROUP BY with an optional HAVING |
||
| 1229 | * |
||
| 1230 | * @param array $options Associative array of options |
||
| 1231 | * @return string |
||
| 1232 | * @see DatabaseBase::select() |
||
| 1233 | * @since 1.21 |
||
| 1234 | */ |
||
| 1235 | public function makeGroupByWithHaving( $options ) { |
||
| 1252 | |||
| 1253 | /** |
||
| 1254 | * Returns an optional ORDER BY |
||
| 1255 | * |
||
| 1256 | * @param array $options Associative array of options |
||
| 1257 | * @return string |
||
| 1258 | * @see DatabaseBase::select() |
||
| 1259 | * @since 1.21 |
||
| 1260 | */ |
||
| 1261 | public function makeOrderBy( $options ) { |
||
| 1272 | |||
| 1273 | // See IDatabase::select for the docs for this function |
||
| 1274 | public function select( $table, $vars, $conds = '', $fname = __METHOD__, |
||
| 1280 | |||
| 1281 | public function selectSQLText( $table, $vars, $conds = '', $fname = __METHOD__, |
||
| 1331 | |||
| 1332 | public function selectRow( $table, $vars, $conds, $fname = __METHOD__, |
||
| 1351 | |||
| 1352 | public function estimateRowCount( |
||
| 1365 | |||
| 1366 | public function selectRowCount( |
||
| 1380 | |||
| 1381 | /** |
||
| 1382 | * Removes most variables from an SQL query and replaces them with X or N for numbers. |
||
| 1383 | * It's only slightly flawed. Don't use for anything important. |
||
| 1384 | * |
||
| 1385 | * @param string $sql A SQL Query |
||
| 1386 | * |
||
| 1387 | * @return string |
||
| 1388 | */ |
||
| 1389 | protected static function generalizeSQL( $sql ) { |
||
| 1410 | |||
| 1411 | public function fieldExists( $table, $field, $fname = __METHOD__ ) { |
||
| 1416 | |||
| 1417 | public function indexExists( $table, $index, $fname = __METHOD__ ) { |
||
| 1429 | |||
| 1430 | public function tableExists( $table, $fname = __METHOD__ ) { |
||
| 1438 | |||
| 1439 | public function indexUnique( $table, $index ) { |
||
| 1448 | |||
| 1449 | /** |
||
| 1450 | * Helper for DatabaseBase::insert(). |
||
| 1451 | * |
||
| 1452 | * @param array $options |
||
| 1453 | * @return string |
||
| 1454 | */ |
||
| 1455 | protected function makeInsertOptions( $options ) { |
||
| 1458 | |||
| 1459 | public function insert( $table, $a, $fname = __METHOD__, $options = [] ) { |
||
| 1510 | |||
| 1511 | /** |
||
| 1512 | * Make UPDATE options array for DatabaseBase::makeUpdateOptions |
||
| 1513 | * |
||
| 1514 | * @param array $options |
||
| 1515 | * @return array |
||
| 1516 | */ |
||
| 1517 | protected function makeUpdateOptionsArray( $options ) { |
||
| 1534 | |||
| 1535 | /** |
||
| 1536 | * Make UPDATE options for the DatabaseBase::update function |
||
| 1537 | * |
||
| 1538 | * @param array $options The options passed to DatabaseBase::update |
||
| 1539 | * @return string |
||
| 1540 | */ |
||
| 1541 | protected function makeUpdateOptions( $options ) { |
||
| 1546 | |||
| 1547 | function update( $table, $values, $conds, $fname = __METHOD__, $options = [] ) { |
||
| 1558 | |||
| 1559 | public function makeList( $a, $mode = LIST_COMMA ) { |
||
| 1633 | |||
| 1634 | public function makeWhereFrom2d( $data, $baseKey, $subKey ) { |
||
| 1652 | |||
| 1653 | /** |
||
| 1654 | * Return aggregated value alias |
||
| 1655 | * |
||
| 1656 | * @param array $valuedata |
||
| 1657 | * @param string $valuename |
||
| 1658 | * |
||
| 1659 | * @return string |
||
| 1660 | */ |
||
| 1661 | public function aggregateValue( $valuedata, $valuename = 'value' ) { |
||
| 1664 | |||
| 1665 | public function bitNot( $field ) { |
||
| 1668 | |||
| 1669 | public function bitAnd( $fieldLeft, $fieldRight ) { |
||
| 1672 | |||
| 1673 | public function bitOr( $fieldLeft, $fieldRight ) { |
||
| 1676 | |||
| 1677 | public function buildConcat( $stringList ) { |
||
| 1680 | |||
| 1681 | View Code Duplication | public function buildGroupConcatField( |
|
| 1688 | |||
| 1689 | public function selectDB( $db ) { |
||
| 1697 | |||
| 1698 | public function getDBname() { |
||
| 1701 | |||
| 1702 | public function getServer() { |
||
| 1705 | |||
| 1706 | /** |
||
| 1707 | * Format a table name ready for use in constructing an SQL query |
||
| 1708 | * |
||
| 1709 | * This does two important things: it quotes the table names to clean them up, |
||
| 1710 | * and it adds a table prefix if only given a table name with no quotes. |
||
| 1711 | * |
||
| 1712 | * All functions of this object which require a table name call this function |
||
| 1713 | * themselves. Pass the canonical name to such functions. This is only needed |
||
| 1714 | * when calling query() directly. |
||
| 1715 | * |
||
| 1716 | * @note This function does not sanitize user input. It is not safe to use |
||
| 1717 | * this function to escape user input. |
||
| 1718 | * @param string $name Database table name |
||
| 1719 | * @param string $format One of: |
||
| 1720 | * quoted - Automatically pass the table name through addIdentifierQuotes() |
||
| 1721 | * so that it can be used in a query. |
||
| 1722 | * raw - Do not add identifier quotes to the table name |
||
| 1723 | * @return string Full database name |
||
| 1724 | */ |
||
| 1725 | public function tableName( $name, $format = 'quoted' ) { |
||
| 1803 | |||
| 1804 | /** |
||
| 1805 | * Fetch a number of table names into an array |
||
| 1806 | * This is handy when you need to construct SQL for joins |
||
| 1807 | * |
||
| 1808 | * Example: |
||
| 1809 | * extract( $dbr->tableNames( 'user', 'watchlist' ) ); |
||
| 1810 | * $sql = "SELECT wl_namespace,wl_title FROM $watchlist,$user |
||
| 1811 | * WHERE wl_user=user_id AND wl_user=$nameWithQuotes"; |
||
| 1812 | * |
||
| 1813 | * @return array |
||
| 1814 | */ |
||
| 1815 | View Code Duplication | public function tableNames() { |
|
| 1825 | |||
| 1826 | /** |
||
| 1827 | * Fetch a number of table names into an zero-indexed numerical array |
||
| 1828 | * This is handy when you need to construct SQL for joins |
||
| 1829 | * |
||
| 1830 | * Example: |
||
| 1831 | * list( $user, $watchlist ) = $dbr->tableNamesN( 'user', 'watchlist' ); |
||
| 1832 | * $sql = "SELECT wl_namespace,wl_title FROM $watchlist,$user |
||
| 1833 | * WHERE wl_user=user_id AND wl_user=$nameWithQuotes"; |
||
| 1834 | * |
||
| 1835 | * @return array |
||
| 1836 | */ |
||
| 1837 | View Code Duplication | public function tableNamesN() { |
|
| 1847 | |||
| 1848 | /** |
||
| 1849 | * Get an aliased table name |
||
| 1850 | * e.g. tableName AS newTableName |
||
| 1851 | * |
||
| 1852 | * @param string $name Table name, see tableName() |
||
| 1853 | * @param string|bool $alias Alias (optional) |
||
| 1854 | * @return string SQL name for aliased table. Will not alias a table to its own name |
||
| 1855 | */ |
||
| 1856 | public function tableNameWithAlias( $name, $alias = false ) { |
||
| 1863 | |||
| 1864 | /** |
||
| 1865 | * Gets an array of aliased table names |
||
| 1866 | * |
||
| 1867 | * @param array $tables Array( [alias] => table ) |
||
| 1868 | * @return string[] See tableNameWithAlias() |
||
| 1869 | */ |
||
| 1870 | public function tableNamesWithAlias( $tables ) { |
||
| 1881 | |||
| 1882 | /** |
||
| 1883 | * Get an aliased field name |
||
| 1884 | * e.g. fieldName AS newFieldName |
||
| 1885 | * |
||
| 1886 | * @param string $name Field name |
||
| 1887 | * @param string|bool $alias Alias (optional) |
||
| 1888 | * @return string SQL name for aliased field. Will not alias a field to its own name |
||
| 1889 | */ |
||
| 1890 | public function fieldNameWithAlias( $name, $alias = false ) { |
||
| 1897 | |||
| 1898 | /** |
||
| 1899 | * Gets an array of aliased field names |
||
| 1900 | * |
||
| 1901 | * @param array $fields Array( [alias] => field ) |
||
| 1902 | * @return string[] See fieldNameWithAlias() |
||
| 1903 | */ |
||
| 1904 | public function fieldNamesWithAlias( $fields ) { |
||
| 1915 | |||
| 1916 | /** |
||
| 1917 | * Get the aliased table name clause for a FROM clause |
||
| 1918 | * which might have a JOIN and/or USE INDEX clause |
||
| 1919 | * |
||
| 1920 | * @param array $tables ( [alias] => table ) |
||
| 1921 | * @param array $use_index Same as for select() |
||
| 1922 | * @param array $join_conds Same as for select() |
||
| 1923 | * @return string |
||
| 1924 | */ |
||
| 1925 | protected function tableNamesWithUseIndexOrJOIN( |
||
| 1977 | |||
| 1978 | /** |
||
| 1979 | * Get the name of an index in a given table. |
||
| 1980 | * |
||
| 1981 | * @param string $index |
||
| 1982 | * @return string |
||
| 1983 | */ |
||
| 1984 | protected function indexName( $index ) { |
||
| 1998 | |||
| 1999 | public function addQuotes( $s ) { |
||
| 2013 | |||
| 2014 | /** |
||
| 2015 | * Quotes an identifier using `backticks` or "double quotes" depending on the database type. |
||
| 2016 | * MySQL uses `backticks` while basically everything else uses double quotes. |
||
| 2017 | * Since MySQL is the odd one out here the double quotes are our generic |
||
| 2018 | * and we implement backticks in DatabaseMysql. |
||
| 2019 | * |
||
| 2020 | * @param string $s |
||
| 2021 | * @return string |
||
| 2022 | */ |
||
| 2023 | public function addIdentifierQuotes( $s ) { |
||
| 2026 | |||
| 2027 | /** |
||
| 2028 | * Returns if the given identifier looks quoted or not according to |
||
| 2029 | * the database convention for quoting identifiers . |
||
| 2030 | * |
||
| 2031 | * @note Do not use this to determine if untrusted input is safe. |
||
| 2032 | * A malicious user can trick this function. |
||
| 2033 | * @param string $name |
||
| 2034 | * @return bool |
||
| 2035 | */ |
||
| 2036 | public function isQuotedIdentifier( $name ) { |
||
| 2039 | |||
| 2040 | /** |
||
| 2041 | * @param string $s |
||
| 2042 | * @return string |
||
| 2043 | */ |
||
| 2044 | protected function escapeLikeInternal( $s ) { |
||
| 2047 | |||
| 2048 | public function buildLike() { |
||
| 2067 | |||
| 2068 | public function anyChar() { |
||
| 2071 | |||
| 2072 | public function anyString() { |
||
| 2075 | |||
| 2076 | public function nextSequenceValue( $seqName ) { |
||
| 2079 | |||
| 2080 | /** |
||
| 2081 | * USE INDEX clause. Unlikely to be useful for anything but MySQL. This |
||
| 2082 | * is only needed because a) MySQL must be as efficient as possible due to |
||
| 2083 | * its use on Wikipedia, and b) MySQL 4.0 is kind of dumb sometimes about |
||
| 2084 | * which index to pick. Anyway, other databases might have different |
||
| 2085 | * indexes on a given table. So don't bother overriding this unless you're |
||
| 2086 | * MySQL. |
||
| 2087 | * @param string $index |
||
| 2088 | * @return string |
||
| 2089 | */ |
||
| 2090 | public function useIndexClause( $index ) { |
||
| 2093 | |||
| 2094 | public function replace( $table, $uniqueIndexes, $rows, $fname = __METHOD__ ) { |
||
| 2141 | |||
| 2142 | /** |
||
| 2143 | * REPLACE query wrapper for MySQL and SQLite, which have a native REPLACE |
||
| 2144 | * statement. |
||
| 2145 | * |
||
| 2146 | * @param string $table Table name |
||
| 2147 | * @param array|string $rows Row(s) to insert |
||
| 2148 | * @param string $fname Caller function name |
||
| 2149 | * |
||
| 2150 | * @return ResultWrapper |
||
| 2151 | */ |
||
| 2152 | protected function nativeReplace( $table, $rows, $fname ) { |
||
| 2175 | |||
| 2176 | public function upsert( $table, array $rows, array $uniqueIndexes, array $set, |
||
| 2229 | |||
| 2230 | View Code Duplication | public function deleteJoin( $delTable, $joinTable, $delVar, $joinVar, $conds, |
|
| 2248 | |||
| 2249 | /** |
||
| 2250 | * Returns the size of a text field, or -1 for "unlimited" |
||
| 2251 | * |
||
| 2252 | * @param string $table |
||
| 2253 | * @param string $field |
||
| 2254 | * @return int |
||
| 2255 | */ |
||
| 2256 | public function textFieldSize( $table, $field ) { |
||
| 2272 | |||
| 2273 | /** |
||
| 2274 | * A string to insert into queries to show that they're low-priority, like |
||
| 2275 | * MySQL's LOW_PRIORITY. If no such feature exists, return an empty |
||
| 2276 | * string and nothing bad should happen. |
||
| 2277 | * |
||
| 2278 | * @return string Returns the text of the low priority option if it is |
||
| 2279 | * supported, or a blank string otherwise |
||
| 2280 | */ |
||
| 2281 | public function lowPriorityOption() { |
||
| 2284 | |||
| 2285 | public function delete( $table, $conds, $fname = __METHOD__ ) { |
||
| 2302 | |||
| 2303 | public function insertSelect( $destTable, $srcTable, $varMap, $conds, |
||
| 2342 | |||
| 2343 | /** |
||
| 2344 | * Construct a LIMIT query with optional offset. This is used for query |
||
| 2345 | * pages. The SQL should be adjusted so that only the first $limit rows |
||
| 2346 | * are returned. If $offset is provided as well, then the first $offset |
||
| 2347 | * rows should be discarded, and the next $limit rows should be returned. |
||
| 2348 | * If the result of the query is not ordered, then the rows to be returned |
||
| 2349 | * are theoretically arbitrary. |
||
| 2350 | * |
||
| 2351 | * $sql is expected to be a SELECT, if that makes a difference. |
||
| 2352 | * |
||
| 2353 | * The version provided by default works in MySQL and SQLite. It will very |
||
| 2354 | * likely need to be overridden for most other DBMSes. |
||
| 2355 | * |
||
| 2356 | * @param string $sql SQL query we will append the limit too |
||
| 2357 | * @param int $limit The SQL limit |
||
| 2358 | * @param int|bool $offset The SQL offset (default false) |
||
| 2359 | * @throws DBUnexpectedError |
||
| 2360 | * @return string |
||
| 2361 | */ |
||
| 2362 | public function limitResult( $sql, $limit, $offset = false ) { |
||
| 2371 | |||
| 2372 | public function unionSupportsOrderAndLimit() { |
||
| 2375 | |||
| 2376 | public function unionQueries( $sqls, $all ) { |
||
| 2381 | |||
| 2382 | public function conditional( $cond, $trueVal, $falseVal ) { |
||
| 2389 | |||
| 2390 | public function strreplace( $orig, $old, $new ) { |
||
| 2393 | |||
| 2394 | public function getServerUptime() { |
||
| 2397 | |||
| 2398 | public function wasDeadlock() { |
||
| 2401 | |||
| 2402 | public function wasLockTimeout() { |
||
| 2405 | |||
| 2406 | public function wasErrorReissuable() { |
||
| 2409 | |||
| 2410 | public function wasReadOnlyError() { |
||
| 2413 | |||
| 2414 | /** |
||
| 2415 | * Determines if the given query error was a connection drop |
||
| 2416 | * STUB |
||
| 2417 | * |
||
| 2418 | * @param integer|string $errno |
||
| 2419 | * @return bool |
||
| 2420 | */ |
||
| 2421 | public function wasConnectionError( $errno ) { |
||
| 2424 | |||
| 2425 | /** |
||
| 2426 | * Perform a deadlock-prone transaction. |
||
| 2427 | * |
||
| 2428 | * This function invokes a callback function to perform a set of write |
||
| 2429 | * queries. If a deadlock occurs during the processing, the transaction |
||
| 2430 | * will be rolled back and the callback function will be called again. |
||
| 2431 | * |
||
| 2432 | * Avoid using this method outside of Job or Maintenance classes. |
||
| 2433 | * |
||
| 2434 | * Usage: |
||
| 2435 | * $dbw->deadlockLoop( callback, ... ); |
||
| 2436 | * |
||
| 2437 | * Extra arguments are passed through to the specified callback function. |
||
| 2438 | * This method requires that no transactions are already active to avoid |
||
| 2439 | * causing premature commits or exceptions. |
||
| 2440 | * |
||
| 2441 | * Returns whatever the callback function returned on its successful, |
||
| 2442 | * iteration, or false on error, for example if the retry limit was |
||
| 2443 | * reached. |
||
| 2444 | * |
||
| 2445 | * @return mixed |
||
| 2446 | * @throws DBUnexpectedError |
||
| 2447 | * @throws Exception |
||
| 2448 | */ |
||
| 2449 | public function deadlockLoop() { |
||
| 2484 | |||
| 2485 | public function masterPosWait( DBMasterPos $pos, $timeout ) { |
||
| 2489 | |||
| 2490 | public function getSlavePos() { |
||
| 2494 | |||
| 2495 | public function getMasterPos() { |
||
| 2499 | |||
| 2500 | public function serverIsReadOnly() { |
||
| 2503 | |||
| 2504 | final public function onTransactionResolution( callable $callback ) { |
||
| 2510 | |||
| 2511 | final public function onTransactionIdle( callable $callback ) { |
||
| 2517 | |||
| 2518 | final public function onTransactionPreCommitOrIdle( callable $callback ) { |
||
| 2533 | |||
| 2534 | /** |
||
| 2535 | * Whether to disable running of post-commit callbacks |
||
| 2536 | * |
||
| 2537 | * This method should not be used outside of Database/LoadBalancer |
||
| 2538 | * |
||
| 2539 | * @param bool $suppress |
||
| 2540 | * @since 1.28 |
||
| 2541 | */ |
||
| 2542 | final public function setPostCommitCallbackSupression( $suppress ) { |
||
| 2545 | |||
| 2546 | /** |
||
| 2547 | * Actually run and consume any "on transaction idle/resolution" callbacks. |
||
| 2548 | * |
||
| 2549 | * This method should not be used outside of Database/LoadBalancer |
||
| 2550 | * |
||
| 2551 | * @param integer $trigger IDatabase::TRIGGER_* constant |
||
| 2552 | * @since 1.20 |
||
| 2553 | * @throws Exception |
||
| 2554 | */ |
||
| 2555 | public function runOnTransactionIdleCallbacks( $trigger ) { |
||
| 2598 | |||
| 2599 | /** |
||
| 2600 | * Actually run and consume any "on transaction pre-commit" callbacks. |
||
| 2601 | * |
||
| 2602 | * This method should not be used outside of Database/LoadBalancer |
||
| 2603 | * |
||
| 2604 | * @since 1.22 |
||
| 2605 | */ |
||
| 2606 | public function runOnTransactionPreCommitCallbacks() { |
||
| 2628 | |||
| 2629 | final public function startAtomic( $fname = __METHOD__ ) { |
||
| 2642 | |||
| 2643 | final public function endAtomic( $fname = __METHOD__ ) { |
||
| 2657 | |||
| 2658 | final public function doAtomicSection( $fname, callable $callback ) { |
||
| 2668 | |||
| 2669 | final public function begin( $fname = __METHOD__ ) { |
||
| 2727 | |||
| 2728 | /** |
||
| 2729 | * Issues the BEGIN command to the database server. |
||
| 2730 | * |
||
| 2731 | * @see DatabaseBase::begin() |
||
| 2732 | * @param string $fname |
||
| 2733 | */ |
||
| 2734 | protected function doBegin( $fname ) { |
||
| 2738 | |||
| 2739 | final public function commit( $fname = __METHOD__, $flush = '' ) { |
||
| 2784 | |||
| 2785 | /** |
||
| 2786 | * Issues the COMMIT command to the database server. |
||
| 2787 | * |
||
| 2788 | * @see DatabaseBase::commit() |
||
| 2789 | * @param string $fname |
||
| 2790 | */ |
||
| 2791 | protected function doCommit( $fname ) { |
||
| 2797 | |||
| 2798 | final public function rollback( $fname = __METHOD__, $flush = '' ) { |
||
| 2824 | |||
| 2825 | /** |
||
| 2826 | * Issues the ROLLBACK command to the database server. |
||
| 2827 | * |
||
| 2828 | * @see DatabaseBase::rollback() |
||
| 2829 | * @param string $fname |
||
| 2830 | */ |
||
| 2831 | protected function doRollback( $fname ) { |
||
| 2839 | |||
| 2840 | /** |
||
| 2841 | * @return bool |
||
| 2842 | */ |
||
| 2843 | protected function explicitTrxActive() { |
||
| 2846 | |||
| 2847 | /** |
||
| 2848 | * Creates a new table with structure copied from existing table |
||
| 2849 | * Note that unlike most database abstraction functions, this function does not |
||
| 2850 | * automatically append database prefix, because it works at a lower |
||
| 2851 | * abstraction level. |
||
| 2852 | * The table names passed to this function shall not be quoted (this |
||
| 2853 | * function calls addIdentifierQuotes when needed). |
||
| 2854 | * |
||
| 2855 | * @param string $oldName Name of table whose structure should be copied |
||
| 2856 | * @param string $newName Name of table to be created |
||
| 2857 | * @param bool $temporary Whether the new table should be temporary |
||
| 2858 | * @param string $fname Calling function name |
||
| 2859 | * @throws MWException |
||
| 2860 | * @return bool True if operation was successful |
||
| 2861 | */ |
||
| 2862 | public function duplicateTableStructure( $oldName, $newName, $temporary = false, |
||
| 2868 | |||
| 2869 | function listTables( $prefix = null, $fname = __METHOD__ ) { |
||
| 2872 | |||
| 2873 | /** |
||
| 2874 | * Reset the views process cache set by listViews() |
||
| 2875 | * @since 1.22 |
||
| 2876 | */ |
||
| 2877 | final public function clearViewsCache() { |
||
| 2880 | |||
| 2881 | /** |
||
| 2882 | * Lists all the VIEWs in the database |
||
| 2883 | * |
||
| 2884 | * For caching purposes the list of all views should be stored in |
||
| 2885 | * $this->allViews. The process cache can be cleared with clearViewsCache() |
||
| 2886 | * |
||
| 2887 | * @param string $prefix Only show VIEWs with this prefix, eg. unit_test_ |
||
| 2888 | * @param string $fname Name of calling function |
||
| 2889 | * @throws MWException |
||
| 2890 | * @return array |
||
| 2891 | * @since 1.22 |
||
| 2892 | */ |
||
| 2893 | public function listViews( $prefix = null, $fname = __METHOD__ ) { |
||
| 2896 | |||
| 2897 | /** |
||
| 2898 | * Differentiates between a TABLE and a VIEW |
||
| 2899 | * |
||
| 2900 | * @param string $name Name of the database-structure to test. |
||
| 2901 | * @throws MWException |
||
| 2902 | * @return bool |
||
| 2903 | * @since 1.22 |
||
| 2904 | */ |
||
| 2905 | public function isView( $name ) { |
||
| 2908 | |||
| 2909 | public function timestamp( $ts = 0 ) { |
||
| 2912 | |||
| 2913 | public function timestampOrNull( $ts = null ) { |
||
| 2920 | |||
| 2921 | /** |
||
| 2922 | * Take the result from a query, and wrap it in a ResultWrapper if |
||
| 2923 | * necessary. Boolean values are passed through as is, to indicate success |
||
| 2924 | * of write queries or failure. |
||
| 2925 | * |
||
| 2926 | * Once upon a time, DatabaseBase::query() returned a bare MySQL result |
||
| 2927 | * resource, and it was necessary to call this function to convert it to |
||
| 2928 | * a wrapper. Nowadays, raw database objects are never exposed to external |
||
| 2929 | * callers, so this is unnecessary in external code. |
||
| 2930 | * |
||
| 2931 | * @param bool|ResultWrapper|resource|object $result |
||
| 2932 | * @return bool|ResultWrapper |
||
| 2933 | */ |
||
| 2934 | protected function resultObject( $result ) { |
||
| 2946 | |||
| 2947 | public function ping() { |
||
| 2956 | |||
| 2957 | /** |
||
| 2958 | * @return bool |
||
| 2959 | */ |
||
| 2960 | protected function reconnect() { |
||
| 2964 | |||
| 2965 | public function getSessionLagStatus() { |
||
| 2968 | |||
| 2969 | /** |
||
| 2970 | * Get the slave lag when the current transaction started |
||
| 2971 | * |
||
| 2972 | * This is useful when transactions might use snapshot isolation |
||
| 2973 | * (e.g. REPEATABLE-READ in innodb), so the "real" lag of that data |
||
| 2974 | * is this lag plus transaction duration. If they don't, it is still |
||
| 2975 | * safe to be pessimistic. This returns null if there is no transaction. |
||
| 2976 | * |
||
| 2977 | * @return array|null ('lag': seconds or false on error, 'since': UNIX timestamp of BEGIN) |
||
| 2978 | * @since 1.27 |
||
| 2979 | */ |
||
| 2980 | public function getTransactionLagStatus() { |
||
| 2985 | |||
| 2986 | /** |
||
| 2987 | * Get a slave lag estimate for this server |
||
| 2988 | * |
||
| 2989 | * @return array ('lag': seconds or false on error, 'since': UNIX timestamp of estimate) |
||
| 2990 | * @since 1.27 |
||
| 2991 | */ |
||
| 2992 | public function getApproximateLagStatus() { |
||
| 2998 | |||
| 2999 | /** |
||
| 3000 | * Merge the result of getSessionLagStatus() for several DBs |
||
| 3001 | * using the most pessimistic values to estimate the lag of |
||
| 3002 | * any data derived from them in combination |
||
| 3003 | * |
||
| 3004 | * This is information is useful for caching modules |
||
| 3005 | * |
||
| 3006 | * @see WANObjectCache::set() |
||
| 3007 | * @see WANObjectCache::getWithSetCallback() |
||
| 3008 | * |
||
| 3009 | * @param IDatabase $db1 |
||
| 3010 | * @param IDatabase ... |
||
| 3011 | * @return array Map of values: |
||
| 3012 | * - lag: highest lag of any of the DBs or false on error (e.g. replication stopped) |
||
| 3013 | * - since: oldest UNIX timestamp of any of the DB lag estimates |
||
| 3014 | * - pending: whether any of the DBs have uncommitted changes |
||
| 3015 | * @since 1.27 |
||
| 3016 | */ |
||
| 3017 | public static function getCacheSetOptions( IDatabase $db1 ) { |
||
| 3033 | |||
| 3034 | public function getLag() { |
||
| 3037 | |||
| 3038 | function maxListLen() { |
||
| 3041 | |||
| 3042 | public function encodeBlob( $b ) { |
||
| 3045 | |||
| 3046 | public function decodeBlob( $b ) { |
||
| 3052 | |||
| 3053 | public function setSessionOptions( array $options ) { |
||
| 3055 | |||
| 3056 | /** |
||
| 3057 | * Read and execute SQL commands from a file. |
||
| 3058 | * |
||
| 3059 | * Returns true on success, error string or exception on failure (depending |
||
| 3060 | * on object's error ignore settings). |
||
| 3061 | * |
||
| 3062 | * @param string $filename File name to open |
||
| 3063 | * @param bool|callable $lineCallback Optional function called before reading each line |
||
| 3064 | * @param bool|callable $resultCallback Optional function called for each MySQL result |
||
| 3065 | * @param bool|string $fname Calling function name or false if name should be |
||
| 3066 | * generated dynamically using $filename |
||
| 3067 | * @param bool|callable $inputCallback Optional function called for each |
||
| 3068 | * complete line sent |
||
| 3069 | * @throws Exception|MWException |
||
| 3070 | * @return bool|string |
||
| 3071 | */ |
||
| 3072 | public function sourceFile( |
||
| 3098 | |||
| 3099 | /** |
||
| 3100 | * Get the full path of a patch file. Originally based on archive() |
||
| 3101 | * from updaters.inc. Keep in mind this always returns a patch, as |
||
| 3102 | * it fails back to MySQL if no DB-specific patch can be found |
||
| 3103 | * |
||
| 3104 | * @param string $patch The name of the patch, like patch-something.sql |
||
| 3105 | * @return string Full path to patch file |
||
| 3106 | */ |
||
| 3107 | View Code Duplication | public function patchPath( $patch ) { |
|
| 3117 | |||
| 3118 | public function setSchemaVars( $vars ) { |
||
| 3121 | |||
| 3122 | /** |
||
| 3123 | * Read and execute commands from an open file handle. |
||
| 3124 | * |
||
| 3125 | * Returns true on success, error string or exception on failure (depending |
||
| 3126 | * on object's error ignore settings). |
||
| 3127 | * |
||
| 3128 | * @param resource $fp File handle |
||
| 3129 | * @param bool|callable $lineCallback Optional function called before reading each query |
||
| 3130 | * @param bool|callable $resultCallback Optional function called for each MySQL result |
||
| 3131 | * @param string $fname Calling function name |
||
| 3132 | * @param bool|callable $inputCallback Optional function called for each complete query sent |
||
| 3133 | * @return bool|string |
||
| 3134 | */ |
||
| 3135 | public function sourceStream( $fp, $lineCallback = false, $resultCallback = false, |
||
| 3185 | |||
| 3186 | /** |
||
| 3187 | * Called by sourceStream() to check if we've reached a statement end |
||
| 3188 | * |
||
| 3189 | * @param string $sql SQL assembled so far |
||
| 3190 | * @param string $newLine New line about to be added to $sql |
||
| 3191 | * @return bool Whether $newLine contains end of the statement |
||
| 3192 | */ |
||
| 3193 | public function streamStatementEnd( &$sql, &$newLine ) { |
||
| 3204 | |||
| 3205 | /** |
||
| 3206 | * Database independent variable replacement. Replaces a set of variables |
||
| 3207 | * in an SQL statement with their contents as given by $this->getSchemaVars(). |
||
| 3208 | * |
||
| 3209 | * Supports '{$var}' `{$var}` and / *$var* / (without the spaces) style variables. |
||
| 3210 | * |
||
| 3211 | * - '{$var}' should be used for text and is passed through the database's |
||
| 3212 | * addQuotes method. |
||
| 3213 | * - `{$var}` should be used for identifiers (e.g. table and database names). |
||
| 3214 | * It is passed through the database's addIdentifierQuotes method which |
||
| 3215 | * can be overridden if the database uses something other than backticks. |
||
| 3216 | * - / *_* / or / *$wgDBprefix* / passes the name that follows through the |
||
| 3217 | * database's tableName method. |
||
| 3218 | * - / *i* / passes the name that follows through the database's indexName method. |
||
| 3219 | * - In all other cases, / *$var* / is left unencoded. Except for table options, |
||
| 3220 | * its use should be avoided. In 1.24 and older, string encoding was applied. |
||
| 3221 | * |
||
| 3222 | * @param string $ins SQL statement to replace variables in |
||
| 3223 | * @return string The new SQL statement with variables replaced |
||
| 3224 | */ |
||
| 3225 | protected function replaceVars( $ins ) { |
||
| 3256 | |||
| 3257 | /** |
||
| 3258 | * Get schema variables. If none have been set via setSchemaVars(), then |
||
| 3259 | * use some defaults from the current object. |
||
| 3260 | * |
||
| 3261 | * @return array |
||
| 3262 | */ |
||
| 3263 | protected function getSchemaVars() { |
||
| 3270 | |||
| 3271 | /** |
||
| 3272 | * Get schema variables to use if none have been set via setSchemaVars(). |
||
| 3273 | * |
||
| 3274 | * Override this in derived classes to provide variables for tables.sql |
||
| 3275 | * and SQL patch files. |
||
| 3276 | * |
||
| 3277 | * @return array |
||
| 3278 | */ |
||
| 3279 | protected function getDefaultSchemaVars() { |
||
| 3282 | |||
| 3283 | public function lockIsFree( $lockName, $method ) { |
||
| 3286 | |||
| 3287 | public function lock( $lockName, $method, $timeout = 5 ) { |
||
| 3292 | |||
| 3293 | public function unlock( $lockName, $method ) { |
||
| 3298 | |||
| 3299 | public function getScopedLockAndFlush( $lockKey, $fname, $timeout ) { |
||
| 3313 | |||
| 3314 | public function namedLocksEnqueue() { |
||
| 3317 | |||
| 3318 | /** |
||
| 3319 | * Lock specific tables |
||
| 3320 | * |
||
| 3321 | * @param array $read Array of tables to lock for read access |
||
| 3322 | * @param array $write Array of tables to lock for write access |
||
| 3323 | * @param string $method Name of caller |
||
| 3324 | * @param bool $lowPriority Whether to indicate writes to be LOW PRIORITY |
||
| 3325 | * @return bool |
||
| 3326 | */ |
||
| 3327 | public function lockTables( $read, $write, $method, $lowPriority = true ) { |
||
| 3330 | |||
| 3331 | /** |
||
| 3332 | * Unlock specific tables |
||
| 3333 | * |
||
| 3334 | * @param string $method The caller |
||
| 3335 | * @return bool |
||
| 3336 | */ |
||
| 3337 | public function unlockTables( $method ) { |
||
| 3340 | |||
| 3341 | /** |
||
| 3342 | * Delete a table |
||
| 3343 | * @param string $tableName |
||
| 3344 | * @param string $fName |
||
| 3345 | * @return bool|ResultWrapper |
||
| 3346 | * @since 1.18 |
||
| 3347 | */ |
||
| 3348 | View Code Duplication | public function dropTable( $tableName, $fName = __METHOD__ ) { |
|
| 3359 | |||
| 3360 | /** |
||
| 3361 | * Get search engine class. All subclasses of this need to implement this |
||
| 3362 | * if they wish to use searching. |
||
| 3363 | * |
||
| 3364 | * @return string |
||
| 3365 | */ |
||
| 3366 | public function getSearchEngine() { |
||
| 3369 | |||
| 3370 | public function getInfinity() { |
||
| 3373 | |||
| 3374 | public function encodeExpiry( $expiry ) { |
||
| 3379 | |||
| 3380 | public function decodeExpiry( $expiry, $format = TS_MW ) { |
||
| 3385 | |||
| 3386 | public function setBigSelects( $value = true ) { |
||
| 3389 | |||
| 3390 | public function isReadOnly() { |
||
| 3393 | |||
| 3394 | /** |
||
| 3395 | * @return string|bool Reason this DB is read-only or false if it is not |
||
| 3396 | */ |
||
| 3397 | protected function getReadOnlyReason() { |
||
| 3402 | |||
| 3403 | /** |
||
| 3404 | * @since 1.19 |
||
| 3405 | * @return string |
||
| 3406 | */ |
||
| 3407 | public function __toString() { |
||
| 3410 | |||
| 3411 | /** |
||
| 3412 | * Run a few simple sanity checks |
||
| 3413 | */ |
||
| 3414 | public function __destruct() { |
||
| 3432 | } |
||
| 3433 | |||
| 3441 |
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.