迁移数据库初始化安装能力
This commit is contained in:
180
install/define/field.go
Normal file
180
install/define/field.go
Normal file
@ -0,0 +1,180 @@
|
||||
// Package define ...
|
||||
//
|
||||
// Description : define ...
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 2023-09-17 18:07
|
||||
package define
|
||||
|
||||
import "strings"
|
||||
|
||||
// Field 表字段结构
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 11:19 2023/9/17
|
||||
type Field struct {
|
||||
Name string `json:"name"` // 字段名
|
||||
Type string `json:"type"` // 字段类型
|
||||
Unsigned bool `json:"unsigned"` // 无符号
|
||||
NotNull bool `json:"not_null"` // 禁止为空
|
||||
DefaultValue *string `json:"default_value"` // 默认值
|
||||
PrimaryKey bool `json:"primary_key"` // 是否主键
|
||||
IsAutoIncrement bool `json:"is_auto_increment"` // 是否自增
|
||||
Comment string `json:"comment"` // 字段注释
|
||||
OnUpdate string `json:"on_update"` // 更新时自动更新数据
|
||||
isFormat bool `json:"-"` // 是否格式化过
|
||||
}
|
||||
|
||||
// Format 格式化
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 15:08 2023/9/17
|
||||
func (f *Field) Format() {
|
||||
if f.isFormat {
|
||||
return
|
||||
}
|
||||
f.isFormat = true
|
||||
f.Name = strings.TrimSpace(f.Name)
|
||||
if !strings.HasPrefix(f.Name, "`") {
|
||||
f.Name = "`" + f.Name
|
||||
}
|
||||
if !strings.HasSuffix(f.Name, "`") {
|
||||
f.Name = f.Name + "`"
|
||||
}
|
||||
f.OnUpdate = strings.TrimSpace(f.OnUpdate)
|
||||
f.Type = strings.ToUpper(strings.TrimSpace(f.Type))
|
||||
f.Comment = strings.TrimSpace(f.Comment)
|
||||
}
|
||||
|
||||
// IsInt 是否是数字类型, 条件 : 类型关键字包含int
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 15:09 2023/9/17
|
||||
func (f *Field) IsInt() bool {
|
||||
f.Format()
|
||||
return strings.Contains(f.Type, "INT")
|
||||
}
|
||||
|
||||
// UseAutoIncrement 是否自增
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 15:13 2023/9/17
|
||||
func (f *Field) UseAutoIncrement() bool {
|
||||
return f.IsAutoIncrement && f.IsInt()
|
||||
}
|
||||
|
||||
// UseDefaultValue 使用默认值, 条件 : 非自增 + 设置了默认值
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 15:15 2023/9/17
|
||||
func (f *Field) UseDefaultValue() bool {
|
||||
return !f.UseAutoIncrement() && !strings.Contains(strings.ToUpper(f.Type), "TEXT") && nil != f.DefaultValue
|
||||
}
|
||||
|
||||
// UseUnsigned 无符号
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 16:12 2023/9/17
|
||||
func (f *Field) UseUnsigned() bool {
|
||||
return f.Unsigned && f.IsInt()
|
||||
}
|
||||
|
||||
// Validate 验证
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 16:10 2023/9/17
|
||||
func (f *Field) Validate() error {
|
||||
if len(f.Name) <= 0 {
|
||||
return FieldNameIsEmpty
|
||||
}
|
||||
if len(f.Type) == 0 {
|
||||
return FieldTypeIsEmpty
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// ToString 转字符串
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 16:04 2023/9/17
|
||||
func (f *Field) ToString() (string, error) {
|
||||
f.Format()
|
||||
if err := f.Validate(); nil != err {
|
||||
return "", err
|
||||
}
|
||||
fieldTpl := MysqlFieldTpl
|
||||
dataReplaceTable := map[string]string{
|
||||
FieldName: f.Name, // 字段名
|
||||
FieldType: f.Type, // 字段类型
|
||||
FieldUnsigned: "", // 无符号
|
||||
FieldNotNull: "", // 不为空
|
||||
FieldAutoIncrement: "", // 是否自增
|
||||
FieldDefaultValue: "", // 默认值
|
||||
FieldOnUpdate: "", // 自动更新
|
||||
}
|
||||
|
||||
// 是否有符号, 整数且设置有符号
|
||||
if f.UseUnsigned() {
|
||||
dataReplaceTable[FieldUnsigned] = "UNSIGNED"
|
||||
}
|
||||
|
||||
// 是否为空
|
||||
if f.NotNull {
|
||||
dataReplaceTable[FieldNotNull] = "NOT NULL"
|
||||
}
|
||||
|
||||
// 是否自增
|
||||
if f.UseAutoIncrement() {
|
||||
dataReplaceTable[FieldAutoIncrement] = "AUTO_INCREMENT"
|
||||
}
|
||||
|
||||
// 默认值
|
||||
if f.UseDefaultValue() {
|
||||
defaultVal := *f.DefaultValue
|
||||
if strings.Contains(defaultVal, "()") {
|
||||
// 默认值是Mysql函数
|
||||
dataReplaceTable[FieldDefaultValue] = "DEFAULT " + defaultVal
|
||||
} else {
|
||||
// 默认值是指定的固定值
|
||||
if f.IsInt() {
|
||||
dataReplaceTable[FieldDefaultValue] = "DEFAULT " + defaultVal
|
||||
} else {
|
||||
dataReplaceTable[FieldDefaultValue] = "DEFAULT '" + defaultVal + "'"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if len(f.OnUpdate) > 0 {
|
||||
dataReplaceTable[FieldOnUpdate] = " ON UPDATE " + f.OnUpdate
|
||||
}
|
||||
|
||||
// 注释
|
||||
f.Comment = strings.TrimSpace(f.Comment)
|
||||
if len(f.Comment) > 0 {
|
||||
dataReplaceTable[FieldComment] = "COMMENT '" + f.Comment + "'"
|
||||
}
|
||||
|
||||
for source, target := range dataReplaceTable {
|
||||
fieldTpl = strings.ReplaceAll(fieldTpl, source, target)
|
||||
}
|
||||
return fieldTpl, nil
|
||||
}
|
||||
|
||||
// ToStringIgnoreError ...
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 15:59 2023/9/18
|
||||
func (f *Field) ToStringIgnoreError() string {
|
||||
sql, _ := f.ToString()
|
||||
return sql
|
||||
}
|
Reference in New Issue
Block a user