Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 21 |
|
0.00% |
0 / 4 |
CRAP | |
0.00% |
0 / 1 |
DBAccessObjectUtils | |
0.00% |
0 / 21 |
|
0.00% |
0 / 4 |
132 | |
0.00% |
0 / 1 |
hasFlags | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
getDBOptions | |
0.00% |
0 / 11 |
|
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 | use Wikimedia\Rdbms\IConnectionProvider; |
25 | use Wikimedia\Rdbms\IReadableDatabase; |
26 | |
27 | /** |
28 | * Helper class for DAO classes |
29 | * |
30 | * @since 1.26 |
31 | */ |
32 | class DBAccessObjectUtils implements IDBAccessObject { |
33 | /** |
34 | * @param int $bitfield |
35 | * @param int $flags IDBAccessObject::READ_* constant |
36 | * @return bool Bitfield has flag $flag set |
37 | */ |
38 | public static function hasFlags( $bitfield, $flags ) { |
39 | return ( $bitfield & $flags ) == $flags; |
40 | } |
41 | |
42 | /** |
43 | * Get an appropriate DB index and options |
44 | * |
45 | * @param int $bitfield Bitfield of IDBAccessObject::READ_* constants |
46 | * @return array List of DB indexes and options in this order: |
47 | * - DB_PRIMARY or DB_REPLICA constant for the initial query |
48 | * - SELECT options array for the initial query |
49 | */ |
50 | public static function getDBOptions( $bitfield ) { |
51 | if ( self::hasFlags( $bitfield, IDBAccessObject::READ_LATEST_IMMUTABLE ) ) { |
52 | $index = DB_REPLICA; // override READ_LATEST if set |
53 | } elseif ( self::hasFlags( $bitfield, IDBAccessObject::READ_LATEST ) ) { |
54 | $index = DB_PRIMARY; |
55 | } else { |
56 | $index = DB_REPLICA; |
57 | } |
58 | |
59 | $lockingOptions = []; |
60 | if ( self::hasFlags( $bitfield, IDBAccessObject::READ_EXCLUSIVE ) ) { |
61 | $lockingOptions[] = 'FOR UPDATE'; |
62 | } elseif ( self::hasFlags( $bitfield, IDBAccessObject::READ_LOCKING ) ) { |
63 | $lockingOptions[] = 'LOCK IN SHARE MODE'; |
64 | } |
65 | |
66 | return [ $index, $lockingOptions ]; |
67 | } |
68 | |
69 | /** |
70 | * Takes $index from ::getDBOptions() and return proper Database object |
71 | * |
72 | * @deprecated since 1.42 |
73 | * |
74 | * @param IConnectionProvider $dbProvider |
75 | * @param int $index either DB_REPLICA or DB_PRIMARY |
76 | * @return IReadableDatabase |
77 | */ |
78 | public static function getDBFromIndex( IConnectionProvider $dbProvider, int $index ): IReadableDatabase { |
79 | wfDeprecated( __METHOD__, '1.42' ); |
80 | if ( $index === DB_PRIMARY ) { |
81 | return $dbProvider->getPrimaryDatabase(); |
82 | } elseif ( $index === DB_REPLICA ) { |
83 | return $dbProvider->getReplicaDatabase(); |
84 | } else { |
85 | throw new InvalidArgumentException( '$index must be either DB_REPLICA or DB_PRIMARY' ); |
86 | } |
87 | } |
88 | |
89 | /** |
90 | * @param IConnectionProvider $dbProvider |
91 | * @param int $recency IDBAccessObject::READ_* constant |
92 | * @return IReadableDatabase |
93 | * @since 1.42 |
94 | */ |
95 | public static function getDBFromRecency( IConnectionProvider $dbProvider, int $recency ): IReadableDatabase { |
96 | if ( self::hasFlags( $recency, IDBAccessObject::READ_LATEST ) ) { |
97 | return $dbProvider->getPrimaryDatabase(); |
98 | } |
99 | return $dbProvider->getReplicaDatabase(); |
100 | } |
101 | } |