* @file
* @ingroup Database
*/
+namespace Wikimedia\Rdbms;
+
+use Wikimedia\Timestamp\ConvertibleTimestamp;
use Wikimedia\WaitConditionLoop;
-use Wikimedia\Rdbms\Blob;
-use Wikimedia\Rdbms\PostgresBlob;
-use Wikimedia\Rdbms\PostgresField;
+use MediaWiki;
+use Exception;
/**
* @ingroup Database
$this->mDBname = $dbName;
$connectVars = [
- 'dbname' => $dbName,
+ // pg_connect() user $user as the default database. Since a database is *required*,
+ // at least pick a "don't care" database that is more likely to exist. This case
+ // arrises when LoadBalancer::getConnection( $i, [], '' ) is used.
+ 'dbname' => strlen( $dbName ) ? $dbName : 'postgres',
'user' => $user,
'password' => $password
];
return $this->mConn;
}
+ public function databasesAreIndependent() {
+ return true;
+ }
+
/**
* Postgres doesn't support selectDB in the same way MySQL does. So if the
* DB name doesn't match the open connection, open a new one
* @param string $db
* @return bool
+ * @throws DBUnexpectedError
*/
public function selectDB( $db ) {
if ( $this->mDBname !== $db ) {
*
* @since 1.19
* @param string $text Postgreql array returned in a text form like {a,b}
- * @param string $output
+ * @param string[] $output
* @param int|bool $limit
* @param int $offset
- * @return string
+ * @return string[]
*/
private function pg_array_parse( $text, &$output, $limit = false, $offset = 1 ) {
if ( false === $limit ) {
return parent::streamStatementEnd( $sql, $newLine );
}
+ public function doLockTables( array $read, array $write, $method ) {
+ $tablesWrite = [];
+ foreach ( $write as $table ) {
+ $tablesWrite[] = $this->tableName( $table );
+ }
+ $tablesRead = [];
+ foreach ( $read as $table ) {
+ $tablesRead[] = $this->tableName( $table );
+ }
+
+ // Acquire locks for the duration of the current transaction...
+ if ( $tablesWrite ) {
+ $this->query(
+ 'LOCK TABLE ONLY ' . implode( ',', $tablesWrite ) . ' IN EXCLUSIVE MODE',
+ $method
+ );
+ }
+ if ( $tablesRead ) {
+ $this->query(
+ 'LOCK TABLE ONLY ' . implode( ',', $tablesRead ) . ' IN SHARE MODE',
+ $method
+ );
+ }
+
+ return true;
+ }
+
public function lockIsFree( $lockName, $method ) {
// http://www.postgresql.org/docs/8.2/static/functions-admin.html#FUNCTIONS-ADVISORY-LOCKS
$key = $this->addQuotes( $this->bigintFromLockName( $lockName ) );
* @return string Integer
*/
private function bigintFromLockName( $lockName ) {
- return Wikimedia\base_convert( substr( sha1( $lockName ), 0, 15 ), 16, 10 );
+ return \Wikimedia\base_convert( substr( sha1( $lockName ), 0, 15 ), 16, 10 );
}
}
+
+class_alias( DatabasePostgres::class, 'DatabasePostgres' );