1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
|
<?php
/**
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
* http://www.gnu.org/copyleft/gpl.html
*
* @file
*/
namespace Wikimedia\Rdbms\Database;
use Wikimedia\Rdbms\DBLanguageError;
use Wikimedia\Rdbms\Platform\ISQLPlatform;
/**
* @ingroup Database
* @internal
* @since 1.39
*/
class DatabaseFlags implements IDatabaseFlags {
/** @var int Current bit field of class DBO_* constants */
protected $flags;
/** @var int[] Prior flags member variable values */
private $priorFlags = [];
/** @var string[] List of DBO_* flags that can be changed after connection */
protected const MUTABLE_FLAGS = [
'DBO_DEBUG',
'DBO_NOBUFFER',
'DBO_TRX',
'DBO_DDLMODE',
];
/** @var int Bit field of all DBO_* flags that can be changed after connection */
protected const DBO_MUTABLE = (
self::DBO_DEBUG | self::DBO_NOBUFFER | self::DBO_TRX | self::DBO_DDLMODE
);
public function __construct( $flags ) {
$this->flags = $flags;
}
public function setFlag( $flag, $remember = self::REMEMBER_NOTHING ) {
if ( $flag & ~static::DBO_MUTABLE ) {
throw new DBLanguageError(
"Got $flag (allowed: " . implode( ', ', static::MUTABLE_FLAGS ) . ')'
);
}
if ( $remember === self::REMEMBER_PRIOR ) {
$this->priorFlags[] = $this->flags;
}
$this->flags |= $flag;
}
public function clearFlag( $flag, $remember = self::REMEMBER_NOTHING ) {
if ( $flag & ~static::DBO_MUTABLE ) {
throw new DBLanguageError(
"Got $flag (allowed: " . implode( ', ', static::MUTABLE_FLAGS ) . ')'
);
}
if ( $remember === self::REMEMBER_PRIOR ) {
$this->priorFlags[] = $this->flags;
}
$this->flags &= ~$flag;
}
public function restoreFlags( $state = self::RESTORE_PRIOR ) {
if ( !$this->priorFlags ) {
return;
}
if ( $state === self::RESTORE_INITIAL ) {
$this->flags = reset( $this->priorFlags );
$this->priorFlags = [];
} else {
$this->flags = array_pop( $this->priorFlags );
}
}
public function getFlag( $flag ) {
return ( ( $this->flags & $flag ) === $flag );
}
/**
* @param int $flags A bit field of flags
* @param int $bit Bit flag constant
* @return bool Whether the bit field has the specified bit flag set
*/
public static function contains( int $flags, int $bit ) {
return ( ( $flags & $bit ) === $bit );
}
/**
* @param int $queryFlags A bit field of ISQLPlatform::QUERY_* constants
* @return bool Whether the implicit transaction flag is set and applies to the query flags
*/
public function hasApplicableImplicitTrxFlag( int $queryFlags ) {
return $this->hasImplicitTrxFlag() && !(
self::contains( $queryFlags, ISQLPlatform::QUERY_CHANGE_TRX ) ||
self::contains( $queryFlags, ISQLPlatform::QUERY_CHANGE_SCHEMA ) ||
self::contains( $queryFlags, ISQLPlatform::QUERY_CHANGE_LOCKS ) ||
self::contains( $queryFlags, ISQLPlatform::QUERY_IGNORE_DBO_TRX )
);
}
/**
* @return bool Whether the implicit transaction flag is set
*/
public function hasImplicitTrxFlag() {
return $this->getFlag( self::DBO_TRX );
}
}
|