2023-01-06 00:39:21 +01:00
|
|
|
package logs
|
2022-11-24 12:53:27 +01:00
|
|
|
|
|
|
|
import (
|
2023-07-27 17:44:06 +02:00
|
|
|
"blackforestbytes.com/simplecloudnotifier/db"
|
2022-11-24 12:53:27 +01:00
|
|
|
"errors"
|
|
|
|
"gogs.mikescher.com/BlackForestBytes/goext/langext"
|
2022-12-07 23:32:58 +01:00
|
|
|
"gogs.mikescher.com/BlackForestBytes/goext/sq"
|
2022-11-24 12:53:27 +01:00
|
|
|
)
|
|
|
|
|
2023-07-27 17:44:06 +02:00
|
|
|
func (db *Database) ReadSchema(ctx db.TxContext) (retval int, reterr error) {
|
|
|
|
tx, err := ctx.GetOrCreateTransaction(db)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
2022-11-24 12:53:27 +01:00
|
|
|
|
2023-07-27 17:44:06 +02:00
|
|
|
r1, err := tx.Query(ctx, "SELECT name FROM sqlite_master WHERE type = :typ AND name = :name", sq.PP{"typ": "table", "name": "meta"})
|
2022-11-24 12:53:27 +01:00
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
2022-11-30 22:59:33 +01:00
|
|
|
defer func() {
|
|
|
|
err = r1.Close()
|
|
|
|
if err != nil {
|
|
|
|
// overwrite return values
|
|
|
|
retval = 0
|
|
|
|
reterr = err
|
|
|
|
}
|
|
|
|
}()
|
2022-11-24 12:53:27 +01:00
|
|
|
|
|
|
|
if !r1.Next() {
|
|
|
|
return 0, nil
|
|
|
|
}
|
|
|
|
|
2022-12-08 11:31:52 +01:00
|
|
|
err = r1.Close()
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
2023-07-27 17:44:06 +02:00
|
|
|
r2, err := tx.Query(ctx, "SELECT value_int FROM meta WHERE meta_key = :key", sq.PP{"key": "schema"})
|
2022-11-24 12:53:27 +01:00
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
2022-11-30 22:59:33 +01:00
|
|
|
defer func() {
|
|
|
|
err = r2.Close()
|
|
|
|
if err != nil {
|
|
|
|
// overwrite return values
|
|
|
|
retval = 0
|
|
|
|
reterr = err
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2022-11-24 12:53:27 +01:00
|
|
|
if !r2.Next() {
|
|
|
|
return 0, errors.New("no schema entry in meta table")
|
|
|
|
}
|
|
|
|
|
|
|
|
var dbschema int
|
|
|
|
err = r2.Scan(&dbschema)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
2022-12-08 11:31:52 +01:00
|
|
|
err = r2.Close()
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
2022-11-24 12:53:27 +01:00
|
|
|
return dbschema, nil
|
|
|
|
}
|
|
|
|
|
2023-07-27 17:44:06 +02:00
|
|
|
func (db *Database) WriteMetaString(ctx db.TxContext, key string, value string) error {
|
|
|
|
tx, err := ctx.GetOrCreateTransaction(db)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = tx.Exec(ctx, "INSERT INTO meta (meta_key, value_txt) VALUES (:key, :val) ON CONFLICT(meta_key) DO UPDATE SET value_txt = :val", sq.PP{
|
2022-12-07 22:11:44 +01:00
|
|
|
"key": key,
|
|
|
|
"val": value,
|
|
|
|
})
|
2022-11-24 12:53:27 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-07-27 17:44:06 +02:00
|
|
|
func (db *Database) WriteMetaInt(ctx db.TxContext, key string, value int64) error {
|
|
|
|
tx, err := ctx.GetOrCreateTransaction(db)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = tx.Exec(ctx, "INSERT INTO meta (meta_key, value_int) VALUES (:key, :val) ON CONFLICT(meta_key) DO UPDATE SET value_int = :val", sq.PP{
|
2022-12-07 22:11:44 +01:00
|
|
|
"key": key,
|
|
|
|
"val": value,
|
|
|
|
})
|
2022-11-24 12:53:27 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-07-27 17:44:06 +02:00
|
|
|
func (db *Database) WriteMetaReal(ctx db.TxContext, key string, value float64) error {
|
|
|
|
tx, err := ctx.GetOrCreateTransaction(db)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = tx.Exec(ctx, "INSERT INTO meta (meta_key, value_real) VALUES (:key, :val) ON CONFLICT(meta_key) DO UPDATE SET value_real = :val", sq.PP{
|
2022-12-07 22:11:44 +01:00
|
|
|
"key": key,
|
|
|
|
"val": value,
|
|
|
|
})
|
2022-11-24 12:53:27 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-07-27 17:44:06 +02:00
|
|
|
func (db *Database) WriteMetaBlob(ctx db.TxContext, key string, value []byte) error {
|
|
|
|
tx, err := ctx.GetOrCreateTransaction(db)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = tx.Exec(ctx, "INSERT INTO meta (meta_key, value_blob) VALUES (:key, :val) ON CONFLICT(meta_key) DO UPDATE SET value_blob = :val", sq.PP{
|
2022-12-07 22:11:44 +01:00
|
|
|
"key": key,
|
|
|
|
"val": value,
|
|
|
|
})
|
2022-11-24 12:53:27 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-07-27 17:44:06 +02:00
|
|
|
func (db *Database) ReadMetaString(ctx db.TxContext, key string) (retval *string, reterr error) {
|
|
|
|
tx, err := ctx.GetOrCreateTransaction(db)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
r2, err := tx.Query(ctx, "SELECT value_txt FROM meta WHERE meta_key = :key", sq.PP{"key": key})
|
2022-11-24 12:53:27 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-11-30 22:59:33 +01:00
|
|
|
defer func() {
|
|
|
|
err = r2.Close()
|
|
|
|
if err != nil {
|
|
|
|
// overwrite return values
|
|
|
|
retval = nil
|
|
|
|
reterr = err
|
|
|
|
}
|
|
|
|
}()
|
2022-11-24 12:53:27 +01:00
|
|
|
if !r2.Next() {
|
|
|
|
return nil, errors.New("no matching entry in meta table")
|
|
|
|
}
|
|
|
|
|
|
|
|
var value string
|
|
|
|
err = r2.Scan(&value)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-12-08 11:31:52 +01:00
|
|
|
err = r2.Close()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-11-24 12:53:27 +01:00
|
|
|
return langext.Ptr(value), nil
|
|
|
|
}
|
|
|
|
|
2023-07-27 17:44:06 +02:00
|
|
|
func (db *Database) ReadMetaInt(ctx db.TxContext, key string) (retval *int64, reterr error) {
|
|
|
|
tx, err := ctx.GetOrCreateTransaction(db)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
r2, err := tx.Query(ctx, "SELECT value_int FROM meta WHERE meta_key = :key", sq.PP{"key": key})
|
2022-11-24 12:53:27 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-11-30 22:59:33 +01:00
|
|
|
defer func() {
|
|
|
|
err = r2.Close()
|
|
|
|
if err != nil {
|
|
|
|
// overwrite return values
|
|
|
|
retval = nil
|
|
|
|
reterr = err
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2022-11-24 12:53:27 +01:00
|
|
|
if !r2.Next() {
|
|
|
|
return nil, errors.New("no matching entry in meta table")
|
|
|
|
}
|
|
|
|
|
|
|
|
var value int64
|
|
|
|
err = r2.Scan(&value)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-12-08 11:31:52 +01:00
|
|
|
err = r2.Close()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-11-24 12:53:27 +01:00
|
|
|
return langext.Ptr(value), nil
|
|
|
|
}
|
|
|
|
|
2023-07-27 17:44:06 +02:00
|
|
|
func (db *Database) ReadMetaReal(ctx db.TxContext, key string) (retval *float64, reterr error) {
|
|
|
|
tx, err := ctx.GetOrCreateTransaction(db)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
r2, err := tx.Query(ctx, "SELECT value_real FROM meta WHERE meta_key = :key", sq.PP{"key": key})
|
2022-11-24 12:53:27 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-11-30 22:59:33 +01:00
|
|
|
defer func() {
|
|
|
|
err = r2.Close()
|
|
|
|
if err != nil {
|
|
|
|
// overwrite return values
|
|
|
|
retval = nil
|
|
|
|
reterr = err
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2022-11-24 12:53:27 +01:00
|
|
|
if !r2.Next() {
|
|
|
|
return nil, errors.New("no matching entry in meta table")
|
|
|
|
}
|
|
|
|
|
|
|
|
var value float64
|
|
|
|
err = r2.Scan(&value)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-12-08 11:31:52 +01:00
|
|
|
err = r2.Close()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-11-24 12:53:27 +01:00
|
|
|
return langext.Ptr(value), nil
|
|
|
|
}
|
|
|
|
|
2023-07-27 17:44:06 +02:00
|
|
|
func (db *Database) ReadMetaBlob(ctx db.TxContext, key string) (retval *[]byte, reterr error) {
|
|
|
|
tx, err := ctx.GetOrCreateTransaction(db)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
r2, err := tx.Query(ctx, "SELECT value_blob FROM meta WHERE meta_key = :key", sq.PP{"key": key})
|
2022-11-24 12:53:27 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-11-30 22:59:33 +01:00
|
|
|
defer func() {
|
|
|
|
err = r2.Close()
|
|
|
|
if err != nil {
|
|
|
|
// overwrite return values
|
|
|
|
retval = nil
|
|
|
|
reterr = err
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2022-11-24 12:53:27 +01:00
|
|
|
if !r2.Next() {
|
|
|
|
return nil, errors.New("no matching entry in meta table")
|
|
|
|
}
|
|
|
|
|
|
|
|
var value []byte
|
|
|
|
err = r2.Scan(&value)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-12-08 11:31:52 +01:00
|
|
|
err = r2.Close()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-11-24 12:53:27 +01:00
|
|
|
return langext.Ptr(value), nil
|
|
|
|
}
|
|
|
|
|
2023-07-27 17:44:06 +02:00
|
|
|
func (db *Database) DeleteMeta(ctx db.TxContext, key string) error {
|
|
|
|
tx, err := ctx.GetOrCreateTransaction(db)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = tx.Exec(ctx, "DELETE FROM meta WHERE meta_key = :key", sq.PP{"key": key})
|
2022-11-24 12:53:27 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|