Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[stable10] Backport of setvalues to use insertIfNotExists #28486

Merged
merged 1 commit into from
Jul 26, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 10 additions & 12 deletions lib/private/DB/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ private function getType($value) {
}

/**
* Insert or update a row value
* Insert or update a row value.
*
* @param string $table
* @param array $keys (column name => value)
Expand All @@ -271,16 +271,13 @@ private function getType($value) {
* @throws PreConditionNotMetException
*/
public function setValues($table, array $keys, array $values, array $updatePreconditionValues = []) {
try {
$insertQb = $this->getQueryBuilder();
$insertQb->insert($table)
->values(
array_map(function($value) use ($insertQb) {
return $insertQb->createNamedParameter($value, $this->getType($value));
}, array_merge($keys, $values))
);
return $insertQb->execute();
} catch (ConstraintViolationException $e) {
// Try to insert whole record into the table ($toInsert) if predicate NOT EXISTS ($compare) is satisfied
$toInsert = array_merge($keys, $values);
$compare = array_keys($keys);
$tableName = $this->tablePrefix . $table;
$affected = $this->adapter->insertIfNotExist($tableName, $toInsert, $compare);

if ($affected === 0) {
// value already exists, try update
$updateQb = $this->getQueryBuilder();
$updateQb->update($table);
Expand All @@ -303,8 +300,9 @@ public function setValues($table, array $keys, array $values, array $updatePreco
throw new PreConditionNotMetException();
}

return 0;
}

return $affected;
}

/**
Expand Down