/Users/erlendaasland/src/cpython.git/Modules/_sqlite/connection.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* connection.c - the connection type |
2 | | * |
3 | | * Copyright (C) 2004-2010 Gerhard Häring <gh@ghaering.de> |
4 | | * |
5 | | * This file is part of pysqlite. |
6 | | * |
7 | | * This software is provided 'as-is', without any express or implied |
8 | | * warranty. In no event will the authors be held liable for any damages |
9 | | * arising from the use of this software. |
10 | | * |
11 | | * Permission is granted to anyone to use this software for any purpose, |
12 | | * including commercial applications, and to alter it and redistribute it |
13 | | * freely, subject to the following restrictions: |
14 | | * |
15 | | * 1. The origin of this software must not be misrepresented; you must not |
16 | | * claim that you wrote the original software. If you use this software |
17 | | * in a product, an acknowledgment in the product documentation would be |
18 | | * appreciated but is not required. |
19 | | * 2. Altered source versions must be plainly marked as such, and must not be |
20 | | * misrepresented as being the original software. |
21 | | * 3. This notice may not be removed or altered from any source distribution. |
22 | | */ |
23 | | |
24 | | #include "module.h" |
25 | | #include "structmember.h" // PyMemberDef |
26 | | #include "connection.h" |
27 | | #include "statement.h" |
28 | | #include "cursor.h" |
29 | | #include "blob.h" |
30 | | #include "prepare_protocol.h" |
31 | | #include "util.h" |
32 | | |
33 | | #if SQLITE_VERSION_NUMBER >= 3014000 |
34 | | #define HAVE_TRACE_V2 |
35 | | #endif |
36 | | |
37 | | #if SQLITE_VERSION_NUMBER >= 3025000 |
38 | | #define HAVE_WINDOW_FUNCTIONS |
39 | | #endif |
40 | | |
41 | | static const char * |
42 | | get_isolation_level(const char *level) |
43 | 532 | { |
44 | 532 | assert(level != NULL); |
45 | 0 | static const char *const allowed_levels[] = { |
46 | 532 | "", |
47 | 532 | "DEFERRED", |
48 | 532 | "IMMEDIATE", |
49 | 532 | "EXCLUSIVE", |
50 | 532 | NULL |
51 | 532 | }; |
52 | 654 | for (int i = 0; allowed_levels[i] != NULL; i++) { |
53 | 636 | const char *candidate = allowed_levels[i]; |
54 | 636 | if (sqlite3_stricmp(level, candidate) == 0) { |
55 | 514 | return candidate; |
56 | 514 | } |
57 | 636 | } |
58 | 18 | PyErr_SetString(PyExc_ValueError, |
59 | 18 | "isolation_level string must be '', 'DEFERRED', " |
60 | 18 | "'IMMEDIATE', or 'EXCLUSIVE'"); |
61 | 18 | return NULL; |
62 | 532 | } |
63 | | |
64 | | static int |
65 | | isolation_level_converter(PyObject *str_or_none, const char **result) |
66 | 540 | { |
67 | 540 | if (Py_IsNone(str_or_none)) { |
68 | 4 | *result = NULL; |
69 | 4 | } |
70 | 536 | else if (PyUnicode_Check(str_or_none)) { |
71 | 533 | Py_ssize_t sz; |
72 | 533 | const char *str = PyUnicode_AsUTF8AndSize(str_or_none, &sz); |
73 | 533 | if (str == NULL) { |
74 | 0 | return 0; |
75 | 0 | } |
76 | 533 | if (strlen(str) != (size_t)sz) { |
77 | 1 | PyErr_SetString(PyExc_ValueError, "embedded null character"); |
78 | 1 | return 0; |
79 | 1 | } |
80 | | |
81 | 532 | const char *level = get_isolation_level(str); |
82 | 532 | if (level == NULL) { |
83 | 18 | return 0; |
84 | 18 | } |
85 | 514 | *result = level; |
86 | 514 | } |
87 | 3 | else { |
88 | 3 | PyErr_SetString(PyExc_TypeError, |
89 | 3 | "isolation_level must be str or None"); |
90 | 3 | return 0; |
91 | 3 | } |
92 | 518 | return 1; |
93 | 540 | } |
94 | | |
95 | 501 | #define clinic_state() (pysqlite_get_state_by_type(Py_TYPE(self))) |
96 | | #include "clinic/connection.c.h" |
97 | | #undef clinic_state |
98 | | |
99 | | /*[clinic input] |
100 | | module _sqlite3 |
101 | | class _sqlite3.Connection "pysqlite_Connection *" "clinic_state()->ConnectionType" |
102 | | [clinic start generated code]*/ |
103 | | /*[clinic end generated code: output=da39a3ee5e6b4b0d input=67369db2faf80891]*/ |
104 | | |
105 | | static void _pysqlite_drop_unused_cursor_references(pysqlite_Connection* self); |
106 | | static void free_callback_context(callback_context *ctx); |
107 | | static void set_callback_context(callback_context **ctx_pp, |
108 | | callback_context *ctx); |
109 | | static void connection_close(pysqlite_Connection *self); |
110 | | PyObject *_pysqlite_query_execute(pysqlite_Cursor *, int, PyObject *, PyObject *); |
111 | | |
112 | | static PyObject * |
113 | | new_statement_cache(pysqlite_Connection *self, pysqlite_state *state, |
114 | | int maxsize) |
115 | 486 | { |
116 | 486 | PyObject *args[] = { NULL, PyLong_FromLong(maxsize), }; |
117 | 486 | if (args[1] == NULL) { |
118 | 0 | return NULL; |
119 | 0 | } |
120 | 486 | PyObject *lru_cache = state->lru_cache; |
121 | 486 | size_t nargsf = 1 | PY_VECTORCALL_ARGUMENTS_OFFSET; |
122 | 486 | PyObject *inner = PyObject_Vectorcall(lru_cache, args + 1, nargsf, NULL); |
123 | 486 | Py_DECREF(args[1]); |
124 | 486 | if (inner == NULL) { |
125 | 0 | return NULL; |
126 | 0 | } |
127 | | |
128 | 486 | args[1] = (PyObject *)self; // Borrowed ref. |
129 | 486 | nargsf = 1 | PY_VECTORCALL_ARGUMENTS_OFFSET; |
130 | 486 | PyObject *res = PyObject_Vectorcall(inner, args + 1, nargsf, NULL); |
131 | 486 | Py_DECREF(inner); |
132 | 486 | return res; |
133 | 486 | } |
134 | | |
135 | | /*[python input] |
136 | | class IsolationLevel_converter(CConverter): |
137 | | type = "const char *" |
138 | | converter = "isolation_level_converter" |
139 | | |
140 | | [python start generated code]*/ |
141 | | /*[python end generated code: output=da39a3ee5e6b4b0d input=cbcfe85b253061c2]*/ |
142 | | |
143 | | /*[clinic input] |
144 | | _sqlite3.Connection.__init__ as pysqlite_connection_init |
145 | | |
146 | | database: object |
147 | | timeout: double = 5.0 |
148 | | detect_types: int = 0 |
149 | | isolation_level: IsolationLevel = "" |
150 | | check_same_thread: bool(accept={int}) = True |
151 | | factory: object(c_default='(PyObject*)clinic_state()->ConnectionType') = ConnectionType |
152 | | cached_statements as cache_size: int = 128 |
153 | | uri: bool = False |
154 | | [clinic start generated code]*/ |
155 | | |
156 | | static int |
157 | | pysqlite_connection_init_impl(pysqlite_Connection *self, PyObject *database, |
158 | | double timeout, int detect_types, |
159 | | const char *isolation_level, |
160 | | int check_same_thread, PyObject *factory, |
161 | | int cache_size, int uri) |
162 | | /*[clinic end generated code: output=839eb2fee4293bda input=b8ce63dc6f70a383]*/ |
163 | 490 | { |
164 | 490 | if (PySys_Audit("sqlite3.connect", "O", database) < 0) { |
165 | 0 | return -1; |
166 | 0 | } |
167 | | |
168 | 490 | PyObject *bytes; |
169 | 490 | if (!PyUnicode_FSConverter(database, &bytes)) { |
170 | 0 | return -1; |
171 | 0 | } |
172 | | |
173 | 490 | if (self->initialized) { |
174 | 2 | PyTypeObject *tp = Py_TYPE(self); |
175 | 2 | tp->tp_clear((PyObject *)self); |
176 | 2 | connection_close(self); |
177 | 2 | self->initialized = 0; |
178 | 2 | } |
179 | | |
180 | | // Create and configure SQLite database object. |
181 | 490 | sqlite3 *db; |
182 | 490 | int rc; |
183 | 490 | Py_BEGIN_ALLOW_THREADS |
184 | 490 | rc = sqlite3_open_v2(PyBytes_AS_STRING(bytes), &db, |
185 | 490 | SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE | |
186 | 490 | (uri ? SQLITE_OPEN_URI : 0), NULL); |
187 | 490 | if (rc == SQLITE_OK) { |
188 | 486 | (void)sqlite3_busy_timeout(db, (int)(timeout*1000)); |
189 | 486 | } |
190 | 490 | Py_END_ALLOW_THREADS |
191 | | |
192 | 490 | Py_DECREF(bytes); |
193 | 490 | if (db == NULL && rc == SQLITE_NOMEM) { |
194 | 0 | PyErr_NoMemory(); |
195 | 0 | return -1; |
196 | 0 | } |
197 | | |
198 | 490 | pysqlite_state *state = pysqlite_get_state_by_type(Py_TYPE(self)); |
199 | 490 | if (rc != SQLITE_OK) { |
200 | 4 | _pysqlite_seterror(state, db); |
201 | 4 | goto error; |
202 | 4 | } |
203 | | |
204 | | // Create LRU statement cache; returns a new reference. |
205 | 486 | PyObject *statement_cache = new_statement_cache(self, state, cache_size); |
206 | 486 | if (statement_cache == NULL) { |
207 | 0 | goto error; |
208 | 0 | } |
209 | | |
210 | | /* Create lists of weak references to cursors and blobs */ |
211 | 486 | PyObject *cursors = PyList_New(0); |
212 | 486 | if (cursors == NULL) { |
213 | 0 | Py_DECREF(statement_cache); |
214 | 0 | goto error; |
215 | 0 | } |
216 | | |
217 | 486 | PyObject *blobs = PyList_New(0); |
218 | 486 | if (blobs == NULL) { |
219 | 0 | Py_DECREF(statement_cache); |
220 | 0 | Py_DECREF(cursors); |
221 | 0 | goto error; |
222 | 0 | } |
223 | | |
224 | | // Init connection state members. |
225 | 486 | self->db = db; |
226 | 486 | self->state = state; |
227 | 486 | self->detect_types = detect_types; |
228 | 486 | self->isolation_level = isolation_level; |
229 | 486 | self->check_same_thread = check_same_thread; |
230 | 486 | self->thread_ident = PyThread_get_thread_ident(); |
231 | 486 | self->statement_cache = statement_cache; |
232 | 486 | self->cursors = cursors; |
233 | 486 | self->blobs = blobs; |
234 | 486 | self->created_cursors = 0; |
235 | 486 | self->row_factory = Py_NewRef(Py_None); |
236 | 486 | self->text_factory = Py_NewRef(&PyUnicode_Type); |
237 | 486 | self->trace_ctx = NULL; |
238 | 486 | self->progress_ctx = NULL; |
239 | 486 | self->authorizer_ctx = NULL; |
240 | | |
241 | | // Borrowed refs |
242 | 486 | self->Warning = state->Warning; |
243 | 486 | self->Error = state->Error; |
244 | 486 | self->InterfaceError = state->InterfaceError; |
245 | 486 | self->DatabaseError = state->DatabaseError; |
246 | 486 | self->DataError = state->DataError; |
247 | 486 | self->OperationalError = state->OperationalError; |
248 | 486 | self->IntegrityError = state->IntegrityError; |
249 | 486 | self->InternalError = state->InternalError; |
250 | 486 | self->ProgrammingError = state->ProgrammingError; |
251 | 486 | self->NotSupportedError = state->NotSupportedError; |
252 | | |
253 | 486 | if (PySys_Audit("sqlite3.connect/handle", "O", self) < 0) { |
254 | 0 | return -1; // Don't goto error; at this point, dealloc will clean up. |
255 | 0 | } |
256 | | |
257 | 486 | self->initialized = 1; |
258 | 486 | return 0; |
259 | | |
260 | 4 | error: |
261 | | // There are no statements or other SQLite objects attached to the |
262 | | // database, so sqlite3_close() should always return SQLITE_OK. |
263 | 4 | rc = sqlite3_close(db); |
264 | 4 | assert(rc == SQLITE_OK); |
265 | 0 | return -1; |
266 | 486 | } |
267 | | |
268 | 762 | #define VISIT_CALLBACK_CONTEXT(ctx) \ |
269 | 762 | do { \ |
270 | 762 | if (ctx) { \ |
271 | 34 | Py_VISIT(ctx->callable); \ |
272 | 34 | Py_VISIT(ctx->module); \ |
273 | 34 | } \ |
274 | 762 | } while (0) |
275 | | |
276 | | static int |
277 | | connection_traverse(pysqlite_Connection *self, visitproc visit, void *arg) |
278 | 254 | { |
279 | 254 | Py_VISIT(Py_TYPE(self)); |
280 | 254 | Py_VISIT(self->statement_cache); |
281 | 254 | Py_VISIT(self->cursors); |
282 | 254 | Py_VISIT(self->blobs); |
283 | 254 | Py_VISIT(self->row_factory); |
284 | 254 | Py_VISIT(self->text_factory); |
285 | 254 | VISIT_CALLBACK_CONTEXT(self->trace_ctx); |
286 | 254 | VISIT_CALLBACK_CONTEXT(self->progress_ctx); |
287 | 254 | VISIT_CALLBACK_CONTEXT(self->authorizer_ctx); |
288 | 254 | #undef VISIT_CALLBACK_CONTEXT |
289 | 254 | return 0; |
290 | 254 | } |
291 | | |
292 | | static inline void |
293 | | clear_callback_context(callback_context *ctx) |
294 | 1.80k | { |
295 | 1.80k | if (ctx != NULL) { |
296 | 34 | Py_CLEAR(ctx->callable); |
297 | 34 | Py_CLEAR(ctx->module); |
298 | 34 | } |
299 | 1.80k | } |
300 | | |
301 | | static int |
302 | | connection_clear(pysqlite_Connection *self) |
303 | 600 | { |
304 | 600 | Py_CLEAR(self->statement_cache); |
305 | 600 | Py_CLEAR(self->cursors); |
306 | 600 | Py_CLEAR(self->blobs); |
307 | 600 | Py_CLEAR(self->row_factory); |
308 | 600 | Py_CLEAR(self->text_factory); |
309 | 600 | clear_callback_context(self->trace_ctx); |
310 | 600 | clear_callback_context(self->progress_ctx); |
311 | 600 | clear_callback_context(self->authorizer_ctx); |
312 | 600 | return 0; |
313 | 600 | } |
314 | | |
315 | | static void |
316 | | free_callback_contexts(pysqlite_Connection *self) |
317 | 486 | { |
318 | 486 | set_callback_context(&self->trace_ctx, NULL); |
319 | 486 | set_callback_context(&self->progress_ctx, NULL); |
320 | 486 | set_callback_context(&self->authorizer_ctx, NULL); |
321 | 486 | } |
322 | | |
323 | | static void |
324 | | connection_close(pysqlite_Connection *self) |
325 | 890 | { |
326 | 890 | if (self->db) { |
327 | 486 | free_callback_contexts(self); |
328 | | |
329 | 486 | sqlite3 *db = self->db; |
330 | 486 | self->db = NULL; |
331 | | |
332 | 486 | Py_BEGIN_ALLOW_THREADS |
333 | 486 | int rc = sqlite3_close_v2(db); |
334 | 486 | assert(rc == SQLITE_OK), (void)rc; |
335 | 486 | Py_END_ALLOW_THREADS |
336 | 486 | } |
337 | 890 | } |
338 | | |
339 | | static void |
340 | | connection_dealloc(pysqlite_Connection *self) |
341 | 500 | { |
342 | 500 | PyTypeObject *tp = Py_TYPE(self); |
343 | 500 | PyObject_GC_UnTrack(self); |
344 | 500 | tp->tp_clear((PyObject *)self); |
345 | | |
346 | | /* Clean up if user has not called .close() explicitly. */ |
347 | 500 | connection_close(self); |
348 | | |
349 | 500 | tp->tp_free(self); |
350 | 500 | Py_DECREF(tp); |
351 | 500 | } |
352 | | |
353 | | /*[clinic input] |
354 | | _sqlite3.Connection.cursor as pysqlite_connection_cursor |
355 | | |
356 | | factory: object = NULL |
357 | | |
358 | | Return a cursor for the connection. |
359 | | [clinic start generated code]*/ |
360 | | |
361 | | static PyObject * |
362 | | pysqlite_connection_cursor_impl(pysqlite_Connection *self, PyObject *factory) |
363 | | /*[clinic end generated code: output=562432a9e6af2aa1 input=4127345aa091b650]*/ |
364 | 1.30k | { |
365 | 1.30k | PyObject* cursor; |
366 | | |
367 | 1.30k | if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) { |
368 | 9 | return NULL; |
369 | 9 | } |
370 | | |
371 | 1.30k | if (factory == NULL) { |
372 | 1.29k | factory = (PyObject *)self->state->CursorType; |
373 | 1.29k | } |
374 | | |
375 | 1.30k | cursor = PyObject_CallOneArg(factory, (PyObject *)self); |
376 | 1.30k | if (cursor == NULL) |
377 | 3 | return NULL; |
378 | 1.29k | if (!PyObject_TypeCheck(cursor, self->state->CursorType)) { |
379 | 2 | PyErr_Format(PyExc_TypeError, |
380 | 2 | "factory must return a cursor, not %.100s", |
381 | 2 | Py_TYPE(cursor)->tp_name); |
382 | 2 | Py_DECREF(cursor); |
383 | 2 | return NULL; |
384 | 2 | } |
385 | | |
386 | 1.29k | _pysqlite_drop_unused_cursor_references(self); |
387 | | |
388 | 1.29k | if (cursor && self->row_factory != Py_None) { |
389 | 15 | Py_INCREF(self->row_factory); |
390 | 15 | Py_XSETREF(((pysqlite_Cursor *)cursor)->row_factory, self->row_factory); |
391 | 15 | } |
392 | | |
393 | 1.29k | return cursor; |
394 | 1.29k | } |
395 | | |
396 | | /*[clinic input] |
397 | | _sqlite3.Connection.blobopen as blobopen |
398 | | |
399 | | table: str |
400 | | Table name. |
401 | | column as col: str |
402 | | Column name. |
403 | | row: int |
404 | | Row index. |
405 | | / |
406 | | * |
407 | | readonly: bool(accept={int}) = False |
408 | | Open the BLOB without write permissions. |
409 | | name: str = "main" |
410 | | Database name. |
411 | | |
412 | | Open and return a BLOB object. |
413 | | [clinic start generated code]*/ |
414 | | |
415 | | static PyObject * |
416 | | blobopen_impl(pysqlite_Connection *self, const char *table, const char *col, |
417 | | int row, int readonly, const char *name) |
418 | | /*[clinic end generated code: output=0c8e2e58516d0b5c input=1e7052516acfc94d]*/ |
419 | 47 | { |
420 | 47 | if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) { |
421 | 1 | return NULL; |
422 | 1 | } |
423 | | |
424 | 46 | int rc; |
425 | 46 | sqlite3_blob *blob; |
426 | | |
427 | 46 | Py_BEGIN_ALLOW_THREADS |
428 | 46 | rc = sqlite3_blob_open(self->db, name, table, col, row, !readonly, &blob); |
429 | 46 | Py_END_ALLOW_THREADS |
430 | | |
431 | 46 | if (rc == SQLITE_MISUSE) { |
432 | 0 | PyErr_Format(self->state->InterfaceError, sqlite3_errstr(rc)); |
433 | 0 | return NULL; |
434 | 0 | } |
435 | 46 | else if (rc != SQLITE_OK) { |
436 | 4 | _pysqlite_seterror(self->state, self->db); |
437 | 4 | return NULL; |
438 | 4 | } |
439 | | |
440 | 42 | pysqlite_Blob *obj = PyObject_GC_New(pysqlite_Blob, self->state->BlobType); |
441 | 42 | if (obj == NULL) { |
442 | 0 | goto error; |
443 | 0 | } |
444 | | |
445 | 42 | obj->connection = (pysqlite_Connection *)Py_NewRef(self); |
446 | 42 | obj->blob = blob; |
447 | 42 | obj->offset = 0; |
448 | 42 | obj->in_weakreflist = NULL; |
449 | | |
450 | 42 | PyObject_GC_Track(obj); |
451 | | |
452 | | // Add our blob to connection blobs list |
453 | 42 | PyObject *weakref = PyWeakref_NewRef((PyObject *)obj, NULL); |
454 | 42 | if (weakref == NULL) { |
455 | 0 | goto error; |
456 | 0 | } |
457 | 42 | rc = PyList_Append(self->blobs, weakref); |
458 | 42 | Py_DECREF(weakref); |
459 | 42 | if (rc < 0) { |
460 | 0 | goto error; |
461 | 0 | } |
462 | | |
463 | 42 | return (PyObject *)obj; |
464 | | |
465 | 0 | error: |
466 | 0 | Py_XDECREF(obj); |
467 | 0 | return NULL; |
468 | 42 | } |
469 | | |
470 | | /*[clinic input] |
471 | | _sqlite3.Connection.close as pysqlite_connection_close |
472 | | |
473 | | Close the database connection. |
474 | | |
475 | | Any pending transaction is not committed implicitly. |
476 | | [clinic start generated code]*/ |
477 | | |
478 | | static PyObject * |
479 | | pysqlite_connection_close_impl(pysqlite_Connection *self) |
480 | | /*[clinic end generated code: output=a546a0da212c9b97 input=b3ed5b74f6fefc06]*/ |
481 | 390 | { |
482 | 390 | if (!pysqlite_check_thread(self)) { |
483 | 1 | return NULL; |
484 | 1 | } |
485 | | |
486 | 389 | if (!self->initialized) { |
487 | 1 | PyTypeObject *tp = Py_TYPE(self); |
488 | 1 | pysqlite_state *state = pysqlite_get_state_by_type(tp); |
489 | 1 | PyErr_SetString(state->ProgrammingError, |
490 | 1 | "Base Connection.__init__ not called."); |
491 | 1 | return NULL; |
492 | 1 | } |
493 | | |
494 | 388 | pysqlite_close_all_blobs(self); |
495 | 388 | Py_CLEAR(self->statement_cache); |
496 | 388 | connection_close(self); |
497 | | |
498 | 388 | Py_RETURN_NONE; |
499 | 389 | } |
500 | | |
501 | | /* |
502 | | * Checks if a connection object is usable (i. e. not closed). |
503 | | * |
504 | | * 0 => error; 1 => ok |
505 | | */ |
506 | | int pysqlite_check_connection(pysqlite_Connection* con) |
507 | 6.82k | { |
508 | 6.82k | if (!con->initialized) { |
509 | 7 | pysqlite_state *state = pysqlite_get_state_by_type(Py_TYPE(con)); |
510 | 7 | PyErr_SetString(state->ProgrammingError, |
511 | 7 | "Base Connection.__init__ not called."); |
512 | 7 | return 0; |
513 | 7 | } |
514 | | |
515 | 6.81k | if (!con->db) { |
516 | 23 | PyErr_SetString(con->state->ProgrammingError, |
517 | 23 | "Cannot operate on a closed database."); |
518 | 23 | return 0; |
519 | 6.79k | } else { |
520 | 6.79k | return 1; |
521 | 6.79k | } |
522 | 6.81k | } |
523 | | |
524 | | /*[clinic input] |
525 | | _sqlite3.Connection.commit as pysqlite_connection_commit |
526 | | |
527 | | Commit any pending transaction to the database. |
528 | | |
529 | | If there is no open transaction, this method is a no-op. |
530 | | [clinic start generated code]*/ |
531 | | |
532 | | static PyObject * |
533 | | pysqlite_connection_commit_impl(pysqlite_Connection *self) |
534 | | /*[clinic end generated code: output=3da45579e89407f2 input=c8793c97c3446065]*/ |
535 | 91 | { |
536 | 91 | if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) { |
537 | 2 | return NULL; |
538 | 2 | } |
539 | | |
540 | 89 | if (!sqlite3_get_autocommit(self->db)) { |
541 | 65 | int rc; |
542 | | |
543 | 65 | Py_BEGIN_ALLOW_THREADS |
544 | 65 | sqlite3_stmt *statement; |
545 | 65 | rc = sqlite3_prepare_v2(self->db, "COMMIT", 7, &statement, NULL); |
546 | 65 | if (rc == SQLITE_OK) { |
547 | 65 | (void)sqlite3_step(statement); |
548 | 65 | rc = sqlite3_finalize(statement); |
549 | 65 | } |
550 | 65 | Py_END_ALLOW_THREADS |
551 | | |
552 | 65 | if (rc != SQLITE_OK) { |
553 | 1 | (void)_pysqlite_seterror(self->state, self->db); |
554 | 1 | return NULL; |
555 | 1 | } |
556 | 65 | } |
557 | | |
558 | 89 | Py_RETURN_NONE; |
559 | 89 | } |
560 | | |
561 | | /*[clinic input] |
562 | | _sqlite3.Connection.rollback as pysqlite_connection_rollback |
563 | | |
564 | | Roll back to the start of any pending transaction. |
565 | | |
566 | | If there is no open transaction, this method is a no-op. |
567 | | [clinic start generated code]*/ |
568 | | |
569 | | static PyObject * |
570 | | pysqlite_connection_rollback_impl(pysqlite_Connection *self) |
571 | | /*[clinic end generated code: output=b66fa0d43e7ef305 input=7f60a2f1076f16b3]*/ |
572 | 18 | { |
573 | 18 | if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) { |
574 | 2 | return NULL; |
575 | 2 | } |
576 | | |
577 | 16 | if (!sqlite3_get_autocommit(self->db)) { |
578 | 10 | int rc; |
579 | | |
580 | 10 | Py_BEGIN_ALLOW_THREADS |
581 | 10 | sqlite3_stmt *statement; |
582 | 10 | rc = sqlite3_prepare_v2(self->db, "ROLLBACK", 9, &statement, NULL); |
583 | 10 | if (rc == SQLITE_OK) { |
584 | 10 | (void)sqlite3_step(statement); |
585 | 10 | rc = sqlite3_finalize(statement); |
586 | 10 | } |
587 | 10 | Py_END_ALLOW_THREADS |
588 | | |
589 | 10 | if (rc != SQLITE_OK) { |
590 | 0 | (void)_pysqlite_seterror(self->state, self->db); |
591 | 0 | return NULL; |
592 | 0 | } |
593 | | |
594 | 10 | } |
595 | | |
596 | 16 | Py_RETURN_NONE; |
597 | 16 | } |
598 | | |
599 | | static int |
600 | | _pysqlite_set_result(sqlite3_context* context, PyObject* py_val) |
601 | 111 | { |
602 | 111 | if (py_val == Py_None) { |
603 | 20 | sqlite3_result_null(context); |
604 | 91 | } else if (PyLong_Check(py_val)) { |
605 | 75 | sqlite_int64 value = _pysqlite_long_as_int64(py_val); |
606 | 75 | if (value == -1 && PyErr_Occurred()) |
607 | 7 | return -1; |
608 | 68 | sqlite3_result_int64(context, value); |
609 | 68 | } else if (PyFloat_Check(py_val)) { |
610 | 3 | double value = PyFloat_AsDouble(py_val); |
611 | 3 | if (value == -1 && PyErr_Occurred()) { |
612 | 0 | return -1; |
613 | 0 | } |
614 | 3 | sqlite3_result_double(context, value); |
615 | 13 | } else if (PyUnicode_Check(py_val)) { |
616 | 9 | Py_ssize_t sz; |
617 | 9 | const char *str = PyUnicode_AsUTF8AndSize(py_val, &sz); |
618 | 9 | if (str == NULL) { |
619 | 4 | return -1; |
620 | 4 | } |
621 | 5 | if (sz > INT_MAX) { |
622 | 0 | PyErr_SetString(PyExc_OverflowError, |
623 | 0 | "string is longer than INT_MAX bytes"); |
624 | 0 | return -1; |
625 | 0 | } |
626 | 5 | sqlite3_result_text(context, str, (int)sz, SQLITE_TRANSIENT); |
627 | 5 | } else if (PyObject_CheckBuffer(py_val)) { |
628 | 3 | Py_buffer view; |
629 | 3 | if (PyObject_GetBuffer(py_val, &view, PyBUF_SIMPLE) != 0) { |
630 | 2 | return -1; |
631 | 2 | } |
632 | 1 | if (view.len > INT_MAX) { |
633 | 0 | PyErr_SetString(PyExc_OverflowError, |
634 | 0 | "BLOB longer than INT_MAX bytes"); |
635 | 0 | PyBuffer_Release(&view); |
636 | 0 | return -1; |
637 | 0 | } |
638 | 1 | sqlite3_result_blob(context, view.buf, (int)view.len, SQLITE_TRANSIENT); |
639 | 1 | PyBuffer_Release(&view); |
640 | 1 | } else { |
641 | 1 | callback_context *ctx = (callback_context *)sqlite3_user_data(context); |
642 | 1 | PyErr_Format(ctx->state->ProgrammingError, |
643 | 1 | "User-defined functions cannot return '%s' values to " |
644 | 1 | "SQLite", |
645 | 1 | Py_TYPE(py_val)->tp_name); |
646 | 1 | return -1; |
647 | 1 | } |
648 | 97 | return 0; |
649 | 111 | } |
650 | | |
651 | | static PyObject * |
652 | | _pysqlite_build_py_params(sqlite3_context *context, int argc, |
653 | | sqlite3_value **argv) |
654 | 141 | { |
655 | 141 | PyObject* args; |
656 | 141 | int i; |
657 | 141 | sqlite3_value* cur_value; |
658 | 141 | PyObject* cur_py_value; |
659 | | |
660 | 141 | args = PyTuple_New(argc); |
661 | 141 | if (!args) { |
662 | 0 | return NULL; |
663 | 0 | } |
664 | | |
665 | 266 | for (i = 0; i < argc; i++) { |
666 | 125 | cur_value = argv[i]; |
667 | 125 | switch (sqlite3_value_type(argv[i])) { |
668 | 96 | case SQLITE_INTEGER: |
669 | 96 | cur_py_value = PyLong_FromLongLong(sqlite3_value_int64(cur_value)); |
670 | 96 | break; |
671 | 3 | case SQLITE_FLOAT: |
672 | 3 | cur_py_value = PyFloat_FromDouble(sqlite3_value_double(cur_value)); |
673 | 3 | break; |
674 | 18 | case SQLITE_TEXT: { |
675 | 18 | sqlite3 *db = sqlite3_context_db_handle(context); |
676 | 18 | const char *text = (const char *)sqlite3_value_text(cur_value); |
677 | | |
678 | 18 | if (text == NULL && sqlite3_errcode(db) == SQLITE_NOMEM) { |
679 | 0 | PyErr_NoMemory(); |
680 | 0 | goto error; |
681 | 0 | } |
682 | | |
683 | 18 | Py_ssize_t size = sqlite3_value_bytes(cur_value); |
684 | 18 | cur_py_value = PyUnicode_FromStringAndSize(text, size); |
685 | 18 | break; |
686 | 18 | } |
687 | 5 | case SQLITE_BLOB: { |
688 | 5 | sqlite3 *db = sqlite3_context_db_handle(context); |
689 | 5 | const void *blob = sqlite3_value_blob(cur_value); |
690 | | |
691 | 5 | if (blob == NULL && sqlite3_errcode(db) == SQLITE_NOMEM) { |
692 | 0 | PyErr_NoMemory(); |
693 | 0 | goto error; |
694 | 0 | } |
695 | | |
696 | 5 | Py_ssize_t size = sqlite3_value_bytes(cur_value); |
697 | 5 | cur_py_value = PyBytes_FromStringAndSize(blob, size); |
698 | 5 | break; |
699 | 5 | } |
700 | 3 | case SQLITE_NULL: |
701 | 3 | default: |
702 | 3 | cur_py_value = Py_NewRef(Py_None); |
703 | 125 | } |
704 | | |
705 | 125 | if (!cur_py_value) { |
706 | 0 | goto error; |
707 | 0 | } |
708 | | |
709 | 125 | PyTuple_SET_ITEM(args, i, cur_py_value); |
710 | 125 | } |
711 | | |
712 | 141 | return args; |
713 | | |
714 | 0 | error: |
715 | 0 | Py_DECREF(args); |
716 | 0 | return NULL; |
717 | 141 | } |
718 | | |
719 | | static void |
720 | | print_or_clear_traceback(callback_context *ctx) |
721 | 62 | { |
722 | 62 | assert(ctx != NULL); |
723 | 0 | assert(ctx->state != NULL); |
724 | 62 | if (ctx->state->enable_callback_tracebacks) { |
725 | 28 | PyErr_WriteUnraisable(ctx->callable); |
726 | 28 | } |
727 | 34 | else { |
728 | 34 | PyErr_Clear(); |
729 | 34 | } |
730 | 62 | } |
731 | | |
732 | | // Checks the Python exception and sets the appropriate SQLite error code. |
733 | | static void |
734 | | set_sqlite_error(sqlite3_context *context, const char *msg) |
735 | 48 | { |
736 | 48 | assert(PyErr_Occurred()); |
737 | 48 | if (PyErr_ExceptionMatches(PyExc_MemoryError)) { |
738 | 2 | sqlite3_result_error_nomem(context); |
739 | 2 | } |
740 | 46 | else if (PyErr_ExceptionMatches(PyExc_OverflowError)) { |
741 | 9 | sqlite3_result_error_toobig(context); |
742 | 9 | } |
743 | 37 | else { |
744 | 37 | sqlite3_result_error(context, msg, -1); |
745 | 37 | } |
746 | 48 | callback_context *ctx = (callback_context *)sqlite3_user_data(context); |
747 | 48 | print_or_clear_traceback(ctx); |
748 | 48 | } |
749 | | |
750 | | static void |
751 | | func_callback(sqlite3_context *context, int argc, sqlite3_value **argv) |
752 | 51 | { |
753 | 51 | PyGILState_STATE threadstate = PyGILState_Ensure(); |
754 | | |
755 | 51 | PyObject* args; |
756 | 51 | PyObject* py_retval = NULL; |
757 | 51 | int ok; |
758 | | |
759 | 51 | args = _pysqlite_build_py_params(context, argc, argv); |
760 | 51 | if (args) { |
761 | 51 | callback_context *ctx = (callback_context *)sqlite3_user_data(context); |
762 | 51 | assert(ctx != NULL); |
763 | 0 | py_retval = PyObject_CallObject(ctx->callable, args); |
764 | 51 | Py_DECREF(args); |
765 | 51 | } |
766 | | |
767 | 0 | ok = 0; |
768 | 51 | if (py_retval) { |
769 | 45 | ok = _pysqlite_set_result(context, py_retval) == 0; |
770 | 45 | Py_DECREF(py_retval); |
771 | 45 | } |
772 | 51 | if (!ok) { |
773 | 19 | set_sqlite_error(context, "user-defined function raised exception"); |
774 | 19 | } |
775 | | |
776 | 51 | PyGILState_Release(threadstate); |
777 | 51 | } |
778 | | |
779 | | static void |
780 | | step_callback(sqlite3_context *context, int argc, sqlite3_value **params) |
781 | 78 | { |
782 | 78 | PyGILState_STATE threadstate = PyGILState_Ensure(); |
783 | | |
784 | 78 | PyObject* args; |
785 | 78 | PyObject* function_result = NULL; |
786 | 78 | PyObject** aggregate_instance; |
787 | 78 | PyObject* stepmethod = NULL; |
788 | | |
789 | 78 | callback_context *ctx = (callback_context *)sqlite3_user_data(context); |
790 | 78 | assert(ctx != NULL); |
791 | | |
792 | 0 | aggregate_instance = (PyObject**)sqlite3_aggregate_context(context, sizeof(PyObject*)); |
793 | 78 | if (*aggregate_instance == NULL) { |
794 | 39 | *aggregate_instance = PyObject_CallNoArgs(ctx->callable); |
795 | 39 | if (!*aggregate_instance) { |
796 | 4 | set_sqlite_error(context, |
797 | 4 | "user-defined aggregate's '__init__' method raised error"); |
798 | 4 | goto error; |
799 | 4 | } |
800 | 39 | } |
801 | | |
802 | 74 | stepmethod = PyObject_GetAttr(*aggregate_instance, ctx->state->str_step); |
803 | 74 | if (!stepmethod) { |
804 | 4 | set_sqlite_error(context, |
805 | 4 | "user-defined aggregate's 'step' method not defined"); |
806 | 4 | goto error; |
807 | 4 | } |
808 | | |
809 | 70 | args = _pysqlite_build_py_params(context, argc, params); |
810 | 70 | if (!args) { |
811 | 0 | goto error; |
812 | 0 | } |
813 | | |
814 | 70 | function_result = PyObject_CallObject(stepmethod, args); |
815 | 70 | Py_DECREF(args); |
816 | | |
817 | 70 | if (!function_result) { |
818 | 4 | set_sqlite_error(context, |
819 | 4 | "user-defined aggregate's 'step' method raised error"); |
820 | 4 | } |
821 | | |
822 | 78 | error: |
823 | 78 | Py_XDECREF(stepmethod); |
824 | 78 | Py_XDECREF(function_result); |
825 | | |
826 | 78 | PyGILState_Release(threadstate); |
827 | 78 | } |
828 | | |
829 | | static void |
830 | | final_callback(sqlite3_context *context) |
831 | 40 | { |
832 | 40 | PyGILState_STATE threadstate = PyGILState_Ensure(); |
833 | | |
834 | 40 | PyObject* function_result; |
835 | 40 | PyObject** aggregate_instance; |
836 | 40 | int ok; |
837 | 40 | PyObject *exception, *value, *tb; |
838 | | |
839 | 40 | aggregate_instance = (PyObject**)sqlite3_aggregate_context(context, 0); |
840 | 40 | if (aggregate_instance == NULL) { |
841 | | /* No rows matched the query; the step handler was never called. */ |
842 | 1 | goto error; |
843 | 1 | } |
844 | 39 | else if (!*aggregate_instance) { |
845 | | /* this branch is executed if there was an exception in the aggregate's |
846 | | * __init__ */ |
847 | | |
848 | 4 | goto error; |
849 | 4 | } |
850 | | |
851 | | // Keep the exception (if any) of the last call to step, value, or inverse |
852 | 35 | PyErr_Fetch(&exception, &value, &tb); |
853 | | |
854 | 35 | callback_context *ctx = (callback_context *)sqlite3_user_data(context); |
855 | 35 | assert(ctx != NULL); |
856 | 0 | function_result = PyObject_CallMethodNoArgs(*aggregate_instance, |
857 | 35 | ctx->state->str_finalize); |
858 | 35 | Py_DECREF(*aggregate_instance); |
859 | | |
860 | 35 | ok = 0; |
861 | 35 | if (function_result) { |
862 | 27 | ok = _pysqlite_set_result(context, function_result) == 0; |
863 | 27 | Py_DECREF(function_result); |
864 | 27 | } |
865 | 35 | if (!ok) { |
866 | 8 | int attr_err = PyErr_ExceptionMatches(PyExc_AttributeError); |
867 | 8 | _PyErr_ChainExceptions(exception, value, tb); |
868 | | |
869 | | /* Note: contrary to the step, value, and inverse callbacks, SQLite |
870 | | * does _not_, as of SQLite 3.38.0, propagate errors to sqlite3_step() |
871 | | * from the finalize callback. This implies that execute*() will not |
872 | | * raise OperationalError, as it normally would. */ |
873 | 8 | set_sqlite_error(context, attr_err |
874 | 8 | ? "user-defined aggregate's 'finalize' method not defined" |
875 | 8 | : "user-defined aggregate's 'finalize' method raised error"); |
876 | 8 | } |
877 | 27 | else { |
878 | 27 | PyErr_Restore(exception, value, tb); |
879 | 27 | } |
880 | | |
881 | 40 | error: |
882 | 40 | PyGILState_Release(threadstate); |
883 | 40 | } |
884 | | |
885 | | static void _pysqlite_drop_unused_cursor_references(pysqlite_Connection* self) |
886 | 1.29k | { |
887 | 1.29k | PyObject* new_list; |
888 | 1.29k | PyObject* weakref; |
889 | 1.29k | int i; |
890 | | |
891 | | /* we only need to do this once in a while */ |
892 | 1.29k | if (self->created_cursors++ < 200) { |
893 | 1.29k | return; |
894 | 1.29k | } |
895 | | |
896 | 2 | self->created_cursors = 0; |
897 | | |
898 | 2 | new_list = PyList_New(0); |
899 | 2 | if (!new_list) { |
900 | 0 | return; |
901 | 0 | } |
902 | | |
903 | 406 | for (i = 0; i < PyList_Size(self->cursors); i++) { |
904 | 404 | weakref = PyList_GetItem(self->cursors, i); |
905 | 404 | if (PyWeakref_GetObject(weakref) != Py_None) { |
906 | 4 | if (PyList_Append(new_list, weakref) != 0) { |
907 | 0 | Py_DECREF(new_list); |
908 | 0 | return; |
909 | 0 | } |
910 | 4 | } |
911 | 404 | } |
912 | | |
913 | 2 | Py_SETREF(self->cursors, new_list); |
914 | 2 | } |
915 | | |
916 | | /* Allocate a UDF/callback context structure. In order to ensure that the state |
917 | | * pointer always outlives the callback context, we make sure it owns a |
918 | | * reference to the module itself. create_callback_context() is always called |
919 | | * from connection methods, so we use the defining class to fetch the module |
920 | | * pointer. |
921 | | */ |
922 | | static callback_context * |
923 | | create_callback_context(PyTypeObject *cls, PyObject *callable) |
924 | 784 | { |
925 | 784 | callback_context *ctx = PyMem_Malloc(sizeof(callback_context)); |
926 | 784 | if (ctx != NULL) { |
927 | 784 | PyObject *module = PyType_GetModule(cls); |
928 | 784 | ctx->callable = Py_NewRef(callable); |
929 | 784 | ctx->module = Py_NewRef(module); |
930 | 784 | ctx->state = pysqlite_get_state(module); |
931 | 784 | } |
932 | 784 | return ctx; |
933 | 784 | } |
934 | | |
935 | | static void |
936 | | free_callback_context(callback_context *ctx) |
937 | 784 | { |
938 | 784 | assert(ctx != NULL); |
939 | 784 | Py_XDECREF(ctx->callable); |
940 | 784 | Py_XDECREF(ctx->module); |
941 | 784 | PyMem_Free(ctx); |
942 | 784 | } |
943 | | |
944 | | static void |
945 | | set_callback_context(callback_context **ctx_pp, callback_context *ctx) |
946 | 1.52k | { |
947 | 1.52k | assert(ctx_pp != NULL); |
948 | 0 | callback_context *tmp = *ctx_pp; |
949 | 1.52k | *ctx_pp = ctx; |
950 | 1.52k | if (tmp != NULL) { |
951 | 50 | free_callback_context(tmp); |
952 | 50 | } |
953 | 1.52k | } |
954 | | |
955 | | static void |
956 | | destructor_callback(void *ctx) |
957 | 734 | { |
958 | 734 | if (ctx != NULL) { |
959 | | // This function may be called without the GIL held, so we need to |
960 | | // ensure that we destroy 'ctx' with the GIL held. |
961 | 734 | PyGILState_STATE gstate = PyGILState_Ensure(); |
962 | 734 | free_callback_context((callback_context *)ctx); |
963 | 734 | PyGILState_Release(gstate); |
964 | 734 | } |
965 | 734 | } |
966 | | |
967 | | /*[clinic input] |
968 | | _sqlite3.Connection.create_function as pysqlite_connection_create_function |
969 | | |
970 | | cls: defining_class |
971 | | / |
972 | | name: str |
973 | | narg: int |
974 | | func: object |
975 | | * |
976 | | deterministic: bool = False |
977 | | |
978 | | Creates a new function. |
979 | | [clinic start generated code]*/ |
980 | | |
981 | | static PyObject * |
982 | | pysqlite_connection_create_function_impl(pysqlite_Connection *self, |
983 | | PyTypeObject *cls, const char *name, |
984 | | int narg, PyObject *func, |
985 | | int deterministic) |
986 | | /*[clinic end generated code: output=8a811529287ad240 input=b3e8e1d8ddaffbef]*/ |
987 | 564 | { |
988 | 564 | int rc; |
989 | 564 | int flags = SQLITE_UTF8; |
990 | | |
991 | 564 | if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) { |
992 | 2 | return NULL; |
993 | 2 | } |
994 | | |
995 | 562 | if (deterministic) { |
996 | | #if SQLITE_VERSION_NUMBER < 3008003 |
997 | | PyErr_SetString(self->NotSupportedError, |
998 | | "deterministic=True requires SQLite 3.8.3 or higher"); |
999 | | return NULL; |
1000 | | #else |
1001 | 1 | if (sqlite3_libversion_number() < 3008003) { |
1002 | 0 | PyErr_SetString(self->NotSupportedError, |
1003 | 0 | "deterministic=True requires SQLite 3.8.3 or higher"); |
1004 | 0 | return NULL; |
1005 | 0 | } |
1006 | 1 | flags |= SQLITE_DETERMINISTIC; |
1007 | 1 | #endif |
1008 | 1 | } |
1009 | 562 | callback_context *ctx = create_callback_context(cls, func); |
1010 | 562 | if (ctx == NULL) { |
1011 | 0 | return NULL; |
1012 | 0 | } |
1013 | 562 | rc = sqlite3_create_function_v2(self->db, name, narg, flags, ctx, |
1014 | 562 | func_callback, |
1015 | 562 | NULL, |
1016 | 562 | NULL, |
1017 | 562 | &destructor_callback); // will decref func |
1018 | | |
1019 | 562 | if (rc != SQLITE_OK) { |
1020 | | /* Workaround for SQLite bug: no error code or string is available here */ |
1021 | 2 | PyErr_SetString(self->OperationalError, "Error creating function"); |
1022 | 2 | return NULL; |
1023 | 2 | } |
1024 | 562 | Py_RETURN_NONE; |
1025 | 562 | } |
1026 | | |
1027 | | #ifdef HAVE_WINDOW_FUNCTIONS |
1028 | | /* |
1029 | | * Regarding the 'inverse' aggregate callback: |
1030 | | * This method is only required by window aggregate functions, not |
1031 | | * ordinary aggregate function implementations. It is invoked to remove |
1032 | | * a row from the current window. The function arguments, if any, |
1033 | | * correspond to the row being removed. |
1034 | | */ |
1035 | | static void |
1036 | | inverse_callback(sqlite3_context *context, int argc, sqlite3_value **params) |
1037 | 22 | { |
1038 | 22 | PyGILState_STATE gilstate = PyGILState_Ensure(); |
1039 | | |
1040 | 22 | callback_context *ctx = (callback_context *)sqlite3_user_data(context); |
1041 | 22 | assert(ctx != NULL); |
1042 | | |
1043 | 0 | int size = sizeof(PyObject *); |
1044 | 22 | PyObject **cls = (PyObject **)sqlite3_aggregate_context(context, size); |
1045 | 22 | assert(cls != NULL); |
1046 | 0 | assert(*cls != NULL); |
1047 | | |
1048 | 0 | PyObject *method = PyObject_GetAttr(*cls, ctx->state->str_inverse); |
1049 | 22 | if (method == NULL) { |
1050 | 2 | set_sqlite_error(context, |
1051 | 2 | "user-defined aggregate's 'inverse' method not defined"); |
1052 | 2 | goto exit; |
1053 | 2 | } |
1054 | | |
1055 | 20 | PyObject *args = _pysqlite_build_py_params(context, argc, params); |
1056 | 20 | if (args == NULL) { |
1057 | 0 | set_sqlite_error(context, |
1058 | 0 | "unable to build arguments for user-defined aggregate's " |
1059 | 0 | "'inverse' method"); |
1060 | 0 | goto exit; |
1061 | 0 | } |
1062 | | |
1063 | 20 | PyObject *res = PyObject_CallObject(method, args); |
1064 | 20 | Py_DECREF(args); |
1065 | 20 | if (res == NULL) { |
1066 | 2 | set_sqlite_error(context, |
1067 | 2 | "user-defined aggregate's 'inverse' method raised error"); |
1068 | 2 | goto exit; |
1069 | 2 | } |
1070 | 18 | Py_DECREF(res); |
1071 | | |
1072 | 22 | exit: |
1073 | 22 | Py_XDECREF(method); |
1074 | 22 | PyGILState_Release(gilstate); |
1075 | 22 | } |
1076 | | |
1077 | | /* |
1078 | | * Regarding the 'value' aggregate callback: |
1079 | | * This method is only required by window aggregate functions, not |
1080 | | * ordinary aggregate function implementations. It is invoked to return |
1081 | | * the current value of the aggregate. |
1082 | | */ |
1083 | | static void |
1084 | | value_callback(sqlite3_context *context) |
1085 | 43 | { |
1086 | 43 | PyGILState_STATE gilstate = PyGILState_Ensure(); |
1087 | | |
1088 | 43 | callback_context *ctx = (callback_context *)sqlite3_user_data(context); |
1089 | 43 | assert(ctx != NULL); |
1090 | | |
1091 | 0 | int size = sizeof(PyObject *); |
1092 | 43 | PyObject **cls = (PyObject **)sqlite3_aggregate_context(context, size); |
1093 | 43 | assert(cls != NULL); |
1094 | 0 | assert(*cls != NULL); |
1095 | | |
1096 | 0 | PyObject *res = PyObject_CallMethodNoArgs(*cls, ctx->state->str_value); |
1097 | 43 | if (res == NULL) { |
1098 | 4 | int attr_err = PyErr_ExceptionMatches(PyExc_AttributeError); |
1099 | 4 | set_sqlite_error(context, attr_err |
1100 | 4 | ? "user-defined aggregate's 'value' method not defined" |
1101 | 4 | : "user-defined aggregate's 'value' method raised error"); |
1102 | 4 | } |
1103 | 39 | else { |
1104 | 39 | int rc = _pysqlite_set_result(context, res); |
1105 | 39 | Py_DECREF(res); |
1106 | 39 | if (rc < 0) { |
1107 | 1 | set_sqlite_error(context, |
1108 | 1 | "unable to set result from user-defined aggregate's " |
1109 | 1 | "'value' method"); |
1110 | 1 | } |
1111 | 39 | } |
1112 | | |
1113 | 43 | PyGILState_Release(gilstate); |
1114 | 43 | } |
1115 | | |
1116 | | /*[clinic input] |
1117 | | _sqlite3.Connection.create_window_function as create_window_function |
1118 | | |
1119 | | cls: defining_class |
1120 | | name: str |
1121 | | The name of the SQL aggregate window function to be created or |
1122 | | redefined. |
1123 | | num_params: int |
1124 | | The number of arguments the step and inverse methods takes. |
1125 | | aggregate_class: object |
1126 | | A class with step(), finalize(), value(), and inverse() methods. |
1127 | | Set to None to clear the window function. |
1128 | | / |
1129 | | |
1130 | | Creates or redefines an aggregate window function. Non-standard. |
1131 | | [clinic start generated code]*/ |
1132 | | |
1133 | | static PyObject * |
1134 | | create_window_function_impl(pysqlite_Connection *self, PyTypeObject *cls, |
1135 | | const char *name, int num_params, |
1136 | | PyObject *aggregate_class) |
1137 | | /*[clinic end generated code: output=5332cd9464522235 input=46d57a54225b5228]*/ |
1138 | 32 | { |
1139 | 32 | if (sqlite3_libversion_number() < 3025000) { |
1140 | 0 | PyErr_SetString(self->NotSupportedError, |
1141 | 0 | "create_window_function() requires " |
1142 | 0 | "SQLite 3.25.0 or higher"); |
1143 | 0 | return NULL; |
1144 | 0 | } |
1145 | | |
1146 | 32 | if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) { |
1147 | 1 | return NULL; |
1148 | 1 | } |
1149 | | |
1150 | 31 | int flags = SQLITE_UTF8; |
1151 | 31 | int rc; |
1152 | 31 | if (Py_IsNone(aggregate_class)) { |
1153 | 1 | rc = sqlite3_create_window_function(self->db, name, num_params, flags, |
1154 | 1 | 0, 0, 0, 0, 0, 0); |
1155 | 1 | } |
1156 | 30 | else { |
1157 | 30 | callback_context *ctx = create_callback_context(cls, aggregate_class); |
1158 | 30 | if (ctx == NULL) { |
1159 | 0 | return NULL; |
1160 | 0 | } |
1161 | 30 | rc = sqlite3_create_window_function(self->db, name, num_params, flags, |
1162 | 30 | ctx, |
1163 | 30 | &step_callback, |
1164 | 30 | &final_callback, |
1165 | 30 | &value_callback, |
1166 | 30 | &inverse_callback, |
1167 | 30 | &destructor_callback); |
1168 | 30 | } |
1169 | | |
1170 | 31 | if (rc != SQLITE_OK) { |
1171 | | // Errors are not set on the database connection, so we cannot |
1172 | | // use _pysqlite_seterror(). |
1173 | 1 | PyErr_SetString(self->ProgrammingError, sqlite3_errstr(rc)); |
1174 | 1 | return NULL; |
1175 | 1 | } |
1176 | 31 | Py_RETURN_NONE; |
1177 | 31 | } |
1178 | | #endif |
1179 | | |
1180 | | /*[clinic input] |
1181 | | _sqlite3.Connection.create_aggregate as pysqlite_connection_create_aggregate |
1182 | | |
1183 | | cls: defining_class |
1184 | | / |
1185 | | name: str |
1186 | | n_arg: int |
1187 | | aggregate_class: object |
1188 | | |
1189 | | Creates a new aggregate. |
1190 | | [clinic start generated code]*/ |
1191 | | |
1192 | | static PyObject * |
1193 | | pysqlite_connection_create_aggregate_impl(pysqlite_Connection *self, |
1194 | | PyTypeObject *cls, |
1195 | | const char *name, int n_arg, |
1196 | | PyObject *aggregate_class) |
1197 | | /*[clinic end generated code: output=1b02d0f0aec7ff96 input=68a2a26366d4c686]*/ |
1198 | 136 | { |
1199 | 136 | int rc; |
1200 | | |
1201 | 136 | if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) { |
1202 | 1 | return NULL; |
1203 | 1 | } |
1204 | | |
1205 | 135 | callback_context *ctx = create_callback_context(cls, aggregate_class); |
1206 | 135 | if (ctx == NULL) { |
1207 | 0 | return NULL; |
1208 | 0 | } |
1209 | 135 | rc = sqlite3_create_function_v2(self->db, name, n_arg, SQLITE_UTF8, ctx, |
1210 | 135 | 0, |
1211 | 135 | &step_callback, |
1212 | 135 | &final_callback, |
1213 | 135 | &destructor_callback); // will decref func |
1214 | 135 | if (rc != SQLITE_OK) { |
1215 | | /* Workaround for SQLite bug: no error code or string is available here */ |
1216 | 0 | PyErr_SetString(self->OperationalError, "Error creating aggregate"); |
1217 | 0 | return NULL; |
1218 | 0 | } |
1219 | 135 | Py_RETURN_NONE; |
1220 | 135 | } |
1221 | | |
1222 | | static int |
1223 | | authorizer_callback(void *ctx, int action, const char *arg1, |
1224 | | const char *arg2 , const char *dbname, |
1225 | | const char *access_attempt_source) |
1226 | 21 | { |
1227 | 21 | PyGILState_STATE gilstate = PyGILState_Ensure(); |
1228 | | |
1229 | 21 | PyObject *ret; |
1230 | 21 | int rc = SQLITE_DENY; |
1231 | | |
1232 | 21 | assert(ctx != NULL); |
1233 | 0 | PyObject *callable = ((callback_context *)ctx)->callable; |
1234 | 21 | ret = PyObject_CallFunction(callable, "issss", action, arg1, arg2, dbname, |
1235 | 21 | access_attempt_source); |
1236 | | |
1237 | 21 | if (ret == NULL) { |
1238 | 4 | print_or_clear_traceback(ctx); |
1239 | 4 | rc = SQLITE_DENY; |
1240 | 4 | } |
1241 | 17 | else { |
1242 | 17 | if (PyLong_Check(ret)) { |
1243 | 15 | rc = _PyLong_AsInt(ret); |
1244 | 15 | if (rc == -1 && PyErr_Occurred()) { |
1245 | 2 | print_or_clear_traceback(ctx); |
1246 | 2 | rc = SQLITE_DENY; |
1247 | 2 | } |
1248 | 15 | } |
1249 | 2 | else { |
1250 | 2 | rc = SQLITE_DENY; |
1251 | 2 | } |
1252 | 17 | Py_DECREF(ret); |
1253 | 17 | } |
1254 | | |
1255 | 21 | PyGILState_Release(gilstate); |
1256 | 21 | return rc; |
1257 | 21 | } |
1258 | | |
1259 | | static int |
1260 | | progress_callback(void *ctx) |
1261 | 149 | { |
1262 | 149 | PyGILState_STATE gilstate = PyGILState_Ensure(); |
1263 | | |
1264 | 149 | int rc; |
1265 | 149 | PyObject *ret; |
1266 | | |
1267 | 149 | assert(ctx != NULL); |
1268 | 0 | PyObject *callable = ((callback_context *)ctx)->callable; |
1269 | 149 | ret = PyObject_CallNoArgs(callable); |
1270 | 149 | if (!ret) { |
1271 | | /* abort query if error occurred */ |
1272 | 2 | rc = -1; |
1273 | 2 | } |
1274 | 147 | else { |
1275 | 147 | rc = PyObject_IsTrue(ret); |
1276 | 147 | Py_DECREF(ret); |
1277 | 147 | } |
1278 | 149 | if (rc < 0) { |
1279 | 4 | print_or_clear_traceback(ctx); |
1280 | 4 | } |
1281 | | |
1282 | 149 | PyGILState_Release(gilstate); |
1283 | 149 | return rc; |
1284 | 149 | } |
1285 | | |
1286 | | #ifdef HAVE_TRACE_V2 |
1287 | | /* |
1288 | | * From https://sqlite.org/c3ref/trace_v2.html: |
1289 | | * The integer return value from the callback is currently ignored, though this |
1290 | | * may change in future releases. Callback implementations should return zero |
1291 | | * to ensure future compatibility. |
1292 | | */ |
1293 | | static int |
1294 | | trace_callback(unsigned int type, void *ctx, void *stmt, void *sql) |
1295 | | #else |
1296 | | static void |
1297 | | trace_callback(void *ctx, const char *sql) |
1298 | | #endif |
1299 | 52 | { |
1300 | 52 | #ifdef HAVE_TRACE_V2 |
1301 | 52 | if (type != SQLITE_TRACE_STMT) { |
1302 | 0 | return 0; |
1303 | 0 | } |
1304 | 52 | #endif |
1305 | | |
1306 | 52 | PyGILState_STATE gilstate = PyGILState_Ensure(); |
1307 | | |
1308 | 52 | assert(ctx != NULL); |
1309 | 0 | pysqlite_state *state = ((callback_context *)ctx)->state; |
1310 | 52 | assert(state != NULL); |
1311 | | |
1312 | 0 | PyObject *py_statement = NULL; |
1313 | 52 | #ifdef HAVE_TRACE_V2 |
1314 | 52 | const char *expanded_sql = sqlite3_expanded_sql((sqlite3_stmt *)stmt); |
1315 | 52 | if (expanded_sql == NULL) { |
1316 | 2 | sqlite3 *db = sqlite3_db_handle((sqlite3_stmt *)stmt); |
1317 | 2 | if (sqlite3_errcode(db) == SQLITE_NOMEM) { |
1318 | 0 | (void)PyErr_NoMemory(); |
1319 | 0 | goto exit; |
1320 | 0 | } |
1321 | | |
1322 | 2 | PyErr_SetString(state->DataError, |
1323 | 2 | "Expanded SQL string exceeds the maximum string length"); |
1324 | 2 | print_or_clear_traceback((callback_context *)ctx); |
1325 | | |
1326 | | // Fall back to unexpanded sql |
1327 | 2 | py_statement = PyUnicode_FromString((const char *)sql); |
1328 | 2 | } |
1329 | 50 | else { |
1330 | 50 | py_statement = PyUnicode_FromString(expanded_sql); |
1331 | 50 | sqlite3_free((void *)expanded_sql); |
1332 | 50 | } |
1333 | | #else |
1334 | | if (sql == NULL) { |
1335 | | PyErr_SetString(state->DataError, |
1336 | | "Expanded SQL string exceeds the maximum string length"); |
1337 | | print_or_clear_traceback((callback_context *)ctx); |
1338 | | goto exit; |
1339 | | } |
1340 | | py_statement = PyUnicode_FromString(sql); |
1341 | | #endif |
1342 | 52 | if (py_statement) { |
1343 | 52 | PyObject *callable = ((callback_context *)ctx)->callable; |
1344 | 52 | PyObject *ret = PyObject_CallOneArg(callable, py_statement); |
1345 | 52 | Py_DECREF(py_statement); |
1346 | 52 | Py_XDECREF(ret); |
1347 | 52 | } |
1348 | 52 | if (PyErr_Occurred()) { |
1349 | 2 | print_or_clear_traceback((callback_context *)ctx); |
1350 | 2 | } |
1351 | | |
1352 | 52 | exit: |
1353 | 52 | PyGILState_Release(gilstate); |
1354 | 52 | #ifdef HAVE_TRACE_V2 |
1355 | 52 | return 0; |
1356 | 52 | #endif |
1357 | 52 | } |
1358 | | |
1359 | | /*[clinic input] |
1360 | | _sqlite3.Connection.set_authorizer as pysqlite_connection_set_authorizer |
1361 | | |
1362 | | cls: defining_class |
1363 | | / |
1364 | | authorizer_callback as callable: object |
1365 | | |
1366 | | Sets authorizer callback. |
1367 | | [clinic start generated code]*/ |
1368 | | |
1369 | | static PyObject * |
1370 | | pysqlite_connection_set_authorizer_impl(pysqlite_Connection *self, |
1371 | | PyTypeObject *cls, |
1372 | | PyObject *callable) |
1373 | | /*[clinic end generated code: output=75fa60114fc971c3 input=605d32ba92dd3eca]*/ |
1374 | 21 | { |
1375 | 21 | if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) { |
1376 | 2 | return NULL; |
1377 | 2 | } |
1378 | | |
1379 | 19 | int rc; |
1380 | 19 | if (callable == Py_None) { |
1381 | 5 | rc = sqlite3_set_authorizer(self->db, NULL, NULL); |
1382 | 5 | set_callback_context(&self->authorizer_ctx, NULL); |
1383 | 5 | } |
1384 | 14 | else { |
1385 | 14 | callback_context *ctx = create_callback_context(cls, callable); |
1386 | 14 | if (ctx == NULL) { |
1387 | 0 | return NULL; |
1388 | 0 | } |
1389 | 14 | rc = sqlite3_set_authorizer(self->db, authorizer_callback, ctx); |
1390 | 14 | set_callback_context(&self->authorizer_ctx, ctx); |
1391 | 14 | } |
1392 | 19 | if (rc != SQLITE_OK) { |
1393 | 0 | PyErr_SetString(self->OperationalError, |
1394 | 0 | "Error setting authorizer callback"); |
1395 | 0 | set_callback_context(&self->authorizer_ctx, NULL); |
1396 | 0 | return NULL; |
1397 | 0 | } |
1398 | 19 | Py_RETURN_NONE; |
1399 | 19 | } |
1400 | | |
1401 | | /*[clinic input] |
1402 | | _sqlite3.Connection.set_progress_handler as pysqlite_connection_set_progress_handler |
1403 | | |
1404 | | cls: defining_class |
1405 | | / |
1406 | | progress_handler as callable: object |
1407 | | n: int |
1408 | | |
1409 | | Sets progress handler callback. |
1410 | | [clinic start generated code]*/ |
1411 | | |
1412 | | static PyObject * |
1413 | | pysqlite_connection_set_progress_handler_impl(pysqlite_Connection *self, |
1414 | | PyTypeObject *cls, |
1415 | | PyObject *callable, int n) |
1416 | | /*[clinic end generated code: output=0739957fd8034a50 input=f7c1837984bd86db]*/ |
1417 | 14 | { |
1418 | 14 | if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) { |
1419 | 1 | return NULL; |
1420 | 1 | } |
1421 | | |
1422 | 13 | if (callable == Py_None) { |
1423 | | /* None clears the progress handler previously set */ |
1424 | 2 | sqlite3_progress_handler(self->db, 0, 0, (void*)0); |
1425 | 2 | set_callback_context(&self->progress_ctx, NULL); |
1426 | 2 | } |
1427 | 11 | else { |
1428 | 11 | callback_context *ctx = create_callback_context(cls, callable); |
1429 | 11 | if (ctx == NULL) { |
1430 | 0 | return NULL; |
1431 | 0 | } |
1432 | 11 | sqlite3_progress_handler(self->db, n, progress_callback, ctx); |
1433 | 11 | set_callback_context(&self->progress_ctx, ctx); |
1434 | 11 | } |
1435 | 13 | Py_RETURN_NONE; |
1436 | 13 | } |
1437 | | |
1438 | | /*[clinic input] |
1439 | | _sqlite3.Connection.set_trace_callback as pysqlite_connection_set_trace_callback |
1440 | | |
1441 | | cls: defining_class |
1442 | | / |
1443 | | trace_callback as callable: object |
1444 | | |
1445 | | Sets a trace callback called for each SQL statement (passed as unicode). |
1446 | | [clinic start generated code]*/ |
1447 | | |
1448 | | static PyObject * |
1449 | | pysqlite_connection_set_trace_callback_impl(pysqlite_Connection *self, |
1450 | | PyTypeObject *cls, |
1451 | | PyObject *callable) |
1452 | | /*[clinic end generated code: output=d91048c03bfcee05 input=351a94210c5f81bb]*/ |
1453 | 33 | { |
1454 | 33 | if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) { |
1455 | 1 | return NULL; |
1456 | 1 | } |
1457 | | |
1458 | 32 | if (callable == Py_None) { |
1459 | | /* |
1460 | | * None clears the trace callback previously set |
1461 | | * |
1462 | | * Ref. |
1463 | | * - https://sqlite.org/c3ref/c_trace.html |
1464 | | * - https://sqlite.org/c3ref/trace_v2.html |
1465 | | */ |
1466 | 7 | #ifdef HAVE_TRACE_V2 |
1467 | 7 | sqlite3_trace_v2(self->db, SQLITE_TRACE_STMT, 0, 0); |
1468 | | #else |
1469 | | sqlite3_trace(self->db, 0, (void*)0); |
1470 | | #endif |
1471 | 7 | set_callback_context(&self->trace_ctx, NULL); |
1472 | 7 | } |
1473 | 25 | else { |
1474 | 25 | callback_context *ctx = create_callback_context(cls, callable); |
1475 | 25 | if (ctx == NULL) { |
1476 | 0 | return NULL; |
1477 | 0 | } |
1478 | 25 | #ifdef HAVE_TRACE_V2 |
1479 | 25 | sqlite3_trace_v2(self->db, SQLITE_TRACE_STMT, trace_callback, ctx); |
1480 | | #else |
1481 | | sqlite3_trace(self->db, trace_callback, ctx); |
1482 | | #endif |
1483 | 25 | set_callback_context(&self->trace_ctx, ctx); |
1484 | 25 | } |
1485 | | |
1486 | 32 | Py_RETURN_NONE; |
1487 | 32 | } |
1488 | | |
1489 | | #ifdef PY_SQLITE_ENABLE_LOAD_EXTENSION |
1490 | | /*[clinic input] |
1491 | | _sqlite3.Connection.enable_load_extension as pysqlite_connection_enable_load_extension |
1492 | | |
1493 | | enable as onoff: bool(accept={int}) |
1494 | | / |
1495 | | |
1496 | | Enable dynamic loading of SQLite extension modules. |
1497 | | [clinic start generated code]*/ |
1498 | | |
1499 | | static PyObject * |
1500 | | pysqlite_connection_enable_load_extension_impl(pysqlite_Connection *self, |
1501 | | int onoff) |
1502 | | /*[clinic end generated code: output=9cac37190d388baf input=5f00e93f7a9d3540]*/ |
1503 | | { |
1504 | | int rc; |
1505 | | |
1506 | | if (PySys_Audit("sqlite3.enable_load_extension", |
1507 | | "OO", self, onoff ? Py_True : Py_False) < 0) { |
1508 | | return NULL; |
1509 | | } |
1510 | | |
1511 | | if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) { |
1512 | | return NULL; |
1513 | | } |
1514 | | |
1515 | | rc = sqlite3_enable_load_extension(self->db, onoff); |
1516 | | |
1517 | | if (rc != SQLITE_OK) { |
1518 | | PyErr_SetString(self->OperationalError, |
1519 | | "Error enabling load extension"); |
1520 | | return NULL; |
1521 | | } else { |
1522 | | Py_RETURN_NONE; |
1523 | | } |
1524 | | } |
1525 | | |
1526 | | /*[clinic input] |
1527 | | _sqlite3.Connection.load_extension as pysqlite_connection_load_extension |
1528 | | |
1529 | | name as extension_name: str |
1530 | | / |
1531 | | |
1532 | | Load SQLite extension module. |
1533 | | [clinic start generated code]*/ |
1534 | | |
1535 | | static PyObject * |
1536 | | pysqlite_connection_load_extension_impl(pysqlite_Connection *self, |
1537 | | const char *extension_name) |
1538 | | /*[clinic end generated code: output=47eb1d7312bc97a7 input=edd507389d89d621]*/ |
1539 | | { |
1540 | | int rc; |
1541 | | char* errmsg; |
1542 | | |
1543 | | if (PySys_Audit("sqlite3.load_extension", "Os", self, extension_name) < 0) { |
1544 | | return NULL; |
1545 | | } |
1546 | | |
1547 | | if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) { |
1548 | | return NULL; |
1549 | | } |
1550 | | |
1551 | | rc = sqlite3_load_extension(self->db, extension_name, 0, &errmsg); |
1552 | | if (rc != 0) { |
1553 | | PyErr_SetString(self->OperationalError, errmsg); |
1554 | | return NULL; |
1555 | | } else { |
1556 | | Py_RETURN_NONE; |
1557 | | } |
1558 | | } |
1559 | | #endif |
1560 | | |
1561 | | int pysqlite_check_thread(pysqlite_Connection* self) |
1562 | 8.41k | { |
1563 | 8.41k | if (self->check_same_thread) { |
1564 | 8.41k | if (PyThread_get_thread_ident() != self->thread_ident) { |
1565 | 17 | PyErr_Format(self->ProgrammingError, |
1566 | 17 | "SQLite objects created in a thread can only be used in that same thread. " |
1567 | 17 | "The object was created in thread id %lu and this is thread id %lu.", |
1568 | 17 | self->thread_ident, PyThread_get_thread_ident()); |
1569 | 17 | return 0; |
1570 | 17 | } |
1571 | | |
1572 | 8.41k | } |
1573 | 8.40k | return 1; |
1574 | 8.41k | } |
1575 | | |
1576 | | static PyObject* pysqlite_connection_get_isolation_level(pysqlite_Connection* self, void* unused) |
1577 | 32 | { |
1578 | 32 | if (!pysqlite_check_connection(self)) { |
1579 | 1 | return NULL; |
1580 | 1 | } |
1581 | 31 | if (self->isolation_level != NULL) { |
1582 | 28 | return PyUnicode_FromString(self->isolation_level); |
1583 | 28 | } |
1584 | 31 | Py_RETURN_NONE; |
1585 | 31 | } |
1586 | | |
1587 | | static PyObject* pysqlite_connection_get_total_changes(pysqlite_Connection* self, void* unused) |
1588 | 2 | { |
1589 | 2 | if (!pysqlite_check_connection(self)) { |
1590 | 1 | return NULL; |
1591 | 1 | } |
1592 | 1 | return PyLong_FromLong(sqlite3_total_changes(self->db)); |
1593 | 2 | } |
1594 | | |
1595 | | static PyObject* pysqlite_connection_get_in_transaction(pysqlite_Connection* self, void* unused) |
1596 | 9 | { |
1597 | 9 | if (!pysqlite_check_connection(self)) { |
1598 | 1 | return NULL; |
1599 | 1 | } |
1600 | 8 | if (!sqlite3_get_autocommit(self->db)) { |
1601 | 3 | Py_RETURN_TRUE; |
1602 | 3 | } |
1603 | 8 | Py_RETURN_FALSE; |
1604 | 8 | } |
1605 | | |
1606 | | static int |
1607 | | pysqlite_connection_set_isolation_level(pysqlite_Connection* self, PyObject* isolation_level, void *Py_UNUSED(ignored)) |
1608 | 51 | { |
1609 | 51 | if (isolation_level == NULL) { |
1610 | 1 | PyErr_SetString(PyExc_AttributeError, "cannot delete attribute"); |
1611 | 1 | return -1; |
1612 | 1 | } |
1613 | 50 | if (Py_IsNone(isolation_level)) { |
1614 | 7 | self->isolation_level = NULL; |
1615 | | |
1616 | | // Execute a COMMIT to re-enable autocommit mode |
1617 | 7 | PyObject *res = pysqlite_connection_commit_impl(self); |
1618 | 7 | if (res == NULL) { |
1619 | 0 | return -1; |
1620 | 0 | } |
1621 | 7 | Py_DECREF(res); |
1622 | 7 | return 0; |
1623 | 7 | } |
1624 | 43 | if (!isolation_level_converter(isolation_level, &self->isolation_level)) { |
1625 | 13 | return -1; |
1626 | 13 | } |
1627 | 30 | return 0; |
1628 | 43 | } |
1629 | | |
1630 | | static PyObject * |
1631 | | pysqlite_connection_call(pysqlite_Connection *self, PyObject *args, |
1632 | | PyObject *kwargs) |
1633 | 1.57k | { |
1634 | 1.57k | PyObject* sql; |
1635 | 1.57k | pysqlite_Statement* statement; |
1636 | | |
1637 | 1.57k | if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) { |
1638 | 1 | return NULL; |
1639 | 1 | } |
1640 | | |
1641 | 1.57k | if (!_PyArg_NoKeywords(MODULE_NAME ".Connection", kwargs)) |
1642 | 0 | return NULL; |
1643 | | |
1644 | 1.57k | if (!PyArg_ParseTuple(args, "U", &sql)) |
1645 | 1 | return NULL; |
1646 | | |
1647 | 1.57k | statement = pysqlite_statement_create(self, sql); |
1648 | 1.57k | if (statement == NULL) { |
1649 | 46 | return NULL; |
1650 | 46 | } |
1651 | | |
1652 | 1.52k | return (PyObject*)statement; |
1653 | 1.57k | } |
1654 | | |
1655 | | /*[clinic input] |
1656 | | _sqlite3.Connection.execute as pysqlite_connection_execute |
1657 | | |
1658 | | sql: unicode |
1659 | | parameters: object = NULL |
1660 | | / |
1661 | | |
1662 | | Executes an SQL statement. |
1663 | | [clinic start generated code]*/ |
1664 | | |
1665 | | static PyObject * |
1666 | | pysqlite_connection_execute_impl(pysqlite_Connection *self, PyObject *sql, |
1667 | | PyObject *parameters) |
1668 | | /*[clinic end generated code: output=5be05ae01ee17ee4 input=27aa7792681ddba2]*/ |
1669 | 856 | { |
1670 | 856 | PyObject* result = 0; |
1671 | | |
1672 | 856 | PyObject *cursor = pysqlite_connection_cursor_impl(self, NULL); |
1673 | 856 | if (!cursor) { |
1674 | 1 | goto error; |
1675 | 1 | } |
1676 | | |
1677 | 855 | result = _pysqlite_query_execute((pysqlite_Cursor *)cursor, 0, sql, parameters); |
1678 | 855 | if (!result) { |
1679 | 35 | Py_CLEAR(cursor); |
1680 | 35 | } |
1681 | | |
1682 | 856 | error: |
1683 | 856 | Py_XDECREF(result); |
1684 | | |
1685 | 856 | return cursor; |
1686 | 855 | } |
1687 | | |
1688 | | /*[clinic input] |
1689 | | _sqlite3.Connection.executemany as pysqlite_connection_executemany |
1690 | | |
1691 | | sql: unicode |
1692 | | parameters: object |
1693 | | / |
1694 | | |
1695 | | Repeatedly executes an SQL statement. |
1696 | | [clinic start generated code]*/ |
1697 | | |
1698 | | static PyObject * |
1699 | | pysqlite_connection_executemany_impl(pysqlite_Connection *self, |
1700 | | PyObject *sql, PyObject *parameters) |
1701 | | /*[clinic end generated code: output=776cd2fd20bfe71f input=495be76551d525db]*/ |
1702 | 37 | { |
1703 | 37 | PyObject* result = 0; |
1704 | | |
1705 | 37 | PyObject *cursor = pysqlite_connection_cursor_impl(self, NULL); |
1706 | 37 | if (!cursor) { |
1707 | 2 | goto error; |
1708 | 2 | } |
1709 | | |
1710 | 35 | result = _pysqlite_query_execute((pysqlite_Cursor *)cursor, 1, sql, parameters); |
1711 | 35 | if (!result) { |
1712 | 0 | Py_CLEAR(cursor); |
1713 | 0 | } |
1714 | | |
1715 | 37 | error: |
1716 | 37 | Py_XDECREF(result); |
1717 | | |
1718 | 37 | return cursor; |
1719 | 35 | } |
1720 | | |
1721 | | /*[clinic input] |
1722 | | _sqlite3.Connection.executescript as pysqlite_connection_executescript |
1723 | | |
1724 | | sql_script as script_obj: object |
1725 | | / |
1726 | | |
1727 | | Executes multiple SQL statements at once. |
1728 | | [clinic start generated code]*/ |
1729 | | |
1730 | | static PyObject * |
1731 | | pysqlite_connection_executescript(pysqlite_Connection *self, |
1732 | | PyObject *script_obj) |
1733 | | /*[clinic end generated code: output=4c4f9d77aa0ae37d input=f6e5f1ccfa313db4]*/ |
1734 | 20 | { |
1735 | 20 | PyObject* result = 0; |
1736 | | |
1737 | 20 | PyObject *cursor = pysqlite_connection_cursor_impl(self, NULL); |
1738 | 20 | if (!cursor) { |
1739 | 1 | goto error; |
1740 | 1 | } |
1741 | | |
1742 | 19 | PyObject *meth = self->state->str_executescript; // borrowed ref. |
1743 | 19 | result = PyObject_CallMethodObjArgs(cursor, meth, script_obj, NULL); |
1744 | 19 | if (!result) { |
1745 | 1 | Py_CLEAR(cursor); |
1746 | 1 | } |
1747 | | |
1748 | 20 | error: |
1749 | 20 | Py_XDECREF(result); |
1750 | | |
1751 | 20 | return cursor; |
1752 | 19 | } |
1753 | | |
1754 | | /* ------------------------- COLLATION CODE ------------------------ */ |
1755 | | |
1756 | | static int |
1757 | | collation_callback(void *context, int text1_length, const void *text1_data, |
1758 | | int text2_length, const void *text2_data) |
1759 | 8 | { |
1760 | 8 | PyGILState_STATE gilstate = PyGILState_Ensure(); |
1761 | | |
1762 | 8 | PyObject* string1 = 0; |
1763 | 8 | PyObject* string2 = 0; |
1764 | 8 | PyObject* retval = NULL; |
1765 | 8 | long longval; |
1766 | 8 | int result = 0; |
1767 | | |
1768 | | /* This callback may be executed multiple times per sqlite3_step(). Bail if |
1769 | | * the previous call failed */ |
1770 | 8 | if (PyErr_Occurred()) { |
1771 | 0 | goto finally; |
1772 | 0 | } |
1773 | | |
1774 | 8 | string1 = PyUnicode_FromStringAndSize((const char*)text1_data, text1_length); |
1775 | 8 | string2 = PyUnicode_FromStringAndSize((const char*)text2_data, text2_length); |
1776 | | |
1777 | 8 | if (!string1 || !string2) { |
1778 | 0 | goto finally; /* failed to allocate strings */ |
1779 | 0 | } |
1780 | | |
1781 | 8 | callback_context *ctx = (callback_context *)context; |
1782 | 8 | assert(ctx != NULL); |
1783 | 0 | PyObject *args[] = { NULL, string1, string2 }; // Borrowed refs. |
1784 | 8 | size_t nargsf = 2 | PY_VECTORCALL_ARGUMENTS_OFFSET; |
1785 | 8 | retval = PyObject_Vectorcall(ctx->callable, args + 1, nargsf, NULL); |
1786 | 8 | if (retval == NULL) { |
1787 | | /* execution failed */ |
1788 | 0 | goto finally; |
1789 | 0 | } |
1790 | | |
1791 | 8 | longval = PyLong_AsLongAndOverflow(retval, &result); |
1792 | 8 | if (longval == -1 && PyErr_Occurred()) { |
1793 | 0 | PyErr_Clear(); |
1794 | 0 | result = 0; |
1795 | 0 | } |
1796 | 8 | else if (!result) { |
1797 | 8 | if (longval > 0) |
1798 | 8 | result = 1; |
1799 | 0 | else if (longval < 0) |
1800 | 0 | result = -1; |
1801 | 8 | } |
1802 | | |
1803 | 8 | finally: |
1804 | 8 | Py_XDECREF(string1); |
1805 | 8 | Py_XDECREF(string2); |
1806 | 8 | Py_XDECREF(retval); |
1807 | 8 | PyGILState_Release(gilstate); |
1808 | 8 | return result; |
1809 | 8 | } |
1810 | | |
1811 | | /*[clinic input] |
1812 | | _sqlite3.Connection.interrupt as pysqlite_connection_interrupt |
1813 | | |
1814 | | Abort any pending database operation. |
1815 | | [clinic start generated code]*/ |
1816 | | |
1817 | | static PyObject * |
1818 | | pysqlite_connection_interrupt_impl(pysqlite_Connection *self) |
1819 | | /*[clinic end generated code: output=f193204bc9e70b47 input=75ad03ade7012859]*/ |
1820 | 2 | { |
1821 | 2 | PyObject* retval = NULL; |
1822 | | |
1823 | 2 | if (!pysqlite_check_connection(self)) { |
1824 | 1 | goto finally; |
1825 | 1 | } |
1826 | | |
1827 | 1 | sqlite3_interrupt(self->db); |
1828 | | |
1829 | 1 | retval = Py_NewRef(Py_None); |
1830 | | |
1831 | 2 | finally: |
1832 | 2 | return retval; |
1833 | 1 | } |
1834 | | |
1835 | | /* Function author: Paul Kippes <kippesp@gmail.com> |
1836 | | * Class method of Connection to call the Python function _iterdump |
1837 | | * of the sqlite3 module. |
1838 | | */ |
1839 | | /*[clinic input] |
1840 | | _sqlite3.Connection.iterdump as pysqlite_connection_iterdump |
1841 | | |
1842 | | Returns iterator to the dump of the database in an SQL text format. |
1843 | | [clinic start generated code]*/ |
1844 | | |
1845 | | static PyObject * |
1846 | | pysqlite_connection_iterdump_impl(pysqlite_Connection *self) |
1847 | | /*[clinic end generated code: output=586997aaf9808768 input=1911ca756066da89]*/ |
1848 | 5 | { |
1849 | 5 | if (!pysqlite_check_connection(self)) { |
1850 | 1 | return NULL; |
1851 | 1 | } |
1852 | | |
1853 | 4 | PyObject *iterdump = _PyImport_GetModuleAttrString(MODULE_NAME ".dump", "_iterdump"); |
1854 | 4 | if (!iterdump) { |
1855 | 0 | if (!PyErr_Occurred()) { |
1856 | 0 | PyErr_SetString(self->OperationalError, |
1857 | 0 | "Failed to obtain _iterdump() reference"); |
1858 | 0 | } |
1859 | 0 | return NULL; |
1860 | 0 | } |
1861 | | |
1862 | 4 | PyObject *retval = PyObject_CallOneArg(iterdump, (PyObject *)self); |
1863 | 4 | Py_DECREF(iterdump); |
1864 | 4 | return retval; |
1865 | 4 | } |
1866 | | |
1867 | | /*[clinic input] |
1868 | | _sqlite3.Connection.backup as pysqlite_connection_backup |
1869 | | |
1870 | | target: object(type='pysqlite_Connection *', subclass_of='clinic_state()->ConnectionType') |
1871 | | * |
1872 | | pages: int = -1 |
1873 | | progress: object = None |
1874 | | name: str = "main" |
1875 | | sleep: double = 0.250 |
1876 | | |
1877 | | Makes a backup of the database. |
1878 | | [clinic start generated code]*/ |
1879 | | |
1880 | | static PyObject * |
1881 | | pysqlite_connection_backup_impl(pysqlite_Connection *self, |
1882 | | pysqlite_Connection *target, int pages, |
1883 | | PyObject *progress, const char *name, |
1884 | | double sleep) |
1885 | | /*[clinic end generated code: output=306a3e6a38c36334 input=c6519d0f59d0fd7f]*/ |
1886 | 15 | { |
1887 | 15 | int rc; |
1888 | 15 | int sleep_ms = (int)(sleep * 1000.0); |
1889 | 15 | sqlite3 *bck_conn; |
1890 | 15 | sqlite3_backup *bck_handle; |
1891 | | |
1892 | 15 | if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) { |
1893 | 1 | return NULL; |
1894 | 1 | } |
1895 | | |
1896 | 14 | if (!pysqlite_check_connection(target)) { |
1897 | 1 | return NULL; |
1898 | 1 | } |
1899 | | |
1900 | 13 | if (target == self) { |
1901 | 1 | PyErr_SetString(PyExc_ValueError, "target cannot be the same connection instance"); |
1902 | 1 | return NULL; |
1903 | 1 | } |
1904 | | |
1905 | | #if SQLITE_VERSION_NUMBER < 3008008 |
1906 | | /* Since 3.8.8 this is already done, per commit |
1907 | | https://www.sqlite.org/src/info/169b5505498c0a7e */ |
1908 | | if (!sqlite3_get_autocommit(target->db)) { |
1909 | | PyErr_SetString(self->OperationalError, "target is in transaction"); |
1910 | | return NULL; |
1911 | | } |
1912 | | #endif |
1913 | | |
1914 | 12 | if (progress != Py_None && !PyCallable_Check(progress)) { |
1915 | 1 | PyErr_SetString(PyExc_TypeError, "progress argument must be a callable"); |
1916 | 1 | return NULL; |
1917 | 1 | } |
1918 | | |
1919 | 11 | if (pages == 0) { |
1920 | 0 | pages = -1; |
1921 | 0 | } |
1922 | | |
1923 | 11 | bck_conn = target->db; |
1924 | | |
1925 | 11 | Py_BEGIN_ALLOW_THREADS |
1926 | 11 | bck_handle = sqlite3_backup_init(bck_conn, "main", self->db, name); |
1927 | 11 | Py_END_ALLOW_THREADS |
1928 | | |
1929 | 11 | if (bck_handle == NULL) { |
1930 | 2 | _pysqlite_seterror(self->state, bck_conn); |
1931 | 2 | return NULL; |
1932 | 2 | } |
1933 | | |
1934 | 12 | do { |
1935 | 12 | Py_BEGIN_ALLOW_THREADS |
1936 | 12 | rc = sqlite3_backup_step(bck_handle, pages); |
1937 | 12 | Py_END_ALLOW_THREADS |
1938 | | |
1939 | 12 | if (progress != Py_None) { |
1940 | 8 | int remaining = sqlite3_backup_remaining(bck_handle); |
1941 | 8 | int pagecount = sqlite3_backup_pagecount(bck_handle); |
1942 | 8 | PyObject *res = PyObject_CallFunction(progress, "iii", rc, |
1943 | 8 | remaining, pagecount); |
1944 | 8 | if (res == NULL) { |
1945 | | /* Callback failed: abort backup and bail. */ |
1946 | 1 | Py_BEGIN_ALLOW_THREADS |
1947 | 1 | sqlite3_backup_finish(bck_handle); |
1948 | 1 | Py_END_ALLOW_THREADS |
1949 | 1 | return NULL; |
1950 | 1 | } |
1951 | 7 | Py_DECREF(res); |
1952 | 7 | } |
1953 | | |
1954 | | /* Sleep for a while if there are still further pages to copy and |
1955 | | the engine could not make any progress */ |
1956 | 11 | if (rc == SQLITE_BUSY || rc == SQLITE_LOCKED) { |
1957 | 0 | Py_BEGIN_ALLOW_THREADS |
1958 | 0 | sqlite3_sleep(sleep_ms); |
1959 | 0 | Py_END_ALLOW_THREADS |
1960 | 0 | } |
1961 | 11 | } while (rc == SQLITE_OK || rc == SQLITE_BUSY || rc == SQLITE_LOCKED); |
1962 | | |
1963 | 8 | Py_BEGIN_ALLOW_THREADS |
1964 | 8 | rc = sqlite3_backup_finish(bck_handle); |
1965 | 8 | Py_END_ALLOW_THREADS |
1966 | | |
1967 | 8 | if (rc != SQLITE_OK) { |
1968 | 0 | _pysqlite_seterror(self->state, bck_conn); |
1969 | 0 | return NULL; |
1970 | 0 | } |
1971 | | |
1972 | 8 | Py_RETURN_NONE; |
1973 | 8 | } |
1974 | | |
1975 | | /*[clinic input] |
1976 | | _sqlite3.Connection.create_collation as pysqlite_connection_create_collation |
1977 | | |
1978 | | cls: defining_class |
1979 | | name: str |
1980 | | callback as callable: object |
1981 | | / |
1982 | | |
1983 | | Creates a collation function. |
1984 | | [clinic start generated code]*/ |
1985 | | |
1986 | | static PyObject * |
1987 | | pysqlite_connection_create_collation_impl(pysqlite_Connection *self, |
1988 | | PyTypeObject *cls, |
1989 | | const char *name, |
1990 | | PyObject *callable) |
1991 | | /*[clinic end generated code: output=32d339e97869c378 input=f67ecd2e31e61ad3]*/ |
1992 | 11 | { |
1993 | 11 | if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) { |
1994 | 1 | return NULL; |
1995 | 1 | } |
1996 | | |
1997 | 10 | callback_context *ctx = NULL; |
1998 | 10 | int rc; |
1999 | 10 | int flags = SQLITE_UTF8; |
2000 | 10 | if (callable == Py_None) { |
2001 | 2 | rc = sqlite3_create_collation_v2(self->db, name, flags, |
2002 | 2 | NULL, NULL, NULL); |
2003 | 2 | } |
2004 | 8 | else { |
2005 | 8 | if (!PyCallable_Check(callable)) { |
2006 | 1 | PyErr_SetString(PyExc_TypeError, "parameter must be callable"); |
2007 | 1 | return NULL; |
2008 | 1 | } |
2009 | 7 | ctx = create_callback_context(cls, callable); |
2010 | 7 | if (ctx == NULL) { |
2011 | 0 | return NULL; |
2012 | 0 | } |
2013 | 7 | rc = sqlite3_create_collation_v2(self->db, name, flags, ctx, |
2014 | 7 | &collation_callback, |
2015 | 7 | &destructor_callback); |
2016 | 7 | } |
2017 | | |
2018 | 9 | if (rc != SQLITE_OK) { |
2019 | | /* Unlike other sqlite3_* functions, the destructor callback is _not_ |
2020 | | * called if sqlite3_create_collation_v2() fails, so we have to free |
2021 | | * the context before returning. |
2022 | | */ |
2023 | 0 | if (callable != Py_None) { |
2024 | 0 | free_callback_context(ctx); |
2025 | 0 | } |
2026 | 0 | _pysqlite_seterror(self->state, self->db); |
2027 | 0 | return NULL; |
2028 | 0 | } |
2029 | | |
2030 | 9 | Py_RETURN_NONE; |
2031 | 9 | } |
2032 | | |
2033 | | #ifdef PY_SQLITE_HAVE_SERIALIZE |
2034 | | /*[clinic input] |
2035 | | _sqlite3.Connection.serialize as serialize |
2036 | | |
2037 | | * |
2038 | | name: str = "main" |
2039 | | Which database to serialize. |
2040 | | |
2041 | | Serialize a database into a byte string. |
2042 | | |
2043 | | For an ordinary on-disk database file, the serialization is just a copy of the |
2044 | | disk file. For an in-memory database or a "temp" database, the serialization is |
2045 | | the same sequence of bytes which would be written to disk if that database |
2046 | | were backed up to disk. |
2047 | | [clinic start generated code]*/ |
2048 | | |
2049 | | static PyObject * |
2050 | | serialize_impl(pysqlite_Connection *self, const char *name) |
2051 | | /*[clinic end generated code: output=97342b0e55239dd3 input=d2eb5194a65abe2b]*/ |
2052 | 2 | { |
2053 | 2 | if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) { |
2054 | 1 | return NULL; |
2055 | 1 | } |
2056 | | |
2057 | | /* If SQLite has a contiguous memory representation of the database, we can |
2058 | | * avoid memory allocations, so we try with the no-copy flag first. |
2059 | | */ |
2060 | 1 | sqlite3_int64 size; |
2061 | 1 | unsigned int flags = SQLITE_SERIALIZE_NOCOPY; |
2062 | 1 | const char *data; |
2063 | | |
2064 | 1 | Py_BEGIN_ALLOW_THREADS |
2065 | 1 | data = (const char *)sqlite3_serialize(self->db, name, &size, flags); |
2066 | 1 | if (data == NULL) { |
2067 | 1 | flags &= ~SQLITE_SERIALIZE_NOCOPY; |
2068 | 1 | data = (const char *)sqlite3_serialize(self->db, name, &size, flags); |
2069 | 1 | } |
2070 | 1 | Py_END_ALLOW_THREADS |
2071 | | |
2072 | 1 | if (data == NULL) { |
2073 | 0 | PyErr_Format(self->OperationalError, "unable to serialize '%s'", |
2074 | 0 | name); |
2075 | 0 | return NULL; |
2076 | 0 | } |
2077 | 1 | PyObject *res = PyBytes_FromStringAndSize(data, (Py_ssize_t)size); |
2078 | 1 | if (!(flags & SQLITE_SERIALIZE_NOCOPY)) { |
2079 | 1 | sqlite3_free((void *)data); |
2080 | 1 | } |
2081 | 1 | return res; |
2082 | 1 | } |
2083 | | |
2084 | | /*[clinic input] |
2085 | | _sqlite3.Connection.deserialize as deserialize |
2086 | | |
2087 | | data: Py_buffer(accept={buffer, str}) |
2088 | | The serialized database content. |
2089 | | / |
2090 | | * |
2091 | | name: str = "main" |
2092 | | Which database to reopen with the deserialization. |
2093 | | |
2094 | | Load a serialized database. |
2095 | | |
2096 | | The deserialize interface causes the database connection to disconnect from the |
2097 | | target database, and then reopen it as an in-memory database based on the given |
2098 | | serialized data. |
2099 | | |
2100 | | The deserialize interface will fail with SQLITE_BUSY if the database is |
2101 | | currently in a read transaction or is involved in a backup operation. |
2102 | | [clinic start generated code]*/ |
2103 | | |
2104 | | static PyObject * |
2105 | | deserialize_impl(pysqlite_Connection *self, Py_buffer *data, |
2106 | | const char *name) |
2107 | | /*[clinic end generated code: output=e394c798b98bad89 input=1be4ca1faacf28f2]*/ |
2108 | 3 | { |
2109 | 3 | if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) { |
2110 | 1 | return NULL; |
2111 | 1 | } |
2112 | | |
2113 | | /* Transfer ownership of the buffer to SQLite: |
2114 | | * - Move buffer from Py to SQLite |
2115 | | * - Tell SQLite to free buffer memory |
2116 | | * - Tell SQLite that it is permitted to grow the resulting database |
2117 | | * |
2118 | | * Make sure we don't overflow sqlite3_deserialize(); it accepts a signed |
2119 | | * 64-bit int as its data size argument. |
2120 | | * |
2121 | | * We can safely use sqlite3_malloc64 here, since it was introduced before |
2122 | | * the serialize APIs. |
2123 | | */ |
2124 | 2 | if (data->len > 9223372036854775807) { // (1 << 63) - 1 |
2125 | 0 | PyErr_SetString(PyExc_OverflowError, "'data' is too large"); |
2126 | 0 | return NULL; |
2127 | 0 | } |
2128 | | |
2129 | 2 | sqlite3_int64 size = (sqlite3_int64)data->len; |
2130 | 2 | unsigned char *buf = sqlite3_malloc64(size); |
2131 | 2 | if (buf == NULL) { |
2132 | 0 | return PyErr_NoMemory(); |
2133 | 0 | } |
2134 | | |
2135 | 2 | const unsigned int flags = SQLITE_DESERIALIZE_FREEONCLOSE | |
2136 | 2 | SQLITE_DESERIALIZE_RESIZEABLE; |
2137 | 2 | int rc; |
2138 | 2 | Py_BEGIN_ALLOW_THREADS |
2139 | 2 | (void)memcpy(buf, data->buf, data->len); |
2140 | 2 | rc = sqlite3_deserialize(self->db, name, buf, size, size, flags); |
2141 | 2 | Py_END_ALLOW_THREADS |
2142 | | |
2143 | 2 | if (rc != SQLITE_OK) { |
2144 | 0 | (void)_pysqlite_seterror(self->state, self->db); |
2145 | 0 | return NULL; |
2146 | 0 | } |
2147 | 2 | Py_RETURN_NONE; |
2148 | 2 | } |
2149 | | #endif // PY_SQLITE_HAVE_SERIALIZE |
2150 | | |
2151 | | |
2152 | | /*[clinic input] |
2153 | | _sqlite3.Connection.__enter__ as pysqlite_connection_enter |
2154 | | |
2155 | | Called when the connection is used as a context manager. |
2156 | | |
2157 | | Returns itself as a convenience to the caller. |
2158 | | [clinic start generated code]*/ |
2159 | | |
2160 | | static PyObject * |
2161 | | pysqlite_connection_enter_impl(pysqlite_Connection *self) |
2162 | | /*[clinic end generated code: output=457b09726d3e9dcd input=127d7a4f17e86d8f]*/ |
2163 | 44 | { |
2164 | 44 | if (!pysqlite_check_connection(self)) { |
2165 | 1 | return NULL; |
2166 | 1 | } |
2167 | 43 | return Py_NewRef((PyObject *)self); |
2168 | 44 | } |
2169 | | |
2170 | | /*[clinic input] |
2171 | | _sqlite3.Connection.__exit__ as pysqlite_connection_exit |
2172 | | |
2173 | | type as exc_type: object |
2174 | | value as exc_value: object |
2175 | | traceback as exc_tb: object |
2176 | | / |
2177 | | |
2178 | | Called when the connection is used as a context manager. |
2179 | | |
2180 | | If there was any exception, a rollback takes place; otherwise we commit. |
2181 | | [clinic start generated code]*/ |
2182 | | |
2183 | | static PyObject * |
2184 | | pysqlite_connection_exit_impl(pysqlite_Connection *self, PyObject *exc_type, |
2185 | | PyObject *exc_value, PyObject *exc_tb) |
2186 | | /*[clinic end generated code: output=0705200e9321202a input=bd66f1532c9c54a7]*/ |
2187 | 43 | { |
2188 | 43 | int commit = 0; |
2189 | 43 | PyObject* result; |
2190 | | |
2191 | 43 | if (exc_type == Py_None && exc_value == Py_None && exc_tb == Py_None) { |
2192 | 39 | commit = 1; |
2193 | 39 | result = pysqlite_connection_commit_impl(self); |
2194 | 39 | } |
2195 | 4 | else { |
2196 | 4 | result = pysqlite_connection_rollback_impl(self); |
2197 | 4 | } |
2198 | | |
2199 | 43 | if (result == NULL) { |
2200 | 1 | if (commit) { |
2201 | | /* Commit failed; try to rollback in order to unlock the database. |
2202 | | * If rollback also fails, chain the exceptions. */ |
2203 | 1 | PyObject *exc, *val, *tb; |
2204 | 1 | PyErr_Fetch(&exc, &val, &tb); |
2205 | 1 | result = pysqlite_connection_rollback_impl(self); |
2206 | 1 | if (result == NULL) { |
2207 | 0 | _PyErr_ChainExceptions(exc, val, tb); |
2208 | 0 | } |
2209 | 1 | else { |
2210 | 1 | Py_DECREF(result); |
2211 | 1 | PyErr_Restore(exc, val, tb); |
2212 | 1 | } |
2213 | 1 | } |
2214 | 1 | return NULL; |
2215 | 1 | } |
2216 | 42 | Py_DECREF(result); |
2217 | | |
2218 | 42 | Py_RETURN_FALSE; |
2219 | 43 | } |
2220 | | |
2221 | | /*[clinic input] |
2222 | | _sqlite3.Connection.setlimit as setlimit |
2223 | | |
2224 | | category: int |
2225 | | The limit category to be set. |
2226 | | limit: int |
2227 | | The new limit. If the new limit is a negative number, the limit is |
2228 | | unchanged. |
2229 | | / |
2230 | | |
2231 | | Set connection run-time limits. |
2232 | | |
2233 | | Attempts to increase a limit above its hard upper bound are silently truncated |
2234 | | to the hard upper bound. Regardless of whether or not the limit was changed, |
2235 | | the prior value of the limit is returned. |
2236 | | [clinic start generated code]*/ |
2237 | | |
2238 | | static PyObject * |
2239 | | setlimit_impl(pysqlite_Connection *self, int category, int limit) |
2240 | | /*[clinic end generated code: output=0d208213f8d68ccd input=9bd469537e195635]*/ |
2241 | 20 | { |
2242 | 20 | if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) { |
2243 | 2 | return NULL; |
2244 | 2 | } |
2245 | | |
2246 | 18 | int old_limit = sqlite3_limit(self->db, category, limit); |
2247 | 18 | if (old_limit < 0) { |
2248 | 2 | PyErr_SetString(self->ProgrammingError, "'category' is out of bounds"); |
2249 | 2 | return NULL; |
2250 | 2 | } |
2251 | 16 | return PyLong_FromLong(old_limit); |
2252 | 18 | } |
2253 | | |
2254 | | /*[clinic input] |
2255 | | _sqlite3.Connection.getlimit as getlimit |
2256 | | |
2257 | | category: int |
2258 | | The limit category to be queried. |
2259 | | / |
2260 | | |
2261 | | Get connection run-time limits. |
2262 | | [clinic start generated code]*/ |
2263 | | |
2264 | | static PyObject * |
2265 | | getlimit_impl(pysqlite_Connection *self, int category) |
2266 | | /*[clinic end generated code: output=7c3f5d11f24cecb1 input=61e0849fb4fb058f]*/ |
2267 | 4 | { |
2268 | 4 | return setlimit_impl(self, category, -1); |
2269 | 4 | } |
2270 | | |
2271 | | |
2272 | | static const char connection_doc[] = |
2273 | | PyDoc_STR("SQLite database connection object."); |
2274 | | |
2275 | | static PyGetSetDef connection_getset[] = { |
2276 | | {"isolation_level", (getter)pysqlite_connection_get_isolation_level, (setter)pysqlite_connection_set_isolation_level}, |
2277 | | {"total_changes", (getter)pysqlite_connection_get_total_changes, (setter)0}, |
2278 | | {"in_transaction", (getter)pysqlite_connection_get_in_transaction, (setter)0}, |
2279 | | {NULL} |
2280 | | }; |
2281 | | |
2282 | | static PyMethodDef connection_methods[] = { |
2283 | | PYSQLITE_CONNECTION_BACKUP_METHODDEF |
2284 | | PYSQLITE_CONNECTION_CLOSE_METHODDEF |
2285 | | PYSQLITE_CONNECTION_COMMIT_METHODDEF |
2286 | | PYSQLITE_CONNECTION_CREATE_AGGREGATE_METHODDEF |
2287 | | PYSQLITE_CONNECTION_CREATE_COLLATION_METHODDEF |
2288 | | PYSQLITE_CONNECTION_CREATE_FUNCTION_METHODDEF |
2289 | | PYSQLITE_CONNECTION_CURSOR_METHODDEF |
2290 | | PYSQLITE_CONNECTION_ENABLE_LOAD_EXTENSION_METHODDEF |
2291 | | PYSQLITE_CONNECTION_ENTER_METHODDEF |
2292 | | PYSQLITE_CONNECTION_EXECUTEMANY_METHODDEF |
2293 | | PYSQLITE_CONNECTION_EXECUTESCRIPT_METHODDEF |
2294 | | PYSQLITE_CONNECTION_EXECUTE_METHODDEF |
2295 | | PYSQLITE_CONNECTION_EXIT_METHODDEF |
2296 | | PYSQLITE_CONNECTION_INTERRUPT_METHODDEF |
2297 | | PYSQLITE_CONNECTION_ITERDUMP_METHODDEF |
2298 | | PYSQLITE_CONNECTION_LOAD_EXTENSION_METHODDEF |
2299 | | PYSQLITE_CONNECTION_ROLLBACK_METHODDEF |
2300 | | PYSQLITE_CONNECTION_SET_AUTHORIZER_METHODDEF |
2301 | | PYSQLITE_CONNECTION_SET_PROGRESS_HANDLER_METHODDEF |
2302 | | PYSQLITE_CONNECTION_SET_TRACE_CALLBACK_METHODDEF |
2303 | | SETLIMIT_METHODDEF |
2304 | | GETLIMIT_METHODDEF |
2305 | | SERIALIZE_METHODDEF |
2306 | | DESERIALIZE_METHODDEF |
2307 | | CREATE_WINDOW_FUNCTION_METHODDEF |
2308 | | BLOBOPEN_METHODDEF |
2309 | | {NULL, NULL} |
2310 | | }; |
2311 | | |
2312 | | static struct PyMemberDef connection_members[] = |
2313 | | { |
2314 | | {"Warning", T_OBJECT, offsetof(pysqlite_Connection, Warning), READONLY}, |
2315 | | {"Error", T_OBJECT, offsetof(pysqlite_Connection, Error), READONLY}, |
2316 | | {"InterfaceError", T_OBJECT, offsetof(pysqlite_Connection, InterfaceError), READONLY}, |
2317 | | {"DatabaseError", T_OBJECT, offsetof(pysqlite_Connection, DatabaseError), READONLY}, |
2318 | | {"DataError", T_OBJECT, offsetof(pysqlite_Connection, DataError), READONLY}, |
2319 | | {"OperationalError", T_OBJECT, offsetof(pysqlite_Connection, OperationalError), READONLY}, |
2320 | | {"IntegrityError", T_OBJECT, offsetof(pysqlite_Connection, IntegrityError), READONLY}, |
2321 | | {"InternalError", T_OBJECT, offsetof(pysqlite_Connection, InternalError), READONLY}, |
2322 | | {"ProgrammingError", T_OBJECT, offsetof(pysqlite_Connection, ProgrammingError), READONLY}, |
2323 | | {"NotSupportedError", T_OBJECT, offsetof(pysqlite_Connection, NotSupportedError), READONLY}, |
2324 | | {"row_factory", T_OBJECT, offsetof(pysqlite_Connection, row_factory)}, |
2325 | | {"text_factory", T_OBJECT, offsetof(pysqlite_Connection, text_factory)}, |
2326 | | {NULL} |
2327 | | }; |
2328 | | |
2329 | | static PyType_Slot connection_slots[] = { |
2330 | | {Py_tp_dealloc, connection_dealloc}, |
2331 | | {Py_tp_doc, (void *)connection_doc}, |
2332 | | {Py_tp_methods, connection_methods}, |
2333 | | {Py_tp_members, connection_members}, |
2334 | | {Py_tp_getset, connection_getset}, |
2335 | | {Py_tp_init, pysqlite_connection_init}, |
2336 | | {Py_tp_call, pysqlite_connection_call}, |
2337 | | {Py_tp_traverse, connection_traverse}, |
2338 | | {Py_tp_clear, connection_clear}, |
2339 | | {0, NULL}, |
2340 | | }; |
2341 | | |
2342 | | static PyType_Spec connection_spec = { |
2343 | | .name = MODULE_NAME ".Connection", |
2344 | | .basicsize = sizeof(pysqlite_Connection), |
2345 | | .flags = (Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | |
2346 | | Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_IMMUTABLETYPE), |
2347 | | .slots = connection_slots, |
2348 | | }; |
2349 | | |
2350 | | int |
2351 | | pysqlite_connection_setup_types(PyObject *module) |
2352 | 1 | { |
2353 | 1 | PyObject *type = PyType_FromModuleAndSpec(module, &connection_spec, NULL); |
2354 | 1 | if (type == NULL) { |
2355 | 0 | return -1; |
2356 | 0 | } |
2357 | 1 | pysqlite_state *state = pysqlite_get_state(module); |
2358 | 1 | state->ConnectionType = (PyTypeObject *)type; |
2359 | 1 | return 0; |
2360 | 1 | } |