sdbc/insert_scope.go
2023-09-16 20:21:42 +08:00

53 lines
1.1 KiB
Go

package sdbc
import "context"
type _insert struct {
scope *scope
sdbc *Driver
ctx context.Context
selectOpts []interface{}
omitOpts []string
}
func (i *_insert) SetContext(ctx context.Context) *_insert {
i.ctx = ctx
return i
}
// SetSelect 只插入字段
func (i *_insert) SetSelect(selects ...any) *_insert {
i.selectOpts = selects
return i
}
// SetOmit 忽略字段
func (i *_insert) SetOmit(omits ...string) *_insert {
i.omitOpts = omits
return i
}
// Insert 插入数据
func (i *_insert) Insert(docs any) error {
db := i.sdbc.client
if len(i.selectOpts) != 0 {
db = db.Select(i.selectOpts[0], i.selectOpts[1:]...)
}
if len(i.omitOpts) != 0 {
db = db.Omit(i.omitOpts...)
}
return db.Model(i.scope.model.ptr()).Create(docs).Error
}
// InsertBatch 按批插入数据
func (i *_insert) InsertBatch(docs any) error {
db := i.sdbc.client
if len(i.selectOpts) != 0 {
db = db.Select(i.selectOpts[0], i.selectOpts[1:]...)
}
if len(i.omitOpts) != 0 {
db = db.Omit(i.omitOpts...)
}
return db.Model(i.scope.model.ptr()).CreateInBatches(docs, 1000).Error
}