From a132fca1ba3e07e0f73faf8d1d104275938fae33 Mon Sep 17 00:00:00 2001 From: cyberguru1 Date: Thu, 28 May 2026 17:29:12 -0500 Subject: [PATCH] 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. --- db/sqlc/accounts.sql.go | 55 ++++++++++++++++++++++++++++++++++++ db/sqlc/querier.go | 2 ++ db/sqlc/queries/accounts.sql | 12 ++++++++ 3 files changed, 69 insertions(+) diff --git a/db/sqlc/accounts.sql.go b/db/sqlc/accounts.sql.go index 6322e709..7b399fc6 100644 --- a/db/sqlc/accounts.sql.go +++ b/db/sqlc/accounts.sql.go @@ -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 diff --git a/db/sqlc/querier.go b/db/sqlc/querier.go index 4b32259f..96cea34b 100644 --- a/db/sqlc/querier.go +++ b/db/sqlc/querier.go @@ -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 diff --git a/db/sqlc/queries/accounts.sql b/db/sqlc/queries/accounts.sql index 4fff012a..416b74d7 100644 --- a/db/sqlc/queries/accounts.sql +++ b/db/sqlc/queries/accounts.sql @@ -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