Skip to content

Commit 0f3617e

Browse files
committed
Change internal ID to sequence
1 parent e97cc6b commit 0f3617e

File tree

17 files changed

+138
-138
lines changed

17 files changed

+138
-138
lines changed

src/Database/Adapter.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -747,12 +747,12 @@ abstract public function deleteDocument(string $collection, string $id): bool;
747747
* Delete Documents
748748
*
749749
* @param string $collection
750-
* @param array<string> $internalIds
750+
* @param array<string> $sequences
751751
* @param array<string> $permissionIds
752752
*
753753
* @return int
754754
*/
755-
abstract public function deleteDocuments(string $collection, array $internalIds, array $permissionIds): int;
755+
abstract public function deleteDocuments(string $collection, array $sequences, array $permissionIds): int;
756756

757757
/**
758758
* Find Documents

src/Database/Adapter/MariaDB.php

Lines changed: 34 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -564,7 +564,7 @@ public function updateRelationship(
564564
$collection = $this->getDocument(Database::METADATA, $collection);
565565
$relatedCollection = $this->getDocument(Database::METADATA, $relatedCollection);
566566

567-
$junction = $this->getSQLTable('_' . $collection->getInternalId() . '_' . $relatedCollection->getInternalId());
567+
$junction = $this->getSQLTable('_' . $collection->getSequence() . '_' . $relatedCollection->getSequence());
568568

569569
if (!\is_null($newKey)) {
570570
$sql = "ALTER TABLE {$junction} RENAME COLUMN `{$key}` TO `{$newKey}`;";
@@ -648,12 +648,12 @@ public function deleteRelationship(
648648
$relatedCollection = $this->getDocument(Database::METADATA, $relatedCollection);
649649

650650
$junction = $side === Database::RELATION_SIDE_PARENT
651-
? $this->getSQLTable('_' . $collection->getInternalId() . '_' . $relatedCollection->getInternalId())
652-
: $this->getSQLTable('_' . $relatedCollection->getInternalId() . '_' . $collection->getInternalId());
651+
? $this->getSQLTable('_' . $collection->getSequence() . '_' . $relatedCollection->getSequence())
652+
: $this->getSQLTable('_' . $relatedCollection->getSequence() . '_' . $collection->getSequence());
653653

654654
$perms = $side === Database::RELATION_SIDE_PARENT
655-
? $this->getSQLTable('_' . $collection->getInternalId() . '_' . $relatedCollection->getInternalId() . '_perms')
656-
: $this->getSQLTable('_' . $relatedCollection->getInternalId() . '_' . $collection->getInternalId() . '_perms');
655+
? $this->getSQLTable('_' . $collection->getSequence() . '_' . $relatedCollection->getSequence() . '_perms')
656+
: $this->getSQLTable('_' . $relatedCollection->getSequence() . '_' . $collection->getSequence() . '_perms');
657657

658658
$sql = "DROP TABLE {$junction}; DROP TABLE {$perms}";
659659
break;
@@ -718,7 +718,7 @@ public function createIndex(string $collection, string $id, string $type, array
718718
}
719719

720720
/**
721-
* We do not have internalId's added to list, since we check only for array field
721+
* We do not have sequence's added to list, since we check only for array field
722722
*/
723723
$collectionAttributes = \json_decode($collection->getAttribute('attributes', []), true);
724724

@@ -843,7 +843,7 @@ public function createDocument(string $collection, Document $document): Document
843843
}
844844

845845
// Insert internal ID if set
846-
if (!empty($document->getInternalId())) {
846+
if (!empty($document->getSequence())) {
847847
$bindKey = '_id';
848848
$columns .= "_id, ";
849849
$columnNames .= ':' . $bindKey . ', ';
@@ -860,8 +860,8 @@ public function createDocument(string $collection, Document $document): Document
860860

861861
$stmt->bindValue(':_uid', $document->getId());
862862

863-
if (!empty($document->getInternalId())) {
864-
$stmt->bindValue(':_id', $document->getInternalId());
863+
if (!empty($document->getSequence())) {
864+
$stmt->bindValue(':_id', $document->getSequence());
865865
}
866866

867867
$attributeIndex = 0;
@@ -905,10 +905,10 @@ public function createDocument(string $collection, Document $document): Document
905905

906906
$stmt->execute();
907907

908-
$document['$internalId'] = $this->pdo->lastInsertId();
908+
$document['$sequence'] = $this->pdo->lastInsertId();
909909

910-
if (empty($document['$internalId'])) {
911-
throw new DatabaseException('Error creating document empty "$internalId"');
910+
if (empty($document['$sequence'])) {
911+
throw new DatabaseException('Error creating document empty "$sequence"');
912912
}
913913

914914
if (isset($stmtPermissions)) {
@@ -943,15 +943,15 @@ public function createDocuments(string $collection, array $documents): array
943943

944944
$attributeKeys = Database::INTERNAL_ATTRIBUTE_KEYS;
945945

946-
$hasInternalId = null;
946+
$hasSequence = null;
947947
foreach ($documents as $document) {
948948
$attributes = $document->getAttributes();
949949
$attributeKeys = [...$attributeKeys, ...\array_keys($attributes)];
950950

951-
if ($hasInternalId === null) {
952-
$hasInternalId = !empty($document->getInternalId());
953-
} elseif ($hasInternalId == empty($document->getInternalId())) {
954-
throw new DatabaseException('All documents must have an internalId if one is set');
951+
if ($hasSequence === null) {
952+
$hasSequence = !empty($document->getSequence());
953+
} elseif ($hasSequence == empty($document->getSequence())) {
954+
throw new DatabaseException('All documents must have an sequence if one is set');
955955
}
956956
}
957957
$attributeKeys = array_unique($attributeKeys);
@@ -980,8 +980,8 @@ public function createDocuments(string $collection, array $documents): array
980980
$attributes['_updatedAt'] = $document->getUpdatedAt();
981981
$attributes['_permissions'] = \json_encode($document->getPermissions());
982982

983-
if (!empty($document->getInternalId())) {
984-
$attributes['_id'] = $document->getInternalId();
983+
if (!empty($document->getSequence())) {
984+
$attributes['_id'] = $document->getSequence();
985985
$attributeKeys[] = '_id';
986986
} else {
987987
$documentIds[] = $document->getId();
@@ -1051,15 +1051,15 @@ public function createDocuments(string $collection, array $documents): array
10511051
$stmtPermissions?->execute();
10521052
}
10531053

1054-
$internalIds = $this->getInternalIds(
1054+
$sequences = $this->getSequences(
10551055
$collection,
10561056
$documentIds,
10571057
$documentTenants
10581058
);
10591059

10601060
foreach ($documents as $document) {
1061-
if (isset($internalIds[$document->getId()])) {
1062-
$document['$internalId'] = $internalIds[$document->getId()];
1061+
if (isset($sequences[$document->getId()])) {
1062+
$document['$sequence'] = $sequences[$document->getId()];
10631063
}
10641064
}
10651065
} catch (PDOException $e) {
@@ -1253,15 +1253,15 @@ public function updateDocument(string $collection, string $id, Document $documen
12531253
$sql = "
12541254
UPDATE {$this->getSQLTable($name)}
12551255
SET {$columns} _uid = :_newUid
1256-
WHERE _id=:_internalId
1256+
WHERE _id=:_sequence
12571257
{$this->getTenantQuery($collection)}
12581258
";
12591259

12601260
$sql = $this->trigger(Database::EVENT_DOCUMENT_UPDATE, $sql);
12611261

12621262
$stmt = $this->getPDO()->prepare($sql);
12631263

1264-
$stmt->bindValue(':_internalId', $document->getInternalId());
1264+
$stmt->bindValue(':_sequence', $document->getSequence());
12651265
$stmt->bindValue(':_newUid', $document->getId());
12661266

12671267
if ($this->sharedTables) {
@@ -1331,8 +1331,8 @@ public function createOrUpdateDocuments(
13311331
$attributes['_updatedAt'] = $document->getUpdatedAt();
13321332
$attributes['_permissions'] = \json_encode($document->getPermissions());
13331333

1334-
if (!empty($document->getInternalId())) {
1335-
$attributes['_id'] = $document->getInternalId();
1334+
if (!empty($document->getSequence())) {
1335+
$attributes['_id'] = $document->getSequence();
13361336
} else {
13371337
$documentIds[] = $document->getId();
13381338
}
@@ -1495,15 +1495,15 @@ public function createOrUpdateDocuments(
14951495
$stmtAddPermissions->execute();
14961496
}
14971497

1498-
$internalIds = $this->getInternalIds(
1498+
$sequences = $this->getSequences(
14991499
$collection,
15001500
$documentIds,
15011501
$documentTenants
15021502
);
15031503

15041504
foreach ($changes as $change) {
1505-
if (isset($internalIds[$change->getNew()->getId()])) {
1506-
$change->getNew()->setAttribute('$internalId', $internalIds[$change->getNew()->getId()]);
1505+
if (isset($sequences[$change->getNew()->getId()])) {
1506+
$change->getNew()->setAttribute('$sequence', $sequences[$change->getNew()->getId()]);
15071507
}
15081508
}
15091509
} catch (PDOException $e) {
@@ -1663,12 +1663,12 @@ public function find(string $collection, array $queries = [], ?int $limit = 25,
16631663

16641664
// Get most dominant/first order attribute
16651665
if ($i === 0 && !empty($cursor)) {
1666-
$orderMethodInternalId = Query::TYPE_GREATER; // To preserve natural order
1666+
$orderMethodSequence = Query::TYPE_GREATER; // To preserve natural order
16671667
$orderMethod = $orderType === Database::ORDER_DESC ? Query::TYPE_LESSER : Query::TYPE_GREATER;
16681668

16691669
if ($cursorDirection === Database::CURSOR_BEFORE) {
16701670
$orderType = $orderType === Database::ORDER_ASC ? Database::ORDER_DESC : Database::ORDER_ASC;
1671-
$orderMethodInternalId = $orderType === Database::ORDER_ASC ? Query::TYPE_LESSER : Query::TYPE_GREATER;
1671+
$orderMethodSequence = $orderType === Database::ORDER_ASC ? Query::TYPE_LESSER : Query::TYPE_GREATER;
16721672
$orderMethod = $orderType === Database::ORDER_DESC ? Query::TYPE_LESSER : Query::TYPE_GREATER;
16731673
}
16741674

@@ -1686,7 +1686,7 @@ public function find(string $collection, array $queries = [], ?int $limit = 25,
16861686
OR (
16871687
{$this->quote($alias)}.{$this->quote($attribute)} = :cursor
16881688
AND
1689-
{$this->quote($alias)}._id {$this->getSQLOperator($orderMethodInternalId)} {$cursor['$internalId']}
1689+
{$this->quote($alias)}._id {$this->getSQLOperator($orderMethodSequence)} {$cursor['$sequence']}
16901690
)
16911691
)";
16921692
} elseif ($cursorDirection === Database::CURSOR_BEFORE) {
@@ -1710,7 +1710,7 @@ public function find(string $collection, array $queries = [], ?int $limit = 25,
17101710
: Query::TYPE_LESSER;
17111711
}
17121712

1713-
$where[] = "({$this->quote($alias)}._id {$this->getSQLOperator($orderMethod)} {$cursor['$internalId']})";
1713+
$where[] = "({$this->quote($alias)}._id {$this->getSQLOperator($orderMethod)} {$cursor['$sequence']})";
17141714
}
17151715

17161716
// Allow order type without any order attribute, fallback to the natural order (_id)
@@ -1788,7 +1788,7 @@ public function find(string $collection, array $queries = [], ?int $limit = 25,
17881788
unset($results[$index]['_uid']);
17891789
}
17901790
if (\array_key_exists('_id', $document)) {
1791-
$results[$index]['$internalId'] = $document['_id'];
1791+
$results[$index]['$sequence'] = $document['_id'];
17921792
unset($results[$index]['_id']);
17931793
}
17941794
if (\array_key_exists('_tenant', $document)) {

src/Database/Adapter/Pool.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,7 @@ public function deleteDocument(string $collection, string $id): bool
255255
return $this->delegate(__FUNCTION__, \func_get_args());
256256
}
257257

258-
public function deleteDocuments(string $collection, array $internalIds, array $permissionIds): int
258+
public function deleteDocuments(string $collection, array $sequences, array $permissionIds): int
259259
{
260260
return $this->delegate(__FUNCTION__, \func_get_args());
261261
}

0 commit comments

Comments
 (0)