Skip to content

Commit 934d5c1

Browse files
authored
Merge pull request huandu#21 from huandu/feature-replace-into
refs huandu#18. add new method `ReplaceInto` in `InsertBuilder`
2 parents a9b2a7d + cd2b98b commit 934d5c1

File tree

2 files changed

+29
-2
lines changed

2 files changed

+29
-2
lines changed

insert.go

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,14 @@ func NewInsertBuilder() *InsertBuilder {
1717
func newInsertBuilder() *InsertBuilder {
1818
args := &Args{}
1919
return &InsertBuilder{
20+
verb: "INSERT",
2021
args: args,
2122
}
2223
}
2324

2425
// InsertBuilder is a builder to build INSERT.
2526
type InsertBuilder struct {
27+
verb string
2628
table string
2729
cols []string
2830
values [][]string
@@ -36,6 +38,14 @@ func (ib *InsertBuilder) InsertInto(table string) *InsertBuilder {
3638
return ib
3739
}
3840

41+
// ReplaceInto sets table name and changes the verb of ib to REPLACE.
42+
// REPLACE INTO is a MySQL extension to the SQL standard.
43+
func (ib *InsertBuilder) ReplaceInto(table string) *InsertBuilder {
44+
ib.verb = "REPLACE"
45+
ib.table = Escape(table)
46+
return ib
47+
}
48+
3949
// Cols sets columns in INSERT.
4050
func (ib *InsertBuilder) Cols(col ...string) *InsertBuilder {
4151
ib.cols = EscapeAll(col...)
@@ -54,7 +64,7 @@ func (ib *InsertBuilder) Values(value ...interface{}) *InsertBuilder {
5464
return ib
5565
}
5666

57-
// String returns the compiled DELETE string.
67+
// String returns the compiled INSERT string.
5868
func (ib *InsertBuilder) String() string {
5969
s, _ := ib.Build()
6070
return s
@@ -70,7 +80,8 @@ func (ib *InsertBuilder) Build() (sql string, args []interface{}) {
7080
// They can be used in `DB#Query` of package `database/sql` directly.
7181
func (ib *InsertBuilder) BuildWithFlavor(flavor Flavor, initialArg ...interface{}) (sql string, args []interface{}) {
7282
buf := &bytes.Buffer{}
73-
buf.WriteString("INSERT INTO ")
83+
buf.WriteString(ib.verb)
84+
buf.WriteString(" INTO ")
7485
buf.WriteString(ib.table)
7586

7687
if len(ib.cols) > 0 {

insert_test.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,19 @@ func ExampleInsertBuilder() {
2222
// INSERT INTO demo.user (id, name, status, created_at) VALUES (?, ?, ?, UNIX_TIMESTAMP(NOW())), (?, ?, ?, ?)
2323
// [1 Huan Du 1 2 Charmy Liu 1 1234567890]
2424
}
25+
26+
func ExampleInsertBuilder_replaceInto() {
27+
ib := NewInsertBuilder()
28+
ib.ReplaceInto("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+
// REPLACE INTO demo.user (id, name, status, created_at) VALUES (?, ?, ?, UNIX_TIMESTAMP(NOW())), (?, ?, ?, ?)
39+
// [1 Huan Du 1 2 Charmy Liu 1 1234567890]
40+
}

0 commit comments

Comments
 (0)