Skip to content
Open
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
3 changes: 2 additions & 1 deletion sqlgen/insert_sql_generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,8 @@ func (isg *insertSQLGenerator) Generate(

// Adds the correct fragment to being an INSERT statement
func (isg *insertSQLGenerator) InsertBeginSQL(b sb.SQLBuilder, o exp.ConflictExpression) {
if isg.DialectOptions().SupportsInsertIgnoreSyntax && o != nil {
_, isNotDoNothingConflictExpression := o.(exp.ConflictUpdateExpression)
if isg.DialectOptions().SupportsInsertIgnoreSyntax && o != nil && !isNotDoNothingConflictExpression {
b.Write(isg.DialectOptions().InsertIgnoreClause)
} else {
b.Write(isg.DialectOptions().InsertClause)
Expand Down
28 changes: 22 additions & 6 deletions sqlgen/insert_sql_generator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -333,33 +333,33 @@ func (igs *insertSQLGeneratorSuite) TestGenerate_onConflict() {

insertTestCase{
clause: icDu,
sql: `insert ignore into "test" ("a") VALUES ('a1') on conflict (test) do update set "a"='b'`,
sql: `INSERT INTO "test" ("a") VALUES ('a1') on conflict (test) do update set "a"='b'`,
},
insertTestCase{
clause: icDu,
sql: `insert ignore into "test" ("a") VALUES (?) on conflict (test) do update set "a"=?`,
sql: `INSERT INTO "test" ("a") VALUES (?) on conflict (test) do update set "a"=?`,
isPrepared: true,
args: []interface{}{"a1", "b"},
},

insertTestCase{
clause: icDoc,
sql: `insert ignore into "test" ("a") VALUES ('a1') on conflict on constraint test do update set "a"='b'`,
sql: `INSERT INTO "test" ("a") VALUES ('a1') on conflict on constraint test do update set "a"='b'`,
},
insertTestCase{
clause: icDoc,
sql: `insert ignore into "test" ("a") VALUES (?) on conflict on constraint test do update set "a"=?`,
sql: `INSERT INTO "test" ("a") VALUES (?) on conflict on constraint test do update set "a"=?`,
isPrepared: true,
args: []interface{}{"a1", "b"},
},

insertTestCase{
clause: icDuw,
sql: `insert ignore into "test" ("a") VALUES ('a1') on conflict (test) do update set "a"='b' WHERE ("foo" IS TRUE)`,
sql: `INSERT INTO "test" ("a") VALUES ('a1') on conflict (test) do update set "a"='b' WHERE ("foo" IS TRUE)`,
},
insertTestCase{
clause: icDuw,
sql: `insert ignore into "test" ("a") VALUES (?) on conflict (test) do update set "a"=? WHERE ("foo" IS TRUE)`,
sql: `INSERT INTO "test" ("a") VALUES (?) on conflict (test) do update set "a"=? WHERE ("foo" IS TRUE)`,
isPrepared: true,
args: []interface{}{"a1", "b"},
},
Expand All @@ -378,6 +378,22 @@ func (igs *insertSQLGeneratorSuite) TestGenerate_onConflict() {
insertTestCase{clause: icDuw, err: expectedErr},
insertTestCase{clause: icDuw, err: expectedErr, isPrepared: true},
)

opts.InsertIgnoreClause = []byte("INSERT IGNORE INTO")
opts.ConflictFragment = []byte("")
opts.ConflictDoUpdateFragment = []byte(" ON DUPLICATE KEY UPDATE ")
opts.ConflictDoNothingFragment = []byte("")
opts.SupportsConflictTarget = false
igs.assertCases(
sqlgen.NewInsertSQLGenerator("test", opts),
insertTestCase{clause: icDn, sql: `INSERT IGNORE INTO "test" ("a") VALUES ('a1')`},
insertTestCase{
clause: icDu,
sql: `INSERT INTO "test" ("a") VALUES (?) ON DUPLICATE KEY UPDATE "a"=?`,
isPrepared: true,
args: []interface{}{"a1", "b"},
},
)
}

func (igs *insertSQLGeneratorSuite) TestGenerate_withCommonTables() {
Expand Down