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