升级 install 能力, 支持sqlite

This commit is contained in:
白茶清欢 2024-06-17 16:30:41 +08:00
parent da7c75526a
commit 24aa0d3f9b
7 changed files with 64 additions and 15 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,7 @@ func (f *Field) ToString() (string, error) {
if err := f.Validate(); nil != err {
return "", err
}
fieldTpl := MysqlFieldTpl
fieldTpl := f.getFieldTpl()
dataReplaceTable := map[string]string{
FieldName: f.Name, // 字段名
FieldType: f.Type, // 字段类型
@ -178,3 +181,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

@ -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<白茶清欢>
@ -82,7 +96,7 @@ func (t *Table) Validate() error {
//
// Date : 18:46 2023/9/17
func (t *Table) ToString() (string, error) {
tableTpl := MysqlTableTpl
tableTpl := t.getTableSqlTpl()
replaceTable := map[string]string{
TableName: t.Name, // 表名
TableFieldList: "", // 表字段
@ -109,7 +123,7 @@ func (t *Table) ToString() (string, error) {
}
indexList = append(indexList, indexStr)
}
if len(indexList) == 0 {
if len(indexList) == 0 || consts.DatabaseDriverSqlite3 == DatabaseDriver {
replaceTable[TableFieldList] = strings.Join(fieldList, ",")
} else {
replaceTable[TableFieldList] = strings.Join(fieldList, ",") + ","
@ -151,3 +165,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")
}

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"