From 8163afaf1b0657b5e848c6053fb9992eeda180bf Mon Sep 17 00:00:00 2001 From: Reinaldy Rafli Date: Tue, 9 Nov 2021 11:06:40 +0700 Subject: [PATCH] fix: make PlaceholderFormat not required --- upsert.go | 13 +++++++++ upsert_test.go | 71 ++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 84 insertions(+) diff --git a/upsert.go b/upsert.go index 58c2b0b..54e1f7b 100644 --- a/upsert.go +++ b/upsert.go @@ -187,6 +187,19 @@ func (d *upsertData) ToSql() (sqlStr string, args []interface{}, err error) { sql.WriteString(";") + if d.Placeholder == "" { + switch d.Dialect { + case MySQL: + d.Placeholder = Question + case PostgreSQL: + d.Placeholder = Dollar + case SQLite: + d.Placeholder = Question + case MSSQL: + d.Placeholder = AtP + } + } + sqlStr = ReplacePlaceholder(sql.String(), d.Placeholder) return } diff --git a/upsert_test.go b/upsert_test.go index 029197e..317ae8a 100644 --- a/upsert_test.go +++ b/upsert_test.go @@ -153,3 +153,74 @@ func TestUpsert_EmitErrors(t *testing.T) { } }) } + +func TestUpsert_WithoutReplacePlaceHolder(t *testing.T) { + t.Run("PostgreSQL", func(t *testing.T) { + sql, args, err := bob. + Upsert("users", bob.PostgreSQL). + Columns("name", "email"). + Values("John Doe", "john@doe.com"). + Key("email"). + Replace("name", "John Does"). + ToSql() + if err != nil { + t.Error(err) + } + + desiredSql := "INSERT INTO \"users\" (\"name\", \"email\") VALUES ($1, $2) ON CONFLICT (\"email\") DO UPDATE SET \"name\" = $3;" + desiredArgs := []interface{}{"John Doe", "john@doe.com", "John Does"} + + if sql != desiredSql { + t.Error("sql is not the same as result: ", sql) + } + if !reflect.DeepEqual(args, desiredArgs) { + t.Error("args is not the same as result: ", args) + } + }) + + t.Run("MSSQL", func(t *testing.T) { + sql, args, err := bob. + Upsert("users", bob.MSSQL). + Columns("name", "email"). + Values("John Doe", "john@doe.com"). + Key("email", "john@doe.com"). + Replace("name", "John Does"). + ToSql() + if err != nil { + t.Error(err) + } + + 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;" + desiredArgs := []interface{}{"john@doe.com", "John Doe", "john@doe.com", "John Does", "john@doe.com"} + + if sql != desiredSql { + t.Error("sql is not the same as result: ", sql) + } + if !reflect.DeepEqual(args, desiredArgs) { + t.Error("args is not the same as result: ", args) + } + }) + + t.Run("SQLite", func(t *testing.T) { + sql, args, err := bob. + Upsert("users", bob.SQLite). + Columns("name", "email"). + Values("John Doe", "john@doe.com"). + Key("email"). + Replace("name", "John Does"). + ToSql() + if err != nil { + t.Error(err) + } + + desiredSql := "INSERT INTO \"users\" (\"name\", \"email\") VALUES (?, ?) ON CONFLICT (\"email\") DO UPDATE SET \"name\" = ?;" + desiredArgs := []interface{}{"John Doe", "john@doe.com", "John Does"} + + if sql != desiredSql { + t.Error("sql is not the same as result: ", sql) + } + if !reflect.DeepEqual(args, desiredArgs) { + t.Error("args is not the same as result: ", args) + } + }) +} \ No newline at end of file