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