Skip to content

Commit 95f7952

Browse files
committed
Merge branch 'main' of github.com:utopia-php/database into fix-cursor
2 parents 85b7ff6 + a1be186 commit 95f7952

13 files changed

Lines changed: 499 additions & 413 deletions

File tree

src/Database/Adapter.php

Lines changed: 23 additions & 8 deletions
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
@@ -733,6 +734,13 @@ abstract public function createOrUpdateDocuments(
733734
array $changes
734735
): array;
735736

737+
/**
738+
* @param string $collection
739+
* @param array<Document> $documents
740+
* @return array<Document>
741+
*/
742+
abstract public function getSequences(string $collection, array $documents): array;
743+
736744
/**
737745
* Delete Document
738746
*
@@ -891,6 +899,20 @@ abstract public function getSupportForSchemaAttributes(): bool;
891899
*/
892900
abstract public function getSupportForIndex(): bool;
893901

902+
/**
903+
* Is indexing array supported?
904+
*
905+
* @return bool
906+
*/
907+
abstract public function getSupportForIndexArray(): bool;
908+
909+
/**
910+
* Is cast index as array supported?
911+
*
912+
* @return bool
913+
*/
914+
abstract public function getSupportForCastIndexArray(): bool;
915+
894916
/**
895917
* Is unique index supported?
896918
*
@@ -964,13 +986,6 @@ abstract public function getSupportForAttributeResizing(): bool;
964986
*/
965987
abstract public function getSupportForGetConnectionId(): bool;
966988

967-
/**
968-
* Is cast index as array supported?
969-
*
970-
* @return bool
971-
*/
972-
abstract public function getSupportForCastIndexArray(): bool;
973-
974989
/**
975990
* Is upserting supported?
976991
*

src/Database/Adapter/MariaDB.php

Lines changed: 110 additions & 117 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
}
@@ -1171,8 +1174,6 @@ public function createOrUpdateDocuments(
11711174
$bindIndex = 0;
11721175
$batchKeys = [];
11731176
$bindValues = [];
1174-
$documentIds = [];
1175-
$documentTenants = [];
11761177

11771178
foreach ($changes as $change) {
11781179
$document = $change->getNew();
@@ -1184,14 +1185,10 @@ public function createOrUpdateDocuments(
11841185

11851186
if (!empty($document->getSequence())) {
11861187
$attributes['_id'] = $document->getSequence();
1187-
} else {
1188-
$documentIds[] = $document->getId();
11891188
}
11901189

11911190
if ($this->sharedTables) {
1192-
$attributes['_tenant']
1193-
= $documentTenants[]
1194-
= $document->getTenant();
1191+
$attributes['_tenant'] = $document->getTenant();
11951192
}
11961193

11971194
\ksort($attributes);
@@ -1349,18 +1346,6 @@ public function createOrUpdateDocuments(
13491346
}
13501347
$stmtAddPermissions->execute();
13511348
}
1352-
1353-
$sequences = $this->getSequences(
1354-
$collection,
1355-
$documentIds,
1356-
$documentTenants
1357-
);
1358-
1359-
foreach ($changes as $change) {
1360-
if (isset($sequences[$change->getNew()->getId()])) {
1361-
$change->getNew()->setAttribute('$sequence', $sequences[$change->getNew()->getId()]);
1362-
}
1363-
}
13641349
} catch (PDOException $e) {
13651350
throw $this->processException($e);
13661351
}
@@ -1628,7 +1613,7 @@ public function find(string $collection, array $queries = [], ?int $limit = 25,
16281613
unset($results[$index]['_id']);
16291614
}
16301615
if (\array_key_exists('_tenant', $document)) {
1631-
$results[$index]['$tenant'] = $document['_tenant'] === null ? null : (int)$document['_tenant'];
1616+
$results[$index]['$tenant'] = $document['_tenant'];
16321617
unset($results[$index]['_tenant']);
16331618
}
16341619
if (\array_key_exists('_createdAt', $document)) {
@@ -2100,4 +2085,12 @@ public function getSupportForNumericCasting(): bool
21002085
{
21012086
return true;
21022087
}
2088+
2089+
public function getSupportForIndexArray(): bool
2090+
{
2091+
/**
2092+
* Disabled to be compatible with Mysql adapter
2093+
*/
2094+
return false;
2095+
}
21032096
}

0 commit comments

Comments
 (0)