This repository has been archived by the owner on May 1, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcliviipy.c
694 lines (603 loc) · 15.4 KB
/
cliviipy.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
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
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
/*
* $Id: cliviipy.c 2013/10/10 11:00:00 $
*
* This program is free software.
* You can distribute/modify this program under the terms of
* the Apache License, Version 2.0.
*/
#include "cliviipy.h"
#include <abstract.h>
#include <boolobject.h>
#include <floatobject.h>
#include <intobject.h>
#include <limits.h>
#include <longobject.h>
#include <methodobject.h>
#include <modsupport.h>
#include <object.h>
#include <parcel.h>
#include <pyconfig.h>
#include <pyerrors.h>
#include <pyport.h>
#include <stdio.h>
#include <string.h>
#include <stringobject.h>
#include <tupleobject.h>
#include <structmember.h>
typedef union {
PclWord SQLLen;
struct {
Pclchar Fractional;
Pclchar Width;
} DECLen;
} DecLenUnion;
PyObject *unpackInt(CliInfoType *info, CliDataType *data, int *datIdx) {
PY_LONG_LONG raw;
PyObject *result;
char c1;
short s1;
long l1;
switch (info->SQLLen) {
case 1:
memcpy(&c1, &data->Data[*datIdx], sizeof(c1));
raw = c1;
break;
case 2:
memcpy(&s1, &data->Data[*datIdx], sizeof(s1));
raw = s1;
break;
case 4:
memcpy(&l1, &data->Data[*datIdx], sizeof(l1));
raw = l1;
break;
case 8:
memcpy(&raw, &data->Data[*datIdx], sizeof(raw));
break;
default:
memcpy(&raw, &data->Data[*datIdx], info->SQLLen);
break;
}
if ((raw >= LONG_MIN) && (raw <= LONG_MAX)) {
result = PyInt_FromLong(Py_SAFE_DOWNCAST(raw, PY_LONG_LONG, long));
} else {
result = PyLong_FromLongLong(raw);
}
*datIdx += info->SQLLen;
return result;
}
PyObject *unpackDate(CliInfoType *info, CliDataType *data, int *datIdx) {
long raw;
PyObject *result;
memcpy(&raw, &data->Data[*datIdx], sizeof(raw));
raw = raw + 19000000;
result = PyInt_FromLong(raw);
*datIdx += info->SQLLen;
return result;
}
PyObject *unpackDouble(CliInfoType *info, CliDataType *data, int *datIdx) {
double raw;
PyObject *result;
memcpy(&raw, &data->Data[*datIdx], sizeof(raw));
result = PyFloat_FromDouble(raw);
*datIdx += info->SQLLen;
return result;
}
PyObject *unpackDecimal(CliInfoType *info, CliDataType *data, int *datIdx) {
int i, pos, sign;
PY_LONG_LONG raw;
PyObject *result;
DecLenUnion decLen;
char buf1[20], buf2[20];
char c1;
short s1;
long l1;
decLen.SQLLen = info->SQLLen;
if (decLen.DECLen.Width <= 2) {
memcpy(&c1, &data->Data[*datIdx], sizeof(c1));
raw = c1;
decLen.DECLen.Width = 1;
} else if (decLen.DECLen.Width <= 4) {
memcpy(&s1, &data->Data[*datIdx], sizeof(s1));
raw = s1;
decLen.DECLen.Width = 2;
} else if (decLen.DECLen.Width <= 9) {
memcpy(&l1, &data->Data[*datIdx], sizeof(l1));
raw = l1;
decLen.DECLen.Width = 4;
} else if (decLen.DECLen.Width <= 18) {
memcpy(&raw, &data->Data[*datIdx], sizeof(raw));
decLen.DECLen.Width = 8;
} else {
decLen.DECLen.Width = 16;
}
if (decLen.DECLen.Width == 16) {
Py_INCREF(Py_None);
result = Py_None;
} else {
sprintf(buf1, "%I64d", raw);
if (buf1[0] == '-') {
strcpy(buf2, buf1);
strcpy(buf1, &buf2[1]);
sign = 1;
} else {
sign = 0;
}
if (decLen.DECLen.Fractional > 0) {
if (strlen(buf1) <= (unsigned char) decLen.DECLen.Fractional) {
pos = decLen.DECLen.Fractional - strlen(buf1) + 2;
strcpy(buf2, buf1);
strcpy(&buf1[pos], buf2);
for (i = 0; i < pos; i++) {
buf1[i] = '0';
}
buf1[1] = '.';
} else {
pos = strlen(buf1) - decLen.DECLen.Fractional;
strcpy(buf2, &buf1[pos]);
strcpy(&buf1[pos + 1], buf2);
buf1[pos] = '.';
}
}
if (sign == 1) {
strcpy(buf2, buf1);
strcpy(&buf1[1], buf2);
buf1[0] = '-';
}
result = PyString_FromString(buf1);
}
*datIdx += decLen.DECLen.Width;
return result;
}
PyObject *unpackFixStr(CliInfoType *info, CliDataType *data, int *datIdx,
char *charset) {
PyObject *rawstr, *result;
rawstr = PyString_FromStringAndSize(&data->Data[*datIdx], info->SQLLen);
if (!rawstr) {
return NULL;
}
result = PyString_AsDecodedObject(rawstr, charset, "");
*datIdx += info->SQLLen;
return result;
}
PyObject *unpackVarStr(CliInfoType *info, CliDataType *data, int *datIdx,
char *charset) {
PclWord chars;
PyObject *rawstr, *result;
memcpy(&chars, &data->Data[*datIdx], sizeof(chars));
*datIdx += 2;
rawstr = PyString_FromStringAndSize(&data->Data[*datIdx], chars);
if (!rawstr) {
return NULL;
}
result = PyString_AsDecodedObject(rawstr, charset, "");
*datIdx += chars;
return result;
}
PyObject* unpackField(int fldIdx, CliInfoType *info, CliDataType *data,
int *datIdx, char *charset) {
PclByte nullByte;
int chrIdx, bitIdx;
PyObject *result = NULL;
switch (info->SQLType) {
case 452:
case 453:
case 468:
case 469:
case 692:
case 693:
// Fixed String Type
result = unpackFixStr(info, data, datIdx, charset);
break;
case 496:
case 497:
// Fixed Integer Type
result = unpackInt(info, data, datIdx);
break;
case 500:
case 501:
// Fixed Short Type
result = unpackInt(info, data, datIdx);
break;
case 600:
case 601:
// Fixed Long Type
result = unpackInt(info, data, datIdx);
break;
case 756:
case 757:
// Fixed Byte Type
result = unpackInt(info, data, datIdx);
break;
case 480:
case 481:
// Fixed Float Types
result = unpackDouble(info, data, datIdx);
break;
case 484:
case 485:
// Fixed Decimal Types
result = unpackDecimal(info, data, datIdx);
break;
case 448:
case 449:
case 456:
case 457:
case 464:
case 465:
case 472:
case 473:
case 688:
case 689:
case 696:
case 697:
// Varchar Types
result = unpackVarStr(info, data, datIdx, charset);
break;
case 752:
case 753:
// Date Types
result = unpackDate(info, data, datIdx);
break;
}
if (result != NULL) {
chrIdx = fldIdx / 8;
bitIdx = fldIdx % 8;
memcpy(&nullByte, &data->Data[chrIdx], sizeof(nullByte));
if (nullByte & (128 >> bitIdx)) {
Py_DECREF(result);
Py_INCREF(Py_None);
result = Py_None;
}
}
return result;
}
PyObject* unpackColumns(CliMetaType meta) {
int i, idx;
PclInt16 int16;
PyObject *value, *result;
result = PyTuple_New(meta.prep.ColumnCount);
if (result == NULL) {
return NULL;
}
idx = 0;
for (i = 0; i < meta.prep.ColumnCount; i++) {
idx += 2;
idx += 2;
memcpy(&int16, &meta.cols[idx], sizeof(int16));
idx += 2;
idx += int16;
memcpy(&int16, &meta.cols[idx], sizeof(int16));
idx += 2;
idx += int16;
memcpy(&int16, &meta.cols[idx], sizeof(int16));
idx += 2;
value = PyString_FromStringAndSize(&meta.cols[idx], int16);
if (value == NULL) {
Py_DECREF(result);
return NULL;
}
PyTuple_SET_ITEM(result, i, value);
idx += int16;
}
return result;
}
static void CliConn_Dealloc(CliConn* self) {
bool status = true;
status = cli_cln(&self->cli);
if (!status) {
PyErr_SetString(CliFail, self->cli.errMsg);
}
Py_TYPE(self)->tp_free((PyObject*) self);
}
static int CliConn_Init(CliConn* self, PyObject* args, PyObject* kwds) {
bool status = true;
status = cli_ini(&self->cli);
if (!status) {
PyErr_SetString(CliFail, self->cli.errMsg);
return -1;
}
return 0;
}
static PyObject* CliConn_Enter(CliConn* self, PyObject* args) {
bool status = true;
if (self->cli.logged) {
PyErr_SetString(CliFail, "Already logged on.");
return NULL;
}
status = cli_con(&self->cli);
if (!status) {
PyErr_SetString(CliFail, self->cli.errMsg);
return NULL;
}
Py_INCREF(self);
return (PyObject*) self;
}
static PyObject* CliConn_Exit(CliConn* self, PyObject* args) {
bool status = true;
if (!self->cli.logged) {
PyErr_SetString(CliFail, "Already logged off.");
return NULL;
}
status = cli_dsc(&self->cli);
if (!status) {
PyErr_SetString(CliFail, self->cli.errMsg);
return NULL;
}
Py_INCREF(Py_False);
return Py_False;
}
static PyObject* CliConn_Update(CliConn* self, PyObject* args) {
long count = 0;
long status = -1;
char* query_text;
PyObject* result;
if (!PyArg_ParseTuple(args, "s", &query_text)) {
return NULL;
}
if (!self->cli.logged) {
PyErr_SetString(CliFail, "Already logged off.");
return NULL;
}
count = cli_irq(&self->cli, query_text);
if (count < 0) {
PyErr_SetString(CliFail, self->cli.errMsg);
return NULL;
}
status = cli_erq(&self->cli);
if (status < 0) {
PyErr_SetString(CliFail, self->cli.errMsg);
return NULL;
}
result = Py_BuildValue("l", count);
return result;
}
static PyObject* CliConn_Query(CliConn* self, PyObject* args) {
long count = 0;
char* query_text;
PyObject *createArgs, *result;
if (!PyArg_ParseTuple(args, "s", &query_text)) {
return NULL;
}
if (!self->cli.logged) {
PyErr_SetString(CliFail, "Already logged off.");
return NULL;
}
count = cli_irq(&self->cli, query_text);
if (count < 0) {
PyErr_SetString(CliFail, self->cli.errMsg);
return NULL;
}
createArgs = PyTuple_New(1);
if (createArgs == NULL) {
return NULL;
}
Py_INCREF(self);
PyTuple_SET_ITEM(createArgs, 0, (PyObject* ) self);
result = PyObject_Call((PyObject*) &CliIterType, createArgs, NULL);
Py_DECREF(createArgs);
return result;
}
static PyMethodDef CliConn_Methods[] = { { "__enter__",
(PyCFunction) CliConn_Enter, METH_NOARGS }, { "__exit__",
(PyCFunction) CliConn_Exit, METH_VARARGS }, { "update",
(PyCFunction) CliConn_Update, METH_VARARGS }, { "query",
(PyCFunction) CliConn_Query, METH_VARARGS }, { 0, 0, 0, 0 } };
PyTypeObject CliConnType = { PyObject_HEAD_INIT(NULL) 0, /* ob_size */
"clivii.CliConn", /* tp_name */
sizeof(CliConn), /* tp_basicsize */
0, /* tp_itemsize */
(destructor) CliConn_Dealloc, /* 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 */
"Teradata Connection Object", /* tp_doc */
0, /* tp_traverse */
0, /* tp_clear */
0, /* tp_richcompare */
0, /* tp_weaklistoffset */
0, /* tp_iter */
0, /* tp_iternext */
CliConn_Methods, /* tp_methods */
0, /* tp_members */
0, /* tp_getset */
0, /* tp_base */
0, /* tp_dict */
0, /* tp_descr_get */
0, /* tp_descr_set */
0, /* tp_dictoffset */
(initproc) CliConn_Init, /* tp_init */
};
static void CliIter_Dealloc(CliIter* self) {
Py_CLEAR(self->conn);
Py_CLEAR(self->cols);
Py_TYPE(self)->tp_free((PyObject*) self);
}
static int CliIter_Init(CliIter* self, PyObject* args, PyObject* kwds) {
CliConn* conn;
if (!PyArg_ParseTuple(args, "O!", &CliConnType, &conn)) {
return -1;
}
Py_INCREF(conn);
self->conn = conn;
self->cols = NULL;
return 0;
}
static PyObject* CliIter_Enter(CliIter* self, PyObject* args) {
long status = -1;
CliMetaType meta;
if (!self->conn->cli.logged) {
PyErr_SetString(CliFail, "Already logged off.");
return NULL;
}
status = cli_fpp(&self->conn->cli, &meta);
if (status < 0) {
PyErr_SetString(CliFail, self->conn->cli.errMsg);
return NULL;
}
self->cols = unpackColumns(meta);
if (self->cols == NULL) {
PyErr_SetString(CliFail, "Unpack columns failed.");
return NULL;
}
status = cli_fip(&self->conn->cli, &self->head);
if (status < 0) {
PyErr_SetString(CliFail, self->conn->cli.errMsg);
return NULL;
}
Py_INCREF(self);
return (PyObject*) self;
}
static PyObject* CliIter_Next(CliIter* self) {
int i, idx;
long status = -1;
PyObject *value, *result;
if (!self->conn->cli.logged) {
PyErr_SetString(CliFail, "Already logged off.");
return NULL;
}
status = cli_frp(&self->conn->cli, &self->data);
if (status < 0) {
PyErr_SetString(CliFail, self->conn->cli.errMsg);
return NULL;
} else if (status == 0) {
return NULL;
}
result = PyTuple_New(self->head.FieldCount);
if (result == NULL) {
return NULL;
}
idx = (self->head.FieldCount + 7) / 8;
for (i = 0; i < self->head.FieldCount; i++) {
value = unpackField(i, &self->head.InfoVar[i], &self->data, &idx,
self->conn->cli.charset);
if (value == NULL) {
Py_DECREF(result);
return NULL;
}
PyTuple_SET_ITEM(result, i, value);
}
return result;
}
static PyObject* CliIter_Exit(CliIter* self, PyObject* args) {
long status = -1;
if (!self->conn->cli.logged) {
PyErr_SetString(CliFail, "Already logged off.");
return NULL;
}
status = cli_erq(&self->conn->cli);
if (status < 0) {
PyErr_SetString(CliFail, self->conn->cli.errMsg);
return NULL;
}
Py_INCREF(Py_False);
return Py_False;
}
static PyMethodDef CliIter_Methods[] = { { "__enter__",
(PyCFunction) CliIter_Enter, METH_NOARGS }, { "__exit__",
(PyCFunction) CliIter_Exit, METH_VARARGS }, { 0, 0, 0, 0 } };
static PyMemberDef CliIter_Members[] = { { "cols", T_OBJECT_EX, offsetof(
CliIter, cols), READONLY, "column names" }, { NULL } };
PyTypeObject CliIterType = { PyObject_HEAD_INIT(NULL) 0, /* ob_size */
"clivii.CliIter", /* tp_name */
sizeof(CliIter), /* tp_basicsize */
0, /* tp_itemsize */
(destructor) CliIter_Dealloc, /* 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 */
"Teradata Resultset Object", /* tp_doc */
0, /* tp_traverse */
0, /* tp_clear */
0, /* tp_richcompare */
0, /* tp_weaklistoffset */
PyObject_SelfIter, /* tp_iter */
(iternextfunc) CliIter_Next, /* tp_iternext */
CliIter_Methods, /* tp_methods */
CliIter_Members, /* tp_members */
0, /* tp_getset */
0, /* tp_base */
0, /* tp_dict */
0, /* tp_descr_get */
0, /* tp_descr_set */
0, /* tp_dictoffset */
(initproc) CliIter_Init, /* tp_init */
};
static PyObject* CliPy_Conn(PyObject* self, PyObject* args) {
char *logstr;
PyObject *createArgs, *result;
if (!PyArg_ParseTuple(args, "s", &logstr)) {
return NULL;
}
createArgs = PyTuple_New(0);
if (createArgs == NULL) {
return NULL;
}
result = PyObject_Call((PyObject*) &CliConnType, createArgs, NULL);
Py_DECREF(createArgs);
strcpy(((CliConn *) result)->cli.logstr, logstr);
sprintf(((CliConn *) result)->cli.charset, "%-30s", "UTF8");
return result;
}
static PyMethodDef clivii_Methods[] = { { "connect", (PyCFunction) CliPy_Conn,
METH_VARARGS }, { 0, 0, 0, 0 } };
#ifndef PyMODINIT_FUNC
#define PyMODINIT_FUNC void
#endif
PyMODINIT_FUNC initclivii(void) {
PyObject* cliModule;
CliConnType.tp_new = PyType_GenericNew;
if (PyType_Ready(&CliConnType) < 0) {
return;
}
CliIterType.tp_new = PyType_GenericNew;
if (PyType_Ready(&CliIterType) < 0) {
return;
}
cliModule = Py_InitModule3("clivii", clivii_Methods,
"Python Teradata Access Module.");
if (cliModule == NULL) {
return;
}
if (PyModule_AddStringConstant(cliModule, "version", COPCLIVersion) < 0) {
return;
}
Py_INCREF(&CliConnType);
if (PyModule_AddObject(cliModule, "CliConn", (PyObject *) &CliConnType)
< 0) {
return;
}
Py_INCREF(&CliIterType);
if (PyModule_AddObject(cliModule, "CliIter", (PyObject *) &CliIterType)
< 0) {
return;
}
CliFail = PyErr_NewException("clivii.CliFail", NULL, NULL);
Py_INCREF(CliFail);
if (PyModule_AddObject(cliModule, "CliFail", CliFail) < 0) {
return;
}
}