-
Notifications
You must be signed in to change notification settings - Fork 0
/
update.go
53 lines (41 loc) · 1.21 KB
/
update.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package jdb
import (
"bytes"
"context"
)
var updateColumnsSQL = "parent_kind = ?, parent_id = ?, unique_string_key = ?, string_key = ?, numeric_key = ?, time_key = ?, data = ?, update_time = "
type UpdateBuilder struct {
q *Query
wb *WhereBuilder
value interface{}
}
func newUpdateBuilder(q *Query, wb *WhereBuilder, value interface{}) *UpdateBuilder {
if wb == nil {
wb = newWhereBuilder(q)
}
return &UpdateBuilder{q: q, wb: wb, value: value}
}
func (b *UpdateBuilder) Exec(ctx context.Context, tx *Tx) error {
_, err := tx.exec(ctx, b)
return err
}
func (b *UpdateBuilder) ToSQL() (string, []interface{}, error) {
var params []interface{}
query := &bytes.Buffer{}
r, err := rowScanInput(b.q.kind, b.value)
if err != nil {
return "", nil, err
}
query.WriteString("UPDATE ")
query.WriteString(b.q.table)
query.WriteString(" SET ")
query.WriteString(updateColumnsSQL)
query.WriteString(b.q.d.TimestampExpression())
query.WriteString(" ")
params = append(params, r.ParentKind, r.ParentID, r.UniqueStringKey, r.StringKey, r.NumericKey, r.TimeKey, r.Data)
err = b.wb.toWhereSQL(query, ¶ms)
if err != nil {
return "", nil, err
}
return b.q.d.ReplacePlaceHolders(query.String()), params, nil
}