database/install/define/index.go

129 lines
3.5 KiB
Go
Raw Normal View History

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