Skip to content
Merged
Show file tree
Hide file tree
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
128 changes: 61 additions & 67 deletions src/Database/Adapter/MariaDB.php
Original file line number Diff line number Diff line change
Expand Up @@ -646,6 +646,7 @@ public function deleteIndex(string $collection, string $id): bool
* @throws Exception
* @throws PDOException
* @throws DuplicateException
* @throws \Throwable
*/
public function createDocument(string $collection, Document $document): Document
{
Expand All @@ -658,12 +659,6 @@ public function createDocument(string $collection, Document $document): Document
$columns = '';
$columnNames = '';

try {
$this->getPDO()->beginTransaction();
} catch (PDOException $e) {
$this->getPDO()->rollBack();
}

/**
* Insert Attributes
*/
Expand Down Expand Up @@ -732,27 +727,28 @@ public function createDocument(string $collection, Document $document): Document
}

try {
$this->getPDO()->beginTransaction();
$stmt->execute();

$document['$internalId'] = $this->getDocument($collection, $document->getId())->getInternalId();

if (isset($stmtPermissions)) {
$stmtPermissions->execute();
}
} catch (PDOException $e) {

$this->getPDO()->commit();
} catch (\Throwable $e) {
$this->getPDO()->rollBack();
switch ($e->getCode()) {
case 1062:
case 23000:
throw new DuplicateException('Duplicated document: ' . $e->getMessage());

default:
throw $e;
if($e instanceof PDOException) {
switch ($e->getCode()) {
case 1062:
case 23000:
throw new DuplicateException('Duplicated document: ' . $e->getMessage());
}
}
}

if (!$this->getPDO()->commit()) {
throw new DatabaseException('Failed to commit transaction');
throw $e;
}

return $document;
Expand All @@ -768,16 +764,17 @@ public function createDocument(string $collection, Document $document): Document
* @return array<Document>
*
* @throws DuplicateException
* @throws \Throwable
*/
public function createDocuments(string $collection, array $documents, int $batchSize = Database::INSERT_BATCH_SIZE): array
{
if (empty($documents)) {
return $documents;
}

$this->getPDO()->beginTransaction();

try {
$this->getPDO()->beginTransaction();

$name = $this->filter($collection);
$batches = \array_chunk($documents, max(1, $batchSize));

Expand Down Expand Up @@ -845,20 +842,22 @@ public function createDocuments(string $collection, array $documents, int $batch
}
}

if (!$this->getPDO()->commit()) {
throw new Exception('Failed to commit transaction');
}

return $documents;

} catch (PDOException $e) {
$this->getPDO()->commit();
} catch (\Throwable $e) {
$this->getPDO()->rollBack();

throw match ($e->getCode()) {
1062, 23000 => new DuplicateException('Duplicated document: ' . $e->getMessage()),
default => $e,
};
if($e instanceof PDOException) {
switch ($e->getCode()) {
case 1062:
case 23000:
throw new DuplicateException('Duplicated document: ' . $e->getMessage());
}
}

throw $e;
}

return $documents;
}

/**
Expand All @@ -870,6 +869,7 @@ public function createDocuments(string $collection, array $documents, int $batch
* @throws Exception
* @throws PDOException
* @throws DuplicateException
* @throws \Throwable
*/
public function updateDocument(string $collection, Document $document): Document
{
Expand Down Expand Up @@ -909,12 +909,6 @@ public function updateDocument(string $collection, Document $document): Document
return $carry;
}, $initial);

try {
$this->getPDO()->beginTransaction();
} catch (PDOException $e) {
$this->getPDO()->rollBack();
}

/**
* Get removed Permissions
*/
Expand Down Expand Up @@ -1041,6 +1035,8 @@ public function updateDocument(string $collection, Document $document): Document
}

try {
$this->getPDO()->beginTransaction();

$stmt->execute();

if (isset($stmtRemovePermissions)) {
Expand All @@ -1049,20 +1045,20 @@ public function updateDocument(string $collection, Document $document): Document
if (isset($stmtAddPermissions)) {
$stmtAddPermissions->execute();
}
} catch (PDOException $e) {

$this->getPDO()->commit();
} catch (\Throwable $e) {
$this->getPDO()->rollBack();
switch ($e->getCode()) {
case 1062:
case 23000:
throw new DuplicateException('Duplicated document: ' . $e->getMessage());

default:
throw $e;
if($e instanceof PDOException) {
switch ($e->getCode()) {
case 1062:
case 23000:
throw new DuplicateException('Duplicated document: ' . $e->getMessage());
}
}
}

if (!$this->getPDO()->commit()) {
throw new DatabaseException('Failed to commit transaction');
throw $e;
}

return $document;
Expand All @@ -1078,16 +1074,17 @@ public function updateDocument(string $collection, Document $document): Document
* @return array<Document>
*
* @throws DuplicateException
* @throws \Throwable
*/
public function updateDocuments(string $collection, array $documents, int $batchSize = Database::INSERT_BATCH_SIZE): array
{
if (empty($documents)) {
return $documents;
}

$this->getPDO()->beginTransaction();

try {
$this->getPDO()->beginTransaction();

$name = $this->filter($collection);
$batches = \array_chunk($documents, max(1, $batchSize));

Expand Down Expand Up @@ -1268,19 +1265,22 @@ public function updateDocuments(string $collection, array $documents, int $batch
}
}

if (!$this->getPDO()->commit()) {
throw new Exception('Failed to commit transaction');
}

return $documents;
} catch (PDOException $e) {
$this->getPDO()->commit();
} catch (\Throwable $e) {
$this->getPDO()->rollBack();

throw match ($e->getCode()) {
1062, 23000 => new DuplicateException('Duplicated document: ' . $e->getMessage()),
default => $e,
};
if($e instanceof PDOException) {
switch ($e->getCode()) {
case 1062:
case 23000:
throw new DuplicateException('Duplicated document: ' . $e->getMessage());
}
}

throw $e;
}

return $documents;
}

/**
Expand Down Expand Up @@ -1335,12 +1335,6 @@ public function deleteDocument(string $collection, string $id): bool
{
$name = $this->filter($collection);

try {
$this->getPDO()->beginTransaction();
} catch (PDOException $e) {
$this->getPDO()->rollBack();
}

$sql = "
DELETE FROM {$this->getSQLTable($name)}
WHERE _uid = :_uid
Expand All @@ -1363,21 +1357,21 @@ public function deleteDocument(string $collection, string $id): bool
$stmtPermissions->bindValue(':_uid', $id);

try {
$this->getPDO()->beginTransaction();

if (!$stmt->execute()) {
throw new DatabaseException('Failed to delete document');
}
if (!$stmtPermissions->execute()) {
throw new DatabaseException('Failed to clean permissions');
}

$this->getPDO()->commit();
} catch (\Throwable $th) {
$this->getPDO()->rollBack();
throw new DatabaseException($th->getMessage());
}

if (!$this->getPDO()->commit()) {
throw new DatabaseException('Failed to commit transaction');
}

return true;
}

Expand Down
2 changes: 1 addition & 1 deletion src/Database/Adapter/SQL.php
Original file line number Diff line number Diff line change
Expand Up @@ -829,7 +829,7 @@ protected function getSQLPermissionsCondition(string $collection, array $roles):
{
$roles = array_map(fn (string $role) => $this->getPDO()->quote($role), $roles);
return "table_main._uid IN (
SELECT distinct(_document)
SELECT _document
FROM {$this->getSQLTable($collection . '_perms')}
WHERE _permission IN (" . implode(', ', $roles) . ")
AND _type = 'read'
Expand Down