return $this->__call( __FUNCTION__, func_get_args() );
}
- public function setLBInfo( $name, $value = null ) {
+ public function setLBInfo( $nameOrArray, $value = null ) {
// Disallow things that might confuse the LoadBalancer tracking
throw new DBUnexpectedError( $this, "Changing LB info is disallowed to enable reuse." );
}
return null;
}
- public function setLBInfo( $name, $value = null ) {
- if ( is_null( $value ) ) {
- $this->lbInfo = $name;
+ public function setLBInfo( $nameOrArray, $value = null ) {
+ if ( is_array( $nameOrArray ) ) {
+ $this->lbInfo = $nameOrArray;
+ } elseif ( is_string( $nameOrArray ) ) {
+ if ( $value !== null ) {
+ $this->lbInfo[$nameOrArray] = $value;
+ } else {
+ unset( $this->lbInfo[$nameOrArray] );
+ }
} else {
- $this->lbInfo[$name] = $value;
+ throw new InvalidArgumentException( "Got non-string key" );
}
}
public function getLBInfo( $name = null );
/**
- * Set the LB info array, or a member of it. If called with one parameter,
- * the LB info array is set to that parameter. If it is called with two
- * parameters, the member with the given name is set to the given value.
+ * Set the entire array or a particular key of the managing load balancer info array
*
- * @param array|string $name
- * @param array|null $value
+ * @param array|string $nameOrArray The new array or the name of a key to set
+ * @param array|null $value If $nameOrArray is a string, the new key value (null to unset)
*/
- public function setLBInfo( $name, $value = null );
+ public function setLBInfo( $nameOrArray, $value = null );
/**
* Set a lazy-connecting DB handle to the master DB (for replication status purposes)
/**
* Change the current database
*
- * This should not be called outside LoadBalancer for connections managed by a LoadBalancer
+ * This should only be called by a load balancer or if the handle is not attached to one
*
* @param string $db
* @return bool True unless an exception was thrown
/**
* Set the current domain (database, schema, and table prefix)
*
- * This will throw an error for some database types if the database unspecified
+ * This will throw an error for some database types if the database is unspecified
*
- * This should not be called outside LoadBalancer for connections managed by a LoadBalancer
+ * This should only be called by a load balancer or if the handle is not attached to one
*
* @param string|DatabaseDomain $domain
* @since 1.32
}
if ( $conn->getFlag( $conn::DBO_TRX ) ) {
- $conn->setLBInfo( 'trxRoundId', false );
+ $conn->setLBInfo( 'trxRoundId', null ); // remove the round ID
}
if ( $conn->getFlag( $conn::DBO_DEFAULT ) ) {
$this->assertSame( $oldDomain, $this->db->getDomainId() );
}
+ /**
+ * @covers Wikimedia\Rdbms\Database::getLBInfo
+ * @covers Wikimedia\Rdbms\Database::setLBInfo
+ */
+ public function testGetSetLBInfo() {
+ $db = $this->getMockDB();
+
+ $this->assertEquals( [], $db->getLBInfo() );
+ $this->assertNull( $db->getLBInfo( 'pringles' ) );
+
+ $db->setLBInfo( 'soda', 'water' );
+ $this->assertEquals( [ 'soda' => 'water' ], $db->getLBInfo() );
+ $this->assertNull( $db->getLBInfo( 'pringles' ) );
+ $this->assertEquals( 'water', $db->getLBInfo( 'soda' ) );
+
+ $db->setLBInfo( 'basketball', 'Lebron' );
+ $this->assertEquals( [ 'soda' => 'water', 'basketball' => 'Lebron' ], $db->getLBInfo() );
+ $this->assertEquals( 'water', $db->getLBInfo( 'soda' ) );
+ $this->assertEquals( 'Lebron', $db->getLBInfo( 'basketball' ) );
+
+ $db->setLBInfo( 'soda', null );
+ $this->assertEquals( [ 'basketball' => 'Lebron' ], $db->getLBInfo() );
+
+ $db->setLBInfo( [ 'King' => 'James' ] );
+ $this->assertNull( $db->getLBInfo( 'basketball' ) );
+ $this->assertEquals( [ 'King' => 'James' ], $db->getLBInfo() );
+ }
}