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

Support MSSQL placeholders #44

Merged
merged 2 commits into from
Apr 23, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 53 additions & 0 deletions example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,31 @@ func Example_sliceMySQL() {
// [42 true str]
}

func Example_sliceMSSQL() {
params := []any{42, true, "str"}

var b builq.Builder
b.Addf("INSERT INTO table (id, flag, name)")
b.Addf("VALUES (%+@);", params)

query, args, err := b.Build()
if err != nil {
panic(err)
}

fmt.Println("query:")
fmt.Println(query)
fmt.Println("args:")
fmt.Println(args)

// Output:
// query:
// INSERT INTO table (id, flag, name)
// VALUES (@p1, @p2, @p3);
// args:
// [42 true str]
}

func Example_insertReturn() {
cols := builq.Columns{"id", "is_active", "name"}
params := []any{true, "str"}
Expand Down Expand Up @@ -463,6 +488,34 @@ func Example_batchMySQL() {
// [42 true str 69 true noice]
}

func Example_batchMSSQL() {
params := [][]any{
{42, true, "str"},
{69, true, "noice"},
}

var b builq.Builder
b.Addf("INSERT INTO table (id, flag, name)")
b.Addf("VALUES %#@;", params)

query, args, err := b.Build()
if err != nil {
panic(err)
}

fmt.Println("query:")
fmt.Println(query)
fmt.Println("args:")
fmt.Println(args)

// Output:
// query:
// INSERT INTO table (id, flag, name)
// VALUES (@p1, @p2, @p3), (@p4, @p5, @p6);
// args:
// [42 true str 69 true noice]
}

func Example_sliceInBatch() {
params := [][]any{
{42, []any{1, 2, 3}},
Expand Down
9 changes: 7 additions & 2 deletions write.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func (b *Builder) write(sb *strings.Builder, resArgs *[]any, s string, args ...a
}

switch verb := s[0]; verb {
case '$', '?', 's', 'd':
case '$', '?', '@', 's', 'd':
if argID >= len(args) {
return fmt.Errorf("%w: have %d args, want %d", errTooFewArguments, len(args), argID+1)
}
Expand All @@ -49,7 +49,7 @@ func (b *Builder) write(sb *strings.Builder, resArgs *[]any, s string, args ...a
}

switch verb := s[0]; verb {
case '$', '?':
case '$', '?', '@':
if argID >= len(args) {
return fmt.Errorf("%w: have %d args, want %d", errTooFewArguments, len(args), argID+1)
}
Expand Down Expand Up @@ -136,6 +136,11 @@ func (b *Builder) writeArg(sb *strings.Builder, resArgs *[]any, verb byte, arg a
case '?':
sb.WriteByte('?')
*resArgs = append(*resArgs, arg)
case '@':
b.counter++
sb.WriteString("@p")
sb.WriteString(strconv.Itoa(b.counter))
*resArgs = append(*resArgs, arg)
case 's':
isSimple = true
switch arg := arg.(type) {
Expand Down
Loading