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