我正在尝试按如下方式构建查询,这里我使用的是 gorp。
func GetAgregatedDownloadsFromTo(constrains Constrains) [] dao.NameValue {
dbMap := utils.GetDBConnection("radsummary");
defer dbMap.Db.Close()
var totalDailyDownloads[] NameValue
query := "SELECT SUM(outputoctets) as value ,date as name FROM dailyacct where date >= ? AND date < ? "
if len(constrains.LocationGroups) > 0 {
query = query + " AND calledstationid=? "
for i := 1; i< len(constrains.LocationGroups); i++ {
query = query + " OR calledstationid=? "
}
query = query + " group by date"
print(query)
_, err := dbMap.Select(&totalDailyDownloads, query, constrains.From, constrains.To, constrains.LocationGroups...)
if err != nil {
panic(err.Error()) // proper error handling instead of panic
}
}
return totalDailyDownloads
}
type Constrains struct {
From string `json:"from"`
To string `json:"to"`
LocationGroups []string `json:"locationgroups"`
}
查询构造基于constrains.LocationGroups 的长度。我遇到的麻烦是将可变数量的 args 传递给 Select 查询,一旦我给出了 constrains.LocationGroups... 作为选择查询参数,它会引发编译器错误too many arguments in call to dbMap.Select
是否有可能实现这种要求?感谢您的意见。