Files
lnd/sqldb/sqlutils.go
Elle Mouton 9cbc1f804e multi: use sqldb.NoOpReset helper
Define a re-usable "reset" function, sqldb.NoOpReset, that can be used
for the reset parameter in sql ExecTx calls.
2025-05-28 10:18:53 +02:00

59 lines
1.4 KiB
Go

package sqldb
import (
"database/sql"
"time"
"golang.org/x/exp/constraints"
)
// NoOpReset is a no-op function that can be used as a default
// reset function ExecTx calls.
var NoOpReset = func() {}
// SQLInt32 turns a numerical integer type into the NullInt32 that sql/sqlc
// uses when an integer field can be permitted to be NULL.
//
// We use this constraints.Integer constraint here which maps to all signed and
// unsigned integer types.
func SQLInt32[T constraints.Integer](num T) sql.NullInt32 {
return sql.NullInt32{
Int32: int32(num),
Valid: true,
}
}
// SQLInt64 turns a numerical integer type into the NullInt64 that sql/sqlc
// uses when an integer field can be permitted to be NULL.
//
// We use this constraints.Integer constraint here which maps to all signed and
// unsigned integer types.
func SQLInt64[T constraints.Integer](num T) sql.NullInt64 {
return sql.NullInt64{
Int64: int64(num),
Valid: true,
}
}
// SQLStr turns a string into the NullString that sql/sqlc uses when a string
// can be permitted to be NULL.
func SQLStr(s string) sql.NullString {
if s == "" {
return sql.NullString{}
}
return sql.NullString{
String: s,
Valid: true,
}
}
// SQLTime turns a time.Time into the NullTime that sql/sqlc uses when a time
// can be permitted to be NULL.
func SQLTime(t time.Time) sql.NullTime {
return sql.NullTime{
Time: t,
Valid: true,
}
}