diff --git a/src/Database/Adapter/MariaDB.php b/src/Database/Adapter/MariaDB.php index ce9a04503..9dec1385f 100644 --- a/src/Database/Adapter/MariaDB.php +++ b/src/Database/Adapter/MariaDB.php @@ -564,7 +564,7 @@ public function updateRelationship( $collection = $this->getDocument(Database::METADATA, $collection); $relatedCollection = $this->getDocument(Database::METADATA, $relatedCollection); - $junction = $this->getSQLTable('_' . $collection->getInternalId() . '_' . $relatedCollection->getInternalId()); + $junction = $this->getSQLTable('_' . $collection->getSequence() . '_' . $relatedCollection->getSequence()); if (!\is_null($newKey)) { $sql = "ALTER TABLE {$junction} RENAME COLUMN `{$key}` TO `{$newKey}`;"; @@ -648,12 +648,12 @@ public function deleteRelationship( $relatedCollection = $this->getDocument(Database::METADATA, $relatedCollection); $junction = $side === Database::RELATION_SIDE_PARENT - ? $this->getSQLTable('_' . $collection->getInternalId() . '_' . $relatedCollection->getInternalId()) - : $this->getSQLTable('_' . $relatedCollection->getInternalId() . '_' . $collection->getInternalId()); + ? $this->getSQLTable('_' . $collection->getSequence() . '_' . $relatedCollection->getSequence()) + : $this->getSQLTable('_' . $relatedCollection->getSequence() . '_' . $collection->getSequence()); $perms = $side === Database::RELATION_SIDE_PARENT - ? $this->getSQLTable('_' . $collection->getInternalId() . '_' . $relatedCollection->getInternalId() . '_perms') - : $this->getSQLTable('_' . $relatedCollection->getInternalId() . '_' . $collection->getInternalId() . '_perms'); + ? $this->getSQLTable('_' . $collection->getSequence() . '_' . $relatedCollection->getSequence() . '_perms') + : $this->getSQLTable('_' . $relatedCollection->getSequence() . '_' . $collection->getSequence() . '_perms'); $sql = "DROP TABLE {$junction}; DROP TABLE {$perms}"; break; @@ -718,7 +718,7 @@ public function createIndex(string $collection, string $id, string $type, array } /** - * We do not have internalId's added to list, since we check only for array field + * We do not have sequence's added to list, since we check only for array field */ $collectionAttributes = \json_decode($collection->getAttribute('attributes', []), true); @@ -843,7 +843,7 @@ public function createDocument(string $collection, Document $document): Document } // Insert internal ID if set - if (!empty($document->getInternalId())) { + if (!empty($document->getSequence())) { $bindKey = '_id'; $columns .= "_id, "; $columnNames .= ':' . $bindKey . ', '; @@ -860,8 +860,8 @@ public function createDocument(string $collection, Document $document): Document $stmt->bindValue(':_uid', $document->getId()); - if (!empty($document->getInternalId())) { - $stmt->bindValue(':_id', $document->getInternalId()); + if (!empty($document->getSequence())) { + $stmt->bindValue(':_id', $document->getSequence()); } $attributeIndex = 0; @@ -943,15 +943,15 @@ public function createDocuments(string $collection, array $documents): array $attributeKeys = Database::INTERNAL_ATTRIBUTE_KEYS; - $hasInternalId = null; + $hasSequence = null; foreach ($documents as $document) { $attributes = $document->getAttributes(); $attributeKeys = [...$attributeKeys, ...\array_keys($attributes)]; - if ($hasInternalId === null) { - $hasInternalId = !empty($document->getInternalId()); - } elseif ($hasInternalId == empty($document->getInternalId())) { - throw new DatabaseException('All documents must have an internalId if one is set'); + if ($hasSequence === null) { + $hasSequence = !empty($document->getSequence()); + } elseif ($hasSequence == empty($document->getSequence())) { + throw new DatabaseException('All documents must have an sequence if one is set'); } } $attributeKeys = array_unique($attributeKeys); @@ -980,8 +980,8 @@ public function createDocuments(string $collection, array $documents): array $attributes['_updatedAt'] = $document->getUpdatedAt(); $attributes['_permissions'] = \json_encode($document->getPermissions()); - if (!empty($document->getInternalId())) { - $attributes['_id'] = $document->getInternalId(); + if (!empty($document->getSequence())) { + $attributes['_id'] = $document->getSequence(); $attributeKeys[] = '_id'; } else { $documentIds[] = $document->getId(); @@ -1051,7 +1051,7 @@ public function createDocuments(string $collection, array $documents): array $stmtPermissions?->execute(); } - $sequences = $this->getInternalIds( + $sequences = $this->getSequences( $collection, $documentIds, $documentTenants @@ -1253,7 +1253,7 @@ public function updateDocument(string $collection, string $id, Document $documen $sql = " UPDATE {$this->getSQLTable($name)} SET {$columns} _uid = :_newUid - WHERE _id=:_internalId + WHERE _id=:_sequence {$this->getTenantQuery($collection)} "; @@ -1261,7 +1261,7 @@ public function updateDocument(string $collection, string $id, Document $documen $stmt = $this->getPDO()->prepare($sql); - $stmt->bindValue(':_internalId', $document->getInternalId()); + $stmt->bindValue(':_sequence', $document->getSequence()); $stmt->bindValue(':_newUid', $document->getId()); if ($this->sharedTables) { @@ -1331,8 +1331,8 @@ public function createOrUpdateDocuments( $attributes['_updatedAt'] = $document->getUpdatedAt(); $attributes['_permissions'] = \json_encode($document->getPermissions()); - if (!empty($document->getInternalId())) { - $attributes['_id'] = $document->getInternalId(); + if (!empty($document->getSequence())) { + $attributes['_id'] = $document->getSequence(); } else { $documentIds[] = $document->getId(); } @@ -1495,7 +1495,7 @@ public function createOrUpdateDocuments( $stmtAddPermissions->execute(); } - $sequences = $this->getInternalIds( + $sequences = $this->getSequences( $collection, $documentIds, $documentTenants @@ -1663,12 +1663,12 @@ public function find(string $collection, array $queries = [], ?int $limit = 25, // Get most dominant/first order attribute if ($i === 0 && !empty($cursor)) { - $orderMethodInternalId = Query::TYPE_GREATER; // To preserve natural order + $orderMethodSequence = Query::TYPE_GREATER; // To preserve natural order $orderMethod = $orderType === Database::ORDER_DESC ? Query::TYPE_LESSER : Query::TYPE_GREATER; if ($cursorDirection === Database::CURSOR_BEFORE) { $orderType = $orderType === Database::ORDER_ASC ? Database::ORDER_DESC : Database::ORDER_ASC; - $orderMethodInternalId = $orderType === Database::ORDER_ASC ? Query::TYPE_LESSER : Query::TYPE_GREATER; + $orderMethodSequence = $orderType === Database::ORDER_ASC ? Query::TYPE_LESSER : Query::TYPE_GREATER; $orderMethod = $orderType === Database::ORDER_DESC ? Query::TYPE_LESSER : Query::TYPE_GREATER; } @@ -1686,7 +1686,7 @@ public function find(string $collection, array $queries = [], ?int $limit = 25, OR ( {$this->quote($alias)}.{$this->quote($attribute)} = :cursor AND - {$this->quote($alias)}._id {$this->getSQLOperator($orderMethodInternalId)} {$cursor['$sequence']} + {$this->quote($alias)}._id {$this->getSQLOperator($orderMethodSequence)} {$cursor['$sequence']} ) )"; } elseif ($cursorDirection === Database::CURSOR_BEFORE) { diff --git a/src/Database/Adapter/Postgres.php b/src/Database/Adapter/Postgres.php index fe6ff68bf..d9cb0b543 100644 --- a/src/Database/Adapter/Postgres.php +++ b/src/Database/Adapter/Postgres.php @@ -710,7 +710,7 @@ public function updateRelationship( $collection = $this->getDocument(Database::METADATA, $collection); $relatedCollection = $this->getDocument(Database::METADATA, $relatedCollection); - $junction = $this->getSQLTable('_' . $collection->getInternalId() . '_' . $relatedCollection->getInternalId()); + $junction = $this->getSQLTable('_' . $collection->getSequence() . '_' . $relatedCollection->getSequence()); if (!\is_null($newKey)) { $sql = "ALTER TABLE {$junction} RENAME COLUMN \"{$key}\" TO \"{$newKey}\";"; @@ -795,12 +795,12 @@ public function deleteRelationship( $relatedCollection = $this->getDocument(Database::METADATA, $relatedCollection); $junction = $side === Database::RELATION_SIDE_PARENT - ? $this->getSQLTable('_' . $collection->getInternalId() . '_' . $relatedCollection->getInternalId()) - : $this->getSQLTable('_' . $relatedCollection->getInternalId() . '_' . $collection->getInternalId()); + ? $this->getSQLTable('_' . $collection->getSequence() . '_' . $relatedCollection->getSequence()) + : $this->getSQLTable('_' . $relatedCollection->getSequence() . '_' . $collection->getSequence()); $perms = $side === Database::RELATION_SIDE_PARENT - ? $this->getSQLTable('_' . $collection->getInternalId() . '_' . $relatedCollection->getInternalId() . '_perms') - : $this->getSQLTable('_' . $relatedCollection->getInternalId() . '_' . $collection->getInternalId() . '_perms'); + ? $this->getSQLTable('_' . $collection->getSequence() . '_' . $relatedCollection->getSequence() . '_perms') + : $this->getSQLTable('_' . $relatedCollection->getSequence() . '_' . $collection->getSequence() . '_perms'); $sql = "DROP TABLE {$junction}; DROP TABLE {$perms}"; break; @@ -957,7 +957,7 @@ public function createDocument(string $collection, Document $document): Document $columnNames = ''; // Insert internal id if set - if (!empty($document->getInternalId())) { + if (!empty($document->getSequence())) { $bindKey = '_id'; $columns .= "\"_id\", "; $columnNames .= ':' . $bindKey . ', '; @@ -983,8 +983,8 @@ public function createDocument(string $collection, Document $document): Document $stmt->bindValue(':_uid', $document->getId(), PDO::PARAM_STR); - if (!empty($document->getInternalId())) { - $stmt->bindValue(':_id', $document->getInternalId(), PDO::PARAM_STR); + if (!empty($document->getSequence())) { + $stmt->bindValue(':_id', $document->getSequence(), PDO::PARAM_STR); } $attributeIndex = 0; @@ -1029,7 +1029,7 @@ public function createDocument(string $collection, Document $document): Document try { $this->execute($stmt); $lastInsertedId = $this->getPDO()->lastInsertId(); - // internalId can be manually as well + // Sequence can be manually set as well $document['$sequence'] ??= $lastInsertedId; if (isset($stmtPermissions)) { @@ -1062,15 +1062,15 @@ public function createDocuments(string $collection, array $documents): array $name = $this->filter($collection); $attributeKeys = Database::INTERNAL_ATTRIBUTE_KEYS; - $hasInternalId = null; + $hasSequence = null; foreach ($documents as $document) { $attributes = $document->getAttributes(); $attributeKeys = array_merge($attributeKeys, array_keys($attributes)); - if ($hasInternalId === null) { - $hasInternalId = !empty($document->getInternalId()); - } elseif ($hasInternalId == empty($document->getInternalId())) { - throw new DatabaseException('All documents must have an internalId if one is set'); + if ($hasSequence === null) { + $hasSequence = !empty($document->getSequence()); + } elseif ($hasSequence == empty($document->getSequence())) { + throw new DatabaseException('All documents must have an sequence if one is set'); } } $attributeKeys = array_unique($attributeKeys); @@ -1099,9 +1099,9 @@ public function createDocuments(string $collection, array $documents): array $attributes['_updatedAt'] = $document->getUpdatedAt(); $attributes['_permissions'] = \json_encode($document->getPermissions()); - if (!empty($document->getInternalId())) { + if (!empty($document->getSequence())) { $sequences[$document->getId()] = true; - $attributes['_id'] = $document->getInternalId(); + $attributes['_id'] = $document->getSequence(); $attributeKeys[] = '_id'; } @@ -1177,7 +1177,7 @@ public function createDocuments(string $collection, array $documents): array $collection, $document->getId(), [Query::select(['$sequence'])] - )->getInternalId(); + )->getSequence(); } } @@ -1352,7 +1352,7 @@ public function updateDocument(string $collection, string $id, Document $documen $sql = " UPDATE {$this->getSQLTable($name)} SET {$columns} _uid = :_newUid - WHERE _id=:_internalId + WHERE _id=:_sequence {$this->getTenantQuery($collection)} "; @@ -1360,7 +1360,7 @@ public function updateDocument(string $collection, string $id, Document $documen $stmt = $this->getPDO()->prepare($sql); - $stmt->bindValue(':_internalId', $document->getInternalId()); + $stmt->bindValue(':_sequence', $document->getSequence()); $stmt->bindValue(':_newUid', $document->getId()); if ($this->sharedTables) { @@ -1555,12 +1555,12 @@ public function find(string $collection, array $queries = [], ?int $limit = 25, // Get most dominant/first order attribute if ($i === 0 && !empty($cursor)) { - $orderMethodInternalId = Query::TYPE_GREATER; // To preserve natural order + $orderMethodSequence = Query::TYPE_GREATER; // To preserve natural order $orderMethod = $orderType === Database::ORDER_DESC ? Query::TYPE_LESSER : Query::TYPE_GREATER; if ($cursorDirection === Database::CURSOR_BEFORE) { $orderType = $orderType === Database::ORDER_ASC ? Database::ORDER_DESC : Database::ORDER_ASC; - $orderMethodInternalId = $orderType === Database::ORDER_ASC ? Query::TYPE_LESSER : Query::TYPE_GREATER; + $orderMethodSequence = $orderType === Database::ORDER_ASC ? Query::TYPE_LESSER : Query::TYPE_GREATER; $orderMethod = $orderType === Database::ORDER_DESC ? Query::TYPE_LESSER : Query::TYPE_GREATER; } @@ -1578,7 +1578,7 @@ public function find(string $collection, array $queries = [], ?int $limit = 25, OR ( {$this->quote($alias)}.{$this->quote($attribute)} = :cursor AND - {$this->quote($alias)}._id {$this->getSQLOperator($orderMethodInternalId)} {$cursor['$sequence']} + {$this->quote($alias)}._id {$this->getSQLOperator($orderMethodSequence)} {$cursor['$sequence']} ) )"; } elseif ($cursorDirection === Database::CURSOR_BEFORE) { @@ -1662,7 +1662,7 @@ public function find(string $collection, array $queries = [], ?int $limit = 25, try { $stmt = $this->getPDO()->prepare($sql); foreach ($binds as $key => $value) { - if ($key === ":internalId") { + if ($key === ":sequence") { $stmt->bindValue($key, $value, PDO::PARAM_INT); } else { $stmt->bindValue($key, $value, $this->getPDOType($value)); @@ -1927,8 +1927,8 @@ protected function getSQLCondition(Query $query, array &$binds): string default => $value }; if ($attribute === $this->quote("_id")) { - $binds[":internalId"] = $value; - $conditions[] = "{$alias}.{$attribute} {$operator} :internalId"; + $binds[":sequence"] = $value; + $conditions[] = "{$alias}.{$attribute} {$operator} :sequence"; } else { $binds[":{$placeholder}_{$key}"] = $value; $conditions[] = "{$alias}.{$attribute} {$operator} :{$placeholder}_{$key}"; diff --git a/src/Database/Adapter/SQL.php b/src/Database/Adapter/SQL.php index 17f2a46a1..1aa215d7b 100644 --- a/src/Database/Adapter/SQL.php +++ b/src/Database/Adapter/SQL.php @@ -441,7 +441,7 @@ public function updateDocuments(string $collection, Document $updates, array $do } $name = $this->filter($collection); - $sequences = \array_map(fn ($document) => $document->getInternalId(), $documents); + $sequences = \array_map(fn ($document) => $document->getSequence(), $documents); $sql = " UPDATE {$this->getSQLTable($name)} @@ -716,7 +716,7 @@ public function deleteDocuments(string $collection, array $sequences, array $per * @return array * @throws DatabaseException */ - protected function getInternalIds(string $collection, array $documentIds, array $documentTenants = []): array + protected function getSequences(string $collection, array $documentIds, array $documentTenants = []): array { $sequences = []; @@ -744,7 +744,7 @@ protected function getInternalIds(string $collection, array $documentIds, array } $stmt->execute(); - $results = $stmt->fetchAll(\PDO::FETCH_KEY_PAIR); // Fetch as [documentId => internalId] + $results = $stmt->fetchAll(\PDO::FETCH_KEY_PAIR); // Fetch as [documentId => sequence] $stmt->closeCursor(); $sequences = [...$sequences, ...$results]; diff --git a/src/Database/Adapter/SQLite.php b/src/Database/Adapter/SQLite.php index 5f68b61db..be2c4b5f7 100644 --- a/src/Database/Adapter/SQLite.php +++ b/src/Database/Adapter/SQLite.php @@ -543,7 +543,7 @@ public function createDocument(string $collection, Document $document): Document } // Insert manual id if set - if (!empty($document->getInternalId())) { + if (!empty($document->getSequence())) { $values[] = '_id'; $columns[] = "_id"; } @@ -560,8 +560,8 @@ public function createDocument(string $collection, Document $document): Document $stmt->bindValue(':_uid', $document->getId(), PDO::PARAM_STR); // Bind internal id if set - if (!empty($document->getInternalId())) { - $stmt->bindValue(':_id', $document->getInternalId(), PDO::PARAM_STR); + if (!empty($document->getSequence())) { + $stmt->bindValue(':_id', $document->getSequence(), PDO::PARAM_STR); } $attributeIndex = 0; diff --git a/src/Database/Database.php b/src/Database/Database.php index 2bccb5799..6a52a5a5d 100644 --- a/src/Database/Database.php +++ b/src/Database/Database.php @@ -2507,7 +2507,7 @@ public function createRelationship( $relatedCollection->setAttribute('attributes', $twoWayRelationship, Document::SET_TYPE_APPEND); if ($type === self::RELATION_MANY_TO_MANY) { - $this->silent(fn () => $this->createCollection('_' . $collection->getInternalId() . '_' . $relatedCollection->getInternalId(), [ + $this->silent(fn () => $this->createCollection('_' . $collection->getSequence() . '_' . $relatedCollection->getSequence(), [ new Document([ '$id' => $id, 'key' => $id, @@ -4855,8 +4855,8 @@ private function updateDocumentRelationships(Document $collection, Document $old private function getJunctionCollection(Document $collection, Document $relatedCollection, string $side): string { return $side === Database::RELATION_SIDE_PARENT - ? '_' . $collection->getInternalId() . '_' . $relatedCollection->getInternalId() - : '_' . $relatedCollection->getInternalId() . '_' . $collection->getInternalId(); + ? '_' . $collection->getSequence() . '_' . $relatedCollection->getSequence() + : '_' . $relatedCollection->getSequence() . '_' . $collection->getSequence(); } /** @@ -5791,7 +5791,7 @@ public function deleteDocuments( $sequences = []; $permissionIds = []; foreach ($batch as $document) { - $sequences[] = $document->getInternalId(); + $sequences[] = $document->getSequence(); if (!empty($document->getPermissions())) { $permissionIds[] = $document->getId(); } diff --git a/src/Database/Document.php b/src/Database/Document.php index 824ac6f8d..2ed634f46 100644 --- a/src/Database/Document.php +++ b/src/Database/Document.php @@ -64,7 +64,7 @@ public function getId(): string /** * @return string */ - public function getInternalId(): string + public function getSequence(): string { return $this->getAttribute('$sequence', ''); } diff --git a/tests/e2e/Adapter/Scopes/CollectionTests.php b/tests/e2e/Adapter/Scopes/CollectionTests.php index 8abd2e71b..6c7244918 100644 --- a/tests/e2e/Adapter/Scopes/CollectionTests.php +++ b/tests/e2e/Adapter/Scopes/CollectionTests.php @@ -1392,8 +1392,8 @@ public function testCreatedAtUpdatedAt(): void ], ])); - $this->assertNotEmpty($document->getInternalId()); - $this->assertNotNull($document->getInternalId()); + $this->assertNotEmpty($document->getSequence()); + $this->assertNotNull($document->getSequence()); } /** diff --git a/tests/e2e/Adapter/Scopes/DocumentTests.php b/tests/e2e/Adapter/Scopes/DocumentTests.php index 252884332..654b6b521 100644 --- a/tests/e2e/Adapter/Scopes/DocumentTests.php +++ b/tests/e2e/Adapter/Scopes/DocumentTests.php @@ -117,7 +117,7 @@ public function testCreateDocument(): Document 'with-dash' => 'Works', ])); - $this->assertEquals('56000', $manualIdDocument->getInternalId()); + $this->assertEquals('56000', $manualIdDocument->getSequence()); $this->assertNotEmpty(true, $manualIdDocument->getId()); $this->assertIsString($manualIdDocument->getAttribute('string')); $this->assertEquals('text📝', $manualIdDocument->getAttribute('string')); // Also makes sure an emoji is working @@ -142,7 +142,7 @@ public function testCreateDocument(): Document $manualIdDocument = static::getDatabase()->getDocument('documents', '56000'); - $this->assertEquals('56000', $manualIdDocument->getInternalId()); + $this->assertEquals('56000', $manualIdDocument->getSequence()); $this->assertNotEmpty(true, $manualIdDocument->getId()); $this->assertIsString($manualIdDocument->getAttribute('string')); $this->assertEquals('text📝', $manualIdDocument->getAttribute('string')); // Also makes sure an emoji is working @@ -292,7 +292,7 @@ public function testCreateDocumentsWithDifferentAttributes(): void $this->assertEquals('default', $results[1]->getAttribute('string_default')); /** - * Expect fail, mix of internalId and no internalId + * Expect fail, mix of sequence and no sequence */ $documents = [ new Document([ @@ -984,7 +984,7 @@ public function testFind(): array ])); return [ - '$sequence' => $document->getInternalId() + '$sequence' => $document->getSequence() ]; } diff --git a/tests/e2e/Adapter/Scopes/Relationships/OneToOneTests.php b/tests/e2e/Adapter/Scopes/Relationships/OneToOneTests.php index ee82b9631..9c25b468f 100644 --- a/tests/e2e/Adapter/Scopes/Relationships/OneToOneTests.php +++ b/tests/e2e/Adapter/Scopes/Relationships/OneToOneTests.php @@ -2219,7 +2219,7 @@ public function testDeleteTwoWayRelationshipFromChild(): void $drivers = static::getDatabase()->getCollection('drivers'); $licenses = static::getDatabase()->getCollection('licenses'); - $junction = static::getDatabase()->getCollection('_' . $licenses->getInternalId() . '_' . $drivers->getInternalId()); + $junction = static::getDatabase()->getCollection('_' . $licenses->getSequence() . '_' . $drivers->getSequence()); $this->assertEquals(1, \count($drivers->getAttribute('attributes'))); $this->assertEquals(0, \count($drivers->getAttribute('indexes')));