ledger.sql.go239 lines, 6.4 KB
| 1 | // Code generated by sqlc. DO NOT EDIT. |
| 2 | // versions: |
| 3 | // sqlc v1.31.1 |
| 4 | // source: ledger.sql |
| 5 | |
| 6 | package walletdb |
| 7 | |
| 8 | import ( |
| 9 | "context" |
| 10 | "time" |
| 11 | |
| 12 | "github.com/google/uuid" |
| 13 | ) |
| 14 | |
| 15 | const balanceBefore = `-- name: BalanceBefore :one |
| 16 | SELECT coalesce(sum(CASE WHEN e.direction = a.normal_balance THEN e.amount ELSE -e.amount END), 0)::bigint |
| 17 | FROM entries e |
| 18 | JOIN accounts a ON a.id = e.account_id |
| 19 | WHERE e.account_id = $1 AND e.created_at < $2 |
| 20 | ` |
| 21 | |
| 22 | type BalanceBeforeParams struct { |
| 23 | AccountID uuid.UUID `json:"account_id"` |
| 24 | Before time.Time `json:"before"` |
| 25 | } |
| 26 | |
| 27 | // An account's balance from its entries before a moment: a statement's |
| 28 | // opening balance. |
| 29 | func (q *Queries) BalanceBefore(ctx context.Context, arg BalanceBeforeParams) (int64, error) { |
| 30 | row := q.db.QueryRow(ctx, balanceBefore, arg.AccountID, arg.Before) |
| 31 | var column_1 int64 |
| 32 | err := row.Scan(&column_1) |
| 33 | return column_1, err |
| 34 | } |
| 35 | |
| 36 | const createEntry = `-- name: CreateEntry :one |
| 37 | INSERT INTO entries (transaction_id, account_id, direction, amount, currency) |
| 38 | VALUES ($1, $2, $3, $4, $5) |
| 39 | RETURNING id, transaction_id, account_id, direction, amount, currency, created_at |
| 40 | ` |
| 41 | |
| 42 | type CreateEntryParams struct { |
| 43 | TransactionID uuid.UUID `json:"transaction_id"` |
| 44 | AccountID uuid.UUID `json:"account_id"` |
| 45 | Direction EntryDirection `json:"direction"` |
| 46 | Amount int64 `json:"amount"` |
| 47 | Currency string `json:"currency"` |
| 48 | } |
| 49 | |
| 50 | func (q *Queries) CreateEntry(ctx context.Context, arg CreateEntryParams) (Entry, error) { |
| 51 | row := q.db.QueryRow(ctx, createEntry, |
| 52 | arg.TransactionID, |
| 53 | arg.AccountID, |
| 54 | arg.Direction, |
| 55 | arg.Amount, |
| 56 | arg.Currency, |
| 57 | ) |
| 58 | var i Entry |
| 59 | err := row.Scan( |
| 60 | &i.ID, |
| 61 | &i.TransactionID, |
| 62 | &i.AccountID, |
| 63 | &i.Direction, |
| 64 | &i.Amount, |
| 65 | &i.Currency, |
| 66 | &i.CreatedAt, |
| 67 | ) |
| 68 | return i, err |
| 69 | } |
| 70 | |
| 71 | const createTransaction = `-- name: CreateTransaction :one |
| 72 | INSERT INTO transactions (description) |
| 73 | VALUES ($1) |
| 74 | RETURNING id, description, created_at |
| 75 | ` |
| 76 | |
| 77 | func (q *Queries) CreateTransaction(ctx context.Context, description string) (Transaction, error) { |
| 78 | row := q.db.QueryRow(ctx, createTransaction, description) |
| 79 | var i Transaction |
| 80 | err := row.Scan(&i.ID, &i.Description, &i.CreatedAt) |
| 81 | return i, err |
| 82 | } |
| 83 | |
| 84 | const listEntriesByAccount = `-- name: ListEntriesByAccount :many |
| 85 | SELECT id, transaction_id, account_id, direction, amount, currency, created_at FROM entries |
| 86 | WHERE account_id = $1 |
| 87 | AND id < $2 |
| 88 | ORDER BY id DESC |
| 89 | LIMIT $3 |
| 90 | ` |
| 91 | |
| 92 | type ListEntriesByAccountParams struct { |
| 93 | AccountID uuid.UUID `json:"account_id"` |
| 94 | BeforeID int64 `json:"before_id"` |
| 95 | PageSize int32 `json:"page_size"` |
| 96 | } |
| 97 | |
| 98 | // Newest first, one page at a time: pass the last id you saw as before_id. |
| 99 | func (q *Queries) ListEntriesByAccount(ctx context.Context, arg ListEntriesByAccountParams) ([]Entry, error) { |
| 100 | rows, err := q.db.Query(ctx, listEntriesByAccount, arg.AccountID, arg.BeforeID, arg.PageSize) |
| 101 | if err != nil { |
| 102 | return nil, err |
| 103 | } |
| 104 | defer rows.Close() |
| 105 | items := []Entry{} |
| 106 | for rows.Next() { |
| 107 | var i Entry |
| 108 | if err := rows.Scan( |
| 109 | &i.ID, |
| 110 | &i.TransactionID, |
| 111 | &i.AccountID, |
| 112 | &i.Direction, |
| 113 | &i.Amount, |
| 114 | &i.Currency, |
| 115 | &i.CreatedAt, |
| 116 | ); err != nil { |
| 117 | return nil, err |
| 118 | } |
| 119 | items = append(items, i) |
| 120 | } |
| 121 | if err := rows.Err(); err != nil { |
| 122 | return nil, err |
| 123 | } |
| 124 | return items, nil |
| 125 | } |
| 126 | |
| 127 | const statementPage = `-- name: StatementPage :many |
| 128 | SELECT e.id, e.created_at, e.transaction_id, t.description, e.direction, e.amount |
| 129 | FROM entries e |
| 130 | JOIN transactions t ON t.id = e.transaction_id |
| 131 | WHERE e.account_id = $1 |
| 132 | AND (e.created_at, e.id) > ($2::timestamptz, $3::bigint) |
| 133 | AND e.created_at < $4 |
| 134 | ORDER BY e.created_at, e.id |
| 135 | LIMIT $5 |
| 136 | ` |
| 137 | |
| 138 | type StatementPageParams struct { |
| 139 | AccountID uuid.UUID `json:"account_id"` |
| 140 | AfterAt time.Time `json:"after_at"` |
| 141 | AfterID int64 `json:"after_id"` |
| 142 | Until time.Time `json:"until"` |
| 143 | PageSize int32 `json:"page_size"` |
| 144 | } |
| 145 | |
| 146 | type StatementPageRow struct { |
| 147 | ID int64 `json:"id"` |
| 148 | CreatedAt time.Time `json:"created_at"` |
| 149 | TransactionID uuid.UUID `json:"transaction_id"` |
| 150 | Description string `json:"description"` |
| 151 | Direction EntryDirection `json:"direction"` |
| 152 | Amount int64 `json:"amount"` |
| 153 | } |
| 154 | |
| 155 | // A page of an account's entries posted before until, oldest first, after the |
| 156 | // entry at (after_at, after_id): keyset pagination, in the order of the index |
| 157 | // on (account_id, created_at, id), as fast for the last page as the first. |
| 158 | // The first page starts after (since, 0). |
| 159 | func (q *Queries) StatementPage(ctx context.Context, arg StatementPageParams) ([]StatementPageRow, error) { |
| 160 | rows, err := q.db.Query(ctx, statementPage, |
| 161 | arg.AccountID, |
| 162 | arg.AfterAt, |
| 163 | arg.AfterID, |
| 164 | arg.Until, |
| 165 | arg.PageSize, |
| 166 | ) |
| 167 | if err != nil { |
| 168 | return nil, err |
| 169 | } |
| 170 | defer rows.Close() |
| 171 | items := []StatementPageRow{} |
| 172 | for rows.Next() { |
| 173 | var i StatementPageRow |
| 174 | if err := rows.Scan( |
| 175 | &i.ID, |
| 176 | &i.CreatedAt, |
| 177 | &i.TransactionID, |
| 178 | &i.Description, |
| 179 | &i.Direction, |
| 180 | &i.Amount, |
| 181 | ); err != nil { |
| 182 | return nil, err |
| 183 | } |
| 184 | items = append(items, i) |
| 185 | } |
| 186 | if err := rows.Err(); err != nil { |
| 187 | return nil, err |
| 188 | } |
| 189 | return items, nil |
| 190 | } |
| 191 | |
| 192 | const trialBalance = `-- name: TrialBalance :many |
| 193 | SELECT currency, coalesce(account, 'total')::text AS account, |
| 194 | sum(debit)::bigint AS debits, sum(credit)::bigint AS credits |
| 195 | FROM ( |
| 196 | SELECT a.currency, CASE a.kind WHEN 'wallet' THEN 'customer wallets' ELSE a.name END AS account, |
| 197 | CASE e.direction WHEN 'debit' THEN e.amount ELSE 0 END AS debit, |
| 198 | CASE e.direction WHEN 'credit' THEN e.amount ELSE 0 END AS credit |
| 199 | FROM entries e |
| 200 | JOIN accounts a ON a.id = e.account_id |
| 201 | WHERE e.created_at < $1 |
| 202 | ) AS lines |
| 203 | GROUP BY currency, ROLLUP (account) |
| 204 | ORDER BY currency, account IS NULL, account |
| 205 | ` |
| 206 | |
| 207 | type TrialBalanceRow struct { |
| 208 | Currency string `json:"currency"` |
| 209 | Account string `json:"account"` |
| 210 | Debits int64 `json:"debits"` |
| 211 | Credits int64 `json:"credits"` |
| 212 | } |
| 213 | |
| 214 | // Every ledger account's debits and credits before a moment, with customer |
| 215 | // wallets as one account, and a total per currency, where the two must match. |
| 216 | func (q *Queries) TrialBalance(ctx context.Context, before time.Time) ([]TrialBalanceRow, error) { |
| 217 | rows, err := q.db.Query(ctx, trialBalance, before) |
| 218 | if err != nil { |
| 219 | return nil, err |
| 220 | } |
| 221 | defer rows.Close() |
| 222 | items := []TrialBalanceRow{} |
| 223 | for rows.Next() { |
| 224 | var i TrialBalanceRow |
| 225 | if err := rows.Scan( |
| 226 | &i.Currency, |
| 227 | &i.Account, |
| 228 | &i.Debits, |
| 229 | &i.Credits, |
| 230 | ); err != nil { |
| 231 | return nil, err |
| 232 | } |
| 233 | items = append(items, i) |
| 234 | } |
| 235 | if err := rows.Err(); err != nil { |
| 236 | return nil, err |
| 237 | } |
| 238 | return items, nil |
| 239 | } |