db/sqlc: add queries for account payments

Add SQL queries to select account payments with pagination (limit and
offset), and a query to count the total payments for a given account.
This commit is contained in:
cyberguru1 2026-05-28 17:29:12 -05:00
parent f5a1a020eb
commit a132fca1ba
No known key found for this signature in database
GPG key ID: F0FB5ECF1A8786E6
3 changed files with 69 additions and 0 deletions

View file

@ -11,6 +11,48 @@ import (
"time"
)
const accountPaymentsPaginated = `-- name: AccountPaymentsPaginated :many
SELECT account_id, hash, status, full_amount_msat
FROM account_payments
WHERE account_id = $1
ORDER BY hash ASC
LIMIT $2 OFFSET $3
`
type AccountPaymentsPaginatedParams struct {
AccountID int64
Limit int32
Offset int32
}
func (q *Queries) AccountPaymentsPaginated(ctx context.Context, arg AccountPaymentsPaginatedParams) ([]AccountPayment, error) {
rows, err := q.db.QueryContext(ctx, accountPaymentsPaginated, arg.AccountID, arg.Limit, arg.Offset)
if err != nil {
return nil, err
}
defer rows.Close()
var items []AccountPayment
for rows.Next() {
var i AccountPayment
if err := rows.Scan(
&i.AccountID,
&i.Hash,
&i.Status,
&i.FullAmountMsat,
); err != nil {
return nil, err
}
items = append(items, i)
}
if err := rows.Close(); err != nil {
return nil, err
}
if err := rows.Err(); err != nil {
return nil, err
}
return items, nil
}
const addAccountInvoice = `-- name: AddAccountInvoice :exec
INSERT INTO account_invoices (account_id, hash)
VALUES ($1, $2)
@ -26,6 +68,19 @@ func (q *Queries) AddAccountInvoice(ctx context.Context, arg AddAccountInvoicePa
return err
}
const countAccountPayments = `-- name: CountAccountPayments :one
SELECT COUNT(*)
FROM account_payments
WHERE account_id = $1
`
func (q *Queries) CountAccountPayments(ctx context.Context, accountID int64) (int64, error) {
row := q.db.QueryRowContext(ctx, countAccountPayments, accountID)
var count int64
err := row.Scan(&count)
return count, err
}
const deleteAccount = `-- name: DeleteAccount :exec
DELETE FROM accounts
WHERE id = $1

View file

@ -10,7 +10,9 @@ import (
)
type Querier interface {
AccountPaymentsPaginated(ctx context.Context, arg AccountPaymentsPaginatedParams) ([]AccountPayment, error)
AddAccountInvoice(ctx context.Context, arg AddAccountInvoiceParams) error
CountAccountPayments(ctx context.Context, accountID int64) (int64, error)
DeleteAccount(ctx context.Context, id int64) error
DeleteAccountPayment(ctx context.Context, arg DeleteAccountPaymentParams) error
DeleteAllTempKVStores(ctx context.Context) error

View file

@ -80,6 +80,18 @@ WHERE account_id = $1;
SELECT *
FROM account_payments;
-- name: AccountPaymentsPaginated :many
SELECT *
FROM account_payments
WHERE account_id = $1
ORDER BY hash ASC
LIMIT $2 OFFSET $3;
-- name: CountAccountPayments :one
SELECT COUNT(*)
FROM account_payments
WHERE account_id = $1;
-- name: ListAccountInvoices :many
SELECT *
FROM account_invoices