Skip to content

Commit 7b70b4a

Browse files
committed
allow to call WithContext after OnDuplicateKeyIgnore
1 parent ed36ef0 commit 7b70b4a

2 files changed

Lines changed: 16 additions & 7 deletions

File tree

insert.go

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,15 @@ type insertWithValues interface {
2828
toInsertWithContext
2929
toInsertFinal
3030
Values(values ...interface{}) insertWithValues
31-
OnDuplicateKeyIgnore() toInsertFinal
31+
OnDuplicateKeyIgnore() toInsertWithDuplicateKey
3232
OnDuplicateKeyUpdate() insertWithOnDuplicateKeyUpdateBegin
3333
}
3434

3535
type insertWithModels interface {
3636
toInsertWithContext
3737
toInsertFinal
3838
Models(models ...interface{}) insertWithModels
39-
OnDuplicateKeyIgnore() toInsertFinal
39+
OnDuplicateKeyIgnore() toInsertWithDuplicateKey
4040
OnDuplicateKeyUpdate() insertWithOnDuplicateKeyUpdateBegin
4141
}
4242

@@ -46,10 +46,8 @@ type insertWithOnDuplicateKeyUpdateBegin interface {
4646
}
4747

4848
type insertWithOnDuplicateKeyUpdate interface {
49-
toInsertWithContext
50-
toInsertFinal
51-
Set(Field Field, value interface{}) insertWithOnDuplicateKeyUpdate
52-
SetIf(condition bool, Field Field, value interface{}) insertWithOnDuplicateKeyUpdate
49+
insertWithOnDuplicateKeyUpdateBegin
50+
toInsertWithDuplicateKey
5351
}
5452

5553
type toInsertWithContext interface {
@@ -61,6 +59,11 @@ type toInsertFinal interface {
6159
Execute() (result sql.Result, err error)
6260
}
6361

62+
type toInsertWithDuplicateKey interface {
63+
toInsertWithContext
64+
toInsertFinal
65+
}
66+
6467
func (d *database) InsertInto(table Table) insertWithTable {
6568
return insertStatus{method: "INSERT", scope: scope{Database: d, Tables: []Table{table}}}
6669
}
@@ -131,7 +134,7 @@ func (s insertStatus) Set(field Field, value interface{}) insertWithOnDuplicateK
131134
return s
132135
}
133136

134-
func (s insertStatus) OnDuplicateKeyIgnore() toInsertFinal {
137+
func (s insertStatus) OnDuplicateKeyIgnore() toInsertWithDuplicateKey {
135138
firstField := s.scope.Tables[0].GetFields()[0]
136139
return s.OnDuplicateKeyUpdate().Set(firstField, firstField)
137140
}

insert_test.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,4 +147,10 @@ func TestInsert(t *testing.T) {
147147
if _, err := db.InsertInto(Test).Fields(Test.F1).Values(1).WithContext(context.Background()).Execute(); err != nil {
148148
t.Error(err)
149149
}
150+
151+
if _, err := db.InsertInto(Test).
152+
Fields(Test.F1).Values(1).
153+
OnDuplicateKeyUpdate().Set(Test.F1, errExpr).WithContext(context.Background()).Execute(); err == nil {
154+
t.Error("should get error here")
155+
}
150156
}

0 commit comments

Comments
 (0)