scripts: add sql slices workaround to sqlc gen script

This copies the workaround introduced in the taproot-assets code base
and will allow us to use `WHERE x in <list>` type queries.
This commit is contained in:
Elle Mouton
2025-07-14 12:28:09 +02:00
parent 5c62e904e2
commit 5afd9a5678
3 changed files with 129 additions and 1 deletions

39
sqldb/sqlc/db_custom.go Normal file
View File

@@ -0,0 +1,39 @@
package sqlc
import (
"fmt"
"strings"
)
// makeQueryParams generates a string of query parameters for a SQL query. It is
// meant to replace the `?` placeholders in a SQL query with numbered parameters
// like `$1`, `$2`, etc. This is required for the sqlc /*SLICE:<field_name>*/
// workaround. See scripts/gen_sqlc_docker.sh for more details.
func makeQueryParams(numTotalArgs, numListArgs int) string {
if numListArgs == 0 {
return ""
}
var b strings.Builder
// Pre-allocate a rough estimation of the buffer size to avoid
// re-allocations. A parameter like $1000, takes 6 bytes.
b.Grow(numListArgs * 6)
diff := numTotalArgs - numListArgs
for i := 0; i < numListArgs; i++ {
if i > 0 {
// We don't need to check the error here because the
// WriteString method of strings.Builder always returns
// nil.
_, _ = b.WriteString(",")
}
// We don't need to check the error here because the
// Write method (called by fmt.Fprintf) of strings.Builder
// always returns nil.
_, _ = fmt.Fprintf(&b, "$%d", i+diff+1)
}
return b.String()
}