github.com/ecodeclub/eorm@v0.0.2-0.20231001112437-dae71da914d0/predicate.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 eorm
    16  
    17  import operator "github.com/ecodeclub/eorm/internal/operator"
    18  
    19  // type op Operator.Op
    20  var (
    21  	opLT      = operator.OpLT
    22  	opLTEQ    = operator.OpLTEQ
    23  	opGT      = operator.OpGT
    24  	opGTEQ    = operator.OpGTEQ
    25  	opEQ      = operator.OpEQ
    26  	opNEQ     = operator.OpNEQ
    27  	opAdd     = operator.OpAdd
    28  	opMulti   = operator.OpMulti
    29  	opAnd     = operator.OpAnd
    30  	opOr      = operator.OpOr
    31  	opNot     = operator.OpNot
    32  	opIn      = operator.OpIn
    33  	opNotIN   = operator.OpNotIN
    34  	opFalse   = operator.OpFalse
    35  	opLike    = operator.OpLike
    36  	opNotLike = operator.OpNotLike
    37  	opExist   = operator.OpExist
    38  )
    39  
    40  // Predicate will be used in Where Or Having
    41  type Predicate binaryExpr
    42  
    43  var emptyPredicate = Predicate{}
    44  
    45  func (Predicate) expr() (string, error) {
    46  	return "", nil
    47  }
    48  
    49  // Exist indicates "Exist"
    50  func Exist(sub Subquery) Predicate {
    51  	return Predicate{
    52  		op:    opExist,
    53  		right: sub,
    54  	}
    55  }
    56  
    57  // Not indicates "NOT"
    58  func Not(p Predicate) Predicate {
    59  	return Predicate{
    60  		left:  Raw(""),
    61  		op:    opNot,
    62  		right: p,
    63  	}
    64  }
    65  
    66  // And indicates "AND"
    67  func (p Predicate) And(pred Predicate) Predicate {
    68  	return Predicate{
    69  		left:  p,
    70  		op:    opAnd,
    71  		right: pred,
    72  	}
    73  }
    74  
    75  // Or indicates "OR"
    76  func (p Predicate) Or(pred Predicate) Predicate {
    77  	return Predicate{
    78  		left:  p,
    79  		op:    opOr,
    80  		right: pred,
    81  	}
    82  }