Enable bound parameters in sql queries

This commit is contained in:
Tobie Morgan Hitchcock 2016-09-07 16:44:23 +01:00
parent 96b44a8da6
commit af4281c920
5 changed files with 38 additions and 29 deletions

View file

@ -28,7 +28,7 @@ func (p *Parser) parseCond() (mul []Expr, err error) {
one := &BinaryExpression{} one := &BinaryExpression{}
tok, lit, err = p.shouldBe(ID, IDENT, NULL, VOID, MISSING, EMPTY, NOW, DATE, TIME, TRUE, FALSE, STRING, REGION, NUMBER, DOUBLE, REGEX, JSON, ARRAY, BOUNDPARAM) tok, lit, err = p.shouldBe(ID, IDENT, THING, NULL, VOID, MISSING, EMPTY, NOW, DATE, TIME, TRUE, FALSE, STRING, REGION, NUMBER, DOUBLE, REGEX, JSON, ARRAY, PARAM)
if err != nil { if err != nil {
return nil, &ParseError{Found: lit, Expected: []string{"field name"}} return nil, &ParseError{Found: lit, Expected: []string{"field name"}}
} }
@ -70,7 +70,7 @@ func (p *Parser) parseCond() (mul []Expr, err error) {
} }
} }
tok, lit, err = p.shouldBe(ID, IDENT, NULL, VOID, MISSING, EMPTY, NOW, DATE, TIME, TRUE, FALSE, STRING, REGION, NUMBER, DOUBLE, REGEX, JSON, ARRAY, BOUNDPARAM) tok, lit, err = p.shouldBe(ID, IDENT, THING, NULL, VOID, MISSING, EMPTY, NOW, DATE, TIME, TRUE, FALSE, STRING, REGION, NUMBER, DOUBLE, REGEX, JSON, ARRAY, PARAM)
if err != nil { if err != nil {
return nil, &ParseError{Found: lit, Expected: []string{"field value"}} return nil, &ParseError{Found: lit, Expected: []string{"field value"}}
} }

View file

@ -67,7 +67,7 @@ func (p *Parser) parseSet() (mul []Expr, err error) {
} }
one.Op = tok one.Op = tok
tok, lit, err = p.shouldBe(IDENT, NULL, VOID, NOW, DATE, TIME, TRUE, FALSE, STRING, REGION, NUMBER, DOUBLE, JSON, ARRAY, BOUNDPARAM) tok, lit, err = p.shouldBe(IDENT, NULL, VOID, NOW, DATE, TIME, TRUE, FALSE, STRING, REGION, NUMBER, DOUBLE, JSON, ARRAY, PARAM)
if err != nil { if err != nil {
return nil, &ParseError{Found: lit, Expected: []string{"field value"}} return nil, &ParseError{Found: lit, Expected: []string{"field value"}}
} }

View file

@ -26,6 +26,7 @@ import (
type Parser struct { type Parser struct {
s *Scanner s *Scanner
c *fibre.Context c *fibre.Context
v map[string]interface{}
buf struct { buf struct {
tok Token // last read token tok Token // last read token
lit string // last read literal lit string // last read literal

View file

@ -34,6 +34,7 @@ const (
PATH // person->like->person PATH // person->like->person
JSON // {"test":true} JSON // {"test":true}
IDENT // something IDENT // something
THING // @class:id
STRING // "something" STRING // "something"
REGION // "a multiline \n string" REGION // "a multiline \n string"
NUMBER // 123456 NUMBER // 123456
@ -41,7 +42,7 @@ const (
REGEX // /.*/ REGEX // /.*/
ARRAY // [0,1,2] ARRAY // [0,1,2]
DURATION // 13h DURATION // 13h
BOUNDPARAM // $1 PARAM // $1
EAT // @ EAT // @
DOT // . DOT // .
@ -204,6 +205,7 @@ var tokens = [...]string{
PATH: "PATH", PATH: "PATH",
JSON: "JSON", JSON: "JSON",
IDENT: "IDENT", IDENT: "IDENT",
THING: "THING",
STRING: "STRING", STRING: "STRING",
REGION: "REGION", REGION: "REGION",
NUMBER: "NUMBER", NUMBER: "NUMBER",
@ -211,7 +213,7 @@ var tokens = [...]string{
REGEX: "REGEX", REGEX: "REGEX",
ARRAY: "ARRAY", ARRAY: "ARRAY",
DURATION: "DURATION", DURATION: "DURATION",
BOUNDPARAM: "BOUNDPARAM", PARAM: "PARAM",
EAT: "@", EAT: "@",
DOT: ".", DOT: ".",

View file

@ -138,6 +138,12 @@ func (p *Parser) declare(tok Token, lit string) (interface{}, error) {
} }
return j, nil return j, nil
case PARAM:
if p, ok := p.v[lit]; ok {
return p, nil
}
return nil, fmt.Errorf("Param %s is not defined", lit)
} }
return lit, nil return lit, nil