From cec12053f4f3358318a6d5d97d50adb73d37e724 Mon Sep 17 00:00:00 2001 From: briantenazas Date: Mon, 14 Jan 2019 13:52:07 -0500 Subject: [PATCH] Add MSSQL placeholder --- placeholder.go | 10 ++++++++++ placeholder_test.go | 6 ++++++ 2 files changed, 16 insertions(+) diff --git a/placeholder.go b/placeholder.go index a0161823..53c1f9f1 100644 --- a/placeholder.go +++ b/placeholder.go @@ -26,6 +26,10 @@ var ( // Colon is a PlaceholderFormat instance that replaces placeholders with // colon-prefixed positional placeholders (e.g. :1, :2, :3). Colon = colonFormat{} + + // Position is a PlaceholderFormat instance that replaces placeholders with + // @p-prefixed positional placeholders normally used with MSSQL drivers (e.g @p1, @p2) + Position = positionFormat{} ) type questionFormat struct{} @@ -46,6 +50,12 @@ func (colonFormat) ReplacePlaceholders(sql string) (string, error) { return replacePositionalPlaceholders(sql, ":") } +type positionFormat struct{} + +func (positionFormat) ReplacePlaceholders(sql string) (string, error) { + return replacePositionalPlaceholders(sql, "@p") +} + // Placeholders returns a string with count ? placeholders joined with commas. func Placeholders(count int) string { if count < 1 { diff --git a/placeholder_test.go b/placeholder_test.go index 6ba06035..f0f5d4a4 100644 --- a/placeholder_test.go +++ b/placeholder_test.go @@ -25,6 +25,12 @@ func TestColon(t *testing.T) { assert.Equal(t, "x = :1 AND y = :2", s) } +func TestPosition(t *testing.T) { + sql := "x = ? AND y = ?" + s, _ := Position.ReplacePlaceholders(sql) + assert.Equal(t, "x = @p1 AND y = @p2", s) +} + func TestPlaceholders(t *testing.T) { assert.Equal(t, Placeholders(2), "?,?") }