Variable.c 53 KB

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