forked from oracle/python-cx_Oracle
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcxoSodaDoc.c
More file actions
265 lines (224 loc) · 9.17 KB
/
cxoSodaDoc.c
File metadata and controls
265 lines (224 loc) · 9.17 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
//-----------------------------------------------------------------------------
// Copyright (c) 2018, 2020, Oracle and/or its affiliates. All rights reserved.
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
// cxoSodaDoc.c
// Defines the routines for handling SODA documents.
//-----------------------------------------------------------------------------
#include "cxoModule.h"
// forward declarations
static PyObject *cxoSodaDoc_getContentAsString(cxoSodaDoc *doc,
PyObject *args);
//-----------------------------------------------------------------------------
// cxoSodaDoc_new()
// Create a new SODA document.
//-----------------------------------------------------------------------------
cxoSodaDoc *cxoSodaDoc_new(cxoSodaDatabase *db, dpiSodaDoc *handle)
{
cxoSodaDoc *doc;
doc = (cxoSodaDoc*) cxoPyTypeSodaDoc.tp_alloc(&cxoPyTypeSodaDoc, 0);
if (!doc) {
dpiSodaDoc_release(handle);
return NULL;
}
Py_INCREF(db);
doc->db = db;
doc->handle = handle;
return doc;
}
//-----------------------------------------------------------------------------
// cxoSodaDoc_free()
// Free the memory associated with a SODA document.
//-----------------------------------------------------------------------------
static void cxoSodaDoc_free(cxoSodaDoc *doc)
{
if (doc->handle) {
dpiSodaDoc_release(doc->handle);
doc->handle = NULL;
}
Py_CLEAR(doc->db);
Py_TYPE(doc)->tp_free((PyObject*) doc);
}
//-----------------------------------------------------------------------------
// cxoSodaDoc_repr()
// Return a string representation of a SODA document.
//-----------------------------------------------------------------------------
static PyObject *cxoSodaDoc_repr(cxoSodaDoc *doc)
{
PyObject *module, *name, *result, *keyObj;
uint32_t keyLength;
const char *key;
if (dpiSodaDoc_getKey(doc->handle, &key, &keyLength) < 0)
return cxoError_raiseAndReturnNull();
keyObj = PyUnicode_Decode(key, keyLength,
doc->db->connection->encodingInfo.encoding, NULL);
if (!keyObj)
return NULL;
if (cxoUtils_getModuleAndName(Py_TYPE(doc), &module, &name) < 0) {
Py_DECREF(keyObj);
return NULL;
}
result = cxoUtils_formatString("<%s.%s with key %s>",
PyTuple_Pack(3, module, name, keyObj));
Py_DECREF(module);
Py_DECREF(name);
return result;
}
//-----------------------------------------------------------------------------
// cxoSodaDoc_getCreatedOn()
// Retrieve the time the SODA document was created, as a string in ISO 8601
// format.
//-----------------------------------------------------------------------------
static PyObject *cxoSodaDoc_getCreatedOn(cxoSodaDoc *doc, void *unused)
{
uint32_t valueLength;
const char *value;
if (dpiSodaDoc_getCreatedOn(doc->handle, &value, &valueLength) < 0)
return cxoError_raiseAndReturnNull();
if (valueLength > 0)
return PyUnicode_Decode(value, valueLength,
doc->db->connection->encodingInfo.encoding, NULL);
Py_RETURN_NONE;
}
//-----------------------------------------------------------------------------
// cxoSodaDoc_getKey()
// Retrieve the key for the SODA document.
//-----------------------------------------------------------------------------
static PyObject *cxoSodaDoc_getKey(cxoSodaDoc *doc, void *unused)
{
uint32_t valueLength;
const char *value;
if (dpiSodaDoc_getKey(doc->handle, &value, &valueLength) < 0)
return cxoError_raiseAndReturnNull();
if (valueLength > 0)
return PyUnicode_Decode(value, valueLength,
doc->db->connection->encodingInfo.encoding, NULL);
Py_RETURN_NONE;
}
//-----------------------------------------------------------------------------
// cxoSodaDoc_getLastModified()
// Retrieve the time the SODA document was last modified, as a string in ISO
// 8601 format.
//-----------------------------------------------------------------------------
static PyObject *cxoSodaDoc_getLastModified(cxoSodaDoc *doc, void *unused)
{
uint32_t valueLength;
const char *value;
if (dpiSodaDoc_getLastModified(doc->handle, &value, &valueLength) < 0)
return cxoError_raiseAndReturnNull();
if (valueLength > 0)
return PyUnicode_Decode(value, valueLength,
doc->db->connection->encodingInfo.encoding, NULL);
Py_RETURN_NONE;
}
//-----------------------------------------------------------------------------
// cxoSodaDoc_getMediaType()
// Retrieve the media type of the SODA document.
//-----------------------------------------------------------------------------
static PyObject *cxoSodaDoc_getMediaType(cxoSodaDoc *doc, void *unused)
{
uint32_t valueLength;
const char *value;
if (dpiSodaDoc_getMediaType(doc->handle, &value, &valueLength) < 0)
return cxoError_raiseAndReturnNull();
if (valueLength > 0)
return PyUnicode_Decode(value, valueLength,
doc->db->connection->encodingInfo.encoding, NULL);
Py_RETURN_NONE;
}
//-----------------------------------------------------------------------------
// cxoSodaDoc_getVersion()
// Retrieve the version for the SODA document.
//-----------------------------------------------------------------------------
static PyObject *cxoSodaDoc_getVersion(cxoSodaDoc *doc, void *unused)
{
uint32_t valueLength;
const char *value;
if (dpiSodaDoc_getVersion(doc->handle, &value, &valueLength) < 0)
return cxoError_raiseAndReturnNull();
if (valueLength > 0)
return PyUnicode_Decode(value, valueLength,
doc->db->connection->encodingInfo.encoding, NULL);
Py_RETURN_NONE;
}
//-----------------------------------------------------------------------------
// cxoSodaDoc_getContent()
// Get the content from the document and return a Python object.
//-----------------------------------------------------------------------------
static PyObject *cxoSodaDoc_getContent(cxoSodaDoc *doc, PyObject *args)
{
PyObject *str, *result;
str = cxoSodaDoc_getContentAsString(doc, args);
if (!str)
return NULL;
if (str == Py_None)
return str;
result = PyObject_CallFunctionObjArgs(cxoJsonLoadFunction, str, NULL);
Py_DECREF(str);
return result;
}
//-----------------------------------------------------------------------------
// cxoSodaDoc_getContentAsBytes()
// Get the content from the document and return a bytes object.
//-----------------------------------------------------------------------------
static PyObject *cxoSodaDoc_getContentAsBytes(cxoSodaDoc *doc, PyObject *args)
{
const char *content, *encoding;
uint32_t contentLength;
if (dpiSodaDoc_getContent(doc->handle, &content, &contentLength,
&encoding) < 0)
return cxoError_raiseAndReturnNull();
if (contentLength > 0)
return PyBytes_FromStringAndSize(content, contentLength);
Py_RETURN_NONE;
}
//-----------------------------------------------------------------------------
// cxoSodaDoc_getContentAsString()
// Get the content from the document and return a string.
//-----------------------------------------------------------------------------
static PyObject *cxoSodaDoc_getContentAsString(cxoSodaDoc *doc, PyObject *args)
{
const char *content, *encoding;
uint32_t contentLength;
if (dpiSodaDoc_getContent(doc->handle, &content, &contentLength,
&encoding) < 0)
return cxoError_raiseAndReturnNull();
if (contentLength > 0)
return PyUnicode_Decode(content, contentLength, encoding, NULL);
Py_RETURN_NONE;
}
//-----------------------------------------------------------------------------
// declaration of methods
//-----------------------------------------------------------------------------
static PyMethodDef cxoMethods[] = {
{ "getContent", (PyCFunction) cxoSodaDoc_getContent, METH_NOARGS },
{ "getContentAsBytes", (PyCFunction) cxoSodaDoc_getContentAsBytes,
METH_NOARGS },
{ "getContentAsString", (PyCFunction) cxoSodaDoc_getContentAsString,
METH_NOARGS },
{ NULL }
};
//-----------------------------------------------------------------------------
// declaration of calculated members
//-----------------------------------------------------------------------------
static PyGetSetDef cxoCalcMembers[] = {
{ "createdOn", (getter) cxoSodaDoc_getCreatedOn, 0, 0, 0 },
{ "key", (getter) cxoSodaDoc_getKey, 0, 0, 0 },
{ "lastModified", (getter) cxoSodaDoc_getLastModified, 0, 0, 0 },
{ "mediaType", (getter) cxoSodaDoc_getMediaType, 0, 0, 0 },
{ "version", (getter) cxoSodaDoc_getVersion, 0, 0, 0 },
{ NULL }
};
//-----------------------------------------------------------------------------
// declaration of Python type
//-----------------------------------------------------------------------------
PyTypeObject cxoPyTypeSodaDoc = {
PyVarObject_HEAD_INIT(NULL, 0)
.tp_name = "cx_Oracle.SodaDoc",
.tp_basicsize = sizeof(cxoSodaDoc),
.tp_dealloc = (destructor) cxoSodaDoc_free,
.tp_repr = (reprfunc) cxoSodaDoc_repr,
.tp_flags = Py_TPFLAGS_DEFAULT,
.tp_methods = cxoMethods,
.tp_getset = cxoCalcMembers
};