Merge pull request #33 from ddouglas/ddouglas-adding-insert-ignore

Adding Insert Ignore to Insert Builder
This commit is contained in:
Huan Du
2019-09-06 10:24:28 +08:00
committed by GitHub
2 changed files with 23 additions and 0 deletions
+7
View File
@@ -38,6 +38,13 @@ func (ib *InsertBuilder) InsertInto(table string) *InsertBuilder {
return ib
}
// InsertIgnoreInto sets table name in INSERT IGNORE.
func (ib *InsertBuilder) InsertIgnoreInto(table string) *InsertBuilder {
ib.verb = "INSERT IGNORE"
ib.table = Escape(table)
return ib
}
// ReplaceInto sets table name and changes the verb of ib to REPLACE.
// REPLACE INTO is a MySQL extension to the SQL standard.
func (ib *InsertBuilder) ReplaceInto(table string) *InsertBuilder {
+16
View File
@@ -23,6 +23,22 @@ func ExampleInsertBuilder() {
// [1 Huan Du 1 2 Charmy Liu 1 1234567890]
}
func ExampleInsertBuilder_insertIgnore() {
ib := NewInsertBuilder()
ib.InsertIgnoreInto("demo.user")
ib.Cols("id", "name", "status", "created_at")
ib.Values(1, "Huan Du", 1, Raw("UNIX_TIMESTAMP(NOW())"))
ib.Values(2, "Charmy Liu", 1, 1234567890)
sql, args := ib.Build()
fmt.Println(sql)
fmt.Println(args)
// Output:
// INSERT IGNORE INTO demo.user (id, name, status, created_at) VALUES (?, ?, ?, UNIX_TIMESTAMP(NOW())), (?, ?, ?, ?)
// [1 Huan Du 1 2 Charmy Liu 1 1234567890]
}
func ExampleInsertBuilder_replaceInto() {
ib := NewInsertBuilder()
ib.ReplaceInto("demo.user")