-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUtils.cpp
More file actions
555 lines (471 loc) · 14 KB
/
Utils.cpp
File metadata and controls
555 lines (471 loc) · 14 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
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
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
// Copyright © 2014 CCP ehf.
#include "StdAfx.h"
#include "Utils.h"
#include <IBlueOS.h>
#include <stdio.h>
#include <vector>
#include <string>
#include <atlstr.h>
PyObject* Utilities::DbExc_RuntimeError = NULL;
PyObject* Utilities::DbExc_WindowsError = NULL;
PyObject* Utilities::DbExc_SQLError = NULL;
PyObject* Utilities::DbExc_ConnectionError = NULL;
static CcpLogChannel_t s_chUtils = CCP_LOG_DEFINE_CHANNEL( "Utils" );
//--------------------------------------------------------------------
// InsertNewException - helper func
//--------------------------------------------------------------------
static PyObject* InsertNewException(const char* name, PyObject* parent, PyObject* dict)
{
PyObject* newexc = PyErr_NewException((char*)name, parent, NULL);
if (newexc == NULL)
{
newexc = parent;
Py_INCREF(newexc);
}
else
{
PyDict_SetItemString(dict, (char*)name, newexc);
}
return newexc;
}
//--------------------------------------------------------------------
// InitUtilities
//--------------------------------------------------------------------
bool Utilities::InitUtilities(PyObject* dict)
{
DbExc_RuntimeError = PyExc_RuntimeError;
Py_INCREF(DbExc_RuntimeError);
DbExc_WindowsError = PyExc_WindowsError;
Py_INCREF(DbExc_WindowsError);
// SQLError is declared in /lib/ccp_exceptions.py
DbExc_SQLError = PyDict_GetItemString(PyEval_GetBuiltins(), "SQLError");
if (DbExc_SQLError == NULL)
{
// dang it!
DbExc_SQLError = DbExc_RuntimeError;
CCP_LOGERR_CH( s_chUtils, "SQLError not found in Python builtins. Defaulting to RuntimeError");
}
Py_INCREF(DbExc_SQLError);
// ConnectionError is declared in /lib/ccp_exceptions.py
DbExc_ConnectionError = PyDict_GetItemString(PyEval_GetBuiltins(), "ConnectionError");
if (DbExc_ConnectionError == NULL)
{
// dang it!
DbExc_ConnectionError = DbExc_RuntimeError;
CCP_LOGERR_CH( s_chUtils, "ConnectionError not found in Python builtins. Defaulting to RuntimeError");
}
Py_INCREF(DbExc_ConnectionError);
return true;
}
//--------------------------------------------------------------------
// UninitUtilities
//--------------------------------------------------------------------
bool Utilities::UninitUtilities()
{
Py_XDECREF(DbExc_RuntimeError);
return true;
}
//--------------------------------------------------------------------
// DBTypeName
//--------------------------------------------------------------------
const char* Utilities::DBTypeName(DBTYPE type)
{
const char* name;
static char retval[200];
switch(type & 0xFF)
{
case DBTYPE_EMPTY: name = "DBTYPE_EMPTY"; break;
case DBTYPE_NULL: name = "DBTYPE_NULL"; break;
case DBTYPE_I2: name = "DBTYPE_I2"; break;
case DBTYPE_I4: name = "DBTYPE_I4"; break;
case DBTYPE_R4: name = "DBTYPE_R4"; break;
case DBTYPE_R8: name = "DBTYPE_R8"; break;
case DBTYPE_CY: name = "DBTYPE_CY"; break;
case DBTYPE_DATE: name = "DBTYPE_DATE"; break;
case DBTYPE_BSTR: name = "DBTYPE_BSTR"; break;
case DBTYPE_IDISPATCH: name = "DBTYPE_IDISPATCH"; break;
case DBTYPE_ERROR: name = "DBTYPE_ERROR"; break;
case DBTYPE_BOOL: name = "DBTYPE_BOOL"; break;
case DBTYPE_VARIANT: name = "DBTYPE_VARIANT"; break;
case DBTYPE_IUNKNOWN: name = "DBTYPE_IUNKNOWN"; break;
case DBTYPE_DECIMAL: name = "DBTYPE_DECIMAL: "; break;
case DBTYPE_UI1: name = "DBTYPE_UI1"; break;
case DBTYPE_I1: name = "DBTYPE_I1"; break;
case DBTYPE_UI2: name = "DBTYPE_UI2"; break;
case DBTYPE_UI4: name = "DBTYPE_UI4"; break;
case DBTYPE_I8: name = "DBTYPE_I8"; break;
case DBTYPE_UI8: name = "DBTYPE_UI8"; break;
case DBTYPE_GUID: name = "DBTYPE_GUID"; break;
case DBTYPE_BYTES: name = "DBTYPE_BYTES"; break;
case DBTYPE_STR: name = "DBTYPE_STR"; break;
case DBTYPE_WSTR: name = "DBTYPE_WSTR"; break;
case DBTYPE_NUMERIC: name = "DBTYPE_NUMERIC"; break;
case DBTYPE_UDT: name = "DBTYPE_UDT"; break;
case DBTYPE_DBDATE: name = "DBTYPE_DBDATE"; break;
case DBTYPE_DBTIME: name = "DBTYPE_DBTIME"; break;
case DBTYPE_DBTIMESTAMP: name = "DBTYPE_DBTIMESTAMP"; break;
case DBTYPE_HCHAPTER: name = "DBTYPE_HCHAPTER"; break;
case DBTYPE_FILETIME: name = "DBTYPE_FILETIME"; break;
case DBTYPE_PROPVARIANT: name = "DBTYPE_PROPVARIANT"; break;
case DBTYPE_VARNUMERIC: name = "DBTYPE_VARNUMERIC"; break;
case DBTYPE_DBTIME2: name = "DBTYPE_DBTIME2"; break;
default:
sprintf_s(retval, "Unexpected DB type %d", type);
return retval;
}
strcpy_s(retval, name);
if (type & DBTYPE_ARRAY)
strcat_s(retval, " | DBTYPE_ARRAY");
if (type & DBTYPE_BYREF)
strcat_s(retval, " | DBTYPE_BYREF");
if (type & DBTYPE_VECTOR)
strcat_s(retval, " | DBTYPE_VECTOR");
if (type & DBTYPE_RESERVED)
strcat_s(retval, " | DBTYPE_RESERVED");
return retval;
}
//--------------------------------------------------------------------
// GetSizeofDBType
//--------------------------------------------------------------------
size_t Utilities::GetSizeofDBType(DBTYPE bType)
{
if( bType & DBTYPE_BYREF )
return sizeof(void*);
if( bType & DBTYPE_ARRAY )
return sizeof(SAFEARRAY*);
if( bType & DBTYPE_VECTOR )
return sizeof(DBVECTOR);
switch( bType )
{
case DBTYPE_I2:
return sizeof(signed short);
break;
case DBTYPE_I4:
return sizeof(signed int);
break;
case DBTYPE_R4:
return sizeof(float);
break;
case DBTYPE_R8:
return sizeof(double);
break;
case DBTYPE_CY:
return sizeof(__int64);
break;
case DBTYPE_DATE:
return sizeof(DATE);
break;
case DBTYPE_BSTR:
return sizeof(BSTR*);
break;
case DBTYPE_IDISPATCH:
return sizeof(IDispatch*);
break;
case DBTYPE_ERROR:
return sizeof(SCODE);
break;
case DBTYPE_BOOL:
return sizeof(VARIANT_BOOL);
break;
case DBTYPE_VARIANT:
return sizeof(VARIANT);
break;
case DBTYPE_IUNKNOWN:
return sizeof(IUnknown*);
break;
case DBTYPE_DECIMAL:
return sizeof(DECIMAL);
break;
case DBTYPE_UI1:
return sizeof(unsigned char);
break;
case DBTYPE_I1:
return sizeof(signed char);
break;
case DBTYPE_UI2:
return sizeof(unsigned short);
break;
case DBTYPE_UI4:
return sizeof(unsigned int);
break;
case DBTYPE_I8:
return sizeof(signed __int64);
break;
case DBTYPE_UI8:
return sizeof(unsigned __int64);
break;
case DBTYPE_GUID:
return sizeof(GUID);
break;
case DBTYPE_BYTES:
return sizeof(BYTE);
break;
case DBTYPE_STR:
return sizeof(char);
break;
case DBTYPE_WSTR:
return sizeof(short);
break;
case DBTYPE_NUMERIC:
return sizeof(DB_NUMERIC);
break;
case DBTYPE_DBDATE:
return sizeof(DBDATE);
break;
case DBTYPE_DBTIME:
return sizeof(DBTIME);
break;
case DBTYPE_DBTIMESTAMP:
return sizeof(DBTIMESTAMP);
break;
default:
return sizeof(__int64);
}
}
//--------------------------------------------------------------------
// SetErr32
//--------------------------------------------------------------------
PyObject* Utilities::SetErr32(int err, const char* format, ...)
{
CComPtr<IErrorInfo> errinfo;
CComBSTR desc, src;
if (!err)
err = GetLastError();
//retrieve DB error
if (GetErrorInfo(0, &errinfo) == S_OK)
{
errinfo->GetDescription(&desc);
errinfo->GetSource(&src);
}
//compute given message
char *msg = 0;
if (format) {
va_list args;
va_start(args, format);
size_t len = _vscprintf(format, args)+1;
msg = new char[len];
if (msg)
vsprintf_s(msg, len, format, args);
va_end(args);
}
format = "Msg: %s Desc: %S, Source: %S";
size_t len2 = _scprintf( format, msg ? msg : "none", desc ? desc : CStringW( L"?" ), src ? src : CStringW( L"?" ) ) + 1;
char *msg2 = new char[len2];
if( msg2 )
sprintf_s( msg2, len2, format, msg ? msg : "none", desc ? desc : CStringW( L"?" ), src ? src : CStringW( L"?" ) );
PyErr_SetFromWindowsErrWithFilename(err,msg2?msg2:"none");
delete[] msg2;
delete [] msg;
return NULL;
}
//--------------------------------------------------------------------
// SetErrBlue
//--------------------------------------------------------------------
PyObject* Utilities::SetErrBlue(const char* format, ...)
{
char* err;
BeOS->FormatError(&err);
BeOS->SetError(BECLEAR);
if (!format)
format = "";
va_list args;
va_start(args, format);
size_t len = _vscprintf(format, args)+1;
if (err)
len += strlen(err)+1;
char *msg = new char[len];
if (msg) {
vsprintf_s(msg, len, format, args);
if (err) {
strcat_s(msg, len, " ");
strcat_s(msg, len, err);
}
}
CCP_FREE( err );
PyErr_SetString(PyExc_RuntimeError, msg?msg:"?"); //we don't have PyExc_BlueError here.
delete[] msg;
return 0;
}
void Utilities::FormatErrorRec(std::vector<std::wstring> &result, ULONG ne, const CDBErrorInfo &ei)
{
for(ULONG i=0; i<ne; ++i) {
//Get source and description
CComBSTR descr, src;
HRESULT hr = ei.GetAllErrorInfo(i, LOCALE_SYSTEM_DEFAULT, &descr, &src);
if (!SUCCEEDED(hr)) {
result.push_back(L"No error info");
continue; //no use continuin.
}
// Get specific MS SQL Server error info
CComPtr<ISQLServerErrorInfo> sqlservererr;
hr = ei.GetCustomErrorObject(i, __uuidof(sqlservererr), (IUnknown**)&sqlservererr);
if (SUCCEEDED(hr) && sqlservererr != NULL) {
CComHeapPtr<SSERRORINFO> ssinfo;
CComHeapPtr<OLECHAR> sserrstr;
hr = sqlservererr->GetErrorInfo(&ssinfo, &sserrstr);
if (SUCCEEDED(hr)) {
CStringW msg;
msg.Format(L"Source: %s, SQLServerErrorInfo: \"%s\" on %s, in %s:%d. Native:%d, state:%d, class:%d",
src.m_str,
ssinfo->pwszMessage,
ssinfo->pwszServer,
ssinfo->pwszProcedure,
ssinfo->wLineNumber, ssinfo->lNative, ssinfo->bState, ssinfo->bClass);
result.push_back(std::wstring(msg));
continue;
}
}
//ok, more generic then:
CComPtr<ISQLErrorInfo> sqlerr;
hr = ei.GetCustomErrorObject(i, __uuidof(sqlerr), (IUnknown**)&sqlerr);
if (SUCCEEDED(hr) && sqlerr != NULL) {
CComBSTR ansierr;
LONG nativeerr = 0;
hr = sqlerr->GetSQLInfo(&ansierr, &nativeerr);
if (SUCCEEDED(hr)) {
CStringW msg;
msg.Format( L"Source: %s, message: \"s\", sSQLErrorInfo: \"%s\" : %d",
src.m_str,
descr.m_str,
ansierr ? ansierr : CStringW( L"<none>" ),
nativeerr );
result.push_back(std::wstring(msg));
continue;
}
}
//the most generic:
CStringW msg;
msg.Format( L"ErrorInfo: \"%s\" from \"%s\"",
descr ? descr : CStringW( L"<none>" ),
src ? src : CStringW( L"<none>" ) );
result.push_back(std::wstring(msg));
}
}
bool Utilities::ParamErrorsToPy(HRESULT hr, ATL::CDynamicParameterAccessor &accessor, PyObject **paramErrors, PyObject **columnErrors)
{
//get column errors
*paramErrors = *columnErrors = 0;
BluePyList pe(0), ce(0);
if (!pe || !ce) return false;
if (hr == DB_E_ERRORSOCCURRED)
{
// Errors occurred while getting data for all columns or parameter
// Warning conditions did not occur for any columns or parameter
const char* DBSTATUSTEXT[] =
{
"DBSTATUS_S_OK",
"DBSTATUS_E_BADACCESSOR",
"DBSTATUS_E_CANTCONVERTVALUE",
"DBSTATUS_S_ISNULL",
"DBSTATUS_S_TRUNCATED",
"DBSTATUS_E_SIGNMISMATCH",
"DBSTATUS_E_DATAOVERFLOW",
"DBSTATUS_E_CANTCREATE",
"DBSTATUS_E_UNAVAILABLE",
"DBSTATUS_E_PERMISSIONDENIED",
"DBSTATUS_E_INTEGRITYVIOLATION",
"DBSTATUS_E_SCHEMAVIOLATION",
"DBSTATUS_E_BADSTATUS",
"DBSTATUS_S_DEFAULT",
};
ULONG i;
for (i = 1; i < accessor.GetParamCount(); i++)
{
DBSTATUS status;
if (accessor.GetParamStatus(i, &status) && status != DBSTATUS_S_OK && status != DBSTATUS_E_UNAVAILABLE)
{
DBTYPE type = 0;
accessor.GetParamType(i+1, &type); //weird how some functions have other param indices
BluePy v(Py_BuildValue(
"iuisis",
i, accessor.GetParamName(i+1), status, DBSTATUSTEXT[status], type, Utilities::DBTypeName(type)
));
if (!v || !pe.Append(v))
return false;
}
}
for (i = 0; i < accessor.GetColumnCount(); i++)
{
DBSTATUS status;
if (accessor.GetStatus(i, &status) && status != DBSTATUS_S_OK && status != DBSTATUS_E_UNAVAILABLE)
{
BluePy v(Py_BuildValue(
"iuis",
i, accessor.GetColumnName(i), status, DBSTATUSTEXT[status]
));
if (!v || !ce.Append(v))
return false;
}
}
}
*paramErrors = pe.Detach();
*columnErrors = ce.Detach();
return true;
}
PyObject *Utilities::ErrorRecToPy(const CDBErrorInfo *errorInfo, ULONG nErrors)
{
BluePyList errorRecords(0);
for (ULONG i = 0; errorInfo != 0 && i < nErrors; i++)
{
LCID lcid = GetSystemDefaultLCID();
CComBSTR desc;
CComBSTR source;
// Get error record
HRESULT hr = errorInfo->GetAllErrorInfo(i, lcid, &desc, &source);
if (FAILED(hr))
continue;
// Get specific MS SQL Server error info
CComPtr<ISQLServerErrorInfo> sqlservererr;
CComHeapPtr<SSERRORINFO> ssinfo;
CComHeapPtr<OLECHAR> sserrstr;
hr = errorInfo->GetCustomErrorObject(i, __uuidof(sqlservererr), (IUnknown**)&sqlservererr);
if (SUCCEEDED(hr) && sqlservererr != NULL)
hr = sqlservererr->GetErrorInfo(&ssinfo, &sserrstr);
BluePy rec;
if (ssinfo) {
rec = BluePy(Py_BuildValue("u ul u uk BB",
source.m_str,
ssinfo->pwszMessage, ssinfo->lNative,
ssinfo->pwszServer,
ssinfo->pwszProcedure, ssinfo->wLineNumber,
ssinfo->bState, ssinfo->bClass));
} else {
rec = BluePy(Py_BuildValue("uu",
source.m_str, desc.m_str));
}
errorRecords.Append(rec);
}
return errorRecords.Detach();
}
// The list of "soft" SQL errors
bool Utilities::IsSoftError(HRESULT hr)
{
assert(FAILED(hr));
// if this is an ITF error we know it's soft
HRESULT facility = HRESULT_FACILITY(hr);
if (facility == FACILITY_ITF) {
return true;
}
// Nonni 06-27-01: this should not be necessary since we've already checked for ITF
// but better safe than sorry. Refactor this out later after we've confirmed
// clean logs for a few months
const HRESULT ok[] = {
DB_E_ERRORSINCOMMAND, //primarily, this is user exceptions
DB_SEC_E_PERMISSIONDENIED,
DB_E_PARAMNOTOPTIONAL,
DB_E_CANCELED
};
for(size_t i=0; i<_countof(ok); ++i) {
if (hr == ok[i]) {
CCP_LOGWARN_CH( s_chUtils, "ITF check did not classify a soft error properly: %d", hr);
return true;
}
}
return false;
}
PyObject *Utilities::ErrorClass(HRESULT hr)
{
if (IsSoftError(hr))
return Utilities::DbExc_SQLError;
return Utilities::DbExc_ConnectionError;
}