2019-03-24 14:57:29 +01:00
|
|
|
package qcode
|
|
|
|
|
|
|
|
import (
|
2019-04-21 01:42:08 +02:00
|
|
|
"errors"
|
2019-03-24 14:57:29 +01:00
|
|
|
"fmt"
|
2019-10-14 08:51:36 +02:00
|
|
|
"strconv"
|
2019-03-24 14:57:29 +01:00
|
|
|
"strings"
|
2019-06-15 07:10:53 +02:00
|
|
|
"sync"
|
2019-03-24 14:57:29 +01:00
|
|
|
|
2020-04-10 08:27:43 +02:00
|
|
|
"github.com/dosco/super-graph/core/internal/util"
|
2019-03-25 05:43:14 +01:00
|
|
|
"github.com/gobuffalo/flect"
|
2019-03-24 14:57:29 +01:00
|
|
|
)
|
|
|
|
|
2019-09-05 06:09:56 +02:00
|
|
|
type QType int
|
2020-06-15 09:06:50 +02:00
|
|
|
type SType int
|
2019-09-06 06:34:23 +02:00
|
|
|
type Action int
|
2019-09-05 06:09:56 +02:00
|
|
|
|
2019-05-13 01:27:26 +02:00
|
|
|
const (
|
|
|
|
maxSelectors = 30
|
2019-10-25 06:01:22 +02:00
|
|
|
)
|
2019-09-05 06:09:56 +02:00
|
|
|
|
2019-10-25 06:01:22 +02:00
|
|
|
const (
|
2020-06-15 09:06:50 +02:00
|
|
|
QTUnknown QType = iota
|
|
|
|
QTQuery
|
2019-11-25 08:22:33 +01:00
|
|
|
QTMutation
|
2019-10-14 08:51:36 +02:00
|
|
|
QTInsert
|
|
|
|
QTUpdate
|
|
|
|
QTDelete
|
|
|
|
QTUpsert
|
2019-05-13 01:27:26 +02:00
|
|
|
)
|
|
|
|
|
2020-06-15 09:06:50 +02:00
|
|
|
const (
|
|
|
|
STNone SType = iota
|
|
|
|
STUnion
|
|
|
|
STMember
|
|
|
|
)
|
|
|
|
|
2019-03-24 14:57:29 +01:00
|
|
|
type QCode struct {
|
2019-10-14 08:51:36 +02:00
|
|
|
Type QType
|
|
|
|
ActionVar string
|
|
|
|
Selects []Select
|
2019-11-19 06:47:55 +01:00
|
|
|
Roots []int32
|
|
|
|
rootsA [5]int32
|
2019-03-24 14:57:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
type Select struct {
|
2019-06-08 02:53:08 +02:00
|
|
|
ID int32
|
|
|
|
ParentID int32
|
2020-06-15 09:06:50 +02:00
|
|
|
UParentID int32
|
|
|
|
Type SType
|
2019-04-07 07:12:11 +02:00
|
|
|
Args map[string]*Node
|
2019-11-29 07:38:23 +01:00
|
|
|
Name string
|
2019-03-24 14:57:29 +01:00
|
|
|
FieldName string
|
2019-05-13 01:27:26 +02:00
|
|
|
Cols []Column
|
2019-03-24 14:57:29 +01:00
|
|
|
Where *Exp
|
|
|
|
OrderBy []*OrderBy
|
|
|
|
DistinctOn []string
|
|
|
|
Paging Paging
|
2019-06-08 02:53:08 +02:00
|
|
|
Children []int32
|
2019-10-14 08:51:36 +02:00
|
|
|
Functions bool
|
|
|
|
Allowed map[string]struct{}
|
2019-10-30 08:27:11 +01:00
|
|
|
PresetMap map[string]string
|
|
|
|
PresetList []string
|
2020-01-21 05:38:17 +01:00
|
|
|
SkipRender bool
|
2019-10-14 08:51:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
type Column struct {
|
|
|
|
Table string
|
|
|
|
Name string
|
|
|
|
FieldName string
|
2019-03-24 14:57:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
type Exp struct {
|
2019-11-05 05:44:42 +01:00
|
|
|
Op ExpOp
|
|
|
|
Col string
|
|
|
|
NestedCols []string
|
|
|
|
Type ValType
|
2020-02-19 05:52:44 +01:00
|
|
|
Table string
|
2019-11-05 05:44:42 +01:00
|
|
|
Val string
|
|
|
|
ListType ValType
|
|
|
|
ListVal []string
|
|
|
|
Children []*Exp
|
|
|
|
childrenA [5]*Exp
|
|
|
|
doFree bool
|
2019-06-15 07:10:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
var zeroExp = Exp{doFree: true}
|
|
|
|
|
|
|
|
func (ex *Exp) Reset() {
|
|
|
|
*ex = zeroExp
|
2019-03-24 14:57:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
type OrderBy struct {
|
|
|
|
Col string
|
|
|
|
Order Order
|
|
|
|
}
|
|
|
|
|
2020-02-10 07:45:37 +01:00
|
|
|
type PagingType int
|
|
|
|
|
|
|
|
const (
|
|
|
|
PtOffset PagingType = iota
|
|
|
|
PtForward
|
|
|
|
PtBackward
|
|
|
|
)
|
|
|
|
|
2019-03-24 14:57:29 +01:00
|
|
|
type Paging struct {
|
2020-02-10 07:45:37 +01:00
|
|
|
Type PagingType
|
2019-10-14 08:51:36 +02:00
|
|
|
Limit string
|
|
|
|
Offset string
|
2020-02-19 05:52:44 +01:00
|
|
|
Cursor bool
|
2019-10-14 08:51:36 +02:00
|
|
|
NoLimit bool
|
2019-03-24 14:57:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
type ExpOp int
|
|
|
|
|
|
|
|
const (
|
2019-04-09 03:24:29 +02:00
|
|
|
OpNop ExpOp = iota
|
|
|
|
OpAnd
|
2019-03-24 14:57:29 +01:00
|
|
|
OpOr
|
|
|
|
OpNot
|
|
|
|
OpEquals
|
|
|
|
OpNotEquals
|
|
|
|
OpGreaterOrEquals
|
|
|
|
OpLesserOrEquals
|
|
|
|
OpGreaterThan
|
|
|
|
OpLesserThan
|
|
|
|
OpIn
|
|
|
|
OpNotIn
|
|
|
|
OpLike
|
|
|
|
OpNotLike
|
|
|
|
OpILike
|
|
|
|
OpNotILike
|
|
|
|
OpSimilar
|
|
|
|
OpNotSimilar
|
|
|
|
OpContains
|
|
|
|
OpContainedIn
|
|
|
|
OpHasKey
|
|
|
|
OpHasKeyAny
|
|
|
|
OpHasKeyAll
|
|
|
|
OpIsNull
|
2019-04-04 06:53:24 +02:00
|
|
|
OpEqID
|
2019-04-05 07:44:30 +02:00
|
|
|
OpTsQuery
|
2019-10-26 07:34:29 +02:00
|
|
|
OpFalse
|
2020-02-22 21:58:37 +01:00
|
|
|
OpNotDistinct
|
|
|
|
OpDistinct
|
2019-03-24 14:57:29 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
type ValType int
|
|
|
|
|
|
|
|
const (
|
|
|
|
ValStr ValType = iota + 1
|
|
|
|
ValInt
|
|
|
|
ValFloat
|
|
|
|
ValBool
|
|
|
|
ValList
|
|
|
|
ValVar
|
2019-03-27 07:58:19 +01:00
|
|
|
ValNone
|
2020-02-19 05:52:44 +01:00
|
|
|
ValRef
|
2019-03-24 14:57:29 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
type AggregrateOp int
|
|
|
|
|
|
|
|
const (
|
|
|
|
AgCount AggregrateOp = iota + 1
|
|
|
|
AgSum
|
|
|
|
AgAvg
|
|
|
|
AgMax
|
|
|
|
AgMin
|
|
|
|
)
|
|
|
|
|
|
|
|
type Order int
|
|
|
|
|
|
|
|
const (
|
|
|
|
OrderAsc Order = iota + 1
|
|
|
|
OrderDesc
|
|
|
|
OrderAscNullsFirst
|
|
|
|
OrderAscNullsLast
|
|
|
|
OrderDescNullsFirst
|
|
|
|
OrderDescNullsLast
|
|
|
|
)
|
|
|
|
|
2019-04-08 08:47:59 +02:00
|
|
|
type Compiler struct {
|
2019-10-14 08:51:36 +02:00
|
|
|
tr map[string]map[string]*trval
|
2019-04-08 08:47:59 +02:00
|
|
|
bl map[string]struct{}
|
2020-05-28 06:07:01 +02:00
|
|
|
|
|
|
|
defBlock bool
|
2019-04-08 08:47:59 +02:00
|
|
|
}
|
|
|
|
|
2019-06-15 07:10:53 +02:00
|
|
|
var expPool = sync.Pool{
|
2019-10-03 09:08:01 +02:00
|
|
|
New: func() interface{} { return &Exp{doFree: true} },
|
2019-06-15 07:10:53 +02:00
|
|
|
}
|
|
|
|
|
2019-06-01 08:03:09 +02:00
|
|
|
func NewCompiler(c Config) (*Compiler, error) {
|
2020-05-28 06:07:01 +02:00
|
|
|
co := &Compiler{defBlock: c.DefaultBlock}
|
2019-10-14 08:51:36 +02:00
|
|
|
co.tr = make(map[string]map[string]*trval)
|
2019-10-06 22:28:10 +02:00
|
|
|
co.bl = make(map[string]struct{}, len(c.Blocklist))
|
2019-04-08 08:47:59 +02:00
|
|
|
|
2019-09-08 07:54:38 +02:00
|
|
|
for i := range c.Blocklist {
|
2019-10-14 08:51:36 +02:00
|
|
|
co.bl[strings.ToLower(c.Blocklist[i])] = struct{}{}
|
2019-10-06 22:28:10 +02:00
|
|
|
}
|
|
|
|
|
2019-10-14 08:51:36 +02:00
|
|
|
seedExp := [100]Exp{}
|
2019-10-06 22:28:10 +02:00
|
|
|
|
2019-10-14 08:51:36 +02:00
|
|
|
for i := range seedExp {
|
|
|
|
seedExp[i].doFree = true
|
|
|
|
expPool.Put(&seedExp[i])
|
2019-10-06 22:28:10 +02:00
|
|
|
}
|
|
|
|
|
2019-10-14 08:51:36 +02:00
|
|
|
return co, nil
|
|
|
|
}
|
2019-10-06 22:28:10 +02:00
|
|
|
|
2020-02-22 21:58:37 +01:00
|
|
|
func NewFilter() *Exp {
|
2020-02-10 07:45:37 +01:00
|
|
|
ex := expPool.Get().(*Exp)
|
|
|
|
ex.Reset()
|
|
|
|
|
|
|
|
return ex
|
|
|
|
}
|
|
|
|
|
2019-10-14 08:51:36 +02:00
|
|
|
func (com *Compiler) AddRole(role, table string, trc TRConfig) error {
|
|
|
|
var err error
|
2020-05-23 17:43:57 +02:00
|
|
|
trv := &trval{}
|
2019-03-24 14:57:29 +01:00
|
|
|
|
2019-10-14 08:51:36 +02:00
|
|
|
// query config
|
2020-01-21 05:38:17 +01:00
|
|
|
trv.query.fil, trv.query.filNU, err = compileFilter(trc.Query.Filters)
|
2019-10-06 22:28:10 +02:00
|
|
|
if err != nil {
|
2019-10-14 08:51:36 +02:00
|
|
|
return err
|
2019-10-06 22:28:10 +02:00
|
|
|
}
|
2019-10-14 08:51:36 +02:00
|
|
|
if trc.Query.Limit > 0 {
|
|
|
|
trv.query.limit = strconv.Itoa(trc.Query.Limit)
|
|
|
|
}
|
2019-10-30 08:27:11 +01:00
|
|
|
trv.query.cols = listToMap(trc.Query.Columns)
|
2019-10-14 08:51:36 +02:00
|
|
|
trv.query.disable.funcs = trc.Query.DisableFunctions
|
2020-05-22 08:24:13 +02:00
|
|
|
trv.query.block = trc.Query.Block
|
2019-03-24 14:57:29 +01:00
|
|
|
|
2019-10-14 08:51:36 +02:00
|
|
|
// insert config
|
2020-01-21 05:38:17 +01:00
|
|
|
trv.insert.fil, trv.insert.filNU, err = compileFilter(trc.Insert.Filters)
|
|
|
|
if err != nil {
|
2019-10-14 08:51:36 +02:00
|
|
|
return err
|
2019-10-06 22:28:10 +02:00
|
|
|
}
|
2019-10-30 08:27:11 +01:00
|
|
|
trv.insert.cols = listToMap(trc.Insert.Columns)
|
2020-05-27 01:41:28 +02:00
|
|
|
trv.insert.psmap = trc.Insert.Presets
|
2019-10-30 08:27:11 +01:00
|
|
|
trv.insert.pslist = mapToList(trv.insert.psmap)
|
2020-05-22 08:24:13 +02:00
|
|
|
trv.insert.block = trc.Insert.Block
|
2019-10-06 22:28:10 +02:00
|
|
|
|
2019-10-14 08:51:36 +02:00
|
|
|
// update config
|
2020-01-21 05:38:17 +01:00
|
|
|
trv.update.fil, trv.update.filNU, err = compileFilter(trc.Update.Filters)
|
|
|
|
if err != nil {
|
2019-10-14 08:51:36 +02:00
|
|
|
return err
|
|
|
|
}
|
2019-10-30 08:27:11 +01:00
|
|
|
trv.update.cols = listToMap(trc.Update.Columns)
|
2020-05-27 01:41:28 +02:00
|
|
|
trv.update.psmap = trc.Update.Presets
|
2019-10-30 08:27:11 +01:00
|
|
|
trv.update.pslist = mapToList(trv.update.psmap)
|
2020-05-22 08:24:13 +02:00
|
|
|
trv.update.block = trc.Update.Block
|
2019-10-06 22:28:10 +02:00
|
|
|
|
2019-10-14 08:51:36 +02:00
|
|
|
// delete config
|
2020-01-21 05:38:17 +01:00
|
|
|
trv.delete.fil, trv.delete.filNU, err = compileFilter(trc.Delete.Filters)
|
|
|
|
if err != nil {
|
2019-10-14 08:51:36 +02:00
|
|
|
return err
|
|
|
|
}
|
2019-10-30 08:27:11 +01:00
|
|
|
trv.delete.cols = listToMap(trc.Delete.Columns)
|
2020-05-22 08:24:13 +02:00
|
|
|
trv.delete.block = trc.Delete.Block
|
2019-10-06 22:28:10 +02:00
|
|
|
|
2019-10-14 08:51:36 +02:00
|
|
|
singular := flect.Singularize(table)
|
|
|
|
plural := flect.Pluralize(table)
|
2019-06-14 06:32:15 +02:00
|
|
|
|
2019-10-14 08:51:36 +02:00
|
|
|
if _, ok := com.tr[role]; !ok {
|
|
|
|
com.tr[role] = make(map[string]*trval)
|
2019-04-08 08:47:59 +02:00
|
|
|
}
|
2019-03-24 14:57:29 +01:00
|
|
|
|
2019-10-14 08:51:36 +02:00
|
|
|
com.tr[role][singular] = trv
|
|
|
|
com.tr[role][plural] = trv
|
|
|
|
return nil
|
2019-03-24 14:57:29 +01:00
|
|
|
}
|
|
|
|
|
2019-10-14 08:51:36 +02:00
|
|
|
func (com *Compiler) Compile(query []byte, role string) (*QCode, error) {
|
2019-03-24 14:57:29 +01:00
|
|
|
var err error
|
|
|
|
|
2019-10-14 08:51:36 +02:00
|
|
|
qc := QCode{Type: QTQuery}
|
2019-11-19 06:47:55 +01:00
|
|
|
qc.Roots = qc.rootsA[:0]
|
2019-10-14 08:51:36 +02:00
|
|
|
|
2019-06-15 04:17:21 +02:00
|
|
|
op, err := Parse(query)
|
2019-03-24 14:57:29 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-10-14 08:51:36 +02:00
|
|
|
if err = com.compileQuery(&qc, op, role); err != nil {
|
2019-03-24 14:57:29 +01:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-06-09 01:28:22 +02:00
|
|
|
freeNodes(op)
|
2019-06-15 04:17:21 +02:00
|
|
|
opPool.Put(op)
|
|
|
|
|
2019-09-05 06:09:56 +02:00
|
|
|
return &qc, nil
|
2019-06-15 04:17:21 +02:00
|
|
|
}
|
|
|
|
|
2019-10-14 08:51:36 +02:00
|
|
|
func (com *Compiler) compileQuery(qc *QCode, op *Operation, role string) error {
|
2019-06-08 02:53:08 +02:00
|
|
|
id := int32(0)
|
2019-03-24 14:57:29 +01:00
|
|
|
|
2019-10-14 08:51:36 +02:00
|
|
|
if len(op.Fields) == 0 {
|
|
|
|
return errors.New("invalid graphql no query found")
|
|
|
|
}
|
|
|
|
|
|
|
|
if op.Type == opMutate {
|
|
|
|
if err := com.setMutationType(qc, op.Fields[0].Args); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-13 01:27:26 +02:00
|
|
|
selects := make([]Select, 0, 5)
|
2019-06-16 21:28:50 +02:00
|
|
|
st := NewStack()
|
2019-10-14 08:51:36 +02:00
|
|
|
action := qc.Type
|
2019-03-24 14:57:29 +01:00
|
|
|
|
2019-05-13 01:27:26 +02:00
|
|
|
if len(op.Fields) == 0 {
|
2019-10-14 08:51:36 +02:00
|
|
|
return errors.New("empty query")
|
2019-03-24 14:57:29 +01:00
|
|
|
}
|
2019-11-19 06:47:55 +01:00
|
|
|
|
|
|
|
for i := range op.Fields {
|
|
|
|
if op.Fields[i].ParentID == -1 {
|
2019-12-30 07:17:37 +01:00
|
|
|
val := op.Fields[i].ID | (-1 << 16)
|
|
|
|
st.Push(val)
|
2019-11-19 06:47:55 +01:00
|
|
|
}
|
|
|
|
}
|
2019-03-24 14:57:29 +01:00
|
|
|
|
|
|
|
for {
|
|
|
|
if st.Len() == 0 {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
2019-05-13 01:27:26 +02:00
|
|
|
if id >= maxSelectors {
|
2019-10-14 08:51:36 +02:00
|
|
|
return fmt.Errorf("selector limit reached (%d)", maxSelectors)
|
2019-05-13 01:27:26 +02:00
|
|
|
}
|
|
|
|
|
2019-12-30 07:17:37 +01:00
|
|
|
val := st.Pop()
|
|
|
|
fid := val & 0xFFFF
|
|
|
|
parentID := (val >> 16) & 0xFFFF
|
|
|
|
|
2019-05-13 01:27:26 +02:00
|
|
|
field := &op.Fields[fid]
|
2019-03-24 14:57:29 +01:00
|
|
|
|
2019-06-15 04:17:21 +02:00
|
|
|
if _, ok := com.bl[field.Name]; ok {
|
2019-03-24 14:57:29 +01:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2019-12-09 07:48:18 +01:00
|
|
|
if field.ParentID == -1 {
|
|
|
|
parentID = -1
|
|
|
|
}
|
|
|
|
|
2019-10-15 08:30:19 +02:00
|
|
|
trv := com.getRole(role, field.Name)
|
2020-05-24 08:24:24 +02:00
|
|
|
skipRender := false
|
|
|
|
|
|
|
|
if trv != nil {
|
|
|
|
switch action {
|
|
|
|
case QTQuery:
|
|
|
|
if trv.query.block {
|
|
|
|
skipRender = true
|
|
|
|
}
|
|
|
|
|
|
|
|
case QTInsert:
|
|
|
|
if trv.insert.block {
|
2020-05-24 23:43:54 +02:00
|
|
|
return fmt.Errorf("%s, insert blocked: %s", role, field.Name)
|
2020-05-24 08:24:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
case QTUpdate:
|
|
|
|
if trv.update.block {
|
2020-05-24 23:43:54 +02:00
|
|
|
return fmt.Errorf("%s, update blocked: %s", role, field.Name)
|
2020-05-24 08:24:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
case QTDelete:
|
|
|
|
if trv.delete.block {
|
2020-05-24 23:43:54 +02:00
|
|
|
return fmt.Errorf("%s, delete blocked: %s", role, field.Name)
|
2020-05-24 08:24:24 +02:00
|
|
|
}
|
2020-05-22 08:24:13 +02:00
|
|
|
}
|
|
|
|
|
2020-05-24 08:24:24 +02:00
|
|
|
} else if role == "anon" {
|
2020-05-28 06:07:01 +02:00
|
|
|
skipRender = com.defBlock
|
2020-05-22 08:24:13 +02:00
|
|
|
}
|
|
|
|
|
2019-06-14 06:32:15 +02:00
|
|
|
selects = append(selects, Select{
|
2020-05-24 08:24:24 +02:00
|
|
|
ID: id,
|
|
|
|
ParentID: parentID,
|
|
|
|
Name: field.Name,
|
|
|
|
SkipRender: skipRender,
|
2019-06-14 06:32:15 +02:00
|
|
|
})
|
|
|
|
s := &selects[(len(selects) - 1)]
|
2019-05-13 01:27:26 +02:00
|
|
|
|
2020-06-15 09:06:50 +02:00
|
|
|
if field.Union {
|
|
|
|
s.Type = STUnion
|
|
|
|
}
|
|
|
|
|
2019-03-24 14:57:29 +01:00
|
|
|
if len(field.Alias) != 0 {
|
|
|
|
s.FieldName = field.Alias
|
|
|
|
} else {
|
2019-11-29 07:38:23 +01:00
|
|
|
s.FieldName = s.Name
|
2019-03-24 14:57:29 +01:00
|
|
|
}
|
|
|
|
|
2020-05-24 08:24:24 +02:00
|
|
|
if s.ParentID == -1 {
|
|
|
|
qc.Roots = append(qc.Roots, s.ID)
|
|
|
|
} else {
|
|
|
|
p := &selects[s.ParentID]
|
|
|
|
p.Children = append(p.Children, s.ID)
|
2020-06-15 09:06:50 +02:00
|
|
|
|
|
|
|
if p.Type == STUnion {
|
|
|
|
s.Type = STMember
|
|
|
|
s.UParentID = p.ParentID
|
|
|
|
}
|
2020-05-24 08:24:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if skipRender {
|
|
|
|
id++
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
s.Children = make([]int32, 0, 5)
|
|
|
|
s.Functions = true
|
|
|
|
|
|
|
|
if trv != nil {
|
|
|
|
s.Allowed = trv.allowedColumns(action)
|
|
|
|
|
|
|
|
switch action {
|
|
|
|
case QTQuery:
|
|
|
|
s.Functions = !trv.query.disable.funcs
|
|
|
|
s.Paging.Limit = trv.query.limit
|
|
|
|
|
|
|
|
case QTInsert:
|
|
|
|
s.PresetMap = trv.insert.psmap
|
|
|
|
s.PresetList = trv.insert.pslist
|
|
|
|
|
|
|
|
case QTUpdate:
|
|
|
|
s.PresetMap = trv.update.psmap
|
|
|
|
s.PresetList = trv.update.pslist
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-21 05:38:17 +01:00
|
|
|
err := com.compileArgs(qc, s, field.Args, role)
|
2019-03-24 14:57:29 +01:00
|
|
|
if err != nil {
|
2019-10-14 08:51:36 +02:00
|
|
|
return err
|
2019-03-24 14:57:29 +01:00
|
|
|
}
|
|
|
|
|
2020-02-22 21:58:37 +01:00
|
|
|
// Order is important AddFilters must come after compileArgs
|
|
|
|
com.AddFilters(qc, s, role)
|
2019-11-29 06:09:50 +01:00
|
|
|
|
2019-05-13 01:27:26 +02:00
|
|
|
s.Cols = make([]Column, 0, len(field.Children))
|
2020-06-07 19:02:57 +02:00
|
|
|
cm := make(map[string]struct{})
|
2019-10-14 08:51:36 +02:00
|
|
|
action = QTQuery
|
2019-05-13 01:27:26 +02:00
|
|
|
|
|
|
|
for _, cid := range field.Children {
|
|
|
|
f := op.Fields[cid]
|
2019-03-24 14:57:29 +01:00
|
|
|
|
2019-06-15 04:17:21 +02:00
|
|
|
if _, ok := com.bl[f.Name]; ok {
|
2019-03-24 14:57:29 +01:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2020-06-07 19:02:57 +02:00
|
|
|
var fname string
|
|
|
|
|
|
|
|
if f.Alias != "" {
|
|
|
|
fname = f.Alias
|
|
|
|
} else {
|
|
|
|
fname = f.Name
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, ok := cm[fname]; ok {
|
|
|
|
continue
|
|
|
|
} else {
|
|
|
|
cm[fname] = struct{}{}
|
|
|
|
}
|
|
|
|
|
2019-05-13 01:27:26 +02:00
|
|
|
if len(f.Children) != 0 {
|
2019-12-30 07:17:37 +01:00
|
|
|
val := f.ID | (s.ID << 16)
|
|
|
|
st.Push(val)
|
2019-05-13 01:27:26 +02:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2020-06-07 19:02:57 +02:00
|
|
|
col := Column{Name: f.Name, FieldName: fname}
|
2019-05-13 01:27:26 +02:00
|
|
|
s.Cols = append(s.Cols, col)
|
2019-03-24 14:57:29 +01:00
|
|
|
}
|
|
|
|
|
2019-05-13 01:27:26 +02:00
|
|
|
id++
|
2019-03-24 14:57:29 +01:00
|
|
|
}
|
|
|
|
|
2019-10-06 22:28:10 +02:00
|
|
|
if id == 0 {
|
2019-10-14 08:51:36 +02:00
|
|
|
return errors.New("invalid query")
|
2019-10-06 22:28:10 +02:00
|
|
|
}
|
2019-04-20 23:27:03 +02:00
|
|
|
|
2019-11-19 06:47:55 +01:00
|
|
|
qc.Selects = selects[:id]
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-02-22 21:58:37 +01:00
|
|
|
func (com *Compiler) AddFilters(qc *QCode, sel *Select, role string) {
|
2019-10-06 22:28:10 +02:00
|
|
|
var fil *Exp
|
2020-05-24 08:24:24 +02:00
|
|
|
var nu bool // need user_id (or not) in this filter
|
2019-10-06 22:28:10 +02:00
|
|
|
|
2019-12-02 16:52:22 +01:00
|
|
|
if trv, ok := com.tr[role][sel.Name]; ok {
|
2020-01-21 05:38:17 +01:00
|
|
|
fil, nu = trv.filter(qc.Type)
|
2019-10-06 22:28:10 +02:00
|
|
|
}
|
2019-03-24 14:57:29 +01:00
|
|
|
|
2019-11-19 06:47:55 +01:00
|
|
|
if fil == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-01-21 05:38:17 +01:00
|
|
|
if nu && role == "anon" {
|
|
|
|
sel.SkipRender = true
|
|
|
|
}
|
|
|
|
|
2019-11-19 06:47:55 +01:00
|
|
|
switch fil.Op {
|
|
|
|
case OpNop:
|
|
|
|
case OpFalse:
|
2019-12-02 16:52:22 +01:00
|
|
|
sel.Where = fil
|
2019-11-19 06:47:55 +01:00
|
|
|
default:
|
2020-02-22 21:58:37 +01:00
|
|
|
AddFilter(sel, fil)
|
2019-04-21 01:42:08 +02:00
|
|
|
}
|
2019-03-24 14:57:29 +01:00
|
|
|
}
|
|
|
|
|
2020-01-21 05:38:17 +01:00
|
|
|
func (com *Compiler) compileArgs(qc *QCode, sel *Select, args []Arg, role string) error {
|
2019-03-24 14:57:29 +01:00
|
|
|
var err error
|
2020-02-10 07:45:37 +01:00
|
|
|
|
2019-03-24 14:57:29 +01:00
|
|
|
for i := range args {
|
2019-06-01 08:03:09 +02:00
|
|
|
arg := &args[i]
|
|
|
|
|
2019-06-15 04:17:21 +02:00
|
|
|
switch arg.Name {
|
2019-04-04 06:53:24 +02:00
|
|
|
case "id":
|
2020-06-09 01:28:22 +02:00
|
|
|
err = com.compileArgID(sel, arg)
|
2019-12-02 16:52:22 +01:00
|
|
|
|
2019-04-05 07:44:30 +02:00
|
|
|
case "search":
|
2020-06-09 01:28:22 +02:00
|
|
|
err = com.compileArgSearch(sel, arg)
|
2019-12-02 16:52:22 +01:00
|
|
|
|
2019-03-24 14:57:29 +01:00
|
|
|
case "where":
|
2020-06-09 01:28:22 +02:00
|
|
|
err = com.compileArgWhere(sel, arg, role)
|
2019-12-02 16:52:22 +01:00
|
|
|
|
2019-03-24 14:57:29 +01:00
|
|
|
case "orderby", "order_by", "order":
|
2020-06-09 01:28:22 +02:00
|
|
|
err = com.compileArgOrderBy(sel, arg)
|
2019-12-02 16:52:22 +01:00
|
|
|
|
2019-03-24 14:57:29 +01:00
|
|
|
case "distinct_on", "distinct":
|
2020-06-09 01:28:22 +02:00
|
|
|
err = com.compileArgDistinctOn(sel, arg)
|
2019-12-02 16:52:22 +01:00
|
|
|
|
2019-03-24 14:57:29 +01:00
|
|
|
case "limit":
|
2020-06-09 01:28:22 +02:00
|
|
|
err = com.compileArgLimit(sel, arg)
|
2019-12-02 16:52:22 +01:00
|
|
|
|
2019-03-24 14:57:29 +01:00
|
|
|
case "offset":
|
2020-06-09 01:28:22 +02:00
|
|
|
err = com.compileArgOffset(sel, arg)
|
2020-02-10 07:45:37 +01:00
|
|
|
|
|
|
|
case "first":
|
2020-06-09 01:28:22 +02:00
|
|
|
err = com.compileArgFirstLast(sel, arg, PtForward)
|
2020-02-10 07:45:37 +01:00
|
|
|
|
|
|
|
case "last":
|
2020-06-09 01:28:22 +02:00
|
|
|
err = com.compileArgFirstLast(sel, arg, PtBackward)
|
2020-02-10 07:45:37 +01:00
|
|
|
|
|
|
|
case "after":
|
2020-06-09 01:28:22 +02:00
|
|
|
err = com.compileArgAfterBefore(sel, arg, PtForward)
|
2020-02-10 07:45:37 +01:00
|
|
|
|
|
|
|
case "before":
|
2020-06-09 01:28:22 +02:00
|
|
|
err = com.compileArgAfterBefore(sel, arg, PtBackward)
|
2019-04-04 06:53:24 +02:00
|
|
|
}
|
2019-04-06 08:35:08 +02:00
|
|
|
|
2019-12-02 16:52:22 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2019-06-01 08:03:09 +02:00
|
|
|
}
|
2019-03-24 14:57:29 +01:00
|
|
|
}
|
|
|
|
|
2019-04-04 06:53:24 +02:00
|
|
|
return nil
|
2019-03-24 14:57:29 +01:00
|
|
|
}
|
|
|
|
|
2019-10-14 08:51:36 +02:00
|
|
|
func (com *Compiler) setMutationType(qc *QCode, args []Arg) error {
|
|
|
|
setActionVar := func(arg *Arg) error {
|
2019-12-02 16:52:22 +01:00
|
|
|
if arg.Val.Type != NodeVar {
|
2020-02-19 05:52:44 +01:00
|
|
|
return argErr(arg.Name, "variable")
|
2019-10-14 08:51:36 +02:00
|
|
|
}
|
|
|
|
qc.ActionVar = arg.Val.Val
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := range args {
|
|
|
|
arg := &args[i]
|
|
|
|
|
|
|
|
switch arg.Name {
|
|
|
|
case "insert":
|
|
|
|
qc.Type = QTInsert
|
|
|
|
return setActionVar(arg)
|
|
|
|
case "update":
|
|
|
|
qc.Type = QTUpdate
|
|
|
|
return setActionVar(arg)
|
|
|
|
case "upsert":
|
|
|
|
qc.Type = QTUpsert
|
|
|
|
return setActionVar(arg)
|
|
|
|
case "delete":
|
|
|
|
qc.Type = QTDelete
|
|
|
|
|
2019-12-02 16:52:22 +01:00
|
|
|
if arg.Val.Type != NodeBool {
|
2020-02-19 05:52:44 +01:00
|
|
|
return argErr(arg.Name, "boolen")
|
2019-10-14 08:51:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if arg.Val.Val == "false" {
|
|
|
|
qc.Type = QTQuery
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-01-21 05:38:17 +01:00
|
|
|
func (com *Compiler) compileArgObj(st *util.Stack, arg *Arg) (*Exp, bool, error) {
|
2019-12-02 16:52:22 +01:00
|
|
|
if arg.Val.Type != NodeObj {
|
2020-01-21 05:38:17 +01:00
|
|
|
return nil, false, fmt.Errorf("expecting an object")
|
2019-03-24 14:57:29 +01:00
|
|
|
}
|
|
|
|
|
2019-06-17 02:51:36 +02:00
|
|
|
return com.compileArgNode(st, arg.Val, true)
|
2019-03-24 14:57:29 +01:00
|
|
|
}
|
|
|
|
|
2020-01-21 05:38:17 +01:00
|
|
|
func (com *Compiler) compileArgNode(st *util.Stack, node *Node, usePool bool) (*Exp, bool, error) {
|
2019-03-24 14:57:29 +01:00
|
|
|
var root *Exp
|
2020-01-21 05:38:17 +01:00
|
|
|
var needsUser bool
|
2019-03-24 14:57:29 +01:00
|
|
|
|
2019-06-01 08:03:09 +02:00
|
|
|
if node == nil || len(node.Children) == 0 {
|
2020-02-10 07:45:37 +01:00
|
|
|
return nil, false, errors.New("invalid argument value")
|
2019-04-21 07:08:51 +02:00
|
|
|
}
|
|
|
|
|
2019-06-16 21:28:50 +02:00
|
|
|
pushChild(st, nil, node)
|
2019-03-24 14:57:29 +01:00
|
|
|
|
|
|
|
for {
|
|
|
|
if st.Len() == 0 {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
intf := st.Pop()
|
2020-02-10 07:45:37 +01:00
|
|
|
|
2019-06-16 21:28:50 +02:00
|
|
|
node, ok := intf.(*Node)
|
|
|
|
if !ok || node == nil {
|
2020-01-21 05:38:17 +01:00
|
|
|
return nil, needsUser, fmt.Errorf("16: unexpected value %v (%t)", intf, intf)
|
2019-03-24 14:57:29 +01:00
|
|
|
}
|
|
|
|
|
2019-06-16 21:41:28 +02:00
|
|
|
// Objects inside a list
|
2019-06-16 21:28:50 +02:00
|
|
|
if len(node.Name) == 0 {
|
|
|
|
pushChildren(st, node.exp, node)
|
|
|
|
continue
|
|
|
|
|
|
|
|
} else {
|
|
|
|
if _, ok := com.bl[node.Name]; ok {
|
2019-04-07 07:12:11 +02:00
|
|
|
continue
|
|
|
|
}
|
2019-03-24 14:57:29 +01:00
|
|
|
}
|
|
|
|
|
2019-06-16 21:28:50 +02:00
|
|
|
ex, err := newExp(st, node, usePool)
|
2019-03-27 07:58:19 +01:00
|
|
|
if err != nil {
|
2020-01-21 05:38:17 +01:00
|
|
|
return nil, needsUser, err
|
2019-03-24 14:57:29 +01:00
|
|
|
}
|
|
|
|
|
2019-03-27 07:58:19 +01:00
|
|
|
if ex == nil {
|
|
|
|
continue
|
2019-03-24 14:57:29 +01:00
|
|
|
}
|
|
|
|
|
2020-01-21 05:38:17 +01:00
|
|
|
if ex.Type == ValVar && ex.Val == "user_id" {
|
|
|
|
needsUser = true
|
|
|
|
}
|
|
|
|
|
2019-06-16 21:28:50 +02:00
|
|
|
if node.exp == nil {
|
2019-03-24 14:57:29 +01:00
|
|
|
root = ex
|
|
|
|
} else {
|
2019-06-16 21:28:50 +02:00
|
|
|
node.exp.Children = append(node.exp.Children, ex)
|
2019-03-24 14:57:29 +01:00
|
|
|
}
|
2019-06-01 08:03:09 +02:00
|
|
|
}
|
|
|
|
|
2020-01-21 05:38:17 +01:00
|
|
|
return root, needsUser, nil
|
2019-03-24 14:57:29 +01:00
|
|
|
}
|
|
|
|
|
2020-06-09 01:28:22 +02:00
|
|
|
func (com *Compiler) compileArgID(sel *Select, arg *Arg) error {
|
2019-10-14 08:51:36 +02:00
|
|
|
if sel.ID != 0 {
|
2020-06-09 01:28:22 +02:00
|
|
|
return nil
|
2019-10-14 08:51:36 +02:00
|
|
|
}
|
|
|
|
|
2019-04-04 06:53:24 +02:00
|
|
|
if sel.Where != nil && sel.Where.Op == OpEqID {
|
2020-06-09 01:28:22 +02:00
|
|
|
return nil
|
2019-04-04 06:53:24 +02:00
|
|
|
}
|
|
|
|
|
2020-02-19 05:52:44 +01:00
|
|
|
if arg.Val.Type != NodeVar {
|
2020-06-09 01:28:22 +02:00
|
|
|
return argErr("id", "variable")
|
2020-02-19 05:52:44 +01:00
|
|
|
}
|
|
|
|
|
2019-06-15 07:10:53 +02:00
|
|
|
ex := expPool.Get().(*Exp)
|
|
|
|
ex.Reset()
|
|
|
|
|
|
|
|
ex.Op = OpEqID
|
2020-02-19 05:52:44 +01:00
|
|
|
ex.Type = ValVar
|
2019-06-15 07:10:53 +02:00
|
|
|
ex.Val = arg.Val.Val
|
2019-04-04 06:53:24 +02:00
|
|
|
|
|
|
|
sel.Where = ex
|
2020-06-09 01:28:22 +02:00
|
|
|
return nil
|
2019-04-04 06:53:24 +02:00
|
|
|
}
|
|
|
|
|
2020-06-09 01:28:22 +02:00
|
|
|
func (com *Compiler) compileArgSearch(sel *Select, arg *Arg) error {
|
2020-02-19 05:52:44 +01:00
|
|
|
if arg.Val.Type != NodeVar {
|
2020-06-09 01:28:22 +02:00
|
|
|
return argErr("search", "variable")
|
2020-02-19 05:52:44 +01:00
|
|
|
}
|
|
|
|
|
2019-06-15 07:10:53 +02:00
|
|
|
ex := expPool.Get().(*Exp)
|
|
|
|
ex.Reset()
|
|
|
|
|
|
|
|
ex.Op = OpTsQuery
|
2020-02-19 05:52:44 +01:00
|
|
|
ex.Type = ValVar
|
2019-06-15 07:10:53 +02:00
|
|
|
ex.Val = arg.Val.Val
|
2019-04-05 07:44:30 +02:00
|
|
|
|
2019-12-02 16:52:22 +01:00
|
|
|
if sel.Args == nil {
|
|
|
|
sel.Args = make(map[string]*Node)
|
|
|
|
}
|
|
|
|
|
|
|
|
sel.Args[arg.Name] = arg.Val
|
2020-06-09 01:28:22 +02:00
|
|
|
arg.df = true
|
2020-02-22 21:58:37 +01:00
|
|
|
AddFilter(sel, ex)
|
2020-02-19 05:52:44 +01:00
|
|
|
|
2020-06-09 01:28:22 +02:00
|
|
|
return nil
|
2019-04-05 07:44:30 +02:00
|
|
|
}
|
|
|
|
|
2020-06-09 01:28:22 +02:00
|
|
|
func (com *Compiler) compileArgWhere(sel *Select, arg *Arg, role string) error {
|
2019-06-17 02:51:36 +02:00
|
|
|
st := util.NewStack()
|
2019-03-24 14:57:29 +01:00
|
|
|
var err error
|
|
|
|
|
2020-01-21 05:38:17 +01:00
|
|
|
ex, nu, err := com.compileArgObj(st, arg)
|
2019-03-24 14:57:29 +01:00
|
|
|
if err != nil {
|
2020-06-09 01:28:22 +02:00
|
|
|
return err
|
2019-03-24 14:57:29 +01:00
|
|
|
}
|
|
|
|
|
2020-01-21 05:38:17 +01:00
|
|
|
if nu && role == "anon" {
|
|
|
|
sel.SkipRender = true
|
|
|
|
}
|
2020-02-22 21:58:37 +01:00
|
|
|
AddFilter(sel, ex)
|
2020-01-21 05:38:17 +01:00
|
|
|
|
2020-06-09 01:28:22 +02:00
|
|
|
return nil
|
2019-03-24 14:57:29 +01:00
|
|
|
}
|
|
|
|
|
2020-06-09 01:28:22 +02:00
|
|
|
func (com *Compiler) compileArgOrderBy(sel *Select, arg *Arg) error {
|
2019-12-02 16:52:22 +01:00
|
|
|
if arg.Val.Type != NodeObj {
|
2020-06-09 01:28:22 +02:00
|
|
|
return fmt.Errorf("expecting an object")
|
2019-03-24 14:57:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
st := util.NewStack()
|
|
|
|
|
|
|
|
for i := range arg.Val.Children {
|
|
|
|
st.Push(arg.Val.Children[i])
|
|
|
|
}
|
|
|
|
|
|
|
|
for {
|
|
|
|
if st.Len() == 0 {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
intf := st.Pop()
|
|
|
|
node, ok := intf.(*Node)
|
|
|
|
|
|
|
|
if !ok || node == nil {
|
2020-06-09 01:28:22 +02:00
|
|
|
return fmt.Errorf("17: unexpected value %v (%t)", intf, intf)
|
2019-03-24 14:57:29 +01:00
|
|
|
}
|
|
|
|
|
2019-06-15 04:17:21 +02:00
|
|
|
if _, ok := com.bl[node.Name]; ok {
|
2019-03-24 14:57:29 +01:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2020-02-19 05:52:44 +01:00
|
|
|
if node.Type != NodeStr && node.Type != NodeVar {
|
2020-06-09 01:28:22 +02:00
|
|
|
return fmt.Errorf("expecting a string or variable")
|
2019-03-24 14:57:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
ob := &OrderBy{}
|
|
|
|
|
2019-06-15 04:17:21 +02:00
|
|
|
switch node.Val {
|
2019-03-24 14:57:29 +01:00
|
|
|
case "asc":
|
|
|
|
ob.Order = OrderAsc
|
|
|
|
case "desc":
|
|
|
|
ob.Order = OrderDesc
|
|
|
|
case "asc_nulls_first":
|
|
|
|
ob.Order = OrderAscNullsFirst
|
|
|
|
case "desc_nulls_first":
|
|
|
|
ob.Order = OrderDescNullsFirst
|
|
|
|
case "asc_nulls_last":
|
|
|
|
ob.Order = OrderAscNullsLast
|
|
|
|
case "desc_nulls_last":
|
|
|
|
ob.Order = OrderDescNullsLast
|
|
|
|
default:
|
2020-06-09 01:28:22 +02:00
|
|
|
return fmt.Errorf("valid values include asc, desc, asc_nulls_first and desc_nulls_first")
|
2019-03-24 14:57:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
setOrderByColName(ob, node)
|
|
|
|
sel.OrderBy = append(sel.OrderBy, ob)
|
|
|
|
}
|
2020-06-09 01:28:22 +02:00
|
|
|
return nil
|
2019-03-24 14:57:29 +01:00
|
|
|
}
|
|
|
|
|
2020-06-09 01:28:22 +02:00
|
|
|
func (com *Compiler) compileArgDistinctOn(sel *Select, arg *Arg) error {
|
2019-03-24 14:57:29 +01:00
|
|
|
node := arg.Val
|
|
|
|
|
2019-06-15 04:17:21 +02:00
|
|
|
if _, ok := com.bl[node.Name]; ok {
|
2020-06-09 01:28:22 +02:00
|
|
|
return nil
|
2019-03-24 14:57:29 +01:00
|
|
|
}
|
|
|
|
|
2019-12-02 16:52:22 +01:00
|
|
|
if node.Type != NodeList && node.Type != NodeStr {
|
2020-06-09 01:28:22 +02:00
|
|
|
return fmt.Errorf("expecting a list of strings or just a string")
|
2019-03-24 14:57:29 +01:00
|
|
|
}
|
|
|
|
|
2019-12-02 16:52:22 +01:00
|
|
|
if node.Type == NodeStr {
|
2019-03-24 14:57:29 +01:00
|
|
|
sel.DistinctOn = append(sel.DistinctOn, node.Val)
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := range node.Children {
|
|
|
|
sel.DistinctOn = append(sel.DistinctOn, node.Children[i].Val)
|
|
|
|
}
|
|
|
|
|
2020-06-09 01:28:22 +02:00
|
|
|
return nil
|
2019-03-24 14:57:29 +01:00
|
|
|
}
|
|
|
|
|
2020-06-09 01:28:22 +02:00
|
|
|
func (com *Compiler) compileArgLimit(sel *Select, arg *Arg) error {
|
2019-03-24 14:57:29 +01:00
|
|
|
node := arg.Val
|
|
|
|
|
2019-12-02 16:52:22 +01:00
|
|
|
if node.Type != NodeInt {
|
2020-06-09 01:28:22 +02:00
|
|
|
return argErr("limit", "number")
|
2019-03-24 14:57:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
sel.Paging.Limit = node.Val
|
|
|
|
|
2020-06-09 01:28:22 +02:00
|
|
|
return nil
|
2019-03-24 14:57:29 +01:00
|
|
|
}
|
|
|
|
|
2020-06-09 01:28:22 +02:00
|
|
|
func (com *Compiler) compileArgOffset(sel *Select, arg *Arg) error {
|
2019-03-24 14:57:29 +01:00
|
|
|
node := arg.Val
|
|
|
|
|
2020-02-19 05:52:44 +01:00
|
|
|
if node.Type != NodeVar {
|
2020-06-09 01:28:22 +02:00
|
|
|
return argErr("offset", "variable")
|
2019-03-24 14:57:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
sel.Paging.Offset = node.Val
|
2020-06-09 01:28:22 +02:00
|
|
|
return nil
|
2019-03-24 14:57:29 +01:00
|
|
|
}
|
|
|
|
|
2020-06-09 01:28:22 +02:00
|
|
|
func (com *Compiler) compileArgFirstLast(sel *Select, arg *Arg, pt PagingType) error {
|
2020-02-10 07:45:37 +01:00
|
|
|
node := arg.Val
|
|
|
|
|
2020-02-19 05:52:44 +01:00
|
|
|
if node.Type != NodeInt {
|
2020-06-09 01:28:22 +02:00
|
|
|
return argErr(arg.Name, "number")
|
2020-02-10 07:45:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
sel.Paging.Type = pt
|
|
|
|
sel.Paging.Limit = node.Val
|
|
|
|
|
2020-06-09 01:28:22 +02:00
|
|
|
return nil
|
2020-02-10 07:45:37 +01:00
|
|
|
}
|
|
|
|
|
2020-06-09 01:28:22 +02:00
|
|
|
func (com *Compiler) compileArgAfterBefore(sel *Select, arg *Arg, pt PagingType) error {
|
2020-02-10 07:45:37 +01:00
|
|
|
node := arg.Val
|
|
|
|
|
2020-02-19 05:52:44 +01:00
|
|
|
if node.Type != NodeVar || node.Val != "cursor" {
|
2020-06-09 01:28:22 +02:00
|
|
|
return fmt.Errorf("value for argument '%s' must be a variable named $cursor", arg.Name)
|
2020-02-10 07:45:37 +01:00
|
|
|
}
|
|
|
|
sel.Paging.Type = pt
|
2020-02-19 05:52:44 +01:00
|
|
|
sel.Paging.Cursor = true
|
2020-02-10 07:45:37 +01:00
|
|
|
|
2020-06-09 01:28:22 +02:00
|
|
|
return nil
|
2020-02-10 07:45:37 +01:00
|
|
|
}
|
|
|
|
|
2020-05-24 08:24:24 +02:00
|
|
|
// var zeroTrv = &trval{}
|
2019-10-15 08:30:19 +02:00
|
|
|
|
|
|
|
func (com *Compiler) getRole(role, field string) *trval {
|
|
|
|
if trv, ok := com.tr[role][field]; ok {
|
|
|
|
return trv
|
|
|
|
}
|
2020-05-24 08:24:24 +02:00
|
|
|
|
|
|
|
return nil
|
|
|
|
// } else {
|
|
|
|
// return zeroTrv
|
|
|
|
// }
|
2019-10-15 08:30:19 +02:00
|
|
|
}
|
|
|
|
|
2020-02-22 21:58:37 +01:00
|
|
|
func AddFilter(sel *Select, fil *Exp) {
|
2020-02-10 07:45:37 +01:00
|
|
|
if sel.Where != nil {
|
|
|
|
ow := sel.Where
|
|
|
|
|
2020-02-20 18:23:29 +01:00
|
|
|
if sel.Where.Op != OpAnd || !sel.Where.doFree {
|
2020-02-19 05:52:44 +01:00
|
|
|
sel.Where = expPool.Get().(*Exp)
|
|
|
|
sel.Where.Reset()
|
|
|
|
sel.Where.Op = OpAnd
|
|
|
|
sel.Where.Children = sel.Where.childrenA[:2]
|
|
|
|
sel.Where.Children[0] = fil
|
|
|
|
sel.Where.Children[1] = ow
|
|
|
|
|
|
|
|
} else {
|
|
|
|
sel.Where.Children = append(sel.Where.Children, fil)
|
|
|
|
}
|
2020-02-10 07:45:37 +01:00
|
|
|
|
|
|
|
} else {
|
|
|
|
sel.Where = fil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-16 21:28:50 +02:00
|
|
|
func newExp(st *util.Stack, node *Node, usePool bool) (*Exp, error) {
|
2019-06-15 04:17:21 +02:00
|
|
|
name := node.Name
|
2019-03-27 07:58:19 +01:00
|
|
|
if name[0] == '_' {
|
|
|
|
name = name[1:]
|
|
|
|
}
|
|
|
|
|
2019-06-15 07:10:53 +02:00
|
|
|
var ex *Exp
|
|
|
|
|
|
|
|
if usePool {
|
|
|
|
ex = expPool.Get().(*Exp)
|
|
|
|
ex.Reset()
|
|
|
|
} else {
|
2019-10-03 09:08:01 +02:00
|
|
|
ex = &Exp{doFree: false}
|
2019-06-15 07:10:53 +02:00
|
|
|
}
|
2020-02-10 07:45:37 +01:00
|
|
|
|
2019-06-15 07:10:53 +02:00
|
|
|
ex.Children = ex.childrenA[:0]
|
2019-06-15 04:17:21 +02:00
|
|
|
|
2019-03-27 07:58:19 +01:00
|
|
|
switch name {
|
|
|
|
case "and":
|
2019-06-16 21:41:28 +02:00
|
|
|
if len(node.Children) == 0 {
|
|
|
|
return nil, errors.New("missing expression after 'AND' operator")
|
|
|
|
}
|
2019-03-27 07:58:19 +01:00
|
|
|
ex.Op = OpAnd
|
|
|
|
pushChildren(st, ex, node)
|
|
|
|
case "or":
|
2019-06-16 21:41:28 +02:00
|
|
|
if len(node.Children) == 0 {
|
|
|
|
return nil, errors.New("missing expression after 'OR' operator")
|
|
|
|
}
|
2019-03-27 07:58:19 +01:00
|
|
|
ex.Op = OpOr
|
|
|
|
pushChildren(st, ex, node)
|
|
|
|
case "not":
|
2019-06-16 21:41:28 +02:00
|
|
|
if len(node.Children) == 0 {
|
|
|
|
return nil, errors.New("missing expression after 'NOT' operator")
|
|
|
|
}
|
2019-03-27 07:58:19 +01:00
|
|
|
ex.Op = OpNot
|
2019-06-16 21:28:50 +02:00
|
|
|
pushChild(st, ex, node)
|
2019-03-27 07:58:19 +01:00
|
|
|
case "eq", "equals":
|
|
|
|
ex.Op = OpEquals
|
|
|
|
ex.Val = node.Val
|
|
|
|
case "neq", "not_equals":
|
|
|
|
ex.Op = OpNotEquals
|
|
|
|
ex.Val = node.Val
|
|
|
|
case "gt", "greater_than":
|
|
|
|
ex.Op = OpGreaterThan
|
|
|
|
ex.Val = node.Val
|
|
|
|
case "lt", "lesser_than":
|
|
|
|
ex.Op = OpLesserThan
|
|
|
|
ex.Val = node.Val
|
|
|
|
case "gte", "greater_or_equals":
|
|
|
|
ex.Op = OpGreaterOrEquals
|
|
|
|
ex.Val = node.Val
|
|
|
|
case "lte", "lesser_or_equals":
|
|
|
|
ex.Op = OpLesserOrEquals
|
|
|
|
ex.Val = node.Val
|
|
|
|
case "in":
|
|
|
|
ex.Op = OpIn
|
|
|
|
setListVal(ex, node)
|
|
|
|
case "nin", "not_in":
|
|
|
|
ex.Op = OpNotIn
|
|
|
|
setListVal(ex, node)
|
|
|
|
case "like":
|
|
|
|
ex.Op = OpLike
|
|
|
|
ex.Val = node.Val
|
|
|
|
case "nlike", "not_like":
|
|
|
|
ex.Op = OpNotLike
|
|
|
|
ex.Val = node.Val
|
|
|
|
case "ilike":
|
|
|
|
ex.Op = OpILike
|
|
|
|
ex.Val = node.Val
|
|
|
|
case "nilike", "not_ilike":
|
|
|
|
ex.Op = OpILike
|
|
|
|
ex.Val = node.Val
|
|
|
|
case "similar":
|
|
|
|
ex.Op = OpSimilar
|
|
|
|
ex.Val = node.Val
|
|
|
|
case "nsimilar", "not_similar":
|
|
|
|
ex.Op = OpNotSimilar
|
|
|
|
ex.Val = node.Val
|
|
|
|
case "contains":
|
|
|
|
ex.Op = OpContains
|
|
|
|
ex.Val = node.Val
|
|
|
|
case "contained_in":
|
|
|
|
ex.Op = OpContainedIn
|
|
|
|
ex.Val = node.Val
|
|
|
|
case "has_key":
|
|
|
|
ex.Op = OpHasKey
|
|
|
|
ex.Val = node.Val
|
|
|
|
case "has_key_any":
|
|
|
|
ex.Op = OpHasKeyAny
|
|
|
|
ex.Val = node.Val
|
|
|
|
case "has_key_all":
|
|
|
|
ex.Op = OpHasKeyAll
|
|
|
|
ex.Val = node.Val
|
|
|
|
case "is_null":
|
|
|
|
ex.Op = OpIsNull
|
|
|
|
ex.Val = node.Val
|
2020-02-22 21:58:37 +01:00
|
|
|
case "null_eq", "ndis", "not_distinct":
|
|
|
|
ex.Op = OpNotDistinct
|
|
|
|
ex.Val = node.Val
|
|
|
|
case "null_neq", "dis", "distinct":
|
|
|
|
ex.Op = OpDistinct
|
|
|
|
ex.Val = node.Val
|
2019-03-27 07:58:19 +01:00
|
|
|
default:
|
2020-05-20 06:03:05 +02:00
|
|
|
if len(node.Children) == 0 {
|
|
|
|
return nil, fmt.Errorf("[Where] invalid operation: %s", name)
|
|
|
|
}
|
2019-06-16 21:28:50 +02:00
|
|
|
pushChildren(st, node.exp, node)
|
2019-03-27 07:58:19 +01:00
|
|
|
return nil, nil // skip node
|
|
|
|
}
|
|
|
|
|
|
|
|
if ex.Op != OpAnd && ex.Op != OpOr && ex.Op != OpNot {
|
|
|
|
switch node.Type {
|
2019-12-02 16:52:22 +01:00
|
|
|
case NodeStr:
|
2019-03-27 07:58:19 +01:00
|
|
|
ex.Type = ValStr
|
2019-12-02 16:52:22 +01:00
|
|
|
case NodeInt:
|
2019-03-27 07:58:19 +01:00
|
|
|
ex.Type = ValInt
|
2019-12-02 16:52:22 +01:00
|
|
|
case NodeBool:
|
2019-03-27 07:58:19 +01:00
|
|
|
ex.Type = ValBool
|
2019-12-02 16:52:22 +01:00
|
|
|
case NodeFloat:
|
2019-03-27 07:58:19 +01:00
|
|
|
ex.Type = ValFloat
|
2019-12-02 16:52:22 +01:00
|
|
|
case NodeList:
|
2019-03-27 07:58:19 +01:00
|
|
|
ex.Type = ValList
|
2019-12-02 16:52:22 +01:00
|
|
|
case NodeVar:
|
2019-03-27 07:58:19 +01:00
|
|
|
ex.Type = ValVar
|
|
|
|
default:
|
2020-05-20 06:03:05 +02:00
|
|
|
return nil, fmt.Errorf("[Where] invalid values for: %s", name)
|
2019-03-27 07:58:19 +01:00
|
|
|
}
|
2020-05-20 06:03:05 +02:00
|
|
|
|
2019-03-27 07:58:19 +01:00
|
|
|
setWhereColName(ex, node)
|
|
|
|
}
|
|
|
|
|
|
|
|
return ex, nil
|
|
|
|
}
|
|
|
|
|
2019-03-24 14:57:29 +01:00
|
|
|
func setListVal(ex *Exp, node *Node) {
|
|
|
|
if len(node.Children) != 0 {
|
|
|
|
switch node.Children[0].Type {
|
2019-12-02 16:52:22 +01:00
|
|
|
case NodeStr:
|
2019-03-24 14:57:29 +01:00
|
|
|
ex.ListType = ValStr
|
2019-12-02 16:52:22 +01:00
|
|
|
case NodeInt:
|
2019-03-24 14:57:29 +01:00
|
|
|
ex.ListType = ValInt
|
2019-12-02 16:52:22 +01:00
|
|
|
case NodeBool:
|
2019-03-24 14:57:29 +01:00
|
|
|
ex.ListType = ValBool
|
2019-12-02 16:52:22 +01:00
|
|
|
case NodeFloat:
|
2019-03-24 14:57:29 +01:00
|
|
|
ex.ListType = ValFloat
|
|
|
|
}
|
2020-05-24 23:43:54 +02:00
|
|
|
} else {
|
|
|
|
ex.Val = node.Val
|
|
|
|
return
|
2019-03-24 14:57:29 +01:00
|
|
|
}
|
2020-05-24 23:43:54 +02:00
|
|
|
|
2019-03-24 14:57:29 +01:00
|
|
|
for i := range node.Children {
|
|
|
|
ex.ListVal = append(ex.ListVal, node.Children[i].Val)
|
|
|
|
}
|
2020-05-24 23:43:54 +02:00
|
|
|
|
2019-03-24 14:57:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func setWhereColName(ex *Exp, node *Node) {
|
|
|
|
var list []string
|
2019-05-13 01:27:26 +02:00
|
|
|
|
2019-03-24 14:57:29 +01:00
|
|
|
for n := node.Parent; n != nil; n = n.Parent {
|
2019-12-02 16:52:22 +01:00
|
|
|
if n.Type != NodeObj {
|
2019-03-24 14:57:29 +01:00
|
|
|
continue
|
|
|
|
}
|
2019-05-13 01:27:26 +02:00
|
|
|
if len(n.Name) != 0 {
|
2019-06-15 04:17:21 +02:00
|
|
|
k := n.Name
|
2019-05-13 01:27:26 +02:00
|
|
|
if k == "and" || k == "or" || k == "not" ||
|
|
|
|
k == "_and" || k == "_or" || k == "_not" {
|
|
|
|
continue
|
|
|
|
}
|
2019-03-24 14:57:29 +01:00
|
|
|
list = append([]string{k}, list...)
|
|
|
|
}
|
|
|
|
}
|
2020-01-13 15:34:15 +01:00
|
|
|
listlen := len(list)
|
|
|
|
|
|
|
|
if listlen == 1 {
|
2019-03-24 14:57:29 +01:00
|
|
|
ex.Col = list[0]
|
2020-01-13 15:34:15 +01:00
|
|
|
} else if listlen > 1 {
|
|
|
|
ex.Col = list[listlen-1]
|
|
|
|
ex.NestedCols = list[:listlen]
|
2019-03-24 14:57:29 +01:00
|
|
|
}
|
2020-05-20 06:03:05 +02:00
|
|
|
|
2019-03-24 14:57:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func setOrderByColName(ob *OrderBy, node *Node) {
|
|
|
|
var list []string
|
2019-05-13 01:27:26 +02:00
|
|
|
|
2019-03-24 14:57:29 +01:00
|
|
|
for n := node; n != nil; n = n.Parent {
|
2019-05-13 01:27:26 +02:00
|
|
|
if len(n.Name) != 0 {
|
2019-06-15 04:17:21 +02:00
|
|
|
list = append([]string{n.Name}, list...)
|
2019-03-24 14:57:29 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if len(list) != 0 {
|
2019-05-13 01:27:26 +02:00
|
|
|
ob.Col = buildPath(list)
|
2019-03-24 14:57:29 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-16 21:28:50 +02:00
|
|
|
func pushChildren(st *util.Stack, exp *Exp, node *Node) {
|
2019-03-24 14:57:29 +01:00
|
|
|
for i := range node.Children {
|
2019-06-16 21:28:50 +02:00
|
|
|
node.Children[i].exp = exp
|
|
|
|
st.Push(node.Children[i])
|
2019-03-24 14:57:29 +01:00
|
|
|
}
|
|
|
|
}
|
2019-04-08 08:47:59 +02:00
|
|
|
|
2019-06-16 21:28:50 +02:00
|
|
|
func pushChild(st *util.Stack, exp *Exp, node *Node) {
|
|
|
|
node.Children[0].exp = exp
|
|
|
|
st.Push(node.Children[0])
|
|
|
|
}
|
|
|
|
|
2020-01-21 05:38:17 +01:00
|
|
|
func compileFilter(filter []string) (*Exp, bool, error) {
|
2019-04-08 08:47:59 +02:00
|
|
|
var fl *Exp
|
2020-01-21 05:38:17 +01:00
|
|
|
var needsUser bool
|
|
|
|
|
2019-04-08 08:47:59 +02:00
|
|
|
com := &Compiler{}
|
2019-06-17 02:51:36 +02:00
|
|
|
st := util.NewStack()
|
2019-04-08 08:47:59 +02:00
|
|
|
|
2019-04-09 03:24:29 +02:00
|
|
|
if len(filter) == 0 {
|
2020-01-21 05:38:17 +01:00
|
|
|
return &Exp{Op: OpNop, doFree: false}, false, nil
|
2019-04-09 03:24:29 +02:00
|
|
|
}
|
|
|
|
|
2019-04-08 08:47:59 +02:00
|
|
|
for i := range filter {
|
2019-10-26 07:34:29 +02:00
|
|
|
if filter[i] == "false" {
|
2020-01-21 05:38:17 +01:00
|
|
|
return &Exp{Op: OpFalse, doFree: false}, false, nil
|
2019-10-26 07:34:29 +02:00
|
|
|
}
|
|
|
|
|
2019-04-08 08:47:59 +02:00
|
|
|
node, err := ParseArgValue(filter[i])
|
|
|
|
if err != nil {
|
2020-01-21 05:38:17 +01:00
|
|
|
return nil, false, err
|
2019-04-08 08:47:59 +02:00
|
|
|
}
|
2020-01-21 05:38:17 +01:00
|
|
|
f, nu, err := com.compileArgNode(st, node, false)
|
2019-04-08 08:47:59 +02:00
|
|
|
if err != nil {
|
2020-01-21 05:38:17 +01:00
|
|
|
return nil, false, err
|
|
|
|
}
|
|
|
|
if nu {
|
|
|
|
needsUser = true
|
2019-04-08 08:47:59 +02:00
|
|
|
}
|
2019-10-03 09:08:01 +02:00
|
|
|
|
2020-01-13 15:34:15 +01:00
|
|
|
// TODO: Invalid table names in nested where causes fail silently
|
|
|
|
// returning a nil 'f' this needs to be fixed
|
|
|
|
|
|
|
|
// TODO: Invalid where clauses such as missing op (eg. eq) also fail silently
|
|
|
|
|
2019-04-08 08:47:59 +02:00
|
|
|
if fl == nil {
|
|
|
|
fl = f
|
|
|
|
} else {
|
2019-10-03 09:08:01 +02:00
|
|
|
fl = &Exp{Op: OpAnd, Children: []*Exp{fl, f}, doFree: false}
|
2019-04-08 08:47:59 +02:00
|
|
|
}
|
|
|
|
}
|
2020-01-21 05:38:17 +01:00
|
|
|
return fl, needsUser, nil
|
2019-04-08 08:47:59 +02:00
|
|
|
}
|
2019-05-13 01:27:26 +02:00
|
|
|
|
|
|
|
func buildPath(a []string) string {
|
|
|
|
switch len(a) {
|
|
|
|
case 0:
|
|
|
|
return ""
|
|
|
|
case 1:
|
|
|
|
return a[0]
|
|
|
|
}
|
|
|
|
|
|
|
|
n := len(a) - 1
|
|
|
|
for i := 0; i < len(a); i++ {
|
|
|
|
n += len(a[i])
|
|
|
|
}
|
|
|
|
|
|
|
|
var b strings.Builder
|
|
|
|
b.Grow(n)
|
|
|
|
b.WriteString(a[0])
|
|
|
|
for _, s := range a[1:] {
|
|
|
|
b.WriteRune('.')
|
|
|
|
b.WriteString(s)
|
|
|
|
}
|
|
|
|
return b.String()
|
|
|
|
}
|
2019-05-15 04:32:12 +02:00
|
|
|
|
2019-06-01 08:03:09 +02:00
|
|
|
func (t ExpOp) String() string {
|
|
|
|
var v string
|
|
|
|
|
|
|
|
switch t {
|
|
|
|
case OpNop:
|
|
|
|
v = "op-nop"
|
|
|
|
case OpAnd:
|
|
|
|
v = "op-and"
|
|
|
|
case OpOr:
|
|
|
|
v = "op-or"
|
|
|
|
case OpNot:
|
|
|
|
v = "op-not"
|
|
|
|
case OpEquals:
|
|
|
|
v = "op-equals"
|
|
|
|
case OpNotEquals:
|
|
|
|
v = "op-not-equals"
|
|
|
|
case OpGreaterOrEquals:
|
|
|
|
v = "op-greater-or-equals"
|
|
|
|
case OpLesserOrEquals:
|
|
|
|
v = "op-lesser-or-equals"
|
|
|
|
case OpGreaterThan:
|
|
|
|
v = "op-greater-than"
|
|
|
|
case OpLesserThan:
|
|
|
|
v = "op-lesser-than"
|
|
|
|
case OpIn:
|
|
|
|
v = "op-in"
|
|
|
|
case OpNotIn:
|
|
|
|
v = "op-not-in"
|
|
|
|
case OpLike:
|
|
|
|
v = "op-like"
|
|
|
|
case OpNotLike:
|
|
|
|
v = "op-not-like"
|
|
|
|
case OpILike:
|
|
|
|
v = "op-i-like"
|
|
|
|
case OpNotILike:
|
|
|
|
v = "op-not-i-like"
|
|
|
|
case OpSimilar:
|
|
|
|
v = "op-similar"
|
|
|
|
case OpNotSimilar:
|
|
|
|
v = "op-not-similar"
|
|
|
|
case OpContains:
|
|
|
|
v = "op-contains"
|
|
|
|
case OpContainedIn:
|
|
|
|
v = "op-contained-in"
|
|
|
|
case OpHasKey:
|
|
|
|
v = "op-has-key"
|
|
|
|
case OpHasKeyAny:
|
|
|
|
v = "op-has-key-any"
|
|
|
|
case OpHasKeyAll:
|
|
|
|
v = "op-has-key-all"
|
|
|
|
case OpIsNull:
|
|
|
|
v = "op-is-null"
|
|
|
|
case OpEqID:
|
|
|
|
v = "op-eq-id"
|
|
|
|
case OpTsQuery:
|
|
|
|
v = "op-ts-query"
|
|
|
|
}
|
|
|
|
return fmt.Sprintf("<%s>", v)
|
|
|
|
}
|
2019-06-15 07:10:53 +02:00
|
|
|
|
|
|
|
func FreeExp(ex *Exp) {
|
|
|
|
if ex.doFree {
|
|
|
|
expPool.Put(ex)
|
|
|
|
}
|
|
|
|
}
|
2020-02-19 05:52:44 +01:00
|
|
|
|
|
|
|
func argErr(name, ty string) error {
|
|
|
|
return fmt.Errorf("value for argument '%s' must be a %s", name, ty)
|
|
|
|
}
|
2020-06-09 01:28:22 +02:00
|
|
|
|
|
|
|
func freeNodes(op *Operation) {
|
|
|
|
var st *util.Stack
|
|
|
|
fm := make(map[*Node]struct{})
|
|
|
|
|
|
|
|
for i := range op.Args {
|
|
|
|
arg := op.Args[i]
|
|
|
|
if arg.df {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := range arg.Val.Children {
|
|
|
|
if st == nil {
|
|
|
|
st = util.NewStack()
|
|
|
|
}
|
|
|
|
c := arg.Val.Children[i]
|
|
|
|
if _, ok := fm[c]; !ok {
|
|
|
|
st.Push(c)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, ok := fm[arg.Val]; !ok {
|
|
|
|
nodePool.Put(arg.Val)
|
|
|
|
fm[arg.Val] = struct{}{}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := range op.Fields {
|
|
|
|
f := op.Fields[i]
|
|
|
|
|
|
|
|
for j := range f.Args {
|
|
|
|
arg := f.Args[j]
|
|
|
|
if arg.df {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
for k := range arg.Val.Children {
|
|
|
|
if st == nil {
|
|
|
|
st = util.NewStack()
|
|
|
|
}
|
|
|
|
c := arg.Val.Children[k]
|
|
|
|
if _, ok := fm[c]; !ok {
|
|
|
|
st.Push(c)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, ok := fm[arg.Val]; !ok {
|
|
|
|
nodePool.Put(arg.Val)
|
|
|
|
fm[arg.Val] = struct{}{}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if st == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
for {
|
|
|
|
if st.Len() == 0 {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
intf := st.Pop()
|
|
|
|
node, ok := intf.(*Node)
|
|
|
|
if !ok || node == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := range node.Children {
|
|
|
|
st.Push(node.Children[i])
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, ok := fm[node]; !ok {
|
|
|
|
nodePool.Put(node)
|
|
|
|
fm[node] = struct{}{}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|