@@ -16,6 +16,7 @@ type Args struct {
16
16
// The default flavor used by `Args#Compile`
17
17
Flavor Flavor
18
18
19
+ indexBase int
19
20
argValues []interface {}
20
21
namedArgs map [string ]int
21
22
sqlNamedArgs map [string ]int
@@ -47,7 +48,7 @@ func (args *Args) Add(arg interface{}) string {
47
48
}
48
49
49
50
func (args * Args ) add (arg interface {}) int {
50
- idx := len (args .argValues )
51
+ idx := len (args .argValues ) + args . indexBase
51
52
52
53
switch a := arg .(type ) {
53
54
case sql.NamedArg :
@@ -164,7 +165,7 @@ func (args *Args) compileNamed(ctx *argsCompileContext, format string) string {
164
165
format = format [i + 1 :]
165
166
166
167
if p , ok := args .namedArgs [name ]; ok {
167
- format , _ = args .compileSuccessive (ctx , format , p )
168
+ format , _ = args .compileSuccessive (ctx , format , p - args . indexBase )
168
169
}
169
170
170
171
return format
@@ -181,14 +182,17 @@ func (args *Args) compileDigits(ctx *argsCompileContext, format string, offset i
181
182
format = format [i :]
182
183
183
184
if pointer , err := strconv .Atoi (digits ); err == nil {
184
- return args .compileSuccessive (ctx , format , pointer )
185
+ return args .compileSuccessive (ctx , format , pointer - args . indexBase )
185
186
}
186
187
187
188
return format , offset
188
189
}
189
190
190
191
func (args * Args ) compileSuccessive (ctx * argsCompileContext , format string , offset int ) (string , int ) {
191
- if offset >= len (args .argValues ) {
192
+ if offset < 0 || offset >= len (args .argValues ) {
193
+ ctx .WriteString ("/* INVALID ARG $" )
194
+ ctx .WriteString (strconv .Itoa (offset ))
195
+ ctx .WriteString (" */" )
192
196
return format , offset
193
197
}
194
198
0 commit comments