如何使用gorm动态添加查询参数?

时间:2019-06-25 01:50:17

标签: java go gorm

我是golang开发的新手。我有6个参数可以使用gorm传递给查询。这是选择查询,因此,基于输入值,我们需要对值进行过滤。因此,我们需要将过滤器动态传递到查询中。我尝试过但没有解决办法。

func GetUsers(DB * gorm.DB,偏移量int,限制int,用户uibackendmodels.UserDetails)(用户[] uibackendmodels.UserDetails,错误错误){

query := "SELECT userid, username, nickname, email, mobile, location, status, roleids, trsids, brandids, languagecode, createdat, createdby, modified, modifiedby" +
    " FROM users WHERE 1=1 "

if User.UserName != "" || User.NickName != "" {
    nameQuery := "("
    if User.UserName != "" {
        nameQuery = nameQuery + " username LIKE " + "'" + User.UserName + "%'"
    }
    if User.NickName != "" && User.UserName != "" {
        nameQuery = nameQuery + " OR nickname LIKE " + "'" + User.NickName + "%'"
    } else if User.NickName != "" {
        nameQuery = nameQuery + " nickname LIKE " + "'" + User.NickName + "%'"
    }
    query = query + " AND " + nameQuery + ")"
}

if User.BrandIDs != nil && len(User.BrandIDs) > 0 {
    brandIds := "("
    for i := range User.BrandIDs {
        if len(User.BrandIDs) == (i + 1) {
            brandIds = brandIds + "'" + User.BrandIDs[i] + "' =  ANY (brandids) "
        } else {
            brandIds = brandIds + "'" + User.BrandIDs[i] + "' =  ANY (brandids) OR "
        }
    }
    query = query + " AND " + brandIds + ")"
}

if User.TRSIDs != nil && len(User.TRSIDs) > 0 {
    trsIds := "("
    for i := range User.TRSIDs {
        if len(User.TRSIDs) == (i + 1) {
            trsIds = trsIds + "'" + User.TRSIDs[i] + "' =  ANY (trsids) "
        } else {
            trsIds = trsIds + "'" + User.TRSIDs[i] + "' =  ANY (trsids) OR "
        }
    }
    query = query + " AND " + trsIds + ")"
}
if User.RoleIDs != nil && len(User.RoleIDs) > 0 {
    roleIds := "("
    for i := range User.RoleIDs {
        if len(User.RoleIDs) == (i + 1) {
            roleIds = roleIds + strconv.FormatInt(int64(User.RoleIDs[i]), 10) + " =  ANY (roleids) "
        } else {
            roleIds = roleIds + strconv.FormatInt(int64(User.RoleIDs[i]), 10) + " =  ANY (roleids) OR "
        }
    }
    query = query + " AND " + roleIds + ")"
}
if User.Status != "" {
    query = query + " AND  status = " + "'" + EnumToString(User.Status) + "'"
}
query = query + " AND  status != 'deleted' ORDER BY modified desc LIMIT " + strconv.Itoa(Limit) + " OFFSET " + strconv.Itoa(Offset)

if err := DB.Raw(query).Scan(&Users).Error; err != nil {
    return nil, err
}
return Users, nil

}

我需要为上述查询动态添加参数。

1 个答案:

答案 0 :(得分:0)

在GORM中,您可以通过丰富对象并将其传递给函数来处理基本输入,如下所示:

// Would get the First user with Name="jinzhu" and Age="20"
db.Where(&User{Name: "jinzhu", Age: 20}).First(&user)

每当您无法使用GORM的默认功能表示语句的一部分时,就可以使用常规的SQL和方法链接:

// Would get the first User with Name="jinzhu" and id in (4,5,6,7)
ids := [4]int{4,5,6,7}
db.Where(&User{Name: "jinzhu"}).Where("id IN (?)", ids).First(&user)

您还可以使用变量链接方法

base := db.Where(&User{Name: "jinzhu"})
if someThing {
    base = base.Where("id IN (?)", ids) // Adding this condition just if someThing is true
}
base.First(&user) // Query your results

More Information about Method Chaining