forked from oracle/python-cx_Oracle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcxoJsonBuffer.c
269 lines (236 loc) · 9.28 KB
/
cxoJsonBuffer.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
//-----------------------------------------------------------------------------
// Copyright (c) 2020, Oracle and/or its affiliates. All rights reserved.
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
// cxoJsonBuffer.c
// Defines buffer structure and routines for populating JSON values. These
// are used to translate Python objects (scalars, dictionaries and lists) into
// JSON values stored in the database.
//-----------------------------------------------------------------------------
#include "cxoModule.h"
#define CXO_JSON_ENCODING "UTF-8"
//-----------------------------------------------------------------------------
// cxoJsonBuffer_getBuffer()
// Acquire a new buffer from the array of buffers. If one is not available,
// more space is allocated in chunks.
//-----------------------------------------------------------------------------
static int cxoJsonBuffer_getBuffer(cxoJsonBuffer *buf, cxoBuffer **buffer)
{
cxoBuffer *tempBuffers;
if (buf->numBuffers == buf->allocatedBuffers) {
buf->allocatedBuffers += 16;
tempBuffers = PyMem_Realloc(buf->buffers,
buf->allocatedBuffers * sizeof(cxoBuffer));
if (!tempBuffers) {
PyErr_NoMemory();
return -1;
}
buf->buffers = tempBuffers;
}
*buffer = &buf->buffers[buf->numBuffers++];
return 0;
}
//-----------------------------------------------------------------------------
// cxoJsonBuffer_populateNode()
// Populate a particular node with the value of the Python object.
//-----------------------------------------------------------------------------
static int cxoJsonBuffer_populateNode(cxoJsonBuffer *buf, dpiJsonNode *node,
PyObject *value)
{
cxoTransformNum transformNum;
PyObject *childValue, *key;
cxoBuffer *tempBuffer;
Py_ssize_t pos, size;
dpiJsonArray *array;
dpiJsonObject *obj;
char message[250];
uint32_t i;
// handle NULL values
if (value == Py_None) {
node->oracleTypeNum = DPI_ORACLE_TYPE_NONE;
node->nativeTypeNum = DPI_NATIVE_TYPE_NULL;
return 0;
}
// handle arrays
if (PyList_Check(value)) {
// initialize array
node->oracleTypeNum = DPI_ORACLE_TYPE_JSON_ARRAY;
node->nativeTypeNum = DPI_NATIVE_TYPE_JSON_ARRAY;
array = &node->value->asJsonArray;
array->numElements = (uint32_t) PyList_GET_SIZE(value);
array->elements = PyMem_Calloc(array->numElements,
sizeof(dpiJsonNode));
array->elementValues = PyMem_Calloc(array->numElements,
sizeof(dpiDataBuffer));
if (!array->elements || !array->elementValues) {
PyErr_NoMemory();
return -1;
}
// process each element of the array
for (i = 0; i < array->numElements; i++) {
childValue = PyList_GET_ITEM(value, i);
array->elements[i].value = &array->elementValues[i];
if (cxoJsonBuffer_populateNode(buf, &array->elements[i],
childValue) < 0)
return -1;
}
return 0;
}
// handle dictionaries
if (PyDict_Check(value)) {
// initialize object
node->oracleTypeNum = DPI_ORACLE_TYPE_JSON_OBJECT;
node->nativeTypeNum = DPI_NATIVE_TYPE_JSON_OBJECT;
obj = &node->value->asJsonObject;
size = PyDict_Size(value);
if (size < 0)
return -1;
obj->numFields = (uint32_t) size;
obj->fieldNames = PyMem_Calloc(obj->numFields, sizeof(char*));
obj->fieldNameLengths = PyMem_Calloc(obj->numFields, sizeof(uint32_t));
obj->fields = PyMem_Calloc(obj->numFields, sizeof(dpiJsonNode));
obj->fieldValues = PyMem_Calloc(obj->numFields,
sizeof(dpiDataBuffer));
if (!obj->fieldNames || !obj->fieldNameLengths || !obj->fields ||
!obj->fieldValues) {
PyErr_NoMemory();
return -1;
}
// process each entry in the dictionary
i = 0;
pos = 0;
while (PyDict_Next(value, &pos, &key, &childValue)) {
if (cxoJsonBuffer_getBuffer(buf, &tempBuffer) < 0)
return -1;
if (cxoBuffer_fromObject(tempBuffer, key, CXO_JSON_ENCODING) < 0)
return -1;
obj->fields[i].value = &obj->fieldValues[i];
obj->fieldNames[i] = (char*) tempBuffer->ptr;
obj->fieldNameLengths[i] = tempBuffer->size;
if (cxoJsonBuffer_populateNode(buf, &obj->fields[i],
childValue) < 0)
return -1;
i++;
}
return 0;
}
// handle scalar values
tempBuffer = NULL;
transformNum = cxoTransform_getNumFromPythonValue(value, 1);
switch (transformNum) {
// strings and bytes must have a buffer made available for them to
// store a reference to the object and the actual pointer and length;
// numbers are converted to a string in order to prevent precision loss
case CXO_TRANSFORM_STRING:
case CXO_TRANSFORM_BINARY:
case CXO_TRANSFORM_INT:
case CXO_TRANSFORM_FLOAT:
case CXO_TRANSFORM_DECIMAL:
if (cxoJsonBuffer_getBuffer(buf, &tempBuffer) < 0)
return -1;
break;
// swap CXO_TRANSFORM_DATETIME to CXO_TRANSFORM_TIMESTAMP to preserve
// fractional seconds
case CXO_TRANSFORM_DATETIME:
transformNum = CXO_TRANSFORM_TIMESTAMP;
break;
// all other types do not need any special processing
case CXO_TRANSFORM_BOOLEAN:
case CXO_TRANSFORM_DATE:
case CXO_TRANSFORM_TIMEDELTA:
break;
// any other type is not currently supported
default:
snprintf(message, sizeof(message), "Python type %s not supported.",
Py_TYPE(value)->tp_name);
cxoError_raiseFromString(cxoNotSupportedErrorException, message);
return -1;
}
// transform the Python value into the Oracle value
cxoTransform_getTypeInfo(transformNum, &node->oracleTypeNum,
&node->nativeTypeNum);
if (cxoTransform_fromPython(transformNum, &node->nativeTypeNum, value,
node->value, tempBuffer, CXO_JSON_ENCODING, CXO_JSON_ENCODING,
NULL, 0) < 0)
return -1;
return 0;
}
//-----------------------------------------------------------------------------
// cxoJsonBuffer_freeNode()
// Frees any arrays allocated earlier for the specified node.
//-----------------------------------------------------------------------------
static void cxoJsonBuffer_freeNode(dpiJsonNode *node)
{
dpiJsonArray *array;
dpiJsonObject *obj;
uint32_t i;
switch (node->nativeTypeNum) {
case DPI_NATIVE_TYPE_JSON_ARRAY:
array = &node->value->asJsonArray;
if (array->elements) {
for (i = 0; i < array->numElements; i++) {
if (array->elements[i].value)
cxoJsonBuffer_freeNode(&array->elements[i]);
}
PyMem_Free(array->elements);
array->elements = NULL;
}
if (array->elementValues) {
PyMem_Free(array->elementValues);
array->elementValues = NULL;
}
break;
case DPI_NATIVE_TYPE_JSON_OBJECT:
obj = &node->value->asJsonObject;
if (obj->fields) {
for (i = 0; i < obj->numFields; i++) {
if (obj->fields[i].value)
cxoJsonBuffer_freeNode(&obj->fields[i]);
}
PyMem_Free(obj->fields);
obj->fields = NULL;
}
if (obj->fieldNames) {
PyMem_Free(obj->fieldNames);
obj->fieldNames = NULL;
}
if (obj->fieldNameLengths) {
PyMem_Free(obj->fieldNameLengths);
obj->fieldNameLengths = NULL;
}
if (obj->fieldValues) {
PyMem_Free(obj->fieldValues);
obj->fieldValues = NULL;
}
break;
}
}
//-----------------------------------------------------------------------------
// cxoJsonBuffer_free()
// Frees any memory allocated for the JSON buffer.
//-----------------------------------------------------------------------------
void cxoJsonBuffer_free(cxoJsonBuffer *buf)
{
uint32_t i;
if (buf->buffers) {
for (i = 0; i < buf->numBuffers; i++)
cxoBuffer_clear(&buf->buffers[i]);
PyMem_Free(buf->buffers);
buf->buffers = NULL;
}
cxoJsonBuffer_freeNode(&buf->topNode);
}
//-----------------------------------------------------------------------------
// cxoJsonBuffer_fromObject()
// Populate the JSON buffer from a Python object.
//-----------------------------------------------------------------------------
int cxoJsonBuffer_fromObject(cxoJsonBuffer *buf, PyObject *obj)
{
// initialize JSON buffer structure
buf->topNode.value = &buf->topNodeBuffer;
buf->allocatedBuffers = 0;
buf->numBuffers = 0;
buf->buffers = NULL;
// populate the top level node
return cxoJsonBuffer_populateNode(buf, &buf->topNode, obj);
}