···6767 GroupBy("department").
6868 OrderBy("name", Ascending).
6969 Limit(10),
7070- expectedSql: "SELECT name, age, department FROM users WHERE ((active) = (?)) and ((age) > (?)) GROUP BY department ORDER BY name asc LIMIT 10",
7070+ expectedSql: "SELECT name, age, department FROM users WHERE ((active) = (?)) AND ((age) > (?)) GROUP BY department ORDER BY name asc LIMIT 10",
7171 expectedArgs: []any{true, 18},
7272 },
7373 }
···184184 expectedRows: 2,
185185 },
186186 {
187187- name: "Select users with salary between 70000 and 80000",
187187+ name: "Select users with salary between 70000 AND 80000",
188188 stmt: Select("name", "salary").
189189 From("users").
190190 Where(Gte("salary", 70000.0).And(Lte("salary", 80000.0))),
+143
update.go
···11+package norm
22+33+import (
44+ "context"
55+ "database/sql"
66+ "fmt"
77+ "strings"
88+)
99+1010+type update struct {
1111+ table string
1212+ or UpdateOr
1313+ sets []struct {
1414+ col string
1515+ val any
1616+ }
1717+ where *Expr
1818+}
1919+2020+type UpdateOr int
2121+2222+const (
2323+ UpdateNone UpdateOr = iota
2424+ UpdateAbort
2525+ UpdateFail
2626+ UpdateIgnore
2727+ UpdateReplace
2828+ UpdateRollback
2929+)
3030+3131+func (u UpdateOr) String() string {
3232+ switch u {
3333+ case UpdateAbort:
3434+ return "ABORT"
3535+ case UpdateFail:
3636+ return "FAIL"
3737+ case UpdateIgnore:
3838+ return "IGNORE"
3939+ case UpdateReplace:
4040+ return "REPLACE"
4141+ case UpdateRollback:
4242+ return "ROLLBACK"
4343+ default:
4444+ return ""
4545+ }
4646+}
4747+4848+func Update(table string) update {
4949+ return update{table: table}
5050+}
5151+5252+type UpdateOpt func(s *update)
5353+5454+func (u update) Or(option UpdateOr) update {
5555+ u.or = option
5656+ return u
5757+}
5858+5959+func (u update) Set(column string, value any) update {
6060+ u.sets = append(u.sets, struct {
6161+ col string
6262+ val any
6363+ }{
6464+ column, value,
6565+ })
6666+ return u
6767+}
6868+6969+func (u update) Sets(values map[string]any) update {
7070+ for column, value := range values {
7171+ u = u.Set(column, value)
7272+ }
7373+ return u
7474+}
7575+7676+func (u update) Where(expr Expr) update {
7777+ u.where = &expr
7878+ return u
7979+}
8080+8181+func (u update) Compile() (string, []any, error) {
8282+ var sql strings.Builder
8383+ var args []any
8484+8585+ sql.WriteString("UPDATE ")
8686+8787+ orKw := u.or.String()
8888+ if orKw != "" {
8989+ sql.WriteString("OR ")
9090+ sql.WriteString(u.or.String())
9191+ sql.WriteString(" ")
9292+ }
9393+9494+ if u.table == "" {
9595+ return "", nil, fmt.Errorf("table name is required")
9696+ }
9797+ sql.WriteString(u.table)
9898+9999+ if len(u.sets) == 0 {
100100+ return "", nil, fmt.Errorf("no SET clauses supplied")
101101+ }
102102+103103+ sql.WriteString(" SET ")
104104+105105+ for i, set := range u.sets {
106106+ if i != 0 {
107107+ sql.WriteString(", ")
108108+ }
109109+ sql.WriteString(set.col)
110110+ sql.WriteString(" = ?")
111111+ args = append(args, set.val)
112112+ }
113113+114114+ if u.where != nil {
115115+ sql.WriteString(" WHERE ")
116116+ sql.WriteString(u.where.String())
117117+118118+ args = append(args, u.where.Binds()...)
119119+ }
120120+121121+ return sql.String(), args, nil
122122+}
123123+124124+func (u update) MustCompile() (string, []any) {
125125+ sql, args, err := u.Compile()
126126+ if err != nil {
127127+ panic(err)
128128+ }
129129+130130+ return sql, args
131131+}
132132+133133+func (u update) Build(p Database) (*sql.Stmt, []any, error) { return Build(u, p) }
134134+func (u update) MustBuild(p Database) (*sql.Stmt, []any) { return MustBuild(u, p) }
135135+136136+func (u update) Exec(p Database) (sql.Result, error) { return Exec(u, p) }
137137+func (u update) ExecContext(ctx context.Context, p Database) (sql.Result, error) {
138138+ return ExecContext(ctx, u, p)
139139+}
140140+func (u update) MustExec(p Database) sql.Result { return MustExec(u, p) }
141141+func (u update) MustExecContext(ctx context.Context, p Database) sql.Result {
142142+ return MustExecContext(ctx, u, p)
143143+}