From 67b4a4d9453bb90d12a1ea213e607456cea0ae55 Mon Sep 17 00:00:00 2001 From: Vikram Rangnekar Date: Tue, 11 Feb 2020 11:41:35 +0530 Subject: [PATCH] Fix issue with cursor as a variable --- docs/guide.md | 2 +- psql/mutate.go | 2 +- psql/query.go | 22 +++++++++++++++------- qcode/qcode.go | 11 +++++------ 4 files changed, 22 insertions(+), 15 deletions(-) diff --git a/docs/guide.md b/docs/guide.md index 404371e..a01b38a 100644 --- a/docs/guide.md +++ b/docs/guide.md @@ -1024,7 +1024,7 @@ mutation { } ``` -#### Pagination +### Pagination This is a must have feature of any API. When you want your users to go thought a list page by page or implement some fancy infinite scroll you're going to need pagination. There are two ways to paginate in Super Graph. diff --git a/psql/mutate.go b/psql/mutate.go index 08dad58..4319635 100644 --- a/psql/mutate.go +++ b/psql/mutate.go @@ -77,7 +77,7 @@ func (co *Compiler) compileMutation(qc *qcode.QCode, w io.Writer, vars Variables root.Where = nil root.Args = nil - return c.compileQuery(qc, w) + return c.compileQuery(qc, w, vars) } type kvitem struct { diff --git a/psql/query.go b/psql/query.go index e9f6cc2..85d22cc 100644 --- a/psql/query.go +++ b/psql/query.go @@ -71,7 +71,7 @@ func (co *Compiler) CompileEx(qc *qcode.QCode, vars Variables) (uint32, []byte, func (co *Compiler) Compile(qc *qcode.QCode, w io.Writer, vars Variables) (uint32, error) { switch qc.Type { case qcode.QTQuery: - return co.compileQuery(qc, w) + return co.compileQuery(qc, w, vars) case qcode.QTInsert, qcode.QTUpdate, qcode.QTDelete, qcode.QTUpsert: return co.compileMutation(qc, w, vars) } @@ -79,7 +79,7 @@ func (co *Compiler) Compile(qc *qcode.QCode, w io.Writer, vars Variables) (uint3 return 0, fmt.Errorf("Unknown operation type %d", qc.Type) } -func (co *Compiler) compileQuery(qc *qcode.QCode, w io.Writer) (uint32, error) { +func (co *Compiler) compileQuery(qc *qcode.QCode, w io.Writer, vars Variables) (uint32, error) { if len(qc.Selects) == 0 { return 0, errors.New("empty query") } @@ -150,7 +150,7 @@ func (co *Compiler) compileQuery(qc *qcode.QCode, w io.Writer) (uint32, error) { c.renderLateralJoin(sel) } - skipped, err := c.renderSelect(sel, ti) + skipped, err := c.renderSelect(sel, ti, vars) if err != nil { return 0, err } @@ -196,7 +196,7 @@ func (co *Compiler) compileQuery(qc *qcode.QCode, w io.Writer) (uint32, error) { return ignored, nil } -func (c *compilerContext) initSelector(sel *qcode.Select, ti *DBTableInfo) (uint32, []*qcode.Column, error) { +func (c *compilerContext) initSelector(sel *qcode.Select, ti *DBTableInfo, vars Variables) (uint32, []*qcode.Column, error) { var skipped uint32 cols := make([]*qcode.Column, 0, len(sel.Cols)) @@ -232,7 +232,15 @@ func (c *compilerContext) initSelector(sel *qcode.Select, ti *DBTableInfo) (uint }) if len(sel.Paging.Cursor) != 0 { - v, err := c.decryptor(sel.Paging.Cursor) + var v []byte + var err error + + if cursor, ok := vars[sel.Paging.Cursor]; ok && cursor[0] == '"' { + v, err = c.decryptor(string(cursor[1 : len(cursor)-1])) + } else { + v, err = c.decryptor(sel.Paging.Cursor) + } + if err != nil { return 0, nil, err } @@ -289,7 +297,7 @@ func (c *compilerContext) initSelector(sel *qcode.Select, ti *DBTableInfo) (uint return skipped, cols, nil } -func (c *compilerContext) renderSelect(sel *qcode.Select, ti *DBTableInfo) (uint32, error) { +func (c *compilerContext) renderSelect(sel *qcode.Select, ti *DBTableInfo, vars Variables) (uint32, error) { var rel *DBRel var err error @@ -302,7 +310,7 @@ func (c *compilerContext) renderSelect(sel *qcode.Select, ti *DBTableInfo) (uint } } - skipped, childCols, err := c.initSelector(sel, ti) + skipped, childCols, err := c.initSelector(sel, ti, vars) if err != nil { return 0, err } diff --git a/qcode/qcode.go b/qcode/qcode.go index c0a3c94..d5a1fb4 100644 --- a/qcode/qcode.go +++ b/qcode/qcode.go @@ -711,7 +711,7 @@ func (com *Compiler) compileArgOrderBy(sel *Select, arg *Arg) (error, bool) { } if _, ok := com.bl[node.Name]; ok { - //FreeNode(node, 2) + FreeNode(node, 2) continue } @@ -719,7 +719,7 @@ func (com *Compiler) compileArgOrderBy(sel *Select, arg *Arg) (error, bool) { for i := range node.Children { st.Push(node.Children[i]) } - //FreeNode(node, 3) + FreeNode(node, 3) continue } @@ -768,7 +768,6 @@ func (com *Compiler) compileArgDistinctOn(sel *Select, arg *Arg) (error, bool) { sel.DistinctOn = append(sel.DistinctOn, node.Children[i].Val) FreeNode(node.Children[i], 5) } - //FreeNode(node, 5) return nil, false } @@ -788,7 +787,7 @@ func (com *Compiler) compileArgLimit(sel *Select, arg *Arg) (error, bool) { func (com *Compiler) compileArgOffset(sel *Select, arg *Arg) (error, bool) { node := arg.Val - if node.Type != NodeInt { + if node.Type != NodeInt && node.Type != NodeVar { return fmt.Errorf("expecting an integer"), false } @@ -799,7 +798,7 @@ func (com *Compiler) compileArgOffset(sel *Select, arg *Arg) (error, bool) { func (com *Compiler) compileArgFirstLast(sel *Select, arg *Arg, pt PagingType) (error, bool) { node := arg.Val - if node.Type != NodeInt { + if node.Type != NodeInt && node.Type != NodeVar { return fmt.Errorf("expecting an integer"), false } @@ -812,7 +811,7 @@ func (com *Compiler) compileArgFirstLast(sel *Select, arg *Arg, pt PagingType) ( func (com *Compiler) compileArgAfterBefore(sel *Select, arg *Arg, pt PagingType) (error, bool) { node := arg.Val - if node.Type != NodeStr { + if node.Type != NodeStr && node.Type != NodeVar { return fmt.Errorf("expecting a string"), false }