github.com/octohelm/storage@v0.0.0-20240516030302-1ac2cc1ea347/pkg/sqlbuilder/condition.go (about)

     1  package sqlbuilder
     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  
    16  type SqlConditionMarker interface {
    17  	asCondition()
    18  }
    19  
    20  func AsCond(ex SqlExpr) *Condition {
    21  	if c, ok := ex.(*Condition); ok {
    22  		return c
    23  	}
    24  	return &Condition{expr: ex}
    25  }
    26  
    27  type Condition struct {
    28  	expr SqlExpr
    29  	SqlConditionMarker
    30  }
    31  
    32  func (c *Condition) Ex(ctx context.Context) *Ex {
    33  	if IsNilExpr(c.expr) {
    34  		return nil
    35  	}
    36  	return c.expr.Ex(ctx)
    37  }
    38  
    39  func (c *Condition) IsNil() bool {
    40  	return c == nil || IsNilExpr(c.expr)
    41  }
    42  
    43  func And(conditions ...SqlExpr) SqlCondition {
    44  	return composedCondition("AND", filterNilCondition(conditions)...)
    45  }
    46  
    47  func Or(conditions ...SqlExpr) SqlCondition {
    48  	return composedCondition("OR", filterNilCondition(conditions)...)
    49  }
    50  
    51  func Xor(conditions ...SqlExpr) SqlCondition {
    52  	return composedCondition("XOR", filterNilCondition(conditions)...)
    53  }
    54  
    55  func filterNilCondition(conditions []SqlExpr) []SqlCondition {
    56  	finals := make([]SqlCondition, 0, len(conditions))
    57  
    58  	for i := range conditions {
    59  		condition := AsCond(conditions[i])
    60  		if IsNilExpr(condition) {
    61  			continue
    62  		}
    63  		finals = append(finals, condition)
    64  	}
    65  
    66  	return finals
    67  }
    68  
    69  func composedCondition(op string, conditions ...SqlCondition) SqlCondition {
    70  	return &ComposedCondition{op: op, conditions: conditions}
    71  }
    72  
    73  type ComposedCondition struct {
    74  	SqlConditionMarker
    75  
    76  	op         string
    77  	conditions []SqlCondition
    78  }
    79  
    80  func (c *ComposedCondition) And(cond SqlCondition) SqlCondition {
    81  	return And(c, cond)
    82  }
    83  
    84  func (c *ComposedCondition) Or(cond SqlCondition) SqlCondition {
    85  	return Or(c, cond)
    86  }
    87  
    88  func (c *ComposedCondition) Xor(cond SqlCondition) SqlCondition {
    89  	return Xor(c, cond)
    90  }
    91  
    92  func (c *ComposedCondition) IsNil() bool {
    93  	if c == nil {
    94  		return true
    95  	}
    96  	if c.op == "" {
    97  		return true
    98  	}
    99  
   100  	isNil := true
   101  
   102  	for i := range c.conditions {
   103  		if !IsNilExpr(c.conditions[i]) {
   104  			isNil = false
   105  			continue
   106  		}
   107  	}
   108  
   109  	return isNil
   110  }
   111  
   112  func (c *ComposedCondition) Ex(ctx context.Context) *Ex {
   113  	e := Expr("")
   114  	e.Grow(len(c.conditions))
   115  
   116  	for i := range c.conditions {
   117  		condition := c.conditions[i]
   118  
   119  		if i > 0 {
   120  			e.WriteQueryByte(' ')
   121  			e.WriteQuery(c.op)
   122  			e.WriteQueryByte(' ')
   123  		}
   124  
   125  		e.WriteGroup(func(e *Ex) {
   126  			e.WriteExpr(condition)
   127  		})
   128  	}
   129  
   130  	return e.Ex(ctx)
   131  }