/
transactions.sql.go
247 lines (225 loc) · 6 KB
/
transactions.sql.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
// Code generated by sqlc. DO NOT EDIT.
// versions:
// sqlc v1.16.0
// source: transactions.sql
package db
import (
"context"
"database/sql"
"time"
"github.com/jackc/pgtype"
)
const createTransaction = `-- name: CreateTransaction :exec
INSERT INTO transactions (
id, account_id, payee_id, category_id, date, amount, notes
) VALUES ($1, $2, $3, $4, $5, $6, $7)
`
type CreateTransactionParams struct {
ID string
AccountID string
PayeeID sql.NullString
CategoryID sql.NullString
Date time.Time
Amount pgtype.Numeric
Notes sql.NullString
}
func (q *Queries) CreateTransaction(ctx context.Context, arg CreateTransactionParams) error {
_, err := q.db.Exec(ctx, createTransaction,
arg.ID,
arg.AccountID,
arg.PayeeID,
arg.CategoryID,
arg.Date,
arg.Amount,
arg.Notes,
)
return err
}
const getActivityBeforeDateByCategory = `-- name: GetActivityBeforeDateByCategory :many
SELECT categories.id, sum(transactions.amount)::numeric as activity
FROM transactions
JOIN categories
ON transactions.category_id = categories.id
JOIN accounts
ON accounts.id = transactions.account_id
AND accounts.budget_id = $1
WHERE transactions.date < $2
GROUP BY (
categories.id
)
`
type GetActivityBeforeDateByCategoryParams struct {
BudgetID string
Date time.Time
}
type GetActivityBeforeDateByCategoryRow struct {
ID string
Activity pgtype.Numeric
}
func (q *Queries) GetActivityBeforeDateByCategory(ctx context.Context, arg GetActivityBeforeDateByCategoryParams) ([]GetActivityBeforeDateByCategoryRow, error) {
rows, err := q.db.Query(ctx, getActivityBeforeDateByCategory, arg.BudgetID, arg.Date)
if err != nil {
return nil, err
}
defer rows.Close()
var items []GetActivityBeforeDateByCategoryRow
for rows.Next() {
var i GetActivityBeforeDateByCategoryRow
if err := rows.Scan(&i.ID, &i.Activity); err != nil {
return nil, err
}
items = append(items, i)
}
if err := rows.Err(); err != nil {
return nil, err
}
return items, nil
}
const getIncomeBetween = `-- name: GetIncomeBetween :one
SELECT sum(transactions.amount)::numeric
FROM transactions
JOIN categories
ON categories.id = transactions.category_id
JOIN category_groups
ON category_groups.id = categories.group_id
AND category_groups.is_income = true
JOIN accounts
ON accounts.id = transactions.account_id
AND accounts.budget_id = $1
WHERE
transactions.date <= $2
AND transactions.date >= $3
`
type GetIncomeBetweenParams struct {
BudgetID string
EndDate time.Time
BeginDate time.Time
}
func (q *Queries) GetIncomeBetween(ctx context.Context, arg GetIncomeBetweenParams) (pgtype.Numeric, error) {
row := q.db.QueryRow(ctx, getIncomeBetween, arg.BudgetID, arg.EndDate, arg.BeginDate)
var column_1 pgtype.Numeric
err := row.Scan(&column_1)
return column_1, err
}
const getTransaction = `-- name: GetTransaction :one
SELECT transactions.id, transactions.account_id, transactions.payee_id, transactions.category_id, transactions.date, transactions.amount, transactions.notes, transactions.created_at, accounts.name as account_name, accounts.budget_id as budget_id
FROM transactions
JOIN accounts
ON accounts.id = transactions.account_id
WHERE transactions.id = $1
`
type GetTransactionRow struct {
ID string
AccountID string
PayeeID sql.NullString
CategoryID sql.NullString
Date time.Time
Amount pgtype.Numeric
Notes sql.NullString
CreatedAt time.Time
AccountName string
BudgetID string
}
func (q *Queries) GetTransaction(ctx context.Context, id string) (GetTransactionRow, error) {
row := q.db.QueryRow(ctx, getTransaction, id)
var i GetTransactionRow
err := row.Scan(
&i.ID,
&i.AccountID,
&i.PayeeID,
&i.CategoryID,
&i.Date,
&i.Amount,
&i.Notes,
&i.CreatedAt,
&i.AccountName,
&i.BudgetID,
)
return i, err
}
const getTransactionsForBudget = `-- name: GetTransactionsForBudget :many
SELECT
transactions.id, transactions.account_id, transactions.payee_id, transactions.category_id, transactions.date, transactions.amount, transactions.notes, transactions.created_at,
accounts.name as account_name,
categories.name as category_name,
payees.name as payee_name
FROM transactions
JOIN accounts
ON accounts.id = transactions.account_id
AND accounts.budget_id = $1
LEFT JOIN categories
ON categories.id = transactions.category_id
LEFT JOIN payees
ON payees.id = transactions.payee_id
ORDER BY date desc
`
type GetTransactionsForBudgetRow struct {
ID string
AccountID string
PayeeID sql.NullString
CategoryID sql.NullString
Date time.Time
Amount pgtype.Numeric
Notes sql.NullString
CreatedAt time.Time
AccountName string
CategoryName sql.NullString
PayeeName sql.NullString
}
func (q *Queries) GetTransactionsForBudget(ctx context.Context, budgetID string) ([]GetTransactionsForBudgetRow, error) {
rows, err := q.db.Query(ctx, getTransactionsForBudget, budgetID)
if err != nil {
return nil, err
}
defer rows.Close()
var items []GetTransactionsForBudgetRow
for rows.Next() {
var i GetTransactionsForBudgetRow
if err := rows.Scan(
&i.ID,
&i.AccountID,
&i.PayeeID,
&i.CategoryID,
&i.Date,
&i.Amount,
&i.Notes,
&i.CreatedAt,
&i.AccountName,
&i.CategoryName,
&i.PayeeName,
); err != nil {
return nil, err
}
items = append(items, i)
}
if err := rows.Err(); err != nil {
return nil, err
}
return items, nil
}
const updateTransaction = `-- name: UpdateTransaction :exec
UPDATE transactions
SET account_id=$1, category_id=$2, payee_id=$3, date=$4, amount=$5, notes=$6
WHERE id=$7
`
type UpdateTransactionParams struct {
AccountID string
CategoryID sql.NullString
PayeeID sql.NullString
Date time.Time
Amount pgtype.Numeric
Notes sql.NullString
ID string
}
func (q *Queries) UpdateTransaction(ctx context.Context, arg UpdateTransactionParams) error {
_, err := q.db.Exec(ctx, updateTransaction,
arg.AccountID,
arg.CategoryID,
arg.PayeeID,
arg.Date,
arg.Amount,
arg.Notes,
arg.ID,
)
return err
}