Callback.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. //-----------------------------------------------------------------------------
  2. // Callback.c
  3. // Definition of OCI callback functions.
  4. //-----------------------------------------------------------------------------
  5. //-----------------------------------------------------------------------------
  6. // Callback_NewVariable()
  7. // Return a new variable from a callback.
  8. //-----------------------------------------------------------------------------
  9. static udt_Variable *Callback_NewVariable(
  10. udt_Connection *connection, // connection to use
  11. ub2 oracleType, // Oracle type of data
  12. ub4 bufferSize, // maximum length of elements
  13. void *data, // data pointer
  14. void *indicator, // indicator pointer
  15. ub2 *returnCode, // return code pointer
  16. ub2 *actualLength) // actual length pointer
  17. {
  18. udt_VariableType *type;
  19. udt_Variable *var;
  20. // determine the type to use
  21. type = Variable_TypeByOracleDataType(oracleType, SQLCS_IMPLICIT);
  22. if (!type)
  23. return NULL;
  24. // attempt to allocate the object
  25. var = (udt_Variable*) type->pythonType->tp_alloc(type->pythonType, 0);
  26. if (!var)
  27. return NULL;
  28. // perform basic initialization
  29. // note that the number of allocated elements is set arbitrarily high
  30. // because the OCI doesn't give information about how many elements are
  31. // actually allocated; that has to be implied by the number of rows
  32. // passed to OCIStmtFetch and OCIStmtExecute
  33. Py_INCREF(connection->environment);
  34. var->environment = connection->environment;
  35. var->boundCursorHandle = NULL;
  36. var->bindHandle = NULL;
  37. var->defineHandle = NULL;
  38. var->boundName = NULL;
  39. var->allocatedElements = 2147483647;
  40. var->actualElements = 0;
  41. var->isArray = 0;
  42. var->isAllocatedInternally = 0;
  43. var->type = type;
  44. var->indicator = indicator;
  45. var->data = data;
  46. var->actualLength = actualLength;
  47. var->returnCode = returnCode;
  48. var->size = type->size;
  49. var->bufferSize = type->size;
  50. if (type->isVariableLength)
  51. var->bufferSize = bufferSize;
  52. return var;
  53. }
  54. //-----------------------------------------------------------------------------
  55. // Callback_BindByNameArgs()
  56. // Return the arguments to be passed when OCIBindByName is called.
  57. //-----------------------------------------------------------------------------
  58. static PyObject *Callback_BindByNameArgs(
  59. udt_Connection *connection, // connection to use
  60. va_list args) // arguments to OCI function
  61. {
  62. ub4 nameLength, allocatedElements, *actualElements;
  63. ub2 dataType, *actualLength, *returnCode;
  64. dvoid *indicator, *value;
  65. OCIBind **bindHandlePtr;
  66. OCIError *errorHandle;
  67. udt_Variable *var;
  68. PyObject *result;
  69. sb4 valueLength;
  70. OCIStmt *handle;
  71. text *name;
  72. handle = va_arg(args, OCIStmt*);
  73. bindHandlePtr = va_arg(args, OCIBind**);
  74. errorHandle = va_arg(args, OCIError*);
  75. name = va_arg(args, text*);
  76. nameLength = va_arg(args, ub4);
  77. value = va_arg(args, dvoid*);
  78. valueLength = va_arg(args, sb4);
  79. dataType = va_arg(args, int);
  80. indicator = va_arg(args, dvoid*);
  81. actualLength = va_arg(args, ub2*);
  82. returnCode = va_arg(args, ub2*);
  83. allocatedElements = va_arg(args, ub4);
  84. actualElements = va_arg(args, ub4*);
  85. var = Callback_NewVariable(connection, dataType, valueLength, value,
  86. indicator, returnCode, actualLength);
  87. if (!var)
  88. return NULL;
  89. if (allocatedElements > 0) {
  90. var->isArray = 1;
  91. var->actualElements = *actualElements;
  92. }
  93. result = Py_BuildValue("ls#O", handle, name, nameLength, var);
  94. Py_DECREF(var);
  95. return result;
  96. }
  97. //-----------------------------------------------------------------------------
  98. // Callback_DefineByPosArgs()
  99. // Return the arguments to be passed when OCIDefineByPos is called.
  100. //-----------------------------------------------------------------------------
  101. static PyObject *Callback_DefineByPosArgs(
  102. udt_Connection *connection, // connection to use
  103. va_list args) // arguments to OCI function
  104. {
  105. ub2 dataType, *actualLength, *returnCode;
  106. OCIDefine **defineHandle;
  107. dvoid *indicator, *value;
  108. OCIError *errorHandle;
  109. udt_Variable *var;
  110. PyObject *result;
  111. OCIStmt *handle;
  112. sb4 valueLength;
  113. ub4 position;
  114. handle = va_arg(args, OCIStmt*);
  115. defineHandle = va_arg(args, OCIDefine**);
  116. errorHandle = va_arg(args, OCIError*);
  117. position = va_arg(args, ub4);
  118. value = va_arg(args, dvoid*);
  119. valueLength = va_arg(args, sb4);
  120. dataType = va_arg(args, int);
  121. indicator = va_arg(args, dvoid*);
  122. actualLength = va_arg(args, ub2*);
  123. returnCode = va_arg(args, ub2*);
  124. // create a variable
  125. var = Callback_NewVariable(connection, dataType, valueLength, value,
  126. indicator, returnCode, actualLength);
  127. if (!var)
  128. return NULL;
  129. result = Py_BuildValue("liO", handle, position, var);
  130. Py_DECREF(var);
  131. return result;
  132. }
  133. //-----------------------------------------------------------------------------
  134. // Callback_ExecuteArgs()
  135. // Return the arguments to be passed when OCIStmtExecute is called.
  136. //-----------------------------------------------------------------------------
  137. static PyObject *Callback_ExecuteArgs(
  138. va_list args) // arguments to OCI function
  139. {
  140. OCISvcCtx* serviceContextHandle;
  141. OCIError *errorHandle;
  142. ub4 iters, rowoff;
  143. OCIStmt *handle;
  144. serviceContextHandle = va_arg(args, OCISvcCtx*);
  145. handle = va_arg(args, OCIStmt*);
  146. errorHandle = va_arg(args, OCIError*);
  147. iters = va_arg(args, ub4);
  148. rowoff = va_arg(args, ub4);
  149. return Py_BuildValue("lii", handle, iters, rowoff);
  150. }
  151. //-----------------------------------------------------------------------------
  152. // Callback_FetchArgs()
  153. // Return the arguments to be passed when OCIStmtFetch is called.
  154. //-----------------------------------------------------------------------------
  155. static PyObject *Callback_FetchArgs(
  156. udt_Connection *connection, // connection to use
  157. va_list args) // arguments to OCI function
  158. {
  159. ub4 numRows, rowCount;
  160. OCIError *errorHandle;
  161. OCIStmt *handle;
  162. sword status;
  163. handle = va_arg(args, OCIStmt*);
  164. errorHandle = va_arg(args, OCIError*);
  165. numRows = va_arg(args, ub4);
  166. status = OCIAttrGet(handle, OCI_HTYPE_STMT, &rowCount, 0,
  167. OCI_ATTR_ROW_COUNT, connection->environment->errorHandle);
  168. if (Environment_CheckForError(connection->environment, status,
  169. "Callback_FetchArgs()") < 0)
  170. return NULL;
  171. return Py_BuildValue("lii", handle, numRows, rowCount);
  172. }
  173. //-----------------------------------------------------------------------------
  174. // Callback_PrepareArgs()
  175. // Return the arguments to be passed when OCIStmtPrepare is called.
  176. //-----------------------------------------------------------------------------
  177. static PyObject *Callback_PrepareArgs(
  178. va_list args) // arguments to OCI function
  179. {
  180. OCIError *errorHandle;
  181. ub4 statementLength;
  182. OCIStmt *handle;
  183. text *statement;
  184. handle = va_arg(args, OCIStmt*);
  185. errorHandle = va_arg(args, OCIError*);
  186. statement = va_arg(args, text *);
  187. statementLength = va_arg(args, ub4);
  188. return Py_BuildValue("ls#", handle, statement, statementLength);
  189. }
  190. //-----------------------------------------------------------------------------
  191. // Callback_GetArgs()
  192. // Return the arguments to be passed to the Python callback method.
  193. //-----------------------------------------------------------------------------
  194. static PyObject *Callback_GetArgs(
  195. udt_Connection *connection, // connection to use
  196. ub4 functionCode, // function code
  197. va_list args) // OCI function arguments
  198. {
  199. switch (functionCode) {
  200. case OCI_FNCODE_BINDBYNAME:
  201. return Callback_BindByNameArgs(connection, args);
  202. case OCI_FNCODE_DEFINEBYPOS:
  203. return Callback_DefineByPosArgs(connection, args);
  204. case OCI_FNCODE_STMTEXECUTE:
  205. return Callback_ExecuteArgs(args);
  206. case OCI_FNCODE_STMTFETCH:
  207. return Callback_FetchArgs(connection, args);
  208. case OCI_FNCODE_STMTPREPARE:
  209. return Callback_PrepareArgs(args);
  210. }
  211. return PyTuple_New(0);
  212. }
  213. //-----------------------------------------------------------------------------
  214. // Callback_Call()
  215. // Actually make the call to the Python function.
  216. //-----------------------------------------------------------------------------
  217. static sword Callback_Call(
  218. PyObject *tuple, // tuple containing connection/callback
  219. ub4 functionCode, // function code
  220. va_list args) // arguments
  221. {
  222. PyObject *callback, *callbackArgs, *result;
  223. udt_Connection *connection;
  224. // determine the connection and callback
  225. connection = (udt_Connection*) PyTuple_GET_ITEM(tuple, 0);
  226. callback = PyTuple_GET_ITEM(tuple, 1);
  227. // determine the arguments to pass to the function
  228. callbackArgs = Callback_GetArgs(connection, functionCode, args);
  229. if (!callbackArgs)
  230. return OCI_ERROR;
  231. // actually make the call to the method
  232. result = PyEval_CallObject(callback, callbackArgs);
  233. Py_DECREF(callbackArgs);
  234. if (!result)
  235. return OCI_ERROR;
  236. Py_DECREF(result);
  237. return OCI_SUCCESS;
  238. }
  239. //-----------------------------------------------------------------------------
  240. // Callback_Handler()
  241. // Callback handler for calling Python code within an OCI callback.
  242. //-----------------------------------------------------------------------------
  243. static sword Callback_Handler(
  244. PyObject *tuple, // tuple containing connection/callback
  245. dvoid *handle, // pointer to handle
  246. ub4 handleType, // handle type
  247. ub4 functionCode, // function code
  248. ub1 when, // when being called
  249. sword returnCode, // return code
  250. ub4 *errorCode, // error code (IN/OUT)
  251. va_list args) // arguments
  252. {
  253. #ifdef WITH_THREAD
  254. PyGILState_STATE gstate = PyGILState_Ensure();
  255. #endif
  256. sword result;
  257. // perform the call
  258. result = Callback_Call(tuple, functionCode, args);
  259. if (result != OCI_CONTINUE)
  260. PyErr_Print();
  261. // restore thread state, if necessary
  262. #ifdef WITH_THREAD
  263. PyGILState_Release(gstate);
  264. #endif
  265. return result;
  266. }