Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
93.33% |
28 / 30 |
|
75.00% |
6 / 8 |
CRAP | |
0.00% |
0 / 1 |
| DatabaseFlags | |
93.33% |
28 / 30 |
|
75.00% |
6 / 8 |
18.10 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| setFlag | |
100.00% |
7 / 7 |
|
100.00% |
1 / 1 |
3 | |||
| clearFlag | |
85.71% |
6 / 7 |
|
0.00% |
0 / 1 |
3.03 | |||
| restoreFlags | |
83.33% |
5 / 6 |
|
0.00% |
0 / 1 |
3.04 | |||
| getFlag | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| contains | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| hasApplicableImplicitTrxFlag | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
5 | |||
| hasImplicitTrxFlag | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| 1 | <?php |
| 2 | /** |
| 3 | * @license GPL-2.0-or-later |
| 4 | * @file |
| 5 | */ |
| 6 | namespace Wikimedia\Rdbms\Database; |
| 7 | |
| 8 | use Wikimedia\Rdbms\DBLanguageError; |
| 9 | use Wikimedia\Rdbms\Platform\ISQLPlatform; |
| 10 | |
| 11 | /** |
| 12 | * @ingroup Database |
| 13 | * @internal |
| 14 | * @since 1.39 |
| 15 | */ |
| 16 | class DatabaseFlags implements IDatabaseFlags { |
| 17 | /** @var int Current bit field of class DBO_* constants */ |
| 18 | protected $flags; |
| 19 | /** @var int[] Prior flags member variable values */ |
| 20 | private $priorFlags = []; |
| 21 | |
| 22 | /** @var string[] List of DBO_* flags that can be changed after connection */ |
| 23 | protected const MUTABLE_FLAGS = [ |
| 24 | 'DBO_DEBUG', |
| 25 | 'DBO_NOBUFFER', |
| 26 | 'DBO_TRX', |
| 27 | 'DBO_DDLMODE', |
| 28 | ]; |
| 29 | /** @var int Bit field of all DBO_* flags that can be changed after connection */ |
| 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 | |
| 38 | /** @inheritDoc */ |
| 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 | |
| 53 | /** @inheritDoc */ |
| 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 | |
| 68 | /** @inheritDoc */ |
| 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 | |
| 82 | /** @inheritDoc */ |
| 83 | public function getFlag( $flag ) { |
| 84 | return ( ( $this->flags & $flag ) === $flag ); |
| 85 | } |
| 86 | |
| 87 | /** |
| 88 | * @param int $flags A bit field of flags |
| 89 | * @param int $bit Bit flag constant |
| 90 | * @return bool Whether the bit field has the specified bit flag set |
| 91 | */ |
| 92 | public static function contains( int $flags, int $bit ) { |
| 93 | return ( ( $flags & $bit ) === $bit ); |
| 94 | } |
| 95 | |
| 96 | /** |
| 97 | * @param int $queryFlags A bit field of ISQLPlatform::QUERY_* constants |
| 98 | * @return bool Whether the implicit transaction flag is set and applies to the query flags |
| 99 | */ |
| 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 | |
| 109 | /** |
| 110 | * @return bool Whether the implicit transaction flag is set |
| 111 | */ |
| 112 | public function hasImplicitTrxFlag() { |
| 113 | return $this->getFlag( self::DBO_TRX ); |
| 114 | } |
| 115 | } |