MediaWiki  master
DBAccessObjectUtils.php
Go to the documentation of this file.
1 <?php
26 
38  public static function hasFlags( $bitfield, $flags ) {
39  return ( $bitfield & $flags ) == $flags;
40  }
41 
55  public static function getDBOptions( $bitfield ) {
56  if ( self::hasFlags( $bitfield, self::READ_LATEST_IMMUTABLE ) ) {
57  $index = DB_REPLICA; // override READ_LATEST if set
58  $fallbackIndex = DB_PRIMARY;
59  } elseif ( self::hasFlags( $bitfield, self::READ_LATEST ) ) {
60  $index = DB_PRIMARY;
61  $fallbackIndex = null;
62  } else {
63  $index = DB_REPLICA;
64  $fallbackIndex = null;
65  }
66 
67  $lockingOptions = [];
68  if ( self::hasFlags( $bitfield, self::READ_EXCLUSIVE ) ) {
69  $lockingOptions[] = 'FOR UPDATE';
70  } elseif ( self::hasFlags( $bitfield, self::READ_LOCKING ) ) {
71  $lockingOptions[] = 'LOCK IN SHARE MODE';
72  }
73 
74  if ( $fallbackIndex !== null ) {
75  $options = []; // locks on DB_REPLICA make no sense
76  $fallbackOptions = $lockingOptions;
77  } else {
78  $options = $lockingOptions;
79  $fallbackOptions = []; // no fallback
80  }
81 
82  return [ $index, $options, $fallbackIndex, $fallbackOptions ];
83  }
84 
94  public static function getDBFromIndex( IConnectionProvider $dbProvider, int $index ): IReadableDatabase {
95  if ( $index === DB_PRIMARY ) {
96  return $dbProvider->getPrimaryDatabase();
97  } elseif ( $index === DB_REPLICA ) {
98  return $dbProvider->getReplicaDatabase();
99  } else {
100  throw new InvalidArgumentException( '$index must be either DB_REPLICA or DB_PRIMARY' );
101  }
102  }
103 }
if(!defined('MW_SETUP_CALLBACK'))
Definition: WebStart.php:88
Helper class for DAO classes.
static getDBOptions( $bitfield)
Get an appropriate DB index, options, and fallback DB index for a query.
static getDBFromIndex(IConnectionProvider $dbProvider, int $index)
Takes $index from ::getDBOptions() and return proper Database object.
static hasFlags( $bitfield, $flags)
Interface for database access objects.
Provide primary and replica IDatabase connections.
getPrimaryDatabase( $domain=false)
Get connection to the primary database.
A database connection without write operations.
const DB_REPLICA
Definition: defines.php:26
const DB_PRIMARY
Definition: defines.php:28