Skip to content

Commit 167e2ad

Browse files
authored
Merge pull request #87 from zhongr3n/zhongr3n/fix-insert-ignore
Add insert ignore support for postgres and sqlite
2 parents de26a75 + fc1e041 commit 167e2ad

File tree

3 files changed

+57
-3
lines changed

3 files changed

+57
-3
lines changed

flavor.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,3 +131,27 @@ func (f Flavor) Quote(name string) string {
131131

132132
return name
133133
}
134+
135+
// PrepareInsertIgnore prepares the insert builder to build insert ignore SQL statement based on the sql flavor
136+
func (f Flavor) PrepareInsertIgnore(table string, ib *InsertBuilder) {
137+
switch ib.args.Flavor {
138+
case MySQL:
139+
ib.verb = "INSERT IGNORE"
140+
case PostgreSQL:
141+
// see https://www.postgresql.org/docs/current/sql-insert.html
142+
ib.verb = "INSERT"
143+
// add sql statement at the end after values, i.e. INSERT INTO ... ON CONFLICT DO NOTHING
144+
ib.marker = insertMarkerAfterValues
145+
ib.SQL("ON CONFLICT DO NOTHING")
146+
case SQLite:
147+
// see https://www.sqlite.org/lang_insert.html
148+
ib.verb = "INSERT OR IGNORE"
149+
default:
150+
// panic if the db flavor is not supported
151+
panic(fmt.Errorf("unsupported db flavor: %s", ib.args.Flavor.String()))
152+
}
153+
154+
// Set the table and reset the marker right after insert into
155+
ib.table = Escape(table)
156+
ib.marker = insertMarkerAfterInsertInto
157+
}

insert.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,7 @@ func InsertIgnoreInto(table string) *InsertBuilder {
6464

6565
// InsertIgnoreInto sets table name in INSERT IGNORE.
6666
func (ib *InsertBuilder) InsertIgnoreInto(table string) *InsertBuilder {
67-
ib.verb = "INSERT IGNORE"
68-
ib.table = Escape(table)
69-
ib.marker = insertMarkerAfterInsertInto
67+
ib.args.Flavor.PrepareInsertIgnore(table, ib)
7068
return ib
7169
}
7270

insert_test.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,38 @@ func ExampleInsertBuilder_insertIgnore() {
8181
// [1 Huan Du 1 2 Charmy Liu 1 1234567890]
8282
}
8383

84+
func ExampleInsertBuilder_insertIgnore_postgres() {
85+
ib := PostgreSQL.NewInsertBuilder()
86+
ib.InsertIgnoreInto("demo.user")
87+
ib.Cols("id", "name", "status", "created_at")
88+
ib.Values(1, "Huan Du", 1, Raw("UNIX_TIMESTAMP(NOW())"))
89+
ib.Values(2, "Charmy Liu", 1, 1234567890)
90+
91+
sql, args := ib.Build()
92+
fmt.Println(sql)
93+
fmt.Println(args)
94+
95+
// Output:
96+
// INSERT INTO demo.user (id, name, status, created_at) VALUES ($1, $2, $3, UNIX_TIMESTAMP(NOW())), ($4, $5, $6, $7) ON CONFLICT DO NOTHING
97+
// [1 Huan Du 1 2 Charmy Liu 1 1234567890]
98+
}
99+
100+
func ExampleInsertBuilder_insertIgnore_sqlite() {
101+
ib := SQLite.NewInsertBuilder()
102+
ib.InsertIgnoreInto("demo.user")
103+
ib.Cols("id", "name", "status", "created_at")
104+
ib.Values(1, "Huan Du", 1, Raw("UNIX_TIMESTAMP(NOW())"))
105+
ib.Values(2, "Charmy Liu", 1, 1234567890)
106+
107+
sql, args := ib.Build()
108+
fmt.Println(sql)
109+
fmt.Println(args)
110+
111+
// Output:
112+
// INSERT OR IGNORE INTO demo.user (id, name, status, created_at) VALUES (?, ?, ?, UNIX_TIMESTAMP(NOW())), (?, ?, ?, ?)
113+
// [1 Huan Du 1 2 Charmy Liu 1 1234567890]
114+
}
115+
84116
func ExampleInsertBuilder_replaceInto() {
85117
ib := NewInsertBuilder()
86118
ib.ReplaceInto("demo.user")

0 commit comments

Comments
 (0)