-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSqlCommand.cpp
More file actions
747 lines (645 loc) · 22.5 KB
/
SqlCommand.cpp
File metadata and controls
747 lines (645 loc) · 22.5 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
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
// Copyright © 2014 CCP ehf.
#include "SqlCommand.h"
#include "PythonBuff.h"
#include "utils.h"
using Utilities::DbExc_RuntimeError;
#include "NSession.h"
//--------------------------------------------------------------------
// SQLCommand::Prepare: Make the command ready
//--------------------------------------------------------------------
bool SQLCommand::Prepare(size_t ¶mLen, CSession *session, PyObject *schema, PyObject* args)
{
AUTOTASKLETC("DB::SQLCommand::Prepare", mNSession->mTimerDetail >= 2);
HRESULT hr;
char* procname;
PyObject* parameters;
PyObject* procParamSchema;
SetBlobHandling(DBBLOBHANDLING_NOSTREAMS);
if (!PyArg_ParseTuple(args, "sO:Execute", &procname, ¶meters))
return false;
if (!PyList_Check(parameters) && !PyDict_Check(parameters)) {
PyErr_SetString(PyExc_TypeError, "Second argument must be a list or dictionary");
return false;
}
PyObject* procSchema = PyTuple_GET_ITEM(schema, 0);
procParamSchema = PyDict_GetItemString(procSchema, procname);
if (procParamSchema == NULL || strcmp(procname, "__schemas") == 0)
return PyErr_Format(DbExc_RuntimeError, "Stored procedure '%s' not found", procname), false;
CString csSQL;
if (!SelectProcedure(csSQL, procname, procParamSchema))
return false;
hr = Create(*session, csSQL, DBGUID_SQL);
if (FAILED(hr))
return Utilities::SetErr32(hr, "Couldn't create command for %s", procname), false;
//Bind parameters, using the custom bind thing. Note, this is like this in
//the atl samples.
void* pDummy;
hr = BindParameters( &(m_hParameterAccessor), m_spCommand, procParamSchema, &pDummy, true, true, parameters);
if (FAILED(hr))
return Utilities::SetErr32(hr, "Couldn't bind parameters for %s", csSQL), false;
// Now let's put in the parameters
if (!SetDefaultParams())
return false;
if (PyList_Check(parameters))
{
if (!SetParamsFromList(paramLen, parameters))
return false;
} else {
if (!SetParamsFromDict(paramLen, parameters))
return false;
}
return true;
}
bool SQLCommand::SelectProcedure(CString &csSQL, const char *procname, PyObject *procParamSchema)
{
// It's a stored proc call
Py_ssize_t numargs = PyList_GET_SIZE(procParamSchema);
if (numargs == 1) {
csSQL.Format("{ ? = CALL %s;1 }", procname);
} else {
CString csParams = "?";
for (Py_ssize_t i = 2; i<numargs; i++)
csParams += ",?";
csSQL.Format("{ ? = CALL %s;1(%s) }", procname, csParams);
}
return true;
}
//This dude returns parameter info from the already cached schema.
//Similar to the ICommandWithParameters::GetParameterInfo
//but without a server roundtrip.
HRESULT SQLCommand::GetParameterInfo (
DB_UPARAMS *pcParams,
DBPARAMINFO **prgParamInfo,
OLECHAR **ppNamesBuffer,
PyObject *procParamSchema)
{
// Create some binding sjite
Py_ssize_t size = PyList_GET_SIZE(procParamSchema);
DB_UPARAMS cols = Py_SAFE_DOWNCAST(size, Py_ssize_t, DB_UPARAMS);
DBPARAMINFO* pinfo = (DBPARAMINFO*)CoTaskMemAlloc(cols * sizeof(DBPARAMINFO));
if (!pinfo)
return E_OUTOFMEMORY;
size_t namesize = 0;
DB_UPARAMS i;
for (i = 0; i < cols; i++)
{
PyObject* colinfo = PyList_GET_ITEM(procParamSchema, i);
CMiniProcParamsInfo* info =
(CMiniProcParamsInfo*)PyCapsule_GetPointer(
PyList_GET_ITEM(colinfo,6),
"CMiniProcParamsInfo");
// Calculate total size of name buffer
namesize += strlen(info->m_szParameterName)+1;
// Set the parameter characteristic flags
switch(info->m_nType){
case DBPARAMTYPE_INPUT:
pinfo[i].dwFlags = DBPARAMFLAGS_ISINPUT; break;
case DBPARAMTYPE_INPUTOUTPUT:
pinfo[i].dwFlags = DBPARAMFLAGS_ISINPUT | DBPARAMFLAGS_ISOUTPUT; break;
case DBPARAMTYPE_OUTPUT:
case DBPARAMTYPE_RETURNVALUE:
pinfo[i].dwFlags = DBPARAMFLAGS_ISOUTPUT; break;
default:
CCP_ASSERT(0);
}
if (info->m_bIsNullable)
pinfo[i].dwFlags |= DBPARAMFLAGS_ISNULLABLE;
if (info->m_nMaxLength > 8192)
pinfo[i].dwFlags |= DBPARAMFLAGS_ISLONG;
// Set the ordinal, starting with 1
pinfo[i].iOrdinal = i+1;
//temporarily store the pointer here. translate and modify later
pinfo[i].pwszName = (LPOLESTR)info->m_szParameterName;
// There's no type info
pinfo[i].pTypeInfo = NULL;
pinfo[i].ulParamSize =
info->m_nMaxLength ? info->m_nMaxLength : Utilities::GetSizeofDBType(info->m_nDataType);
pinfo[i].wType = info->m_nDataType;
pinfo[i].bPrecision = (BYTE)info->m_nPrecision;
pinfo[i].bScale = (BYTE)info->m_nScale;
}
// Build the names
LPOLESTR pnames = (LPOLESTR)CoTaskMemAlloc(namesize * sizeof(wchar_t));
if (!pnames) {
CoTaskMemFree(pinfo);
return E_OUTOFMEMORY;
}
LPOLESTR ix = pnames;
for (i = 0; i < cols; i++)
{
const char *pName = (const char*)pinfo[i].pwszName;
CA2W pNameOLE(pName);
wcscpy_s(ix, namesize, pNameOLE);
pinfo[i].pwszName = ix;
size_t l = wcslen(ix)+1;
ix += l;
namesize -= l;
}
*pcParams = cols;
*prgParamInfo = pinfo;
*ppNamesBuffer = pnames;
return S_OK;
}
// This is cut and pasted from atldbcli.h, line 4974. Exactly the same as there, but
// uses the function above to get the paraminfo.
HRESULT SQLCommand::BindParameters(HACCESSOR* pHAccessor, ICommand* pCommand, PyObject *procParamScema,
void** ppParameterBuffer, bool fBindLength, bool fBindStatus, PyObject *params) throw()
{
// If we have already bound the parameters then just return
// the pointer to the parameter buffer
if (*pHAccessor != NULL)
{
*ppParameterBuffer = m_pParameterBuffer;
return S_OK;
}
CComPtr<IAccessor> spAccessor;
ATLASSERT(pCommand != NULL);
HRESULT hr = pCommand->QueryInterface(&spAccessor);
if (FAILED(hr))
return hr;
// This is our new hack. CCPmodification.
DB_UPARAMS ulParams = 0;
CComHeapPtr<DBPARAMINFO> spParamInfo;
LPOLESTR pNamesBuffer;
// Get Parameter Information
hr = GetParameterInfo(&ulParams, &spParamInfo, &pNamesBuffer, procParamScema);
if (FAILED(hr))
return hr;
//CCPMOdification ends here.
// Create the parameter information for binding
hr = AllocateParameterInfo(ulParams);
if (FAILED(hr))
{
CoTaskMemFree(pNamesBuffer);
return hr;
}
DBBYTEOFFSET nOffset = 0;
DBBYTEOFFSET nDataOffset = 0;
DBBYTEOFFSET nLengthOffset = 0;
DBBYTEOFFSET nStatusOffset = 0;
DBBINDING* pCurrent = m_pParameterEntry;
for (ULONG l=0; l<ulParams; l++)
{
m_pParameterEntry[l].eParamIO = 0;
if (spParamInfo[l].dwFlags & DBPARAMFLAGS_ISINPUT)
m_pParameterEntry[l].eParamIO |= DBPARAMIO_INPUT;
if (spParamInfo[l].dwFlags & DBPARAMFLAGS_ISOUTPUT)
m_pParameterEntry[l].eParamIO |= DBPARAMIO_OUTPUT;
//CCP modification: Handle long data in input
DBOBJECT* dbo = NULL;
if (spParamInfo[l].dwFlags & DBPARAMFLAGS_ISLONG) {
SSIZE_T size = GetStringSize(&spParamInfo[l], params);
if (size < 0) {
spParamInfo[l].ulParamSize = 0; //no data
} else if (size>=0 && (size_t)size <= m_nBlobSize) {
//we just pack it in there.
spParamInfo[l].ulParamSize = Py_SAFE_DOWNCAST(size, SSIZE_T, DBLENGTH);
} else {
//we must pass this large parameter as a blob (CCP addition, blobs weren't supported)
spParamInfo[l].wType = DBTYPE_IUNKNOWN;
spParamInfo[l].ulParamSize = sizeof(void*);
dbo = new DBOBJECT;
if (!dbo) {
if (l == 0)
//otherwise, the pointer has been placed in there correctly
CoTaskMemFree(pNamesBuffer);
return E_OUTOFMEMORY;
}
m_pParameterEntry[l].pObject = dbo;
dbo->dwFlags = STGM_READ;
dbo->iid = IID_ISequentialStream;
}
}
// if this is a BLOB, truncate column length to m_nBlobSize (like 8000 bytes)
if( spParamInfo[l].ulParamSize > m_nBlobSize )
spParamInfo[l].ulParamSize = m_nBlobSize;
// if this is a string, recalculate column size in bytes
DBLENGTH colLength = spParamInfo[l].ulParamSize;
if (spParamInfo[l].wType == DBTYPE_STR)
colLength += 1;
if (spParamInfo[l].wType == DBTYPE_WSTR)
colLength = colLength*2 + 2;
// Calculate the column data offset
nDataOffset = AlignAndIncrementOffset( nOffset, colLength, GetAlignment( spParamInfo[l].wType ) );
if( fBindLength )
{
// Calculate the column length offset
nLengthOffset = AlignAndIncrementOffset( nOffset, sizeof(DBLENGTH), __alignof(DBLENGTH) );
}
if( fBindStatus )
{
// Calculate the column status offset
nStatusOffset = AlignAndIncrementOffset( nOffset, sizeof(DBSTATUS), __alignof(DBSTATUS) );
}
//CCP modification too
CDynamicParameterAccessor::Bind(pCurrent, spParamInfo[l].iOrdinal, spParamInfo[l].wType,
colLength, spParamInfo[l].bPrecision, spParamInfo[l].bScale,
m_pParameterEntry[l].eParamIO, nDataOffset, nLengthOffset, nStatusOffset,
dbo);
pCurrent++;
m_ppParamName[l] = pNamesBuffer;
if (pNamesBuffer && *pNamesBuffer)
{
// Search for the NULL termination character
while (*pNamesBuffer++)
;
}
}
// Allocate memory for the new buffer
m_pParameterBuffer = NULL;
ATLTRY(m_pParameterBuffer = new BYTE[nOffset]);
if (m_pParameterBuffer == NULL)
{
// Note that pNamesBuffer will be freed in the destructor
// by freeing *m_ppParamName
return E_OUTOFMEMORY;
}
*ppParameterBuffer = m_pParameterBuffer;
m_nParameterBufferSize = nOffset;
m_nParams = ulParams;
hr = BindEntries(m_pParameterEntry, ulParams, pHAccessor, nOffset, spAccessor);
return hr;
}
//This function gets the size of a parameter blob
SSIZE_T SQLCommand::GetStringSize(DBPARAMINFO *info, PyObject *params)
{
BluePy object;
if (!params)
return -1;
if (PyList_Check(params))
object = BluePy(PyList_GetItem(params, (info->iOrdinal-2)), true); //weird, ordinals start at 2
else
object = BluePy(PyDict_GetItemString(params, CW2A(info->pwszName)), true);
if (!object) {
PyErr_Clear();
return -1;
}
if( PyUnicode_Check( object.o ) )
return PyUnicode_GET_LENGTH( object.o ); //return number of wide chars
if( object.o->ob_type->tp_as_buffer )
{
if( !PyObject_CheckBuffer( object.o ) )
{
PyErr_SetString( PyExc_TypeError, "Object must have a buffer interface." );
return -1;
}
Py_buffer view;
if( PyObject_GetBuffer( object.o, &view, 0 ) != 0 )
{
PyErr_SetString( PyExc_TypeError, "Object must have a buffer interface." );
return -1;
}
if( !PyBuffer_IsContiguous( &view, 'A' ) )
{
PyBuffer_Release( &view );
PyErr_SetString( PyExc_TypeError, "Object buffer must be contiguous." );
return -1;
}
SSIZE_T bufferLength = view.len;
PyBuffer_Release( &view );
return bufferLength;
}
return -1;
}
// Set return value to OK
bool SQLCommand::SetDefaultParams()
{
if (!SetParamStatus(1, DBSTATUS_S_OK))
return PyErr_SetString(DbExc_RuntimeError, "Couldn't set param status for return column"), false;
// Set all other parameters to default
DB_UPARAMS n = GetParamCount();
for (DBORDINAL i = 1; i < n; i++)
if (!SetParamStatus(i+1, DBSTATUS_S_DEFAULT)) {
PyErr_Format(DbExc_RuntimeError, "Couldn't set param status for column %d:%s to DBSTATUS_S_DEFAULT",
i, (const char*)CW2A(GetParamName(i+1)));
return false;
}
return true;
}
bool SQLCommand::SetParamsFromList(size_t ¶mLen, PyObject *plist)
{
if (PyList_GET_SIZE(plist) > (Py_ssize_t)(GetParamCount()-1)) {
PyErr_Format(DbExc_RuntimeError,
"The stored procedure accepts at most %d argument(s), not %d",
GetParamCount()-1, PyList_GET_SIZE(plist));
return false;
}
paramLen = 0;
for (int i = 0; i < PyList_GET_SIZE(plist); i++)
{
size_t len;
if (!SetPyParam(len, i+2, PyList_GET_ITEM(plist, i)))
return false;
paramLen += len;
}
return true;
}
bool SQLCommand::SetParamsFromDict(size_t ¶mLen, PyObject *pdict)
{
// Key/value arguments
Py_ssize_t j = 0;
PyObject* key;
PyObject* value;
paramLen = 0;
while (PyDict_Next(pdict, &j, &key, &value))
{
BluePyStr pyname(BluePy(PyObject_Str(key)));
// Look for the field
DBORDINAL col;
if (!_GetParameterNo(CA2T(pyname.Str()), col))
{
return PyErr_Format( DbExc_RuntimeError,"%s has no argument '%s'.", "", pyname.Str()), false;
}
size_t len;
if (!SetPyParam(len, col+1, value))
return false;
paramLen += len;
}
return true;
}
//Set an integer parameter
template<class T>
bool SQLCommand::SetPyParamInt(size_t &len, DBORDINAL col, PyObject *value)
{
if( !PyLong_Check( value ) )
{
return PyErr_Format( DbExc_RuntimeError, "Argument %d(%s) must be a long", col - 1, (const char*)CW2A( GetParamName( col ) ) ), false;
}
long long l = PyLong_AsLongLong( value );
if (l == -1 && PyErr_Occurred())
return false;
T v = (T)l;
if (v != l) {
PyErr_Format(DbExc_RuntimeError, "Argument %d(%s) out of range:%lld", col, (const char*)CW2A(GetParamName(col)), l);
return false;
}
SetParam(col, &v);
len = sizeof(T);
return true;
}
bool SQLCommand::SetPyParam(size_t ¶mLen, DBORDINAL nparam, PyObject *value)
{
//first, if we have a pynone, set the paramstatus
if (value == Py_None) {
SetParamStatus(nparam, DBSTATUS_S_ISNULL);
paramLen = 0;
return true;
}
DBTYPE type;
GetParamType(nparam, &type);
switch (type) {
case DBTYPE_BOOL: {
int res = PyObject_IsTrue( value );
if(res == -1)
{
return PyErr_Format( DbExc_RuntimeError, "Argument %d(%s) must evaluate to a boolean", nparam - 1, (const char*)CW2A( GetParamName( nparam ) ) ), false;
}
VARIANT_BOOL p = res ? VARIANT_TRUE : VARIANT_FALSE;
SetParam(nparam, &p);
paramLen = 1;
return true; }
case DBTYPE_I1:
return SetPyParamInt<signed char>(paramLen, nparam, value);
case DBTYPE_UI1:
return SetPyParamInt<unsigned char>(paramLen, nparam, value);
case DBTYPE_I2:
return SetPyParamInt<signed short>(paramLen, nparam, value);
case DBTYPE_UI2:
return SetPyParamInt<unsigned short>(paramLen, nparam, value);
case DBTYPE_I4:
return SetPyParamInt<signed long>(paramLen, nparam, value);
case DBTYPE_UI4: {
//NOTE: This type is slightly broken. The value here should be an unsigned long
//Issue has always been present and is also reflected in Blue PyRowSet
if( !PyLong_Check( value ) )
{
return PyErr_Format( DbExc_RuntimeError, "Argument %d(%s) must be a long", nparam - 1, (const char*)CW2A( GetParamName( nparam ) ) ), false;
}
long l = PyLong_AsLong( value );
if( l == -1 && PyErr_Occurred() )
return PyErr_Format( DbExc_RuntimeError, "Failed to convert argument %d(%s) to long.", nparam - 1, (const char*)CW2A( GetParamName( nparam ) ) ), false;
SetParam( nparam, &l );
paramLen = sizeof( long );
return true;
}
case DBTYPE_I8:
case DBTYPE_FILETIME:{
if( !PyLong_Check( value ) )
{
return PyErr_Format( DbExc_RuntimeError, "Argument %d(%s) must be a long", nparam - 1, (const char*)CW2A( GetParamName( nparam ) ) ), false;
}
signed __int64 ll = PyLong_AsLongLong(value);
if( ll == -1 && PyErr_Occurred() )
return PyErr_Format( DbExc_RuntimeError, "Failed to convert argument %d(%s) to long long.", nparam - 1, (const char*)CW2A( GetParamName( nparam ) ) ), false;
SetParam(nparam, &ll);
paramLen = sizeof(ll);
return true;}
case DBTYPE_UI8: {
if( !PyLong_Check( value ) )
{
return PyErr_Format( DbExc_RuntimeError, "Argument %d(%s) must be a long", nparam - 1, (const char*)CW2A( GetParamName( nparam ) ) ), false;
}
unsigned __int64 ll = PyLong_AsUnsignedLongLong(value);
if (ll == (unsigned __int64)-1 && PyErr_Occurred())
return PyErr_Format( DbExc_RuntimeError, "Failed to convert argument %d(%s) to unsigned long long.", nparam - 1, (const char*)CW2A( GetParamName( nparam ) ) ), false;
SetParam(nparam, &ll);
paramLen = sizeof(ll);
return true;}
case DBTYPE_R4: {
if( !PyFloat_Check( value ) && !PyLong_Check( value ) )
{
return PyErr_Format( DbExc_RuntimeError, "Argument %d(%s) must be a number", nparam - 1, (const char*)CW2A( GetParamName( nparam ) ) ), false;
}
float rr = (float)PyFloat_AsDouble(value);
if (rr == -1.0f && PyErr_Occurred())
return false;
SetParam(nparam, &rr);
paramLen = sizeof(rr);
return true;}
case DBTYPE_R8: {
if( !PyFloat_Check( value ) && !PyLong_Check( value ))
{
return PyErr_Format( DbExc_RuntimeError, "Argument %d(%s) must be a number", nparam - 1, (const char*)CW2A( GetParamName( nparam ) ) ), false;
}
double rr = PyFloat_AsDouble(value);
if (rr == -1.0 && PyErr_Occurred())
return false;
SetParam(nparam, &rr);
paramLen = sizeof(rr);
return true;}
case DBTYPE_CY: {
if( !PyFloat_Check( value ) && !PyLong_Check( value ) )
{
return PyErr_Format( DbExc_RuntimeError, "Argument %d(%s) must be a number", nparam - 1, (const char*)CW2A( GetParamName( nparam ) ) ), false;
}
double dd = PyFloat_AsDouble(value);
if (dd == -1.0 && PyErr_Occurred())
return false;
__int64 rr = (__int64)(floor(dd * 100.0 + 0.5) * 100.0);
SetParam(nparam, &rr);
paramLen = sizeof(rr);
return true;}
case DBTYPE_STR: {
if( !PyUnicode_Check( value ) )
{
return PyErr_Format( DbExc_RuntimeError, "Argument %d(%s) must be an ASCII string", nparam - 1, (const char*)CW2A( GetParamName( nparam ) ) ), false;
}
//Convert using ASCII
PyObject* valueAsAscii = PyUnicode_AsASCIIString( value );
if( !valueAsAscii )
return PyErr_Format( DbExc_RuntimeError, "Argument %d(%s) must be an ASCII string", nparam - 1, (const char*)CW2A( GetParamName( nparam ) ) ), false;
const char* str = PyBytes_AsString( valueAsAscii );
if (!SetParamString(nparam, str)) {
Py_DecRef( valueAsAscii );
DBLENGTH max;
GetParamSize(nparam, &max);
return PyErr_Format(DbExc_RuntimeError, "Argument %d(%s) too long, can be at most %d chars", nparam-1, (const char*)CW2A(GetParamName(nparam)), max), false;
}
/* only the actual string data is sent, not the max column size. Verified using network packet sniffing */
paramLen = strlen(str); //this string is preallocated
Py_DecRef( valueAsAscii );
break;}
case DBTYPE_BSTR:
case DBTYPE_WSTR: {
if( !PyUnicode_Check( value ) )
{
return PyErr_Format( DbExc_RuntimeError, "Argument %d(%s) must be UnicodeType", nparam - 1, (const char*)CW2A( GetParamName( nparam ) ) ), false;
}
wchar_t* str = PyUnicode_AsWideCharString( value, nullptr );
if( !str )
{
return PyErr_Format( DbExc_RuntimeError, "Failed to get wchar from unicode argument %d(%s)", nparam - 1, (const char*)CW2A( GetParamName( nparam ) ) ), false;
}
if (!SetParamString(nparam, str)) {
DBLENGTH max;
GetParamSize(nparam, &max);
PyMem_Free( str );
return PyErr_Format( DbExc_RuntimeError, "Argument %d(%s) too long, can be at most %d chars", nparam - 1, (const char*)CW2A( GetParamName( nparam ) ), max / 2 ), false;
}
paramLen = wcslen(str) * sizeof(wchar_t);
PyMem_Free( str );
break;}
case DBTYPE_BYTES: {
if( !PyObject_CheckBuffer( value ) )
{
return PyErr_Format( DbExc_RuntimeError, "Argument %d(%s) must have buffer interface", nparam - 1, (const char*)CW2A( GetParamName( nparam ) ) ), false;
}
Py_buffer view;
if( PyObject_GetBuffer( value, &view, 0 ) != 0 )
{
return PyErr_Format( DbExc_RuntimeError, "Argument %d(%s) must have buffer interface", nparam - 1, (const char*)CW2A( GetParamName( nparam ) ) ), false;
}
if( !PyBuffer_IsContiguous( &view, 'A' ) )
{
PyBuffer_Release( &view );
return PyErr_Format( PyExc_TypeError, "Buffer argument %d(%s) must be contiguous", nparam - 1, (const char*)CW2A( GetParamName( nparam ) ) ), false;
}
DBLENGTH dblen;
GetParamSize( nparam, &dblen );
if( view.len > (Py_ssize_t)dblen )
{
PyBuffer_Release( &view );
return PyErr_Format( DbExc_RuntimeError, "Argument %d(%s) BLOB too large. is %d, can be at most %d", nparam - 1, (const char*)CW2A( GetParamName( nparam ) ), view.len, dblen ), false;
}
char* dest = (char*)GetParam( nparam );
memcpy( dest, view.buf, view.len );
SetParamLength( nparam, Py_SAFE_DOWNCAST( view.len, Py_ssize_t, DBLENGTH ) );
SetParamStatus( nparam, view.len ? DBSTATUS_S_OK : DBSTATUS_S_ISNULL );
paramLen = view.len;
PyBuffer_Release( &view );
break;
}
case DBTYPE_IUNKNOWN: {
PythonBuff *pbuff = new PythonBuff(value);
if (!pbuff)
return PyErr_NoMemory(), false;
if (pbuff->GetLength() == -1) {
delete pbuff;
return false;
}
if (!pbuff->Valid()) {
delete pbuff;
return PyErr_Format(DbExc_RuntimeError, "Argument %d(%s) must have buffer interface", nparam-1, (const char*)CW2A(GetParamName(nparam))), false;
}
IUnknown *iu = pbuff;
SetParam(nparam, &iu);
size_t bufflen = pbuff->GetLength();
SetParamLength(nparam, Py_SAFE_DOWNCAST(bufflen, size_t, DBLENGTH));
paramLen = bufflen;
break;}
case DBTYPE_DBTIMESTAMP:
case DBTYPE_DBDATE: {
if( !PyLong_Check( value ) )
{
return PyErr_Format( DbExc_RuntimeError, "Argument %d(%s) must be a long", nparam - 1, (const char*)CW2A( GetParamName( nparam ) ) ), false;
}
__int64 filetime = PyLong_AsLongLong(value);
if (filetime == -1 && PyErr_Occurred())
return false;
DBLENGTH dstlen;
HRESULT hr = mNSession->mConv->DataConvert(
DBTYPE_FILETIME, type,
sizeof(filetime), &dstlen,
&filetime, GetParam(nparam),
sizeof (DBTIMESTAMP),
DBSTATUS_S_OK, GetParamStatus(nparam),
0, 0,
DBDATACONVERT_DEFAULT);
if (FAILED(hr))
{
return Utilities::SetErr32( hr, "DataConvert failed" ), false;
}
paramLen = dstlen;
return true; }
case DBTYPE_DBTIME2: {
if( !PyLong_Check( value ) )
{
return PyErr_Format( DbExc_RuntimeError, "Argument %d(%s) must be a long", nparam - 1, (const char*)CW2A( GetParamName( nparam ) ) ), false;
}
__int64 filetime = PyLong_AsLongLong(value);
if (filetime == -1 && PyErr_Occurred())
return false;
DBTIME2 time;
time.fraction = (ULONG)(filetime % 10000000) * 100;
filetime /= 10000000;
time.second = (USHORT)(filetime % 60);
filetime /= 60;
time.minute = (USHORT)(filetime % 60);
filetime /= 60;
time.hour = (USHORT)(filetime % 24);
SetParam(nparam, &time);
paramLen = sizeof(time);
break; }
default:
return PyErr_Format(DbExc_RuntimeError, "Argument %d(%s) unexpected DB type: %d", nparam-1, (const char*)CW2A(GetParamName(nparam)), type), false;
}
return true;
}
//--------------------------------------------------------------------
// SetErr32
//--------------------------------------------------------------------
PyObject* SQLCommand::Raise(const char *msg, HRESULT hr, const CDBErrorInfo *errorInfo, ULONG nErrors )
{
BluePy none(Py_None, true);
//get column errors
BluePy paramErrors, columnErrors;
if (!Utilities::ParamErrorsToPy(hr, *this, ¶mErrors, &columnErrors))
return 0;
//Get Error Records
BluePy errorRecords(Utilities::ErrorRecToPy(errorInfo, nErrors));
if (!errorRecords)
return 0;
//Get the command
GUID sqlguid = DBGUID_SQL;
CComQIPtr<ICommandText> cmdtext(m_spCommand);
CComHeapPtr<OLECHAR> sql;
if (cmdtext)
cmdtext->GetCommandText(&sqlguid, &sql);
// Put everything together
BluePy errorArgs = BluePy(Py_BuildValue("isOuOO",
hr, msg, errorRecords,
sql?sql:(wchar_t*)L"", paramErrors, columnErrors));
if (!errorArgs)
return 0;
return PyErr_SetObject(Utilities::ErrorClass(hr), errorArgs), 0;
}