database/install/helper.go

252 lines
6.1 KiB
Go
Raw Permalink Normal View History

2024-06-17 11:46:46 +08:00
// Package install ...
//
// Description : define ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 2023-09-17 19:41
package install
import (
"fmt"
"git.zhangdeman.cn/zhangdeman/database/install/define"
"git.zhangdeman.cn/zhangdeman/wrapper"
)
var (
// Helper ...
Helper *helper
)
func init() {
Helper = &helper{}
}
// helper ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 19:42 2023/9/17
type helper struct {
}
// NewPrimaryID 主键ID
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 19:52 2023/9/17
func (h *helper) NewPrimaryID(name string, comment string) *define.Field {
return &define.Field{
Name: name,
Type: "BIGINT(20)",
Unsigned: true,
NotNull: true,
DefaultValue: wrapper.String("0").ToStringPtr().Value,
PrimaryKey: true,
IsAutoIncrement: true,
Comment: comment,
}
}
// NewBigintField 生成bigint字段
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 19:43 2023/9/17
func (h *helper) NewBigintField(name string, unsigned bool, defaultVal string, comment string) *define.Field {
return &define.Field{
Name: name,
Type: "BIGINT(20)",
Unsigned: unsigned,
NotNull: true,
DefaultValue: wrapper.String(defaultVal).ToStringPtr().Value,
PrimaryKey: false,
IsAutoIncrement: false,
Comment: comment,
}
}
// NewIntField 生成int类型
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 19:57 2023/9/17
func (h *helper) NewIntField(name string, unsigned bool, defaultVal string, comment string) *define.Field {
return &define.Field{
Name: name,
Type: "INT(11)",
Unsigned: unsigned,
NotNull: true,
DefaultValue: wrapper.String(defaultVal).ToStringPtr().Value,
PrimaryKey: false,
IsAutoIncrement: false,
Comment: comment,
}
}
// NewVarcharField varchar字段
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 19:59 2023/9/17
func (h *helper) NewVarcharField(name string, length int, defaultVal string, comment string) *define.Field {
return &define.Field{
Name: name,
Type: fmt.Sprintf("VARCHAR(%v)", length),
Unsigned: false,
NotNull: true,
DefaultValue: wrapper.String(defaultVal).ToStringPtr().Value,
PrimaryKey: false,
IsAutoIncrement: false,
Comment: comment,
}
}
// NewCharField char字段
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 20:01 2023/9/17
func (h *helper) NewCharField(name string, length int, comment string, defaultVal string) *define.Field {
return &define.Field{
Name: name,
Type: fmt.Sprintf("CHAR(%v)", length),
Unsigned: false,
NotNull: true,
DefaultValue: wrapper.String(defaultVal).ToStringPtr().Value,
PrimaryKey: false,
IsAutoIncrement: false,
Comment: comment,
}
}
// NewTextField text字段
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 14:49 2024/1/11
func (h *helper) NewTextField(name string, comment string) *define.Field {
return &define.Field{
Name: name,
Type: "TEXT",
Unsigned: false,
NotNull: false,
DefaultValue: nil,
PrimaryKey: false,
IsAutoIncrement: false,
Comment: comment,
}
}
// NewTimestampField ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 14:02 2023/9/18
func (h *helper) NewTimestampField(name string, comment string, defaultVal string) *define.Field {
return &define.Field{
Name: name,
Type: "TIMESTAMP",
Unsigned: false,
NotNull: true,
DefaultValue: wrapper.String(defaultVal).ToStringPtr().Value,
PrimaryKey: false,
IsAutoIncrement: false,
Comment: comment,
}
}
// NewTimestampFieldWithUpdate 自动更新字段
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 14:03 2023/9/18
func (h *helper) NewTimestampFieldWithUpdate(name string, comment string, defaultVal string) *define.Field {
return &define.Field{
Name: name,
Type: "TIMESTAMP",
Unsigned: false,
NotNull: true,
DefaultValue: wrapper.String(defaultVal).ToStringPtr().Value,
PrimaryKey: false,
IsAutoIncrement: false,
OnUpdate: "current_timestamp()",
Comment: comment,
}
}
// NewDefaultTable 数据表
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 14:11 2023/9/18
func (h *helper) NewDefaultTable(name string, comment string, fieldList []*define.Field, indexList []*define.Index) *define.Table {
return &define.Table{
Name: name,
Engine: define.TableEngineInnoDB,
DefaultCharset: define.TableCharsetUtf8mb4,
DefaultCollate: define.TableCollateUtf8mb4GeneralCi,
Comment: comment,
AutoIncrement: 1,
FieldList: fieldList,
IndexList: indexList,
}
}
// NewPrimaryIndex ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 16:11 2023/9/18
func (h *helper) NewPrimaryIndex(fieldName string) *define.Index {
return &define.Index{
Type: define.TableIndexPrimary,
Name: fieldName,
FieldList: []string{fieldName},
IsFormat: false,
}
}
// NewUniqueIndex 生成唯一索引
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 17:05 2023/9/18
func (h *helper) NewUniqueIndex(idxName string, fieldNameList []string) *define.Index {
return &define.Index{
Type: define.TableIndexUnique,
Name: idxName,
FieldList: fieldNameList,
IsFormat: false,
}
}
// NewKeyIndex ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 17:05 2023/9/18
func (h *helper) NewKeyIndex(idxName string, fieldNameList []string) *define.Index {
return &define.Index{
Type: define.TableIndexKey,
Name: idxName,
FieldList: fieldNameList,
IsFormat: false,
}
}
// NewIndexIndex ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 17:05 2023/9/18
func (h *helper) NewIndexIndex(idxName string, fieldNameList []string) *define.Index {
return &define.Index{
Type: define.TableIndexIndex,
Name: idxName,
FieldList: fieldNameList,
IsFormat: false,
}
}