golang实现原始数据库过滤语法

栏目: Go · 发布时间: 7年前

golang gin 数据库过滤语法

func (s APIGetAlarmListsInputs) collectFilters() string {
    tmp := []string{}
    if s.StartTime != 0 {
        tmp = append(tmp, fmt.Sprintf("timestamp >= FROM_UNIXTIME(%v)", s.StartTime))
    }
    if s.EndTime != 0 {
        tmp = append(tmp, fmt.Sprintf("timestamp <= FROM_UNIXTIME(%v)", s.EndTime))
    }
    if s.Priority != -1 {
        tmp = append(tmp, fmt.Sprintf("priority = %d", s.Priority))
    }
    if s.Status != "" {
        status := ""
        statusTmp := strings.Split(s.Status, ",")
        for indx, n := range statusTmp {
            if indx == 0 {
                status = fmt.Sprintf(" status = '%s' ", n)
            } else {
                status = fmt.Sprintf(" %s OR status = '%s' ", status, n)
            }
        }
        status = fmt.Sprintf("( %s )", status)
        tmp = append(tmp, status)
    }
    if s.ProcessStatus != "" {
        pstatus := ""
        pstatusTmp := strings.Split(s.ProcessStatus, ",")
        for indx, n := range pstatusTmp {
            if indx == 0 {
                pstatus = fmt.Sprintf(" process_status = '%s' ", n)
            } else {
                pstatus = fmt.Sprintf(" %s OR process_status = '%s' ", pstatus, n)
            }
        }
        pstatus = fmt.Sprintf("( %s )", pstatus)
        tmp = append(tmp, pstatus)
    }
    if s.Metrics != "" {
        tmp = append(tmp, fmt.Sprintf("metrics regexp '%s'", s.Metrics))
    }
    if s.EventId != "" {
        tmp = append(tmp, fmt.Sprintf("id = '%s'", s.EventId))
    }
    if s.Endpoints != "" {
        tmp = append(tmp, fmt.Sprintf("endpoint = '%s'", s.Endpoints))
    }
    if s.StrategyId != 0 {
        tmp = append(tmp, fmt.Sprintf("strategy_id = %d", s.StrategyId))
    }
    if s.TemplateId != 0 {
        tmp = append(tmp, fmt.Sprintf("template_id = %d", s.TemplateId))
    }

    filterStrTmp := strings.Join(tmp, " AND ")
    if filterStrTmp != "" {
        filterStrTmp = fmt.Sprintf("WHERE %s", filterStrTmp)
    }
    return filterStrTmp
}

以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

运营之光

运营之光

黄有璨 / 电子工业出版社 / 2016-9-1 / 59.00元

在互联网行业内,“运营”这个职能发展到一定阶段后,往往更需要有成熟的知识体系和工作方法来给予行业从业者们以指引。 《运营之光:我的互联网运营方法论与自白》尤其难得之处在于:它既对“什么是运营”这样的概念认知类问题进行了解读,又带有大量实际的工作技巧、工作思维和工作方法,还包含了很多对于运营的思考、宏观分析和建议,可谓内容完整而全面,同时书中加入了作者亲历的大量真实案例,让全书读起来深入浅出、......一起来看看 《运营之光》 这本书的介绍吧!

RGB转16进制工具
RGB转16进制工具

RGB HEX 互转工具

XML 在线格式化
XML 在线格式化

在线 XML 格式化压缩工具

RGB HSV 转换
RGB HSV 转换

RGB HSV 互转工具