Skip to content

Commit 86e31fb

Browse files
authored
Merge pull request #1046 from utopia-php/fix-dotted-attribute-keys
fix(sql): filter dotted attribute keys in batch create and upsert
2 parents 37210aa + 916669b commit 86e31fb

2 files changed

Lines changed: 36 additions & 4 deletions

File tree

‎src/Database/Adapter/SQL.php‎

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -924,7 +924,7 @@ private function buildDocumentsInsert(string $name, array $documents, array $spa
924924
$spatialMap = \array_fill_keys($spatialAttributes, true);
925925

926926
foreach ($spatialAttributes as $spatialColumn) {
927-
$builder->insertColumnExpression($spatialColumn, $this->getSpatialGeometryFromText('?'));
927+
$builder->insertColumnExpression($this->filter($spatialColumn), $this->getSpatialGeometryFromText('?'));
928928
}
929929

930930
$intBools = $this->supports(Capability::IntegerBooleans);
@@ -3912,6 +3912,9 @@ protected function executeUpsertBatch(
39123912
): void {
39133913
$builder = $this->dialectBuilder()->into($this->getTableRaw($name));
39143914

3915+
$spatialAttributes = \array_map($this->filter(...), $spatialAttributes);
3916+
$operators = \array_combine(\array_map($this->filter(...), \array_keys($operators)), $operators);
3917+
39153918
foreach ($spatialAttributes as $spatialColumn) {
39163919
$builder->insertColumnExpression($spatialColumn, $this->getSpatialGeometryFromText('?'));
39173920
}
@@ -3956,11 +3959,14 @@ protected function executeUpsertBatch(
39563959

39573960
$currentRegularAttributes = $this->decorateRow($currentRegularAttributes, $document);
39583961

3959-
foreach (\array_keys($currentRegularAttributes) as $column) {
3962+
$row = [];
3963+
foreach ($currentRegularAttributes as $column => $value) {
3964+
$column = $this->filter($column);
3965+
$row[$column] = $value;
39603966
$allColumnNames[$column] = true;
39613967
}
39623968

3963-
$documentsData[] = $currentRegularAttributes;
3969+
$documentsData[] = $row;
39643970
}
39653971

39663972
foreach (\array_keys($operators) as $column) {
@@ -6550,7 +6556,7 @@ protected function buildDocumentRow(Document $document, array $attributeKeys, ar
65506556
if ($intBools && ! isset($spatialMap[$key])) {
65516557
$value = (\is_bool($value)) ? (int) $value : $value;
65526558
}
6553-
$row[$key] = $value;
6559+
$row[$this->filter($key)] = $value;
65546560
}
65556561

65566562
return $row;

‎tests/e2e/Adapter/Scopes/AttributeTests.php‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -519,6 +519,32 @@ public function testDottedAttributeKeysInGroups(): void
519519
$database->deleteCollection($collection);
520520
}
521521

522+
public function testDottedAttributeKeysInBatchWrites(): void
523+
{
524+
$database = $this->getDatabase();
525+
if (! $database->getAdapter()->hasFeature(Feature\Upserts::class)) {
526+
$this->expectNotToPerformAssertions();
527+
528+
return;
529+
}
530+
531+
$collection = $this->createDottedKeyCollection($database);
532+
533+
$database->createDocuments($collection, [
534+
new Document(['$id' => 'e', 'dots.name' => 'x', 'dots.score' => 7]),
535+
new Document(['$id' => 'f', 'dots.name' => 'x', 'dots.score' => 11]),
536+
]);
537+
$database->getAuthorization()->skip(fn (): int => $database->upsertDocuments($collection, [
538+
new Document(['$id' => 'e', 'dots.name' => 'y', 'dots.score' => 7]),
539+
new Document(['$id' => 'g', 'dots.name' => 'y', 'dots.score' => 13]),
540+
]));
541+
542+
$this->assertSame(['e', 'g'], $this->sortedIds($database->find($collection, [Query::equal('dots.name', ['y'])])));
543+
$this->assertSame(['f'], $this->sortedIds($database->find($collection, [Query::equal('dots.name', ['x'])])));
544+
545+
$database->deleteCollection($collection);
546+
}
547+
522548
/**
523549
* @param array<string, mixed> $group
524550
*/

0 commit comments

Comments
 (0)