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 | /** Minimum time to wait before retry, in microseconds */ |
||
36 | const DEADLOCK_DELAY_MIN = 500000; |
||
37 | /** Maximum time to wait before retry */ |
||
38 | const DEADLOCK_DELAY_MAX = 1500000; |
||
39 | |||
40 | /** How long before it is worth doing a dummy query to test the connection */ |
||
41 | const PING_TTL = 1.0; |
||
42 | const PING_QUERY = 'SELECT 1 AS ping'; |
||
43 | |||
44 | const TINY_WRITE_SEC = .010; |
||
45 | const SLOW_WRITE_SEC = .500; |
||
46 | const SMALL_WRITE_ROWS = 100; |
||
47 | |||
48 | /** @var string SQL query */ |
||
49 | protected $mLastQuery = ''; |
||
50 | /** @var bool */ |
||
51 | protected $mDoneWrites = false; |
||
52 | /** @var string|bool */ |
||
53 | protected $mPHPError = false; |
||
54 | /** @var string */ |
||
55 | protected $mServer; |
||
56 | /** @var string */ |
||
57 | protected $mUser; |
||
58 | /** @var string */ |
||
59 | protected $mPassword; |
||
60 | /** @var string */ |
||
61 | protected $mDBname; |
||
62 | /** @var bool */ |
||
63 | protected $cliMode; |
||
64 | |||
65 | /** @var BagOStuff APC cache */ |
||
66 | protected $srvCache; |
||
67 | |||
68 | /** @var resource Database connection */ |
||
69 | protected $mConn = null; |
||
70 | /** @var bool */ |
||
71 | protected $mOpened = false; |
||
72 | |||
73 | /** @var array[] List of (callable, method name) */ |
||
74 | protected $mTrxIdleCallbacks = []; |
||
75 | /** @var array[] List of (callable, method name) */ |
||
76 | protected $mTrxPreCommitCallbacks = []; |
||
77 | /** @var array[] List of (callable, method name) */ |
||
78 | protected $mTrxEndCallbacks = []; |
||
79 | /** @var array[] Map of (name => (callable, method name)) */ |
||
80 | protected $mTrxRecurringCallbacks = []; |
||
81 | /** @var bool Whether to suppress triggering of transaction end callbacks */ |
||
82 | protected $mTrxEndCallbacksSuppressed = false; |
||
83 | |||
84 | /** @var string */ |
||
85 | protected $mTablePrefix; |
||
86 | /** @var string */ |
||
87 | protected $mSchema; |
||
88 | /** @var integer */ |
||
89 | protected $mFlags; |
||
90 | /** @var bool */ |
||
91 | protected $mForeign; |
||
92 | /** @var array */ |
||
93 | protected $mLBInfo = []; |
||
94 | /** @var bool|null */ |
||
95 | protected $mDefaultBigSelects = null; |
||
96 | /** @var array|bool */ |
||
97 | protected $mSchemaVars = false; |
||
98 | /** @var array */ |
||
99 | protected $mSessionVars = []; |
||
100 | /** @var array|null */ |
||
101 | protected $preparedArgs; |
||
102 | /** @var string|bool|null Stashed value of html_errors INI setting */ |
||
103 | protected $htmlErrors; |
||
104 | /** @var string */ |
||
105 | protected $delimiter = ';'; |
||
106 | |||
107 | /** |
||
108 | * Either 1 if a transaction is active or 0 otherwise. |
||
109 | * The other Trx fields may not be meaningfull if this is 0. |
||
110 | * |
||
111 | * @var int |
||
112 | */ |
||
113 | protected $mTrxLevel = 0; |
||
114 | /** |
||
115 | * Either a short hexidecimal string if a transaction is active or "" |
||
116 | * |
||
117 | * @var string |
||
118 | * @see DatabaseBase::mTrxLevel |
||
119 | */ |
||
120 | protected $mTrxShortId = ''; |
||
121 | /** |
||
122 | * The UNIX time that the transaction started. Callers can assume that if |
||
123 | * snapshot isolation is used, then the data is *at least* up to date to that |
||
124 | * point (possibly more up-to-date since the first SELECT defines the snapshot). |
||
125 | * |
||
126 | * @var float|null |
||
127 | * @see DatabaseBase::mTrxLevel |
||
128 | */ |
||
129 | private $mTrxTimestamp = null; |
||
130 | /** @var float Lag estimate at the time of BEGIN */ |
||
131 | private $mTrxReplicaLag = null; |
||
132 | /** |
||
133 | * Remembers the function name given for starting the most recent transaction via begin(). |
||
134 | * Used to provide additional context for error reporting. |
||
135 | * |
||
136 | * @var string |
||
137 | * @see DatabaseBase::mTrxLevel |
||
138 | */ |
||
139 | private $mTrxFname = null; |
||
140 | /** |
||
141 | * Record if possible write queries were done in the last transaction started |
||
142 | * |
||
143 | * @var bool |
||
144 | * @see DatabaseBase::mTrxLevel |
||
145 | */ |
||
146 | private $mTrxDoneWrites = false; |
||
147 | /** |
||
148 | * Record if the current transaction was started implicitly due to DBO_TRX being set. |
||
149 | * |
||
150 | * @var bool |
||
151 | * @see DatabaseBase::mTrxLevel |
||
152 | */ |
||
153 | private $mTrxAutomatic = false; |
||
154 | /** |
||
155 | * Array of levels of atomicity within transactions |
||
156 | * |
||
157 | * @var array |
||
158 | */ |
||
159 | private $mTrxAtomicLevels = []; |
||
160 | /** |
||
161 | * Record if the current transaction was started implicitly by DatabaseBase::startAtomic |
||
162 | * |
||
163 | * @var bool |
||
164 | */ |
||
165 | private $mTrxAutomaticAtomic = false; |
||
166 | /** |
||
167 | * Track the write query callers of the current transaction |
||
168 | * |
||
169 | * @var string[] |
||
170 | */ |
||
171 | private $mTrxWriteCallers = []; |
||
172 | /** |
||
173 | * @var float Seconds spent in write queries for the current transaction |
||
174 | */ |
||
175 | private $mTrxWriteDuration = 0.0; |
||
176 | /** |
||
177 | * @var integer Number of write queries for the current transaction |
||
178 | */ |
||
179 | private $mTrxWriteQueryCount = 0; |
||
180 | /** |
||
181 | * @var float Like mTrxWriteQueryCount but excludes lock-bound, easy to replicate, queries |
||
182 | */ |
||
183 | private $mTrxWriteAdjDuration = 0.0; |
||
184 | /** |
||
185 | * @var integer Number of write queries counted in mTrxWriteAdjDuration |
||
186 | */ |
||
187 | private $mTrxWriteAdjQueryCount = 0; |
||
188 | /** |
||
189 | * @var float RTT time estimate |
||
190 | */ |
||
191 | private $mRTTEstimate = 0.0; |
||
192 | |||
193 | /** @var array Map of (name => 1) for locks obtained via lock() */ |
||
194 | private $mNamedLocksHeld = []; |
||
195 | |||
196 | /** @var IDatabase|null Lazy handle to the master DB this server replicates from */ |
||
197 | private $lazyMasterHandle; |
||
198 | |||
199 | /** |
||
200 | * @since 1.21 |
||
201 | * @var resource File handle for upgrade |
||
202 | */ |
||
203 | protected $fileHandle = null; |
||
204 | |||
205 | /** |
||
206 | * @since 1.22 |
||
207 | * @var string[] Process cache of VIEWs names in the database |
||
208 | */ |
||
209 | protected $allViews = null; |
||
210 | |||
211 | /** @var float UNIX timestamp */ |
||
212 | protected $lastPing = 0.0; |
||
213 | |||
214 | /** @var int[] Prior mFlags values */ |
||
215 | private $priorFlags = []; |
||
216 | |||
217 | /** @var Profiler */ |
||
218 | protected $profiler; |
||
219 | /** @var TransactionProfiler */ |
||
220 | protected $trxProfiler; |
||
221 | |||
222 | public function getServerInfo() { |
||
225 | |||
226 | /** |
||
227 | * @return string Command delimiter used by this database engine |
||
228 | */ |
||
229 | public function getDelimiter() { |
||
232 | |||
233 | /** |
||
234 | * Boolean, controls output of large amounts of debug information. |
||
235 | * @param bool|null $debug |
||
236 | * - true to enable debugging |
||
237 | * - false to disable debugging |
||
238 | * - omitted or null to do nothing |
||
239 | * |
||
240 | * @return bool|null Previous value of the flag |
||
241 | */ |
||
242 | public function debug( $debug = null ) { |
||
245 | |||
246 | public function bufferResults( $buffer = null ) { |
||
253 | |||
254 | /** |
||
255 | * Turns on (false) or off (true) the automatic generation and sending |
||
256 | * of a "we're sorry, but there has been a database error" page on |
||
257 | * database errors. Default is on (false). When turned off, the |
||
258 | * code should use lastErrno() and lastError() to handle the |
||
259 | * situation as appropriate. |
||
260 | * |
||
261 | * Do not use this function outside of the Database classes. |
||
262 | * |
||
263 | * @param null|bool $ignoreErrors |
||
264 | * @return bool The previous value of the flag. |
||
265 | */ |
||
266 | protected function ignoreErrors( $ignoreErrors = null ) { |
||
269 | |||
270 | public function trxLevel() { |
||
273 | |||
274 | public function trxTimestamp() { |
||
277 | |||
278 | public function tablePrefix( $prefix = null ) { |
||
281 | |||
282 | public function dbSchema( $schema = null ) { |
||
285 | |||
286 | /** |
||
287 | * Set the filehandle to copy write statements to. |
||
288 | * |
||
289 | * @param resource $fh File handle |
||
290 | */ |
||
291 | public function setFileHandle( $fh ) { |
||
294 | |||
295 | public function getLBInfo( $name = null ) { |
||
306 | |||
307 | public function setLBInfo( $name, $value = null ) { |
||
314 | |||
315 | /** |
||
316 | * Set a lazy-connecting DB handle to the master DB (for replication status purposes) |
||
317 | * |
||
318 | * @param IDatabase $conn |
||
319 | * @since 1.27 |
||
320 | */ |
||
321 | public function setLazyMasterHandle( IDatabase $conn ) { |
||
324 | |||
325 | /** |
||
326 | * @return IDatabase|null |
||
327 | * @see setLazyMasterHandle() |
||
328 | * @since 1.27 |
||
329 | */ |
||
330 | public function getLazyMasterHandle() { |
||
333 | |||
334 | /** |
||
335 | * @param TransactionProfiler $profiler |
||
336 | * @since 1.27 |
||
337 | */ |
||
338 | public function setTransactionProfiler( TransactionProfiler $profiler ) { |
||
341 | |||
342 | /** |
||
343 | * Returns true if this database supports (and uses) cascading deletes |
||
344 | * |
||
345 | * @return bool |
||
346 | */ |
||
347 | public function cascadingDeletes() { |
||
350 | |||
351 | /** |
||
352 | * Returns true if this database supports (and uses) triggers (e.g. on the page table) |
||
353 | * |
||
354 | * @return bool |
||
355 | */ |
||
356 | public function cleanupTriggers() { |
||
359 | |||
360 | /** |
||
361 | * Returns true if this database is strict about what can be put into an IP field. |
||
362 | * Specifically, it uses a NULL value instead of an empty string. |
||
363 | * |
||
364 | * @return bool |
||
365 | */ |
||
366 | public function strictIPs() { |
||
369 | |||
370 | /** |
||
371 | * Returns true if this database uses timestamps rather than integers |
||
372 | * |
||
373 | * @return bool |
||
374 | */ |
||
375 | public function realTimestamps() { |
||
378 | |||
379 | public function implicitGroupby() { |
||
382 | |||
383 | public function implicitOrderby() { |
||
386 | |||
387 | /** |
||
388 | * Returns true if this database can do a native search on IP columns |
||
389 | * e.g. this works as expected: .. WHERE rc_ip = '127.42.12.102/32'; |
||
390 | * |
||
391 | * @return bool |
||
392 | */ |
||
393 | public function searchableIPs() { |
||
396 | |||
397 | /** |
||
398 | * Returns true if this database can use functional indexes |
||
399 | * |
||
400 | * @return bool |
||
401 | */ |
||
402 | public function functionalIndexes() { |
||
405 | |||
406 | public function lastQuery() { |
||
409 | |||
410 | public function doneWrites() { |
||
413 | |||
414 | public function lastDoneWrites() { |
||
417 | |||
418 | public function writesPending() { |
||
421 | |||
422 | public function writesOrCallbacksPending() { |
||
427 | |||
428 | public function pendingWriteQueryDuration( $type = self::ESTIMATE_TOTAL ) { |
||
449 | |||
450 | public function pendingWriteCallers() { |
||
453 | |||
454 | public function isOpen() { |
||
457 | |||
458 | public function setFlag( $flag, $remember = self::REMEMBER_NOTHING ) { |
||
464 | |||
465 | public function clearFlag( $flag, $remember = self::REMEMBER_NOTHING ) { |
||
471 | |||
472 | public function restoreFlags( $state = self::RESTORE_PRIOR ) { |
||
484 | |||
485 | public function getFlag( $flag ) { |
||
488 | |||
489 | public function getProperty( $name ) { |
||
492 | |||
493 | public function getWikiID() { |
||
500 | |||
501 | /** |
||
502 | * Return a path to the DBMS-specific SQL file if it exists, |
||
503 | * otherwise default SQL file |
||
504 | * |
||
505 | * @param string $filename |
||
506 | * @return string |
||
507 | */ |
||
508 | View Code Duplication | private function getSqlFilePath( $filename ) { |
|
517 | |||
518 | /** |
||
519 | * Return a path to the DBMS-specific schema file, |
||
520 | * otherwise default to tables.sql |
||
521 | * |
||
522 | * @return string |
||
523 | */ |
||
524 | public function getSchemaPath() { |
||
527 | |||
528 | /** |
||
529 | * Return a path to the DBMS-specific update key file, |
||
530 | * otherwise default to update-keys.sql |
||
531 | * |
||
532 | * @return string |
||
533 | */ |
||
534 | public function getUpdateKeysPath() { |
||
537 | |||
538 | /** |
||
539 | * Get information about an index into an object |
||
540 | * @param string $table Table name |
||
541 | * @param string $index Index name |
||
542 | * @param string $fname Calling function name |
||
543 | * @return mixed Database-specific index description class or false if the index does not exist |
||
544 | */ |
||
545 | abstract function indexInfo( $table, $index, $fname = __METHOD__ ); |
||
546 | |||
547 | /** |
||
548 | * Wrapper for addslashes() |
||
549 | * |
||
550 | * @param string $s String to be slashed. |
||
551 | * @return string Slashed string. |
||
552 | */ |
||
553 | abstract function strencode( $s ); |
||
554 | |||
555 | /** |
||
556 | * Constructor. |
||
557 | * |
||
558 | * FIXME: It is possible to construct a Database object with no associated |
||
559 | * connection object, by specifying no parameters to __construct(). This |
||
560 | * feature is deprecated and should be removed. |
||
561 | * |
||
562 | * DatabaseBase subclasses should not be constructed directly in external |
||
563 | * code. DatabaseBase::factory() should be used instead. |
||
564 | * |
||
565 | * @param array $params Parameters passed from DatabaseBase::factory() |
||
566 | */ |
||
567 | function __construct( array $params ) { |
||
624 | |||
625 | /** |
||
626 | * Called by serialize. Throw an exception when DB connection is serialized. |
||
627 | * This causes problems on some database engines because the connection is |
||
628 | * not restored on unserialize. |
||
629 | */ |
||
630 | public function __sleep() { |
||
634 | |||
635 | /** |
||
636 | * Given a DB type, construct the name of the appropriate child class of |
||
637 | * DatabaseBase. This is designed to replace all of the manual stuff like: |
||
638 | * $class = 'Database' . ucfirst( strtolower( $dbType ) ); |
||
639 | * as well as validate against the canonical list of DB types we have |
||
640 | * |
||
641 | * This factory function is mostly useful for when you need to connect to a |
||
642 | * database other than the MediaWiki default (such as for external auth, |
||
643 | * an extension, et cetera). Do not use this to connect to the MediaWiki |
||
644 | * database. Example uses in core: |
||
645 | * @see LoadBalancer::reallyOpenConnection() |
||
646 | * @see ForeignDBRepo::getMasterDB() |
||
647 | * @see WebInstallerDBConnect::execute() |
||
648 | * |
||
649 | * @since 1.18 |
||
650 | * |
||
651 | * @param string $dbType A possible DB type |
||
652 | * @param array $p An array of options to pass to the constructor. |
||
653 | * Valid options are: host, user, password, dbname, flags, tablePrefix, schema, driver |
||
654 | * @throws MWException If the database driver or extension cannot be found |
||
655 | * @return DatabaseBase|null DatabaseBase subclass or null |
||
656 | */ |
||
657 | final public static function factory( $dbType, $p = [] ) { |
||
724 | |||
725 | protected function installErrorHandler() { |
||
730 | |||
731 | /** |
||
732 | * @return bool|string |
||
733 | */ |
||
734 | protected function restoreErrorHandler() { |
||
748 | |||
749 | /** |
||
750 | * @param int $errno |
||
751 | * @param string $errstr |
||
752 | */ |
||
753 | public function connectionErrorHandler( $errno, $errstr ) { |
||
756 | |||
757 | /** |
||
758 | * Create a log context to pass to wfLogDBError or other logging functions. |
||
759 | * |
||
760 | * @param array $extras Additional data to add to context |
||
761 | * @return array |
||
762 | */ |
||
763 | protected function getLogContext( array $extras = [] ) { |
||
773 | |||
774 | public function close() { |
||
796 | |||
797 | /** |
||
798 | * Make sure isOpen() returns true as a sanity check |
||
799 | * |
||
800 | * @throws DBUnexpectedError |
||
801 | */ |
||
802 | protected function assertOpen() { |
||
807 | |||
808 | /** |
||
809 | * Closes underlying database connection |
||
810 | * @since 1.20 |
||
811 | * @return bool Whether connection was closed successfully |
||
812 | */ |
||
813 | abstract protected function closeConnection(); |
||
814 | |||
815 | function reportConnectionError( $error = 'Unknown error' ) { |
||
824 | |||
825 | /** |
||
826 | * The DBMS-dependent part of query() |
||
827 | * |
||
828 | * @param string $sql SQL query. |
||
829 | * @return ResultWrapper|bool Result object to feed to fetchObject, |
||
830 | * fetchRow, ...; or false on failure |
||
831 | */ |
||
832 | abstract protected function doQuery( $sql ); |
||
833 | |||
834 | /** |
||
835 | * Determine whether a query writes to the DB. |
||
836 | * Should return true if unsure. |
||
837 | * |
||
838 | * @param string $sql |
||
839 | * @return bool |
||
840 | */ |
||
841 | protected function isWriteQuery( $sql ) { |
||
845 | |||
846 | /** |
||
847 | * @param $sql |
||
848 | * @return string|null |
||
849 | */ |
||
850 | protected function getQueryVerb( $sql ) { |
||
853 | |||
854 | /** |
||
855 | * Determine whether a SQL statement is sensitive to isolation level. |
||
856 | * A SQL statement is considered transactable if its result could vary |
||
857 | * depending on the transaction isolation level. Operational commands |
||
858 | * such as 'SET' and 'SHOW' are not considered to be transactable. |
||
859 | * |
||
860 | * @param string $sql |
||
861 | * @return bool |
||
862 | */ |
||
863 | protected function isTransactableQuery( $sql ) { |
||
867 | |||
868 | public function query( $sql, $fname = __METHOD__, $tempIgnore = false ) { |
||
970 | |||
971 | private function doProfiledQuery( $sql, $commentedSql, $isWrite, $fname ) { |
||
1011 | |||
1012 | /** |
||
1013 | * Update the estimated run-time of a query, not counting large row lock times |
||
1014 | * |
||
1015 | * LoadBalancer can be set to rollback transactions that will create huge replication |
||
1016 | * lag. It bases this estimate off of pendingWriteQueryDuration(). Certain simple |
||
1017 | * queries, like inserting a row can take a long time due to row locking. This method |
||
1018 | * uses some simple heuristics to discount those cases. |
||
1019 | * |
||
1020 | * @param string $sql A SQL write query |
||
1021 | * @param float $runtime Total runtime, including RTT |
||
1022 | */ |
||
1023 | private function updateTrxWriteQueryTime( $sql, $runtime ) { |
||
1043 | |||
1044 | private function canRecoverFromDisconnect( $sql, $priorWritesPending ) { |
||
1063 | |||
1064 | private function handleTransactionLoss() { |
||
1078 | |||
1079 | public function reportQueryError( $error, $errno, $sql, $fname, $tempIgnore = false ) { |
||
1098 | |||
1099 | /** |
||
1100 | * Intended to be compatible with the PEAR::DB wrapper functions. |
||
1101 | * http://pear.php.net/manual/en/package.database.db.intro-execute.php |
||
1102 | * |
||
1103 | * ? = scalar value, quoted as necessary |
||
1104 | * ! = raw SQL bit (a function for instance) |
||
1105 | * & = filename; reads the file and inserts as a blob |
||
1106 | * (we don't use this though...) |
||
1107 | * |
||
1108 | * @param string $sql |
||
1109 | * @param string $func |
||
1110 | * |
||
1111 | * @return array |
||
1112 | */ |
||
1113 | protected function prepare( $sql, $func = 'DatabaseBase::prepare' ) { |
||
1120 | |||
1121 | /** |
||
1122 | * Free a prepared query, generated by prepare(). |
||
1123 | * @param string $prepared |
||
1124 | */ |
||
1125 | protected function freePrepared( $prepared ) { |
||
1128 | |||
1129 | /** |
||
1130 | * Execute a prepared query with the various arguments |
||
1131 | * @param string $prepared The prepared sql |
||
1132 | * @param mixed $args Either an array here, or put scalars as varargs |
||
1133 | * |
||
1134 | * @return ResultWrapper |
||
1135 | */ |
||
1136 | public function execute( $prepared, $args = null ) { |
||
1147 | |||
1148 | /** |
||
1149 | * For faking prepared SQL statements on DBs that don't support it directly. |
||
1150 | * |
||
1151 | * @param string $preparedQuery A 'preparable' SQL statement |
||
1152 | * @param array $args Array of Arguments to fill it with |
||
1153 | * @return string Executable SQL |
||
1154 | */ |
||
1155 | public function fillPrepared( $preparedQuery, $args ) { |
||
1162 | |||
1163 | /** |
||
1164 | * preg_callback func for fillPrepared() |
||
1165 | * The arguments should be in $this->preparedArgs and must not be touched |
||
1166 | * while we're doing this. |
||
1167 | * |
||
1168 | * @param array $matches |
||
1169 | * @throws DBUnexpectedError |
||
1170 | * @return string |
||
1171 | */ |
||
1172 | protected function fillPreparedArg( $matches ) { |
||
1202 | |||
1203 | public function freeResult( $res ) { |
||
1205 | |||
1206 | public function selectField( |
||
1232 | |||
1233 | public function selectFieldValues( |
||
1258 | |||
1259 | /** |
||
1260 | * Returns an optional USE INDEX clause to go after the table, and a |
||
1261 | * string to go at the end of the query. |
||
1262 | * |
||
1263 | * @param array $options Associative array of options to be turned into |
||
1264 | * an SQL query, valid keys are listed in the function. |
||
1265 | * @return array |
||
1266 | * @see DatabaseBase::select() |
||
1267 | */ |
||
1268 | public function makeSelectOptions( $options ) { |
||
1348 | |||
1349 | /** |
||
1350 | * Returns an optional GROUP BY with an optional HAVING |
||
1351 | * |
||
1352 | * @param array $options Associative array of options |
||
1353 | * @return string |
||
1354 | * @see DatabaseBase::select() |
||
1355 | * @since 1.21 |
||
1356 | */ |
||
1357 | public function makeGroupByWithHaving( $options ) { |
||
1374 | |||
1375 | /** |
||
1376 | * Returns an optional ORDER BY |
||
1377 | * |
||
1378 | * @param array $options Associative array of options |
||
1379 | * @return string |
||
1380 | * @see DatabaseBase::select() |
||
1381 | * @since 1.21 |
||
1382 | */ |
||
1383 | public function makeOrderBy( $options ) { |
||
1394 | |||
1395 | // See IDatabase::select for the docs for this function |
||
1396 | public function select( $table, $vars, $conds = '', $fname = __METHOD__, |
||
1402 | |||
1403 | public function selectSQLText( $table, $vars, $conds = '', $fname = __METHOD__, |
||
1456 | |||
1457 | public function selectRow( $table, $vars, $conds, $fname = __METHOD__, |
||
1476 | |||
1477 | public function estimateRowCount( |
||
1490 | |||
1491 | public function selectRowCount( |
||
1505 | |||
1506 | /** |
||
1507 | * Removes most variables from an SQL query and replaces them with X or N for numbers. |
||
1508 | * It's only slightly flawed. Don't use for anything important. |
||
1509 | * |
||
1510 | * @param string $sql A SQL Query |
||
1511 | * |
||
1512 | * @return string |
||
1513 | */ |
||
1514 | protected static function generalizeSQL( $sql ) { |
||
1535 | |||
1536 | public function fieldExists( $table, $field, $fname = __METHOD__ ) { |
||
1541 | |||
1542 | public function indexExists( $table, $index, $fname = __METHOD__ ) { |
||
1554 | |||
1555 | public function tableExists( $table, $fname = __METHOD__ ) { |
||
1563 | |||
1564 | public function indexUnique( $table, $index ) { |
||
1573 | |||
1574 | /** |
||
1575 | * Helper for DatabaseBase::insert(). |
||
1576 | * |
||
1577 | * @param array $options |
||
1578 | * @return string |
||
1579 | */ |
||
1580 | protected function makeInsertOptions( $options ) { |
||
1583 | |||
1584 | public function insert( $table, $a, $fname = __METHOD__, $options = [] ) { |
||
1635 | |||
1636 | /** |
||
1637 | * Make UPDATE options array for DatabaseBase::makeUpdateOptions |
||
1638 | * |
||
1639 | * @param array $options |
||
1640 | * @return array |
||
1641 | */ |
||
1642 | protected function makeUpdateOptionsArray( $options ) { |
||
1659 | |||
1660 | /** |
||
1661 | * Make UPDATE options for the DatabaseBase::update function |
||
1662 | * |
||
1663 | * @param array $options The options passed to DatabaseBase::update |
||
1664 | * @return string |
||
1665 | */ |
||
1666 | protected function makeUpdateOptions( $options ) { |
||
1671 | |||
1672 | function update( $table, $values, $conds, $fname = __METHOD__, $options = [] ) { |
||
1683 | |||
1684 | public function makeList( $a, $mode = LIST_COMMA ) { |
||
1758 | |||
1759 | public function makeWhereFrom2d( $data, $baseKey, $subKey ) { |
||
1777 | |||
1778 | /** |
||
1779 | * Return aggregated value alias |
||
1780 | * |
||
1781 | * @param array $valuedata |
||
1782 | * @param string $valuename |
||
1783 | * |
||
1784 | * @return string |
||
1785 | */ |
||
1786 | public function aggregateValue( $valuedata, $valuename = 'value' ) { |
||
1789 | |||
1790 | public function bitNot( $field ) { |
||
1793 | |||
1794 | public function bitAnd( $fieldLeft, $fieldRight ) { |
||
1797 | |||
1798 | public function bitOr( $fieldLeft, $fieldRight ) { |
||
1801 | |||
1802 | public function buildConcat( $stringList ) { |
||
1805 | |||
1806 | View Code Duplication | public function buildGroupConcatField( |
|
1813 | |||
1814 | /** |
||
1815 | * @param string $field Field or column to cast |
||
1816 | * @return string |
||
1817 | * @since 1.28 |
||
1818 | */ |
||
1819 | public function buildStringCast( $field ) { |
||
1822 | |||
1823 | public function selectDB( $db ) { |
||
1831 | |||
1832 | public function getDBname() { |
||
1835 | |||
1836 | public function getServer() { |
||
1839 | |||
1840 | /** |
||
1841 | * Format a table name ready for use in constructing an SQL query |
||
1842 | * |
||
1843 | * This does two important things: it quotes the table names to clean them up, |
||
1844 | * and it adds a table prefix if only given a table name with no quotes. |
||
1845 | * |
||
1846 | * All functions of this object which require a table name call this function |
||
1847 | * themselves. Pass the canonical name to such functions. This is only needed |
||
1848 | * when calling query() directly. |
||
1849 | * |
||
1850 | * @note This function does not sanitize user input. It is not safe to use |
||
1851 | * this function to escape user input. |
||
1852 | * @param string $name Database table name |
||
1853 | * @param string $format One of: |
||
1854 | * quoted - Automatically pass the table name through addIdentifierQuotes() |
||
1855 | * so that it can be used in a query. |
||
1856 | * raw - Do not add identifier quotes to the table name |
||
1857 | * @return string Full database name |
||
1858 | */ |
||
1859 | public function tableName( $name, $format = 'quoted' ) { |
||
1937 | |||
1938 | /** |
||
1939 | * Fetch a number of table names into an array |
||
1940 | * This is handy when you need to construct SQL for joins |
||
1941 | * |
||
1942 | * Example: |
||
1943 | * extract( $dbr->tableNames( 'user', 'watchlist' ) ); |
||
1944 | * $sql = "SELECT wl_namespace,wl_title FROM $watchlist,$user |
||
1945 | * WHERE wl_user=user_id AND wl_user=$nameWithQuotes"; |
||
1946 | * |
||
1947 | * @return array |
||
1948 | */ |
||
1949 | View Code Duplication | public function tableNames() { |
|
1959 | |||
1960 | /** |
||
1961 | * Fetch a number of table names into an zero-indexed numerical array |
||
1962 | * This is handy when you need to construct SQL for joins |
||
1963 | * |
||
1964 | * Example: |
||
1965 | * list( $user, $watchlist ) = $dbr->tableNamesN( 'user', 'watchlist' ); |
||
1966 | * $sql = "SELECT wl_namespace,wl_title FROM $watchlist,$user |
||
1967 | * WHERE wl_user=user_id AND wl_user=$nameWithQuotes"; |
||
1968 | * |
||
1969 | * @return array |
||
1970 | */ |
||
1971 | View Code Duplication | public function tableNamesN() { |
|
1981 | |||
1982 | /** |
||
1983 | * Get an aliased table name |
||
1984 | * e.g. tableName AS newTableName |
||
1985 | * |
||
1986 | * @param string $name Table name, see tableName() |
||
1987 | * @param string|bool $alias Alias (optional) |
||
1988 | * @return string SQL name for aliased table. Will not alias a table to its own name |
||
1989 | */ |
||
1990 | public function tableNameWithAlias( $name, $alias = false ) { |
||
1997 | |||
1998 | /** |
||
1999 | * Gets an array of aliased table names |
||
2000 | * |
||
2001 | * @param array $tables [ [alias] => table ] |
||
2002 | * @return string[] See tableNameWithAlias() |
||
2003 | */ |
||
2004 | public function tableNamesWithAlias( $tables ) { |
||
2015 | |||
2016 | /** |
||
2017 | * Get an aliased field name |
||
2018 | * e.g. fieldName AS newFieldName |
||
2019 | * |
||
2020 | * @param string $name Field name |
||
2021 | * @param string|bool $alias Alias (optional) |
||
2022 | * @return string SQL name for aliased field. Will not alias a field to its own name |
||
2023 | */ |
||
2024 | public function fieldNameWithAlias( $name, $alias = false ) { |
||
2031 | |||
2032 | /** |
||
2033 | * Gets an array of aliased field names |
||
2034 | * |
||
2035 | * @param array $fields [ [alias] => field ] |
||
2036 | * @return string[] See fieldNameWithAlias() |
||
2037 | */ |
||
2038 | public function fieldNamesWithAlias( $fields ) { |
||
2049 | |||
2050 | /** |
||
2051 | * Get the aliased table name clause for a FROM clause |
||
2052 | * which might have a JOIN and/or USE INDEX or IGNORE INDEX clause |
||
2053 | * |
||
2054 | * @param array $tables ( [alias] => table ) |
||
2055 | * @param array $use_index Same as for select() |
||
2056 | * @param array $ignore_index Same as for select() |
||
2057 | * @param array $join_conds Same as for select() |
||
2058 | * @return string |
||
2059 | */ |
||
2060 | protected function tableNamesWithIndexClauseOrJOIN( |
||
2127 | |||
2128 | /** |
||
2129 | * Get the name of an index in a given table. |
||
2130 | * |
||
2131 | * @param string $index |
||
2132 | * @return string |
||
2133 | */ |
||
2134 | protected function indexName( $index ) { |
||
2148 | |||
2149 | public function addQuotes( $s ) { |
||
2163 | |||
2164 | /** |
||
2165 | * Quotes an identifier using `backticks` or "double quotes" depending on the database type. |
||
2166 | * MySQL uses `backticks` while basically everything else uses double quotes. |
||
2167 | * Since MySQL is the odd one out here the double quotes are our generic |
||
2168 | * and we implement backticks in DatabaseMysql. |
||
2169 | * |
||
2170 | * @param string $s |
||
2171 | * @return string |
||
2172 | */ |
||
2173 | public function addIdentifierQuotes( $s ) { |
||
2176 | |||
2177 | /** |
||
2178 | * Returns if the given identifier looks quoted or not according to |
||
2179 | * the database convention for quoting identifiers . |
||
2180 | * |
||
2181 | * @note Do not use this to determine if untrusted input is safe. |
||
2182 | * A malicious user can trick this function. |
||
2183 | * @param string $name |
||
2184 | * @return bool |
||
2185 | */ |
||
2186 | public function isQuotedIdentifier( $name ) { |
||
2189 | |||
2190 | /** |
||
2191 | * @param string $s |
||
2192 | * @return string |
||
2193 | */ |
||
2194 | protected function escapeLikeInternal( $s ) { |
||
2197 | |||
2198 | public function buildLike() { |
||
2217 | |||
2218 | public function anyChar() { |
||
2221 | |||
2222 | public function anyString() { |
||
2225 | |||
2226 | public function nextSequenceValue( $seqName ) { |
||
2229 | |||
2230 | /** |
||
2231 | * USE INDEX clause. Unlikely to be useful for anything but MySQL. This |
||
2232 | * is only needed because a) MySQL must be as efficient as possible due to |
||
2233 | * its use on Wikipedia, and b) MySQL 4.0 is kind of dumb sometimes about |
||
2234 | * which index to pick. Anyway, other databases might have different |
||
2235 | * indexes on a given table. So don't bother overriding this unless you're |
||
2236 | * MySQL. |
||
2237 | * @param string $index |
||
2238 | * @return string |
||
2239 | */ |
||
2240 | public function useIndexClause( $index ) { |
||
2243 | |||
2244 | /** |
||
2245 | * IGNORE INDEX clause. Unlikely to be useful for anything but MySQL. This |
||
2246 | * is only needed because a) MySQL must be as efficient as possible due to |
||
2247 | * its use on Wikipedia, and b) MySQL 4.0 is kind of dumb sometimes about |
||
2248 | * which index to pick. Anyway, other databases might have different |
||
2249 | * indexes on a given table. So don't bother overriding this unless you're |
||
2250 | * MySQL. |
||
2251 | * @param string $index |
||
2252 | * @return string |
||
2253 | */ |
||
2254 | public function ignoreIndexClause( $index ) { |
||
2257 | |||
2258 | public function replace( $table, $uniqueIndexes, $rows, $fname = __METHOD__ ) { |
||
2305 | |||
2306 | /** |
||
2307 | * REPLACE query wrapper for MySQL and SQLite, which have a native REPLACE |
||
2308 | * statement. |
||
2309 | * |
||
2310 | * @param string $table Table name |
||
2311 | * @param array|string $rows Row(s) to insert |
||
2312 | * @param string $fname Caller function name |
||
2313 | * |
||
2314 | * @return ResultWrapper |
||
2315 | */ |
||
2316 | protected function nativeReplace( $table, $rows, $fname ) { |
||
2339 | |||
2340 | public function upsert( $table, array $rows, array $uniqueIndexes, array $set, |
||
2393 | |||
2394 | View Code Duplication | public function deleteJoin( $delTable, $joinTable, $delVar, $joinVar, $conds, |
|
2412 | |||
2413 | /** |
||
2414 | * Returns the size of a text field, or -1 for "unlimited" |
||
2415 | * |
||
2416 | * @param string $table |
||
2417 | * @param string $field |
||
2418 | * @return int |
||
2419 | */ |
||
2420 | public function textFieldSize( $table, $field ) { |
||
2436 | |||
2437 | /** |
||
2438 | * A string to insert into queries to show that they're low-priority, like |
||
2439 | * MySQL's LOW_PRIORITY. If no such feature exists, return an empty |
||
2440 | * string and nothing bad should happen. |
||
2441 | * |
||
2442 | * @return string Returns the text of the low priority option if it is |
||
2443 | * supported, or a blank string otherwise |
||
2444 | */ |
||
2445 | public function lowPriorityOption() { |
||
2448 | |||
2449 | public function delete( $table, $conds, $fname = __METHOD__ ) { |
||
2466 | |||
2467 | public function insertSelect( |
||
2505 | |||
2506 | public function nativeInsertSelect( $destTable, $srcTable, $varMap, $conds, |
||
2546 | |||
2547 | /** |
||
2548 | * Construct a LIMIT query with optional offset. This is used for query |
||
2549 | * pages. The SQL should be adjusted so that only the first $limit rows |
||
2550 | * are returned. If $offset is provided as well, then the first $offset |
||
2551 | * rows should be discarded, and the next $limit rows should be returned. |
||
2552 | * If the result of the query is not ordered, then the rows to be returned |
||
2553 | * are theoretically arbitrary. |
||
2554 | * |
||
2555 | * $sql is expected to be a SELECT, if that makes a difference. |
||
2556 | * |
||
2557 | * The version provided by default works in MySQL and SQLite. It will very |
||
2558 | * likely need to be overridden for most other DBMSes. |
||
2559 | * |
||
2560 | * @param string $sql SQL query we will append the limit too |
||
2561 | * @param int $limit The SQL limit |
||
2562 | * @param int|bool $offset The SQL offset (default false) |
||
2563 | * @throws DBUnexpectedError |
||
2564 | * @return string |
||
2565 | */ |
||
2566 | public function limitResult( $sql, $limit, $offset = false ) { |
||
2575 | |||
2576 | public function unionSupportsOrderAndLimit() { |
||
2579 | |||
2580 | public function unionQueries( $sqls, $all ) { |
||
2585 | |||
2586 | public function conditional( $cond, $trueVal, $falseVal ) { |
||
2593 | |||
2594 | public function strreplace( $orig, $old, $new ) { |
||
2597 | |||
2598 | public function getServerUptime() { |
||
2601 | |||
2602 | public function wasDeadlock() { |
||
2605 | |||
2606 | public function wasLockTimeout() { |
||
2609 | |||
2610 | public function wasErrorReissuable() { |
||
2613 | |||
2614 | public function wasReadOnlyError() { |
||
2617 | |||
2618 | /** |
||
2619 | * Determines if the given query error was a connection drop |
||
2620 | * STUB |
||
2621 | * |
||
2622 | * @param integer|string $errno |
||
2623 | * @return bool |
||
2624 | */ |
||
2625 | public function wasConnectionError( $errno ) { |
||
2628 | |||
2629 | /** |
||
2630 | * Perform a deadlock-prone transaction. |
||
2631 | * |
||
2632 | * This function invokes a callback function to perform a set of write |
||
2633 | * queries. If a deadlock occurs during the processing, the transaction |
||
2634 | * will be rolled back and the callback function will be called again. |
||
2635 | * |
||
2636 | * Avoid using this method outside of Job or Maintenance classes. |
||
2637 | * |
||
2638 | * Usage: |
||
2639 | * $dbw->deadlockLoop( callback, ... ); |
||
2640 | * |
||
2641 | * Extra arguments are passed through to the specified callback function. |
||
2642 | * This method requires that no transactions are already active to avoid |
||
2643 | * causing premature commits or exceptions. |
||
2644 | * |
||
2645 | * Returns whatever the callback function returned on its successful, |
||
2646 | * iteration, or false on error, for example if the retry limit was |
||
2647 | * reached. |
||
2648 | * |
||
2649 | * @return mixed |
||
2650 | * @throws DBUnexpectedError |
||
2651 | * @throws Exception |
||
2652 | */ |
||
2653 | public function deadlockLoop() { |
||
2688 | |||
2689 | public function masterPosWait( DBMasterPos $pos, $timeout ) { |
||
2693 | |||
2694 | public function getSlavePos() { |
||
2698 | |||
2699 | public function getMasterPos() { |
||
2703 | |||
2704 | public function serverIsReadOnly() { |
||
2707 | |||
2708 | final public function onTransactionResolution( callable $callback ) { |
||
2714 | |||
2715 | final public function onTransactionIdle( callable $callback ) { |
||
2721 | |||
2722 | final public function onTransactionPreCommitOrIdle( callable $callback ) { |
||
2737 | |||
2738 | final public function setTransactionListener( $name, callable $callback = null ) { |
||
2745 | |||
2746 | /** |
||
2747 | * Whether to disable running of post-COMMIT/ROLLBACK callbacks |
||
2748 | * |
||
2749 | * This method should not be used outside of Database/LoadBalancer |
||
2750 | * |
||
2751 | * @param bool $suppress |
||
2752 | * @since 1.28 |
||
2753 | */ |
||
2754 | final public function setTrxEndCallbackSuppression( $suppress ) { |
||
2757 | |||
2758 | /** |
||
2759 | * Actually run and consume any "on transaction idle/resolution" callbacks. |
||
2760 | * |
||
2761 | * This method should not be used outside of Database/LoadBalancer |
||
2762 | * |
||
2763 | * @param integer $trigger IDatabase::TRIGGER_* constant |
||
2764 | * @since 1.20 |
||
2765 | * @throws Exception |
||
2766 | */ |
||
2767 | public function runOnTransactionIdleCallbacks( $trigger ) { |
||
2808 | |||
2809 | /** |
||
2810 | * Actually run and consume any "on transaction pre-commit" callbacks. |
||
2811 | * |
||
2812 | * This method should not be used outside of Database/LoadBalancer |
||
2813 | * |
||
2814 | * @since 1.22 |
||
2815 | * @throws Exception |
||
2816 | */ |
||
2817 | public function runOnTransactionPreCommitCallbacks() { |
||
2837 | |||
2838 | /** |
||
2839 | * Actually run any "transaction listener" callbacks. |
||
2840 | * |
||
2841 | * This method should not be used outside of Database/LoadBalancer |
||
2842 | * |
||
2843 | * @param integer $trigger IDatabase::TRIGGER_* constant |
||
2844 | * @throws Exception |
||
2845 | * @since 1.20 |
||
2846 | */ |
||
2847 | public function runTransactionListenerCallbacks( $trigger ) { |
||
2869 | |||
2870 | final public function startAtomic( $fname = __METHOD__ ) { |
||
2882 | |||
2883 | final public function endAtomic( $fname = __METHOD__ ) { |
||
2897 | |||
2898 | final public function doAtomicSection( $fname, callable $callback ) { |
||
2910 | |||
2911 | final public function begin( $fname = __METHOD__, $mode = self::TRANSACTION_EXPLICIT ) { |
||
2958 | |||
2959 | /** |
||
2960 | * Issues the BEGIN command to the database server. |
||
2961 | * |
||
2962 | * @see DatabaseBase::begin() |
||
2963 | * @param string $fname |
||
2964 | */ |
||
2965 | protected function doBegin( $fname ) { |
||
2969 | |||
2970 | final public function commit( $fname = __METHOD__, $flush = '' ) { |
||
3017 | |||
3018 | /** |
||
3019 | * Issues the COMMIT command to the database server. |
||
3020 | * |
||
3021 | * @see DatabaseBase::commit() |
||
3022 | * @param string $fname |
||
3023 | */ |
||
3024 | protected function doCommit( $fname ) { |
||
3030 | |||
3031 | final public function rollback( $fname = __METHOD__, $flush = '' ) { |
||
3063 | |||
3064 | /** |
||
3065 | * Issues the ROLLBACK command to the database server. |
||
3066 | * |
||
3067 | * @see DatabaseBase::rollback() |
||
3068 | * @param string $fname |
||
3069 | */ |
||
3070 | protected function doRollback( $fname ) { |
||
3078 | |||
3079 | public function flushSnapshot( $fname = __METHOD__ ) { |
||
3090 | |||
3091 | public function explicitTrxActive() { |
||
3094 | |||
3095 | /** |
||
3096 | * Creates a new table with structure copied from existing table |
||
3097 | * Note that unlike most database abstraction functions, this function does not |
||
3098 | * automatically append database prefix, because it works at a lower |
||
3099 | * abstraction level. |
||
3100 | * The table names passed to this function shall not be quoted (this |
||
3101 | * function calls addIdentifierQuotes when needed). |
||
3102 | * |
||
3103 | * @param string $oldName Name of table whose structure should be copied |
||
3104 | * @param string $newName Name of table to be created |
||
3105 | * @param bool $temporary Whether the new table should be temporary |
||
3106 | * @param string $fname Calling function name |
||
3107 | * @throws MWException |
||
3108 | * @return bool True if operation was successful |
||
3109 | */ |
||
3110 | public function duplicateTableStructure( $oldName, $newName, $temporary = false, |
||
3116 | |||
3117 | function listTables( $prefix = null, $fname = __METHOD__ ) { |
||
3120 | |||
3121 | /** |
||
3122 | * Reset the views process cache set by listViews() |
||
3123 | * @since 1.22 |
||
3124 | */ |
||
3125 | final public function clearViewsCache() { |
||
3128 | |||
3129 | /** |
||
3130 | * Lists all the VIEWs in the database |
||
3131 | * |
||
3132 | * For caching purposes the list of all views should be stored in |
||
3133 | * $this->allViews. The process cache can be cleared with clearViewsCache() |
||
3134 | * |
||
3135 | * @param string $prefix Only show VIEWs with this prefix, eg. unit_test_ |
||
3136 | * @param string $fname Name of calling function |
||
3137 | * @throws MWException |
||
3138 | * @return array |
||
3139 | * @since 1.22 |
||
3140 | */ |
||
3141 | public function listViews( $prefix = null, $fname = __METHOD__ ) { |
||
3144 | |||
3145 | /** |
||
3146 | * Differentiates between a TABLE and a VIEW |
||
3147 | * |
||
3148 | * @param string $name Name of the database-structure to test. |
||
3149 | * @throws MWException |
||
3150 | * @return bool |
||
3151 | * @since 1.22 |
||
3152 | */ |
||
3153 | public function isView( $name ) { |
||
3156 | |||
3157 | public function timestamp( $ts = 0 ) { |
||
3160 | |||
3161 | public function timestampOrNull( $ts = null ) { |
||
3168 | |||
3169 | /** |
||
3170 | * Take the result from a query, and wrap it in a ResultWrapper if |
||
3171 | * necessary. Boolean values are passed through as is, to indicate success |
||
3172 | * of write queries or failure. |
||
3173 | * |
||
3174 | * Once upon a time, DatabaseBase::query() returned a bare MySQL result |
||
3175 | * resource, and it was necessary to call this function to convert it to |
||
3176 | * a wrapper. Nowadays, raw database objects are never exposed to external |
||
3177 | * callers, so this is unnecessary in external code. |
||
3178 | * |
||
3179 | * @param bool|ResultWrapper|resource|object $result |
||
3180 | * @return bool|ResultWrapper |
||
3181 | */ |
||
3182 | protected function resultObject( $result ) { |
||
3194 | |||
3195 | public function ping( &$rtt = null ) { |
||
3215 | |||
3216 | /** |
||
3217 | * @return bool |
||
3218 | */ |
||
3219 | protected function reconnect() { |
||
3233 | |||
3234 | public function getSessionLagStatus() { |
||
3237 | |||
3238 | /** |
||
3239 | * Get the replica DB lag when the current transaction started |
||
3240 | * |
||
3241 | * This is useful when transactions might use snapshot isolation |
||
3242 | * (e.g. REPEATABLE-READ in innodb), so the "real" lag of that data |
||
3243 | * is this lag plus transaction duration. If they don't, it is still |
||
3244 | * safe to be pessimistic. This returns null if there is no transaction. |
||
3245 | * |
||
3246 | * @return array|null ('lag': seconds or false on error, 'since': UNIX timestamp of BEGIN) |
||
3247 | * @since 1.27 |
||
3248 | */ |
||
3249 | public function getTransactionLagStatus() { |
||
3254 | |||
3255 | /** |
||
3256 | * Get a replica DB lag estimate for this server |
||
3257 | * |
||
3258 | * @return array ('lag': seconds or false on error, 'since': UNIX timestamp of estimate) |
||
3259 | * @since 1.27 |
||
3260 | */ |
||
3261 | public function getApproximateLagStatus() { |
||
3267 | |||
3268 | /** |
||
3269 | * Merge the result of getSessionLagStatus() for several DBs |
||
3270 | * using the most pessimistic values to estimate the lag of |
||
3271 | * any data derived from them in combination |
||
3272 | * |
||
3273 | * This is information is useful for caching modules |
||
3274 | * |
||
3275 | * @see WANObjectCache::set() |
||
3276 | * @see WANObjectCache::getWithSetCallback() |
||
3277 | * |
||
3278 | * @param IDatabase $db1 |
||
3279 | * @param IDatabase ... |
||
3280 | * @return array Map of values: |
||
3281 | * - lag: highest lag of any of the DBs or false on error (e.g. replication stopped) |
||
3282 | * - since: oldest UNIX timestamp of any of the DB lag estimates |
||
3283 | * - pending: whether any of the DBs have uncommitted changes |
||
3284 | * @since 1.27 |
||
3285 | */ |
||
3286 | public static function getCacheSetOptions( IDatabase $db1 ) { |
||
3302 | |||
3303 | public function getLag() { |
||
3306 | |||
3307 | function maxListLen() { |
||
3310 | |||
3311 | public function encodeBlob( $b ) { |
||
3314 | |||
3315 | public function decodeBlob( $b ) { |
||
3321 | |||
3322 | public function setSessionOptions( array $options ) { |
||
3324 | |||
3325 | /** |
||
3326 | * Read and execute SQL commands from a file. |
||
3327 | * |
||
3328 | * Returns true on success, error string or exception on failure (depending |
||
3329 | * on object's error ignore settings). |
||
3330 | * |
||
3331 | * @param string $filename File name to open |
||
3332 | * @param bool|callable $lineCallback Optional function called before reading each line |
||
3333 | * @param bool|callable $resultCallback Optional function called for each MySQL result |
||
3334 | * @param bool|string $fname Calling function name or false if name should be |
||
3335 | * generated dynamically using $filename |
||
3336 | * @param bool|callable $inputCallback Optional function called for each |
||
3337 | * complete line sent |
||
3338 | * @throws Exception|MWException |
||
3339 | * @return bool|string |
||
3340 | */ |
||
3341 | public function sourceFile( |
||
3367 | |||
3368 | /** |
||
3369 | * Get the full path of a patch file. Originally based on archive() |
||
3370 | * from updaters.inc. Keep in mind this always returns a patch, as |
||
3371 | * it fails back to MySQL if no DB-specific patch can be found |
||
3372 | * |
||
3373 | * @param string $patch The name of the patch, like patch-something.sql |
||
3374 | * @return string Full path to patch file |
||
3375 | */ |
||
3376 | View Code Duplication | public function patchPath( $patch ) { |
|
3386 | |||
3387 | public function setSchemaVars( $vars ) { |
||
3390 | |||
3391 | /** |
||
3392 | * Read and execute commands from an open file handle. |
||
3393 | * |
||
3394 | * Returns true on success, error string or exception on failure (depending |
||
3395 | * on object's error ignore settings). |
||
3396 | * |
||
3397 | * @param resource $fp File handle |
||
3398 | * @param bool|callable $lineCallback Optional function called before reading each query |
||
3399 | * @param bool|callable $resultCallback Optional function called for each MySQL result |
||
3400 | * @param string $fname Calling function name |
||
3401 | * @param bool|callable $inputCallback Optional function called for each complete query sent |
||
3402 | * @return bool|string |
||
3403 | */ |
||
3404 | public function sourceStream( $fp, $lineCallback = false, $resultCallback = false, |
||
3454 | |||
3455 | /** |
||
3456 | * Called by sourceStream() to check if we've reached a statement end |
||
3457 | * |
||
3458 | * @param string $sql SQL assembled so far |
||
3459 | * @param string $newLine New line about to be added to $sql |
||
3460 | * @return bool Whether $newLine contains end of the statement |
||
3461 | */ |
||
3462 | public function streamStatementEnd( &$sql, &$newLine ) { |
||
3473 | |||
3474 | /** |
||
3475 | * Database independent variable replacement. Replaces a set of variables |
||
3476 | * in an SQL statement with their contents as given by $this->getSchemaVars(). |
||
3477 | * |
||
3478 | * Supports '{$var}' `{$var}` and / *$var* / (without the spaces) style variables. |
||
3479 | * |
||
3480 | * - '{$var}' should be used for text and is passed through the database's |
||
3481 | * addQuotes method. |
||
3482 | * - `{$var}` should be used for identifiers (e.g. table and database names). |
||
3483 | * It is passed through the database's addIdentifierQuotes method which |
||
3484 | * can be overridden if the database uses something other than backticks. |
||
3485 | * - / *_* / or / *$wgDBprefix* / passes the name that follows through the |
||
3486 | * database's tableName method. |
||
3487 | * - / *i* / passes the name that follows through the database's indexName method. |
||
3488 | * - In all other cases, / *$var* / is left unencoded. Except for table options, |
||
3489 | * its use should be avoided. In 1.24 and older, string encoding was applied. |
||
3490 | * |
||
3491 | * @param string $ins SQL statement to replace variables in |
||
3492 | * @return string The new SQL statement with variables replaced |
||
3493 | */ |
||
3494 | protected function replaceVars( $ins ) { |
||
3525 | |||
3526 | /** |
||
3527 | * Get schema variables. If none have been set via setSchemaVars(), then |
||
3528 | * use some defaults from the current object. |
||
3529 | * |
||
3530 | * @return array |
||
3531 | */ |
||
3532 | protected function getSchemaVars() { |
||
3539 | |||
3540 | /** |
||
3541 | * Get schema variables to use if none have been set via setSchemaVars(). |
||
3542 | * |
||
3543 | * Override this in derived classes to provide variables for tables.sql |
||
3544 | * and SQL patch files. |
||
3545 | * |
||
3546 | * @return array |
||
3547 | */ |
||
3548 | protected function getDefaultSchemaVars() { |
||
3551 | |||
3552 | public function lockIsFree( $lockName, $method ) { |
||
3555 | |||
3556 | public function lock( $lockName, $method, $timeout = 5 ) { |
||
3561 | |||
3562 | public function unlock( $lockName, $method ) { |
||
3567 | |||
3568 | public function getScopedLockAndFlush( $lockKey, $fname, $timeout ) { |
||
3598 | |||
3599 | public function namedLocksEnqueue() { |
||
3602 | |||
3603 | /** |
||
3604 | * Lock specific tables |
||
3605 | * |
||
3606 | * @param array $read Array of tables to lock for read access |
||
3607 | * @param array $write Array of tables to lock for write access |
||
3608 | * @param string $method Name of caller |
||
3609 | * @param bool $lowPriority Whether to indicate writes to be LOW PRIORITY |
||
3610 | * @return bool |
||
3611 | */ |
||
3612 | public function lockTables( $read, $write, $method, $lowPriority = true ) { |
||
3615 | |||
3616 | /** |
||
3617 | * Unlock specific tables |
||
3618 | * |
||
3619 | * @param string $method The caller |
||
3620 | * @return bool |
||
3621 | */ |
||
3622 | public function unlockTables( $method ) { |
||
3625 | |||
3626 | /** |
||
3627 | * Delete a table |
||
3628 | * @param string $tableName |
||
3629 | * @param string $fName |
||
3630 | * @return bool|ResultWrapper |
||
3631 | * @since 1.18 |
||
3632 | */ |
||
3633 | View Code Duplication | public function dropTable( $tableName, $fName = __METHOD__ ) { |
|
3644 | |||
3645 | /** |
||
3646 | * Get search engine class. All subclasses of this need to implement this |
||
3647 | * if they wish to use searching. |
||
3648 | * |
||
3649 | * @return string |
||
3650 | */ |
||
3651 | public function getSearchEngine() { |
||
3654 | |||
3655 | public function getInfinity() { |
||
3658 | |||
3659 | public function encodeExpiry( $expiry ) { |
||
3664 | |||
3665 | public function decodeExpiry( $expiry, $format = TS_MW ) { |
||
3670 | |||
3671 | public function setBigSelects( $value = true ) { |
||
3674 | |||
3675 | public function isReadOnly() { |
||
3678 | |||
3679 | /** |
||
3680 | * @return string|bool Reason this DB is read-only or false if it is not |
||
3681 | */ |
||
3682 | protected function getReadOnlyReason() { |
||
3687 | |||
3688 | /** |
||
3689 | * @since 1.19 |
||
3690 | * @return string |
||
3691 | */ |
||
3692 | public function __toString() { |
||
3695 | |||
3696 | /** |
||
3697 | * Run a few simple sanity checks |
||
3698 | */ |
||
3699 | public function __destruct() { |
||
3717 | } |
||
3718 | |||
3726 |
This check looks at variables that have been passed in as parameters and are passed out again to other methods.
If the outgoing method call has stricter type requirements than the method itself, an issue is raised.
An additional type check may prevent trouble.