forked from oracle/python-cx_Oracle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcxoSodaDatabase.c
332 lines (295 loc) · 11.4 KB
/
cxoSodaDatabase.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
//-----------------------------------------------------------------------------
// Copyright (c) 2018, 2020, Oracle and/or its affiliates. All rights reserved.
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
// cxoSodaDatabase.c
// Defines the routines for handling the SODA database.
//-----------------------------------------------------------------------------
#include "cxoModule.h"
//-----------------------------------------------------------------------------
// cxoSodaDatabase_new()
// Create a new SODA database object.
//-----------------------------------------------------------------------------
cxoSodaDatabase *cxoSodaDatabase_new(cxoConnection *connection)
{
cxoSodaDatabase *db;
PyObject *module;
// load JSON dump/load functions, if needed
if (!cxoJsonDumpFunction || !cxoJsonLoadFunction) {
module = PyImport_ImportModule("json");
if (!module)
return NULL;
if (!cxoJsonDumpFunction) {
cxoJsonDumpFunction = PyObject_GetAttrString(module, "dumps");
if (!cxoJsonDumpFunction)
return NULL;
}
if (!cxoJsonLoadFunction) {
cxoJsonLoadFunction = PyObject_GetAttrString(module, "loads");
if (!cxoJsonLoadFunction)
return NULL;
}
}
// create SODA database object
db = (cxoSodaDatabase*)
cxoPyTypeSodaDatabase.tp_alloc(&cxoPyTypeSodaDatabase, 0);
if (!db)
return NULL;
if (dpiConn_getSodaDb(connection->handle, &db->handle) < 0) {
Py_DECREF(db);
cxoError_raiseAndReturnNull();
return NULL;
}
Py_INCREF(connection);
db->connection = connection;
return db;
}
//-----------------------------------------------------------------------------
// cxoSodaDatabase_free()
// Free the memory associated with a SODA database.
//-----------------------------------------------------------------------------
static void cxoSodaDatabase_free(cxoSodaDatabase *db)
{
if (db->handle) {
dpiSodaDb_release(db->handle);
db->handle = NULL;
}
Py_CLEAR(db->connection);
Py_TYPE(db)->tp_free((PyObject*) db);
}
//-----------------------------------------------------------------------------
// cxoSodaDatabase_repr()
// Return a string representation of a SODA database.
//-----------------------------------------------------------------------------
static PyObject *cxoSodaDatabase_repr(cxoSodaDatabase *db)
{
PyObject *connectionRepr, *module, *name, *result;
connectionRepr = PyObject_Repr((PyObject*) db->connection);
if (!connectionRepr)
return NULL;
if (cxoUtils_getModuleAndName(Py_TYPE(db), &module, &name) < 0) {
Py_DECREF(connectionRepr);
return NULL;
}
result = cxoUtils_formatString("<%s.%s on %s>",
PyTuple_Pack(3, module, name, connectionRepr));
Py_DECREF(module);
Py_DECREF(name);
Py_DECREF(connectionRepr);
return result;
}
//-----------------------------------------------------------------------------
// cxoSodaDatabase_createCollection()
// Create a SODA collection and return it.
//-----------------------------------------------------------------------------
static PyObject *cxoSodaDatabase_createCollection(cxoSodaDatabase *db,
PyObject *args, PyObject *keywordArgs)
{
static char *keywordList[] = { "name", "metadata", "mapMode", NULL };
PyObject *nameObj, *metadataObj, *mapModeObj;
cxoBuffer nameBuffer, metadataBuffer;
cxoSodaCollection *coll;
const char *encoding;
dpiSodaColl *handle;
int status, mapMode;
uint32_t flags;
// parse arguments
nameObj = metadataObj = mapModeObj = NULL;
if (!PyArg_ParseTupleAndKeywords(args, keywordArgs, "O|OO", keywordList,
&nameObj, &metadataObj, &mapModeObj))
return NULL;
encoding = db->connection->encodingInfo.encoding;
if (cxoBuffer_fromObject(&nameBuffer, nameObj, encoding) < 0)
return NULL;
if (cxoUtils_processJsonArg(metadataObj, &metadataBuffer) < 0) {
cxoBuffer_clear(&nameBuffer);
return NULL;
}
if (cxoUtils_getBooleanValue(mapModeObj, 0, &mapMode) < 0) {
cxoBuffer_clear(&nameBuffer);
cxoBuffer_clear(&metadataBuffer);
return NULL;
}
// create collection
if (cxoConnection_getSodaFlags(db->connection, &flags) < 0)
return NULL;
if (mapMode)
flags |= DPI_SODA_FLAGS_CREATE_COLL_MAP;
Py_BEGIN_ALLOW_THREADS
status = dpiSodaDb_createCollection(db->handle, nameBuffer.ptr,
nameBuffer.size, metadataBuffer.ptr, metadataBuffer.size, flags,
&handle);
Py_END_ALLOW_THREADS
cxoBuffer_clear(&nameBuffer);
cxoBuffer_clear(&metadataBuffer);
if (status < 0)
return cxoError_raiseAndReturnNull();
coll = cxoSodaCollection_new(db, handle);
if (!coll) {
dpiSodaColl_release(handle);
return NULL;
}
return (PyObject*) coll;
}
//-----------------------------------------------------------------------------
// cxoSodaDatabase_createDocument()
// Create a SODA document with the specified key, content and media type.
//-----------------------------------------------------------------------------
static PyObject *cxoSodaDatabase_createDocument(cxoSodaDatabase *db,
PyObject *args, PyObject *keywordArgs)
{
static char *keywordList[] = { "content", "key", "mediaType", NULL };
cxoBuffer contentBuffer, keyBuffer, mediaTypeBuffer;
PyObject *contentObj, *keyObj, *mediaTypeObj;
const char *encoding;
dpiSodaDoc *doc;
int status;
// parse arguments
keyObj = mediaTypeObj = NULL;
if (!PyArg_ParseTupleAndKeywords(args, keywordArgs, "O|OO", keywordList,
&contentObj, &keyObj, &mediaTypeObj))
return NULL;
// content must be converted to string if it is a dictionary
if (PyDict_Check(contentObj)) {
contentObj = PyObject_CallFunctionObjArgs(cxoJsonDumpFunction,
contentObj, NULL);
if (!contentObj)
return NULL;
}
// get buffers for each of the content, key and media type parameters
if (cxoUtils_processJsonArg(contentObj, &contentBuffer) < 0)
return NULL;
encoding = db->connection->encodingInfo.encoding;
if (cxoBuffer_fromObject(&keyBuffer, keyObj, encoding) < 0) {
cxoBuffer_clear(&contentBuffer);
return NULL;
}
if (cxoBuffer_fromObject(&mediaTypeBuffer, mediaTypeObj, encoding) < 0) {
cxoBuffer_clear(&contentBuffer);
cxoBuffer_clear(&keyBuffer);
return NULL;
}
// create SODA document
status = dpiSodaDb_createDocument(db->handle, keyBuffer.ptr,
keyBuffer.size, contentBuffer.ptr, contentBuffer.size,
mediaTypeBuffer.ptr, mediaTypeBuffer.size, DPI_SODA_FLAGS_DEFAULT,
&doc);
cxoBuffer_clear(&contentBuffer);
cxoBuffer_clear(&keyBuffer);
cxoBuffer_clear(&mediaTypeBuffer);
if (status < 0)
return cxoError_raiseAndReturnNull();
return (PyObject*) cxoSodaDoc_new(db, doc);
}
//-----------------------------------------------------------------------------
// cxoSodaDatabase_getCollectionNames()
// Return a list of the names of the collections found in the database.
//-----------------------------------------------------------------------------
static PyObject *cxoSodaDatabase_getCollectionNames(cxoSodaDatabase *db,
PyObject *args, PyObject *keywordArgs)
{
static char *keywordList[] = { "startName", "limit", NULL };
PyObject *startName, *result, *temp;
dpiSodaCollNames collNames;
cxoBuffer startNameBuffer;
uint32_t limit, i, flags;
const char *encoding;
int status;
// parse arguments
limit = 0;
startName = NULL;
if (!PyArg_ParseTupleAndKeywords(args, keywordArgs, "|Oi", keywordList,
&startName, &limit))
return NULL;
// get collection names from the database
encoding = db->connection->encodingInfo.encoding;
if (cxoBuffer_fromObject(&startNameBuffer, startName, encoding) < 0)
return NULL;
if (cxoConnection_getSodaFlags(db->connection, &flags) < 0)
return NULL;
Py_BEGIN_ALLOW_THREADS
status = dpiSodaDb_getCollectionNames(db->handle,
(const char*) startNameBuffer.ptr, startNameBuffer.size, limit,
flags, &collNames);
Py_END_ALLOW_THREADS
cxoBuffer_clear(&startNameBuffer);
if (status < 0)
return cxoError_raiseAndReturnNull();
// transform results into a Python list
result = PyList_New(collNames.numNames);
if (!result)
return NULL;
for (i = 0; i < collNames.numNames; i++) {
temp = PyUnicode_Decode(collNames.names[i], collNames.nameLengths[i],
encoding, NULL);
if (!temp) {
Py_DECREF(result);
return NULL;
}
PyList_SET_ITEM(result, i, temp);
}
if (dpiSodaDb_freeCollectionNames(db->handle, &collNames) < 0) {
Py_DECREF(result);
return cxoError_raiseAndReturnNull();
}
return result;
}
//-----------------------------------------------------------------------------
// cxoSodaDatabase_openCollection()
// Open a SODA collection and return it.
//-----------------------------------------------------------------------------
static PyObject *cxoSodaDatabase_openCollection(cxoSodaDatabase *db,
PyObject *nameObj)
{
cxoSodaCollection *coll;
cxoBuffer nameBuffer;
dpiSodaColl *handle;
uint32_t flags;
int status;
// open collection
if (cxoBuffer_fromObject(&nameBuffer, nameObj,
db->connection->encodingInfo.encoding) < 0)
return NULL;
if (cxoConnection_getSodaFlags(db->connection, &flags) < 0)
return NULL;
Py_BEGIN_ALLOW_THREADS
status = dpiSodaDb_openCollection(db->handle, nameBuffer.ptr,
nameBuffer.size, flags, &handle);
Py_END_ALLOW_THREADS
cxoBuffer_clear(&nameBuffer);
if (status < 0)
return cxoError_raiseAndReturnNull();
if (!handle)
Py_RETURN_NONE;
coll = cxoSodaCollection_new(db, handle);
if (!coll) {
dpiSodaColl_release(handle);
return NULL;
}
return (PyObject*) coll;
}
//-----------------------------------------------------------------------------
// declaration of methods for Python type
//-----------------------------------------------------------------------------
static PyMethodDef cxoMethods[] = {
{ "createCollection", (PyCFunction) cxoSodaDatabase_createCollection,
METH_VARARGS | METH_KEYWORDS },
{ "createDocument", (PyCFunction) cxoSodaDatabase_createDocument,
METH_VARARGS | METH_KEYWORDS },
{ "getCollectionNames", (PyCFunction) cxoSodaDatabase_getCollectionNames,
METH_VARARGS | METH_KEYWORDS },
{ "openCollection", (PyCFunction) cxoSodaDatabase_openCollection, METH_O },
{ NULL }
};
//-----------------------------------------------------------------------------
// declaration of Python type
//-----------------------------------------------------------------------------
PyTypeObject cxoPyTypeSodaDatabase = {
PyVarObject_HEAD_INIT(NULL, 0)
.tp_name = "cx_Oracle.SodaDatabase",
.tp_basicsize = sizeof(cxoSodaDatabase),
.tp_dealloc = (destructor) cxoSodaDatabase_free,
.tp_repr = (reprfunc) cxoSodaDatabase_repr,
.tp_flags = Py_TPFLAGS_DEFAULT,
.tp_methods = cxoMethods
};