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 all commits
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{}
5 changes: 3 additions & 2 deletions controllers/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,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,7 +85,7 @@ 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, err := dao.Execute(nsid.(string), params.Gql, params.ParamList)
if err == nil {
res.Code = 0
res.Data = &result
Expand Down
42 changes: 25 additions & 17 deletions service/dao/dao.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,14 @@ import (
"github.com/vesoft-inc/nebula-http-gateway/service/pool"

nebula "github.com/vesoft-inc/nebula-go/v2"
nebulaType "github.com/vesoft-inc/nebula-go/v2/nebula"
nebulatype "github.com/vesoft-inc/nebula-go/v2/nebula"
)

type ExecuteResult struct {
Headers []string `json:"headers"`
Tables []map[string]common.Any `json:"tables"`
TimeCost int64 `json:"timeCost"`
Headers []string `json:"headers"`
Tables []map[string]common.Any `json:"tables"`
TimeCost int64 `json:"timeCost"`
LocalParams common.ParameterMap `json:"localParams"`
}

type list []common.Any
Expand Down Expand Up @@ -44,21 +45,21 @@ func getBasicValue(valWarp *nebula.ValueWrapper) (common.Any, error) {
if valType == "null" {
value, err := valWarp.AsNull()
switch value {
case nebulaType.NullType___NULL__:
case nebulatype.NullType___NULL__:
return "NULL", err
case nebulaType.NullType_NaN:
case nebulatype.NullType_NaN:
return "NaN", err
case nebulaType.NullType_BAD_DATA:
case nebulatype.NullType_BAD_DATA:
return "BAD_DATA", err
case nebulaType.NullType_BAD_TYPE:
case nebulatype.NullType_BAD_TYPE:
return "BAD_TYPE", err
case nebulaType.NullType_OUT_OF_RANGE:
case nebulatype.NullType_OUT_OF_RANGE:
return "OUT_OF_RANGE", err
case nebulaType.NullType_DIV_BY_ZERO:
case nebulatype.NullType_DIV_BY_ZERO:
return "DIV_BY_ZERO", err
case nebulaType.NullType_UNKNOWN_PROP:
case nebulatype.NullType_UNKNOWN_PROP:
return "UNKNOWN_PROP", err
case nebulaType.NullType_ERR_OVERFLOW:
case nebulatype.NullType_ERR_OVERFLOW:
return "ERR_OVERFLOW", err
}
return "NULL", err
Expand Down Expand Up @@ -287,26 +288,34 @@ 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, err error) {
result = ExecuteResult{
Headers: make([]string, 0),
Tables: make([]map[string]common.Any, 0),
Headers: make([]string, 0),
Tables: make([]map[string]common.Any, 0),
LocalParams: nil,
}
connection, err := pool.GetConnection(nsid)
if err != nil {
return result, err
}

responseChannel := make(chan pool.ChannelResponse)
connection.RequestChannel <- pool.ChannelRequest{
Gql: gql,
ResponseChannel: responseChannel,
ParamList: paramList,
}
response := <-responseChannel
paramsMap := response.Params
if len(paramsMap) > 0 {
result.LocalParams = paramsMap
}
if response.Error != nil {
return result, response.Error
}
resp := response.Result
if response.Result == nil {
return result, nil
}
if resp.IsSetPlanDesc() {
format := string(resp.GetPlanDesc().GetFormat())
if format == "row" {
Expand Down Expand Up @@ -334,7 +343,6 @@ func Execute(nsid string, gql string) (result ExecuteResult, err error) {
return result, err
}
}

if !resp.IsSucceed() {
logs.Info("ErrorCode: %v, ErrorMsg: %s", resp.GetErrorCode(), resp.GetErrorMsg())
return result, errors.New(string(resp.GetErrorMsg()))
Expand Down
Loading