navidrome/persistence/helpers.go

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

58 lines
1.4 KiB
Go
Raw Normal View History

package persistence
import (
"encoding/json"
"fmt"
"regexp"
"strings"
2020-03-26 01:33:32 +01:00
"github.com/Masterminds/squirrel"
"github.com/deluan/navidrome/model"
"github.com/deluan/navidrome/utils"
)
func toSqlArgs(rec interface{}) (map[string]interface{}, error) {
// Convert to JSON...
b, err := json.Marshal(rec)
if err != nil {
return nil, err
}
// ... then convert to map
var m map[string]interface{}
err = json.Unmarshal(b, &m)
r := make(map[string]interface{}, len(m))
for f, v := range m {
isAnnotationField := utils.StringInSlice(f, model.AnnotationFields)
isBookmarkField := utils.StringInSlice(f, model.BookmarkFields)
if !isAnnotationField && !isBookmarkField && v != nil {
r[toSnakeCase(f)] = v
}
}
return r, err
}
var matchFirstCap = regexp.MustCompile("(.)([A-Z][a-z]+)")
var matchAllCap = regexp.MustCompile("([a-z0-9])([A-Z])")
func toSnakeCase(str string) string {
snake := matchFirstCap.ReplaceAllString(str, "${1}_${2}")
snake = matchAllCap.ReplaceAllString(snake, "${1}_${2}")
return strings.ToLower(snake)
}
2020-05-01 15:17:21 +02:00
func exists(subTable string, cond squirrel.Sqlizer) existsCond {
return existsCond{subTable: subTable, cond: cond}
2020-03-26 01:33:32 +01:00
}
2020-05-01 15:17:21 +02:00
type existsCond struct {
2020-03-26 01:33:32 +01:00
subTable string
cond squirrel.Sqlizer
}
2020-03-25 23:51:13 +01:00
2020-05-01 15:17:21 +02:00
func (e existsCond) ToSql() (string, []interface{}, error) {
2020-03-26 01:33:32 +01:00
sql, args, err := e.cond.ToSql()
sql = fmt.Sprintf("exists (select 1 from %s where %s)", e.subTable, sql)
return sql, args, err
2020-03-25 23:51:13 +01:00
}