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

@ -29,19 +29,20 @@ const (
literalsBeg literalsBeg
DATE // 1970-01-01 DATE // 1970-01-01
TIME // 1970-01-01T00:00:00+00:00 TIME // 1970-01-01T00:00:00+00:00
PATH // person->like->person PATH // person->like->person
JSON // {"test":true} JSON // {"test":true}
IDENT // something IDENT // something
STRING // "something" THING // @class:id
REGION // "a multiline \n string" STRING // "something"
NUMBER // 123456 REGION // "a multiline \n string"
DOUBLE // 123.456 NUMBER // 123456
REGEX // /.*/ DOUBLE // 123.456
ARRAY // [0,1,2] REGEX // /.*/
DURATION // 13h ARRAY // [0,1,2]
BOUNDPARAM // $1 DURATION // 13h
PARAM // $1
EAT // @ EAT // @
DOT // . DOT // .
@ -199,19 +200,20 @@ var tokens = [...]string{
// literals // literals
DATE: "DATE", DATE: "DATE",
TIME: "TIME", TIME: "TIME",
PATH: "PATH", PATH: "PATH",
JSON: "JSON", JSON: "JSON",
IDENT: "IDENT", IDENT: "IDENT",
STRING: "STRING", THING: "THING",
REGION: "REGION", STRING: "STRING",
NUMBER: "NUMBER", REGION: "REGION",
DOUBLE: "DOUBLE", NUMBER: "NUMBER",
REGEX: "REGEX", DOUBLE: "DOUBLE",
ARRAY: "ARRAY", REGEX: "REGEX",
DURATION: "DURATION", ARRAY: "ARRAY",
BOUNDPARAM: "BOUNDPARAM", DURATION: "DURATION",
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