Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix UniqueConstraintViolationException while insert into files_external config tables #12417

Merged
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
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