Skip to content
This repository has been 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.

Commit

Permalink
fix bug when insert multiple slices with customize table name (#1433)
Browse files Browse the repository at this point in the history
* fix bug when insert multiple slices with customize table name

* fix tests on mssql

* fix tests
  • Loading branch information
lunny authored Sep 25, 2019
1 parent 59ed80c commit 691f6e7
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 2 deletions.
3 changes: 3 additions & 0 deletions dialect_mssql.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
8 changes: 6 additions & 2 deletions session_insert.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}:
Expand All @@ -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 {
Expand All @@ -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 {
Expand Down
39 changes: 39 additions & 0 deletions session_insert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -909,3 +909,42 @@ 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())

tableName := `prd_nightly_rate_16`
assert.NoError(t, testEngine.Table(tableName).Sync2(new(NightlyRate)))

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},
},
}

_, err = trans.Table(tableName).Insert(rtArr...)
assert.NoError(t, err)

assert.NoError(t, trans.Commit())
}

0 comments on commit 691f6e7

Please sign in to comment.