-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpy_Dameng.c
More file actions
864 lines (718 loc) · 25.5 KB
/
py_Dameng.c
File metadata and controls
864 lines (718 loc) · 25.5 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
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
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
/** cx_Dameng.cpp : 定义 DLL 的初始化例程。 **/
//
#include "py_Dameng.h"
#include <datetime.h>
#include "Error.h"
#include "DPI.h"
#include "var_pub.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "trc.h"
#ifdef WIN32
#include <Windows.h>
#endif
#if PY_MAJOR_VERSION >= 3
#include <moduleobject.h>
#endif
// define macro for adding type objects
#define ADD_TYPE_OBJECT(name, type) \
Py_INCREF(type); \
if (PyModule_AddObject(module, name, (PyObject*) type) < 0) \
return NULL;
// define macros for making types ready
#define MAKE_TYPE_READY(type) \
if (PyType_Ready(type) < 0) \
return NULL;
#define MAKE_dmVar_TYPE_READY(type) \
(type)->tp_base = &g_BaseVarType; \
MAKE_TYPE_READY(type)
// define base exception
#if PY_MAJOR_VERSION >= 3
#define BASE_EXCEPTION NULL
#define TYPE_ERROR "expecting string or bytes object"
#else
#define BASE_EXCEPTION PyExc_StandardError
#define TYPE_ERROR "expecting string, unicode or buffer object"
#endif
#ifdef WIN32
#define DIR_CHAR '\\'
#else
#define DIR_CHAR '/'
#endif
#define MAX_PATH_LEN 256
#define MAX_LINE_LENGTH 100
typedef sdbyte pathname_t[MAX_PATH_LEN + 1];
PyObject *g_WarningException = NULL;
PyObject *g_ErrorException = NULL;
PyObject *g_InterfaceErrorException = NULL;
PyObject *g_DatabaseErrorException = NULL;
PyObject *g_DataErrorException = NULL;
PyObject *g_OperationalErrorException = NULL;
PyObject *g_IntegrityErrorException = NULL;
PyObject *g_InternalErrorException = NULL;
PyObject *g_ProgrammingErrorException = NULL;
PyObject *g_NotSupportedErrorException = NULL;
PyTypeObject* g_decimal_type = NULL;
//-----------------------------------------------------------------------------
// SetException()
// Create an exception and set it in the provided dictionary.
//-----------------------------------------------------------------------------
static
sdint2
SetException(
PyObject* module, // module object
PyObject** exception, // exception to create
char* name, // name of the exception
PyObject* baseException
) // exception to base exception on
{
char buffer[100];
aq_sprintf(buffer, 100, "dmPython.%s", name);
*exception = PyErr_NewException(buffer, baseException, NULL);
if (!*exception)
return -1;
return PyModule_AddObject(module, name, *exception);
}
/* Parse arg tuple that can contain an optional float-or-None value;
format needs to be "|O:name".
Returns non-zero on success (parallels PyArg_ParseTuple).
*/
static void
error_time_t_overflow(void)
{
PyErr_SetString(PyExc_OverflowError,
"timestamp out of range for platform time_t");
}
static
int
parse_time_t_args(
PyObject* args,
char* format,
time_t* pwhen
)
{
PyObject* ot = NULL;
if (!PyArg_ParseTuple(args, format, &ot))
return -1;
if (ot == NULL || ot == Py_None)
{
*pwhen = time(NULL);
return 0;
}
if (PyFloat_Check(ot))
{
double d;
double intpart;
double err;
d = PyFloat_AsDouble(ot);
(void)modf(d, &intpart);
*pwhen = (time_t)intpart;
err = intpart - (double)(*pwhen);
if (err <= -1.0 || err >= 1.0)
{
error_time_t_overflow();
return -1;
}
return 0;
}
{
#if defined(HAVE_LONG_LONG) && SIZEOF_TIME_T == SIZEOF_LONG_LONG
PY_LONG_LONG val;
val = PyLong_AsLongLong(ot);
#else
long val;
assert(sizeof(time_t) <= sizeof(long));
val = PyLong_AsLong(ot);
#endif
if (val == -1 && PyErr_Occurred())
{
if (PyErr_ExceptionMatches(PyExc_OverflowError))
error_time_t_overflow();
return -1;
}
*pwhen = (time_t)val;
}
return 0;
}
static
int
pylocaltime(
time_t* timep,
struct tm* result)
{
struct tm *local;
assert (timep != NULL);
local = localtime(timep);
if (local == NULL) {
/* unconvertible time */
#ifdef EINVAL
if (errno == 0)
errno = EINVAL;
#endif
PyErr_SetFromErrno(PyExc_OSError);
return -1;
}
*result = *local;
return 0;
}
//-----------------------------------------------------------------------------
//DateFromTicks(ticks)
// This function constructs an object holding a date value
// from the given ticks value (number of seconds since the
// epoch; see the documentation of the standard Python time
// module for details).
//-----------------------------------------------------------------------------
static
PyObject*
dmDateFromTicks(
PyObject* module,
PyObject* args
)
{
return PyDate_FromTimestamp(args);
}
//-----------------------------------------------------------------------------
//TimeFromTicks(ticks)
// This function constructs an object holding a time value
// from the given ticks value (number of seconds since the
// epoch; see the documentation of the standard Python time
// module for details).
//-----------------------------------------------------------------------------
static
PyObject*
dmTimeFromTicks(
PyObject* module,
PyObject* args
)
{
time_t when;
struct tm fields;
if (parse_time_t_args(args, "|O:localtime", &when) < 0)
return NULL;
if (pylocaltime(&when, &fields) < 0)
return NULL;
return PyTime_FromTime(fields.tm_hour, fields.tm_min, fields.tm_sec, 0);
}
//-----------------------------------------------------------------------------
//TimestampFromTicks(ticks)
// This function constructs an object holding a time stamp value
// from the given ticks value (number of seconds since the
// epoch; see the documentation of the standard Python time
// module for details).
//-----------------------------------------------------------------------------
static
PyObject*
dmTimestampFromTicks(
PyObject* module,
PyObject* args
)
{
time_t when;
struct tm fields;
if (parse_time_t_args(args, "|O:localtime", &when) < 0)
return NULL;
if (pylocaltime(&when, &fields) < 0)
return NULL;
return PyDateTime_FromDateAndTime(fields.tm_year + 1900, fields.tm_mon + 1, fields.tm_mday, fields.tm_hour, fields.tm_min, fields.tm_sec, 0);
}
static
PyObject*
dmStringFromBytes(
PyObject* module,
PyObject* args
)
{
PyObject* bsObject = NULL;
Py_ssize_t len;
if (!PyArg_ParseTuple(args, "O", &bsObject))
return NULL;
if (!PyBytes_Check(bsObject))
{
PyErr_SetString(PyExc_TypeError,
"expect a Bytes Object");
return NULL;
}
len = PyBytes_GET_SIZE(bsObject);
return exLobVar_BytesToString(bsObject, len);
}
//-----------------------------------------------------------------------------
// Declaration of methods supported by this module
//-----------------------------------------------------------------------------
static PyMethodDef g_ModuleMethods[] = {
{ "DateFromTicks", (PyCFunction)dmDateFromTicks, METH_VARARGS, "This function constructs an object holding a date value \n"
"from the given ticks value (number of seconds since the \n"
"epoch; see the documentation of the standard Python time \n"
"module for details"},
{ "TimeFromTicks", (PyCFunction)dmTimeFromTicks, METH_VARARGS, "This function constructs an object holding a time value \n"
"from the given ticks value (number of seconds since the \n"
"epoch; see the documentation of the standard Python time \n"
"module for details"},
{ "TimestampFromTicks", (PyCFunction)dmTimestampFromTicks, METH_VARARGS, "This function constructs an object holding a time stamp value \n"
"from the given ticks value (number of seconds since the \n"
"epoch; see the documentation of the standard Python time \n"
"module for details"},
{ "StringFromBytes", (PyCFunction)dmStringFromBytes, METH_VARARGS, "This function constructs an object holding a string value \n"
"from the given bytes value"},
{ NULL }
};
#if PY_MAJOR_VERSION >= 3
//-----------------------------------------------------------------------------
// Declaration of module definition for Python 3.x.
//-----------------------------------------------------------------------------
static struct PyModuleDef g_ModuleDef = {
PyModuleDef_HEAD_INIT,
"dmPython",
NULL,
-1,
g_ModuleMethods, // methods
NULL, // m_reload
NULL, // traverse
NULL, // clear
NULL // free
};
#endif
static
sdint2
import_types()
{
PyObject *pdt = NULL, *decimalmod = NULL;
pdt = PyImport_ImportModule("datetime");
if (!pdt)
return -1;
PyDateTime_IMPORT;
dmVar_Import();
Cursor_Data_init();
decimalmod = PyImport_ImportModule("decimal");
if (!decimalmod)
{
PyErr_SetString(PyExc_RuntimeError, "Unable to import decimal");
return -1;
}
g_decimal_type = (PyTypeObject*)PyObject_GetAttrString(decimalmod, "Decimal");
Py_DECREF(decimalmod);
if (g_decimal_type == 0)
{
PyErr_SetString(PyExc_RuntimeError, "Unable to import decimal.Decimal.");
return -1;
}
return 0;
}
/** 增加相关操作常量 **/
static
void
module_add_oper_const(
PyObject* module
)
{
/** shutdown相关 **/
PyModule_AddStringConstant(module, "SHUTDOWN_DEFAULT", SHUTDOWN_DEFAULT);
PyModule_AddStringConstant(module, "SHUTDOWN_ABORT", SHUTDOWN_ABORT);
PyModule_AddStringConstant(module, "SHUTDOWN_IMMEDIATE", SHUTDOWN_IMMEDIATE);
PyModule_AddStringConstant(module, "SHUTDOWN_TRANSACTIONAL", SHUTDOWN_TRANSACTIONAL);
PyModule_AddStringConstant(module, "SHUTDOWN_NORMAL", SHUTDOWN_NORMAL);
/** debug相关 **/
PyModule_AddIntConstant(module, "DEBUG_CLOSE", DEBUG_CLOSE);
PyModule_AddIntConstant(module, "DEBUG_OPEN", DEBUG_OPEN);
PyModule_AddIntConstant(module, "DEBUG_SWITCH", DEBUG_SWITCH);
PyModule_AddIntConstant(module, "DEBUG_SIMPLE", DEBUG_SIMPLE);
/** 隔离级相关 **/
PyModule_AddIntConstant(module, "ISO_LEVEL_READ_DEFAULT", ISO_LEVEL_READ_COMMITTED);
PyModule_AddIntConstant(module, "ISO_LEVEL_READ_UNCOMMITTED", ISO_LEVEL_READ_UNCOMMITTED);
PyModule_AddIntConstant(module, "ISO_LEVEL_READ_COMMITTED", ISO_LEVEL_READ_COMMITTED);
PyModule_AddIntConstant(module, "ISO_LEVEL_REPEATABLE_READ", ISO_LEVEL_REPEATABLE_READ);
PyModule_AddIntConstant(module, "ISO_LEVEL_SERIALIZABLE", ISO_LEVEL_SERIALIZABLE);
/** access_mode相关 **/
PyModule_AddIntConstant(module, "DSQL_MODE_READ_ONLY", DSQL_MODE_READ_ONLY);
PyModule_AddIntConstant(module, "DSQL_MODE_READ_WRITE", DSQL_MODE_READ_WRITE);
/** autocommit相关 **/
PyModule_AddIntConstant(module, "DSQL_AUTOCOMMIT_ON", DSQL_AUTOCOMMIT_ON);
PyModule_AddIntConstant(module, "DSQL_AUTOCOMMIT_OFF", DSQL_AUTOCOMMIT_OFF);
/** 编码方式相关 **/
PyModule_AddIntConstant(module, "PG_UTF8", PG_UTF8);
PyModule_AddIntConstant(module, "PG_GBK", PG_GBK);
PyModule_AddIntConstant(module, "PG_BIG5", PG_BIG5);
PyModule_AddIntConstant(module, "PG_ISO_8859_9", PG_ISO_8859_9);
PyModule_AddIntConstant(module, "PG_EUC_JP", PG_EUC_JP);
PyModule_AddIntConstant(module, "PG_EUC_KR", PG_EUC_KR);
PyModule_AddIntConstant(module, "PG_KOI8R", PG_KOI8R);
PyModule_AddIntConstant(module, "PG_ISO_8859_1", PG_ISO_8859_1);
PyModule_AddIntConstant(module, "PG_SQL_ASCII", PG_SQL_ASCII);
PyModule_AddIntConstant(module, "PG_GB18030", PG_GB18030);
PyModule_AddIntConstant(module, "PG_ISO_8859_11", PG_ISO_8859_11);
/** 语言类型 **/
PyModule_AddIntConstant(module, "LANGUAGE_CN", LANGUAGE_CN);
PyModule_AddIntConstant(module, "LANGUAGE_EN", LANGUAGE_EN);
#ifdef LANGUAGE_CNT_HK
PyModule_AddIntConstant(module, "LANGUAGE_CNT_HK", LANGUAGE_CNT_HK); //繁体中文
#endif
/** TRUE/FALSE **/
PyModule_AddIntConstant(module, "DSQL_TRUE", DSQL_TRUE);
PyModule_AddIntConstant(module, "DSQL_FALSE", DSQL_FALSE);
/** RWSEPERATE相关 **/
PyModule_AddIntConstant(module, "DSQL_RWSEPARATE_ON", DSQL_RWSEPARATE_ON);
PyModule_AddIntConstant(module, "DSQL_RWSEPARATE_OFF", DSQL_RWSEPARATE_OFF);
/** trx_state相关 **/
PyModule_AddIntConstant(module, "DSQL_TRX_ACTIVE", DSQL_TRX_ACTIVE);
PyModule_AddIntConstant(module, "DSQL_TRX_COMPLETE", DSQL_TRX_COMPLETE);
/** mpp登陆方式相关 **/
PyModule_AddIntConstant(module, "DSQL_MPP_LOGIN_GLOBAL", DSQL_MPP_LOGIN_GLOBAL);
PyModule_AddIntConstant(module, "DSQL_MPP_LOGIN_LOCAL", DSQL_MPP_LOGIN_LOCAL);
/** 回滚后游标状态相关 **/
PyModule_AddIntConstant(module, "DSQL_CB_CLOSE", DSQL_CB_CLOSE);
PyModule_AddIntConstant(module, "DSQL_CB_PRESERVE", DSQL_CB_PRESERVE);
/* CURSOR类型 */
PyModule_AddIntConstant(module, "TupleCursor", TUPLE_CURSOR);
PyModule_AddIntConstant(module, "DictCursor", DICT_CURSOR);
}
/**增加变量相关**/
static
PyObject*
module_add_var_obj(
PyObject* module
)
{
MAKE_dmVar_TYPE_READY(&g_IntervalVarType);
ADD_TYPE_OBJECT("INTERVAL", &g_IntervalVarType);
MAKE_dmVar_TYPE_READY(&g_YMIntervalVarType);
ADD_TYPE_OBJECT("YEAR_MONTH_INTERVAL", &g_YMIntervalVarType);
MAKE_dmVar_TYPE_READY(&g_BLobVarType);
ADD_TYPE_OBJECT("BLOB", &g_BLobVarType);
MAKE_dmVar_TYPE_READY(&g_CLobVarType);
ADD_TYPE_OBJECT("CLOB", &g_CLobVarType);
MAKE_TYPE_READY(&g_exLobVarType);
ADD_TYPE_OBJECT("LOB", &g_exLobVarType);
MAKE_dmVar_TYPE_READY(&g_BFileVarType);
ADD_TYPE_OBJECT("BFILE", &g_BFileVarType);
MAKE_TYPE_READY(&g_exBFileVarType);
ADD_TYPE_OBJECT("exBFILE", &g_exBFileVarType);
MAKE_dmVar_TYPE_READY(&g_LongBinaryVarType);
ADD_TYPE_OBJECT("LONG_BINARY", &g_LongBinaryVarType);
MAKE_dmVar_TYPE_READY(&g_LongStringVarType);
ADD_TYPE_OBJECT("LONG_STRING", &g_LongStringVarType);
MAKE_dmVar_TYPE_READY(&g_DateType);
ADD_TYPE_OBJECT("DATE", &g_DateType);
MAKE_dmVar_TYPE_READY(&g_TimeType);
ADD_TYPE_OBJECT("TIME", &g_TimeType);
MAKE_dmVar_TYPE_READY(&g_TimestampType);
ADD_TYPE_OBJECT("TIMESTAMP", &g_TimestampType);
MAKE_dmVar_TYPE_READY(&g_CursorVarType);
ADD_TYPE_OBJECT("CURSOR", &g_CursorVarType);
MAKE_dmVar_TYPE_READY(&g_StringType);
ADD_TYPE_OBJECT("STRING", &g_StringType);
MAKE_dmVar_TYPE_READY(&g_FixedCharType);
ADD_TYPE_OBJECT("FIXED_STRING", &g_FixedCharType);
MAKE_dmVar_TYPE_READY(&g_BinaryType);
ADD_TYPE_OBJECT("BINARY", &g_BinaryType);
MAKE_dmVar_TYPE_READY(&g_FixedBinaryType);
ADD_TYPE_OBJECT("FIXED_BINARY", &g_FixedBinaryType);
#if PY_MAJOR_VERSION < 3
MAKE_dmVar_TYPE_READY(&g_UnicodeStrType);
ADD_TYPE_OBJECT("UNICODE_STRING", &g_UnicodeStrType);
MAKE_dmVar_TYPE_READY(&g_FixedUnicodeCharType);
ADD_TYPE_OBJECT("FIXED_UNICODE_STRING", &g_FixedUnicodeCharType);
#endif
MAKE_TYPE_READY(&g_ExternalObjectVarType);
MAKE_TYPE_READY(&g_ObjectAttributeType);
MAKE_TYPE_READY(&g_ObjectTypeType);
ADD_TYPE_OBJECT("objectvar", &g_ExternalObjectVarType);
MAKE_dmVar_TYPE_READY(&g_ObjectVarType);
ADD_TYPE_OBJECT("OBJECTVAR", &g_ObjectVarType);
MAKE_dmVar_TYPE_READY(&g_NumberType);
ADD_TYPE_OBJECT("NUMBER", &g_NumberType);
MAKE_dmVar_TYPE_READY(&g_DoubleType);
ADD_TYPE_OBJECT("DOUBLE", &g_DoubleType);
MAKE_dmVar_TYPE_READY(&g_FloatType);
ADD_TYPE_OBJECT("REAL", &g_FloatType);
MAKE_dmVar_TYPE_READY(&g_BooleanType);
ADD_TYPE_OBJECT("BOOLEAN", &g_BooleanType);
MAKE_dmVar_TYPE_READY(&g_NumberStrType);
ADD_TYPE_OBJECT("DECIMAL", &g_NumberStrType);
MAKE_dmVar_TYPE_READY(&g_TimeTZType);
ADD_TYPE_OBJECT("TIME_WITH_TIMEZONE", &g_TimeTZType);
MAKE_dmVar_TYPE_READY(&g_TimestampTZType);
ADD_TYPE_OBJECT("TIMESTAMP_WITH_TIMEZONE", &g_TimestampTZType);
MAKE_dmVar_TYPE_READY(&g_BigintType);
ADD_TYPE_OBJECT("BIGINT", &g_BigintType);
MAKE_dmVar_TYPE_READY(&g_RowIdType);
ADD_TYPE_OBJECT("ROWID", &g_RowIdType);
return NULL;
}
/*********************************************
Purpose:
获取dm_svc.conf的路径,不存在返回空
**********************************************/
udbyte *
dmPython_get_svc_path(udbyte* real_path)
{
udint4 path_len;
pathname_t path;
udbyte* svc_path = getenv("DM_SVC_PATH");
//判断环境变量DM_SVC_PATH下是否存在dm_svc.conf
if (svc_path != NULL && strlen(svc_path) != 0 && strlen(svc_path)< MAX_PATH_LEN + 1)
{
path_len = strlen(svc_path);
if (svc_path[path_len - 1] == DIR_CHAR)
aq_sprintf(real_path, 513, "%s%s", svc_path, "dm_svc.conf");
else
aq_sprintf(real_path, 513, "%s%c%s", svc_path, DIR_CHAR, "dm_svc.conf");
}
//如果不存在,则判断系统目录下是否存在dm_svc.conf
if (*real_path == END ||
access(real_path, 0) == -1)
{
#ifdef WIN32
GetSystemDirectory(path, MAX_PATH_LEN + 1);
#else
strcpy(path,"/etc");
#endif
aq_sprintf(real_path, 513, "%s%c%s", path, DIR_CHAR, "dm_svc.conf");
}
//文件不存在返回NULL
if (access(real_path, 0) == -1)
return NULL;
return real_path;
}
/*********************************************
Purpose:
从dm_svc.conf读取dmpython_trace的值
**********************************************/
static
sdint2
dmPython_init_trace_mod()
{
udbyte path[MAX_PATH_LEN * 2 + 1] = { END };
udbyte* path_ptr;
FILE* file;
udbyte line[MAX_LINE_LENGTH];
udbyte name[MAX_LINE_LENGTH];
udbyte value[MAX_LINE_LENGTH];
udbyte* cleanname;
udbyte* cleanvalue;
//获取dm_svc.conf的路径
path_ptr = dmPython_get_svc_path(path);
if (path_ptr == NULL)
{
dmpython_trace_mod = DMPYTHON_TRACE_OFF;
return 0;
}
//打开文件,无法打开就将不打开dmPython_trace
file = fopen(path, "r");
if (file == NULL )
{
dmpython_trace_mod = DMPYTHON_TRACE_OFF;
return 0;
}
//读取DMPYTHON_TRACE的值
while (fgets(line, sizeof(line), file))
{
if (sscanf(line, "%[^=]=(%[^)]", name, value) == 2)
{
cleanname = strtok(name, " ");
cleanvalue = strtok(value, " ");
if (strcmp(cleanname, "DMPYTHON_TRACE") == 0)
{
if (strcmp(cleanvalue, "1") == 0)
{
dmpython_trace_mod = DMPYTHON_TRACE_ON;
fclose(file);
return 0;
}
}
}
}
dmpython_trace_mod = DMPYTHON_TRACE_OFF;
fclose(file);
return 0;
}
//-----------------------------------------------------------------------------
// Module_Initialize()
// Initialization routine for the module.
//-----------------------------------------------------------------------------
static
PyObject*
Module_Initialize(void)
{
PyObject *module;
#ifdef WITH_THREAD
PyEval_InitThreads();
#endif
// initialize module and retrieve the dictionary
#if PY_MAJOR_VERSION >= 3
module = PyModule_Create(&g_ModuleDef);
#else
module = Py_InitModule("dmPython", g_ModuleMethods);
#endif
if (!module)
return NULL;
if (import_types() < 0)
return NULL;
MAKE_TYPE_READY(&g_ConnectionType);
MAKE_TYPE_READY(&g_EnvironmentType);
MAKE_TYPE_READY(&g_ErrorType);
MAKE_TYPE_READY(&g_CursorType);
MAKE_TYPE_READY(&RowType);
// set up the types that are available
ADD_TYPE_OBJECT("Connection", &g_ConnectionType);
ADD_TYPE_OBJECT("Environment", &g_EnvironmentType);
ADD_TYPE_OBJECT("Cursor", &g_CursorType);
ADD_TYPE_OBJECT("DmError", &g_ErrorType);
// the name "connect" is required by the DB API
ADD_TYPE_OBJECT("connect", &g_ConnectionType);
ADD_TYPE_OBJECT("Connect", &g_ConnectionType);
// constant from api 2.0
PyModule_AddStringConstant(module, "apilevel", "2.0");
PyModule_AddIntConstant(module, "threadsafety", 1);
PyModule_AddStringConstant(module, "paramstyle", "qmark");
// add version and build time for easier support
if (PyModule_AddStringConstant(module, "version",
BUILD_VERSION_STRING) < 0)
return NULL;
if (PyModule_AddStringConstant(module, "buildtime",
__DATE__ " " __TIME__) < 0)
return NULL;
/** 增加常量 **/
module_add_oper_const(module);
/** 增加变量相关 **/
module_add_var_obj(module);
/** 初始化对象类型 **/
/*
// 和g_StringType等全局变量类型重名,先去掉
#if PY_MAJOR_VERSION < 3
ADD_TYPE_OBJECT("STRING", &PyString_Type);
#else
ADD_TYPE_OBJECT("STRING", &PyUnicode_Type);
#endif
ADD_TYPE_OBJECT("BINARY", &py_Binary_Type);
ADD_TYPE_OBJECT("Binary", &py_Binary_Type)
ADD_TYPE_OBJECT("NUMBER", &PyFloat_Type);
ADD_TYPE_OBJECT("ROWID", &PyLong_Type);
*/
Py_INCREF((PyObject*)PyDateTimeAPI->DateTimeType);
PyModule_AddObject(module, "DATETIME", (PyObject*)PyDateTimeAPI->DateTimeType);
Py_INCREF((PyObject*)PyDateTimeAPI->DateTimeType);
PyModule_AddObject(module, "Timestamp", (PyObject*)PyDateTimeAPI->DateTimeType);
Py_INCREF((PyObject*)PyDateTimeAPI->DateType);
PyModule_AddObject(module, "Date", (PyObject*)PyDateTimeAPI->DateType);
Py_INCREF((PyObject*)PyDateTimeAPI->TimeType);
PyModule_AddObject(module, "Time", (PyObject*)PyDateTimeAPI->TimeType);
// create exception object and add it to the dictionary
if (SetException(module, &g_WarningException,
"Warning", BASE_EXCEPTION) < 0)
return NULL;
if (SetException(module, &g_ErrorException,
"Error", BASE_EXCEPTION) < 0)
return NULL;
if (SetException(module, &g_InterfaceErrorException,
"InterfaceError", g_ErrorException) < 0)
return NULL;
if (SetException(module, &g_DatabaseErrorException,
"DatabaseError", g_ErrorException) < 0)
return NULL;
if (SetException(module, &g_DataErrorException,
"DataError", g_DatabaseErrorException) < 0)
return NULL;
if (SetException(module, &g_OperationalErrorException,
"OperationalError", g_DatabaseErrorException) < 0)
return NULL;
if (SetException(module, &g_IntegrityErrorException,
"IntegrityError", g_DatabaseErrorException) < 0)
return NULL;
if (SetException(module, &g_InternalErrorException,
"InternalError", g_DatabaseErrorException) < 0)
return NULL;
if (SetException(module, &g_ProgrammingErrorException,
"ProgrammingError", g_DatabaseErrorException) < 0)
return NULL;
if (SetException(module, &g_NotSupportedErrorException,
"NotSupportedError", g_DatabaseErrorException) < 0)
return NULL;
dmPython_init_trace_mod();
return module;
}
//-----------------------------------------------------------------------------
// Start routine for the module.
//-----------------------------------------------------------------------------
#if PY_MAJOR_VERSION >= 3
PyMODINIT_FUNC PyInit_dmPython(void)
{
return Module_Initialize();
}
#else
PyMODINIT_FUNC initdmPython(void)
{
Module_Initialize();
}
#endif
unsigned int /*dst的数据长度*/
aq_sprintf_inner(
char* dst, /*IN:目标缓冲区*/
int dst_len, /*IN:目标长度*/
char* fmt, /*IN:格式串*/
... /*IN:格式串中的参数*/
)
{
va_list argptr;
unsigned int len;
size_t count;
count = (size_t)dst_len;
va_start(argptr, fmt);
#ifdef _WIN32
//写入字符大于等于size时不加末尾0,大于size时返回-1
len = _vsnprintf(dst, count, fmt, argptr);
#else
//写入字符大于等于size时返回源串长度,否则返回写入长度,不含末尾0
len = vsnprintf(dst, count, fmt, argptr);
#endif
va_end(argptr);
return len;
}
int
DmIntNumber_AsInt(
PyObject* nobj,
char* pname
)
{
long value;
char buffer[200];
int flag = 0;
#if PY_MAJOR_VERSION < 3
if (PyInt_Check(nobj))
flag = 1;
#else
if (PyLong_Check(nobj))
flag = 1;
#endif
if (nobj == NULL || !flag)
{
if (pname != NULL)
{
aq_sprintf(buffer, 200, "%s : expecting an Integer value.\n", pname);
PyErr_SetString(PyExc_TypeError, buffer);
}
else
{
PyErr_SetString(PyExc_TypeError,
"expecting an Integer value.");
}
return -1;
}
value = PyLong_AsUnsignedLong(nobj);
if (PyErr_Occurred())
return -1;
if (value > INT_MAX || value < INT_MIN)
{
if (pname != NULL)
{
aq_sprintf(buffer, 200, "%s : value overflow.\n", pname);
PyErr_SetString(PyExc_TypeError, buffer);
}
else
{
PyErr_SetString(PyExc_TypeError,
"value overflow.");
}
return -1;
}
return value;
}