1 /*
2 Another Python Sqlite Wrapper
3
4 This wrapper aims to be the minimum necessary layer over SQLite 3
5 itself.
6
7 It assumes we are running as 32 bit int with a 64 bit long long type
8 available.
9
10 See the accompanying LICENSE file.
11 */
12
13 /**
14
15 .. module:: apsw
16 :synopsis: Python access to SQLite database library
17
18 APSW Module
19 ***********
20
21 The module is the main interface to SQLite. Methods and data on the
22 module have process wide effects. You can instantiate the
23 :class:`Connection` and :class:`zeroblob` objects using
24 :meth:`Connection` and :meth:`zeroblob` respectively.
25
26 API Reference
27 =============
28 */
29
30 /* Fight with setuptools over ndebug */
31 #ifdef APSW_NO_NDEBUG
32 #ifdef NDEBUG
33 #undef NDEBUG
34 #endif
35 #endif
36
37 #ifdef APSW_USE_SQLITE_CONFIG
38 #include APSW_USE_SQLITE_CONFIG
39 #endif
40
41 /* SQLite amalgamation */
42 #ifdef APSW_USE_SQLITE_AMALGAMATION
43 /* See SQLite ticket 2554 */
44 #define SQLITE_API static
45 #define SQLITE_EXTERN static
46 #define SQLITE_ENABLE_API_ARMOR 1
47 #include APSW_USE_SQLITE_AMALGAMATION
48 #undef small
49
50 /* Fight with SQLite over ndebug */
51 #ifdef APSW_NO_NDEBUG
52 #ifdef NDEBUG
53 #undef NDEBUG
54 #endif
55 #endif
56
57 #else
58 /* SQLite 3 headers */
59 #include "sqlite3.h"
60 #endif
61
62 #if SQLITE_VERSION_NUMBER < 3034000
63 #error Your SQLite version is too old. It must be at least 3.34
64 #endif
65
66 /* system headers */
67 #include <assert.h>
68 #include <stdarg.h>
69
70 /* Get the version number */
71 #include "apswversion.h"
72
73 /* Python headers */
74 #include <Python.h>
75 #include <pythread.h>
76 #include "structmember.h"
77
78 #ifdef APSW_TESTFIXTURES
79 /* Fault injection */
80 #define APSW_FAULT_INJECT(name, good, bad) \
81 do \
82 { \
83 if (APSW_Should_Fault(#name)) \
84 { \
85 do \
86 { \
87 bad; \
88 } while (0); \
89 } \
90 else \
91 { \
92 do \
93 { \
94 good; \
95 } while (0); \
96 } \
97 } while (0)
98
99 static int APSW_Should_Fault(const char *);
100
101 /* Are we Python 2.x (x>=5) and doing 64 bit? - _LP64 is best way I can find as sizeof isn't valid in cpp #if */
102 #if PY_VERSION_HEX >= 0x02050000 && defined(_LP64) && _LP64
103 #define APSW_TEST_LARGE_OBJECTS
104 #endif
105
106 #else /* APSW_TESTFIXTURES */
107 #define APSW_FAULT_INJECT(name, good, bad) \
108 do \
109 { \
110 good; \
111 } while (0)
112
113 #endif
114
115 /* The encoding we use with SQLite. SQLite supports either utf8 or 16
116 bit unicode (host byte order). If the latter is used then all
117 functions have "16" appended to their name. The encoding used also
118 affects how strings are stored in the database. We use utf8 since
119 it is more space efficient, and Python can't make its mind up about
120 Unicode (it uses 16 or 32 bit unichars and often likes to use Byte
121 Order Markers as well). */
122 #define STRENCODING "utf-8"
123
124 /* The module object */
125 static PyObject *apswmodule;
126
127 /* Everything except the module itself is in separate files */
128 #ifdef PYPY_VERSION
129 #include "pypycompat.c"
130 #endif
131
132 /* Augment tracebacks */
133 #include "traceback.c"
134
135 /* Make various versions of Python code compatible with each other */
136 #include "pyutil.c"
137
138 /* Exceptions we can raise */
139 #include "exceptions.c"
140
141 /* various utility functions and macros */
142 #include "util.c"
143
144 /* buffer used in statement cache */
145 #include "apswbuffer.c"
146
147 /* The statement cache */
148 #include "statementcache.c"
149
150 /* connections */
151 #include "connection.c"
152
153 /* backup */
154 #include "backup.c"
155
156 /* Zeroblob and blob */
157 #include "blob.c"
158
159 /* cursors */
160 #include "cursor.c"
161
162 /* virtual tables */
163 #include "vtable.c"
164
165 /* virtual file system */
166 #include "vfs.c"
167
168 /* MODULE METHODS */
169
170 /** .. method:: sqlitelibversion() -> string
171
172 Returns the version of the SQLite library. This value is queried at
173 run time from the library so if you use shared libraries it will be
174 the version in the shared library.
175
176 -* sqlite3_libversion
177 */
178
179 static PyObject *
getsqliteversion(void)180 getsqliteversion(void)
181 {
182 return MAKESTR(sqlite3_libversion());
183 }
184
185 /** .. method:: sqlite3_sourceid() -> string
186
187 Returns the exact checkin information for the SQLite 3 source
188 being used.
189
190 -* sqlite3_sourceid
191 */
192
193 static PyObject *
get_sqlite3_sourceid(void)194 get_sqlite3_sourceid(void)
195 {
196 return MAKESTR(sqlite3_sourceid());
197 }
198
199 /** .. method:: apswversion() -> string
200
201 Returns the APSW version.
202 */
203 static PyObject *
getapswversion(void)204 getapswversion(void)
205 {
206 return MAKESTR(APSW_VERSION);
207 }
208
209 /** .. method:: enablesharedcache(bool)
210
211 If you use the same :class:`Connection` across threads or use
212 multiple :class:`connections <Connection>` accessing the same file,
213 then SQLite can `share the cache between them
214 <https://sqlite.org/sharedcache.html>`_. It is :ref:`not
215 recommended <sharedcache>` that you use this.
216
217 -* sqlite3_enable_shared_cache
218 */
219 static PyObject *
enablesharedcache(APSW_ARGUNUSED PyObject * self,PyObject * args)220 enablesharedcache(APSW_ARGUNUSED PyObject *self, PyObject *args)
221 {
222 int setting, res;
223 if (!PyArg_ParseTuple(args, "i:enablesharedcache(boolean)", &setting))
224 return NULL;
225
226 APSW_FAULT_INJECT(EnableSharedCacheFail, res = sqlite3_enable_shared_cache(setting), res = SQLITE_NOMEM);
227 SET_EXC(res, NULL);
228
229 if (res != SQLITE_OK)
230 return NULL;
231
232 Py_RETURN_NONE;
233 }
234
235 /** .. method:: initialize()
236
237 It is unlikely you will want to call this method as SQLite automatically initializes.
238
239 -* sqlite3_initialize
240 */
241
242 static PyObject *
initialize(void)243 initialize(void)
244 {
245 int res;
246
247 res = sqlite3_initialize();
248 APSW_FAULT_INJECT(InitializeFail, , res = SQLITE_NOMEM);
249 SET_EXC(res, NULL);
250
251 if (res != SQLITE_OK)
252 return NULL;
253
254 Py_RETURN_NONE;
255 }
256
257 /** .. method:: shutdown()
258
259 It is unlikely you will want to call this method and there is no
260 need to do so. It is a **really** bad idea to call it unless you
261 are absolutely sure all :class:`connections <Connection>`,
262 :class:`blobs <blob>`, :class:`cursors <Cursor>`, :class:`vfs <VFS>`
263 etc have been closed, deleted and garbage collected.
264
265 -* sqlite3_shutdown
266 */
267
268 static PyObject *
sqliteshutdown(void)269 sqliteshutdown(void)
270 {
271 int res;
272
273 APSW_FAULT_INJECT(ShutdownFail, res = sqlite3_shutdown(), res = SQLITE_NOMEM);
274 SET_EXC(res, NULL);
275
276 if (res != SQLITE_OK)
277 return NULL;
278
279 Py_RETURN_NONE;
280 }
281
282 /** .. method:: config(op[, *args])
283
284 :param op: A `configuration operation <https://sqlite.org/c3ref/c_config_chunkalloc.html>`_
285 :param args: Zero or more arguments as appropriate for *op*
286
287 Many operations don't make sense from a Python program. The
288 following configuration operations are supported: SQLITE_CONFIG_LOG,
289 SQLITE_CONFIG_SINGLETHREAD, SQLITE_CONFIG_MULTITHREAD,
290 SQLITE_CONFIG_SERIALIZED, SQLITE_CONFIG_URI, SQLITE_CONFIG_MEMSTATUS,
291 SQLITE_CONFIG_COVERING_INDEX_SCAN, SQLITE_CONFIG_PCACHE_HDRSZ,
292 SQLITE_CONFIG_PMASZ, and SQLITE_CONFIG_STMTJRNL_SPILL.
293
294 See :ref:`tips <diagnostics_tips>` for an example of how to receive
295 log messages (SQLITE_CONFIG_LOG)
296
297 -* sqlite3_config
298 */
299
300 #ifdef EXPERIMENTAL
301 static PyObject *logger_cb = NULL;
302
303 static void
apsw_logger(void * arg,int errcode,const char * message)304 apsw_logger(void *arg, int errcode, const char *message)
305 {
306 PyGILState_STATE gilstate;
307 PyObject *etype = NULL, *evalue = NULL, *etraceback = NULL;
308 PyObject *res = NULL;
309 PyObject *msgaspystring = NULL;
310
311 gilstate = PyGILState_Ensure();
312 assert(arg == logger_cb);
313 assert(arg);
314 PyErr_Fetch(&etype, &evalue, &etraceback);
315
316 msgaspystring = convertutf8string(message);
317 if (msgaspystring)
318 res = PyEval_CallFunction(arg, "iO", errcode, msgaspystring);
319 if (!res)
320 {
321 AddTraceBackHere(__FILE__, __LINE__, "Call_Logger",
322 "{s: O, s: i, s: s}",
323 "logger", arg,
324 "errcode", errcode,
325 "message", message);
326 apsw_write_unraiseable(NULL);
327 }
328 else
329 Py_DECREF(res);
330
331 Py_XDECREF(msgaspystring);
332 if (etype || evalue || etraceback)
333 PyErr_Restore(etype, evalue, etraceback);
334 PyGILState_Release(gilstate);
335 }
336
337 static PyObject *
config(APSW_ARGUNUSED PyObject * self,PyObject * args)338 config(APSW_ARGUNUSED PyObject *self, PyObject *args)
339 {
340 int res, optdup;
341 long opt;
342
343 if (PyTuple_GET_SIZE(args) < 1 || !PyIntLong_Check(PyTuple_GET_ITEM(args, 0)))
344 return PyErr_Format(PyExc_TypeError, "There should be at least one argument with the first being a number");
345
346 opt = PyIntLong_AsLong(PyTuple_GET_ITEM(args, 0));
347 if (PyErr_Occurred())
348 return NULL;
349
350 switch (opt)
351 {
352 case SQLITE_CONFIG_SINGLETHREAD:
353 case SQLITE_CONFIG_MULTITHREAD:
354 case SQLITE_CONFIG_SERIALIZED:
355 case SQLITE_CONFIG_URI:
356 if (!PyArg_ParseTuple(args, "i", &optdup))
357 return NULL;
358 assert(opt == optdup);
359 res = sqlite3_config((int)opt);
360 break;
361
362 case SQLITE_CONFIG_PCACHE_HDRSZ:
363 {
364 int outval = -1;
365 if (!PyArg_ParseTuple(args, "i", &optdup))
366 return NULL;
367 assert(opt == optdup);
368 res = sqlite3_config((int)opt, &outval);
369 if (res)
370 {
371 SET_EXC(res, NULL);
372 return NULL;
373 }
374 return PyInt_FromLong(outval);
375 }
376
377 case SQLITE_CONFIG_MEMSTATUS:
378 case SQLITE_CONFIG_COVERING_INDEX_SCAN:
379 case SQLITE_CONFIG_PMASZ:
380 case SQLITE_CONFIG_STMTJRNL_SPILL:
381 {
382 int intval;
383 if (!PyArg_ParseTuple(args, "ii", &optdup, &intval))
384 return NULL;
385 assert(opt == optdup);
386 res = sqlite3_config((int)opt, intval);
387 break;
388 }
389
390 case SQLITE_CONFIG_LOG:
391 {
392 PyObject *logger;
393 if (!PyArg_ParseTuple(args, "iO", &optdup, &logger))
394 return NULL;
395 if (logger == Py_None)
396 {
397 res = sqlite3_config((int)opt, NULL);
398 if (res == SQLITE_OK)
399 Py_CLEAR(logger_cb);
400 }
401 else if (!PyCallable_Check(logger))
402 {
403 return PyErr_Format(PyExc_TypeError, "Logger should be None or a callable");
404 }
405 else
406 {
407 res = sqlite3_config((int)opt, apsw_logger, logger);
408 if (res == SQLITE_OK)
409 {
410 Py_CLEAR(logger_cb);
411 logger_cb = logger;
412 Py_INCREF(logger);
413 }
414 }
415 break;
416 }
417
418 default:
419 return PyErr_Format(PyExc_TypeError, "Unknown config type %d", (int)opt);
420 }
421
422 SET_EXC(res, NULL);
423
424 if (res != SQLITE_OK)
425 return NULL;
426
427 Py_RETURN_NONE;
428 }
429 #endif /* EXPERIMENTAL */
430
431 /** .. method:: memoryused() -> int
432
433 Returns the amount of memory SQLite is currently using.
434
435 .. seealso::
436 :meth:`status`
437
438
439 -* sqlite3_memory_used
440 */
441 static PyObject *
memoryused(void)442 memoryused(void)
443 {
444 return PyLong_FromLongLong(sqlite3_memory_used());
445 }
446
447 /** .. method:: memoryhighwater(reset=False) -> int
448
449 Returns the maximum amount of memory SQLite has used. If *reset* is
450 True then the high water mark is reset to the current value.
451
452 .. seealso::
453
454 :meth:`status`
455
456 -* sqlite3_memory_highwater
457 */
458 static PyObject *
memoryhighwater(APSW_ARGUNUSED PyObject * self,PyObject * args)459 memoryhighwater(APSW_ARGUNUSED PyObject *self, PyObject *args)
460 {
461 int reset = 0;
462
463 if (!PyArg_ParseTuple(args, "|i:memoryhighwater(reset=False)", &reset))
464 return NULL;
465
466 return PyLong_FromLongLong(sqlite3_memory_highwater(reset));
467 }
468
469 /** .. method:: softheaplimit(bytes) -> oldlimit
470
471 Requests SQLite try to keep memory usage below *bytes* bytes and
472 returns the previous setting.
473
474 -* sqlite3_soft_heap_limit64
475 */
476 static PyObject *
softheaplimit(APSW_ARGUNUSED PyObject * self,PyObject * args)477 softheaplimit(APSW_ARGUNUSED PyObject *self, PyObject *args)
478 {
479 long long limit, oldlimit;
480
481 if (!PyArg_ParseTuple(args, "L", &limit))
482 return NULL;
483
484 oldlimit = sqlite3_soft_heap_limit64(limit);
485
486 return PyLong_FromLongLong(oldlimit);
487 }
488
489 /** .. method:: randomness(bytes) -> data
490
491 Gets random data from SQLite's random number generator.
492
493 :param bytes: How many bytes to return
494 :rtype: (Python 2) string, (Python 3) bytes
495
496 -* sqlite3_randomness
497 */
498 static PyObject *
randomness(APSW_ARGUNUSED PyObject * self,PyObject * args)499 randomness(APSW_ARGUNUSED PyObject *self, PyObject *args)
500 {
501 int amount;
502 PyObject *bytes;
503
504 if (!PyArg_ParseTuple(args, "i", &amount))
505 return NULL;
506 if (amount < 0)
507 return PyErr_Format(PyExc_ValueError, "Can't have negative number of bytes");
508 bytes = PyBytes_FromStringAndSize(NULL, amount);
509 if (!bytes)
510 return bytes;
511 sqlite3_randomness(amount, PyBytes_AS_STRING(bytes));
512 return bytes;
513 }
514
515 /** .. method:: releasememory(bytes) -> int
516
517 Requests SQLite try to free *bytes* bytes of memory. Returns how
518 many bytes were freed.
519
520 -* sqlite3_release_memory
521 */
522
523 static PyObject *
releasememory(APSW_ARGUNUSED PyObject * self,PyObject * args)524 releasememory(APSW_ARGUNUSED PyObject *self, PyObject *args)
525 {
526 int amount;
527
528 if (!PyArg_ParseTuple(args, "i", &amount))
529 return NULL;
530
531 return PyInt_FromLong(sqlite3_release_memory(amount));
532 }
533
534 /** .. method:: status(op, reset=False) -> (int, int)
535
536 Returns current and highwater measurements.
537
538 :param op: A `status parameter <https://sqlite.org/c3ref/c_status_malloc_size.html>`_
539 :param reset: If *True* then the highwater is set to the current value
540 :returns: A tuple of current value and highwater value
541
542 .. seealso::
543
544 * :ref:`Status example <example-status>`
545
546 -* sqlite3_status64
547
548 */
549 static PyObject *
status(APSW_ARGUNUSED PyObject * self,PyObject * args)550 status(APSW_ARGUNUSED PyObject *self, PyObject *args)
551 {
552 int res, op, reset = 0;
553 sqlite3_int64 current = 0, highwater = 0;
554
555 if (!PyArg_ParseTuple(args, "i|i:status(op, reset=False)", &op, &reset))
556 return NULL;
557
558 res = sqlite3_status64(op, ¤t, &highwater, reset);
559 SET_EXC(res, NULL);
560
561 if (res != SQLITE_OK)
562 return NULL;
563
564 return Py_BuildValue("(LL)", current, highwater);
565 }
566
567 /** .. method:: vfsnames() -> list(string)
568
569 Returns a list of the currently installed :ref:`vfs <vfs>`. The first
570 item in the list is the default vfs.
571 */
572 static PyObject *
vfsnames(APSW_ARGUNUSED PyObject * self)573 vfsnames(APSW_ARGUNUSED PyObject *self)
574 {
575 PyObject *result = NULL, *str = NULL;
576 sqlite3_vfs *vfs = sqlite3_vfs_find(0);
577
578 result = PyList_New(0);
579 if (!result)
580 goto error;
581
582 while (vfs)
583 {
584 APSW_FAULT_INJECT(vfsnamesfails,
585 str = convertutf8string(vfs->zName),
586 str = PyErr_NoMemory());
587 if (!str)
588 goto error;
589 if (PyList_Append(result, str))
590 goto error;
591 Py_DECREF(str);
592 vfs = vfs->pNext;
593 }
594 return result;
595
596 error:
597 Py_XDECREF(str);
598 Py_XDECREF(result);
599 return NULL;
600 }
601
602 /** .. method:: exceptionfor(int) -> Exception
603
604 If you would like to raise an exception that corresponds to a
605 particular SQLite `error code
606 <https://sqlite.org/c3ref/c_abort.html>`_ then call this function.
607 It also understands `extended error codes
608 <https://sqlite.org/c3ref/c_ioerr_access.html>`_.
609
610 For example to raise `SQLITE_IOERR_ACCESS <https://sqlite.org/c3ref/c_ioerr_access.html>`_::
611
612 raise apsw.exceptionfor(apsw.SQLITE_IOERR_ACCESS)
613
614 */
615 static PyObject *
getapswexceptionfor(APSW_ARGUNUSED PyObject * self,PyObject * pycode)616 getapswexceptionfor(APSW_ARGUNUSED PyObject *self, PyObject *pycode)
617 {
618 int code, i;
619 PyObject *result = NULL;
620
621 if (!PyIntLong_Check(pycode))
622 return PyErr_Format(PyExc_TypeError, "Argument should be an integer");
623 code = PyIntLong_AsLong(pycode);
624 if (PyErr_Occurred())
625 return NULL;
626
627 for (i = 0; exc_descriptors[i].name; i++)
628 if (exc_descriptors[i].code == (code & 0xff))
629 {
630 result = PyObject_CallObject(exc_descriptors[i].cls, NULL);
631 if (!result)
632 return result;
633 break;
634 }
635 if (!result)
636 return PyErr_Format(PyExc_ValueError, "%d is not a known error code", code);
637
638 PyObject_SetAttrString(result, "extendedresult", PyInt_FromLong(code));
639 PyObject_SetAttrString(result, "result", PyInt_FromLong(code & 0xff));
640 return result;
641 }
642
643 /** .. method:: complete(statement) -> bool
644
645 Returns True if the input string comprises one or more complete SQL
646 statements by looking for an unquoted trailing semi-colon.
647
648 An example use would be if you were prompting the user for SQL
649 statements and needed to know if you had a whole statement, or
650 needed to ask for another line::
651
652 statement=raw_input("SQL> ")
653 while not apsw.complete(statement):
654 more=raw_input(" .. ")
655 statement=statement+"\n"+more
656
657 -* sqlite3_complete
658 */
659 static PyObject *
apswcomplete(APSW_ARGUNUSED Connection * self,PyObject * args)660 apswcomplete(APSW_ARGUNUSED Connection *self, PyObject *args)
661 {
662 char *statements = NULL;
663 int res;
664
665 if (!PyArg_ParseTuple(args, "es:complete(statement)", STRENCODING, &statements))
666 return NULL;
667
668 res = sqlite3_complete(statements);
669
670 PyMem_Free(statements);
671
672 if (res)
673 {
674 Py_INCREF(Py_True);
675 return Py_True;
676 }
677 Py_INCREF(Py_False);
678 return Py_False;
679 }
680
681 #if defined(APSW_TESTFIXTURES) && defined(APSW_USE_SQLITE_AMALGAMATION)
682 /* a routine to reset the random number generator so that we can test xRandomness */
683 static PyObject *
apsw_test_reset_rng(APSW_ARGUNUSED PyObject * self)684 apsw_test_reset_rng(APSW_ARGUNUSED PyObject *self)
685 {
686 /* See sqlite3PrngResetState in sqlite's random.c */
687 GLOBAL(struct sqlite3PrngType, sqlite3Prng).isInit = 0;
688
689 Py_RETURN_NONE;
690 }
691 #endif
692
693 #ifdef APSW_TESTFIXTURES
694 static PyObject *
apsw_fini(APSW_ARGUNUSED PyObject * self)695 apsw_fini(APSW_ARGUNUSED PyObject *self)
696 {
697 APSWBuffer_fini();
698 Py_XDECREF(tls_errmsg);
699
700 Py_RETURN_NONE;
701 }
702 #endif
703
704 #ifdef APSW_FORK_CHECKER
705
706 /*
707 We want to verify that SQLite objects are not used across forks.
708 One way is to modify all calls to SQLite to do the checking but
709 this is a pain as well as a performance hit. Instead we use the
710 approach of providing an alternative mutex implementation since
711 pretty much every SQLite API call takes and releases a mutex.
712
713 Our diverted functions check the process id on calls and set the
714 process id on allocating a mutex. We have to avoid the checks for
715 the static mutexes.
716
717 This code also doesn't bother with some things like checking malloc
718 results. It is intended to only be used to verify correctness with
719 test suites. The code that sets Python exceptions is also very
720 brute force and is likely to cause problems. That however is a
721 good thing - you will really be sure there is a problem!
722 */
723
724 typedef struct
725 {
726 pid_t pid;
727 sqlite3_mutex *underlying_mutex;
728 } apsw_mutex;
729
730 static apsw_mutex *apsw_mutexes[] =
731 {
732 NULL, /* not used - fast */
733 NULL, /* not used - recursive */
734 NULL, /* from this point on corresponds to the various static mutexes */
735 NULL,
736 NULL,
737 NULL,
738 NULL,
739 NULL,
740 NULL,
741 NULL,
742 NULL};
743
744 static sqlite3_mutex_methods apsw_orig_mutex_methods;
745
746 static int
apsw_xMutexInit(void)747 apsw_xMutexInit(void)
748 {
749 return apsw_orig_mutex_methods.xMutexInit();
750 }
751
752 static int
apsw_xMutexEnd(void)753 apsw_xMutexEnd(void)
754 {
755 return apsw_orig_mutex_methods.xMutexEnd();
756 }
757
758 static sqlite3_mutex *
apsw_xMutexAlloc(int which)759 apsw_xMutexAlloc(int which)
760 {
761 switch (which)
762 {
763 case SQLITE_MUTEX_FAST:
764 case SQLITE_MUTEX_RECURSIVE:
765 {
766 apsw_mutex *am;
767 sqlite3_mutex *m = apsw_orig_mutex_methods.xMutexAlloc(which);
768
769 if (!m)
770 return m;
771
772 am = malloc(sizeof(apsw_mutex));
773 am->pid = getpid();
774 am->underlying_mutex = m;
775 return (sqlite3_mutex *)am;
776 }
777 default:
778 /* verify we have space */
779 assert(which < sizeof(apsw_mutexes) / sizeof(apsw_mutexes[0]));
780 /* fill in if missing */
781 if (!apsw_mutexes[which])
782 {
783 apsw_mutexes[which] = malloc(sizeof(apsw_mutex));
784 apsw_mutexes[which]->pid = 0;
785 apsw_mutexes[which]->underlying_mutex = apsw_orig_mutex_methods.xMutexAlloc(which);
786 }
787 return (sqlite3_mutex *)apsw_mutexes[which];
788 }
789 }
790
791 static int
apsw_check_mutex(apsw_mutex * am)792 apsw_check_mutex(apsw_mutex *am)
793 {
794 if (am->pid && am->pid != getpid())
795 {
796 PyGILState_STATE gilstate;
797 gilstate = PyGILState_Ensure();
798 PyErr_Format(ExcForkingViolation, "SQLite object allocated in one process is being used in another (across a fork)");
799 apsw_write_unraiseable(NULL);
800 PyErr_Format(ExcForkingViolation, "SQLite object allocated in one process is being used in another (across a fork)");
801 PyGILState_Release(gilstate);
802 return SQLITE_MISUSE;
803 }
804 return SQLITE_OK;
805 }
806
807 static void
apsw_xMutexFree(sqlite3_mutex * mutex)808 apsw_xMutexFree(sqlite3_mutex *mutex)
809 {
810 apsw_mutex *am = (apsw_mutex *)mutex;
811 apsw_check_mutex(am);
812 apsw_orig_mutex_methods.xMutexFree(am->underlying_mutex);
813 }
814
815 static void
apsw_xMutexEnter(sqlite3_mutex * mutex)816 apsw_xMutexEnter(sqlite3_mutex *mutex)
817 {
818 apsw_mutex *am = (apsw_mutex *)mutex;
819 apsw_check_mutex(am);
820 apsw_orig_mutex_methods.xMutexEnter(am->underlying_mutex);
821 }
822
823 static int
apsw_xMutexTry(sqlite3_mutex * mutex)824 apsw_xMutexTry(sqlite3_mutex *mutex)
825 {
826 apsw_mutex *am = (apsw_mutex *)mutex;
827 if (apsw_check_mutex(am))
828 return SQLITE_MISUSE;
829 return apsw_orig_mutex_methods.xMutexTry(am->underlying_mutex);
830 }
831
832 static void
apsw_xMutexLeave(sqlite3_mutex * mutex)833 apsw_xMutexLeave(sqlite3_mutex *mutex)
834 {
835 apsw_mutex *am = (apsw_mutex *)mutex;
836 apsw_check_mutex(am);
837 apsw_orig_mutex_methods.xMutexLeave(am->underlying_mutex);
838 }
839
840 #ifdef SQLITE_DEBUG
841 static int
apsw_xMutexHeld(sqlite3_mutex * mutex)842 apsw_xMutexHeld(sqlite3_mutex *mutex)
843 {
844 apsw_mutex *am = (apsw_mutex *)mutex;
845 apsw_check_mutex(am);
846 return apsw_orig_mutex_methods.xMutexHeld(am->underlying_mutex);
847 }
848
849 static int
apsw_xMutexNotheld(sqlite3_mutex * mutex)850 apsw_xMutexNotheld(sqlite3_mutex *mutex)
851 {
852 apsw_mutex *am = (apsw_mutex *)mutex;
853 apsw_check_mutex(am);
854 return apsw_orig_mutex_methods.xMutexNotheld(am->underlying_mutex);
855 }
856 #endif
857
858 static sqlite3_mutex_methods apsw_mutex_methods =
859 {
860 apsw_xMutexInit,
861 apsw_xMutexEnd,
862 apsw_xMutexAlloc,
863 apsw_xMutexFree,
864 apsw_xMutexEnter,
865 apsw_xMutexTry,
866 apsw_xMutexLeave,
867 #ifdef SQLITE_DEBUG
868 apsw_xMutexHeld,
869 apsw_xMutexNotheld
870 #else
871 0,
872 0
873 #endif
874 };
875
876 /** .. method:: fork_checker()
877
878 **Note** This method is not available on Windows as it does not
879 support the fork system call.
880
881 SQLite does not allow the use of database connections across `forked
882 <http://en.wikipedia.org/wiki/Fork_(operating_system)>`__ processes
883 (see the `SQLite FAQ Q6 <https://sqlite.org/faq.html#q6>`__).
884 (Forking creates a child process that is a duplicate of the parent
885 including the state of all data structures in the program. If you
886 do this to SQLite then parent and child would both consider
887 themselves owners of open databases and silently corrupt each
888 other's work and interfere with each other's locks.)
889
890 One example of how you may end up using fork is if you use the
891 `multiprocessing module
892 <http://docs.python.org/library/multiprocessing.html>`__ which uses
893 fork to make child processes.
894
895 If you do use fork or multiprocessing on a platform that supports
896 fork then you **must** ensure database connections and their objects
897 (cursors, backup, blobs etc) are not used in the parent process, or
898 are all closed before calling fork or starting a `Process
899 <http://docs.python.org/library/multiprocessing.html#process-and-exceptions>`__.
900 (Note you must call close to ensure the underlying SQLite objects
901 are closed. It is also a good idea to call `gc.collect(2)
902 <http://docs.python.org/library/gc.html#gc.collect>`__ to ensure
903 anything you may have missed is also deallocated.)
904
905 Once you run this method, extra checking code is inserted into
906 SQLite's mutex operations (at a very small performance penalty) that
907 verifies objects are not used across processes. You will get a
908 :exc:`ForkingViolationError` if you do so. Note that due to the way
909 Python's internals work, the exception will be delivered to
910 `sys.excepthook` in addition to the normal exception mechanisms and
911 may be reported by Python after the line where the issue actually
912 arose. (Destructors of objects you didn't close also run between
913 lines.)
914
915 You should only call this method as the first line after importing
916 APSW, as it has to shutdown and re-initialize SQLite. If you have
917 any SQLite objects already allocated when calling the method then
918 the program will later crash. The recommended use is to use the fork
919 checking as part of your test suite.
920 */
921 static PyObject *
apsw_fork_checker(APSW_ARGUNUSED PyObject * self)922 apsw_fork_checker(APSW_ARGUNUSED PyObject *self)
923 {
924 int rc;
925
926 /* ignore multiple attempts to use this routine */
927 if (apsw_orig_mutex_methods.xMutexInit)
928 goto ok;
929
930 /* Ensure mutex methods available and installed */
931 rc = sqlite3_initialize();
932 if (rc)
933 goto fail;
934
935 /* then do a shutdown as we can't get or change mutex while sqlite is running */
936 rc = sqlite3_shutdown();
937 if (rc)
938 goto fail;
939
940 rc = sqlite3_config(SQLITE_CONFIG_GETMUTEX, &apsw_orig_mutex_methods);
941 if (rc)
942 goto fail;
943
944 rc = sqlite3_config(SQLITE_CONFIG_MUTEX, &apsw_mutex_methods);
945 if (rc)
946 goto fail;
947
948 /* start back up again */
949 rc = sqlite3_initialize();
950 if (rc)
951 goto fail;
952
953 ok:
954 Py_RETURN_NONE;
955
956 fail:
957 assert(rc != SQLITE_OK);
958 SET_EXC(rc, NULL);
959 return NULL;
960 }
961 #endif
962
963 /** .. attribute:: compile_options
964
965 A tuple of the options used to compile SQLite. For example it
966 will be something like this::
967
968 ('ENABLE_LOCKING_STYLE=0', 'TEMP_STORE=1', 'THREADSAFE=1')
969
970 -* sqlite3_compileoption_get
971 */
972 static PyObject *
get_compile_options(void)973 get_compile_options(void)
974 {
975 int i, count = 0;
976 const char *opt;
977 PyObject *tmpstring;
978 PyObject *res = 0;
979
980 for (i = 0;; i++)
981 {
982 opt = sqlite3_compileoption_get(i); /* No PYSQLITE_CALL needed */
983 if (!opt)
984 break;
985 }
986 count = i;
987
988 res = PyTuple_New(count);
989 if (!res)
990 goto fail;
991 for (i = 0; i < count; i++)
992 {
993 opt = sqlite3_compileoption_get(i); /* No PYSQLITE_CALL needed */
994 assert(opt);
995 tmpstring = MAKESTR(opt);
996 if (!tmpstring)
997 goto fail;
998 PyTuple_SET_ITEM(res, i, tmpstring);
999 }
1000
1001 return res;
1002 fail:
1003 Py_XDECREF(res);
1004 return NULL;
1005 }
1006
1007 /** .. attribute:: keywords
1008
1009 A set containing every SQLite keyword
1010
1011 -* sqlite3_keyword_count sqlite3_keyword_name
1012
1013 */
1014 static PyObject *
get_keywords(void)1015 get_keywords(void)
1016 {
1017 int i, j, count, size;
1018 PyObject *res = NULL, *tmpstring;
1019 const char *name;
1020
1021 res = PySet_New(0);
1022 if (!res)
1023 goto fail;
1024
1025 count = sqlite3_keyword_count(); /* No PYSQLITE_CALL needed */
1026 for (i = 0; i < count; i++)
1027 {
1028 j = sqlite3_keyword_name(i, &name, &size); /* No PYSQLITE_CALL needed */
1029 assert(j == SQLITE_OK);
1030 tmpstring = convertutf8stringsize(name, size);
1031 if (!tmpstring)
1032 goto fail;
1033 j = PySet_Add(res, tmpstring);
1034 Py_DECREF(tmpstring);
1035 if (j)
1036 goto fail;
1037 }
1038
1039 return res;
1040 fail:
1041 Py_XDECREF(res);
1042 return NULL;
1043 }
1044
1045 /** .. method:: format_sql_value(value) -> string
1046
1047 Returns a Python string (unicode) representing the supplied value in
1048 SQL syntax. Python 2 note: You must supply unicode strings not
1049 plain strings.
1050
1051 */
1052 static PyObject *
formatsqlvalue(APSW_ARGUNUSED PyObject * self,PyObject * value)1053 formatsqlvalue(APSW_ARGUNUSED PyObject *self, PyObject *value)
1054 {
1055 /* NULL/None */
1056 if (value == Py_None)
1057 {
1058 static PyObject *nullstr;
1059 if (!nullstr)
1060 nullstr = PyObject_Unicode(MAKESTR("NULL"));
1061 Py_INCREF(nullstr);
1062 return nullstr;
1063 }
1064 /* Integer/Long/Float */
1065 if (PyIntLong_Check(value) /* ::TODO:: verify L is not appended in py 2.3 and similar vintage */
1066 || PyFloat_Check(value))
1067 return PyObject_Unicode(value);
1068 #if PY_MAJOR_VERSION < 3
1069 /* We don't support plain strings only unicode */
1070 if (PyString_Check(value))
1071 return PyErr_Format(PyExc_TypeError, "Old plain strings not supported - use unicode");
1072 #endif
1073 /* Unicode */
1074 if (PyUnicode_Check(value))
1075 {
1076 /* We optimize for the default case of there being no nuls or single quotes */
1077 PyObject *unires;
1078 Py_UNICODE *res;
1079 Py_ssize_t left;
1080 unires = PyUnicode_FromUnicode(NULL, PyUnicode_GET_SIZE(value) + 2);
1081 if (!unires)
1082 return NULL;
1083 res = PyUnicode_AS_UNICODE(unires);
1084 *res++ = '\'';
1085 memcpy(res, PyUnicode_AS_UNICODE(value), PyUnicode_GET_DATA_SIZE(value));
1086 res += PyUnicode_GET_SIZE(value);
1087 *res++ = '\'';
1088 /* Now look for nuls and single quotes */
1089 res = PyUnicode_AS_UNICODE(unires) + 1;
1090 left = PyUnicode_GET_SIZE(value);
1091 for (; left; left--, res++)
1092 {
1093 if (*res == '\'' || *res == 0)
1094 {
1095 /* we add one char for ' and 10 for null */
1096 const int moveamount = *res == '\'' ? 1 : 10;
1097 int retval;
1098 APSW_FAULT_INJECT(FormatSQLValueResizeFails,
1099 retval = PyUnicode_Resize(&unires, PyUnicode_GET_SIZE(unires) + moveamount),
1100 retval = PyUnicode_Resize(&unires, -17));
1101 if (retval == -1)
1102 {
1103 Py_DECREF(unires);
1104 return NULL;
1105 }
1106 res = PyUnicode_AS_UNICODE(unires) + (PyUnicode_GET_SIZE(unires) - left - moveamount - 1);
1107 memmove(res + moveamount, res, sizeof(Py_UNICODE) * (left + 1));
1108 if (*res == 0)
1109 {
1110 *res++ = '\'';
1111 *res++ = '|';
1112 *res++ = '|';
1113 *res++ = 'X';
1114 *res++ = '\'';
1115 *res++ = '0';
1116 *res++ = '0';
1117 *res++ = '\'';
1118 *res++ = '|';
1119 *res++ = '|';
1120 *res = '\'';
1121 }
1122 else
1123 res++;
1124 }
1125 }
1126 APSW_Unicode_Return(unires);
1127 }
1128 /* Blob */
1129 if (
1130 #if PY_MAJOR_VERSION < 3
1131 PyBuffer_Check(value)
1132 #else
1133 PyBytes_Check(value)
1134 #endif
1135 )
1136 {
1137 const unsigned char *buffer;
1138 Py_ssize_t buflen;
1139 int asrb;
1140 PyObject *unires;
1141 Py_UNICODE *res;
1142 READBUFFERVARS;
1143
1144 #define _HEXDIGITS
1145 compat_PyObjectReadBuffer(value);
1146 APSW_FAULT_INJECT(FormatSQLValueAsReadBufferFails,
1147 ,
1148 ENDREADBUFFER;
1149 (PyErr_NoMemory(), asrb = -1));
1150 if (asrb != 0)
1151 return NULL;
1152 /* 3 is X, ', ' */
1153 APSW_FAULT_INJECT(FormatSQLValuePyUnicodeFromUnicodeFails,
1154 unires = PyUnicode_FromUnicode(NULL, buflen * 2 + 3),
1155 unires = PyErr_NoMemory());
1156 if (!unires)
1157 {
1158 ENDREADBUFFER;
1159 return NULL;
1160 }
1161 res = PyUnicode_AS_UNICODE(unires);
1162 *res++ = 'X';
1163 *res++ = '\'';
1164 /* About the billionth time I have written a hex conversion routine */
1165 for (; buflen; buflen--)
1166 {
1167 *res++ = "0123456789ABCDEF"[(*buffer) >> 4];
1168 *res++ = "0123456789ABCDEF"[(*buffer++) & 0x0f];
1169 }
1170 *res++ = '\'';
1171
1172 ENDREADBUFFER;
1173 APSW_Unicode_Return(unires);
1174 }
1175
1176 return PyErr_Format(PyExc_TypeError, "Unsupported type");
1177 }
1178
1179 /** .. automethod:: main()
1180
1181 Sphinx automethod is too stupid, so this text is replaced by
1182 my code with the actual docstring from tools.py:main().
1183 */
1184
1185 /** .. method:: log(level, message)
1186
1187 Calls the SQLite logging interface. Note that you must format the
1188 message before passing it to this method::
1189
1190 apsw.log(apsw.SQLITE_NOMEM, "Need %d bytes of memory" % (1234,))
1191
1192 See :ref:`tips <diagnostics_tips>` for an example of how to
1193 receive log messages.
1194
1195 -* sqlite3_log
1196 */
1197 static PyObject *
apsw_log(APSW_ARGUNUSED PyObject * self,PyObject * args)1198 apsw_log(APSW_ARGUNUSED PyObject *self, PyObject *args)
1199 {
1200 int level;
1201 char *message;
1202 if (!PyArg_ParseTuple(args, "ies", &level, STRENCODING, &message))
1203 return NULL;
1204 sqlite3_log(level, "%s", message); /* PYSQLITE_CALL not needed */
1205 PyMem_Free(message);
1206 Py_RETURN_NONE;
1207 }
1208
1209 static PyMethodDef module_methods[] = {
1210 {"sqlite3_sourceid", (PyCFunction)get_sqlite3_sourceid, METH_NOARGS,
1211 "Return the source identification of the SQLite library"},
1212 {"sqlitelibversion", (PyCFunction)getsqliteversion, METH_NOARGS,
1213 "Return the version of the SQLite library"},
1214 {"apswversion", (PyCFunction)getapswversion, METH_NOARGS,
1215 "Return the version of the APSW wrapper"},
1216 {"vfsnames", (PyCFunction)vfsnames, METH_NOARGS,
1217 "Returns list of vfs names"},
1218 {"enablesharedcache", (PyCFunction)enablesharedcache, METH_VARARGS,
1219 "Sets shared cache semantics for this thread"},
1220 {"initialize", (PyCFunction)initialize, METH_NOARGS,
1221 "Initialize SQLite library"},
1222 {"shutdown", (PyCFunction)sqliteshutdown, METH_NOARGS,
1223 "Shutdown SQLite library"},
1224 {"format_sql_value", (PyCFunction)formatsqlvalue, METH_O,
1225 "Formats a SQL value as a string"},
1226 #ifdef EXPERIMENTAL
1227 {"config", (PyCFunction)config, METH_VARARGS,
1228 "Calls sqlite3_config"},
1229 {"log", (PyCFunction)apsw_log, METH_VARARGS,
1230 "Calls sqlite3_log"},
1231 #endif
1232 {"memoryused", (PyCFunction)memoryused, METH_NOARGS,
1233 "Current SQLite memory in use"},
1234 {"memoryhighwater", (PyCFunction)memoryhighwater, METH_VARARGS,
1235 "Most amount of memory used"},
1236 {"status", (PyCFunction)status, METH_VARARGS,
1237 "Gets various SQLite counters"},
1238 {"softheaplimit", (PyCFunction)softheaplimit, METH_VARARGS,
1239 "Sets soft limit on SQLite memory usage"},
1240 {"releasememory", (PyCFunction)releasememory, METH_VARARGS,
1241 "Attempts to free specified amount of memory"},
1242 {"randomness", (PyCFunction)randomness, METH_VARARGS,
1243 "Obtains random bytes"},
1244 {"exceptionfor", (PyCFunction)getapswexceptionfor, METH_O,
1245 "Returns exception instance corresponding to supplied sqlite error code"},
1246 {"complete", (PyCFunction)apswcomplete, METH_VARARGS,
1247 "Tests if a complete SQLite statement has been supplied (ie ends with ;)"},
1248 #if defined(APSW_TESTFIXTURES) && defined(APSW_USE_SQLITE_AMALGAMATION)
1249 {"test_reset_rng", (PyCFunction)apsw_test_reset_rng, METH_NOARGS,
1250 "Resets random number generator so we can test vfs xRandomness"},
1251 #endif
1252 #ifdef APSW_TESTFIXTURES
1253 {"_fini", (PyCFunction)apsw_fini, METH_NOARGS,
1254 "Frees all caches and recycle lists"},
1255 #endif
1256 #ifdef APSW_FORK_CHECKER
1257 {"fork_checker", (PyCFunction)apsw_fork_checker, METH_NOARGS,
1258 "Installs fork checking code"},
1259 #endif
1260 {0, 0, 0, 0} /* Sentinel */
1261 };
1262
1263 static void add_shell(PyObject *module);
1264
1265 #if PY_MAJOR_VERSION >= 3
1266 static struct PyModuleDef apswmoduledef = {
1267 PyModuleDef_HEAD_INIT,
1268 "apsw",
1269 NULL,
1270 -1,
1271 module_methods,
1272 0,
1273 0,
1274 0,
1275 0};
1276 #endif
1277
1278 PyMODINIT_FUNC
1279 #if PY_MAJOR_VERSION < 3
initapsw(void)1280 initapsw(void)
1281 #else
1282 PyInit_apsw(void)
1283 #endif
1284 {
1285 PyObject *m = NULL;
1286 PyObject *thedict = NULL;
1287 const char *mapping_name = NULL;
1288 PyObject *hooks;
1289 unsigned int i;
1290
1291 assert(sizeof(int) == 4); /* we expect 32 bit ints */
1292 assert(sizeof(long long) == 8); /* we expect 64 bit long long */
1293
1294 /* Check SQLite was compiled with thread safety */
1295 if (!sqlite3_threadsafe())
1296 {
1297 PyErr_Format(PyExc_EnvironmentError, "SQLite was compiled without thread safety and cannot be used.");
1298 goto fail;
1299 }
1300
1301 if (PyType_Ready(&ConnectionType) < 0 || PyType_Ready(&APSWCursorType) < 0 || PyType_Ready(&ZeroBlobBindType) < 0 || PyType_Ready(&APSWBlobType) < 0 || PyType_Ready(&APSWVFSType) < 0 || PyType_Ready(&APSWVFSFileType) < 0 || PyType_Ready(&APSWURIFilenameType) < 0 || PyType_Ready(&APSWStatementType) < 0 || PyType_Ready(&APSWBufferType) < 0 || PyType_Ready(&FunctionCBInfoType) < 0
1302 #ifdef EXPERIMENTAL
1303 || PyType_Ready(&APSWBackupType) < 0
1304 #endif
1305 )
1306 goto fail;
1307
1308 /* ensure threads are available */
1309 PyEval_InitThreads();
1310
1311 #if PY_MAJOR_VERSION < 3
1312 m = apswmodule = Py_InitModule3("apsw", module_methods,
1313 "Another Python SQLite Wrapper.");
1314 #else
1315 m = apswmodule = PyModule_Create(&apswmoduledef);
1316 #endif
1317
1318 if (m == NULL)
1319 goto fail;
1320
1321 Py_INCREF(m);
1322
1323 if (init_exceptions(m))
1324 goto fail;
1325
1326 Py_INCREF(&ConnectionType);
1327 PyModule_AddObject(m, "Connection", (PyObject *)&ConnectionType);
1328
1329 Py_INCREF(&APSWCursorType);
1330 PyModule_AddObject(m, "Cursor", (PyObject *)&APSWCursorType);
1331
1332 Py_INCREF(&APSWBlobType);
1333 PyModule_AddObject(m, "Blob", (PyObject *)&APSWBlobType);
1334
1335 Py_INCREF(&APSWBackupType);
1336 PyModule_AddObject(m, "Backup", (PyObject *)&APSWBackupType);
1337
1338 Py_INCREF(&ZeroBlobBindType);
1339 PyModule_AddObject(m, "zeroblob", (PyObject *)&ZeroBlobBindType);
1340
1341 Py_INCREF(&APSWVFSType);
1342 PyModule_AddObject(m, "VFS", (PyObject *)&APSWVFSType);
1343 Py_INCREF(&APSWVFSFileType);
1344 PyModule_AddObject(m, "VFSFile", (PyObject *)&APSWVFSFileType);
1345 Py_INCREF(&APSWURIFilenameType);
1346 PyModule_AddObject(m, "URIFilename", (PyObject *)&APSWURIFilenameType);
1347
1348 /** .. attribute:: connection_hooks
1349
1350 The purpose of the hooks is to allow the easy registration of
1351 :meth:`functions <Connection.createscalarfunction>`,
1352 :ref:`virtual tables <virtualtables>` or similar items with
1353 each :class:`Connection` as it is created. The default value is an empty
1354 list. Whenever a Connection is created, each item in
1355 apsw.connection_hooks is invoked with a single parameter being
1356 the new Connection object. If the hook raises an exception then
1357 the creation of the Connection fails.
1358
1359 If you wanted to store your own defined functions in the
1360 database then you could define a hook that looked in the
1361 relevant tables, got the Python text and turned it into the
1362 functions.
1363 */
1364 hooks = PyList_New(0);
1365 if (!hooks)
1366 goto fail;
1367 PyModule_AddObject(m, "connection_hooks", hooks);
1368
1369 /** .. data:: SQLITE_VERSION_NUMBER
1370
1371 The integer version number of SQLite that APSW was compiled
1372 against. For example SQLite 3.6.4 will have the value *3006004*.
1373 This number may be different than the actual library in use if the
1374 library is shared and has been updated. Call
1375 :meth:`sqlitelibversion` to get the actual library version.
1376
1377 */
1378 PyModule_AddIntConstant(m, "SQLITE_VERSION_NUMBER", SQLITE_VERSION_NUMBER);
1379
1380 /** .. attribute:: using_amalgamation
1381
1382 If True then `SQLite amalgamation
1383 <https://sqlite.org/cvstrac/wiki?p=TheAmalgamation>`__ is in
1384 use (statically compiled into APSW). Using the amalgamation means
1385 that SQLite shared libraries are not used and will not affect your
1386 code.
1387
1388 */
1389
1390 #ifdef APSW_USE_SQLITE_AMALGAMATION
1391 Py_INCREF(Py_True);
1392 PyModule_AddObject(m, "using_amalgamation", Py_True);
1393 #else
1394 Py_INCREF(Py_False);
1395 PyModule_AddObject(m, "using_amalgamation", Py_False);
1396 #endif
1397
1398 /**
1399
1400 .. _sqliteconstants:
1401
1402 SQLite constants
1403 ================
1404
1405 SQLite has `many constants
1406 <https://sqlite.org/c3ref/constlist.html>`_ used in various
1407 interfaces. To use a constant such as :const:`SQLITE_OK`, just
1408 use ``apsw.SQLITE_OK``.
1409
1410 The same values can be used in different contexts. For example
1411 :const:`SQLITE_OK` and :const:`SQLITE_CREATE_INDEX` both have a value
1412 of zero. For each group of constants there is also a mapping (dict)
1413 available that you can supply a string to and get the corresponding
1414 numeric value, or supply a numeric value and get the corresponding
1415 string. These can help improve diagnostics/logging, calling other
1416 modules etc. For example::
1417
1418 apsw.mapping_authorizer_function["SQLITE_READ"] == 20
1419 apsw.mapping_authorizer_function[20] == "SQLITE_READ"
1420
1421
1422 */
1423
1424 /* add in some constants and also put them in a corresponding mapping dictionary */
1425
1426 {
1427
1428 /* sentinel should be a number that doesn't exist */
1429 #define SENTINEL -786343
1430 #define DICT(n) \
1431 { \
1432 n, SENTINEL \
1433 }
1434 #define END \
1435 { \
1436 NULL, 0 \
1437 }
1438 #define ADDINT(n) \
1439 { \
1440 #n, n \
1441 }
1442
1443 static const struct
1444 {
1445 const char *name;
1446 const int value;
1447 } integers[] = {
1448 DICT("mapping_authorizer_return"),
1449 ADDINT(SQLITE_DENY),
1450 ADDINT(SQLITE_IGNORE),
1451 ADDINT(SQLITE_OK),
1452 END,
1453
1454 DICT("mapping_authorizer_function"),
1455 ADDINT(SQLITE_CREATE_INDEX),
1456 ADDINT(SQLITE_CREATE_TABLE),
1457 ADDINT(SQLITE_CREATE_TEMP_INDEX),
1458 ADDINT(SQLITE_CREATE_TEMP_TABLE),
1459 ADDINT(SQLITE_CREATE_TEMP_TRIGGER),
1460 ADDINT(SQLITE_CREATE_TEMP_VIEW),
1461 ADDINT(SQLITE_CREATE_TRIGGER),
1462 ADDINT(SQLITE_CREATE_VIEW),
1463 ADDINT(SQLITE_DELETE),
1464 ADDINT(SQLITE_DROP_INDEX),
1465 ADDINT(SQLITE_DROP_TABLE),
1466 ADDINT(SQLITE_DROP_TEMP_INDEX),
1467 ADDINT(SQLITE_DROP_TEMP_TABLE),
1468 ADDINT(SQLITE_DROP_TEMP_TRIGGER),
1469 ADDINT(SQLITE_DROP_TEMP_VIEW),
1470 ADDINT(SQLITE_DROP_TRIGGER),
1471 ADDINT(SQLITE_DROP_VIEW),
1472 ADDINT(SQLITE_INSERT),
1473 ADDINT(SQLITE_PRAGMA),
1474 ADDINT(SQLITE_READ),
1475 ADDINT(SQLITE_SELECT),
1476 ADDINT(SQLITE_TRANSACTION),
1477 ADDINT(SQLITE_UPDATE),
1478 ADDINT(SQLITE_ATTACH),
1479 ADDINT(SQLITE_DETACH),
1480 ADDINT(SQLITE_ALTER_TABLE),
1481 ADDINT(SQLITE_REINDEX),
1482 ADDINT(SQLITE_COPY),
1483 ADDINT(SQLITE_ANALYZE),
1484 ADDINT(SQLITE_CREATE_VTABLE),
1485 ADDINT(SQLITE_DROP_VTABLE),
1486 ADDINT(SQLITE_FUNCTION),
1487 ADDINT(SQLITE_SAVEPOINT),
1488 ADDINT(SQLITE_RECURSIVE),
1489 END,
1490
1491 /* vtable best index constraints */
1492 DICT("mapping_bestindex_constraints"),
1493 ADDINT(SQLITE_INDEX_CONSTRAINT_EQ),
1494 ADDINT(SQLITE_INDEX_CONSTRAINT_GT),
1495 ADDINT(SQLITE_INDEX_CONSTRAINT_LE),
1496 ADDINT(SQLITE_INDEX_CONSTRAINT_LT),
1497 ADDINT(SQLITE_INDEX_CONSTRAINT_GE),
1498 ADDINT(SQLITE_INDEX_CONSTRAINT_MATCH),
1499 ADDINT(SQLITE_INDEX_CONSTRAINT_LIKE),
1500 ADDINT(SQLITE_INDEX_CONSTRAINT_REGEXP),
1501 ADDINT(SQLITE_INDEX_CONSTRAINT_GLOB),
1502 ADDINT(SQLITE_INDEX_CONSTRAINT_ISNULL),
1503 ADDINT(SQLITE_INDEX_CONSTRAINT_ISNOT),
1504 ADDINT(SQLITE_INDEX_CONSTRAINT_ISNOTNULL),
1505 ADDINT(SQLITE_INDEX_CONSTRAINT_IS),
1506 ADDINT(SQLITE_INDEX_CONSTRAINT_NE),
1507 ADDINT(SQLITE_INDEX_CONSTRAINT_FUNCTION),
1508 END,
1509
1510 /* extended result codes */
1511 DICT("mapping_extended_result_codes"),
1512 ADDINT(SQLITE_IOERR_READ),
1513 ADDINT(SQLITE_IOERR_SHORT_READ),
1514 ADDINT(SQLITE_IOERR_WRITE),
1515 ADDINT(SQLITE_IOERR_FSYNC),
1516 ADDINT(SQLITE_IOERR_DIR_FSYNC),
1517 ADDINT(SQLITE_IOERR_TRUNCATE),
1518 ADDINT(SQLITE_IOERR_FSTAT),
1519 ADDINT(SQLITE_IOERR_UNLOCK),
1520 ADDINT(SQLITE_IOERR_RDLOCK),
1521 ADDINT(SQLITE_IOERR_DELETE),
1522 ADDINT(SQLITE_IOERR_BLOCKED),
1523 ADDINT(SQLITE_IOERR_NOMEM),
1524 ADDINT(SQLITE_IOERR_ACCESS),
1525 ADDINT(SQLITE_IOERR_CHECKRESERVEDLOCK),
1526 ADDINT(SQLITE_IOERR_LOCK),
1527 ADDINT(SQLITE_IOERR_CLOSE),
1528 ADDINT(SQLITE_IOERR_DIR_CLOSE),
1529 ADDINT(SQLITE_LOCKED_SHAREDCACHE),
1530 ADDINT(SQLITE_BUSY_RECOVERY),
1531 ADDINT(SQLITE_CANTOPEN_NOTEMPDIR),
1532 ADDINT(SQLITE_IOERR_SHMOPEN),
1533 ADDINT(SQLITE_IOERR_SHMSIZE),
1534 ADDINT(SQLITE_IOERR_SHMLOCK),
1535 ADDINT(SQLITE_CORRUPT_VTAB),
1536 ADDINT(SQLITE_IOERR_SEEK),
1537 ADDINT(SQLITE_IOERR_SHMMAP),
1538 ADDINT(SQLITE_READONLY_CANTLOCK),
1539 ADDINT(SQLITE_READONLY_RECOVERY),
1540 ADDINT(SQLITE_ABORT_ROLLBACK),
1541 ADDINT(SQLITE_CANTOPEN_ISDIR),
1542 ADDINT(SQLITE_CANTOPEN_FULLPATH),
1543 ADDINT(SQLITE_IOERR_DELETE_NOENT),
1544 ADDINT(SQLITE_CONSTRAINT_CHECK),
1545 ADDINT(SQLITE_CONSTRAINT_COMMITHOOK),
1546 ADDINT(SQLITE_CONSTRAINT_FOREIGNKEY),
1547 ADDINT(SQLITE_CONSTRAINT_FUNCTION),
1548 ADDINT(SQLITE_CONSTRAINT_NOTNULL),
1549 ADDINT(SQLITE_CONSTRAINT_PRIMARYKEY),
1550 ADDINT(SQLITE_CONSTRAINT_TRIGGER),
1551 ADDINT(SQLITE_CONSTRAINT_UNIQUE),
1552 ADDINT(SQLITE_CONSTRAINT_VTAB),
1553 ADDINT(SQLITE_READONLY_ROLLBACK),
1554 ADDINT(SQLITE_IOERR_MMAP),
1555 ADDINT(SQLITE_NOTICE_RECOVER_ROLLBACK),
1556 ADDINT(SQLITE_NOTICE_RECOVER_WAL),
1557 ADDINT(SQLITE_BUSY_SNAPSHOT),
1558 ADDINT(SQLITE_IOERR_GETTEMPPATH),
1559 ADDINT(SQLITE_WARNING_AUTOINDEX),
1560 ADDINT(SQLITE_CANTOPEN_CONVPATH),
1561 ADDINT(SQLITE_IOERR_CONVPATH),
1562 ADDINT(SQLITE_CONSTRAINT_ROWID),
1563 ADDINT(SQLITE_READONLY_DBMOVED),
1564 ADDINT(SQLITE_AUTH_USER),
1565 ADDINT(SQLITE_IOERR_VNODE),
1566 ADDINT(SQLITE_IOERR_AUTH),
1567 ADDINT(SQLITE_OK_LOAD_PERMANENTLY),
1568 ADDINT(SQLITE_IOERR_ROLLBACK_ATOMIC),
1569 ADDINT(SQLITE_IOERR_COMMIT_ATOMIC),
1570 ADDINT(SQLITE_IOERR_BEGIN_ATOMIC),
1571 ADDINT(SQLITE_READONLY_CANTINIT),
1572 ADDINT(SQLITE_ERROR_RETRY),
1573 ADDINT(SQLITE_ERROR_MISSING_COLLSEQ),
1574 ADDINT(SQLITE_READONLY_DIRECTORY),
1575 ADDINT(SQLITE_LOCKED_VTAB),
1576 ADDINT(SQLITE_CORRUPT_SEQUENCE),
1577 ADDINT(SQLITE_CANTOPEN_DIRTYWAL),
1578 ADDINT(SQLITE_ERROR_SNAPSHOT),
1579 ADDINT(SQLITE_CONSTRAINT_PINNED),
1580 ADDINT(SQLITE_OK_SYMLINK),
1581 ADDINT(SQLITE_CANTOPEN_SYMLINK),
1582 ADDINT(SQLITE_IOERR_DATA),
1583 ADDINT(SQLITE_CORRUPT_INDEX),
1584 ADDINT(SQLITE_BUSY_TIMEOUT),
1585 ADDINT(SQLITE_IOERR_CORRUPTFS),
1586 END,
1587
1588 /* error codes */
1589 DICT("mapping_result_codes"),
1590 ADDINT(SQLITE_OK),
1591 ADDINT(SQLITE_ERROR),
1592 ADDINT(SQLITE_INTERNAL),
1593 ADDINT(SQLITE_PERM),
1594 ADDINT(SQLITE_ABORT),
1595 ADDINT(SQLITE_BUSY),
1596 ADDINT(SQLITE_LOCKED),
1597 ADDINT(SQLITE_NOMEM),
1598 ADDINT(SQLITE_READONLY),
1599 ADDINT(SQLITE_INTERRUPT),
1600 ADDINT(SQLITE_IOERR),
1601 ADDINT(SQLITE_CORRUPT),
1602 ADDINT(SQLITE_FULL),
1603 ADDINT(SQLITE_CANTOPEN),
1604 ADDINT(SQLITE_PROTOCOL),
1605 ADDINT(SQLITE_EMPTY),
1606 ADDINT(SQLITE_SCHEMA),
1607 ADDINT(SQLITE_CONSTRAINT),
1608 ADDINT(SQLITE_MISMATCH),
1609 ADDINT(SQLITE_MISUSE),
1610 ADDINT(SQLITE_NOLFS),
1611 ADDINT(SQLITE_AUTH),
1612 ADDINT(SQLITE_FORMAT),
1613 ADDINT(SQLITE_RANGE),
1614 ADDINT(SQLITE_NOTADB),
1615 ADDINT(SQLITE_NOTFOUND),
1616 ADDINT(SQLITE_TOOBIG),
1617 ADDINT(SQLITE_NOTICE),
1618 ADDINT(SQLITE_WARNING),
1619 /* you can't get these from apsw code but present for completeness */
1620 ADDINT(SQLITE_DONE),
1621 ADDINT(SQLITE_ROW),
1622 END,
1623
1624 /* open flags */
1625 DICT("mapping_open_flags"),
1626 ADDINT(SQLITE_OPEN_READONLY),
1627 ADDINT(SQLITE_OPEN_READWRITE),
1628 ADDINT(SQLITE_OPEN_CREATE),
1629 ADDINT(SQLITE_OPEN_DELETEONCLOSE),
1630 ADDINT(SQLITE_OPEN_EXCLUSIVE),
1631 ADDINT(SQLITE_OPEN_MAIN_DB),
1632 ADDINT(SQLITE_OPEN_TEMP_DB),
1633 ADDINT(SQLITE_OPEN_TRANSIENT_DB),
1634 ADDINT(SQLITE_OPEN_MAIN_JOURNAL),
1635 ADDINT(SQLITE_OPEN_TEMP_JOURNAL),
1636 ADDINT(SQLITE_OPEN_SUBJOURNAL),
1637 ADDINT(SQLITE_OPEN_NOMUTEX),
1638 ADDINT(SQLITE_OPEN_FULLMUTEX),
1639 ADDINT(SQLITE_OPEN_PRIVATECACHE),
1640 ADDINT(SQLITE_OPEN_SHAREDCACHE),
1641 ADDINT(SQLITE_OPEN_AUTOPROXY),
1642 ADDINT(SQLITE_OPEN_WAL),
1643 ADDINT(SQLITE_OPEN_URI),
1644 ADDINT(SQLITE_OPEN_MEMORY),
1645 ADDINT(SQLITE_OPEN_NOFOLLOW),
1646 ADDINT(SQLITE_OPEN_SUPER_JOURNAL),
1647 END,
1648
1649 /* limits */
1650 DICT("mapping_limits"),
1651 ADDINT(SQLITE_LIMIT_LENGTH),
1652 ADDINT(SQLITE_LIMIT_SQL_LENGTH),
1653 ADDINT(SQLITE_LIMIT_COLUMN),
1654 ADDINT(SQLITE_LIMIT_EXPR_DEPTH),
1655 ADDINT(SQLITE_LIMIT_COMPOUND_SELECT),
1656 ADDINT(SQLITE_LIMIT_VDBE_OP),
1657 ADDINT(SQLITE_LIMIT_FUNCTION_ARG),
1658 ADDINT(SQLITE_LIMIT_ATTACHED),
1659 ADDINT(SQLITE_LIMIT_LIKE_PATTERN_LENGTH),
1660 ADDINT(SQLITE_LIMIT_VARIABLE_NUMBER),
1661 ADDINT(SQLITE_LIMIT_TRIGGER_DEPTH),
1662 ADDINT(SQLITE_LIMIT_WORKER_THREADS),
1663 /* We don't include the MAX limits - see https://github.com/rogerbinns/apsw/issues/17 */
1664 END,
1665
1666 DICT("mapping_config"),
1667 ADDINT(SQLITE_CONFIG_SINGLETHREAD),
1668 ADDINT(SQLITE_CONFIG_MULTITHREAD),
1669 ADDINT(SQLITE_CONFIG_SERIALIZED),
1670 ADDINT(SQLITE_CONFIG_MALLOC),
1671 ADDINT(SQLITE_CONFIG_GETMALLOC),
1672 ADDINT(SQLITE_CONFIG_SCRATCH),
1673 ADDINT(SQLITE_CONFIG_PAGECACHE),
1674 ADDINT(SQLITE_CONFIG_HEAP),
1675 ADDINT(SQLITE_CONFIG_MEMSTATUS),
1676 ADDINT(SQLITE_CONFIG_MUTEX),
1677 ADDINT(SQLITE_CONFIG_GETMUTEX),
1678 ADDINT(SQLITE_CONFIG_LOOKASIDE),
1679 ADDINT(SQLITE_CONFIG_LOG),
1680 ADDINT(SQLITE_CONFIG_GETPCACHE),
1681 ADDINT(SQLITE_CONFIG_PCACHE),
1682 ADDINT(SQLITE_CONFIG_URI),
1683 ADDINT(SQLITE_CONFIG_PCACHE2),
1684 ADDINT(SQLITE_CONFIG_GETPCACHE2),
1685 ADDINT(SQLITE_CONFIG_COVERING_INDEX_SCAN),
1686 ADDINT(SQLITE_CONFIG_SQLLOG),
1687 ADDINT(SQLITE_CONFIG_MMAP_SIZE),
1688 ADDINT(SQLITE_CONFIG_WIN32_HEAPSIZE),
1689 ADDINT(SQLITE_CONFIG_PCACHE_HDRSZ),
1690 ADDINT(SQLITE_CONFIG_PMASZ),
1691 ADDINT(SQLITE_CONFIG_STMTJRNL_SPILL),
1692 ADDINT(SQLITE_CONFIG_SMALL_MALLOC),
1693 ADDINT(SQLITE_CONFIG_SORTERREF_SIZE),
1694 ADDINT(SQLITE_CONFIG_MEMDB_MAXSIZE),
1695 END,
1696
1697 DICT("mapping_db_config"),
1698 ADDINT(SQLITE_DBCONFIG_LOOKASIDE),
1699 ADDINT(SQLITE_DBCONFIG_ENABLE_FKEY),
1700 ADDINT(SQLITE_DBCONFIG_ENABLE_TRIGGER),
1701 ADDINT(SQLITE_DBCONFIG_ENABLE_FTS3_TOKENIZER),
1702 ADDINT(SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION),
1703 ADDINT(SQLITE_DBCONFIG_MAINDBNAME),
1704 ADDINT(SQLITE_DBCONFIG_NO_CKPT_ON_CLOSE),
1705 ADDINT(SQLITE_DBCONFIG_ENABLE_QPSG),
1706 ADDINT(SQLITE_DBCONFIG_TRIGGER_EQP),
1707 #ifdef SQLITE_DBCONFIG_MAX
1708 /* hopefully this constant will be removed */
1709 ADDINT(SQLITE_DBCONFIG_MAX),
1710 #endif
1711 ADDINT(SQLITE_DBCONFIG_RESET_DATABASE),
1712 ADDINT(SQLITE_DBCONFIG_DEFENSIVE),
1713 ADDINT(SQLITE_DBCONFIG_WRITABLE_SCHEMA),
1714 ADDINT(SQLITE_DBCONFIG_DQS_DML),
1715 ADDINT(SQLITE_DBCONFIG_DQS_DDL),
1716 ADDINT(SQLITE_DBCONFIG_LEGACY_ALTER_TABLE),
1717 ADDINT(SQLITE_DBCONFIG_ENABLE_VIEW),
1718 ADDINT(SQLITE_DBCONFIG_TRUSTED_SCHEMA),
1719 ADDINT(SQLITE_DBCONFIG_LEGACY_FILE_FORMAT),
1720 END,
1721
1722 DICT("mapping_status"),
1723 ADDINT(SQLITE_STATUS_MEMORY_USED),
1724 ADDINT(SQLITE_STATUS_PAGECACHE_USED),
1725 ADDINT(SQLITE_STATUS_PAGECACHE_OVERFLOW),
1726 ADDINT(SQLITE_STATUS_SCRATCH_USED),
1727 ADDINT(SQLITE_STATUS_SCRATCH_OVERFLOW),
1728 ADDINT(SQLITE_STATUS_MALLOC_SIZE),
1729 ADDINT(SQLITE_STATUS_PARSER_STACK),
1730 ADDINT(SQLITE_STATUS_PAGECACHE_SIZE),
1731 ADDINT(SQLITE_STATUS_SCRATCH_SIZE),
1732 ADDINT(SQLITE_STATUS_MALLOC_COUNT),
1733 END,
1734
1735 DICT("mapping_db_status"),
1736 ADDINT(SQLITE_DBSTATUS_LOOKASIDE_USED),
1737 ADDINT(SQLITE_DBSTATUS_CACHE_USED),
1738 ADDINT(SQLITE_DBSTATUS_MAX),
1739 ADDINT(SQLITE_DBSTATUS_SCHEMA_USED),
1740 ADDINT(SQLITE_DBSTATUS_STMT_USED),
1741 ADDINT(SQLITE_DBSTATUS_LOOKASIDE_HIT),
1742 ADDINT(SQLITE_DBSTATUS_LOOKASIDE_MISS_FULL),
1743 ADDINT(SQLITE_DBSTATUS_LOOKASIDE_MISS_SIZE),
1744 ADDINT(SQLITE_DBSTATUS_CACHE_HIT),
1745 ADDINT(SQLITE_DBSTATUS_CACHE_MISS),
1746 ADDINT(SQLITE_DBSTATUS_CACHE_WRITE),
1747 ADDINT(SQLITE_DBSTATUS_DEFERRED_FKS),
1748 ADDINT(SQLITE_DBSTATUS_CACHE_USED_SHARED),
1749 ADDINT(SQLITE_DBSTATUS_CACHE_SPILL),
1750 END,
1751
1752 DICT("mapping_locking_level"),
1753 ADDINT(SQLITE_LOCK_NONE),
1754 ADDINT(SQLITE_LOCK_SHARED),
1755 ADDINT(SQLITE_LOCK_RESERVED),
1756 ADDINT(SQLITE_LOCK_PENDING),
1757 ADDINT(SQLITE_LOCK_EXCLUSIVE),
1758 END,
1759
1760 DICT("mapping_access"),
1761 ADDINT(SQLITE_ACCESS_EXISTS),
1762 ADDINT(SQLITE_ACCESS_READWRITE),
1763 ADDINT(SQLITE_ACCESS_READ),
1764 END,
1765
1766 DICT("mapping_device_characteristics"),
1767 ADDINT(SQLITE_IOCAP_ATOMIC),
1768 ADDINT(SQLITE_IOCAP_ATOMIC512),
1769 ADDINT(SQLITE_IOCAP_ATOMIC1K),
1770 ADDINT(SQLITE_IOCAP_ATOMIC2K),
1771 ADDINT(SQLITE_IOCAP_ATOMIC4K),
1772 ADDINT(SQLITE_IOCAP_ATOMIC8K),
1773 ADDINT(SQLITE_IOCAP_ATOMIC16K),
1774 ADDINT(SQLITE_IOCAP_ATOMIC32K),
1775 ADDINT(SQLITE_IOCAP_ATOMIC64K),
1776 ADDINT(SQLITE_IOCAP_SAFE_APPEND),
1777 ADDINT(SQLITE_IOCAP_SEQUENTIAL),
1778 ADDINT(SQLITE_IOCAP_UNDELETABLE_WHEN_OPEN),
1779 ADDINT(SQLITE_IOCAP_POWERSAFE_OVERWRITE),
1780 ADDINT(SQLITE_IOCAP_IMMUTABLE),
1781 ADDINT(SQLITE_IOCAP_BATCH_ATOMIC),
1782 END,
1783
1784 DICT("mapping_sync"),
1785 ADDINT(SQLITE_SYNC_NORMAL),
1786 ADDINT(SQLITE_SYNC_FULL),
1787 ADDINT(SQLITE_SYNC_DATAONLY),
1788 END,
1789
1790 DICT("mapping_wal_checkpoint"),
1791 ADDINT(SQLITE_CHECKPOINT_PASSIVE),
1792 ADDINT(SQLITE_CHECKPOINT_FULL),
1793 ADDINT(SQLITE_CHECKPOINT_RESTART),
1794 ADDINT(SQLITE_CHECKPOINT_TRUNCATE),
1795 END,
1796
1797 DICT("mapping_file_control"),
1798 ADDINT(SQLITE_FCNTL_LOCKSTATE),
1799 ADDINT(SQLITE_FCNTL_SIZE_HINT),
1800 ADDINT(SQLITE_FCNTL_CHUNK_SIZE),
1801 ADDINT(SQLITE_FCNTL_FILE_POINTER),
1802 ADDINT(SQLITE_FCNTL_SYNC_OMITTED),
1803 ADDINT(SQLITE_FCNTL_PERSIST_WAL),
1804 ADDINT(SQLITE_FCNTL_WIN32_AV_RETRY),
1805 ADDINT(SQLITE_FCNTL_OVERWRITE),
1806 ADDINT(SQLITE_FCNTL_POWERSAFE_OVERWRITE),
1807 ADDINT(SQLITE_FCNTL_VFSNAME),
1808 ADDINT(SQLITE_FCNTL_PRAGMA),
1809 ADDINT(SQLITE_FCNTL_BUSYHANDLER),
1810 ADDINT(SQLITE_FCNTL_TEMPFILENAME),
1811 ADDINT(SQLITE_FCNTL_MMAP_SIZE),
1812 ADDINT(SQLITE_FCNTL_TRACE),
1813 ADDINT(SQLITE_FCNTL_COMMIT_PHASETWO),
1814 ADDINT(SQLITE_FCNTL_HAS_MOVED),
1815 ADDINT(SQLITE_FCNTL_SYNC),
1816 ADDINT(SQLITE_FCNTL_WIN32_SET_HANDLE),
1817 ADDINT(SQLITE_FCNTL_LAST_ERRNO),
1818 ADDINT(SQLITE_FCNTL_WAL_BLOCK),
1819 ADDINT(SQLITE_FCNTL_GET_LOCKPROXYFILE),
1820 ADDINT(SQLITE_FCNTL_SET_LOCKPROXYFILE),
1821 ADDINT(SQLITE_FCNTL_RBU),
1822 ADDINT(SQLITE_FCNTL_ZIPVFS),
1823 ADDINT(SQLITE_FCNTL_JOURNAL_POINTER),
1824 ADDINT(SQLITE_FCNTL_VFS_POINTER),
1825 ADDINT(SQLITE_FCNTL_WIN32_GET_HANDLE),
1826 ADDINT(SQLITE_FCNTL_PDB),
1827 ADDINT(SQLITE_FCNTL_COMMIT_ATOMIC_WRITE),
1828 ADDINT(SQLITE_FCNTL_ROLLBACK_ATOMIC_WRITE),
1829 ADDINT(SQLITE_FCNTL_BEGIN_ATOMIC_WRITE),
1830 ADDINT(SQLITE_FCNTL_LOCK_TIMEOUT),
1831 ADDINT(SQLITE_FCNTL_DATA_VERSION),
1832 ADDINT(SQLITE_FCNTL_SIZE_LIMIT),
1833 ADDINT(SQLITE_FCNTL_CKPT_DONE),
1834 ADDINT(SQLITE_FCNTL_CKPT_START),
1835 ADDINT(SQLITE_FCNTL_RESERVE_BYTES),
1836 END,
1837
1838 DICT("mapping_conflict_resolution_modes"),
1839 ADDINT(SQLITE_ROLLBACK),
1840 ADDINT(SQLITE_IGNORE),
1841 ADDINT(SQLITE_FAIL),
1842 ADDINT(SQLITE_ABORT),
1843 ADDINT(SQLITE_REPLACE),
1844 END,
1845
1846 DICT("mapping_virtual_table_configuration_options"),
1847 ADDINT(SQLITE_VTAB_CONSTRAINT_SUPPORT),
1848 ADDINT(SQLITE_VTAB_DIRECTONLY),
1849 ADDINT(SQLITE_VTAB_INNOCUOUS),
1850 END,
1851
1852 DICT("mapping_xshmlock_flags"),
1853 ADDINT(SQLITE_SHM_EXCLUSIVE),
1854 ADDINT(SQLITE_SHM_LOCK),
1855 ADDINT(SQLITE_SHM_SHARED),
1856 ADDINT(SQLITE_SHM_UNLOCK),
1857 END,
1858
1859 DICT("mapping_virtual_table_scan_flags"),
1860 ADDINT(SQLITE_INDEX_SCAN_UNIQUE),
1861 END,
1862
1863 DICT("mapping_txn_state"),
1864 ADDINT(SQLITE_TXN_NONE),
1865 ADDINT(SQLITE_TXN_READ),
1866 ADDINT(SQLITE_TXN_WRITE),
1867 END};
1868
1869 for (i = 0; i < sizeof(integers) / sizeof(integers[0]); i++)
1870 {
1871 const char *name = integers[i].name;
1872 int value = integers[i].value;
1873 PyObject *pyname;
1874 PyObject *pyvalue;
1875
1876 /* should be at dict */
1877 if (!thedict)
1878 {
1879 assert(value == SENTINEL);
1880 assert(mapping_name == NULL);
1881 mapping_name = name;
1882 thedict = PyDict_New();
1883 continue;
1884 }
1885 /* at END? */
1886 if (!name)
1887 {
1888 assert(thedict);
1889 PyModule_AddObject(m, mapping_name, thedict);
1890 thedict = NULL;
1891 mapping_name = NULL;
1892 continue;
1893 }
1894 /* regular ADDINT */
1895 PyModule_AddIntConstant(m, name, value);
1896 pyname = MAKESTR(name);
1897 pyvalue = PyInt_FromLong(value);
1898 if (!pyname || !pyvalue)
1899 goto fail;
1900 PyDict_SetItem(thedict, pyname, pyvalue);
1901 PyDict_SetItem(thedict, pyvalue, pyname);
1902 Py_DECREF(pyname);
1903 Py_DECREF(pyvalue);
1904 }
1905 /* should have ended with END so thedict should be NULL */
1906 assert(thedict == NULL);
1907 }
1908
1909 add_shell(m);
1910
1911 PyModule_AddObject(m, "compile_options", get_compile_options());
1912 PyModule_AddObject(m, "keywords", get_keywords());
1913
1914 if (!PyErr_Occurred())
1915 {
1916 return
1917 #if PY_MAJOR_VERSION >= 3
1918 m
1919 #endif
1920 ;
1921 }
1922
1923 fail:
1924 Py_XDECREF(m);
1925 return
1926 #if PY_MAJOR_VERSION >= 3
1927 NULL
1928 #endif
1929 ;
1930 }
1931
1932 static void
add_shell(PyObject * apswmodule)1933 add_shell(PyObject *apswmodule)
1934 {
1935 #ifndef PYPY_VERSION
1936 PyObject *res = NULL, *maindict = NULL, *apswdict, *msvciscrap = NULL;
1937
1938 maindict = PyModule_GetDict(PyImport_AddModule("__main__"));
1939 apswdict = PyModule_GetDict(apswmodule);
1940 PyDict_SetItemString(apswdict, "__builtins__", PyDict_GetItemString(maindict, "__builtins__"));
1941 PyDict_SetItemString(apswdict, "apsw", apswmodule);
1942
1943 /* the toy compiler from microsoft falls over on string constants
1944 bigger than will fit in a 16 bit quantity. You remember 16 bits?
1945 All the rage in the early 1980s. So we have to compose chunks
1946 into a bytes and use that instead. The format string is as many
1947 %s as there are chunks. It is generated in setup.py.
1948 */
1949 msvciscrap = PyBytes_FromFormat(
1950 #include "shell.c"
1951 );
1952 if (msvciscrap)
1953 res = PyRun_StringFlags(PyBytes_AS_STRING(msvciscrap), Py_file_input, apswdict, apswdict, NULL);
1954 if (!res)
1955 PyErr_Print();
1956 assert(res);
1957 Py_XDECREF(res);
1958 Py_XDECREF(msvciscrap);
1959 #endif
1960 }
1961
1962 #ifdef APSW_TESTFIXTURES
1963 static int
APSW_Should_Fault(const char * name)1964 APSW_Should_Fault(const char *name)
1965 {
1966 PyGILState_STATE gilstate;
1967 PyObject *faultdict = NULL, *truthval = NULL, *value = NULL;
1968 int res = 0;
1969
1970 gilstate = PyGILState_Ensure();
1971
1972 if (!PyObject_HasAttrString(apswmodule, "faultdict"))
1973 PyObject_SetAttrString(apswmodule, "faultdict", PyDict_New());
1974
1975 value = MAKESTR(name);
1976
1977 faultdict = PyObject_GetAttrString(apswmodule, "faultdict");
1978
1979 truthval = PyDict_GetItem(faultdict, value);
1980 if (!truthval)
1981 goto finally;
1982
1983 /* set false if present - one shot firing */
1984 PyDict_SetItem(faultdict, value, Py_False);
1985 res = PyObject_IsTrue(truthval);
1986
1987 finally:
1988 Py_XDECREF(value);
1989 Py_XDECREF(faultdict);
1990
1991 PyGILState_Release(gilstate);
1992 return res;
1993 }
1994 #endif
1995