DatabaseMysqlBase visibility cleanups
[mediawiki.git] / includes / dao / DBAccessObjectUtils.php
blobcc63446cb36b063687935f37f7eac51551657029
1 <?php
2 /**
3 * This file contains database access object related constants.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
20 * @file
21 * @ingroup Database
24 /**
25 * Helper class for DAO classes
27 * @since 1.26
29 class DBAccessObjectUtils implements IDBAccessObject {
30 /**
31 * @param integer $bitfield
32 * @param integer $flags IDBAccessObject::READ_* constant
33 * @return bool Bitfield has flag $flag set
35 public static function hasFlags( $bitfield, $flags ) {
36 return ( $bitfield & $flags ) == $flags;
39 /**
40 * Get an appropriate DB index, options, and fallback DB index for a query
42 * The fallback DB index and options are to be used if the entity is not found
43 * with the initial DB index, typically querying the master DB to avoid lag
45 * @param integer $bitfield Bitfield of IDBAccessObject::READ_* constants
46 * @return array List of DB indexes and options in this order:
47 * - DB_MASTER or DB_REPLICA constant for the initial query
48 * - SELECT options array for the initial query
49 * - DB_MASTER constant for the fallback query; null if no fallback should happen
50 * - SELECT options array for the fallback query; empty if no fallback should happen
52 public static function getDBOptions( $bitfield ) {
53 if ( self::hasFlags( $bitfield, self::READ_LATEST_IMMUTABLE ) ) {
54 $index = DB_REPLICA; // override READ_LATEST if set
55 $fallbackIndex = DB_MASTER;
56 } elseif ( self::hasFlags( $bitfield, self::READ_LATEST ) ) {
57 $index = DB_MASTER;
58 $fallbackIndex = null;
59 } else {
60 $index = DB_REPLICA;
61 $fallbackIndex = null;
64 $lockingOptions = [];
65 if ( self::hasFlags( $bitfield, self::READ_EXCLUSIVE ) ) {
66 $lockingOptions[] = 'FOR UPDATE';
67 } elseif ( self::hasFlags( $bitfield, self::READ_LOCKING ) ) {
68 $lockingOptions[] = 'LOCK IN SHARE MODE';
71 if ( $fallbackIndex !== null ) {
72 $options = []; // locks on DB_REPLICA make no sense
73 $fallbackOptions = $lockingOptions;
74 } else {
75 $options = $lockingOptions;
76 $fallbackOptions = []; // no fallback
79 return [ $index, $options, $fallbackIndex, $fallbackOptions ];