github.com/kunlun-qilian/sqlx/v3@v3.0.0/builder/addition_combination.go (about)

     1  package builder
     2  
     3  import (
     4  	"context"
     5  )
     6  
     7  type CombinationAddition struct {
     8  }
     9  
    10  func (CombinationAddition) AdditionType() AdditionType {
    11  	return AdditionCombination
    12  }
    13  
    14  func Union() *combination {
    15  	return &combination{
    16  		operator: "UNION",
    17  	}
    18  }
    19  
    20  func Intersect() *combination {
    21  	return &combination{
    22  		operator: "INTERSECT",
    23  	}
    24  }
    25  
    26  func Expect() *combination {
    27  	return &combination{
    28  		operator: "EXCEPT",
    29  	}
    30  }
    31  
    32  var _ Addition = (*combination)(nil)
    33  
    34  type combination struct {
    35  	CombinationAddition
    36  	operator   string // UNION | INTERSECT | EXCEPT
    37  	method     string // ALL | DISTINCT
    38  	stmtSelect SelectStatement
    39  }
    40  
    41  func (c *combination) IsNil() bool {
    42  	return c == nil || IsNilExpr(c.stmtSelect)
    43  }
    44  
    45  func (c combination) All(stmtSelect SelectStatement) *combination {
    46  	c.method = "ALL"
    47  	c.stmtSelect = stmtSelect
    48  	return &c
    49  }
    50  
    51  func (c combination) Distinct(stmtSelect SelectStatement) *combination {
    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  }