Merge "Rewrite pref cleanup script"
[lhc/web/wiklou.git] / includes / libs / rdbms / database / DatabasePostgres.php
index bdac06c..5bf845b 100644 (file)
@@ -39,8 +39,6 @@ class DatabasePostgres extends Database {
        /** @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 */
@@ -352,14 +350,10 @@ class DatabasePostgres extends Database {
                return pg_field_name( $res, $n );
        }
 
-       /**
-        * Return the result of the last call to nextSequenceValue();
-        * This must be called after nextSequenceValue().
-        *
-        * @return int|null
-        */
        public function insertId() {
-               return $this->mInsertId;
+               $res = $this->query( "SELECT lastval()" );
+               $row = $this->fetchRow( $res );
+               return is_null( $row[0] ) ? null : (int)$row[0];
        }
 
        public function dataSeek( $res, $row ) {
@@ -390,7 +384,7 @@ class DatabasePostgres extends Database {
                }
        }
 
-       public function affectedRows() {
+       protected function fetchAffectedRowCount() {
                if ( !is_null( $this->mAffectedRows ) ) {
                        // Forced result for simulated queries
                        return $this->mAffectedRows;
@@ -521,6 +515,10 @@ __INDEXATTR__;
        public function selectSQLText(
                $table, $vars, $conds = '', $fname = __METHOD__, $options = [], $join_conds = []
        ) {
+               if ( is_string( $options ) ) {
+                       $options = [ $options ];
+               }
+
                // 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
@@ -532,10 +530,30 @@ __INDEXATTR__;
                        $forUpdateKey = array_search( 'FOR UPDATE', $options, true );
                        if ( $forUpdateKey !== false && $join_conds ) {
                                unset( $options[$forUpdateKey] );
+                               $options['FOR UPDATE'] = [];
+
+                               $toCheck = $table;
+                               reset( $toCheck );
+                               while ( $toCheck ) {
+                                       $alias = key( $toCheck );
+                                       $name = $toCheck[$alias];
+                                       unset( $toCheck[$alias] );
+
+                                       $hasAlias = !is_numeric( $alias );
+                                       if ( !$hasAlias && is_string( $name ) ) {
+                                               $alias = $name;
+                                       }
 
-                               foreach ( $join_conds as $table_cond => $join_cond ) {
-                                       if ( 0 === preg_match( '/^(?:LEFT|RIGHT|FULL)(?: OUTER)? JOIN$/i', $join_cond[0] ) ) {
-                                               $options['FOR UPDATE'][] = $table_cond;
+                                       if ( !isset( $join_conds[$alias] ) ||
+                                               !preg_match( '/^(?:LEFT|RIGHT|FULL)(?: OUTER)? JOIN$/i', $join_conds[$alias][0] )
+                                       ) {
+                                               if ( is_array( $name ) ) {
+                                                       // It's a parenthesized group, process all the tables inside the group.
+                                                       $toCheck = array_merge( $toCheck, $name );
+                                               } else {
+                                                       // Quote alias names so $this->tableName() won't mangle them
+                                                       $options['FOR UPDATE'][] = $hasAlias ? $this->addIdentifierQuotes( $alias ) : $alias;
+                                               }
                                        }
                                }
                        }
@@ -583,6 +601,7 @@ __INDEXATTR__;
                }
 
                // If IGNORE is set, we use savepoints to emulate mysql's behavior
+               // @todo If PostgreSQL 9.5+, we could use ON CONFLICT DO NOTHING instead
                $savepoint = $olde = null;
                $numrowsinserted = 0;
                if ( in_array( 'IGNORE', $options ) ) {
@@ -696,39 +715,17 @@ __INDEXATTR__;
                }
 
                /*
-                * If IGNORE is set, we use savepoints to emulate mysql's behavior
-                * Ignore LOW PRIORITY option, since it is MySQL-specific
+                * If IGNORE is set, use the non-native version.
+                * @todo If PostgreSQL 9.5+, we could use ON CONFLICT DO NOTHING
                 */
-               $savepoint = $olde = null;
-               $numrowsinserted = 0;
                if ( in_array( 'IGNORE', $insertOptions ) ) {
-                       $savepoint = new SavepointPostgres( $this, 'mw', $this->queryLogger );
-                       $olde = error_reporting( 0 );
-                       $savepoint->savepoint();
+                       return $this->nonNativeInsertSelect(
+                               $destTable, $srcTable, $varMap, $conds, $fname, $insertOptions, $selectOptions, $selectJoinConds
+                       );
                }
 
-               $res = parent::nativeInsertSelect( $destTable, $srcTable, $varMap, $conds, $fname,
+               return parent::nativeInsertSelect( $destTable, $srcTable, $varMap, $conds, $fname,
                        $insertOptions, $selectOptions, $selectJoinConds );
-
-               if ( $savepoint ) {
-                       $bar = pg_result_error( $this->mLastResult );
-                       if ( $bar != false ) {
-                               $savepoint->rollback();
-                       } else {
-                               $savepoint->release();
-                               $numrowsinserted++;
-                       }
-                       error_reporting( $olde );
-                       $savepoint->commit();
-
-                       // Set the affected row count for the whole operation
-                       $this->mAffectedRows = $numrowsinserted;
-
-                       // IGNORE always returns true
-                       return true;
-               }
-
-               return $res;
        }
 
        public function tableName( $name, $format = 'quoted' ) {
@@ -756,12 +753,7 @@ __INDEXATTR__;
        }
 
        public function nextSequenceValue( $seqName ) {
-               $safeseq = str_replace( "'", "''", $seqName );
-               $res = $this->query( "SELECT nextval('$safeseq')" );
-               $row = $this->fetchRow( $res );
-               $this->mInsertId = is_null( $row[0] ) ? null : (int)$row[0];
-
-               return $this->mInsertId;
+               return new NextSequenceValue;
        }
 
        /**
@@ -1149,8 +1141,8 @@ SQL;
        }
 
        /**
-        * @var string $table
-        * @var string $field
+        * @param string $table
+        * @param string $field
         * @return PostgresField|null
         */
        public function fieldInfo( $table, $field ) {
@@ -1187,7 +1179,7 @@ SQL;
 
        public function strencode( $s ) {
                // Should not be called by us
-               return pg_escape_string( $this->getBindingHandle(), $s );
+               return pg_escape_string( $this->getBindingHandle(), (string)$s );
        }
 
        public function addQuotes( $s ) {
@@ -1204,9 +1196,11 @@ SQL;
                                $s = pg_escape_bytea( $conn, $s->fetch() );
                        }
                        return "'$s'";
+               } elseif ( $s instanceof NextSequenceValue ) {
+                       return 'DEFAULT';
                }
 
-               return "'" . pg_escape_string( $conn, $s ) . "'";
+               return "'" . pg_escape_string( $conn, (string)$s ) . "'";
        }
 
        /**