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) {
|
func (e *executor) executeInfo(ctx context.Context, ast *sql.InfoStatement) (out []interface{}, err error) {
|
||||||
|
|
||||||
switch ast.Kind {
|
switch ast.Kind {
|
||||||
case sql.NAMESPACE:
|
case sql.ALL:
|
||||||
|
return e.executeInfoKV(ctx, ast)
|
||||||
|
case sql.NAMESPACE, sql.NS:
|
||||||
return e.executeInfoNS(ctx, ast)
|
return e.executeInfoNS(ctx, ast)
|
||||||
case sql.DATABASE:
|
case sql.DATABASE, sql.DB:
|
||||||
return e.executeInfoDB(ctx, ast)
|
return e.executeInfoDB(ctx, ast)
|
||||||
case sql.SCOPE:
|
case sql.SCOPE:
|
||||||
return e.executeInfoSC(ctx, ast)
|
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) {
|
func (e *executor) executeInfoNS(ctx context.Context, ast *sql.InfoStatement) (out []interface{}, err error) {
|
||||||
|
|
||||||
db, err := e.dbo.AllDB(ctx, ast.NS)
|
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
|
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
|
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 {
|
if stmt.KV, stmt.NS, stmt.DB, err = p.o.get(AuthNS); err != nil {
|
||||||
return nil, err
|
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 {
|
if stmt.KV, stmt.NS, stmt.DB, err = p.o.get(AuthDB); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
|
@ -288,7 +288,14 @@ func Test_Parse_Queries_Info(t *testing.T) {
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
sql: `INFO FOR`,
|
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`,
|
sql: `INFO FOR NAMESPACE`,
|
||||||
|
@ -297,6 +304,14 @@ func Test_Parse_Queries_Info(t *testing.T) {
|
||||||
Kind: NAMESPACE,
|
Kind: NAMESPACE,
|
||||||
}}},
|
}}},
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
sql: `INFO FOR NS`,
|
||||||
|
str: `INFO FOR NAMESPACE`,
|
||||||
|
res: &Query{Statements: []Statement{&InfoStatement{
|
||||||
|
KV: "*", NS: "*", DB: "*",
|
||||||
|
Kind: NS,
|
||||||
|
}}},
|
||||||
|
},
|
||||||
{
|
{
|
||||||
sql: `INFO FOR DATABASE`,
|
sql: `INFO FOR DATABASE`,
|
||||||
res: &Query{Statements: []Statement{&InfoStatement{
|
res: &Query{Statements: []Statement{&InfoStatement{
|
||||||
|
@ -304,6 +319,14 @@ func Test_Parse_Queries_Info(t *testing.T) {
|
||||||
Kind: DATABASE,
|
Kind: DATABASE,
|
||||||
}}},
|
}}},
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
sql: `INFO FOR DB`,
|
||||||
|
str: `INFO FOR DATABASE`,
|
||||||
|
res: &Query{Statements: []Statement{&InfoStatement{
|
||||||
|
KV: "*", NS: "*", DB: "*",
|
||||||
|
Kind: DB,
|
||||||
|
}}},
|
||||||
|
},
|
||||||
{
|
{
|
||||||
sql: `INFO FOR SCOPE`,
|
sql: `INFO FOR SCOPE`,
|
||||||
err: "Found `` but expected `name`",
|
err: "Found `` but expected `name`",
|
||||||
|
|
|
@ -165,9 +165,11 @@ func (this UseStatement) String() string {
|
||||||
|
|
||||||
func (this InfoStatement) String() string {
|
func (this InfoStatement) String() string {
|
||||||
switch this.Kind {
|
switch this.Kind {
|
||||||
case NAMESPACE:
|
case ALL:
|
||||||
|
return "INFO FOR ALL"
|
||||||
|
case NAMESPACE, NS:
|
||||||
return "INFO FOR NAMESPACE"
|
return "INFO FOR NAMESPACE"
|
||||||
case DATABASE:
|
case DATABASE, DB:
|
||||||
return "INFO FOR DATABASE"
|
return "INFO FOR DATABASE"
|
||||||
case SCOPE:
|
case SCOPE:
|
||||||
return print("INFO FOR SCOPE %s", this.What)
|
return print("INFO FOR SCOPE %s", this.What)
|
||||||
|
|
|
@ -99,6 +99,7 @@ const (
|
||||||
keywordsBeg
|
keywordsBeg
|
||||||
|
|
||||||
AFTER
|
AFTER
|
||||||
|
ALL
|
||||||
ALLCONTAINEDIN
|
ALLCONTAINEDIN
|
||||||
AND
|
AND
|
||||||
AS
|
AS
|
||||||
|
@ -275,6 +276,7 @@ var tokens = [...]string{
|
||||||
// keywords
|
// keywords
|
||||||
|
|
||||||
AFTER: "AFTER",
|
AFTER: "AFTER",
|
||||||
|
ALL: "ALL",
|
||||||
ALLCONTAINEDIN: "ALLCONTAINEDIN",
|
ALLCONTAINEDIN: "ALLCONTAINEDIN",
|
||||||
AND: "AND",
|
AND: "AND",
|
||||||
AS: "AS",
|
AS: "AS",
|
||||||
|
|
Loading…
Reference in a new issue