accounts.sql.go162 lines, 3.7 KB
| 1 | // Code generated by sqlc. DO NOT EDIT. |
| 2 | // versions: |
| 3 | // sqlc v1.31.1 |
| 4 | // source: accounts.sql |
| 5 | |
| 6 | package walletdb |
| 7 | |
| 8 | import ( |
| 9 | "context" |
| 10 | |
| 11 | "github.com/google/uuid" |
| 12 | ) |
| 13 | |
| 14 | const createAccount = `-- name: CreateAccount :one |
| 15 | INSERT INTO accounts (kind, owner_id, name, currency, normal_balance) |
| 16 | VALUES ($1, $2, $3, $4, $5) |
| 17 | RETURNING id, owner_id, name, currency, normal_balance, created_at, kind, frozen |
| 18 | ` |
| 19 | |
| 20 | type CreateAccountParams struct { |
| 21 | Kind AccountKind `json:"kind"` |
| 22 | OwnerID *uuid.UUID `json:"owner_id"` |
| 23 | Name string `json:"name"` |
| 24 | Currency string `json:"currency"` |
| 25 | NormalBalance EntryDirection `json:"normal_balance"` |
| 26 | } |
| 27 | |
| 28 | func (q *Queries) CreateAccount(ctx context.Context, arg CreateAccountParams) (Account, error) { |
| 29 | row := q.db.QueryRow(ctx, createAccount, |
| 30 | arg.Kind, |
| 31 | arg.OwnerID, |
| 32 | arg.Name, |
| 33 | arg.Currency, |
| 34 | arg.NormalBalance, |
| 35 | ) |
| 36 | var i Account |
| 37 | err := row.Scan( |
| 38 | &i.ID, |
| 39 | &i.OwnerID, |
| 40 | &i.Name, |
| 41 | &i.Currency, |
| 42 | &i.NormalBalance, |
| 43 | &i.CreatedAt, |
| 44 | &i.Kind, |
| 45 | &i.Frozen, |
| 46 | ) |
| 47 | return i, err |
| 48 | } |
| 49 | |
| 50 | const getAccount = `-- name: GetAccount :one |
| 51 | SELECT id, owner_id, name, currency, normal_balance, created_at, kind, frozen FROM accounts |
| 52 | WHERE id = $1 |
| 53 | ` |
| 54 | |
| 55 | func (q *Queries) GetAccount(ctx context.Context, id uuid.UUID) (Account, error) { |
| 56 | row := q.db.QueryRow(ctx, getAccount, id) |
| 57 | var i Account |
| 58 | err := row.Scan( |
| 59 | &i.ID, |
| 60 | &i.OwnerID, |
| 61 | &i.Name, |
| 62 | &i.Currency, |
| 63 | &i.NormalBalance, |
| 64 | &i.CreatedAt, |
| 65 | &i.Kind, |
| 66 | &i.Frozen, |
| 67 | ) |
| 68 | return i, err |
| 69 | } |
| 70 | |
| 71 | const getAccountBalance = `-- name: GetAccountBalance :one |
| 72 | SELECT account_id, name, currency, balance FROM account_balances |
| 73 | WHERE account_id = $1 |
| 74 | ` |
| 75 | |
| 76 | func (q *Queries) GetAccountBalance(ctx context.Context, accountID uuid.UUID) (AccountBalance, error) { |
| 77 | row := q.db.QueryRow(ctx, getAccountBalance, accountID) |
| 78 | var i AccountBalance |
| 79 | err := row.Scan( |
| 80 | &i.AccountID, |
| 81 | &i.Name, |
| 82 | &i.Currency, |
| 83 | &i.Balance, |
| 84 | ) |
| 85 | return i, err |
| 86 | } |
| 87 | |
| 88 | const getSystemAccount = `-- name: GetSystemAccount :one |
| 89 | SELECT id, owner_id, name, currency, normal_balance, created_at, kind, frozen FROM accounts |
| 90 | WHERE kind = 'system' AND name = $1 AND currency = $2 |
| 91 | ` |
| 92 | |
| 93 | type GetSystemAccountParams struct { |
| 94 | Name string `json:"name"` |
| 95 | Currency string `json:"currency"` |
| 96 | } |
| 97 | |
| 98 | func (q *Queries) GetSystemAccount(ctx context.Context, arg GetSystemAccountParams) (Account, error) { |
| 99 | row := q.db.QueryRow(ctx, getSystemAccount, arg.Name, arg.Currency) |
| 100 | var i Account |
| 101 | err := row.Scan( |
| 102 | &i.ID, |
| 103 | &i.OwnerID, |
| 104 | &i.Name, |
| 105 | &i.Currency, |
| 106 | &i.NormalBalance, |
| 107 | &i.CreatedAt, |
| 108 | &i.Kind, |
| 109 | &i.Frozen, |
| 110 | ) |
| 111 | return i, err |
| 112 | } |
| 113 | |
| 114 | const listAccountsByOwner = `-- name: ListAccountsByOwner :many |
| 115 | SELECT id, owner_id, name, currency, normal_balance, created_at, kind, frozen FROM accounts |
| 116 | WHERE owner_id = $1::uuid |
| 117 | ORDER BY created_at |
| 118 | ` |
| 119 | |
| 120 | func (q *Queries) ListAccountsByOwner(ctx context.Context, ownerID uuid.UUID) ([]Account, error) { |
| 121 | rows, err := q.db.Query(ctx, listAccountsByOwner, ownerID) |
| 122 | if err != nil { |
| 123 | return nil, err |
| 124 | } |
| 125 | defer rows.Close() |
| 126 | items := []Account{} |
| 127 | for rows.Next() { |
| 128 | var i Account |
| 129 | if err := rows.Scan( |
| 130 | &i.ID, |
| 131 | &i.OwnerID, |
| 132 | &i.Name, |
| 133 | &i.Currency, |
| 134 | &i.NormalBalance, |
| 135 | &i.CreatedAt, |
| 136 | &i.Kind, |
| 137 | &i.Frozen, |
| 138 | ); err != nil { |
| 139 | return nil, err |
| 140 | } |
| 141 | items = append(items, i) |
| 142 | } |
| 143 | if err := rows.Err(); err != nil { |
| 144 | return nil, err |
| 145 | } |
| 146 | return items, nil |
| 147 | } |
| 148 | |
| 149 | const lockAccount = `-- name: LockAccount :one |
| 150 | SELECT id FROM accounts |
| 151 | WHERE id = $1 |
| 152 | FOR UPDATE |
| 153 | ` |
| 154 | |
| 155 | // Locks an account's row until the end of the transaction. Anyone else who |
| 156 | // locks it waits until we commit or roll back. |
| 157 | func (q *Queries) LockAccount(ctx context.Context, id uuid.UUID) (uuid.UUID, error) { |
| 158 | row := q.db.QueryRow(ctx, lockAccount, id) |
| 159 | var id_2 uuid.UUID |
| 160 | err := row.Scan(&id_2) |
| 161 | return id_2, err |
| 162 | } |