Skip to content

Commit 0b30244

Browse files
added index validtor for spatial index
1 parent 966c3cd commit 0b30244

File tree

1 file changed

+46
-1
lines changed

1 file changed

+46
-1
lines changed

src/Database/Validator/Index.php

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,18 +24,30 @@ class Index extends Validator
2424

2525
protected bool $arrayIndexSupport;
2626

27+
protected bool $spatialIndexSupport;
28+
29+
protected bool $spatialIndexNullSupport;
30+
31+
protected bool $spatialIndexOrderSupport;
32+
2733
/**
2834
* @param array<Document> $attributes
2935
* @param int $maxLength
3036
* @param array<string> $reservedKeys
3137
* @param bool $arrayIndexSupport
38+
* @param bool $spatialIndexSupport
39+
* @param bool $spatialIndexNullSupport
40+
* @param bool $spatialIndexOrderSupport
3241
* @throws DatabaseException
3342
*/
34-
public function __construct(array $attributes, int $maxLength, array $reservedKeys = [], bool $arrayIndexSupport = false)
43+
public function __construct(array $attributes, int $maxLength, array $reservedKeys = [], bool $arrayIndexSupport = false, bool $spatialIndexSupport = false, bool $spatialIndexNullSupport = false, bool $spatialIndexOrderSupport = false)
3544
{
3645
$this->maxLength = $maxLength;
3746
$this->reservedKeys = $reservedKeys;
3847
$this->arrayIndexSupport = $arrayIndexSupport;
48+
$this->spatialIndexSupport = $spatialIndexSupport;
49+
$this->spatialIndexNullSupport = $spatialIndexNullSupport;
50+
$this->spatialIndexOrderSupport = $spatialIndexOrderSupport;
3951

4052
foreach ($attributes as $attribute) {
4153
$key = \strtolower($attribute->getAttribute('key', $attribute->getAttribute('$id')));
@@ -289,6 +301,10 @@ public function isValid($value): bool
289301
return false;
290302
}
291303

304+
if (!$this->checkSpatialIndex($value)) {
305+
return false;
306+
}
307+
292308
return true;
293309
}
294310

@@ -315,4 +331,33 @@ public function getType(): string
315331
{
316332
return self::TYPE_OBJECT;
317333
}
334+
335+
/**
336+
* @param Document $index
337+
* @return bool
338+
*/
339+
public function checkSpatialIndex(Document $index): bool
340+
{
341+
$attributes = $index->getAttribute('attributes', []);
342+
$orders = $index->getAttribute('orders', []);
343+
foreach ($attributes as $attributeName) {
344+
$attribute = $this->attributes[\strtolower($attributeName)] ?? new Document();
345+
$attributeType = $attribute->getAttribute('type');
346+
$required = $attribute->getAttribute('required');
347+
if (in_array($attributeType, Database::SPATIAL_TYPES) && !$this->spatialIndexSupport) {
348+
$this->message = "Spatial index is not supported";
349+
return false;
350+
}
351+
if (in_array($attributeType, Database::SPATIAL_TYPES) && !$required && !$this->spatialIndexNullSupport) {
352+
$this->message = 'Spatial indexes do not allow null values. Mark the attribute "' . $attributeName . '" as required or create the index on a column with no null values.';
353+
return false;
354+
}
355+
if (in_array($attributeType, Database::SPATIAL_TYPES) && !empty($orders) && !$this->spatialIndexOrderSupport) {
356+
$this->message = 'Spatial indexes with explicit orders are not supported. Remove the orders to create this index.';
357+
return false;
358+
}
359+
360+
}
361+
return true;
362+
}
318363
}

0 commit comments

Comments
 (0)