MediaWiki master
DBAccessObjectUtils.php
Go to the documentation of this file.
1<?php
24namespace Wikimedia\Rdbms;
25
26use InvalidArgumentException;
27
39 public static function hasFlags( $bitfield, $flags ) {
40 return ( $bitfield & $flags ) == $flags;
41 }
42
52 public static function getDBOptions( $bitfield ) {
53 wfDeprecated( __METHOD__, '1.43' );
54 if ( self::hasFlags( $bitfield, IDBAccessObject::READ_LATEST_IMMUTABLE ) ) {
55 $index = DB_REPLICA; // override READ_LATEST if set
56 } elseif ( self::hasFlags( $bitfield, IDBAccessObject::READ_LATEST ) ) {
57 $index = DB_PRIMARY;
58 } else {
59 $index = DB_REPLICA;
60 }
61
62 $lockingOptions = [];
63 if ( self::hasFlags( $bitfield, IDBAccessObject::READ_EXCLUSIVE ) ) {
64 $lockingOptions[] = 'FOR UPDATE';
65 } elseif ( self::hasFlags( $bitfield, IDBAccessObject::READ_LOCKING ) ) {
66 $lockingOptions[] = 'LOCK IN SHARE MODE';
67 }
68
69 return [ $index, $lockingOptions ];
70 }
71
81 public static function getDBFromIndex( IConnectionProvider $dbProvider, int $index ): IReadableDatabase {
82 wfDeprecated( __METHOD__, '1.42' );
83 if ( $index === DB_PRIMARY ) {
84 return $dbProvider->getPrimaryDatabase();
85 } elseif ( $index === DB_REPLICA ) {
86 return $dbProvider->getReplicaDatabase();
87 } else {
88 throw new InvalidArgumentException( '$index must be either DB_REPLICA or DB_PRIMARY' );
89 }
90 }
91
98 public static function getDBFromRecency( IConnectionProvider $dbProvider, int $recency ): IReadableDatabase {
99 if ( self::hasFlags( $recency, IDBAccessObject::READ_LATEST ) ) {
100 return $dbProvider->getPrimaryDatabase();
101 }
102 return $dbProvider->getReplicaDatabase();
103 }
104}
105
107class_alias( DBAccessObjectUtils::class, 'DBAccessObjectUtils' );
wfDeprecated( $function, $version=false, $component=false, $callerOffset=2)
Logs a warning that a deprecated feature was used.
if(!defined('MW_SETUP_CALLBACK'))
Definition WebStart.php:81
static getDBFromIndex(IConnectionProvider $dbProvider, int $index)
Takes $index from ::getDBOptions() and return proper Database object.
static getDBOptions( $bitfield)
Get an appropriate DB index and options.
static getDBFromRecency(IConnectionProvider $dbProvider, int $recency)
Provide primary and replica IDatabase connections.
getPrimaryDatabase( $domain=false)
Get connection to the primary database.
getReplicaDatabase( $domain=false, $group=null)
Get connection to a replica database.
Interface for database access objects.
const READ_LATEST
Read from the primary/quorum.
const READ_LOCKING
Read from the primary/quorum and lock out other writers.
const READ_LATEST_IMMUTABLE
Read from a replica DB or without a quorum, using the primary/quorum on miss.
const READ_EXCLUSIVE
Read from the primary/quorum and lock out other writers and locking readers.
A database connection without write operations.
const DB_REPLICA
Definition defines.php:26
const DB_PRIMARY
Definition defines.php:28