Skip to content

Commit

Permalink
support cypher parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
czpmango committed Sep 2, 2021
1 parent 3b3ec6d commit 76e3879
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 23 deletions.
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,5 @@ require (
github.com/vesoft-inc/nebula-go/v2 v2.0.0-20210701060243-a0577f67f375
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68 // indirect
)

replace github.com/vesoft-inc/nebula-go/v2 => /home/kyle.cao/workspace/fork/nebula-go
17 changes: 11 additions & 6 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ github.com/jedib0t/go-pretty/v6 v6.0.5 h1:oOo0/jSb3NEYKT6l1hhFXoX2UZnkanMuCE2DVT
github.com/jedib0t/go-pretty/v6 v6.0.5/go.mod h1:MTr6FgcfNdnN5wPVBzJ6mhJeDyiF0yBvS2TMXEV/XSU=
github.com/jievince/liner v1.2.3 h1:hpMEqBKkIg/RHzHCHZqbs19MDq8bT8b5o85D3o/Yggk=
github.com/jievince/liner v1.2.3/go.mod h1:6szfFB+ea00sIHdOn/4gDFoD6sa2UaUHR28Ca8QGj9U=
github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI=
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/mattn/go-runewidth v0.0.3/go.mod h1:LwmH8dsx7+W8Uxz3IHJYH5QSwggIsqBzpuz5H//U1FU=
github.com/mattn/go-runewidth v0.0.9 h1:Lm995f3rfxdpd6TSmuVCHVb/QhupuXlYr8sCI/QdE+0=
github.com/mattn/go-runewidth v0.0.9/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m2gUSrubnMI=
Expand All @@ -15,14 +20,14 @@ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZb
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.6.1 h1:hDPOHmpOpP40lSULcqw7IrRb/u7w6RpDC9399XyoNd0=
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/vesoft-inc/nebula-go/v2 v2.0.0-20210701060243-a0577f67f375 h1:7us19i0qiJffpnKeWMQTPxAjaYItR8xTVohSa3JZVqk=
github.com/vesoft-inc/nebula-go/v2 v2.0.0-20210701060243-a0577f67f375/go.mod h1:B7nR6+nOSo0umq/HkCmUfyRtYrJVOsNiPS9u4djDbSc=
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
golang.org/x/sys v0.0.0-20180816055513-1c9583448a9c/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68 h1:nxC68pudNYkKU6jWhgrqdreuFiOQWj1Fs7T3VrH4Pjw=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 h1:qIbj1fsPNlZgppZ+VLlY7N33q108Sa+fhmuc+sWQYwY=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b h1:h8qDotaEPuJATrMmW04NCwg7v22aHH28wwpauUhK9Oo=
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
80 changes: 63 additions & 17 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
package main

import (
"encoding/json"
"flag"
"fmt"
"log"
Expand All @@ -21,7 +22,8 @@ import (
"github.com/vesoft-inc/nebula-console/box"
"github.com/vesoft-inc/nebula-console/cli"
"github.com/vesoft-inc/nebula-console/printer"
nebula "github.com/vesoft-inc/nebula-go/v2"
nebulago "github.com/vesoft-inc/nebula-go/v2"
nebula "github.com/vesoft-inc/nebula-go/v2/nebula"
)

// Console side commands
Expand All @@ -37,8 +39,7 @@ const (
Params = 7
)

type Any interface{}
type ParameterMap map[string]Any
type ParameterMap map[string]interface{}

var parameterMap ParameterMap

Expand Down Expand Up @@ -208,12 +209,19 @@ func executeConsoleCmd(c cli.Cli, cmd int, args []string) {
if len(res) != 1 || len(res[0]) != 3 {
return
}
k, v := res[0][1], res[0][2]

parameterMap[k] = v
tmp := make(ParameterMap)
param := "{\"" + res[0][1] + "\"" + ":" + res[0][2] + "}"
err := json.Unmarshal([]byte(param), &tmp)
if err != nil {
printConsoleResp("Error: parameter parsing failed")
return
}
for k, v := range tmp {
parameterMap[k] = v
}

case Params:
// TODO: print params (czp)
for k := range parameterMap {
fmt.Println(k, " => ", parameterMap[k])
}
Expand All @@ -222,7 +230,7 @@ func executeConsoleCmd(c cli.Cli, cmd int, args []string) {
}
}

func printResultSet(res *nebula.ResultSet, startTime time.Time) (duration time.Duration) {
func printResultSet(res *nebulago.ResultSet, startTime time.Time) (duration time.Duration) {
if !res.IsSucceed() && !res.IsPartialSucceed() {
fmt.Printf("[ERROR (%d)]: %s", res.GetErrorCode(), res.GetErrorMsg())
fmt.Println()
Expand Down Expand Up @@ -294,7 +302,18 @@ func loop(c cli.Cli) error {
var t2 int32 = 0
for i := 0; i < g_repeats; i++ {
start := time.Now()
res, err := session.Execute(line)
// convert interface{} to nebula.Value
params := make(map[string]*nebula.Value)
for k, v := range parameterMap {
value, err := Value(v)
if err != nil {
printConsoleResp(err.Error())
return err
}
params[k] = value
}

res, err := session.Execute(line, params)
if err != nil {
return err
}
Expand Down Expand Up @@ -360,16 +379,43 @@ func validateFlags() {
}
}

var pool *nebula.ConnectionPool
// construct go-type to nebula.Value
func Value(any interface{}) (value *nebula.Value, err error) {
value = nebula.NewValue()
if v, ok := any.(int); ok {
ival := int64(v)
value.IVal = &ival
return
}
if v, ok := any.(bool); ok {
value.BVal = &v
return
}
if v, ok := any.(float64); ok {
value.FVal = &v
return
}
if v, ok := any.(float32); ok {
fval := float64(v)
value.FVal = &fval
return
}
if v, ok := any.(string); ok {
value.SVal = []byte(v)
return
}
// unsupport other Value type, use this function carefully
err = fmt.Errorf("Do not support convert %T to nebula.Value", any)
return
}

var pool *nebulago.ConnectionPool

var session *nebula.Session
var session *nebulago.Session

func main() {
flag.Parse()
parameterMap = make(ParameterMap)
fmt.Println("czp map:")
fmt.Println(parameterMap["p1"])
fmt.Println(parameterMap["p2"])

if flag.NFlag() == 1 && *version {
fmt.Printf("nebula-console version Git: %s, Build Time: %s\n", gitCommit, buildDate)
Expand All @@ -390,16 +436,16 @@ func main() {
historyHome = filepath.Dir(ex) // Set to executable folder
}

hostAddress := nebula.HostAddress{Host: *address, Port: *port}
hostList := []nebula.HostAddress{hostAddress}
poolConfig := nebula.PoolConfig{
hostAddress := nebulago.HostAddress{Host: *address, Port: *port}
hostList := []nebulago.HostAddress{hostAddress}
poolConfig := nebulago.PoolConfig{
TimeOut: time.Duration(*timeout) * time.Millisecond,
IdleTime: 0 * time.Millisecond,
MaxConnPoolSize: 2,
MinConnPoolSize: 0,
}
var err error
pool, err = nebula.NewConnectionPool(hostList, poolConfig, nebula.DefaultLogger{})
pool, err = nebulago.NewConnectionPool(hostList, poolConfig, nebulago.DefaultLogger{})
if err != nil {
log.Panicf(fmt.Sprintf("Fail to initialize the connection pool, host: %s, port: %d, %s", *address, *port, err.Error()))
}
Expand Down

0 comments on commit 76e3879

Please sign in to comment.