Compare commits
No commits in common. "a212583f41e04b490b8c8194cb98b100759b3373" and "c97a3b3bcb76eeb006a610f91e9d0c8cb97288e6" have entirely different histories.
a212583f41
...
c97a3b3bcb
@ -1,59 +0,0 @@
|
|||||||
// Package {PACKAGE} ...
|
|
||||||
//
|
|
||||||
// Description : {PACKAGE} ...
|
|
||||||
package {PACKAGE}
|
|
||||||
|
|
||||||
import (
|
|
||||||
"gorm.io/gorm"
|
|
||||||
"errors"
|
|
||||||
)
|
|
||||||
|
|
||||||
// {DATA_STRUCT_NAME} 数据库表的数据结构
|
|
||||||
{DATA_STRUCT}
|
|
||||||
|
|
||||||
// New{DATA_STRUCT_DAO} 获取DAO实例
|
|
||||||
func New{DATA_STRUCT_DAO}() *{DATA_STRUCT_DAO} {
|
|
||||||
return &{DATA_STRUCT_DAO}{
|
|
||||||
table : "{DB_TABLE_NAME}",
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// {DATA_STRUCT_DAO} sql2go tool generate
|
|
||||||
type {DATA_STRUCT_DAO} struct {
|
|
||||||
table string
|
|
||||||
}
|
|
||||||
|
|
||||||
// GetDetailBy{PRIMARY_KEY} 根据主键ID获取详情
|
|
||||||
func ({DAO_RECEIVER} *{DATA_STRUCT_DAO}) GetDetailBy{PRIMARY_KEY}(dbInstance *gorm.DB, {PRIMARY_KEY} {PRIMARY_KEY_TYPE}) (*{DATA_STRUCT_NAME}, error) {
|
|
||||||
var (
|
|
||||||
err error
|
|
||||||
detail {DATA_STRUCT_NAME}
|
|
||||||
)
|
|
||||||
if err = dbInstance.Table({DAO_RECEIVER}.table).Where("{PRIMARY_KEY_FIELD} = ?", {PRIMARY_KEY}).Limit(1).First(&detail).Error; nil != err {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
return &detail, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// GetList 获取数据列表
|
|
||||||
func ({DAO_RECEIVER} *{DATA_STRUCT_DAO}) GetList(dbInstance *gorm.DB, condition map[string]interface{}, limit int, offset int) ([]{DATA_STRUCT_NAME}, error) {
|
|
||||||
if nil == condition {
|
|
||||||
condition = make(map[string]interface{})
|
|
||||||
}
|
|
||||||
var (
|
|
||||||
err error
|
|
||||||
list []{DATA_STRUCT_NAME}
|
|
||||||
)
|
|
||||||
if err = dbInstance.Table({DAO_RECEIVER}.table).Where(condition).Limit(limit).Offset(offset).Find(&list).Error; nil != err {
|
|
||||||
return make([]{DATA_STRUCT_NAME}, 0), err
|
|
||||||
}
|
|
||||||
return list, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// Create 创建数据
|
|
||||||
func ({DAO_RECEIVER} *{DATA_STRUCT_DAO}) Create(dbInstance *gorm.DB, data *{DATA_STRUCT_NAME}) error {
|
|
||||||
if nil == data {
|
|
||||||
return errors.New("data is nil")
|
|
||||||
}
|
|
||||||
return dbInstance.Table({DAO_RECEIVER}.table).Create(data).Error
|
|
||||||
}
|
|
@ -1,59 +0,0 @@
|
|||||||
// Package sql2go...
|
|
||||||
//
|
|
||||||
// Description : sql2go...
|
|
||||||
//
|
|
||||||
// Author : go_developer@163.com<白茶清欢>
|
|
||||||
//
|
|
||||||
// Date : 2021-11-17 10:14 上午
|
|
||||||
package sql2go
|
|
||||||
|
|
||||||
import (
|
|
||||||
"strings"
|
|
||||||
|
|
||||||
"git.zhangdeman.cn/zhangdeman/gopkg/util"
|
|
||||||
)
|
|
||||||
|
|
||||||
// GenerateDao 根据sql自动生成dao
|
|
||||||
//
|
|
||||||
// Author : go_developer@163.com<白茶清欢>
|
|
||||||
//
|
|
||||||
// Date : 10:15 上午 2021/11/17
|
|
||||||
func GenerateDao(sql string, packageName string) (string, error) {
|
|
||||||
if len(packageName) == 0 {
|
|
||||||
packageName = "dao"
|
|
||||||
}
|
|
||||||
dataStruct, basic, err := ParseCreateTableSql(sql)
|
|
||||||
if nil != err {
|
|
||||||
return "", err
|
|
||||||
}
|
|
||||||
// 读入模板
|
|
||||||
tplByte, _ := util.ReadFileContent("dao_tpl.tpl")
|
|
||||||
tpl := string(tplByte)
|
|
||||||
replaceTable := map[string]string{
|
|
||||||
"{PACKAGE}": packageName,
|
|
||||||
"{DATA_STRUCT_DAO}": basic.ModelStruct + "Dao",
|
|
||||||
"{DATA_STRUCT_NAME}": basic.ModelStruct,
|
|
||||||
"{DATA_STRUCT}": dataStruct,
|
|
||||||
"{DAO_RECEIVER}": getDaoReceiver(basic.TableName),
|
|
||||||
"{DB_TABLE_NAME}": basic.TableName,
|
|
||||||
"{PRIMARY_KEY}": "ID",
|
|
||||||
"{PRIMARY_KEY_TYPE}": basic.PrimaryFieldType,
|
|
||||||
}
|
|
||||||
|
|
||||||
for oldVal, newVal := range replaceTable {
|
|
||||||
tpl = strings.ReplaceAll(tpl, oldVal, newVal)
|
|
||||||
}
|
|
||||||
return tpl, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func getDaoReceiver(tableName string) string {
|
|
||||||
nameArr := strings.Split(tableName, "_")
|
|
||||||
result := ""
|
|
||||||
for _, item := range nameArr {
|
|
||||||
if len(item) > 0 {
|
|
||||||
result += string([]byte(item)[0])
|
|
||||||
}
|
|
||||||
}
|
|
||||||
result += "d"
|
|
||||||
return result
|
|
||||||
}
|
|
@ -20,44 +20,23 @@ const (
|
|||||||
CreateSQLColumnTPL = "{FIELD} {TYPE} `json:\"{JSON_TAG}\" gorm:\"column:{COLUMN};default:{DEFAULT_VALUE};{NOT_NULL}\"` // {COMMENT}"
|
CreateSQLColumnTPL = "{FIELD} {TYPE} `json:\"{JSON_TAG}\" gorm:\"column:{COLUMN};default:{DEFAULT_VALUE};{NOT_NULL}\"` // {COMMENT}"
|
||||||
)
|
)
|
||||||
|
|
||||||
// BasicTableInfo ...
|
|
||||||
//
|
|
||||||
// Author : go_developer@163.com<白茶清欢>
|
|
||||||
//
|
|
||||||
// Date : 11:47 上午 2021/11/17
|
|
||||||
type BasicTableInfo struct {
|
|
||||||
TableName string
|
|
||||||
ModelStruct string
|
|
||||||
PrimaryField string
|
|
||||||
PrimaryFieldType string
|
|
||||||
}
|
|
||||||
|
|
||||||
// ParseCreateTableSql 解析建表sql
|
// ParseCreateTableSql 解析建表sql
|
||||||
//
|
//
|
||||||
// Author : go_developer@163.com<白茶清欢>
|
// Author : go_developer@163.com<白茶清欢>
|
||||||
//
|
//
|
||||||
// Date : 4:49 下午 2021/10/25
|
// Date : 4:49 下午 2021/10/25
|
||||||
func ParseCreateTableSql(sql string) (string, *BasicTableInfo, error) {
|
func ParseCreateTableSql(sql string) (string, error) {
|
||||||
var (
|
var (
|
||||||
stmt sqlparser.Statement
|
stmt sqlparser.Statement
|
||||||
err error
|
err error
|
||||||
basic *BasicTableInfo
|
|
||||||
)
|
)
|
||||||
basic = &BasicTableInfo{
|
|
||||||
TableName: "",
|
|
||||||
ModelStruct: "",
|
|
||||||
PrimaryField: "ID",
|
|
||||||
PrimaryFieldType: "",
|
|
||||||
}
|
|
||||||
sql = strings.ReplaceAll(strings.ReplaceAll(sql, "CURRENT_TIMESTAMP()", "CURRENT_TIMESTAMP"), "current_timestamp()", "CURRENT_TIMESTAMP")
|
sql = strings.ReplaceAll(strings.ReplaceAll(sql, "CURRENT_TIMESTAMP()", "CURRENT_TIMESTAMP"), "current_timestamp()", "CURRENT_TIMESTAMP")
|
||||||
if stmt, err = sqlparser.ParseStrictDDL(sql); nil != err {
|
if stmt, err = sqlparser.ParseStrictDDL(sql); nil != err {
|
||||||
return "", nil, err
|
return "", err
|
||||||
}
|
}
|
||||||
|
|
||||||
r := stmt.(*sqlparser.DDL)
|
r := stmt.(*sqlparser.DDL)
|
||||||
basic.TableName = sqlparser.String(r.NewName)
|
structResult := "type " + util.SnakeCaseToCamel(sqlparser.String(r.NewName)) + " struct { \n"
|
||||||
basic.ModelStruct = util.SnakeCaseToCamel(basic.TableName)
|
|
||||||
structResult := "type " + basic.ModelStruct + " struct { \n"
|
|
||||||
|
|
||||||
for _, item := range r.TableSpec.Columns {
|
for _, item := range r.TableSpec.Columns {
|
||||||
data := map[string]string{
|
data := map[string]string{
|
||||||
@ -68,9 +47,6 @@ func ParseCreateTableSql(sql string) (string, *BasicTableInfo, error) {
|
|||||||
"{COMMENT}": item.Name.String() + " " + string(item.Type.Comment.Val),
|
"{COMMENT}": item.Name.String() + " " + string(item.Type.Comment.Val),
|
||||||
"{TYPE}": sqlTypeMap[item.Type.Type],
|
"{TYPE}": sqlTypeMap[item.Type.Type],
|
||||||
}
|
}
|
||||||
if data["{FIELD}"] == "ID" {
|
|
||||||
basic.PrimaryFieldType = data["{TYPE}"]
|
|
||||||
}
|
|
||||||
if item.Type.NotNull {
|
if item.Type.NotNull {
|
||||||
data["{NOT_NULL}"] = "NOT NULL"
|
data["{NOT_NULL}"] = "NOT NULL"
|
||||||
}
|
}
|
||||||
@ -84,5 +60,5 @@ func ParseCreateTableSql(sql string) (string, *BasicTableInfo, error) {
|
|||||||
structResult += val + "\n"
|
structResult += val + "\n"
|
||||||
}
|
}
|
||||||
structResult = structResult + "}"
|
structResult = structResult + "}"
|
||||||
return structResult, basic, nil
|
return structResult, nil
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user