-
Notifications
You must be signed in to change notification settings - Fork 1
/
update.go
39 lines (30 loc) · 888 Bytes
/
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
package gatsby
import "github.com/c9s/gatsby/sqlutils"
import "strconv"
func Update(executor Executor, val interface{}, driver int) *Result {
pkName := sqlutils.GetPrimaryKeyColumnName(val)
if pkName == nil {
panic("primary key column is not defined.")
}
sql, values := sqlutils.BuildUpdateClause(val, GetHolderTypeByDriver(driver))
var id = sqlutils.GetPrimaryKeyValue(val)
values = append(values, id)
// sql += fmt.Sprintf(" WHERE %s = $%d", *pkName, len(values))
if driver == DriverPg {
sql += " WHERE " + *pkName + " = $" + strconv.Itoa(len(values))
} else {
sql += " WHERE " + *pkName + " = ?"
}
stmt, err := executor.Prepare(sql)
if err != nil {
return NewErrorResult(err, sql)
}
defer stmt.Close()
res, err := stmt.Exec(values...)
if err != nil {
return NewErrorResult(err, sql)
}
result := NewResult(sql)
result.Result = res
return result
}