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

     1  package builder
     2  
     3  import "context"
     4  
     5  type StmtInsert struct {
     6  	tbl         *Table
     7  	modifiers   []string
     8  	assignments []*Assignment
     9  	adds        Additions
    10  }
    11  
    12  func Insert(modifiers ...string) *StmtInsert {
    13  	return &StmtInsert{modifiers: modifiers}
    14  }
    15  
    16  func (s StmtInsert) Into(tbl *Table, adds ...Addition) *StmtInsert {
    17  	s.tbl, s.adds = tbl, adds
    18  	return &s
    19  }
    20  
    21  func (s StmtInsert) Values(cols *Columns, values ...interface{}) *StmtInsert {
    22  	s.assignments = Assignments{ColumnsAndValues(cols, values...)}
    23  	return &s
    24  }
    25  
    26  func (s *StmtInsert) IsNil() bool {
    27  	return s == nil || s.tbl == nil || len(s.assignments) == 0
    28  }
    29  
    30  func (s *StmtInsert) Ex(ctx context.Context) *Ex {
    31  	e := Expr("INSERT")
    32  	if len(s.modifiers) > 0 {
    33  		for i := range s.modifiers {
    34  			e.WriteQueryByte(' ')
    35  			e.WriteQuery(s.modifiers[i])
    36  		}
    37  	}
    38  	e.WriteQuery(" INTO ")
    39  	e.WriteExpr(s.tbl)
    40  	e.WriteQueryByte(' ')
    41  
    42  	e.WriteExpr(ExprBy(func(ctx context.Context) *Ex {
    43  		e := Expr("")
    44  		e.Grow(len(s.assignments))
    45  		ctx = ContextWithToggleUseValues(ctx, true)
    46  		WriteAssignments(e, s.assignments...)
    47  		return e.Ex(ctx)
    48  	}))
    49  	WriteAdditions(e, s.adds...)
    50  	return e.Ex(ctx)
    51  }
    52  
    53  // TODO OnDuplicateKeyUpdate (mysql feature)
    54  // TODO Returning