迁移数据库初始化安装能力
This commit is contained in:
107
install/define/index.go
Normal file
107
install/define/index.go
Normal file
@ -0,0 +1,107 @@
|
||||
// Package define ...
|
||||
//
|
||||
// Description : define ...
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 2023-09-17 18:07
|
||||
package define
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// Index 索引数据结构
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 11:39 2023/9/17
|
||||
type Index struct {
|
||||
Type string `json:"type"` // 索引类型 : unique / index / key / primary 等
|
||||
Name string `json:"name"` // 索引名称
|
||||
FieldList []string `json:"field_list"` // 索引字段列表
|
||||
IsFormat bool `json:"-"` // 是否格式化
|
||||
}
|
||||
|
||||
// Format 格式化
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 15:35 2023/9/17
|
||||
func (i *Index) Format() {
|
||||
if i.IsFormat {
|
||||
return
|
||||
}
|
||||
i.IsFormat = true
|
||||
i.Type = strings.ToUpper(strings.TrimSpace(i.Type))
|
||||
i.Name = strings.TrimSpace(i.Name)
|
||||
if !strings.HasPrefix(i.Name, "`") {
|
||||
i.Name = "`" + i.Name
|
||||
}
|
||||
if !strings.HasSuffix(i.Name, "`") {
|
||||
i.Name = i.Name + "`"
|
||||
}
|
||||
for idx := 0; idx < len(i.FieldList); idx++ {
|
||||
if !strings.HasPrefix(i.FieldList[idx], "`") {
|
||||
i.FieldList[idx] = "`" + i.FieldList[idx]
|
||||
}
|
||||
if !strings.HasSuffix(i.FieldList[idx], "`") {
|
||||
i.FieldList[idx] = i.FieldList[idx] + "`"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Validate 验证数据
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 16:17 2023/9/17
|
||||
func (i *Index) Validate() error {
|
||||
i.Format()
|
||||
if len(i.Name) <= 2 {
|
||||
return IndexNameIsEmpty
|
||||
}
|
||||
if len(i.Type) == 0 {
|
||||
return IndexTypeIsEmpty
|
||||
}
|
||||
if len(i.FieldList) == 0 {
|
||||
return IndexFieldIsEmpty
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// ToString 生成sql语句
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 17:47 2023/9/17
|
||||
func (i *Index) ToString() (string, error) {
|
||||
if err := i.Validate(); nil != err {
|
||||
return "", err
|
||||
}
|
||||
|
||||
// 拼接sql
|
||||
switch i.Type {
|
||||
case TableIndexPrimary:
|
||||
return fmt.Sprintf("PRIMARY KEY (%v)", i.FieldList[0]), nil
|
||||
case TableIndexUnique:
|
||||
return fmt.Sprintf("UNIQUE KEY %v (%v)", i.Name, strings.Join(i.FieldList, ",")), nil
|
||||
case TableIndexKey:
|
||||
return fmt.Sprintf("KEY %v (%v)", i.Name, strings.Join(i.FieldList, ",")), nil
|
||||
case TableIndexIndex:
|
||||
return fmt.Sprintf("INDEX %v (%v)", i.Name, strings.Join(i.FieldList, ",")), nil
|
||||
default:
|
||||
return fmt.Sprintf("%v %v (%v)", i.Type, i.Name, strings.Join(i.FieldList, ",")), nil
|
||||
}
|
||||
}
|
||||
|
||||
// ToStringIgnoreError ...
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 15:59 2023/9/18
|
||||
func (i *Index) ToStringIgnoreError() string {
|
||||
sql, _ := i.ToString()
|
||||
return sql
|
||||
}
|
Reference in New Issue
Block a user