Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 25 additions & 25 deletions src/Database/Adapter/MariaDB.php
Original file line number Diff line number Diff line change
Expand Up @@ -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}`;";
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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 . ', ';
Expand All @@ -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;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -1051,7 +1051,7 @@ public function createDocuments(string $collection, array $documents): array
$stmtPermissions?->execute();
}

$sequences = $this->getInternalIds(
$sequences = $this->getSequences(
$collection,
$documentIds,
$documentTenants
Expand Down Expand Up @@ -1253,15 +1253,15 @@ 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)}
";

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

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

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

if ($this->sharedTables) {
Expand Down Expand Up @@ -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();
}
Expand Down Expand Up @@ -1495,7 +1495,7 @@ public function createOrUpdateDocuments(
$stmtAddPermissions->execute();
}

$sequences = $this->getInternalIds(
$sequences = $this->getSequences(
$collection,
$documentIds,
$documentTenants
Expand Down Expand Up @@ -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;
}

Expand All @@ -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) {
Expand Down
50 changes: 25 additions & 25 deletions src/Database/Adapter/Postgres.php
Original file line number Diff line number Diff line change
Expand Up @@ -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}\";";
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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 . ', ';
Expand All @@ -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;
Expand Down Expand Up @@ -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)) {
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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';
}

Expand Down Expand Up @@ -1177,7 +1177,7 @@ public function createDocuments(string $collection, array $documents): array
$collection,
$document->getId(),
[Query::select(['$sequence'])]
)->getInternalId();
)->getSequence();
}
}

Expand Down Expand Up @@ -1352,15 +1352,15 @@ 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)}
";

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

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

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

if ($this->sharedTables) {
Expand Down Expand Up @@ -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;
}

Expand All @@ -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) {
Expand Down Expand Up @@ -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));
Expand Down Expand Up @@ -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}";
Expand Down
6 changes: 3 additions & 3 deletions src/Database/Adapter/SQL.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)}
Expand Down Expand Up @@ -716,7 +716,7 @@ public function deleteDocuments(string $collection, array $sequences, array $per
* @return array<string>
* @throws DatabaseException
*/
protected function getInternalIds(string $collection, array $documentIds, array $documentTenants = []): array
protected function getSequences(string $collection, array $documentIds, array $documentTenants = []): array
{
$sequences = [];

Expand Down Expand Up @@ -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];
Expand Down
6 changes: 3 additions & 3 deletions src/Database/Adapter/SQLite.php
Original file line number Diff line number Diff line change
Expand Up @@ -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";
}
Expand All @@ -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;
Expand Down
Loading