-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCDatabase.cpp
531 lines (428 loc) · 14.9 KB
/
CDatabase.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
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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
#include "CDatabase.h"
namespace ODBCDatabase
{
// mutex to call SQLDriverConnectW. This func crashes in multythread
boost::recursive_mutex driverConnect;
CDatabase::CDatabase(const wstring delim)
{
connected_ = false;
delim_ = delim;
answer_ = L"";
RETCODE retCode;
hEnv_ = NULL;
hDbc_ = NULL;
hStmt_ = NULL;
// Allocate an environment
if( SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &hEnv_) == SQL_ERROR )
{
LOG(WARNING) << "Unable to allocate an environment handle (can't connect to database)!" << std::endl;
return;
}
// Register this as an application that expects 3.x behavior,
// you must register something if you use AllocHandle
retCode = SQLSetEnvAttr(hEnv_, SQL_ATTR_ODBC_VERSION, (SQLPOINTER)SQL_OV_ODBC3, 0);
if( retCode != SQL_SUCCESS && retCode != SQL_SUCCESS_WITH_INFO )
{
HandleDiagnosticRecord(hEnv_, SQL_HANDLE_ENV, retCode);
Disconnect();
return;
}
// Allocate a connection
retCode = SQLAllocHandle(SQL_HANDLE_DBC, hEnv_, &hDbc_);
if( retCode != SQL_SUCCESS && retCode != SQL_SUCCESS_WITH_INFO )
{
HandleDiagnosticRecord(hEnv_, SQL_HANDLE_ENV, retCode);
Disconnect();
return;
}
// Connect to the driver. Use the connection string if supplied
// on the input, otherwise let the driver manager prompt for input.
{
boost::recursive_mutex::scoped_lock lk(driverConnect);
retCode = SQLDriverConnectW(hDbc_, hWnd, (SQLWCHAR *)ConectionString, SQL_NTS, NULL, 0, NULL, SQL_DRIVER_NOPROMPT);
}
if( retCode != SQL_SUCCESS && retCode != SQL_SUCCESS_WITH_INFO ){
HandleDiagnosticRecord(hDbc_, SQL_HANDLE_DBC, retCode);
Disconnect();
return;
}
connected_ = true;
VLOG(1) << "DEBUG: ODBC: Connected to db!" << std::endl;
}
void CDatabase::Disconnect()
{
connected_ = false;
VLOG(1) << "DEBUG: ODBC: Disconnected from db!" << std::endl;
try{
for( auto & thisBinding : bindings_ )
{
//????? ?????? ????????? ?????? ??? wszBuffer, ????? ???????? ??????? free
if( thisBinding.wszBuffer != NULL )
{
//VLOG(1) << "ODBC: free bind!" << std::endl;
free(thisBinding.wszBuffer);
thisBinding.wszBuffer = NULL;
}
}
bindings_.clear();
}catch(exception& e)
{
LOG(WARNING) << "ERROR: " << e.what();
}
if( hStmt_ != NULL )
{
SQLFreeHandle(SQL_HANDLE_STMT, hStmt_);
}
if( hDbc_ != NULL )
{
SQLDisconnect(hDbc_);
SQLFreeHandle(SQL_HANDLE_DBC, hDbc_);
}
if( hEnv_ != NULL )
{
SQLFreeHandle(SQL_HANDLE_ENV, hEnv_);
}
}
void CDatabase::getAnswer(SQLSMALLINT& cCols)
{
try{
RETCODE retCode = SQL_SUCCESS;
bool result;
// allocate memory for each column
result = allocateBindings(cCols);
if( !result )
{
return;
}
// get the titles
/*result = getTitles();
if( !result )
{
return;
}*/
// Fetch and display the data
bool fNoData = false;
do
{
// Fetch a row
retCode = SQLFetch(hStmt_);
if( retCode == SQL_ERROR || retCode == SQL_INVALID_HANDLE )
{
HandleDiagnosticRecord(hStmt_, SQL_HANDLE_STMT, retCode);
Disconnect();
fNoData = true;
} else if( retCode == SQL_NO_DATA_FOUND )
{
fNoData = true;
} else
{
// Display the data. Ignore truncations
for(auto & thisBinding: bindings_ )
{
if( thisBinding.indPtr != SQL_NULL_DATA )
{
answer_ += wstring(thisBinding.wszBuffer) + delim_;
} else
{
answer_ += wstring(L"<NULL>" + delim_);
}
}
answer_.push_back(L'\n');
// Cleaning up wszBuffer before next Fetch
for( auto & thisBinding : bindings_ )
{
ZeroMemory(thisBinding.wszBuffer, thisBinding.cDisplaySize);
}
}
} while( !fNoData );
/*for( auto & thisBinding : bindings_ )
{
if( thisBinding.wszBuffer )
{
free(thisBinding.wszBuffer);
}
}
bindings_.clear();*/
} catch( exception& e )
{
LOG(WARNING) << "ERROR: " << e.what();
}
}
/************************************************************************
/* allocateBindings: Get column information and allocate bindings_
/* for each column.
/*
/* Parameters:
/* cCols Number of columns in the result set
/************************************************************************/
bool CDatabase::allocateBindings(SQLSMALLINT & cCols)
{
try {
Binding ThisBinding;
SQLLEN cchDisplay, ssType;
//SQLSMALLINT cchColumnNameLength; // column name length in cheracters
RETCODE retCode;
for( SQLSMALLINT iCol = 1; iCol <= cCols; iCol++ )
{
cchDisplay = 0;
// Figure out the display length of the column (we will
// bind to char since we are only displaying data, in general
// you should bind to the appropriate C type if you are going
// to manipulate data since it is much faster...)
//
// PS: SQL_DESC_LENGTH - A numeric value that is either the maximum or actual character length of
// a character string or binary data type.It is the maximum character length for a
// fixed - length data type, or the actual character length for a variable - length
// data type.Its value always excludes the null - termination byte that ends the character string
// SQL_DESC_DISPLAY_SIZE - count of character to display. WAS IN SIMPLE FROM MICROSOFT
retCode = SQLColAttributeW(hStmt_, iCol, SQL_DESC_LENGTH /*SQL_DESC_DISPLAY_SIZE*/, NULL, 0, NULL, &cchDisplay);
if( retCode != SQL_SUCCESS )
{
HandleDiagnosticRecord(hStmt_, SQL_HANDLE_STMT, retCode);
if( retCode == SQL_ERROR || retCode == SQL_INVALID_HANDLE )
{
Disconnect();
return false;
}
}
// Figure out if this is a character or numeric column; this is
// used to determine if we want to display the data left- or right-
// aligned.
// SQL_DESC_CONCISE_TYPE maps to the 1.x SQL_COLUMN_TYPE.
// This is what you must use if you want to work
// against a 2.x driver.
retCode = SQLColAttributeW(hStmt_, iCol, SQL_DESC_CONCISE_TYPE, NULL, 0, NULL, &ssType);
if( retCode != SQL_SUCCESS )
{
HandleDiagnosticRecord(hStmt_, SQL_HANDLE_STMT, retCode);
if( retCode == SQL_ERROR || retCode == SQL_INVALID_HANDLE )
{
Disconnect();
return false;
}
}
ThisBinding.fChar = (ssType == SQL_WCHAR ||
ssType == SQL_WVARCHAR ||
ssType == SQL_WLONGVARCHAR);
// Arbitrary limit on display size
if( cchDisplay > MAX_WIDTH_OF_DATA_IN_COLOMN || cchDisplay < 0)
{
LOG_FIRST_N(WARNING, 1) << "Colomn '" << iCol <<"' needs: '" << cchDisplay << "' characters for longest value in it. This is unpredictable number. "
<< "The maximum number of characters was changed to: '" << MAX_WIDTH_OF_DATA_IN_COLOMN <<"'";
cchDisplay = MAX_WIDTH_OF_DATA_IN_COLOMN;
}
// Allocate a buffer big enough to hold the text representation
// of the data. Add one character for the null terminator
ThisBinding.wszBuffer = (WCHAR *)malloc((cchDisplay + 1) * sizeof(WCHAR));
if( !(ThisBinding.wszBuffer) )
{
LOG(WARNING) << "Out of memory!" << std::endl;
Disconnect();
return false;
}
ZeroMemory(ThisBinding.wszBuffer, cchDisplay + 1);
// Map this buffer to the driver's buffer. At Fetch time,
// the driver will fill in this data. Note that the size is
// count of bytes (for Unicode). All ODBC functions that take
// SQLPOINTER use count of bytes; all functions that take only
// strings use count of characters.
retCode = SQLBindCol(hStmt_, iCol, SQL_C_WCHAR, (SQLPOINTER)ThisBinding.wszBuffer, (SQLINTEGER)(cchDisplay + 1) * sizeof(WCHAR), &ThisBinding.indPtr);
if( retCode != SQL_SUCCESS )
{
HandleDiagnosticRecord(hStmt_, SQL_HANDLE_STMT, retCode);
if( retCode == SQL_ERROR || retCode == SQL_INVALID_HANDLE )
{
Disconnect();
return false;
}
}
// Now set the display size that we will use to display
// the data. Figure out the length of the column name
/*retCode = SQLColAttributeW(hStmt_, iCol, SQL_DESC_NAME, NULL, 0, &cchColumnNameLength, NULL);
if( retCode != SQL_SUCCESS )
{
HandleDiagnosticRecord(hStmt_, SQL_HANDLE_STMT, retCode);
if( retCode == SQL_ERROR || retCode == SQL_INVALID_HANDLE )
{
Disconnect();
return false;
}
}
ThisBinding.cDisplaySize = max((SQLSMALLINT)cchDisplay, cchColumnNameLength);
*/
ThisBinding.cDisplaySize = cchDisplay + 1;
//if( ThisBinding.cDisplaySize < NULL_SIZE )
// ThisBinding.cDisplaySize = NULL_SIZE;
bindings_.push_back(ThisBinding);
}
return true;
} catch( exception& e )
{
LOG(WARNING) << "ERROR: " << e.what();
return false;
}
}
bool CDatabase::getTitles()
{
RETCODE retCode;
WCHAR wszTitle[MAX_WIDTH_OF_DATA_IN_COLOMN];
SQLSMALLINT iCol = 1;
try {
for( auto& thisBinding : bindings_ )
{
ZeroMemory(wszTitle, MAX_WIDTH_OF_DATA_IN_COLOMN);
retCode = SQLColAttributeW(hStmt_, iCol++, SQL_DESC_NAME, wszTitle, sizeof(wszTitle) /*Note count of bytes!*/, NULL, NULL);
if( retCode != SQL_SUCCESS )
{
HandleDiagnosticRecord(hStmt_, SQL_HANDLE_STMT, retCode);
if( retCode == SQL_ERROR || retCode == SQL_INVALID_HANDLE )
{
Disconnect();
return false;
}
}
size_t len = 0;
for(auto& ch: wszTitle )
{
if( ch == 0 )
{
break;
}
len++;
}
answer_ += wstring(wszTitle, len) + delim_;
}
answer_ += wstring(L"\n\n");
return true;
} catch( exception& e )
{
LOG(WARNING) << "ERROR: " << e.what();
return false;
}
}
void CDatabase::HandleDiagnosticRecord(SQLHANDLE hHandle, SQLSMALLINT hType, RETCODE RetCode)
{
SQLSMALLINT iRec = 0;
SQLINTEGER iError;
SQLCHAR wszMessage[1000];
SQLCHAR wszState[SQL_SQLSTATE_SIZE + 1];
if( RetCode == SQL_INVALID_HANDLE )
{
LOG(WARNING) << "ODBC: Invalid handle with type identifier: '" <<hType <<"' !";
return;
}
bool noNameError = true;
while( SQLGetDiagRecA(hType,
hHandle,
++iRec,
wszState,
&iError,
wszMessage,
(SQLSMALLINT)countof(wszMessage),
(SQLSMALLINT *)NULL) == SQL_SUCCESS )
{
// Hide data truncated..
if( strncmp((CHAR *)wszState, "01004", 5) )
{
LOG(WARNING) <<"ODBC: [" << wszState <<"] " << wszMessage << " (" << iError <<")" <<std::endl;
}
noNameError = false;
}
if( noNameError && (RetCode == SQL_ERROR) )
{
LOG(WARNING) << "ODBC: NoName error";
}
}
bool CDatabase::operator<<(const wstring & query)
{
try {
RETCODE retCode;
SQLSMALLINT sNumResults;
bool result = false;
// Execute the query
if( query.empty() || (! connected_) )
{
return result;
}
retCode = SQLAllocHandle(SQL_HANDLE_STMT, hDbc_, &hStmt_);
if( retCode != SQL_SUCCESS )
{
HandleDiagnosticRecord(hDbc_, SQL_HANDLE_DBC, retCode);
Disconnect();
return result;
}
//wcscpy_s(wszInput, L"SELECT Events.colEventTime, A_55.colAccountNumber, Events.colCardNumber, Events.colHolderID, colHolderType, Holder.colSurname, Holder.colName, Holder.colMiddlename, colStateCode, colComment, colDepartmentID, Holder.colDepartment, Holder.colTabNumber, G_55.colName AS colAccessGroupName, CP.colID AS colObjectID, CP.colType AS colObjectType, CP.colName AS colObjectName, Events.colDirection, ZoneStart.colName AS colStartAreaName, ZoneTarget.colName AS colTargetAreaName, colEventCode FROM [StopNet4].[dbo].[tblEvents_55] AS Events WITH (NOLOCK) LEFT JOIN [StopNet4].[dbo].fnGetControlPoints() AS CP ON Events.colControlPointID = CP.colID LEFT JOIN ( SELECT colID, colSurname, colName, colMiddlename, '' AS colStateCode, colDepartmentID, colDepartment, colTabNumber, colComment, '1' AS colHolderType FROM [StopNet4].[dbo].[vwEmployees] WITH (NOLOCK) UNION ALL SELECT colID, colSurname, colName, colMiddlename, '' AS colStateCode, NULL, '', '', colComment, '2' AS colHolderType FROM [StopNet4].[dbo].[vwVisitors] WITH (NOLOCK) UNION ALL SELECT colID, '', '', '', colStateCode, NULL, '', '', colComment, '3' AS colHolderType FROM [StopNet4].[dbo].[vwVehicles] WITH (NOLOCK) ) AS Holder ON Events.colHolderID = Holder.colID LEFT JOIN [StopNet4].[dbo].[vwAccounts_55] AS A_55 WITH (NOLOCK) ON Events.colAccountID = A_55.colAccountID LEFT JOIN ( SELECT colID, colName FROM [StopNet4].[dbo].[tblGroupRightsEmployees_55] WITH (NOLOCK) UNION ALL SELECT colID, colName FROM [StopNet4].[dbo].[tblGroupRightsVisitors_55] WITH (NOLOCK) UNION ALL SELECT colID, colName FROM [StopNet4].[dbo].[tblGroupRightsVehicleEmployees_55] WITH (NOLOCK) UNION ALL SELECT colID, colName FROM [StopNet4].[dbo].[tblGroupRightsVehicleVisitors_55] WITH (NOLOCK) ) AS G_55 ON [A_55].colAccessGroupRightsID = G_55.colID LEFT JOIN [StopNet4].[dbo].[tblZones_55] AS ZoneStart WITH (NOLOCK) ON Events.colStartZoneID = ZoneStart.colID LEFT JOIN [StopNet4].[dbo].[tblZones_55] AS ZoneTarget WITH (NOLOCK) ON Events.colTargetZoneID = ZoneTarget.colID ");
retCode = SQLExecDirectW(hStmt_, (SQLWCHAR *)query.c_str(), SQL_NTS);
switch( retCode )
{
case SQL_SUCCESS_WITH_INFO:
{
HandleDiagnosticRecord(hStmt_, SQL_HANDLE_STMT, retCode);
// fall through
}
case SQL_SUCCESS:
{
// If this is a row-returning query, display
// results
retCode = SQLNumResultCols(hStmt_, &sNumResults);
if( retCode != SQL_SUCCESS ){
HandleDiagnosticRecord(hStmt_, SQL_HANDLE_STMT, retCode);
if( retCode == SQL_ERROR || retCode == SQL_INVALID_HANDLE ){
Disconnect();
break; //We must free hStmt_ before return
}
}
if( sNumResults > 0 ){
//get answer on query from db
getAnswer(sNumResults);
} else
{
SQLLEN cRowCount;
retCode = SQLRowCount(hStmt_, &cRowCount);
if( retCode != SQL_SUCCESS ){
HandleDiagnosticRecord(hStmt_, SQL_HANDLE_STMT, retCode);
if( retCode == SQL_ERROR || retCode == SQL_INVALID_HANDLE ){
Disconnect();
break; //We must free hStmt_ before return
}
}
if( cRowCount >= 0 )
{
answer_ += wstring(cRowCount + cRowCount == 1 ? L"row" : L"rows");
}
}
result = true;
break;
}
case SQL_ERROR:
{
HandleDiagnosticRecord(hStmt_, SQL_HANDLE_STMT, retCode);
break;
}
default:
LOG(WARNING) << "ODBC: Unexpected return code: !" <<retCode << std::endl;
}
retCode = SQLFreeStmt(hStmt_, SQL_CLOSE);
if( retCode != SQL_SUCCESS ){
HandleDiagnosticRecord(hStmt_, SQL_HANDLE_STMT, retCode);
}
return result;
} catch( exception& e )
{
LOG(WARNING) << "ERROR: " << e.what();
return false;
}
}
CDatabase::~CDatabase()
{
Disconnect();
}
bool CDatabase::ConnectedOk() const
{
return connected_;
}
void CDatabase::operator>>(wstring & str) const
{
str = answer_;
}
}