mirror of
https://github.com/MeshCore-Beacon/beacon-server.git
synced 2026-12-06 06:14:23 +00:00
* feat(db): packet type domains, row-cursor indexes and the inverted-heard index * feat(api): exact page cursors for packets, nodes and observers * feat(db): normalise packet list filters before choosing a statement * feat(db): one packets statement per filter shape with an exact row cursor * fix(db): let the window statements decide since after until * feat(db): exact keyset cursors for nodes and observers; observers page by last_seen * feat(api): pageCursor on packets, nodes and observers; it wins over cursor * test(planguard): packets statements and node/observer keysets leave pending; diff and storediff cases * chore: bump version to 2.1.0 * test(db): turn off JIT in the keyset cursor tests
64 lines
1.7 KiB
Go
64 lines
1.7 KiB
Go
// Copyright 2026 Beacon Contributors
|
|
// SPDX-License-Identifier: AGPL-3.0-or-later
|
|
|
|
package planguard
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
"slices"
|
|
|
|
"github.com/jackc/pgx/v5/pgconn"
|
|
)
|
|
|
|
// Session sends SQL over the simple protocol, as psql does, so a statement's parameters are typed by the server.
|
|
type Session interface {
|
|
// Exec runs sql (one or more statements) and reads every result. The first error, from the connection or
|
|
// from any statement, is returned.
|
|
Exec(ctx context.Context, sql string) ([]*pgconn.Result, error)
|
|
}
|
|
|
|
// NewSession returns a Session on pc. The caller owns pc.
|
|
func NewSession(pc *pgconn.PgConn) Session { return pgSession{pc} }
|
|
|
|
type pgSession struct{ pc *pgconn.PgConn }
|
|
|
|
func (s pgSession) Exec(ctx context.Context, sql string) ([]*pgconn.Result, error) {
|
|
// As MultiResultReader.ReadAll, but a result without rows keeps its columns: Read fills FieldDescriptions
|
|
// from the first row only.
|
|
mrr := s.pc.Exec(ctx, sql)
|
|
var res []*pgconn.Result
|
|
for mrr.NextResult() {
|
|
rr := mrr.ResultReader()
|
|
r := rr.Read()
|
|
if r.FieldDescriptions == nil && rr.FieldDescriptions() != nil {
|
|
r.FieldDescriptions = slices.Clone(rr.FieldDescriptions())
|
|
}
|
|
res = append(res, r)
|
|
}
|
|
if err := mrr.Close(); err != nil {
|
|
return res, err
|
|
}
|
|
for _, r := range res {
|
|
if r.Err != nil {
|
|
return res, r.Err
|
|
}
|
|
}
|
|
return res, nil
|
|
}
|
|
|
|
// errText is a one-line description of err; a server error gives its SQLSTATE and message.
|
|
func errText(err error) string {
|
|
var pe *pgconn.PgError
|
|
if errors.As(err, &pe) {
|
|
return fmt.Sprintf("%s: %s", pe.Code, pe.Message)
|
|
}
|
|
return firstLine(err.Error())
|
|
}
|
|
|
|
func isSQLState(err error, code string) bool {
|
|
var pe *pgconn.PgError
|
|
return errors.As(err, &pe) && pe.Code == code
|
|
}
|