[英]Multiple queries to Postgres within the same function
I'm new to Go, so sorry for the silly question in advance!我是Go新手,很抱歉提前提出愚蠢的问题!
I'm using Gin framework and want to make multiple queries to the database within the same handler ( database/sql
+ lib/pq
)我正在使用 Gin 框架并希望在同一个处理程序中对数据库进行多个查询(
database/sql
+ lib/pq
)
userIds := []int{}
bookIds := []int{}
var id int
/* Handling first query here */
rows, err := pgClient.Query(getUserIdsQuery)
defer rows.Close()
if err != nil {
return
}
for rows.Next() {
err := rows.Scan(&id)
if err != nil {
return
}
userIds = append(userIds, id)
}
/* Handling second query here */
rows, err = pgClient.Query(getBookIdsQuery)
defer rows.Close()
if err != nil {
return
}
for rows.Next() {
err := rows.Scan(&id)
if err != nil {
return
}
bookIds = append(bookIds, id)
}
I have a couple of questions regarding this code (any improvements and best practices would be appreciated)我对此代码有几个问题(任何改进和最佳实践将不胜感激)
Does Go properly handle defer rows.Close()
in such a case?在这种情况下,Go 是否正确处理
defer rows.Close()
? I mean I have reassignment of rows
variable later down the code, so will compiler track both and properly close at the end of a function?我的意思是我稍后在代码中重新分配了
rows
变量,所以编译器会跟踪两者并在函数结束时正确关闭吗?
Is it ok to reuse id
shared var or should I redeclare it while iterating within rows.Next()
loop?可以重用
id
shared var 还是应该在rows.Next()
循环中迭代时重新声明它?
What's the better approach of having even more queries within one handler?在一个处理程序中拥有更多查询的更好方法是什么? Should I have some kind of
Writer
that accepts query and slice and populate it with ids retrieved?我应该有某种
Writer
接受查询和切片并用检索到的 id 填充它吗?
Thanks.谢谢。
I've never worked with go-pg
library, and my answer is mostly focused on the other stuff, which are generic, and are not specific to golang or go-pg.我从来没有使用过
go-pg
库,我的回答主要集中在其他东西上,它们是通用的,并不特定于 golang 或 go-pg。
rows
here has the same reference while being shared between 2 queries (so one rows.Close()
call would suffice, unless the library has some special implementation), defining two variables is cleaner, like userRows
and bookRows
.rows
2 个查询之间共享时具有相同的引用(因此一个rows.Close()
调用就足够了,除非库有一些特殊的实现),定义两个变量更清晰,例如userRows
和bookRows
。go-pg
, I believe that you wont need to iterate through rows and scan the id for all the rows manually, I believe that the lib has provided some API like this (based on the quick look on the documentations):go-pg
,但我相信你不需要遍历行并手动扫描所有行的 id,我相信 lib 提供了一些这样的 API(基于快速查看文档): userIds := []int{} err := pgClient.Query(&userIds, "select id from users where ...", args...)
// repo layer func getUserIds(args whatever) ([]int, err) {...} // these can be exposed, based on your packaging logic func getBookIds(args whatever) ([]int, err) {...} // service layer, or wherever you want to aggregate both queries func getUserAndBookIds() ([]int, []int, err) { userIds, err := getUserIds(...) // potential error handling bookIds, err := getBookIds(...) // potential error handling return userIds, bookIds, nil // you have done err handling earlier }
I think this code is easier to read/maintain. You can take a look at the go-pg documentations for more details on how to improve your query.您可以查看go-pg 文档以获取有关如何改进查询的更多详细信息。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.