DateTimeVar.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. //-----------------------------------------------------------------------------
  2. // DateTimeVar.c
  3. // Defines the routines for handling date (time) variables.
  4. //-----------------------------------------------------------------------------
  5. //-----------------------------------------------------------------------------
  6. // DateTime type
  7. //-----------------------------------------------------------------------------
  8. typedef struct {
  9. Variable_HEAD
  10. OCIDate *data;
  11. } udt_DateTimeVar;
  12. //-----------------------------------------------------------------------------
  13. // Declaration of date/time variable functions.
  14. //-----------------------------------------------------------------------------
  15. static int DateTimeVar_SetValue(udt_DateTimeVar*, unsigned, PyObject*);
  16. static PyObject *DateTimeVar_GetValue(udt_DateTimeVar*, unsigned);
  17. //-----------------------------------------------------------------------------
  18. // Python type declarations
  19. //-----------------------------------------------------------------------------
  20. static PyTypeObject g_DateTimeVarType = {
  21. PyVarObject_HEAD_INIT(NULL, 0)
  22. "cx_Oracle.DATETIME", // tp_name
  23. sizeof(udt_DateTimeVar), // tp_basicsize
  24. 0, // tp_itemsize
  25. 0, // tp_dealloc
  26. 0, // tp_print
  27. 0, // tp_getattr
  28. 0, // tp_setattr
  29. 0, // tp_compare
  30. 0, // tp_repr
  31. 0, // tp_as_number
  32. 0, // tp_as_sequence
  33. 0, // tp_as_mapping
  34. 0, // tp_hash
  35. 0, // tp_call
  36. 0, // tp_str
  37. 0, // tp_getattro
  38. 0, // tp_setattro
  39. 0, // tp_as_buffer
  40. Py_TPFLAGS_DEFAULT, // tp_flags
  41. 0 // tp_doc
  42. };
  43. //-----------------------------------------------------------------------------
  44. // variable type declarations
  45. //-----------------------------------------------------------------------------
  46. static udt_VariableType vt_DateTime = {
  47. (InitializeProc) NULL,
  48. (FinalizeProc) NULL,
  49. (PreDefineProc) NULL,
  50. (PostDefineProc) NULL,
  51. (PreFetchProc) NULL,
  52. (IsNullProc) NULL,
  53. (SetValueProc) DateTimeVar_SetValue,
  54. (GetValueProc) DateTimeVar_GetValue,
  55. (GetBufferSizeProc) NULL,
  56. &g_DateTimeVarType, // Python type
  57. SQLT_ODT, // Oracle type
  58. SQLCS_IMPLICIT, // charset form
  59. sizeof(OCIDate), // element length (default)
  60. 0, // is character data
  61. 0, // is variable length
  62. 1, // can be copied
  63. 1 // can be in array
  64. };
  65. static udt_VariableType vt_Date = {
  66. (InitializeProc) NULL,
  67. (FinalizeProc) NULL,
  68. (PreDefineProc) NULL,
  69. (PostDefineProc) NULL,
  70. (PreFetchProc) NULL,
  71. (IsNullProc) NULL,
  72. (SetValueProc) DateTimeVar_SetValue,
  73. (GetValueProc) DateTimeVar_GetValue,
  74. (GetBufferSizeProc) NULL,
  75. &g_DateTimeVarType, // Python type
  76. SQLT_ODT, // Oracle type
  77. SQLCS_IMPLICIT, // charset form
  78. sizeof(OCIDate), // element length (default)
  79. 0, // is character data
  80. 0, // is variable length
  81. 1, // can be copied
  82. 1 // can be in array
  83. };
  84. //-----------------------------------------------------------------------------
  85. // DateTimeVar_SetValue()
  86. // Set the value of the variable.
  87. //-----------------------------------------------------------------------------
  88. static int DateTimeVar_SetValue(
  89. udt_DateTimeVar *var, // variable to set value for
  90. unsigned pos, // array position to set
  91. PyObject *value) // value to set
  92. {
  93. ub1 month, day, hour, minute, second;
  94. short year;
  95. if (PyDateTime_Check(value)) {
  96. year = (short) PyDateTime_GET_YEAR(value);
  97. month = PyDateTime_GET_MONTH(value);
  98. day = PyDateTime_GET_DAY(value);
  99. hour = PyDateTime_DATE_GET_HOUR(value);
  100. minute = PyDateTime_DATE_GET_MINUTE(value);
  101. second = PyDateTime_DATE_GET_SECOND(value);
  102. } else if (PyDate_Check(value)) {
  103. year = (short) PyDateTime_GET_YEAR(value);
  104. month = PyDateTime_GET_MONTH(value);
  105. day = PyDateTime_GET_DAY(value);
  106. hour = minute = second = 0;
  107. } else {
  108. PyErr_SetString(PyExc_TypeError, "expecting date data");
  109. return -1;
  110. }
  111. // store a copy of the value
  112. OCIDateSetDate(&var->data[pos], year, month, day);
  113. OCIDateSetTime(&var->data[pos], hour, minute, second);
  114. return 0;
  115. }
  116. //-----------------------------------------------------------------------------
  117. // DateTimeVar_GetValue()
  118. // Returns the value stored at the given array position.
  119. //-----------------------------------------------------------------------------
  120. static PyObject *DateTimeVar_GetValue(
  121. udt_DateTimeVar *var, // variable to determine value for
  122. unsigned pos) // array position
  123. {
  124. return OracleDateToPythonDate(var->type, &var->data[pos]);
  125. }