EXASockets  0.9
Interface to the EXASOL DB via JSON over Websockets protocol
exasockets.h
1 /*
2 websockets.cpp and websockets.h
3 http://www.github.com/marcelboldt/EXASockets
4 
5  The MIT License (MIT)
6 
7 Copyright (C) 2016 Marcel Boldt / EXASOL
8 
9 Permission is hereby granted, free of charge, to any person obtaining a copy
10 of this software and associated documentation files (the "Software"), to deal
11 in the Software without restriction, including without limitation the rights
12 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13 copies of the Software, and to permit persons to whom the Software is
14 furnished to do so, subject to the following conditions:
15 
16 The above copyright notice and this permission notice shall be included in all
17 copies or substantial portions of the Software.
18 
19 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
25 SOFTWARE.
26 
27 Marcel Boldt <marcel.boldt@exasol.com>
28 
29 */
30 
31 #ifndef EXASOCKETS_EXASOCKETS_H
32 #define EXASOCKETS_EXASOCKETS_H
33 
34 #include <websockets/websockets.h>
35 #include "osname.h"
36 #include "exaResultSet.h"
37 
38 #include <rapidjson/document.h>
39 #include <rapidjson/writer.h>
40 #include <rapidjson/stringbuffer.h>
41 #include <rapidjson/allocators.h>
42 #include <iostream>
43 #include <openssl/pem.h>
44 #include <openssl/rsa.h>
45 #include <openssl/bio.h>
46 #include <openssl/err.h>
47 #include <omp.h>
48 
49 
50  // gethostname, getlogin
51 
52 #define __USE_POSIX
53 
54 #include <limits.h> // HOST_NAME_MAX
55 
56 #ifndef HOST_NAME_MAX
57 #define HOST_NAME_MAX 16
58 #endif
59 
60 #define DRV_NAME "EXASOCKETS 0.1"
61 #define FETCH_BYTES 2 * 1048576//2097152//10485760
62 
63 struct exas_rsa_bio_create : public std::exception {
64  const char * what() const throw () {
65  return "Error: failed to create RSA bio";
66  }
67  };
68 
69  struct exas_rsa_create : public std::exception {
70  const char * what() const throw () {
71  return "Error: failed to create RSA";
72  }
73  };
74 
76 public:
78 
92  exasockets_connection(const char *server,
93  uint16_t port,
94  const char *clientName,
95  const char *username,
96  const char *password,
97  int pwd_len,
98  bool autocommit = true,
99  bool use_compression = false,
100  uint64_t sessionId = 0, // 0 -> create randomly
101  bool debug = false
102  );
103 
104  virtual ~exasockets_connection();
105 
106  static int StringToExaDatatype(const char *str);
107 
108  static char *ExaDatatypeToString(const int type);
109 
111 
116  int disconnect(bool throw_message = true);
117 
118  int update_session_attributes();
119 
120 
122 
128  virtual exaResultSetHandler *exec_sql(char *sql);
129 
131 
140  int64_t
141  fetch(exaResultSetHandler *rs, uint64_t numRows = 10000, uint64_t startPosition = 1,
142  uint64_t numBytes = 10485760);
143 
145 
150  virtual int close_result_set(exaResultSetHandler &rs);
151 
153 
158  virtual int close_result_set(int handle);
159 
161 
166  virtual exaResultSetHandler *create_prepared_insert(char *sql);
167 
168 
169  virtual int create_prepared_insert_int(char *sql);
170 
172 
176  virtual void exec_prepared_insert(exaResultSetHandler &rs);
177 
178 
180 
185  virtual int close_prepared(exaResultSetHandler &rs);
186 
187  const char *databaseName() const;
188  const char *identifierQuoteString() const;
189  int maxDataMessageSize() const;
190  int maxIdentifierLength() const;
191  int maxVarcharLength() const;
192  const char *productName() const;
193  int protocolVersion() const;
194  const char *releaseVersion() const;
195  uint64_t session_id() const;
196  const char *timezone() const;
197  const char *timeZoneBehaviour() const;
198  bool isStatus() const;
199 
200  rapidjson::Document resultSet;
201  rapidjson::Value *data; // TODO: remove this
202  bool json_debug_output = false; // set to true for cmd line output of all JSON elements
203 
204 
205 protected:
206  static void append_data_from_Rapid_JSON_Document(exaResultSetHandler *rs, const rapidjson::Value &JSONdata);
207 
208  static exaResultSetHandler *
209  create_exaResultSetHandler_from_RapidJSON_Document(const rapidjson::Value &JSONresultSet, int handle = -1);
210 
211  Websockets_connection *ws_con = nullptr;
212  rapidjson::Document d; // for fetch() - the result set containing a dataset
213  std::stringstream tfile; // ifstream
214  char *logfile = nullptr; // if nullptr, then the tempfile will be names "EXASockets %timestamp% %random_number%.tmp"
215 
216  const char *c_databaseName;
217  const char *c_identifierQuoteString;
218  int c_maxDataMessageSize;
219  int c_maxIdentifierLength;
220  int c_maxVarcharLength;
221  const char *c_productName;
222  int c_protocolVersion;
223  const char *c_releaseVersion;
224  uint64_t c_session_id;
225  const char *c_timezone;
226  const char *c_timeZoneBehaviour; // change to predef
227  bool status;
228 
229 
230  int ws_send_data(const char *data, int len = -1, int type = 1);
231 
232  std::string ws_receive_data();
233 
234 };
235 
236 #endif //EXASOCKETS_EXASOCKETS_H
Definition: exasockets.h:63
Definition: exaResultSet.h:205
Definition: exasockets.h:69
Definition: exasockets.h:75