github.com/machinefi/w3bstream@v1.6.5-rc9.0.20240426031326-b8c7c4876e72/pkg/depends/kit/sqlx/builder/builder_stmt_select.go (about)

     1  package builder
     2  
     3  import "context"
     4  
     5  type SelectStatement interface {
     6  	SqlExpr
     7  	selectStatement()
     8  }
     9  
    10  type StmtSelect struct {
    11  	SelectStatement
    12  
    13  	expr      SqlExpr
    14  	tbl       *Table
    15  	modifiers []string
    16  	adds      []Addition
    17  }
    18  
    19  func Select(e SqlExpr, modifiers ...string) *StmtSelect {
    20  	return &StmtSelect{expr: e, modifiers: modifiers}
    21  }
    22  
    23  func (s *StmtSelect) IsNil() bool { return s == nil }
    24  
    25  func (s StmtSelect) From(tbl *Table, adds ...Addition) *StmtSelect {
    26  	s.tbl, s.adds = tbl, adds
    27  	return &s
    28  }
    29  
    30  func (s *StmtSelect) Ex(ctx context.Context) *Ex {
    31  	multi := false
    32  
    33  	for i := range s.adds {
    34  		add := s.adds[i]
    35  		if IsNilExpr(add) {
    36  			continue
    37  		}
    38  		if add.AdditionKind() == AdditionJoin {
    39  			multi = true
    40  		}
    41  	}
    42  	if multi {
    43  		ctx = ContextWithToggleMultiTable(ctx, true)
    44  	}
    45  
    46  	e := Expr("SELECT")
    47  	e.Grow(len(s.adds) + 2)
    48  
    49  	if len(s.modifiers) > 0 {
    50  		for i := range s.modifiers {
    51  			e.WriteQueryByte(' ')
    52  			e.WriteQuery(s.modifiers[i])
    53  		}
    54  	}
    55  	expr := s.expr
    56  	if IsNilExpr(expr) {
    57  		expr = Expr("*")
    58  	}
    59  	e.WriteQueryByte(' ')
    60  	e.WriteExpr(expr)
    61  
    62  	if !IsNilExpr(s.tbl) {
    63  		e.WriteQuery(" FROM ")
    64  		e.WriteExpr(s.tbl)
    65  	}
    66  	WriteAdditions(e, s.adds...)
    67  	return e.Ex(ctx)
    68  }
    69  
    70  func ForUpdate() *addition { return AsAddition(Expr("FOR UPDATE")) }