Merge "rdbms: make LoadBalancer::reallyOpenConnection() handle setting DBO_TRX"
[lhc/web/wiklou.git] / includes / libs / rdbms / loadbalancer / LoadBalancerSingle.php
1 <?php
2 /**
3 * Simple generator of database connections that always returns the same object.
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 * Trivial LoadBalancer that always returns an injected connection handle.
30 *
31 * Note that, while this LoadBalancer does not open any connections itself,
32 * it still closes the injected connection at times, including during destruction.
33 * It is therefore unsuitable for use in tests unless you have a Database instance
34 * separate from the main test database (which is expected to stay open).
35 */
36 class LoadBalancerSingle extends LoadBalancer {
37 /** @var IDatabase */
38 private $db;
39
40 /**
41 * @param array $params An associative array with one member:
42 * - connection: An IDatabase connection object
43 */
44 public function __construct( array $params ) {
45 if ( !isset( $params['connection'] ) ) {
46 throw new InvalidArgumentException( "Missing 'connection' argument." );
47 }
48
49 $this->db = $params['connection'];
50
51 parent::__construct( [
52 'servers' => [
53 [
54 'type' => $this->db->getType(),
55 'host' => $this->db->getServer(),
56 'dbname' => $this->db->getDBname(),
57 'load' => 1,
58 ]
59 ],
60 'trxProfiler' => $params['trxProfiler'] ?? null,
61 'srvCache' => $params['srvCache'] ?? null,
62 'wanCache' => $params['wanCache'] ?? null,
63 'localDomain' => $params['localDomain'] ?? $this->db->getDomainID(),
64 'readOnlyReason' => $params['readOnlyReason'] ?? false,
65 ] );
66
67 if ( isset( $params['readOnlyReason'] ) ) {
68 $this->db->setLBInfo( 'readOnlyReason', $params['readOnlyReason'] );
69 }
70 }
71
72 /**
73 * @param IDatabase $db Live connection handle
74 * @param array $params Parameter map to LoadBalancerSingle::__constructs()
75 * @return LoadBalancerSingle
76 * @since 1.28
77 */
78 public static function newFromConnection( IDatabase $db, array $params = [] ) {
79 return new static( array_merge(
80 [ 'localDomain' => $db->getDomainID() ],
81 $params,
82 [ 'connection' => $db ]
83 ) );
84 }
85
86 protected function reallyOpenConnection( $i, DatabaseDomain $domain, array $lbInfo = [] ) {
87 return $this->db;
88 }
89
90 public function __destruct() {
91 // do nothing since the connection was injected
92 }
93 }
94
95 /**
96 * @deprecated since 1.29
97 */
98 class_alias( LoadBalancerSingle::class, 'LoadBalancerSingle' );