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

     1  package sqlbuilder
     2  
     3  import (
     4  	"context"
     5  )
     6  
     7  type CombinationAddition interface {
     8  	Addition
     9  	All(stmtSelect SelectStatement) CombinationAddition
    10  	Distinct(stmtSelect SelectStatement) CombinationAddition
    11  }
    12  
    13  func Union() CombinationAddition {
    14  	return &combination{
    15  		operator: "UNION",
    16  	}
    17  }
    18  
    19  func Intersect() CombinationAddition {
    20  	return &combination{
    21  		operator: "INTERSECT",
    22  	}
    23  }
    24  
    25  func Expect() CombinationAddition {
    26  	return &combination{
    27  		operator: "EXCEPT",
    28  	}
    29  }
    30  
    31  type combination struct {
    32  	operator   string // UNION | INTERSECT | EXCEPT
    33  	method     string // ALL | DISTINCT
    34  	stmtSelect SelectStatement
    35  }
    36  
    37  func (combination) AdditionType() AdditionType {
    38  	return AdditionCombination
    39  }
    40  
    41  func (c *combination) IsNil() bool {
    42  	return c == nil || IsNilExpr(c.stmtSelect)
    43  }
    44  
    45  func (c combination) All(stmtSelect SelectStatement) CombinationAddition {
    46  	c.method = "ALL"
    47  	c.stmtSelect = stmtSelect
    48  	return &c
    49  }
    50  
    51  func (c combination) Distinct(stmtSelect SelectStatement) CombinationAddition {
    52  	c.method = "DISTINCT"
    53  	c.stmtSelect = stmtSelect
    54  	return &c
    55  }
    56  
    57  func (c *combination) Ex(ctx context.Context) *Ex {
    58  	e := Expr("")
    59  	e.Grow(1)
    60  
    61  	e.WriteQuery(c.operator)
    62  	e.WriteQueryByte(' ')
    63  
    64  	if c.method != "" {
    65  		e.WriteQuery(c.method)
    66  		e.WriteQueryByte(' ')
    67  	}
    68  
    69  	e.WriteExpr(c.stmtSelect)
    70  
    71  	return e.Ex(ctx)
    72  }