From 30639a1ae953b9d34add71cf976b796ad4b347f0 Mon Sep 17 00:00:00 2001 From: Tobie Morgan Hitchcock Date: Wed, 24 Oct 2018 11:24:24 +0100 Subject: [PATCH] Improve SQL INFO command --- db/info.go | 26 ++++++++++++++++++++++++-- sql/info.go | 12 +++++++++--- sql/sql_test.go | 25 ++++++++++++++++++++++++- sql/string.go | 6 ++++-- sql/tokens.go | 2 ++ 5 files changed, 63 insertions(+), 8 deletions(-) diff --git a/db/info.go b/db/info.go index 9f365516..769bdec7 100644 --- a/db/info.go +++ b/db/info.go @@ -24,9 +24,11 @@ import ( func (e *executor) executeInfo(ctx context.Context, ast *sql.InfoStatement) (out []interface{}, err error) { switch ast.Kind { - case sql.NAMESPACE: + case sql.ALL: + return e.executeInfoKV(ctx, ast) + case sql.NAMESPACE, sql.NS: return e.executeInfoNS(ctx, ast) - case sql.DATABASE: + case sql.DATABASE, sql.DB: return e.executeInfoDB(ctx, ast) case sql.SCOPE: return e.executeInfoSC(ctx, ast) @@ -38,6 +40,26 @@ func (e *executor) executeInfo(ctx context.Context, ast *sql.InfoStatement) (out } +func (e *executor) executeInfoKV(ctx context.Context, ast *sql.InfoStatement) (out []interface{}, err error) { + + ns, err := e.dbo.AllNS(ctx) + if err != nil { + return nil, err + } + + res := data.New() + + nspac := make(map[string]interface{}) + for _, v := range ns { + nspac[v.Name.VA] = v.String() + } + + res.Set(nspac, "namespace") + + return []interface{}{res.Data()}, nil + +} + func (e *executor) executeInfoNS(ctx context.Context, ast *sql.InfoStatement) (out []interface{}, err error) { db, err := e.dbo.AllDB(ctx, ast.NS) diff --git a/sql/info.go b/sql/info.go index a9e8dae4..6044c186 100644 --- a/sql/info.go +++ b/sql/info.go @@ -22,17 +22,23 @@ func (p *parser) parseInfoStatement() (stmt *InfoStatement, err error) { return nil, err } - if stmt.Kind, _, err = p.shouldBe(NAMESPACE, DATABASE, SCOPE, TABLE); err != nil { + if stmt.Kind, _, err = p.shouldBe(ALL, NAMESPACE, DATABASE, SCOPE, TABLE, NS, DB); err != nil { return nil, err } - if is(stmt.Kind, NAMESPACE) { + if is(stmt.Kind, ALL) { + if stmt.KV, stmt.NS, stmt.DB, err = p.o.get(AuthKV); err != nil { + return nil, err + } + } + + if is(stmt.Kind, NAMESPACE, NS) { if stmt.KV, stmt.NS, stmt.DB, err = p.o.get(AuthNS); err != nil { return nil, err } } - if is(stmt.Kind, DATABASE) { + if is(stmt.Kind, DATABASE, DB) { if stmt.KV, stmt.NS, stmt.DB, err = p.o.get(AuthDB); err != nil { return nil, err } diff --git a/sql/sql_test.go b/sql/sql_test.go index 6ba1b9e6..603ff918 100644 --- a/sql/sql_test.go +++ b/sql/sql_test.go @@ -288,7 +288,14 @@ func Test_Parse_Queries_Info(t *testing.T) { }, { sql: `INFO FOR`, - err: "Found `` but expected `NAMESPACE, DATABASE, SCOPE, TABLE`", + err: "Found `` but expected `ALL, NAMESPACE, DATABASE, SCOPE, TABLE, NS, DB`", + }, + { + sql: `INFO FOR ALL`, + res: &Query{Statements: []Statement{&InfoStatement{ + KV: "*", NS: "*", DB: "*", + Kind: ALL, + }}}, }, { sql: `INFO FOR NAMESPACE`, @@ -297,6 +304,14 @@ func Test_Parse_Queries_Info(t *testing.T) { Kind: NAMESPACE, }}}, }, + { + sql: `INFO FOR NS`, + str: `INFO FOR NAMESPACE`, + res: &Query{Statements: []Statement{&InfoStatement{ + KV: "*", NS: "*", DB: "*", + Kind: NS, + }}}, + }, { sql: `INFO FOR DATABASE`, res: &Query{Statements: []Statement{&InfoStatement{ @@ -304,6 +319,14 @@ func Test_Parse_Queries_Info(t *testing.T) { Kind: DATABASE, }}}, }, + { + sql: `INFO FOR DB`, + str: `INFO FOR DATABASE`, + res: &Query{Statements: []Statement{&InfoStatement{ + KV: "*", NS: "*", DB: "*", + Kind: DB, + }}}, + }, { sql: `INFO FOR SCOPE`, err: "Found `` but expected `name`", diff --git a/sql/string.go b/sql/string.go index e5979686..bba8a338 100644 --- a/sql/string.go +++ b/sql/string.go @@ -165,9 +165,11 @@ func (this UseStatement) String() string { func (this InfoStatement) String() string { switch this.Kind { - case NAMESPACE: + case ALL: + return "INFO FOR ALL" + case NAMESPACE, NS: return "INFO FOR NAMESPACE" - case DATABASE: + case DATABASE, DB: return "INFO FOR DATABASE" case SCOPE: return print("INFO FOR SCOPE %s", this.What) diff --git a/sql/tokens.go b/sql/tokens.go index ce0d931d..1bc12716 100644 --- a/sql/tokens.go +++ b/sql/tokens.go @@ -99,6 +99,7 @@ const ( keywordsBeg AFTER + ALL ALLCONTAINEDIN AND AS @@ -275,6 +276,7 @@ var tokens = [...]string{ // keywords AFTER: "AFTER", + ALL: "ALL", ALLCONTAINEDIN: "ALLCONTAINEDIN", AND: "AND", AS: "AS",