go Reverse creation of table structure + native sql query and data reading + use of paradigm

Reverse create table structure

When do you need to create tables in reverse?
There are database tables first. When it is necessary to create a structure and establish an association with the database table, it is necessary to reversely generate the table structure based on the database table.
Tool library (requires installation): gormt

安装命令
go get -u https://github.com/xxjwxc/gormt

insert image description here

./gormt --help
or
./gormt -h

-------------------------------------------------------
base on gorm tools for mysql database to golang struct

Usage:
  main [flags]

Flags:
  -d, --database string   数据库名
  -f, --foreign           是否导出外键关联
  -F, --fun               是否导出函数
  -g, --gui               是否ui显示模式
  -h, --help              help for main
  -H, --host string       数据库地址.(注意-H为大写)
  -o, --outdir string     输出目录
  -p, --password string   密码.
      --port int          端口号 (default 3306)
  -s, --singular          是否禁用表名复数
  -b, --table_names string 表名称  
  -l, --url string        url标签(json,url)
  -u, --user string       用户名.

build command

// 更加详细可以参考上面的命令
go/bin/gormt -H=ip -d=database -p=pwd -u=username --port=3306 -F=false -b table_name1,table_name2...

Native sql query and data reading

// err := DB.Raw(sql, args...).Scan(&res).Error
sql := `SELECT c.name as name,SUM(d.num) as total from (SELECT id,num,time from d WHERE time BETWEEN ? AND ?) d INNER JOIN (SELECT id,name,k from c) c on d.id=c.id GROUP BY DAY(d.time),d.id ORDER BY d.time;`
err := DB.Raw(sql, todayStart, todayStop).Scan(&res).Error

use of paradigms

Not long after I first came into contact with go, I thought that I might not be able to use it, so I checked the information. The paradigm of go seems to have just come out. I really got it.
Why do you think of using paradigms?
Because of the above data query, there is a statistical requirement. Every time a table is checked to implement a function, the code is a bit bloated, because most of the code is the same, that is, the query table is different and the structure of the returned result is different. Everything else is the same. I thought of encapsulating the whole part of the code, but the structure of the returned result cannot be hard-coded, it needs to be determined according to the specific situation, then I need to use an indeterminate type parameter to deal with such a problem, and I thought of the paradigm.

// res 就是 所说的范型
func Statistics[T any](context *gin.Context, DB *gorm.DB, sql string, claim string, res []T) {
    
    
	today := time.Now().Add(-time.Hour * 24)
	todayStart := today.Format("2006-01-02") + " 00:00:00"
	todayStop := today.Format("2006-01-02") + " 23:59:59"
	err := DB.Raw(sql, todayStart, todayStop).Scan(&res).Error
	if err != nil {
    
    
		// 加一个 日志对象, 把错误信息打印到日志里。
		fmt.Println(err)
		return
	}
	b, _ := json.Marshal(res)
	bStr := string(b)
	// 把双引号去掉
	bStr = strings.ReplaceAll(bStr, "\"", "")
	msg := todayStart + " ~ " + todayStop + claim + bStr
	uitls.SendMsg(msg)
	data := gin.H{
    
    
		"resList":    res,
		"len":        len(res),
		"todayStart": todayStart,
		"todayStop":  todayStop,
	}
	response.Response(context, 200, data, "查询结果!")
}

// 调用
var res []model.MyStruct
Statistics[model.MyStruct](context, DB, sql, claim, res)

Replenish

反引号 `` 有点折磨人
在字符串拼接的时候,最好还是把双引号去掉。

Guess you like

Origin blog.csdn.net/wjl__ai__/article/details/124760408