mirror of
https://github.com/lightningnetwork/lnd.git
synced 2025-10-11 11:53:07 +02:00
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:
39
sqldb/sqlc/db_custom.go
Normal file
39
sqldb/sqlc/db_custom.go
Normal 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()
|
||||
}
|
Reference in New Issue
Block a user