Merge "Changed FOR UPDATE handling in Postgresql"
[lhc/web/wiklou.git] / includes / db / DatabasePostgres.php
index cfa2074..cb671d8 100644 (file)
@@ -26,9 +26,9 @@ class PostgresField implements Field {
                $has_default, $default;
 
        /**
-        * @param $db DatabaseBase
-        * @param  $table
-        * @param  $field
+        * @param DatabaseBase $db
+        * @param string $table
+        * @param string $field
         * @return null|PostgresField
         */
        static function fromText( $db, $table, $field ) {
@@ -135,8 +135,7 @@ SQL;
  * @ingroup Database
  */
 class PostgresTransactionState {
-
-       static $WATCHED = array(
+       private static $WATCHED = array(
                array(
                        "desc" => "%s: Connection state changed from %s -> %s\n",
                        "states" => array(
@@ -156,6 +155,12 @@ class PostgresTransactionState {
                )
        );
 
+       /** @var array */
+       private $mNewState;
+
+       /** @var array */
+       private $mCurrentState;
+
        public function __construct( $conn ) {
                $this->mConn = $conn;
                $this->update();
@@ -211,13 +216,15 @@ class PostgresTransactionState {
  * @since 1.19
  */
 class SavepointPostgres {
-       /**
-        * Establish a savepoint within a transaction
-        */
+       /** @var DatabaseBase Establish a savepoint within a transaction */
        protected $dbw;
        protected $id;
        protected $didbegin;
 
+       /**
+        * @param DatabaseBase $dbw
+        * @param $id
+        */
        public function __construct( $dbw, $id ) {
                $this->dbw = $dbw;
                $this->id = $id;
@@ -284,10 +291,26 @@ class SavepointPostgres {
  * @ingroup Database
  */
 class DatabasePostgres extends DatabaseBase {
-       var $mInsertId = null;
-       var $mLastResult = null;
-       var $numeric_version = null;
-       var $mAffectedRows = null;
+       /** @var resource */
+       protected $mLastResult = null;
+
+       /** @var int The number of rows affected as an integer */
+       protected $mAffectedRows = null;
+
+       /** @var int */
+       private $mInsertId = null;
+
+       /** @var float|string */
+       private $numericVersion = null;
+
+       /** @var string Connect string to open a PostgreSQL connection */
+       private $connectString;
+
+       /** @var PostgresTransactionState */
+       private $mTransactionState;
+
+       /** @var string */
+       private $mCoreSchema;
 
        function getType() {
                return 'postgres';
@@ -341,7 +364,7 @@ class DatabasePostgres extends DatabaseBase {
         * @param string $user
         * @param string $password
         * @param string $dbName
-        * @throws DBConnectionError
+        * @throws DBConnectionError|Exception
         * @return DatabaseBase|null
         */
        function open( $server, $user, $password, $dbName ) {
@@ -358,7 +381,7 @@ class DatabasePostgres extends DatabaseBase {
                global $wgDBport;
 
                if ( !strlen( $user ) ) { # e.g. the class is being loaded
-                       return;
+                       return null;
                }
 
                $this->mServer = $server;
@@ -429,7 +452,8 @@ class DatabasePostgres extends DatabaseBase {
        /**
         * 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
-        * @return
+        * @param string $db
+        * @return bool
         */
        function selectDB( $db ) {
                if ( $this->mDBname !== $db ) {
@@ -515,6 +539,10 @@ class DatabasePostgres extends DatabaseBase {
                return $this->query( $sql, $fname, true );
        }
 
+       /**
+        * @param stdClass|ResultWrapper $res
+        * @throws DBUnexpectedError
+        */
        function freeResult( $res ) {
                if ( $res instanceof ResultWrapper ) {
                        $res = $res->result;
@@ -527,6 +555,11 @@ class DatabasePostgres extends DatabaseBase {
                }
        }
 
+       /**
+        * @param ResultWrapper|stdClass $res
+        * @return stdClass
+        * @throws DBUnexpectedError
+        */
        function fetchObject( $res ) {
                if ( $res instanceof ResultWrapper ) {
                        $res = $res->result;
@@ -602,12 +635,17 @@ class DatabasePostgres extends DatabaseBase {
         * Return the result of the last call to nextSequenceValue();
         * This must be called after nextSequenceValue().
         *
-        * @return integer|null
+        * @return int|null
         */
        function insertId() {
                return $this->mInsertId;
        }
 
+       /**
+        * @param mixed $res
+        * @param int $row
+        * @return bool
+        */
        function dataSeek( $res, $row ) {
                if ( $res instanceof ResultWrapper ) {
                        $res = $res->result;
@@ -654,6 +692,12 @@ class DatabasePostgres extends DatabaseBase {
         * This is not necessarily an accurate estimate, so use sparingly
         * Returns -1 if count cannot be found
         * Takes same arguments as Database::select()
+        *
+        * @param string $table
+        * @param string $vars
+        * @param string $conds
+        * @param string $fname
+        * @param array $options
         * @return int
         */
        function estimateRowCount( $table, $vars = '*', $conds = '',
@@ -676,6 +720,10 @@ class DatabasePostgres extends DatabaseBase {
        /**
         * Returns information about an index
         * If errors are explicitly ignored, returns NULL on failure
+        *
+        * @param string $table
+        * @param string $index
+        * @param string $fname
         * @return bool|null
         */
        function indexInfo( $table, $index, $fname = __METHOD__ ) {
@@ -697,7 +745,9 @@ class DatabasePostgres extends DatabaseBase {
         * Returns is of attributes used in index
         *
         * @since 1.19
-        * @return Array
+        * @param string $index
+        * @param bool|string $schema
+        * @return array
         */
        function indexAttributes( $index, $schema = false ) {
                if ( $schema === false ) {
@@ -765,11 +815,31 @@ __INDEXATTR__;
                if ( !$res ) {
                        return null;
                }
-               foreach ( $res as $row ) {
-                       return true;
+
+               return $res->numRows() > 0;
+       }
+
+       /**
+        * Change the FOR UPDATE option as necessary based on the join conditions. Then pass
+        * to the parent function to get the actual SQL text.
+        *
+        * In Postgres when using FOR UPDATE, only the main table and tables that are inner joined
+        * can be locked. That means tables in an outer join cannot be FOR UPDATE locked. Trying to do
+        * so causes a DB error. This wrapper checks which tables can be locked and adjusts it accordingly.
+        */
+       function selectSQLText( $table, $vars, $conds = '', $fname = __METHOD__, $options = array(), $join_conds = array() ) {
+               $forUpdateKey = array_search( 'FOR UPDATE', $options );
+               if ( $forUpdateKey !== false && $join_conds ) {
+                       unset( $options[$forUpdateKey] );
+
+                       foreach ( $join_conds as $table => $join_cond ) {
+                               if ( 0 === preg_match( '/^(?:LEFT|RIGHT|FULL)(?: OUTER)? JOIN$/i', $join_cond[0] ) ) {
+                                       $options['FOR UPDATE'][] = $table;
+                               }
+                       }
                }
 
-               return false;
+               return parent::selectSQLText( $table, $vars, $conds, $fname, $options, $join_conds );
        }
 
        /**
@@ -778,11 +848,10 @@ __INDEXATTR__;
         * $args may be a single associative array, or an array of these with numeric keys,
         * for multi-row insert (Postgres version 8.2 and above only).
         *
-        * @param $table   String: Name of the table to insert to.
-        * @param $args    Array: Items to insert into the table.
-        * @param $fname   String: Name of the function, for profiling
-        * @param string $options or Array. Valid options: IGNORE
-        *
+        * @param string $table Name of the table to insert to.
+        * @param array $args Items to insert into the table.
+        * @param string $fname Name of the function, for profiling
+        * @param array|string $options String or array. Valid options: IGNORE
         * @return bool Success of insert operation. IGNORE always returns true.
         */
        function insert( $table, $args, $fname = __METHOD__, $options = array() ) {
@@ -791,7 +860,7 @@ __INDEXATTR__;
                }
 
                $table = $this->tableName( $table );
-               if ( !isset( $this->numeric_version ) ) {
+               if ( !isset( $this->numericVersion ) ) {
                        $this->getServerVersion();
                }
 
@@ -820,7 +889,7 @@ __INDEXATTR__;
                $sql = "INSERT INTO $table (" . implode( ',', $keys ) . ') VALUES ';
 
                if ( $multi ) {
-                       if ( $this->numeric_version >= 8.2 && !$savepoint ) {
+                       if ( $this->numericVersion >= 8.2 && !$savepoint ) {
                                $first = true;
                                foreach ( $args as $row ) {
                                        if ( $first ) {
@@ -880,7 +949,7 @@ __INDEXATTR__;
                        }
                }
                if ( $savepoint ) {
-                       $olde = error_reporting( $olde );
+                       error_reporting( $olde );
                        $savepoint->commit();
 
                        // Set the affected row count for the whole operation
@@ -901,6 +970,14 @@ __INDEXATTR__;
         * $conds may be "*" to copy the whole table
         * srcTable may be an array of tables.
         * @todo FIXME: Implement this a little better (seperate select/insert)?
+        *
+        * @param string $destTable
+        * @param array|string $srcTable
+        * @param array $varMap
+        * @param array $conds
+        * @param string $fname
+        * @param array $insertOptions
+        * @param array $selectOptions
         * @return bool
         */
        function insertSelect( $destTable, $srcTable, $varMap, $conds, $fname = __METHOD__,
@@ -952,7 +1029,7 @@ __INDEXATTR__;
                                $savepoint->release();
                                $numrowsinserted++;
                        }
-                       $olde = error_reporting( $olde );
+                       error_reporting( $olde );
                        $savepoint->commit();
 
                        // Set the affected row count for the whole operation
@@ -984,7 +1061,9 @@ __INDEXATTR__;
 
        /**
         * Return the next in a sequence, save the value for retrieval via insertId()
-        * @return null
+        * 
+        * @param string $seqName
+        * @return int|null
         */
        function nextSequenceValue( $seqName ) {
                $safeseq = str_replace( "'", "''", $seqName );
@@ -997,7 +1076,9 @@ __INDEXATTR__;
 
        /**
         * Return the current value of a sequence. Assumes it has been nextval'ed in this session.
-        * @return
+        *
+        * @param string $seqName
+        * @return int
         */
        function currentSequenceValue( $seqName ) {
                $safeseq = str_replace( "'", "''", $seqName );
@@ -1074,10 +1155,10 @@ __INDEXATTR__;
         * This should really be handled by PHP PostgreSQL module
         *
         * @since 1.19
-        * @param $text   string: postgreql array returned in a text form like {a,b}
-        * @param $output string
-        * @param $limit  int
-        * @param $offset int
+        * @param string $text Postgreql array returned in a text form like {a,b}
+        * @param string $output
+        * @param int $limit
+        * @param int $offset
         * @return string
         */
        function pg_array_parse( $text, &$output, $limit = false, $offset = 1 ) {
@@ -1115,10 +1196,10 @@ __INDEXATTR__;
        }
 
        /**
-        * @return string wikitext of a link to the server software's web site
+        * @return string Wikitext of a link to the server software's web site
         */
        public function getSoftwareLink() {
-               return '[http://www.postgresql.org/ PostgreSQL]';
+               return '[{{int:version-db-postgres-url}} PostgreSQL]';
        }
 
        /**
@@ -1126,7 +1207,7 @@ __INDEXATTR__;
         * Needs transaction
         *
         * @since 1.19
-        * @return string return default schema for the current session
+        * @return string Default schema for the current session
         */
        function getCurrentSchema() {
                $res = $this->query( "SELECT current_schema()", __METHOD__ );
@@ -1162,7 +1243,7 @@ __INDEXATTR__;
         * Needs transaction
         *
         * @since 1.19
-        * @return array how to search for table names schemas for the current user
+        * @return array How to search for table names schemas for the current user
         */
        function getSearchPath() {
                $res = $this->query( "SHOW search_path", __METHOD__ );
@@ -1195,14 +1276,15 @@ __INDEXATTR__;
         * This will be also called by the installer after the schema is created
         *
         * @since 1.19
-        * @param $desired_schema string
+        *
+        * @param string $desiredSchema
         */
-       function determineCoreSchema( $desired_schema ) {
+       function determineCoreSchema( $desiredSchema ) {
                $this->begin( __METHOD__ );
-               if ( $this->schemaExists( $desired_schema ) ) {
-                       if ( in_array( $desired_schema, $this->getSchemas() ) ) {
-                               $this->mCoreSchema = $desired_schema;
-                               wfDebug( "Schema \"" . $desired_schema . "\" already in the search path\n" );
+               if ( $this->schemaExists( $desiredSchema ) ) {
+                       if ( in_array( $desiredSchema, $this->getSchemas() ) ) {
+                               $this->mCoreSchema = $desiredSchema;
+                               wfDebug( "Schema \"" . $desiredSchema . "\" already in the search path\n" );
                        } else {
                                /**
                                 * Prepend our schema (e.g. 'mediawiki') in front
@@ -1211,14 +1293,14 @@ __INDEXATTR__;
                                 */
                                $search_path = $this->getSearchPath();
                                array_unshift( $search_path,
-                                       $this->addIdentifierQuotes( $desired_schema ) );
+                                       $this->addIdentifierQuotes( $desiredSchema ) );
                                $this->setSearchPath( $search_path );
-                               $this->mCoreSchema = $desired_schema;
-                               wfDebug( "Schema \"" . $desired_schema . "\" added to the search path\n" );
+                               $this->mCoreSchema = $desiredSchema;
+                               wfDebug( "Schema \"" . $desiredSchema . "\" added to the search path\n" );
                        }
                } else {
                        $this->mCoreSchema = $this->getCurrentSchema();
-                       wfDebug( "Schema \"" . $desired_schema . "\" not found, using current \"" .
+                       wfDebug( "Schema \"" . $desiredSchema . "\" not found, using current \"" .
                                $this->mCoreSchema . "\"\n" );
                }
                /* Commit SET otherwise it will be rollbacked on error or IGNORE SELECT */
@@ -1239,26 +1321,29 @@ __INDEXATTR__;
         * @return string Version information from the database
         */
        function getServerVersion() {
-               if ( !isset( $this->numeric_version ) ) {
+               if ( !isset( $this->numericVersion ) ) {
                        $versionInfo = pg_version( $this->mConn );
                        if ( version_compare( $versionInfo['client'], '7.4.0', 'lt' ) ) {
                                // Old client, abort install
-                               $this->numeric_version = '7.3 or earlier';
+                               $this->numericVersion = '7.3 or earlier';
                        } elseif ( isset( $versionInfo['server'] ) ) {
                                // Normal client
-                               $this->numeric_version = $versionInfo['server'];
+                               $this->numericVersion = $versionInfo['server'];
                        } else {
                                // Bug 16937: broken pgsql extension from PHP<5.3
-                               $this->numeric_version = pg_parameter_status( $this->mConn, 'server_version' );
+                               $this->numericVersion = pg_parameter_status( $this->mConn, 'server_version' );
                        }
                }
 
-               return $this->numeric_version;
+               return $this->numericVersion;
        }
 
        /**
         * Query whether a given relation exists (in the given schema, or the
         * default mw one if not given)
+        * @param string $table
+        * @param array|string $types
+        * @param bool|string $schema
         * @return bool
         */
        function relationExists( $table, $types, $schema = false ) {
@@ -1283,6 +1368,9 @@ __INDEXATTR__;
        /**
         * For backward compatibility, this function checks both tables and
         * views.
+        * @param string $table
+        * @param string $fname
+        * @param bool|string $schema
         * @return bool
         */
        function tableExists( $table, $fname = __METHOD__, $schema = false ) {
@@ -1346,6 +1434,7 @@ SQL;
 
        /**
         * Query whether a given schema exists. Returns true if it does, false if it doesn't.
+        * @param string $schema
         * @return bool
         */
        function schemaExists( $schema ) {
@@ -1357,6 +1446,7 @@ SQL;
 
        /**
         * Returns true if a given role (i.e. user) exists, false otherwise.
+        * @param string $roleName
         * @return bool
         */
        function roleExists( $roleName ) {
@@ -1372,6 +1462,8 @@ SQL;
 
        /**
         * pg_field_type() wrapper
+        * @param ResultWrapper|resource $res ResultWrapper or PostgreSQL query result resource
+        * @param int $index Field number, starting from 0
         * @return string
         */
        function fieldType( $res, $index ) {
@@ -1383,7 +1475,7 @@ SQL;
        }
 
        /**
-        * @param $b
+        * @param string $b
         * @return Blob
         */
        function encodeBlob( $b ) {
@@ -1403,7 +1495,7 @@ SQL;
        }
 
        /**
-        * @param $s null|bool|Blob
+        * @param null|bool|Blob $s
         * @return int|string
         */
        function addQuotes( $s ) {
@@ -1422,21 +1514,18 @@ SQL;
         * Postgres specific version of replaceVars.
         * Calls the parent version in Database.php
         *
-        * @private
-        *
         * @param string $ins SQL string, read from a stream (usually tables.sql)
-        *
         * @return string SQL string
         */
        protected function replaceVars( $ins ) {
                $ins = parent::replaceVars( $ins );
 
-               if ( $this->numeric_version >= 8.3 ) {
+               if ( $this->numericVersion >= 8.3 ) {
                        // Thanks for not providing backwards-compatibility, 8.3
                        $ins = preg_replace( "/to_tsvector\s*\(\s*'default'\s*,/", 'to_tsvector(', $ins );
                }
 
-               if ( $this->numeric_version <= 8.1 ) { // Our minimum version
+               if ( $this->numericVersion <= 8.1 ) { // Our minimum version
                        $ins = str_replace( 'USING gin', 'USING gist', $ins );
                }
 
@@ -1446,10 +1535,8 @@ SQL;
        /**
         * Various select options
         *
-        * @private
-        *
         * @param array $options an associative array of options to be turned into
-        *              an SQL query, valid keys are listed in the function.
+        *   an SQL query, valid keys are listed in the function.
         * @return array
         */
        function makeSelectOptions( $options ) {
@@ -1473,9 +1560,12 @@ SQL;
                //              : false );
                //}
 
-               if ( isset( $noKeyOptions['FOR UPDATE'] ) ) {
+               if ( isset( $options['FOR UPDATE'] ) ) {
+                       $postLimitTail .= ' FOR UPDATE OF ' . implode( ', ', $options['FOR UPDATE'] );
+               } else if ( isset( $noKeyOptions['FOR UPDATE'] ) ) {
                        $postLimitTail .= ' FOR UPDATE';
                }
+
                if ( isset( $noKeyOptions['DISTINCT'] ) || isset( $noKeyOptions['DISTINCTROW'] ) ) {
                        $startOpts .= 'DISTINCT';
                }
@@ -1527,9 +1617,9 @@ SQL;
         * Check to see if a named lock is available. This is non-blocking.
         * See http://www.postgresql.org/docs/8.2/static/functions-admin.html#FUNCTIONS-ADVISORY-LOCKS
         *
-        * @param string $lockName name of lock to poll
-        * @param string $method name of method calling us
-        * @return Boolean
+        * @param string $lockName Name of lock to poll
+        * @param string $method Name of method calling us
+        * @return bool
         * @since 1.20
         */
        public function lockIsFree( $lockName, $method ) {
@@ -1543,9 +1633,9 @@ SQL;
 
        /**
         * See http://www.postgresql.org/docs/8.2/static/functions-admin.html#FUNCTIONS-ADVISORY-LOCKS
-        * @param $lockName string
-        * @param $method string
-        * @param $timeout int
+        * @param string $lockName
+        * @param string $method
+        * @param int $timeout
         * @return bool
         */
        public function lock( $lockName, $method, $timeout = 5 ) {
@@ -1568,8 +1658,8 @@ SQL;
        /**
         * See http://www.postgresql.org/docs/8.2/static/functions-admin.html#FUNCTIONS-ADVISORY-LOCKSFROM
         * PG DOCS: http://www.postgresql.org/docs/8.2/static/functions-admin.html#FUNCTIONS-ADVISORY-LOCKS
-        * @param $lockName string
-        * @param $method string
+        * @param string $lockName
+        * @param string $method
         * @return bool
         */
        public function unlock( $lockName, $method ) {