From 829d68c673aa0410e1dc2db67a29e6197bf5526e Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Wed, 25 Sep 2019 10:01:13 +0800 Subject: [PATCH 1/3] fix bug when insert multiple slices with customize table name --- session_insert.go | 8 ++++++-- session_insert_test.go | 38 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 44 insertions(+), 2 deletions(-) diff --git a/session_insert.go b/session_insert.go index de6452909..1e19ce7a4 100644 --- a/session_insert.go +++ b/session_insert.go @@ -25,6 +25,12 @@ func (session *Session) Insert(beans ...interface{}) (int64, error) { defer session.Close() } + session.autoResetStatement = false + defer func() { + session.autoResetStatement = true + session.resetStatement() + }() + for _, bean := range beans { switch bean.(type) { case map[string]interface{}: @@ -35,7 +41,6 @@ func (session *Session) Insert(beans ...interface{}) (int64, error) { affected += cnt case []map[string]interface{}: s := bean.([]map[string]interface{}) - session.autoResetStatement = false for i := 0; i < len(s); i++ { cnt, err := session.insertMapInterface(s[i]) if err != nil { @@ -51,7 +56,6 @@ func (session *Session) Insert(beans ...interface{}) (int64, error) { affected += cnt case []map[string]string: s := bean.([]map[string]string) - session.autoResetStatement = false for i := 0; i < len(s); i++ { cnt, err := session.insertMapString(s[i]) if err != nil { diff --git a/session_insert_test.go b/session_insert_test.go index cc3e663b6..abbf375b1 100644 --- a/session_insert_test.go +++ b/session_insert_test.go @@ -909,3 +909,41 @@ func TestInsertWhere(t *testing.T) { assert.EqualValues(t, "trest3", j3.Name) assert.EqualValues(t, 3, j3.Index) } + +type NightlyRate struct { + ID int64 `xorm:"'id' not null pk BIGINT(20)" json:"id"` +} + +func (NightlyRate) TableName() string { + return "prd_nightly_rate" +} + +func TestMultipleInsertTableName(t *testing.T) { + assert.NoError(t, prepareEngine()) + + trans := testEngine.NewSession() + defer trans.Close() + err := trans.Begin() + assert.NoError(t, err) + + rtArr := []interface{}{ + []*NightlyRate{ + {ID: 1}, + {ID: 2}, + }, + []*NightlyRate{ + {ID: 3}, + {ID: 4}, + }, + []*NightlyRate{ + {ID: 5}, + }, + } + + tableName := `prd_nightly_rate_16` + assert.NoError(t, testEngine.Table(tableName).Sync2(new(NightlyRate))) + _, err = trans.Table(tableName).Insert(rtArr...) + assert.NoError(t, err) + + assert.NoError(t, trans.Commit()) +} From 84f550d40e5571471d6c9e413f6db534aebe8164 Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Wed, 25 Sep 2019 10:51:47 +0800 Subject: [PATCH 2/3] fix tests on mssql --- dialect_mssql.go | 3 +++ 1 file changed, 3 insertions(+) diff --git a/dialect_mssql.go b/dialect_mssql.go index 61061cb25..ce4dd00c1 100644 --- a/dialect_mssql.go +++ b/dialect_mssql.go @@ -254,6 +254,9 @@ func (db *mssql) SqlType(c *core.Column) string { case core.TinyInt: res = core.TinyInt c.Length = 0 + case core.BigInt: + res = core.BigInt + c.Length = 0 default: res = t } From 8e19c2523692e0f13a2fd16852bfe2a2b0d883a8 Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Wed, 25 Sep 2019 15:49:34 +0800 Subject: [PATCH 3/3] fix tests --- session_insert_test.go | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/session_insert_test.go b/session_insert_test.go index abbf375b1..88879ef65 100644 --- a/session_insert_test.go +++ b/session_insert_test.go @@ -921,6 +921,9 @@ func (NightlyRate) TableName() string { func TestMultipleInsertTableName(t *testing.T) { assert.NoError(t, prepareEngine()) + tableName := `prd_nightly_rate_16` + assert.NoError(t, testEngine.Table(tableName).Sync2(new(NightlyRate))) + trans := testEngine.NewSession() defer trans.Close() err := trans.Begin() @@ -940,8 +943,6 @@ func TestMultipleInsertTableName(t *testing.T) { }, } - tableName := `prd_nightly_rate_16` - assert.NoError(t, testEngine.Table(tableName).Sync2(new(NightlyRate))) _, err = trans.Table(tableName).Insert(rtArr...) assert.NoError(t, err)