@@ -16,6 +16,7 @@ type Args struct {
1616 // The default flavor used by `Args#Compile`
1717 Flavor Flavor
1818
19+ indexBase int
1920 argValues []interface {}
2021 namedArgs map [string ]int
2122 sqlNamedArgs map [string ]int
@@ -47,7 +48,7 @@ func (args *Args) Add(arg interface{}) string {
4748}
4849
4950func (args * Args ) add (arg interface {}) int {
50- idx := len (args .argValues )
51+ idx := len (args .argValues ) + args . indexBase
5152
5253 switch a := arg .(type ) {
5354 case sql.NamedArg :
@@ -164,7 +165,7 @@ func (args *Args) compileNamed(ctx *argsCompileContext, format string) string {
164165 format = format [i + 1 :]
165166
166167 if p , ok := args .namedArgs [name ]; ok {
167- format , _ = args .compileSuccessive (ctx , format , p )
168+ format , _ = args .compileSuccessive (ctx , format , p - args . indexBase )
168169 }
169170
170171 return format
@@ -181,14 +182,17 @@ func (args *Args) compileDigits(ctx *argsCompileContext, format string, offset i
181182 format = format [i :]
182183
183184 if pointer , err := strconv .Atoi (digits ); err == nil {
184- return args .compileSuccessive (ctx , format , pointer )
185+ return args .compileSuccessive (ctx , format , pointer - args . indexBase )
185186 }
186187
187188 return format , offset
188189}
189190
190191func (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 (" */" )
192196 return format , offset
193197 }
194198
0 commit comments