database/install/define/table.go

186 lines
4.7 KiB
Go
Raw Permalink Normal View History

2024-06-17 11:46:46 +08:00
// Package define ...
//
// Description : define ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 2023-09-17 11:19
package define
import (
"fmt"
2024-06-17 16:30:41 +08:00
"git.zhangdeman.cn/zhangdeman/consts"
2024-06-17 11:46:46 +08:00
"strings"
)
2024-06-17 16:30:41 +08:00
var (
DatabaseDriver = consts.DatabaseDriverMysql
)
// SetDatabaseDriver 设置数据库驱动
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 14:48 2024/6/17
func SetDatabaseDriver(databaseDriver string) {
DatabaseDriver = databaseDriver
}
2024-06-17 11:46:46 +08:00
// 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
2024-06-17 17:50:38 +08:00
func (t *Table) ToString() ([]string, error) {
2024-06-17 16:30:41 +08:00
tableTpl := t.getTableSqlTpl()
2024-06-17 11:46:46 +08:00
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 {
2024-06-17 17:50:38 +08:00
return nil, err
2024-06-17 11:46:46 +08:00
}
fieldList = append(fieldList, fieldStr)
}
indexList := make([]string, 0)
for _, item := range t.IndexList {
2024-06-17 17:50:38 +08:00
indexStr, err := item.ToString(t.Name)
2024-06-17 11:46:46 +08:00
if nil != err {
2024-06-17 17:50:38 +08:00
return nil, err
}
if len(indexStr) == 0 {
continue
2024-06-17 11:46:46 +08:00
}
indexList = append(indexList, indexStr)
}
2024-06-17 17:50:38 +08:00
replaceTable[TableFieldList] = strings.Join(fieldList, ",")
2024-06-17 18:15:38 +08:00
if len(indexList) > 0 && consts.DatabaseDriverMysql == DatabaseDriver {
replaceTable[TableIndexList] = "," + strings.Join(indexList, ",")
indexList = make([]string, 0)
}
2024-06-17 11:46:46 +08:00
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)
}
2024-06-17 17:50:38 +08:00
return append([]string{tableTpl}, indexList...), nil
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 (t *Table) ToStringIgnoreError() []string {
2024-06-17 11:46:46 +08:00
sql, _ := t.ToString()
return sql
}
2024-06-17 16:30:41 +08:00
// 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")
}