surrealpatch/db/info.go

186 lines
3.7 KiB
Go
Raw Normal View History

2016-09-19 10:08:44 +00:00
// Copyright © 2016 Abcum Ltd
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package db
import (
"context"
2016-09-19 10:08:44 +00:00
"github.com/abcum/surreal/sql"
"github.com/abcum/surreal/util/data"
)
2017-11-16 20:53:39 +00:00
func (e *executor) executeInfo(ctx context.Context, ast *sql.InfoStatement) (out []interface{}, err error) {
2016-09-19 10:08:44 +00:00
switch ast.Kind {
case sql.NAMESPACE:
2017-11-16 20:53:39 +00:00
return e.executeInfoNS(ctx, ast)
case sql.DATABASE:
2017-11-16 20:53:39 +00:00
return e.executeInfoDB(ctx, ast)
case sql.TABLE:
2017-11-16 20:53:39 +00:00
return e.executeInfoTB(ctx, ast)
}
2016-10-18 13:27:17 +00:00
return
}
2017-11-16 20:53:39 +00:00
func (e *executor) executeInfoNS(ctx context.Context, ast *sql.InfoStatement) (out []interface{}, err error) {
2017-11-16 20:53:39 +00:00
db, err := e.dbo.AllDB(ast.NS)
if err != nil {
return nil, err
}
2016-09-19 10:08:44 +00:00
2017-11-16 20:53:39 +00:00
nt, err := e.dbo.AllNT(ast.NS)
if err != nil {
return nil, err
}
2017-11-16 20:53:39 +00:00
nu, err := e.dbo.AllNU(ast.NS)
if err != nil {
return nil, err
}
res := data.New()
dbase := make(map[string]interface{})
for _, v := range db {
dbase[v.Name.ID] = v.String()
}
2016-09-20 23:36:37 +00:00
token := make(map[string]interface{})
for _, v := range nt {
token[v.Name.ID] = v.String()
}
2016-09-19 10:08:44 +00:00
login := make(map[string]interface{})
for _, v := range nu {
login[v.User.ID] = v.String()
}
2016-09-19 10:08:44 +00:00
res.Set(dbase, "database")
res.Set(token, "token")
res.Set(login, "login")
2017-11-16 20:53:39 +00:00
return []interface{}{res.Data()}, nil
2016-09-19 10:08:44 +00:00
}
2017-11-16 20:53:39 +00:00
func (e *executor) executeInfoDB(ctx context.Context, ast *sql.InfoStatement) (out []interface{}, err error) {
2017-11-16 20:53:39 +00:00
tb, err := e.dbo.AllTB(ast.NS, ast.DB)
if err != nil {
return nil, err
}
2017-11-16 20:53:39 +00:00
dt, err := e.dbo.AllDT(ast.NS, ast.DB)
if err != nil {
return nil, err
}
2016-09-19 10:08:44 +00:00
2017-11-16 20:53:39 +00:00
du, err := e.dbo.AllDU(ast.NS, ast.DB)
if err != nil {
return nil, err
}
sc, err := e.dbo.AllSC(ast.NS, ast.DB)
if err != nil {
return nil, err
}
res := data.New()
table := make(map[string]interface{})
for _, v := range tb {
table[v.Name.ID] = v.String()
}
token := make(map[string]interface{})
for _, v := range dt {
token[v.Name.ID] = v.String()
}
login := make(map[string]interface{})
for _, v := range du {
login[v.User.ID] = v.String()
}
scope := make(map[string]interface{})
2017-11-16 20:53:39 +00:00
for _, v := range sc {
scope[v.Name.ID] = v.String()
2017-11-16 20:53:39 +00:00
}
res.Set(table, "table")
res.Set(token, "token")
res.Set(login, "login")
res.Set(scope, "scope")
2017-11-16 20:53:39 +00:00
return []interface{}{res.Data()}, nil
}
2017-11-16 20:53:39 +00:00
func (e *executor) executeInfoTB(ctx context.Context, ast *sql.InfoStatement) (out []interface{}, err error) {
2017-11-16 20:53:39 +00:00
ev, err := e.dbo.AllEV(ast.NS, ast.DB, ast.What.TB)
if err != nil {
return nil, err
}
2016-09-19 10:08:44 +00:00
2017-11-16 20:53:39 +00:00
fd, err := e.dbo.AllFD(ast.NS, ast.DB, ast.What.TB)
if err != nil {
return nil, err
}
2016-09-19 10:08:44 +00:00
2017-11-16 20:53:39 +00:00
ix, err := e.dbo.AllIX(ast.NS, ast.DB, ast.What.TB)
if err != nil {
return nil, err
2016-09-19 10:08:44 +00:00
}
lv, err := e.dbo.AllLV(ast.NS, ast.DB, ast.What.TB)
if err != nil {
return nil, err
}
res := data.New()
event := make(map[string]interface{})
2017-11-16 20:53:39 +00:00
for _, v := range ev {
event[v.Name.ID] = v.String()
}
field := make(map[string]interface{})
for _, v := range fd {
field[v.Name.ID] = v.String()
2017-11-16 20:53:39 +00:00
}
index := make(map[string]interface{})
2017-11-16 20:53:39 +00:00
for _, v := range ix {
index[v.Name.ID] = v.String()
}
lives := make(map[string]interface{})
for _, v := range lv {
lives[v.ID] = v.String()
2017-11-16 20:53:39 +00:00
}
res.Set(event, "event")
res.Set(field, "field")
res.Set(index, "index")
res.Set(lives, "lives")
2017-11-16 20:53:39 +00:00
return []interface{}{res.Data()}, nil
2016-09-19 10:08:44 +00:00
}