Enable query params for USE NS / USE DB in queries

This commit is contained in:
Tobie Morgan Hitchcock 2017-02-20 01:18:34 +00:00
parent ac868694eb
commit e10debba6c
2 changed files with 38 additions and 8 deletions

View file

@ -114,6 +114,7 @@ const (
CONTENT CONTENT
CREATE CREATE
DATABASE DATABASE
DB
DEFAULT DEFAULT
DEFINE DEFINE
DELETE DELETE
@ -152,6 +153,7 @@ const (
NOT NOT
NOTNULL NOTNULL
NOW NOW
NS
NULL NULL
OFFSET OFFSET
ON ON
@ -279,6 +281,7 @@ var tokens = [...]string{
CONTENT: "CONTENT", CONTENT: "CONTENT",
CREATE: "CREATE", CREATE: "CREATE",
DATABASE: "DATABASE", DATABASE: "DATABASE",
DB: "DB",
DEFAULT: "DEFAULT", DEFAULT: "DEFAULT",
DEFINE: "DEFINE", DEFINE: "DEFINE",
DELETE: "DELETE", DELETE: "DELETE",
@ -317,6 +320,7 @@ var tokens = [...]string{
NOT: "NOT", NOT: "NOT",
NOTNULL: "NOTNULL", NOTNULL: "NOTNULL",
NOW: "NOW", NOW: "NOW",
NS: "NS",
NULL: "NULL", NULL: "NULL",
ON: "ON", ON: "ON",
OR: "OR", OR: "OR",

View file

@ -21,18 +21,33 @@ func (p *parser) parseUseStatement() (stmt *UseStatement, err error) {
var tok Token var tok Token
var exi bool var exi bool
tok, _, err = p.shouldBe(NAMESPACE, DATABASE) tok, _, err = p.shouldBe(NAMESPACE, NS, DATABASE, DB)
if err != nil { if err != nil {
return nil, err return nil, err
} }
for { for {
if p.is(tok, NAMESPACE) { var ok bool
var lit string
var val interface{}
_, stmt.NS, err = p.shouldBe(IDENT, STRING) if p.is(tok, NAMESPACE, NS) {
tok, lit, err = p.shouldBe(IDENT, STRING, PARAM)
if err != nil { if err != nil {
return nil, &ParseError{Found: stmt.NS, Expected: []string{"namespace name"}} return nil, &ParseError{Found: lit, Expected: []string{"namespace name"}}
}
switch tok {
default:
val, err = p.declare(STRING, lit)
case PARAM:
val, err = p.declare(PARAM, lit)
}
if stmt.NS, ok = val.(string); !ok || err != nil {
return nil, &ParseError{Found: lit, Expected: []string{"namespace name as a STRING"}}
} }
if err = p.o.ns(stmt.NS); err != nil { if err = p.o.ns(stmt.NS); err != nil {
@ -41,11 +56,22 @@ func (p *parser) parseUseStatement() (stmt *UseStatement, err error) {
} }
if p.is(tok, DATABASE) { if p.is(tok, DATABASE, DB) {
_, stmt.DB, err = p.shouldBe(IDENT, DATE, TIME, STRING, NUMBER, DOUBLE) tok, lit, err = p.shouldBe(IDENT, DATE, TIME, STRING, NUMBER, DOUBLE, PARAM)
if err != nil { if err != nil {
return nil, &ParseError{Found: stmt.DB, Expected: []string{"database name"}} return nil, &ParseError{Found: lit, Expected: []string{"database name"}}
}
switch tok {
default:
val, err = p.declare(STRING, lit)
case PARAM:
val, err = p.declare(PARAM, lit)
}
if stmt.DB, ok = val.(string); !ok || err != nil {
return nil, &ParseError{Found: lit, Expected: []string{"database name as a STRING"}}
} }
if err = p.o.db(stmt.DB); err != nil { if err = p.o.db(stmt.DB); err != nil {
@ -54,7 +80,7 @@ func (p *parser) parseUseStatement() (stmt *UseStatement, err error) {
} }
tok, _, exi = p.mightBe(NAMESPACE, DATABASE) tok, _, exi = p.mightBe(NAMESPACE, NS, DATABASE, DB)
if !exi { if !exi {
break break
} }