Create dynamic query for postgresql in golang with map[string]interface{} input

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

Create dynamic query for postgresql in golang with map[string]interface{} input

问题

我有一个名为user的map[string]interface{}输入,如下所示:

user := map[string]interface{}{
    "firstname": firstname,
    "lastname":  lastname,
    "country":   country,
    "email":     email,
}

上述的值来自其他函数作为变量,因此没有在引号中指示。

例如,我需要根据上述接口生成一个动态查询,如下所示:"INSERT INTO USERTABLE (key1, key2, key3, key4) VALUES (val1, val2, val3, val4) RETURNING id"

其中迭代器将计算键和值,并生成以下查询:

"INSERT INTO USERTABLE (firstname, lastname, country, email) VALUES (firstname, lastname, country, email) RETURNING userid"

希望构建一个动态字符串。

英文:

I have map[string]interface input user like below

user := map[string]interface{}{
       "firstname": firstname,
       "lastname":  lastname,
       "country":   country,
       "email":     email,
   }

>Values given above are from other functions as variables hence not indicated in "".

For example, I need to generate a dynamic query as follows from the above interface: "INSERT INTO USERTABLE (key1, key2, key3, key4) VALUES (val1, val2, val3, val4) RETURNING id

where the iterator will count keys and values and will generate the query as below

"INSERT INTO USERTABLE (firstname, lastname, country, email) VALUES (firstname, lastname, country, email) RETURNING userid"

Looking to build a dynamic string.

答案1

得分: 0

纯Go 😄

func prepareInsert(table string, params map[string]interface{}) string {
	columns := len(params)
	fieldSlice := make([]string, 0, columns)
	for field, _ := range params {
		fieldSlice = append(fieldSlice, field)
	}
	fields := strings.Join(fieldSlice, ",")

	placeholders := prepareQueryPlaceholders(1, columns)
	return fmt.Sprintf(`INSERT INTO %s (%s) VALUES (%s) RETURNING %s`, table, fields, placeholders, fields)
}

func prepareQueryPlaceholders(start, quantity int) string {
	placeholders := make([]string, 0, quantity)
	end := start + quantity
	for i := start; i < end; i++ {
		placeholders = append(placeholders, strings.Join([]string{"$", strconv.Itoa(i)}, ""))
	}
	return strings.Join(placeholders, ",")
}

<kbd>PLAYGROUND</kbd>

英文:

Pure Go 😉

func prepareInsert(table string, params map[string]interface{}) string {
	columns := len(params)
	fieldSlice := make([]string, 0, columns)
	for field, _ := range params {
		fieldSlice = append(fieldSlice, field)
	}
	fields := strings.Join(fieldSlice, &quot;,&quot;)

	placeholders := prepareQueryPlaceholders(1, columns)
	return fmt.Sprintf(`INSERT INTO %s (%s) VALUES (%s) RETURNING %s`, table, fields, placeholders, fields)
}

func prepareQueryPlaceholders(start, quantity int) string {
	placeholders := make([]string, 0, quantity)
	end := start + quantity
	for i := start; i &lt; end; i++ {
		placeholders = append(placeholders, strings.Join([]string{&quot;$&quot;, strconv.Itoa(i)}, &quot;&quot;))
	}
	return strings.Join(placeholders, &quot;,&quot;)
}

<kbd>PLAYGROUND</kbd>

huangapple
  • 本文由 发表于 2022年9月27日 23:43:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/73870312.html
匿名

发表评论

匿名网友

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

确定