Skip to content

Commit 339447d

Browse files
committed
Apply changes to postgres
1 parent 2f0f6d8 commit 339447d

File tree

1 file changed

+34
-23
lines changed

1 file changed

+34
-23
lines changed

src/Database/Adapter/Postgres.php

Lines changed: 34 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -924,17 +924,13 @@ public function createDocument(string $collection, Document $document): Document
924924
$attributes['_permissions'] = \json_encode($document->getPermissions());
925925

926926
if ($this->sharedTables) {
927-
$attributes['_tenant'] = $this->tenant;
927+
$attributes['_tenant'] = $document->getTenant();
928928
}
929929

930930
$name = $this->filter($collection);
931931
$columns = '';
932932
$columnNames = '';
933933

934-
/**
935-
* Insert Attributes
936-
*/
937-
938934
// Insert internal id if set
939935
if (!empty($document->getInternalId())) {
940936
$bindKey = '_id';
@@ -969,12 +965,12 @@ public function createDocument(string $collection, Document $document): Document
969965

970966
$attributeIndex = 0;
971967
foreach ($attributes as $value) {
972-
if (is_array($value)) {
968+
if (\is_array($value)) {
973969
$value = \json_encode($value);
974970
}
975971

976972
$bindKey = 'key_' . $attributeIndex;
977-
$value = (is_bool($value)) ? ($value ? "true" : "false") : $value;
973+
$value = (\is_bool($value)) ? ($value ? "true" : "false") : $value;
978974
$stmt->bindValue(':' . $bindKey, $value, $this->getPDOType($value));
979975
$attributeIndex++;
980976
}
@@ -984,7 +980,7 @@ public function createDocument(string $collection, Document $document): Document
984980
foreach ($document->getPermissionsByType($type) as $permission) {
985981
$permission = \str_replace('"', '', $permission);
986982
$sqlTenant = $this->sharedTables ? ', :_tenant' : '';
987-
$permissions[] = "('{$type}', '{$permission}', '{$document->getId()}' {$sqlTenant})";
983+
$permissions[] = "('{$type}', '{$permission}', :_uid {$sqlTenant})";
988984
}
989985
}
990986

@@ -1000,8 +996,9 @@ public function createDocument(string $collection, Document $document): Document
1000996

1001997
$queryPermissions = $this->trigger(Database::EVENT_PERMISSIONS_CREATE, $queryPermissions);
1002998
$stmtPermissions = $this->getPDO()->prepare($queryPermissions);
999+
$stmtPermissions->bindValue(':_uid', $document->getId());
10031000
if ($sqlTenant) {
1004-
$stmtPermissions->bindValue(':_tenant', $this->tenant);
1001+
$stmtPermissions->bindValue(':_tenant', $document->getTenant());
10051002
}
10061003
}
10071004

@@ -1070,7 +1067,7 @@ public function createDocuments(string $collection, array $documents): array
10701067
$bindValues = [];
10711068
$permissions = [];
10721069

1073-
foreach ($documents as $document) {
1070+
foreach ($documents as $index => $document) {
10741071
$attributes = $document->getAttributes();
10751072
$attributes['_uid'] = $document->getId();
10761073
$attributes['_createdAt'] = $document->getCreatedAt();
@@ -1084,7 +1081,7 @@ public function createDocuments(string $collection, array $documents): array
10841081
}
10851082

10861083
if ($this->sharedTables) {
1087-
$attributes['_tenant'] = $this->tenant;
1084+
$attributes['_tenant'] = $document->getTenant();
10881085
}
10891086

10901087
$bindKeys = [];
@@ -1104,17 +1101,20 @@ public function createDocuments(string $collection, array $documents): array
11041101
$batchKeys[] = '(' . \implode(', ', $bindKeys) . ')';
11051102
foreach (Database::PERMISSIONS as $type) {
11061103
foreach ($document->getPermissionsByType($type) as $permission) {
1104+
$tenantBind = $this->sharedTables ? ", :_tenant_{$index}" : '';
11071105
$permission = \str_replace('"', '', $permission);
1108-
$permissions[] = "('{$type}', '{$permission}', '{$document->getId()}', :_tenant)";
1106+
$permission = "('{$type}', '{$permission}', :_uid_{$index} {$tenantBind})";
1107+
$permissions[] = $permission;
11091108
}
11101109
}
11111110
}
11121111

1113-
$stmt = $this->getPDO()->prepare(
1114-
"
1112+
$batchKeys = \implode(', ', $batchKeys);
1113+
1114+
$stmt = $this->getPDO()->prepare("
11151115
INSERT INTO {$this->getSQLTable($name)} {$columns}
1116-
VALUES " . \implode(', ', $batchKeys)
1117-
);
1116+
VALUES {$batchKeys}
1117+
");
11181118

11191119
foreach ($bindValues as $key => $value) {
11201120
$stmt->bindValue($key, $value, $this->getPDOType($value));
@@ -1123,12 +1123,23 @@ public function createDocuments(string $collection, array $documents): array
11231123
$stmt->execute();
11241124

11251125
if (!empty($permissions)) {
1126-
$stmtPermissions = $this->getPDO()->prepare(
1127-
"
1128-
INSERT INTO {$this->getSQLTable($name . '_perms')} (_type, _permission, _document, _tenant)
1129-
VALUES " . \implode(', ', $permissions)
1130-
);
1131-
$stmtPermissions->bindValue(':_tenant', $this->tenant);
1126+
$tenantColumn = $this->sharedTables ? ', _tenant' : '';
1127+
$permissions = \implode(', ', $permissions);
1128+
1129+
$sqlPermissions = "
1130+
INSERT INTO {$this->getSQLTable($name . '_perms')} (_type, _permission, _document {$tenantColumn})
1131+
VALUES {$permissions};
1132+
";
1133+
1134+
$stmtPermissions = $this->getPDO()->prepare($sqlPermissions);
1135+
1136+
foreach ($documents as $index => $document) {
1137+
$stmtPermissions->bindValue(":_uid_{$index}", $document->getId());
1138+
if ($this->sharedTables) {
1139+
$stmtPermissions->bindValue(":_tenant_{$index}", $document->getTenant());
1140+
}
1141+
}
1142+
11321143
$stmtPermissions?->execute();
11331144
}
11341145
} catch (PDOException $e) {
@@ -2000,7 +2011,7 @@ public function find(string $collection, array $queries = [], ?int $limit = 25,
20002011
unset($results[$index]['_id']);
20012012
}
20022013
if (\array_key_exists('_tenant', $document)) {
2003-
$results[$index]['$tenant'] = $document['_tenant'];
2014+
$results[$index]['$tenant'] = $document['_tenant'] === null ? null : (int)$document['_tenant'];
20042015
unset($results[$index]['_tenant']);
20052016
}
20062017
if (\array_key_exists('_createdAt', $document)) {

0 commit comments

Comments
 (0)