登录
首页 >  Golang >  Go问答

动态拼接条件的 GORM 原生查询

来源:stackoverflow

时间:2024-02-24 20:57:24 495浏览 收藏

欢迎各位小伙伴来到golang学习网,相聚于此都是缘哈哈哈!今天我给大家带来《动态拼接条件的 GORM 原生查询》,这篇文章主要讲到等等知识,如果你对Golang相关的知识非常感兴趣或者正在自学,都可以关注我,我会持续更新相关文章!当然,有什么建议也欢迎在评论留言提出!一起学习!

问题内容

我使用 gorm 原始 sql 查询,但 web 开发人员将其传递给我多个参数。如果参数为空,则不执行查询。目前我使用map来拼接where语句,但是在raw模式下好像不行。

where := make(map[string]interface{})
    if info.UserKey != "" {
        where["pay_merchant_order.merchant_fk"] = info.UserKey
    }
    if info.NickName != "" {
        where["sys_users.nick_name"] = info.NickName
    }
    if info.Type != 0 {
        where["pay_rate.id"] = info.Type
    }
    if info.IsSign != -1 && info.IsSign != 0 {
        where["pay_order.pay_status"] = info.IsSign
    }
    if info.PayOfficialOrder != "" {
        where["pay_order.pay_official_order"] = info.PayOfficialOrder
    }
    if info.Amount != 0 {
        where["pay_order.pay_amount"] = info.Amount
    }
    if info.MOrder != "" {
        where["pay_merchant_order.order"] = info.MOrder
    }
    if info.SOrder != "" {
        where["pay_merchant_order.sys_order_fk"] = info.SOrder
    }
    if info.Phone != "" {
        where["pay_channel_order.phone"] = info.Phone
    }
    if info.Rate != 0 {
        where["pay_order.pay_rate"] = info.Rate
    }
    db := global.GVA_DB.Debug()
    sql := "select pay_merchant_order.id,pay_merchant_order.order,pay_merchant_order.call_status,sys_users.username,pay_order.url,pay_merchant_order.merchant_fk,pay_order.token,pay_merchant_order.call_back,sys_users.nick_name,pay_order.pay_rate as rate,pay_order.pay_amount,pay_actualamount,pay_order.real_pay_amount,pay_order.pay_official_order,pay_order.pay_sys_order,pay_type.mode,pay_rate.name,pay_merchant_order.is_sign,pay_order.created_at,pay_order.pay_time,pay_order.overdue_time,pay_channel_order.phone from pay_merchant_order LEFT JOIN pay_order ON pay_order.pay_sys_order = pay_merchant_order.sys_order_fk LEFT JOIN sys_users ON sys_users.unique = pay_order.pay_merchant LEFT JOIN pay_channel ON pay_channel.id = pay_order.pay_channel_fk LEFT JOIN pay_rate ON pay_rate.id = pay_channel.rate_fk LEFT JOIN pay_payment_type ON pay_payment_type.id = pay_channel.channel_type_fk LEFT JOIN pay_type ON pay_type.id = pay_order.pay_type left join pay_channel_order on pay_channel_order.order_fk = pay_merchant_order.sys_order_fk"
    db.Raw(sql).Where(where).Group("pay_merchant_order.id").Order("pay_merchant_order.created_at desc").Scan(&payMerchantOrders)
    err = db.Count(&total).Error
    err = db.Limit(limit).Offset(offset).Error

请告诉我如何拼接查询条件。


解决方案


尝试两种解决方案:

解决方案 1 - 没有原始查询

//your code for additions to the where clause

...

//the main part of the query
db.table("pay_merchant_order").
joins("left join pay_order on pay_order.pay_sys_order = pay_merchant_order.sys_order_fk").
joins("left join sys_users on sys_users.unique = pay_order.pay_merchant").
joins("left join pay_channel on pay_channel.id = pay_order.pay_channel_fk").
joins("left join pay_rate on pay_rate.id = pay_channel.rate_fk").
joins("left join pay_payment_type on pay_payment_type.id = pay_channel.channel_type_fk").
joins("left join pay_type on pay_type.id = pay_order.pay_type").
joins("left join pay_channel_order on pay_channel_order.order_fk = pay_merchant_order.sys_order_fk").
where(where).
select("pay_merchant_order.id,pay_merchant_order.order,pay_merchant_order.call_status,sys_users.username,pay_order.url,pay_merchant_order.merchant_fk,pay_order.token,pay_merchant_order.call_back,sys_users.nick_name,pay_order.pay_rate as rate,pay_order.pay_amount,pay_actualamount,pay_order.real_pay_amount,pay_order.pay_official_order,pay_order.pay_sys_order,pay_type.mode,pay_rate.name,pay_merchant_order.is_sign,pay_order.created_at,pay_order.pay_time,pay_order.overdue_time,pay_channel_order.phone").
group("pay_merchant_order.id").
order("pay_merchant_order.created_at desc")

然后,您可以使用 scanfindcount 检索所需的信息。

解决方案 2 - 仅原始查询

此解决方案基于为原始查询准备 where 子句并仅运行原始查询。

type WhereClause map[string]interface{}

func (wc WhereClause) ConvertToSQLString() string {
    list := []string{}

    for k, v := range wc {
        list = append(list, fmt.Sprintf("%s=%v", k, v))
    }

    return strings.Join(list, " AND ")
}

where := WhereClause{}
    if info.UserKey != "" {
        where["pay_merchant_order.merchant_fk"] = info.UserKey
    }
    if info.NickName != "" {
        where["sys_users.nick_name"] = info.NickName
    }
    if info.Type != 0 {
        where["pay_rate.id"] = info.Type
    }
    if info.IsSign != -1 && info.IsSign != 0 {
        where["pay_order.pay_status"] = info.IsSign
    }
    if info.PayOfficialOrder != "" {
        where["pay_order.pay_official_order"] = info.PayOfficialOrder
    }
    if info.Amount != 0 {
        where["pay_order.pay_amount"] = info.Amount
    }
    if info.MOrder != "" {
        where["pay_merchant_order.order"] = info.MOrder
    }
    if info.SOrder != "" {
        where["pay_merchant_order.sys_order_fk"] = info.SOrder
    }
    if info.Phone != "" {
        where["pay_channel_order.phone"] = info.Phone
    }
    if info.Rate != 0 {
        where["pay_order.pay_rate"] = info.Rate
    }
db := global.GVA_DB.Debug()
sql := fmt.Sprintf(`SELECT pay_merchant_order.id,pay_merchant_order.order,pay_merchant_order.call_status,sys_users.username,pay_order.url,pay_merchant_order.merchant_fk,pay_order.token,pay_merchant_order.call_back,sys_users.nick_name,pay_order.pay_rate as rate,pay_order.pay_amount,pay_actualamount,pay_order.real_pay_amount,pay_order.pay_official_order,pay_order.pay_sys_order,pay_type.mode,pay_rate.name,pay_merchant_order.is_sign,pay_order.created_at,pay_order.pay_time,pay_order.overdue_time,pay_channel_order.phone 
FROM pay_merchant_order LEFT JOIN pay_order ON pay_order.pay_sys_order = pay_merchant_order.sys_order_fk LEFT JOIN sys_users ON sys_users.unique = pay_order.pay_merchant LEFT JOIN pay_channel ON pay_channel.id = pay_order.pay_channel_fk LEFT JOIN pay_rate ON pay_rate.id = pay_channel.rate_fk LEFT JOIN pay_payment_type ON pay_payment_type.id = pay_channel.channel_type_fk LEFT JOIN pay_type ON pay_type.id = pay_order.pay_type left join pay_channel_order on pay_channel_order.order_fk = pay_merchant_order.sys_order_fk
WHERE %s
GROUP BY pay_merchant_order.id
ORDER BY pay_merchant_order.created_at DESC`, where.ConvertToSQLString())
    
db.Raw(sql).Scan(&payMerchantOrders)

以上就是《动态拼接条件的 GORM 原生查询》的详细内容,更多关于的资料请关注golang学习网公众号!

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>