diff options
author | Roeland Jago Douma <rullzer@users.noreply.github.com> | 2018-10-02 10:03:31 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-10-02 10:03:31 +0200 |
commit | 5581e0e9eb512782d2bc1982639e5fb2ef7c910f (patch) | |
tree | 935439e745ea96c7c8a22b96876aa6c1baf5a7e5 | |
parent | ac1d291b72d2511903c6f8d4e8143b708cdccdbc (diff) | |
parent | 3f594fc1b7fbd34461d9fdfba71f416abb935e25 (diff) | |
download | nextcloud-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.php | 20 |
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 |