我想在Golang的ORM包“gorp”中使用SQL占位符来编写IN子句

最近,在公司里进行了使用Golang开发微服务的工作。
在这个过程中,遇到了一些困难,所以做了一些备忘录。
有一个人讲到了想要在Golang中使用使用了in子句的SQL,而提到了一个名为gorp的ORM包。

Golang的Gorp是什么东西?

能够在golang中使用的ORM软件包
https://github.com/go-gorp/gorp

可以直接编写SQL语句,并进行到结构体的映射。个人而言,我喜欢保持SQL的直接编写方式,这是一个相当不错的软件包。但是……

提前写下结论。

就是强行结构…
简单来说,就是让你把接口传递给第三个参数。


    args := make([]interface{}, len(fileIds))
    quarks := make([]string, len(fileIds))
    for i, fileId := range fileIds {
        args[i] = fileId
        quarks[i] = "?"
    }

    var apps []*App
    _, err := dbMap.Select(&apps, fmt.Sprintf("SELECT * FROM app WHERE file_id in (%s) ORDER BY id DESC", strings.Join(quarks, ",")), args...)
    if err != nil {
        return nil, err
    }

参考:Golang SqlExecutor.Select 的示例

让我阅读公式文档吧

最开始我以为由于是PHP,可以用逗号展开slice并将其作为参数传递…///
但实际上在正常的文档中看到,gorp中要求传递接口。
因为对占位符的值传递方法有点特殊,所以还是要好好阅读文档,避免问题。

公式链接:https://gowalker.org/github.com/go-gorp/gorp#DbMap

也有人面临相同的困扰

type DbMap struct {

    // Db handle to use with this map
    Db  *sql.DB

    // Dialect implementation to use with this map
    Dialect Dialect

    TypeConverter TypeConverter

    // ExpandSlices when enabled will convert slice arguments in mappers into flat
    // values. It will modify the query, adding more placeholders, and the mapper,
    // adding each item of the slice as a new unique entry in the mapper. For
    // example, given the scenario bellow:
    //
    //     dbmap.Select(&output, "SELECT 1 FROM example WHERE id IN (:IDs)", map[string]interface{}{
    //       "IDs": []int64{1, 2, 3},
    //     })
    //
    // The executed query would be:
    //
    //     SELECT 1 FROM example WHERE id IN (:IDs0,:IDs1,:IDs2)
    //
    // With the mapper:
    //
    //     map[string]interface{}{
    //       "IDs":  []int64{1, 2, 3},
    //       "IDs0": int64(1),
    //       "IDs1": int64(2),
    //       "IDs2": int64(3),
    //     }
    //
    // It is also flexible for custom slice types. The value just need to
    // implement stringer or numberer interfaces.
    //
    //     type CustomValue string
    //
    //     const (
    //       CustomValueHey CustomValue = "hey"
    //       CustomValueOh  CustomValue = "oh"
    //     )
    //
    //     type CustomValues []CustomValue
    //
    //     func (c CustomValues) ToStringSlice() []string {
    //       values := make([]string, len(c))
    //       for i := range c {
    //         values[i] = string(c[i])
    //       }
    //       return values
    //     }
    //
    //     func query() {
    //       // ...
    //       result, err := dbmap.Select(&output, "SELECT 1 FROM example WHERE value IN (:Values)", map[string]interface{}{
    //         "Values": CustomValues([]CustomValue{CustomValueHey}),
    //       })
    //       // ...
    //     }
    ExpandSliceArgs bool
    // contains filtered or unexported fields
}

有会写Golang的人来一下

由于没有比想象中更多的内容可写,所以只是宣传一下。
如果想编写Golang并专注于微服务开发的人,我认为我们的公司非常适合您,请务必前来进行休闲面谈!(在Qiita Jobs上招聘中。)
我们在项目中积极采用Golang。

?????正在招聘后端工程师〜?????
https://jobs.qiita.com/employers/255/postings/1169

广告
将在 10 秒后关闭
bannerAds