Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 30
0.00% covered (danger)
0.00%
0 / 5
CRAP
0.00% covered (danger)
0.00%
0 / 1
LoadBalancerSingle
0.00% covered (danger)
0.00%
0 / 30
0.00% covered (danger)
0.00%
0 / 5
72
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 20
0.00% covered (danger)
0.00%
0 / 1
12
 newFromConnection
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
2
 reallyOpenConnection
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
6
 reuseConnection
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 __destruct
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2/**
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
17 *
18 * @file
19 */
20namespace Wikimedia\Rdbms;
21
22use InvalidArgumentException;
23
24/**
25 * Trivial LoadBalancer that always returns an injected connection handle.
26 *
27 * @ingroup Database
28 */
29class LoadBalancerSingle extends LoadBalancer {
30    /** @var Database */
31    private $conn;
32
33    /**
34     * You probably want to use {@link newFromConnection} instead.
35     *
36     * @param array $params An associative array with one member:
37     *   - connection: An IDatabase connection object
38     */
39    public function __construct( array $params ) {
40        /** @var Database $conn */
41        $conn = $params['connection'] ?? null;
42        if ( !$conn ) {
43            throw new InvalidArgumentException( "Missing 'connection' argument." );
44        }
45
46        $this->conn = $conn;
47
48        parent::__construct( [
49            'servers' => [ [
50                'type' => $conn->getType(),
51                'host' => $conn->getServer(),
52                'dbname' => $conn->getDBname(),
53                'load' => 1,
54            ] ],
55            'trxProfiler' => $params['trxProfiler'] ?? null,
56            'srvCache' => $params['srvCache'] ?? null,
57            'wanCache' => $params['wanCache'] ?? null,
58            'localDomain' => $params['localDomain'] ?? $this->conn->getDomainID(),
59            'readOnlyReason' => $params['readOnlyReason'] ?? false,
60            'clusterName' => $params['clusterName'] ?? null,
61        ] );
62
63        if ( isset( $params['readOnlyReason'] ) ) {
64            $conn->setLBInfo( $conn::LB_READ_ONLY_REASON, $params['readOnlyReason'] );
65        }
66    }
67
68    /**
69     * @param IDatabase $db Live connection handle
70     * @param array $params Parameter map to LoadBalancerSingle::__constructs()
71     * @return LoadBalancerSingle
72     * @since 1.28
73     */
74    public static function newFromConnection( IDatabase $db, array $params = [] ) {
75        return new static( array_merge(
76            [ 'localDomain' => $db->getDomainID() ],
77            $params,
78            [ 'connection' => $db ]
79        ) );
80    }
81
82    protected function reallyOpenConnection( $i, DatabaseDomain $domain, array $lbInfo ) {
83        foreach ( $lbInfo as $k => $v ) {
84            $this->conn->setLBInfo( $k, $v );
85        }
86
87        return $this->conn;
88    }
89
90    public function reuseConnection( IDatabase $conn ) {
91        // do nothing since the connection was injected
92    }
93
94    public function __destruct() {
95        // do nothing since the connection was injected
96    }
97}