129 lines
3.3 KiB
Go
129 lines
3.3 KiB
Go
// Package define ...
|
|
//
|
|
// Description : define ...
|
|
//
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
//
|
|
// Date : 2023-09-17 18:07
|
|
package define
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"git.zhangdeman.cn/zhangdeman/consts"
|
|
"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(tableName string) (string, error) {
|
|
if err := i.Validate(); nil != err {
|
|
return "", err
|
|
}
|
|
|
|
switch DatabaseDriver {
|
|
case consts.DatabaseDriverMysql:
|
|
// 拼接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
|
|
}
|
|
case consts.DatabaseDriverSqlite3:
|
|
// 拼接sql
|
|
switch i.Type {
|
|
case TableIndexPrimary:
|
|
return "", nil
|
|
// return fmt.Sprintf("ALTER TABLE %v ADD CONSTRAINT %v PRIMARY KEY AUTOINCREMENT (%v)", tableName, i.FieldList[0], i.FieldList[0]), nil
|
|
case TableIndexUnique:
|
|
return fmt.Sprintf("CREATE UNIQUE INDEX %v ON %v (%v)", i.Name, tableName, strings.Join(i.FieldList, ",")), nil
|
|
case TableIndexKey:
|
|
fallthrough
|
|
case TableIndexIndex:
|
|
return fmt.Sprintf("CREATE INDEX %v ON %v (%v)", i.Name, tableName, strings.Join(i.FieldList, ",")), nil
|
|
default:
|
|
return fmt.Sprintf("CREATE %v %v ON %v (%v)", i.Type, i.Name, tableName, strings.Join(i.FieldList, ",")), nil
|
|
}
|
|
}
|
|
return "", errors.New(DatabaseDriver + " : database driver is not support")
|
|
}
|
|
|
|
// ToStringIgnoreError ...
|
|
//
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
//
|
|
// Date : 15:59 2023/9/18
|
|
func (i *Index) ToStringIgnoreError(tableName string) string {
|
|
sql, _ := i.ToString(tableName)
|
|
return sql
|
|
}
|