Skip to content
Merged
Changes from 1 commit
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
47 changes: 46 additions & 1 deletion src/Database/Validator/Index.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,30 @@ class Index extends Validator

protected bool $arrayIndexSupport;

protected bool $spatialIndexSupport;

protected bool $spatialIndexNullSupport;

protected bool $spatialIndexOrderSupport;

/**
* @param array<Document> $attributes
* @param int $maxLength
* @param array<string> $reservedKeys
* @param bool $arrayIndexSupport
* @param bool $spatialIndexSupport
* @param bool $spatialIndexNullSupport
* @param bool $spatialIndexOrderSupport
* @throws DatabaseException
*/
public function __construct(array $attributes, int $maxLength, array $reservedKeys = [], bool $arrayIndexSupport = false)
public function __construct(array $attributes, int $maxLength, array $reservedKeys = [], bool $arrayIndexSupport = false, bool $spatialIndexSupport = false, bool $spatialIndexNullSupport = false, bool $spatialIndexOrderSupport = false)
{
$this->maxLength = $maxLength;
$this->reservedKeys = $reservedKeys;
$this->arrayIndexSupport = $arrayIndexSupport;
$this->spatialIndexSupport = $spatialIndexSupport;
$this->spatialIndexNullSupport = $spatialIndexNullSupport;
$this->spatialIndexOrderSupport = $spatialIndexOrderSupport;

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

if (!$this->checkSpatialIndex($value)) {
return false;
}

return true;
}

Expand All @@ -315,4 +331,33 @@ public function getType(): string
{
return self::TYPE_OBJECT;
}

/**
* @param Document $index
* @return bool
*/
public function checkSpatialIndex(Document $index): bool
{
$attributes = $index->getAttribute('attributes', []);
$orders = $index->getAttribute('orders', []);
foreach ($attributes as $attributeName) {
$attribute = $this->attributes[\strtolower($attributeName)] ?? new Document();
$attributeType = $attribute->getAttribute('type');
$required = $attribute->getAttribute('required');
if (in_array($attributeType, Database::SPATIAL_TYPES) && !$this->spatialIndexSupport) {
$this->message = "Spatial index is not supported";
return false;
}
if (in_array($attributeType, Database::SPATIAL_TYPES) && !$required && !$this->spatialIndexNullSupport) {
$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.';
return false;
}
if (in_array($attributeType, Database::SPATIAL_TYPES) && !empty($orders) && !$this->spatialIndexOrderSupport) {
$this->message = 'Spatial indexes with explicit orders are not supported. Remove the orders to create this index.';
return false;
}

}
return true;
}
}
Loading