github.com/artisanhe/tools@v1.0.1-0.20210607022958-19a8fef2eb04/sqlx/builder/condition.go (about)

     1  package builder
     2  
     3  import (
     4  	"container/list"
     5  	"fmt"
     6  )
     7  
     8  type Condition Expression
     9  
    10  func NewCondRules() *CondRules {
    11  	return &CondRules{}
    12  }
    13  
    14  type CondRules struct {
    15  	l *list.List
    16  }
    17  
    18  type CondRule struct {
    19  	When       bool
    20  	Conditions []*Condition
    21  }
    22  
    23  func (rules *CondRules) When(rule bool, conds ...*Condition) *CondRules {
    24  	if rules.l == nil {
    25  		rules.l = list.New()
    26  	}
    27  	rules.l.PushBack(&CondRule{
    28  		When:       rule,
    29  		Conditions: conds,
    30  	})
    31  	return rules
    32  }
    33  
    34  func (rules *CondRules) ToCond() *Condition {
    35  	if rules.l == nil {
    36  		return nil
    37  	}
    38  
    39  	list := make([]*Condition, 0)
    40  	i := 0
    41  	for e := rules.l.Front(); e != nil; e = e.Next() {
    42  		r := e.Value.(*CondRule)
    43  		if r.When {
    44  			list = append(list, r.Conditions...)
    45  		}
    46  		i++
    47  	}
    48  	if len(list) == 0 {
    49  		return nil
    50  	}
    51  	return And(list...)
    52  }
    53  
    54  func (c *Condition) Expr() *Expression {
    55  	return (*Expression)(c)
    56  }
    57  
    58  func (c Condition) And(cond *Condition) *Condition {
    59  	return (*Condition)(Expr(
    60  		fmt.Sprintf("(%s) AND (%s)", c.Query, cond.Query),
    61  		append(c.Args, cond.Args...)...,
    62  	))
    63  }
    64  
    65  func (c Condition) Or(cond *Condition) *Condition {
    66  	return (*Condition)(Expr(
    67  		fmt.Sprintf("(%s) OR (%s)", c.Query, cond.Query),
    68  		append(c.Args, cond.Args...)...,
    69  	))
    70  }
    71  
    72  func (c Condition) Xor(cond *Condition) *Condition {
    73  	return (*Condition)(Expr(
    74  		fmt.Sprintf("(%s) XOR (%s)", c.Query, cond.Query),
    75  		append(c.Args, cond.Args...)...,
    76  	))
    77  }
    78  
    79  func And(condList ...*Condition) (cond *Condition) {
    80  	for _, c := range condList {
    81  		if c == nil {
    82  			continue
    83  		}
    84  		if cond == nil {
    85  			cond = c
    86  			continue
    87  		}
    88  		cond = cond.And(c)
    89  	}
    90  	return
    91  }
    92  
    93  func Or(condList ...*Condition) (cond *Condition) {
    94  	for _, c := range condList {
    95  		if c == nil {
    96  			continue
    97  		}
    98  		if cond == nil {
    99  			cond = c
   100  			continue
   101  		}
   102  		cond = cond.Or(c)
   103  	}
   104  	return
   105  }
   106  
   107  func Xor(condList ...*Condition) (cond *Condition) {
   108  	for _, c := range condList {
   109  		if c == nil {
   110  			continue
   111  		}
   112  		if cond == nil {
   113  			cond = c
   114  			continue
   115  		}
   116  		cond = cond.Xor(c)
   117  	}
   118  	return
   119  }