Skip to content

Commit 2cb0fa6

Browse files
committed
SQLite.php
1 parent dd5c81c commit 2cb0fa6

1 file changed

Lines changed: 19 additions & 105 deletions

File tree

src/Database/Adapter/SQLite.php

Lines changed: 19 additions & 105 deletions
Original file line numberDiff line numberDiff line change
@@ -1275,6 +1275,7 @@ public function updateDocument(Document $collection, string $id, Document $docum
12751275
$attributes['_createdAt'] = $document->getCreatedAt();
12761276
$attributes['_updatedAt'] = $document->getUpdatedAt();
12771277
$attributes['_permissions'] = json_encode($document->getPermissions());
1278+
$attributes['_uid'] = $document->getId();
12781279

12791280
if ($this->sharedTables) {
12801281
$attributes['_tenant'] = $this->tenant;
@@ -1284,116 +1285,33 @@ public function updateDocument(Document $collection, string $id, Document $docum
12841285
$columns = '';
12851286

12861287
if (!$skipPermissions) {
1288+
$newUid = $document->offsetExists('$id') ? $document->getId() : $id;
1289+
12871290
$sql = "
1288-
SELECT _type, _permission
1289-
FROM `{$this->getNamespace()}_{$name}_perms`
1291+
DELETE FROM `{$this->getNamespace()}_{$name}_perms`
12901292
WHERE _document = :_uid
12911293
{$this->getTenantQuery($collection)}
12921294
";
12931295

1294-
$sql = $this->trigger(Database::EVENT_PERMISSIONS_READ, $sql);
1295-
1296-
/**
1297-
* Get current permissions from the database
1298-
*/
1299-
$permissionsStmt = $this->getPDO()->prepare($sql);
1300-
$permissionsStmt->bindValue(':_uid', $document->getId());
1296+
$sql = $this->trigger(Database::EVENT_PERMISSIONS_DELETE, $sql);
13011297

1298+
$stmtRemovePermissions = $this->getPDO()->prepare($sql);
1299+
$stmtRemovePermissions->bindValue(':_uid', $id);
13021300
if ($this->sharedTables) {
1303-
$permissionsStmt->bindValue(':_tenant', $this->tenant);
1304-
}
1305-
1306-
$permissionsStmt->execute();
1307-
$permissions = $permissionsStmt->fetchAll();
1308-
$permissionsStmt->closeCursor();
1309-
1310-
$initial = [];
1311-
foreach (Database::PERMISSIONS as $type) {
1312-
$initial[$type] = [];
1313-
}
1314-
1315-
$permissions = array_reduce($permissions, function (array $carry, array $item) {
1316-
$carry[$item['_type']][] = $item['_permission'];
1317-
1318-
return $carry;
1319-
}, $initial);
1320-
1321-
/**
1322-
* Get removed Permissions
1323-
*/
1324-
$removals = [];
1325-
foreach (Database::PERMISSIONS as $type) {
1326-
$diff = \array_diff($permissions[$type], $document->getPermissionsByType($type));
1327-
if (!empty($diff)) {
1328-
$removals[$type] = $diff;
1329-
}
1301+
$stmtRemovePermissions->bindValue(':_tenant', $this->tenant);
13301302
}
13311303

1332-
/**
1333-
* Get added Permissions
1334-
*/
1335-
$additions = [];
1304+
$values = [];
1305+
$binds = [];
13361306
foreach (Database::PERMISSIONS as $type) {
1337-
$diff = \array_diff($document->getPermissionsByType($type), $permissions[$type]);
1338-
if (!empty($diff)) {
1339-
$additions[$type] = $diff;
1340-
}
1341-
}
1342-
1343-
/**
1344-
* Query to remove permissions
1345-
*/
1346-
$removeQuery = '';
1347-
if (!empty($removals)) {
1348-
$removeQuery = ' AND (';
1349-
foreach ($removals as $type => $permissions) {
1350-
$removeQuery .= "(
1351-
_type = '{$type}'
1352-
AND _permission IN (" . implode(', ', \array_map(fn (string $i) => ":_remove_{$type}_{$i}", \array_keys($permissions))) . ")
1353-
)";
1354-
if ($type !== \array_key_last($removals)) {
1355-
$removeQuery .= ' OR ';
1356-
}
1357-
}
1358-
}
1359-
if (!empty($removeQuery)) {
1360-
$removeQuery .= ')';
1361-
$sql = "
1362-
DELETE
1363-
FROM `{$this->getNamespace()}_{$name}_perms`
1364-
WHERE _document = :_uid
1365-
{$this->getTenantQuery($collection)}
1366-
";
1367-
1368-
$removeQuery = $sql . $removeQuery;
1369-
$removeQuery = $this->trigger(Database::EVENT_PERMISSIONS_DELETE, $removeQuery);
1370-
1371-
$stmtRemovePermissions = $this->getPDO()->prepare($removeQuery);
1372-
$stmtRemovePermissions->bindValue(':_uid', $document->getId());
1373-
1374-
if ($this->sharedTables) {
1375-
$stmtRemovePermissions->bindValue(':_tenant', $this->tenant);
1376-
}
1377-
1378-
foreach ($removals as $type => $permissions) {
1379-
foreach ($permissions as $i => $permission) {
1380-
$stmtRemovePermissions->bindValue(":_remove_{$type}_{$i}", $permission);
1381-
}
1307+
foreach ($document->getPermissionsByType($type) as $i => $permission) {
1308+
$tenantQuery = $this->sharedTables ? ', :_tenant' : '';
1309+
$values[] = "(:_uid, '{$type}', :_add_{$type}_{$i} {$tenantQuery})";
1310+
$binds[":_add_{$type}_{$i}"] = $permission;
13821311
}
13831312
}
13841313

1385-
/**
1386-
* Query to add permissions
1387-
*/
1388-
if (!empty($additions)) {
1389-
$values = [];
1390-
foreach ($additions as $type => $permissions) {
1391-
foreach ($permissions as $i => $_) {
1392-
$tenantQuery = $this->sharedTables ? ', :_tenant' : '';
1393-
$values[] = "(:_uid, '{$type}', :_add_{$type}_{$i} {$tenantQuery})";
1394-
}
1395-
}
1396-
1314+
if (!empty($values)) {
13971315
$tenantQuery = $this->sharedTables ? ', _tenant' : '';
13981316

13991317
$sql = "
@@ -1403,16 +1321,13 @@ public function updateDocument(Document $collection, string $id, Document $docum
14031321
$sql = $this->trigger(Database::EVENT_PERMISSIONS_CREATE, $sql);
14041322

14051323
$stmtAddPermissions = $this->getPDO()->prepare($sql);
1406-
1407-
$stmtAddPermissions->bindValue(":_uid", $document->getId());
1324+
$stmtAddPermissions->bindValue(":_uid", $newUid);
14081325
if ($this->sharedTables) {
14091326
$stmtAddPermissions->bindValue(":_tenant", $this->tenant);
14101327
}
14111328

1412-
foreach ($additions as $type => $permissions) {
1413-
foreach ($permissions as $i => $permission) {
1414-
$stmtAddPermissions->bindValue(":_add_{$type}_{$i}", $permission);
1415-
}
1329+
foreach ($binds as $key => $permission) {
1330+
$stmtAddPermissions->bindValue($key, $permission);
14161331
}
14171332
}
14181333
}
@@ -1456,7 +1371,7 @@ public function updateDocument(Document $collection, string $id, Document $docum
14561371

14571372
$sql = "
14581373
UPDATE `{$this->getNamespace()}_{$name}`
1459-
SET {$columns}, _uid = :_newUid
1374+
SET {$columns}
14601375
WHERE _uid = :_existingUid
14611376
{$this->getTenantQuery($collection)}
14621377
";
@@ -1466,7 +1381,6 @@ public function updateDocument(Document $collection, string $id, Document $docum
14661381
$stmt = $this->getPDO()->prepare($sql);
14671382

14681383
$stmt->bindValue(':_existingUid', $id);
1469-
$stmt->bindValue(':_newUid', $document->getId());
14701384

14711385
if ($this->sharedTables) {
14721386
$stmt->bindValue(':_tenant', $this->tenant);

0 commit comments

Comments
 (0)