Skip to content

Commit 13a6687

Browse files
committed
Merge branch 'main' of github.com:utopia-php/database into alter-shared-lock
2 parents 1a344c6 + c34cb23 commit 13a6687

5 files changed

Lines changed: 264 additions & 62 deletions

File tree

src/Database/Adapter/MariaDB.php

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1312,12 +1312,12 @@ public function increaseDocumentAttribute(
13121312
$name = $this->filter($collection);
13131313
$attribute = $this->filter($attribute);
13141314

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

13181318
$sql = "
1319-
UPDATE {$this->getSQLTable($name)}
1320-
SET
1319+
UPDATE {$this->getSQLTable($name)}
1320+
SET
13211321
`{$attribute}` = `{$attribute}` + :val,
13221322
`_updatedAt` = :updatedAt
13231323
WHERE _uid = :_uid
@@ -1333,6 +1333,12 @@ public function increaseDocumentAttribute(
13331333
$stmt->bindValue(':val', $value);
13341334
$stmt->bindValue(':updatedAt', $updatedAt);
13351335

1336+
if ($max !== null) {
1337+
$stmt->bindValue(':max', $max);
1338+
}
1339+
if ($min !== null) {
1340+
$stmt->bindValue(':min', $min);
1341+
}
13361342
if ($this->sharedTables) {
13371343
$stmt->bindValue(':_tenant', $this->tenant);
13381344
}

src/Database/Adapter/Mongo.php

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1706,12 +1706,14 @@ public function increaseDocumentAttribute(string $collection, string $id, string
17061706
$filters['_tenant'] = $this->getTenantFilters($collection);
17071707
}
17081708

1709-
if ($max) {
1710-
$filters[$attribute] = ['$lte' => $max];
1711-
}
1712-
1713-
if ($min) {
1714-
$filters[$attribute] = ['$gte' => $min];
1709+
if ($max !== null || $min !== null) {
1710+
$filters[$attribute] = [];
1711+
if ($max !== null) {
1712+
$filters[$attribute]['$lte'] = $max;
1713+
}
1714+
if ($min !== null) {
1715+
$filters[$attribute]['$gte'] = $min;
1716+
}
17151717
}
17161718

17171719
$options = $this->getTransactionOptions();

src/Database/Adapter/Postgres.php

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1433,12 +1433,12 @@ public function increaseDocumentAttribute(string $collection, string $id, string
14331433
$name = $this->filter($collection);
14341434
$attribute = $this->filter($attribute);
14351435

1436-
$sqlMax = $max ? " AND \"{$attribute}\" <= {$max}" : "";
1437-
$sqlMin = $min ? " AND \"{$attribute}\" >= {$min}" : "";
1436+
$sqlMax = $max !== null ? " AND \"{$attribute}\" <= :max" : "";
1437+
$sqlMin = $min !== null ? " AND \"{$attribute}\" >= :min" : "";
14381438

14391439
$sql = "
1440-
UPDATE {$this->getSQLTable($name)}
1441-
SET
1440+
UPDATE {$this->getSQLTable($name)}
1441+
SET
14421442
\"{$attribute}\" = \"{$attribute}\" + :val,
14431443
\"_updatedAt\" = :updatedAt
14441444
WHERE _uid = :_uid
@@ -1454,6 +1454,12 @@ public function increaseDocumentAttribute(string $collection, string $id, string
14541454
$stmt->bindValue(':val', $value);
14551455
$stmt->bindValue(':updatedAt', $updatedAt);
14561456

1457+
if ($max !== null) {
1458+
$stmt->bindValue(':max', $max);
1459+
}
1460+
if ($min !== null) {
1461+
$stmt->bindValue(':min', $min);
1462+
}
14571463
if ($this->sharedTables) {
14581464
$stmt->bindValue(':_tenant', $this->tenant);
14591465
}

src/Database/Database.php

Lines changed: 58 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -4482,15 +4482,17 @@ public function createDocument(string $collection, Document $document): Document
44824482
}
44834483
}
44844484

4485-
$structure = new Structure(
4486-
$collection,
4487-
$this->adapter->getIdAttributeType(),
4488-
$this->adapter->getMinDateTime(),
4489-
$this->adapter->getMaxDateTime(),
4490-
$this->adapter->getSupportForAttributes()
4491-
);
4492-
if (!$structure->isValid($document)) {
4493-
throw new StructureException($structure->getDescription());
4485+
if ($this->validate) {
4486+
$structure = new Structure(
4487+
$collection,
4488+
$this->adapter->getIdAttributeType(),
4489+
$this->adapter->getMinDateTime(),
4490+
$this->adapter->getMaxDateTime(),
4491+
$this->adapter->getSupportForAttributes()
4492+
);
4493+
if (!$structure->isValid($document)) {
4494+
throw new StructureException($structure->getDescription());
4495+
}
44944496
}
44954497

44964498
$document = $this->adapter->castingBefore($collection, $document);
@@ -4583,15 +4585,17 @@ public function createDocuments(
45834585

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

4586-
$validator = new Structure(
4587-
$collection,
4588-
$this->adapter->getIdAttributeType(),
4589-
$this->adapter->getMinDateTime(),
4590-
$this->adapter->getMaxDateTime(),
4591-
$this->adapter->getSupportForAttributes()
4592-
);
4593-
if (!$validator->isValid($document)) {
4594-
throw new StructureException($validator->getDescription());
4588+
if ($this->validate) {
4589+
$validator = new Structure(
4590+
$collection,
4591+
$this->adapter->getIdAttributeType(),
4592+
$this->adapter->getMinDateTime(),
4593+
$this->adapter->getMaxDateTime(),
4594+
$this->adapter->getSupportForAttributes()
4595+
);
4596+
if (!$validator->isValid($document)) {
4597+
throw new StructureException($validator->getDescription());
4598+
}
45954599
}
45964600

45974601
if ($this->resolveRelationships) {
@@ -5145,16 +5149,18 @@ public function updateDocument(string $collection, string $id, Document $documen
51455149

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

5148-
$structureValidator = new Structure(
5149-
$collection,
5150-
$this->adapter->getIdAttributeType(),
5151-
$this->adapter->getMinDateTime(),
5152-
$this->adapter->getMaxDateTime(),
5153-
$this->adapter->getSupportForAttributes(),
5154-
$old
5155-
);
5156-
if (!$structureValidator->isValid($document)) { // Make sure updated structure still apply collection rules (if any)
5157-
throw new StructureException($structureValidator->getDescription());
5152+
if ($this->validate) {
5153+
$structureValidator = new Structure(
5154+
$collection,
5155+
$this->adapter->getIdAttributeType(),
5156+
$this->adapter->getMinDateTime(),
5157+
$this->adapter->getMaxDateTime(),
5158+
$this->adapter->getSupportForAttributes(),
5159+
$old
5160+
);
5161+
if (!$structureValidator->isValid($document)) { // Make sure updated structure still apply collection rules (if any)
5162+
throw new StructureException($structureValidator->getDescription());
5163+
}
51585164
}
51595165

51605166
if ($this->resolveRelationships) {
@@ -5300,17 +5306,19 @@ public function updateDocuments(
53005306
applyDefaults: false
53015307
);
53025308

5303-
$validator = new PartialStructure(
5304-
$collection,
5305-
$this->adapter->getIdAttributeType(),
5306-
$this->adapter->getMinDateTime(),
5307-
$this->adapter->getMaxDateTime(),
5308-
$this->adapter->getSupportForAttributes(),
5309-
null // No old document available in bulk updates
5310-
);
5309+
if ($this->validate) {
5310+
$validator = new PartialStructure(
5311+
$collection,
5312+
$this->adapter->getIdAttributeType(),
5313+
$this->adapter->getMinDateTime(),
5314+
$this->adapter->getMaxDateTime(),
5315+
$this->adapter->getSupportForAttributes(),
5316+
null // No old document available in bulk updates
5317+
);
53115318

5312-
if (!$validator->isValid($updates)) {
5313-
throw new StructureException($validator->getDescription());
5319+
if (!$validator->isValid($updates)) {
5320+
throw new StructureException($validator->getDescription());
5321+
}
53145322
}
53155323

53165324
$originalLimit = $limit;
@@ -6064,17 +6072,19 @@ public function upsertDocumentsWithIncrease(
60646072
}
60656073
}
60666074

6067-
$validator = new Structure(
6068-
$collection,
6069-
$this->adapter->getIdAttributeType(),
6070-
$this->adapter->getMinDateTime(),
6071-
$this->adapter->getMaxDateTime(),
6072-
$this->adapter->getSupportForAttributes(),
6073-
$old->isEmpty() ? null : $old
6074-
);
6075+
if ($this->validate) {
6076+
$validator = new Structure(
6077+
$collection,
6078+
$this->adapter->getIdAttributeType(),
6079+
$this->adapter->getMinDateTime(),
6080+
$this->adapter->getMaxDateTime(),
6081+
$this->adapter->getSupportForAttributes(),
6082+
$old->isEmpty() ? null : $old
6083+
);
60756084

6076-
if (!$validator->isValid($document)) {
6077-
throw new StructureException($validator->getDescription());
6085+
if (!$validator->isValid($document)) {
6086+
throw new StructureException($validator->getDescription());
6087+
}
60786088
}
60796089

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

0 commit comments

Comments
 (0)