go-common/app/admin/main/up/dao/utils.go
2019-04-22 18:49:16 +08:00

74 lines
1.6 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package dao
import "fmt"
const (
//logicNoOp = 0
logicOpAnd = 1
logicOpOr = 2
)
//Condition 生成的condition为
// [before] [key] [operator] ? [after]
// 如
// [(] [id] [=] ? [and]
// [ctime] [>] ? [)]
// [order by] [time] nil [] //如果value是nil不会设置placeholder ?
type Condition struct {
logicOp int
Before string
Key string
Operator string
Value interface{}
After string
}
//ConcatCondition concat conditions
func ConcatCondition(conditions ...Condition) (conditionStr string, args []interface{}, hasOperator bool) {
hasOperator = false
for _, c := range conditions {
var questionMark = "?"
if c.Value == nil {
questionMark = ""
}
if c.Operator != "" {
hasOperator = true
}
var logicOp = ""
switch c.logicOp {
case logicOpAnd:
logicOp = " and "
case logicOpOr:
logicOp = " or "
}
conditionStr += fmt.Sprintf(" %s %s %s %s %s %s", logicOp, c.Before, c.Key, c.Operator, questionMark, c.After)
if c.Value != nil {
args = append(args, c.Value)
}
}
return
}
//AndCondition and condition
func AndCondition(conditions ...Condition) (result []Condition) {
return addLogicOperator(logicOpAnd, conditions...)
}
//OrCondition or condition
func OrCondition(conditions ...Condition) (result []Condition) {
return addLogicOperator(logicOpOr, conditions...)
}
func addLogicOperator(operator int, conditions ...Condition) (result []Condition) {
var isFirst = true
for _, v := range conditions {
if isFirst {
isFirst = false
} else {
v.logicOp = operator
}
result = append(result, v)
}
return
}