(queryStmt *sql.Stmt)
| 290 | } |
| 291 | |
| 292 | func countParams(queryStmt *sql.Stmt) (int, error) { |
| 293 | // Query with 0 params |
| 294 | rows, err := queryStmt.Query() |
| 295 | if err == nil { |
| 296 | // Query went fine, this means it has 0 params |
| 297 | rows.Close() |
| 298 | return 0, nil |
| 299 | } |
| 300 | |
| 301 | // Query returned an error |
| 302 | // Parse the error to get the expected params count |
| 303 | regex := regexp.MustCompile(`sql: expected (\p{N}+) arguments, got 0`) |
| 304 | regexSubmatches := regex.FindAllStringSubmatch(err.Error(), 1) |
| 305 | if len(regexSubmatches) != 1 || len(regexSubmatches[0]) != 2 { |
| 306 | // This is weird |
| 307 | // queryStmt is prepared (compiled) so it is valid |
| 308 | // but yet there was an error executing queryStmt |
| 309 | return -1, fmt.Errorf("Cannot extract params count from query error: %v", err) |
| 310 | } |
| 311 | |
| 312 | countString := regexSubmatches[0][1] |
| 313 | count, err := strconv.Atoi(countString) |
| 314 | if err != nil { |
| 315 | // This is even weirder |
| 316 | // The regex is \p{N}+ (unicode number sequence) and there was a match, |
| 317 | // but converting it from string to int returned an error |
| 318 | return -1, fmt.Errorf(`Cannot convert \p{N}+ regex to int: %v`, err) |
| 319 | } |
| 320 | return count, nil |
| 321 | } |
no outgoing calls