[feat] 调整 接口文件

This commit is contained in:
2024-05-08 22:58:27 +08:00
parent 91d88930c2
commit 50a0a1a85c
5 changed files with 62 additions and 63 deletions
+40 -16
View File
@@ -12,6 +12,30 @@ import (
"github.com/spf13/cast"
)
const (
SQL contracts.ConditionTokenType = "sql"
FUNC = "func"
PARAM = "param"
STRING = "string"
)
const (
IS_NULL contracts.ConditionOperator = "IS NULL"
IS_NOT_NULL = "IS NOT NULL"
EQ = "="
NE = "!="
GT = ">"
GE = ">="
LT = "<"
LE = "<="
LIKE = "LIKE"
NOT_LIKE = "NOT LIKE"
IN = "IN"
NOT_IN = "NOT IN"
REGEXP = "REGEXP"
NOT_REGEXP = "NOT REGEXP"
)
type ConditionExpr struct {
parent contracts.Condition
operator contracts.ConditionOperator
@@ -65,24 +89,24 @@ func (this ConditionExpr) ToSql(m contracts.ConditionTokenValue) db.Expression {
//
secondary := ""
switch operator {
case contracts.IS_NULL:
case contracts.IS_NOT_NULL:
case IS_NULL:
case IS_NOT_NULL:
secondary = ""
case contracts.EQ, contracts.NE,
contracts.GT, contracts.GE,
contracts.LT, contracts.LE,
contracts.REGEXP, contracts.NOT_REGEXP:
case EQ, NE,
GT, GE,
LT, LE,
REGEXP, NOT_REGEXP:
if this.GetTokenType() == contracts.SQL {
if this.GetTokenType() == SQL {
secondary = value
} else {
secondary = "'" + strings.Trim(value, "'") + "'"
}
case contracts.LIKE, contracts.NOT_LIKE:
case LIKE, NOT_LIKE:
secondary = "'%" + strings.Trim(value, "'") + "%'"
case contracts.IN, contracts.NOT_IN:
case IN, NOT_IN:
secondary = "(" + lo.Ternary(value == "", "''", value) + ")"
}
@@ -118,7 +142,7 @@ func (this ConditionExpr) GetTokenType() contracts.ConditionTokenType {
}
func (this *ConditionExpr) GetTokenSqlValue(m contracts.ConditionTokenValue) string {
if this.GetTokenType() == contracts.SQL {
if this.GetTokenType() == SQL {
return this.token
}
@@ -141,8 +165,8 @@ func (this *ConditionExpr) GetTokenSqlValue(m contracts.ConditionTokenValue) str
}
// 强制使用 in
if this.operator == contracts.EQ {
this.operator = contracts.IN
if this.operator == EQ {
this.operator = IN
}
return strings.Join(aStr, ", ")
@@ -156,14 +180,14 @@ func (this *ConditionExpr) GetTokenSqlValue(m contracts.ConditionTokenValue) str
func (this ConditionExpr) GetTokenValue(m contracts.ConditionTokenValue) any {
switch this.GetTokenType() {
case contracts.PARAM:
case PARAM:
if this.matchPrefix != "" {
return m.GetParam(fmt.Sprintf("%s.%s", this.matchPrefix, this.token)).Value()
}
return m.GetParam(this.token).Value()
case contracts.STRING:
case STRING:
return this.token
case contracts.FUNC:
case FUNC:
switch this.token {
case "UserID":
return m.GetGlobalParamsUser().ID()
@@ -187,7 +211,7 @@ func (this ConditionExpr) IsIgnoreEmptyParma(m contracts.ConditionTokenValue) bo
return false
}
if this.tokenType != contracts.PARAM {
if this.tokenType != PARAM {
return false
}