Simplify query

This commit is contained in:
Jan Bader 2022-04-22 18:39:28 +00:00 committed by Gitea
parent 07c0c56d11
commit 777520e9df
2 changed files with 17 additions and 66 deletions

View File

@ -48,8 +48,8 @@ FROM transactions_by_month
WHERE transactions_by_month.budget_id = @budget_id;
-- name: GetProblematicTransactions :many
SELECT *
FROM transactions
SELECT transactions.*
FROM display_transactions AS transactions
LEFT JOIN accounts
ON transactions.account_id = accounts.id
LEFT JOIN transactions AS otherGroupTransaction
@ -57,7 +57,7 @@ LEFT JOIN transactions AS otherGroupTransaction
AND transactions.id != otherGroupTransaction.id
AND transactions.account_id != otherGroupTransaction.account_id
LEFT JOIn accounts AS otherGroupAccount
ON otherGroupTransaction.account_id = accounts.id
ON otherGroupTransaction.account_id = otherGroupAccount.id
WHERE transactions.category_id IS NULL
AND accounts.on_budget
AND (otherGroupAccount.id IS NULL OR NOT otherGroupAccount.on_budget)

View File

@ -7,7 +7,6 @@ package postgres
import (
"context"
"database/sql"
"time"
"git.javil.eu/jacob1123/budgeteer/postgres/numeric"
@ -119,8 +118,8 @@ func (q *Queries) GetAllTransactionsForBudget(ctx context.Context, budgetID uuid
}
const getProblematicTransactions = `-- name: GetProblematicTransactions :many
SELECT transactions.id, transactions.date, transactions.memo, transactions.amount, transactions.account_id, transactions.category_id, transactions.payee_id, transactions.group_id, transactions.status, accounts.id, accounts.budget_id, accounts.name, accounts.on_budget, accounts.is_open, accounts.last_reconciled, othergrouptransaction.id, othergrouptransaction.date, othergrouptransaction.memo, othergrouptransaction.amount, othergrouptransaction.account_id, othergrouptransaction.category_id, othergrouptransaction.payee_id, othergrouptransaction.group_id, othergrouptransaction.status, othergroupaccount.id, othergroupaccount.budget_id, othergroupaccount.name, othergroupaccount.on_budget, othergroupaccount.is_open, othergroupaccount.last_reconciled
FROM transactions
SELECT transactions.id, transactions.date, transactions.memo, transactions.amount, transactions.group_id, transactions.status, transactions.account, transactions.payee_id, transactions.category_id, transactions.payee, transactions.category_group, transactions.category, transactions.transfer_account, transactions.budget_id, transactions.account_id
FROM display_transactions AS transactions
LEFT JOIN accounts
ON transactions.account_id = accounts.id
LEFT JOIN transactions AS otherGroupTransaction
@ -128,86 +127,38 @@ LEFT JOIN transactions AS otherGroupTransaction
AND transactions.id != otherGroupTransaction.id
AND transactions.account_id != otherGroupTransaction.account_id
LEFT JOIn accounts AS otherGroupAccount
ON otherGroupTransaction.account_id = accounts.id
ON otherGroupTransaction.account_id = otherGroupAccount.id
WHERE transactions.category_id IS NULL
AND accounts.on_budget
AND (otherGroupAccount.id IS NULL OR NOT otherGroupAccount.on_budget)
AND accounts.budget_id = $1
`
type GetProblematicTransactionsRow struct {
ID uuid.UUID
Date time.Time
Memo string
Amount numeric.Numeric
AccountID uuid.UUID
CategoryID uuid.NullUUID
PayeeID uuid.NullUUID
GroupID uuid.NullUUID
Status TransactionStatus
ID_2 uuid.NullUUID
BudgetID uuid.NullUUID
Name sql.NullString
OnBudget sql.NullBool
IsOpen sql.NullBool
LastReconciled sql.NullTime
ID_3 uuid.UUID
Date_2 time.Time
Memo_2 string
Amount_2 numeric.Numeric
AccountID_2 uuid.UUID
CategoryID_2 uuid.NullUUID
PayeeID_2 uuid.NullUUID
GroupID_2 uuid.NullUUID
Status_2 TransactionStatus
ID_4 uuid.NullUUID
BudgetID_2 uuid.NullUUID
Name_2 sql.NullString
OnBudget_2 sql.NullBool
IsOpen_2 sql.NullBool
LastReconciled_2 sql.NullTime
}
func (q *Queries) GetProblematicTransactions(ctx context.Context, budgetID uuid.UUID) ([]GetProblematicTransactionsRow, error) {
func (q *Queries) GetProblematicTransactions(ctx context.Context, budgetID uuid.UUID) ([]DisplayTransaction, error) {
rows, err := q.db.QueryContext(ctx, getProblematicTransactions, budgetID)
if err != nil {
return nil, err
}
defer rows.Close()
var items []GetProblematicTransactionsRow
var items []DisplayTransaction
for rows.Next() {
var i GetProblematicTransactionsRow
var i DisplayTransaction
if err := rows.Scan(
&i.ID,
&i.Date,
&i.Memo,
&i.Amount,
&i.AccountID,
&i.CategoryID,
&i.PayeeID,
&i.GroupID,
&i.Status,
&i.ID_2,
&i.Account,
&i.PayeeID,
&i.CategoryID,
&i.Payee,
&i.CategoryGroup,
&i.Category,
&i.TransferAccount,
&i.BudgetID,
&i.Name,
&i.OnBudget,
&i.IsOpen,
&i.LastReconciled,
&i.ID_3,
&i.Date_2,
&i.Memo_2,
&i.Amount_2,
&i.AccountID_2,
&i.CategoryID_2,
&i.PayeeID_2,
&i.GroupID_2,
&i.Status_2,
&i.ID_4,
&i.BudgetID_2,
&i.Name_2,
&i.OnBudget_2,
&i.IsOpen_2,
&i.LastReconciled_2,
&i.AccountID,
); err != nil {
return nil, err
}