super-graph/qcode/config.go

137 lines
2.2 KiB
Go
Raw Permalink Normal View History

2019-10-14 08:51:36 +02:00
package qcode
import (
"regexp"
"sort"
"strings"
)
2019-10-14 08:51:36 +02:00
type Config struct {
Blocklist []string
}
type QueryConfig struct {
Limit int
2019-10-25 07:39:59 +02:00
Filters []string
2019-10-14 08:51:36 +02:00
Columns []string
DisableFunctions bool
}
type InsertConfig struct {
2019-10-25 07:39:59 +02:00
Filters []string
2019-10-14 08:51:36 +02:00
Columns []string
Presets map[string]string
2019-10-14 08:51:36 +02:00
}
type UpdateConfig struct {
2019-10-25 07:39:59 +02:00
Filters []string
2019-10-14 08:51:36 +02:00
Columns []string
Presets map[string]string
2019-10-14 08:51:36 +02:00
}
type DeleteConfig struct {
2019-10-25 07:39:59 +02:00
Filters []string
2019-10-14 08:51:36 +02:00
Columns []string
}
type TRConfig struct {
Query QueryConfig
Insert InsertConfig
Update UpdateConfig
Delete DeleteConfig
}
type trval struct {
query struct {
limit string
fil *Exp
filNU bool
2019-10-14 08:51:36 +02:00
cols map[string]struct{}
disable struct {
funcs bool
}
}
insert struct {
fil *Exp
filNU bool
cols map[string]struct{}
psmap map[string]string
pslist []string
2019-10-14 08:51:36 +02:00
}
update struct {
fil *Exp
filNU bool
cols map[string]struct{}
psmap map[string]string
pslist []string
2019-10-14 08:51:36 +02:00
}
delete struct {
fil *Exp
filNU bool
cols map[string]struct{}
2019-10-14 08:51:36 +02:00
}
}
func (trv *trval) allowedColumns(qt QType) map[string]struct{} {
switch qt {
case QTQuery:
return trv.query.cols
case QTInsert:
return trv.insert.cols
case QTUpdate:
return trv.update.cols
case QTDelete:
2020-01-13 15:34:15 +01:00
return trv.delete.cols
2019-10-14 08:51:36 +02:00
case QTUpsert:
return trv.insert.cols
}
return nil
}
func (trv *trval) filter(qt QType) (*Exp, bool) {
2019-10-14 08:51:36 +02:00
switch qt {
case QTQuery:
return trv.query.fil, trv.query.filNU
2019-10-14 08:51:36 +02:00
case QTInsert:
return trv.insert.fil, trv.insert.filNU
2019-10-14 08:51:36 +02:00
case QTUpdate:
return trv.update.fil, trv.update.filNU
2019-10-14 08:51:36 +02:00
case QTDelete:
return trv.delete.fil, trv.delete.filNU
2019-10-14 08:51:36 +02:00
case QTUpsert:
return trv.insert.fil, trv.insert.filNU
2019-10-14 08:51:36 +02:00
}
return nil, false
2019-10-14 08:51:36 +02:00
}
func listToMap(list []string) map[string]struct{} {
m := make(map[string]struct{}, len(list))
for i := range list {
m[strings.ToLower(list[i])] = struct{}{}
}
return m
}
func mapToList(m map[string]string) []string {
list := []string{}
for k := range m {
list = append(list, strings.ToLower(k))
}
sort.Strings(list)
return list
}
var varRe = regexp.MustCompile(`\$([a-zA-Z0-9_]+)`)
func parsePresets(m map[string]string) map[string]string {
for k, v := range m {
m[k] = varRe.ReplaceAllString(v, `{{$1}}`)
}
return m
}