forked from voipmonitor/sniffer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
odbc.cpp
169 lines (156 loc) · 4.38 KB
/
odbc.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
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
#include <string.h>
#include <stdio.h>
#include <iostream>
#include <sql.h>
#include "voipmonitor.h"
#include "odbc.h"
Odbc::Odbc() {
this->hEnvironment = NULL;
this->hConnection = NULL;
this->hStatement = NULL;
this->lastError = SQL_SUCCESS;
this->lastErrorNative = 0;
this->lastErrorString = NULL;
}
Odbc::~Odbc() {
this->disconnect();
this->clearLastError();
}
bool Odbc::connect(const char *serverName, const char *userName, const char *password,
ulong odbcVersion, ulong loginTimeOut) {
SQLRETURN rslt;
this->clearLastError();
if(!this->hEnvironment) {
rslt = SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &this->hEnvironment);
if(!this->okRslt(rslt)) {
this->lastError = rslt;
this->setLastErrorString("error in allocate environment handle");
this->disconnect();
return false;
}
if(odbcVersion) {
rslt = SQLSetEnvAttr(hEnvironment, SQL_ATTR_ODBC_VERSION, (SQLPOINTER*)odbcVersion, 0);
if(!this->okRslt(rslt)) {
this->lastError = rslt;
this->setLastErrorString("error in set environment attributes");
this->disconnect();
return false;
}
}
}
if(!this->hConnection) {
rslt = SQLAllocHandle(SQL_HANDLE_DBC, this->hEnvironment, &this->hConnection);
if(!this->okRslt(rslt)) {
this->lastError = rslt;
this->setLastErrorString("error in allocate connection handle");
this->disconnect();
return false;
}
if(loginTimeOut) {
SQLSetConnectAttr(this->hConnection, SQL_LOGIN_TIMEOUT, (SQLPOINTER *)loginTimeOut, 0);
}
rslt = SQLConnect(this->hConnection,
(SQLCHAR*)serverName, SQL_NTS,
(SQLCHAR*)userName, SQL_NTS,
(SQLCHAR*)password, SQL_NTS);
if(!this->okRslt(rslt)) {
this->lastError = rslt;
this->diagError(SQL_HANDLE_DBC);
this->disconnect();
return(false);
}
}
if(!this->hStatement) {
rslt = SQLAllocHandle(SQL_HANDLE_STMT, hConnection, &hStatement);
if(!this->okRslt(rslt)) {
this->lastError = rslt;
this->diagError(SQL_HANDLE_DBC);
this->disconnect();
return(false);
}
}
return true;
}
void Odbc::disconnect() {
if(this->hStatement) {
SQLFreeHandle(SQL_HANDLE_STMT, this->hStatement);
this->hStatement = NULL;
}
if(this->hConnection) {
SQLDisconnect(this->hConnection);
SQLFreeHandle(SQL_HANDLE_DBC, this->hConnection);
this->hConnection = NULL;
}
if(this->hEnvironment) {
SQLFreeHandle(SQL_HANDLE_ENV, this->hEnvironment);
this->hEnvironment = NULL;
}
}
bool Odbc::connected() {
return this->hEnvironment && this->hConnection && this->hEnvironment;
}
void Odbc::bindCol(SQLUSMALLINT colNumber, SQLSMALLINT targetType, SQLPOINTER targetValuePtr,
SQLLEN targetBufferLength, SQLLEN *lenOrInd) {
SQLBindCol(this->hStatement, colNumber, targetType, targetValuePtr, targetBufferLength, lenOrInd);
}
bool Odbc::query(const char *query) {
SQLRETURN rslt = SQLExecDirect(this->hStatement, (SQLCHAR*)query, SQL_NTS);
if(!this->okRslt(rslt)) {
this->lastError = rslt;
this->diagError(SQL_HANDLE_STMT);
return false;
}
return true;
}
bool Odbc::fetchRow() {
SQLRETURN rslt = SQLFetch(hStatement);
if(!this->okRslt(rslt) && rslt != SQL_NO_DATA) {
this->lastError = rslt;
this->diagError(SQL_HANDLE_STMT);
return false;
}
return this->okRslt(rslt);
}
SQLLEN Odbc::getNumRows() {
SQLLEN numRows = -1;
SQLRETURN rslt = SQLRowCount(hStatement, &numRows);
if(!this->okRslt(rslt)) {
this->lastError = rslt;
this->diagError(SQL_HANDLE_STMT);
return -1;
}
return numRows;
}
void Odbc::diagError(SQLSMALLINT handleType) {
SQLCHAR sqlState[10];
SQLINTEGER nativeError;
SQLCHAR messageText[1000];
SQLSMALLINT messageTextLength;
SQLRETURN rslt = SQLGetDiagRec(handleType,
handleType == SQL_HANDLE_DBC ?
this->hConnection :
this->hStatement,
1, sqlState, &nativeError, messageText, sizeof(messageText), &messageTextLength);
if(this->okRslt(rslt)) {
this->lastErrorNative = nativeError;
this->setLastErrorString((char*)messageText);
}
}
void Odbc::setLastErrorString(const char *errorString) {
if(this->lastErrorString) {
delete [] this->lastErrorString;
this->lastErrorString = NULL;
}
if(errorString) {
this->lastErrorString = new FILE_LINE char[strlen(errorString)+1];
strcpy(this->lastErrorString, errorString);
}
}
void Odbc::clearLastError() {
this->lastError = SQL_SUCCESS;
this->lastErrorNative = 0;
if(this->lastErrorString) {
delete [] this->lastErrorString;
this->lastErrorString = NULL;
}
}