-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathvInterval.c
More file actions
357 lines (303 loc) · 11.8 KB
/
vInterval.c
File metadata and controls
357 lines (303 loc) · 11.8 KB
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
/******************************************************
file:
vInterval.h
purpose:
python type define for DM interval variables in dmPython
interface:
{}
history:
Date Who RefDoc Memo
2015-6-8 shenning Created
*******************************************************/
#include "py_Dameng.h"
#include "var_pub.h"
#include <datetime.h>
#include "Error.h"
#include "Buffer.h"
//-----------------------------------------------------------------------------
// Declaration of interval variable functions.
//-----------------------------------------------------------------------------
static int IntervalVar_SetValue(dm_IntervalVar*, unsigned, PyObject*);
static PyObject* IntervalVar_GetValue(dm_IntervalVar*, unsigned);
static int IntervalVar_BindObjectValue(dm_IntervalVar*, unsigned, dhobj, udint4);
static int YMIntervalVar_SetValue(dm_YMIntervalVar*, unsigned, PyObject*);
static PyObject* YMIntervalVar_GetValue(dm_YMIntervalVar*, unsigned);
static int YMIntervalVar_BindObjectValue(dm_YMIntervalVar*, unsigned, dhobj, udint4);
//-----------------------------------------------------------------------------
// Python type declarations
//-----------------------------------------------------------------------------
PyTypeObject g_IntervalVarType = {
PyVarObject_HEAD_INIT(NULL, 0)
"dmPython.INTERVAL", // tp_name
sizeof(dm_IntervalVar), // 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
};
PyTypeObject g_YMIntervalVarType = {
PyVarObject_HEAD_INIT(NULL, 0)
"dmPython.YEAR_MONTH_INTERVAL", // tp_name
sizeof(dm_YMIntervalVar), // 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
//-----------------------------------------------------------------------------
dm_VarType vt_Interval = {
(InitializeProc) NULL,
(FinalizeProc) NULL,
(PreDefineProc) NULL,
(PreFetchProc) NULL,
(IsNullProc) NULL,
(SetValueProc) IntervalVar_SetValue,
(GetValueProc) IntervalVar_GetValue,
(GetBufferSizeProc) NULL,
(BindObjectValueProc)IntervalVar_BindObjectValue,
&g_IntervalVarType, // Python type
DSQL_C_INTERVAL_DAY_TO_SECOND, // CType
sizeof(dpi_interval_t), // element length (default)
0, // is character data
0, // is variable length
1, // can be copied
1 // can be in array
};
dm_VarType vt_YMInterval = {
(InitializeProc) NULL,
(FinalizeProc) NULL,
(PreDefineProc) NULL,
(PreFetchProc) NULL,
(IsNullProc) NULL,
(SetValueProc) YMIntervalVar_SetValue,
(GetValueProc) YMIntervalVar_GetValue,
(GetBufferSizeProc) NULL,
(BindObjectValueProc)YMIntervalVar_BindObjectValue,
&g_YMIntervalVarType, // Python type
DSQL_C_NCHAR, // CType,对应字符串形式
64, // element length (default)
0, // is character data
0, // is variable length
1, // can be copied
1 // can be in array
};
void
IntervalVar_import()
{
PyDateTime_IMPORT;
}
//-----------------------------------------------------------------------------
// IntervalVar_SetValue()
// Set the value of the variable.
//-----------------------------------------------------------------------------
static
int
IntervalVar_SetValue(
dm_IntervalVar* var, // variable to set value for
unsigned pos, // array position to set
PyObject* value // value to set
)
{
int days;
int hours;
int minutes;
int seconds;
int microseconds;
int fraction; /** 纳秒 **/
sdint8 total_seconds;
PyDateTime_Delta* delta;
dpi_interval_t* ds;
if (!PyDelta_Check(value))
{
PyErr_SetString(PyExc_TypeError, "expecting timedelta data");
return -1;
}
delta = (PyDateTime_Delta*) value;
days = delta->days;
hours = delta->seconds / 3600;
minutes = (delta->seconds - hours * 3600) / 60;
seconds = delta->seconds - hours * 3600 - minutes * 60;
fraction = delta->microseconds * 1000;
ds = &var->data[pos];
ds->interval_type = DSQL_IS_DAY_TO_SECOND;
ds->interval_sign = days >= 0 ? 0 : 1;
//delta的日有正负值,时,分,秒,微秒都只有正值,而interval_sign表示的是整个intval是正值还是负值
//因此delta的日为负值时不能表示整个intval是负值,需要进行换算
//例如days = -1, hours = 23表示的是-1小时,而不是-1天23小时
//delta传进来时是已经换算成各个单位能表示的最大值了,day如果为负,整个的结果最终必为负的
if (days < 0)
{
total_seconds = delta->days * 24 * 3600 + delta->seconds;
if (total_seconds < 0 && delta->microseconds > 0)
{
//例如,最终算出来是-1s, +30micros,实际结果应该是0s, -999970 micros
total_seconds++;
microseconds = 1000000 - delta->microseconds;
}
else
{
microseconds = delta->microseconds;
}
seconds = total_seconds % 60;
total_seconds -= seconds;
minutes = (total_seconds / 60) % 60;
total_seconds -= minutes * 60;
hours = (total_seconds / 3600) % 24;
total_seconds -= hours * 3600;
days = total_seconds / (3600 * 24);
fraction = microseconds * 1000;
}
ds->intval.day_second.day = (udint4)abs(days);
ds->intval.day_second.hour = (udint4)abs(hours);
ds->intval.day_second.minute = (udint4)abs(minutes);
ds->intval.day_second.second = (udint4)abs(seconds);
ds->intval.day_second.fraction = (udint4)abs(fraction);
var->indicator[pos] = sizeof(dpi_interval_t);
var->actualLength[pos] = sizeof(dpi_interval_t);
return 0;
}
//-----------------------------------------------------------------------------
// IntervalVar_GetValue()
// Returns the value stored at the given array position.
//-----------------------------------------------------------------------------
static
PyObject*
IntervalVar_GetValue(
dm_IntervalVar* var, // variable to determine value for
unsigned pos // array position
)
{
int days;
int seconds;
int microseconds;
dpi_interval_t* ds;
sdint8 total_seconds;
ds = &var->data[pos];
days = ds->intval.day_second.day;
seconds = ds->intval.day_second.hour * 3600 +
ds->intval.day_second.minute * 60 +
ds->intval.day_second.second;
microseconds= ds->intval.day_second.fraction / 1000;
//interval_sign=1表示的是整个interval都为负数,而转换为delta时,只有days可以是负数,其他位用正数
if (ds->interval_sign == 1)
{
//intv上的day,hour,minute,seconds都是正值,sign用来区分正负
total_seconds = days * 24 * 60 * 60 + seconds; //用正值表示负的总秒数
//microseconds转成正值
microseconds = 1000000 - microseconds; //前面算出来的微秒是正值,但sign=1时表示的是负多少微秒,转成正值
total_seconds++;
//seconds转成正值
seconds = (24 * 60 * 60) - total_seconds % (24 * 60 * 60);
days = total_seconds / (24 * 60 * 60) + 1;
days = days * (-1);
}
return PyDelta_FromDSU(days, seconds, microseconds);
}
static
int
IntervalVar_BindObjectValue(
dm_IntervalVar* var,
unsigned pos,
dhobj hobj,
udint4 val_nth
)
{
DPIRETURN rt = DSQL_SUCCESS;
rt = dpi_set_obj_val(hobj, val_nth, var->type->cType, (dpointer)&var->data[pos], var->indicator[pos]);
if (Environment_CheckForError(var->environment, hobj, DSQL_HANDLE_OBJECT, rt,
"vCursor_BindObjectValue():dpi_set_obj_val") < 0)
{
return -1;
}
return 0;
}
static
int
YMIntervalVar_SetValue(
dm_YMIntervalVar* var,
unsigned pos,
PyObject* value
)
{
dm_Buffer buffer;
// populate the buffer and confirm the maximum size is not exceeded
if (dmBuffer_FromObject(&buffer, value, var->environment->encoding) < 0)
return -1;
if (buffer.size)
{
memcpy(var->data + var->bufferSize * pos, buffer.ptr, buffer.size);
}
// keep a copy of the string
var->indicator[pos] = buffer.size;
var->actualLength[pos] = buffer.size;
dmBuffer_Clear(&buffer);
return 0;
}
static
PyObject*
YMIntervalVar_GetValue(
dm_YMIntervalVar* var,
unsigned pos
)
{
PyObject* stringObj;
char* data;
data = var->data + pos * var->bufferSize;
if (var->type == &vt_YMInterval)
{
stringObj = dmString_FromEncodedString(data, var->actualLength[pos], var->environment->encoding);
if (!stringObj)
return NULL;
return stringObj;
}
PyErr_SetString(g_ErrorException, "expecting year-month interval data");
return NULL;
}
static
int
YMIntervalVar_BindObjectValue(
dm_YMIntervalVar* var,
unsigned pos,
dhobj hobj,
udint4 val_nth
)
{
DPIRETURN rt = DSQL_SUCCESS;
rt = dpi_set_obj_val(hobj, val_nth, var->type->cType, (dpointer)((sdbyte*)var->data + var->bufferSize * pos), var->indicator[pos]);
if (Environment_CheckForError(var->environment, hobj, DSQL_HANDLE_OBJECT, rt,
"vCursor_BindObjectValue():dpi_set_obj_val") < 0)
{
return -1;
}
return 0;
}