Merge "SkinTemplate: Move bottomScripts() back sightly"
[lhc/web/wiklou.git] / includes / db / IDatabase.php
1 <?php
2
3 /**
4 * @defgroup Database Database
5 *
6 * This file deals with database interface functions
7 * and query specifics/optimisations.
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 * http://www.gnu.org/copyleft/gpl.html
23 *
24 * @file
25 * @ingroup Database
26 */
27
28 /**
29 * Basic database interface for live and lazy-loaded DB handles
30 *
31 * @todo: loosen up DB classes from MWException
32 * @note: IDatabase and DBConnRef should be updated to reflect any changes
33 * @ingroup Database
34 */
35 interface IDatabase {
36 /** @var int Callback triggered immediately due to no active transaction */
37 const TRIGGER_IDLE = 1;
38 /** @var int Callback triggered by commit */
39 const TRIGGER_COMMIT = 2;
40 /** @var int Callback triggered by rollback */
41 const TRIGGER_ROLLBACK = 3;
42
43 /** @var string Transaction operation comes from service managing all DBs */
44 const FLUSHING_ALL_PEERS = 'flush';
45 /** @var string Transaction operation comes from the database class internally */
46 const FLUSHING_INTERNAL = 'flush';
47
48 /**
49 * A string describing the current software version, and possibly
50 * other details in a user-friendly way. Will be listed on Special:Version, etc.
51 * Use getServerVersion() to get machine-friendly information.
52 *
53 * @return string Version information from the database server
54 */
55 public function getServerInfo();
56
57 /**
58 * Turns buffering of SQL result sets on (true) or off (false). Default is
59 * "on".
60 *
61 * Unbuffered queries are very troublesome in MySQL:
62 *
63 * - If another query is executed while the first query is being read
64 * out, the first query is killed. This means you can't call normal
65 * MediaWiki functions while you are reading an unbuffered query result
66 * from a normal wfGetDB() connection.
67 *
68 * - Unbuffered queries cause the MySQL server to use large amounts of
69 * memory and to hold broad locks which block other queries.
70 *
71 * If you want to limit client-side memory, it's almost always better to
72 * split up queries into batches using a LIMIT clause than to switch off
73 * buffering.
74 *
75 * @param null|bool $buffer
76 * @return null|bool The previous value of the flag
77 */
78 public function bufferResults( $buffer = null );
79
80 /**
81 * Gets the current transaction level.
82 *
83 * Historically, transactions were allowed to be "nested". This is no
84 * longer supported, so this function really only returns a boolean.
85 *
86 * @return int The previous value
87 */
88 public function trxLevel();
89
90 /**
91 * Get the UNIX timestamp of the time that the transaction was established
92 *
93 * This can be used to reason about the staleness of SELECT data
94 * in REPEATABLE-READ transaction isolation level.
95 *
96 * @return float|null Returns null if there is not active transaction
97 * @since 1.25
98 */
99 public function trxTimestamp();
100
101 /**
102 * Get/set the table prefix.
103 * @param string $prefix The table prefix to set, or omitted to leave it unchanged.
104 * @return string The previous table prefix.
105 */
106 public function tablePrefix( $prefix = null );
107
108 /**
109 * Get/set the db schema.
110 * @param string $schema The database schema to set, or omitted to leave it unchanged.
111 * @return string The previous db schema.
112 */
113 public function dbSchema( $schema = null );
114
115 /**
116 * Get properties passed down from the server info array of the load
117 * balancer.
118 *
119 * @param string $name The entry of the info array to get, or null to get the
120 * whole array
121 *
122 * @return array|mixed|null
123 */
124 public function getLBInfo( $name = null );
125
126 /**
127 * Set the LB info array, or a member of it. If called with one parameter,
128 * the LB info array is set to that parameter. If it is called with two
129 * parameters, the member with the given name is set to the given value.
130 *
131 * @param string $name
132 * @param array $value
133 */
134 public function setLBInfo( $name, $value = null );
135
136 /**
137 * Returns true if this database does an implicit sort when doing GROUP BY
138 *
139 * @return bool
140 */
141 public function implicitGroupby();
142
143 /**
144 * Returns true if this database does an implicit order by when the column has an index
145 * For example: SELECT page_title FROM page LIMIT 1
146 *
147 * @return bool
148 */
149 public function implicitOrderby();
150
151 /**
152 * Return the last query that went through IDatabase::query()
153 * @return string
154 */
155 public function lastQuery();
156
157 /**
158 * Returns true if the connection may have been used for write queries.
159 * Should return true if unsure.
160 *
161 * @return bool
162 */
163 public function doneWrites();
164
165 /**
166 * Returns the last time the connection may have been used for write queries.
167 * Should return a timestamp if unsure.
168 *
169 * @return int|float UNIX timestamp or false
170 * @since 1.24
171 */
172 public function lastDoneWrites();
173
174 /**
175 * @return bool Whether there is a transaction open with possible write queries
176 * @since 1.27
177 */
178 public function writesPending();
179
180 /**
181 * Returns true if there is a transaction open with possible write
182 * queries or transaction pre-commit/idle callbacks waiting on it to finish.
183 *
184 * @return bool
185 */
186 public function writesOrCallbacksPending();
187
188 /**
189 * Get the time spend running write queries for this transaction
190 *
191 * High times could be due to scanning, updates, locking, and such
192 *
193 * @return float|bool Returns false if not transaction is active
194 * @since 1.26
195 */
196 public function pendingWriteQueryDuration();
197
198 /**
199 * Get the list of method names that did write queries for this transaction
200 *
201 * @return array
202 * @since 1.27
203 */
204 public function pendingWriteCallers();
205
206 /**
207 * Is a connection to the database open?
208 * @return bool
209 */
210 public function isOpen();
211
212 /**
213 * Set a flag for this connection
214 *
215 * @param int $flag DBO_* constants from Defines.php:
216 * - DBO_DEBUG: output some debug info (same as debug())
217 * - DBO_NOBUFFER: don't buffer results (inverse of bufferResults())
218 * - DBO_TRX: automatically start transactions
219 * - DBO_DEFAULT: automatically sets DBO_TRX if not in command line mode
220 * and removes it in command line mode
221 * - DBO_PERSISTENT: use persistant database connection
222 */
223 public function setFlag( $flag );
224
225 /**
226 * Clear a flag for this connection
227 *
228 * @param int $flag DBO_* constants from Defines.php:
229 * - DBO_DEBUG: output some debug info (same as debug())
230 * - DBO_NOBUFFER: don't buffer results (inverse of bufferResults())
231 * - DBO_TRX: automatically start transactions
232 * - DBO_DEFAULT: automatically sets DBO_TRX if not in command line mode
233 * and removes it in command line mode
234 * - DBO_PERSISTENT: use persistant database connection
235 */
236 public function clearFlag( $flag );
237
238 /**
239 * Returns a boolean whether the flag $flag is set for this connection
240 *
241 * @param int $flag DBO_* constants from Defines.php:
242 * - DBO_DEBUG: output some debug info (same as debug())
243 * - DBO_NOBUFFER: don't buffer results (inverse of bufferResults())
244 * - DBO_TRX: automatically start transactions
245 * - DBO_PERSISTENT: use persistant database connection
246 * @return bool
247 */
248 public function getFlag( $flag );
249
250 /**
251 * General read-only accessor
252 *
253 * @param string $name
254 * @return string
255 */
256 public function getProperty( $name );
257
258 /**
259 * @return string
260 */
261 public function getWikiID();
262
263 /**
264 * Get the type of the DBMS, as it appears in $wgDBtype.
265 *
266 * @return string
267 */
268 public function getType();
269
270 /**
271 * Open a connection to the database. Usually aborts on failure
272 *
273 * @param string $server Database server host
274 * @param string $user Database user name
275 * @param string $password Database user password
276 * @param string $dbName Database name
277 * @return bool
278 * @throws DBConnectionError
279 */
280 public function open( $server, $user, $password, $dbName );
281
282 /**
283 * Fetch the next row from the given result object, in object form.
284 * Fields can be retrieved with $row->fieldname, with fields acting like
285 * member variables.
286 * If no more rows are available, false is returned.
287 *
288 * @param ResultWrapper|stdClass $res Object as returned from IDatabase::query(), etc.
289 * @return stdClass|bool
290 * @throws DBUnexpectedError Thrown if the database returns an error
291 */
292 public function fetchObject( $res );
293
294 /**
295 * Fetch the next row from the given result object, in associative array
296 * form. Fields are retrieved with $row['fieldname'].
297 * If no more rows are available, false is returned.
298 *
299 * @param ResultWrapper $res Result object as returned from IDatabase::query(), etc.
300 * @return array|bool
301 * @throws DBUnexpectedError Thrown if the database returns an error
302 */
303 public function fetchRow( $res );
304
305 /**
306 * Get the number of rows in a result object
307 *
308 * @param mixed $res A SQL result
309 * @return int
310 */
311 public function numRows( $res );
312
313 /**
314 * Get the number of fields in a result object
315 * @see http://www.php.net/mysql_num_fields
316 *
317 * @param mixed $res A SQL result
318 * @return int
319 */
320 public function numFields( $res );
321
322 /**
323 * Get a field name in a result object
324 * @see http://www.php.net/mysql_field_name
325 *
326 * @param mixed $res A SQL result
327 * @param int $n
328 * @return string
329 */
330 public function fieldName( $res, $n );
331
332 /**
333 * Get the inserted value of an auto-increment row
334 *
335 * The value inserted should be fetched from nextSequenceValue()
336 *
337 * Example:
338 * $id = $dbw->nextSequenceValue( 'page_page_id_seq' );
339 * $dbw->insert( 'page', [ 'page_id' => $id ] );
340 * $id = $dbw->insertId();
341 *
342 * @return int
343 */
344 public function insertId();
345
346 /**
347 * Change the position of the cursor in a result object
348 * @see http://www.php.net/mysql_data_seek
349 *
350 * @param mixed $res A SQL result
351 * @param int $row
352 */
353 public function dataSeek( $res, $row );
354
355 /**
356 * Get the last error number
357 * @see http://www.php.net/mysql_errno
358 *
359 * @return int
360 */
361 public function lastErrno();
362
363 /**
364 * Get a description of the last error
365 * @see http://www.php.net/mysql_error
366 *
367 * @return string
368 */
369 public function lastError();
370
371 /**
372 * mysql_fetch_field() wrapper
373 * Returns false if the field doesn't exist
374 *
375 * @param string $table Table name
376 * @param string $field Field name
377 *
378 * @return Field
379 */
380 public function fieldInfo( $table, $field );
381
382 /**
383 * Get the number of rows affected by the last write query
384 * @see http://www.php.net/mysql_affected_rows
385 *
386 * @return int
387 */
388 public function affectedRows();
389
390 /**
391 * Returns a wikitext link to the DB's website, e.g.,
392 * return "[http://www.mysql.com/ MySQL]";
393 * Should at least contain plain text, if for some reason
394 * your database has no website.
395 *
396 * @return string Wikitext of a link to the server software's web site
397 */
398 public function getSoftwareLink();
399
400 /**
401 * A string describing the current software version, like from
402 * mysql_get_server_info().
403 *
404 * @return string Version information from the database server.
405 */
406 public function getServerVersion();
407
408 /**
409 * Closes a database connection.
410 * if it is open : commits any open transactions
411 *
412 * @throws MWException
413 * @return bool Operation success. true if already closed.
414 */
415 public function close();
416
417 /**
418 * @param string $error Fallback error message, used if none is given by DB
419 * @throws DBConnectionError
420 */
421 public function reportConnectionError( $error = 'Unknown error' );
422
423 /**
424 * Run an SQL query and return the result. Normally throws a DBQueryError
425 * on failure. If errors are ignored, returns false instead.
426 *
427 * In new code, the query wrappers select(), insert(), update(), delete(),
428 * etc. should be used where possible, since they give much better DBMS
429 * independence and automatically quote or validate user input in a variety
430 * of contexts. This function is generally only useful for queries which are
431 * explicitly DBMS-dependent and are unsupported by the query wrappers, such
432 * as CREATE TABLE.
433 *
434 * However, the query wrappers themselves should call this function.
435 *
436 * @param string $sql SQL query
437 * @param string $fname Name of the calling function, for profiling/SHOW PROCESSLIST
438 * comment (you can use __METHOD__ or add some extra info)
439 * @param bool $tempIgnore Whether to avoid throwing an exception on errors...
440 * maybe best to catch the exception instead?
441 * @throws MWException
442 * @return bool|ResultWrapper True for a successful write query, ResultWrapper object
443 * for a successful read query, or false on failure if $tempIgnore set
444 */
445 public function query( $sql, $fname = __METHOD__, $tempIgnore = false );
446
447 /**
448 * Report a query error. Log the error, and if neither the object ignore
449 * flag nor the $tempIgnore flag is set, throw a DBQueryError.
450 *
451 * @param string $error
452 * @param int $errno
453 * @param string $sql
454 * @param string $fname
455 * @param bool $tempIgnore
456 * @throws DBQueryError
457 */
458 public function reportQueryError( $error, $errno, $sql, $fname, $tempIgnore = false );
459
460 /**
461 * Free a result object returned by query() or select(). It's usually not
462 * necessary to call this, just use unset() or let the variable holding
463 * the result object go out of scope.
464 *
465 * @param mixed $res A SQL result
466 */
467 public function freeResult( $res );
468
469 /**
470 * A SELECT wrapper which returns a single field from a single result row.
471 *
472 * Usually throws a DBQueryError on failure. If errors are explicitly
473 * ignored, returns false on failure.
474 *
475 * If no result rows are returned from the query, false is returned.
476 *
477 * @param string|array $table Table name. See IDatabase::select() for details.
478 * @param string $var The field name to select. This must be a valid SQL
479 * fragment: do not use unvalidated user input.
480 * @param string|array $cond The condition array. See IDatabase::select() for details.
481 * @param string $fname The function name of the caller.
482 * @param string|array $options The query options. See IDatabase::select() for details.
483 *
484 * @return bool|mixed The value from the field, or false on failure.
485 */
486 public function selectField(
487 $table, $var, $cond = '', $fname = __METHOD__, $options = []
488 );
489
490 /**
491 * A SELECT wrapper which returns a list of single field values from result rows.
492 *
493 * Usually throws a DBQueryError on failure. If errors are explicitly
494 * ignored, returns false on failure.
495 *
496 * If no result rows are returned from the query, false is returned.
497 *
498 * @param string|array $table Table name. See IDatabase::select() for details.
499 * @param string $var The field name to select. This must be a valid SQL
500 * fragment: do not use unvalidated user input.
501 * @param string|array $cond The condition array. See IDatabase::select() for details.
502 * @param string $fname The function name of the caller.
503 * @param string|array $options The query options. See IDatabase::select() for details.
504 *
505 * @return bool|array The values from the field, or false on failure
506 * @since 1.25
507 */
508 public function selectFieldValues(
509 $table, $var, $cond = '', $fname = __METHOD__, $options = []
510 );
511
512 /**
513 * Execute a SELECT query constructed using the various parameters provided.
514 * See below for full details of the parameters.
515 *
516 * @param string|array $table Table name
517 * @param string|array $vars Field names
518 * @param string|array $conds Conditions
519 * @param string $fname Caller function name
520 * @param array $options Query options
521 * @param array $join_conds Join conditions
522 *
523 *
524 * @param string|array $table
525 *
526 * May be either an array of table names, or a single string holding a table
527 * name. If an array is given, table aliases can be specified, for example:
528 *
529 * [ 'a' => 'user' ]
530 *
531 * This includes the user table in the query, with the alias "a" available
532 * for use in field names (e.g. a.user_name).
533 *
534 * All of the table names given here are automatically run through
535 * DatabaseBase::tableName(), which causes the table prefix (if any) to be
536 * added, and various other table name mappings to be performed.
537 *
538 * Do not use untrusted user input as a table name. Alias names should
539 * not have characters outside of the Basic multilingual plane.
540 *
541 * @param string|array $vars
542 *
543 * May be either a field name or an array of field names. The field names
544 * can be complete fragments of SQL, for direct inclusion into the SELECT
545 * query. If an array is given, field aliases can be specified, for example:
546 *
547 * [ 'maxrev' => 'MAX(rev_id)' ]
548 *
549 * This includes an expression with the alias "maxrev" in the query.
550 *
551 * If an expression is given, care must be taken to ensure that it is
552 * DBMS-independent.
553 *
554 * Untrusted user input must not be passed to this parameter.
555 *
556 * @param string|array $conds
557 *
558 * May be either a string containing a single condition, or an array of
559 * conditions. If an array is given, the conditions constructed from each
560 * element are combined with AND.
561 *
562 * Array elements may take one of two forms:
563 *
564 * - Elements with a numeric key are interpreted as raw SQL fragments.
565 * - Elements with a string key are interpreted as equality conditions,
566 * where the key is the field name.
567 * - If the value of such an array element is a scalar (such as a
568 * string), it will be treated as data and thus quoted appropriately.
569 * If it is null, an IS NULL clause will be added.
570 * - If the value is an array, an IN (...) clause will be constructed
571 * from its non-null elements, and an IS NULL clause will be added
572 * if null is present, such that the field may match any of the
573 * elements in the array. The non-null elements will be quoted.
574 *
575 * Note that expressions are often DBMS-dependent in their syntax.
576 * DBMS-independent wrappers are provided for constructing several types of
577 * expression commonly used in condition queries. See:
578 * - IDatabase::buildLike()
579 * - IDatabase::conditional()
580 *
581 * Untrusted user input is safe in the values of string keys, however untrusted
582 * input must not be used in the array key names or in the values of numeric keys.
583 * Escaping of untrusted input used in values of numeric keys should be done via
584 * IDatabase::addQuotes()
585 *
586 * @param string|array $options
587 *
588 * Optional: Array of query options. Boolean options are specified by
589 * including them in the array as a string value with a numeric key, for
590 * example:
591 *
592 * [ 'FOR UPDATE' ]
593 *
594 * The supported options are:
595 *
596 * - OFFSET: Skip this many rows at the start of the result set. OFFSET
597 * with LIMIT can theoretically be used for paging through a result set,
598 * but this is discouraged in MediaWiki for performance reasons.
599 *
600 * - LIMIT: Integer: return at most this many rows. The rows are sorted
601 * and then the first rows are taken until the limit is reached. LIMIT
602 * is applied to a result set after OFFSET.
603 *
604 * - FOR UPDATE: Boolean: lock the returned rows so that they can't be
605 * changed until the next COMMIT.
606 *
607 * - DISTINCT: Boolean: return only unique result rows.
608 *
609 * - GROUP BY: May be either an SQL fragment string naming a field or
610 * expression to group by, or an array of such SQL fragments.
611 *
612 * - HAVING: May be either an string containing a HAVING clause or an array of
613 * conditions building the HAVING clause. If an array is given, the conditions
614 * constructed from each element are combined with AND.
615 *
616 * - ORDER BY: May be either an SQL fragment giving a field name or
617 * expression to order by, or an array of such SQL fragments.
618 *
619 * - USE INDEX: This may be either a string giving the index name to use
620 * for the query, or an array. If it is an associative array, each key
621 * gives the table name (or alias), each value gives the index name to
622 * use for that table. All strings are SQL fragments and so should be
623 * validated by the caller.
624 *
625 * - EXPLAIN: In MySQL, this causes an EXPLAIN SELECT query to be run,
626 * instead of SELECT.
627 *
628 * And also the following boolean MySQL extensions, see the MySQL manual
629 * for documentation:
630 *
631 * - LOCK IN SHARE MODE
632 * - STRAIGHT_JOIN
633 * - HIGH_PRIORITY
634 * - SQL_BIG_RESULT
635 * - SQL_BUFFER_RESULT
636 * - SQL_SMALL_RESULT
637 * - SQL_CALC_FOUND_ROWS
638 * - SQL_CACHE
639 * - SQL_NO_CACHE
640 *
641 *
642 * @param string|array $join_conds
643 *
644 * Optional associative array of table-specific join conditions. In the
645 * most common case, this is unnecessary, since the join condition can be
646 * in $conds. However, it is useful for doing a LEFT JOIN.
647 *
648 * The key of the array contains the table name or alias. The value is an
649 * array with two elements, numbered 0 and 1. The first gives the type of
650 * join, the second is the same as the $conds parameter. Thus it can be
651 * an SQL fragment, or an array where the string keys are equality and the
652 * numeric keys are SQL fragments all AND'd together. For example:
653 *
654 * [ 'page' => [ 'LEFT JOIN', 'page_latest=rev_id' ] ]
655 *
656 * @return ResultWrapper|bool If the query returned no rows, a ResultWrapper
657 * with no rows in it will be returned. If there was a query error, a
658 * DBQueryError exception will be thrown, except if the "ignore errors"
659 * option was set, in which case false will be returned.
660 */
661 public function select(
662 $table, $vars, $conds = '', $fname = __METHOD__,
663 $options = [], $join_conds = []
664 );
665
666 /**
667 * The equivalent of IDatabase::select() except that the constructed SQL
668 * is returned, instead of being immediately executed. This can be useful for
669 * doing UNION queries, where the SQL text of each query is needed. In general,
670 * however, callers outside of Database classes should just use select().
671 *
672 * @param string|array $table Table name
673 * @param string|array $vars Field names
674 * @param string|array $conds Conditions
675 * @param string $fname Caller function name
676 * @param string|array $options Query options
677 * @param string|array $join_conds Join conditions
678 *
679 * @return string SQL query string.
680 * @see IDatabase::select()
681 */
682 public function selectSQLText(
683 $table, $vars, $conds = '', $fname = __METHOD__,
684 $options = [], $join_conds = []
685 );
686
687 /**
688 * Single row SELECT wrapper. Equivalent to IDatabase::select(), except
689 * that a single row object is returned. If the query returns no rows,
690 * false is returned.
691 *
692 * @param string|array $table Table name
693 * @param string|array $vars Field names
694 * @param array $conds Conditions
695 * @param string $fname Caller function name
696 * @param string|array $options Query options
697 * @param array|string $join_conds Join conditions
698 *
699 * @return stdClass|bool
700 */
701 public function selectRow( $table, $vars, $conds, $fname = __METHOD__,
702 $options = [], $join_conds = []
703 );
704
705 /**
706 * Estimate the number of rows in dataset
707 *
708 * MySQL allows you to estimate the number of rows that would be returned
709 * by a SELECT query, using EXPLAIN SELECT. The estimate is provided using
710 * index cardinality statistics, and is notoriously inaccurate, especially
711 * when large numbers of rows have recently been added or deleted.
712 *
713 * For DBMSs that don't support fast result size estimation, this function
714 * will actually perform the SELECT COUNT(*).
715 *
716 * Takes the same arguments as IDatabase::select().
717 *
718 * @param string $table Table name
719 * @param string $vars Unused
720 * @param array|string $conds Filters on the table
721 * @param string $fname Function name for profiling
722 * @param array $options Options for select
723 * @return int Row count
724 */
725 public function estimateRowCount(
726 $table, $vars = '*', $conds = '', $fname = __METHOD__, $options = []
727 );
728
729 /**
730 * Get the number of rows in dataset
731 *
732 * This is useful when trying to do COUNT(*) but with a LIMIT for performance.
733 *
734 * Takes the same arguments as IDatabase::select().
735 *
736 * @since 1.27 Added $join_conds parameter
737 *
738 * @param array|string $tables Table names
739 * @param string $vars Unused
740 * @param array|string $conds Filters on the table
741 * @param string $fname Function name for profiling
742 * @param array $options Options for select
743 * @param array $join_conds Join conditions (since 1.27)
744 * @return int Row count
745 */
746 public function selectRowCount(
747 $tables, $vars = '*', $conds = '', $fname = __METHOD__, $options = [], $join_conds = []
748 );
749
750 /**
751 * Determines whether a field exists in a table
752 *
753 * @param string $table Table name
754 * @param string $field Filed to check on that table
755 * @param string $fname Calling function name (optional)
756 * @return bool Whether $table has filed $field
757 */
758 public function fieldExists( $table, $field, $fname = __METHOD__ );
759
760 /**
761 * Determines whether an index exists
762 * Usually throws a DBQueryError on failure
763 * If errors are explicitly ignored, returns NULL on failure
764 *
765 * @param string $table
766 * @param string $index
767 * @param string $fname
768 * @return bool|null
769 */
770 public function indexExists( $table, $index, $fname = __METHOD__ );
771
772 /**
773 * Query whether a given table exists
774 *
775 * @param string $table
776 * @param string $fname
777 * @return bool
778 */
779 public function tableExists( $table, $fname = __METHOD__ );
780
781 /**
782 * Determines if a given index is unique
783 *
784 * @param string $table
785 * @param string $index
786 *
787 * @return bool
788 */
789 public function indexUnique( $table, $index );
790
791 /**
792 * INSERT wrapper, inserts an array into a table.
793 *
794 * $a may be either:
795 *
796 * - A single associative array. The array keys are the field names, and
797 * the values are the values to insert. The values are treated as data
798 * and will be quoted appropriately. If NULL is inserted, this will be
799 * converted to a database NULL.
800 * - An array with numeric keys, holding a list of associative arrays.
801 * This causes a multi-row INSERT on DBMSs that support it. The keys in
802 * each subarray must be identical to each other, and in the same order.
803 *
804 * Usually throws a DBQueryError on failure. If errors are explicitly ignored,
805 * returns success.
806 *
807 * $options is an array of options, with boolean options encoded as values
808 * with numeric keys, in the same style as $options in
809 * IDatabase::select(). Supported options are:
810 *
811 * - IGNORE: Boolean: if present, duplicate key errors are ignored, and
812 * any rows which cause duplicate key errors are not inserted. It's
813 * possible to determine how many rows were successfully inserted using
814 * IDatabase::affectedRows().
815 *
816 * @param string $table Table name. This will be passed through
817 * DatabaseBase::tableName().
818 * @param array $a Array of rows to insert
819 * @param string $fname Calling function name (use __METHOD__) for logs/profiling
820 * @param array $options Array of options
821 *
822 * @return bool
823 */
824 public function insert( $table, $a, $fname = __METHOD__, $options = [] );
825
826 /**
827 * UPDATE wrapper. Takes a condition array and a SET array.
828 *
829 * @param string $table Name of the table to UPDATE. This will be passed through
830 * DatabaseBase::tableName().
831 * @param array $values An array of values to SET. For each array element,
832 * the key gives the field name, and the value gives the data to set
833 * that field to. The data will be quoted by IDatabase::addQuotes().
834 * @param array $conds An array of conditions (WHERE). See
835 * IDatabase::select() for the details of the format of condition
836 * arrays. Use '*' to update all rows.
837 * @param string $fname The function name of the caller (from __METHOD__),
838 * for logging and profiling.
839 * @param array $options An array of UPDATE options, can be:
840 * - IGNORE: Ignore unique key conflicts
841 * - LOW_PRIORITY: MySQL-specific, see MySQL manual.
842 * @return bool
843 */
844 public function update( $table, $values, $conds, $fname = __METHOD__, $options = [] );
845
846 /**
847 * Makes an encoded list of strings from an array
848 *
849 * @param array $a Containing the data
850 * @param int $mode Constant
851 * - LIST_COMMA: Comma separated, no field names
852 * - LIST_AND: ANDed WHERE clause (without the WHERE). See the
853 * documentation for $conds in IDatabase::select().
854 * - LIST_OR: ORed WHERE clause (without the WHERE)
855 * - LIST_SET: Comma separated with field names, like a SET clause
856 * - LIST_NAMES: Comma separated field names
857 * @throws MWException|DBUnexpectedError
858 * @return string
859 */
860 public function makeList( $a, $mode = LIST_COMMA );
861
862 /**
863 * Build a partial where clause from a 2-d array such as used for LinkBatch.
864 * The keys on each level may be either integers or strings.
865 *
866 * @param array $data Organized as 2-d
867 * [ baseKeyVal => [ subKeyVal => [ignored], ... ], ... ]
868 * @param string $baseKey Field name to match the base-level keys to (eg 'pl_namespace')
869 * @param string $subKey Field name to match the sub-level keys to (eg 'pl_title')
870 * @return string|bool SQL fragment, or false if no items in array
871 */
872 public function makeWhereFrom2d( $data, $baseKey, $subKey );
873
874 /**
875 * @param string $field
876 * @return string
877 */
878 public function bitNot( $field );
879
880 /**
881 * @param string $fieldLeft
882 * @param string $fieldRight
883 * @return string
884 */
885 public function bitAnd( $fieldLeft, $fieldRight );
886
887 /**
888 * @param string $fieldLeft
889 * @param string $fieldRight
890 * @return string
891 */
892 public function bitOr( $fieldLeft, $fieldRight );
893
894 /**
895 * Build a concatenation list to feed into a SQL query
896 * @param array $stringList List of raw SQL expressions; caller is
897 * responsible for any quoting
898 * @return string
899 */
900 public function buildConcat( $stringList );
901
902 /**
903 * Build a GROUP_CONCAT or equivalent statement for a query.
904 *
905 * This is useful for combining a field for several rows into a single string.
906 * NULL values will not appear in the output, duplicated values will appear,
907 * and the resulting delimiter-separated values have no defined sort order.
908 * Code using the results may need to use the PHP unique() or sort() methods.
909 *
910 * @param string $delim Glue to bind the results together
911 * @param string|array $table Table name
912 * @param string $field Field name
913 * @param string|array $conds Conditions
914 * @param string|array $join_conds Join conditions
915 * @return string SQL text
916 * @since 1.23
917 */
918 public function buildGroupConcatField(
919 $delim, $table, $field, $conds = '', $join_conds = []
920 );
921
922 /**
923 * Change the current database
924 *
925 * @param string $db
926 * @return bool Success or failure
927 */
928 public function selectDB( $db );
929
930 /**
931 * Get the current DB name
932 * @return string
933 */
934 public function getDBname();
935
936 /**
937 * Get the server hostname or IP address
938 * @return string
939 */
940 public function getServer();
941
942 /**
943 * Adds quotes and backslashes.
944 *
945 * @param string|Blob $s
946 * @return string
947 */
948 public function addQuotes( $s );
949
950 /**
951 * LIKE statement wrapper, receives a variable-length argument list with
952 * parts of pattern to match containing either string literals that will be
953 * escaped or tokens returned by anyChar() or anyString(). Alternatively,
954 * the function could be provided with an array of aforementioned
955 * parameters.
956 *
957 * Example: $dbr->buildLike( 'My_page_title/', $dbr->anyString() ) returns
958 * a LIKE clause that searches for subpages of 'My page title'.
959 * Alternatively:
960 * $pattern = [ 'My_page_title/', $dbr->anyString() ];
961 * $query .= $dbr->buildLike( $pattern );
962 *
963 * @since 1.16
964 * @return string Fully built LIKE statement
965 */
966 public function buildLike();
967
968 /**
969 * Returns a token for buildLike() that denotes a '_' to be used in a LIKE query
970 *
971 * @return LikeMatch
972 */
973 public function anyChar();
974
975 /**
976 * Returns a token for buildLike() that denotes a '%' to be used in a LIKE query
977 *
978 * @return LikeMatch
979 */
980 public function anyString();
981
982 /**
983 * Returns an appropriately quoted sequence value for inserting a new row.
984 * MySQL has autoincrement fields, so this is just NULL. But the PostgreSQL
985 * subclass will return an integer, and save the value for insertId()
986 *
987 * Any implementation of this function should *not* involve reusing
988 * sequence numbers created for rolled-back transactions.
989 * See http://bugs.mysql.com/bug.php?id=30767 for details.
990 * @param string $seqName
991 * @return null|int
992 */
993 public function nextSequenceValue( $seqName );
994
995 /**
996 * REPLACE query wrapper.
997 *
998 * REPLACE is a very handy MySQL extension, which functions like an INSERT
999 * except that when there is a duplicate key error, the old row is deleted
1000 * and the new row is inserted in its place.
1001 *
1002 * We simulate this with standard SQL with a DELETE followed by INSERT. To
1003 * perform the delete, we need to know what the unique indexes are so that
1004 * we know how to find the conflicting rows.
1005 *
1006 * It may be more efficient to leave off unique indexes which are unlikely
1007 * to collide. However if you do this, you run the risk of encountering
1008 * errors which wouldn't have occurred in MySQL.
1009 *
1010 * @param string $table The table to replace the row(s) in.
1011 * @param array $uniqueIndexes Is an array of indexes. Each element may be either
1012 * a field name or an array of field names
1013 * @param array $rows Can be either a single row to insert, or multiple rows,
1014 * in the same format as for IDatabase::insert()
1015 * @param string $fname Calling function name (use __METHOD__) for logs/profiling
1016 */
1017 public function replace( $table, $uniqueIndexes, $rows, $fname = __METHOD__ );
1018
1019 /**
1020 * INSERT ON DUPLICATE KEY UPDATE wrapper, upserts an array into a table.
1021 *
1022 * This updates any conflicting rows (according to the unique indexes) using
1023 * the provided SET clause and inserts any remaining (non-conflicted) rows.
1024 *
1025 * $rows may be either:
1026 * - A single associative array. The array keys are the field names, and
1027 * the values are the values to insert. The values are treated as data
1028 * and will be quoted appropriately. If NULL is inserted, this will be
1029 * converted to a database NULL.
1030 * - An array with numeric keys, holding a list of associative arrays.
1031 * This causes a multi-row INSERT on DBMSs that support it. The keys in
1032 * each subarray must be identical to each other, and in the same order.
1033 *
1034 * It may be more efficient to leave off unique indexes which are unlikely
1035 * to collide. However if you do this, you run the risk of encountering
1036 * errors which wouldn't have occurred in MySQL.
1037 *
1038 * Usually throws a DBQueryError on failure. If errors are explicitly ignored,
1039 * returns success.
1040 *
1041 * @since 1.22
1042 *
1043 * @param string $table Table name. This will be passed through DatabaseBase::tableName().
1044 * @param array $rows A single row or list of rows to insert
1045 * @param array $uniqueIndexes List of single field names or field name tuples
1046 * @param array $set An array of values to SET. For each array element, the
1047 * key gives the field name, and the value gives the data to set that
1048 * field to. The data will be quoted by IDatabase::addQuotes().
1049 * @param string $fname Calling function name (use __METHOD__) for logs/profiling
1050 * @throws Exception
1051 * @return bool
1052 */
1053 public function upsert(
1054 $table, array $rows, array $uniqueIndexes, array $set, $fname = __METHOD__
1055 );
1056
1057 /**
1058 * DELETE where the condition is a join.
1059 *
1060 * MySQL overrides this to use a multi-table DELETE syntax, in other databases
1061 * we use sub-selects
1062 *
1063 * For safety, an empty $conds will not delete everything. If you want to
1064 * delete all rows where the join condition matches, set $conds='*'.
1065 *
1066 * DO NOT put the join condition in $conds.
1067 *
1068 * @param string $delTable The table to delete from.
1069 * @param string $joinTable The other table.
1070 * @param string $delVar The variable to join on, in the first table.
1071 * @param string $joinVar The variable to join on, in the second table.
1072 * @param array $conds Condition array of field names mapped to variables,
1073 * ANDed together in the WHERE clause
1074 * @param string $fname Calling function name (use __METHOD__) for logs/profiling
1075 * @throws DBUnexpectedError
1076 */
1077 public function deleteJoin( $delTable, $joinTable, $delVar, $joinVar, $conds,
1078 $fname = __METHOD__
1079 );
1080
1081 /**
1082 * DELETE query wrapper.
1083 *
1084 * @param array $table Table name
1085 * @param string|array $conds Array of conditions. See $conds in IDatabase::select()
1086 * for the format. Use $conds == "*" to delete all rows
1087 * @param string $fname Name of the calling function
1088 * @throws DBUnexpectedError
1089 * @return bool|ResultWrapper
1090 */
1091 public function delete( $table, $conds, $fname = __METHOD__ );
1092
1093 /**
1094 * INSERT SELECT wrapper. Takes data from a SELECT query and inserts it
1095 * into another table.
1096 *
1097 * @param string $destTable The table name to insert into
1098 * @param string|array $srcTable May be either a table name, or an array of table names
1099 * to include in a join.
1100 *
1101 * @param array $varMap Must be an associative array of the form
1102 * [ 'dest1' => 'source1', ... ]. Source items may be literals
1103 * rather than field names, but strings should be quoted with
1104 * IDatabase::addQuotes()
1105 *
1106 * @param array $conds Condition array. See $conds in IDatabase::select() for
1107 * the details of the format of condition arrays. May be "*" to copy the
1108 * whole table.
1109 *
1110 * @param string $fname The function name of the caller, from __METHOD__
1111 *
1112 * @param array $insertOptions Options for the INSERT part of the query, see
1113 * IDatabase::insert() for details.
1114 * @param array $selectOptions Options for the SELECT part of the query, see
1115 * IDatabase::select() for details.
1116 *
1117 * @return ResultWrapper
1118 */
1119 public function insertSelect( $destTable, $srcTable, $varMap, $conds,
1120 $fname = __METHOD__,
1121 $insertOptions = [], $selectOptions = []
1122 );
1123
1124 /**
1125 * Returns true if current database backend supports ORDER BY or LIMIT for separate subqueries
1126 * within the UNION construct.
1127 * @return bool
1128 */
1129 public function unionSupportsOrderAndLimit();
1130
1131 /**
1132 * Construct a UNION query
1133 * This is used for providing overload point for other DB abstractions
1134 * not compatible with the MySQL syntax.
1135 * @param array $sqls SQL statements to combine
1136 * @param bool $all Use UNION ALL
1137 * @return string SQL fragment
1138 */
1139 public function unionQueries( $sqls, $all );
1140
1141 /**
1142 * Returns an SQL expression for a simple conditional. This doesn't need
1143 * to be overridden unless CASE isn't supported in your DBMS.
1144 *
1145 * @param string|array $cond SQL expression which will result in a boolean value
1146 * @param string $trueVal SQL expression to return if true
1147 * @param string $falseVal SQL expression to return if false
1148 * @return string SQL fragment
1149 */
1150 public function conditional( $cond, $trueVal, $falseVal );
1151
1152 /**
1153 * Returns a comand for str_replace function in SQL query.
1154 * Uses REPLACE() in MySQL
1155 *
1156 * @param string $orig Column to modify
1157 * @param string $old Column to seek
1158 * @param string $new Column to replace with
1159 *
1160 * @return string
1161 */
1162 public function strreplace( $orig, $old, $new );
1163
1164 /**
1165 * Determines how long the server has been up
1166 *
1167 * @return int
1168 */
1169 public function getServerUptime();
1170
1171 /**
1172 * Determines if the last failure was due to a deadlock
1173 *
1174 * @return bool
1175 */
1176 public function wasDeadlock();
1177
1178 /**
1179 * Determines if the last failure was due to a lock timeout
1180 *
1181 * @return bool
1182 */
1183 public function wasLockTimeout();
1184
1185 /**
1186 * Determines if the last query error was due to a dropped connection and should
1187 * be dealt with by pinging the connection and reissuing the query.
1188 *
1189 * @return bool
1190 */
1191 public function wasErrorReissuable();
1192
1193 /**
1194 * Determines if the last failure was due to the database being read-only.
1195 *
1196 * @return bool
1197 */
1198 public function wasReadOnlyError();
1199
1200 /**
1201 * Wait for the slave to catch up to a given master position
1202 *
1203 * @param DBMasterPos $pos
1204 * @param int $timeout The maximum number of seconds to wait for synchronisation
1205 * @return int|null Zero if the slave was past that position already,
1206 * greater than zero if we waited for some period of time, less than
1207 * zero if it timed out, and null on error
1208 */
1209 public function masterPosWait( DBMasterPos $pos, $timeout );
1210
1211 /**
1212 * Get the replication position of this slave
1213 *
1214 * @return DBMasterPos|bool False if this is not a slave.
1215 */
1216 public function getSlavePos();
1217
1218 /**
1219 * Get the position of this master
1220 *
1221 * @return DBMasterPos|bool False if this is not a master
1222 */
1223 public function getMasterPos();
1224
1225 /**
1226 * @return bool Whether the DB is marked as read-only server-side
1227 * @since 1.28
1228 */
1229 public function serverIsReadOnly();
1230
1231 /**
1232 * Run a callback as soon as the current transaction commits or rolls back.
1233 * An error is thrown if no transaction is pending. Queries in the function will run in
1234 * AUTO-COMMIT mode unless there are begin() calls. Callbacks must commit any transactions
1235 * that they begin.
1236 *
1237 * This is useful for combining cooperative locks and DB transactions.
1238 *
1239 * The callback takes one argument:
1240 * How the transaction ended (IDatabase::TRIGGER_COMMIT or IDatabase::TRIGGER_ROLLBACK)
1241 *
1242 * @param callable $callback
1243 * @return mixed
1244 * @since 1.28
1245 */
1246 public function onTransactionResolution( callable $callback );
1247
1248 /**
1249 * Run a callback as soon as there is no transaction pending.
1250 * If there is a transaction and it is rolled back, then the callback is cancelled.
1251 * Queries in the function will run in AUTO-COMMIT mode unless there are begin() calls.
1252 * Callbacks must commit any transactions that they begin.
1253 *
1254 * This is useful for updates to different systems or when separate transactions are needed.
1255 * For example, one might want to enqueue jobs into a system outside the database, but only
1256 * after the database is updated so that the jobs will see the data when they actually run.
1257 * It can also be used for updates that easily cause deadlocks if locks are held too long.
1258 *
1259 * Updates will execute in the order they were enqueued.
1260 *
1261 * The callback takes one argument:
1262 * How the transaction ended (IDatabase::TRIGGER_COMMIT or IDatabase::TRIGGER_IDLE)
1263 *
1264 * @param callable $callback
1265 * @since 1.20
1266 */
1267 public function onTransactionIdle( callable $callback );
1268
1269 /**
1270 * Run a callback before the current transaction commits or now if there is none.
1271 * If there is a transaction and it is rolled back, then the callback is cancelled.
1272 * Callbacks must not start nor commit any transactions. If no transaction is active,
1273 * then a transaction will wrap the callback.
1274 *
1275 * This is useful for updates that easily cause deadlocks if locks are held too long
1276 * but where atomicity is strongly desired for these updates and some related updates.
1277 *
1278 * Updates will execute in the order they were enqueued.
1279 *
1280 * @param callable $callback
1281 * @since 1.22
1282 */
1283 public function onTransactionPreCommitOrIdle( callable $callback );
1284
1285 /**
1286 * Begin an atomic section of statements
1287 *
1288 * If a transaction has been started already, just keep track of the given
1289 * section name to make sure the transaction is not committed pre-maturely.
1290 * This function can be used in layers (with sub-sections), so use a stack
1291 * to keep track of the different atomic sections. If there is no transaction,
1292 * start one implicitly.
1293 *
1294 * The goal of this function is to create an atomic section of SQL queries
1295 * without having to start a new transaction if it already exists.
1296 *
1297 * Atomic sections are more strict than transactions. With transactions,
1298 * attempting to begin a new transaction when one is already running results
1299 * in MediaWiki issuing a brief warning and doing an implicit commit. All
1300 * atomic levels *must* be explicitly closed using IDatabase::endAtomic(),
1301 * and any database transactions cannot be began or committed until all atomic
1302 * levels are closed. There is no such thing as implicitly opening or closing
1303 * an atomic section.
1304 *
1305 * @since 1.23
1306 * @param string $fname
1307 * @throws DBError
1308 */
1309 public function startAtomic( $fname = __METHOD__ );
1310
1311 /**
1312 * Ends an atomic section of SQL statements
1313 *
1314 * Ends the next section of atomic SQL statements and commits the transaction
1315 * if necessary.
1316 *
1317 * @since 1.23
1318 * @see IDatabase::startAtomic
1319 * @param string $fname
1320 * @throws DBError
1321 */
1322 public function endAtomic( $fname = __METHOD__ );
1323
1324 /**
1325 * Run a callback to do an atomic set of updates for this database
1326 *
1327 * The $callback takes the following arguments:
1328 * - This database object
1329 * - The value of $fname
1330 *
1331 * If any exception occurs in the callback, then rollback() will be called and the error will
1332 * be re-thrown. It may also be that the rollback itself fails with an exception before then.
1333 * In any case, such errors are expected to terminate the request, without any outside caller
1334 * attempting to catch errors and commit anyway. Note that any rollback undoes all prior
1335 * atomic section and uncommitted updates, which trashes the current request, requiring an
1336 * error to be displayed.
1337 *
1338 * This can be an alternative to explicit startAtomic()/endAtomic() calls.
1339 *
1340 * @see DatabaseBase::startAtomic
1341 * @see DatabaseBase::endAtomic
1342 *
1343 * @param string $fname Caller name (usually __METHOD__)
1344 * @param callable $callback Callback that issues DB updates
1345 * @throws DBError
1346 * @throws RuntimeException
1347 * @throws UnexpectedValueException
1348 * @since 1.27
1349 */
1350 public function doAtomicSection( $fname, callable $callback );
1351
1352 /**
1353 * Begin a transaction. If a transaction is already in progress,
1354 * that transaction will be committed before the new transaction is started.
1355 *
1356 * Note that when the DBO_TRX flag is set (which is usually the case for web
1357 * requests, but not for maintenance scripts), any previous database query
1358 * will have started a transaction automatically.
1359 *
1360 * Nesting of transactions is not supported. Attempts to nest transactions
1361 * will cause a warning, unless the current transaction was started
1362 * automatically because of the DBO_TRX flag.
1363 *
1364 * @param string $fname
1365 * @throws DBError
1366 */
1367 public function begin( $fname = __METHOD__ );
1368
1369 /**
1370 * Commits a transaction previously started using begin().
1371 * If no transaction is in progress, a warning is issued.
1372 *
1373 * Nesting of transactions is not supported.
1374 *
1375 * @param string $fname
1376 * @param string $flush Flush flag, set to situationally valid IDatabase::FLUSHING_*
1377 * constant to disable warnings about explicitly committing implicit transactions,
1378 * or calling commit when no transaction is in progress.
1379 *
1380 * This will trigger an exception if there is an ongoing explicit transaction.
1381 *
1382 * Only set the flush flag if you are sure that these warnings are not applicable,
1383 * and no explicit transactions are open.
1384 *
1385 * @throws DBUnexpectedError
1386 */
1387 public function commit( $fname = __METHOD__, $flush = '' );
1388
1389 /**
1390 * Rollback a transaction previously started using begin().
1391 * If no transaction is in progress, a warning is issued.
1392 *
1393 * No-op on non-transactional databases.
1394 *
1395 * @param string $fname
1396 * @param string $flush Flush flag, set to a situationally valid IDatabase::FLUSHING_*
1397 * constant to disable warnings about calling rollback when no transaction is in
1398 * progress. This will silently break any ongoing explicit transaction. Only set the
1399 * flush flag if you are sure that it is safe to ignore these warnings in your context.
1400 * @throws DBUnexpectedError
1401 * @since 1.23 Added $flush parameter
1402 */
1403 public function rollback( $fname = __METHOD__, $flush = '' );
1404
1405 /**
1406 * List all tables on the database
1407 *
1408 * @param string $prefix Only show tables with this prefix, e.g. mw_
1409 * @param string $fname Calling function name
1410 * @throws MWException
1411 * @return array
1412 */
1413 public function listTables( $prefix = null, $fname = __METHOD__ );
1414
1415 /**
1416 * Convert a timestamp in one of the formats accepted by wfTimestamp()
1417 * to the format used for inserting into timestamp fields in this DBMS.
1418 *
1419 * The result is unquoted, and needs to be passed through addQuotes()
1420 * before it can be included in raw SQL.
1421 *
1422 * @param string|int $ts
1423 *
1424 * @return string
1425 */
1426 public function timestamp( $ts = 0 );
1427
1428 /**
1429 * Convert a timestamp in one of the formats accepted by wfTimestamp()
1430 * to the format used for inserting into timestamp fields in this DBMS. If
1431 * NULL is input, it is passed through, allowing NULL values to be inserted
1432 * into timestamp fields.
1433 *
1434 * The result is unquoted, and needs to be passed through addQuotes()
1435 * before it can be included in raw SQL.
1436 *
1437 * @param string|int $ts
1438 *
1439 * @return string
1440 */
1441 public function timestampOrNull( $ts = null );
1442
1443 /**
1444 * Ping the server and try to reconnect if it there is no connection
1445 *
1446 * @return bool Success or failure
1447 */
1448 public function ping();
1449
1450 /**
1451 * Get slave lag. Currently supported only by MySQL.
1452 *
1453 * Note that this function will generate a fatal error on many
1454 * installations. Most callers should use LoadBalancer::safeGetLag()
1455 * instead.
1456 *
1457 * @return int|bool Database replication lag in seconds or false on error
1458 */
1459 public function getLag();
1460
1461 /**
1462 * Get the slave lag when the current transaction started
1463 * or a general lag estimate if not transaction is active
1464 *
1465 * This is useful when transactions might use snapshot isolation
1466 * (e.g. REPEATABLE-READ in innodb), so the "real" lag of that data
1467 * is this lag plus transaction duration. If they don't, it is still
1468 * safe to be pessimistic. In AUTO-COMMIT mode, this still gives an
1469 * indication of the staleness of subsequent reads.
1470 *
1471 * @return array ('lag': seconds or false on error, 'since': UNIX timestamp of BEGIN)
1472 * @since 1.27
1473 */
1474 public function getSessionLagStatus();
1475
1476 /**
1477 * Return the maximum number of items allowed in a list, or 0 for unlimited.
1478 *
1479 * @return int
1480 */
1481 public function maxListLen();
1482
1483 /**
1484 * Some DBMSs have a special format for inserting into blob fields, they
1485 * don't allow simple quoted strings to be inserted. To insert into such
1486 * a field, pass the data through this function before passing it to
1487 * IDatabase::insert().
1488 *
1489 * @param string $b
1490 * @return string
1491 */
1492 public function encodeBlob( $b );
1493
1494 /**
1495 * Some DBMSs return a special placeholder object representing blob fields
1496 * in result objects. Pass the object through this function to return the
1497 * original string.
1498 *
1499 * @param string|Blob $b
1500 * @return string
1501 */
1502 public function decodeBlob( $b );
1503
1504 /**
1505 * Override database's default behavior. $options include:
1506 * 'connTimeout' : Set the connection timeout value in seconds.
1507 * May be useful for very long batch queries such as
1508 * full-wiki dumps, where a single query reads out over
1509 * hours or days.
1510 *
1511 * @param array $options
1512 * @return void
1513 */
1514 public function setSessionOptions( array $options );
1515
1516 /**
1517 * Set variables to be used in sourceFile/sourceStream, in preference to the
1518 * ones in $GLOBALS. If an array is set here, $GLOBALS will not be used at
1519 * all. If it's set to false, $GLOBALS will be used.
1520 *
1521 * @param bool|array $vars Mapping variable name to value.
1522 */
1523 public function setSchemaVars( $vars );
1524
1525 /**
1526 * Check to see if a named lock is available (non-blocking)
1527 *
1528 * @param string $lockName Name of lock to poll
1529 * @param string $method Name of method calling us
1530 * @return bool
1531 * @since 1.20
1532 */
1533 public function lockIsFree( $lockName, $method );
1534
1535 /**
1536 * Acquire a named lock
1537 *
1538 * Named locks are not related to transactions
1539 *
1540 * @param string $lockName Name of lock to aquire
1541 * @param string $method Name of the calling method
1542 * @param int $timeout Acquisition timeout in seconds
1543 * @return bool
1544 */
1545 public function lock( $lockName, $method, $timeout = 5 );
1546
1547 /**
1548 * Release a lock
1549 *
1550 * Named locks are not related to transactions
1551 *
1552 * @param string $lockName Name of lock to release
1553 * @param string $method Name of the calling method
1554 *
1555 * @return int Returns 1 if the lock was released, 0 if the lock was not established
1556 * by this thread (in which case the lock is not released), and NULL if the named
1557 * lock did not exist
1558 */
1559 public function unlock( $lockName, $method );
1560
1561 /**
1562 * Acquire a named lock, flush any transaction, and return an RAII style unlocker object
1563 *
1564 * This is suitiable for transactions that need to be serialized using cooperative locks,
1565 * where each transaction can see each others' changes. Any transaction is flushed to clear
1566 * out stale REPEATABLE-READ snapshot data. Once the returned object falls out of PHP scope,
1567 * any transaction will be committed and the lock will be released.
1568 *
1569 * If the lock acquisition failed, then no transaction flush happens, and null is returned.
1570 *
1571 * @param string $lockKey Name of lock to release
1572 * @param string $fname Name of the calling method
1573 * @param int $timeout Acquisition timeout in seconds
1574 * @return ScopedCallback|null
1575 * @throws DBUnexpectedError
1576 * @since 1.27
1577 */
1578 public function getScopedLockAndFlush( $lockKey, $fname, $timeout );
1579
1580 /**
1581 * Check to see if a named lock used by lock() use blocking queues
1582 *
1583 * @return bool
1584 * @since 1.26
1585 */
1586 public function namedLocksEnqueue();
1587
1588 /**
1589 * Find out when 'infinity' is. Most DBMSes support this. This is a special
1590 * keyword for timestamps in PostgreSQL, and works with CHAR(14) as well
1591 * because "i" sorts after all numbers.
1592 *
1593 * @return string
1594 */
1595 public function getInfinity();
1596
1597 /**
1598 * Encode an expiry time into the DBMS dependent format
1599 *
1600 * @param string $expiry Timestamp for expiry, or the 'infinity' string
1601 * @return string
1602 */
1603 public function encodeExpiry( $expiry );
1604
1605 /**
1606 * Decode an expiry time into a DBMS independent format
1607 *
1608 * @param string $expiry DB timestamp field value for expiry
1609 * @param int $format TS_* constant, defaults to TS_MW
1610 * @return string
1611 */
1612 public function decodeExpiry( $expiry, $format = TS_MW );
1613
1614 /**
1615 * Allow or deny "big selects" for this session only. This is done by setting
1616 * the sql_big_selects session variable.
1617 *
1618 * This is a MySQL-specific feature.
1619 *
1620 * @param bool|string $value True for allow, false for deny, or "default" to
1621 * restore the initial value
1622 */
1623 public function setBigSelects( $value = true );
1624
1625 /**
1626 * @return bool Whether this DB is read-only
1627 * @since 1.27
1628 */
1629 public function isReadOnly();
1630 }