2024-03-10 18:55:42 +01:00
|
|
|
package sql
|
|
|
|
|
2024-03-10 19:14:08 +01:00
|
|
|
import (
|
|
|
|
"database/sql"
|
|
|
|
"errors"
|
2024-07-07 19:12:21 +02:00
|
|
|
"fmt"
|
2024-03-10 19:14:08 +01:00
|
|
|
"github.com/Masterminds/squirrel"
|
|
|
|
"github.com/ansible-semaphore/semaphore/db"
|
2024-07-07 19:12:21 +02:00
|
|
|
"regexp"
|
2024-03-10 19:14:08 +01:00
|
|
|
)
|
|
|
|
|
2024-03-10 18:55:42 +01:00
|
|
|
func (d *SqlDb) SetOption(key string, value string) error {
|
2024-03-10 19:14:08 +01:00
|
|
|
_, err := d.getOption(key)
|
|
|
|
|
|
|
|
if errors.Is(err, db.ErrNotFound) {
|
|
|
|
_, err = d.insert(
|
|
|
|
"key",
|
2024-04-21 22:38:12 +02:00
|
|
|
"insert into `option` (`key`, `value`) values (?, ?)",
|
2024-03-10 19:14:08 +01:00
|
|
|
key, value)
|
2024-04-21 22:38:12 +02:00
|
|
|
} else if err == nil {
|
|
|
|
_, err = d.exec("update `option` set `value`=? where `key`=?", value, key)
|
2024-03-10 19:14:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-07-07 19:12:21 +02:00
|
|
|
func (d *SqlDb) GetOptions(params db.RetrieveQueryParams) (res map[string]string, err error) {
|
2024-07-06 19:33:16 +02:00
|
|
|
var options []db.Option
|
2024-07-07 19:12:21 +02:00
|
|
|
|
|
|
|
if params.Filter != "" {
|
|
|
|
var m bool
|
2024-07-07 20:08:23 +02:00
|
|
|
m, err = regexp.Match(`^(?:\w.)+$`, []byte(params.Filter))
|
2024-07-07 19:12:21 +02:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if !m {
|
|
|
|
err = fmt.Errorf("invalid filter format")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
err = d.getObjects(0, db.OptionProps, params, func(q squirrel.SelectBuilder) squirrel.SelectBuilder {
|
|
|
|
if params.Filter == "" {
|
|
|
|
return q
|
|
|
|
}
|
2024-07-07 20:08:23 +02:00
|
|
|
return q.Where("`key` = ? OR `key` LIKE ?", params.Filter, params.Filter+".%")
|
2024-07-07 19:12:21 +02:00
|
|
|
}, &options)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-07-06 19:33:16 +02:00
|
|
|
for _, opt := range options {
|
|
|
|
res[opt.Key] = opt.Value
|
|
|
|
}
|
2024-07-07 19:12:21 +02:00
|
|
|
|
2024-07-06 19:33:16 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-03-10 19:14:08 +01:00
|
|
|
func (d *SqlDb) getOption(key string) (value string, err error) {
|
|
|
|
q := squirrel.Select("*").
|
2024-06-12 21:53:00 +02:00
|
|
|
From("`"+db.OptionProps.TableName+"`").
|
2024-04-21 22:38:12 +02:00
|
|
|
Where("`key`=?", key)
|
2024-03-10 19:14:08 +01:00
|
|
|
|
|
|
|
query, args, err := q.ToSql()
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var opt db.Option
|
|
|
|
|
|
|
|
err = d.selectOne(&opt, query, args...)
|
|
|
|
|
|
|
|
if errors.Is(err, sql.ErrNoRows) {
|
|
|
|
err = db.ErrNotFound
|
|
|
|
}
|
|
|
|
|
|
|
|
value = opt.Value
|
|
|
|
|
|
|
|
return
|
2024-03-10 18:55:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (d *SqlDb) GetOption(key string) (value string, err error) {
|
2024-03-10 19:14:08 +01:00
|
|
|
|
|
|
|
value, err = d.getOption(key)
|
|
|
|
|
|
|
|
if errors.Is(err, db.ErrNotFound) {
|
|
|
|
err = nil
|
|
|
|
}
|
|
|
|
|
2024-03-10 18:55:42 +01:00
|
|
|
return
|
|
|
|
}
|