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
24namespace Wikimedia\Rdbms;
25
26use InvalidArgumentException;
27
28/**
29 * Trivial LoadBalancer that always returns an injected connection handle.
30 */
31class LoadBalancerSingle extends LoadBalancer {
32	/** @var IDatabase */
33	private $db;
34
35	/**
36	 * You probably want to use {@link newFromConnection} instead.
37	 *
38	 * @param array $params An associative array with one member:
39	 *   - connection: An IDatabase connection object
40	 */
41	public function __construct( array $params ) {
42		/** @var IDatabase $conn */
43		$conn = $params['connection'] ?? null;
44		if ( !$conn ) {
45			throw new InvalidArgumentException( "Missing 'connection' argument." );
46		}
47
48		$this->db = $conn;
49
50		parent::__construct( [
51			'servers' => [ [
52				'type' => $conn->getType(),
53				'host' => $conn->getServer(),
54				'dbname' => $conn->getDBname(),
55				'load' => 1,
56			] ],
57			'trxProfiler' => $params['trxProfiler'] ?? null,
58			'srvCache' => $params['srvCache'] ?? null,
59			'wanCache' => $params['wanCache'] ?? null,
60			'localDomain' => $params['localDomain'] ?? $this->db->getDomainID(),
61			'readOnlyReason' => $params['readOnlyReason'] ?? false,
62			'clusterName' => $params['clusterName'] ?? null,
63		] );
64
65		if ( isset( $params['readOnlyReason'] ) ) {
66			$conn->setLBInfo( $conn::LB_READ_ONLY_REASON, $params['readOnlyReason'] );
67		}
68	}
69
70	/**
71	 * @param IDatabase $db Live connection handle
72	 * @param array $params Parameter map to LoadBalancerSingle::__constructs()
73	 * @return LoadBalancerSingle
74	 * @since 1.28
75	 */
76	public static function newFromConnection( IDatabase $db, array $params = [] ) {
77		return new static( array_merge(
78			[ 'localDomain' => $db->getDomainID() ],
79			$params,
80			[ 'connection' => $db ]
81		) );
82	}
83
84	protected function reallyOpenConnection( $i, DatabaseDomain $domain, array $lbInfo = [] ) {
85		return $this->db;
86	}
87
88	public function __destruct() {
89		// do nothing since the connection was injected
90	}
91}
92
93/**
94 * @deprecated since 1.29
95 */
96class_alias( LoadBalancerSingle::class, 'LoadBalancerSingle' );
97