Skip to content

Commit a1be186

Browse files
authored
Merge pull request #611 from utopia-php/optimize-update-document
Skip permissions update
2 parents 276fffb + 130beb0 commit a1be186

File tree

6 files changed

+294
-279
lines changed

6 files changed

+294
-279
lines changed

src/Database/Adapter.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -697,10 +697,11 @@ abstract public function createDocuments(string $collection, array $documents):
697697
* @param string $collection
698698
* @param string $id
699699
* @param Document $document
700+
* @param bool $skipPermissions
700701
*
701702
* @return Document
702703
*/
703-
abstract public function updateDocument(string $collection, string $id, Document $document): Document;
704+
abstract public function updateDocument(string $collection, string $id, Document $document, bool $skipPermissions): Document;
704705

705706
/**
706707
* Update documents

src/Database/Adapter/MariaDB.php

Lines changed: 100 additions & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -926,13 +926,14 @@ public function createDocument(string $collection, Document $document): Document
926926
* @param string $collection
927927
* @param string $id
928928
* @param Document $document
929+
* @param bool $skipPermissions
929930
* @return Document
930931
* @throws Exception
931932
* @throws PDOException
932933
* @throws DuplicateException
933934
* @throws \Throwable
934935
*/
935-
public function updateDocument(string $collection, string $id, Document $document): Document
936+
public function updateDocument(string $collection, string $id, Document $document, bool $skipPermissions): Document
936937
{
937938
try {
938939
$attributes = $document->getAttributes();
@@ -943,149 +944,151 @@ public function updateDocument(string $collection, string $id, Document $documen
943944
$name = $this->filter($collection);
944945
$columns = '';
945946

946-
$sql = "
947+
if (!$skipPermissions) {
948+
$sql = "
947949
SELECT _type, _permission
948950
FROM {$this->getSQLTable($name . '_perms')}
949951
WHERE _document = :_uid
950952
{$this->getTenantQuery($collection)}
951953
";
952954

953-
$sql = $this->trigger(Database::EVENT_PERMISSIONS_READ, $sql);
955+
$sql = $this->trigger(Database::EVENT_PERMISSIONS_READ, $sql);
954956

955-
/**
956-
* Get current permissions from the database
957-
*/
958-
$sqlPermissions = $this->getPDO()->prepare($sql);
959-
$sqlPermissions->bindValue(':_uid', $document->getId());
957+
/**
958+
* Get current permissions from the database
959+
*/
960+
$sqlPermissions = $this->getPDO()->prepare($sql);
961+
$sqlPermissions->bindValue(':_uid', $document->getId());
960962

961-
if ($this->sharedTables) {
962-
$sqlPermissions->bindValue(':_tenant', $this->tenant);
963-
}
963+
if ($this->sharedTables) {
964+
$sqlPermissions->bindValue(':_tenant', $this->tenant);
965+
}
964966

965-
$sqlPermissions->execute();
966-
$permissions = $sqlPermissions->fetchAll();
967-
$sqlPermissions->closeCursor();
967+
$sqlPermissions->execute();
968+
$permissions = $sqlPermissions->fetchAll();
969+
$sqlPermissions->closeCursor();
968970

969-
$initial = [];
970-
foreach (Database::PERMISSIONS as $type) {
971-
$initial[$type] = [];
972-
}
971+
$initial = [];
972+
foreach (Database::PERMISSIONS as $type) {
973+
$initial[$type] = [];
974+
}
973975

974-
$permissions = array_reduce($permissions, function (array $carry, array $item) {
975-
$carry[$item['_type']][] = $item['_permission'];
976+
$permissions = array_reduce($permissions, function (array $carry, array $item) {
977+
$carry[$item['_type']][] = $item['_permission'];
976978

977-
return $carry;
978-
}, $initial);
979+
return $carry;
980+
}, $initial);
979981

980-
/**
981-
* Get removed Permissions
982-
*/
983-
$removals = [];
984-
foreach (Database::PERMISSIONS as $type) {
985-
$diff = \array_diff($permissions[$type], $document->getPermissionsByType($type));
986-
if (!empty($diff)) {
987-
$removals[$type] = $diff;
982+
/**
983+
* Get removed Permissions
984+
*/
985+
$removals = [];
986+
foreach (Database::PERMISSIONS as $type) {
987+
$diff = \array_diff($permissions[$type], $document->getPermissionsByType($type));
988+
if (!empty($diff)) {
989+
$removals[$type] = $diff;
990+
}
988991
}
989-
}
990992

991-
/**
992-
* Get added Permissions
993-
*/
994-
$additions = [];
995-
foreach (Database::PERMISSIONS as $type) {
996-
$diff = \array_diff($document->getPermissionsByType($type), $permissions[$type]);
997-
if (!empty($diff)) {
998-
$additions[$type] = $diff;
993+
/**
994+
* Get added Permissions
995+
*/
996+
$additions = [];
997+
foreach (Database::PERMISSIONS as $type) {
998+
$diff = \array_diff($document->getPermissionsByType($type), $permissions[$type]);
999+
if (!empty($diff)) {
1000+
$additions[$type] = $diff;
1001+
}
9991002
}
1000-
}
10011003

1002-
/**
1003-
* Query to remove permissions
1004-
*/
1005-
$removeQuery = '';
1006-
if (!empty($removals)) {
1007-
$removeQuery = ' AND (';
1008-
foreach ($removals as $type => $permissions) {
1009-
$removeQuery .= "(
1004+
/**
1005+
* Query to remove permissions
1006+
*/
1007+
$removeQuery = '';
1008+
if (!empty($removals)) {
1009+
$removeQuery = ' AND (';
1010+
foreach ($removals as $type => $permissions) {
1011+
$removeQuery .= "(
10101012
_type = '{$type}'
10111013
AND _permission IN (" . implode(', ', \array_map(fn (string $i) => ":_remove_{$type}_{$i}", \array_keys($permissions))) . ")
10121014
)";
1013-
if ($type !== \array_key_last($removals)) {
1014-
$removeQuery .= ' OR ';
1015+
if ($type !== \array_key_last($removals)) {
1016+
$removeQuery .= ' OR ';
1017+
}
10151018
}
10161019
}
1017-
}
1018-
if (!empty($removeQuery)) {
1019-
$removeQuery .= ')';
1020-
$sql = "
1020+
if (!empty($removeQuery)) {
1021+
$removeQuery .= ')';
1022+
$sql = "
10211023
DELETE
10221024
FROM {$this->getSQLTable($name . '_perms')}
10231025
WHERE _document = :_uid
10241026
{$this->getTenantQuery($collection)}
10251027
";
10261028

1027-
$removeQuery = $sql . $removeQuery;
1029+
$removeQuery = $sql . $removeQuery;
10281030

1029-
$removeQuery = $this->trigger(Database::EVENT_PERMISSIONS_DELETE, $removeQuery);
1031+
$removeQuery = $this->trigger(Database::EVENT_PERMISSIONS_DELETE, $removeQuery);
10301032

1031-
$stmtRemovePermissions = $this->getPDO()->prepare($removeQuery);
1032-
$stmtRemovePermissions->bindValue(':_uid', $document->getId());
1033+
$stmtRemovePermissions = $this->getPDO()->prepare($removeQuery);
1034+
$stmtRemovePermissions->bindValue(':_uid', $document->getId());
10331035

1034-
if ($this->sharedTables) {
1035-
$stmtRemovePermissions->bindValue(':_tenant', $this->tenant);
1036-
}
1036+
if ($this->sharedTables) {
1037+
$stmtRemovePermissions->bindValue(':_tenant', $this->tenant);
1038+
}
10371039

1038-
foreach ($removals as $type => $permissions) {
1039-
foreach ($permissions as $i => $permission) {
1040-
$stmtRemovePermissions->bindValue(":_remove_{$type}_{$i}", $permission);
1040+
foreach ($removals as $type => $permissions) {
1041+
foreach ($permissions as $i => $permission) {
1042+
$stmtRemovePermissions->bindValue(":_remove_{$type}_{$i}", $permission);
1043+
}
10411044
}
10421045
}
1043-
}
1044-
1045-
/**
1046-
* Query to add permissions
1047-
*/
1048-
if (!empty($additions)) {
1049-
$values = [];
1050-
foreach ($additions as $type => $permissions) {
1051-
foreach ($permissions as $i => $_) {
1052-
$value = "( :_uid, '{$type}', :_add_{$type}_{$i}";
10531046

1054-
if ($this->sharedTables) {
1055-
$value .= ", :_tenant)";
1056-
} else {
1057-
$value .= ")";
1047+
/**
1048+
* Query to add permissions
1049+
*/
1050+
if (!empty($additions)) {
1051+
$values = [];
1052+
foreach ($additions as $type => $permissions) {
1053+
foreach ($permissions as $i => $_) {
1054+
$value = "( :_uid, '{$type}', :_add_{$type}_{$i}";
1055+
1056+
if ($this->sharedTables) {
1057+
$value .= ", :_tenant)";
1058+
} else {
1059+
$value .= ")";
1060+
}
1061+
1062+
$values[] = $value;
10581063
}
1059-
1060-
$values[] = $value;
10611064
}
1062-
}
10631065

1064-
$sql = "
1066+
$sql = "
10651067
INSERT INTO {$this->getSQLTable($name . '_perms')} (_document, _type, _permission
10661068
";
10671069

1068-
if ($this->sharedTables) {
1069-
$sql .= ', _tenant)';
1070-
} else {
1071-
$sql .= ')';
1072-
}
1070+
if ($this->sharedTables) {
1071+
$sql .= ', _tenant)';
1072+
} else {
1073+
$sql .= ')';
1074+
}
10731075

1074-
$sql .= " VALUES " . \implode(', ', $values);
1076+
$sql .= " VALUES " . \implode(', ', $values);
10751077

1076-
$sql = $this->trigger(Database::EVENT_PERMISSIONS_CREATE, $sql);
1078+
$sql = $this->trigger(Database::EVENT_PERMISSIONS_CREATE, $sql);
10771079

1078-
$stmtAddPermissions = $this->getPDO()->prepare($sql);
1080+
$stmtAddPermissions = $this->getPDO()->prepare($sql);
10791081

1080-
$stmtAddPermissions->bindValue(":_uid", $document->getId());
1082+
$stmtAddPermissions->bindValue(":_uid", $document->getId());
10811083

1082-
if ($this->sharedTables) {
1083-
$stmtAddPermissions->bindValue(":_tenant", $this->tenant);
1084-
}
1084+
if ($this->sharedTables) {
1085+
$stmtAddPermissions->bindValue(":_tenant", $this->tenant);
1086+
}
10851087

1086-
foreach ($additions as $type => $permissions) {
1087-
foreach ($permissions as $i => $permission) {
1088-
$stmtAddPermissions->bindValue(":_add_{$type}_{$i}", $permission);
1088+
foreach ($additions as $type => $permissions) {
1089+
foreach ($permissions as $i => $permission) {
1090+
$stmtAddPermissions->bindValue(":_add_{$type}_{$i}", $permission);
1091+
}
10891092
}
10901093
}
10911094
}

src/Database/Adapter/Pool.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,7 @@ public function createDocuments(string $collection, array $documents): array
235235
return $this->delegate(__FUNCTION__, \func_get_args());
236236
}
237237

238-
public function updateDocument(string $collection, string $id, Document $document): Document
238+
public function updateDocument(string $collection, string $id, Document $document, bool $skipPermissions): Document
239239
{
240240
return $this->delegate(__FUNCTION__, \func_get_args());
241241
}

0 commit comments

Comments
 (0)