MediaWiki master
DatabaseFlags.php
Go to the documentation of this file.
1<?php
7
10
16class DatabaseFlags implements IDatabaseFlags {
18 protected $flags;
20 private $priorFlags = [];
21
23 protected const MUTABLE_FLAGS = [
24 'DBO_DEBUG',
25 'DBO_NOBUFFER',
26 'DBO_TRX',
27 'DBO_DDLMODE',
28 ];
30 protected const DBO_MUTABLE = (
31 self::DBO_DEBUG | self::DBO_NOBUFFER | self::DBO_TRX | self::DBO_DDLMODE
32 );
33
34 public function __construct( int $flags ) {
35 $this->flags = $flags;
36 }
37
39 public function setFlag( $flag, $remember = self::REMEMBER_NOTHING ) {
40 if ( $flag & ~static::DBO_MUTABLE ) {
41 throw new DBLanguageError(
42 "Got $flag (allowed: " . implode( ', ', static::MUTABLE_FLAGS ) . ')'
43 );
44 }
45
46 if ( $remember === self::REMEMBER_PRIOR ) {
47 $this->priorFlags[] = $this->flags;
48 }
49
50 $this->flags |= $flag;
51 }
52
54 public function clearFlag( $flag, $remember = self::REMEMBER_NOTHING ) {
55 if ( $flag & ~static::DBO_MUTABLE ) {
56 throw new DBLanguageError(
57 "Got $flag (allowed: " . implode( ', ', static::MUTABLE_FLAGS ) . ')'
58 );
59 }
60
61 if ( $remember === self::REMEMBER_PRIOR ) {
62 $this->priorFlags[] = $this->flags;
63 }
64
65 $this->flags &= ~$flag;
66 }
67
69 public function restoreFlags( $state = self::RESTORE_PRIOR ) {
70 if ( !$this->priorFlags ) {
71 return;
72 }
73
74 if ( $state === self::RESTORE_INITIAL ) {
75 $this->flags = reset( $this->priorFlags );
76 $this->priorFlags = [];
77 } else {
78 $this->flags = array_pop( $this->priorFlags );
79 }
80 }
81
83 public function getFlag( $flag ) {
84 return ( ( $this->flags & $flag ) === $flag );
85 }
86
92 public static function contains( int $flags, int $bit ) {
93 return ( ( $flags & $bit ) === $bit );
94 }
95
100 public function hasApplicableImplicitTrxFlag( int $queryFlags ) {
101 return $this->hasImplicitTrxFlag() && !(
102 self::contains( $queryFlags, ISQLPlatform::QUERY_CHANGE_TRX ) ||
103 self::contains( $queryFlags, ISQLPlatform::QUERY_CHANGE_SCHEMA ) ||
104 self::contains( $queryFlags, ISQLPlatform::QUERY_CHANGE_LOCKS ) ||
105 self::contains( $queryFlags, ISQLPlatform::QUERY_IGNORE_DBO_TRX )
106 );
107 }
108
112 public function hasImplicitTrxFlag() {
113 return $this->getFlag( self::DBO_TRX );
114 }
115}
restoreFlags( $state=self::RESTORE_PRIOR)
Restore the flags to their prior state before the last setFlag/clearFlag call.1.28
int $flags
Current bit field of class DBO_* constants.
getFlag( $flag)
Returns a boolean whether the flag $flag is set for this connection.bool
static contains(int $flags, int $bit)
clearFlag( $flag, $remember=self::REMEMBER_NOTHING)
Clear a flag for this connection.
setFlag( $flag, $remember=self::REMEMBER_NOTHING)
Set a flag for this connection.
const DBO_DDLMODE
Schema file mode; was used by Oracle.
Interface for query language.