Golang UPDATE column with Postgres

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

Golang UPDATE column with Postgres

问题

假设我有一个名为employments的表和一个名为Employment的结构体。

  1. type Employment struct {
  2. ID int `json:"id"`
  3. Created_at string `json:"created_at"`
  4. Updated_at string `json:"updated_at"`
  5. Education string `json:"education"`
  6. Job string `json:"job"`
  7. Position string `json:"position"`
  8. Business_phone string `json:"business_phone"`
  9. Next_payday string `json:"next_payday"`
  10. Employment_type int `json:"employment_type"`
  11. Income float64 `json:"income"`
  12. Additional float64 `json:"additional"`
  13. }

用户可以更新他们的employment,问题是我不知道用户想要更新哪些字段。

因此,我决定遍历输入的结构体,获取非空字段,以生成查询字符串,类似于UPDATE employments SET position=$1, income=$2 WHERE id=$3

这是我这次得到的代码:

  1. func FlexibleUpdate(table_name string, str interface{}, cond string, ret string) string {
  2. query := "UPDATE " + table_name + " SET "
  3. j := 0
  4. m := structs.Map(str)
  5. for i := range m {
  6. if m[i] != "" && m[i] != 0 && m[i] != 0.0 {
  7. j++
  8. query = query + strings.ToLower(i) + "=$" + strconv.Itoa(j) + ","
  9. }
  10. }
  11. j++
  12. // 添加条件
  13. if cond != "" {
  14. query = query[:len(query)-1] + " WHERE " + cond + "=$" + strconv.Itoa(j)
  15. }
  16. // 返回值
  17. if ret != "" {
  18. query = query + " RETURNING " + ret
  19. }
  20. return query
  21. }

我不知道如何将输入值分配给$1, $2, ...以执行查询。

  1. database.DB.QueryRow(query_string, value_1, value_2, ...)

如果你有任何想法或其他解决方法,请告诉我。

英文:

Let's say I have a table employments and a struct Employment

  1. type Employment struct {
  2. ID int `json:"id"`
  3. Created_at string `json:"created_at"`
  4. Updated_at string `json:"updated_at"`
  5. Education string `json:"education"`
  6. Job string `json:"job"`
  7. Position string `json:"position"`
  8. Business_phone string `json:"business_phone"`
  9. Next_payday string `json:"next_payday"`
  10. Employment_type int `json:"employment_type"`
  11. Income float64 `json:"income"`
  12. Additional float64 `json:"additional"`
  13. }

User can update their employment, the problem is I don't know which fields user want to update.

So that, I decided to range over input struct to get non nil fields to generate query string, some thing like UPDATE employments SET position=$1, income=$2 WHERE id=$3

This is what I got this time

  1. func FlexibleUpdate(table_name string, str interface{}, cond string, ret string) string {
  2. query := "UPDATE " + table_name + " SET "
  3. j := 0
  4. m := structs.Map(str)
  5. for i := range m {
  6. if m[i] != "" && m[i] != 0 && m[i] != 0.0 && {
  7. j++
  8. query = query + strings.ToLower(i) + "=$" + strconv.Itoa(j) + ","
  9. }
  10. }
  11. j++
  12. // adding conditions
  13. if cond != "" {
  14. query = query[:len(query)-1] + " WHERE " + cond + "=$" + strconv.Itoa(j)
  15. }
  16. // return values
  17. if ret != "" {
  18. query = query + " RETURNING " + ret
  19. }
  20. return query
  21. }

I don't know how to assign input values to $1, $2, ... to execute query

  1. database.DB.QueryRow(query_string, value_1, value_2, ...)

Let me know if you have any idea or another way to resolve it.

答案1

得分: 2

只需在切片中收集非nil值,然后在执行查询时使用该切片和...

  1. var values []interface{}
  2. for i := range m {
  3. if v := m[i]; v != "" && v != 0 && v != 0.0 && /* 这里你漏掉了一个条件 */ {
  4. j++
  5. query = query + strings.ToLower(i) + "=$" + strconv.Itoa(j) + ","
  6. values = append(values, v)
  7. }
  8. }
  9. // ....
  10. database.DB.QueryRow(query_string, values...)

请注意,这只是代码的一部分,可能需要根据上下文进行适当的修改和使用。

英文:

Just collect the non-nil values in a slice and then use that slice with ... when executing the query.

  1. var values []interface{}
  2. for i := range m {
  3. if v := m[i]; v != "" && v != 0 && v != 0.0 && /* you're missing a condition here */{
  4. j++
  5. query = query + strings.ToLower(i) + "=$" + strconv.Itoa(j) + ","
  6. values = append(values, v)
  7. }
  8. }
  9. // ....
  10. database.DB.QueryRow(query_string, values...)

huangapple
  • 本文由 发表于 2017年8月23日 01:20:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/45823566.html
匿名

发表评论

匿名网友

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

确定