Codebase list python-cx-oracle / f6f53092-631a-4f23-a503-f9ea6dae6db6/main src / cxoLob.c
f6f53092-631a-4f23-a503-f9ea6dae6db6/main

Tree @f6f53092-631a-4f23-a503-f9ea6dae6db6/main (Download .tar.gz)

cxoLob.c @f6f53092-631a-4f23-a503-f9ea6dae6db6/mainraw · history · blame

  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
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
//-----------------------------------------------------------------------------
// Copyright (c) 2016, 2018, 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.
//-----------------------------------------------------------------------------

//-----------------------------------------------------------------------------
// cxoLob.c
//   Defines the routines for handling LOB values.
//-----------------------------------------------------------------------------

#include "cxoModule.h"

//-----------------------------------------------------------------------------
// Declaration of external LOB functions.
//-----------------------------------------------------------------------------
static void cxoLob_free(cxoLob*);
static PyObject *cxoLob_str(cxoLob*);
static PyObject *cxoLob_size(cxoLob*, PyObject*);
static PyObject *cxoLob_open(cxoLob*, PyObject*);
static PyObject *cxoLob_close(cxoLob*, PyObject*);
static PyObject *cxoLob_read(cxoLob*, PyObject*, PyObject*);
static PyObject *cxoLob_write(cxoLob*, PyObject*, PyObject*);
static PyObject *cxoLob_trim(cxoLob*, PyObject*, PyObject*);
static PyObject *cxoLob_getChunkSize(cxoLob*, PyObject*);
static PyObject *cxoLob_isOpen(cxoLob*, PyObject*);
static PyObject *cxoLob_getFileName(cxoLob*, PyObject*);
static PyObject *cxoLob_setFileName(cxoLob*, PyObject*);
static PyObject *cxoLob_fileExists(cxoLob*, PyObject*);
static PyObject *cxoLob_reduce(cxoLob*);


//-----------------------------------------------------------------------------
// declaration of methods for Python type "LOB"
//-----------------------------------------------------------------------------
static PyMethodDef cxoLobMethods[] = {
    { "size", (PyCFunction) cxoLob_size, METH_NOARGS },
    { "open", (PyCFunction) cxoLob_open, METH_NOARGS },
    { "close", (PyCFunction) cxoLob_close, METH_NOARGS },
    { "read", (PyCFunction) cxoLob_read, METH_VARARGS | METH_KEYWORDS },
    { "write", (PyCFunction) cxoLob_write, METH_VARARGS | METH_KEYWORDS },
    { "trim", (PyCFunction) cxoLob_trim, METH_VARARGS | METH_KEYWORDS },
    { "getchunksize", (PyCFunction) cxoLob_getChunkSize, METH_NOARGS },
    { "isopen", (PyCFunction) cxoLob_isOpen, METH_NOARGS },
    { "getfilename", (PyCFunction) cxoLob_getFileName, METH_NOARGS },
    { "setfilename", (PyCFunction) cxoLob_setFileName, METH_VARARGS },
    { "fileexists", (PyCFunction) cxoLob_fileExists, METH_NOARGS },
    { "__reduce__", (PyCFunction) cxoLob_reduce, METH_NOARGS },
    { NULL, NULL }
};


//-----------------------------------------------------------------------------
// Python type declaration
//-----------------------------------------------------------------------------
PyTypeObject cxoPyTypeLob = {
    PyVarObject_HEAD_INIT(NULL, 0)
    "cx_Oracle.LOB",                    // tp_name
    sizeof(cxoLob),                     // tp_basicsize
    0,                                  // tp_itemsize
    (destructor) cxoLob_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
    (reprfunc) cxoLob_str,              // tp_str
    0,                                  // tp_getattro
    0,                                  // tp_setattro
    0,                                  // tp_as_buffer
    Py_TPFLAGS_DEFAULT,                 // tp_flags
    0,                                  // tp_doc
    0,                                  // tp_traverse
    0,                                  // tp_clear
    0,                                  // tp_richcompare
    0,                                  // tp_weaklistoffset
    0,                                  // tp_iter
    0,                                  // tp_iternext
    cxoLobMethods,                      // tp_methods
    0,                                  // tp_members
    0,                                  // tp_getset
    0,                                  // tp_base
    0,                                  // tp_dict
    0,                                  // tp_descr_get
    0,                                  // tp_descr_set
    0,                                  // tp_dictoffset
    0,                                  // tp_init
    0,                                  // tp_alloc
    0,                                  // tp_new
    0,                                  // tp_free
    0,                                  // tp_is_gc
    0                                   // tp_bases
};


//-----------------------------------------------------------------------------
// cxoLob_new()
//   Create a new LOB.
//-----------------------------------------------------------------------------
PyObject *cxoLob_new(cxoConnection *connection, dpiOracleTypeNum oracleTypeNum,
        dpiLob *handle)
{
    cxoLob *lob;

    lob = (cxoLob*) cxoPyTypeLob.tp_alloc(&cxoPyTypeLob, 0);
    if (!lob)
        return NULL;
    lob->handle = handle;
    lob->oracleTypeNum = oracleTypeNum;
    Py_INCREF(connection);
    lob->connection = connection;
    return (PyObject*) lob;
}


//-----------------------------------------------------------------------------
// cxoLob_free()
//   Free a LOB.
//-----------------------------------------------------------------------------
static void cxoLob_free(cxoLob *lob)
{
    if (lob->handle) {
        dpiLob_release(lob->handle);
        lob->handle = NULL;
    }
    Py_CLEAR(lob->connection);
    Py_TYPE(lob)->tp_free((PyObject*) lob);
}


//-----------------------------------------------------------------------------
// cxoLob_internalRead()
//   Return a portion (or all) of the data in the LOB.
//-----------------------------------------------------------------------------
static PyObject *cxoLob_internalRead(cxoLob *lob, uint64_t offset,
        uint64_t amount)
{
    uint64_t bufferSize;
    PyObject *result;
    char *buffer;
    int status;

    // modify the arguments
    if (amount == (uint64_t)(-1)) {
        if (dpiLob_getSize(lob->handle, &amount) < 0)
            return cxoError_raiseAndReturnNull();
        if (amount >= offset)
            amount = amount - offset + 1;
        else amount = 1;
    }

    // create a buffer of the correct size
    if (dpiLob_getBufferSize(lob->handle, amount, &bufferSize) < 0)
        return cxoError_raiseAndReturnNull();
    buffer = (char*) PyMem_Malloc((Py_ssize_t) bufferSize);
    if (!buffer)
        return PyErr_NoMemory();

    // read the LOB
    Py_BEGIN_ALLOW_THREADS
    status = dpiLob_readBytes(lob->handle, offset, amount, buffer,
            &bufferSize);
    Py_END_ALLOW_THREADS
    if (status < 0) {
        PyMem_Free(buffer);
        return cxoError_raiseAndReturnNull();
    }

    // return the result
    if (lob->oracleTypeNum == DPI_ORACLE_TYPE_NCLOB)
        result = PyUnicode_Decode(buffer, (Py_ssize_t) bufferSize,
                lob->connection->encodingInfo.nencoding, NULL);
    else if (lob->oracleTypeNum == DPI_ORACLE_TYPE_CLOB)
        result = cxoPyString_fromEncodedString(buffer, (Py_ssize_t) bufferSize,
                lob->connection->encodingInfo.encoding, NULL);
    else result = PyBytes_FromStringAndSize(buffer, (Py_ssize_t) bufferSize);
    PyMem_Free(buffer);
    return result;
}


//-----------------------------------------------------------------------------
// cxoLob_internalWrite()
//   Write the data in the Python object to the LOB.
//-----------------------------------------------------------------------------
static int cxoLob_internalWrite(cxoLob *lob, PyObject *dataObj,
        uint64_t offset)
{
    const char *encoding;
    cxoBuffer buffer;
    int status;

    if (lob->oracleTypeNum == DPI_ORACLE_TYPE_NCLOB)
        encoding = lob->connection->encodingInfo.nencoding;
    else encoding = lob->connection->encodingInfo.encoding;
    if (cxoBuffer_fromObject(&buffer, dataObj, encoding) < 0)
        return -1;
    Py_BEGIN_ALLOW_THREADS
    status = dpiLob_writeBytes(lob->handle, offset,
            (char*) buffer.ptr, buffer.size);
    Py_END_ALLOW_THREADS
    cxoBuffer_clear(&buffer);
    if (status < 0)
        return cxoError_raiseAndReturnInt();
    return 0;
}


//-----------------------------------------------------------------------------
// cxoLob_size()
//   Return the size of the data in the LOB.
//-----------------------------------------------------------------------------
static PyObject *cxoLob_size(cxoLob *lob, PyObject *args)
{
    uint64_t length;

    if (dpiLob_getSize(lob->handle, &length) < 0)
        return cxoError_raiseAndReturnNull();
    return PyLong_FromUnsignedLongLong(length);
}


//-----------------------------------------------------------------------------
// cxoLob_open()
//   Open the LOB to speed further accesses.
//-----------------------------------------------------------------------------
static PyObject *cxoLob_open(cxoLob *lob, PyObject *args)
{
    int status;

    Py_BEGIN_ALLOW_THREADS
    status = dpiLob_openResource(lob->handle);
    Py_END_ALLOW_THREADS
    if (status < 0)
        return cxoError_raiseAndReturnNull();
    Py_RETURN_NONE;
}


//-----------------------------------------------------------------------------
// cxoLob_close()
//   Close the LOB.
//-----------------------------------------------------------------------------
static PyObject *cxoLob_close(cxoLob *lob, PyObject *args)
{
    int status;

    Py_BEGIN_ALLOW_THREADS
    status = dpiLob_closeResource(lob->handle);
    Py_END_ALLOW_THREADS
    if (status < 0)
        return cxoError_raiseAndReturnNull();
    Py_RETURN_NONE;
}


//-----------------------------------------------------------------------------
// cxoLob_read()
//   Return a portion (or all) of the data in the LOB.
//-----------------------------------------------------------------------------
static PyObject *cxoLob_read(cxoLob *lob, PyObject *args, PyObject *keywordArgs)
{
    static char *keywordList[] = { "offset", "amount", NULL };
    unsigned PY_LONG_LONG offset, amount;

    // offset and amount are expected, both optional
    offset = 1;
    amount = (unsigned PY_LONG_LONG)(-1);
    if (!PyArg_ParseTupleAndKeywords(args, keywordArgs, "|KK", keywordList,
            &offset, &amount))
        return NULL;
    return cxoLob_internalRead(lob, (uint64_t) offset, (uint64_t) amount);
}


//-----------------------------------------------------------------------------
// cxoLob_str()
//   Return all of the data in the LOB.
//-----------------------------------------------------------------------------
static PyObject *cxoLob_str(cxoLob *lob)
{
    return cxoLob_internalRead(lob, 1, (uint64_t)(-1));
}


//-----------------------------------------------------------------------------
// cxoLob_write()
//   Write a value to the LOB.
//-----------------------------------------------------------------------------
static PyObject *cxoLob_write(cxoLob *lob, PyObject *args,
        PyObject *keywordArgs)
{
    static char *keywordList[] = { "data", "offset", NULL };
    unsigned PY_LONG_LONG offset;
    PyObject *dataObj;

    offset = 1;
    if (!PyArg_ParseTupleAndKeywords(args, keywordArgs, "O|K", keywordList,
            &dataObj, &offset))
        return NULL;
    if (cxoLob_internalWrite(lob, dataObj, (uint64_t) offset) < 0)
        return NULL;
    Py_RETURN_NONE;
}


//-----------------------------------------------------------------------------
// cxoLob_trim()
//   Trim the LOB to the specified length.
//-----------------------------------------------------------------------------
static PyObject *cxoLob_trim(cxoLob *lob, PyObject *args,
        PyObject *keywordArgs)
{
    static char *keywordList[] = { "newSize", NULL };
    unsigned PY_LONG_LONG newSize;
    int status;

    newSize = 0;
    if (!PyArg_ParseTupleAndKeywords(args, keywordArgs, "|K", keywordList,
            &newSize))
        return NULL;
    Py_BEGIN_ALLOW_THREADS
    status = dpiLob_trim(lob->handle, (uint64_t) newSize);
    Py_END_ALLOW_THREADS
    if (status < 0)
        return cxoError_raiseAndReturnNull();
    Py_RETURN_NONE;
}


//-----------------------------------------------------------------------------
// cxoLob_reduce()
//   Method provided for pickling/unpickling of LOBs.
//-----------------------------------------------------------------------------
static PyObject *cxoLob_reduce(cxoLob *lob)
{
    PyObject *result, *value;

    value = cxoLob_str(lob);
    if (!value)
        return NULL;
    result = Py_BuildValue("(O(O))", Py_TYPE(value), value);
    Py_DECREF(value);
    return result;
}


//-----------------------------------------------------------------------------
// cxoLob_getChunkSize()
//   Return the chunk size that should be used when reading/writing the LOB in
// chunks.
//-----------------------------------------------------------------------------
static PyObject *cxoLob_getChunkSize(cxoLob *lob, PyObject *args)
{
    uint32_t size;

    if (dpiLob_getChunkSize(lob->handle, &size) < 0)
        return cxoError_raiseAndReturnNull();
    return PyInt_FromLong(size);
}


//-----------------------------------------------------------------------------
// cxoLob_isOpen()
//   Return a boolean indicating if the lob is open or not.
//-----------------------------------------------------------------------------
static PyObject *cxoLob_isOpen(cxoLob *lob, PyObject *args)
{
    int isOpen, status;

    Py_BEGIN_ALLOW_THREADS
    status = dpiLob_getIsResourceOpen(lob->handle, &isOpen);
    Py_END_ALLOW_THREADS
    if (status < 0)
        return cxoError_raiseAndReturnNull();
    return PyBool_FromLong(isOpen);
}


//-----------------------------------------------------------------------------
// cxoLob_getFileName()
//   Return the directory alias and file name for the BFILE lob.
//-----------------------------------------------------------------------------
static PyObject *cxoLob_getFileName(cxoLob *lob, PyObject *args)
{
    uint32_t directoryAliasLength, fileNameLength;
    const char *directoryAlias, *fileName;
    PyObject *result, *temp;
    int status;

    // get the information from the LOB
    Py_BEGIN_ALLOW_THREADS
    status = dpiLob_getDirectoryAndFileName(lob->handle, &directoryAlias,
            &directoryAliasLength, &fileName, &fileNameLength);
    Py_END_ALLOW_THREADS
    if (status < 0)
        return cxoError_raiseAndReturnNull();

    // create the two-tuple for returning
    result = PyTuple_New(2);
    if (!result)
        return NULL;
    temp = cxoPyString_fromEncodedString(directoryAlias, directoryAliasLength,
            lob->connection->encodingInfo.encoding, NULL);
    if (!temp) {
        Py_DECREF(result);
        return NULL;
    }
    PyTuple_SET_ITEM(result, 0, temp);
    temp = cxoPyString_fromEncodedString(fileName, fileNameLength,
            lob->connection->encodingInfo.encoding, NULL);
    if (!temp) {
        Py_DECREF(result);
        return NULL;
    }
    PyTuple_SET_ITEM(result, 1, temp);

    return result;
}


//-----------------------------------------------------------------------------
// cxoLob_setFileName()
//   Set the directory alias and file name for the BFILE lob.
//-----------------------------------------------------------------------------
static PyObject *cxoLob_setFileName(cxoLob *lob, PyObject *args)
{
    cxoBuffer directoryAliasBuffer, fileNameBuffer;
    PyObject *directoryAliasObj, *fileNameObj;
    int status;

    // get the directory alias and file name
    if (!PyArg_ParseTuple(args, "OO", &directoryAliasObj, &fileNameObj))
        return NULL;
    if (cxoBuffer_fromObject(&directoryAliasBuffer, directoryAliasObj,
            lob->connection->encodingInfo.encoding) < 0)
        return NULL;
    if (cxoBuffer_fromObject(&fileNameBuffer, fileNameObj,
            lob->connection->encodingInfo.encoding) < 0) {
        cxoBuffer_clear(&directoryAliasBuffer);
        return NULL;
    }

    // perform the work
    Py_BEGIN_ALLOW_THREADS
    status = dpiLob_setDirectoryAndFileName(lob->handle,
            (char*) directoryAliasBuffer.ptr, directoryAliasBuffer.size,
            (char*) fileNameBuffer.ptr, fileNameBuffer.size);
    Py_END_ALLOW_THREADS
    cxoBuffer_clear(&directoryAliasBuffer);
    cxoBuffer_clear(&fileNameBuffer);
    if (status < 0)
        return cxoError_raiseAndReturnNull();

    Py_RETURN_NONE;
}


//-----------------------------------------------------------------------------
// cxoLob_fileExists()
//   Return a boolean indicating if the BFIILE lob exists.
//-----------------------------------------------------------------------------
static PyObject *cxoLob_fileExists(cxoLob *lob, PyObject *args)
{
    int status, exists;

    Py_BEGIN_ALLOW_THREADS
    status = dpiLob_getFileExists(lob->handle, &exists);
    Py_END_ALLOW_THREADS
    if (status < 0)
        return cxoError_raiseAndReturnNull();
    if (exists)
        Py_RETURN_TRUE;
    Py_RETURN_FALSE;
}