summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRoeland Jago Douma <rullzer@users.noreply.github.com>2018-10-02 10:03:31 +0200
committerGitHub <noreply@github.com>2018-10-02 10:03:31 +0200
commit5581e0e9eb512782d2bc1982639e5fb2ef7c910f (patch)
tree935439e745ea96c7c8a22b96876aa6c1baf5a7e5
parentac1d291b72d2511903c6f8d4e8143b708cdccdbc (diff)
parent3f594fc1b7fbd34461d9fdfba71f416abb935e25 (diff)
downloadnextcloud-server-5581e0e9eb512782d2bc1982639e5fb2ef7c910f.tar.gz
nextcloud-server-5581e0e9eb512782d2bc1982639e5fb2ef7c910f.zip
Merge pull request #11084 from nextcloud/feature/qbmapper-insert-or-update
Add QBMapper::insertOrUpdate()
-rw-r--r--lib/public/AppFramework/Db/QBMapper.php20
1 files changed, 19 insertions, 1 deletions
diff --git a/lib/public/AppFramework/Db/QBMapper.php b/lib/public/AppFramework/Db/QBMapper.php
index a9b38732a30..dbc47d2d43d 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,24 @@ 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
+ * @throws \InvalidArgumentException if entity has no 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