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
50 public static function getDBOptions( $bitfield ) {
51 if ( self::hasFlags( $bitfield, IDBAccessObject::READ_LATEST_IMMUTABLE ) ) {
52 $index = DB_REPLICA; // override READ_LATEST if set
53 } elseif ( self::hasFlags( $bitfield, IDBAccessObject::READ_LATEST ) ) {
54 $index = DB_PRIMARY;
55 } else {
56 $index = DB_REPLICA;
57 }
58
59 $lockingOptions = [];
60 if ( self::hasFlags( $bitfield, IDBAccessObject::READ_EXCLUSIVE ) ) {
61 $lockingOptions[] = 'FOR UPDATE';
62 } elseif ( self::hasFlags( $bitfield, IDBAccessObject::READ_LOCKING ) ) {
63 $lockingOptions[] = 'LOCK IN SHARE MODE';
64 }
65
66 return [ $index, $lockingOptions ];
67 }
68
78 public static function getDBFromIndex( IConnectionProvider $dbProvider, int $index ): IReadableDatabase {
79 wfDeprecated( __METHOD__, '1.42' );
80 if ( $index === DB_PRIMARY ) {
81 return $dbProvider->getPrimaryDatabase();
82 } elseif ( $index === DB_REPLICA ) {
83 return $dbProvider->getReplicaDatabase();
84 } else {
85 throw new InvalidArgumentException( '$index must be either DB_REPLICA or DB_PRIMARY' );
86 }
87 }
88
95 public static function getDBFromRecency( IConnectionProvider $dbProvider, int $recency ): IReadableDatabase {
96 if ( self::hasFlags( $recency, IDBAccessObject::READ_LATEST ) ) {
97 return $dbProvider->getPrimaryDatabase();
98 }
99 return $dbProvider->getReplicaDatabase();
100 }
101}
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
Helper class for DAO classes.
static getDBFromRecency(IConnectionProvider $dbProvider, int $recency)
static getDBOptions( $bitfield)
Get an appropriate DB index and options.
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.
getReplicaDatabase( $domain=false, $group=null)
Get connection to a replica database.
A database connection without write operations.
const DB_REPLICA
Definition defines.php:26
const DB_PRIMARY
Definition defines.php:28