Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat:support cypher parameter #72

Merged
merged 3 commits into from
Dec 30, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The default value of res.Params is nil, need not to assign with nil.

if len(paramsMap) > 0 {
    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) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not put params into ExecuteResult?

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