From 54d68175d2cf88f5d3528852d8275ebbae9c1f6e Mon Sep 17 00:00:00 2001 From: Tim Wundenberg Date: Sun, 4 May 2025 15:17:48 +0200 Subject: [PATCH] feat: use sqlx --- db/account.go | 43 ++++++++----------------------------------- go.mod | 1 + go.sum | 2 ++ 3 files changed, 11 insertions(+), 35 deletions(-) diff --git a/db/account.go b/db/account.go index 4a007d7..fa14b53 100644 --- a/db/account.go +++ b/db/account.go @@ -4,7 +4,7 @@ import ( "spend-sparrow/log" "spend-sparrow/types" - "database/sql" + "github.com/jmoiron/sqlx" "github.com/google/uuid" ) @@ -18,10 +18,10 @@ type Account interface { } type AccountSqlite struct { - db *sql.DB + db *sqlx.DB } -func NewAccountSqlite(db *sql.DB) *AccountSqlite { +func NewAccountSqlite(db *sqlx.DB) *AccountSqlite { return &AccountSqlite{db: db} } @@ -60,7 +60,8 @@ func (db AccountSqlite) Update(account *types.Account) error { func (db AccountSqlite) GetAll(groupId uuid.UUID) ([]*types.Account, error) { - rows, err := db.db.Query(` + accounts := make([]*types.Account, 0) + err := db.db.Select(&accounts, ` SELECT id, name, current_balance, last_transaction, oink_balance, @@ -73,23 +74,13 @@ func (db AccountSqlite) GetAll(groupId uuid.UUID) ([]*types.Account, error) { return nil, types.ErrInternal } - var accounts = make([]*types.Account, 0) - for rows.Next() { - - account, err := scanAccount(rows) - if err != nil { - return nil, types.ErrInternal - } - - accounts = append(accounts, account) - } - return accounts, nil } func (db AccountSqlite) Get(groupId uuid.UUID, id uuid.UUID) (*types.Account, error) { - rows, err := db.db.Query(` + account := &types.Account{} + err := db.db.Get(account, ` SELECT id, name, current_balance, last_transaction, oink_balance, @@ -102,25 +93,7 @@ func (db AccountSqlite) Get(groupId uuid.UUID, id uuid.UUID) (*types.Account, er return nil, types.ErrInternal } - if !rows.Next() { - return nil, ErrNotFound - } - - return scanAccount(rows) -} - -func scanAccount(rows *sql.Rows) (*types.Account, error) { - var ( - account types.Account - ) - - err := rows.Scan(&account.Id, &account.Name, &account.CurrentBalance, &account.LastTransaction, &account.OinkBalance, &account.CreatedAt, &account.CreatedBy, &account.UpdatedAt, &account.UpdatedBy) - if err != nil { - log.Error("Could not scan account: %v", err) - return nil, types.ErrInternal - } - - return &account, nil + return account, nil } func (db AccountSqlite) Delete(id uuid.UUID) error { diff --git a/go.mod b/go.mod index f58a0b0..f4cfb1f 100644 --- a/go.mod +++ b/go.mod @@ -8,6 +8,7 @@ require ( github.com/a-h/templ v0.3.857 github.com/golang-migrate/migrate/v4 v4.18.2 github.com/google/uuid v1.6.0 + github.com/jmoiron/sqlx v1.4.0 github.com/joho/godotenv v1.5.1 github.com/mattn/go-sqlite3 v1.14.24 github.com/prometheus/client_golang v1.21.1 diff --git a/go.sum b/go.sum index af9ceb4..29e772e 100644 --- a/go.sum +++ b/go.sum @@ -17,6 +17,8 @@ github.com/hashicorp/errwrap v1.1.0 h1:OxrOeh75EUXMY8TBjag2fzXGZ40LB6IKw45YeGUDY github.com/hashicorp/errwrap v1.1.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4= github.com/hashicorp/go-multierror v1.1.1 h1:H5DkEtf6CXdFp0N0Em5UCwQpXMWke8IA0+lD48awMYo= github.com/hashicorp/go-multierror v1.1.1/go.mod h1:iw975J/qwKPdAO1clOe2L8331t/9/fmwbPZ6JB6eMoM= +github.com/jmoiron/sqlx v1.4.0 h1:1PLqN7S1UYp5t4SrVVnt4nUVNemrDAtxlulVe+Qgm3o= +github.com/jmoiron/sqlx v1.4.0/go.mod h1:ZrZ7UsYB/weZdl2Bxg6jCRO9c3YHl8r3ahlKmRT4JLY= github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0= github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4= github.com/klauspost/compress v1.17.11 h1:In6xLpyWOi1+C7tXUUWv2ot1QvBjxevKAaI6IXrJmUc=