Skip to content

Commit

Permalink
internal/querytext: new package that extracts the parameter parser (d…
Browse files Browse the repository at this point in the history
  • Loading branch information
kardianos authored Jul 20, 2019
1 parent 2b613d2 commit 23f0cad
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 5 deletions.
12 changes: 10 additions & 2 deletions parser.go → internal/querytext/parser.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
package mssql
// Package querytext is the old query parser and parameter substitute process.
// Do not use on new code.
//
// This package is not subject to any API compatibility guarantee.
package querytext

import (
"bytes"
Expand Down Expand Up @@ -40,7 +44,11 @@ func (p *parser) write(ch rune) {

type stateFunc func(*parser) stateFunc

func parseParams(query string) (string, int) {
// ParseParams rewrites the query from using "?" placeholders
// to using "@pN" parameter names that SQL Server will accept.
//
// This function and package is not subject to any API compatibility guarantee.
func ParseParams(query string) (string, int) {
p := &parser{
r: bytes.NewReader([]byte(query)),
namedParams: map[string]bool{},
Expand Down
4 changes: 2 additions & 2 deletions parser_test.go → internal/querytext/parser_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package mssql
package querytext

import (
"testing"
Expand Down Expand Up @@ -49,7 +49,7 @@ func TestParseParams(t *testing.T) {
}

for _, v := range values {
d, n := parseParams(v.s)
d, n := ParseParams(v.s)
if d != v.d {
t.Errorf("Parse params don't match for %s, got %s but expected %s", v.s, d, v.d)
}
Expand Down
4 changes: 3 additions & 1 deletion mssql.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import (
"strings"
"time"
"unicode"

"github.com/denisenkom/go-mssqldb/internal/querytext"
)

// ReturnStatus may be used to return the return value from a proc.
Expand Down Expand Up @@ -385,7 +387,7 @@ func (c *Conn) Prepare(query string) (driver.Stmt, error) {
func (c *Conn) prepareContext(ctx context.Context, query string) (*Stmt, error) {
paramCount := -1
if c.processQueryText {
query, paramCount = parseParams(query)
query, paramCount = querytext.ParseParams(query)
}
return &Stmt{c, query, paramCount, nil}, nil
}
Expand Down

0 comments on commit 23f0cad

Please sign in to comment.