Switched to purer libraries

This commit is contained in:
Magnus Åhall 2026-08-19 17:53:52 +02:00
parent 8fa8bb4c3a
commit 9496947f01
21 changed files with 390 additions and 316 deletions

33
area.go
View file

@ -2,10 +2,11 @@ package main
import (
// External
werr "git.gibonuddevalla.se/go/wrappederror"
werr "git.ahall.se/go/wrappederror"
"github.com/jackc/pgx/v5"
// Standard
"database/sql"
"context"
"encoding/json"
"sort"
)
@ -19,7 +20,7 @@ type Area struct {
func AreaRetrieve() (areas []Area, err error) { // {{{
areas = []Area{}
row := service.Db.Conn.QueryRow(`
row := db.QueryRow(context.Background(), `
SELECT
jsonb_agg(jsonsections)
FROM (
@ -59,21 +60,23 @@ func AreaRetrieve() (areas []Area, err error) { // {{{
return
} // }}}
func AreaCreate(name string) (err error) { // {{{
_, err = service.Db.Conn.Exec(`INSERT INTO area(name) VALUES($1)`, name)
_, err = db.Exec(context.Background(), `INSERT INTO area(name) VALUES($1)`, name)
return
} // }}}
func AreaRename(id int, name string) (err error) { // {{{
_, err = service.Db.Conn.Exec(`UPDATE area SET name=$2 WHERE id=$1`, id, name)
_, err = db.Exec(context.Background(), `UPDATE area SET name=$2 WHERE id=$1`, id, name)
return
} // }}}
func AreaDelete(id int) (err error) { // {{{
var trx *sql.Tx
trx, err = service.Db.Conn.Begin()
ctx := context.Background()
var trx pgx.Tx
trx, err = db.Begin(ctx)
if err != nil {
err = werr.Wrap(err).WithData(id)
}
_, err = trx.Exec(`
_, err = trx.Exec(context.Background(), `
DELETE
FROM trigger t
USING section s
@ -84,34 +87,34 @@ func AreaDelete(id int) (err error) { // {{{
id,
)
if err != nil {
err2 := trx.Rollback()
err2 := trx.Rollback(ctx)
if err2 != nil {
return werr.Wrap(err2).WithData(err)
}
return werr.Wrap(err).WithData(id)
}
_, err = trx.Exec(`DELETE FROM public.section WHERE area_id = $1`, id)
_, err = trx.Exec(ctx, `DELETE FROM public.section WHERE area_id = $1`, id)
if err != nil {
err2 := trx.Rollback()
err2 := trx.Rollback(ctx)
if err2 != nil {
return werr.Wrap(err2).WithData(err)
}
return werr.Wrap(err).WithData(id)
}
_, err = trx.Exec(`DELETE FROM public.area WHERE id = $1`, id)
_, err = trx.Exec(ctx, `DELETE FROM public.area WHERE id = $1`, id)
if err != nil {
err2 := trx.Rollback()
err2 := trx.Rollback(ctx)
if err2 != nil {
return werr.Wrap(err2).WithData(err)
}
return werr.Wrap(err).WithData(id)
}
err = trx.Commit()
err = trx.Commit(ctx)
if err != nil {
err2 := trx.Rollback()
err2 := trx.Rollback(ctx)
if err2 != nil {
return werr.Wrap(err2).WithData(err)
}