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
6 changes: 3 additions & 3 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 0 additions & 9 deletions src/Database/Adapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -1208,15 +1208,6 @@ abstract public function getInternalIndexesKeys(): array;
*/
abstract public function getSchemaAttributes(string $collection): array;

/**
* Get the query to check for tenant when in shared tables mode
*
* @param string $collection The collection being queried
* @param string $alias The alias of the parent collection if in a subquery
* @return string
*/
abstract public function getTenantQuery(string $collection, string $alias = ''): string;

/**
* @param mixed $stmt
* @return bool
Expand Down
127 changes: 77 additions & 50 deletions src/Database/Adapter/Mongo.php
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,11 @@ public function createCollection(string $name, array $attributes = [], array $in
$key[$attribute] = $order;
}

$newIndexes[$i] = ['key' => $key, 'name' => $this->filter($index->getId()), 'unique' => $unique];
$newIndexes[$i] = [
'key' => $key,
'name' => $this->filter($index->getId()),
'unique' => $unique
];
}

if (!$this->getClient()->createIndexes($id, $newIndexes)) {
Expand Down Expand Up @@ -337,7 +341,7 @@ public function getSizeOfCollection(string $collection): int
{
$namespace = $this->getNamespace();
$collection = $this->filter($collection);
$collection = $namespace. '_' . $collection;
$collection = $namespace . '_' . $collection;

$command = [
'collStats' => $collection,
Expand Down Expand Up @@ -745,7 +749,7 @@ public function getDocument(string $collection, string $id, array $queries = [],
$filters = ['_uid' => $id];

if ($this->sharedTables) {
$filters['_tenant'] = $this->getTenant();
$filters['_tenant'] = $this->getTenantFilters($collection);
}

$options = [];
Expand Down Expand Up @@ -803,17 +807,14 @@ public function createDocument(string $collection, Document $document): Document
return new Document($result);
}


/**
* Returns the document after casting from
*@param Document $collection
* @param Document $collection
* @param Document $document

* @return Document
*/
public function castingAfter($collection, $document): Document
public function castingAfter(Document $collection, Document $document): Document
{

if (!$this->getSupportForInternalCasting()) {
return $document;
}
Expand Down Expand Up @@ -850,7 +851,7 @@ public function castingAfter($collection, $document): Document
break;
case Database::VAR_DATETIME :
if ($node instanceof UTCDateTime) {
$node = DateTime::format($node->toDateTime());
$node = DateTime::format($node->toDateTime());
}
break;
default:
Expand All @@ -866,14 +867,13 @@ public function castingAfter($collection, $document): Document

/**
* Returns the document after casting to
*@param Document $collection
* @param Document $collection
* @param Document $document

* @return Document
* @throws Exception
*/
public function castingBefore($collection, $document): Document
public function castingBefore(Document $collection, Document $document): Document
{

if (!$this->getSupportForInternalCasting()) {
return $document;
}
Expand Down Expand Up @@ -939,7 +939,7 @@ public function createDocuments(string $collection, array $documents): array

$records = [];
$hasSequence = null;
$documents = array_map(fn ($doc) => clone $doc, $documents);
$documents = \array_map(fn ($doc) => clone $doc, $documents);

foreach ($documents as $document) {
$sequence = $document->getSequence();
Expand All @@ -950,12 +950,6 @@ public function createDocuments(string $collection, array $documents): array
throw new DatabaseException('All documents must have an sequence if one is set');
}

$document->removeAttribute('$sequence');

if ($this->sharedTables) {
$document->setAttribute('$tenant', $this->getTenant());
}

$record = $this->replaceChars('$', '_', (array)$document);

if (!empty($sequence)) {
Expand Down Expand Up @@ -985,15 +979,14 @@ public function createDocuments(string $collection, array $documents): array
*/
private function insertDocument(string $name, array $document): array
{

try {
$this->client->insert($name, $document);

$filters = [];
$filters['_uid'] = $document['_uid'];

if ($this->sharedTables) {
$filters['_tenant'] = $this->getTenant();
$filters['_tenant'] = $this->getTenantFilters($name);
}

$result = $this->client->find(
Expand All @@ -1008,17 +1001,16 @@ private function insertDocument(string $name, array $document): array
}
}



/**
* Update Document
*
* @param string $collection
* @param string $id
* @param Document $document
*
* @param bool $skipPermissions
* @return Document
* @throws Exception
* @throws DatabaseException
* @throws Duplicate
*/
public function updateDocument(string $collection, string $id, Document $document, bool $skipPermissions): Document
{
Expand All @@ -1027,12 +1019,13 @@ public function updateDocument(string $collection, string $id, Document $documen
$record = $document->getArrayCopy();
$record = $this->replaceChars('$', '_', $record);


$filters = [];
$filters['_uid'] = $id;

if ($this->sharedTables) {
$filters['_tenant'] = $this->getTenant();
$filters['_tenant'] = $this->getTenantFilters($collection);
}

try {
unset($record['_id']); // Don't update _id

Expand Down Expand Up @@ -1069,7 +1062,7 @@ public function updateDocuments(string $collection, Document $updates, array $do
$filters = $this->buildFilters($queries);

if ($this->sharedTables) {
$filters['_tenant'] = $this->getTenant();
$filters['_tenant'] = $this->getTenantFilters($collection);
}

$record = $updates->getArrayCopy();
Expand Down Expand Up @@ -1125,10 +1118,10 @@ public function createOrUpdateDocuments(string $collection, string $attribute, a
$record = $this->removeNullKeys($record);

// Build filter for upsert
$filter = ['_uid' => $document->getId()];
$filters = ['_uid' => $document->getId()];

if ($this->sharedTables) {
$filter['_tenant'] = $document->getTenant();
$filters['_tenant'] = $this->getTenantFilters($collection);
}

unset($record['_id']); // Don't update _id
Expand All @@ -1154,7 +1147,7 @@ public function createOrUpdateDocuments(string $collection, string $attribute, a
}

$operations[] = [
'filter' => $filter,
'filter' => $filters,
'update' => $update,
];
}
Expand All @@ -1178,6 +1171,8 @@ public function createOrUpdateDocuments(string $collection, string $attribute, a
* @param string $collection
* @param array<Document> $documents
* @return array<Document>
* @throws DatabaseException
* @throws MongoException
*/
public function getSequences(string $collection, array $documents): array
{
Expand All @@ -1203,7 +1198,7 @@ public function getSequences(string $collection, array $documents): array
$filters = ['_uid' => ['$in' => $documentIds]];

if ($this->sharedTables) {
$filters['_tenant'] = ['$in' => $documentTenants];
$filters['_tenant'] = $this->getTenantFilters($collection, $documentTenants);
}

$results = $this->client->find($name, $filters, ['projection' => ['_uid' => 1, '_id' => 1]]);
Expand Down Expand Up @@ -1242,7 +1237,7 @@ public function increaseDocumentAttribute(string $collection, string $id, string
$filters = ['_uid' => $id];

if ($this->sharedTables) {
$filters['_tenant'] = $this->getTenant();
$filters['_tenant'] = $this->getTenantFilters($collection);
}

if ($max) {
Expand Down Expand Up @@ -1280,8 +1275,9 @@ public function deleteDocument(string $collection, string $id): bool

$filters = [];
$filters['_uid'] = $id;

if ($this->sharedTables) {
$filters['_tenant'] = $this->getTenant();
$filters['_tenant'] = $this->getTenantFilters($collection);
}

$result = $this->client->delete($name, $filters);
Expand All @@ -1308,7 +1304,7 @@ public function deleteDocuments(string $collection, array $sequences, array $per
$filters = $this->buildFilters([new Query(Query::TYPE_EQUAL, '_id', $sequences)]);

if ($this->sharedTables) {
$filters['_tenant'] = $this->getTenant();
$filters['_tenant'] = $this->getTenantFilters($collection);
}

$filters = $this->replaceInternalIdsKeys($filters, '$', '_', $this->operators);
Expand Down Expand Up @@ -1346,7 +1342,6 @@ public function updateAttribute(string $collection, string $id, string $type, in
if (!empty($newKey) && $newKey !== $id) {
return $this->renameAttribute($collection, $id, $newKey);
}

return true;
}

Expand Down Expand Up @@ -1391,14 +1386,13 @@ protected function getInternalKeyForAttribute(string $attribute): string
*/
public function find(string $collection, array $queries = [], ?int $limit = 25, ?int $offset = null, array $orderAttributes = [], array $orderTypes = [], array $cursor = [], string $cursorDirection = Database::CURSOR_AFTER, string $forPermission = Database::PERMISSION_READ): array
{

$name = $this->getNamespace() . '_' . $this->filter($collection);
$queries = array_map(fn ($query) => clone $query, $queries);

$filters = $this->buildFilters($queries);

if ($this->sharedTables) {
$filters['_tenant'] = $this->getTenant();
$filters['_tenant'] = $this->getTenantFilters($collection);
}

// permissions
Expand Down Expand Up @@ -1525,7 +1519,6 @@ public function find(string $collection, array $queries = [], ?int $limit = 25,
}



/**
* Converts timestamp to Mongo\BSON datetime format.
*
Expand Down Expand Up @@ -1597,6 +1590,10 @@ public function count(string $collection, array $queries = [], ?int $max = null)
// queries
$filters = $this->buildFilters($queries);

if ($this->sharedTables) {
$filters['_tenant'] = $this->getTenantFilters($collection);
}

// permissions
if (Authorization::$status) { // skip if authorization is disabled
$roles = \implode('|', Authorization::getRoles());
Expand Down Expand Up @@ -1625,6 +1622,10 @@ public function sum(string $collection, string $attribute, array $queries = [],
$queries = array_map(fn ($query) => clone $query, $queries);
$filters = $this->buildFilters($queries);

if ($this->sharedTables) {
$filters['_tenant'] = $this->getTenantFilters($collection);
}

// permissions
if (Authorization::$status) { // skip if authorization is disabled
$roles = \implode('|', Authorization::getRoles());
Expand Down Expand Up @@ -1843,10 +1844,10 @@ protected function getQueryValue(string $method, mixed $value): mixed
switch ($method) {
case Query::TYPE_STARTS_WITH:
$value = $this->escapeWildcards($value);
return $value.'.*';
return $value . '.*';
case Query::TYPE_ENDS_WITH:
$value = $this->escapeWildcards($value);
return '.*'.$value;
return '.*' . $value;
default:
return $value;
}
Expand All @@ -1865,7 +1866,7 @@ protected function getOrder(string $order): int
return match ($order) {
Database::ORDER_ASC => 1,
Database::ORDER_DESC => -1,
default => throw new DatabaseException('Unknown sort order:' . $order . '. Must be one of ' . Database::ORDER_ASC . ', ' . Database::ORDER_DESC),
default => throw new DatabaseException('Unknown sort order:' . $order . '. Must be one of ' . Database::ORDER_ASC . ', ' . Database::ORDER_DESC),
};
}

Expand Down Expand Up @@ -2294,8 +2295,8 @@ protected function execute(mixed $stmt): bool
}

/**
* @return string
*/
* @return string
*/
public function getIdAttributeType(): string
{
return Database::VAR_OBJECT_ID;
Expand Down Expand Up @@ -2324,10 +2325,36 @@ public function getSchemaAttributes(string $collection): array
return [];
}

public function getTenantQuery(string $collection, string $parentAlias = ''): string
{
// ** tenant in mongodb is an int but we need to return a string in order to be compatible with the rest of the code
return (string)$this->getTenant();
}
/**
* @param string $collection
* @param array<int> $tenants
* @return int|array<string, array<int>>
*/
public function getTenantFilters(
string $collection,
array $tenants = [],
): int|array {
$values = [];
if (!$this->sharedTables) {
return $values;
}

if (\count($tenants) === 0) {
$values[] = $this->getTenant();
} else {
for ($index = 0; $index < \count($tenants); $index++) {
$values[] = $tenants[$index];
}
}

if ($collection === Database::METADATA) {
$values[] = null;
}

if (\count($values) === 1) {
return $values[0];
}

return ['$in' => $values];
}
}
Loading