sqldb: export sql null type helpers

This commit is contained in:
Andras Banki-Horvath committed 2024-04-09 20:46:11 +02:00
1 parent 16c8339034
commit 6d316ef56f
2 files changed
+26 -26

No files matched your search

+18 -18
View File
@@ -196,13 +196,13 @@ func (i *InvoiceStore) AddInvoice(ctx context.Context,
err := i.db.ExecTx(ctx, &writeTxOpts, func(db InvoiceQueries) error { err := i.db.ExecTx(ctx, &writeTxOpts, func(db InvoiceQueries) error {
params := sqlc.InsertInvoiceParams{ params := sqlc.InsertInvoiceParams{
Hash: paymentHash[:], Hash: paymentHash[:],
Memo: sqlStr(string(newInvoice.Memo)), Memo: SQLStr(string(newInvoice.Memo)),
AmountMsat: int64(newInvoice.Terms.Value), AmountMsat: int64(newInvoice.Terms.Value),
// Note: BOLT12 invoices don't have a final cltv delta. // Note: BOLT12 invoices don't have a final cltv delta.
CltvDelta: sqlInt32(newInvoice.Terms.FinalCltvDelta), CltvDelta: SQLInt32(newInvoice.Terms.FinalCltvDelta),
Expiry: int32(newInvoice.Terms.Expiry), Expiry: int32(newInvoice.Terms.Expiry),
// Note: keysend invoices don't have a payment request. // Note: keysend invoices don't have a payment request.
PaymentRequest: sqlStr(string( PaymentRequest: SQLStr(string(
newInvoice.PaymentRequest), newInvoice.PaymentRequest),
), ),
PaymentRequestHash: paymentRequestHash, PaymentRequestHash: paymentRequestHash,
@@ -678,7 +678,7 @@ func (i *InvoiceStore) InvoicesSettledSince(ctx context.Context, idx uint64) (
err := queryWithLimit(func(offset int) (int, error) { err := queryWithLimit(func(offset int) (int, error) {
params := sqlc.FilterInvoicesParams{ params := sqlc.FilterInvoicesParams{
SettleIndexGet: sqlInt64(settleIdx + 1), SettleIndexGet: SQLInt64(settleIdx + 1),
NumLimit: int32(limit), NumLimit: int32(limit),
NumOffset: int32(offset), NumOffset: int32(offset),
} }
@@ -715,7 +715,7 @@ func (i *InvoiceStore) InvoicesSettledSince(ctx context.Context, idx uint64) (
// the provided index. // the provided index.
ampInvoices, err := i.db.FetchSettledAMPSubInvoices( ampInvoices, err := i.db.FetchSettledAMPSubInvoices(
ctx, sqlc.FetchSettledAMPSubInvoicesParams{ ctx, sqlc.FetchSettledAMPSubInvoicesParams{
SettleIndexGet: sqlInt64(idx + 1), SettleIndexGet: SQLInt64(idx + 1),
}, },
) )
if err != nil { if err != nil {
@@ -792,7 +792,7 @@ func (i *InvoiceStore) InvoicesAddedSince(ctx context.Context, idx uint64) (
return queryWithLimit(func(offset int) (int, error) { return queryWithLimit(func(offset int) (int, error) {
params := sqlc.FilterInvoicesParams{ params := sqlc.FilterInvoicesParams{
AddIndexGet: sqlInt64(addIdx + 1), AddIndexGet: SQLInt64(addIdx + 1),
NumLimit: int32(limit), NumLimit: int32(limit),
NumOffset: int32(offset), NumOffset: int32(offset),
} }
@@ -856,7 +856,7 @@ func (i *InvoiceStore) QueryInvoices(ctx context.Context,
if !q.Reversed { if !q.Reversed {
// The invoice with index offset id must not be // The invoice with index offset id must not be
// included in the results. // included in the results.
params.AddIndexGet = sqlInt64( params.AddIndexGet = SQLInt64(
q.IndexOffset + uint64(offset) + 1, q.IndexOffset + uint64(offset) + 1,
) )
} }
@@ -867,13 +867,13 @@ func (i *InvoiceStore) QueryInvoices(ctx context.Context,
// If the index offset was not set, we want to // If the index offset was not set, we want to
// fetch from the lastest invoice. // fetch from the lastest invoice.
if idx == 0 { if idx == 0 {
params.AddIndexLet = sqlInt64( params.AddIndexLet = SQLInt64(
int64(math.MaxInt64), int64(math.MaxInt64),
) )
} else { } else {
// The invoice with index offset id must // The invoice with index offset id must
// not be included in the results. // not be included in the results.
params.AddIndexLet = sqlInt64( params.AddIndexLet = SQLInt64(
idx - int32(offset) - 1, idx - int32(offset) - 1,
) )
} }
@@ -882,13 +882,13 @@ func (i *InvoiceStore) QueryInvoices(ctx context.Context,
} }
if q.CreationDateStart != 0 { if q.CreationDateStart != 0 {
params.CreatedAfter = sqlTime( params.CreatedAfter = SQLTime(
time.Unix(q.CreationDateStart, 0).UTC(), time.Unix(q.CreationDateStart, 0).UTC(),
) )
} }
if q.CreationDateEnd != 0 { if q.CreationDateEnd != 0 {
params.CreatedBefore = sqlTime( params.CreatedBefore = SQLTime(
time.Unix(q.CreationDateEnd, 0).UTC(), time.Unix(q.CreationDateEnd, 0).UTC(),
) )
} }
@@ -1081,7 +1081,7 @@ func (s *sqlInvoiceUpdater) ResolveHtlc(circuitKey models.CircuitKey,
), ),
InvoiceID: int64(s.invoice.AddIndex), InvoiceID: int64(s.invoice.AddIndex),
State: int16(state), State: int16(state),
ResolveTime: sqlTime(resolveTime.UTC()), ResolveTime: SQLTime(resolveTime.UTC()),
}) })
} }
@@ -1129,10 +1129,10 @@ func (s *sqlInvoiceUpdater) UpdateInvoiceState(
return err return err
} }
settleIndex = sqlInt64(nextSettleIndex) settleIndex = SQLInt64(nextSettleIndex)
// If the invoice is settled, we'll also update the settle time. // If the invoice is settled, we'll also update the settle time.
settledAt = sqlTime(s.updateTime.UTC()) settledAt = SQLTime(s.updateTime.UTC())
err = s.db.OnInvoiceSettled( err = s.db.OnInvoiceSettled(
s.ctx, sqlc.OnInvoiceSettledParams{ s.ctx, sqlc.OnInvoiceSettledParams{
@@ -1219,10 +1219,10 @@ func (s *sqlInvoiceUpdater) UpdateAmpState(setID [32]byte,
return err return err
} }
settleIndex = sqlInt64(nextSettleIndex) settleIndex = SQLInt64(nextSettleIndex)
// If the invoice is settled, we'll also update the settle time. // If the invoice is settled, we'll also update the settle time.
settledAt = sqlTime(s.updateTime.UTC()) settledAt = SQLTime(s.updateTime.UTC())
err = s.db.OnAMPSubInvoiceSettled( err = s.db.OnAMPSubInvoiceSettled(
s.ctx, sqlc.OnAMPSubInvoiceSettledParams{ s.ctx, sqlc.OnAMPSubInvoiceSettledParams{
@@ -1338,11 +1338,11 @@ func (i *InvoiceStore) DeleteInvoice(ctx context.Context,
err := i.db.ExecTx(ctx, &writeTxOpt, func(db InvoiceQueries) error { err := i.db.ExecTx(ctx, &writeTxOpt, func(db InvoiceQueries) error {
for _, ref := range invoicesToDelete { for _, ref := range invoicesToDelete {
params := sqlc.DeleteInvoiceParams{ params := sqlc.DeleteInvoiceParams{
AddIndex: sqlInt64(ref.AddIndex), AddIndex: SQLInt64(ref.AddIndex),
} }
if ref.SettleIndex != 0 { if ref.SettleIndex != 0 {
params.SettleIndex = sqlInt64(ref.SettleIndex) params.SettleIndex = SQLInt64(ref.SettleIndex)
} }
if ref.PayHash != lntypes.ZeroHash { if ref.PayHash != lntypes.ZeroHash {
+8 -8
View File
@@ -7,33 +7,33 @@ import (
"golang.org/x/exp/constraints" "golang.org/x/exp/constraints"
) )
// sqlInt32 turns a numerical integer type into the NullInt32 that sql/sqlc // SQLInt32 turns a numerical integer type into the NullInt32 that sql/sqlc
// uses when an integer field can be permitted to be NULL. // uses when an integer field can be permitted to be NULL.
// //
// We use this constraints.Integer constraint here which maps to all signed and // We use this constraints.Integer constraint here which maps to all signed and
// unsigned integer types. // unsigned integer types.
func sqlInt32[T constraints.Integer](num T) sql.NullInt32 { func SQLInt32[T constraints.Integer](num T) sql.NullInt32 {
return sql.NullInt32{ return sql.NullInt32{
Int32: int32(num), Int32: int32(num),
Valid: true, Valid: true,
} }
} }
// sqlInt64 turns a numerical integer type into the NullInt64 that sql/sqlc // SQLInt64 turns a numerical integer type into the NullInt64 that sql/sqlc
// uses when an integer field can be permitted to be NULL. // uses when an integer field can be permitted to be NULL.
// //
// We use this constraints.Integer constraint here which maps to all signed and // We use this constraints.Integer constraint here which maps to all signed and
// unsigned integer types. // unsigned integer types.
func sqlInt64[T constraints.Integer](num T) sql.NullInt64 { func SQLInt64[T constraints.Integer](num T) sql.NullInt64 {
return sql.NullInt64{ return sql.NullInt64{
Int64: int64(num), Int64: int64(num),
Valid: true, Valid: true,
} }
} }
// sqlStr turns a string into the NullString that sql/sqlc uses when a string // SQLStr turns a string into the NullString that sql/sqlc uses when a string
// can be permitted to be NULL. // can be permitted to be NULL.
func sqlStr(s string) sql.NullString { func SQLStr(s string) sql.NullString {
if s == "" { if s == "" {
return sql.NullString{} return sql.NullString{}
} }
@@ -44,9 +44,9 @@ func sqlStr(s string) sql.NullString {
} }
} }
// sqlTime turns a time.Time into the NullTime that sql/sqlc uses when a time // SQLTime turns a time.Time into the NullTime that sql/sqlc uses when a time
// can be permitted to be NULL. // can be permitted to be NULL.
func sqlTime(t time.Time) sql.NullTime { func SQLTime(t time.Time) sql.NullTime {
return sql.NullTime{ return sql.NullTime{
Time: t, Time: t,
Valid: true, Valid: true,