Move LikeMatch to Rdbms namespace
[mediawiki.git] / includes / libs / rdbms / lbfactory / LBFactorySingle.php
blobf9926437e60b256dd670982985455c56bcb76139
1 <?php
2 /**
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
20 * @file
21 * @ingroup Database
24 namespace Wikimedia\Rdbms;
26 use LoadBalancerSingle;
27 use IDatabase;
28 use InvalidArgumentException;
29 use BadMethodCallException;
31 /**
32 * An LBFactory class that always returns a single database object.
34 class LBFactorySingle extends LBFactory {
35 /** @var LoadBalancerSingle */
36 private $lb;
38 /**
39 * @param array $conf An associative array with one member:
40 * - connection: The IDatabase connection object
42 public function __construct( array $conf ) {
43 parent::__construct( $conf );
45 if ( !isset( $conf['connection'] ) ) {
46 throw new InvalidArgumentException( "Missing 'connection' argument." );
49 $lb = new LoadBalancerSingle( array_merge( $this->baseLoadBalancerParams(), $conf ) );
50 $this->initLoadBalancer( $lb );
51 $this->lb = $lb;
54 /**
55 * @param IDatabase $db Live connection handle
56 * @param array $params Parameter map to LBFactorySingle::__constructs()
57 * @return LBFactorySingle
58 * @since 1.28
60 public static function newFromConnection( IDatabase $db, array $params = [] ) {
61 return new static( [ 'connection' => $db ] + $params );
64 /**
65 * @param bool|string $domain (unused)
66 * @return LoadBalancerSingle
68 public function newMainLB( $domain = false ) {
69 return $this->lb;
72 /**
73 * @param bool|string $domain (unused)
74 * @return LoadBalancerSingle
76 public function getMainLB( $domain = false ) {
77 return $this->lb;
80 public function newExternalLB( $cluster ) {
81 throw new BadMethodCallException( "Method is not supported." );
84 public function getExternalLB( $cluster ) {
85 throw new BadMethodCallException( "Method is not supported." );
88 /**
89 * @return LoadBalancerSingle[] Map of (cluster name => LoadBalancer)
91 public function getAllMainLBs() {
92 return [ 'DEFAULT' => $this->lb ];
95 /**
96 * @return LoadBalancerSingle[] Map of (cluster name => LoadBalancer)
98 public function getAllExternalLBs() {
99 return [];
103 * @param string|callable $callback
104 * @param array $params
106 public function forEachLB( $callback, array $params = [] ) {
107 call_user_func_array( $callback, array_merge( [ $this->lb ], $params ) );