Compare commits

..

No commits in common. "c990e34ba57de9c514c7a5fc1a6338d6b3228384" and "da7c75526aecdbd26ebdbf0a1bc95c561fc6ec52" have entirely different histories.

8 changed files with 40 additions and 116 deletions

View File

@ -234,11 +234,11 @@ func (c *client) GetDatabaseClient(conf *Driver, logInstance *zap.Logger) (*gorm
err error
)
if conf.DBType == consts.DatabaseDriverMysql {
if conf.DBType == DriverTypeMysql {
if instance, err = gorm.Open(mysql.Open(c.buildConnectionDSN(conf)), &gorm.Config{}); nil != err {
return nil, err
}
} else if conf.DBType == consts.DatabaseDriverSqlite3 {
} else if conf.DBType == DriverTypeSqlite3 {
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 == consts.DatabaseDriverSqlite3 {
if conf.DBType == DriverTypeSqlite3 {
// 兼容sqlite3
return conf.Host
}

View File

@ -61,6 +61,13 @@ 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-20240617073616-39e82fd033ed
git.zhangdeman.cn/zhangdeman/consts v0.0.0-20240617031827-bf96f1ebd52e
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,8 +10,6 @@ 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,10 +7,7 @@
// Date : 2023-09-17 18:07
package define
import (
"git.zhangdeman.cn/zhangdeman/consts"
"strings"
)
import "strings"
// Field 表字段结构
//
@ -114,10 +111,7 @@ func (f *Field) ToString() (string, error) {
if err := f.Validate(); nil != err {
return "", err
}
fieldTpl := f.getFieldTpl()
if f.PrimaryKey && DatabaseDriver == consts.DatabaseDriverSqlite3 {
f.Type = "INTEGER PRIMARY KEY AUTOINCREMENT"
}
fieldTpl := MysqlFieldTpl
dataReplaceTable := map[string]string{
FieldName: f.Name, // 字段名
FieldType: f.Type, // 字段类型
@ -184,18 +178,3 @@ 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,9 +8,7 @@
package define
import (
"errors"
"fmt"
"git.zhangdeman.cn/zhangdeman/consts"
"strings"
)
@ -78,13 +76,11 @@ func (i *Index) Validate() error {
// Author : go_developer@163.com<白茶清欢>
//
// Date : 17:47 2023/9/17
func (i *Index) ToString(tableName string) (string, error) {
func (i *Index) ToString() (string, error) {
if err := i.Validate(); nil != err {
return "", err
}
switch DatabaseDriver {
case consts.DatabaseDriverMysql:
// 拼接sql
switch i.Type {
case TableIndexPrimary:
@ -98,23 +94,6 @@ func (i *Index) ToString(tableName string) (string, error) {
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 ...
@ -122,7 +101,7 @@ func (i *Index) ToString(tableName string) (string, error) {
// Author : go_developer@163.com<白茶清欢>
//
// Date : 15:59 2023/9/18
func (i *Index) ToStringIgnoreError(tableName string) string {
sql, _ := i.ToString(tableName)
func (i *Index) ToStringIgnoreError() string {
sql, _ := i.ToString()
return sql
}

View File

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