Merge pull request '支持生成sqlite建表语句' (#4) from feture/support_sqlite into master

Reviewed-on: #4
This commit is contained in:
白茶清欢 2024-06-17 18:16:09 +08:00
commit c990e34ba5
8 changed files with 116 additions and 40 deletions

View File

@ -234,11 +234,11 @@ func (c *client) GetDatabaseClient(conf *Driver, logInstance *zap.Logger) (*gorm
err error
)
if conf.DBType == DriverTypeMysql {
if conf.DBType == consts.DatabaseDriverMysql {
if instance, err = gorm.Open(mysql.Open(c.buildConnectionDSN(conf)), &gorm.Config{}); nil != err {
return nil, err
}
} else if conf.DBType == DriverTypeSqlite3 {
} else if conf.DBType == consts.DatabaseDriverSqlite3 {
if instance, err = gorm.Open(sqlite.Open(c.buildConnectionDSN(conf)), &gorm.Config{}); nil != err {
return nil, err
}
@ -257,7 +257,7 @@ func (c *client) GetDatabaseClient(conf *Driver, logInstance *zap.Logger) (*gorm
//
// Date : 18:42 2022/6/11
func (c *client) buildConnectionDSN(conf *Driver) string {
if conf.DBType == DriverTypeSqlite3 {
if conf.DBType == consts.DatabaseDriverSqlite3 {
// 兼容sqlite3
return conf.Host
}

View File

@ -61,13 +61,6 @@ type Driver struct {
Timezone string `json:"timezone" yaml:"timezone"` // 时区
}
const (
// DriverTypeMysql mysql 驱动
DriverTypeMysql = "mysql"
// DriverTypeSqlite3 sqlite3
DriverTypeSqlite3 = "sqlite3"
)
// Connection 连接数配置
//
// Author : go_developer@163.com<白茶清欢>

2
go.mod
View File

@ -5,7 +5,7 @@ go 1.21
toolchain go1.21.3
require (
git.zhangdeman.cn/zhangdeman/consts v0.0.0-20240617031827-bf96f1ebd52e
git.zhangdeman.cn/zhangdeman/consts v0.0.0-20240617073616-39e82fd033ed
git.zhangdeman.cn/zhangdeman/logger v0.0.0-20240308024848-54993a0ed4a6
git.zhangdeman.cn/zhangdeman/op_type v0.0.0-20240122104027-4928421213c0
git.zhangdeman.cn/zhangdeman/serialize v0.0.0-20240325080031-1f58204e8687

2
go.sum
View File

@ -10,6 +10,8 @@ git.zhangdeman.cn/zhangdeman/consts v0.0.0-20240612081722-31c64d4d4ce7 h1:QR8vMX
git.zhangdeman.cn/zhangdeman/consts v0.0.0-20240612081722-31c64d4d4ce7/go.mod h1:IXXaZkb7vGzGnGM5RRWrASAuwrVSNxuoe0DmeXx5g6k=
git.zhangdeman.cn/zhangdeman/consts v0.0.0-20240617031827-bf96f1ebd52e h1:hgOeedMt/VC62PQkGbzzDpVGcYF7LifYRq7swFzSdHQ=
git.zhangdeman.cn/zhangdeman/consts v0.0.0-20240617031827-bf96f1ebd52e/go.mod h1:IXXaZkb7vGzGnGM5RRWrASAuwrVSNxuoe0DmeXx5g6k=
git.zhangdeman.cn/zhangdeman/consts v0.0.0-20240617073616-39e82fd033ed h1:BGv+y6ZdtkxI4HeSIHun0QRrIzjLnUht0bKdyO2t6n4=
git.zhangdeman.cn/zhangdeman/consts v0.0.0-20240617073616-39e82fd033ed/go.mod h1:IXXaZkb7vGzGnGM5RRWrASAuwrVSNxuoe0DmeXx5g6k=
git.zhangdeman.cn/zhangdeman/easymap v0.0.0-20240311030808-e2a2e6a3c211 h1:I/wOsRpCSRkU9vo1u703slQsmK0wnNeZzsWQOGtIAG0=
git.zhangdeman.cn/zhangdeman/easymap v0.0.0-20240311030808-e2a2e6a3c211/go.mod h1:SrtvrQRdzt+8KfYzvosH++gWxo2ShPTzR1m3VQ6uX7U=
git.zhangdeman.cn/zhangdeman/gin v0.0.0-20240315025001-0ffe074179ad h1:7gjk79UkYOo7AvEFXSZoHeobahGZjltOCLdpFe5Wy80=

View File

@ -7,7 +7,10 @@
// Date : 2023-09-17 18:07
package define
import "strings"
import (
"git.zhangdeman.cn/zhangdeman/consts"
"strings"
)
// Field 表字段结构
//
@ -111,7 +114,10 @@ func (f *Field) ToString() (string, error) {
if err := f.Validate(); nil != err {
return "", err
}
fieldTpl := MysqlFieldTpl
fieldTpl := f.getFieldTpl()
if f.PrimaryKey && DatabaseDriver == consts.DatabaseDriverSqlite3 {
f.Type = "INTEGER PRIMARY KEY AUTOINCREMENT"
}
dataReplaceTable := map[string]string{
FieldName: f.Name, // 字段名
FieldType: f.Type, // 字段类型
@ -178,3 +184,18 @@ func (f *Field) ToStringIgnoreError() string {
sql, _ := f.ToString()
return sql
}
// getFieldTpl 表字段模板
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 15:14 2024/6/17
func (f *Field) getFieldTpl() string {
switch DatabaseDriver {
case consts.DatabaseDriverMysql:
return MysqlFieldTpl
case consts.DatabaseDriverSqlite3:
return SqliteFieldTpl
}
panic(DatabaseDriver + " : database driver is not support")
}

View File

@ -8,7 +8,9 @@
package define
import (
"errors"
"fmt"
"git.zhangdeman.cn/zhangdeman/consts"
"strings"
)
@ -76,24 +78,43 @@ func (i *Index) Validate() error {
// Author : go_developer@163.com<白茶清欢>
//
// Date : 17:47 2023/9/17
func (i *Index) ToString() (string, error) {
func (i *Index) ToString(tableName string) (string, error) {
if err := i.Validate(); nil != err {
return "", err
}
// 拼接sql
switch i.Type {
case TableIndexPrimary:
return fmt.Sprintf("PRIMARY KEY (%v)", i.FieldList[0]), nil
case TableIndexUnique:
return fmt.Sprintf("UNIQUE KEY %v (%v)", i.Name, strings.Join(i.FieldList, ",")), nil
case TableIndexKey:
return fmt.Sprintf("KEY %v (%v)", i.Name, strings.Join(i.FieldList, ",")), nil
case TableIndexIndex:
return fmt.Sprintf("INDEX %v (%v)", i.Name, strings.Join(i.FieldList, ",")), nil
default:
return fmt.Sprintf("%v %v (%v)", i.Type, i.Name, strings.Join(i.FieldList, ",")), nil
switch DatabaseDriver {
case consts.DatabaseDriverMysql:
// 拼接sql
switch i.Type {
case TableIndexPrimary:
return fmt.Sprintf("PRIMARY KEY (%v)", i.FieldList[0]), nil
case TableIndexUnique:
return fmt.Sprintf("UNIQUE KEY %v (%v)", i.Name, strings.Join(i.FieldList, ",")), nil
case TableIndexKey:
return fmt.Sprintf("KEY %v (%v)", i.Name, strings.Join(i.FieldList, ",")), nil
case TableIndexIndex:
return fmt.Sprintf("INDEX %v (%v)", i.Name, strings.Join(i.FieldList, ",")), nil
default:
return fmt.Sprintf("%v %v (%v)", 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
}
}
return "", errors.New(DatabaseDriver + " : database driver is not support")
}
// ToStringIgnoreError ...
@ -101,7 +122,7 @@ func (i *Index) ToString() (string, error) {
// Author : go_developer@163.com<白茶清欢>
//
// Date : 15:59 2023/9/18
func (i *Index) ToStringIgnoreError() string {
sql, _ := i.ToString()
func (i *Index) ToStringIgnoreError(tableName string) string {
sql, _ := i.ToString(tableName)
return sql
}

View File

@ -9,9 +9,23 @@ 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<白茶清欢>
@ -81,8 +95,8 @@ func (t *Table) Validate() error {
// Author : go_developer@163.com<白茶清欢>
//
// Date : 18:46 2023/9/17
func (t *Table) ToString() (string, error) {
tableTpl := MysqlTableTpl
func (t *Table) ToString() ([]string, error) {
tableTpl := t.getTableSqlTpl()
replaceTable := map[string]string{
TableName: t.Name, // 表名
TableFieldList: "", // 表字段
@ -97,24 +111,26 @@ func (t *Table) ToString() (string, error) {
for _, item := range t.FieldList {
fieldStr, err := item.ToString()
if nil != err {
return "", err
return nil, err
}
fieldList = append(fieldList, fieldStr)
}
indexList := make([]string, 0)
for _, item := range t.IndexList {
indexStr, err := item.ToString()
indexStr, err := item.ToString(t.Name)
if nil != err {
return "", err
return nil, err
}
if len(indexStr) == 0 {
continue
}
indexList = append(indexList, indexStr)
}
if len(indexList) == 0 {
replaceTable[TableFieldList] = strings.Join(fieldList, ",")
} else {
replaceTable[TableFieldList] = strings.Join(fieldList, ",") + ","
replaceTable[TableFieldList] = strings.Join(fieldList, ",")
if len(indexList) > 0 && consts.DatabaseDriverMysql == DatabaseDriver {
replaceTable[TableIndexList] = "," + strings.Join(indexList, ",")
indexList = make([]string, 0)
}
replaceTable[TableIndexList] = strings.Join(indexList, ",")
if len(t.Engine) > 0 {
replaceTable[TableEngine] = "ENGINE=" + t.Engine
@ -139,7 +155,8 @@ func (t *Table) ToString() (string, error) {
for source, target := range replaceTable {
tableTpl = strings.ReplaceAll(tableTpl, source, target)
}
return tableTpl, nil
return append([]string{tableTpl}, indexList...), nil
}
// ToStringIgnoreError ...
@ -147,7 +164,22 @@ func (t *Table) ToString() (string, error) {
// Author : go_developer@163.com<白茶清欢>
//
// Date : 15:59 2023/9/18
func (t *Table) ToStringIgnoreError() string {
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")
}

View File

@ -61,6 +61,13 @@ var (
MysqlTableTpl = fmt.Sprintf("CREATE TABLE %v (%v %v) %v %v %v %v %v;", TableName, TableFieldList, TableIndexList, TableEngine, TableAutoIncrement, TableDefaultCharset, TableDefaultCollate, TableComment)
)
var (
// SqliteFieldTpl sqlite 表字段模板
SqliteFieldTpl = fmt.Sprintf("%v %v", FieldName, FieldType)
// SqliteTableTpl 创建表sql语句
SqliteTableTpl = fmt.Sprintf("CREATE TABLE %v (%v);", TableName, TableFieldList)
)
const (
// TableIndexPrimary 主键索引
TableIndexPrimary = "PRIMARY"