Skip to content

Commit ecc143f

Browse files
authored
Merge pull request #103 from utopia-php/fix-index-id-escape
Escape index id properly
2 parents 2f2527b + 971ac20 commit ecc143f

3 files changed

Lines changed: 6 additions & 3 deletions

File tree

src/Database/Adapter/MariaDB.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1191,7 +1191,7 @@ protected function getSQLIndex(string $collection, string $id, string $type, ar
11911191
break;
11921192
}
11931193

1194-
return 'CREATE '.$type.' '.$id.' ON `'.$this->getDefaultDatabase().'`.`'.$this->getNamespace().'_'.$collection.'` ( '.implode(', ', $attributes).' );';
1194+
return 'CREATE '.$type.' `'.$id.'` ON `'.$this->getDefaultDatabase().'`.`'.$this->getNamespace().'_'.$collection.'` ( '.implode(', ', $attributes).' );';
11951195
}
11961196

11971197
/**

src/Database/Adapter/MySQL.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ protected function getSQLIndex(string $collection, string $id, string $type, arr
9999
break;
100100
}
101101

102-
return 'CREATE '.$type.' '.$id.' ON `'.$this->getDefaultDatabase().'`.`'.$this->getNamespace().'_'.$collection.'` ( '.implode(', ', $attributes).' );';
102+
return 'CREATE '.$type.' `'.$id.'` ON `'.$this->getDefaultDatabase().'`.`'.$this->getNamespace().'_'.$collection.'` ( '.implode(', ', $attributes).' );';
103103
}
104104

105105
/**

tests/Database/Base.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,7 @@ public function testCreateDeleteIndex()
240240
static::getDatabase()->createCollection('indexes');
241241

242242
$this->assertEquals(true, static::getDatabase()->createAttribute('indexes', 'string', Database::VAR_STRING, 128, true));
243+
$this->assertEquals(true, static::getDatabase()->createAttribute('indexes', 'order', Database::VAR_STRING, 128, true));
243244
$this->assertEquals(true, static::getDatabase()->createAttribute('indexes', 'integer', Database::VAR_INTEGER, 0, true));
244245
$this->assertEquals(true, static::getDatabase()->createAttribute('indexes', 'float', Database::VAR_FLOAT, 0, true));
245246
$this->assertEquals(true, static::getDatabase()->createAttribute('indexes', 'boolean', Database::VAR_BOOLEAN, 0, true));
@@ -249,15 +250,17 @@ public function testCreateDeleteIndex()
249250
$this->assertEquals(true, static::getDatabase()->createIndex('indexes', 'index2', Database::INDEX_KEY, ['float', 'integer'], [], [Database::ORDER_ASC, Database::ORDER_DESC]));
250251
$this->assertEquals(true, static::getDatabase()->createIndex('indexes', 'index3', Database::INDEX_KEY, ['integer', 'boolean'], [], [Database::ORDER_ASC, Database::ORDER_DESC, Database::ORDER_DESC]));
251252
$this->assertEquals(true, static::getDatabase()->createIndex('indexes', 'index4', Database::INDEX_UNIQUE, ['string'], [128], [Database::ORDER_ASC]));
253+
$this->assertEquals(true, static::getDatabase()->createIndex('indexes', 'order', Database::INDEX_UNIQUE, ['order'], [128], [Database::ORDER_ASC]));
252254

253255
$collection = static::getDatabase()->getCollection('indexes');
254-
$this->assertCount(4, $collection->getAttribute('indexes'));
256+
$this->assertCount(5, $collection->getAttribute('indexes'));
255257

256258
// Delete Indexes
257259
$this->assertEquals(true, static::getDatabase()->deleteIndex('indexes', 'index1'));
258260
$this->assertEquals(true, static::getDatabase()->deleteIndex('indexes', 'index2'));
259261
$this->assertEquals(true, static::getDatabase()->deleteIndex('indexes', 'index3'));
260262
$this->assertEquals(true, static::getDatabase()->deleteIndex('indexes', 'index4'));
263+
$this->assertEquals(true, static::getDatabase()->deleteIndex('indexes', 'order'));
261264

262265
$collection = static::getDatabase()->getCollection('indexes');
263266
$this->assertCount(0, $collection->getAttribute('indexes'));

0 commit comments

Comments
 (0)