From 4ad35bff8c5b36eb54791f73136c0c30cdbf0a65 Mon Sep 17 00:00:00 2001 From: Kijin Sung Date: Sat, 11 Jul 2020 18:00:36 +0900 Subject: [PATCH] Support index options --- common/framework/db.php | 6 ++++-- common/framework/parsers/dbtable/index.php | 1 + common/framework/parsers/dbtable/table.php | 4 ++++ common/framework/parsers/dbtableparser.php | 14 +++++++++++++- tests/_data/dbtable/example.xml | 2 +- tests/unit/framework/parsers/DBTableParserTest.php | 4 ++-- 6 files changed, 25 insertions(+), 6 deletions(-) diff --git a/common/framework/db.php b/common/framework/db.php index 786f74547..45b0fffeb 100644 --- a/common/framework/db.php +++ b/common/framework/db.php @@ -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); diff --git a/common/framework/parsers/dbtable/index.php b/common/framework/parsers/dbtable/index.php index fddd75fe1..2abab2f03 100644 --- a/common/framework/parsers/dbtable/index.php +++ b/common/framework/parsers/dbtable/index.php @@ -10,4 +10,5 @@ class Index public $name; public $columns = array(); public $type = null; + public $options = null; } diff --git a/common/framework/parsers/dbtable/table.php b/common/framework/parsers/dbtable/table.php index d35183586..45dedece8 100644 --- a/common/framework/parsers/dbtable/table.php +++ b/common/framework/parsers/dbtable/table.php @@ -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; } diff --git a/common/framework/parsers/dbtableparser.php b/common/framework/parsers/dbtableparser.php index 2ed0e854f..cfd548bac 100644 --- a/common/framework/parsers/dbtableparser.php +++ b/common/framework/parsers/dbtableparser.php @@ -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; } diff --git a/tests/_data/dbtable/example.xml b/tests/_data/dbtable/example.xml index 3ac947e6b..f5e49a10f 100644 --- a/tests/_data/dbtable/example.xml +++ b/tests/_data/dbtable/example.xml @@ -11,7 +11,7 @@ - + diff --git a/tests/unit/framework/parsers/DBTableParserTest.php b/tests/unit/framework/parsers/DBTableParserTest.php index aac06c5a2..f8324b2e2 100644 --- a/tests/unit/framework/parsers/DBTableParserTest.php +++ b/tests/unit/framework/parsers/DBTableParserTest.php @@ -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);