github.com/eden-framework/sqlx@v0.0.2/builder/condition.go (about)

     1  package builder
     2  
     3  import (
     4  	"context"
     5  )
     6  
     7  func EmptyCond() SqlCondition {
     8  	return (*Condition)(nil)
     9  }
    10  
    11  type SqlCondition interface {
    12  	SqlExpr
    13  	SqlConditionMarker
    14  
    15  	And(cond SqlCondition) SqlCondition
    16  	Or(cond SqlCondition) SqlCondition
    17  	Xor(cond SqlCondition) SqlCondition
    18  }
    19  
    20  type SqlConditionMarker interface {
    21  	asCondition()
    22  }
    23  
    24  func AsCond(ex SqlExpr) *Condition {
    25  	return &Condition{expr: ex}
    26  }
    27  
    28  type Condition struct {
    29  	expr SqlExpr
    30  	SqlConditionMarker
    31  }
    32  
    33  func (c *Condition) Ex(ctx context.Context) *Ex {
    34  	if IsNilExpr(c.expr) {
    35  		return nil
    36  	}
    37  	return c.expr.Ex(ctx)
    38  }
    39  
    40  func (c *Condition) IsNil() bool {
    41  	return c == nil || IsNilExpr(c.expr)
    42  }
    43  
    44  func (c *Condition) And(cond SqlCondition) SqlCondition {
    45  	return And(c, cond)
    46  }
    47  
    48  func (c *Condition) Or(cond SqlCondition) SqlCondition {
    49  	return Or(c, cond)
    50  }
    51  
    52  func (c *Condition) Xor(cond SqlCondition) SqlCondition {
    53  	return Xor(c, cond)
    54  }
    55  
    56  func And(conditions ...SqlCondition) SqlCondition {
    57  	return composedCondition("AND", conditions...)
    58  }
    59  
    60  func Or(conditions ...SqlCondition) SqlCondition {
    61  	return composedCondition("OR", conditions...)
    62  }
    63  
    64  func Xor(conditions ...SqlCondition) SqlCondition {
    65  	return composedCondition("XOR", conditions...)
    66  }
    67  
    68  func composedCondition(op string, conditions ...SqlCondition) SqlCondition {
    69  	final := filterNilCondition(conditions...)
    70  
    71  	if len(final) == 0 {
    72  		return nil
    73  	}
    74  
    75  	if len(final) == 1 {
    76  		return final[0]
    77  	}
    78  
    79  	return &ComposedCondition{op: op, conditions: final}
    80  }
    81  
    82  func filterNilCondition(conditions ...SqlCondition) []SqlCondition {
    83  	finalConditions := make([]SqlCondition, 0)
    84  
    85  	for i := range conditions {
    86  		condition := conditions[i]
    87  		if IsNilExpr(condition) {
    88  			continue
    89  		}
    90  		finalConditions = append(finalConditions, condition)
    91  	}
    92  
    93  	return finalConditions
    94  }
    95  
    96  type ComposedCondition struct {
    97  	op         string
    98  	conditions []SqlCondition
    99  	SqlConditionMarker
   100  }
   101  
   102  func (c *ComposedCondition) And(cond SqlCondition) SqlCondition {
   103  	return And(c, cond)
   104  }
   105  
   106  func (c *ComposedCondition) Or(cond SqlCondition) SqlCondition {
   107  	return Or(c, cond)
   108  }
   109  
   110  func (c *ComposedCondition) Xor(cond SqlCondition) SqlCondition {
   111  	return Xor(c, cond)
   112  }
   113  
   114  func (c *ComposedCondition) IsNil() bool {
   115  	return c == nil || c.op == "" || len(c.conditions) == 0
   116  }
   117  
   118  func (c *ComposedCondition) Ex(ctx context.Context) *Ex {
   119  	e := Expr("")
   120  
   121  	count := 0
   122  
   123  	for i := range c.conditions {
   124  		condition := c.conditions[i]
   125  		if condition == nil || condition.IsNil() {
   126  			continue
   127  		}
   128  
   129  		if count > 0 {
   130  			e.WriteByte(' ')
   131  			e.WriteString(c.op)
   132  			e.WriteByte(' ')
   133  		}
   134  
   135  		e.WriteGroup(func(e *Ex) {
   136  			e.WriteExpr(condition)
   137  		})
   138  
   139  		count++
   140  	}
   141  
   142  	return e.Ex(ctx)
   143  }