Semaphore/db/sql/option.go

59 lines
979 B
Go
Raw Normal View History

2024-03-10 18:55:42 +01:00
package sql
2024-03-10 19:14:08 +01:00
import (
"database/sql"
"errors"
"github.com/Masterminds/squirrel"
"github.com/ansible-semaphore/semaphore/db"
)
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.exec("update option set value=? where key=?", key)
} else {
_, err = d.insert(
"key",
"insert into option (key, value) values (?, ?)",
key, value)
}
return err
}
func (d *SqlDb) getOption(key string) (value string, err error) {
q := squirrel.Select("*").
From(db.OptionProps.TableName).
Where("key=?", key)
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
}