2019-10-14 08:51:36 +02:00
|
|
|
package qcode
|
|
|
|
|
2019-10-30 08:27:11 +01:00
|
|
|
import (
|
|
|
|
"sort"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
2019-10-14 08:51:36 +02:00
|
|
|
type Config struct {
|
2020-05-23 22:37:15 +02:00
|
|
|
Blocklist []string
|
2019-10-14 08:51:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
2020-05-22 08:24:13 +02:00
|
|
|
Block bool
|
2019-10-14 08:51:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
type InsertConfig struct {
|
2019-10-25 07:39:59 +02:00
|
|
|
Filters []string
|
2019-10-14 08:51:36 +02:00
|
|
|
Columns []string
|
2019-10-30 08:27:11 +01:00
|
|
|
Presets map[string]string
|
2020-05-22 08:24:13 +02:00
|
|
|
Block bool
|
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
|
2019-10-30 08:27:11 +01:00
|
|
|
Presets map[string]string
|
2020-05-22 08:24:13 +02:00
|
|
|
Block bool
|
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
|
2020-05-22 08:24:13 +02:00
|
|
|
Block bool
|
2019-10-14 08:51:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
type TRConfig struct {
|
2020-05-23 17:43:57 +02:00
|
|
|
Query QueryConfig
|
|
|
|
Insert InsertConfig
|
|
|
|
Update UpdateConfig
|
|
|
|
Delete DeleteConfig
|
2019-10-14 08:51:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
type trval struct {
|
2020-05-27 01:41:28 +02:00
|
|
|
query struct {
|
2019-10-14 08:51:36 +02:00
|
|
|
limit string
|
|
|
|
fil *Exp
|
2020-01-21 05:38:17 +01:00
|
|
|
filNU bool
|
2019-10-14 08:51:36 +02:00
|
|
|
cols map[string]struct{}
|
2020-05-22 08:24:13 +02:00
|
|
|
disable struct{ funcs bool }
|
|
|
|
block bool
|
2019-10-14 08:51:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
insert struct {
|
2019-10-30 08:27:11 +01:00
|
|
|
fil *Exp
|
2020-01-21 05:38:17 +01:00
|
|
|
filNU bool
|
2019-10-30 08:27:11 +01:00
|
|
|
cols map[string]struct{}
|
|
|
|
psmap map[string]string
|
|
|
|
pslist []string
|
2020-05-22 08:24:13 +02:00
|
|
|
block bool
|
2019-10-14 08:51:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
update struct {
|
2019-10-30 08:27:11 +01:00
|
|
|
fil *Exp
|
2020-01-21 05:38:17 +01:00
|
|
|
filNU bool
|
2019-10-30 08:27:11 +01:00
|
|
|
cols map[string]struct{}
|
|
|
|
psmap map[string]string
|
|
|
|
pslist []string
|
2020-05-22 08:24:13 +02:00
|
|
|
block bool
|
2019-10-14 08:51:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
delete struct {
|
2020-01-21 05:38:17 +01:00
|
|
|
fil *Exp
|
|
|
|
filNU bool
|
|
|
|
cols map[string]struct{}
|
2020-05-22 08:24:13 +02:00
|
|
|
block bool
|
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
|
|
|
|
}
|
|
|
|
|
2020-01-21 05:38:17 +01:00
|
|
|
func (trv *trval) filter(qt QType) (*Exp, bool) {
|
2019-10-14 08:51:36 +02:00
|
|
|
switch qt {
|
|
|
|
case QTQuery:
|
2020-01-21 05:38:17 +01:00
|
|
|
return trv.query.fil, trv.query.filNU
|
2019-10-14 08:51:36 +02:00
|
|
|
case QTInsert:
|
2020-01-21 05:38:17 +01:00
|
|
|
return trv.insert.fil, trv.insert.filNU
|
2019-10-14 08:51:36 +02:00
|
|
|
case QTUpdate:
|
2020-01-21 05:38:17 +01:00
|
|
|
return trv.update.fil, trv.update.filNU
|
2019-10-14 08:51:36 +02:00
|
|
|
case QTDelete:
|
2020-01-21 05:38:17 +01:00
|
|
|
return trv.delete.fil, trv.delete.filNU
|
2019-10-14 08:51:36 +02:00
|
|
|
case QTUpsert:
|
2020-01-21 05:38:17 +01:00
|
|
|
return trv.insert.fil, trv.insert.filNU
|
2019-10-14 08:51:36 +02:00
|
|
|
}
|
|
|
|
|
2020-01-21 05:38:17 +01:00
|
|
|
return nil, false
|
2019-10-14 08:51:36 +02:00
|
|
|
}
|
2019-10-30 08:27:11 +01: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{}
|
2019-11-28 07:25:46 +01:00
|
|
|
for k := range m {
|
2019-10-30 08:27:11 +01:00
|
|
|
list = append(list, strings.ToLower(k))
|
|
|
|
}
|
|
|
|
sort.Strings(list)
|
|
|
|
return list
|
|
|
|
}
|