From ec117b96613b2222fd9fc023f65b621fde3b11bb Mon Sep 17 00:00:00 2001 From: "kyle.cao" Date: Tue, 21 Dec 2021 16:12:31 +0800 Subject: [PATCH] support parameter(variable) (#167) --- basic_example/parameter_example.go | 118 +++ client_test.go | 103 +- connection.go | 23 + .../graph_service-remote.go | 100 +- nebula/graph/graphservice.go | 879 +++++++++++++++++- session.go | 42 + 6 files changed, 1210 insertions(+), 55 deletions(-) create mode 100644 basic_example/parameter_example.go diff --git a/basic_example/parameter_example.go b/basic_example/parameter_example.go new file mode 100644 index 00000000..eb700cb8 --- /dev/null +++ b/basic_example/parameter_example.go @@ -0,0 +1,118 @@ +/* Copyright (c) 2021 vesoft inc. All rights reserved. + * + * This source code is licensed under Apache 2.0 License, + * attached with Common Clause Condition 1.0, found in the LICENSES directory. + */ + +package main + +import ( + "fmt" + "strings" + "sync" + + nebulago "github.com/vesoft-inc/nebula-go/v2" + nebula "github.com/vesoft-inc/nebula-go/v2/nebula" +) + +const ( + address = "127.0.0.1" + // The default port of Nebula Graph 2.x is 9669. + // 3699 is only for testing. + port = 3699 + username = "root" + password = "nebula" +) + +// Initialize logger +var log = nebulago.DefaultLogger{} + +func main() { + hostAddress := nebulago.HostAddress{Host: address, Port: port} + hostList := []nebulago.HostAddress{hostAddress} + // Create configs for connection pool using default values + testPoolConfig := nebulago.GetDefaultConf() + + // Initialize connection pool + pool, err := nebulago.NewConnectionPool(hostList, testPoolConfig, log) + if err != nil { + log.Fatal(fmt.Sprintf("Fail to initialize the connection pool, host: %s, port: %d, %s", address, port, err.Error())) + } + // Close all connections in the pool + defer pool.Close() + // Create session and send query in go routine + var wg sync.WaitGroup + wg.Add(1) + go func(wg *sync.WaitGroup) { + defer wg.Done() + // Create session + session, err := pool.GetSession(username, password) + if err != nil { + log.Fatal(fmt.Sprintf("Fail to create a new session from connection pool, username: %s, password: %s, %s", + username, password, err.Error())) + } + // Release session and return connection back to connection pool + defer session.Release() + + checkResultSet := func(prefix string, res *nebulago.ResultSet) { + if !res.IsSucceed() { + log.Fatal(fmt.Sprintf("%s, ErrorCode: %v, ErrorMsg: %s", prefix, res.GetErrorCode(), res.GetErrorMsg())) + } + } + + var params map[string]*nebula.Value + params = make(map[string]*nebula.Value) + + var bVal bool = true + var iVal int64 = 3 + // bool + p1 := nebula.Value{BVal: &bVal} + // int + p2 := nebula.Value{IVal: &iVal} + // list + lSlice := []*nebula.Value{&p1,&p2} + var lVal nebula.NList + lVal.Values = lSlice + p3 := nebula.Value{LVal: &lVal} + // map + var nmap map[string]*nebula.Value = map[string]*nebula.Value{"a": &p1, "b": &p2} + var mVal nebula.NMap + mVal.Kvs = nmap + p4 := nebula.Value{MVal: &mVal} + + params["p1"] = &p1 + params["p2"] = &p2 + params["p3"] = &p3 + params["p4"] = &p4 + + + // Extract data from the resultSet + { + query := "RETURN abs($p2)+1 AS col1, toBoolean($p1) and false AS col2, $p3, $p4.a" + // Send query + // resultSet, err := session.ExecuteWithParameter(query, params) + resultSet, err := session.ExecuteWithParameter(query,params) + if err != nil { + fmt.Print(err.Error()) + return + } + checkResultSet(query, resultSet) + + // Get all column names from the resultSet + colNames := resultSet.GetColNames() + fmt.Printf("Column names: %s\n", strings.Join(colNames, ", ")) + fmt.Print(resultSet.AsStringTable()) + // Get a row from resultSet + record, err := resultSet.GetRowValuesByIndex(0) + if err != nil { + log.Error(err.Error()) + } + // Print whole row + fmt.Printf("The first row elements: %s\n", record.String()) + } + }(&wg) + wg.Wait() + + fmt.Print("\n") + log.Info("Nebula Go Client Gorountines Example Finished") +} diff --git a/client_test.go b/client_test.go index 16849b3a..89017222 100644 --- a/client_test.go +++ b/client_test.go @@ -49,6 +49,8 @@ var nebulaLog = DefaultLogger{} // Create default configs var testPoolConfig = GetDefaultConf() +var params map[string]*nebula.Value + // Before run `go test -v`, you should start a nebula server listening on 3699 port. // Using docker-compose is the easiest way and you can reference this file: // https://github.com/vesoft-inc/nebula/blob/master/docker/docker-compose.yaml @@ -506,7 +508,6 @@ func TestServiceDataIO(t *testing.T) { } assert.Equal(t, int8(sessionCreatedTime.Hour()), localTime.GetHour()) } - dropSpace(t, session, "client_test") } @@ -960,6 +961,73 @@ func TestExecuteJson(t *testing.T) { } } +func TestExecuteWithParameter(t *testing.T) { + hostList := []HostAddress{{Host: address, Port: port}} + + testPoolConfig = PoolConfig{ + TimeOut: 0 * time.Millisecond, + IdleTime: 0 * time.Millisecond, + MaxConnPoolSize: 10, + MinConnPoolSize: 1, + } + + // Initialize connection pool + pool, err := NewConnectionPool(hostList, testPoolConfig, nebulaLog) + if err != nil { + t.Fatalf("fail to initialize the connection pool, host: %s, port: %d, %s", address, port, err.Error()) + } + // close all connections in the pool + defer pool.Close() + + // Create session + session, err := pool.GetSession(username, password) + if err != nil { + t.Fatalf("fail to create a new session from connection pool, username: %s, password: %s, %s", + username, password, err.Error()) + } + defer session.Release() + + // Create schemas + createTestDataSchema(t, session) + // Load data + loadTestData(t, session) + // p1:true p2:3 p3:[true,3] p4:{"a":true,"b":"Bob"} + prepareParameter() + // Complex result + { + resp, err := tryToExecuteWithParameter(session, "MATCH (v:person {name: $p4.b}) WHERE v.age>$p2-3 and $p1==true RETURN v ORDER BY $p3[0] LIMIT $p2", params) + if err != nil { + t.Fatalf(err.Error()) + return + } + assert.Equal(t, 1, resp.GetRowSize()) + record, err := resp.GetRowValuesByIndex(0) + if err != nil { + t.Fatalf(err.Error()) + return + } + valWrap, err := record.GetValueByIndex(0) + if err != nil { + t.Fatalf(err.Error()) + return + } + node, err := valWrap.AsNode() + if err != nil { + t.Fatalf(err.Error()) + return + } + assert.Equal(t, + "(\"Bob\" :student{name: \"Bob\"} "+ + ":person{age: 10, birthday: 2010-09-10T10:08:02.000000, book_num: 100, "+ + "child_name: \"Hello Worl\", expend: 100.0, "+ + "first_out_city: 1111, friends: 10, grade: 3, "+ + "hobby: __NULL__, is_girl: false, "+ + "morning: 07:10:00.000000, name: \"Bob\", "+ + "property: 1000.0, start_school: 2017-09-10})", + node.String()) + } +} + func TestReconnect(t *testing.T) { hostList := poolAddress @@ -1073,6 +1141,17 @@ func tryToExecute(session *Session, query string) (resp *ResultSet, err error) { return } +func tryToExecuteWithParameter(session *Session, query string, params map[string]*nebula.Value) (resp *ResultSet, err error) { + for i := 3; i > 0; i-- { + resp, err = session.ExecuteWithParameter(query, params) + if err == nil && resp.IsSucceed() { + return + } + time.Sleep(2 * time.Second) + } + return +} + // creates schema func createTestDataSchema(t *testing.T, session *Session) { createSchema := "CREATE SPACE IF NOT EXISTS test_data(vid_type = FIXED_STRING(30));" + @@ -1163,6 +1242,28 @@ func loadTestData(t *testing.T, session *Session) { checkResultSet(t, query, resultSet) } +func prepareParameter() { + // p1:true p2:3 p3:[true,3] p4:{"a":true,"b":"Bob"} + params = make(map[string]*nebula.Value) + var bVal bool = true + var iVal int64 = 3 + p1 := nebula.Value{BVal: &bVal} + p2 := nebula.Value{IVal: &iVal} + p5 := nebula.Value{SVal: []byte("Bob")} + lSlice := []*nebula.Value{&p1, &p2} + var lVal nebula.NList + lVal.Values = lSlice + p3 := nebula.Value{LVal: &lVal} + var nmap map[string]*nebula.Value = map[string]*nebula.Value{"a": &p1, "b": &p5} + var mVal nebula.NMap + mVal.Kvs = nmap + p4 := nebula.Value{MVal: &mVal} + params["p1"] = &p1 + params["p2"] = &p2 + params["p3"] = &p3 + params["p4"] = &p4 +} + func dropSpace(t *testing.T, session *Session, spaceName string) { query := fmt.Sprintf("DROP SPACE IF EXISTS %s;", spaceName) resultSet, err := tryToExecute(session, query) diff --git a/connection.go b/connection.go index 9a6a9bde..d3260d81 100644 --- a/connection.go +++ b/connection.go @@ -127,6 +127,23 @@ func (cn *connection) execute(sessionID int64, stmt string) (*graph.ExecutionRes return resp, err } +func (cn *connection) executeWithParameter(sessionID int64, stmt string, params map[string]*nebula.Value) (*graph.ExecutionResponse, error) { + resp, err := cn.graph.ExecuteWithParameter(sessionID, []byte(stmt), params) + if err != nil { + // reopen the connection if timeout + if _, ok := err.(thrift.TransportException); ok { + if err.(thrift.TransportException).TypeID() == thrift.TIMED_OUT { + reopenErr := cn.reopen() + if reopenErr != nil { + return nil, reopenErr + } + return cn.graph.ExecuteWithParameter(sessionID, []byte(stmt), params) + } + } + } + + return resp, err +} func (cn *connection) executeJson(sessionID int64, stmt string) ([]byte, error) { jsonResp, err := cn.graph.ExecuteJson(sessionID, []byte(stmt)) if err != nil { @@ -151,6 +168,12 @@ func (cn *connection) ping() bool { return err == nil } +// Check connection to host address +func (cn *connection) pingWithParameter() bool { + _, err := cn.executeWithParameter(0, "YIELD 1", nil) + return err == nil +} + // Sign out and release seesin ID func (cn *connection) signOut(sessionID int64) error { // Release session ID to graphd diff --git a/nebula/graph/graph_service-remote/graph_service-remote.go b/nebula/graph/graph_service-remote/graph_service-remote.go index 8666dcfa..b37de618 100755 --- a/nebula/graph/graph_service-remote/graph_service-remote.go +++ b/nebula/graph/graph_service-remote/graph_service-remote.go @@ -24,7 +24,9 @@ func Usage() { fmt.Fprintln(os.Stderr, " AuthResponse authenticate(string username, string password)") fmt.Fprintln(os.Stderr, " void signout(i64 sessionId)") fmt.Fprintln(os.Stderr, " ExecutionResponse execute(i64 sessionId, string stmt)") + fmt.Fprintln(os.Stderr, " ExecutionResponse executeWithParameter(i64 sessionId, string stmt, parameterMap)") fmt.Fprintln(os.Stderr, " string executeJson(i64 sessionId, string stmt)") + fmt.Fprintln(os.Stderr, " string executeJsonWithParameter(i64 sessionId, string stmt, parameterMap)") fmt.Fprintln(os.Stderr, " VerifyClientVersionResp verifyClientVersion(VerifyClientVersionReq req)") fmt.Fprintln(os.Stderr) os.Exit(0) @@ -137,8 +139,8 @@ func main() { fmt.Fprintln(os.Stderr, "Signout requires 1 args") flag.Usage() } - argvalue0, err13 := (strconv.ParseInt(flag.Arg(1), 10, 64)) - if err13 != nil { + argvalue0, err17 := (strconv.ParseInt(flag.Arg(1), 10, 64)) + if err17 != nil { Usage() return } @@ -151,8 +153,8 @@ func main() { fmt.Fprintln(os.Stderr, "Execute requires 2 args") flag.Usage() } - argvalue0, err14 := (strconv.ParseInt(flag.Arg(1), 10, 64)) - if err14 != nil { + argvalue0, err18 := (strconv.ParseInt(flag.Arg(1), 10, 64)) + if err18 != nil { Usage() return } @@ -162,13 +164,47 @@ func main() { fmt.Print(client.Execute(value0, value1)) fmt.Print("\n") break + case "executeWithParameter": + if flag.NArg() - 1 != 3 { + fmt.Fprintln(os.Stderr, "ExecuteWithParameter requires 3 args") + flag.Usage() + } + argvalue0, err20 := (strconv.ParseInt(flag.Arg(1), 10, 64)) + if err20 != nil { + Usage() + return + } + value0 := argvalue0 + argvalue1 := []byte(flag.Arg(2)) + value1 := argvalue1 + arg22 := flag.Arg(3) + mbTrans23 := thrift.NewMemoryBufferLen(len(arg22)) + defer mbTrans23.Close() + _, err24 := mbTrans23.WriteString(arg22) + if err24 != nil { + Usage() + return + } + factory25 := thrift.NewSimpleJSONProtocolFactory() + jsProt26 := factory25.GetProtocol(mbTrans23) + containerStruct2 := graph.NewGraphServiceExecuteWithParameterArgs() + err27 := containerStruct2.ReadField3(jsProt26) + if err27 != nil { + Usage() + return + } + argvalue2 := containerStruct2.ParameterMap + value2 := argvalue2 + fmt.Print(client.ExecuteWithParameter(value0, value1, value2)) + fmt.Print("\n") + break case "executeJson": if flag.NArg() - 1 != 2 { fmt.Fprintln(os.Stderr, "ExecuteJson requires 2 args") flag.Usage() } - argvalue0, err16 := (strconv.ParseInt(flag.Arg(1), 10, 64)) - if err16 != nil { + argvalue0, err28 := (strconv.ParseInt(flag.Arg(1), 10, 64)) + if err28 != nil { Usage() return } @@ -178,24 +214,58 @@ func main() { fmt.Print(client.ExecuteJson(value0, value1)) fmt.Print("\n") break + case "executeJsonWithParameter": + if flag.NArg() - 1 != 3 { + fmt.Fprintln(os.Stderr, "ExecuteJsonWithParameter requires 3 args") + flag.Usage() + } + argvalue0, err30 := (strconv.ParseInt(flag.Arg(1), 10, 64)) + if err30 != nil { + Usage() + return + } + value0 := argvalue0 + argvalue1 := []byte(flag.Arg(2)) + value1 := argvalue1 + arg32 := flag.Arg(3) + mbTrans33 := thrift.NewMemoryBufferLen(len(arg32)) + defer mbTrans33.Close() + _, err34 := mbTrans33.WriteString(arg32) + if err34 != nil { + Usage() + return + } + factory35 := thrift.NewSimpleJSONProtocolFactory() + jsProt36 := factory35.GetProtocol(mbTrans33) + containerStruct2 := graph.NewGraphServiceExecuteJsonWithParameterArgs() + err37 := containerStruct2.ReadField3(jsProt36) + if err37 != nil { + Usage() + return + } + argvalue2 := containerStruct2.ParameterMap + value2 := argvalue2 + fmt.Print(client.ExecuteJsonWithParameter(value0, value1, value2)) + fmt.Print("\n") + break case "verifyClientVersion": if flag.NArg() - 1 != 1 { fmt.Fprintln(os.Stderr, "VerifyClientVersion requires 1 args") flag.Usage() } - arg18 := flag.Arg(1) - mbTrans19 := thrift.NewMemoryBufferLen(len(arg18)) - defer mbTrans19.Close() - _, err20 := mbTrans19.WriteString(arg18) - if err20 != nil { + arg38 := flag.Arg(1) + mbTrans39 := thrift.NewMemoryBufferLen(len(arg38)) + defer mbTrans39.Close() + _, err40 := mbTrans39.WriteString(arg38) + if err40 != nil { Usage() return } - factory21 := thrift.NewSimpleJSONProtocolFactory() - jsProt22 := factory21.GetProtocol(mbTrans19) + factory41 := thrift.NewSimpleJSONProtocolFactory() + jsProt42 := factory41.GetProtocol(mbTrans39) argvalue0 := graph.NewVerifyClientVersionReq() - err23 := argvalue0.Read(jsProt22) - if err23 != nil { + err43 := argvalue0.Read(jsProt42) + if err43 != nil { Usage() return } diff --git a/nebula/graph/graphservice.go b/nebula/graph/graphservice.go index 3e4b0e82..a631cf9c 100644 --- a/nebula/graph/graphservice.go +++ b/nebula/graph/graphservice.go @@ -37,8 +37,18 @@ type GraphService interface { // Parameters: // - SessionId // - Stmt + // - ParameterMap + ExecuteWithParameter(ctx context.Context, sessionId int64, stmt []byte, parameterMap map[string]*nebula0.Value) (_r *ExecutionResponse, err error) + // Parameters: + // - SessionId + // - Stmt ExecuteJson(ctx context.Context, sessionId int64, stmt []byte) (_r []byte, err error) // Parameters: + // - SessionId + // - Stmt + // - ParameterMap + ExecuteJsonWithParameter(ctx context.Context, sessionId int64, stmt []byte, parameterMap map[string]*nebula0.Value) (_r []byte, err error) + // Parameters: // - Req VerifyClientVersion(ctx context.Context, req *VerifyClientVersionReq) (_r *VerifyClientVersionResp, err error) } @@ -59,8 +69,18 @@ type GraphServiceClientInterface interface { // Parameters: // - SessionId // - Stmt + // - ParameterMap + ExecuteWithParameter(sessionId int64, stmt []byte, parameterMap map[string]*nebula0.Value) (_r *ExecutionResponse, err error) + // Parameters: + // - SessionId + // - Stmt ExecuteJson(sessionId int64, stmt []byte) (_r []byte, err error) // Parameters: + // - SessionId + // - Stmt + // - ParameterMap + ExecuteJsonWithParameter(sessionId int64, stmt []byte, parameterMap map[string]*nebula0.Value) (_r []byte, err error) + // Parameters: // - Req VerifyClientVersion(req *VerifyClientVersionReq) (_r *VerifyClientVersionResp, err error) } @@ -149,6 +169,30 @@ func (p *GraphServiceClient) recvExecute() (value *ExecutionResponse, err error) return result.GetSuccess(), nil } +// Parameters: +// - SessionId +// - Stmt +// - ParameterMap +func (p *GraphServiceClient) ExecuteWithParameter(sessionId int64, stmt []byte, parameterMap map[string]*nebula0.Value) (_r *ExecutionResponse, err error) { + args := GraphServiceExecuteWithParameterArgs{ + SessionId : sessionId, + Stmt : stmt, + ParameterMap : parameterMap, + } + err = p.CC.SendMsg("executeWithParameter", &args, thrift.CALL) + if err != nil { return } + return p.recvExecuteWithParameter() +} + + +func (p *GraphServiceClient) recvExecuteWithParameter() (value *ExecutionResponse, err error) { + var result GraphServiceExecuteWithParameterResult + err = p.CC.RecvMsg("executeWithParameter", &result) + if err != nil { return } + + return result.GetSuccess(), nil +} + // Parameters: // - SessionId // - Stmt @@ -171,6 +215,30 @@ func (p *GraphServiceClient) recvExecuteJson() (value []byte, err error) { return result.GetSuccess(), nil } +// Parameters: +// - SessionId +// - Stmt +// - ParameterMap +func (p *GraphServiceClient) ExecuteJsonWithParameter(sessionId int64, stmt []byte, parameterMap map[string]*nebula0.Value) (_r []byte, err error) { + args := GraphServiceExecuteJsonWithParameterArgs{ + SessionId : sessionId, + Stmt : stmt, + ParameterMap : parameterMap, + } + err = p.CC.SendMsg("executeJsonWithParameter", &args, thrift.CALL) + if err != nil { return } + return p.recvExecuteJsonWithParameter() +} + + +func (p *GraphServiceClient) recvExecuteJsonWithParameter() (value []byte, err error) { + var result GraphServiceExecuteJsonWithParameterResult + err = p.CC.RecvMsg("executeJsonWithParameter", &result) + if err != nil { return } + + return result.GetSuccess(), nil +} + // Parameters: // - Req func (p *GraphServiceClient) VerifyClientVersion(req *VerifyClientVersionReq) (_r *VerifyClientVersionResp, err error) { @@ -289,6 +357,32 @@ func (p *GraphServiceThreadsafeClient) recvExecute() (value *ExecutionResponse, return result.GetSuccess(), nil } +// Parameters: +// - SessionId +// - Stmt +// - ParameterMap +func (p *GraphServiceThreadsafeClient) ExecuteWithParameter(sessionId int64, stmt []byte, parameterMap map[string]*nebula0.Value) (_r *ExecutionResponse, err error) { + p.Mu.Lock() + defer p.Mu.Unlock() + args := GraphServiceExecuteWithParameterArgs{ + SessionId : sessionId, + Stmt : stmt, + ParameterMap : parameterMap, + } + err = p.CC.SendMsg("executeWithParameter", &args, thrift.CALL) + if err != nil { return } + return p.recvExecuteWithParameter() +} + + +func (p *GraphServiceThreadsafeClient) recvExecuteWithParameter() (value *ExecutionResponse, err error) { + var result GraphServiceExecuteWithParameterResult + err = p.CC.RecvMsg("executeWithParameter", &result) + if err != nil { return } + + return result.GetSuccess(), nil +} + // Parameters: // - SessionId // - Stmt @@ -313,6 +407,32 @@ func (p *GraphServiceThreadsafeClient) recvExecuteJson() (value []byte, err erro return result.GetSuccess(), nil } +// Parameters: +// - SessionId +// - Stmt +// - ParameterMap +func (p *GraphServiceThreadsafeClient) ExecuteJsonWithParameter(sessionId int64, stmt []byte, parameterMap map[string]*nebula0.Value) (_r []byte, err error) { + p.Mu.Lock() + defer p.Mu.Unlock() + args := GraphServiceExecuteJsonWithParameterArgs{ + SessionId : sessionId, + Stmt : stmt, + ParameterMap : parameterMap, + } + err = p.CC.SendMsg("executeJsonWithParameter", &args, thrift.CALL) + if err != nil { return } + return p.recvExecuteJsonWithParameter() +} + + +func (p *GraphServiceThreadsafeClient) recvExecuteJsonWithParameter() (value []byte, err error) { + var result GraphServiceExecuteJsonWithParameterResult + err = p.CC.RecvMsg("executeJsonWithParameter", &result) + if err != nil { return } + + return result.GetSuccess(), nil +} + // Parameters: // - Req func (p *GraphServiceThreadsafeClient) VerifyClientVersion(req *VerifyClientVersionReq) (_r *VerifyClientVersionResp, err error) { @@ -398,6 +518,23 @@ func (p *GraphServiceChannelClient) Execute(ctx context.Context, sessionId int64 return result.GetSuccess(), nil } +// Parameters: +// - SessionId +// - Stmt +// - ParameterMap +func (p *GraphServiceChannelClient) ExecuteWithParameter(ctx context.Context, sessionId int64, stmt []byte, parameterMap map[string]*nebula0.Value) (_r *ExecutionResponse, err error) { + args := GraphServiceExecuteWithParameterArgs{ + SessionId : sessionId, + Stmt : stmt, + ParameterMap : parameterMap, + } + var result GraphServiceExecuteWithParameterResult + err = p.RequestChannel.Call(ctx, "executeWithParameter", &args, &result) + if err != nil { return } + + return result.GetSuccess(), nil +} + // Parameters: // - SessionId // - Stmt @@ -413,6 +550,23 @@ func (p *GraphServiceChannelClient) ExecuteJson(ctx context.Context, sessionId i return result.GetSuccess(), nil } +// Parameters: +// - SessionId +// - Stmt +// - ParameterMap +func (p *GraphServiceChannelClient) ExecuteJsonWithParameter(ctx context.Context, sessionId int64, stmt []byte, parameterMap map[string]*nebula0.Value) (_r []byte, err error) { + args := GraphServiceExecuteJsonWithParameterArgs{ + SessionId : sessionId, + Stmt : stmt, + ParameterMap : parameterMap, + } + var result GraphServiceExecuteJsonWithParameterResult + err = p.RequestChannel.Call(ctx, "executeJsonWithParameter", &args, &result) + if err != nil { return } + + return result.GetSuccess(), nil +} + // Parameters: // - Req func (p *GraphServiceChannelClient) VerifyClientVersion(ctx context.Context, req *VerifyClientVersionReq) (_r *VerifyClientVersionResp, err error) { @@ -452,7 +606,9 @@ func NewGraphServiceProcessor(handler GraphService) *GraphServiceProcessor { self9.processorMap["authenticate"] = &graphServiceProcessorAuthenticate{handler:handler} self9.processorMap["signout"] = &graphServiceProcessorSignout{handler:handler} self9.processorMap["execute"] = &graphServiceProcessorExecute{handler:handler} + self9.processorMap["executeWithParameter"] = &graphServiceProcessorExecuteWithParameter{handler:handler} self9.processorMap["executeJson"] = &graphServiceProcessorExecuteJson{handler:handler} + self9.processorMap["executeJsonWithParameter"] = &graphServiceProcessorExecuteJsonWithParameter{handler:handler} self9.processorMap["verifyClientVersion"] = &graphServiceProcessorVerifyClientVersion{handler:handler} return self9 } @@ -604,6 +760,56 @@ func (p *graphServiceProcessorExecute) RunContext(ctx context.Context, argStruct return &result, nil } +type graphServiceProcessorExecuteWithParameter struct { + handler GraphService +} + +func (p *graphServiceProcessorExecuteWithParameter) Read(iprot thrift.Protocol) (thrift.Struct, thrift.Exception) { + args := GraphServiceExecuteWithParameterArgs{} + if err := args.Read(iprot); err != nil { + return nil, err + } + iprot.ReadMessageEnd() + return &args, nil +} + +func (p *graphServiceProcessorExecuteWithParameter) Write(seqId int32, result thrift.WritableStruct, oprot thrift.Protocol) (err thrift.Exception) { + var err2 error + messageType := thrift.REPLY + switch result.(type) { + case thrift.ApplicationException: + messageType = thrift.EXCEPTION + } + if err2 = oprot.WriteMessageBegin("executeWithParameter", messageType, seqId); err2 != nil { + err = err2 + } + if err2 = result.Write(oprot); err == nil && err2 != nil { + err = err2 + } + if err2 = oprot.WriteMessageEnd(); err == nil && err2 != nil { + err = err2 + } + if err2 = oprot.Flush(); err == nil && err2 != nil { + err = err2 + } + return err +} + +func (p *graphServiceProcessorExecuteWithParameter) RunContext(ctx context.Context, argStruct thrift.Struct) (thrift.WritableStruct, thrift.ApplicationException) { + args := argStruct.(*GraphServiceExecuteWithParameterArgs) + var result GraphServiceExecuteWithParameterResult + if retval, err := p.handler.ExecuteWithParameter(ctx, args.SessionId, args.Stmt, args.ParameterMap); err != nil { + switch err.(type) { + default: + x := thrift.NewApplicationException(thrift.INTERNAL_ERROR, "Internal error processing executeWithParameter: " + err.Error()) + return x, x + } + } else { + result.Success = retval + } + return &result, nil +} + type graphServiceProcessorExecuteJson struct { handler GraphService } @@ -654,6 +860,56 @@ func (p *graphServiceProcessorExecuteJson) RunContext(ctx context.Context, argSt return &result, nil } +type graphServiceProcessorExecuteJsonWithParameter struct { + handler GraphService +} + +func (p *graphServiceProcessorExecuteJsonWithParameter) Read(iprot thrift.Protocol) (thrift.Struct, thrift.Exception) { + args := GraphServiceExecuteJsonWithParameterArgs{} + if err := args.Read(iprot); err != nil { + return nil, err + } + iprot.ReadMessageEnd() + return &args, nil +} + +func (p *graphServiceProcessorExecuteJsonWithParameter) Write(seqId int32, result thrift.WritableStruct, oprot thrift.Protocol) (err thrift.Exception) { + var err2 error + messageType := thrift.REPLY + switch result.(type) { + case thrift.ApplicationException: + messageType = thrift.EXCEPTION + } + if err2 = oprot.WriteMessageBegin("executeJsonWithParameter", messageType, seqId); err2 != nil { + err = err2 + } + if err2 = result.Write(oprot); err == nil && err2 != nil { + err = err2 + } + if err2 = oprot.WriteMessageEnd(); err == nil && err2 != nil { + err = err2 + } + if err2 = oprot.Flush(); err == nil && err2 != nil { + err = err2 + } + return err +} + +func (p *graphServiceProcessorExecuteJsonWithParameter) RunContext(ctx context.Context, argStruct thrift.Struct) (thrift.WritableStruct, thrift.ApplicationException) { + args := argStruct.(*GraphServiceExecuteJsonWithParameterArgs) + var result GraphServiceExecuteJsonWithParameterResult + if retval, err := p.handler.ExecuteJsonWithParameter(ctx, args.SessionId, args.Stmt, args.ParameterMap); err != nil { + switch err.(type) { + default: + x := thrift.NewApplicationException(thrift.INTERNAL_ERROR, "Internal error processing executeJsonWithParameter: " + err.Error()) + return x, x + } + } else { + result.Success = retval + } + return &result, nil +} + type graphServiceProcessorVerifyClientVersion struct { handler GraphService } @@ -1230,25 +1486,31 @@ func (p *GraphServiceExecuteResult) String() string { // Attributes: // - SessionId // - Stmt -type GraphServiceExecuteJsonArgs struct { +// - ParameterMap +type GraphServiceExecuteWithParameterArgs struct { thrift.IRequest SessionId int64 `thrift:"sessionId,1" db:"sessionId" json:"sessionId"` Stmt []byte `thrift:"stmt,2" db:"stmt" json:"stmt"` + ParameterMap map[string]*nebula0.Value `thrift:"parameterMap,3" db:"parameterMap" json:"parameterMap"` } -func NewGraphServiceExecuteJsonArgs() *GraphServiceExecuteJsonArgs { - return &GraphServiceExecuteJsonArgs{} +func NewGraphServiceExecuteWithParameterArgs() *GraphServiceExecuteWithParameterArgs { + return &GraphServiceExecuteWithParameterArgs{} } -func (p *GraphServiceExecuteJsonArgs) GetSessionId() int64 { +func (p *GraphServiceExecuteWithParameterArgs) GetSessionId() int64 { return p.SessionId } -func (p *GraphServiceExecuteJsonArgs) GetStmt() []byte { +func (p *GraphServiceExecuteWithParameterArgs) GetStmt() []byte { return p.Stmt } -func (p *GraphServiceExecuteJsonArgs) Read(iprot thrift.Protocol) error { + +func (p *GraphServiceExecuteWithParameterArgs) GetParameterMap() map[string]*nebula0.Value { + return p.ParameterMap +} +func (p *GraphServiceExecuteWithParameterArgs) Read(iprot thrift.Protocol) error { if _, err := iprot.ReadStructBegin(); err != nil { return thrift.PrependError(fmt.Sprintf("%T read error: ", p), err) } @@ -1269,6 +1531,10 @@ func (p *GraphServiceExecuteJsonArgs) Read(iprot thrift.Protocol) error { if err := p.ReadField2(iprot); err != nil { return err } + case 3: + if err := p.ReadField3(iprot); err != nil { + return err + } default: if err := iprot.Skip(fieldTypeId); err != nil { return err @@ -1284,7 +1550,7 @@ func (p *GraphServiceExecuteJsonArgs) Read(iprot thrift.Protocol) error { return nil } -func (p *GraphServiceExecuteJsonArgs) ReadField1(iprot thrift.Protocol) error { +func (p *GraphServiceExecuteWithParameterArgs) ReadField1(iprot thrift.Protocol) error { if v, err := iprot.ReadI64(); err != nil { return thrift.PrependError("error reading field 1: ", err) } else { @@ -1293,7 +1559,7 @@ func (p *GraphServiceExecuteJsonArgs) ReadField1(iprot thrift.Protocol) error { return nil } -func (p *GraphServiceExecuteJsonArgs) ReadField2(iprot thrift.Protocol) error { +func (p *GraphServiceExecuteWithParameterArgs) ReadField2(iprot thrift.Protocol) error { if v, err := iprot.ReadBinary(); err != nil { return thrift.PrependError("error reading field 2: ", err) } else { @@ -1302,11 +1568,38 @@ func (p *GraphServiceExecuteJsonArgs) ReadField2(iprot thrift.Protocol) error { return nil } -func (p *GraphServiceExecuteJsonArgs) Write(oprot thrift.Protocol) error { - if err := oprot.WriteStructBegin("executeJson_args"); err != nil { +func (p *GraphServiceExecuteWithParameterArgs) ReadField3(iprot thrift.Protocol) error { + _, _, size, err := iprot.ReadMapBegin() + if err != nil { + return thrift.PrependError("error reading map begin: ", err) + } + tMap := make(map[string]*nebula0.Value, size) + p.ParameterMap = tMap + for i := 0; i < size; i ++ { +var _key11 string + if v, err := iprot.ReadString(); err != nil { + return thrift.PrependError("error reading field 0: ", err) +} else { + _key11 = v +} + _val12 := nebula0.NewValue() + if err := _val12.Read(iprot); err != nil { + return thrift.PrependError(fmt.Sprintf("%T error reading struct: ", _val12), err) + } + p.ParameterMap[_key11] = _val12 + } + if err := iprot.ReadMapEnd(); err != nil { + return thrift.PrependError("error reading map end: ", err) + } + return nil +} + +func (p *GraphServiceExecuteWithParameterArgs) Write(oprot thrift.Protocol) error { + if err := oprot.WriteStructBegin("executeWithParameter_args"); err != nil { return thrift.PrependError(fmt.Sprintf("%T write struct begin error: ", p), err) } if err := p.writeField1(oprot); err != nil { return err } if err := p.writeField2(oprot); err != nil { return err } + if err := p.writeField3(oprot); err != nil { return err } if err := oprot.WriteFieldStop(); err != nil { return thrift.PrependError("write field stop error: ", err) } if err := oprot.WriteStructEnd(); err != nil { @@ -1314,7 +1607,7 @@ func (p *GraphServiceExecuteJsonArgs) Write(oprot thrift.Protocol) error { return nil } -func (p *GraphServiceExecuteJsonArgs) writeField1(oprot thrift.Protocol) (err error) { +func (p *GraphServiceExecuteWithParameterArgs) writeField1(oprot thrift.Protocol) (err error) { if err := oprot.WriteFieldBegin("sessionId", thrift.I64, 1); err != nil { return thrift.PrependError(fmt.Sprintf("%T write field begin error 1:sessionId: ", p), err) } if err := oprot.WriteI64(int64(p.SessionId)); err != nil { @@ -1324,7 +1617,7 @@ func (p *GraphServiceExecuteJsonArgs) writeField1(oprot thrift.Protocol) (err er return err } -func (p *GraphServiceExecuteJsonArgs) writeField2(oprot thrift.Protocol) (err error) { +func (p *GraphServiceExecuteWithParameterArgs) writeField2(oprot thrift.Protocol) (err error) { if err := oprot.WriteFieldBegin("stmt", thrift.STRING, 2); err != nil { return thrift.PrependError(fmt.Sprintf("%T write field begin error 2:stmt: ", p), err) } if err := oprot.WriteBinary(p.Stmt); err != nil { @@ -1334,37 +1627,61 @@ func (p *GraphServiceExecuteJsonArgs) writeField2(oprot thrift.Protocol) (err er return err } -func (p *GraphServiceExecuteJsonArgs) String() string { +func (p *GraphServiceExecuteWithParameterArgs) writeField3(oprot thrift.Protocol) (err error) { + if err := oprot.WriteFieldBegin("parameterMap", thrift.MAP, 3); err != nil { + return thrift.PrependError(fmt.Sprintf("%T write field begin error 3:parameterMap: ", p), err) } + if err := oprot.WriteMapBegin(thrift.STRING, thrift.STRUCT, len(p.ParameterMap)); err != nil { + return thrift.PrependError("error writing map begin: ", err) + } + for k, v := range p.ParameterMap { + if err := oprot.WriteString(string(k)); err != nil { + return thrift.PrependError(fmt.Sprintf("%T. (0) field write error: ", p), err) } + if err := v.Write(oprot); err != nil { + return thrift.PrependError(fmt.Sprintf("%T error writing struct: ", v), err) + } + } + if err := oprot.WriteMapEnd(); err != nil { + return thrift.PrependError("error writing map end: ", err) + } + if err := oprot.WriteFieldEnd(); err != nil { + return thrift.PrependError(fmt.Sprintf("%T write field end error 3:parameterMap: ", p), err) } + return err +} + +func (p *GraphServiceExecuteWithParameterArgs) String() string { if p == nil { return "" } sessionIdVal := fmt.Sprintf("%v", p.SessionId) stmtVal := fmt.Sprintf("%v", p.Stmt) - return fmt.Sprintf("GraphServiceExecuteJsonArgs({SessionId:%s Stmt:%s})", sessionIdVal, stmtVal) + parameterMapVal := fmt.Sprintf("%v", p.ParameterMap) + return fmt.Sprintf("GraphServiceExecuteWithParameterArgs({SessionId:%s Stmt:%s ParameterMap:%s})", sessionIdVal, stmtVal, parameterMapVal) } // Attributes: // - Success -type GraphServiceExecuteJsonResult struct { +type GraphServiceExecuteWithParameterResult struct { thrift.IResponse - Success []byte `thrift:"success,0" db:"success" json:"success,omitempty"` + Success *ExecutionResponse `thrift:"success,0" db:"success" json:"success,omitempty"` } -func NewGraphServiceExecuteJsonResult() *GraphServiceExecuteJsonResult { - return &GraphServiceExecuteJsonResult{} +func NewGraphServiceExecuteWithParameterResult() *GraphServiceExecuteWithParameterResult { + return &GraphServiceExecuteWithParameterResult{} } -var GraphServiceExecuteJsonResult_Success_DEFAULT []byte - -func (p *GraphServiceExecuteJsonResult) GetSuccess() []byte { - return p.Success +var GraphServiceExecuteWithParameterResult_Success_DEFAULT *ExecutionResponse +func (p *GraphServiceExecuteWithParameterResult) GetSuccess() *ExecutionResponse { + if !p.IsSetSuccess() { + return GraphServiceExecuteWithParameterResult_Success_DEFAULT + } +return p.Success } -func (p *GraphServiceExecuteJsonResult) IsSetSuccess() bool { +func (p *GraphServiceExecuteWithParameterResult) IsSetSuccess() bool { return p != nil && p.Success != nil } -func (p *GraphServiceExecuteJsonResult) Read(iprot thrift.Protocol) error { +func (p *GraphServiceExecuteWithParameterResult) Read(iprot thrift.Protocol) error { if _, err := iprot.ReadStructBegin(); err != nil { return thrift.PrependError(fmt.Sprintf("%T read error: ", p), err) } @@ -1396,17 +1713,16 @@ func (p *GraphServiceExecuteJsonResult) Read(iprot thrift.Protocol) error { return nil } -func (p *GraphServiceExecuteJsonResult) ReadField0(iprot thrift.Protocol) error { - if v, err := iprot.ReadBinary(); err != nil { - return thrift.PrependError("error reading field 0: ", err) -} else { - p.Success = v -} +func (p *GraphServiceExecuteWithParameterResult) ReadField0(iprot thrift.Protocol) error { + p.Success = NewExecutionResponse() + if err := p.Success.Read(iprot); err != nil { + return thrift.PrependError(fmt.Sprintf("%T error reading struct: ", p.Success), err) + } return nil } -func (p *GraphServiceExecuteJsonResult) Write(oprot thrift.Protocol) error { - if err := oprot.WriteStructBegin("executeJson_result"); err != nil { +func (p *GraphServiceExecuteWithParameterResult) Write(oprot thrift.Protocol) error { + if err := oprot.WriteStructBegin("executeWithParameter_result"); err != nil { return thrift.PrependError(fmt.Sprintf("%T write struct begin error: ", p), err) } if err := p.writeField0(oprot); err != nil { return err } if err := oprot.WriteFieldStop(); err != nil { @@ -1416,25 +1732,510 @@ func (p *GraphServiceExecuteJsonResult) Write(oprot thrift.Protocol) error { return nil } -func (p *GraphServiceExecuteJsonResult) writeField0(oprot thrift.Protocol) (err error) { +func (p *GraphServiceExecuteWithParameterResult) writeField0(oprot thrift.Protocol) (err error) { if p.IsSetSuccess() { - if err := oprot.WriteFieldBegin("success", thrift.STRING, 0); err != nil { + if err := oprot.WriteFieldBegin("success", thrift.STRUCT, 0); err != nil { return thrift.PrependError(fmt.Sprintf("%T write field begin error 0:success: ", p), err) } - if err := oprot.WriteBinary(p.Success); err != nil { - return thrift.PrependError(fmt.Sprintf("%T.success (0) field write error: ", p), err) } + if err := p.Success.Write(oprot); err != nil { + return thrift.PrependError(fmt.Sprintf("%T error writing struct: ", p.Success), err) + } if err := oprot.WriteFieldEnd(); err != nil { return thrift.PrependError(fmt.Sprintf("%T write field end error 0:success: ", p), err) } } return err } -func (p *GraphServiceExecuteJsonResult) String() string { +func (p *GraphServiceExecuteWithParameterResult) String() string { if p == nil { return "" } - successVal := fmt.Sprintf("%v", p.Success) - return fmt.Sprintf("GraphServiceExecuteJsonResult({Success:%s})", successVal) + var successVal string + if p.Success == nil { + successVal = "" + } else { + successVal = fmt.Sprintf("%v", p.Success) + } + return fmt.Sprintf("GraphServiceExecuteWithParameterResult({Success:%s})", successVal) +} + +// Attributes: +// - SessionId +// - Stmt +type GraphServiceExecuteJsonArgs struct { + thrift.IRequest + SessionId int64 `thrift:"sessionId,1" db:"sessionId" json:"sessionId"` + Stmt []byte `thrift:"stmt,2" db:"stmt" json:"stmt"` +} + +func NewGraphServiceExecuteJsonArgs() *GraphServiceExecuteJsonArgs { + return &GraphServiceExecuteJsonArgs{} +} + + +func (p *GraphServiceExecuteJsonArgs) GetSessionId() int64 { + return p.SessionId +} + +func (p *GraphServiceExecuteJsonArgs) GetStmt() []byte { + return p.Stmt +} +func (p *GraphServiceExecuteJsonArgs) Read(iprot thrift.Protocol) error { + if _, err := iprot.ReadStructBegin(); err != nil { + return thrift.PrependError(fmt.Sprintf("%T read error: ", p), err) + } + + + for { + _, fieldTypeId, fieldId, err := iprot.ReadFieldBegin() + if err != nil { + return thrift.PrependError(fmt.Sprintf("%T field %d read error: ", p, fieldId), err) + } + if fieldTypeId == thrift.STOP { break; } + switch fieldId { + case 1: + if err := p.ReadField1(iprot); err != nil { + return err + } + case 2: + if err := p.ReadField2(iprot); err != nil { + return err + } + default: + if err := iprot.Skip(fieldTypeId); err != nil { + return err + } + } + if err := iprot.ReadFieldEnd(); err != nil { + return err + } + } + if err := iprot.ReadStructEnd(); err != nil { + return thrift.PrependError(fmt.Sprintf("%T read struct end error: ", p), err) + } + return nil +} + +func (p *GraphServiceExecuteJsonArgs) ReadField1(iprot thrift.Protocol) error { + if v, err := iprot.ReadI64(); err != nil { + return thrift.PrependError("error reading field 1: ", err) +} else { + p.SessionId = v +} + return nil +} + +func (p *GraphServiceExecuteJsonArgs) ReadField2(iprot thrift.Protocol) error { + if v, err := iprot.ReadBinary(); err != nil { + return thrift.PrependError("error reading field 2: ", err) +} else { + p.Stmt = v +} + return nil +} + +func (p *GraphServiceExecuteJsonArgs) Write(oprot thrift.Protocol) error { + if err := oprot.WriteStructBegin("executeJson_args"); err != nil { + return thrift.PrependError(fmt.Sprintf("%T write struct begin error: ", p), err) } + if err := p.writeField1(oprot); err != nil { return err } + if err := p.writeField2(oprot); err != nil { return err } + if err := oprot.WriteFieldStop(); err != nil { + return thrift.PrependError("write field stop error: ", err) } + if err := oprot.WriteStructEnd(); err != nil { + return thrift.PrependError("write struct stop error: ", err) } + return nil +} + +func (p *GraphServiceExecuteJsonArgs) writeField1(oprot thrift.Protocol) (err error) { + if err := oprot.WriteFieldBegin("sessionId", thrift.I64, 1); err != nil { + return thrift.PrependError(fmt.Sprintf("%T write field begin error 1:sessionId: ", p), err) } + if err := oprot.WriteI64(int64(p.SessionId)); err != nil { + return thrift.PrependError(fmt.Sprintf("%T.sessionId (1) field write error: ", p), err) } + if err := oprot.WriteFieldEnd(); err != nil { + return thrift.PrependError(fmt.Sprintf("%T write field end error 1:sessionId: ", p), err) } + return err +} + +func (p *GraphServiceExecuteJsonArgs) writeField2(oprot thrift.Protocol) (err error) { + if err := oprot.WriteFieldBegin("stmt", thrift.STRING, 2); err != nil { + return thrift.PrependError(fmt.Sprintf("%T write field begin error 2:stmt: ", p), err) } + if err := oprot.WriteBinary(p.Stmt); err != nil { + return thrift.PrependError(fmt.Sprintf("%T.stmt (2) field write error: ", p), err) } + if err := oprot.WriteFieldEnd(); err != nil { + return thrift.PrependError(fmt.Sprintf("%T write field end error 2:stmt: ", p), err) } + return err +} + +func (p *GraphServiceExecuteJsonArgs) String() string { + if p == nil { + return "" + } + + sessionIdVal := fmt.Sprintf("%v", p.SessionId) + stmtVal := fmt.Sprintf("%v", p.Stmt) + return fmt.Sprintf("GraphServiceExecuteJsonArgs({SessionId:%s Stmt:%s})", sessionIdVal, stmtVal) +} + +// Attributes: +// - Success +type GraphServiceExecuteJsonResult struct { + thrift.IResponse + Success []byte `thrift:"success,0" db:"success" json:"success,omitempty"` +} + +func NewGraphServiceExecuteJsonResult() *GraphServiceExecuteJsonResult { + return &GraphServiceExecuteJsonResult{} +} + +var GraphServiceExecuteJsonResult_Success_DEFAULT []byte + +func (p *GraphServiceExecuteJsonResult) GetSuccess() []byte { + return p.Success +} +func (p *GraphServiceExecuteJsonResult) IsSetSuccess() bool { + return p != nil && p.Success != nil +} + +func (p *GraphServiceExecuteJsonResult) Read(iprot thrift.Protocol) error { + if _, err := iprot.ReadStructBegin(); err != nil { + return thrift.PrependError(fmt.Sprintf("%T read error: ", p), err) + } + + + for { + _, fieldTypeId, fieldId, err := iprot.ReadFieldBegin() + if err != nil { + return thrift.PrependError(fmt.Sprintf("%T field %d read error: ", p, fieldId), err) + } + if fieldTypeId == thrift.STOP { break; } + switch fieldId { + case 0: + if err := p.ReadField0(iprot); err != nil { + return err + } + default: + if err := iprot.Skip(fieldTypeId); err != nil { + return err + } + } + if err := iprot.ReadFieldEnd(); err != nil { + return err + } + } + if err := iprot.ReadStructEnd(); err != nil { + return thrift.PrependError(fmt.Sprintf("%T read struct end error: ", p), err) + } + return nil +} + +func (p *GraphServiceExecuteJsonResult) ReadField0(iprot thrift.Protocol) error { + if v, err := iprot.ReadBinary(); err != nil { + return thrift.PrependError("error reading field 0: ", err) +} else { + p.Success = v +} + return nil +} + +func (p *GraphServiceExecuteJsonResult) Write(oprot thrift.Protocol) error { + if err := oprot.WriteStructBegin("executeJson_result"); err != nil { + return thrift.PrependError(fmt.Sprintf("%T write struct begin error: ", p), err) } + if err := p.writeField0(oprot); err != nil { return err } + if err := oprot.WriteFieldStop(); err != nil { + return thrift.PrependError("write field stop error: ", err) } + if err := oprot.WriteStructEnd(); err != nil { + return thrift.PrependError("write struct stop error: ", err) } + return nil +} + +func (p *GraphServiceExecuteJsonResult) writeField0(oprot thrift.Protocol) (err error) { + if p.IsSetSuccess() { + if err := oprot.WriteFieldBegin("success", thrift.STRING, 0); err != nil { + return thrift.PrependError(fmt.Sprintf("%T write field begin error 0:success: ", p), err) } + if err := oprot.WriteBinary(p.Success); err != nil { + return thrift.PrependError(fmt.Sprintf("%T.success (0) field write error: ", p), err) } + if err := oprot.WriteFieldEnd(); err != nil { + return thrift.PrependError(fmt.Sprintf("%T write field end error 0:success: ", p), err) } + } + return err +} + +func (p *GraphServiceExecuteJsonResult) String() string { + if p == nil { + return "" + } + + successVal := fmt.Sprintf("%v", p.Success) + return fmt.Sprintf("GraphServiceExecuteJsonResult({Success:%s})", successVal) +} + +// Attributes: +// - SessionId +// - Stmt +// - ParameterMap +type GraphServiceExecuteJsonWithParameterArgs struct { + thrift.IRequest + SessionId int64 `thrift:"sessionId,1" db:"sessionId" json:"sessionId"` + Stmt []byte `thrift:"stmt,2" db:"stmt" json:"stmt"` + ParameterMap map[string]*nebula0.Value `thrift:"parameterMap,3" db:"parameterMap" json:"parameterMap"` +} + +func NewGraphServiceExecuteJsonWithParameterArgs() *GraphServiceExecuteJsonWithParameterArgs { + return &GraphServiceExecuteJsonWithParameterArgs{} +} + + +func (p *GraphServiceExecuteJsonWithParameterArgs) GetSessionId() int64 { + return p.SessionId +} + +func (p *GraphServiceExecuteJsonWithParameterArgs) GetStmt() []byte { + return p.Stmt +} + +func (p *GraphServiceExecuteJsonWithParameterArgs) GetParameterMap() map[string]*nebula0.Value { + return p.ParameterMap +} +func (p *GraphServiceExecuteJsonWithParameterArgs) Read(iprot thrift.Protocol) error { + if _, err := iprot.ReadStructBegin(); err != nil { + return thrift.PrependError(fmt.Sprintf("%T read error: ", p), err) + } + + + for { + _, fieldTypeId, fieldId, err := iprot.ReadFieldBegin() + if err != nil { + return thrift.PrependError(fmt.Sprintf("%T field %d read error: ", p, fieldId), err) + } + if fieldTypeId == thrift.STOP { break; } + switch fieldId { + case 1: + if err := p.ReadField1(iprot); err != nil { + return err + } + case 2: + if err := p.ReadField2(iprot); err != nil { + return err + } + case 3: + if err := p.ReadField3(iprot); err != nil { + return err + } + default: + if err := iprot.Skip(fieldTypeId); err != nil { + return err + } + } + if err := iprot.ReadFieldEnd(); err != nil { + return err + } + } + if err := iprot.ReadStructEnd(); err != nil { + return thrift.PrependError(fmt.Sprintf("%T read struct end error: ", p), err) + } + return nil +} + +func (p *GraphServiceExecuteJsonWithParameterArgs) ReadField1(iprot thrift.Protocol) error { + if v, err := iprot.ReadI64(); err != nil { + return thrift.PrependError("error reading field 1: ", err) +} else { + p.SessionId = v +} + return nil +} + +func (p *GraphServiceExecuteJsonWithParameterArgs) ReadField2(iprot thrift.Protocol) error { + if v, err := iprot.ReadBinary(); err != nil { + return thrift.PrependError("error reading field 2: ", err) +} else { + p.Stmt = v +} + return nil +} + +func (p *GraphServiceExecuteJsonWithParameterArgs) ReadField3(iprot thrift.Protocol) error { + _, _, size, err := iprot.ReadMapBegin() + if err != nil { + return thrift.PrependError("error reading map begin: ", err) + } + tMap := make(map[string]*nebula0.Value, size) + p.ParameterMap = tMap + for i := 0; i < size; i ++ { +var _key13 string + if v, err := iprot.ReadString(); err != nil { + return thrift.PrependError("error reading field 0: ", err) +} else { + _key13 = v +} + _val14 := nebula0.NewValue() + if err := _val14.Read(iprot); err != nil { + return thrift.PrependError(fmt.Sprintf("%T error reading struct: ", _val14), err) + } + p.ParameterMap[_key13] = _val14 + } + if err := iprot.ReadMapEnd(); err != nil { + return thrift.PrependError("error reading map end: ", err) + } + return nil +} + +func (p *GraphServiceExecuteJsonWithParameterArgs) Write(oprot thrift.Protocol) error { + if err := oprot.WriteStructBegin("executeJsonWithParameter_args"); err != nil { + return thrift.PrependError(fmt.Sprintf("%T write struct begin error: ", p), err) } + if err := p.writeField1(oprot); err != nil { return err } + if err := p.writeField2(oprot); err != nil { return err } + if err := p.writeField3(oprot); err != nil { return err } + if err := oprot.WriteFieldStop(); err != nil { + return thrift.PrependError("write field stop error: ", err) } + if err := oprot.WriteStructEnd(); err != nil { + return thrift.PrependError("write struct stop error: ", err) } + return nil +} + +func (p *GraphServiceExecuteJsonWithParameterArgs) writeField1(oprot thrift.Protocol) (err error) { + if err := oprot.WriteFieldBegin("sessionId", thrift.I64, 1); err != nil { + return thrift.PrependError(fmt.Sprintf("%T write field begin error 1:sessionId: ", p), err) } + if err := oprot.WriteI64(int64(p.SessionId)); err != nil { + return thrift.PrependError(fmt.Sprintf("%T.sessionId (1) field write error: ", p), err) } + if err := oprot.WriteFieldEnd(); err != nil { + return thrift.PrependError(fmt.Sprintf("%T write field end error 1:sessionId: ", p), err) } + return err +} + +func (p *GraphServiceExecuteJsonWithParameterArgs) writeField2(oprot thrift.Protocol) (err error) { + if err := oprot.WriteFieldBegin("stmt", thrift.STRING, 2); err != nil { + return thrift.PrependError(fmt.Sprintf("%T write field begin error 2:stmt: ", p), err) } + if err := oprot.WriteBinary(p.Stmt); err != nil { + return thrift.PrependError(fmt.Sprintf("%T.stmt (2) field write error: ", p), err) } + if err := oprot.WriteFieldEnd(); err != nil { + return thrift.PrependError(fmt.Sprintf("%T write field end error 2:stmt: ", p), err) } + return err +} + +func (p *GraphServiceExecuteJsonWithParameterArgs) writeField3(oprot thrift.Protocol) (err error) { + if err := oprot.WriteFieldBegin("parameterMap", thrift.MAP, 3); err != nil { + return thrift.PrependError(fmt.Sprintf("%T write field begin error 3:parameterMap: ", p), err) } + if err := oprot.WriteMapBegin(thrift.STRING, thrift.STRUCT, len(p.ParameterMap)); err != nil { + return thrift.PrependError("error writing map begin: ", err) + } + for k, v := range p.ParameterMap { + if err := oprot.WriteString(string(k)); err != nil { + return thrift.PrependError(fmt.Sprintf("%T. (0) field write error: ", p), err) } + if err := v.Write(oprot); err != nil { + return thrift.PrependError(fmt.Sprintf("%T error writing struct: ", v), err) + } + } + if err := oprot.WriteMapEnd(); err != nil { + return thrift.PrependError("error writing map end: ", err) + } + if err := oprot.WriteFieldEnd(); err != nil { + return thrift.PrependError(fmt.Sprintf("%T write field end error 3:parameterMap: ", p), err) } + return err +} + +func (p *GraphServiceExecuteJsonWithParameterArgs) String() string { + if p == nil { + return "" + } + + sessionIdVal := fmt.Sprintf("%v", p.SessionId) + stmtVal := fmt.Sprintf("%v", p.Stmt) + parameterMapVal := fmt.Sprintf("%v", p.ParameterMap) + return fmt.Sprintf("GraphServiceExecuteJsonWithParameterArgs({SessionId:%s Stmt:%s ParameterMap:%s})", sessionIdVal, stmtVal, parameterMapVal) +} + +// Attributes: +// - Success +type GraphServiceExecuteJsonWithParameterResult struct { + thrift.IResponse + Success []byte `thrift:"success,0" db:"success" json:"success,omitempty"` +} + +func NewGraphServiceExecuteJsonWithParameterResult() *GraphServiceExecuteJsonWithParameterResult { + return &GraphServiceExecuteJsonWithParameterResult{} +} + +var GraphServiceExecuteJsonWithParameterResult_Success_DEFAULT []byte + +func (p *GraphServiceExecuteJsonWithParameterResult) GetSuccess() []byte { + return p.Success +} +func (p *GraphServiceExecuteJsonWithParameterResult) IsSetSuccess() bool { + return p != nil && p.Success != nil +} + +func (p *GraphServiceExecuteJsonWithParameterResult) Read(iprot thrift.Protocol) error { + if _, err := iprot.ReadStructBegin(); err != nil { + return thrift.PrependError(fmt.Sprintf("%T read error: ", p), err) + } + + + for { + _, fieldTypeId, fieldId, err := iprot.ReadFieldBegin() + if err != nil { + return thrift.PrependError(fmt.Sprintf("%T field %d read error: ", p, fieldId), err) + } + if fieldTypeId == thrift.STOP { break; } + switch fieldId { + case 0: + if err := p.ReadField0(iprot); err != nil { + return err + } + default: + if err := iprot.Skip(fieldTypeId); err != nil { + return err + } + } + if err := iprot.ReadFieldEnd(); err != nil { + return err + } + } + if err := iprot.ReadStructEnd(); err != nil { + return thrift.PrependError(fmt.Sprintf("%T read struct end error: ", p), err) + } + return nil +} + +func (p *GraphServiceExecuteJsonWithParameterResult) ReadField0(iprot thrift.Protocol) error { + if v, err := iprot.ReadBinary(); err != nil { + return thrift.PrependError("error reading field 0: ", err) +} else { + p.Success = v +} + return nil +} + +func (p *GraphServiceExecuteJsonWithParameterResult) Write(oprot thrift.Protocol) error { + if err := oprot.WriteStructBegin("executeJsonWithParameter_result"); err != nil { + return thrift.PrependError(fmt.Sprintf("%T write struct begin error: ", p), err) } + if err := p.writeField0(oprot); err != nil { return err } + if err := oprot.WriteFieldStop(); err != nil { + return thrift.PrependError("write field stop error: ", err) } + if err := oprot.WriteStructEnd(); err != nil { + return thrift.PrependError("write struct stop error: ", err) } + return nil +} + +func (p *GraphServiceExecuteJsonWithParameterResult) writeField0(oprot thrift.Protocol) (err error) { + if p.IsSetSuccess() { + if err := oprot.WriteFieldBegin("success", thrift.STRING, 0); err != nil { + return thrift.PrependError(fmt.Sprintf("%T write field begin error 0:success: ", p), err) } + if err := oprot.WriteBinary(p.Success); err != nil { + return thrift.PrependError(fmt.Sprintf("%T.success (0) field write error: ", p), err) } + if err := oprot.WriteFieldEnd(); err != nil { + return thrift.PrependError(fmt.Sprintf("%T write field end error 0:success: ", p), err) } + } + return err +} + +func (p *GraphServiceExecuteJsonWithParameterResult) String() string { + if p == nil { + return "" + } + + successVal := fmt.Sprintf("%v", p.Success) + return fmt.Sprintf("GraphServiceExecuteJsonWithParameterResult({Success:%s})", successVal) } // Attributes: diff --git a/session.go b/session.go index b15e22df..bedc1abd 100644 --- a/session.go +++ b/session.go @@ -75,6 +75,48 @@ func (session *Session) Execute(stmt string) (*ResultSet, error) { } } +// Execute returns the result of given query as a ResultSet +func (session *Session) ExecuteWithParameter(stmt string, params map[string]*nebula.Value) (*ResultSet, error) { + if session.connection == nil { + return nil, fmt.Errorf("failed to execute: Session has been released") + } + resp, err := session.connection.executeWithParameter(session.sessionID, stmt, params) + if err == nil { + resSet, err := genResultSet(resp, session.timezoneInfo) + if err != nil { + return nil, err + } + return resSet, nil + } + // Reconnect only if the tranport is closed + err2, ok := err.(thrift.TransportException) + if !ok { + return nil, err + } + if err2.TypeID() == thrift.END_OF_FILE { + _err := session.reConnect() + if _err != nil { + session.log.Error(fmt.Sprintf("Failed to reconnect, %s", _err.Error())) + return nil, _err + } + session.log.Info(fmt.Sprintf("Successfully reconnect to host: %s, port: %d", + session.connection.severAddress.Host, session.connection.severAddress.Port)) + // Execute with the new connetion + resp, err := session.connection.executeWithParameter(session.sessionID, stmt, params) + if err != nil { + return nil, err + } + resSet, err := genResultSet(resp, session.timezoneInfo) + if err != nil { + return nil, err + } + return resSet, nil + } else { // No need to reconnect + session.log.Error(fmt.Sprintf("Error info: %s", err2.Error())) + return nil, err2 + } +} + // ExecuteJson returns the result of the given query as a json string // Date and Datetime will be returned in UTC // JSON struct: