database/install/define/table.go

186 lines
4.7 KiB
Go

// Package define ...
//
// Description : define ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 2023-09-17 11:19
package define
import (
"fmt"
"git.zhangdeman.cn/zhangdeman/consts"
"strings"
)
var (
DatabaseDriver = consts.DatabaseDriverMysql
)
// SetDatabaseDriver 设置数据库驱动
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 14:48 2024/6/17
func SetDatabaseDriver(databaseDriver string) {
DatabaseDriver = databaseDriver
}
// Table 数据表的数据结构
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 11:22 2023/9/17
type Table struct {
Name string `json:"name"` // 表名称
Engine string `json:"engine"` // 存储引擎
DefaultCharset string `json:"default_charset"` // 默认编码
DefaultCollate string `json:"default_collate"` // 默认集合
Comment string `json:"comment"` // 表注释
AutoIncrement int `json:"auto_increment"` // 自增起始值
FieldList []*Field `json:"field_list"` // 表字段列表
IndexList []*Index `json:"index_list"` // 表索引列表
isFormat bool `json:"-"` // 是否格式化过
}
// Format 格式化
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 18:14 2023/9/17
func (t *Table) Format() {
if t.isFormat {
return
}
t.isFormat = true
t.Name = strings.TrimSpace(t.Name)
t.Engine = strings.TrimSpace(t.Engine)
if len(t.Engine) == 0 {
t.Engine = "InnoDB"
}
t.DefaultCharset = strings.TrimSpace(t.DefaultCharset)
if len(t.DefaultCharset) == 0 {
t.DefaultCharset = "utf8mb4"
}
t.DefaultCollate = strings.TrimSpace(t.DefaultCollate)
if len(t.DefaultCollate) == 0 {
t.DefaultCollate = "utf8mb4_general_ci"
}
for _, item := range t.FieldList {
item.Format()
}
for _, item := range t.IndexList {
item.Format()
}
}
// Validate 数据验证
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 18:23 2023/9/17
func (t *Table) Validate() error {
t.Format()
if len(t.Name) <= 2 {
return TableNameIsEmpty
}
if len(t.FieldList) == 0 {
return TableFieldListIsEmpty
}
return nil
}
// ToString 生成sql
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 18:46 2023/9/17
func (t *Table) ToString() ([]string, error) {
tableTpl := t.getTableSqlTpl()
replaceTable := map[string]string{
TableName: t.Name, // 表名
TableFieldList: "", // 表字段
TableIndexList: "", // 表索引
TableEngine: "", // 表引擎
TableAutoIncrement: "", // 自增起点
TableDefaultCharset: "", // 默认编码
TableDefaultCollate: "", // 默认字符集
TableComment: "", // 表注释
}
fieldList := make([]string, 0)
for _, item := range t.FieldList {
fieldStr, err := item.ToString()
if nil != err {
return nil, err
}
fieldList = append(fieldList, fieldStr)
}
indexList := make([]string, 0)
for _, item := range t.IndexList {
indexStr, err := item.ToString(t.Name)
if nil != err {
return nil, err
}
if len(indexStr) == 0 {
continue
}
indexList = append(indexList, indexStr)
}
replaceTable[TableFieldList] = strings.Join(fieldList, ",")
if len(indexList) > 0 && consts.DatabaseDriverMysql == DatabaseDriver {
replaceTable[TableIndexList] = "," + strings.Join(indexList, ",")
indexList = make([]string, 0)
}
if len(t.Engine) > 0 {
replaceTable[TableEngine] = "ENGINE=" + t.Engine
}
if t.AutoIncrement > 0 {
replaceTable[TableAutoIncrement] = fmt.Sprintf("AUTO_INCREMENT=%v", t.AutoIncrement)
}
if len(t.DefaultCharset) > 0 {
replaceTable[TableDefaultCharset] = "DEFAULT CHARSET=" + t.DefaultCharset
}
if len(t.DefaultCollate) > 0 {
if len(replaceTable[TableDefaultCharset]) == 0 {
replaceTable[TableDefaultCollate] = "DEFAULT COLLATE=" + t.DefaultCollate
} else {
replaceTable[TableDefaultCollate] = "COLLATE=" + t.DefaultCollate
}
}
if len(t.Comment) > 0 {
replaceTable[TableComment] = fmt.Sprintf("COMMENT='%v'", t.Comment)
}
for source, target := range replaceTable {
tableTpl = strings.ReplaceAll(tableTpl, source, target)
}
return append([]string{tableTpl}, indexList...), nil
}
// ToStringIgnoreError ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 15:59 2023/9/18
func (t *Table) ToStringIgnoreError() []string {
sql, _ := t.ToString()
return sql
}
// getTableSqlTpl 获取表结构的模板
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 14:50 2024/6/17
func (t *Table) getTableSqlTpl() string {
switch DatabaseDriver {
case consts.DatabaseDriverMysql:
return MysqlTableTpl
case consts.DatabaseDriverSqlite3:
return SqliteTableTpl
}
panic(DatabaseDriver + " : database driver is not support")
}