allow to call WithContext after OnDuplicateKeyIgnore

This commit is contained in:
Qishuai Liu 2025-08-25 10:26:21 +09:00
parent ed36ef030f
commit 7b70b4a1c1
No known key found for this signature in database
2 changed files with 16 additions and 7 deletions

View File

@ -28,7 +28,7 @@ type insertWithValues interface {
toInsertWithContext
toInsertFinal
Values(values ...interface{}) insertWithValues
OnDuplicateKeyIgnore() toInsertFinal
OnDuplicateKeyIgnore() toInsertWithDuplicateKey
OnDuplicateKeyUpdate() insertWithOnDuplicateKeyUpdateBegin
}
@ -36,7 +36,7 @@ type insertWithModels interface {
toInsertWithContext
toInsertFinal
Models(models ...interface{}) insertWithModels
OnDuplicateKeyIgnore() toInsertFinal
OnDuplicateKeyIgnore() toInsertWithDuplicateKey
OnDuplicateKeyUpdate() insertWithOnDuplicateKeyUpdateBegin
}
@ -46,10 +46,8 @@ type insertWithOnDuplicateKeyUpdateBegin interface {
}
type insertWithOnDuplicateKeyUpdate interface {
toInsertWithContext
toInsertFinal
Set(Field Field, value interface{}) insertWithOnDuplicateKeyUpdate
SetIf(condition bool, Field Field, value interface{}) insertWithOnDuplicateKeyUpdate
insertWithOnDuplicateKeyUpdateBegin
toInsertWithDuplicateKey
}
type toInsertWithContext interface {
@ -61,6 +59,11 @@ type toInsertFinal interface {
Execute() (result sql.Result, err error)
}
type toInsertWithDuplicateKey interface {
toInsertWithContext
toInsertFinal
}
func (d *database) InsertInto(table Table) insertWithTable {
return insertStatus{method: "INSERT", scope: scope{Database: d, Tables: []Table{table}}}
}
@ -131,7 +134,7 @@ func (s insertStatus) Set(field Field, value interface{}) insertWithOnDuplicateK
return s
}
func (s insertStatus) OnDuplicateKeyIgnore() toInsertFinal {
func (s insertStatus) OnDuplicateKeyIgnore() toInsertWithDuplicateKey {
firstField := s.scope.Tables[0].GetFields()[0]
return s.OnDuplicateKeyUpdate().Set(firstField, firstField)
}

View File

@ -147,4 +147,10 @@ func TestInsert(t *testing.T) {
if _, err := db.InsertInto(Test).Fields(Test.F1).Values(1).WithContext(context.Background()).Execute(); err != nil {
t.Error(err)
}
if _, err := db.InsertInto(Test).
Fields(Test.F1).Values(1).
OnDuplicateKeyUpdate().Set(Test.F1, errExpr).WithContext(context.Background()).Execute(); err == nil {
t.Error("should get error here")
}
}