@@ -166,36 +166,49 @@ func (mc *mysqlConn) Prepare(query string) (driver.Stmt, error) {
166
166
}
167
167
168
168
// estimateParamLength calculates upper bound of string length from types.
169
- func estimateParamLength (args []driver.Value ) (int , bool ) {
170
- l := 0
169
+ func estimateParamLength (query string , args []driver.Value ) (min , max int ) {
170
+ min = len (query )
171
+ max = len (query )
171
172
for _ , a := range args {
173
+ if a == nil {
174
+ min += 4
175
+ max += 4
176
+ continue
177
+ }
172
178
switch v := a .(type ) {
173
179
case int64 , float64 :
174
- // 24 (-1.7976931348623157e+308) may be upper bound. But I'm not sure.
175
- l += 25
176
- case bool :
177
- l += 1 // 0 or 1
180
+ min += 1
181
+ max += 25 // 24 (-1.7976931348623157e+308) may be upper bound. But I'm not sure.
182
+ case bool : // 0 or 1
183
+ min += 1
184
+ max += 1
178
185
case time.Time :
179
- l += 30 // '1234-12-23 12:34:56.777777'
186
+ min += 23 // '1234-12-23 12:34:56'
187
+ max += 30 // '1234-12-23 12:34:56.777777'
180
188
case string :
181
- l += len (v )* 2 + 2
189
+ min += len (v ) + 2
190
+ max += len (v )* 2 + 2
182
191
case []byte :
183
- l += len (v )* 2 + 2
192
+ min += len (v ) + 2
193
+ max += len (v )* 2 + 2
184
194
default :
185
- return 0 , false
195
+ panic ( "driver.Value has invalid type" )
186
196
}
187
197
}
188
- return l , true
198
+ return
189
199
}
190
200
191
201
func (mc * mysqlConn ) interpolateParams (query string , args []driver.Value ) (string , error ) {
192
- estimated , ok := estimateParamLength (args )
193
- if ! ok {
202
+ min , max := estimateParamLength (query , args )
203
+ if min + 4 > maxPacketSize { // 4 bytes for packet header
194
204
return "" , driver .ErrSkip
195
205
}
196
- estimated += len (query )
206
+ bufSize := max
207
+ if max > maxPacketSize { // maxPacketSize is upper bound of mc.buffer
208
+ bufSize = maxPacketSize
209
+ }
197
210
198
- buf := make ([] byte , 0 , estimated )
211
+ buf := mc . buf . takeBuffer ( bufSize )[: 0 ]
199
212
argPos := 0
200
213
201
214
for i := 0 ; i < len (query ); i ++ {
0 commit comments