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
14 changes: 10 additions & 4 deletions src/Database/Adapter/MariaDB.php
Original file line number Diff line number Diff line change
Expand Up @@ -1312,12 +1312,12 @@ public function increaseDocumentAttribute(
$name = $this->filter($collection);
$attribute = $this->filter($attribute);

$sqlMax = $max ? " AND `{$attribute}` <= {$max}" : '';
$sqlMin = $min ? " AND `{$attribute}` >= {$min}" : '';
$sqlMax = $max !== null ? " AND `{$attribute}` <= :max" : '';
$sqlMin = $min !== null ? " AND `{$attribute}` >= :min" : '';

$sql = "
UPDATE {$this->getSQLTable($name)}
SET
UPDATE {$this->getSQLTable($name)}
SET
`{$attribute}` = `{$attribute}` + :val,
`_updatedAt` = :updatedAt
WHERE _uid = :_uid
Expand All @@ -1333,6 +1333,12 @@ public function increaseDocumentAttribute(
$stmt->bindValue(':val', $value);
$stmt->bindValue(':updatedAt', $updatedAt);

if ($max !== null) {
$stmt->bindValue(':max', $max);
}
if ($min !== null) {
$stmt->bindValue(':min', $min);
}
if ($this->sharedTables) {
$stmt->bindValue(':_tenant', $this->tenant);
}
Expand Down
14 changes: 8 additions & 6 deletions src/Database/Adapter/Mongo.php
Original file line number Diff line number Diff line change
Expand Up @@ -1706,12 +1706,14 @@ public function increaseDocumentAttribute(string $collection, string $id, string
$filters['_tenant'] = $this->getTenantFilters($collection);
}

if ($max) {
$filters[$attribute] = ['$lte' => $max];
}

if ($min) {
$filters[$attribute] = ['$gte' => $min];
if ($max !== null || $min !== null) {
$filters[$attribute] = [];
if ($max !== null) {
$filters[$attribute]['$lte'] = $max;
}
if ($min !== null) {
$filters[$attribute]['$gte'] = $min;
}
}

$options = $this->getTransactionOptions();
Expand Down
14 changes: 10 additions & 4 deletions src/Database/Adapter/Postgres.php
Original file line number Diff line number Diff line change
Expand Up @@ -1433,12 +1433,12 @@ public function increaseDocumentAttribute(string $collection, string $id, string
$name = $this->filter($collection);
$attribute = $this->filter($attribute);

$sqlMax = $max ? " AND \"{$attribute}\" <= {$max}" : "";
$sqlMin = $min ? " AND \"{$attribute}\" >= {$min}" : "";
$sqlMax = $max !== null ? " AND \"{$attribute}\" <= :max" : "";
$sqlMin = $min !== null ? " AND \"{$attribute}\" >= :min" : "";

$sql = "
UPDATE {$this->getSQLTable($name)}
SET
UPDATE {$this->getSQLTable($name)}
SET
\"{$attribute}\" = \"{$attribute}\" + :val,
\"_updatedAt\" = :updatedAt
WHERE _uid = :_uid
Expand All @@ -1454,6 +1454,12 @@ public function increaseDocumentAttribute(string $collection, string $id, string
$stmt->bindValue(':val', $value);
$stmt->bindValue(':updatedAt', $updatedAt);

if ($max !== null) {
$stmt->bindValue(':max', $max);
}
if ($min !== null) {
$stmt->bindValue(':min', $min);
}
if ($this->sharedTables) {
$stmt->bindValue(':_tenant', $this->tenant);
}
Expand Down
106 changes: 58 additions & 48 deletions src/Database/Database.php
Original file line number Diff line number Diff line change
Expand Up @@ -4464,15 +4464,17 @@ public function createDocument(string $collection, Document $document): Document
}
}

$structure = new Structure(
$collection,
$this->adapter->getIdAttributeType(),
$this->adapter->getMinDateTime(),
$this->adapter->getMaxDateTime(),
$this->adapter->getSupportForAttributes()
);
if (!$structure->isValid($document)) {
throw new StructureException($structure->getDescription());
if ($this->validate) {
$structure = new Structure(
$collection,
$this->adapter->getIdAttributeType(),
$this->adapter->getMinDateTime(),
$this->adapter->getMaxDateTime(),
$this->adapter->getSupportForAttributes()
);
if (!$structure->isValid($document)) {
throw new StructureException($structure->getDescription());
}
}

$document = $this->adapter->castingBefore($collection, $document);
Expand Down Expand Up @@ -4565,15 +4567,17 @@ public function createDocuments(

$document = $this->encode($collection, $document);

$validator = new Structure(
$collection,
$this->adapter->getIdAttributeType(),
$this->adapter->getMinDateTime(),
$this->adapter->getMaxDateTime(),
$this->adapter->getSupportForAttributes()
);
if (!$validator->isValid($document)) {
throw new StructureException($validator->getDescription());
if ($this->validate) {
$validator = new Structure(
$collection,
$this->adapter->getIdAttributeType(),
$this->adapter->getMinDateTime(),
$this->adapter->getMaxDateTime(),
$this->adapter->getSupportForAttributes()
);
if (!$validator->isValid($document)) {
throw new StructureException($validator->getDescription());
}
}

if ($this->resolveRelationships) {
Expand Down Expand Up @@ -5127,16 +5131,18 @@ public function updateDocument(string $collection, string $id, Document $documen

$document = $this->encode($collection, $document);

$structureValidator = new Structure(
$collection,
$this->adapter->getIdAttributeType(),
$this->adapter->getMinDateTime(),
$this->adapter->getMaxDateTime(),
$this->adapter->getSupportForAttributes(),
$old
);
if (!$structureValidator->isValid($document)) { // Make sure updated structure still apply collection rules (if any)
throw new StructureException($structureValidator->getDescription());
if ($this->validate) {
$structureValidator = new Structure(
$collection,
$this->adapter->getIdAttributeType(),
$this->adapter->getMinDateTime(),
$this->adapter->getMaxDateTime(),
$this->adapter->getSupportForAttributes(),
$old
);
if (!$structureValidator->isValid($document)) { // Make sure updated structure still apply collection rules (if any)
throw new StructureException($structureValidator->getDescription());
}
}

if ($this->resolveRelationships) {
Expand Down Expand Up @@ -5282,17 +5288,19 @@ public function updateDocuments(
applyDefaults: false
);

$validator = new PartialStructure(
$collection,
$this->adapter->getIdAttributeType(),
$this->adapter->getMinDateTime(),
$this->adapter->getMaxDateTime(),
$this->adapter->getSupportForAttributes(),
null // No old document available in bulk updates
);
if ($this->validate) {
$validator = new PartialStructure(
$collection,
$this->adapter->getIdAttributeType(),
$this->adapter->getMinDateTime(),
$this->adapter->getMaxDateTime(),
$this->adapter->getSupportForAttributes(),
null // No old document available in bulk updates
);

if (!$validator->isValid($updates)) {
throw new StructureException($validator->getDescription());
if (!$validator->isValid($updates)) {
throw new StructureException($validator->getDescription());
}
}

$originalLimit = $limit;
Expand Down Expand Up @@ -6046,17 +6054,19 @@ public function upsertDocumentsWithIncrease(
}
}

$validator = new Structure(
$collection,
$this->adapter->getIdAttributeType(),
$this->adapter->getMinDateTime(),
$this->adapter->getMaxDateTime(),
$this->adapter->getSupportForAttributes(),
$old->isEmpty() ? null : $old
);
if ($this->validate) {
$validator = new Structure(
$collection,
$this->adapter->getIdAttributeType(),
$this->adapter->getMinDateTime(),
$this->adapter->getMaxDateTime(),
$this->adapter->getSupportForAttributes(),
$old->isEmpty() ? null : $old
);

if (!$validator->isValid($document)) {
throw new StructureException($validator->getDescription());
if (!$validator->isValid($document)) {
throw new StructureException($validator->getDescription());
}
}

$document = $this->encode($collection, $document);
Expand Down
Loading