Merge "Handle proxy-based TLS when placed in front of Swift"
[lhc/web/wiklou.git] / includes / libs / rdbms / database / DatabasePostgres.php
index 7b8a940..6cf890d 100644 (file)
  * @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
@@ -103,7 +105,10 @@ class DatabasePostgres extends 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
                ];
@@ -163,11 +168,16 @@ class DatabasePostgres extends Database {
                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 ) {
@@ -857,10 +867,10 @@ __INDEXATTR__;
         *
         * @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 ) {
@@ -1303,6 +1313,33 @@ SQL;
                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 ) );
@@ -1354,6 +1391,8 @@ SQL;
         * @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' );