Improve SQL INFO command
This commit is contained in:
parent
27f77e8e5d
commit
30639a1ae9
5 changed files with 63 additions and 8 deletions
26
db/info.go
26
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)
|
||||
|
|
12
sql/info.go
12
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
|
||||
}
|
||||
|
|
|
@ -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`",
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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",
|
||||
|
|
Loading…
Reference in a new issue