use insertIfNotExists to store new mimetypes.

Also throw an error if we can't find the mimetype after insert

Signed-off-by: Robin Appelman <robin@icewind.nl>
This commit is contained in:
Robin Appelman 2018-08-09 15:19:23 +02:00
parent a5985ad0f4
commit 706813ab08
No known key found for this signature in database
GPG Key ID: 42B69D8A64526EFB
1 changed files with 7 additions and 14 deletions

View File

@ -114,20 +114,9 @@ class Loader implements IMimeTypeLoader {
* @param int inserted ID
*/
protected function store($mimetype) {
try {
$qb = $this->dbConnection->getQueryBuilder();
$qb->insert('mimetypes')
->values([
'mimetype' => $qb->createNamedParameter($mimetype)
$this->dbConnection->insertIfNotExist('*PREFIX*mimetypes', [
'mimetype' => $mimetype
]);
$qb->execute();
} catch (UniqueConstraintViolationException $e) {
if ($this->dbConnection->inTransaction()) {
// if we're inside a transaction we can't recover safely
throw $e;
}
// something inserted it before us
}
$fetch = $this->dbConnection->getQueryBuilder();
$fetch->select('id')
@ -137,6 +126,10 @@ class Loader implements IMimeTypeLoader {
));
$row = $fetch->execute()->fetch();
if (!$row) {
throw new \Exception("Failed to get mimetype id for $mimetype after trying to store it");
}
$this->mimetypes[$row['id']] = $mimetype;
$this->mimetypeIds[$mimetype] = $row['id'];
return $row['id'];