Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add MSSQL placeholder #175

Open
wants to merge 1 commit into
base: v1
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 10 additions & 0 deletions placeholder.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}
Expand All @@ -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 {
Expand Down
6 changes: 6 additions & 0 deletions placeholder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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), "?,?")
}
Expand Down