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