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 (
|
2017-03-03 21:39:37 +00:00
|
|
|
"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
|
|
|
|
2016-11-23 12:53:27 +00:00
|
|
|
switch ast.Kind {
|
2018-10-24 10:24:24 +00:00
|
|
|
case sql.ALL:
|
|
|
|
return e.executeInfoKV(ctx, ast)
|
|
|
|
case sql.NAMESPACE, sql.NS:
|
2017-11-16 20:53:39 +00:00
|
|
|
return e.executeInfoNS(ctx, ast)
|
2018-10-24 10:24:24 +00:00
|
|
|
case sql.DATABASE, sql.DB:
|
2017-11-16 20:53:39 +00:00
|
|
|
return e.executeInfoDB(ctx, ast)
|
2018-09-13 14:23:17 +00:00
|
|
|
case sql.SCOPE:
|
|
|
|
return e.executeInfoSC(ctx, ast)
|
2016-11-23 12:53:27 +00:00
|
|
|
case sql.TABLE:
|
2017-11-16 20:53:39 +00:00
|
|
|
return e.executeInfoTB(ctx, ast)
|
2016-11-23 12:53:27 +00:00
|
|
|
}
|
2016-10-18 13:27:17 +00:00
|
|
|
|
2016-11-23 12:53:27 +00:00
|
|
|
return
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-10-24 10:24:24 +00:00
|
|
|
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
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2017-11-16 20:53:39 +00:00
|
|
|
func (e *executor) executeInfoNS(ctx context.Context, ast *sql.InfoStatement) (out []interface{}, err error) {
|
2016-11-23 12:53:27 +00:00
|
|
|
|
2018-05-02 02:05:03 +00:00
|
|
|
db, err := e.dbo.AllDB(ctx, ast.NS)
|
2017-02-12 11:56:46 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2016-09-19 10:08:44 +00:00
|
|
|
|
2018-05-02 02:05:03 +00:00
|
|
|
nt, err := e.dbo.AllNT(ctx, ast.NS)
|
2017-02-12 11:56:46 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-05-02 02:05:03 +00:00
|
|
|
nu, err := e.dbo.AllNU(ctx, ast.NS)
|
2017-02-12 11:56:46 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2016-11-23 12:53:27 +00:00
|
|
|
|
2017-02-12 11:56:46 +00:00
|
|
|
res := data.New()
|
2016-11-23 12:53:27 +00:00
|
|
|
|
2018-04-24 23:55:49 +00:00
|
|
|
dbase := make(map[string]interface{})
|
2017-02-12 11:56:46 +00:00
|
|
|
for _, v := range db {
|
2018-08-10 18:34:16 +00:00
|
|
|
dbase[v.Name.VA] = v.String()
|
2016-11-23 12:53:27 +00:00
|
|
|
}
|
2016-09-20 23:36:37 +00:00
|
|
|
|
2018-04-24 23:55:49 +00:00
|
|
|
token := make(map[string]interface{})
|
2017-02-12 11:56:46 +00:00
|
|
|
for _, v := range nt {
|
2018-08-10 18:34:16 +00:00
|
|
|
token[v.Name.VA] = v.String()
|
2016-11-23 12:53:27 +00:00
|
|
|
}
|
2016-09-19 10:08:44 +00:00
|
|
|
|
2018-04-24 23:55:49 +00:00
|
|
|
login := make(map[string]interface{})
|
2017-02-12 11:56:46 +00:00
|
|
|
for _, v := range nu {
|
2018-08-10 18:34:16 +00:00
|
|
|
login[v.User.VA] = v.String()
|
2016-11-23 12:53:27 +00:00
|
|
|
}
|
2016-09-19 10:08:44 +00:00
|
|
|
|
2018-04-24 23:55:49 +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
|
|
|
|
2016-11-23 12:53:27 +00:00
|
|
|
}
|
|
|
|
|
2017-11-16 20:53:39 +00:00
|
|
|
func (e *executor) executeInfoDB(ctx context.Context, ast *sql.InfoStatement) (out []interface{}, err error) {
|
2016-11-23 12:53:27 +00:00
|
|
|
|
2018-05-02 02:05:03 +00:00
|
|
|
tb, err := e.dbo.AllTB(ctx, ast.NS, ast.DB)
|
2017-02-12 11:56:46 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2016-11-23 12:53:27 +00:00
|
|
|
|
2018-05-02 02:05:03 +00:00
|
|
|
dt, err := e.dbo.AllDT(ctx, ast.NS, ast.DB)
|
2017-02-12 11:56:46 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2016-09-19 10:08:44 +00:00
|
|
|
|
2018-05-02 02:05:03 +00:00
|
|
|
du, err := e.dbo.AllDU(ctx, ast.NS, ast.DB)
|
2017-11-16 20:53:39 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-05-02 02:05:03 +00:00
|
|
|
sc, err := e.dbo.AllSC(ctx, ast.NS, ast.DB)
|
2017-02-12 11:56:46 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2016-11-23 12:53:27 +00:00
|
|
|
}
|
|
|
|
|
2017-02-12 11:56:46 +00:00
|
|
|
res := data.New()
|
|
|
|
|
2018-04-24 23:55:49 +00:00
|
|
|
table := make(map[string]interface{})
|
2017-02-12 11:56:46 +00:00
|
|
|
for _, v := range tb {
|
2018-08-10 18:34:16 +00:00
|
|
|
table[v.Name.VA] = v.String()
|
2016-11-23 12:53:27 +00:00
|
|
|
}
|
|
|
|
|
2018-04-24 23:55:49 +00:00
|
|
|
token := make(map[string]interface{})
|
2017-02-12 11:56:46 +00:00
|
|
|
for _, v := range dt {
|
2018-08-10 18:34:16 +00:00
|
|
|
token[v.Name.VA] = v.String()
|
2016-11-23 12:53:27 +00:00
|
|
|
}
|
|
|
|
|
2018-04-24 23:55:49 +00:00
|
|
|
login := make(map[string]interface{})
|
2017-02-12 11:56:46 +00:00
|
|
|
for _, v := range du {
|
2018-08-10 18:34:16 +00:00
|
|
|
login[v.User.VA] = v.String()
|
2016-11-23 12:53:27 +00:00
|
|
|
}
|
|
|
|
|
2018-04-24 23:55:49 +00:00
|
|
|
scope := make(map[string]interface{})
|
2017-11-16 20:53:39 +00:00
|
|
|
for _, v := range sc {
|
2018-08-10 18:34:16 +00:00
|
|
|
scope[v.Name.VA] = v.String()
|
2017-11-16 20:53:39 +00:00
|
|
|
}
|
2016-11-23 12:53:27 +00:00
|
|
|
|
2018-04-24 23:55:49 +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
|
2016-11-23 12:53:27 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-09-13 14:23:17 +00:00
|
|
|
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
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2017-11-16 20:53:39 +00:00
|
|
|
func (e *executor) executeInfoTB(ctx context.Context, ast *sql.InfoStatement) (out []interface{}, err error) {
|
2016-11-23 12:53:27 +00:00
|
|
|
|
2018-09-13 14:23:17 +00:00
|
|
|
ev, err := e.dbo.AllEV(ctx, ast.NS, ast.DB, ast.What.VA)
|
2017-02-12 11:56:46 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2016-09-19 10:08:44 +00:00
|
|
|
|
2018-09-13 14:23:17 +00:00
|
|
|
fd, err := e.dbo.AllFD(ctx, ast.NS, ast.DB, ast.What.VA)
|
2017-02-12 11:56:46 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2016-09-19 10:08:44 +00:00
|
|
|
|
2018-09-13 14:23:17 +00:00
|
|
|
ix, err := e.dbo.AllIX(ctx, ast.NS, ast.DB, ast.What.VA)
|
2017-02-12 11:56:46 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2016-09-19 10:08:44 +00:00
|
|
|
}
|
|
|
|
|
2018-09-13 14:23:17 +00:00
|
|
|
ft, err := e.dbo.AllFT(ctx, ast.NS, ast.DB, ast.What.VA)
|
2018-04-28 21:08:34 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2017-02-12 11:56:46 +00:00
|
|
|
res := data.New()
|
|
|
|
|
2018-04-24 23:55:49 +00:00
|
|
|
event := make(map[string]interface{})
|
2017-11-16 20:53:39 +00:00
|
|
|
for _, v := range ev {
|
2018-08-10 18:34:16 +00:00
|
|
|
event[v.Name.VA] = v.String()
|
2016-11-23 12:53:27 +00:00
|
|
|
}
|
|
|
|
|
2018-04-24 23:55:49 +00:00
|
|
|
field := make(map[string]interface{})
|
2017-02-12 11:56:46 +00:00
|
|
|
for _, v := range fd {
|
2018-08-10 18:34:16 +00:00
|
|
|
field[v.Name.VA] = v.String()
|
2017-11-16 20:53:39 +00:00
|
|
|
}
|
2016-11-23 12:53:27 +00:00
|
|
|
|
2018-04-24 23:55:49 +00:00
|
|
|
index := make(map[string]interface{})
|
2017-11-16 20:53:39 +00:00
|
|
|
for _, v := range ix {
|
2018-08-10 18:34:16 +00:00
|
|
|
index[v.Name.VA] = v.String()
|
2018-04-24 23:55:49 +00:00
|
|
|
}
|
|
|
|
|
2018-04-28 21:08:34 +00:00
|
|
|
table := make(map[string]interface{})
|
|
|
|
for _, v := range ft {
|
2018-08-10 18:34:16 +00:00
|
|
|
table[v.Name.VA] = v.String()
|
2018-04-28 21:08:34 +00:00
|
|
|
}
|
|
|
|
|
2018-04-24 23:55:49 +00:00
|
|
|
res.Set(event, "event")
|
|
|
|
res.Set(field, "field")
|
|
|
|
res.Set(index, "index")
|
2018-04-28 21:08:34 +00:00
|
|
|
res.Set(table, "table")
|
2018-04-24 23:55:49 +00:00
|
|
|
|
2017-11-16 20:53:39 +00:00
|
|
|
return []interface{}{res.Data()}, nil
|
2016-09-19 10:08:44 +00:00
|
|
|
|
|
|
|
}
|