f5/pagequery.go
2023-08-20 13:07:00 +08:00

243 lines
4.2 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 pageQuery struct {
}
type PageQueryFilter interface {
And() PageQueryFilter
Or() PageQueryFilter
Not() PageQueryFilter
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 []PageQueryFilter
}
type PageQueryParam struct {
Filter *PageQueryComp
OrderBy string
}
func (this *PageQueryParam) genSql() string {
sql := " "
/*
for _, field := range this.Filter {
}*/
sql += " " + this.OrderBy + " "
return sql
}
func (this *pageQuery) init() {
}
func (this *pageQuery) unInit() {
}
func (this *pageQuery) Like(fieldName string, val string) PageQueryFilter {
f := &PageQueryOne{
cond: QC_LIKE,
fieldName: fieldName,
val: val}
return f
}
func (this *pageQuery) EQ(fieldName string, val string) PageQueryFilter {
f := &PageQueryOne{
cond: QC_EQ,
fieldName: fieldName,
val: val}
return f
}
func (this *pageQuery) LT(fieldName string, val string) PageQueryFilter {
f := &PageQueryOne{
cond: QC_LT,
fieldName: fieldName,
val: val}
return f
}
func (this *pageQuery) LE(fieldName string, val string) PageQueryFilter {
f := &PageQueryOne{
cond: QC_LE,
fieldName: fieldName,
val: val}
return f
}
func (this *pageQuery) GT(fieldName string, val string) PageQueryFilter {
f := &PageQueryOne{
cond: QC_GT,
fieldName: fieldName,
val: val}
return f
}
func (this *pageQuery) GE(fieldName string, val string) PageQueryFilter {
f := &PageQueryOne{
cond: QC_GE,
fieldName: fieldName,
val: val}
return f
}
func (this *pageQuery) Custom(fieldName string, val string) PageQueryFilter {
f := &PageQueryOne{
cond: QC_CUSTOM,
fieldName: fieldName,
val: val}
return f
}
func (this *pageQuery) Comp(subFilters ...PageQueryFilter) PageQueryFilter {
f := &PageQueryComp{
subFilters: subFilters}
return f
}
func (this *PageQueryOne) IgnoreEmpy() PageQueryFilter {
this.ignoreEmpty = true
return this
}
func (this *PageQueryOne) And() PageQueryFilter {
this.linkOp = LK_AND
return this
}
func (this *PageQueryOne) Or() PageQueryFilter {
this.linkOp = LK_OR
return this
}
func (this *PageQueryOne) Not() PageQueryFilter {
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() PageQueryFilter {
this.linkOp = LK_AND
return this
}
func (this *PageQueryComp) Or() PageQueryFilter {
this.linkOp = LK_OR
return this
}
func (this *PageQueryComp) Not() PageQueryFilter {
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
}