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;
}