Skip to content

Commit

Permalink
TWEAK: escape placeholder by repeating it twice
Browse files Browse the repository at this point in the history
fix #106
  • Loading branch information
taylorchu committed May 10, 2018
1 parent 106750d commit bbb1206
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 4 deletions.
21 changes: 17 additions & 4 deletions interpolate.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,9 @@ func InterpolateForDialect(query string, value []interface{}, d Dialect) (string
return i.String(), nil
}

func (i *interpolator) interpolate(query string, value []interface{}, topLevel bool) error {
if strings.Count(query, placeholder) != len(value) {
return ErrPlaceholderCount
}
var escapedPlaceholder = strings.Repeat(placeholder, 2)

func (i *interpolator) interpolate(query string, value []interface{}, topLevel bool) error {
valueIndex := 0

for {
Expand All @@ -56,6 +54,17 @@ func (i *interpolator) interpolate(query string, value []interface{}, topLevel b
break
}

// escape placeholder by repeating it twice
if strings.HasPrefix(query[index:], escapedPlaceholder) {
i.WriteString(query[:index+len(escapedPlaceholder)])
query = query[index+len(escapedPlaceholder):]
continue
}

if valueIndex >= len(value) {
break
}

i.WriteString(query[:index])
if _, ok := value[valueIndex].([]byte); ok && i.IgnoreBinary {
i.WriteString(i.Placeholder(i.N))
Expand All @@ -71,6 +80,10 @@ func (i *interpolator) interpolate(query string, value []interface{}, topLevel b
valueIndex++
}

if valueIndex != len(value) {
return ErrPlaceholderCount
}

// placeholder not found; write remaining query
i.WriteString(query)

Expand Down
10 changes: 10 additions & 0 deletions interpolate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,16 @@ func TestInterpolateForDialect(t *testing.T) {
value: []interface{}{(*int64)(nil)},
want: "NULL",
},
{
query: "???? ? ?? ? ??",
value: []interface{}{1, 2},
want: "???? 1 ?? 2 ??",
},
{
query: "???",
value: []interface{}{1},
want: "??1",
},
} {
s, err := InterpolateForDialect(test.query, test.value, dialect.MySQL)
require.NoError(t, err)
Expand Down

0 comments on commit bbb1206

Please sign in to comment.