forked from rogerbinns/apsw
-
Notifications
You must be signed in to change notification settings - Fork 0
/
apsw.c
2260 lines (1873 loc) · 59.2 KB
/
apsw.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
Another Python Sqlite Wrapper
This wrapper aims to be the minimum necessary layer over SQLite 3
itself.
It assumes we are running as 32 bit int with a 64 bit long long type
available.
See the accompanying LICENSE file.
*/
/**
.. module:: apsw
:synopsis: Python access to SQLite database library
APSW Module
***********
The module is the main interface to SQLite. Methods and data on the
module have process wide effects.
.. _type_stubs:
Type Annotations
================
Comprehensive `type annotations
<https://docs.python.org/3/library/typing.html>`__ `are included <https://github.com/rogerbinns/apsw/blob/master/apsw/__init__.pyi>`__,
and your code can be checked using tools like `mypy
<https://mypy-lang.org/>`__. You can refer to the types below for your
annotations (eg as :class:`apsw.SQLiteValue`)
Your source files should include::
from __future__ import annotations
.. note::
These types are **not** available at run time, and have no effect when
your code is running. They are only referenced when running a type
checker, or using an `IDE
<https://en.wikipedia.org/wiki/Language_Server_Protocol>`__.
You will require a recent version of Python to use the type
annotations.
.. include:: ../doc/typing.rstgen
API Reference
=============
*/
/* Fight with setuptools over ndebug */
#ifdef APSW_NO_NDEBUG
#ifdef NDEBUG
#undef NDEBUG
#endif
#endif
#ifdef APSW_USE_SQLITE_CONFIG
#include "sqlite3config.h"
#endif
/* SQLite amalgamation */
#ifdef APSW_USE_SQLITE_AMALGAMATION
#define SQLITE_OMIT_DEPRECATED
#ifndef SQLITE_MAX_ATTACHED
#define SQLITE_MAX_ATTACHED 125
#endif
#ifndef SQLITE_MAX_MMAP_SIZE
#define SQLITE_MAX_MMAP_SIZE 0x1000000000000LL
#endif
#ifndef APSW_NO_NDEBUG
#define SQLITE_API static
#define SQLITE_EXTERN static
#endif
#ifdef SQLITE_DEBUG
#define SQLITE_ENABLE_API_ARMOR 1
#endif
#include "sqlite3.c"
/* Fight with SQLite over ndebug */
#ifdef APSW_NO_NDEBUG
#ifdef NDEBUG
#undef NDEBUG
#endif
#endif
#else
/* SQLite 3 headers */
#include "sqlite3.h"
#endif
#if SQLITE_VERSION_NUMBER < 3044000
#error Your SQLite version is too old. It must be at least 3.44
#endif
/* system headers */
#include <assert.h>
#include <stdarg.h>
#ifdef _MSC_VER
#include <malloc.h>
#endif
/* Get the version number */
#include "apswversion.h"
#include "apsw.docstrings"
/* Python headers */
#define PY_SSIZE_T_CLEAN
#include <Python.h>
#include <pythread.h>
#include "structmember.h"
/* This function does nothing in regular builds, but in faultinjection
builds allows for an existing exception to be injected in callbacks */
static int MakeExistingException(void) { return 0; }
#include "faultinject.h"
#ifdef APSW_TESTFIXTURES
/* Fault injection */
#define APSW_FAULT_INJECT(faultName, good, bad) \
do \
{ \
if (APSW_Should_Fault(#faultName)) \
{ \
do \
{ \
bad; \
} while (0); \
} \
else \
{ \
do \
{ \
good; \
} while (0); \
} \
} while (0)
static int APSW_Should_Fault(const char *);
/* Are we doing 64 bit? - _LP64 is best way I can find as sizeof isn't valid in cpp #if */
#if defined(_LP64) && _LP64
#define APSW_TEST_LARGE_OBJECTS
#endif
#else /* APSW_TESTFIXTURES */
#define APSW_FAULT_INJECT(faultName, good, bad) \
do \
{ \
good; \
} while (0)
#endif
/* The module object */
static PyObject *apswmodule;
/* root exception class */
static PyObject *APSWException;
/* no change sentinel for vtable updates */
static PyTypeObject apsw_no_change_object = {
PyVarObject_HEAD_INIT(NULL, 0)
.tp_name = "apsw.no_change",
.tp_doc = Apsw_no_change_DOC,
};
typedef struct
{
PyObject_HEAD long long blobsize;
int init_was_called;
} ZeroBlobBind;
static void apsw_write_unraisable(PyObject *hookobject);
/* string constants struct */
#include "stringconstants.c"
/* Make various versions of Python code compatible with each other */
#include "pyutil.c"
/* Augment tracebacks */
#include "traceback.c"
/* various utility functions and macros */
#include "util.c"
/* Argument parsing helpers */
#include "argparse.c"
/* Exceptions we can raise */
#include "exceptions.c"
/* The statement cache */
#include "statementcache.c"
/* connections */
#include "connection.c"
/* backup */
#include "backup.c"
/* Zeroblob and blob */
#include "blob.c"
static int allow_missing_dict_bindings = 0;
/* cursors */
#include "cursor.c"
/* virtual tables */
#include "vtable.c"
/* virtual file system */
#include "vfs.c"
/* constants */
#include "constants.c"
/* MODULE METHODS */
/** .. method:: sqlite_lib_version() -> str
Returns the version of the SQLite library. This value is queried at
run time from the library so if you use shared libraries it will be
the version in the shared library.
-* sqlite3_libversion
*/
static PyObject *
get_sqlite_version(void)
{
return PyUnicode_FromString(sqlite3_libversion());
}
/** .. method:: sqlite3_sourceid() -> str
Returns the exact checkin information for the SQLite 3 source
being used.
-* sqlite3_sourceid
*/
static PyObject *
get_sqlite3_sourceid(void)
{
return PyUnicode_FromString(sqlite3_sourceid());
}
/** .. method:: apsw_version() -> str
Returns the APSW version.
*/
static PyObject *
get_apsw_version(void)
{
return PyUnicode_FromString(APSW_VERSION);
}
/** .. method:: enable_shared_cache(enable: bool) -> None
`Discouraged
<https://sqlite.org/sharedcache.html#use_of_shared_cache_is_discouraged>`__.
-* sqlite3_enable_shared_cache
*/
static PyObject *
enable_shared_cache(PyObject *Py_UNUSED(self), PyObject *const *fast_args, Py_ssize_t fast_nargs, PyObject *fast_kwnames)
{
int enable = 0, res;
{
Apsw_enable_shared_cache_CHECK;
ARG_PROLOG(1, Apsw_enable_shared_cache_KWNAMES);
ARG_MANDATORY ARG_bool(enable);
ARG_EPILOG(NULL, Apsw_enable_shared_cache_USAGE, );
}
res = sqlite3_enable_shared_cache(enable);
SET_EXC(res, NULL);
if (res != SQLITE_OK)
return NULL;
Py_RETURN_NONE;
}
/** .. method:: connections() -> list[Connection]
Returns a list of the connections
*/
static PyObject *the_connections;
static PyObject *
apsw_connections(PyObject *Py_UNUSED(self))
{
Py_ssize_t i;
PyObject *res = PyList_New(0), *item = NULL;
for (i = 0; i < PyList_GET_SIZE(the_connections); i++)
{
if (PyWeakref_GetRef(PyList_GET_ITEM(the_connections, i), &item) < 0)
goto fail;
if (item)
{
if (PyList_Append(res, item))
goto fail;
Py_CLEAR(item);
}
}
return res;
fail:
Py_XDECREF(res);
Py_XDECREF(item);
return NULL;
}
static void
apsw_connection_remove(Connection *con)
{
Py_ssize_t i;
for (i = 0; i < PyList_GET_SIZE(the_connections);)
{
PyObject *wr = PyList_GET_ITEM(the_connections, i);
PyObject *wo = NULL;
if (PyWeakref_GetRef(wr, &wo) < 0)
{
apsw_write_unraisable(NULL);
continue;
}
if (!wo || wo == (PyObject *)con)
{
if (PyList_SetSlice(the_connections, i, i + 1, NULL))
apsw_write_unraisable(NULL);
if (!wo)
continue;
Py_DECREF(wo);
return;
}
Py_DECREF(wo);
i++;
}
}
static int
apsw_connection_add(Connection *con)
{
PyObject *weakref = PyWeakref_NewRef((PyObject *)con, NULL);
if (!weakref)
return -1;
int res = PyList_Append(the_connections, weakref);
Py_DECREF(weakref);
return res;
}
/** .. method:: initialize() -> None
It is unlikely you will want to call this method as SQLite automatically initializes.
-* sqlite3_initialize
*/
static PyObject *
initialize(void)
{
int res;
res = sqlite3_initialize();
if (res)
{
SET_EXC(res, NULL);
return NULL;
}
Py_RETURN_NONE;
}
/** .. method:: shutdown() -> None
It is unlikely you will want to call this method and there is no
need to do so. It is a **really** bad idea to call it unless you
are absolutely sure all :class:`connections <Connection>`,
:class:`blobs <Blob>`, :class:`cursors <Cursor>`, :class:`vfs <VFS>`
etc have been closed, deleted and garbage collected.
-* sqlite3_shutdown
*/
#ifdef APSW_FORK_CHECKER
static void free_fork_checker(void);
#endif
static PyObject *
sqliteshutdown(void)
{
int res;
res = sqlite3_shutdown();
SET_EXC(res, NULL);
if (res != SQLITE_OK)
return NULL;
#ifdef APSW_FORK_CHECKER
free_fork_checker();
#endif
Py_RETURN_NONE;
}
/** .. method:: config(op: int, *args: Any) -> None
:param op: A `configuration operation <https://sqlite.org/c3ref/c_config_chunkalloc.html>`_
:param args: Zero or more arguments as appropriate for *op*
Some operations don't make sense from a Python program. All the
remaining are supported.
-* sqlite3_config
*/
static PyObject *logger_cb = NULL;
static void
apsw_logger(void *arg, int errcode, const char *message)
{
PyGILState_STATE gilstate;
PyObject *res = NULL;
gilstate = PyGILState_Ensure();
MakeExistingException();
assert(arg == logger_cb);
assert(arg);
PY_ERR_FETCH(exc);
PyObject *vargs[] = {NULL, PyLong_FromLong(errcode), PyUnicode_FromString(message)};
if (vargs[1] && vargs[2])
res = PyObject_Vectorcall(arg, vargs + 1, 2 | PY_VECTORCALL_ARGUMENTS_OFFSET, NULL);
Py_XDECREF(vargs[1]);
Py_XDECREF(vargs[2]);
if (!res)
{
/* apsw_write_unraisable writes to sqlite3_log so if we are in too
much recursion, avoid going further */
if (PyErr_ExceptionMatches(PyExc_RecursionError))
PyErr_Clear();
else
{
AddTraceBackHere(__FILE__, __LINE__, "apsw_sqlite3_log_receiver",
"{s: O, s: i, s: s}",
"logger", OBJ(arg),
"errcode", errcode,
"message", message);
apsw_write_unraisable(NULL);
}
}
else
Py_DECREF(res);
if (PY_ERR_NOT_NULL(exc))
PY_ERR_RESTORE(exc);
PyGILState_Release(gilstate);
}
static PyObject *
config(PyObject *Py_UNUSED(self), PyObject *args)
{
int res, optdup;
int opt;
if (PyTuple_GET_SIZE(args) < 1 || !PyLong_Check(PyTuple_GET_ITEM(args, 0)))
return PyErr_Format(PyExc_TypeError, "There should be at least one argument with the first being a number");
opt = PyLong_AsInt(PyTuple_GET_ITEM(args, 0));
if (PyErr_Occurred())
return NULL;
switch (opt)
{
case SQLITE_CONFIG_SINGLETHREAD:
case SQLITE_CONFIG_MULTITHREAD:
case SQLITE_CONFIG_SERIALIZED:
if (!PyArg_ParseTuple(args, "i", &optdup))
return NULL;
assert(opt == optdup);
res = sqlite3_config(opt);
break;
case SQLITE_CONFIG_PCACHE_HDRSZ:
{
int outval = -1;
if (!PyArg_ParseTuple(args, "i", &optdup))
return NULL;
assert(opt == optdup);
res = sqlite3_config(opt, &outval);
if (res)
{
SET_EXC(res, NULL);
return NULL;
}
return PyLong_FromLong(outval);
}
case SQLITE_CONFIG_URI:
case SQLITE_CONFIG_MEMSTATUS:
case SQLITE_CONFIG_COVERING_INDEX_SCAN:
case SQLITE_CONFIG_PMASZ:
case SQLITE_CONFIG_STMTJRNL_SPILL:
case SQLITE_CONFIG_SORTERREF_SIZE:
case SQLITE_CONFIG_LOOKASIDE:
case SQLITE_CONFIG_SMALL_MALLOC:
{
int intval;
if (!PyArg_ParseTuple(args, "ii", &optdup, &intval))
return NULL;
assert(opt == optdup);
res = sqlite3_config(opt, intval);
break;
}
case SQLITE_CONFIG_LOG:
{
PyObject *logger;
if (!PyArg_ParseTuple(args, "iO", &optdup, &logger))
return NULL;
if (Py_IsNone(logger))
{
res = sqlite3_config(opt, NULL);
if (res == SQLITE_OK)
Py_CLEAR(logger_cb);
}
else if (!PyCallable_Check(logger))
{
return PyErr_Format(PyExc_TypeError, "Logger should be None or a callable");
}
else
{
res = sqlite3_config((int)opt, apsw_logger, logger);
if (res == SQLITE_OK)
{
Py_CLEAR(logger_cb);
logger_cb = Py_NewRef(logger);
}
}
break;
}
case SQLITE_CONFIG_MMAP_SIZE:
{
sqlite3_int64 default_limit, max_limit;
if (!PyArg_ParseTuple(args, "iLL", &optdup, &default_limit, &max_limit))
return NULL;
assert(opt == optdup);
res = sqlite3_config(opt, default_limit, max_limit);
break;
}
case SQLITE_CONFIG_MEMDB_MAXSIZE:
{
sqlite3_int64 limit;
if (!PyArg_ParseTuple(args, "iL", &optdup, &limit))
return NULL;
assert(opt == optdup);
res = sqlite3_config(opt, limit);
break;
}
default:
return PyErr_Format(PyExc_TypeError, "Unknown config type %d", (int)opt);
}
SET_EXC(res, NULL);
if (res != SQLITE_OK)
return NULL;
Py_RETURN_NONE;
}
/** .. method:: memory_used() -> int
Returns the amount of memory SQLite is currently using.
.. seealso::
:meth:`status`
-* sqlite3_memory_used
*/
static PyObject *
memory_used(void)
{
return PyLong_FromLongLong(sqlite3_memory_used());
}
/** .. method:: memory_high_water(reset: bool = False) -> int
Returns the maximum amount of memory SQLite has used. If *reset* is
True then the high water mark is reset to the current value.
.. seealso::
:meth:`status`
-* sqlite3_memory_highwater
*/
static PyObject *
memory_high_water(PyObject *Py_UNUSED(self), PyObject *const *fast_args, Py_ssize_t fast_nargs, PyObject *fast_kwnames)
{
int reset = 0;
{
Apsw_memory_high_water_CHECK;
ARG_PROLOG(1, Apsw_memory_high_water_KWNAMES);
ARG_OPTIONAL ARG_bool(reset);
ARG_EPILOG(NULL, Apsw_memory_high_water_USAGE, );
}
return PyLong_FromLongLong(sqlite3_memory_highwater(reset));
}
/** .. method:: soft_heap_limit(limit: int) -> int
Requests SQLite try to keep memory usage below *limit* bytes and
returns the previous limit.
.. seealso::
:meth:`hard_heap_limit`
-* sqlite3_soft_heap_limit64
*/
static PyObject *
soft_heap_limit(PyObject *Py_UNUSED(self), PyObject *const *fast_args, Py_ssize_t fast_nargs, PyObject *fast_kwnames)
{
sqlite3_int64 limit, oldlimit;
{
Apsw_soft_heap_limit_CHECK;
ARG_PROLOG(1, Apsw_soft_heap_limit_KWNAMES);
ARG_MANDATORY ARG_int64(limit);
ARG_EPILOG(NULL, Apsw_soft_heap_limit_USAGE, );
}
oldlimit = sqlite3_soft_heap_limit64(limit);
return PyLong_FromLongLong(oldlimit);
}
/** .. method:: hard_heap_limit(limit: int) -> int
Enforces SQLite keeping memory usage below *limit* bytes and
returns the previous limit.
.. seealso::
:meth:`soft_heap_limit`
-* sqlite3_hard_heap_limit64
*/
static PyObject *
apsw_hard_heap_limit(PyObject *Py_UNUSED(self), PyObject *const *fast_args, Py_ssize_t fast_nargs, PyObject *fast_kwnames)
{
sqlite3_int64 limit, oldlimit;
{
Apsw_hard_heap_limit_CHECK;
ARG_PROLOG(1, Apsw_hard_heap_limit_KWNAMES);
ARG_MANDATORY ARG_int64(limit);
ARG_EPILOG(NULL, Apsw_hard_heap_limit_USAGE, );
}
oldlimit = sqlite3_hard_heap_limit64(limit);
return PyLong_FromLongLong(oldlimit);
}
/** .. method:: randomness(amount: int) -> bytes
Gets random data from SQLite's random number generator.
:param amount: How many bytes to return
-* sqlite3_randomness
*/
static PyObject *
randomness(PyObject *Py_UNUSED(self), PyObject *const *fast_args, Py_ssize_t fast_nargs, PyObject *fast_kwnames)
{
int amount;
PyObject *bytes;
{
Apsw_randomness_CHECK;
ARG_PROLOG(1, Apsw_randomness_KWNAMES);
ARG_MANDATORY ARG_int(amount);
ARG_EPILOG(NULL, Apsw_randomness_USAGE, );
}
if (amount < 0)
return PyErr_Format(PyExc_ValueError, "Can't have negative number of bytes");
bytes = PyBytes_FromStringAndSize(NULL, amount);
if (!bytes)
return bytes;
sqlite3_randomness(amount, PyBytes_AS_STRING(bytes));
return bytes;
}
/** .. method:: release_memory(amount: int) -> int
Requests SQLite try to free *amount* bytes of memory. Returns how
many bytes were freed.
-* sqlite3_release_memory
*/
static PyObject *
release_memory(PyObject *Py_UNUSED(self), PyObject *const *fast_args, Py_ssize_t fast_nargs, PyObject *fast_kwnames)
{
int amount;
{
Apsw_release_memory_CHECK;
ARG_PROLOG(1, Apsw_release_memory_KWNAMES);
ARG_MANDATORY ARG_int(amount);
ARG_EPILOG(NULL, Apsw_release_memory_USAGE, );
}
return PyLong_FromLong(sqlite3_release_memory(amount));
}
/** .. method:: status(op: int, reset: bool = False) -> tuple[int, int]
Returns current and highwater measurements.
:param op: A `status parameter <https://sqlite.org/c3ref/c_status_malloc_size.html>`_
:param reset: If *True* then the highwater is set to the current value
:returns: A tuple of current value and highwater value
.. seealso::
* :meth:`Connection.status` for statistics about a :class:`Connection`
* :ref:`Status example <example_status>`
-* sqlite3_status64
*/
static PyObject *
status(PyObject *Py_UNUSED(self), PyObject *const *fast_args, Py_ssize_t fast_nargs, PyObject *fast_kwnames)
{
int res, op, reset = 0;
sqlite3_int64 current = 0, highwater = 0;
{
Apsw_status_CHECK;
ARG_PROLOG(2, Apsw_status_KWNAMES);
ARG_MANDATORY ARG_int(op);
ARG_OPTIONAL ARG_bool(reset);
ARG_EPILOG(NULL, Apsw_status_USAGE, );
}
res = sqlite3_status64(op, ¤t, &highwater, reset);
SET_EXC(res, NULL);
if (res != SQLITE_OK)
return NULL;
return Py_BuildValue("(LL)", current, highwater);
}
/** .. method:: vfs_names() -> list[str]
Returns a list of the currently installed :ref:`vfs <vfs>`. The first
item in the list is the default vfs.
-* sqlite3_vfs_find
*/
static PyObject *
vfs_names(PyObject *Py_UNUSED(self))
{
PyObject *result = NULL, *str = NULL;
int res;
sqlite3_vfs *vfs = sqlite3_vfs_find(0);
result = PyList_New(0);
if (!result)
goto error;
while (vfs)
{
str = convertutf8string(vfs->zName);
if (!str)
goto error;
res = PyList_Append(result, str);
if (res)
goto error;
Py_DECREF(str);
vfs = vfs->pNext;
}
return result;
error:
Py_XDECREF(str);
Py_XDECREF(result);
return NULL;
}
/* macros to build the format string and values. int, string and pointers */
#undef I
#undef S
#undef P
#define I(n) #n, vfs->n
#define S(n) #n, vfs->n
#define P(n) #n, PyLong_FromVoidPtr, vfs->n
#define VFS1_BUILD "si si si ss sO& sO& sO& sO& sO& sO& sO& sO& sO& sO& sO& sO& sO&"
#define VFS1_FIELDS I(iVersion), I(szOsFile), I(mxPathname), S(zName), P(pAppData), \
P(xOpen), P(xDelete), P(xAccess), P(xFullPathname), P(xDlOpen), P(xDlError), \
P(xDlSym), P(xDlClose), P(xRandomness), P(xSleep), P(xGetLastError), P(xCurrentTime)
#define VFS2_BUILD "sO&"
#define VFS2_FIELDS P(xCurrentTimeInt64)
#define VFS3_BUILD "sO& sO& sO&"
#define VFS3_FIELDS P(xSetSystemCall), P(xGetSystemCall), P(xNextSystemCall)
/** .. method:: vfs_details() -> list[dict[str, int | str]]
Returns a list with details of each :ref:`vfs <vfs>`. The detail is a
dictionary with the keys being the names of the `sqlite3_vfs
<https://sqlite.org/c3ref/vfs.html>`__ data structure, and their
corresponding values.
Pointers are converted using `PyLong_FromVoidPtr
<https://docs.python.org/3/c-api/long.html?highlight=voidptr#c.PyLong_FromVoidPtr>`__.
-* sqlite3_vfs_find
*/
static PyObject *
vfs_details(PyObject *Py_UNUSED(self))
{
PyObject *result, *dict;
sqlite3_vfs *vfs = sqlite3_vfs_find(0);
int res;
result = PyList_New(0);
if (!result)
return NULL;
while (vfs)
{
switch (vfs->iVersion)
{
case 0: /* some older sqlite source does this */
case 1:
dict = Py_BuildValue("{" VFS1_BUILD "}", VFS1_FIELDS);
break;
case 2:
dict = Py_BuildValue("{" VFS1_BUILD VFS2_BUILD "}", VFS1_FIELDS, VFS2_FIELDS);
break;
default: /* handle 4+ */
case 3:
dict = Py_BuildValue("{" VFS1_BUILD VFS2_BUILD VFS3_BUILD "}", VFS1_FIELDS, VFS2_FIELDS, VFS3_FIELDS);
}
if (!dict)
{
Py_DECREF(result);
return NULL;
}
res = PyList_Append(result, dict);
Py_DECREF(dict);
if (res != 0)
{
Py_DECREF(result);
return NULL;
}
vfs = vfs->pNext;
}
return result;
}
#undef I
#undef S
#undef P
/** .. method:: exception_for(code: int) -> Exception
If you would like to raise an exception that corresponds to a
particular SQLite `error code
<https://sqlite.org/c3ref/c_abort.html>`_ then call this function.
It also understands `extended error codes
<https://sqlite.org/c3ref/c_ioerr_access.html>`_.
For example to raise `SQLITE_IOERR_ACCESS <https://sqlite.org/c3ref/c_ioerr_access.html>`_::
raise apsw.exception_for(apsw.SQLITE_IOERR_ACCESS)
*/
static PyObject *
get_apsw_exception_for(PyObject *Py_UNUSED(self), PyObject *const *fast_args, Py_ssize_t fast_nargs, PyObject *fast_kwnames)
{
int code = 0, i;
PyObject *result = NULL, *tmp = NULL;
{
Apsw_exception_for_CHECK;
ARG_PROLOG(1, Apsw_exception_for_KWNAMES);
ARG_MANDATORY ARG_int(code);
ARG_EPILOG(NULL, Apsw_exception_for_USAGE, );
}
for (i = 0; exc_descriptors[i].name; i++)
if (exc_descriptors[i].code == (code & 0xff))
{
PyObject *vargs[] = {NULL};
result = PyObject_Vectorcall(exc_descriptors[i].cls, vargs + 1, 0 | PY_VECTORCALL_ARGUMENTS_OFFSET, NULL);
if (!result)
return result;
break;
}
if (!result)
return PyErr_Format(PyExc_ValueError, "%d is not a known error code", code);
tmp = PyLong_FromLong(code);
if (!tmp)
goto error;
if (0 != PyObject_SetAttr(result, apst.extendedresult, tmp))
goto error;
Py_DECREF(tmp);
tmp = PyLong_FromLong(code & 0xff);
if (!tmp)
goto error;
if (0 != PyObject_SetAttr(result, apst.result, tmp))
goto error;
Py_DECREF(tmp);
return result;
error:
Py_XDECREF(tmp);
Py_CLEAR(result);
return NULL;
}
/** .. method:: complete(statement: str) -> bool
Returns True if the input string comprises one or more complete SQL
statements by looking for an unquoted trailing semi-colon. It does
not consider comments or blank lines to be complete.
An example use would be if you were prompting the user for SQL
statements and needed to know if you had a whole statement, or
needed to ask for another line::
statement = input("SQL> ")
while not apsw.complete(statement):
more = input(" .. ")
statement = statement + "\\n" + more
-* sqlite3_complete
*/
static PyObject *
apswcomplete(PyObject *Py_UNUSED(self), PyObject *const *fast_args, Py_ssize_t fast_nargs, PyObject *fast_kwnames)
{
const char *statement = NULL;
int res;
{
Apsw_complete_CHECK;
ARG_PROLOG(1, Apsw_complete_KWNAMES);
ARG_MANDATORY ARG_str(statement);
ARG_EPILOG(NULL, Apsw_complete_USAGE, );
}
res = sqlite3_complete(statement);
if (res)
Py_RETURN_TRUE;
Py_RETURN_FALSE;
}
#ifdef APSW_TESTFIXTURES
static PyObject *
apsw_fini(PyObject *Py_UNUSED(self))
{
Py_XDECREF(tls_errmsg);
fini_apsw_strings();
Py_RETURN_NONE;
}
#endif
#ifdef APSW_FORK_CHECKER
/*
We want to verify that SQLite objects are not used across forks.
One way is to modify all calls to SQLite to do the checking but
this is a pain as well as a performance hit. Instead we use the
approach of providing an alternative mutex implementation since
pretty much every SQLite API call takes and releases a mutex.