| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
namespace Wikimedia\Rdbms\Database; |
| 21 |
|
| 22 |
use Wikimedia\Rdbms\DBLanguageError; |
| 23 |
use Wikimedia\Rdbms\Platform\ISQLPlatform; |
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
class DatabaseFlags implements IDatabaseFlags { |
| 31 |
|
| 32 |
protected $flags; |
| 33 |
|
| 34 |
private $priorFlags = []; |
| 35 |
|
| 36 |
|
| 37 |
protected const MUTABLE_FLAGS = [ |
| 38 |
'DBO_DEBUG', |
| 39 |
'DBO_NOBUFFER', |
| 40 |
'DBO_TRX', |
| 41 |
'DBO_DDLMODE', |
| 42 |
]; |
| 43 |
|
| 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 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
public static function contains( int $flags, int $bit ) { |
| 103 |
return ( ( $flags & $bit ) === $bit ); |
| 104 |
} |
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 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 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
public function hasImplicitTrxFlag() { |
| 123 |
return $this->getFlag( self::DBO_TRX ); |
| 124 |
} |
| 125 |
} |