From fdbf38f8337a81bf77974c09185d2c9e658bed56 Mon Sep 17 00:00:00 2001 From: Tobie Morgan Hitchcock Date: Sat, 5 Nov 2016 14:00:41 +0000 Subject: [PATCH] Enable parameters in @thing:id in sql parser --- sql/scanner.go | 49 ++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 48 insertions(+), 1 deletion(-) diff --git a/sql/scanner.go b/sql/scanner.go index c0da5390..cb819126 100644 --- a/sql/scanner.go +++ b/sql/scanner.go @@ -422,6 +422,14 @@ func (s *scanner) scanThing(chp ...rune) (tok Token, lit string, val interface{} tok = THING + // Store whether params + var tbp bool + var idp bool + + // Store section values + var tbv interface{} + var idv interface{} + // Create a buffer var buf bytes.Buffer var beg bytes.Buffer @@ -440,6 +448,11 @@ func (s *scanner) scanThing(chp ...rune) (tok Token, lit string, val interface{} tok, lit, _ = s.scanIdent(ch) beg.WriteString(lit) break + } else if ch == '$' { + tbp = true // The TB is a param + tok, lit, _ = s.scanParams(ch) + beg.WriteString(lit) + break } else if ch == '`' { tok, lit, _ = s.scanQuoted(ch) beg.WriteString(lit) @@ -483,6 +496,11 @@ func (s *scanner) scanThing(chp ...rune) (tok Token, lit string, val interface{} tok, lit, _ = s.scanIdent(ch) end.WriteString(lit) break + } else if ch == '$' { + idp = true // The ID is a param + tok, lit, _ = s.scanParams(ch) + end.WriteString(lit) + break } else if ch == '`' { tok, lit, _ = s.scanQuoted(ch) end.WriteString(lit) @@ -505,7 +523,36 @@ func (s *scanner) scanThing(chp ...rune) (tok Token, lit string, val interface{} return ILLEGAL, buf.String() + beg.String() + mid.String() + end.String(), val } - val = NewThing(beg.String(), end.String()) + tbv = beg.String() + idv = end.String() + + if tbp { // The TB is a param + if p, ok := s.p.v[tbv.(string)]; ok { + switch p.(type) { + case bool, int64, float64, string, []interface{}, map[string]interface{}: + tbv = p + default: + return ILLEGAL, buf.String() + beg.String() + mid.String() + end.String(), val + } + } else { + return ILLEGAL, buf.String() + beg.String() + mid.String() + end.String(), val + } + } + + if idp { // The ID is a param + if p, ok := s.p.v[idv.(string)]; ok { + switch p.(type) { + case bool, int64, float64, string, []interface{}, map[string]interface{}: + idv = p + default: + return ILLEGAL, buf.String() + beg.String() + mid.String() + end.String(), val + } + } else { + return ILLEGAL, buf.String() + beg.String() + mid.String() + end.String(), val + } + } + + val = NewThing(tbv, idv) // Otherwise return as a regular thing. return THING, buf.String() + beg.String() + mid.String() + end.String(), val