-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathvDateTime.c
More file actions
594 lines (520 loc) · 19.2 KB
/
vDateTime.c
File metadata and controls
594 lines (520 loc) · 19.2 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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
/******************************************************
file:
vDateTime.c
purpose:
python type define for DM data/time/timestamp variables in dmPython
interface:
{}
history:
Date Who RefDoc Memo
2015-6-9 wmm Created
*******************************************************/
#include "Error.h"
#include "py_Dameng.h"
#include "Buffer.h"
#include "var_pub.h"
#include <datetime.h>
//-----------------------------------------------------------------------------
// Declaration of date/time variable functions.
//-----------------------------------------------------------------------------
static int DateVar_SetValue(dm_DateVar*, unsigned, PyObject*);
static PyObject* DateVar_GetValue(dm_DateVar*, unsigned);
static int DateVar_BindObjectValue(dm_DateVar*, unsigned, dhobj, udint4);
static int TimeVar_SetValue(dm_TimeVar*, unsigned, PyObject*);
static PyObject* TimeVar_GetValue(dm_TimeVar*, unsigned);
static int TimeVar_BindObjectValue(dm_TimeVar*, unsigned, dhobj, udint4);
static int TimestampVar_SetValue(dm_TimestampVar*, unsigned, PyObject*);
static PyObject* TimestampVar_GetValue(dm_TimestampVar*, unsigned);
static int TimestampVar_BindObjectValue(dm_TimestampVar*, unsigned, dhobj, udint4);
static int TZVar_SetValue(dm_TZVar*, unsigned, PyObject*);
static PyObject* TZVar_GetValue(dm_TZVar*, unsigned);
static int TZVar_BindObjectValue(dm_TZVar*, unsigned, dhobj, udint4);
//-----------------------------------------------------------------------------
// Python type declarations
//-----------------------------------------------------------------------------
PyTypeObject g_DateType = {
PyVarObject_HEAD_INIT(NULL, 0)
"dmPython.DATE", // tp_name
sizeof(dm_DateVar), // 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_TimeType = {
PyVarObject_HEAD_INIT(NULL, 0)
"dmPython.TIME", // tp_name
sizeof(dm_TimeVar), // 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_TimestampType = {
PyVarObject_HEAD_INIT(NULL, 0)
"dmPython.TIMESTAMP", // tp_name
sizeof(dm_TimestampVar), // 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_TimeTZType = {
PyVarObject_HEAD_INIT(NULL, 0)
"dmPython.TIME_WITH_TIMEZONE", // tp_name
sizeof(dm_TZVar), // 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_TimestampTZType = {
PyVarObject_HEAD_INIT(NULL, 0)
"dmPython.TIMESTAMP_WITH_TIMEZONE", // tp_name
sizeof(dm_TZVar), // 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_Date = {
(InitializeProc) NULL,
(FinalizeProc) NULL,
(PreDefineProc) NULL,
(PreFetchProc) NULL,
(IsNullProc) NULL,
(SetValueProc) DateVar_SetValue,
(GetValueProc) DateVar_GetValue,
(GetBufferSizeProc) NULL,
(BindObjectValueProc)DateVar_BindObjectValue,
&g_DateType, // Python type
DSQL_C_DATE, // C type
sizeof(dpi_date_t), // element length (default)
0, // is character data
0, // is variable length
1, // can be copied
1 // can be in array
};
dm_VarType vt_Time = {
(InitializeProc) NULL,
(FinalizeProc) NULL,
(PreDefineProc) NULL,
(PreFetchProc) NULL,
(IsNullProc) NULL,
(SetValueProc) TimeVar_SetValue,
(GetValueProc) TimeVar_GetValue,
(GetBufferSizeProc) NULL,
(BindObjectValueProc)TimeVar_BindObjectValue,
&g_TimeType, // Python type
DSQL_C_TIME, // C type
sizeof(dpi_time_t), // element length (default)
0, // is character data
0, // is variable length
1, // can be copied
1 // can be in array
};
dm_VarType vt_Timestamp = {
(InitializeProc) NULL,
(FinalizeProc) NULL,
(PreDefineProc) NULL,
(PreFetchProc) NULL,
(IsNullProc) NULL,
(SetValueProc) TimestampVar_SetValue,
(GetValueProc) TimestampVar_GetValue,
(GetBufferSizeProc) NULL,
(BindObjectValueProc)TimestampVar_BindObjectValue,
&g_TimestampType, // Python type
DSQL_C_TIMESTAMP, // C type
sizeof(dpi_timestamp_t), // element length (default)
0, // is character data
0, // is variable length
1, // can be copied
1 // can be in array
};
dm_VarType vt_TimeTZ = {
(InitializeProc) NULL,
(FinalizeProc) NULL,
(PreDefineProc) NULL,
(PreFetchProc) NULL,
(IsNullProc) NULL,
(SetValueProc) TZVar_SetValue,
(GetValueProc) TZVar_GetValue,
(GetBufferSizeProc) NULL,
(BindObjectValueProc)TZVar_BindObjectValue,
&g_TimeTZType, // Python type
DSQL_C_NCHAR, // C type,以字符串形式处理
64, // element length (default),预留64个字节长度
0, // is character data
0, // is variable length
1, // can be copied
1 // can be in array
};
dm_VarType vt_TimestampTZ = {
(InitializeProc) NULL,
(FinalizeProc) NULL,
(PreDefineProc) NULL,
(PreFetchProc) NULL,
(IsNullProc) NULL,
(SetValueProc)TimestampVar_SetValue,
(GetValueProc)TimestampVar_GetValue,
(GetBufferSizeProc) NULL,
(BindObjectValueProc)TimestampVar_BindObjectValue,
&g_TimestampTZType, // Python type
DSQL_C_TIMESTAMP, // C type
sizeof(dpi_timestamp_t), // element length (default)
0, // is character data
0, // is variable length
1, // can be copied
1 // can be in array
};
void
DateVar_import()
{
PyDateTime_IMPORT;
}
static
int
DateVar_SetValue(
dm_DateVar* var, // variable to set value for
unsigned pos, // array position to set
PyObject* value // value to set
)
{
short year;
udbyte month;
udbyte day;
dpi_date_t* date;
if (!PyDate_Check(value))
{
PyErr_SetString(PyExc_TypeError, "expecting date data");
return -1;
}
year = (short) PyDateTime_GET_YEAR(value);
month = PyDateTime_GET_MONTH(value);
day = PyDateTime_GET_DAY(value);
date = &var->data[pos];
date->year = year;
date->month = month;
date->day = day;
var->indicator[pos] = sizeof(dpi_date_t);
var->actualLength[pos] = sizeof(dpi_date_t);
return 0;
}
static
int
TimeVar_SetValue(
dm_TimeVar* var, // variable to set value for
unsigned pos, // array position to set
PyObject* value // value to set
)
{
udbyte hour;
udbyte minute;
udbyte second;
dpi_time_t* time;
if (!PyTime_Check(value))
{
PyErr_SetString(PyExc_TypeError, "expecting time data");
return -1;
}
hour = PyDateTime_TIME_GET_HOUR(value);
minute = PyDateTime_TIME_GET_MINUTE(value);
second = PyDateTime_TIME_GET_SECOND(value);
time = &var->data[pos];
time->hour = hour;
time->minute = minute;
time->second = second;
var->indicator[pos] = sizeof(dpi_time_t);
var->actualLength[pos] = sizeof(dpi_time_t);
return 0;
}
static
int
TimestampVar_SetValue(
dm_TimestampVar* var, // variable to set value for
unsigned pos, // array position to set
PyObject* value // value to set
)
{
short year;
udbyte month;
udbyte day;
udbyte hour;
udbyte minute;
udbyte second;
udint4 usecond;
dpi_timestamp_t* ts;
if (!PyDateTime_Check(value))
{
PyErr_SetString(PyExc_TypeError, "expecting datetime data");
return -1;
}
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);
usecond = PyDateTime_DATE_GET_MICROSECOND(value);
ts = &var->data[pos];
ts->year = year;
ts->month = month;
ts->day = day;
ts->hour = hour;
ts->minute = minute;
ts->second = second;
ts->fraction = usecond * 1000;
var->indicator[pos] = sizeof(dpi_timestamp_t);
var->actualLength[pos] = sizeof(dpi_timestamp_t);
return 0;
}
static
PyObject*
DateVar_GetValue(
dm_DateVar* var, // variable to determine value for
unsigned pos // array position
)
{
int year;
int month;
int day;
year = var->data[pos].year;
month = var->data[pos].month;
day = var->data[pos].day;
if (year <= 0 || year > 9999)
{
PyErr_SetString(PyExc_ValueError, "year is out of range");
return NULL;
}
return PyDate_FromDate(year, month, day);
}
static
int
DateVar_BindObjectValue(
dm_DateVar* 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
PyObject*
TimeVar_GetValue(
dm_TimeVar* var, // variable to determine value for
unsigned pos // array position
)
{
udint2 hour;
udint2 minute;
udint2 second;
hour = var->data[pos].hour;
minute = var->data[pos].minute;
second = var->data[pos].second;
return PyTime_FromTime(hour, minute, second, 0);
}
static
int
TimeVar_BindObjectValue(
dm_TimeVar* 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
PyObject*
TimestampVar_GetValue(
dm_TimestampVar* var, // variable to determine value for
unsigned pos // array position
)
{
int year;
int month;
int day;
int hour;
int minute;
int second;
int usecond;
year = var->data[pos].year;
month = var->data[pos].month;
day = var->data[pos].day;
hour = var->data[pos].hour;
minute = var->data[pos].minute;
second = var->data[pos].second;
usecond = var->data[pos].fraction / 1000;
if (year <= 0 || year > 9999)
{
PyErr_SetString(PyExc_ValueError, "year is out of range");
return NULL;
}
return PyDateTime_FromDateAndTime(year, month, day, hour, minute, second, usecond);
}
static
int
TimestampVar_BindObjectValue(
dm_TimestampVar* 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
TZVar_SetValue(
dm_TZVar* var, // variable to set value for
unsigned pos, // array position to set
PyObject* value // value to set
)
{
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*
TZVar_GetValue(
dm_TZVar* var, // variable to determine value for
unsigned pos // array position
)
{
PyObject* stringObj;
char* data;
data = var->data + pos * var->bufferSize;
if (var->type == &vt_TimeTZ || var->type == &vt_TimestampTZ)
{
stringObj = dmString_FromEncodedString(data, var->actualLength[pos], var->environment->encoding);
if (!stringObj)
return NULL;
return stringObj;
}
PyErr_SetString(g_ErrorException, "expecting time with time zone or timestamp with time zone data");
return NULL;
}
static
int
TZVar_BindObjectValue(
dm_TZVar* 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;
}