From f41a0837a981ac37f00d3196db4086557527e155 Mon Sep 17 00:00:00 2001 From: Aaron Schulz Date: Tue, 12 Mar 2019 20:09:21 -0700 Subject: [PATCH] rdbms: clarify $uniqueIndexes argument to replace()/upsert() Also make upsert() match replace() for consistency. Change-Id: I208f3ab810a61c6949ac0050436767675f99a60b --- includes/db/DatabaseOracle.php | 2 +- includes/libs/rdbms/database/DBConnRef.php | 2 +- includes/libs/rdbms/database/Database.php | 4 +++- .../libs/rdbms/database/DatabaseMysqlBase.php | 18 ++---------------- includes/libs/rdbms/database/IDatabase.php | 14 +++++++++----- 5 files changed, 16 insertions(+), 24 deletions(-) diff --git a/includes/db/DatabaseOracle.php b/includes/db/DatabaseOracle.php index bb2d3f780c..a051d83aef 100644 --- a/includes/db/DatabaseOracle.php +++ b/includes/db/DatabaseOracle.php @@ -591,7 +591,7 @@ class DatabaseOracle extends Database { } } - public function upsert( $table, array $rows, array $uniqueIndexes, array $set, + public function upsert( $table, array $rows, $uniqueIndexes, array $set, $fname = __METHOD__ ) { if ( $rows === [] ) { diff --git a/includes/libs/rdbms/database/DBConnRef.php b/includes/libs/rdbms/database/DBConnRef.php index ab70fc80cc..4fdac81e66 100644 --- a/includes/libs/rdbms/database/DBConnRef.php +++ b/includes/libs/rdbms/database/DBConnRef.php @@ -417,7 +417,7 @@ class DBConnRef implements IDatabase { } public function upsert( - $table, array $rows, array $uniqueIndexes, array $set, $fname = __METHOD__ + $table, array $rows, $uniqueIndexes, array $set, $fname = __METHOD__ ) { return $this->__call( __FUNCTION__, func_get_args() ); } diff --git a/includes/libs/rdbms/database/Database.php b/includes/libs/rdbms/database/Database.php index 49b2792210..3077fbe479 100644 --- a/includes/libs/rdbms/database/Database.php +++ b/includes/libs/rdbms/database/Database.php @@ -2782,6 +2782,7 @@ abstract class Database implements IDatabase, IMaintainableDatabase, LoggerAware return; } + $uniqueIndexes = (array)$uniqueIndexes; // Single row case if ( !is_array( reset( $rows ) ) ) { $rows = [ $rows ]; @@ -2861,13 +2862,14 @@ abstract class Database implements IDatabase, IMaintainableDatabase, LoggerAware $this->query( $sql, $fname ); } - public function upsert( $table, array $rows, array $uniqueIndexes, array $set, + public function upsert( $table, array $rows, $uniqueIndexes, array $set, $fname = __METHOD__ ) { if ( $rows === [] ) { return true; // nothing to do } + $uniqueIndexes = (array)$uniqueIndexes; if ( !is_array( reset( $rows ) ) ) { $rows = [ $rows ]; } diff --git a/includes/libs/rdbms/database/DatabaseMysqlBase.php b/includes/libs/rdbms/database/DatabaseMysqlBase.php index 62110ef45a..7fbd34d02b 100644 --- a/includes/libs/rdbms/database/DatabaseMysqlBase.php +++ b/includes/libs/rdbms/database/DatabaseMysqlBase.php @@ -489,12 +489,6 @@ abstract class DatabaseMysqlBase extends Database { return $errno == 2062; } - /** - * @param string $table - * @param array $uniqueIndexes - * @param array $rows - * @param string $fname - */ public function replace( $table, $uniqueIndexes, $rows, $fname = __METHOD__ ) { $this->nativeReplace( $table, $rows, $fname ); } @@ -1326,16 +1320,8 @@ abstract class DatabaseMysqlBase extends Database { $this->query( $sql, $fname ); } - /** - * @param string $table - * @param array $rows - * @param array $uniqueIndexes - * @param array $set - * @param string $fname - * @return bool - */ - public function upsert( $table, array $rows, array $uniqueIndexes, - array $set, $fname = __METHOD__ + public function upsert( + $table, array $rows, $uniqueIndexes, array $set, $fname = __METHOD__ ) { if ( $rows === [] ) { return true; // nothing to do diff --git a/includes/libs/rdbms/database/IDatabase.php b/includes/libs/rdbms/database/IDatabase.php index 7d9eac1a57..3401541a17 100644 --- a/includes/libs/rdbms/database/IDatabase.php +++ b/includes/libs/rdbms/database/IDatabase.php @@ -1232,8 +1232,10 @@ interface IDatabase { * errors which wouldn't have occurred in MySQL. * * @param string $table The table to replace the row(s) in. - * @param array $uniqueIndexes Either a list of fields that define a unique index or - * an array of such lists if there are multiple unique indexes defined in the schema + * @param array[]|string[]|string $uniqueIndexes All unique indexes. One of the following: + * a) the one unique field in the table (when no composite unique key exist) + * b) a list of all unique fields in the table (when no composite unique key exist) + * c) a list of all unique indexes in the table (each as a list of the indexed fields) * @param array $rows Can be either a single row to insert, or multiple rows, * in the same format as for IDatabase::insert() * @param string $fname Calling function name (use __METHOD__) for logs/profiling @@ -1267,8 +1269,10 @@ interface IDatabase { * * @param string $table Table name. This will be passed through Database::tableName(). * @param array $rows A single row or list of rows to insert - * @param array $uniqueIndexes Either a list of fields that define a unique index or - * an array of such lists if there are multiple unique indexes defined in the schema + * @param array[]|string[]|string $uniqueIndexes All unique indexes. One of the following: + * a) the one unique field in the table (when no composite unique key exist) + * b) a list of all unique fields in the table (when no composite unique key exist) + * c) a list of all unique indexes in the table (each as a list of the indexed fields) * @param array $set An array of values to SET. For each array element, the * key gives the field name, and the value gives the data to set that * field to. The data will be quoted by IDatabase::addQuotes(). @@ -1279,7 +1283,7 @@ interface IDatabase { * @return bool Return true if no exception was thrown (deprecated since 1.33) */ public function upsert( - $table, array $rows, array $uniqueIndexes, array $set, $fname = __METHOD__ + $table, array $rows, $uniqueIndexes, array $set, $fname = __METHOD__ ); /** -- 2.20.1