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

View file

@ -2,10 +2,11 @@ package main
import (
// External
werr "git.gibonuddevalla.se/go/wrappederror"
"github.com/jmoiron/sqlx"
werr "git.ahall.se/go/wrappederror"
"github.com/jackc/pgx/v5"
// Standard
"context"
"database/sql"
"errors"
"strings"
@ -73,7 +74,8 @@ func (dp Datapoint) Update() (err error) { // {{{
}
if dp.ID == 0 {
_, err = service.Db.Conn.Exec(
_, err = db.Exec(
context.Background(),
`INSERT INTO datapoint("group", name, datatype, nodata_problem_seconds, comment) VALUES($1, $2, $3, $4, $5)`,
dp.Group,
name,
@ -85,7 +87,8 @@ func (dp Datapoint) Update() (err error) { // {{{
/* Keep nodata_is_problem as is unless the nodata_problem_seconds is changed.
* Otherwise unnecessary nodata problems could be notified when updating unrelated
* datapoint properties. */
_, err = service.Db.Conn.Exec(
_, err = db.Exec(
context.Background(),
`
UPDATE datapoint
SET
@ -125,7 +128,7 @@ func DatapointAdd[T any](name string, value T) (err error) { // {{{
value any
}
row := service.Db.Conn.QueryRow(`SELECT id, datatype FROM datapoint WHERE name=$1`, name)
row := db.QueryRow(context.Background(), `SELECT id, datatype FROM datapoint WHERE name=$1`, name)
var dpID int
var dpType DatapointType
@ -140,9 +143,9 @@ func DatapointAdd[T any](name string, value T) (err error) { // {{{
switch dpType {
case INT:
_, err = service.Db.Conn.Exec(`INSERT INTO datapoint_value(datapoint_id, value_int) VALUES($1, $2)`, dpID, value)
_, err = db.Exec(context.Background(), `INSERT INTO datapoint_value(datapoint_id, value_int) VALUES($1, $2)`, dpID, value)
case STRING:
_, err = service.Db.Conn.Exec(`INSERT INTO datapoint_value(datapoint_id, value_string) VALUES($1, $2)`, dpID, value)
_, err = db.Exec(context.Background(), `INSERT INTO datapoint_value(datapoint_id, value_string) VALUES($1, $2)`, dpID, value)
case DATETIME:
// Time value is required to be a RFC 3339 formatted time string
var t time.Time
@ -155,7 +158,7 @@ func DatapointAdd[T any](name string, value T) (err error) { // {{{
return werr.Wrap(err).WithData(dpRequest{dpID, value}).Log()
}
_, err = service.Db.Conn.Exec(`INSERT INTO datapoint_value(datapoint_id, value_datetime) VALUES($1, $2)`, dpID, t)
_, err = db.Exec(context.Background(), `INSERT INTO datapoint_value(datapoint_id, value_datetime) VALUES($1, $2)`, dpID, t)
}
if err != nil {
err = werr.Wrap(err).WithData(dpRequest{dpID, value})
@ -167,8 +170,10 @@ func DatapointAdd[T any](name string, value T) (err error) { // {{{
func DatapointsRetrieve() (dps []Datapoint, err error) { // {{{
dps = []Datapoint{}
var rows *sqlx.Rows
rows, err = service.Db.Conn.Queryx(`
var rows pgx.Rows
rows, err = db.Query(
context.Background(),
`
SELECT
id, name, datatype, last_value, "group", comment, nodata_problem_seconds,
last_value_id AS v_id,
@ -205,37 +210,43 @@ func DatapointsRetrieve() (dps []Datapoint, err error) { // {{{
ValueDateTime sql.NullTime `db:"value_datetime"`
}
for rows.Next() {
dp := Datapoint{}
dpv := DatapointValue{}
res := DbRes{}
err = rows.StructScan(&res)
if err != nil {
err = werr.Wrap(err)
dps, err = pgx.CollectRows(
rows,
func(row pgx.CollectableRow) (dp Datapoint, err error) {
var dpv DatapointValue
var res DbRes
res, err = pgx.RowToStructByName[DbRes](row)
if err != nil {
err = werr.Wrap(err)
return
}
dp.ID = res.ID
dp.Name = res.Name
dp.Group = res.Group
dp.Datatype = res.Datatype
dp.Comment = res.Comment
dp.LastValue = res.LastValue
dp.Found = true
dp.NodataProblemSeconds = res.NodataProblemSeconds
if res.VID.Valid {
dpv.ID = int(res.VID.Int64)
dpv.Ts = res.Ts.Time
dpv.ValueInt = res.ValueInt
dpv.ValueString = res.ValueString
dpv.ValueDateTime = res.ValueDateTime
dp.LastDatapointValue = dpv
}
return
}
})
dp.ID = res.ID
dp.Name = res.Name
dp.Group = res.Group
dp.Datatype = res.Datatype
dp.Comment = res.Comment
dp.LastValue = res.LastValue
dp.Found = true
dp.NodataProblemSeconds = res.NodataProblemSeconds
if res.VID.Valid {
dpv.ID = int(res.VID.Int64)
dpv.Ts = res.Ts.Time
dpv.ValueInt = res.ValueInt
dpv.ValueString = res.ValueString
dpv.ValueDateTime = res.ValueDateTime
dp.LastDatapointValue = dpv
}
dps = append(dps, dp)
if err != nil {
err = werr.Wrap(err)
return
}
return
} // }}}
func DatapointRetrieve(id int, name string) (dp Datapoint, err error) { // {{{
@ -250,8 +261,12 @@ func DatapointRetrieve(id int, name string) (dp Datapoint, err error) { // {{{
param = name
}
row := service.Db.Conn.QueryRowx(query, param)
err = row.StructScan(&dp)
rows, _ := db.Query(context.Background(), query, param)
dp, err = pgx.CollectExactlyOneRow(rows, pgx.RowToStructByName[Datapoint])
if err != nil {
err = werr.Wrap(err)
return
}
if err == sql.ErrNoRows {
dp = Datapoint{
@ -266,7 +281,9 @@ func DatapointRetrieve(id int, name string) (dp Datapoint, err error) { // {{{
return
}
row = service.Db.Conn.QueryRowx(`
rows, _ = db.Query(
context.Background(),
`
SELECT *
FROM datapoint_value
WHERE datapoint_id = $1
@ -275,7 +292,7 @@ func DatapointRetrieve(id int, name string) (dp Datapoint, err error) { // {{{
`,
dp.ID,
)
err = row.StructScan(&dp.LastDatapointValue)
dp, err = pgx.CollectExactlyOneRow(rows, pgx.RowToStructByName[Datapoint])
if err == sql.ErrNoRows {
err = nil
return
@ -290,50 +307,41 @@ func DatapointRetrieve(id int, name string) (dp Datapoint, err error) { // {{{
} // }}}
func DatapointDelete(id int) (err error) { // {{{
var dpName string
row := service.Db.Conn.QueryRow(`SELECT name FROM public.datapoint WHERE id = $1`, id)
err = row.Scan(&dpName)
err = db.QueryRow(context.Background(), `SELECT name FROM public.datapoint WHERE id = $1`, id).Scan(&dpName)
if err != nil {
err = werr.Wrap(err).WithData(id)
return
}
var rows *sql.Rows
rows, err = service.Db.Conn.Query(`SELECT name FROM public.trigger WHERE datapoints ? $1`, dpName)
if err != nil {
err = werr.Wrap(err).WithData(dpName)
return
}
defer rows.Close()
var triggerNames []string
var name string
for rows.Next() {
err = rows.Scan(&name)
if err != nil {
err = werr.Wrap(err)
return
}
triggerNames = append(triggerNames, name)
rows, _ := db.Query(context.Background(), `SELECT name FROM public.trigger WHERE datapoints ? $1`, dpName)
triggerNames, err = pgx.CollectRows(rows, pgx.RowTo[string])
if err != nil {
err = werr.Wrap(err).WithData(id)
return
}
if len(triggerNames) > 0 {
return werr.New("Datapoint '%s' used in the following triggers: %s", dpName, strings.Join(triggerNames, ", "))
}
_, err = service.Db.Conn.Exec(`DELETE FROM datapoint WHERE id=$1`, id)
_, err = db.Exec(context.Background(), `DELETE FROM datapoint WHERE id=$1`, id)
if err != nil {
err = werr.Wrap(err).WithData(id)
return
}
return
} // }}}
func DatapointValues(id int, from, to time.Time) (values []DatapointValue, err error) { // {{{
_, err = service.Db.Conn.Exec(`SELECT set_config('timezone', $1, false)`, smonConfig.Timezone().String())
_, err = db.Exec(context.Background(), `SELECT set_config('timezone', $1, false)`, smonConfig.Timezone().String())
if err != nil {
err = werr.Wrap(err).WithData(smonConfig.Timezone().String())
return
}
rows, err := service.Db.Conn.Queryx(
rows, _ := db.Query(
context.Background(),
`
SELECT
id,
@ -354,20 +362,11 @@ func DatapointValues(id int, from, to time.Time) (values []DatapointValue, err e
from,
to,
)
values, err = pgx.CollectRows(rows, pgx.RowToStructByName[DatapointValue])
if err != nil {
err = werr.Wrap(err).WithData(id)
return
}
defer rows.Close()
for rows.Next() {
dpv := DatapointValue{}
err = rows.StructScan(&dpv)
if err != nil {
err = werr.Wrap(err).WithData(id)
return
}
values = append(values, dpv)
}
return
} // }}}