GORM增加sqlcommenter特性

什么是sqlcommenter? sqlcommenter is a suite of middlewares/plugins that enable your ORMs to augment SQL statements before execution, with comments containing information about the code that caused its execution. This helps in easily correlating slow performance with source code and giving insights into backend database performance. In short it provides some observability into the state of your client-side applications and their impact on the database’s server-side. ...

2024-04-18 · 2 min · 742 words · Liudon

源码分析:GORM是如何生成sql的

在gorm下实现sqlcommenter过程中,遇到一些问题,顺便把gorm整个流程梳理了一遍,整理记录一下。 gorm使用示例 package main import ( "gorm.io/driver/mysql" "gorm.io/gorm" ) type Product struct { gorm.Model Code string Price uint } func main() { // 参考 https://github.com/go-sql-driver/mysql#dsn-data-source-name 获取详情 dsn := "user:pass@tcp(127.0.0.1:3306)/dbname?charset=utf8mb4&parseTime=True&loc=Local" db, err := gorm.Open(mysql.Open(dsn), &gorm.Config{}) var product Product db.First(&product, 1) // 根据整型主键查找 } 我们以First查询为例,看一下是怎么转成具体sql的。 ...

2024-04-18 · 6 min · 2737 words · Liudon