diff --git a/parser.go b/parser.go index 1086db7..561e2f1 100644 --- a/parser.go +++ b/parser.go @@ -11,10 +11,6 @@ import ( "time" ) -var ( - reservedOps = []string{OffsetOp, LimitOp, SelectOp, SortOp} -) - const ( OffsetOp = "offset" LimitOp = "limit" @@ -65,7 +61,7 @@ func (tb TokenBloc) String() (s string) { } func (r *RqlRootNode) parseSpecialOps() { - if parseLimit(r.Node, r) || parseSort(r.Node, r) || parseOffset(r.Node, r) || parseFields(r.Node, r) { + if parseLimitOffset(r.Node, r) || parseSort(r.Node, r) || parseFields(r.Node, r) { r.Node = nil } else if r.Node != nil { if strings.ToUpper(r.Node.Op) == "AND" { @@ -73,7 +69,7 @@ func (r *RqlRootNode) parseSpecialOps() { for _, c := range r.Node.Args { switch n := c.(type) { case *RqlNode: - isSpecialOps := parseLimit(n, r) || parseSort(n, r) || parseOffset(n, r) || parseFields(n, r) + isSpecialOps := parseLimitOffset(n, r) || parseSort(n, r) || parseFields(n, r) if !isSpecialOps { tmpNodeArgs = append(tmpNodeArgs, n) } @@ -87,28 +83,20 @@ func (r *RqlRootNode) parseSpecialOps() { } } -func parseLimit(n *RqlNode, root *RqlRootNode) (isLimitOp bool) { +func parseLimitOffset(n *RqlNode, root *RqlRootNode) (isLimitOp bool) { if n == nil { return false } - if n.Op == LimitOp { - root.limit = n.Args[1].(string) + if strings.ToLower(n.Op) == LimitOp { + root.limit = n.Args[0].(string) + if len(n.Args) > 1 { + root.offset = n.Args[1].(string) + } isLimitOp = true } return } -func parseOffset(n *RqlNode, root *RqlRootNode) (isOffsetOp bool) { - if n == nil { - return false - } - if n.Op == OffsetOp { - root.offset = n.Args[1].(string) - isOffsetOp = true - } - return -} - func parseSort(n *RqlNode, root *RqlRootNode) (isSortOp bool) { if n == nil { return false @@ -456,9 +444,6 @@ func getBlocNode(tb []TokenString) (*RqlNode, error) { if err != nil { return nil, err } - } else if isReservedBloc(tb) { - n.Op = tb[0].s - n.Args = []interface{}{tb[0].s, tb[2].s} } else if isSimpleEqualBloc(tb) { n.Op = "eq" n.Args = []interface{}{tb[0].s, tb[2].s} @@ -555,16 +540,6 @@ func isSimpleEqualBloc(tb []TokenString) bool { return isSimple } -func isReservedBloc(tb []TokenString) bool { - matchReserved := false - for _, r := range reservedOps { - if tb[0].s == r { - matchReserved = true - } - } - return tb[0].t == Ident && tb[1].t == EqualSign && matchReserved -} - func isDoubleEqualBloc(tb []TokenString) bool { return tb[0].t == Ident && tb[1].t == EqualSign && tb[2].t == Ident && tb[3].t == EqualSign } diff --git a/pkg/driver/sql_test.go b/pkg/driver/sql_test.go index 80d2e2d..3394176 100644 --- a/pkg/driver/sql_test.go +++ b/pkg/driver/sql_test.go @@ -59,7 +59,7 @@ var tests = []Test{ }, { Name: `Sort and limit`, - RQL: `eq(foo,42)&sort(+price,-length)&limit=10&offset=20`, + RQL: `eq(foo,42)&sort(+price,-length)&limit(10,20)`, SQL: `WHERE ((foo = 42)) ORDER BY price, length DESC LIMIT 10 OFFSET 20`, WantParseError: false, WantTranslatorError: false, diff --git a/validation.go b/validation.go index 0de3dd3..5c2089a 100644 --- a/validation.go +++ b/validation.go @@ -135,7 +135,7 @@ func (p *Parser) validateSpecialOps(r *RqlRootNode) error { return err } } - if len(r.Sort()) > 0 { + if p.c != nil && len(r.Sort()) > 0 { err := p.validateSort(r.Sort()) if err != nil { return err @@ -173,7 +173,7 @@ func (p *Parser) validateLimit(l string) error { if limit < 0 { return fmt.Errorf("specified limit is less than zero") } - if limit > p.c.LimitMaxValue { + if p.c != nil && limit > p.c.LimitMaxValue { return fmt.Errorf("specified limit is more than the max limit %d allowed", p.c.DefaultLimit) } return nil