buildIndexDeclare

@param string $name
@param array  $columns
@param string $table

@return string
186533
public static function buildIndexDeclare($name, $columns, $table = null)
    {
        $query = static::getQuery();
        $cols = [];

        foreach ((array) $columns as $key => $val) {
            $col = is_numeric($key) ? $val : $key;

            $cols[] = $query->quoteName(trim(explode('(', $col)[0]));
        }

        $cols = '(' . implode(', ', $cols) . ')';

        return static::build(
            $name ? $query->quoteName($name) : null,
            $table ? 'ON ' . $query->quoteName($table) : null,
            $cols
        );
    }
905411
public function unique($columns, $name = null, $options = [])
    {
        $columns = is_array($columns) ? $columns : [$columns];

        return $this->table->addUniqueIndex($columns, $name, $options);
    }
544022
public function getColumn($name)
    {
        if (!$this->hasColumn($name)) {
            throw SchemaException::tableColumnDoesNotExist($this->getName(), $name);
        }

        return $this->columns[$name];
    }
370171
protected function getDropColumnSql($table, $name)
	{
		$sql = 'ALTER TABLE ' . $this->db->quoteName($table) . ' DROP COLUMN ' . $this->db->quoteName($name);

		return $sql;
	}
550227
protected function resolveTableNames($table,$name)
	{
		$parts=explode('.',str_replace('`','',$name));
		if(isset($parts[1]))
		{
			$table->schemaName=$parts[0];
			$table->name=$parts[1];
			$table->rawName=$this->quoteTableName($table->schemaName).'.'.$this->quoteTableName($table->name);
		}
		else
		{
			$table->name=$parts[0];
			$table->rawName=$this->quoteTableName($table->name);
		}
	}
330988
public function index(string $name = ''): array
    {
        //获取原始索引信息
        $indexes = $this->getIndex($name);
        $ret = [];

        //逐个处理
        foreach ($indexes as $index) {
            //索引名称
            $name = $index['Key_name'];

            //列名
            $col = $index['Column_name'];

            //列信息
            $columnInfo = [
                'name' => $col,
                'null' => $index['Null'] ? true : false,//列中是否含有NULL
                'subPart' => $index['Sub_part'],//如果列只是被部分地编入索引，则为被编入索引的字符的数目。如果整列被编入索引，则为NULL
            ];

            //索引中的第一列,则创建
            if ($index['Seq_in_index'] == 1) {
                $ret[$name] = [
                    'tableName' => $index['Table'], //所属表名
                    'isUnique' => !$index['Non_unique'], //是否唯一索引
                    'collation' => $index['Collation'], //这个有A和NULL, A是不区分大小写?
                    'columns' => [$col => $columnInfo],//本索引中包含的列
                    'cardinality' => $index['Cardinality'], //索引中唯一值的数目的估计值。
                    'packed' => $index['Packed'],//指示关键字如何被压缩。如果没有被压缩，则为NULL。
                    'type' => $index['Index_type'],//用过的索引方法（BTREE, FULLTEXT, HASH, RTREE）。
                    'comment' => $index['Comment'],//说明
                ];
            } else {
                //索引中的后继列,则只增加列信息
                $ret[$name]['columns'][$col] = $columnInfo;
            }
        }

        return $ret;
    }
1286784
public static boolean columnExist(CSTable table, String name) {
        for (int i = 1; i <= table.getColumnCount(); i++) {
            if (table.getColumnName(i).startsWith(name)) {
                return true;
            }
        }
        return false;
    }
143700
public function key($type, $columns, $name)
		{
			$parameters = array('name' => $name, 'columns' => (array) $columns);

			return $this->command($type, $parameters);
		}
708385
protected function resolveTableNames($table,$name)
	{
		$parts=explode('.',str_replace('"','',$name));
		if(isset($parts[1]))
		{
			$schemaName=$parts[0];
			$tableName=$parts[1];
		}
		else
		{
			$schemaName=self::DEFAULT_SCHEMA;
			$tableName=$parts[0];
		}

		$table->name=$tableName;
		$table->schemaName=$schemaName;
		if($schemaName===self::DEFAULT_SCHEMA)
			$table->rawName=$this->quoteTableName($tableName);
		else
			$table->rawName=$this->quoteTableName($schemaName).'.'.$this->quoteTableName($tableName);
	}
185683
public function getColumnDeclarationSQL($name, array $field)
    {
        if (isset($field['columnDefinition'])) {
            $columnDef = $this->getCustomTypeDeclarationSQL($field);
        } else {
            $collation = isset($field['collation']) && $field['collation'] ?
                ' ' . $this->getColumnCollationDeclarationSQL($field['collation']) : '';

            $notnull = isset($field['notnull']) && $field['notnull'] ? ' NOT NULL' : '';

            $unique = isset($field['unique']) && $field['unique'] ?
                ' ' . $this->getUniqueFieldDeclarationSQL() : '';

            $check = isset($field['check']) && $field['check'] ?
                ' ' . $field['check'] : '';

            $typeDecl  = $field['type']->getSQLDeclaration($field, $this);
            $columnDef = $typeDecl . $collation . $notnull . $unique . $check;
        }

        return $name . ' ' . $columnDef;
    }
837651
protected function getReferencingField($table, $name)
    {
        $references = $this->getReferences();
        $rf = null;
        foreach ($references as $item) {
            if ($item->refTable === $table && $table . '_by_' . implode('_', $item->refField) === $name) {
                $rf = $item->refField[0];
                break;
            }
        }

        return $rf;
    }
397432
public function index($columns, $name = null, $algorithm = null)
    {
        $indexName = $name ?? md5(uniqid());

        return $this->indexCommand('index', $columns, $indexName, $algorithm);
    }
158122
public function addColumnText($key, $name, $column = null)
	{
		$this->addColumnCheck($key);
		$column = $column ?: $key;

		return $this->addColumn($key, new Column\ColumnText($this, $key, $column, $name));
	}
526047
final public function findIndexColumnByName(string $name): ? IndexColumn
    {
        /** @var IndexColumn $indexColumn */
        foreach ($this->columns as $indexColumn) {
            $column = $indexColumn->getColumn();
            if ($column::getName() === $name) {
                return $indexColumn;
            }
        }

        return null;
    }
13972
public function unique($columns, $name = null, $algorithm = null)
    {
        return $this->indexCommand('unique', $columns, $name, $algorithm);
    }
827046
protected function assembleIndexKey($index_cols,$table_name) {
		if (!is_array($index_cols))
			$index_cols = array($index_cols);
		$name = $table_name.'___'.implode('__', $index_cols);
		if (strlen($name)>64)
			$name=$table_name.'___'.\Base::instance()->hash(implode('__', $index_cols));
		if (strlen($name)>64)
			$name='___'.\Base::instance()->hash($table_name.'___'.implode('__', $index_cols));
		return $name;
	}
501713
private function updateTable(array $columns, Table &$table) {
        $this->updateReferences($table);
        $canUpdate = false;
        foreach ($columns['remove'] as $columnName) {
            $canUpdate = true;
            $table->dropColumn(Util::camelTo_($columnName));
        }

        foreach ($columns['update'] as $columnName => &$desc) {
            $canUpdate = true;
            $dbColumnName = Util::camelTo_($columnName);

            $this->checkIndexes($dbColumnName, $desc, $table);

            if (isset($desc['attrs']['reference'])) {
                $onDelete = (isset($desc['attrs']['reference']['onDelete'])) ?
                        $desc['attrs']['reference']['onDelete'] : 'RESTRICT';
                $onUpdate = (isset($desc['attrs']['reference']['onUpdate'])) ?
                        $desc['attrs']['reference']['onUpdate'] : 'RESTRICT';
                $table->alterReference($dbColumnName, $desc['attrs']['reference']['table'], $desc['attrs']['reference']['column'], $onDelete, $onUpdate);
                unset($desc['attrs']['reference']['table']);
                unset($desc['attrs']['reference']['column']);
                unset($desc['attrs']['reference']['model']);

                if (isset($desc['attrs']['reference']['onUpdate']))
                    unset($desc['attrs']['reference']['onUpdate']);
                if (isset($desc['attrs']['reference']['onDelete']))
                    unset($desc['attrs']['reference']['onDelete']);

                if (!empty($desc['attrs']['reference'])) {
                    $desc['attrs'] = array_merge($desc['attrs'], $desc['attrs']['reference']);
                    unset($desc['attrs']['reference']);
                }
            }

            $table->alterColumn($dbColumnName, $this->parseAttributes($columnName, $desc, false));

            if (isset($desc['attrs']['primary']) && $desc['attrs']['primary'])
                $table->setPrimaryKey($dbColumnName);
        }

        foreach ($columns['new'] as $columnName => $desc) {
            $canUpdate = true;
            $dbColumnName = Util::camelTo_($columnName);

            if (isset($desc['attrs']['reference'])) {
                $onDelete = (isset($desc['attrs']['reference']['onDelete'])) ?
                        $desc['attrs']['reference']['onDelete'] : 'RESTRICT';
                $onUpdate = (isset($desc['attrs']['reference']['onUpdate'])) ?
                        $desc['attrs']['reference']['onUpdate'] : 'RESTRICT';
                $table->addReference($dbColumnName, $desc['attrs']['reference']['table'], $desc['attrs']['reference']['column'], $onDelete, $onUpdate);
                unset($desc['attrs']['reference']['table']);
                unset($desc['attrs']['reference']['column']);
                unset($desc['attrs']['reference']['model']);

                if (isset($desc['attrs']['reference']['onUpdate']))
                    unset($desc['attrs']['reference']['onUpdate']);
                if (isset($desc['attrs']['reference']['onDelete']))
                    unset($desc['attrs']['reference']['onDelete']);

                if (!empty($desc['attrs']['reference'])) {
                    $desc['attrs'] = array_merge($desc['attrs'], $desc['attrs']['reference']);
                    unset($desc['attrs']['reference']);
                }
            }

            $table->addColumn($dbColumnName, $this->parseAttributes($columnName, $desc, false));

            $this->checkIndexes($dbColumnName, $desc, $table);

            if (isset($desc['attrs']['primary']) && $desc['attrs']['primary'])
                $table->setPrimaryKey($dbColumnName);
        }

        if ($canUpdate) {
            $droppedRefs = array();
            foreach ($table->getBackReferences() as $refArray) {
                foreach ($refArray as $ref) {
                    $refTable = new Table($ref['refTable'], $table->getConnection());
                    foreach ($refTable->getReferences() as $col => $array) {
                        if ($refTable->getName() !== $table->getName() && $array['refTable'] === $table->getName()) {
                            $refTable->dropReference($col);
                            $droppedRefs[$col][] = array_merge(array(
                                'table' => $refTable,
                                    ), $array);

                            $table->getConnection()->alterTable($refTable);
                        }
                    }
                }
            }
            $return = $table->getConnection()->alterTable($table);

            foreach ($droppedRefs as $col => $array) {
                foreach ($array as $ref) {
                    $onDelete = (!empty($ref['onDelete'])) ? $ref['onDelete'] : 'RESTRICT';
                    $onUpdate = (!empty($ref['onUpdate'])) ? $ref['onUpdate'] : 'RESTRICT';
                    $ref['table']->addReference($col, $ref['refTable'], $ref['refColumn'], $onDelete, $onUpdate);

                    $table->getConnection()->alterTable($ref['table']);
                }
            }
            if ($return) {
                $table->getConnection()->flush();
                return true;
            }
        }

        return false;
    }