cd998c3e7b0aa3c25ef0869634ca8d24175db1af
3 * Simple generator of database connections that always returns the same object.
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.
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.
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
24 namespace Wikimedia\Rdbms
;
26 use InvalidArgumentException
;
27 use BadMethodCallException
;
30 * An LBFactory class that always returns a single database object.
32 class LBFactorySingle
extends LBFactory
{
33 /** @var LoadBalancerSingle */
37 * @param array $conf An associative array with one member:
38 * - connection: The IDatabase connection object
40 public function __construct( array $conf ) {
41 parent
::__construct( $conf );
43 if ( !isset( $conf['connection'] ) ) {
44 throw new InvalidArgumentException( "Missing 'connection' argument." );
47 $lb = new LoadBalancerSingle( array_merge( $this->baseLoadBalancerParams(), $conf ) );
48 $this->initLoadBalancer( $lb );
53 * @param IDatabase $db Live connection handle
54 * @param array $params Parameter map to LBFactorySingle::__constructs()
55 * @return LBFactorySingle
58 public static function newFromConnection( IDatabase
$db, array $params = [] ) {
59 return new static( [ 'connection' => $db ] +
$params );
63 * @param bool|string $domain (unused)
64 * @return LoadBalancerSingle
66 public function newMainLB( $domain = false ) {
71 * @param bool|string $domain (unused)
72 * @return LoadBalancerSingle
74 public function getMainLB( $domain = false ) {
78 public function newExternalLB( $cluster ) {
79 throw new BadMethodCallException( "Method is not supported." );
82 public function getExternalLB( $cluster ) {
83 throw new BadMethodCallException( "Method is not supported." );
87 * @return LoadBalancerSingle[] Map of (cluster name => LoadBalancer)
89 public function getAllMainLBs() {
90 return [ 'DEFAULT' => $this->lb
];
94 * @return LoadBalancerSingle[] Map of (cluster name => LoadBalancer)
96 public function getAllExternalLBs() {
101 * @param string|callable $callback
102 * @param array $params
104 public function forEachLB( $callback, array $params = [] ) {
105 call_user_func_array( $callback, array_merge( [ $this->lb
], $params ) );