-
Notifications
You must be signed in to change notification settings - Fork 9
/
SQLClient.cpp
114 lines (96 loc) · 3.34 KB
/
SQLClient.cpp
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
#include "SQLClient.h"
SQLClient::SQLClient(const char * host, unsigned int port) {
hostname = new char[strlen(host) + 1];
strcpy(hostname, host);
setLogin = false;
SQLLogged = false;
SQLPort = port;
}
void SQLClient::login(const char * user, const char * passwd) {
username = new char[strlen(user) + 1];
strcpy(username, user);
password = new char[strlen(passwd) + 1];
strcpy(password, passwd);
setLogin = true;
}
bool SQLClient::connect(const char * databaseName) {
SQLRETURN sqlReturn;
SQLCHAR inBuff[CON_LEN];
SWORD sLen;
bool result = false;
if (SQLLogged) { SQLLogged = false; close(); }
if (InConnectionString != NULL) {
if (setLogin) {
if (SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &hEnvironment) == SQL_SUCCESS) {
if (SQLSetEnvAttr(hEnvironment, SQL_ATTR_ODBC_VERSION,(SQLPOINTER) SQL_OV_ODBC3, SQL_IS_INTEGER) == SQL_SUCCESS) {
if ((SQLAllocHandle(SQL_HANDLE_DBC, hEnvironment, (SQLHDBC FAR *) &hDbc)) == SQL_SUCCESS) {
sqlReturn = SQLDriverConnect(hDbc, NULL, InConnectionString, strlen((char *) InConnectionString), inBuff, CON_LEN - 1, &sLen, SQL_DRIVER_COMPLETE_REQUIRED);
if ((sqlReturn == SQL_SUCCESS) || (sqlReturn == SQL_SUCCESS_WITH_INFO)) {
SQLAllocStmt(hDbc, &hStmt);
result = true; SQLLogged = true;
database = (char *) malloc(strlen(databaseName) + 1);
strcpy(database, databaseName);
}
else {
SQLFreeHandle(SQL_HANDLE_DBC, &hDbc);
SQLFreeHandle(SQL_HANDLE_ENV, &hEnvironment);
}
}
else SQLFreeHandle(SQL_HANDLE_ENV, &hEnvironment);
}
}
}
free(InConnectionString);
}
return result;
}
bool SQLClient::execSQL(const char * SQLstatement) {
bool result = false;
if (SQLLogged) {
if (SQLExecDirect(hStmt, (SQLCHAR *) SQLstatement, strlen(SQLstatement)) == SQL_SUCCESS) result = true;
}
return result;
}
bool SQLClient::close() {
if (SQLLogged) {
SQLDisconnect(hDbc);
SQLFreeHandle(SQL_HANDLE_DBC, &hDbc);
SQLFreeHandle(SQL_HANDLE_ENV, &hEnvironment);
SQLFreeHandle(SQL_HANDLE_STMT, &hStmt);
return true;
}
return false;
}
void SQLClient::setHostname(const char * host) {
delete [] hostname;
hostname = new char[strlen(host) + 1];
strcpy(hostname, host);
}
void SQLClient::setUsername(const char * user) {
delete [] username;
username = new char[strlen(user) + 1];
strcpy(username, user);
}
void SQLClient::setPassword(const char * passwd) {
delete [] password;
password = new char[strlen(passwd) + 1];
strcpy(password, passwd);
}
void SQLClient::setDatabase(const char * databaseName) {
delete [] database;
database = new char[strlen(databaseName) + 1];
strcpy(database, databaseName);
}
char * SQLClient::getHostname() { return hostname; }
char * SQLClient::getUsername() {
if (setLogin) return username;
else return NULL;
}
char * SQLClient::getPassword() {
if (setLogin) return password;
else return NULL;
}
char * SQLClient::getDatabase() {
if (SQLLogged) return database;
else return NULL;
}