Files
lnd/sqldb/sqlc/db_custom.go
Elle Mouton 5afd9a5678 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.
2025-07-22 17:14:55 +02:00

40 lines
1.0 KiB
Go

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()
}