Skip to content

Commit 276fffb

Browse files
authored
Merge pull request #628 from utopia-php/validate-create-index-array
Validate create index array
2 parents 363ee53 + d8f155f commit 276fffb

File tree

3 files changed

+24
-12
lines changed

3 files changed

+24
-12
lines changed

src/Database/Adapter/MariaDB.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2102,4 +2102,12 @@ public function getSupportForNumericCasting(): bool
21022102
{
21032103
return true;
21042104
}
2105+
2106+
public function getSupportForIndexArray(): bool
2107+
{
2108+
/**
2109+
* Disabled to be compatible with Mysql adapter
2110+
*/
2111+
return false;
2112+
}
21052113
}

src/Database/Database.php

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1242,10 +1242,6 @@ public function createCollection(string $id, array $attributes = [], array $inde
12421242

12431243
$isArray = $collectionAttribute->getAttribute('array', false);
12441244
if ($isArray) {
1245-
if (!$this->adapter->getSupportForIndexArray()) {
1246-
throw new IndexException('Indexing an array attribute is not supported');
1247-
}
1248-
12491245
if ($this->adapter->getMaxIndexLength() > 0) {
12501246
$lengths[$i] = self::ARRAY_INDEX_LENGTH;
12511247
}
@@ -1274,7 +1270,8 @@ public function createCollection(string $id, array $attributes = [], array $inde
12741270
$validator = new IndexValidator(
12751271
$attributes,
12761272
$this->adapter->getMaxIndexLength(),
1277-
$this->adapter->getInternalIndexesKeys()
1273+
$this->adapter->getInternalIndexesKeys(),
1274+
$this->adapter->getSupportForIndexArray()
12781275
);
12791276
foreach ($indexes as $index) {
12801277
if (!$validator->isValid($index)) {
@@ -2199,7 +2196,8 @@ public function updateAttribute(string $collection, string $id, ?string $type =
21992196
$validator = new IndexValidator(
22002197
$attributes,
22012198
$this->adapter->getMaxIndexLength(),
2202-
$this->adapter->getInternalIndexesKeys()
2199+
$this->adapter->getInternalIndexesKeys(),
2200+
$this->adapter->getSupportForIndexArray()
22032201
);
22042202

22052203
foreach ($indexes as $index) {
@@ -3079,10 +3077,6 @@ public function createIndex(string $collection, string $id, string $type, array
30793077

30803078
$isArray = $collectionAttribute->getAttribute('array', false);
30813079
if ($isArray) {
3082-
if (!$this->adapter->getSupportForIndexArray()) {
3083-
throw new IndexException('Indexing an array attribute is not supported');
3084-
}
3085-
30863080
if ($this->adapter->getMaxIndexLength() > 0) {
30873081
$lengths[$i] = self::ARRAY_INDEX_LENGTH;
30883082
}
@@ -3108,7 +3102,8 @@ public function createIndex(string $collection, string $id, string $type, array
31083102
$validator = new IndexValidator(
31093103
$collection->getAttribute('attributes', []),
31103104
$this->adapter->getMaxIndexLength(),
3111-
$this->adapter->getInternalIndexesKeys()
3105+
$this->adapter->getInternalIndexesKeys(),
3106+
$this->adapter->getSupportForIndexArray()
31123107
);
31133108
if (!$validator->isValid($index)) {
31143109
throw new IndexException($validator->getDescription());

src/Database/Validator/Index.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,20 @@ class Index extends Validator
2222
*/
2323
protected array $reservedKeys;
2424

25+
protected bool $arrayIndexSupport;
26+
2527
/**
2628
* @param array<Document> $attributes
2729
* @param int $maxLength
2830
* @param array<string> $reservedKeys
31+
* @param bool $arrayIndexSupport
2932
* @throws DatabaseException
3033
*/
31-
public function __construct(array $attributes, int $maxLength, array $reservedKeys = [])
34+
public function __construct(array $attributes, int $maxLength, array $reservedKeys = [], bool $arrayIndexSupport = false)
3235
{
3336
$this->maxLength = $maxLength;
3437
$this->reservedKeys = $reservedKeys;
38+
$this->arrayIndexSupport = $arrayIndexSupport;
3539

3640
foreach ($attributes as $attribute) {
3741
$key = \strtolower($attribute->getAttribute('key', $attribute->getAttribute('$id')));
@@ -156,6 +160,11 @@ public function checkArrayIndex(Document $index): bool
156160
$this->message = 'Invalid index order "' . $direction . '" on array attribute "'. $attribute->getAttribute('key', '') .'"';
157161
return false;
158162
}
163+
164+
if ($this->arrayIndexSupport === false) {
165+
$this->message = 'Indexing an array attribute is not supported';
166+
return false;
167+
}
159168
} elseif ($attribute->getAttribute('type') !== Database::VAR_STRING && !empty($lengths[$attributePosition])) {
160169
$this->message = 'Cannot set a length on "'. $attribute->getAttribute('type') . '" attributes';
161170
return false;

0 commit comments

Comments
 (0)