forked from oracle/python-cx_Oracle
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcxoUtils.c
More file actions
319 lines (287 loc) · 12 KB
/
cxoUtils.c
File metadata and controls
319 lines (287 loc) · 12 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
//-----------------------------------------------------------------------------
// Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
// cxoUtils.c
// Utility functions used in cx_Oracle.
//-----------------------------------------------------------------------------
#include "cxoModule.h"
//-----------------------------------------------------------------------------
// cxoUtils_convertOciAttrToPythonValue()
// Convert the OCI attribute value to an equivalent Python value using the
// specified type.
//-----------------------------------------------------------------------------
PyObject *cxoUtils_convertOciAttrToPythonValue(unsigned attrType,
dpiDataBuffer *value, uint32_t valueLength, const char *encoding)
{
switch (attrType) {
case CXO_OCI_ATTR_TYPE_STRING:
if (!value->asString) {
Py_RETURN_NONE;
}
return PyUnicode_Decode(value->asString, valueLength, encoding,
NULL);
case CXO_OCI_ATTR_TYPE_BOOLEAN:
if (value->asBoolean) {
Py_RETURN_TRUE;
}
Py_RETURN_FALSE;
case CXO_OCI_ATTR_TYPE_UINT8:
return PyLong_FromUnsignedLong(value->asUint8);
case CXO_OCI_ATTR_TYPE_UINT16:
return PyLong_FromUnsignedLong(value->asUint16);
case CXO_OCI_ATTR_TYPE_UINT32:
return PyLong_FromUnsignedLong(value->asUint32);
case CXO_OCI_ATTR_TYPE_UINT64:
return PyLong_FromUnsignedLongLong(value->asUint64);
}
return cxoError_raiseFromString(cxoProgrammingErrorException,
"invalid attribute type specified");
}
//-----------------------------------------------------------------------------
// cxoUtils_convertPythonValueToOciAttr()
// Convert the Python value to an equivalent OCI attribute value using the
// specified type.
//-----------------------------------------------------------------------------
int cxoUtils_convertPythonValueToOciAttr(PyObject *value, unsigned attrType,
cxoBuffer *buffer, dpiDataBuffer *ociBuffer, void **ociValue,
uint32_t *ociValueLength, const char *encoding)
{
unsigned long tempValue;
switch (attrType) {
case CXO_OCI_ATTR_TYPE_STRING:
if (cxoBuffer_fromObject(buffer, value, encoding) < 0)
return -1;
*ociValue = (void*) buffer->ptr;
*ociValueLength = (uint32_t) buffer->size;
break;
case CXO_OCI_ATTR_TYPE_BOOLEAN:
ociBuffer->asBoolean = PyObject_IsTrue(value);
if (PyErr_Occurred())
return -1;
*ociValue = &ociBuffer->asBoolean;
*ociValueLength = sizeof(ociBuffer->asBoolean);
break;
case CXO_OCI_ATTR_TYPE_UINT8:
tempValue = PyLong_AsUnsignedLong(value);
if (PyErr_Occurred())
return -1;
if (tempValue > UINT8_MAX) {
PyErr_SetString(PyExc_OverflowError,
"Python int too large to convert to uint8_t");
return -1;
}
ociBuffer->asUint8 = (uint8_t) tempValue;
*ociValue = &ociBuffer->asUint8;
*ociValueLength = sizeof(ociBuffer->asUint8);
break;
case CXO_OCI_ATTR_TYPE_UINT16:
tempValue = PyLong_AsUnsignedLong(value);
if (PyErr_Occurred())
return -1;
if (tempValue > UINT16_MAX) {
PyErr_SetString(PyExc_OverflowError,
"Python int too large to convert to uint16_t");
return -1;
}
ociBuffer->asUint16 = (uint16_t) tempValue;
*ociValue = &ociBuffer->asUint16;
*ociValueLength = sizeof(ociBuffer->asUint16);
break;
case CXO_OCI_ATTR_TYPE_UINT32:
tempValue = PyLong_AsUnsignedLong(value);
if (PyErr_Occurred())
return -1;
if (tempValue > UINT32_MAX) {
PyErr_SetString(PyExc_OverflowError,
"Python int too large to convert to uint32_t");
return -1;
}
ociBuffer->asUint32 = (uint32_t) tempValue;
*ociValue = &ociBuffer->asUint32;
*ociValueLength = sizeof(ociBuffer->asUint32);
break;
case CXO_OCI_ATTR_TYPE_UINT64:
ociBuffer->asUint64 = (uint64_t) PyLong_AsUnsignedLongLong(value);
if (PyErr_Occurred())
return -1;
*ociValue = &ociBuffer->asUint64;
*ociValueLength = sizeof(ociBuffer->asUint64);
break;
default:
cxoError_raiseFromString(cxoProgrammingErrorException,
"invalid attribute type specified");
return -1;
}
return 0;
}
//-----------------------------------------------------------------------------
// cxoUtils_formatString()
// Return a Python string formatted using the given format string and
// arguments. The arguments have a reference taken from them after they have
// been used (which should mean that they are destroyed).
//-----------------------------------------------------------------------------
PyObject *cxoUtils_formatString(const char *format, PyObject *args)
{
PyObject *formatObj, *result;
// assume that a NULL value for arguments implies building the arguments
// failed and a Python exception has already been raised
if (!args)
return NULL;
// convert string format to Python object
formatObj = PyUnicode_DecodeASCII(format, strlen(format), NULL);
if (!formatObj) {
Py_DECREF(args);
return NULL;
}
// create formatted result
result = PyUnicode_Format(formatObj, args);
Py_DECREF(args);
Py_DECREF(formatObj);
return result;
}
//-----------------------------------------------------------------------------
// cxoUtils_getAdjustedEncoding()
// Return the adjusted encoding to use when encoding and decoding strings
// that are passed to and from the Oracle database. The Oracle client interface
// does not support the inclusion of a BOM in the encoded string but assumes
// native endian order for UTF-16. Python generates a BOM at the beginning of
// the encoded string if plain UTF-16 is specified. For this reason, the
// correct byte order must be determined and used inside Python so that the
// Oracle client receives the data it expects.
//-----------------------------------------------------------------------------
const char *cxoUtils_getAdjustedEncoding(const char *encoding)
{
static const union {
unsigned char bytes[4];
uint32_t value;
} hostOrder = { { 0, 1, 2, 3 } };
if (!encoding || strcmp(encoding, "UTF-16") != 0)
return encoding;
return (hostOrder.value == 0x03020100) ? "UTF-16LE" : "UTF-16BE";
}
//-----------------------------------------------------------------------------
// cxoUtils_getModuleAndName()
// Return the module and name for the type.
//-----------------------------------------------------------------------------
int cxoUtils_getModuleAndName(PyTypeObject *type, PyObject **module,
PyObject **name)
{
*module = PyObject_GetAttrString( (PyObject*) type, "__module__");
if (!*module)
return -1;
*name = PyObject_GetAttrString( (PyObject*) type, "__name__");
if (!*name) {
Py_DECREF(*module);
return -1;
}
return 0;
}
//-----------------------------------------------------------------------------
// cxoUtils_initializeDPI()
// Initialize the ODPI-C library. This is done when the first standalone
// connection or session pool is created, rather than when the module is first
// imported so that manipulating environment variables such as NLS_LANG will
// work as expected. It also has the additional benefit of reducing the number
// of errors that can take place when the module is imported.
//-----------------------------------------------------------------------------
int cxoUtils_initializeDPI(dpiContextCreateParams *params)
{
dpiContextCreateParams localParams;
dpiErrorInfo errorInfo;
dpiContext *context;
// if already initialized and parameters were passed, raise an exception;
// otherwise do nothing as this is implicitly called when creating a
// standalone connection or session pool and when getting the Oracle Client
// library version
if (cxoDpiContext) {
if (!params)
return 0;
cxoError_raiseFromString(cxoProgrammingErrorException,
"Oracle Client library has already been initialized");
return -1;
}
// set up parameters used for initializing ODPI-C
if (params) {
memcpy(&localParams, params, sizeof(dpiContextCreateParams));
} else {
memset(&localParams, 0, sizeof(dpiContextCreateParams));
}
localParams.defaultEncoding = "UTF-8";
if (!localParams.defaultDriverName)
localParams.defaultDriverName = CXO_DRIVER_NAME;
if (!localParams.loadErrorUrl)
localParams.loadErrorUrl = "https://cx-oracle.readthedocs.io/en/"
"latest/user_guide/installation.html";
// create ODPI-C context with the specified parameters
if (dpiContext_createWithParams(DPI_MAJOR_VERSION, DPI_MINOR_VERSION,
&localParams, &context, &errorInfo) < 0)
return cxoError_raiseFromInfo(&errorInfo);
if (dpiContext_getClientVersion(context, &cxoClientVersionInfo) < 0) {
cxoError_raiseAndReturnInt();
dpiContext_destroy(context);
return -1;
}
cxoDpiContext = context;
return 0;
}
//-----------------------------------------------------------------------------
// cxoUtils_processJsonArg()
// Process the argument which is expected to be either a string or bytes, or
// a dictionary or list which is converted to a string via the json.dumps()
// method. All strings are encoded to UTF-8 which is what SODA expects.
//-----------------------------------------------------------------------------
int cxoUtils_processJsonArg(PyObject *arg, cxoBuffer *buffer)
{
int converted = 0;
if (arg && (PyDict_Check(arg) || PyList_Check(arg))) {
arg = PyObject_CallFunctionObjArgs(cxoJsonDumpFunction, arg, NULL);
if (!arg)
return -1;
converted = 1;
}
if (cxoBuffer_fromObject(buffer, arg, "UTF-8") < 0)
return -1;
if (converted)
Py_DECREF(arg);
return 0;
}
//-----------------------------------------------------------------------------
// cxoUtils_processSodaDocArg()
// Process a SODA document argument. This is expectd to be an actual SODA
// document object or a dictionary. If the argument refers to a dictionary or
// list, a new SODA document will be created with the given content and without
// a key or media type specified.
//-----------------------------------------------------------------------------
int cxoUtils_processSodaDocArg(cxoSodaDatabase *db, PyObject *arg,
dpiSodaDoc **handle)
{
cxoBuffer buffer;
cxoSodaDoc *doc;
if (PyObject_TypeCheck(arg, &cxoPyTypeSodaDoc)) {
doc = (cxoSodaDoc*) arg;
if (dpiSodaDoc_addRef(doc->handle) < 0)
return cxoError_raiseAndReturnInt();
*handle = doc->handle;
} else if (PyDict_Check(arg) || PyList_Check(arg)) {
arg = PyObject_CallFunctionObjArgs(cxoJsonDumpFunction, arg, NULL);
if (!arg)
return -1;
if (cxoBuffer_fromObject(&buffer, arg, "UTF-8") < 0) {
Py_DECREF(arg);
return -1;
}
Py_DECREF(arg);
if (dpiSodaDb_createDocument(db->handle, NULL, 0, buffer.ptr,
buffer.size, NULL, 0, DPI_SODA_FLAGS_DEFAULT, handle) < 0) {
cxoBuffer_clear(&buffer);
return cxoError_raiseAndReturnInt();
}
cxoBuffer_clear(&buffer);
} else {
PyErr_SetString(PyExc_TypeError,
"value must be a SODA document or a dictionary or list");
return -1;
}
return 0;
}