Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/nextcloud/server.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorChristoph Wurst <christoph@winzerhof-wurst.at>2018-09-06 09:46:18 +0300
committerRoeland Jago Douma <roeland@famdouma.nl>2018-10-02 09:09:27 +0300
commit40fdff5b8073f370daf347f24d9d95d111e62b96 (patch)
treeb53ee17c2c563a4e4f199b8213b87030896de16d /lib
parent7526971c95250807ac1ec485297919a27ee0dcfc (diff)
Add QBMapper::insertOrUpdate()
This allows elegant upserts where the entity ID is provided (e.g. by an external system) and when that data is fed into our database multiple times. Signed-off-by: Christoph Wurst <christoph@winzerhof-wurst.at>
Diffstat (limited to 'lib')
-rw-r--r--lib/public/AppFramework/Db/QBMapper.php19
1 files changed, 18 insertions, 1 deletions
diff --git a/lib/public/AppFramework/Db/QBMapper.php b/lib/public/AppFramework/Db/QBMapper.php
index a9b38732a30..b8ebd379b4d 100644
--- a/lib/public/AppFramework/Db/QBMapper.php
+++ b/lib/public/AppFramework/Db/QBMapper.php
@@ -24,6 +24,7 @@ declare(strict_types=1);
namespace OCP\AppFramework\Db;
+use Doctrine\DBAL\Exception\UniqueConstraintViolationException;
use OCP\DB\QueryBuilder\IQueryBuilder;
use OCP\IDBConnection;
@@ -123,7 +124,23 @@ abstract class QBMapper {
return $entity;
}
-
+ /**
+ * Tries to creates a new entry in the db from an entity and
+ * updates an existing entry if duplicate keys are detected
+ * by the database
+ *
+ * @param Entity $entity the entity that should be created/updated
+ * @return Entity the saved entity with the (new) id
+ * @since 15.0.0
+ * @suppress SqlInjectionChecker
+ */
+ public function insertOrUpdate(Entity $entity): Entity {
+ try {
+ return $this->insert($entity);
+ } catch (UniqueConstraintViolationException $ex) {
+ return $this->update($entity);
+ }
+ }
/**
* Updates an entry in the db from an entity