NebulaGraph CPP Client  release-3.8
Session.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 <common/datatypes/DataSet.h>
9 
10 #include <atomic>
11 
12 #include "nebula/client/Connection.h"
13 
14 namespace nebula {
15 
16 class ConnectionPool;
17 
18 class Session {
19  public:
20  using ExecuteCallback = std::function<void(ExecutionResponse &&)>;
21  using ExecuteJsonCallback = std::function<void(std::string &&)>;
22 
23  Session() = default;
24  Session(int64_t sessionId,
25  Connection &&conn,
26  ConnectionPool *pool,
27  const std::string &username,
28  const std::string &password,
29  const std::string &timezoneName,
30  int32_t offsetSecs,
31  bool retryConnect)
32  : sessionId_(sessionId),
33  conn_(std::move(conn)),
34  pool_(pool),
35  username_(username),
36  password_(password),
37  timezoneName_(timezoneName),
38  offsetSecs_(offsetSecs),
39  retryConnect_(retryConnect) {}
40  Session(const Session &) = delete; // no copy
41  Session(Session &&session)
42  : sessionId_(session.sessionId_),
43  conn_(std::move(session.conn_)),
44  pool_(session.pool_),
45  username_(std::move(session.username_)),
46  password_(std::move(session.password_)),
47  timezoneName_(std::move(session.timezoneName_)),
48  offsetSecs_(session.offsetSecs_),
49  retryConnect_(session.retryConnect_) {
50  session.sessionId_ = -1;
51  session.pool_ = nullptr;
52  session.offsetSecs_ = 0;
53  }
54  void operator=(Session &&session) {
55  sessionId_ = session.sessionId_;
56  username_ = std::move(session.username_);
57  password_ = std::move(session.password_);
58  timezoneName_ = std::move(session.timezoneName_);
59  offsetSecs_ = session.offsetSecs_;
60  session.sessionId_ = -1;
61  session.pool_ = nullptr;
62  session.offsetSecs_ = 0;
63  }
64  ~Session() {
65  release();
66  }
67 
68  ExecutionResponse execute(const std::string &stmt);
69 
70  void asyncExecute(const std::string &stmt, ExecuteCallback cb);
71 
72  ExecutionResponse executeWithParameter(const std::string &stmt,
73  const std::unordered_map<std::string, Value> &parameters);
74 
75  void asyncExecuteWithParameter(const std::string &stmt,
76  const std::unordered_map<std::string, Value> &parameters,
77  ExecuteCallback cb);
78 
79  std::string executeJson(const std::string &stmt);
80 
81  void asyncExecuteJson(const std::string &stmt, ExecuteJsonCallback cb);
82 
83  std::string executeJsonWithParameter(const std::string &stmt,
84  const std::unordered_map<std::string, Value> &parameters);
85 
86  void asyncExecuteJsonWithParameter(const std::string &stmt,
87  const std::unordered_map<std::string, Value> &parameters,
88  ExecuteJsonCallback cb);
89 
90  bool ping();
91 
92  ErrorCode retryConnect();
93 
94  void release();
95 
96  bool valid() const {
97  return sessionId_ > 0;
98  }
99 
100  const std::string &timeZoneName() const {
101  return timezoneName_;
102  }
103 
104  int32_t timeZoneOffsetSecs() const {
105  return offsetSecs_;
106  }
107 
108  // convert the time to server time zone
109  void toLocal(DataSet &data) {
110  return toLocal(data, offsetSecs_);
111  }
112 
113  static bool isSessionError(const ExecutionResponse &resp) {
114  return resp.errorCode == ErrorCode::E_SESSION_INVALID ||
115  resp.errorCode == ErrorCode::E_SESSION_NOT_FOUND;
116  }
117 
118  // convert the time to specific time zone
119  static void toLocal(DataSet &data, int32_t offsetSecs);
120 
121  private:
122  int64_t sessionId_{-1};
123  Connection conn_;
124  ConnectionPool *pool_{nullptr};
125  std::string username_;
126  std::string password_;
127  // empty means not a named timezone
128  std::string timezoneName_;
129  int32_t offsetSecs_;
130  bool retryConnect_{true};
131  std::atomic<bool> connectionIsBroken_{false};
132 };
133 
134 } // namespace nebula