Skip to content

Commit

Permalink
完善全文索引
Browse files Browse the repository at this point in the history
  • Loading branch information
kongyu committed Jan 31, 2024
1 parent 63a9e9e commit eee48ca
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 18 deletions.
1 change: 1 addition & 0 deletions cmd/test_yaml_to_sql/etc2/Entity.Company.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ Table:
- uuid2
fulltext_indexes:
fulltext_intro:
with_parser: simple
columns:
- intro
# - uuid2
Expand Down
2 changes: 1 addition & 1 deletion cmd/test_yaml_to_sql/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ func main() {
// fmt.Println(s)
// }
// InitConfig()
yts := tblschema.NewYamlToSqlHandler().SetYamlPath("./etc/").
yts := tblschema.NewYamlToSqlHandler().SetYamlPath("./etc2/").
SetDsn("root:tiger@(127.0.0.1:3306)/pulingfu?charset=utf8mb4&parseTime=True&loc=Local")

yts.ExecuteSchemaSafeCheck()
Expand Down
6 changes: 4 additions & 2 deletions information_schema/info.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@ type YmlIndexes struct {
// type YmlIndexes

type SqlIndexesSerialize struct {
UnqIndexes map[string]map[string][]string `json:"unique_indexes"`
Indexes map[string]map[string][]string `json:"indexes"`
UnqIndexes map[string]map[string][]string `json:"unique_indexes"`
Indexes map[string]map[string][]string `json:"indexes"`
FulltextIndexes map[string]map[string][]string `json:"fulltext_indexes"`
}

type SqlColumnsSerialize map[string]map[string]string
Expand All @@ -44,6 +45,7 @@ type SqlIndexes struct {
Key_name string `gorm:"column:Key_name"`
Seq_in_index int `gorm:"column:Seq_in_index"`
Column_name string `gorm:"column:Column_name"`
IndexType string `gorm:"column:Index_type"`
// Column_name string `gorm:"column:Column_name"`
}

Expand Down
51 changes: 36 additions & 15 deletions yaml_to_sql_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -907,25 +907,33 @@ func (ts *YamlToSqlHandler) getGetChangeTableSql(tbl gjson.Result, sqlTbl inform
var sqlIndexesSerialize information_schema.SqlIndexesSerialize
sqlIndexesSerialize.UnqIndexes = map[string]map[string][]string{}
sqlIndexesSerialize.Indexes = map[string]map[string][]string{}
sqlIndexesSerialize.FulltextIndexes = map[string]map[string][]string{}

//计算sql
for _, sqlIndex := range sqlIndexes {
if strings.ToLower(sqlIndex.Key_name) == "primary" {
continue
}
if sqlIndex.Non_unique == 0 {
// fmt.Println(sqlIndex.Key_name)
if sqlIndexesSerialize.UnqIndexes[sqlIndex.Key_name] == nil {
sqlIndexesSerialize.UnqIndexes[sqlIndex.Key_name] = map[string][]string{}
if strings.ToLower(sqlIndex.IndexType) == "fulltext" {
if sqlIndexesSerialize.FulltextIndexes[sqlIndex.Key_name] == nil {
sqlIndexesSerialize.FulltextIndexes[sqlIndex.Key_name] = map[string][]string{}
}
sqlIndexesSerialize.UnqIndexes[sqlIndex.Key_name]["columns"] = append(sqlIndexesSerialize.UnqIndexes[sqlIndex.Key_name]["columns"], sqlIndex.Column_name)
}
if sqlIndex.Non_unique == 1 {
// fmt.Println(sqlIndex.Key_name)
if sqlIndexesSerialize.Indexes[sqlIndex.Key_name] == nil {
sqlIndexesSerialize.Indexes[sqlIndex.Key_name] = map[string][]string{}
sqlIndexesSerialize.FulltextIndexes[sqlIndex.Key_name]["columns"] = append(sqlIndexesSerialize.FulltextIndexes[sqlIndex.Key_name]["columns"], sqlIndex.Column_name)
} else if strings.ToLower(sqlIndex.IndexType) == "btree" {
if sqlIndex.Non_unique == 0 {
// fmt.Println(sqlIndex.Key_name)
if sqlIndexesSerialize.UnqIndexes[sqlIndex.Key_name] == nil {
sqlIndexesSerialize.UnqIndexes[sqlIndex.Key_name] = map[string][]string{}
}
sqlIndexesSerialize.UnqIndexes[sqlIndex.Key_name]["columns"] = append(sqlIndexesSerialize.UnqIndexes[sqlIndex.Key_name]["columns"], sqlIndex.Column_name)
}
if sqlIndex.Non_unique == 1 {
// fmt.Println(sqlIndex.Key_name)
if sqlIndexesSerialize.Indexes[sqlIndex.Key_name] == nil {
sqlIndexesSerialize.Indexes[sqlIndex.Key_name] = map[string][]string{}
}
sqlIndexesSerialize.Indexes[sqlIndex.Key_name]["columns"] = append(sqlIndexesSerialize.Indexes[sqlIndex.Key_name]["columns"], sqlIndex.Column_name)
}
sqlIndexesSerialize.Indexes[sqlIndex.Key_name]["columns"] = append(sqlIndexesSerialize.Indexes[sqlIndex.Key_name]["columns"], sqlIndex.Column_name)
}
}

Expand Down Expand Up @@ -984,7 +992,9 @@ func (ts *YamlToSqlHandler) getGetChangeTableSql(tbl gjson.Result, sqlTbl inform
if gjson.Get(string(sqlIndexesJ), "fulltext_indexes").Exists() {
gjson.Get(string(sqlIndexesJ), "fulltext_indexes").ForEach(func(key, value gjson.Result) bool {
if tbl.Get("fulltext_indexes." + key.String()).Exists() {
if strings.Compare(value.String(), tbl.Get("fulltext_indexes."+key.String()).String()) == 0 {
// fmt.Println(tbl.Get("fulltext_indexes." + key.String() + ".columns").String())
// fmt.Println(tbl.Get("fulltext_indexes." + key.String() + ".with_parser").String())
if strings.Compare(value.Get("columns").String(), tbl.Get("fulltext_indexes."+key.String()+".columns").String()) == 0 {
// keepThisKey = true
// fmt.Println(">>>>>>保留==")
// fmt.Println("unqkey= ", key.String())
Expand All @@ -1002,11 +1012,16 @@ func (ts *YamlToSqlHandler) getGetChangeTableSql(tbl gjson.Result, sqlTbl inform
sqlcol = fmt.Sprintf("%s%s,", sqlcol, v)
}
sqlcol = sqlcol[:len(sqlcol)-1]
sql = fmt.Sprintf("%sCREATE FULLTEXT INDEX %s ON %s(%s);\n",
with_parser := tbl.Get("fulltext_indexes." + key.String() + ".with_parser").String()
if with_parser == "" {
with_parser = "ngram"
}
sql = fmt.Sprintf("%sCREATE FULLTEXT INDEX %s ON %s(%s) WITH PARSER %s;\n",
sql,
key.String(),
tname,
sqlcol,
with_parser,
)
// keepThisKey = false
// needReaddKey[key.String()] = true
Expand Down Expand Up @@ -1091,18 +1106,24 @@ func (ts *YamlToSqlHandler) getGetChangeTableSql(tbl gjson.Result, sqlTbl inform
return true
})
tbl.Get("fulltext_indexes").ForEach(func(key, value gjson.Result) bool {

if !gjson.Get(string(sqlIndexesJ), "fulltext_indexes."+key.String()).Exists() {
sqlcol := ""
for _, v := range value.Get("columns").Array() {
sqlcol = fmt.Sprintf("%s%s,", sqlcol, v)
}
sqlcol = sqlcol[:len(sqlcol)-1]
sql = fmt.Sprintf("%sCREATE FULLTEXT INDEX %s ON %s(%s);\n",

with_parser := tbl.Get("fulltext_indexes." + key.String() + ".with_parser").String()
if with_parser == "" {
with_parser = "ngram"
}

sql = fmt.Sprintf("%sCREATE FULLTEXT INDEX %s ON %s(%s) WITH PARSER %s;\n",
sql,
key.String(),
tname,
sqlcol,
with_parser,
)
}
return true
Expand Down

0 comments on commit eee48ca

Please sign in to comment.