IntervalVar.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. //-----------------------------------------------------------------------------
  2. // IntervalVar.c
  3. // Defines the routines for handling interval variables.
  4. //-----------------------------------------------------------------------------
  5. //-----------------------------------------------------------------------------
  6. // Interval type
  7. //-----------------------------------------------------------------------------
  8. typedef struct {
  9. Variable_HEAD
  10. OCIInterval **data;
  11. } udt_IntervalVar;
  12. //-----------------------------------------------------------------------------
  13. // Declaration of interval variable functions.
  14. //-----------------------------------------------------------------------------
  15. static int IntervalVar_Initialize(udt_IntervalVar*, udt_Cursor*);
  16. static void IntervalVar_Finalize(udt_IntervalVar*);
  17. static int IntervalVar_SetValue(udt_IntervalVar*, unsigned, PyObject*);
  18. static PyObject *IntervalVar_GetValue(udt_IntervalVar*, unsigned);
  19. //-----------------------------------------------------------------------------
  20. // Python type declarations
  21. //-----------------------------------------------------------------------------
  22. static PyTypeObject g_IntervalVarType = {
  23. PyVarObject_HEAD_INIT(NULL, 0)
  24. "cx_Oracle.INTERVAL", // tp_name
  25. sizeof(udt_IntervalVar), // tp_basicsize
  26. 0, // tp_itemsize
  27. 0, // tp_dealloc
  28. 0, // tp_print
  29. 0, // tp_getattr
  30. 0, // tp_setattr
  31. 0, // tp_compare
  32. 0, // tp_repr
  33. 0, // tp_as_number
  34. 0, // tp_as_sequence
  35. 0, // tp_as_mapping
  36. 0, // tp_hash
  37. 0, // tp_call
  38. 0, // tp_str
  39. 0, // tp_getattro
  40. 0, // tp_setattro
  41. 0, // tp_as_buffer
  42. Py_TPFLAGS_DEFAULT, // tp_flags
  43. 0 // tp_doc
  44. };
  45. //-----------------------------------------------------------------------------
  46. // variable type declarations
  47. //-----------------------------------------------------------------------------
  48. static udt_VariableType vt_Interval = {
  49. (InitializeProc) IntervalVar_Initialize,
  50. (FinalizeProc) IntervalVar_Finalize,
  51. (PreDefineProc) NULL,
  52. (PostDefineProc) NULL,
  53. (PreFetchProc) NULL,
  54. (IsNullProc) NULL,
  55. (SetValueProc) IntervalVar_SetValue,
  56. (GetValueProc) IntervalVar_GetValue,
  57. (GetBufferSizeProc) NULL,
  58. &g_IntervalVarType, // Python type
  59. SQLT_INTERVAL_DS, // Oracle type
  60. SQLCS_IMPLICIT, // charset form
  61. sizeof(OCIInterval*), // element length (default)
  62. 0, // is character data
  63. 0, // is variable length
  64. 1, // can be copied
  65. 1 // can be in array
  66. };
  67. //-----------------------------------------------------------------------------
  68. // IntervalVar_Initialize()
  69. // Initialize the variable.
  70. //-----------------------------------------------------------------------------
  71. static int IntervalVar_Initialize(
  72. udt_IntervalVar *var, // variable to initialize
  73. udt_Cursor *cursor) // cursor created by
  74. {
  75. sword status;
  76. ub4 i;
  77. // initialize the interval locators
  78. for (i = 0; i < var->allocatedElements; i++) {
  79. status = OCIDescriptorAlloc(var->environment->handle,
  80. (dvoid**) &var->data[i], OCI_DTYPE_INTERVAL_DS, 0, 0);
  81. if (Environment_CheckForError(var->environment, status,
  82. "IntervalVar_Initialize()") < 0)
  83. return -1;
  84. }
  85. return 0;
  86. }
  87. //-----------------------------------------------------------------------------
  88. // IntervalVar_Finalize()
  89. // Prepare for variable destruction.
  90. //-----------------------------------------------------------------------------
  91. static void IntervalVar_Finalize(
  92. udt_IntervalVar *var) // variable to free
  93. {
  94. ub4 i;
  95. for (i = 0; i < var->allocatedElements; i++) {
  96. if (var->data[i])
  97. OCIDescriptorFree(var->data[i], OCI_DTYPE_INTERVAL_DS);
  98. }
  99. }
  100. //-----------------------------------------------------------------------------
  101. // IntervalVar_SetValue()
  102. // Set the value of the variable.
  103. //-----------------------------------------------------------------------------
  104. static int IntervalVar_SetValue(
  105. udt_IntervalVar *var, // variable to set value for
  106. unsigned pos, // array position to set
  107. PyObject *value) // value to set
  108. {
  109. sb4 hours, minutes, seconds;
  110. PyDateTime_Delta *delta;
  111. sword status;
  112. if (!PyDelta_Check(value)) {
  113. PyErr_SetString(PyExc_TypeError, "expecting timedelta data");
  114. return -1;
  115. }
  116. delta = (PyDateTime_Delta*) value;
  117. hours = (sb4) delta->seconds / 3600;
  118. seconds = delta->seconds - hours * 3600;
  119. minutes = (sb4) seconds / 60;
  120. seconds -= minutes * 60;
  121. status = OCIIntervalSetDaySecond(var->environment->handle,
  122. var->environment->errorHandle, delta->days, hours, minutes,
  123. seconds, delta->microseconds*1000, var->data[pos]);
  124. if (Environment_CheckForError(var->environment, status,
  125. "IntervalVar_SetValue()") < 0)
  126. return -1;
  127. return 0;
  128. }
  129. //-----------------------------------------------------------------------------
  130. // IntervalVar_GetValue()
  131. // Returns the value stored at the given array position.
  132. //-----------------------------------------------------------------------------
  133. static PyObject *IntervalVar_GetValue(
  134. udt_IntervalVar *var, // variable to determine value for
  135. unsigned pos) // array position
  136. {
  137. return OracleIntervalToPythonDelta(var->environment, var->data[pos]);
  138. }