-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRow.cpp
More file actions
246 lines (231 loc) · 5.7 KB
/
Row.cpp
File metadata and controls
246 lines (231 loc) · 5.7 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
// Copyright © 2023 CCP ehf.
#include "Row.h"
#include <msdadc.h>
#include "NSession.h"
void Row::DeleteRow( Row* r, RowDescriptor const& d )
{
r->~Row();
}
DelayedException* Row::NewRow( Row** res, SimplePoolAllocator& ba, DBLENGTH& recvLen, RowDescriptor const& d, ACCESSOR& a, int numCols, Store& store )
{
int len = offsetof( Row, mData ) + d.mTotalLen;
void* data = ba.malloc( len );
*res = 0;
if( !data )
return DelayedException::NoMem( "couldn't allocate %d bytes for Row", len );
Row* row = new( data ) Row();
DelayedException* exc = row->Init( recvLen, d, a, numCols, store);
if( exc )
{
row->~Row();
return exc;
}
*res = row;
return 0;
}
DelayedException* Row::Init( DBLENGTH& recvLen, RowDescriptor const& d, ACCESSOR& a, int nc, Store& store )
{
memset( &mData, 0, d.mTotalLen );
recvLen = 0;
for( int index = 0; index < nc; index++ )
{
const DBORDINAL ordinal = a.GetOrdinal_ByIndex( index );
CCP_ASSERT( ordinal > 0 );
const DBORDINAL i = ordinal - 1;
DBSTATUS status = a.GetStatus_ByIndex( index );
if( status == DBSTATUS_S_ISNULL )
{
SetBit( d, d.mSNull + i );
continue;
}
if( status != DBSTATUS_S_OK )
{
CString msg;
if( status == DBSTATUS_S_TRUNCATED )
{
msg.Format( "column %d truncated.", ordinal );
Warn( PYDBWARN( msg ) );
}
else
{
msg.Format( "Unexpected status of column %d: %d", ordinal, status );
PYDBERROR( msg );
}
}
DBTYPE dbtype = a.GetColumnType_ByIndex( index );
if( dbtype & ( DBTYPE_ARRAY | DBTYPE_VECTOR ) )
{
CString msg;
msg.Format( "Unexpected type of column %d: %d", ordinal, dbtype );
PYDBERROR( msg );
}
//store stats on received data
DBLENGTH dblen = a.GetLength_ByIndex( index );
if( dblen != ~0 )
recvLen += dblen;
bool byref = !!( dbtype & DBTYPE_BYREF );
switch( dbtype & 0xff )
{
case DBTYPE_I1:
case DBTYPE_UI1: {
__int8 b;
a.GetValue_ByIndex( index, &b );
SetData( d, b, i );
break;
}
case DBTYPE_I2:
case DBTYPE_UI2: {
__int16 b;
a.GetValue_ByIndex( index, &b );
SetData( d, b, i );
break;
}
case DBTYPE_UI4: {
unsigned long b;
a.GetValue_ByIndex( index, &b );
SetData( d, b, i );
break;
}
case DBTYPE_I4:
case DBTYPE_R4: {
__int32 b;
a.GetValue_ByIndex( index, &b );
SetData( d, b, i );
break;
}
case DBTYPE_I8:
case DBTYPE_UI8:
case DBTYPE_R8:
case DBTYPE_CY:
case DBTYPE_FILETIME: {
__int64 b;
a.GetValue_ByIndex( index, &b );
SetData( d, b, i );
break;
}
case DBTYPE_BOOL: {
VARIANT_BOOL b;
a.GetValue_ByIndex( index, &b );
bool bb = !!b;
SetData( d, bb, i );
break;
}
case DBTYPE_STR: {
char* s = byref ? *(char**)a.GetValue( i + 1 ) : (char*)a.GetValue( i + 1 );
StringStoreElement* elem;
DelayedException* e = store.stringStore.Insert( elem, s, strlen( s ));
if( e )
return e;
SetData( d, elem, i );
break;
}
case DBTYPE_WSTR:
case DBTYPE_BSTR: {
wchar_t* s = byref ? *(wchar_t**)a.GetValue( i + 1 ) : (wchar_t*)a.GetValue( i + 1 );
WStringStoreElement* elem;
DelayedException* e = store.wStringStore.Insert( elem, s, wcslen( s ));
if( e )
return e;
SetData( d, elem, i );
break;
}
case DBTYPE_BYTES: {
DBLENGTH len;
if( !a.GetLength( i + 1, &len ) )
continue;
char* src = (char*)a.GetValue( i + 1 );
if( byref && src )
src = *(char**)src;
if( !src )
len = 0;
ByteStoreElement* elem;
DelayedException* e = store.byteStore.Insert( elem, src, len);
if( e )
return e;
SetData( d, elem, i );
break;
}
case DBTYPE_DBTIMESTAMP: //sql "datetime", "smalldatetime", "datetime2"
case DBTYPE_DBDATE: //sql "date"
{
DBLENGTH len;
if( !a.GetLength( i + 1, &len ) )
continue;
HRESULT hr;
DBLENGTH dstlen;
DBSTATUS srcstatus = status;
DBSTATUS dststatus;
FILETIME ftime;
hr = d.mNSession->mConv->DataConvert(
dbtype & 0xff,
DBTYPE_FILETIME,
len,
&dstlen,
a.GetValue( i + 1 ),
&ftime,
sizeof( ftime ),
srcstatus,
&dststatus,
0,
0,
DBDATACONVERT_DEFAULT );
if( FAILED( hr ) )
{
CString msg;
msg.Format( "Data conversion failed on column %d(%s)", i, (const char*)CW2A( a.GetColumnName( i + 1 ) ) );
return DelayedException::New( hr, msg );
}
SetData( d, ftime, i );
break;
}
case DBTYPE_DBTIME2: {
//The time. Convert to FILETIME units (1e-7s)
const DBTIME2& t2 = *reinterpret_cast<const DBTIME2*>( a.GetValue( i + 1 ) );
unsigned __int64 time = 3600 * t2.hour + 60 * t2.minute + t2.second;
time *= 10000000;
time += t2.fraction / 100; // (ns to 1e-7s);
SetData( d, time, i );
break;
}
default: {
CString msg;
msg.Format( "Unexpected type of column %d(%s): %d", i, (const char*)CW2A( a.GetColumnName( i + 1 ) ), dbtype );
PYDBERROR( msg );
}
}
}
return 0;
}
PyObject* Row::ToPython( const RowDescriptor& rd, PyObject* pyrd, ToPythonCtxt& ctxt )
{
// Raise any warning (or exception if turns out to be one
if( mWarning.get() )
{
if( !mWarning->Raise() )
return 0;
}
BluePy data( PyCapsule_New( &mData, "DBRow", 0 ) );
if( !data )
return 0;
//storing the method name here saves us loads of time in large rowsets
static PyObject* method = 0;
if( !method )
{
method = PyUnicode_InternFromString( "DBRow" );
if( !method )
return 0;
}
//convert any pointers to StringStore elements to the proper Python pointers.
//this is a once only operation, and so the row cannot be converted to Python more than once.
for( int i = 0; i < rd.mNObjects; i++ )
{
StoreElementBase* ptr;
GetObject( rd, i, ptr );
if( ptr )
{
PyObject* pystr = ptr->GetPython();
SetObject( rd, i, pystr );
}
}
return PyObject_CallMethodObjArgs( ctxt.mBlue, method, pyrd, data, 0 );
}