MediaWiki master
DatabaseFlags.php
Go to the documentation of this file.
1<?php
21
24
30class DatabaseFlags implements IDatabaseFlags {
32 protected $flags;
34 private $priorFlags = [];
35
37 protected const MUTABLE_FLAGS = [
38 'DBO_DEBUG',
39 'DBO_NOBUFFER',
40 'DBO_TRX',
41 'DBO_DDLMODE',
42 ];
44 protected const DBO_MUTABLE = (
45 self::DBO_DEBUG | self::DBO_NOBUFFER | self::DBO_TRX | self::DBO_DDLMODE
46 );
47
48 public function __construct( $flags ) {
49 $this->flags = $flags;
50 }
51
52 public function setFlag( $flag, $remember = self::REMEMBER_NOTHING ) {
53 if ( $flag & ~static::DBO_MUTABLE ) {
54 throw new DBLanguageError(
55 "Got $flag (allowed: " . implode( ', ', static::MUTABLE_FLAGS ) . ')'
56 );
57 }
58
59 if ( $remember === self::REMEMBER_PRIOR ) {
60 $this->priorFlags[] = $this->flags;
61 }
62
63 $this->flags |= $flag;
64 }
65
66 public function clearFlag( $flag, $remember = self::REMEMBER_NOTHING ) {
67 if ( $flag & ~static::DBO_MUTABLE ) {
68 throw new DBLanguageError(
69 "Got $flag (allowed: " . implode( ', ', static::MUTABLE_FLAGS ) . ')'
70 );
71 }
72
73 if ( $remember === self::REMEMBER_PRIOR ) {
74 $this->priorFlags[] = $this->flags;
75 }
76
77 $this->flags &= ~$flag;
78 }
79
80 public function restoreFlags( $state = self::RESTORE_PRIOR ) {
81 if ( !$this->priorFlags ) {
82 return;
83 }
84
85 if ( $state === self::RESTORE_INITIAL ) {
86 $this->flags = reset( $this->priorFlags );
87 $this->priorFlags = [];
88 } else {
89 $this->flags = array_pop( $this->priorFlags );
90 }
91 }
92
93 public function getFlag( $flag ) {
94 return ( ( $this->flags & $flag ) === $flag );
95 }
96
102 public static function contains( int $flags, int $bit ) {
103 return ( ( $flags & $bit ) === $bit );
104 }
105
110 public function hasApplicableImplicitTrxFlag( int $queryFlags ) {
111 return $this->hasImplicitTrxFlag() && !(
112 self::contains( $queryFlags, ISQLPlatform::QUERY_CHANGE_TRX ) ||
113 self::contains( $queryFlags, ISQLPlatform::QUERY_CHANGE_SCHEMA ) ||
114 self::contains( $queryFlags, ISQLPlatform::QUERY_CHANGE_LOCKS ) ||
115 self::contains( $queryFlags, ISQLPlatform::QUERY_IGNORE_DBO_TRX )
116 );
117 }
118
122 public function hasImplicitTrxFlag() {
123 return $this->getFlag( self::DBO_TRX );
124 }
125}
restoreFlags( $state=self::RESTORE_PRIOR)
Restore the flags to their prior state before the last setFlag/clearFlag call.
int $flags
Current bit field of class DBO_* constants.
getFlag( $flag)
Returns a boolean whether the flag $flag is set for this connection.
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.