Skip to content

Commit 700414d

Browse files
committed
Adding Insert Ignore to Insert Builder
1 parent 604a8e6 commit 700414d

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

insert.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,13 @@ func (ib *InsertBuilder) InsertInto(table string) *InsertBuilder {
3838
return ib
3939
}
4040

41+
// InsertIgnoreInto sets table name in INSERT.
42+
func (ib *InsertBuilder) InsertIgnoreInto(table string) *InsertBuilder {
43+
ib.verb = "INSERT IGNORE"
44+
ib.table = Escape(table)
45+
return ib
46+
}
47+
4148
// ReplaceInto sets table name and changes the verb of ib to REPLACE.
4249
// REPLACE INTO is a MySQL extension to the SQL standard.
4350
func (ib *InsertBuilder) ReplaceInto(table string) *InsertBuilder {

insert_test.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,22 @@ func ExampleInsertBuilder() {
2323
// [1 Huan Du 1 2 Charmy Liu 1 1234567890]
2424
}
2525

26+
func ExampleInsertIngoreBuilder() {
27+
ib := NewInsertBuilder()
28+
ib.InsertIgnoreInto("demo.user")
29+
ib.Cols("id", "name", "status", "created_at")
30+
ib.Values(1, "Huan Du", 1, Raw("UNIX_TIMESTAMP(NOW())"))
31+
ib.Values(2, "Charmy Liu", 1, 1234567890)
32+
33+
sql, args := ib.Build()
34+
fmt.Println(sql)
35+
fmt.Println(args)
36+
37+
// Output:
38+
// INSERT IGNORE INTO demo.user (id, name, status, created_at) VALUES (?, ?, ?, UNIX_TIMESTAMP(NOW())), (?, ?, ?, ?)
39+
// [1 Huan Du 1 2 Charmy Liu 1 1234567890]
40+
}
41+
2642
func ExampleInsertBuilder_replaceInto() {
2743
ib := NewInsertBuilder()
2844
ib.ReplaceInto("demo.user")

0 commit comments

Comments
 (0)