quintodrome/persistence/sql_repository.go

124 lines
2.5 KiB
Go
Raw Normal View History

package persistence
2020-01-12 13:32:06 -09:00
import (
"github.com/astaxie/beego/orm"
2020-01-14 18:22:34 -09:00
"github.com/cloudsonic/sonic-server/model"
2020-01-12 13:32:06 -09:00
)
type sqlRepository struct {
2020-01-12 20:04:11 -09:00
tableName string
ormer orm.Ormer
2020-01-12 13:32:06 -09:00
}
func (r *sqlRepository) newQuery(options ...model.QueryOptions) orm.QuerySeter {
q := r.ormer.QueryTable(r.tableName)
2020-01-12 14:36:19 -09:00
if len(options) > 0 {
opts := options[0]
q = q.Offset(opts.Offset)
if opts.Max > 0 {
q = q.Limit(opts.Max)
2020-01-12 14:36:19 -09:00
}
if opts.Sort != "" {
if opts.Order == "desc" {
q = q.OrderBy("-" + opts.Sort)
2020-01-12 14:36:19 -09:00
} else {
q = q.OrderBy(opts.Sort)
2020-01-12 14:36:19 -09:00
}
}
}
return q
2020-01-12 13:32:06 -09:00
}
func (r *sqlRepository) CountAll() (int64, error) {
return r.newQuery().Count()
2020-01-12 13:32:06 -09:00
}
func (r *sqlRepository) Exists(id string) (bool, error) {
c, err := r.newQuery().Filter("id", id).Count()
2020-01-12 13:32:06 -09:00
return c == 1, err
}
2020-01-12 14:55:55 -09:00
// TODO This is used to generate random lists. Can be optimized in SQL: https://stackoverflow.com/a/19419
func (r *sqlRepository) GetAllIds() ([]string, error) {
qs := r.newQuery()
2020-01-12 14:55:55 -09:00
var values []orm.Params
num, err := qs.Values(&values, "id")
if num == 0 {
return nil, err
}
result := collectField(values, func(item interface{}) string {
2020-01-12 14:55:55 -09:00
return item.(orm.Params)["ID"].(string)
})
return result, nil
}
2020-01-14 15:20:47 -09:00
// "Hack" to bypass Postgres driver limitation
func (r *sqlRepository) insert(record interface{}) error {
_, err := r.ormer.Insert(record)
2020-01-14 15:20:47 -09:00
if err != nil && err.Error() != "LastInsertId is not supported by this driver" {
return err
}
return nil
}
func (r *sqlRepository) put(id string, a interface{}) error {
c, err := r.newQuery().Filter("id", id).Count()
if err != nil {
2020-01-12 13:32:06 -09:00
return err
}
if c == 0 {
err = r.insert(a)
2020-01-14 15:20:47 -09:00
if err != nil && err.Error() == "LastInsertId is not supported by this driver" {
err = nil
}
return err
}
_, err = r.ormer.Update(a)
return err
2020-01-12 13:32:06 -09:00
}
2020-01-12 20:04:11 -09:00
func paginateSlice(slice []string, skip int, size int) []string {
if skip > len(slice) {
skip = len(slice)
}
end := skip + size
if end > len(slice) {
end = len(slice)
}
return slice[skip:end]
}
func difference(slice1 []string, slice2 []string) []string {
var diffStr []string
m := map[string]int{}
for _, s1Val := range slice1 {
m[s1Val] = 1
}
for _, s2Val := range slice2 {
m[s2Val] = m[s2Val] + 1
}
for mKey, mVal := range m {
if mVal == 1 {
diffStr = append(diffStr, mKey)
2020-01-12 13:32:06 -09:00
}
2020-01-12 20:04:11 -09:00
}
return diffStr
}
2020-01-16 11:56:24 -09:00
func (r *sqlRepository) Delete(id string) error {
_, err := r.newQuery().Filter("id", id).Delete()
2020-01-16 11:56:24 -09:00
return err
}
2020-01-13 05:06:11 -09:00
func (r *sqlRepository) DeleteAll() error {
_, err := r.newQuery().Filter("id__isnull", false).Delete()
return err
2020-01-13 05:06:11 -09:00
}