Add scope tokens to SQL INFO queries

This commit is contained in:
Tobie Morgan Hitchcock 2018-09-13 15:23:17 +01:00
parent 6ed01504ec
commit 41c2fd0b3e
6 changed files with 81 additions and 10 deletions

View file

@ -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
}

View file

@ -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()

View file

@ -86,7 +86,7 @@ type InfoStatement struct {
NS string
DB string
Kind Token
What *Table
What *Ident
}
// --------------------------------------------------

View file

@ -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
}
}

View file

@ -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"},
}}},
},
{

View file

@ -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)
}