-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathvLob.c
More file actions
349 lines (303 loc) · 11.3 KB
/
vLob.c
File metadata and controls
349 lines (303 loc) · 11.3 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
/******************************************************
file:
vLob.c
purpose:
python type define for DM LOB variables in dmPython.just be used to col description
interface:
{}
history:
Date Who RefDoc Memo
2015-6-9 shenning Created
*******************************************************/
#include "Buffer.h"
#include "Error.h"
#include "var_pub.h"
//-----------------------------------------------------------------------------
// Declaration of LOB variable functions.
//-----------------------------------------------------------------------------
static int vLob_Initialize(dm_LobVar*, dm_Cursor*);
static void vLob_Finalize(dm_LobVar*);
static int vLob_SetValue(dm_LobVar*, unsigned, PyObject*);
static PyObject *vLob_GetValue(dm_LobVar*, unsigned);
static int vLob_BindObjectValue(dm_LobVar*, unsigned, dhobj, udint4);
static int vLob_PreDefine(dm_LobVar*, dhdesc, sdint2);
//-----------------------------------------------------------------------------
// Python type for CLOB declarations
//-----------------------------------------------------------------------------
PyTypeObject g_CLobVarType = {
PyVarObject_HEAD_INIT(NULL, 0)
"dmPython.CLOB", // tp_name
sizeof(dm_LobVar), // tp_basicsize
0, // tp_itemsize
0, // tp_dealloc
0, // tp_print
0, // tp_getattr
0, // tp_setattr
0, // tp_compare
0, // tp_repr
0, // tp_as_number
0, // tp_as_sequence
0, // tp_as_mapping
0, // tp_hash
0, // tp_call
0, // tp_str
0, // tp_getattro
0, // tp_setattro
0, // tp_as_buffer
Py_TPFLAGS_DEFAULT, // tp_flags
0 // tp_doc
};
//-----------------------------------------------------------------------------
// Python type for BLOB declarations
//-----------------------------------------------------------------------------
PyTypeObject g_BLobVarType = {
PyVarObject_HEAD_INIT(NULL, 0)
"dmPython.BLOB", // tp_name
sizeof(dm_LobVar), // tp_basicsize
0, // tp_itemsize
0, // tp_dealloc
0, // tp_print
0, // tp_getattr
0, // tp_setattr
0, // tp_compare
0, // tp_repr
0, // tp_as_number
0, // tp_as_sequence
0, // tp_as_mapping
0, // tp_hash
0, // tp_call
0, // tp_str
0, // tp_getattro
0, // tp_setattro
0, // tp_as_buffer
Py_TPFLAGS_DEFAULT, // tp_flags
0 // tp_doc
};
//-----------------------------------------------------------------------------
// variable type declarations
//-----------------------------------------------------------------------------
dm_VarType vt_CLOB = {
(InitializeProc) vLob_Initialize,
(FinalizeProc) vLob_Finalize,
(PreDefineProc) vLob_PreDefine,
(PreFetchProc) NULL,
(IsNullProc) NULL,
(SetValueProc) vLob_SetValue,
(GetValueProc) vLob_GetValue,
(GetBufferSizeProc) NULL,
(BindObjectValueProc)vLob_BindObjectValue,
&g_CLobVarType, // Python type
DSQL_C_LOB_HANDLE, // cType
sizeof(dhloblctr), // element length
1, // is character data
0, // is variable length
0, // can be copied
0 // can be in array
};
dm_VarType vt_BLOB = {
(InitializeProc) vLob_Initialize,
(FinalizeProc) vLob_Finalize,
(PreDefineProc) vLob_PreDefine,
(PreFetchProc) NULL,
(IsNullProc) NULL,
(SetValueProc) vLob_SetValue,
(GetValueProc) vLob_GetValue,
(GetBufferSizeProc) NULL,
(BindObjectValueProc)vLob_BindObjectValue,
&g_BLobVarType, // Python type
DSQL_C_LOB_HANDLE, // cType
sizeof(dhloblctr), // element length
0, // is character data
0, // is variable length
0, // can be copied
0 // can be in array
};
//-----------------------------------------------------------------------------
// LobVar_Initialize()
// Initialize the variable.
//-----------------------------------------------------------------------------
static
int
vLob_Initialize(
dm_LobVar* var, // variable to initialize
dm_Cursor* cursor // cursor created by
)
{
udint4 i;
// initialize members
Py_INCREF(cursor->connection);
var->connection = cursor->connection;
var->exLobs = PyList_New(var->allocatedElements);
if (!var->exLobs)
return -1;
// initialize the LOB locators
for (i = 0; i < var->allocatedElements; i++)
{
var->data[i] = NULL;
}
return 0;
}
//-----------------------------------------------------------------------------
// LobVar_Finalize()
// Prepare for variable destruction.
//-----------------------------------------------------------------------------
static
void
vLob_Finalize(
dm_LobVar* var // variable to free
)
{
udint4 i;
dm_ExternalLobVar* exLob;
for (i = 0; i < var->allocatedElements; i++)
{
if (var->exLobs != NULL)
exLob = (dm_ExternalLobVar*)PyList_GET_ITEM(var->exLobs, i);
else
exLob = NULL;
/** 通过exLob赋值的LOB句柄不预释放 **/
if (var->data[i] != NULL && exLob == NULL && var->connection->hcon != NULL)
{
dpi_free_lob_locator(var->data[i]);
}
var->data[i] = NULL;
}
Py_CLEAR(var->exLobs);
Py_CLEAR(var->connection);
}
//-----------------------------------------------------------------------------
// vLob_PreDefine()
// Performs additional steps required for defining objects.
//-----------------------------------------------------------------------------
static int vLob_PreDefine(
dm_LobVar* var, // variable to set up
dhdesc hdesc_col,
sdint2 pos // position in define list,1-based
)
{
DPIRETURN rt;
udint4 i;
// initialize the LOB locators
for (i = 0; i < var->allocatedElements; i++)
{
rt = dpi_alloc_lob_locator2(var->connection->hcon, (dhloblctr*)&var->data[i]);
if (Environment_CheckForError(var->environment, var->connection->hcon, DSQL_HANDLE_LOB_LOCATOR, rt,
"vLob_PreDefine():dpi_alloc_lob_locator2") < 0)
{
return -1;
}
}
return 0;
}
//-----------------------------------------------------------------------------
// LobVar_Write()
// Write data to the LOB variable.
//-----------------------------------------------------------------------------
int
vLobVar_Write(
dm_LobVar* var, // variable to perform write against
unsigned position, // position to perform write against
PyObject* dataObj, // data object to write into LOB
udint4 start_pos, // offset into variable
udint4* amount // amount to write
)
{
dm_Buffer buffer;
DPIRETURN rt = DSQL_SUCCESS;
sdint2 ctype;
// verify the data type
if (dmBuffer_FromObject(&buffer, dataObj, var->environment->encoding) < 0)
return -1;
*amount = (udint4)buffer.size;
// nothing to do if no data to write
if (*amount == 0)
{
dmBuffer_Clear(&buffer);
return 0;
}
if (Py_TYPE(var) == &g_BLobVarType)
ctype = DSQL_C_BINARY;
else
ctype = DSQL_C_NCHAR;
Py_BEGIN_ALLOW_THREADS
rt = dpi_lob_write(var->data[position], start_pos, ctype, (dpointer)(char*)buffer.ptr, *amount, amount);
Py_END_ALLOW_THREADS
dmBuffer_Clear(&buffer);
if (Environment_CheckForError(var->environment, var->data[position], DSQL_HANDLE_LOB_LOCATOR, rt,
"vLobVar_Write():dpi_lob_write") < 0)
{
return -1;
}
return 0;
}
//-----------------------------------------------------------------------------
// vLob_SetValue()
// Set the value of the variable.
//-----------------------------------------------------------------------------
static
int
vLob_SetValue(
dm_LobVar* var, // variable to set value for
unsigned pos, // array position to set
PyObject* value // value to set
)
{
dm_ExternalLobVar* exlob;
if (!PyObject_IsInstance(value, (PyObject*) &g_exLobVarType))
{
PyErr_SetString(PyExc_TypeError, "expecting a Lob Object");
return -1;
}
exlob = (dm_ExternalLobVar*)value;
Py_XDECREF(PyList_GET_ITEM(var->exLobs, pos));
Py_INCREF(value);
PyList_SET_ITEM(var->exLobs, pos, value);
var->data[pos] = exlob->lobVar->data[exlob->pos];
var->indicator[pos] = sizeof(dhloblctr);
var->actualLength[pos] = sizeof(dhloblctr);
return 0;
}
//-----------------------------------------------------------------------------
// LobVar_GetValue()
// Returns the value stored at the given array position.
//-----------------------------------------------------------------------------
static
PyObject*
vLob_GetValue(
dm_LobVar* var, // variable to determine value for
unsigned pos // array position
)
{
dm_ExternalLobVar* lob_val;
PyObject* ret;
lob_val = ExternalLobVar_New(var, pos);
if (lob_val->lobVar->type == &vt_CLOB)
{
ret = exLobVar_Str(lob_val);
}
else
{
ret = exLobVar_Bytes(lob_val);
}
//Py_DECREF(var);
Py_DECREF(lob_val);
return ret;
}
static
int
vLob_BindObjectValue(
dm_LobVar* var,
unsigned pos,
dhobj hobj,
udint4 val_nth
)
{
DPIRETURN rt;
rt = dpi_set_obj_val(hobj, val_nth, var->type->cType, (dpointer)&var->data[pos], var->indicator[pos]);
if (Environment_CheckForError(var->environment, hobj, DSQL_HANDLE_OBJECT, rt,
"vCursor_BindObjectValue():dpi_set_obj_val") < 0)
{
return -1;
}
return 0;
}