Support index options

This commit is contained in:
Kijin Sung 2020-07-11 18:00:36 +09:00
parent 25373e6540
commit 4ad35bff8c
6 changed files with 25 additions and 6 deletions

View file

@ -866,9 +866,10 @@ class DB
* @param string $index_name
* @param array $columns
* @param string $type
* @param string $options
* @return \BaseObject
*/
public function addIndex(string $table_name, string $index_name, $columns, $type = ''): \BaseObject
public function addIndex(string $table_name, string $index_name, $columns, $type = '', $options = ''): \BaseObject
{
if (!is_array($columns))
{
@ -880,7 +881,7 @@ class DB
$type = 'UNIQUE';
}
$query = vsprintf("ALTER TABLE `%s` ADD %s `%s` (%s);", array(
$query = vsprintf("ALTER TABLE `%s` ADD %s `%s` (%s) %s", array(
$this->addQuotes($this->_prefix . $table_name),
ltrim($type . ' INDEX'),
$this->addQuotes($index_name),
@ -894,6 +895,7 @@ class DB
return '`' . $this->addQuotes($column_name) . '`';
}
}, $columns)),
$options,
));
$result = $this->_handle->exec($query);

View file

@ -10,4 +10,5 @@ class Index
public $name;
public $columns = array();
public $type = null;
public $options = null;
}

View file

@ -92,6 +92,10 @@ class Table
}
$idxtype = $index->type ? ($index->type . ' INDEX') : 'INDEX';
$idxdef = ' ' . $idxtype . ' `' . $index->name . '` (' . implode(', ', $idxcolumns) . ')';
if ($index->options)
{
$idxdef .= ' ' . $index->options;
}
$columns[] = $idxdef;
}

View file

@ -138,6 +138,7 @@ class DBTableParser extends BaseParser
// Load indexes.
foreach ($xml->index as $index_info)
{
// Get the index name and list of columns.
$index_info = self::_getAttributes($index_info);
$index = new DBTable\Index;
$index->name = $index_info['name'];
@ -155,6 +156,7 @@ class DBTableParser extends BaseParser
}
}
// Get the index type.
if (isset($index_info['type']) && $index_info['type'])
{
$index->type = strtoupper($index_info['type']);
@ -163,11 +165,21 @@ class DBTableParser extends BaseParser
{
$index->type = 'UNIQUE';
}
// Set attributes on indexed columns.
if (isset($table->columns[$idxcolumn]) && is_object($table->columns[$idxcolumn]))
{
$table->columns[$idxcolumn]->is_indexed = true;
$table->columns[$idxcolumn]->is_unique = $index->type === 'UNIQUE';
$table->columns[$idxcolumn]->is_unique = $index->type === 'UNIQUE' ? true : $table->columns[$idxcolumn]->is_unique;
}
// If any index options are given, also store them in the index class.
if (isset($index_info['options']) && $index_info['options'])
{
$index->options = $index_info['options'];
}
// Add the index to the column definition.
$table->indexes[$index->name] = $index;
}

View file

@ -11,7 +11,7 @@
<index name="idx_status" columns="status(6)" />
<index name="unique_list_order" column="list_order" unique="unique" />
<index name="spatial_geometry" column="geometry" type="spatial" />
<index name="fulltext_description" column="description" type="fulltext" />
<index name="fulltext_description" column="description" type="fulltext" options="WITH PARSER ngram" />
<constraint type="foreign key" column="module_srl" references="module.module_srl" ondelete="CASCADE" />
<constraint type="check" condition="list_order &lt; 0" />
</table>

View file

@ -25,7 +25,7 @@ class DBTableParserTest extends \Codeception\TestCase\Test
$this->assertEquals('UNIQUE', $table->indexes['unique_list_order']->type);
$this->assertEquals('SPATIAL', $table->indexes['spatial_geometry']->type);
$this->assertEquals('FULLTEXT', $table->indexes['fulltext_description']->type);
$this->assertNull($table->indexes['idx_comment_srl']->type);
$this->assertEquals('WITH PARSER ngram', $table->indexes['fulltext_description']->options);
$this->assertEquals(2, count($table->constraints));
$this->assertEquals('FOREIGN KEY', $table->constraints[0]->type);
@ -50,7 +50,7 @@ class DBTableParserTest extends \Codeception\TestCase\Test
$this->assertContains('INDEX `idx_status` (`status`(6)),', $sql);
$this->assertContains('UNIQUE INDEX `unique_list_order` (`list_order`),', $sql);
$this->assertContains('SPATIAL INDEX `spatial_geometry` (`geometry`),', $sql);
$this->assertContains('FULLTEXT INDEX `fulltext_description` (`description`),', $sql);
$this->assertContains('FULLTEXT INDEX `fulltext_description` (`description`) WITH PARSER ngram,', $sql);
$this->assertContains('FOREIGN KEY (`module_srl`) REFERENCES `rx_module` (`module_srl`) ON DELETE CASCADE ON UPDATE RESTRICT', $sql);
$this->assertContains('CHECK (list_order < 0)', $sql);
$this->assertContains('CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci', $sql);