Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 23 |
|
0.00% |
0 / 4 |
CRAP | |
0.00% |
0 / 1 |
DBAccessObjectUtils | |
0.00% |
0 / 22 |
|
0.00% |
0 / 4 |
132 | |
0.00% |
0 / 1 |
hasFlags | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
getDBOptions | |
0.00% |
0 / 12 |
|
0.00% |
0 / 1 |
30 | |||
getDBFromIndex | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
12 | |||
getDBFromRecency | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
6 |
1 | <?php |
2 | /** |
3 | * This file contains database access object related constants. |
4 | * |
5 | * This program is free software; you can redistribute it and/or modify |
6 | * it under the terms of the GNU General Public License as published by |
7 | * the Free Software Foundation; either version 2 of the License, or |
8 | * (at your option) any later version. |
9 | * |
10 | * This program is distributed in the hope that it will be useful, |
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
13 | * GNU General Public License for more details. |
14 | * |
15 | * You should have received a copy of the GNU General Public License along |
16 | * with this program; if not, write to the Free Software Foundation, Inc., |
17 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
18 | * http://www.gnu.org/copyleft/gpl.html |
19 | * |
20 | * @file |
21 | * @ingroup Database |
22 | */ |
23 | |
24 | namespace Wikimedia\Rdbms; |
25 | |
26 | use InvalidArgumentException; |
27 | |
28 | /** |
29 | * Helper class for DAO classes |
30 | * |
31 | * @since 1.26 |
32 | */ |
33 | class DBAccessObjectUtils implements IDBAccessObject { |
34 | /** |
35 | * @param int $bitfield |
36 | * @param int $flags IDBAccessObject::READ_* constant |
37 | * @return bool Bitfield has flag $flag set |
38 | */ |
39 | public static function hasFlags( $bitfield, $flags ) { |
40 | return ( $bitfield & $flags ) == $flags; |
41 | } |
42 | |
43 | /** |
44 | * Get an appropriate DB index and options |
45 | * |
46 | * @deprecated since 1.43 |
47 | * @param int $bitfield Bitfield of IDBAccessObject::READ_* constants |
48 | * @return array List of DB indexes and options in this order: |
49 | * - DB_PRIMARY or DB_REPLICA constant for the initial query |
50 | * - SELECT options array for the initial query |
51 | */ |
52 | public static function getDBOptions( $bitfield ) { |
53 | wfDeprecated( __METHOD__, '1.43' ); |
54 | if ( self::hasFlags( $bitfield, IDBAccessObject::READ_LATEST_IMMUTABLE ) ) { |
55 | $index = DB_REPLICA; // override READ_LATEST if set |
56 | } elseif ( self::hasFlags( $bitfield, IDBAccessObject::READ_LATEST ) ) { |
57 | $index = DB_PRIMARY; |
58 | } else { |
59 | $index = DB_REPLICA; |
60 | } |
61 | |
62 | $lockingOptions = []; |
63 | if ( self::hasFlags( $bitfield, IDBAccessObject::READ_EXCLUSIVE ) ) { |
64 | $lockingOptions[] = 'FOR UPDATE'; |
65 | } elseif ( self::hasFlags( $bitfield, IDBAccessObject::READ_LOCKING ) ) { |
66 | $lockingOptions[] = 'LOCK IN SHARE MODE'; |
67 | } |
68 | |
69 | return [ $index, $lockingOptions ]; |
70 | } |
71 | |
72 | /** |
73 | * Takes $index from ::getDBOptions() and return proper Database object |
74 | * |
75 | * @deprecated since 1.42 |
76 | * |
77 | * @param IConnectionProvider $dbProvider |
78 | * @param int $index either DB_REPLICA or DB_PRIMARY |
79 | * @return IReadableDatabase |
80 | */ |
81 | public static function getDBFromIndex( IConnectionProvider $dbProvider, int $index ): IReadableDatabase { |
82 | wfDeprecated( __METHOD__, '1.42' ); |
83 | if ( $index === DB_PRIMARY ) { |
84 | return $dbProvider->getPrimaryDatabase(); |
85 | } elseif ( $index === DB_REPLICA ) { |
86 | return $dbProvider->getReplicaDatabase(); |
87 | } else { |
88 | throw new InvalidArgumentException( '$index must be either DB_REPLICA or DB_PRIMARY' ); |
89 | } |
90 | } |
91 | |
92 | /** |
93 | * @param IConnectionProvider $dbProvider |
94 | * @param int $recency IDBAccessObject::READ_* constant |
95 | * @return IReadableDatabase |
96 | * @since 1.42 |
97 | */ |
98 | public static function getDBFromRecency( IConnectionProvider $dbProvider, int $recency ): IReadableDatabase { |
99 | if ( self::hasFlags( $recency, IDBAccessObject::READ_LATEST ) ) { |
100 | return $dbProvider->getPrimaryDatabase(); |
101 | } |
102 | return $dbProvider->getReplicaDatabase(); |
103 | } |
104 | } |
105 | |
106 | /** @deprecated class alias since 1.43 */ |
107 | class_alias( DBAccessObjectUtils::class, 'DBAccessObjectUtils' ); |