github.com/ecodeclub/eorm@v0.0.2-0.20231001112437-dae71da914d0/internal/operator/operator.go (about)

     1  // Copyright 2021 ecodeclub
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  // http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package Operator
    16  
    17  import "github.com/ecodeclub/eorm/internal/errs"
    18  
    19  type Op struct {
    20  	Symbol string
    21  	Text   string
    22  }
    23  
    24  var emptyOp = Op{}
    25  
    26  var (
    27  	OpLT   = Op{Symbol: "<", Text: "<"}
    28  	OpLTEQ = Op{Symbol: "<=", Text: "<="}
    29  	OpGT   = Op{Symbol: ">", Text: ">"}
    30  	OpGTEQ = Op{Symbol: ">=", Text: ">="}
    31  	OpEQ   = Op{Symbol: "=", Text: "="}
    32  	OpNEQ  = Op{Symbol: "!=", Text: "!="}
    33  	OpAdd  = Op{Symbol: "+", Text: "+"}
    34  	// OpIn   = Op{Symbol: "IN", Text: " IN "}
    35  	// OpMinus = Op{Symbol:"-", Text: "-"}
    36  	OpMulti = Op{Symbol: "*", Text: "*"}
    37  	// OpDiv = Op{Symbol:"/", Text: "/"}
    38  	OpAnd     = Op{Symbol: "AND", Text: " AND "}
    39  	OpOr      = Op{Symbol: "OR", Text: " OR "}
    40  	OpNot     = Op{Symbol: "NOT", Text: "NOT "}
    41  	OpIn      = Op{Symbol: "IN", Text: " IN "}
    42  	OpNotIN   = Op{Symbol: "NOT IN", Text: " NOT IN "}
    43  	OpFalse   = Op{Symbol: "FALSE", Text: "FALSE"}
    44  	OpLike    = Op{Symbol: "LIKE", Text: " LIKE "}
    45  	OpNotLike = Op{Symbol: "NOT LIKE", Text: " NOT LIKE "}
    46  	OpExist   = Op{Symbol: "EXIST", Text: "EXIST "}
    47  )
    48  
    49  func NegateOp(op Op) (Op, error) {
    50  	switch op {
    51  	case OpNEQ:
    52  		return OpEQ, nil
    53  	case OpEQ:
    54  		return OpNEQ, nil
    55  	case OpIn:
    56  		return OpNotIN, nil
    57  	case OpNotIN:
    58  		return OpIn, nil
    59  	case OpGT:
    60  		return OpLTEQ, nil
    61  	case OpLT:
    62  		return OpGTEQ, nil
    63  	case OpGTEQ:
    64  		return OpLT, nil
    65  	case OpLTEQ:
    66  		return OpGT, nil
    67  	default:
    68  		return emptyOp, errs.NewUnsupportedOperatorError(op.Text)
    69  	}
    70  }