传递可变参数用于选择查询

huangapple go评论116阅读模式
英文:

Passing a variadic parameter for select query

问题

我正在尝试构建以下查询,这里我使用了gorp。

  1. func GetAgregatedDownloadsFromTo(constrains Constrains) []dao.NameValue {
  2. dbMap := utils.GetDBConnection("radsummary")
  3. defer dbMap.Db.Close()
  4. var totalDailyDownloads []dao.NameValue
  5. query := "SELECT SUM(outputoctets) as value, date as name FROM dailyacct WHERE date >= ? AND date < ?"
  6. if len(constrains.LocationGroups) > 0 {
  7. query = query + " AND calledstationid=?"
  8. for i := 1; i < len(constrains.LocationGroups); i++ {
  9. query = query + " OR calledstationid=?"
  10. }
  11. query = query + " GROUP BY date"
  12. print(query)
  13. _, err := dbMap.Select(&totalDailyDownloads, query, constrains.From, constrains.To, constrains.LocationGroups...)
  14. if err != nil {
  15. panic(err.Error()) // proper error handling instead of panic
  16. }
  17. }
  18. return totalDailyDownloads
  19. }
  20. type Constrains struct {
  21. From string `json:"from"`
  22. To string `json:"to"`
  23. LocationGroups []string `json:"locationgroups"`
  24. }

查询的构建基于constrains.LocationGroups的长度。我遇到的问题是在将可变数量的参数传递给Select查询时,一旦我将constrains.LocationGroups...作为查询参数,它就会抛出编译器错误too many arguments in call to dbMap.Select

是否可能实现这种需求?感谢您的建议。

英文:

I'm trying to build a query as follows, here I'm using gorp.

  1. func GetAgregatedDownloadsFromTo(constrains Constrains) [] dao.NameValue {
  2. dbMap := utils.GetDBConnection(&quot;radsummary&quot;);
  3. defer dbMap.Db.Close()
  4. var totalDailyDownloads[] NameValue
  5. query := &quot;SELECT SUM(outputoctets) as value ,date as name FROM dailyacct where date &gt;= ? AND date &lt; ? &quot;
  6. if len(constrains.LocationGroups) &gt; 0 {
  7. query = query + &quot; AND calledstationid=? &quot;
  8. for i := 1; i&lt; len(constrains.LocationGroups); i++ {
  9. query = query + &quot; OR calledstationid=? &quot;
  10. }
  11. query = query + &quot; group by date&quot;
  12. print(query)
  13. _, err := dbMap.Select(&amp;totalDailyDownloads, query, constrains.From, constrains.To, constrains.LocationGroups...)
  14. if err != nil {
  15. panic(err.Error()) // proper error handling instead of panic
  16. }
  17. }
  18. return totalDailyDownloads
  19. }
  20. type Constrains struct {
  21. From string `json:&quot;from&quot;`
  22. To string `json:&quot;to&quot;`
  23. LocationGroups []string `json:&quot;locationgroups&quot;`
  24. }

Query construction happens based on the length of constrains.LocationGroups. Trouble I'm having is passing the variable number of args to the Select query once I give constrains.LocationGroups... as select query parameters it throws a compiler error too many arguments in call to dbMap.Select

Is it possible to achieve this kind of requirement?. Appreciate your input.

答案1

得分: 0

根据https://stackoverflow.com/questions/7975095/pass-string-slice-to-variadic-empty-interface-parameter?rq=1找到了一个答案。

以下是实现该任务的更新代码:

  1. func GetAgregatedDownloadsFromTo(constrains dao.Constrains) []dao.NameValue {
  2. dbMap := utils.GetDBConnection("radsummary")
  3. defer dbMap.Db.Close()
  4. var totalDailyDownloads []dao.NameValue
  5. query := "SELECT SUM(outputoctets) as value ,date as name FROM dailyacct where date >= ? AND date < ? "
  6. if len(constrains.LocationGroups) > 0 {
  7. args := make([]interface{}, len(constrains.LocationGroups)+2)
  8. args[0] = constrains.From
  9. args[1] = constrains.To
  10. for index, value := range constrains.LocationGroups {
  11. args[index+2] = value
  12. }
  13. query = query + " AND calledstationid=? "
  14. for i := 1; i < len(constrains.LocationGroups); i++ {
  15. query = query + " OR calledstationid=? "
  16. }
  17. query = query + " group by date"
  18. print(query)
  19. _, err := dbMap.Select(&totalDailyDownloads, query, args...)
  20. if err != nil {
  21. panic(err.Error()) // proper error handling instead of panic
  22. }
  23. }
  24. return totalDailyDownloads
  25. }

在这里,我需要将字符串切片转换为接口切片。

英文:

Found an answer based on https://stackoverflow.com/questions/7975095/pass-string-slice-to-variadic-empty-interface-parameter?rq=1

Below is the updated code to achieve the task

  1. func GetAgregatedDownloadsFromTo(constrains dao.Constrains) [] dao.NameValue {
  2. dbMap := utils.GetDBConnection(&quot;radsummary&quot;);
  3. defer dbMap.Db.Close()
  4. var totalDailyDownloads[] dao.NameValue
  5. query := &quot;SELECT SUM(outputoctets) as value ,date as name FROM dailyacct where date &gt;= ? AND date &lt; ? &quot;
  6. if len(constrains.LocationGroups) &gt; 0 {
  7. args := make([]interface{}, len(constrains.LocationGroups)+2)
  8. args[0] = constrains.From
  9. args[1] = constrains.To
  10. for index, value := range constrains.LocationGroups { args[index+2] = value }
  11. query = query + &quot; AND calledstationid=? &quot;
  12. for i := 1; i&lt; len(constrains.LocationGroups); i++ {
  13. query = query + &quot; OR calledstationid=? &quot;
  14. }
  15. query = query + &quot; group by date&quot;
  16. print(query)
  17. _, err := dbMap.Select(&amp;totalDailyDownloads, query, args...)
  18. if err != nil {
  19. panic(err.Error()) // proper error handling instead of panic
  20. }
  21. }
  22. return totalDailyDownloads
  23. }

Here I had to convert the string slice to an interface slice.

huangapple
  • 本文由 发表于 2016年1月1日 08:39:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/34552723.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定