Skip to content

Commit

Permalink
feat:support cypher parameter
Browse files Browse the repository at this point in the history
refactor: remove vscode file

update: fix typo value

mod: update local cmd

mod: update params

mod: support params

mod: add type
  • Loading branch information
xigongdaEricyang authored and hetao92 committed Dec 29, 2021
1 parent bcfc850 commit 3efe157
Show file tree
Hide file tree
Showing 5 changed files with 283 additions and 25 deletions.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ nebula-httpd
.idea
.vscode/
vendor/

# Dependency directories (remove the comment below to include it)
tmp/

Expand Down
3 changes: 3 additions & 0 deletions common/common.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
package common

type Any interface{}

type ParameterList []string
type ParameterMap map[string]interface{}
17 changes: 12 additions & 5 deletions controllers/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@ type DatabaseController struct {
}

type Response struct {
Code int `json:"code"`
Data common.Any `json:"data"`
Message string `json:"message"`
Code int `json:"code"`
Data common.Any `json:"data"`
Message string `json:"message"`
Params common.ParameterMap `json:"params"`
}

type Request struct {
Expand All @@ -26,7 +27,8 @@ type Request struct {
}

type ExecuteRequest struct {
Gql string `json:"gql"`
Gql string `json:"gql"`
ParamList common.ParameterList `json:"paramList"`
}

type Data map[string]interface{}
Expand Down Expand Up @@ -84,14 +86,19 @@ func (this *DatabaseController) Execute() {
res.Message = "connection refused for lack of session"
} else {
json.Unmarshal(this.Ctx.Input.RequestBody, &params)
result, err := dao.Execute(nsid.(string), params.Gql)
result, paramsMap, err := dao.Execute(nsid.(string), params.Gql, params.ParamList)
if err == nil {
res.Code = 0
res.Data = &result
} else {
res.Code = -1
res.Message = err.Error()
}
if len(paramsMap) == 0 {
res.Params = nil
} else {
res.Params = paramsMap
}
}
this.Data["json"] = &res
this.ServeJSON()
Expand Down
29 changes: 16 additions & 13 deletions service/dao/dao.go
Original file line number Diff line number Diff line change
Expand Up @@ -287,26 +287,30 @@ func Disconnect(nsid string) {
pool.Disconnect(nsid)
}

func Execute(nsid string, gql string) (result ExecuteResult, err error) {
func Execute(nsid string, gql string, paramList common.ParameterList) (result ExecuteResult, params common.ParameterMap, err error) {
result = ExecuteResult{
Headers: make([]string, 0),
Tables: make([]map[string]common.Any, 0),
}
connection, err := pool.GetConnection(nsid)
if err != nil {
return result, err
return result, nil, err
}

responseChannel := make(chan pool.ChannelResponse)
connection.RequestChannel <- pool.ChannelRequest{
Gql: gql,
ResponseChannel: responseChannel,
ParamList: paramList,
}
response := <-responseChannel
paramsMap := response.Params
if response.Error != nil {
return result, response.Error
return result, paramsMap, response.Error
}
resp := response.Result
if response.Result == nil {
return result, paramsMap, nil
}
if resp.IsSetPlanDesc() {
format := string(resp.GetPlanDesc().GetFormat())
if format == "row" {
Expand All @@ -321,7 +325,7 @@ func Execute(nsid string, gql string) (result ExecuteResult, err error) {
rowValue["operator info"] = rows[i][4]
result.Tables = append(result.Tables, rowValue)
}
return result, err
return result, paramsMap, err
} else {
var rowValue = make(map[string]common.Any)
result.Headers = append(result.Headers, "format")
Expand All @@ -331,13 +335,12 @@ func Execute(nsid string, gql string) (result ExecuteResult, err error) {
rowValue["format"] = resp.MakeDotGraphByStruct()
}
result.Tables = append(result.Tables, rowValue)
return result, err
return result, paramsMap, err
}
}

if !resp.IsSucceed() {
logs.Info("ErrorCode: %v, ErrorMsg: %s", resp.GetErrorCode(), resp.GetErrorMsg())
return result, errors.New(string(resp.GetErrorMsg()))
return result, paramsMap, errors.New(string(resp.GetErrorMsg()))
}
if !resp.IsEmpty() {
rowSize := resp.GetRowSize()
Expand All @@ -351,16 +354,16 @@ func Execute(nsid string, gql string) (result ExecuteResult, err error) {
var _edgesParsedList = make(list, 0)
var _pathsParsedList = make(list, 0)
if err != nil {
return result, err
return result, paramsMap, err
}
for j := 0; j < colSize; j++ {
rowData, err := record.GetValueByIndex(j)
if err != nil {
return result, err
return result, paramsMap, err
}
value, err := getValue(rowData)
if err != nil {
return result, err
return result, paramsMap, err
}
rowValue[result.Headers[j]] = value
valueType := rowData.GetType()
Expand Down Expand Up @@ -396,12 +399,12 @@ func Execute(nsid string, gql string) (result ExecuteResult, err error) {
rowValue["_pathsParsedList"] = _pathsParsedList
}
if err != nil {
return result, err
return result, paramsMap, err
}
}
result.Tables = append(result.Tables, rowValue)
}
}
result.TimeCost = resp.GetLatency()
return result, nil
return result, paramsMap, nil
}
Loading

0 comments on commit 3efe157

Please sign in to comment.