summaryrefslogtreecommitdiff
path: root/src/blob.c
blob: b5ef5b208751b14912ef77c76169d976c0116efc (plain)
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
#include "blob.h"
#include "util.h"


int pysqlite_blob_init(pysqlite_Blob *self, pysqlite_Connection* connection, sqlite3_blob *blob)
{
    Py_INCREF(connection);
    self->connection = connection;
    self->offset=0;
    self->blob = blob;
    self->in_weakreflist = NULL;

    if (!pysqlite_check_thread(self->connection)){
        return -1;
    }
    return 0;
}

static void remove_blob_from_connection_blob_list(pysqlite_Blob* self)
{
    Py_ssize_t i;

    for(i=0;i<PyList_GET_SIZE(self->connection->blobs);i++)
    {
      if(PyWeakref_GetObject(PyList_GET_ITEM(self->connection->blobs, i))==(PyObject *)self)
        {
          PyList_SetSlice(self->connection->blobs, i, i+1, NULL);
          break;
        }
    }
}



static void pysqlite_blob_dealloc(pysqlite_Blob* self)
{
    /* close the blob */
    if (self->blob) {
        Py_BEGIN_ALLOW_THREADS
        sqlite3_blob_close(self->blob);
        Py_END_ALLOW_THREADS
    }

    // remove from connection weaklist
    remove_blob_from_connection_blob_list(self);
    if (self->in_weakreflist != NULL) {
        PyObject_ClearWeakRefs((PyObject*)self);
    }

    Py_XDECREF(self->connection);

    self->blob = NULL;

    self->ob_type->tp_free((PyObject*)self);
}


/*
 * Checks if a blob object is usable (i. e. not closed).
 *
 * 0 => error; 1 => ok
 */
int pysqlite_check_blob(pysqlite_Blob* blob)
{

    if (!blob->blob) {
        PyErr_SetString(pysqlite_ProgrammingError, "Cannot operate on a closed blob.");
        return 0;
    } else if (!pysqlite_check_connection(blob->connection) || !pysqlite_check_thread(blob->connection)) {
        return 0;
    } else {
        return 1;
    }
}


PyObject* pysqlite_blob_close(pysqlite_Blob *self){
    if (!pysqlite_check_blob(self)){
        return NULL;
    }
    /* close the blob */
    if (self->blob) {
        Py_BEGIN_ALLOW_THREADS
        sqlite3_blob_close(self->blob);
        Py_END_ALLOW_THREADS
    }

    self->blob = NULL;

    // remove from connection weaklist
    remove_blob_from_connection_blob_list(self);
    if (self->in_weakreflist != NULL) {
        PyObject_ClearWeakRefs((PyObject*)self);
    }

    Py_RETURN_NONE;
};


PyObject* pysqlite_blob_length(pysqlite_Blob *self){
    int blob_length;
    if (!pysqlite_check_blob(self)){
        return NULL;
    }
    Py_BEGIN_ALLOW_THREADS
    blob_length = sqlite3_blob_bytes(self->blob);
    Py_END_ALLOW_THREADS

    return PyInt_FromLong(blob_length);
};


PyObject* pysqlite_blob_read(pysqlite_Blob *self, PyObject *args){
    int read_length = -1;
    int blob_length = 0;
    PyObject* buffer;
    char* raw_buffer;
    int rc;

    if (!PyArg_ParseTuple(args, "|i", &read_length)) {
        return NULL;
    }

    if (!pysqlite_check_blob(self)){
        return NULL;
    }


    //TODO: make this multithreaded and safe!
    Py_BEGIN_ALLOW_THREADS
    blob_length = sqlite3_blob_bytes(self->blob);
    Py_END_ALLOW_THREADS

    if (read_length < 0) {
        // same as file read.
        read_length = blob_length;
    }
    
    // making sure we don't read more then blob size
    if (self->offset + read_length > blob_length){
        read_length = blob_length - self->offset;
    }

    buffer = PyBytes_FromStringAndSize(NULL, read_length);
    if (!buffer) {
        return NULL;
    }
    raw_buffer = PyBytes_AS_STRING(buffer);

    Py_BEGIN_ALLOW_THREADS
    rc = sqlite3_blob_read(self->blob, raw_buffer, read_length, self->offset);
    Py_END_ALLOW_THREADS

    if (rc != SQLITE_OK){
        Py_DECREF(buffer);
        // For some reasone after modifieng blob the error is not set on the connection db.
        if (rc == SQLITE_ABORT){
            PyErr_SetString(pysqlite_OperationalError, "Cannot operate on modified blob");
        } else {
            _pysqlite_seterror(self->connection->db, NULL);
        }
        return NULL;
    }

    // update offset.
    self->offset += read_length;
    
    return buffer;
};


PyObject* pysqlite_blob_write(pysqlite_Blob *self, PyObject *data){
    Py_ssize_t data_size;
    char *data_buffer;
    int rc;

    if (PyBytes_AsStringAndSize(data, &data_buffer, &data_size)){
        return NULL;
    }

    if (!pysqlite_check_blob(self)){
        return NULL;
    }

    //TODO: throw better error on data bigger then blob.

    Py_BEGIN_ALLOW_THREADS
    rc = sqlite3_blob_write(self->blob, data_buffer, data_size, self->offset);
    Py_END_ALLOW_THREADS
    if (rc != SQLITE_OK) {
        // For some reasone after modifieng blob the error is not set on the connection db.
        if (rc == SQLITE_ABORT){
            PyErr_SetString(pysqlite_OperationalError, "Cannot operate on modified blob");
        } else {
            _pysqlite_seterror(self->connection->db, NULL);
        }
        return NULL;
    }

    self->offset += (int)data_size;
    Py_RETURN_NONE;
}


PyObject* pysqlite_blob_seek(pysqlite_Blob *self, PyObject *args){
    int blob_length, offset, from_what=0;

    if(!PyArg_ParseTuple(args, "i|i", &offset, &from_what)){
        return NULL;
    }


    if (!pysqlite_check_blob(self)){
        return NULL;
    }

    Py_BEGIN_ALLOW_THREADS
    blob_length = sqlite3_blob_bytes(self->blob);
    Py_END_ALLOW_THREADS

    switch(from_what){
        case 0: //realtive to blob begin
            break;
        case 1: //realtive to current position
            offset = self->offset + offset;
            break;
        case 2: //realtive to blob end
            offset = blob_length + offset;
            break;
        default:
            return PyErr_Format(PyExc_ValueError, "from_what should be 0, 1 or 2");
    }

    if (offset < 0 || offset > blob_length){
        return PyErr_Format(PyExc_ValueError, "offset out of blob range");
    }

    self->offset = offset;
    Py_RETURN_NONE;
};


PyObject* pysqlite_blob_tell(pysqlite_Blob *self){
    if (!pysqlite_check_blob(self)){
        return NULL;
    }

    return PyInt_FromLong(self->offset);
}


PyObject* pysqlite_blob_enter(pysqlite_Blob *self){
    if (!pysqlite_check_blob(self)){
        return NULL;
    }

    Py_INCREF(self);
    return (PyObject *)self;
}


PyObject* pysqlite_blob_exit(pysqlite_Blob *self, PyObject *args){
    PyObject *res;
    if (!pysqlite_check_blob(self)){
        return NULL;
    }

    res = pysqlite_blob_close(self);
    Py_XDECREF(res);
    if (!res) {
        return NULL;
    }

    Py_RETURN_FALSE;
}


static PyMethodDef blob_methods[] = {
    {"length", (PyCFunction)pysqlite_blob_length, METH_NOARGS,
        PyDoc_STR("return blob length")},
    {"read", (PyCFunction)pysqlite_blob_read, METH_VARARGS,
        PyDoc_STR("read data from blob")},
    {"write", (PyCFunction)pysqlite_blob_write, METH_O,
        PyDoc_STR("write data to blob")},
    {"close", (PyCFunction)pysqlite_blob_close, METH_NOARGS,
        PyDoc_STR("close blob")},
    {"seek", (PyCFunction)pysqlite_blob_seek, METH_VARARGS,
        PyDoc_STR("change blob current offset")},
    {"tell", (PyCFunction)pysqlite_blob_tell, METH_NOARGS,
        PyDoc_STR("return blob current offset")},
    {"__enter__", (PyCFunction)pysqlite_blob_enter, METH_NOARGS,
        PyDoc_STR("blob context manager enter")},
    {"__exit__", (PyCFunction)pysqlite_blob_exit, METH_VARARGS,
        PyDoc_STR("blob context manager exit")},
    {NULL, NULL}
};


PyTypeObject pysqlite_BlobType = {
        PyVarObject_HEAD_INIT(NULL, 0)
        MODULE_NAME ".Blob",                            /* tp_name */
        sizeof(pysqlite_Blob),                          /* tp_basicsize */
        0,                                              /* tp_itemsize */
        (destructor)pysqlite_blob_dealloc,              /* 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 */
        0,                                              /* tp_str */
        0,                                              /* tp_getattro */
        0,                                              /* tp_setattro */
        0,                                              /* tp_as_buffer */
        Py_TPFLAGS_DEFAULT|Py_TPFLAGS_HAVE_WEAKREFS,    /* tp_flags */
        0,                                              /* tp_doc */
        0,                                              /* tp_traverse */
        0,                                              /* tp_clear */
        0,                                              /* tp_richcompare */
        offsetof(pysqlite_Blob, in_weakreflist),        /* tp_weaklistoffset */
        0,                                              /* tp_iter */
        0,                                              /* tp_iternext */
        blob_methods,                                   /* 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 */
        (initproc)pysqlite_blob_init,                   /* tp_init */
        0,                                              /* tp_alloc */
        0,                                              /* tp_new */
        0                                               /* tp_free */
};

extern int pysqlite_blob_setup_types(void)
{
    pysqlite_BlobType.tp_new = PyType_GenericNew;
    return PyType_Ready(&pysqlite_BlobType);
}