Skip to content

Filter说明

IHEII edited this page Aug 21, 2023 · 1 revision

Filter说明

目前某些版本的server可能不支持Filter

Filter种类

  1. ObTableFilterList: 用于连接不同的Filter
  2. ObTableValueFilter: ValueFilter, 单值的比较
  3. ObTableInFilter/ObTableNotInFilter: 所选列值是否与集合内的值的任意一个相等

ObTableFilterList

用于连接不同的Filter, 支持使用And / Or来连接不同的Filter

const (
	OperatorAnd Operator = iota
	OperatorOr
)

我们也提供了函数方便快速获取一个And的FilterList或者Or的FilterList

func AndList(filters ...ObTableFilter) *ObTableFilterList {
	return NewObTableFilterListWithOperatorAndTableFilter(OperatorAnd, filters...)
}

func OrList(filters ...ObTableFilter) *ObTableFilterList {
	return NewObTableFilterListWithOperatorAndTableFilter(OperatorOr, filters...)
}

ObTableValueFilter

最基础的Filter, 用于比较某一列的值是否满足条件, 满足条件则过滤出来

const (
	LessThan ObCompareOperator = iota
	GreaterThan
	LessOrEqualThan
	GreaterOrEqualThan
	NotEqual
	Equal
	IsNull
	IsNotNull
)

func CompareVal(op ObCompareOperator, columnName string, value interface{}) *ObTableValueFilter {
	return NewObTableValueFilter(op, columnName, value)
}

参数说明:

  • op: 见Const值,定义了列名与value的比较方法
  • columnName: 被比较的列名
  • value: 与列名比较的值

ObTableInFilter / ObTableNotInFilter

func In(columnName string, values ...interface{}) *ObTableInFilter {
	return NewObTableInFilter(columnName, values...)
}

func NotIn(columnName string, values ...interface{}) *ObTableNotInFilter {
	return NewObTableNotInFilter(columnName, values...)
}

参数说明:

  • columnName: 被比较的列名
  • values: 若被比较的多个值, In中判断是否与任意一个值相等, NotIn中判断是否与任意一个值不相等(列的值不出现在values中)

Demo

// 4 < c1 < 6
c1lt6 := filter.CompareVal(filter.LessThan, "c1", int64(6))
c1gt4 := filter.CompareVal(filter.GreaterThan, "c1", int64(4))
c1filterList := filter.AndList(c1lt6, c1gt4)

// 0 >= c2 or c2 >= 2
c2le0 := filter.CompareVal(filter.LessOrEqualThan, "c2", int64(0))
c2ge2 := filter.CompareVal(filter.GreaterOrEqualThan, "c2", int64(2))
c2filterList := filter.orList(c2le0, c2ge2)

// (4 < c1 < 6 ) && (c2 <= 0) && (c2 >= 2)
finalFilter := filter.AndList(c1filterList, c2filterList)