Variable.c 54 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528
  1. //-----------------------------------------------------------------------------
  2. // Variable.c
  3. // Defines Python types for Oracle variables.
  4. //-----------------------------------------------------------------------------
  5. //-----------------------------------------------------------------------------
  6. // define structure common to all variables
  7. //-----------------------------------------------------------------------------
  8. #if ORACLE_VERSION_HEX >= ORACLE_VERSION(12,1)
  9. #define OCIBINDBYNAME OCIBindByName2
  10. #define OCIBINDBYPOS OCIBindByPos2
  11. #define OCIDEFINEBYPOS OCIDefineByPos2
  12. #define ACTUAL_LENGTH_TYPE ub4
  13. #else
  14. #define OCIBINDBYNAME OCIBindByName
  15. #define OCIBINDBYPOS OCIBindByPos
  16. #define OCIDEFINEBYPOS OCIDefineByPos
  17. #define ACTUAL_LENGTH_TYPE ub2
  18. #endif
  19. struct _udt_VariableType;
  20. #define Variable_HEAD \
  21. PyObject_HEAD \
  22. OCIBind *bindHandle; \
  23. OCIDefine *defineHandle; \
  24. OCIStmt *boundCursorHandle; \
  25. PyObject *boundName; \
  26. PyObject *inConverter; \
  27. PyObject *outConverter; \
  28. ub4 boundPos; \
  29. udt_Environment *environment; \
  30. ub4 allocatedElements; \
  31. ub4 actualElements; \
  32. unsigned internalFetchNum; \
  33. int isArray; \
  34. int isAllocatedInternally; \
  35. sb2 *indicator; \
  36. ub2 *returnCode; \
  37. ACTUAL_LENGTH_TYPE *actualLength; \
  38. ub4 size; \
  39. ub4 bufferSize; \
  40. struct _udt_VariableType *type;
  41. typedef struct {
  42. Variable_HEAD
  43. void *data;
  44. } udt_Variable;
  45. //-----------------------------------------------------------------------------
  46. // define function types for the common actions that take place on a variable
  47. //-----------------------------------------------------------------------------
  48. typedef int (*InitializeProc)(udt_Variable*, udt_Cursor*);
  49. typedef void (*FinalizeProc)(udt_Variable*);
  50. typedef int (*PreDefineProc)(udt_Variable*, OCIParam*);
  51. typedef int (*PostDefineProc)(udt_Variable*);
  52. typedef int (*PreFetchProc)(udt_Variable*);
  53. typedef int (*IsNullProc)(udt_Variable*, unsigned);
  54. typedef int (*SetValueProc)(udt_Variable*, unsigned, PyObject*);
  55. typedef PyObject * (*GetValueProc)(udt_Variable*, unsigned);
  56. typedef ub4 (*GetBufferSizeProc)(udt_Variable*);
  57. //-----------------------------------------------------------------------------
  58. // define structure for the common actions that take place on a variable
  59. //-----------------------------------------------------------------------------
  60. typedef struct _udt_VariableType {
  61. InitializeProc initializeProc;
  62. FinalizeProc finalizeProc;
  63. PreDefineProc preDefineProc;
  64. PostDefineProc postDefineProc;
  65. PreFetchProc preFetchProc;
  66. IsNullProc isNullProc;
  67. SetValueProc setValueProc;
  68. GetValueProc getValueProc;
  69. GetBufferSizeProc getBufferSizeProc;
  70. PyTypeObject *pythonType;
  71. ub2 oracleType;
  72. ub1 charsetForm;
  73. ub4 size;
  74. int isCharacterData;
  75. int isVariableLength;
  76. int canBeCopied;
  77. int canBeInArray;
  78. } udt_VariableType;
  79. //-----------------------------------------------------------------------------
  80. // Declaration of common variable functions.
  81. //-----------------------------------------------------------------------------
  82. static void Variable_Free(udt_Variable *);
  83. static PyObject *Variable_Repr(udt_Variable *);
  84. static PyObject *Variable_ExternalCopy(udt_Variable *, PyObject *);
  85. static PyObject *Variable_ExternalSetValue(udt_Variable *, PyObject *);
  86. static PyObject *Variable_ExternalGetValue(udt_Variable *, PyObject *,
  87. PyObject *);
  88. static int Variable_InternalBind(udt_Variable *);
  89. static int Variable_Resize(udt_Variable *, unsigned);
  90. //-----------------------------------------------------------------------------
  91. // declaration of members for variables
  92. //-----------------------------------------------------------------------------
  93. static PyMemberDef g_VariableMembers[] = {
  94. { "bufferSize", T_INT, offsetof(udt_Variable, bufferSize), READONLY },
  95. { "inconverter", T_OBJECT, offsetof(udt_Variable, inConverter), 0 },
  96. { "numElements", T_INT, offsetof(udt_Variable, allocatedElements),
  97. READONLY },
  98. { "outconverter", T_OBJECT, offsetof(udt_Variable, outConverter), 0 },
  99. { "size", T_INT, offsetof(udt_Variable, size), READONLY },
  100. { "maxlength", T_INT, offsetof(udt_Variable, bufferSize), READONLY },
  101. { "allocelems", T_INT, offsetof(udt_Variable, allocatedElements),
  102. READONLY },
  103. { NULL }
  104. };
  105. //-----------------------------------------------------------------------------
  106. // declaration of methods for variables
  107. //-----------------------------------------------------------------------------
  108. static PyMethodDef g_VariableMethods[] = {
  109. { "copy", (PyCFunction) Variable_ExternalCopy, METH_VARARGS },
  110. { "setvalue", (PyCFunction) Variable_ExternalSetValue, METH_VARARGS },
  111. { "getvalue", (PyCFunction) Variable_ExternalGetValue,
  112. METH_VARARGS | METH_KEYWORDS },
  113. { NULL }
  114. };
  115. //-----------------------------------------------------------------------------
  116. // The base variable type
  117. //-----------------------------------------------------------------------------
  118. static PyTypeObject g_BaseVarType = {
  119. PyVarObject_HEAD_INIT(NULL, 0)
  120. "cx_Oracle._BASEVARTYPE", // tp_name
  121. sizeof(udt_Variable), // tp_basicsize
  122. 0, // tp_itemsize
  123. (destructor) Variable_Free, // tp_dealloc
  124. 0, // tp_print
  125. 0, // tp_getattr
  126. 0, // tp_setattr
  127. 0, // tp_compare
  128. (reprfunc) Variable_Repr, // tp_repr
  129. 0, // tp_as_number
  130. 0, // tp_as_sequence
  131. 0, // tp_as_mapping
  132. 0, // tp_hash
  133. 0, // tp_call
  134. 0, // tp_str
  135. 0, // tp_getattro
  136. 0, // tp_setattro
  137. 0, // tp_as_buffer
  138. Py_TPFLAGS_DEFAULT, // tp_flags
  139. 0, // tp_doc
  140. 0, // tp_traverse
  141. 0, // tp_clear
  142. 0, // tp_richcompare
  143. 0, // tp_weaklistoffset
  144. 0, // tp_iter
  145. 0, // tp_iternext
  146. g_VariableMethods, // tp_methods
  147. g_VariableMembers // tp_members
  148. };
  149. #if ORACLE_VERSION_HEX >= ORACLE_VERSION(12, 1)
  150. #include "BooleanVar.c"
  151. #endif
  152. #include "Transforms.c"
  153. #include "StringVar.c"
  154. #include "LongVar.c"
  155. #include "NumberVar.c"
  156. #include "DateTimeVar.c"
  157. #include "TimestampVar.c"
  158. #include "LobVar.c"
  159. #include "CursorVar.c"
  160. #include "ObjectVar.c"
  161. #include "IntervalVar.c"
  162. //-----------------------------------------------------------------------------
  163. // Variable_AllocateData()
  164. // Allocate the data for the variable.
  165. //-----------------------------------------------------------------------------
  166. static int Variable_AllocateData(
  167. udt_Variable *self) // variable to allocate data for
  168. {
  169. unsigned PY_LONG_LONG dataLength;
  170. // set the buffer size for the variable
  171. if (self->type->getBufferSizeProc)
  172. self->bufferSize = (*self->type->getBufferSizeProc)(self);
  173. else self->bufferSize = self->size;
  174. // allocate the data as long as it is small enough
  175. dataLength = (unsigned PY_LONG_LONG) self->allocatedElements *
  176. (unsigned PY_LONG_LONG) self->bufferSize;
  177. if (dataLength > INT_MAX) {
  178. PyErr_SetString(PyExc_ValueError, "array size too large");
  179. return -1;
  180. }
  181. self->data = PyMem_Malloc((size_t) dataLength);
  182. if (!self->data) {
  183. PyErr_NoMemory();
  184. return -1;
  185. }
  186. return 0;
  187. }
  188. //-----------------------------------------------------------------------------
  189. // Variable_New()
  190. // Allocate a new variable.
  191. //-----------------------------------------------------------------------------
  192. static udt_Variable *Variable_New(
  193. udt_Cursor *cursor, // cursor to associate variable with
  194. unsigned numElements, // number of elements to allocate
  195. udt_VariableType *type, // variable type
  196. ub4 size) // used only for variable length types
  197. {
  198. udt_Variable *self;
  199. ub4 i;
  200. // attempt to allocate the object
  201. self = (udt_Variable*) type->pythonType->tp_alloc(type->pythonType, 0);
  202. if (!self)
  203. return NULL;
  204. // perform basic initialization
  205. Py_INCREF(cursor->connection->environment);
  206. self->environment = cursor->connection->environment;
  207. self->boundCursorHandle = NULL;
  208. self->bindHandle = NULL;
  209. self->defineHandle = NULL;
  210. self->boundName = NULL;
  211. self->inConverter = NULL;
  212. self->outConverter = NULL;
  213. self->boundPos = 0;
  214. if (numElements < 1)
  215. self->allocatedElements = 1;
  216. else self->allocatedElements = numElements;
  217. self->actualElements = 0;
  218. self->internalFetchNum = 0;
  219. self->isArray = 0;
  220. self->isAllocatedInternally = 1;
  221. self->type = type;
  222. self->indicator = NULL;
  223. self->data = NULL;
  224. self->actualLength = NULL;
  225. self->returnCode = NULL;
  226. // set the maximum length of the variable, ensure that a minimum of
  227. // 2 bytes is allocated to ensure that the array size check works
  228. self->size = type->size;
  229. if (type->isVariableLength) {
  230. if (size < sizeof(ub2))
  231. size = sizeof(ub2);
  232. self->size = size;
  233. }
  234. // allocate the data for the variable
  235. if (Variable_AllocateData(self) < 0) {
  236. Py_DECREF(self);
  237. return NULL;
  238. }
  239. // allocate the indicator for the variable
  240. self->indicator = PyMem_Malloc(self->allocatedElements * sizeof(sb2));
  241. if (!self->indicator) {
  242. PyErr_NoMemory();
  243. Py_DECREF(self);
  244. return NULL;
  245. }
  246. // ensure that all variable values start out NULL
  247. for (i = 0; i < self->allocatedElements; i++)
  248. self->indicator[i] = OCI_IND_NULL;
  249. // for variable length data, also allocate the return code
  250. if (type->isVariableLength) {
  251. self->returnCode = PyMem_Malloc(self->allocatedElements * sizeof(ub2));
  252. if (!self->returnCode) {
  253. PyErr_NoMemory();
  254. Py_DECREF(self);
  255. return NULL;
  256. }
  257. }
  258. // perform extended initialization
  259. if (self->type->initializeProc) {
  260. if ((*self->type->initializeProc)(self, cursor) < 0) {
  261. Py_DECREF(self);
  262. return NULL;
  263. }
  264. }
  265. return self;
  266. }
  267. //-----------------------------------------------------------------------------
  268. // Variable_Free()
  269. // Free an existing variable.
  270. //-----------------------------------------------------------------------------
  271. static void Variable_Free(
  272. udt_Variable *self) // variable to free
  273. {
  274. if (self->isAllocatedInternally) {
  275. if (self->type->finalizeProc)
  276. (*self->type->finalizeProc)(self);
  277. if (self->indicator)
  278. PyMem_Free(self->indicator);
  279. if (self->data)
  280. PyMem_Free(self->data);
  281. if (self->actualLength)
  282. PyMem_Free(self->actualLength);
  283. if (self->returnCode)
  284. PyMem_Free(self->returnCode);
  285. }
  286. Py_CLEAR(self->environment);
  287. Py_CLEAR(self->boundName);
  288. Py_CLEAR(self->inConverter);
  289. Py_CLEAR(self->outConverter);
  290. Py_TYPE(self)->tp_free((PyObject*) self);
  291. }
  292. //-----------------------------------------------------------------------------
  293. // Variable_Resize()
  294. // Resize the variable.
  295. //-----------------------------------------------------------------------------
  296. static int Variable_Resize(
  297. udt_Variable *self, // variable to resize
  298. unsigned size) // new size to use
  299. {
  300. ub4 origBufferSize, i;
  301. char *origData;
  302. // allocate the data for the new array
  303. origData = self->data;
  304. origBufferSize = self->bufferSize;
  305. self->size = size;
  306. if (Variable_AllocateData(self) < 0)
  307. return -1;
  308. // copy the data from the original array to the new array
  309. for (i = 0; i < self->allocatedElements; i++)
  310. memcpy( (char*) self->data + self->bufferSize * i,
  311. (void*) ( (char*) origData + origBufferSize * i ),
  312. origBufferSize);
  313. PyMem_Free(origData);
  314. // force rebinding
  315. if (self->boundName || self->boundPos > 0) {
  316. if (Variable_InternalBind(self) < 0)
  317. return -1;
  318. }
  319. return 0;
  320. }
  321. //-----------------------------------------------------------------------------
  322. // Variable_Check()
  323. // Returns a boolean indicating if the object is a variable.
  324. //-----------------------------------------------------------------------------
  325. static int Variable_Check(
  326. PyObject *object) // Python object to check
  327. {
  328. return (Py_TYPE(object) == &g_CursorVarType ||
  329. Py_TYPE(object) == &g_DateTimeVarType ||
  330. Py_TYPE(object) == &g_BFILEVarType ||
  331. Py_TYPE(object) == &g_BLOBVarType ||
  332. Py_TYPE(object) == &g_CLOBVarType ||
  333. Py_TYPE(object) == &g_LongStringVarType ||
  334. Py_TYPE(object) == &g_LongBinaryVarType ||
  335. Py_TYPE(object) == &g_NumberVarType ||
  336. Py_TYPE(object) == &g_StringVarType ||
  337. Py_TYPE(object) == &g_FixedCharVarType ||
  338. Py_TYPE(object) == &g_NCLOBVarType ||
  339. Py_TYPE(object) == &g_NCharVarType ||
  340. Py_TYPE(object) == &g_FixedNCharVarType ||
  341. Py_TYPE(object) == &g_LongNCharVarType ||
  342. Py_TYPE(object) == &g_RowidVarType ||
  343. Py_TYPE(object) == &g_BinaryVarType ||
  344. Py_TYPE(object) == &g_TimestampVarType ||
  345. Py_TYPE(object) == &g_IntervalVarType ||
  346. #if ORACLE_VERSION_HEX >= ORACLE_VERSION(12,1)
  347. Py_TYPE(object) == &g_BooleanVarType ||
  348. #endif
  349. Py_TYPE(object) == &g_NativeFloatVarType);
  350. }
  351. //-----------------------------------------------------------------------------
  352. // Variable_TypeByPythonType()
  353. // Return a variable type given a Python type object or NULL if the Python
  354. // type does not have a corresponding variable type.
  355. //-----------------------------------------------------------------------------
  356. static udt_VariableType *Variable_TypeByPythonType(
  357. udt_Cursor* cursor, // cursor variable created for
  358. PyObject* type) // Python type
  359. {
  360. if (type == (PyObject*) &g_StringVarType)
  361. return &vt_String;
  362. if (type == (PyObject*) cxString_Type)
  363. return &vt_String;
  364. if (type == (PyObject*) &g_FixedCharVarType)
  365. return &vt_FixedChar;
  366. if (type == (PyObject*) &g_NCharVarType)
  367. return &vt_NationalCharString;
  368. #if PY_MAJOR_VERSION < 3
  369. if (type == (PyObject*) &PyUnicode_Type)
  370. return &vt_NationalCharString;
  371. #endif
  372. if (type == (PyObject*) &g_FixedNCharVarType)
  373. return &vt_FixedNationalChar;
  374. if (type == (PyObject*) &g_LongNCharVarType)
  375. return &vt_LongNationalCharString;
  376. if (type == (PyObject*) &g_NCLOBVarType)
  377. return &vt_NCLOB;
  378. if (type == (PyObject*) &g_RowidVarType)
  379. return &vt_Rowid;
  380. if (type == (PyObject*) &g_BinaryVarType)
  381. return &vt_Binary;
  382. if (type == (PyObject*) &cxBinary_Type)
  383. return &vt_Binary;
  384. if (type == (PyObject*) &g_LongStringVarType)
  385. return &vt_LongString;
  386. if (type == (PyObject*) &g_LongBinaryVarType)
  387. return &vt_LongBinary;
  388. if (type == (PyObject*) &g_BFILEVarType)
  389. return &vt_BFILE;
  390. if (type == (PyObject*) &g_BLOBVarType)
  391. return &vt_BLOB;
  392. if (type == (PyObject*) &g_CLOBVarType)
  393. return &vt_CLOB;
  394. if (type == (PyObject*) &g_NumberVarType) {
  395. if (cursor->numbersAsStrings)
  396. return &vt_NumberAsString;
  397. return &vt_Float;
  398. }
  399. if (type == (PyObject*) &PyFloat_Type)
  400. return &vt_Float;
  401. #if PY_MAJOR_VERSION < 3
  402. if (type == (PyObject*) &PyInt_Type)
  403. return &vt_Integer;
  404. #endif
  405. if (type == (PyObject*) &PyLong_Type)
  406. return &vt_LongInteger;
  407. #if ORACLE_VERSION_HEX >= ORACLE_VERSION(12,1)
  408. if (type == (PyObject*) &g_BooleanVarType)
  409. return &vt_Boolean;
  410. #endif
  411. if (type == (PyObject*) &PyBool_Type)
  412. return &vt_Boolean;
  413. if (type == (PyObject*) &g_DateTimeVarType)
  414. return &vt_DateTime;
  415. if (type == (PyObject*) PyDateTimeAPI->DateType)
  416. return &vt_Date;
  417. if (type == (PyObject*) PyDateTimeAPI->DateTimeType)
  418. return &vt_DateTime;
  419. if (type == (PyObject*) &g_IntervalVarType)
  420. return &vt_Interval;
  421. if (type == (PyObject*) PyDateTimeAPI->DeltaType)
  422. return &vt_Interval;
  423. if (type == (PyObject*) &g_TimestampVarType)
  424. return &vt_Timestamp;
  425. if (type == (PyObject*) &g_CursorVarType)
  426. return &vt_Cursor;
  427. if (type == (PyObject*) &g_NativeFloatVarType)
  428. return &vt_NativeFloat;
  429. if (type == (PyObject*) &g_ObjectVarType)
  430. return &vt_Object;
  431. PyErr_SetString(g_NotSupportedErrorException,
  432. "Variable_TypeByPythonType(): unhandled data type");
  433. return NULL;
  434. }
  435. //-----------------------------------------------------------------------------
  436. // Variable_TypeByValue()
  437. // Return a variable type given a Python object or NULL if the Python
  438. // object does not have a corresponding variable type.
  439. //-----------------------------------------------------------------------------
  440. static udt_VariableType *Variable_TypeByValue(
  441. PyObject* value, // Python type
  442. ub4* size, // size to use (OUT)
  443. unsigned *numElements) // number of elements (OUT)
  444. {
  445. udt_VariableType *varType;
  446. PyObject *elementValue;
  447. char buffer[200];
  448. int i, result;
  449. // handle scalars
  450. if (value == Py_None) {
  451. *size = 1;
  452. return &vt_String;
  453. }
  454. if (cxString_Check(value)) {
  455. *size = cxString_GetSize(value);
  456. if (*size > 32768)
  457. return &vt_LongString;
  458. return &vt_String;
  459. }
  460. if (PyBool_Check(value))
  461. return &vt_Boolean;
  462. #if PY_MAJOR_VERSION < 3
  463. if (PyUnicode_Check(value)) {
  464. *size = PyUnicode_GET_SIZE(value);
  465. if (*size > 32768)
  466. return &vt_LongNationalCharString;
  467. return &vt_NationalCharString;
  468. }
  469. if (PyInt_Check(value))
  470. return &vt_Integer;
  471. #else
  472. if (PyBytes_Check(value)) {
  473. *size = PyBytes_GET_SIZE(value);
  474. return &vt_Binary;
  475. }
  476. #endif
  477. if (PyLong_Check(value))
  478. return &vt_LongInteger;
  479. if (PyFloat_Check(value))
  480. return &vt_Float;
  481. if (cxBinary_Check(value)) {
  482. udt_Buffer temp;
  483. if (cxBuffer_FromObject(&temp, value, NULL) < 0)
  484. return NULL;
  485. *size = temp.size;
  486. cxBuffer_Clear(&temp);
  487. return &vt_Binary;
  488. }
  489. if (PyDateTime_Check(value))
  490. return &vt_DateTime;
  491. if (PyDate_Check(value))
  492. return &vt_DateTime;
  493. if (PyDelta_Check(value))
  494. return &vt_Interval;
  495. result = PyObject_IsInstance(value, (PyObject*) &g_CursorType);
  496. if (result < 0)
  497. return NULL;
  498. if (result)
  499. return &vt_Cursor;
  500. if (Py_TYPE(value) == g_DateTimeType)
  501. return &vt_DateTime;
  502. if (Py_TYPE(value) == g_DecimalType)
  503. return &vt_NumberAsString;
  504. // handle arrays
  505. if (PyList_Check(value)) {
  506. elementValue = Py_None;
  507. for (i = 0; i < PyList_GET_SIZE(value); i++) {
  508. elementValue = PyList_GET_ITEM(value, i);
  509. if (elementValue != Py_None)
  510. break;
  511. }
  512. varType = Variable_TypeByValue(elementValue, size, numElements);
  513. if (!varType)
  514. return NULL;
  515. *numElements = PyList_GET_SIZE(value);
  516. *size = varType->size;
  517. return varType;
  518. }
  519. sprintf(buffer, "Variable_TypeByValue(): unhandled data type %.*s", 150,
  520. Py_TYPE(value)->tp_name);
  521. PyErr_SetString(g_NotSupportedErrorException, buffer);
  522. return NULL;
  523. }
  524. //-----------------------------------------------------------------------------
  525. // Variable_TypeByOracleDataType()
  526. // Return a variable type given an Oracle data type or NULL if the Oracle
  527. // data type does not have a corresponding variable type.
  528. //-----------------------------------------------------------------------------
  529. static udt_VariableType *Variable_TypeByOracleDataType (
  530. ub2 oracleDataType, // Oracle data type
  531. ub1 charsetForm) // character set form
  532. {
  533. char buffer[100];
  534. switch(oracleDataType) {
  535. case SQLT_LNG:
  536. return &vt_LongString;
  537. case SQLT_AFC:
  538. if (charsetForm == SQLCS_NCHAR)
  539. return &vt_FixedNationalChar;
  540. return &vt_FixedChar;
  541. case SQLT_CHR:
  542. if (charsetForm == SQLCS_NCHAR)
  543. return &vt_NationalCharString;
  544. return &vt_String;
  545. case SQLT_RDD:
  546. return &vt_Rowid;
  547. case SQLT_BIN:
  548. return &vt_Binary;
  549. case SQLT_LBI:
  550. return &vt_LongBinary;
  551. case SQLT_BFLOAT:
  552. case SQLT_IBFLOAT:
  553. case SQLT_BDOUBLE:
  554. case SQLT_IBDOUBLE:
  555. return &vt_NativeFloat;
  556. case SQLT_NUM:
  557. case SQLT_VNU:
  558. return &vt_Float;
  559. case SQLT_DAT:
  560. case SQLT_ODT:
  561. return &vt_DateTime;
  562. case SQLT_DATE:
  563. case SQLT_TIMESTAMP:
  564. case SQLT_TIMESTAMP_TZ:
  565. case SQLT_TIMESTAMP_LTZ:
  566. return &vt_Timestamp;
  567. case SQLT_INTERVAL_DS:
  568. return &vt_Interval;
  569. case SQLT_CLOB:
  570. if (charsetForm == SQLCS_NCHAR)
  571. return &vt_NCLOB;
  572. return &vt_CLOB;
  573. case SQLT_BLOB:
  574. return &vt_BLOB;
  575. case SQLT_BFILE:
  576. return &vt_BFILE;
  577. case SQLT_RSET:
  578. return &vt_Cursor;
  579. case SQLT_NTY:
  580. return &vt_Object;
  581. }
  582. sprintf(buffer, "Variable_TypeByOracleDataType: unhandled data type %d",
  583. oracleDataType);
  584. PyErr_SetString(g_NotSupportedErrorException, buffer);
  585. return NULL;
  586. }
  587. //-----------------------------------------------------------------------------
  588. // Variable_TypeByOracleDescriptor()
  589. // Return a variable type given an Oracle descriptor.
  590. //-----------------------------------------------------------------------------
  591. static udt_VariableType *Variable_TypeByOracleDescriptor(
  592. OCIParam *param, // parameter to get type from
  593. udt_Environment *environment) // environment to use
  594. {
  595. ub1 charsetForm;
  596. ub2 dataType;
  597. sword status;
  598. // retrieve datatype of the parameter
  599. status = OCIAttrGet(param, OCI_HTYPE_DESCRIBE, (dvoid*) &dataType, 0,
  600. OCI_ATTR_DATA_TYPE, environment->errorHandle);
  601. if (Environment_CheckForError(environment, status,
  602. "Variable_TypeByOracleDescriptor(): data type") < 0)
  603. return NULL;
  604. // retrieve character set form of the parameter
  605. if (dataType != SQLT_CHR && dataType != SQLT_AFC &&
  606. dataType != SQLT_CLOB) {
  607. charsetForm = SQLCS_IMPLICIT;
  608. } else {
  609. status = OCIAttrGet(param, OCI_HTYPE_DESCRIBE, (dvoid*) &charsetForm,
  610. 0, OCI_ATTR_CHARSET_FORM, environment->errorHandle);
  611. if (Environment_CheckForError(environment, status,
  612. "Variable_TypeByOracleDescriptor(): charset form") < 0)
  613. return NULL;
  614. }
  615. return Variable_TypeByOracleDataType(dataType, charsetForm);
  616. }
  617. //-----------------------------------------------------------------------------
  618. // Variable_MakeArray()
  619. // Make the variable an array, ensuring that the type supports arrays.
  620. //-----------------------------------------------------------------------------
  621. static int Variable_MakeArray(
  622. udt_Variable *var) // variable to make an array
  623. {
  624. if (!var->type->canBeInArray) {
  625. PyErr_SetString(g_NotSupportedErrorException,
  626. "Variable_MakeArray(): type does not support arrays");
  627. return -1;
  628. }
  629. var->isArray = 1;
  630. return 0;
  631. }
  632. //-----------------------------------------------------------------------------
  633. // Variable_DefaultNewByValue()
  634. // Default method for determining the type of variable to use for the data.
  635. //-----------------------------------------------------------------------------
  636. static udt_Variable *Variable_DefaultNewByValue(
  637. udt_Cursor *cursor, // cursor to associate variable with
  638. PyObject *value, // Python value to associate
  639. unsigned numElements) // number of elements to allocate
  640. {
  641. udt_VariableType *varType;
  642. udt_Variable *var;
  643. ub4 size = 0;
  644. varType = Variable_TypeByValue(value, &size, &numElements);
  645. if (!varType)
  646. return NULL;
  647. var = Variable_New(cursor, numElements, varType, size);
  648. if (!var)
  649. return NULL;
  650. if (PyList_Check(value)) {
  651. if (Variable_MakeArray(var) < 0) {
  652. Py_DECREF(var);
  653. return NULL;
  654. }
  655. }
  656. return var;
  657. }
  658. //-----------------------------------------------------------------------------
  659. // Variable_NewByInputTypeHandler()
  660. // Allocate a new variable by looking at the type of the data.
  661. //-----------------------------------------------------------------------------
  662. static udt_Variable *Variable_NewByInputTypeHandler(
  663. udt_Cursor *cursor, // cursor to associate variable with
  664. PyObject *inputTypeHandler, // input type handler
  665. PyObject *value, // Python value to associate
  666. unsigned numElements) // number of elements to allocate
  667. {
  668. PyObject *var;
  669. var = PyObject_CallFunction(inputTypeHandler, "OOi", cursor, value,
  670. numElements);
  671. if (!var)
  672. return NULL;
  673. if (var != Py_None) {
  674. if (!Variable_Check(var)) {
  675. Py_DECREF(var);
  676. PyErr_SetString(PyExc_TypeError,
  677. "expecting variable from input type handler");
  678. return NULL;
  679. }
  680. return (udt_Variable*) var;
  681. }
  682. Py_DECREF(var);
  683. return Variable_DefaultNewByValue(cursor, value, numElements);
  684. }
  685. //-----------------------------------------------------------------------------
  686. // Variable_NewByValue()
  687. // Allocate a new variable by looking at the type of the data.
  688. //-----------------------------------------------------------------------------
  689. static udt_Variable *Variable_NewByValue(
  690. udt_Cursor *cursor, // cursor to associate variable with
  691. PyObject *value, // Python value to associate
  692. unsigned numElements) // number of elements to allocate
  693. {
  694. if (cursor->inputTypeHandler && cursor->inputTypeHandler != Py_None)
  695. return Variable_NewByInputTypeHandler(cursor, cursor->inputTypeHandler,
  696. value, numElements);
  697. if (cursor->connection->inputTypeHandler &&
  698. cursor->connection->inputTypeHandler != Py_None)
  699. return Variable_NewByInputTypeHandler(cursor,
  700. cursor->connection->inputTypeHandler, value, numElements);
  701. return Variable_DefaultNewByValue(cursor, value, numElements);
  702. }
  703. //-----------------------------------------------------------------------------
  704. // Variable_NewArrayByType()
  705. // Allocate a new PL/SQL array by looking at the Python data type.
  706. //-----------------------------------------------------------------------------
  707. static udt_Variable *Variable_NewArrayByType(
  708. udt_Cursor *cursor, // cursor to bind variable to
  709. PyObject *value) // value to bind
  710. {
  711. PyObject *typeObj, *numElementsObj;
  712. udt_VariableType *varType;
  713. unsigned numElements;
  714. udt_Variable *var;
  715. if (PyList_GET_SIZE(value) != 2) {
  716. PyErr_SetString(g_ProgrammingErrorException,
  717. "expecting an array of two elements [type, numelems]");
  718. return NULL;
  719. }
  720. typeObj = PyList_GET_ITEM(value, 0);
  721. numElementsObj = PyList_GET_ITEM(value, 1);
  722. if (!PyInt_Check(numElementsObj)) {
  723. PyErr_SetString(g_ProgrammingErrorException,
  724. "number of elements must be an integer");
  725. return NULL;
  726. }
  727. varType = Variable_TypeByPythonType(cursor, typeObj);
  728. if (!varType)
  729. return NULL;
  730. numElements = PyInt_AsLong(numElementsObj);
  731. if (PyErr_Occurred())
  732. return NULL;
  733. var = Variable_New(cursor, numElements, varType, varType->size);
  734. if (!var)
  735. return NULL;
  736. if (Variable_MakeArray(var) < 0) {
  737. Py_DECREF(var);
  738. return NULL;
  739. }
  740. return var;
  741. }
  742. //-----------------------------------------------------------------------------
  743. // Variable_NewByType()
  744. // Allocate a new variable by looking at the Python data type.
  745. //-----------------------------------------------------------------------------
  746. static udt_Variable *Variable_NewByType(
  747. udt_Cursor *cursor, // cursor to associate variable with
  748. PyObject *value, // Python data type to associate
  749. unsigned numElements) // number of elements to allocate
  750. {
  751. udt_VariableType *varType;
  752. int size;
  753. // passing an integer is assumed to be a string
  754. if (PyInt_Check(value)) {
  755. size = PyInt_AsLong(value);
  756. if (PyErr_Occurred())
  757. return NULL;
  758. if (size > 32768)
  759. varType = &vt_LongString;
  760. else varType = &vt_String;
  761. return Variable_New(cursor, numElements, varType, size);
  762. }
  763. // passing an array of two elements to define an array
  764. if (PyList_Check(value))
  765. return Variable_NewArrayByType(cursor, value);
  766. // handle directly bound variables
  767. if (Variable_Check(value)) {
  768. Py_INCREF(value);
  769. return (udt_Variable*) value;
  770. }
  771. // everything else ought to be a Python type
  772. varType = Variable_TypeByPythonType(cursor, value);
  773. if (!varType)
  774. return NULL;
  775. return Variable_New(cursor, numElements, varType, varType->size);
  776. }
  777. //-----------------------------------------------------------------------------
  778. // Variable_NewByOutputTypeHandler()
  779. // Create a new variable by calling the output type handler.
  780. //-----------------------------------------------------------------------------
  781. static udt_Variable *Variable_NewByOutputTypeHandler(
  782. udt_Cursor *cursor, // cursor to associate variable with
  783. OCIParam *param, // parameter descriptor
  784. PyObject *outputTypeHandler, // method to call to get type
  785. udt_VariableType *varType, // variable type already chosen
  786. ub4 size, // maximum size of variable
  787. unsigned numElements) // number of elements
  788. {
  789. udt_Variable *var;
  790. PyObject *result;
  791. ub4 nameLength;
  792. sb2 precision;
  793. sword status;
  794. char *name;
  795. sb1 scale;
  796. // determine name of variable
  797. status = OCIAttrGet(param, OCI_HTYPE_DESCRIBE, (dvoid*) &name,
  798. &nameLength, OCI_ATTR_NAME, cursor->environment->errorHandle);
  799. if (Environment_CheckForError(cursor->environment, status,
  800. "Variable_NewByOutputTypeHandler(): get name") < 0)
  801. return NULL;
  802. // retrieve scale and precision of the parameter, if applicable
  803. precision = scale = 0;
  804. if (varType->pythonType == &g_NumberVarType) {
  805. status = OCIAttrGet(param, OCI_HTYPE_DESCRIBE, (dvoid*) &scale, 0,
  806. OCI_ATTR_SCALE, cursor->environment->errorHandle);
  807. if (Environment_CheckForError(cursor->environment, status,
  808. "Variable_NewByOutputTypeHandler(): get scale") < 0)
  809. return NULL;
  810. status = OCIAttrGet(param, OCI_HTYPE_DESCRIBE, (dvoid*) &precision, 0,
  811. OCI_ATTR_PRECISION, cursor->environment->errorHandle);
  812. if (Environment_CheckForError(cursor->environment, status,
  813. "Variable_NewByOutputTypeHandler(): get precision") < 0)
  814. return NULL;
  815. }
  816. // call method, passing parameters
  817. result = PyObject_CallFunction(outputTypeHandler, "Os#Oiii", cursor, name,
  818. nameLength, varType->pythonType, size, precision, scale);
  819. if (!result)
  820. return NULL;
  821. // if result is None, assume default behavior
  822. if (result == Py_None) {
  823. Py_DECREF(result);
  824. return Variable_New(cursor, numElements, varType, size);
  825. }
  826. // otherwise, verify that the result is an actual variable
  827. if (!Variable_Check(result)) {
  828. Py_DECREF(result);
  829. PyErr_SetString(PyExc_TypeError,
  830. "expecting variable from output type handler");
  831. return NULL;
  832. }
  833. // verify that the array size is sufficient to handle the fetch
  834. var = (udt_Variable*) result;
  835. if (var->allocatedElements < cursor->fetchArraySize) {
  836. Py_DECREF(result);
  837. PyErr_SetString(PyExc_TypeError,
  838. "expecting variable with array size large enough for fetch");
  839. return NULL;
  840. }
  841. return var;
  842. }
  843. //-----------------------------------------------------------------------------
  844. // Variable_DefineHelper()
  845. // Helper routine for Variable_Define() used so that constant calls to
  846. // OCIDescriptorFree() is not necessary.
  847. //-----------------------------------------------------------------------------
  848. static udt_Variable *Variable_DefineHelper(
  849. udt_Cursor *cursor, // cursor in use
  850. OCIParam *param, // parameter descriptor
  851. unsigned position, // position in define list
  852. unsigned numElements) // number of elements to create
  853. {
  854. udt_VariableType *varType;
  855. ub2 sizeFromOracle;
  856. udt_Variable *var;
  857. sword status;
  858. ub4 size;
  859. // determine data type
  860. varType = Variable_TypeByOracleDescriptor(param, cursor->environment);
  861. if (!varType)
  862. return NULL;
  863. if (cursor->numbersAsStrings && varType == &vt_Float)
  864. varType = &vt_NumberAsString;
  865. // retrieve size of the parameter
  866. size = varType->size;
  867. if (varType->isVariableLength) {
  868. // determine the maximum length from Oracle
  869. status = OCIAttrGet(param, OCI_HTYPE_DESCRIBE,
  870. (dvoid*) &sizeFromOracle, 0, OCI_ATTR_DATA_SIZE,
  871. cursor->environment->errorHandle);
  872. if (Environment_CheckForError(cursor->environment, status,
  873. "Variable_Define(): data size") < 0)
  874. return NULL;
  875. // use the length from Oracle directly if available
  876. if (sizeFromOracle)
  877. size = sizeFromOracle;
  878. // otherwise, use the value set with the setoutputsize() parameter
  879. else if (cursor->outputSize >= 0) {
  880. if (cursor->outputSizeColumn < 0 ||
  881. (int) position == cursor->outputSizeColumn)
  882. size = cursor->outputSize;
  883. }
  884. }
  885. // create a variable of the correct type
  886. if (cursor->outputTypeHandler && cursor->outputTypeHandler != Py_None)
  887. var = Variable_NewByOutputTypeHandler(cursor, param,
  888. cursor->outputTypeHandler, varType, size, numElements);
  889. else if (cursor->connection->outputTypeHandler &&
  890. cursor->connection->outputTypeHandler != Py_None)
  891. var = Variable_NewByOutputTypeHandler(cursor, param,
  892. cursor->connection->outputTypeHandler, varType, size,
  893. numElements);
  894. else var = Variable_New(cursor, numElements, varType, size);
  895. if (!var)
  896. return NULL;
  897. // call the procedure to set values prior to define
  898. if (var->type->preDefineProc) {
  899. if ((*var->type->preDefineProc)(var, param) < 0) {
  900. Py_DECREF(var);
  901. return NULL;
  902. }
  903. }
  904. // perform the define
  905. status = OCIDEFINEBYPOS(cursor->handle, &var->defineHandle,
  906. var->environment->errorHandle, position, var->data,
  907. var->bufferSize, var->type->oracleType, var->indicator,
  908. var->actualLength, var->returnCode, OCI_DEFAULT);
  909. if (Environment_CheckForError(var->environment, status,
  910. "Variable_Define(): define") < 0) {
  911. Py_DECREF(var);
  912. return NULL;
  913. }
  914. // call the procedure to set values after define
  915. if (var->type->postDefineProc) {
  916. if ((*var->type->postDefineProc)(var) < 0) {
  917. Py_DECREF(var);
  918. return NULL;
  919. }
  920. }
  921. return var;
  922. }
  923. //-----------------------------------------------------------------------------
  924. // Variable_Define()
  925. // Allocate a variable and define it for the given statement.
  926. //-----------------------------------------------------------------------------
  927. static udt_Variable *Variable_Define(
  928. udt_Cursor *cursor, // cursor to define for
  929. unsigned numElements, // number of elements to create
  930. unsigned position) // position to define
  931. {
  932. udt_Variable *var;
  933. OCIParam *param;
  934. sword status;
  935. // retrieve parameter descriptor
  936. status = OCIParamGet(cursor->handle, OCI_HTYPE_STMT,
  937. cursor->environment->errorHandle, (void**) &param, position);
  938. if (Environment_CheckForError(cursor->environment, status,
  939. "Variable_Define(): parameter") < 0)
  940. return NULL;
  941. // call the helper to do the actual work
  942. var = Variable_DefineHelper(cursor, param, position, numElements);
  943. OCIDescriptorFree(param, OCI_DTYPE_PARAM);
  944. return var;
  945. }
  946. //-----------------------------------------------------------------------------
  947. // Variable_InternalBind()
  948. // Allocate a variable and bind it to the given statement.
  949. //-----------------------------------------------------------------------------
  950. static int Variable_InternalBind(
  951. udt_Variable *var) // variable to bind
  952. {
  953. sword status;
  954. // perform the bind
  955. if (var->boundName) {
  956. udt_Buffer buffer;
  957. if (cxBuffer_FromObject(&buffer, var->boundName,
  958. var->environment->encoding) < 0)
  959. return -1;
  960. if (var->isArray) {
  961. status = OCIBINDBYNAME(var->boundCursorHandle, &var->bindHandle,
  962. var->environment->errorHandle, (text*) buffer.ptr,
  963. buffer.size, var->data, var->bufferSize,
  964. var->type->oracleType, var->indicator, var->actualLength,
  965. var->returnCode, var->allocatedElements,
  966. &var->actualElements, OCI_DEFAULT);
  967. } else {
  968. status = OCIBINDBYNAME(var->boundCursorHandle, &var->bindHandle,
  969. var->environment->errorHandle, (text*) buffer.ptr,
  970. buffer.size, var->data, var->bufferSize,
  971. var->type->oracleType, var->indicator, var->actualLength,
  972. var->returnCode, 0, 0, OCI_DEFAULT);
  973. }
  974. cxBuffer_Clear(&buffer);
  975. } else {
  976. if (var->isArray) {
  977. status = OCIBINDBYPOS(var->boundCursorHandle, &var->bindHandle,
  978. var->environment->errorHandle, var->boundPos, var->data,
  979. var->bufferSize, var->type->oracleType, var->indicator,
  980. var->actualLength, var->returnCode, var->allocatedElements,
  981. &var->actualElements, OCI_DEFAULT);
  982. } else {
  983. status = OCIBINDBYPOS(var->boundCursorHandle, &var->bindHandle,
  984. var->environment->errorHandle, var->boundPos, var->data,
  985. var->bufferSize, var->type->oracleType, var->indicator,
  986. var->actualLength, var->returnCode, 0, 0, OCI_DEFAULT);
  987. }
  988. }
  989. if (Environment_CheckForError(var->environment, status,
  990. "Variable_InternalBind()") < 0)
  991. return -1;
  992. // set the charset form and id if applicable
  993. if (var->type->charsetForm != SQLCS_IMPLICIT) {
  994. status = OCIAttrSet(var->bindHandle, OCI_HTYPE_BIND,
  995. (dvoid*) &var->type->charsetForm, 0, OCI_ATTR_CHARSET_FORM,
  996. var->environment->errorHandle);
  997. if (Environment_CheckForError(var->environment, status,
  998. "Variable_InternalBind(): set charset form") < 0)
  999. return -1;
  1000. status = OCIAttrSet(var->bindHandle, OCI_HTYPE_BIND,
  1001. (dvoid*) &var->bufferSize, 0, OCI_ATTR_MAXDATA_SIZE,
  1002. var->environment->errorHandle);
  1003. if (Environment_CheckForError(var->environment, status,
  1004. "Variable_InternalBind(): set max data size") < 0)
  1005. return -1;
  1006. }
  1007. // set the max data size for strings
  1008. if ((var->type == &vt_String || var->type == &vt_FixedChar)
  1009. && var->size > var->type->size) {
  1010. status = OCIAttrSet(var->bindHandle, OCI_HTYPE_BIND,
  1011. (dvoid*) &var->type->size, 0, OCI_ATTR_MAXDATA_SIZE,
  1012. var->environment->errorHandle);
  1013. if (Environment_CheckForError(var->environment, status,
  1014. "Variable_InternalBind(): set max data size") < 0)
  1015. return -1;
  1016. }
  1017. return 0;
  1018. }
  1019. //-----------------------------------------------------------------------------
  1020. // Variable_Bind()
  1021. // Allocate a variable and bind it to the given statement.
  1022. //-----------------------------------------------------------------------------
  1023. static int Variable_Bind(
  1024. udt_Variable *var, // variable to bind
  1025. udt_Cursor *cursor, // cursor to bind to
  1026. PyObject *name, // name to bind to
  1027. ub4 pos) // position to bind to
  1028. {
  1029. // nothing to do if already bound
  1030. if (var->bindHandle && name == var->boundName && pos == var->boundPos)
  1031. return 0;
  1032. // set the instance variables specific for binding
  1033. var->boundPos = pos;
  1034. var->boundCursorHandle = cursor->handle;
  1035. Py_XDECREF(var->boundName);
  1036. Py_XINCREF(name);
  1037. var->boundName = name;
  1038. // perform the bind
  1039. return Variable_InternalBind(var);
  1040. }
  1041. //-----------------------------------------------------------------------------
  1042. // Variable_VerifyFetch()
  1043. // Verifies that truncation or other problems did not take place on retrieve.
  1044. //-----------------------------------------------------------------------------
  1045. static int Variable_VerifyFetch(
  1046. udt_Variable *var, // variable to check fetch for
  1047. unsigned arrayPos) // array position
  1048. {
  1049. char messageText[200];
  1050. udt_Error *error;
  1051. if (var->type->isVariableLength) {
  1052. if (var->returnCode[arrayPos] != 0) {
  1053. error = Error_New(var->environment, "Variable_VerifyFetch()", 0,
  1054. NULL);
  1055. error->code = var->returnCode[arrayPos];
  1056. sprintf(messageText,
  1057. "column at array pos %d fetched with error: %d",
  1058. arrayPos, var->returnCode[arrayPos]);
  1059. error->message = cxString_FromAscii(messageText);
  1060. if (!error->message)
  1061. Py_DECREF(error);
  1062. else PyErr_SetObject(g_DatabaseErrorException, (PyObject*) error);
  1063. return -1;
  1064. }
  1065. }
  1066. return 0;
  1067. }
  1068. //-----------------------------------------------------------------------------
  1069. // Variable_GetSingleValue()
  1070. // Return the value of the variable at the given position.
  1071. //-----------------------------------------------------------------------------
  1072. static PyObject *Variable_GetSingleValue(
  1073. udt_Variable *var, // variable to get the value for
  1074. unsigned arrayPos) // array position
  1075. {
  1076. PyObject *value, *result;
  1077. int isNull;
  1078. // ensure we do not exceed the number of allocated elements
  1079. if (arrayPos >= var->allocatedElements) {
  1080. PyErr_SetString(PyExc_IndexError,
  1081. "Variable_GetSingleValue: array size exceeded");
  1082. return NULL;
  1083. }
  1084. // check for a NULL value
  1085. if (var->type->isNullProc)
  1086. isNull = (*var->type->isNullProc)(var, arrayPos);
  1087. else isNull = (var->indicator[arrayPos] == OCI_IND_NULL);
  1088. if (isNull) {
  1089. Py_INCREF(Py_None);
  1090. return Py_None;
  1091. }
  1092. // check for truncation or other problems on retrieve
  1093. if (Variable_VerifyFetch(var, arrayPos) < 0)
  1094. return NULL;
  1095. // calculate value to return
  1096. value = (*var->type->getValueProc)(var, arrayPos);
  1097. if (value && var->outConverter && var->outConverter != Py_None) {
  1098. result = PyObject_CallFunctionObjArgs(var->outConverter, value, NULL);
  1099. Py_DECREF(value);
  1100. return result;
  1101. }
  1102. return value;
  1103. }
  1104. //-----------------------------------------------------------------------------
  1105. // Variable_GetArrayValue()
  1106. // Return the value of the variable as an array.
  1107. //-----------------------------------------------------------------------------
  1108. static PyObject *Variable_GetArrayValue(
  1109. udt_Variable *var, // variable to get the value for
  1110. ub4 numElements) // number of elements to include
  1111. {
  1112. PyObject *value, *singleValue;
  1113. ub4 i;
  1114. value = PyList_New(numElements);
  1115. if (!value)
  1116. return NULL;
  1117. for (i = 0; i < numElements; i++) {
  1118. singleValue = Variable_GetSingleValue(var, i);
  1119. if (!singleValue) {
  1120. Py_DECREF(value);
  1121. return NULL;
  1122. }
  1123. PyList_SET_ITEM(value, i, singleValue);
  1124. }
  1125. return value;
  1126. }
  1127. //-----------------------------------------------------------------------------
  1128. // Variable_GetValue()
  1129. // Return the value of the variable.
  1130. //-----------------------------------------------------------------------------
  1131. static PyObject *Variable_GetValue(
  1132. udt_Variable *var, // variable to get the value for
  1133. unsigned arrayPos) // array position
  1134. {
  1135. if (var->isArray)
  1136. return Variable_GetArrayValue(var, var->actualElements);
  1137. return Variable_GetSingleValue(var, arrayPos);
  1138. }
  1139. //-----------------------------------------------------------------------------
  1140. // Variable_SetSingleValue()
  1141. // Set a single value in the variable.
  1142. //-----------------------------------------------------------------------------
  1143. static int Variable_SetSingleValue(
  1144. udt_Variable *var, // variable to set value for
  1145. unsigned arrayPos, // array position
  1146. PyObject *value) // value to set
  1147. {
  1148. PyObject *convertedValue = NULL;
  1149. int result;
  1150. // ensure we do not exceed the number of allocated elements
  1151. if (arrayPos >= var->allocatedElements) {
  1152. PyErr_SetString(PyExc_IndexError,
  1153. "Variable_SetSingleValue: array size exceeded");
  1154. return -1;
  1155. }
  1156. // convert value, if necessary
  1157. if (var->inConverter && var->inConverter != Py_None) {
  1158. convertedValue = PyObject_CallFunctionObjArgs(var->inConverter, value,
  1159. NULL);
  1160. if (!convertedValue)
  1161. return -1;
  1162. value = convertedValue;
  1163. }
  1164. // check for a NULL value
  1165. if (value == Py_None) {
  1166. var->indicator[arrayPos] = OCI_IND_NULL;
  1167. Py_XDECREF(convertedValue);
  1168. return 0;
  1169. }
  1170. var->indicator[arrayPos] = OCI_IND_NOTNULL;
  1171. if (var->type->isVariableLength)
  1172. var->returnCode[arrayPos] = 0;
  1173. result = (*var->type->setValueProc)(var, arrayPos, value);
  1174. Py_XDECREF(convertedValue);
  1175. return result;
  1176. }
  1177. //-----------------------------------------------------------------------------
  1178. // Variable_SetArrayValue()
  1179. // Set all of the array values for the variable.
  1180. //-----------------------------------------------------------------------------
  1181. static int Variable_SetArrayValue(
  1182. udt_Variable *var, // variable to set value for
  1183. PyObject *value) // value to set
  1184. {
  1185. unsigned numElements;
  1186. ub4 i;
  1187. // ensure we have an array to set
  1188. if (!PyList_Check(value)) {
  1189. PyErr_SetString(PyExc_TypeError, "expecting array data");
  1190. return -1;
  1191. }
  1192. // ensure we haven't exceeded the number of allocated elements
  1193. numElements = PyList_GET_SIZE(value);
  1194. if (numElements > var->allocatedElements) {
  1195. PyErr_SetString(PyExc_IndexError,
  1196. "Variable_SetArrayValue: array size exceeded");
  1197. return -1;
  1198. }
  1199. // set all of the values
  1200. var->actualElements = numElements;
  1201. for (i = 0; i < var->actualElements; i++) {
  1202. if (Variable_SetSingleValue(var, i, PyList_GET_ITEM(value, i)) < 0)
  1203. return -1;
  1204. }
  1205. return 0;
  1206. }
  1207. //-----------------------------------------------------------------------------
  1208. // Variable_SetValue()
  1209. // Set the value of the variable.
  1210. //-----------------------------------------------------------------------------
  1211. static int Variable_SetValue(
  1212. udt_Variable *var, // variable to set
  1213. unsigned arrayPos, // array position
  1214. PyObject *value) // value to set
  1215. {
  1216. if (var->isArray) {
  1217. if (arrayPos > 0) {
  1218. PyErr_SetString(g_NotSupportedErrorException,
  1219. "arrays of arrays are not supported by the OCI");
  1220. return -1;
  1221. }
  1222. return Variable_SetArrayValue(var, value);
  1223. }
  1224. return Variable_SetSingleValue(var, arrayPos, value);
  1225. }
  1226. //-----------------------------------------------------------------------------
  1227. // Variable_ExternalCopy()
  1228. // Copy the contents of the source variable to the destination variable.
  1229. //-----------------------------------------------------------------------------
  1230. static PyObject *Variable_ExternalCopy(
  1231. udt_Variable *targetVar, // variable to set
  1232. PyObject *args) // arguments
  1233. {
  1234. unsigned sourcePos, targetPos;
  1235. udt_Variable *sourceVar;
  1236. // parse arguments; verify that copy is possible
  1237. if (!PyArg_ParseTuple(args, "Oii", &sourceVar, &sourcePos, &targetPos))
  1238. return NULL;
  1239. if (Py_TYPE(targetVar) != Py_TYPE(sourceVar)) {
  1240. PyErr_SetString(g_ProgrammingErrorException,
  1241. "source and target variable type must match");
  1242. return NULL;
  1243. }
  1244. if (!sourceVar->type->canBeCopied) {
  1245. PyErr_SetString(g_ProgrammingErrorException,
  1246. "variable does not support copying");
  1247. return NULL;
  1248. }
  1249. // ensure array positions are not violated
  1250. if (sourcePos >= sourceVar->allocatedElements) {
  1251. PyErr_SetString(PyExc_IndexError,
  1252. "Variable_ExternalCopy: source array size exceeded");
  1253. return NULL;
  1254. }
  1255. if (targetPos >= targetVar->allocatedElements) {
  1256. PyErr_SetString(PyExc_IndexError,
  1257. "Variable_ExternalCopy: target array size exceeded");
  1258. return NULL;
  1259. }
  1260. // ensure target can support amount data from the source
  1261. if (targetVar->bufferSize < sourceVar->bufferSize) {
  1262. PyErr_SetString(g_ProgrammingErrorException,
  1263. "target variable has insufficient space to copy source data");
  1264. return NULL;
  1265. }
  1266. // handle null case directly
  1267. if (sourceVar->indicator[sourcePos] == OCI_IND_NULL)
  1268. targetVar->indicator[targetPos] = OCI_IND_NULL;
  1269. // otherwise, copy data
  1270. else {
  1271. targetVar->indicator[targetPos] = OCI_IND_NOTNULL;
  1272. if (Variable_VerifyFetch(sourceVar, sourcePos) < 0)
  1273. return NULL;
  1274. if (targetVar->actualLength)
  1275. targetVar->actualLength[targetPos] =
  1276. sourceVar->actualLength[sourcePos];
  1277. if (targetVar->returnCode)
  1278. targetVar->returnCode[targetPos] =
  1279. sourceVar->returnCode[sourcePos];
  1280. memcpy( (char*) targetVar->data + targetPos * targetVar->bufferSize,
  1281. (char*) sourceVar->data + sourcePos * sourceVar->bufferSize,
  1282. sourceVar->bufferSize);
  1283. }
  1284. Py_INCREF(Py_None);
  1285. return Py_None;
  1286. }
  1287. //-----------------------------------------------------------------------------
  1288. // Variable_ExternalSetValue()
  1289. // Set the value of the variable at the given position.
  1290. //-----------------------------------------------------------------------------
  1291. static PyObject *Variable_ExternalSetValue(
  1292. udt_Variable *var, // variable to set
  1293. PyObject *args) // arguments
  1294. {
  1295. PyObject *value;
  1296. unsigned pos;
  1297. if (!PyArg_ParseTuple(args, "iO", &pos, &value))
  1298. return NULL;
  1299. if (Variable_SetValue(var, pos, value) < 0)
  1300. return NULL;
  1301. Py_INCREF(Py_None);
  1302. return Py_None;
  1303. }
  1304. //-----------------------------------------------------------------------------
  1305. // Variable_ExternalGetValue()
  1306. // Return the value of the variable at the given position.
  1307. //-----------------------------------------------------------------------------
  1308. static PyObject *Variable_ExternalGetValue(
  1309. udt_Variable *var, // variable to set
  1310. PyObject *args, // arguments
  1311. PyObject *keywordArgs) // keyword arguments
  1312. {
  1313. static char *keywordList[] = { "pos", NULL };
  1314. unsigned pos = 0;
  1315. if (!PyArg_ParseTupleAndKeywords(args, keywordArgs, "|i", keywordList,
  1316. &pos))
  1317. return NULL;
  1318. return Variable_GetValue(var, pos);
  1319. }
  1320. //-----------------------------------------------------------------------------
  1321. // Variable_Repr()
  1322. // Return a string representation of the variable.
  1323. //-----------------------------------------------------------------------------
  1324. static PyObject *Variable_Repr(
  1325. udt_Variable *var) // variable to return the string for
  1326. {
  1327. PyObject *valueRepr, *value, *module, *name, *result, *format, *formatArgs;
  1328. if (var->isArray)
  1329. value = Variable_GetArrayValue(var, var->actualElements);
  1330. else if (var->allocatedElements == 1)
  1331. value = Variable_GetSingleValue(var, 0);
  1332. else value = Variable_GetArrayValue(var, var->allocatedElements);
  1333. if (!value)
  1334. return NULL;
  1335. valueRepr = PyObject_Repr(value);
  1336. Py_DECREF(value);
  1337. if (!valueRepr)
  1338. return NULL;
  1339. format = cxString_FromAscii("<%s.%s with value %s>");
  1340. if (!format) {
  1341. Py_DECREF(valueRepr);
  1342. return NULL;
  1343. }
  1344. if (GetModuleAndName(Py_TYPE(var), &module, &name) < 0) {
  1345. Py_DECREF(valueRepr);
  1346. Py_DECREF(format);
  1347. return NULL;
  1348. }
  1349. formatArgs = PyTuple_Pack(3, module, name, valueRepr);
  1350. Py_DECREF(module);
  1351. Py_DECREF(name);
  1352. Py_DECREF(valueRepr);
  1353. if (!formatArgs) {
  1354. Py_DECREF(format);
  1355. return NULL;
  1356. }
  1357. result = cxString_Format(format, formatArgs);
  1358. Py_DECREF(format);
  1359. Py_DECREF(formatArgs);
  1360. return result;
  1361. }