1 Star 1 Fork 0

bigbase/pg

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
update.go 4.00 KB
一键复制 编辑 原始数据 按行查看 历史
package orm
import (
"errors"
"fmt"
"reflect"
"github.com/go-pg/pg/internal"
)
func Update(db DB, model ...interface{}) error {
res, err := NewQuery(db, model...).Update()
if err != nil {
return err
}
return internal.AssertOneRow(res.RowsAffected())
}
type updateQuery struct {
q *Query
omitZero bool
}
var _ QueryAppender = (*updateQuery)(nil)
func (q updateQuery) Copy() QueryAppender {
return updateQuery{
q: q.q.Copy(),
}
}
func (q updateQuery) Query() *Query {
return q.q
}
func (q updateQuery) AppendQuery(b []byte) ([]byte, error) {
if q.q.stickyErr != nil {
return nil, q.q.stickyErr
}
var err error
if len(q.q.with) > 0 {
b, err = q.q.appendWith(b)
if err != nil {
return nil, err
}
}
b = append(b, "UPDATE "...)
b = q.q.appendFirstTableWithAlias(b)
b, err = q.mustAppendSet(b)
if err != nil {
return nil, err
}
if q.q.hasOtherTables() || q.modelHasData() {
b = append(b, " FROM "...)
b = q.q.appendOtherTables(b)
b, err = q.appendModelData(b)
if err != nil {
return nil, err
}
}
b, err = q.q.mustAppendWhere(b)
if err != nil {
return nil, err
}
if len(q.q.returning) > 0 {
b = q.q.appendReturning(b)
}
return b, nil
}
func (q *updateQuery) modelHasData() bool {
if !q.q.hasModel() {
return false
}
v := q.q.model.Value()
return v.Kind() == reflect.Slice && v.Len() > 0
}
func (q updateQuery) mustAppendSet(b []byte) ([]byte, error) {
if len(q.q.set) > 0 {
b = q.q.appendSet(b)
return b, nil
}
if q.q.model == nil {
return nil, errors.New("pg: Model is nil")
}
b = append(b, " SET "...)
value := q.q.model.Value()
var err error
if value.Kind() == reflect.Struct {
b, err = q.appendSetStruct(b, value)
} else {
if q.modelHasData() {
b, err = q.appendSetSlice(b, value)
} else {
err = fmt.Errorf("pg: slice %s is empty", value.Type())
}
}
if err != nil {
return nil, err
}
return b, nil
}
func (q updateQuery) appendSetStruct(b []byte, strct reflect.Value) ([]byte, error) {
fields, err := q.q.getFields()
if err != nil {
return nil, err
}
if len(fields) == 0 {
fields = q.q.model.Table().DataFields
}
pos := len(b)
for _, f := range fields {
if q.omitZero && f.OmitZero(strct) {
continue
}
if len(b) != pos {
b = append(b, ", "...)
pos = len(b)
}
b = append(b, f.Column...)
b = append(b, " = "...)
b = f.AppendValue(b, strct, 1)
}
return b, nil
}
func (q updateQuery) appendSetSlice(b []byte, slice reflect.Value) ([]byte, error) {
fields, err := q.q.getFields()
if err != nil {
return nil, err
}
if len(fields) == 0 {
fields = q.q.model.Table().DataFields
}
for i, f := range fields {
if i > 0 {
b = append(b, ", "...)
}
b = append(b, f.Column...)
b = append(b, " = "...)
b = append(b, "_data."...)
b = append(b, f.Column...)
}
return b, nil
}
func (q updateQuery) appendModelData(b []byte) ([]byte, error) {
if !q.q.hasModel() {
return b, nil
}
v := q.q.model.Value()
if v.Kind() != reflect.Slice || v.Len() == 0 {
return b, nil
}
columns, err := q.q.getDataFields()
if err != nil {
return nil, err
}
if len(columns) > 0 {
columns = append(columns, q.q.model.Table().PKs...)
} else {
columns = q.q.model.Table().Fields
}
return appendSliceValues(b, columns, v), nil
}
func appendSliceValues(b []byte, fields []*Field, slice reflect.Value) []byte {
b = append(b, "(VALUES ("...)
for i := 0; i < slice.Len(); i++ {
el := slice.Index(i)
if el.Kind() == reflect.Interface {
el = el.Elem()
}
b = appendValues(b, fields, reflect.Indirect(el))
if i != slice.Len()-1 {
b = append(b, "), ("...)
}
}
b = append(b, ")) AS _data("...)
b = appendColumns(b, fields)
b = append(b, ")"...)
return b
}
func appendValues(b []byte, fields []*Field, v reflect.Value) []byte {
for i, f := range fields {
if i > 0 {
b = append(b, ", "...)
}
if f.OmitZero(v) {
b = append(b, "NULL"...)
} else {
b = f.AppendValue(b, v, 1)
}
if f.HasFlag(customTypeFlag) {
b = append(b, "::"...)
b = append(b, f.SQLType...)
}
}
return b
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/bigbase/pg.git
git@gitee.com:bigbase/pg.git
bigbase
pg
pg
v6.7.2

搜索帮助