Skip to content

Commit

Permalink
fix: remove extra ignore keywords for mysql on duplicate key expr…
Browse files Browse the repository at this point in the history
…ession
  • Loading branch information
autowp committed Nov 5, 2024
1 parent 21b6e6d commit c7e2e1d
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 7 deletions.
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

0 comments on commit c7e2e1d

Please sign in to comment.