我正在使用 golang、go_reform、PostgreSQL。我想要做的是一个 REST 搜索实用程序,一切都很好,直到我遇到条件搜索查询。这里的“有条件”意味着我在一个表中有 10 列要搜索,而且可能有很多组合,所以我无法单独处理它们。我需要的是一个查询构建器,但我不明白如何在 Go 中实现它。现在我有这样的想法,但似乎效率不高
type Query struct {
Id *int64
FirstName *string
MiddleName *string
LastName *string
AreaId *int64
Birthday *time.Time
}
func (table *Query) Find() (*User) {
if table.Id != nil {
idstr := fmt.Sprintf("WHERE Id = %d AND ", table.Id)
}
else idstr := "WHERE "
}
if table.FirstName != "" {
firststr := fmt.Sprintf("FirstName = %s AND", table.FirstName)
}
else firststr := ""
}//and so on
Run Code Online (Sandbox Code Playgroud)
这感觉真的很尴尬,所以我想知道有没有更好的方法来确定进入的字段Find()并基于此构建 SQL 查询。(实际上它是 JSON 格式并绑定到Querystruct,所以也许有一种没有 struct 的方法)。也可能有 SQL 解决方法,但我认为在没有所有可能列的情况下构建查询会更有效。
编辑:顺便说一句,为了让我的 Google 搜索查询更准确,我发现了很多与我的问题相关的东西,可能我现在会尝试使用它。对于那些也感兴趣的人: 旧的去游乐场示例
gorp 包(片段听起来很有希望)
所以,我找到了解决方案。非常感谢Cerise Limón,他的代码非常适合我。
我最终得到的解决方案
控制器
func Find(c echo.Context) (err error) {
model := &models.Query{}
if err = c.Bind(model); err != nil {
return c.JSON(http.StatusInternalServerError, u.Message(false, "Bad request"))
}
resp := model.Find()
return c.JSON(http.StatusOK, resp)
Run Code Online (Sandbox Code Playgroud)
模型
type Query map[string]interface{}
func (model Query) Find() (Query) {
var values []interface{}
var where []string
for k, v := range model {
values = append(values, v)
//MySQL Way: where = append(where, fmt.Sprintf("%s = ?", k))
where = append(where, fmt.Sprintf(`"%s" = %s`,k, "$" + strconv.Itoa(len(values))))
}
string := ("SELECT name FROM users WHERE " + strings.Join(where, " AND "))
//for testing purposes i didn't ran actual query, just print it in the console and returned JSON back
fmt.Println(string)
return model
}
Run Code Online (Sandbox Code Playgroud)
更新:对于 PostgreSQL 用户(感谢 @mkopriva 和他的操场示例),我可以让这个占位符在 PostgreSQL 上正常工作