NebulaGraph CPP Client  release-3.8
Connection.h
1 /* Copyright (c) 2020 vesoft inc. All rights reserved.
2  *
3  * This source code is licensed under Apache 2.0 License.
4  */
5 
6 #pragma once
7 
8 #include <functional>
9 #include <memory>
10 #include <string>
11 
12 #include "common/datatypes/Value.h"
13 #include "common/graph/Response.h"
14 
15 namespace folly {
16 class ScopedEventBaseThread;
17 }
18 
19 namespace nebula {
20 
21 // Wrap the thrift client.
22 namespace graph {
23 namespace cpp2 {
24 class GraphServiceAsyncClient;
25 }
26 } // namespace graph
27 
28 class Connection {
29  public:
30  using ExecuteCallback = std::function<void(ExecutionResponse &&)>;
31  using ExecuteJsonCallback = std::function<void(std::string &&)>;
32 
33  Connection();
34  // disable copy
35  Connection(const Connection &) = delete;
36  Connection &operator=(const Connection &c) = delete;
37 
38  Connection(Connection &&c) noexcept {
39  client_ = c.client_;
40  c.client_ = nullptr;
41 
42  clientLoopThread_ = c.clientLoopThread_;
43  c.clientLoopThread_ = nullptr;
44  }
45 
46  Connection &operator=(Connection &&c);
47 
48  ~Connection();
49 
50  bool open(const std::string &address,
51  int32_t port,
52  uint32_t timeout,
53  bool enableSSL,
54  const std::string &CAPath);
55 
56  AuthResponse authenticate(const std::string &user, const std::string &password);
57 
58  ExecutionResponse execute(int64_t sessionId, const std::string &stmt);
59 
60  void asyncExecute(int64_t sessionId, const std::string &stmt, ExecuteCallback cb);
61 
62  ExecutionResponse executeWithParameter(int64_t sessionId,
63  const std::string &stmt,
64  const std::unordered_map<std::string, Value> &parameters);
65 
66  void asyncExecuteWithParameter(int64_t sessionId,
67  const std::string &stmt,
68  const std::unordered_map<std::string, Value> &parameters,
69  ExecuteCallback cb);
70 
71  std::string executeJson(int64_t sessionId, const std::string &stmt);
72 
73  void asyncExecuteJson(int64_t sessionId, const std::string &stmt, ExecuteJsonCallback cb);
74 
75  std::string executeJsonWithParameter(int64_t sessionId,
76  const std::string &stmt,
77  const std::unordered_map<std::string, Value> &parameters);
78 
79  void asyncExecuteJsonWithParameter(int64_t sessionId,
80  const std::string &stmt,
81  const std::unordered_map<std::string, Value> &parameters,
82  ExecuteJsonCallback cb);
83 
84  VerifyClientVersionResp verifyClientVersion(const VerifyClientVersionReq &req);
85 
86  bool isOpen();
87 
88  void close();
89 
90  bool ping();
91 
92  void signout(int64_t sessionId);
93 
94  private:
95  graph::cpp2::GraphServiceAsyncClient *client_{nullptr};
96  folly::ScopedEventBaseThread *clientLoopThread_{nullptr};
97 };
98 
99 } // namespace nebula