From c7e2e1d174f27fd76144788b9f39d7e906f73635 Mon Sep 17 00:00:00 2001 From: "Dmitry P." Date: Tue, 5 Nov 2024 03:01:11 +0300 Subject: [PATCH] fix: remove extra `ignore` keywords for mysql `on duplicate key` expression --- sqlgen/insert_sql_generator.go | 3 ++- sqlgen/insert_sql_generator_test.go | 28 ++++++++++++++++++++++------ 2 files changed, 24 insertions(+), 7 deletions(-) diff --git a/sqlgen/insert_sql_generator.go b/sqlgen/insert_sql_generator.go index 1c6105b9..3fb37fdc 100644 --- a/sqlgen/insert_sql_generator.go +++ b/sqlgen/insert_sql_generator.go @@ -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) diff --git a/sqlgen/insert_sql_generator_test.go b/sqlgen/insert_sql_generator_test.go index 95479c56..c1accc07 100644 --- a/sqlgen/insert_sql_generator_test.go +++ b/sqlgen/insert_sql_generator_test.go @@ -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"}, }, @@ -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() {