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

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

Passing a variadic parameter for select query

问题

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

func GetAgregatedDownloadsFromTo(constrains Constrains) []dao.NameValue {
    dbMap := utils.GetDBConnection("radsummary")
    defer dbMap.Db.Close()
    var totalDailyDownloads []dao.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的长度。我遇到的问题是在将可变数量的参数传递给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.

func GetAgregatedDownloadsFromTo(constrains Constrains) [] dao.NameValue {
	dbMap := utils.GetDBConnection(&quot;radsummary&quot;);
	defer dbMap.Db.Close()
	var totalDailyDownloads[] NameValue
	query := &quot;SELECT SUM(outputoctets) as value ,date as name FROM dailyacct where date &gt;= ? AND date &lt; ? &quot;

	if len(constrains.LocationGroups) &gt; 0 {
		query = query + &quot; AND calledstationid=? &quot;
		for i := 1; i&lt; len(constrains.LocationGroups); i++ {
   			query = query + &quot; OR calledstationid=? &quot;
		}
		query = query + &quot; group by date&quot;
		print(query)
		_, err := dbMap.Select(&amp;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:&quot;from&quot;`
	To string   `json:&quot;to&quot;`
	LocationGroups []string    `json:&quot;locationgroups&quot;`
}

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找到了一个答案。

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

func GetAgregatedDownloadsFromTo(constrains dao.Constrains) []dao.NameValue {
    dbMap := utils.GetDBConnection("radsummary")
    defer dbMap.Db.Close()
    var totalDailyDownloads []dao.NameValue
    query := "SELECT SUM(outputoctets) as value ,date as name FROM dailyacct where date >= ? AND date < ? "

    if len(constrains.LocationGroups) > 0 {
        args := make([]interface{}, len(constrains.LocationGroups)+2)
        args[0] = constrains.From
        args[1] = constrains.To
        for index, value := range constrains.LocationGroups {
            args[index+2] = value
        }

        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, args...)
        if err != nil {
            panic(err.Error()) // proper error handling instead of panic
        }
    }
    return totalDailyDownloads
}

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

英文:

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

func GetAgregatedDownloadsFromTo(constrains dao.Constrains) [] dao.NameValue {
	dbMap := utils.GetDBConnection(&quot;radsummary&quot;);
	defer dbMap.Db.Close()
	var totalDailyDownloads[] dao.NameValue
	query := &quot;SELECT SUM(outputoctets) as value ,date as name FROM dailyacct where date &gt;= ? AND date &lt; ? &quot;

	if len(constrains.LocationGroups) &gt; 0 {
		args := make([]interface{}, len(constrains.LocationGroups)+2)
		args[0] = constrains.From
		args[1] = constrains.To
		for index, value := range constrains.LocationGroups { args[index+2] = value }

		query = query + &quot; AND calledstationid=? &quot;
		for i := 1; i&lt; len(constrains.LocationGroups); i++ {
   			query = query + &quot; OR calledstationid=? &quot;
		}
		query = query + &quot; group by date&quot;
		print(query)
		_, err := dbMap.Select(&amp;totalDailyDownloads, query, args...)
		if err != nil {
			panic(err.Error()) // proper error handling instead of panic
		}
	}
	return totalDailyDownloads
}

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:

确定