Aaron Schulz has uploaded a new change for review. (
https://gerrit.wikimedia.org/r/404418 )
Change subject: [WIP] rdbms: clean up non-native Database::replace() code
......................................................................
[WIP] rdbms: clean up non-native Database::replace() code
Change-Id: I30263df22066bd6d4836202b1bcad5d1aa1e7383
---
M includes/changetags/ChangeTags.php
M includes/libs/rdbms/database/Database.php
2 files changed, 23 insertions(+), 33 deletions(-)
git pull ssh://gerrit.wikimedia.org:29418/mediawiki/core
refs/changes/18/404418/1
diff --git a/includes/changetags/ChangeTags.php
b/includes/changetags/ChangeTags.php
index 95848ea..acabddb 100644
--- a/includes/changetags/ChangeTags.php
+++ b/includes/changetags/ChangeTags.php
@@ -417,8 +417,10 @@
$dbw->delete( 'tag_summary', $tsConds, __METHOD__ );
} else {
$dbw->replace( 'tag_summary',
- [ 'ts_rev_id', 'ts_rc_id', 'ts_log_id' ],
- array_filter( array_merge( $tsConds, [
'ts_tags' => implode( ',', $newTags ) ] ) ),
+ [ [ 'ts_rev_id' ], [ 'ts_rc_id' ], [
'ts_log_id' ] ],
+ array_filter(
+ array_merge( $tsConds, [ 'ts_tags' =>
implode( ',', $newTags ) ] )
+ ),
__METHOD__
);
}
diff --git a/includes/libs/rdbms/database/Database.php
b/includes/libs/rdbms/database/Database.php
index 2eb5c54..eecb504 100644
--- a/includes/libs/rdbms/database/Database.php
+++ b/includes/libs/rdbms/database/Database.php
@@ -2241,49 +2241,37 @@
}
public function replace( $table, $uniqueIndexes, $rows, $fname =
__METHOD__ ) {
- $quotedTable = $this->tableName( $table );
-
if ( count( $rows ) == 0 ) {
return;
}
- # Single row case
+ // Single row case
if ( !is_array( reset( $rows ) ) ) {
$rows = [ $rows ];
}
- // @FXIME: this is not atomic, but a trx would break
affectedRows()
+ // @FIXME: this is not atomic, but a trx would break
affectedRows()
foreach ( $rows as $row ) {
- # Delete rows which collide
- if ( $uniqueIndexes ) {
- $sql = "DELETE FROM $quotedTable WHERE ";
- $first = true;
- foreach ( $uniqueIndexes as $index ) {
- if ( $first ) {
- $first = false;
- $sql .= '( ';
- } else {
- $sql .= ' ) OR ( ';
- }
- if ( is_array( $index ) ) {
- $first2 = true;
- foreach ( $index as $col ) {
- if ( $first2 ) {
- $first2 = false;
- } else {
- $sql .= ' AND ';
- }
- $sql .= $col . '=' .
$this->addQuotes( $row[$col] );
- }
- } else {
- $sql .= $index . '=' .
$this->addQuotes( $row[$index] );
- }
+ // Delete rows which collide with this one
+ $indexWhereClauses = [];
+ foreach ( $uniqueIndexes as $index ) {
+ $indexColumns = is_array( $index ) ? $index : [
$index ];
+ $indexRowValues = array_intersect_key( $row,
array_flip( $indexColumns ) );
+ if ( count( $indexRowValues ) != count(
$indexColumns ) ) {
+ throw new DBUnexpectedError(
+ $this,
+ "Provided record does not
provide unique key values for " .
+ implode( ', ',
$indexColumns )
+ );
}
- $sql .= ' )';
- $this->query( $sql, $fname );
+ $indexWhereClauses[] = $this->makeList(
$indexRowValues, LIST_AND );
}
- # Now insert the row
+ if ( $indexWhereClauses ) {
+ $this->delete( $table, $this->makeList(
$indexWhereClauses, LIST_OR ), $fname );
+ }
+
+ // Now insert the row
$this->insert( $table, $row, $fname );
}
}
--
To view, visit https://gerrit.wikimedia.org/r/404418
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: newchange
Gerrit-Change-Id: I30263df22066bd6d4836202b1bcad5d1aa1e7383
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/core
Gerrit-Branch: master
Gerrit-Owner: Aaron Schulz <[email protected]>
_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits