Skip to content
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