Skip to content

Commit 8163afa

Browse files
author
Reinaldy Rafli
committed
fix: make PlaceholderFormat not required
1 parent 4ec5b90 commit 8163afa

File tree

2 files changed

+84
-0
lines changed

2 files changed

+84
-0
lines changed

upsert.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,19 @@ func (d *upsertData) ToSql() (sqlStr string, args []interface{}, err error) {
187187

188188
sql.WriteString(";")
189189

190+
if d.Placeholder == "" {
191+
switch d.Dialect {
192+
case MySQL:
193+
d.Placeholder = Question
194+
case PostgreSQL:
195+
d.Placeholder = Dollar
196+
case SQLite:
197+
d.Placeholder = Question
198+
case MSSQL:
199+
d.Placeholder = AtP
200+
}
201+
}
202+
190203
sqlStr = ReplacePlaceholder(sql.String(), d.Placeholder)
191204
return
192205
}

upsert_test.go

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,3 +153,74 @@ func TestUpsert_EmitErrors(t *testing.T) {
153153
}
154154
})
155155
}
156+
157+
func TestUpsert_WithoutReplacePlaceHolder(t *testing.T) {
158+
t.Run("PostgreSQL", func(t *testing.T) {
159+
sql, args, err := bob.
160+
Upsert("users", bob.PostgreSQL).
161+
Columns("name", "email").
162+
Values("John Doe", "john@doe.com").
163+
Key("email").
164+
Replace("name", "John Does").
165+
ToSql()
166+
if err != nil {
167+
t.Error(err)
168+
}
169+
170+
desiredSql := "INSERT INTO \"users\" (\"name\", \"email\") VALUES ($1, $2) ON CONFLICT (\"email\") DO UPDATE SET \"name\" = $3;"
171+
desiredArgs := []interface{}{"John Doe", "john@doe.com", "John Does"}
172+
173+
if sql != desiredSql {
174+
t.Error("sql is not the same as result: ", sql)
175+
}
176+
if !reflect.DeepEqual(args, desiredArgs) {
177+
t.Error("args is not the same as result: ", args)
178+
}
179+
})
180+
181+
t.Run("MSSQL", func(t *testing.T) {
182+
sql, args, err := bob.
183+
Upsert("users", bob.MSSQL).
184+
Columns("name", "email").
185+
Values("John Doe", "john@doe.com").
186+
Key("email", "john@doe.com").
187+
Replace("name", "John Does").
188+
ToSql()
189+
if err != nil {
190+
t.Error(err)
191+
}
192+
193+
desiredSql := "IF NOT EXISTS (SELECT * FROM \"users\" WHERE \"email\" = @p1) INSERT INTO \"users\" (\"name\", \"email\") VALUES (@p2, @p3) ELSE UPDATE \"users\" SET \"name\" = @p4 WHERE \"email\" = @p5;"
194+
desiredArgs := []interface{}{"john@doe.com", "John Doe", "john@doe.com", "John Does", "john@doe.com"}
195+
196+
if sql != desiredSql {
197+
t.Error("sql is not the same as result: ", sql)
198+
}
199+
if !reflect.DeepEqual(args, desiredArgs) {
200+
t.Error("args is not the same as result: ", args)
201+
}
202+
})
203+
204+
t.Run("SQLite", func(t *testing.T) {
205+
sql, args, err := bob.
206+
Upsert("users", bob.SQLite).
207+
Columns("name", "email").
208+
Values("John Doe", "john@doe.com").
209+
Key("email").
210+
Replace("name", "John Does").
211+
ToSql()
212+
if err != nil {
213+
t.Error(err)
214+
}
215+
216+
desiredSql := "INSERT INTO \"users\" (\"name\", \"email\") VALUES (?, ?) ON CONFLICT (\"email\") DO UPDATE SET \"name\" = ?;"
217+
desiredArgs := []interface{}{"John Doe", "john@doe.com", "John Does"}
218+
219+
if sql != desiredSql {
220+
t.Error("sql is not the same as result: ", sql)
221+
}
222+
if !reflect.DeepEqual(args, desiredArgs) {
223+
t.Error("args is not the same as result: ", args)
224+
}
225+
})
226+
}

0 commit comments

Comments
 (0)