Skip to content

Commit e18746a

Browse files
committed
Fix Postgres.php
1 parent a681979 commit e18746a

2 files changed

Lines changed: 39 additions & 35 deletions

File tree

src/Database/Adapter/MariaDB.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1003,7 +1003,7 @@ public function createDocuments(string $collection, array $documents): array
10031003

10041004
$columns = [];
10051005
foreach ($attributeKeys as $key => $attribute) {
1006-
$columns[$key] = "`{$this->filter($attribute)}`";
1006+
$columns[$key] = "{$this->quote($this->filter($attribute))}";
10071007
}
10081008
$columns = '(' . \implode(', ', $columns) . ')';
10091009

src/Database/Adapter/Postgres.php

Lines changed: 38 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1033,6 +1033,7 @@ public function createDocument(string $collection, Document $document): Document
10331033
* @return array<Document>
10341034
*
10351035
* @throws DuplicateException
1036+
* @throws \Throwable
10361037
*/
10371038
public function createDocuments(string $collection, array $documents): array
10381039
{
@@ -1042,12 +1043,13 @@ public function createDocuments(string $collection, array $documents): array
10421043

10431044
try {
10441045
$name = $this->filter($collection);
1046+
10451047
$attributeKeys = Database::INTERNAL_ATTRIBUTE_KEYS;
10461048

10471049
$hasInternalId = null;
10481050
foreach ($documents as $document) {
10491051
$attributes = $document->getAttributes();
1050-
$attributeKeys = array_merge($attributeKeys, array_keys($attributes));
1052+
$attributeKeys = [...$attributeKeys, ...\array_keys($attributes)];
10511053

10521054
if ($hasInternalId === null) {
10531055
$hasInternalId = !empty($document->getInternalId());
@@ -1063,16 +1065,16 @@ public function createDocuments(string $collection, array $documents): array
10631065

10641066
$columns = [];
10651067
foreach ($attributeKeys as $key => $attribute) {
1066-
$columns[$key] = "\"{$this->filter($attribute)}\"";
1068+
$columns[$key] = "{$this->quote($this->filter($attribute))}";
10671069
}
10681070
$columns = '(' . \implode(', ', $columns) . ')';
10691071

1070-
$internalIds = [];
1071-
10721072
$bindIndex = 0;
10731073
$batchKeys = [];
10741074
$bindValues = [];
10751075
$permissions = [];
1076+
$documentIds = [];
1077+
$documentTenants = [];
10761078

10771079
foreach ($documents as $index => $document) {
10781080
$attributes = $document->getAttributes();
@@ -1082,13 +1084,15 @@ public function createDocuments(string $collection, array $documents): array
10821084
$attributes['_permissions'] = \json_encode($document->getPermissions());
10831085

10841086
if (!empty($document->getInternalId())) {
1085-
$internalIds[$document->getId()] = true;
10861087
$attributes['_id'] = $document->getInternalId();
10871088
$attributeKeys[] = '_id';
1089+
} else {
1090+
$documentIds[] = $document->getId();
10881091
}
10891092

10901093
if ($this->sharedTables) {
10911094
$attributes['_tenant'] = $document->getTenant();
1095+
$documentTenants[] = $document->getTenant();
10921096
}
10931097

10941098
$bindKeys = [];
@@ -1149,18 +1153,20 @@ public function createDocuments(string $collection, array $documents): array
11491153

11501154
$stmtPermissions?->execute();
11511155
}
1152-
} catch (PDOException $e) {
1153-
throw $this->processException($e);
1154-
}
11551156

1156-
foreach ($documents as $document) {
1157-
if (!isset($internalIds[$document->getId()])) {
1158-
$document['$internalId'] = $this->getDocument(
1159-
$collection,
1160-
$document->getId(),
1161-
[Query::select(['$internalId'])]
1162-
)->getInternalId();
1157+
$internalIds = $this->getInternalIds(
1158+
$collection,
1159+
$documentIds,
1160+
$documentTenants
1161+
);
1162+
1163+
foreach ($documents as $document) {
1164+
if (isset($internalIds[$document->getId()])) {
1165+
$document['$internalId'] = $internalIds[$document->getId()];
1166+
}
11631167
}
1168+
} catch (PDOException $e) {
1169+
throw $this->processException($e);
11641170
}
11651171

11661172
return $documents;
@@ -1509,9 +1515,9 @@ public function deleteDocument(string $collection, string $id): bool
15091515
* @param array<string, mixed> $cursor
15101516
* @param string $cursorDirection
15111517
* @param string $forPermission
1512-
* @param array $selects
1513-
* @param array $filters
1514-
* @param array $joins
1518+
* @param array<Query> $selects
1519+
* @param array<Query> $filters
1520+
* @param array<Query> $joins
15151521
* @param array<Query> $orderQueries
15161522
* @return array<Document>
15171523
* @throws DatabaseException
@@ -1536,9 +1542,9 @@ public function find(
15361542
$alias = Query::DEFAULT_ALIAS;
15371543
$binds = [];
15381544

1539-
$collection = $context->getCollections()[0]->getId();
1545+
$name = $context->getCollections()[0]->getId();
1546+
$name = $this->filter($name);
15401547

1541-
$mainCollection = $this->filter($collection);
15421548
$roles = Authorization::getRoles();
15431549
$where = [];
15441550
$orders = [];
@@ -1621,21 +1627,19 @@ public function find(
16211627

16221628
$sqlJoin = '';
16231629
foreach ($joins as $join) {
1624-
/**
1625-
* @var $join Query
1626-
*/
16271630
$permissions = '';
1628-
$joinCollectionName = $this->filter($join->getCollection());
1631+
$collection = $join->getCollection();
1632+
$collection = $this->filter($collection);
16291633

1630-
$skipAuth = $context->skipAuth($join->getCollection(), $forPermission);
1634+
$skipAuth = $context->skipAuth($collection, $forPermission);
16311635
if (! $skipAuth) {
1632-
$permissions = 'AND '.$this->getSQLPermissionsCondition($joinCollectionName, $roles, $join->getAlias(), $forPermission);
1636+
$permissions = 'AND '.$this->getSQLPermissionsCondition($collection, $roles, $join->getAlias(), $forPermission);
16331637
}
16341638

1635-
$sqlJoin .= "INNER JOIN {$this->getSQLTable($joinCollectionName)} AS {$this->quote($join->getAlias())}
1639+
$sqlJoin .= "INNER JOIN {$this->getSQLTable($collection)} AS {$this->quote($join->getAlias())}
16361640
ON {$this->getSQLConditions($join->getValues(), $binds)}
16371641
{$permissions}
1638-
{$this->getTenantQuery($joinCollectionName, $join->getAlias())}
1642+
{$this->getTenantQuery($collection, $join->getAlias())}
16391643
";
16401644
}
16411645

@@ -1644,14 +1648,14 @@ public function find(
16441648
$where[] = $conditions;
16451649
}
16461650

1647-
$skipAuth = $context->skipAuth($collection, $forPermission);
1651+
$skipAuth = $context->skipAuth($name, $forPermission);
16481652
if (! $skipAuth) {
1649-
$where[] = $this->getSQLPermissionsCondition($mainCollection, $roles, $alias, $forPermission);
1653+
$where[] = $this->getSQLPermissionsCondition($name, $roles, $alias, $forPermission);
16501654
}
16511655

16521656
if ($this->sharedTables) {
16531657
$binds[':_tenant'] = $this->tenant;
1654-
$where[] = "{$this->getTenantQuery($collection, $alias, condition: '')}";
1658+
$where[] = "{$this->getTenantQuery($name, $alias, condition: '')}";
16551659
}
16561660

16571661
$sqlWhere = !empty($where) ? 'WHERE ' . implode(' AND ', $where) : '';
@@ -1668,11 +1672,11 @@ public function find(
16681672
$sqlLimit .= ' OFFSET :offset';
16691673
}
16701674

1671-
$selections = $this->getAttributeSelections($selects);
1675+
//$selections = $this->getAttributeSelections($selects);
16721676

16731677
$sql = "
1674-
SELECT {$this->getAttributeProjection($selections, $alias)}
1675-
FROM {$this->getSQLTable($mainCollection)} AS {$this->quote($alias)}
1678+
SELECT {$this->getAttributeProjectionV2($selects)}
1679+
FROM {$this->getSQLTable($name)} AS {$this->quote($alias)}
16761680
{$sqlJoin}
16771681
{$sqlWhere}
16781682
{$sqlOrder}

0 commit comments

Comments
 (0)