forked from fsystech/npgsql_wrapper
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpgsql_lib.cpp
More file actions
284 lines (279 loc) · 10.7 KB
/
Copy pathpgsql_lib.cpp
File metadata and controls
284 lines (279 loc) · 10.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
/**
* Copyright (c) 2018, SOW (https://www.safeonline.world). (https://github.com/RKTUXYN) All rights reserved.
* @author {SOW}
* Copyrights licensed under the New BSD License.
* See the accompanying LICENSE file for terms.
*/
# include "pgsql_lib.h"
LPWSTR s2ws(const char* s) {
/*wchar_t wtext[FILENAME_MAX];
mbstowcs(wtext, s, strlen(s) + 1);//Plus null
LPWSTR ptr = wtext;*/
//size_t wn = mbsrtowcs(NULL, &s, 0, NULL);
wchar_t * buf = new wchar_t[_MAX_PATH]();
mbsrtowcs(buf, &s, _MAX_PATH, NULL);
LPWSTR& ptr = buf;
//delete[] buf;
return ptr;
}
pg_sql_lib::pg_sql_lib() {
_connected = false;
_pg_result = NULL; _conn = NULL; _pgsql_proc_iddl = NULL; _pgsql_module = NULL;
_PQclear = NULL; _PQerrorMessage = NULL; _PQexec = NULL; _PQfmod = NULL;
_PQfname = NULL; _PQfsize = NULL; _PQftype = NULL; _PQgetisnull = NULL;
_PQgetlength = NULL; _PQgetResult = NULL; _PQgetvalue = NULL; _PQnfields = NULL;
_PQntuples = NULL; _PQputCopyData = NULL; _PQputCopyEnd = NULL; _PQresultErrorMessage = NULL;
_PQresultStatus = NULL; _PQsetClientEncoding = NULL; _PQsetdbLogin = NULL; _PQstatus = NULL;
_PQconnectdb = NULL; _PQfinish = NULL;
_pq_error_text = new char;
_n_error_text = new char;
_n_error = 0; _pq_error = 0;
_copy_cols_count = 0; _cursor_rows_fetched = 0;
};
pg_sql_lib::~pg_sql_lib() {
if (_connected) { exit_nicely(); }
if (_pgsql_module != NULL) {
FreeLibrary(_pgsql_module);
}
_pg_result = NULL; _conn = NULL; _pgsql_proc_iddl = NULL; _pgsql_module = NULL;
_PQclear = NULL; _PQerrorMessage = NULL; _PQexec = NULL; _PQfmod = NULL;
_PQfname = NULL; _PQfsize = NULL; _PQftype = NULL; _PQgetisnull = NULL;
_PQgetlength = NULL; _PQgetResult = NULL; _PQgetvalue = NULL; _PQnfields = NULL;
_PQntuples = NULL; _PQputCopyData = NULL; _PQputCopyEnd = NULL; _PQresultErrorMessage = NULL;
_PQresultStatus = NULL; _PQsetClientEncoding = NULL; _PQsetdbLogin = NULL; _PQstatus = NULL;
_PQconnectdb = NULL; _PQfinish = NULL;
delete _pq_error_text; delete _n_error_text;
}
// Initialize API
int pg_sql_lib::init(const char* lib_path) {
if (_pgsql_module == NULL) {
// Try to load the library by default path
#if !(defined(_WIN32)||defined(_WIN64)) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__)))
_pgsql_module = dlopen(lib_path, RTLD_NOW);
#else
_pgsql_module = LoadLibraryEx(s2ws(lib_path), NULL, 0x00000008);
#endif
// DLL load failed
if (_pgsql_module == NULL) {
return -1;
}
}
// Get functions
if (_pgsql_module != NULL) {
_PQclear = (PQclearPQFunc)os_support::get_proc_address(_pgsql_module, "PQclear");
_PQerrorMessage = (PQerrorMessagePQFunc)os_support::get_proc_address(_pgsql_module, "PQerrorMessage");
_PQexec = (PQexecPQFunc)os_support::get_proc_address(_pgsql_module, "PQexec");
_PQfmod = (PQfmodPQFunc)os_support::get_proc_address(_pgsql_module, "PQfmod");
_PQfname = (PQfnamePQFunc)os_support::get_proc_address(_pgsql_module, "PQfname");
_PQfsize = (PQfsizePQFunc)os_support::get_proc_address(_pgsql_module, "PQfsize");
_PQftype = (PQftypePQFunc)os_support::get_proc_address(_pgsql_module, "PQftype");
_PQgetisnull = (PQgetisnullPQFunc)os_support::get_proc_address(_pgsql_module, "PQgetisnull");
_PQgetlength = (PQgetlengthPQFunc)os_support::get_proc_address(_pgsql_module, "PQgetlength");
_PQgetResult = (PQgetResultPQFunc)os_support::get_proc_address(_pgsql_module, "PQgetResult");
_PQgetvalue = (PQgetvaluePQFunc)os_support::get_proc_address(_pgsql_module, "PQgetvalue");
_PQnfields = (PQnfieldsPQFunc)os_support::get_proc_address(_pgsql_module, "PQnfields");
_PQntuples = (PQntuplesPQFunc)os_support::get_proc_address(_pgsql_module, "PQntuples");
_PQputCopyData = (PQputCopyDataPQFunc)os_support::get_proc_address(_pgsql_module, "PQputCopyData");
_PQputCopyEnd = (PQputCopyEndPQFunc)os_support::get_proc_address(_pgsql_module, "PQputCopyEnd");
_PQresultErrorMessage = (PQresultErrorMessagePQFunc)os_support::get_proc_address(_pgsql_module, "PQresultErrorMessage");
_PQresultStatus = (PQresultStatusPQFunc)os_support::get_proc_address(_pgsql_module, "PQresultStatus");
_PQsetClientEncoding = (PQsetClientEncodingPQFunc)os_support::get_proc_address(_pgsql_module, "PQsetClientEncoding");
_PQsetdbLogin = (PQsetdbLoginPQFunc)os_support::get_proc_address(_pgsql_module, "PQsetdbLogin");
_PQstatus = (PQstatusPQFunc)os_support::get_proc_address(_pgsql_module, "PQstatus");
_PQconnectdb = (PQconnectdbPQFunc)os_support::get_proc_address(_pgsql_module, "PQconnectdb");
_PQfinish = (PQfinishPQFunc)os_support::get_proc_address(_pgsql_module, "PQfinish");
if (_PQclear == NULL || _PQerrorMessage == NULL || _PQexec == NULL || _PQfmod == NULL ||
_PQfname == NULL || _PQfsize == NULL || _PQftype == NULL || _PQgetisnull == NULL ||
_PQgetlength == NULL || _PQgetResult == NULL || _PQgetvalue == NULL || _PQnfields == NULL ||
_PQntuples == NULL || _PQputCopyData == NULL || _PQputCopyEnd == NULL ||
_PQresultErrorMessage == NULL || _PQresultStatus == NULL || _PQsetClientEncoding == NULL ||
_PQsetdbLogin == NULL || _PQstatus == NULL)
return -1;
}
else {
os_support::get_last_error_text(LIBPQ_DLL_LOAD_ERROR, _n_error_text, 1024);
return -1;
}
return 1;
};
void pg_sql_lib::parse_connection_string(const char * conn, std::string & user, std::string & pwd, std::string & server, std::string & port, std::string & db) {
if (conn == NULL)
return;
std::string* query = new std::string(conn);
std::regex pattern("([\\w+%]+)=([^;]*)");
std::map<std::string, std::string> conn_obj;
auto words_begin = std::sregex_iterator(query->begin(), query->end(), pattern);
auto words_end = std::sregex_iterator();
for (std::sregex_iterator i = words_begin; i != words_end; i++) {
std::string key = (*i)[1].str();
std::string value = (*i)[2].str();
conn_obj[key] = value;
};
delete query;
user = conn_obj["UserId"];
pwd = conn_obj["Password"];
db = conn_obj["Database"];
server = conn_obj["Server"];
port = conn_obj["Port"];
conn_obj.clear();
};
int pg_sql_lib::connect(const char *conn) {
// Check if already connected
if (_connected == true)
return 1;
std::string*user = new std::string();
std::string*pwd = new std::string();
std::string*server = new std::string();
std::string*port = new std::string();
std::string*db = new std::string();
parse_connection_string(conn, *user, *pwd, *server, *port, *db);
/*_PQsetdbLogin(
const char *pghost, const char *pgport,
const char *pgoptions, const char *pgtty,
const char *dbName, const char *login,
const char *pwd
)*/
_conn = _PQsetdbLogin(
(server->empty() ? NULL : server->c_str()),
(port->empty() ? NULL : port->c_str()), NULL, NULL,
(db->empty() ? NULL : db->c_str()),
(user->empty() ? NULL : user->c_str()),
(pwd->empty() ? NULL : pwd->c_str()));
user->clear(); pwd->clear(); server->clear(); port->clear(); db->clear();
delete user; delete pwd; delete server; delete port; delete db;
if (_PQstatus(_conn) != CONNECTION_OK) {
set_error();
return -1;
}
_connected = true;
return 1;
};
void pg_sql_lib::exit_nicely() {
if (!_connected)return;
_PQfinish (_conn);
return;
};
// Get row count for the specified object
int pg_sql_lib::get_row_count(const char *object, char *value) {
if (((object != NULL) && (object[0] == '\0')) || object == NULL) {
return -1;
}
std::string query = "SELECT COUNT(*) FROM ";
query += object;
// Execute the query
int rc = execute_scalar(query.c_str(), value);
query.clear();
return rc;
};
int pg_sql_lib::execute_scalar(const char *query, char *value) {
if ((query != NULL) && (query[0] == '\0'))return -1;
// Execute the statement
PGresult *res = _PQexec(_conn, query);
bool exists = false;
// Get the value of the first column of the first row
if (_PQresultStatus(res) == PGRES_TUPLES_OK) {
value = _PQgetvalue(res, 0, 0);
exists = true;
}
else {
value = NULL;
set_error();
}
_PQclear(res);
return (exists == true) ? 0 : -1;
};
//https://www.postgresql.org/docs/8.3/libpq-example.html
// Execute the statement and get scalar result
int pg_sql_lib::execute_scalar_x(const char *query, std::list< std::string>&out_param_array, std::map<std::string, char*>&out_param_map) {
if (((query != NULL) && (query[0] == '\0')) || query == NULL) {
return -1;
}
bool exists = false;
try {
// Execute the statement
PGresult *res = _PQexec(_conn, query);
// Get the value of the first column of the first row
if (_PQresultStatus(res) == PGRES_TUPLES_OK) {
int field_map = 0;
std::string prop;
for (auto s = out_param_array.begin(); s != out_param_array.end(); ++s) {
prop = *s;
char* resp = _PQgetvalue(res, 0, field_map);
char* copy_resp = new char[strlen(resp) + 1];
strcpy(copy_resp, resp);
out_param_map[prop] = copy_resp;
field_map++;
}
exists = true;
}
else set_error();
_PQclear(res);
} catch (std::exception&e) {
this->set_error(e.what());
}
return (exists == true) ? 0 : -1;
};
// Execute the statement
int pg_sql_lib::execute_non_query(const char *query) {
if (((query != NULL) && (query[0] == '\0')) || query == NULL) {
return -1;
}
// Execute the query
PGresult *result = _PQexec(_conn, query);
bool error = false;
int rc = _PQresultStatus(result);
// Error raised
if (rc != PGRES_COMMAND_OK) {
set_error();
error = true;
}
_PQclear(result);
return (error == true) ? -1 : 0;
};
const int pg_sql_lib::is_api_error() {
return (_n_error >= 0 && _pq_error >= 0) ? -1 : 0;
};
const char * pg_sql_lib::get_last_error() {
if (_n_error >= 0 && _pq_error >= 0)return nullptr;
if (_pq_error < 0) {
return const_cast<const char*>(_pq_error_text);
}
return const_cast<const char*>(_n_error_text);
};
// Set error code and message for the last API call
void pg_sql_lib::set_error() {
_pq_error = -1;
char* erro_msg = _PQerrorMessage(_conn);
_pq_error_text = new char[strlen(erro_msg) + 1];
strcpy(_pq_error_text, erro_msg);
_n_error = 0;
};
void pg_sql_lib::set_error(const char* std_error_msg) {
_n_error = -1;
_n_error_text = new char[strlen(std_error_msg) + 1];
strcpy(_n_error_text, std_error_msg);
_pq_error = 0;
};
#if !(defined(_WIN32)||defined(_WIN64)) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__)))
int pg_sql_lib::load_pgsql_lib(const char* name) {
if (_pgsql_proc_iddl) {
return 1;
}
_pgsql_proc_iddl = os_support::load_library(name);
if (!_pgsql_proc_iddl) {
return -1;
}
return 1;
};
#else
int pg_sql_lib::load_pgsql_lib(const char* name) {
if (_pgsql_proc_iddl) {
return 1;
}
_pgsql_proc_iddl = os_support::load_library(s2ws(name));
if (!_pgsql_proc_iddl) {
return -1;
}
return 1;
};
#endif