Skip to content
This repository was archived by the owner on Sep 7, 2021. It is now read-only.
This repository is currently being migrated. It's locked while the migration is in progress.
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion session_insert.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,16 @@ func (session *Session) Insert(beans ...interface{}) (int64, error) {
defer session.Close()
}

var copyOfOmitColMap columnMap
var copyOfOmitStr string

if len(session.statement.omitColumnMap) > 0 {
copyOfOmitColMap = make([]string, len(session.statement.omitColumnMap))
copy(copyOfOmitColMap, session.statement.omitColumnMap)

copyOfOmitStr = session.statement.OmitStr
}

for _, bean := range beans {
sliceValue := reflect.Indirect(reflect.ValueOf(bean))
if sliceValue.Kind() == reflect.Slice {
Expand All @@ -45,6 +55,11 @@ func (session *Session) Insert(beans ...interface{}) (int64, error) {
}
}
} else {
if len(copyOfOmitColMap) > 0 {
session.statement.omitColumnMap = copyOfOmitColMap
session.statement.OmitStr = copyOfOmitStr
}

cnt, err := session.innerInsert(bean)
if err != nil {
return affected, err
Expand Down Expand Up @@ -424,7 +439,7 @@ func (session *Session) innerInsert(bean interface{}) (int64, error) {

return 1, nil
} else if session.engine.dialect.DBType() == core.POSTGRES && len(table.AutoIncrement) > 0 {
//assert table.AutoIncrement != ""
// assert table.AutoIncrement != ""
sqlStr = sqlStr + " RETURNING " + session.engine.Quote(table.AutoIncrement)
res, err := session.queryBytes(sqlStr, args...)

Expand Down
45 changes: 45 additions & 0 deletions session_insert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,51 @@ func TestInsertMulti(t *testing.T) {
assert.EqualValues(t, 3, num)
}

func TestInsertMultiWithOmit(t *testing.T) {
assert.NoError(t, prepareEngine())

type TestMultiOmit struct {
Id int64 `xorm:"int(11) pk"`
Name string `xorm:"varchar(255)"`
Omitted string `xorm:"varchar(255) 'omitted'"`
}

assert.NoError(t, testEngine.Sync2(new(TestMultiOmit)))

l := []interface{}{
TestMultiOmit{Id: 1, Name: "1", Omitted: "1"},
TestMultiOmit{Id: 2, Name: "1", Omitted: "2"},
TestMultiOmit{Id: 3, Name: "1", Omitted: "3"},
}

check := func() {
var ls []TestMultiOmit
err := testEngine.NewSession().Find(&ls)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NewSession should be closed

assert.NoError(t, err)
assert.EqualValues(t, 3, len(ls))

for e := range ls {
assert.EqualValues(t, "", ls[e].Omitted)
}
}

num, err := testEngine.NewSession().Omit("omitted").
Insert(l ...)
assert.NoError(t, err)
assert.EqualValues(t, 3, num)
check()

num, err = testEngine.NewSession().Delete(TestMultiOmit{Name: "1"})
assert.NoError(t, err)
assert.EqualValues(t, 3, num)

num, err = testEngine.NewSession().Omit("omitted").
Insert(l)
assert.NoError(t, err)
assert.EqualValues(t, 3, num)
check()
}

func insertMultiDatas(step int, datas interface{}) (num int64, err error) {
sliceValue := reflect.Indirect(reflect.ValueOf(datas))
var iLen int64
Expand Down