forked from oracle/python-cx_Oracle
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEnvironment.c
More file actions
404 lines (357 loc) · 14.8 KB
/
Environment.c
File metadata and controls
404 lines (357 loc) · 14.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
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
//-----------------------------------------------------------------------------
// Copyright 2016, 2017, Oracle and/or its affiliates. All rights reserved.
//
// Portions Copyright 2007-2015, Anthony Tuininga. All rights reserved.
//
// Portions Copyright 2001-2007, Computronix (Canada) Ltd., Edmonton, Alberta,
// Canada. All rights reserved.
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
// Environment.c
// Environment handling.
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
// structure for the Python type
//-----------------------------------------------------------------------------
typedef struct {
PyObject_HEAD
OCIEnv *handle;
OCIError *errorHandle;
int maxBytesPerCharacter;
int nmaxBytesPerCharacter;
char *encoding;
char *nencoding;
ub2 charsetId;
ub2 ncharsetId;
PyObject *cloneEnv;
udt_Buffer numberToStringFormatBuffer;
udt_Buffer numberFromStringFormatBuffer;
udt_Buffer nlsNumericCharactersBuffer;
} udt_Environment;
//-----------------------------------------------------------------------------
// forward declarations
//-----------------------------------------------------------------------------
static void Environment_Free(udt_Environment*);
static int Environment_CheckForError(udt_Environment*, sword, const char*);
//-----------------------------------------------------------------------------
// declaration of Python type
//-----------------------------------------------------------------------------
static PyTypeObject g_EnvironmentType = {
PyVarObject_HEAD_INIT(NULL, 0)
"OracleEnvironment", // tp_name
sizeof(udt_Environment), // tp_basicsize
0, // tp_itemsize
(destructor) Environment_Free, // 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
};
#include "Error.c"
//-----------------------------------------------------------------------------
// Environment_New()
// Create a new environment object.
//-----------------------------------------------------------------------------
static udt_Environment *Environment_New(
OCIEnv *handle) // handle to use
{
udt_Environment *env;
udt_Error *errorObj;
sword status;
// create a new object for the Oracle environment
env = (udt_Environment*) g_EnvironmentType.tp_alloc(&g_EnvironmentType, 0);
if (!env)
return NULL;
env->handle = handle;
env->maxBytesPerCharacter = 1;
env->nmaxBytesPerCharacter = 4;
cxBuffer_Init(&env->numberToStringFormatBuffer);
cxBuffer_Init(&env->numberFromStringFormatBuffer);
cxBuffer_Init(&env->nlsNumericCharactersBuffer);
// create the error handle
status = OCIHandleAlloc(handle, (dvoid**) &env->errorHandle,
OCI_HTYPE_ERROR, 0, 0);
if (status != OCI_SUCCESS) {
errorObj = Error_InternalNew(env,
"Environment_New(): create error handle", OCI_HTYPE_ENV,
handle);
if (!errorObj) {
Py_DECREF(env);
return NULL;
}
PyErr_SetObject(g_DatabaseErrorException, (PyObject*) errorObj);
Py_DECREF(env);
return NULL;
}
return env;
}
//-----------------------------------------------------------------------------
// Environment_GetCharacterSetName()
// Retrieve and store the IANA character set name for the attribute.
//-----------------------------------------------------------------------------
static int Environment_GetCharacterSetName(
udt_Environment *self, // environment object
ub2 attribute, // attribute to fetch
const char *overrideValue, // override value, if specified
char **result, // place to store result
ub2 *charsetId) // character set ID (OUT)
{
char charsetName[OCI_NLS_MAXBUFSZ], ianaCharsetName[OCI_NLS_MAXBUFSZ];
sword status;
// get character set id
status = OCIAttrGet(self->handle, OCI_HTYPE_ENV, charsetId, NULL,
attribute, self->errorHandle);
if (Environment_CheckForError(self, status,
"Environment_GetCharacterSetName(): get charset id") < 0)
return -1;
// if override value specified, use it
if (overrideValue) {
*result = PyMem_Malloc(strlen(overrideValue) + 1);
if (!*result)
return -1;
strcpy(*result, overrideValue);
return 0;
}
// get character set name
status = OCINlsCharSetIdToName(self->handle, (text*) charsetName,
OCI_NLS_MAXBUFSZ, *charsetId);
if (Environment_CheckForError(self, status,
"Environment_GetCharacterSetName(): get Oracle charset name") < 0)
return -1;
// get IANA character set name
status = OCINlsNameMap(self->handle, (oratext*) ianaCharsetName,
OCI_NLS_MAXBUFSZ, (oratext*) charsetName, OCI_NLS_CS_ORA_TO_IANA);
if (Environment_CheckForError(self, status,
"Environment_GetCharacterSetName(): translate NLS charset") < 0)
return -1;
// store results
*result = PyMem_Malloc(strlen(ianaCharsetName) + 1);
if (!*result)
return -1;
strcpy(*result, ianaCharsetName);
return 0;
}
//-----------------------------------------------------------------------------
// Environment_SetBuffer()
// Set the buffer in the environment from the specified string.
//-----------------------------------------------------------------------------
static int Environment_SetBuffer(
udt_Buffer *buf, // buffer to set
const char *value, // ASCII value to use
const char *encoding) // encoding to use
{
PyObject *obj;
obj = cxString_FromAscii(value);
if (!obj)
return -1;
if (cxBuffer_FromObject(buf, obj, encoding) < 0) {
Py_DECREF(obj);
return -1;
}
Py_CLEAR(obj);
return 0;
}
//-----------------------------------------------------------------------------
// Environment_LookupCharSet()
// Look up an Oracle character set given the name. This can be either the
// Oracle character set name or the IANA encoding name. A pointer to an
// environment handle is passed and an environment created if needed in
// order to perform the lookup.
//-----------------------------------------------------------------------------
static int Environment_LookupCharSet(
const char *name, // IANA or Oracle character set name
OCIEnv **envHandle, // environment handle (IN/OUT)
ub2 *charsetId) // Oracle character set id (OUT)
{
char oraCharsetName[OCI_NLS_MAXBUFSZ];
sword status;
// if IANA name is null, use the charset 0 which tells Oracle to make use
// of the NLS_LANG and NLS_NCHAR environment variables
if (!name) {
*charsetId = 0;
return 0;
}
// create environment, if needed
if (!*envHandle) {
status = OCIEnvCreate(envHandle, OCI_DEFAULT, NULL, NULL, NULL, NULL,
0, NULL);
if (status != OCI_SUCCESS) {
PyErr_SetString(g_InterfaceErrorException,
"Unable to acquire Oracle environment handle");
return -1;
}
}
// check for the Oracle character set name first
// if that fails, lookup using the IANA character set name
*charsetId = OCINlsCharSetNameToId(*envHandle, (oratext*) name);
if (!*charsetId) {
status = OCINlsNameMap(*envHandle, (oratext*) oraCharsetName,
sizeof(oraCharsetName), (oratext*) name,
OCI_NLS_CS_IANA_TO_ORA);
if (status == OCI_ERROR) {
PyErr_SetString(g_InterfaceErrorException,
"Invalid character set name");
return -1;
}
*charsetId = OCINlsCharSetNameToId(*envHandle,
(oratext*) oraCharsetName);
}
return 0;
}
//-----------------------------------------------------------------------------
// Environment_NewFromScratch()
// Create a new environment object from scratch.
//-----------------------------------------------------------------------------
static udt_Environment *Environment_NewFromScratch(
int threaded, // use threaded mode?
int events, // use events mode?
char *encoding, // override value for encoding
char *nencoding) // override value for nencoding
{
ub2 charsetId, ncharsetId;
udt_Environment *env;
OCIEnv *handle;
sword status;
ub4 mode;
// turn threading mode on, if desired
mode = OCI_OBJECT;
if (threaded)
mode |= OCI_THREADED;
if (events)
mode |= OCI_EVENTS;
// perform lookup of character sets to use
// keep track of any environment that needs to be created in order to
// perform this lookup
handle = NULL;
status = Environment_LookupCharSet(encoding, &handle, &charsetId);
if (status == 0)
status = Environment_LookupCharSet(nencoding, &handle, &ncharsetId);
if (handle) {
OCIHandleFree(handle, OCI_HTYPE_ENV);
handle = NULL;
}
if (status < 0)
return NULL;
// create the new environment handle
status = OCIEnvNlsCreate(&handle, mode, NULL, NULL, NULL, NULL, 0, NULL,
charsetId, ncharsetId);
if (!handle ||
(status != OCI_SUCCESS && status != OCI_SUCCESS_WITH_INFO)) {
PyErr_SetString(g_InterfaceErrorException,
"Unable to acquire Oracle environment handle");
return NULL;
}
// create the environment object
env = Environment_New(handle);
if (!env) {
OCIHandleFree(handle, OCI_HTYPE_ENV);
return NULL;
}
// acquire max bytes per character
status = OCINlsNumericInfoGet(env->handle, env->errorHandle,
&env->maxBytesPerCharacter, OCI_NLS_CHARSET_MAXBYTESZ);
if (Environment_CheckForError(env, status,
"Environment_New(): get max bytes per character") < 0) {
Py_DECREF(env);
return NULL;
}
// determine encodings to use for Unicode values
if (Environment_GetCharacterSetName(env, OCI_ATTR_ENV_CHARSET_ID,
encoding, &env->encoding, &env->charsetId) < 0)
return NULL;
if (Environment_GetCharacterSetName(env, OCI_ATTR_ENV_NCHARSET_ID,
nencoding, &env->nencoding, &env->ncharsetId) < 0)
return NULL;
// max bytes per character for NCHAR can be assigned only if it matches the
// character set used for CHAR data; OCI does not provide a way of
// determining it otherwise
if (env->ncharsetId == env->charsetId)
env->nmaxBytesPerCharacter = env->maxBytesPerCharacter;
// fill buffers for number formats
if (Environment_SetBuffer(&env->numberToStringFormatBuffer, "TM9",
env->encoding) < 0)
return NULL;
if (Environment_SetBuffer(&env->numberFromStringFormatBuffer,
"999999999999999999999999999999999999999999999999999999999999999",
env->encoding) < 0)
return NULL;
if (Environment_SetBuffer(&env->nlsNumericCharactersBuffer,
"NLS_NUMERIC_CHARACTERS='.,'", env->encoding) < 0)
return NULL;
return env;
}
//-----------------------------------------------------------------------------
// Environment_Clone()
// Clone an existing environment which is used when acquiring a connection
// from a session pool, for example.
//-----------------------------------------------------------------------------
static udt_Environment *Environment_Clone(
udt_Environment *cloneEnv) // environment to clone
{
udt_Environment *env;
env = Environment_New(cloneEnv->handle);
if (!env)
return NULL;
env->maxBytesPerCharacter = cloneEnv->maxBytesPerCharacter;
Py_INCREF(cloneEnv);
env->cloneEnv = (PyObject*) cloneEnv;
env->encoding = cloneEnv->encoding;
env->nencoding = cloneEnv->nencoding;
env->charsetId = cloneEnv->charsetId;
env->ncharsetId = cloneEnv->ncharsetId;
cxBuffer_Copy(&env->numberToStringFormatBuffer,
&cloneEnv->numberToStringFormatBuffer);
cxBuffer_Copy(&env->numberFromStringFormatBuffer,
&cloneEnv->numberFromStringFormatBuffer);
cxBuffer_Copy(&env->nlsNumericCharactersBuffer,
&cloneEnv->nlsNumericCharactersBuffer);
return env;
}
//-----------------------------------------------------------------------------
// Environment_Free()
// Deallocate the environment. Note that destroying the environment handle
// will automatically destroy any child handles that were created.
//-----------------------------------------------------------------------------
static void Environment_Free(
udt_Environment *self) // environment object
{
if (self->errorHandle)
OCIHandleFree(self->errorHandle, OCI_HTYPE_ERROR);
if (self->handle && !self->cloneEnv)
OCIHandleFree(self->handle, OCI_HTYPE_ENV);
if (!self->cloneEnv) {
if (self->encoding)
PyMem_Free(self->encoding);
if (self->nencoding)
PyMem_Free(self->nencoding);
}
cxBuffer_Clear(&self->numberToStringFormatBuffer);
cxBuffer_Clear(&self->numberFromStringFormatBuffer);
cxBuffer_Clear(&self->nlsNumericCharactersBuffer);
Py_CLEAR(self->cloneEnv);
Py_TYPE(self)->tp_free((PyObject*) self);
}
//-----------------------------------------------------------------------------
// Environment_CheckForError()
// Check for an error in the last call and if an error has occurred, raise a
// Python exception.
//-----------------------------------------------------------------------------
static int Environment_CheckForError(
udt_Environment *environment, // environment to raise error in
sword status, // status of last call
const char *context) // context
{
return Error_Check(environment, status, context, environment->errorHandle);
}