-
Notifications
You must be signed in to change notification settings - Fork 1
/
Callback.c
303 lines (260 loc) · 10.2 KB
/
Callback.c
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
//-----------------------------------------------------------------------------
// Callback.c
// Definition of OCI callback functions.
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
// Callback_NewVariable()
// Return a new variable from a callback.
//-----------------------------------------------------------------------------
static udt_Variable *Callback_NewVariable(
udt_Connection *connection, // connection to use
ub2 oracleType, // Oracle type of data
ub4 bufferSize, // maximum length of elements
void *data, // data pointer
void *indicator, // indicator pointer
ub2 *returnCode, // return code pointer
ACTUAL_LENGTH_TYPE *actualLength) // actual length pointer
{
udt_VariableType *type;
udt_Variable *var;
// determine the type to use
type = Variable_TypeByOracleDataType(oracleType, SQLCS_IMPLICIT);
if (!type)
return NULL;
// attempt to allocate the object
var = (udt_Variable*) type->pythonType->tp_alloc(type->pythonType, 0);
if (!var)
return NULL;
// perform basic initialization
// note that the number of allocated elements is set arbitrarily high
// because the OCI doesn't give information about how many elements are
// actually allocated; that has to be implied by the number of rows
// passed to OCIStmtFetch and OCIStmtExecute
Py_INCREF(connection->environment);
var->environment = connection->environment;
var->boundCursorHandle = NULL;
var->bindHandle = NULL;
var->defineHandle = NULL;
var->boundName = NULL;
var->allocatedElements = 2147483647;
var->actualElements = 0;
var->isArray = 0;
var->isAllocatedInternally = 0;
var->type = type;
var->indicator = indicator;
var->data = data;
var->actualLength = actualLength;
var->returnCode = returnCode;
var->size = type->size;
var->bufferSize = type->size;
if (type->isVariableLength)
var->bufferSize = bufferSize;
return var;
}
//-----------------------------------------------------------------------------
// Callback_BindByNameArgs()
// Return the arguments to be passed when OCIBindByName is called.
//-----------------------------------------------------------------------------
static PyObject *Callback_BindByNameArgs(
udt_Connection *connection, // connection to use
va_list args) // arguments to OCI function
{
ub4 nameLength, allocatedElements, *actualElements;
ACTUAL_LENGTH_TYPE *actualLength;
ub2 dataType, *returnCode;
dvoid *indicator, *value;
udt_Variable *var;
PyObject *result;
sb4 valueLength;
OCIStmt *handle;
text *name;
handle = va_arg(args, OCIStmt*);
va_arg(args, OCIBind**);
va_arg(args, OCIError*);
name = va_arg(args, text*);
nameLength = va_arg(args, ub4);
value = va_arg(args, dvoid*);
valueLength = va_arg(args, sb4);
dataType = va_arg(args, int);
indicator = va_arg(args, dvoid*);
actualLength = va_arg(args, ACTUAL_LENGTH_TYPE*);
returnCode = va_arg(args, ub2*);
allocatedElements = va_arg(args, ub4);
actualElements = va_arg(args, ub4*);
var = Callback_NewVariable(connection, dataType, valueLength, value,
indicator, returnCode, actualLength);
if (!var)
return NULL;
if (allocatedElements > 0) {
var->isArray = 1;
var->actualElements = *actualElements;
}
result = Py_BuildValue("ls#O", handle, name, nameLength, var);
Py_DECREF(var);
return result;
}
//-----------------------------------------------------------------------------
// Callback_DefineByPosArgs()
// Return the arguments to be passed when OCIDefineByPos is called.
//-----------------------------------------------------------------------------
static PyObject *Callback_DefineByPosArgs(
udt_Connection *connection, // connection to use
va_list args) // arguments to OCI function
{
ACTUAL_LENGTH_TYPE *actualLength;
ub2 dataType, *returnCode;
dvoid *indicator, *value;
udt_Variable *var;
PyObject *result;
OCIStmt *handle;
sb4 valueLength;
ub4 position;
handle = va_arg(args, OCIStmt*);
va_arg(args, OCIDefine**);
va_arg(args, OCIError*);
position = va_arg(args, ub4);
value = va_arg(args, dvoid*);
valueLength = va_arg(args, sb4);
dataType = va_arg(args, int);
indicator = va_arg(args, dvoid*);
actualLength = va_arg(args, ACTUAL_LENGTH_TYPE*);
returnCode = va_arg(args, ub2*);
// create a variable
var = Callback_NewVariable(connection, dataType, valueLength, value,
indicator, returnCode, actualLength);
if (!var)
return NULL;
result = Py_BuildValue("liO", handle, position, var);
Py_DECREF(var);
return result;
}
//-----------------------------------------------------------------------------
// Callback_ExecuteArgs()
// Return the arguments to be passed when OCIStmtExecute is called.
//-----------------------------------------------------------------------------
static PyObject *Callback_ExecuteArgs(
va_list args) // arguments to OCI function
{
ub4 iters, rowoff;
OCIStmt *handle;
va_arg(args, OCISvcCtx*);
handle = va_arg(args, OCIStmt*);
va_arg(args, OCIError*);
iters = va_arg(args, ub4);
rowoff = va_arg(args, ub4);
return Py_BuildValue("lii", handle, iters, rowoff);
}
//-----------------------------------------------------------------------------
// Callback_FetchArgs()
// Return the arguments to be passed when OCIStmtFetch is called.
//-----------------------------------------------------------------------------
static PyObject *Callback_FetchArgs(
udt_Connection *connection, // connection to use
va_list args) // arguments to OCI function
{
ub4 numRows, rowCount;
OCIStmt *handle;
sword status;
handle = va_arg(args, OCIStmt*);
va_arg(args, OCIError*);
numRows = va_arg(args, ub4);
status = OCIAttrGet(handle, OCI_HTYPE_STMT, &rowCount, 0,
OCI_ATTR_ROW_COUNT, connection->environment->errorHandle);
if (Environment_CheckForError(connection->environment, status,
"Callback_FetchArgs()") < 0)
return NULL;
return Py_BuildValue("lii", handle, numRows, rowCount);
}
//-----------------------------------------------------------------------------
// Callback_PrepareArgs()
// Return the arguments to be passed when OCIStmtPrepare is called.
//-----------------------------------------------------------------------------
static PyObject *Callback_PrepareArgs(
va_list args) // arguments to OCI function
{
ub4 statementLength;
OCIStmt *handle;
text *statement;
handle = va_arg(args, OCIStmt*);
va_arg(args, OCIError*);
statement = va_arg(args, text *);
statementLength = va_arg(args, ub4);
return Py_BuildValue("ls#", handle, statement, statementLength);
}
//-----------------------------------------------------------------------------
// Callback_GetArgs()
// Return the arguments to be passed to the Python callback method.
//-----------------------------------------------------------------------------
static PyObject *Callback_GetArgs(
udt_Connection *connection, // connection to use
ub4 functionCode, // function code
va_list args) // OCI function arguments
{
switch (functionCode) {
case OCI_FNCODE_BINDBYNAME:
return Callback_BindByNameArgs(connection, args);
case OCI_FNCODE_DEFINEBYPOS:
return Callback_DefineByPosArgs(connection, args);
case OCI_FNCODE_STMTEXECUTE:
return Callback_ExecuteArgs(args);
case OCI_FNCODE_STMTFETCH:
return Callback_FetchArgs(connection, args);
case OCI_FNCODE_STMTPREPARE:
return Callback_PrepareArgs(args);
}
return PyTuple_New(0);
}
//-----------------------------------------------------------------------------
// Callback_Call()
// Actually make the call to the Python function.
//-----------------------------------------------------------------------------
static sword Callback_Call(
PyObject *tuple, // tuple containing connection/callback
ub4 functionCode, // function code
va_list args) // arguments
{
PyObject *callback, *callbackArgs, *result;
udt_Connection *connection;
// determine the connection and callback
connection = (udt_Connection*) PyTuple_GET_ITEM(tuple, 0);
callback = PyTuple_GET_ITEM(tuple, 1);
// determine the arguments to pass to the function
callbackArgs = Callback_GetArgs(connection, functionCode, args);
if (!callbackArgs)
return OCI_ERROR;
// actually make the call to the method
result = PyEval_CallObject(callback, callbackArgs);
Py_DECREF(callbackArgs);
if (!result)
return OCI_ERROR;
Py_DECREF(result);
return OCI_SUCCESS;
}
//-----------------------------------------------------------------------------
// Callback_Handler()
// Callback handler for calling Python code within an OCI callback.
//-----------------------------------------------------------------------------
static sword Callback_Handler(
PyObject *tuple, // tuple containing connection/callback
dvoid *handle, // pointer to handle
ub4 handleType, // handle type
ub4 functionCode, // function code
ub1 when, // when being called
sword returnCode, // return code
ub4 *errorCode, // error code (IN/OUT)
va_list args) // arguments
{
#ifdef WITH_THREAD
PyGILState_STATE gstate = PyGILState_Ensure();
#endif
sword result;
// perform the call
result = Callback_Call(tuple, functionCode, args);
if (result != OCI_CONTINUE)
PyErr_Print();
// restore thread state, if necessary
#ifdef WITH_THREAD
PyGILState_Release(gstate);
#endif
return result;
}