From 41c2fd0b3ee41c18421af1df0fd4613bf17c4e58 Mon Sep 17 00:00:00 2001 From: Tobie Morgan Hitchcock Date: Thu, 13 Sep 2018 15:23:17 +0100 Subject: [PATCH] Add scope tokens to SQL INFO queries --- db/info.go | 30 ++++++++++++++++++++++++++---- db/info_test.go | 26 ++++++++++++++++++++++++++ sql/ast.go | 2 +- sql/info.go | 13 +++++++++++-- sql/sql_test.go | 18 +++++++++++++++--- sql/string.go | 2 ++ 6 files changed, 81 insertions(+), 10 deletions(-) diff --git a/db/info.go b/db/info.go index f32a3ecd..9f365516 100644 --- a/db/info.go +++ b/db/info.go @@ -28,6 +28,8 @@ func (e *executor) executeInfo(ctx context.Context, ast *sql.InfoStatement) (out return e.executeInfoNS(ctx, ast) case sql.DATABASE: return e.executeInfoDB(ctx, ast) + case sql.SCOPE: + return e.executeInfoSC(ctx, ast) case sql.TABLE: return e.executeInfoTB(ctx, ast) } @@ -131,24 +133,44 @@ func (e *executor) executeInfoDB(ctx context.Context, ast *sql.InfoStatement) (o } +func (e *executor) executeInfoSC(ctx context.Context, ast *sql.InfoStatement) (out []interface{}, err error) { + + st, err := e.dbo.AllST(ctx, ast.NS, ast.DB, ast.What.VA) + if err != nil { + return nil, err + } + + res := data.New() + + token := make(map[string]interface{}) + for _, v := range st { + token[v.Name.VA] = v.String() + } + + res.Set(token, "token") + + return []interface{}{res.Data()}, nil + +} + func (e *executor) executeInfoTB(ctx context.Context, ast *sql.InfoStatement) (out []interface{}, err error) { - ev, err := e.dbo.AllEV(ctx, ast.NS, ast.DB, ast.What.TB) + ev, err := e.dbo.AllEV(ctx, ast.NS, ast.DB, ast.What.VA) if err != nil { return nil, err } - fd, err := e.dbo.AllFD(ctx, ast.NS, ast.DB, ast.What.TB) + fd, err := e.dbo.AllFD(ctx, ast.NS, ast.DB, ast.What.VA) if err != nil { return nil, err } - ix, err := e.dbo.AllIX(ctx, ast.NS, ast.DB, ast.What.TB) + ix, err := e.dbo.AllIX(ctx, ast.NS, ast.DB, ast.What.VA) if err != nil { return nil, err } - ft, err := e.dbo.AllFT(ctx, ast.NS, ast.DB, ast.What.TB) + ft, err := e.dbo.AllFT(ctx, ast.NS, ast.DB, ast.What.VA) if err != nil { return nil, err } diff --git a/db/info_test.go b/db/info_test.go index 2ba76978..12bd4666 100644 --- a/db/info_test.go +++ b/db/info_test.go @@ -108,6 +108,32 @@ func TestInfo(t *testing.T) { }) + Convey("Info for scope", t, func() { + + setupDB() + + txt := ` + USE NS test DB test; + DEFINE SCOPE test; + DEFINE TOKEN test ON SCOPE test TYPE HS512 VALUE "test"; + INFO FOR SCOPE test; + REMOVE TOKEN test ON SCOPE test; + INFO FOR SCOPE test; + ` + + res, err := Execute(setupKV(), txt, nil) + So(err, ShouldBeNil) + So(res, ShouldHaveLength, 6) + So(res[1].Status, ShouldEqual, "OK") + So(res[2].Status, ShouldEqual, "OK") + So(data.Consume(res[3].Result[0]).Get("token").Data(), ShouldHaveLength, 1) + So(data.Consume(res[3].Result[0]).Get("token.test").Data(), ShouldEqual, "DEFINE TOKEN test ON SCOPE test TYPE HS512 VALUE ********") + So(res[4].Status, ShouldEqual, "OK") + So(res[5].Status, ShouldEqual, "OK") + So(data.Consume(res[5].Result[0]).Get("token").Data(), ShouldHaveLength, 0) + + }) + Convey("Info for table", t, func() { setupDB() diff --git a/sql/ast.go b/sql/ast.go index aa6b2ca2..69d88aa9 100644 --- a/sql/ast.go +++ b/sql/ast.go @@ -86,7 +86,7 @@ type InfoStatement struct { NS string DB string Kind Token - What *Table + What *Ident } // -------------------------------------------------- diff --git a/sql/info.go b/sql/info.go index c771b6cf..a9e8dae4 100644 --- a/sql/info.go +++ b/sql/info.go @@ -22,7 +22,7 @@ func (p *parser) parseInfoStatement() (stmt *InfoStatement, err error) { return nil, err } - if stmt.Kind, _, err = p.shouldBe(NAMESPACE, DATABASE, TABLE); err != nil { + if stmt.Kind, _, err = p.shouldBe(NAMESPACE, DATABASE, SCOPE, TABLE); err != nil { return nil, err } @@ -38,11 +38,20 @@ func (p *parser) parseInfoStatement() (stmt *InfoStatement, err error) { } } + if is(stmt.Kind, SCOPE) { + if stmt.KV, stmt.NS, stmt.DB, err = p.o.get(AuthDB); err != nil { + return nil, err + } + if stmt.What, err = p.parseIdent(); err != nil { + return nil, err + } + } + if is(stmt.Kind, TABLE) { if stmt.KV, stmt.NS, stmt.DB, err = p.o.get(AuthDB); err != nil { return nil, err } - if stmt.What, err = p.parseTable(); err != nil { + if stmt.What, err = p.parseIdent(); err != nil { return nil, err } } diff --git a/sql/sql_test.go b/sql/sql_test.go index 4a5e7093..699b2202 100644 --- a/sql/sql_test.go +++ b/sql/sql_test.go @@ -288,7 +288,7 @@ func Test_Parse_Queries_Info(t *testing.T) { }, { sql: `INFO FOR`, - err: "Found `` but expected `NAMESPACE, DATABASE, TABLE`", + err: "Found `` but expected `NAMESPACE, DATABASE, SCOPE, TABLE`", }, { sql: `INFO FOR NAMESPACE`, @@ -304,16 +304,28 @@ func Test_Parse_Queries_Info(t *testing.T) { Kind: DATABASE, }}}, }, + { + sql: `INFO FOR SCOPE`, + err: "Found `` but expected `name`", + }, + { + sql: `INFO FOR SCOPE test`, + res: &Query{Statements: []Statement{&InfoStatement{ + KV: "*", NS: "*", DB: "*", + Kind: SCOPE, + What: &Ident{"test"}, + }}}, + }, { sql: `INFO FOR TABLE`, - err: "Found `` but expected `table`", + err: "Found `` but expected `name`", }, { sql: `INFO FOR TABLE test`, res: &Query{Statements: []Statement{&InfoStatement{ KV: "*", NS: "*", DB: "*", Kind: TABLE, - What: &Table{"test"}, + What: &Ident{"test"}, }}}, }, { diff --git a/sql/string.go b/sql/string.go index 28d59929..42670eea 100644 --- a/sql/string.go +++ b/sql/string.go @@ -163,6 +163,8 @@ func (this InfoStatement) String() string { return "INFO FOR NAMESPACE" case DATABASE: return "INFO FOR DATABASE" + case SCOPE: + return print("INFO FOR SCOPE %s", this.What) default: return print("INFO FOR TABLE %s", this.What) }