f5/dbfilter.go
2023-08-20 13:16:34 +08:00

229 lines
4.0 KiB
Go

package f5
import (
"fmt"
)
type LinkOp int32
const (
LK_AND LinkOp = iota
LK_OR
)
type QueryCond int32
const (
QC_LIKE QueryCond = iota
QC_EQ
QC_LT
QC_LE
QC_GT
QC_GE
QC_CUSTOM
)
type dbFilter struct {
}
type DbQueryFilter interface {
And() DbQueryFilter
Or() DbQueryFilter
Not() DbQueryFilter
GetLinkOp() LinkOp
GenSql() string
}
type PageQueryOne struct {
linkOp LinkOp
not bool
fieldName string
cond QueryCond
ignoreEmpty bool
val string
}
type PageQueryComp struct {
linkOp LinkOp
not bool
subFilters []DbQueryFilter
}
func (this *dbFilter) init() {
}
func (this *dbFilter) unInit() {
}
func (this *dbFilter) Like(fieldName string, val string) DbQueryFilter {
f := &PageQueryOne{
cond: QC_LIKE,
fieldName: fieldName,
val: val}
return f
}
func (this *dbFilter) EQ(fieldName string, val string) DbQueryFilter {
f := &PageQueryOne{
cond: QC_EQ,
fieldName: fieldName,
val: val}
return f
}
func (this *dbFilter) LT(fieldName string, val string) DbQueryFilter {
f := &PageQueryOne{
cond: QC_LT,
fieldName: fieldName,
val: val}
return f
}
func (this *dbFilter) LE(fieldName string, val string) DbQueryFilter {
f := &PageQueryOne{
cond: QC_LE,
fieldName: fieldName,
val: val}
return f
}
func (this *dbFilter) GT(fieldName string, val string) DbQueryFilter {
f := &PageQueryOne{
cond: QC_GT,
fieldName: fieldName,
val: val}
return f
}
func (this *dbFilter) GE(fieldName string, val string) DbQueryFilter {
f := &PageQueryOne{
cond: QC_GE,
fieldName: fieldName,
val: val}
return f
}
func (this *dbFilter) Custom(fieldName string, val string) DbQueryFilter {
f := &PageQueryOne{
cond: QC_CUSTOM,
fieldName: fieldName,
val: val}
return f
}
func (this *dbFilter) Comp(subFilters ...DbQueryFilter) DbQueryFilter {
f := &PageQueryComp{
subFilters: subFilters}
return f
}
func (this *PageQueryOne) IgnoreEmpy() DbQueryFilter {
this.ignoreEmpty = true
return this
}
func (this *PageQueryOne) And() DbQueryFilter {
this.linkOp = LK_AND
return this
}
func (this *PageQueryOne) Or() DbQueryFilter {
this.linkOp = LK_OR
return this
}
func (this *PageQueryOne) Not() DbQueryFilter {
this.not = true
return this
}
func (this *PageQueryOne) GetLinkOp() LinkOp {
return this.linkOp
}
func (this *PageQueryOne) GenSql() string {
if this.ignoreEmpty && this.val == "" {
return " "
}
sql := ""
switch this.cond {
case QC_LIKE:
sql = fmt.Sprintf("%s LIKE '%%%s%%'", this.fieldName, this.val)
case QC_EQ:
sql = fmt.Sprintf("%s = '%s'", this.fieldName, this.val)
case QC_LT:
sql = fmt.Sprintf("%s < '%s'", this.fieldName, this.val)
case QC_LE:
sql = fmt.Sprintf("%s <= '%s'", this.fieldName, this.val)
case QC_GT:
sql = fmt.Sprintf("%s > '%s'", this.fieldName, this.val)
case QC_GE:
sql = fmt.Sprintf("%s >= '%s'", this.fieldName, this.val)
case QC_CUSTOM:
sql = fmt.Sprintf("%s", this.val)
default:
panic("page query cond error")
}
if this.not {
sql = fmt.Sprintf(" NOT (%s) ", sql)
}
switch this.linkOp {
case LK_AND:
sql = fmt.Sprintf(" AND (%s) ", sql)
case LK_OR:
sql = fmt.Sprintf(" OR (%s) ", sql)
default:
panic("page query linkOp error")
}
return sql
}
func (this *PageQueryComp) And() DbQueryFilter {
this.linkOp = LK_AND
return this
}
func (this *PageQueryComp) Or() DbQueryFilter {
this.linkOp = LK_OR
return this
}
func (this *PageQueryComp) Not() DbQueryFilter {
this.not = true
return this
}
func (this *PageQueryComp) GetLinkOp() LinkOp {
return this.linkOp
}
func (this *PageQueryComp) GenSql() string {
if len(this.subFilters) <= 0 {
return " "
}
sql := " "
for index, filter := range this.subFilters {
if index == 0 {
if filter.GetLinkOp() == LK_AND {
sql = " 1=1 "
} else {
sql = " 1<>1 "
}
}
sql += filter.GenSql()
}
if this.not {
sql = fmt.Sprintf(" NOT (%s) ", sql)
}
switch this.linkOp {
case LK_AND:
sql = fmt.Sprintf(" AND (%s) ", sql)
case LK_OR:
sql = fmt.Sprintf(" OR (%s) ", sql)
default:
panic("page query linkOp error")
}
return sql
}