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
4 changes: 2 additions & 2 deletions src/Database/Adapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -747,12 +747,12 @@ abstract public function deleteDocument(string $collection, string $id): bool;
* Delete Documents
*
* @param string $collection
* @param array<string> $internalIds
* @param array<string> $sequences
* @param array<string> $permissionIds
*
* @return int
*/
abstract public function deleteDocuments(string $collection, array $internalIds, array $permissionIds): int;
abstract public function deleteDocuments(string $collection, array $sequences, array $permissionIds): int;

/**
* Find Documents
Expand Down
24 changes: 12 additions & 12 deletions src/Database/Adapter/MariaDB.php
Original file line number Diff line number Diff line change
Expand Up @@ -905,10 +905,10 @@ public function createDocument(string $collection, Document $document): Document

$stmt->execute();

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

if (empty($document['$internalId'])) {
throw new DatabaseException('Error creating document empty "$internalId"');
if (empty($document['$sequence'])) {
throw new DatabaseException('Error creating document empty "$sequence"');
}

if (isset($stmtPermissions)) {
Expand Down Expand Up @@ -1051,15 +1051,15 @@ public function createDocuments(string $collection, array $documents): array
$stmtPermissions?->execute();
}

$internalIds = $this->getInternalIds(
$sequences = $this->getInternalIds(
$collection,
$documentIds,
$documentTenants
);

foreach ($documents as $document) {
if (isset($internalIds[$document->getId()])) {
$document['$internalId'] = $internalIds[$document->getId()];
if (isset($sequences[$document->getId()])) {
$document['$sequence'] = $sequences[$document->getId()];
}
}
} catch (PDOException $e) {
Expand Down Expand Up @@ -1495,15 +1495,15 @@ public function createOrUpdateDocuments(
$stmtAddPermissions->execute();
}

$internalIds = $this->getInternalIds(
$sequences = $this->getInternalIds(
$collection,
$documentIds,
$documentTenants
);

foreach ($changes as $change) {
if (isset($internalIds[$change->getNew()->getId()])) {
$change->getNew()->setAttribute('$internalId', $internalIds[$change->getNew()->getId()]);
if (isset($sequences[$change->getNew()->getId()])) {
$change->getNew()->setAttribute('$sequence', $sequences[$change->getNew()->getId()]);
}
}
} catch (PDOException $e) {
Expand Down Expand Up @@ -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['$internalId']}
{$this->quote($alias)}._id {$this->getSQLOperator($orderMethodInternalId)} {$cursor['$sequence']}
)
)";
} elseif ($cursorDirection === Database::CURSOR_BEFORE) {
Expand All @@ -1710,7 +1710,7 @@ public function find(string $collection, array $queries = [], ?int $limit = 25,
: Query::TYPE_LESSER;
}

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

// Allow order type without any order attribute, fallback to the natural order (_id)
Expand Down Expand Up @@ -1788,7 +1788,7 @@ public function find(string $collection, array $queries = [], ?int $limit = 25,
unset($results[$index]['_uid']);
}
if (\array_key_exists('_id', $document)) {
$results[$index]['$internalId'] = $document['_id'];
$results[$index]['$sequence'] = $document['_id'];
unset($results[$index]['_id']);
}
if (\array_key_exists('_tenant', $document)) {
Expand Down
2 changes: 1 addition & 1 deletion src/Database/Adapter/Pool.php
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ public function deleteDocument(string $collection, string $id): bool
return $this->delegate(__FUNCTION__, \func_get_args());
}

public function deleteDocuments(string $collection, array $internalIds, array $permissionIds): int
public function deleteDocuments(string $collection, array $sequences, array $permissionIds): int
{
return $this->delegate(__FUNCTION__, \func_get_args());
}
Expand Down
18 changes: 9 additions & 9 deletions src/Database/Adapter/Postgres.php
Original file line number Diff line number Diff line change
Expand Up @@ -1030,7 +1030,7 @@ public function createDocument(string $collection, Document $document): Document
$this->execute($stmt);
$lastInsertedId = $this->getPDO()->lastInsertId();
// internalId can be manually as well
$document['$internalId'] ??= $lastInsertedId;
$document['$sequence'] ??= $lastInsertedId;

if (isset($stmtPermissions)) {
$this->execute($stmtPermissions);
Expand Down Expand Up @@ -1085,7 +1085,7 @@ public function createDocuments(string $collection, array $documents): array
}
$columns = '(' . \implode(', ', $columns) . ')';

$internalIds = [];
$sequences = [];

$bindIndex = 0;
$batchKeys = [];
Expand All @@ -1100,7 +1100,7 @@ public function createDocuments(string $collection, array $documents): array
$attributes['_permissions'] = \json_encode($document->getPermissions());

if (!empty($document->getInternalId())) {
$internalIds[$document->getId()] = true;
$sequences[$document->getId()] = true;
$attributes['_id'] = $document->getInternalId();
$attributeKeys[] = '_id';
}
Expand Down Expand Up @@ -1172,11 +1172,11 @@ public function createDocuments(string $collection, array $documents): array
}

foreach ($documents as $document) {
if (!isset($internalIds[$document->getId()])) {
$document['$internalId'] = $this->getDocument(
if (!isset($sequences[$document->getId()])) {
$document['$sequence'] = $this->getDocument(
$collection,
$document->getId(),
[Query::select(['$internalId'])]
[Query::select(['$sequence'])]
)->getInternalId();
}
}
Expand Down Expand Up @@ -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['$internalId']}
{$this->quote($alias)}._id {$this->getSQLOperator($orderMethodInternalId)} {$cursor['$sequence']}
)
)";
} elseif ($cursorDirection === Database::CURSOR_BEFORE) {
Expand All @@ -1602,7 +1602,7 @@ public function find(string $collection, array $queries = [], ?int $limit = 25,
: Query::TYPE_LESSER;
}

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

// Allow order type without any order attribute, fallback to the natural order (_id)
Expand Down Expand Up @@ -1683,7 +1683,7 @@ public function find(string $collection, array $queries = [], ?int $limit = 25,
unset($results[$index]['_uid']);
}
if (\array_key_exists('_id', $document)) {
$results[$index]['$internalId'] = $document['_id'];
$results[$index]['$sequence'] = $document['_id'];
unset($results[$index]['_id']);
}
if (\array_key_exists('_tenant', $document)) {
Expand Down
28 changes: 14 additions & 14 deletions src/Database/Adapter/SQL.php
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,7 @@ public function getDocument(string $collection, string $id, array $queries = [],
$document = $document[0];

if (\array_key_exists('_id', $document)) {
$document['$internalId'] = $document['_id'];
$document['$sequence'] = $document['_id'];
unset($document['_id']);
}
if (\array_key_exists('_uid', $document)) {
Expand Down Expand Up @@ -441,12 +441,12 @@ public function updateDocuments(string $collection, Document $updates, array $do
}

$name = $this->filter($collection);
$internalIds = \array_map(fn ($document) => $document->getInternalId(), $documents);
$sequences = \array_map(fn ($document) => $document->getInternalId(), $documents);

$sql = "
UPDATE {$this->getSQLTable($name)}
SET {$columns}
WHERE _id IN (" . \implode(', ', \array_map(fn ($index) => ":_id_{$index}", \array_keys($internalIds))) . ")
WHERE _id IN (" . \implode(', ', \array_map(fn ($index) => ":_id_{$index}", \array_keys($sequences))) . ")
{$this->getTenantQuery($collection)}
";

Expand All @@ -457,7 +457,7 @@ public function updateDocuments(string $collection, Document $updates, array $do
$stmt->bindValue(':_tenant', $this->tenant);
}

foreach ($internalIds as $id => $value) {
foreach ($sequences as $id => $value) {
$stmt->bindValue(":_id_{$id}", $value);
}

Expand Down Expand Up @@ -640,15 +640,15 @@ public function updateDocuments(string $collection, Document $updates, array $do
* Delete Documents
*
* @param string $collection
* @param array<string> $internalIds
* @param array<string> $sequences
* @param array<string> $permissionIds
*
* @return int
* @throws DatabaseException
*/
public function deleteDocuments(string $collection, array $internalIds, array $permissionIds): int
public function deleteDocuments(string $collection, array $sequences, array $permissionIds): int
{
if (empty($internalIds)) {
if (empty($sequences)) {
return 0;
}

Expand All @@ -657,15 +657,15 @@ public function deleteDocuments(string $collection, array $internalIds, array $p

$sql = "
DELETE FROM {$this->getSQLTable($name)}
WHERE _id IN (" . \implode(', ', \array_map(fn ($index) => ":_id_{$index}", \array_keys($internalIds))) . ")
WHERE _id IN (" . \implode(', ', \array_map(fn ($index) => ":_id_{$index}", \array_keys($sequences))) . ")
{$this->getTenantQuery($collection)}
";

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

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

foreach ($internalIds as $id => $value) {
foreach ($sequences as $id => $value) {
$stmt->bindValue(":_id_{$id}", $value);
}

Expand Down Expand Up @@ -718,7 +718,7 @@ public function deleteDocuments(string $collection, array $internalIds, array $p
*/
protected function getInternalIds(string $collection, array $documentIds, array $documentTenants = []): array
{
$internalIds = [];
$sequences = [];

/**
* UID, _tenant bottleneck is ~ 5000 rows since we use _uid IN query
Expand Down Expand Up @@ -747,10 +747,10 @@ protected function getInternalIds(string $collection, array $documentIds, array
$results = $stmt->fetchAll(\PDO::FETCH_KEY_PAIR); // Fetch as [documentId => internalId]
$stmt->closeCursor();

$internalIds = [...$internalIds, ...$results];
$sequences = [...$sequences, ...$results];
}

return $internalIds;
return $sequences;
}

/**
Expand Down Expand Up @@ -1692,7 +1692,7 @@ protected function getAttributeProjection(array $selections, string $prefix = ''

$internalKeys = [
'$id',
'$internalId',
'$sequence',
'$permissions',
'$createdAt',
'$updatedAt',
Expand Down Expand Up @@ -1721,7 +1721,7 @@ protected function getInternalKeyForAttribute(string $attribute): string
{
return match ($attribute) {
'$id' => '_uid',
'$internalId' => '_id',
'$sequence' => '_id',
'$collection' => '_collection',
'$tenant' => '_tenant',
'$createdAt' => '_createdAt',
Expand Down
2 changes: 1 addition & 1 deletion src/Database/Adapter/SQLite.php
Original file line number Diff line number Diff line change
Expand Up @@ -609,7 +609,7 @@ public function createDocument(string $collection, Document $document): Document
$statment->execute();
$last = $statment->fetch();

$document['$internalId'] = $last['id'];
$document['$sequence'] = $last['id'];

if (isset($stmtPermissions)) {
$stmtPermissions->execute();
Expand Down
12 changes: 6 additions & 6 deletions src/Database/Database.php
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ class Database
'filters' => [],
],
[
'$id' => '$internalId',
'$id' => '$sequence',
'type' => self::VAR_STRING,
'size' => Database::LENGTH_KEY,
'required' => true,
Expand Down Expand Up @@ -5788,10 +5788,10 @@ public function deleteDocuments(
break;
}

$internalIds = [];
$sequences = [];
$permissionIds = [];
foreach ($batch as $document) {
$internalIds[] = $document->getInternalId();
$sequences[] = $document->getInternalId();
if (!empty($document->getPermissions())) {
$permissionIds[] = $document->getId();
}
Expand All @@ -5815,10 +5815,10 @@ public function deleteDocuments(
}
}

$this->withTransaction(function () use ($collection, $internalIds, $permissionIds) {
$this->withTransaction(function () use ($collection, $sequences, $permissionIds) {
$this->adapter->deleteDocuments(
$collection->getId(),
$internalIds,
$sequences,
$permissionIds
);
});
Expand Down Expand Up @@ -6539,7 +6539,7 @@ private function validateSelections(Document $collection, array $queries): array
$selections = \array_merge($selections, $relationshipSelections);

$selections[] = '$id';
$selections[] = '$internalId';
$selections[] = '$sequence';
$selections[] = '$collection';
$selections[] = '$createdAt';
$selections[] = '$updatedAt';
Expand Down
2 changes: 1 addition & 1 deletion src/Database/Document.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public function getId(): string
*/
public function getInternalId(): string
{
return $this->getAttribute('$internalId', '');
return $this->getAttribute('$sequence', '');
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Database/Query.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ class Query
public function __construct(string $method, string $attribute = '', array $values = [])
{
if ($attribute === '' && \in_array($method, [Query::TYPE_ORDER_ASC, Query::TYPE_ORDER_DESC])) {
$attribute = '$internalId';
$attribute = '$sequence';
}

$this->method = $method;
Expand Down
4 changes: 2 additions & 2 deletions src/Database/Validator/Queries/Documents.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ public function __construct(
'array' => false,
]);
$attributes[] = new Document([
'$id' => '$internalId',
'key' => '$internalId',
'$id' => '$sequence',
'key' => '$sequence',
'type' => Database::VAR_STRING,
'array' => false,
]);
Expand Down
2 changes: 1 addition & 1 deletion src/Database/Validator/Query/Select.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class Select extends Base
*/
protected const INTERNAL_ATTRIBUTES = [
'$id',
'$internalId',
'$sequence',
'$createdAt',
'$updatedAt',
'$permissions',
Expand Down
2 changes: 1 addition & 1 deletion src/Database/Validator/Structure.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class Structure extends Validator
'filters' => [],
],
[
'$id' => '$internalId',
'$id' => '$sequence',
'type' => Database::VAR_STRING,
'size' => 255,
'required' => false,
Expand Down
Loading