将go-pg查询转换为纯SQL [英] convert go-pg query into plain sql
本文介绍了将go-pg查询转换为纯SQL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
是否可以转换 go-pg 查询
err = db.Model(story).
Relation("Author").
Where("story.id = ?", story1.Id).
Select()
转换为普通SQL?
这将有助于调试.因此,我可以复制此简单的SQL查询并以字符串形式在psql客户端中运行.可能有某种包装吗?
It would be helpful for debugging. So I could copy this plain SQL query and run in psql client as a string. Probably there is some kind of package for this?
推荐答案
这在项目的Wiki中列出:
This is listed in the project's wiki:
如何查看该库生成的查询?
您可以这样设置查询记录器:
You can setup query logger like this:
type dbLogger struct { }
func (d dbLogger) BeforeQuery(c context.Context, q *pg.QueryEvent) (context.Context, error) {
return c, nil
}
func (d dbLogger) AfterQuery(c context.Context, q *pg.QueryEvent) (context.Context, error) {
fmt.Println(q.FormattedQuery())
return c, nil
}
db := pg.Connect(&pg.Options{...})
db.AddQueryHook(dbLogger{})
这篇关于将go-pg查询转换为纯SQL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文