Skip to content

Commit

Permalink
Fix UniqueConstraintViolationException while insert into files_extern…
Browse files Browse the repository at this point in the history
…al config tables

* followup to #12371

Signed-off-by: Morris Jobke <hey@morrisjobke.de>
  • Loading branch information
MorrisJobke committed Nov 14, 2018
1 parent 859dd1e commit 6bace99
Showing 1 changed file with 28 additions and 18 deletions.
46 changes: 28 additions & 18 deletions apps/files_external/lib/Service/DBConfigService.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

namespace OCA\Files_External\Service;

use Doctrine\DBAL\Exception\UniqueConstraintViolationException;
use OCP\DB\QueryBuilder\IQueryBuilder;
use OCP\IDBConnection;
use OCP\Security\ICrypto;
Expand Down Expand Up @@ -300,12 +301,15 @@ public function setConfig($mountId, $key, $value) {
if ($key === 'password') {
$value = $this->encryptValue($value);
}
$count = $this->connection->insertIfNotExist('*PREFIX*external_config', [
'mount_id' => $mountId,
'key' => $key,
'value' => $value
], ['mount_id', 'key']);
if ($count === 0) {

try {
$builder = $this->connection->getQueryBuilder();
$builder->insert('external_config')
->setValue('mount_id', $builder->createNamedParameter($mountId, IQueryBuilder::PARAM_INT))
->setValue('key', $builder->createNamedParameter($key, IQueryBuilder::PARAM_STR))
->setValue('value', $builder->createNamedParameter($value, IQueryBuilder::PARAM_STR))
->execute();
} catch(UniqueConstraintViolationException $e) {
$builder = $this->connection->getQueryBuilder();
$query = $builder->update('external_config')
->set('value', $builder->createNamedParameter($value, IQueryBuilder::PARAM_STR))
Expand All @@ -321,13 +325,14 @@ public function setConfig($mountId, $key, $value) {
* @param string $value
*/
public function setOption($mountId, $key, $value) {

$count = $this->connection->insertIfNotExist('*PREFIX*external_options', [
'mount_id' => $mountId,
'key' => $key,
'value' => json_encode($value)
], ['mount_id', 'key']);
if ($count === 0) {
try {
$builder = $this->connection->getQueryBuilder();
$builder->insert('external_options')
->setValue('mount_id', $builder->createNamedParameter($mountId, IQueryBuilder::PARAM_INT))
->setValue('key', $builder->createNamedParameter($key, IQueryBuilder::PARAM_STR))
->setValue('value', $builder->createNamedParameter(json_encode($value), IQueryBuilder::PARAM_STR))
->execute();
} catch(UniqueConstraintViolationException $e) {
$builder = $this->connection->getQueryBuilder();
$query = $builder->update('external_options')
->set('value', $builder->createNamedParameter(json_encode($value), IQueryBuilder::PARAM_STR))
Expand All @@ -338,11 +343,16 @@ public function setOption($mountId, $key, $value) {
}

public function addApplicable($mountId, $type, $value) {
$this->connection->insertIfNotExist('*PREFIX*external_applicable', [
'mount_id' => $mountId,
'type' => $type,
'value' => $value
], ['mount_id', 'type', 'value']);
try {
$builder = $this->connection->getQueryBuilder();
$builder->insert('external_applicable')
->setValue('mount_id', $builder->createNamedParameter($mountId))
->setValue('type', $builder->createNamedParameter($type))
->setValue('value', $builder->createNamedParameter($value))
->execute();
} catch(UniqueConstraintViolationException $e) {
// applicable exists already
}
}

public function removeApplicable($mountId, $type, $value) {
Expand Down

0 comments on commit 6bace99

Please sign in to comment.