58 Star 193 Fork 74

hidu/mysql-schema-sync

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
index.go 2.57 KB
一键复制 编辑 原始数据 按行查看 历史
hidu 提交于 2015-11-14 17:41 +08:00 . add check detail log
package internal
import (
"encoding/json"
"fmt"
"log"
"regexp"
"strings"
)
// DbIndex db index
type DbIndex struct {
IndexType indexType
Name string
SQL string
RelationTables []string //相关联的表
}
type indexType string
const (
indexTypePrimary indexType = "PRIMARY"
indexTypeIndex = "INDEX"
indexTypeForeignKey = "FOREIGN KEY"
)
func (idx *DbIndex) alterAddSQL(drop bool) string {
alterSQL := []string{}
if drop {
dropSQL := idx.alterDropSQL()
if dropSQL != "" {
alterSQL = append(alterSQL, dropSQL)
}
}
switch idx.IndexType {
case indexTypePrimary:
alterSQL = append(alterSQL, "ADD "+idx.SQL)
case indexTypeIndex, indexTypeForeignKey:
alterSQL = append(alterSQL, fmt.Sprintf("ADD %s", idx.SQL))
default:
log.Fatalln("unknow indexType", idx.IndexType)
}
return strings.Join(alterSQL, ",\n")
}
func (idx *DbIndex) String() string {
bs, _ := json.MarshalIndent(idx, " ", " ")
return string(bs)
}
func (idx *DbIndex) alterDropSQL() string {
switch idx.IndexType {
case indexTypePrimary:
return "DROP PRIMARY KEY"
case indexTypeIndex:
return fmt.Sprintf("DROP INDEX `%s`", idx.Name)
case indexTypeForeignKey:
return fmt.Sprintf("DROP FOREIGN KEY `%s`", idx.Name)
default:
log.Fatalln("unknow indexType", idx.IndexType)
}
return ""
}
func (idx *DbIndex) addRelationTable(table string) {
table = strings.TrimSpace(table)
if table != "" {
idx.RelationTables = append(idx.RelationTables, table)
}
}
//匹配索引字段
var indexReg = regexp.MustCompile(`^([A-Z]+\s)?KEY\s`)
//匹配外键
var foreignKeyReg = regexp.MustCompile("^CONSTRAINT `(.+)` FOREIGN KEY.+ REFERENCES `(.+)` ")
func parseDbIndexLine(line string) *DbIndex {
line = strings.TrimSpace(line)
idx := &DbIndex{
SQL: line,
RelationTables: []string{},
}
if strings.HasPrefix(line, "PRIMARY") {
idx.IndexType = indexTypePrimary
idx.Name = "PRIMARY KEY"
return idx
}
// UNIQUE KEY `idx_a` (`a`) USING HASH COMMENT '注释',
// FULLTEXT KEY `c` (`c`)
// PRIMARY KEY (`d`)
// KEY `idx_e` (`e`),
if indexReg.MatchString(line) {
arr := strings.Split(line, "`")
idx.IndexType = indexTypeIndex
idx.Name = arr[1]
return idx
}
//CONSTRAINT `busi_table_ibfk_1` FOREIGN KEY (`repo_id`) REFERENCES `repo_table` (`repo_id`)
foreignMatches := foreignKeyReg.FindStringSubmatch(line)
if len(foreignMatches) > 0 {
idx.IndexType = indexTypeForeignKey
idx.Name = foreignMatches[1]
idx.addRelationTable(foreignMatches[2])
return idx
}
log.Fatalln("db_index parse failed,unsupport,line:", line)
return nil
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/hidu/mysql-schema-sync.git
git@gitee.com:hidu/mysql-schema-sync.git
hidu
mysql-schema-sync
mysql-schema-sync
21680d8e8e24

搜索帮助