PageRenderTime 116ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 2ms

/db/sqlite3/src/sqlite3.c

http://github.com/zpao/v8monkey
C | 10885 lines | 2880 code | 471 blank | 7534 comment | 36 complexity | 5a0bcd447240768779afd76ca74478d0 MD5 | raw file
Possible License(s): MPL-2.0-no-copyleft-exception, LGPL-3.0, AGPL-1.0, LGPL-2.1, BSD-3-Clause, GPL-2.0, JSON, Apache-2.0, 0BSD
  1. /******************************************************************************
  2. ** This file is an amalgamation of many separate C source files from SQLite
  3. ** version 3.7.10. By combining all the individual C code files into this
  4. ** single large file, the entire code can be compiled as a single translation
  5. ** unit. This allows many compilers to do optimizations that would not be
  6. ** possible if the files were compiled separately. Performance improvements
  7. ** of 5% or more are commonly seen when SQLite is compiled as a single
  8. ** translation unit.
  9. **
  10. ** This file is all you need to compile SQLite. To use SQLite in other
  11. ** programs, you need this file and the "sqlite3.h" header file that defines
  12. ** the programming interface to the SQLite library. (If you do not have
  13. ** the "sqlite3.h" header file at hand, you will find a copy embedded within
  14. ** the text of this file. Search for "Begin file sqlite3.h" to find the start
  15. ** of the embedded sqlite3.h header file.) Additional code files may be needed
  16. ** if you want a wrapper to interface SQLite with your choice of programming
  17. ** language. The code for the "sqlite3" command-line shell is also in a
  18. ** separate file. This file contains only code for the core SQLite library.
  19. */
  20. #define SQLITE_CORE 1
  21. #define SQLITE_AMALGAMATION 1
  22. #ifndef SQLITE_PRIVATE
  23. # define SQLITE_PRIVATE static
  24. #endif
  25. #ifndef SQLITE_API
  26. # define SQLITE_API
  27. #endif
  28. /************** Begin file sqliteInt.h ***************************************/
  29. /*
  30. ** 2001 September 15
  31. **
  32. ** The author disclaims copyright to this source code. In place of
  33. ** a legal notice, here is a blessing:
  34. **
  35. ** May you do good and not evil.
  36. ** May you find forgiveness for yourself and forgive others.
  37. ** May you share freely, never taking more than you give.
  38. **
  39. *************************************************************************
  40. ** Internal interface definitions for SQLite.
  41. **
  42. */
  43. #ifndef _SQLITEINT_H_
  44. #define _SQLITEINT_H_
  45. /*
  46. ** These #defines should enable >2GB file support on POSIX if the
  47. ** underlying operating system supports it. If the OS lacks
  48. ** large file support, or if the OS is windows, these should be no-ops.
  49. **
  50. ** Ticket #2739: The _LARGEFILE_SOURCE macro must appear before any
  51. ** system #includes. Hence, this block of code must be the very first
  52. ** code in all source files.
  53. **
  54. ** Large file support can be disabled using the -DSQLITE_DISABLE_LFS switch
  55. ** on the compiler command line. This is necessary if you are compiling
  56. ** on a recent machine (ex: Red Hat 7.2) but you want your code to work
  57. ** on an older machine (ex: Red Hat 6.0). If you compile on Red Hat 7.2
  58. ** without this option, LFS is enable. But LFS does not exist in the kernel
  59. ** in Red Hat 6.0, so the code won't work. Hence, for maximum binary
  60. ** portability you should omit LFS.
  61. **
  62. ** Similar is true for Mac OS X. LFS is only supported on Mac OS X 9 and later.
  63. */
  64. #ifndef SQLITE_DISABLE_LFS
  65. # define _LARGE_FILE 1
  66. # ifndef _FILE_OFFSET_BITS
  67. # define _FILE_OFFSET_BITS 64
  68. # endif
  69. # define _LARGEFILE_SOURCE 1
  70. #endif
  71. /*
  72. ** Include the configuration header output by 'configure' if we're using the
  73. ** autoconf-based build
  74. */
  75. #ifdef _HAVE_SQLITE_CONFIG_H
  76. #include "config.h"
  77. #endif
  78. /************** Include sqliteLimit.h in the middle of sqliteInt.h ***********/
  79. /************** Begin file sqliteLimit.h *************************************/
  80. /*
  81. ** 2007 May 7
  82. **
  83. ** The author disclaims copyright to this source code. In place of
  84. ** a legal notice, here is a blessing:
  85. **
  86. ** May you do good and not evil.
  87. ** May you find forgiveness for yourself and forgive others.
  88. ** May you share freely, never taking more than you give.
  89. **
  90. *************************************************************************
  91. **
  92. ** This file defines various limits of what SQLite can process.
  93. */
  94. /*
  95. ** The maximum length of a TEXT or BLOB in bytes. This also
  96. ** limits the size of a row in a table or index.
  97. **
  98. ** The hard limit is the ability of a 32-bit signed integer
  99. ** to count the size: 2^31-1 or 2147483647.
  100. */
  101. #ifndef SQLITE_MAX_LENGTH
  102. # define SQLITE_MAX_LENGTH 1000000000
  103. #endif
  104. /*
  105. ** This is the maximum number of
  106. **
  107. ** * Columns in a table
  108. ** * Columns in an index
  109. ** * Columns in a view
  110. ** * Terms in the SET clause of an UPDATE statement
  111. ** * Terms in the result set of a SELECT statement
  112. ** * Terms in the GROUP BY or ORDER BY clauses of a SELECT statement.
  113. ** * Terms in the VALUES clause of an INSERT statement
  114. **
  115. ** The hard upper limit here is 32676. Most database people will
  116. ** tell you that in a well-normalized database, you usually should
  117. ** not have more than a dozen or so columns in any table. And if
  118. ** that is the case, there is no point in having more than a few
  119. ** dozen values in any of the other situations described above.
  120. */
  121. #ifndef SQLITE_MAX_COLUMN
  122. # define SQLITE_MAX_COLUMN 2000
  123. #endif
  124. /*
  125. ** The maximum length of a single SQL statement in bytes.
  126. **
  127. ** It used to be the case that setting this value to zero would
  128. ** turn the limit off. That is no longer true. It is not possible
  129. ** to turn this limit off.
  130. */
  131. #ifndef SQLITE_MAX_SQL_LENGTH
  132. # define SQLITE_MAX_SQL_LENGTH 1000000000
  133. #endif
  134. /*
  135. ** The maximum depth of an expression tree. This is limited to
  136. ** some extent by SQLITE_MAX_SQL_LENGTH. But sometime you might
  137. ** want to place more severe limits on the complexity of an
  138. ** expression.
  139. **
  140. ** A value of 0 used to mean that the limit was not enforced.
  141. ** But that is no longer true. The limit is now strictly enforced
  142. ** at all times.
  143. */
  144. #ifndef SQLITE_MAX_EXPR_DEPTH
  145. # define SQLITE_MAX_EXPR_DEPTH 1000
  146. #endif
  147. /*
  148. ** The maximum number of terms in a compound SELECT statement.
  149. ** The code generator for compound SELECT statements does one
  150. ** level of recursion for each term. A stack overflow can result
  151. ** if the number of terms is too large. In practice, most SQL
  152. ** never has more than 3 or 4 terms. Use a value of 0 to disable
  153. ** any limit on the number of terms in a compount SELECT.
  154. */
  155. #ifndef SQLITE_MAX_COMPOUND_SELECT
  156. # define SQLITE_MAX_COMPOUND_SELECT 500
  157. #endif
  158. /*
  159. ** The maximum number of opcodes in a VDBE program.
  160. ** Not currently enforced.
  161. */
  162. #ifndef SQLITE_MAX_VDBE_OP
  163. # define SQLITE_MAX_VDBE_OP 25000
  164. #endif
  165. /*
  166. ** The maximum number of arguments to an SQL function.
  167. */
  168. #ifndef SQLITE_MAX_FUNCTION_ARG
  169. # define SQLITE_MAX_FUNCTION_ARG 127
  170. #endif
  171. /*
  172. ** The maximum number of in-memory pages to use for the main database
  173. ** table and for temporary tables. The SQLITE_DEFAULT_CACHE_SIZE
  174. */
  175. #ifndef SQLITE_DEFAULT_CACHE_SIZE
  176. # define SQLITE_DEFAULT_CACHE_SIZE 2000
  177. #endif
  178. #ifndef SQLITE_DEFAULT_TEMP_CACHE_SIZE
  179. # define SQLITE_DEFAULT_TEMP_CACHE_SIZE 500
  180. #endif
  181. /*
  182. ** The default number of frames to accumulate in the log file before
  183. ** checkpointing the database in WAL mode.
  184. */
  185. #ifndef SQLITE_DEFAULT_WAL_AUTOCHECKPOINT
  186. # define SQLITE_DEFAULT_WAL_AUTOCHECKPOINT 1000
  187. #endif
  188. /*
  189. ** The maximum number of attached databases. This must be between 0
  190. ** and 62. The upper bound on 62 is because a 64-bit integer bitmap
  191. ** is used internally to track attached databases.
  192. */
  193. #ifndef SQLITE_MAX_ATTACHED
  194. # define SQLITE_MAX_ATTACHED 10
  195. #endif
  196. /*
  197. ** The maximum value of a ?nnn wildcard that the parser will accept.
  198. */
  199. #ifndef SQLITE_MAX_VARIABLE_NUMBER
  200. # define SQLITE_MAX_VARIABLE_NUMBER 999
  201. #endif
  202. /* Maximum page size. The upper bound on this value is 65536. This a limit
  203. ** imposed by the use of 16-bit offsets within each page.
  204. **
  205. ** Earlier versions of SQLite allowed the user to change this value at
  206. ** compile time. This is no longer permitted, on the grounds that it creates
  207. ** a library that is technically incompatible with an SQLite library
  208. ** compiled with a different limit. If a process operating on a database
  209. ** with a page-size of 65536 bytes crashes, then an instance of SQLite
  210. ** compiled with the default page-size limit will not be able to rollback
  211. ** the aborted transaction. This could lead to database corruption.
  212. */
  213. #ifdef SQLITE_MAX_PAGE_SIZE
  214. # undef SQLITE_MAX_PAGE_SIZE
  215. #endif
  216. #define SQLITE_MAX_PAGE_SIZE 65536
  217. /*
  218. ** The default size of a database page.
  219. */
  220. #ifndef SQLITE_DEFAULT_PAGE_SIZE
  221. # define SQLITE_DEFAULT_PAGE_SIZE 1024
  222. #endif
  223. #if SQLITE_DEFAULT_PAGE_SIZE>SQLITE_MAX_PAGE_SIZE
  224. # undef SQLITE_DEFAULT_PAGE_SIZE
  225. # define SQLITE_DEFAULT_PAGE_SIZE SQLITE_MAX_PAGE_SIZE
  226. #endif
  227. /*
  228. ** Ordinarily, if no value is explicitly provided, SQLite creates databases
  229. ** with page size SQLITE_DEFAULT_PAGE_SIZE. However, based on certain
  230. ** device characteristics (sector-size and atomic write() support),
  231. ** SQLite may choose a larger value. This constant is the maximum value
  232. ** SQLite will choose on its own.
  233. */
  234. #ifndef SQLITE_MAX_DEFAULT_PAGE_SIZE
  235. # define SQLITE_MAX_DEFAULT_PAGE_SIZE 8192
  236. #endif
  237. #if SQLITE_MAX_DEFAULT_PAGE_SIZE>SQLITE_MAX_PAGE_SIZE
  238. # undef SQLITE_MAX_DEFAULT_PAGE_SIZE
  239. # define SQLITE_MAX_DEFAULT_PAGE_SIZE SQLITE_MAX_PAGE_SIZE
  240. #endif
  241. /*
  242. ** Maximum number of pages in one database file.
  243. **
  244. ** This is really just the default value for the max_page_count pragma.
  245. ** This value can be lowered (or raised) at run-time using that the
  246. ** max_page_count macro.
  247. */
  248. #ifndef SQLITE_MAX_PAGE_COUNT
  249. # define SQLITE_MAX_PAGE_COUNT 1073741823
  250. #endif
  251. /*
  252. ** Maximum length (in bytes) of the pattern in a LIKE or GLOB
  253. ** operator.
  254. */
  255. #ifndef SQLITE_MAX_LIKE_PATTERN_LENGTH
  256. # define SQLITE_MAX_LIKE_PATTERN_LENGTH 50000
  257. #endif
  258. /*
  259. ** Maximum depth of recursion for triggers.
  260. **
  261. ** A value of 1 means that a trigger program will not be able to itself
  262. ** fire any triggers. A value of 0 means that no trigger programs at all
  263. ** may be executed.
  264. */
  265. #ifndef SQLITE_MAX_TRIGGER_DEPTH
  266. # define SQLITE_MAX_TRIGGER_DEPTH 1000
  267. #endif
  268. /************** End of sqliteLimit.h *****************************************/
  269. /************** Continuing where we left off in sqliteInt.h ******************/
  270. /* Disable nuisance warnings on Borland compilers */
  271. #if defined(__BORLANDC__)
  272. #pragma warn -rch /* unreachable code */
  273. #pragma warn -ccc /* Condition is always true or false */
  274. #pragma warn -aus /* Assigned value is never used */
  275. #pragma warn -csu /* Comparing signed and unsigned */
  276. #pragma warn -spa /* Suspicious pointer arithmetic */
  277. #endif
  278. /* Needed for various definitions... */
  279. #ifndef _GNU_SOURCE
  280. # define _GNU_SOURCE
  281. #endif
  282. /*
  283. ** Include standard header files as necessary
  284. */
  285. #ifdef HAVE_STDINT_H
  286. #include <stdint.h>
  287. #endif
  288. #ifdef HAVE_INTTYPES_H
  289. #include <inttypes.h>
  290. #endif
  291. /*
  292. ** The following macros are used to cast pointers to integers and
  293. ** integers to pointers. The way you do this varies from one compiler
  294. ** to the next, so we have developed the following set of #if statements
  295. ** to generate appropriate macros for a wide range of compilers.
  296. **
  297. ** The correct "ANSI" way to do this is to use the intptr_t type.
  298. ** Unfortunately, that typedef is not available on all compilers, or
  299. ** if it is available, it requires an #include of specific headers
  300. ** that vary from one machine to the next.
  301. **
  302. ** Ticket #3860: The llvm-gcc-4.2 compiler from Apple chokes on
  303. ** the ((void*)&((char*)0)[X]) construct. But MSVC chokes on ((void*)(X)).
  304. ** So we have to define the macros in different ways depending on the
  305. ** compiler.
  306. */
  307. #if defined(__PTRDIFF_TYPE__) /* This case should work for GCC */
  308. # define SQLITE_INT_TO_PTR(X) ((void*)(__PTRDIFF_TYPE__)(X))
  309. # define SQLITE_PTR_TO_INT(X) ((int)(__PTRDIFF_TYPE__)(X))
  310. #elif !defined(__GNUC__) /* Works for compilers other than LLVM */
  311. # define SQLITE_INT_TO_PTR(X) ((void*)&((char*)0)[X])
  312. # define SQLITE_PTR_TO_INT(X) ((int)(((char*)X)-(char*)0))
  313. #elif defined(HAVE_STDINT_H) /* Use this case if we have ANSI headers */
  314. # define SQLITE_INT_TO_PTR(X) ((void*)(intptr_t)(X))
  315. # define SQLITE_PTR_TO_INT(X) ((int)(intptr_t)(X))
  316. #else /* Generates a warning - but it always works */
  317. # define SQLITE_INT_TO_PTR(X) ((void*)(X))
  318. # define SQLITE_PTR_TO_INT(X) ((int)(X))
  319. #endif
  320. /*
  321. ** The SQLITE_THREADSAFE macro must be defined as 0, 1, or 2.
  322. ** 0 means mutexes are permanently disable and the library is never
  323. ** threadsafe. 1 means the library is serialized which is the highest
  324. ** level of threadsafety. 2 means the libary is multithreaded - multiple
  325. ** threads can use SQLite as long as no two threads try to use the same
  326. ** database connection at the same time.
  327. **
  328. ** Older versions of SQLite used an optional THREADSAFE macro.
  329. ** We support that for legacy.
  330. */
  331. #if !defined(SQLITE_THREADSAFE)
  332. #if defined(THREADSAFE)
  333. # define SQLITE_THREADSAFE THREADSAFE
  334. #else
  335. # define SQLITE_THREADSAFE 1 /* IMP: R-07272-22309 */
  336. #endif
  337. #endif
  338. /*
  339. ** Powersafe overwrite is on by default. But can be turned off using
  340. ** the -DSQLITE_POWERSAFE_OVERWRITE=0 command-line option.
  341. */
  342. #ifndef SQLITE_POWERSAFE_OVERWRITE
  343. # define SQLITE_POWERSAFE_OVERWRITE 1
  344. #endif
  345. /*
  346. ** The SQLITE_DEFAULT_MEMSTATUS macro must be defined as either 0 or 1.
  347. ** It determines whether or not the features related to
  348. ** SQLITE_CONFIG_MEMSTATUS are available by default or not. This value can
  349. ** be overridden at runtime using the sqlite3_config() API.
  350. */
  351. #if !defined(SQLITE_DEFAULT_MEMSTATUS)
  352. # define SQLITE_DEFAULT_MEMSTATUS 1
  353. #endif
  354. /*
  355. ** Exactly one of the following macros must be defined in order to
  356. ** specify which memory allocation subsystem to use.
  357. **
  358. ** SQLITE_SYSTEM_MALLOC // Use normal system malloc()
  359. ** SQLITE_WIN32_MALLOC // Use Win32 native heap API
  360. ** SQLITE_MEMDEBUG // Debugging version of system malloc()
  361. **
  362. ** On Windows, if the SQLITE_WIN32_MALLOC_VALIDATE macro is defined and the
  363. ** assert() macro is enabled, each call into the Win32 native heap subsystem
  364. ** will cause HeapValidate to be called. If heap validation should fail, an
  365. ** assertion will be triggered.
  366. **
  367. ** (Historical note: There used to be several other options, but we've
  368. ** pared it down to just these three.)
  369. **
  370. ** If none of the above are defined, then set SQLITE_SYSTEM_MALLOC as
  371. ** the default.
  372. */
  373. #if defined(SQLITE_SYSTEM_MALLOC)+defined(SQLITE_WIN32_MALLOC)+defined(SQLITE_MEMDEBUG)>1
  374. # error "At most one of the following compile-time configuration options\
  375. is allows: SQLITE_SYSTEM_MALLOC, SQLITE_WIN32_MALLOC, SQLITE_MEMDEBUG"
  376. #endif
  377. #if defined(SQLITE_SYSTEM_MALLOC)+defined(SQLITE_WIN32_MALLOC)+defined(SQLITE_MEMDEBUG)==0
  378. # define SQLITE_SYSTEM_MALLOC 1
  379. #endif
  380. /*
  381. ** If SQLITE_MALLOC_SOFT_LIMIT is not zero, then try to keep the
  382. ** sizes of memory allocations below this value where possible.
  383. */
  384. #if !defined(SQLITE_MALLOC_SOFT_LIMIT)
  385. # define SQLITE_MALLOC_SOFT_LIMIT 1024
  386. #endif
  387. /*
  388. ** We need to define _XOPEN_SOURCE as follows in order to enable
  389. ** recursive mutexes on most Unix systems. But Mac OS X is different.
  390. ** The _XOPEN_SOURCE define causes problems for Mac OS X we are told,
  391. ** so it is omitted there. See ticket #2673.
  392. **
  393. ** Later we learn that _XOPEN_SOURCE is poorly or incorrectly
  394. ** implemented on some systems. So we avoid defining it at all
  395. ** if it is already defined or if it is unneeded because we are
  396. ** not doing a threadsafe build. Ticket #2681.
  397. **
  398. ** See also ticket #2741.
  399. */
  400. #if !defined(_XOPEN_SOURCE) && !defined(__DARWIN__) && !defined(__APPLE__) && SQLITE_THREADSAFE
  401. # define _XOPEN_SOURCE 500 /* Needed to enable pthread recursive mutexes */
  402. #endif
  403. /*
  404. ** The TCL headers are only needed when compiling the TCL bindings.
  405. */
  406. #if defined(SQLITE_TCL) || defined(TCLSH)
  407. # include <tcl.h>
  408. #endif
  409. /*
  410. ** Many people are failing to set -DNDEBUG=1 when compiling SQLite.
  411. ** Setting NDEBUG makes the code smaller and run faster. So the following
  412. ** lines are added to automatically set NDEBUG unless the -DSQLITE_DEBUG=1
  413. ** option is set. Thus NDEBUG becomes an opt-in rather than an opt-out
  414. ** feature.
  415. */
  416. #if !defined(NDEBUG) && !defined(SQLITE_DEBUG)
  417. # define NDEBUG 1
  418. #endif
  419. /*
  420. ** The testcase() macro is used to aid in coverage testing. When
  421. ** doing coverage testing, the condition inside the argument to
  422. ** testcase() must be evaluated both true and false in order to
  423. ** get full branch coverage. The testcase() macro is inserted
  424. ** to help ensure adequate test coverage in places where simple
  425. ** condition/decision coverage is inadequate. For example, testcase()
  426. ** can be used to make sure boundary values are tested. For
  427. ** bitmask tests, testcase() can be used to make sure each bit
  428. ** is significant and used at least once. On switch statements
  429. ** where multiple cases go to the same block of code, testcase()
  430. ** can insure that all cases are evaluated.
  431. **
  432. */
  433. #ifdef SQLITE_COVERAGE_TEST
  434. SQLITE_PRIVATE void sqlite3Coverage(int);
  435. # define testcase(X) if( X ){ sqlite3Coverage(__LINE__); }
  436. #else
  437. # define testcase(X)
  438. #endif
  439. /*
  440. ** The TESTONLY macro is used to enclose variable declarations or
  441. ** other bits of code that are needed to support the arguments
  442. ** within testcase() and assert() macros.
  443. */
  444. #if !defined(NDEBUG) || defined(SQLITE_COVERAGE_TEST)
  445. # define TESTONLY(X) X
  446. #else
  447. # define TESTONLY(X)
  448. #endif
  449. /*
  450. ** Sometimes we need a small amount of code such as a variable initialization
  451. ** to setup for a later assert() statement. We do not want this code to
  452. ** appear when assert() is disabled. The following macro is therefore
  453. ** used to contain that setup code. The "VVA" acronym stands for
  454. ** "Verification, Validation, and Accreditation". In other words, the
  455. ** code within VVA_ONLY() will only run during verification processes.
  456. */
  457. #ifndef NDEBUG
  458. # define VVA_ONLY(X) X
  459. #else
  460. # define VVA_ONLY(X)
  461. #endif
  462. /*
  463. ** The ALWAYS and NEVER macros surround boolean expressions which
  464. ** are intended to always be true or false, respectively. Such
  465. ** expressions could be omitted from the code completely. But they
  466. ** are included in a few cases in order to enhance the resilience
  467. ** of SQLite to unexpected behavior - to make the code "self-healing"
  468. ** or "ductile" rather than being "brittle" and crashing at the first
  469. ** hint of unplanned behavior.
  470. **
  471. ** In other words, ALWAYS and NEVER are added for defensive code.
  472. **
  473. ** When doing coverage testing ALWAYS and NEVER are hard-coded to
  474. ** be true and false so that the unreachable code then specify will
  475. ** not be counted as untested code.
  476. */
  477. #if defined(SQLITE_COVERAGE_TEST)
  478. # define ALWAYS(X) (1)
  479. # define NEVER(X) (0)
  480. #elif !defined(NDEBUG)
  481. # define ALWAYS(X) ((X)?1:(assert(0),0))
  482. # define NEVER(X) ((X)?(assert(0),1):0)
  483. #else
  484. # define ALWAYS(X) (X)
  485. # define NEVER(X) (X)
  486. #endif
  487. /*
  488. ** Return true (non-zero) if the input is a integer that is too large
  489. ** to fit in 32-bits. This macro is used inside of various testcase()
  490. ** macros to verify that we have tested SQLite for large-file support.
  491. */
  492. #define IS_BIG_INT(X) (((X)&~(i64)0xffffffff)!=0)
  493. /*
  494. ** The macro unlikely() is a hint that surrounds a boolean
  495. ** expression that is usually false. Macro likely() surrounds
  496. ** a boolean expression that is usually true. GCC is able to
  497. ** use these hints to generate better code, sometimes.
  498. */
  499. #if defined(__GNUC__) && 0
  500. # define likely(X) __builtin_expect((X),1)
  501. # define unlikely(X) __builtin_expect((X),0)
  502. #else
  503. # define likely(X) !!(X)
  504. # define unlikely(X) !!(X)
  505. #endif
  506. /************** Include sqlite3.h in the middle of sqliteInt.h ***************/
  507. /************** Begin file sqlite3.h *****************************************/
  508. /*
  509. ** 2001 September 15
  510. **
  511. ** The author disclaims copyright to this source code. In place of
  512. ** a legal notice, here is a blessing:
  513. **
  514. ** May you do good and not evil.
  515. ** May you find forgiveness for yourself and forgive others.
  516. ** May you share freely, never taking more than you give.
  517. **
  518. *************************************************************************
  519. ** This header file defines the interface that the SQLite library
  520. ** presents to client programs. If a C-function, structure, datatype,
  521. ** or constant definition does not appear in this file, then it is
  522. ** not a published API of SQLite, is subject to change without
  523. ** notice, and should not be referenced by programs that use SQLite.
  524. **
  525. ** Some of the definitions that are in this file are marked as
  526. ** "experimental". Experimental interfaces are normally new
  527. ** features recently added to SQLite. We do not anticipate changes
  528. ** to experimental interfaces but reserve the right to make minor changes
  529. ** if experience from use "in the wild" suggest such changes are prudent.
  530. **
  531. ** The official C-language API documentation for SQLite is derived
  532. ** from comments in this file. This file is the authoritative source
  533. ** on how SQLite interfaces are suppose to operate.
  534. **
  535. ** The name of this file under configuration management is "sqlite.h.in".
  536. ** The makefile makes some minor changes to this file (such as inserting
  537. ** the version number) and changes its name to "sqlite3.h" as
  538. ** part of the build process.
  539. */
  540. #ifndef _SQLITE3_H_
  541. #define _SQLITE3_H_
  542. #include <stdarg.h> /* Needed for the definition of va_list */
  543. /*
  544. ** Make sure we can call this stuff from C++.
  545. */
  546. #if 0
  547. extern "C" {
  548. #endif
  549. /*
  550. ** Add the ability to override 'extern'
  551. */
  552. #ifndef SQLITE_EXTERN
  553. # define SQLITE_EXTERN extern
  554. #endif
  555. #ifndef SQLITE_API
  556. # define SQLITE_API
  557. #endif
  558. /*
  559. ** These no-op macros are used in front of interfaces to mark those
  560. ** interfaces as either deprecated or experimental. New applications
  561. ** should not use deprecated interfaces - they are support for backwards
  562. ** compatibility only. Application writers should be aware that
  563. ** experimental interfaces are subject to change in point releases.
  564. **
  565. ** These macros used to resolve to various kinds of compiler magic that
  566. ** would generate warning messages when they were used. But that
  567. ** compiler magic ended up generating such a flurry of bug reports
  568. ** that we have taken it all out and gone back to using simple
  569. ** noop macros.
  570. */
  571. #define SQLITE_DEPRECATED
  572. #define SQLITE_EXPERIMENTAL
  573. /*
  574. ** Ensure these symbols were not defined by some previous header file.
  575. */
  576. #ifdef SQLITE_VERSION
  577. # undef SQLITE_VERSION
  578. #endif
  579. #ifdef SQLITE_VERSION_NUMBER
  580. # undef SQLITE_VERSION_NUMBER
  581. #endif
  582. /*
  583. ** CAPI3REF: Compile-Time Library Version Numbers
  584. **
  585. ** ^(The [SQLITE_VERSION] C preprocessor macro in the sqlite3.h header
  586. ** evaluates to a string literal that is the SQLite version in the
  587. ** format "X.Y.Z" where X is the major version number (always 3 for
  588. ** SQLite3) and Y is the minor version number and Z is the release number.)^
  589. ** ^(The [SQLITE_VERSION_NUMBER] C preprocessor macro resolves to an integer
  590. ** with the value (X*1000000 + Y*1000 + Z) where X, Y, and Z are the same
  591. ** numbers used in [SQLITE_VERSION].)^
  592. ** The SQLITE_VERSION_NUMBER for any given release of SQLite will also
  593. ** be larger than the release from which it is derived. Either Y will
  594. ** be held constant and Z will be incremented or else Y will be incremented
  595. ** and Z will be reset to zero.
  596. **
  597. ** Since version 3.6.18, SQLite source code has been stored in the
  598. ** <a href="http://www.fossil-scm.org/">Fossil configuration management
  599. ** system</a>. ^The SQLITE_SOURCE_ID macro evaluates to
  600. ** a string which identifies a particular check-in of SQLite
  601. ** within its configuration management system. ^The SQLITE_SOURCE_ID
  602. ** string contains the date and time of the check-in (UTC) and an SHA1
  603. ** hash of the entire source tree.
  604. **
  605. ** See also: [sqlite3_libversion()],
  606. ** [sqlite3_libversion_number()], [sqlite3_sourceid()],
  607. ** [sqlite_version()] and [sqlite_source_id()].
  608. */
  609. #define SQLITE_VERSION "3.7.10"
  610. #define SQLITE_VERSION_NUMBER 3007010
  611. #define SQLITE_SOURCE_ID "2012-01-16 13:28:40 ebd01a8deffb5024a5d7494eef800d2366d97204"
  612. /*
  613. ** CAPI3REF: Run-Time Library Version Numbers
  614. ** KEYWORDS: sqlite3_version, sqlite3_sourceid
  615. **
  616. ** These interfaces provide the same information as the [SQLITE_VERSION],
  617. ** [SQLITE_VERSION_NUMBER], and [SQLITE_SOURCE_ID] C preprocessor macros
  618. ** but are associated with the library instead of the header file. ^(Cautious
  619. ** programmers might include assert() statements in their application to
  620. ** verify that values returned by these interfaces match the macros in
  621. ** the header, and thus insure that the application is
  622. ** compiled with matching library and header files.
  623. **
  624. ** <blockquote><pre>
  625. ** assert( sqlite3_libversion_number()==SQLITE_VERSION_NUMBER );
  626. ** assert( strcmp(sqlite3_sourceid(),SQLITE_SOURCE_ID)==0 );
  627. ** assert( strcmp(sqlite3_libversion(),SQLITE_VERSION)==0 );
  628. ** </pre></blockquote>)^
  629. **
  630. ** ^The sqlite3_version[] string constant contains the text of [SQLITE_VERSION]
  631. ** macro. ^The sqlite3_libversion() function returns a pointer to the
  632. ** to the sqlite3_version[] string constant. The sqlite3_libversion()
  633. ** function is provided for use in DLLs since DLL users usually do not have
  634. ** direct access to string constants within the DLL. ^The
  635. ** sqlite3_libversion_number() function returns an integer equal to
  636. ** [SQLITE_VERSION_NUMBER]. ^The sqlite3_sourceid() function returns
  637. ** a pointer to a string constant whose value is the same as the
  638. ** [SQLITE_SOURCE_ID] C preprocessor macro.
  639. **
  640. ** See also: [sqlite_version()] and [sqlite_source_id()].
  641. */
  642. SQLITE_API const char sqlite3_version[] = SQLITE_VERSION;
  643. SQLITE_API const char *sqlite3_libversion(void);
  644. SQLITE_API const char *sqlite3_sourceid(void);
  645. SQLITE_API int sqlite3_libversion_number(void);
  646. /*
  647. ** CAPI3REF: Run-Time Library Compilation Options Diagnostics
  648. **
  649. ** ^The sqlite3_compileoption_used() function returns 0 or 1
  650. ** indicating whether the specified option was defined at
  651. ** compile time. ^The SQLITE_ prefix may be omitted from the
  652. ** option name passed to sqlite3_compileoption_used().
  653. **
  654. ** ^The sqlite3_compileoption_get() function allows iterating
  655. ** over the list of options that were defined at compile time by
  656. ** returning the N-th compile time option string. ^If N is out of range,
  657. ** sqlite3_compileoption_get() returns a NULL pointer. ^The SQLITE_
  658. ** prefix is omitted from any strings returned by
  659. ** sqlite3_compileoption_get().
  660. **
  661. ** ^Support for the diagnostic functions sqlite3_compileoption_used()
  662. ** and sqlite3_compileoption_get() may be omitted by specifying the
  663. ** [SQLITE_OMIT_COMPILEOPTION_DIAGS] option at compile time.
  664. **
  665. ** See also: SQL functions [sqlite_compileoption_used()] and
  666. ** [sqlite_compileoption_get()] and the [compile_options pragma].
  667. */
  668. #ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS
  669. SQLITE_API int sqlite3_compileoption_used(const char *zOptName);
  670. SQLITE_API const char *sqlite3_compileoption_get(int N);
  671. #endif
  672. /*
  673. ** CAPI3REF: Test To See If The Library Is Threadsafe
  674. **
  675. ** ^The sqlite3_threadsafe() function returns zero if and only if
  676. ** SQLite was compiled with mutexing code omitted due to the
  677. ** [SQLITE_THREADSAFE] compile-time option being set to 0.
  678. **
  679. ** SQLite can be compiled with or without mutexes. When
  680. ** the [SQLITE_THREADSAFE] C preprocessor macro is 1 or 2, mutexes
  681. ** are enabled and SQLite is threadsafe. When the
  682. ** [SQLITE_THREADSAFE] macro is 0,
  683. ** the mutexes are omitted. Without the mutexes, it is not safe
  684. ** to use SQLite concurrently from more than one thread.
  685. **
  686. ** Enabling mutexes incurs a measurable performance penalty.
  687. ** So if speed is of utmost importance, it makes sense to disable
  688. ** the mutexes. But for maximum safety, mutexes should be enabled.
  689. ** ^The default behavior is for mutexes to be enabled.
  690. **
  691. ** This interface can be used by an application to make sure that the
  692. ** version of SQLite that it is linking against was compiled with
  693. ** the desired setting of the [SQLITE_THREADSAFE] macro.
  694. **
  695. ** This interface only reports on the compile-time mutex setting
  696. ** of the [SQLITE_THREADSAFE] flag. If SQLite is compiled with
  697. ** SQLITE_THREADSAFE=1 or =2 then mutexes are enabled by default but
  698. ** can be fully or partially disabled using a call to [sqlite3_config()]
  699. ** with the verbs [SQLITE_CONFIG_SINGLETHREAD], [SQLITE_CONFIG_MULTITHREAD],
  700. ** or [SQLITE_CONFIG_MUTEX]. ^(The return value of the
  701. ** sqlite3_threadsafe() function shows only the compile-time setting of
  702. ** thread safety, not any run-time changes to that setting made by
  703. ** sqlite3_config(). In other words, the return value from sqlite3_threadsafe()
  704. ** is unchanged by calls to sqlite3_config().)^
  705. **
  706. ** See the [threading mode] documentation for additional information.
  707. */
  708. SQLITE_API int sqlite3_threadsafe(void);
  709. /*
  710. ** CAPI3REF: Database Connection Handle
  711. ** KEYWORDS: {database connection} {database connections}
  712. **
  713. ** Each open SQLite database is represented by a pointer to an instance of
  714. ** the opaque structure named "sqlite3". It is useful to think of an sqlite3
  715. ** pointer as an object. The [sqlite3_open()], [sqlite3_open16()], and
  716. ** [sqlite3_open_v2()] interfaces are its constructors, and [sqlite3_close()]
  717. ** is its destructor. There are many other interfaces (such as
  718. ** [sqlite3_prepare_v2()], [sqlite3_create_function()], and
  719. ** [sqlite3_busy_timeout()] to name but three) that are methods on an
  720. ** sqlite3 object.
  721. */
  722. typedef struct sqlite3 sqlite3;
  723. /*
  724. ** CAPI3REF: 64-Bit Integer Types
  725. ** KEYWORDS: sqlite_int64 sqlite_uint64
  726. **
  727. ** Because there is no cross-platform way to specify 64-bit integer types
  728. ** SQLite includes typedefs for 64-bit signed and unsigned integers.
  729. **
  730. ** The sqlite3_int64 and sqlite3_uint64 are the preferred type definitions.
  731. ** The sqlite_int64 and sqlite_uint64 types are supported for backwards
  732. ** compatibility only.
  733. **
  734. ** ^The sqlite3_int64 and sqlite_int64 types can store integer values
  735. ** between -9223372036854775808 and +9223372036854775807 inclusive. ^The
  736. ** sqlite3_uint64 and sqlite_uint64 types can store integer values
  737. ** between 0 and +18446744073709551615 inclusive.
  738. */
  739. #ifdef SQLITE_INT64_TYPE
  740. typedef SQLITE_INT64_TYPE sqlite_int64;
  741. typedef unsigned SQLITE_INT64_TYPE sqlite_uint64;
  742. #elif defined(_MSC_VER) || defined(__BORLANDC__)
  743. typedef __int64 sqlite_int64;
  744. typedef unsigned __int64 sqlite_uint64;
  745. #else
  746. typedef long long int sqlite_int64;
  747. typedef unsigned long long int sqlite_uint64;
  748. #endif
  749. typedef sqlite_int64 sqlite3_int64;
  750. typedef sqlite_uint64 sqlite3_uint64;
  751. /*
  752. ** If compiling for a processor that lacks floating point support,
  753. ** substitute integer for floating-point.
  754. */
  755. #ifdef SQLITE_OMIT_FLOATING_POINT
  756. # define double sqlite3_int64
  757. #endif
  758. /*
  759. ** CAPI3REF: Closing A Database Connection
  760. **
  761. ** ^The sqlite3_close() routine is the destructor for the [sqlite3] object.
  762. ** ^Calls to sqlite3_close() return SQLITE_OK if the [sqlite3] object is
  763. ** successfully destroyed and all associated resources are deallocated.
  764. **
  765. ** Applications must [sqlite3_finalize | finalize] all [prepared statements]
  766. ** and [sqlite3_blob_close | close] all [BLOB handles] associated with
  767. ** the [sqlite3] object prior to attempting to close the object. ^If
  768. ** sqlite3_close() is called on a [database connection] that still has
  769. ** outstanding [prepared statements] or [BLOB handles], then it returns
  770. ** SQLITE_BUSY.
  771. **
  772. ** ^If [sqlite3_close()] is invoked while a transaction is open,
  773. ** the transaction is automatically rolled back.
  774. **
  775. ** The C parameter to [sqlite3_close(C)] must be either a NULL
  776. ** pointer or an [sqlite3] object pointer obtained
  777. ** from [sqlite3_open()], [sqlite3_open16()], or
  778. ** [sqlite3_open_v2()], and not previously closed.
  779. ** ^Calling sqlite3_close() with a NULL pointer argument is a
  780. ** harmless no-op.
  781. */
  782. SQLITE_API int sqlite3_close(sqlite3 *);
  783. /*
  784. ** The type for a callback function.
  785. ** This is legacy and deprecated. It is included for historical
  786. ** compatibility and is not documented.
  787. */
  788. typedef int (*sqlite3_callback)(void*,int,char**, char**);
  789. /*
  790. ** CAPI3REF: One-Step Query Execution Interface
  791. **
  792. ** The sqlite3_exec() interface is a convenience wrapper around
  793. ** [sqlite3_prepare_v2()], [sqlite3_step()], and [sqlite3_finalize()],
  794. ** that allows an application to run multiple statements of SQL
  795. ** without having to use a lot of C code.
  796. **
  797. ** ^The sqlite3_exec() interface runs zero or more UTF-8 encoded,
  798. ** semicolon-separate SQL statements passed into its 2nd argument,
  799. ** in the context of the [database connection] passed in as its 1st
  800. ** argument. ^If the callback function of the 3rd argument to
  801. ** sqlite3_exec() is not NULL, then it is invoked for each result row
  802. ** coming out of the evaluated SQL statements. ^The 4th argument to
  803. ** sqlite3_exec() is relayed through to the 1st argument of each
  804. ** callback invocation. ^If the callback pointer to sqlite3_exec()
  805. ** is NULL, then no callback is ever invoked and result rows are
  806. ** ignored.
  807. **
  808. ** ^If an error occurs while evaluating the SQL statements passed into
  809. ** sqlite3_exec(), then execution of the current statement stops and
  810. ** subsequent statements are skipped. ^If the 5th parameter to sqlite3_exec()
  811. ** is not NULL then any error message is written into memory obtained
  812. ** from [sqlite3_malloc()] and passed back through the 5th parameter.
  813. ** To avoid memory leaks, the application should invoke [sqlite3_free()]
  814. ** on error message strings returned through the 5th parameter of
  815. ** of sqlite3_exec() after the error message string is no longer needed.
  816. ** ^If the 5th parameter to sqlite3_exec() is not NULL and no errors
  817. ** occur, then sqlite3_exec() sets the pointer in its 5th parameter to
  818. ** NULL before returning.
  819. **
  820. ** ^If an sqlite3_exec() callback returns non-zero, the sqlite3_exec()
  821. ** routine returns SQLITE_ABORT without invoking the callback again and
  822. ** without running any subsequent SQL statements.
  823. **
  824. ** ^The 2nd argument to the sqlite3_exec() callback function is the
  825. ** number of columns in the result. ^The 3rd argument to the sqlite3_exec()
  826. ** callback is an array of pointers to strings obtained as if from
  827. ** [sqlite3_column_text()], one for each column. ^If an element of a
  828. ** result row is NULL then the corresponding string pointer for the
  829. ** sqlite3_exec() callback is a NULL pointer. ^The 4th argument to the
  830. ** sqlite3_exec() callback is an array of pointers to strings where each
  831. ** entry represents the name of corresponding result column as obtained
  832. ** from [sqlite3_column_name()].
  833. **
  834. ** ^If the 2nd parameter to sqlite3_exec() is a NULL pointer, a pointer
  835. ** to an empty string, or a pointer that contains only whitespace and/or
  836. ** SQL comments, then no SQL statements are evaluated and the database
  837. ** is not changed.
  838. **
  839. ** Restrictions:
  840. **
  841. ** <ul>
  842. ** <li> The application must insure that the 1st parameter to sqlite3_exec()
  843. ** is a valid and open [database connection].
  844. ** <li> The application must not close [database connection] specified by
  845. ** the 1st parameter to sqlite3_exec() while sqlite3_exec() is running.
  846. ** <li> The application must not modify the SQL statement text passed into
  847. ** the 2nd parameter of sqlite3_exec() while sqlite3_exec() is running.
  848. ** </ul>
  849. */
  850. SQLITE_API int sqlite3_exec(
  851. sqlite3*, /* An open database */
  852. const char *sql, /* SQL to be evaluated */
  853. int (*callback)(void*,int,char**,char**), /* Callback function */
  854. void *, /* 1st argument to callback */
  855. char **errmsg /* Error msg written here */
  856. );
  857. /*
  858. ** CAPI3REF: Result Codes
  859. ** KEYWORDS: SQLITE_OK {error code} {error codes}
  860. ** KEYWORDS: {result code} {result codes}
  861. **
  862. ** Many SQLite functions return an integer result code from the set shown
  863. ** here in order to indicate success or failure.
  864. **
  865. ** New error codes may be added in future versions of SQLite.
  866. **
  867. ** See also: [SQLITE_IOERR_READ | extended result codes],
  868. ** [sqlite3_vtab_on_conflict()] [SQLITE_ROLLBACK | result codes].
  869. */
  870. #define SQLITE_OK 0 /* Successful result */
  871. /* beginning-of-error-codes */
  872. #define SQLITE_ERROR 1 /* SQL error or missing database */
  873. #define SQLITE_INTERNAL 2 /* Internal logic error in SQLite */
  874. #define SQLITE_PERM 3 /* Access permission denied */
  875. #define SQLITE_ABORT 4 /* Callback routine requested an abort */
  876. #define SQLITE_BUSY 5 /* The database file is locked */
  877. #define SQLITE_LOCKED 6 /* A table in the database is locked */
  878. #define SQLITE_NOMEM 7 /* A malloc() failed */
  879. #define SQLITE_READONLY 8 /* Attempt to write a readonly database */
  880. #define SQLITE_INTERRUPT 9 /* Operation terminated by sqlite3_interrupt()*/
  881. #define SQLITE_IOERR 10 /* Some kind of disk I/O error occurred */
  882. #define SQLITE_CORRUPT 11 /* The database disk image is malformed */
  883. #define SQLITE_NOTFOUND 12 /* Unknown opcode in sqlite3_file_control() */
  884. #define SQLITE_FULL 13 /* Insertion failed because database is full */
  885. #define SQLITE_CANTOPEN 14 /* Unable to open the database file */
  886. #define SQLITE_PROTOCOL 15 /* Database lock protocol error */
  887. #define SQLITE_EMPTY 16 /* Database is empty */
  888. #define SQLITE_SCHEMA 17 /* The database schema changed */
  889. #define SQLITE_TOOBIG 18 /* String or BLOB exceeds size limit */
  890. #define SQLITE_CONSTRAINT 19 /* Abort due to constraint violation */
  891. #define SQLITE_MISMATCH 20 /* Data type mismatch */
  892. #define SQLITE_MISUSE 21 /* Library used incorrectly */
  893. #define SQLITE_NOLFS 22 /* Uses OS features not supported on host */
  894. #define SQLITE_AUTH 23 /* Authorization denied */
  895. #define SQLITE_FORMAT 24 /* Auxiliary database format error */
  896. #define SQLITE_RANGE 25 /* 2nd parameter to sqlite3_bind out of range */
  897. #define SQLITE_NOTADB 26 /* File opened that is not a database file */
  898. #define SQLITE_ROW 100 /* sqlite3_step() has another row ready */
  899. #define SQLITE_DONE 101 /* sqlite3_step() has finished executing */
  900. /* end-of-error-codes */
  901. /*
  902. ** CAPI3REF: Extended Result Codes
  903. ** KEYWORDS: {extended error code} {extended error codes}
  904. ** KEYWORDS: {extended result code} {extended result codes}
  905. **
  906. ** In its default configuration, SQLite API routines return one of 26 integer
  907. ** [SQLITE_OK | result codes]. However, experience has shown that many of
  908. ** these result codes are too coarse-grained. They do not provide as
  909. ** much information about problems as programmers might like. In an effort to
  910. ** address this, newer versions of SQLite (version 3.3.8 and later) include
  911. ** support for additional result codes that provide more detailed information
  912. ** about errors. The extended result codes are enabled or disabled
  913. ** on a per database connection basis using the
  914. ** [sqlite3_extended_result_codes()] API.
  915. **
  916. ** Some of the available extended result codes are listed here.
  917. ** One may expect the number of extended result codes will be expand
  918. ** over time. Software that uses extended result codes should expect
  919. ** to see new result codes in future releases of SQLite.
  920. **
  921. ** The SQLITE_OK result code will never be extended. It will always
  922. ** be exactly zero.
  923. */
  924. #define SQLITE_IOERR_READ (SQLITE_IOERR | (1<<8))
  925. #define SQLITE_IOERR_SHORT_READ (SQLITE_IOERR | (2<<8))
  926. #define SQLITE_IOERR_WRITE (SQLITE_IOERR | (3<<8))
  927. #define SQLITE_IOERR_FSYNC (SQLITE_IOERR | (4<<8))
  928. #define SQLITE_IOERR_DIR_FSYNC (SQLITE_IOERR | (5<<8))
  929. #define SQLITE_IOERR_TRUNCATE (SQLITE_IOERR | (6<<8))
  930. #define SQLITE_IOERR_FSTAT (SQLITE_IOERR | (7<<8))
  931. #define SQLITE_IOERR_UNLOCK (SQLITE_IOERR | (8<<8))
  932. #define SQLITE_IOERR_RDLOCK (SQLITE_IOERR | (9<<8))
  933. #define SQLITE_IOERR_DELETE (SQLITE_IOERR | (10<<8))
  934. #define SQLITE_IOERR_BLOCKED (SQLITE_IOERR | (11<<8))
  935. #define SQLITE_IOERR_NOMEM (SQLITE_IOERR | (12<<8))
  936. #define SQLITE_IOERR_ACCESS (SQLITE_IOERR | (13<<8))
  937. #define SQLITE_IOERR_CHECKRESERVEDLOCK (SQLITE_IOERR | (14<<8))
  938. #define SQLITE_IOERR_LOCK (SQLITE_IOERR | (15<<8))
  939. #define SQLITE_IOERR_CLOSE (SQLITE_IOERR | (16<<8))
  940. #define SQLITE_IOERR_DIR_CLOSE (SQLITE_IOERR | (17<<8))
  941. #define SQLITE_IOERR_SHMOPEN (SQLITE_IOERR | (18<<8))
  942. #define SQLITE_IOERR_SHMSIZE (SQLITE_IOERR | (19<<8))
  943. #define SQLITE_IOERR_SHMLOCK (SQLITE_IOERR | (20<<8))
  944. #define SQLITE_IOERR_SHMMAP (SQLITE_IOERR | (21<<8))
  945. #define SQLITE_IOERR_SEEK (SQLITE_IOERR | (22<<8))
  946. #define SQLITE_LOCKED_SHAREDCACHE (SQLITE_LOCKED | (1<<8))
  947. #define SQLITE_BUSY_RECOVERY (SQLITE_BUSY | (1<<8))
  948. #define SQLITE_CANTOPEN_NOTEMPDIR (SQLITE_CANTOPEN | (1<<8))
  949. #define SQLITE_CORRUPT_VTAB (SQLITE_CORRUPT | (1<<8))
  950. #define SQLITE_READONLY_RECOVERY (SQLITE_READONLY | (1<<8))
  951. #define SQLITE_READONLY_CANTLOCK (SQLITE_READONLY | (2<<8))
  952. /*
  953. ** CAPI3REF: Flags For File Open Operations
  954. **
  955. ** These bit values are intended for use in the
  956. ** 3rd parameter to the [sqlite3_open_v2()] interface and
  957. ** in the 4th parameter to the [sqlite3_vfs.xOpen] method.
  958. */
  959. #define SQLITE_OPEN_READONLY 0x00000001 /* Ok for sqlite3_open_v2() */
  960. #define SQLITE_OPEN_READWRITE 0x00000002 /* Ok for sqlite3_open_v2() */
  961. #define SQLITE_OPEN_CREATE 0x00000004 /* Ok for sqlite3_open_v2() */
  962. #define SQLITE_OPEN_DELETEONCLOSE 0x00000008 /* VFS only */
  963. #define SQLITE_OPEN_EXCLUSIVE 0x00000010 /* VFS only */
  964. #define SQLITE_OPEN_AUTOPROXY 0x00000020 /* VFS only */
  965. #define SQLITE_OPEN_URI 0x00000040 /* Ok for sqlite3_open_v2() */
  966. #define SQLITE_OPEN_MAIN_DB 0x00000100 /* VFS only */
  967. #define SQLITE_OPEN_TEMP_DB 0x00000200 /* VFS only */
  968. #define SQLITE_OPEN_TRANSIENT_DB 0x00000400 /* VFS only */
  969. #define SQLITE_OPEN_MAIN_JOURNAL 0x00000800 /* VFS only */
  970. #define SQLITE_OPEN_TEMP_JOURNAL 0x00001000 /* VFS only */
  971. #define SQLITE_OPEN_SUBJOURNAL 0x00002000 /* VFS only */
  972. #define SQLITE_OPEN_MASTER_JOURNAL 0x00004000 /* VFS only */
  973. #define SQLITE_OPEN_NOMUTEX 0x00008000 /* Ok for sqlite3_open_v2() */
  974. #define SQLITE_OPEN_FULLMUTEX 0x00010000 /* Ok for sqlite3_open_v2() */
  975. #define SQLITE_OPEN_SHAREDCACHE 0x00020000 /* Ok for sqlite3_open_v2() */
  976. #define SQLITE_OPEN_PRIVATECACHE 0x00040000 /* Ok for sqlite3_open_v2() */
  977. #define SQLITE_OPEN_WAL 0x00080000 /* VFS only */
  978. /* Reserved: 0x00F00000 */
  979. /*
  980. ** CAPI3REF: Device Characteristics
  981. **
  982. ** The xDeviceCharacteristics method of the [sqlite3_io_methods]
  983. ** object returns an integer which is a vector of the these
  984. ** bit values expressing I/O characteristics of the mass storage
  985. ** device that holds the file that the [sqlite3_io_methods]
  986. ** refers to.
  987. **
  988. ** The SQLITE_IOCAP_ATOMIC property means that all writes of
  989. ** any size are atomic. The SQLITE_IOCAP_ATOMICnnn values
  990. ** mean that writes of blocks that are nnn bytes in size and
  991. ** are aligned to an address which is an integer multiple of
  992. ** nnn are atomic. The SQLITE_IOCAP_SAFE_APPEND value means
  993. ** that when data is appended to a file, the data is appended
  994. ** first then the size of the file is extended, never the other
  995. ** way around. The SQLITE_IOCAP_SEQUENTIAL property means that
  996. ** information is written to disk in the same order as calls
  997. ** to xWrite(). The SQLITE_IOCAP_POWERSAFE_OVERWRITE property means that
  998. ** after reboot following a crash or power loss, the only bytes in a
  999. ** file that were written at the application level might have changed
  1000. ** and that adjacent bytes, even bytes within the same sector are
  1001. ** guaranteed to be unchanged.
  1002. */
  1003. #define SQLITE_IOCAP_ATOMIC 0x00000001
  1004. #define SQLITE_IOCAP_ATOMIC512 0x00000002
  1005. #define SQLITE_IOCAP_ATOMIC1K 0x00000004
  1006. #define SQLITE_IOCAP_ATOMIC2K 0x00000008
  1007. #define SQLITE_IOCAP_ATOMIC4K 0x00000010
  1008. #define SQLITE_IOCAP_ATOMIC8K 0x00000020
  1009. #define SQLITE_IOCAP_ATOMIC16K 0x00000040
  1010. #define SQLITE_IOCAP_ATOMIC32K 0x00000080
  1011. #define SQLITE_IOCAP_ATOMIC64K 0x00000100
  1012. #define SQLITE_IOCAP_SAFE_APPEND 0x00000200
  1013. #define SQLITE_IOCAP_SEQUENTIAL 0x00000400
  1014. #define SQLITE_IOCAP_UNDELETABLE_WHEN_OPEN 0x00000800
  1015. #define SQLITE_IOCAP_POWERSAFE_OVERWRITE 0x00001000
  1016. /*
  1017. ** CAPI3REF: File Locking Levels
  1018. **
  1019. ** SQLite uses one of these integer values as the second
  1020. ** argument to calls it makes to the xLock() and xUnlock() methods
  1021. ** of an [sqlite3_io_methods] object.
  1022. */
  1023. #define SQLITE_LOCK_NONE 0
  1024. #define SQLITE_LOCK_SHARED 1
  1025. #define SQLITE_LOCK_RESERVED 2
  1026. #define SQLITE_LOCK_PENDING 3
  1027. #define SQLITE_LOCK_EXCLUSIVE 4
  1028. /*
  1029. ** CAPI3REF: Synchronization Type Flags
  1030. **
  1031. ** When SQLite invokes the xSync() method of an
  1032. ** [sqlite3_io_methods] object it uses a combination of
  1033. ** these integer values as the second argument.
  1034. **
  1035. ** When the SQLITE_SYNC_DATAONLY flag is used, it means that the
  1036. ** sync operation only needs to flush data to mass storage. Inode
  1037. ** information need not be flushed. If the lower four bits of the flag
  1038. ** equal SQLITE_SYNC_NORMAL, that means to use normal fsync() semantics.
  1039. ** If the lower four bits equal SQLITE_SYNC_FULL, that means
  1040. ** to use Mac OS X style fullsync instead of fsync().
  1041. **
  1042. ** Do not confuse the SQLITE_SYNC_NORMAL and SQLITE_SYNC_FULL flags
  1043. ** with the [PRAGMA synchronous]=NORMAL and [PRAGMA synchronous]=FULL
  1044. ** settings. The [synchronous pragma] determines when calls to the
  1045. ** xSync VFS method occur and applies uniformly across all platforms.
  1046. ** The SQLITE_SYNC_NORMAL and SQLITE_SYNC_FULL flags determine how
  1047. ** energetic or rigorous or forceful the sync operations are and
  1048. ** only make a difference on Mac OSX for the default SQLite code.
  1049. ** (Third-party VFS implementations might also make the distinction
  1050. ** between SQLITE_SYNC_NORMAL and SQLITE_SYNC_FULL, but among the
  1051. ** operating systems natively supported by SQLite, only Mac OSX
  1052. ** cares about the difference.)
  1053. */
  1054. #define SQLITE_SYNC_NORMAL 0x00002
  1055. #define SQLITE_SYNC_FULL 0x00003
  1056. #define SQLITE_SYNC_DATAONLY 0x00010
  1057. /*
  1058. ** CAPI3REF: OS Interface Open File Handle
  1059. **
  1060. ** An [sqlite3_file] object represents an open file in the
  1061. ** [sqlite3_vfs | OS interface layer]. Individual OS interface
  1062. ** implementations will
  1063. ** want to subclass this object by appending additional fields
  1064. ** for their own use. The pMethods entry is a pointer to an
  1065. ** [sqlite3_io_methods] object that defines methods for performing
  1066. ** I/O operations on the open file.
  1067. */
  1068. typedef struct sqlite3_file sqlite3_file;
  1069. struct sqlite3_file {
  1070. const struct sqlite3_io_methods *pMethods; /* Methods for an open file */
  1071. };
  1072. /*
  1073. ** CAPI3REF: OS Interface File Virtual Methods Object
  1074. **
  1075. ** Every file opened by the [sqlite3_vfs.xOpen] method populates an
  1076. ** [sqlite3_file] object (or, more commonly, a subclass of the
  1077. ** [sqlite3_file] object) with a pointer to an instance of this object.
  1078. ** This object defines the methods used to perform various operations
  1079. ** against the open file represented by the [sqlite3_file] object.
  1080. **
  1081. ** If the [sqlite3_vfs.xOpen] method sets the sqlite3_file.pMethods element
  1082. ** to a non-NULL pointer, then the sqlite3_io_methods.xClose method
  1083. ** may be invoked even if the [sqlite3_vfs.xOpen] reported that it failed. The
  1084. ** only way to prevent a call to xClose following a failed [sqlite3_vfs.xOpen]
  1085. ** is for the [sqlite3_vfs.xOpen] to set the sqlite3_file.pMethods element
  1086. ** to NULL.
  1087. **
  1088. ** The flags argument to xSync may be one of [SQLITE_SYNC_NORMAL] or
  1089. ** [SQLITE_SYNC_FULL]. The first choice is the normal fsync().
  1090. ** The second choice is a Mac OS X style fullsync. The [SQLITE_SYNC_DATAONLY]
  1091. ** flag may be ORed in to indicate that only the data of the file
  1092. ** and not its inode needs to be synced.
  1093. **
  1094. ** The integer values to xLock() and xUnlock() are one of
  1095. ** <ul>
  1096. ** <li> [SQLITE_LOCK_NONE],
  1097. ** <li> [SQLITE_LOCK_SHARED],
  1098. ** <li> [SQLITE_LOCK_RESERVED],
  1099. ** <li> [SQLITE_LOCK_PENDING], or
  1100. ** <li> [SQLITE_LOCK_EXCLUSIVE].
  1101. ** </ul>
  1102. ** xLock() increases the lock. xUnlock() decreases the lock.
  1103. ** The xCheckReservedLock() method checks whether any database connection,
  1104. ** either in this process or in some other process, is holding a RESERVED,
  1105. ** PENDING, or EXCLUSIVE lock on the file. It returns true
  1106. ** if such a lock exists and false otherwise.
  1107. **
  1108. ** The xFileControl() method is a generic interface that allows custom
  1109. ** VFS implementations to directly control an open file using the
  1110. ** [sqlite3_file_control()] interface. The second "op" argument is an
  1111. ** integer opcode. The third argument is a generic pointer intended to
  1112. ** point to a structure that may contain arguments or space in which to
  1113. ** write return values. Potential uses for xFileControl() might be
  1114. ** functions to enable blocking locks with timeouts, to change the
  1115. ** locking strategy (for example to use dot-file locks), to inquire
  1116. ** about the status of a lock, or to break stale locks. The SQLite
  1117. ** core reserves all opcodes less than 100 for its own use.
  1118. ** A [SQLITE_FCNTL_LOCKSTATE | list of opcodes] less than 100 is available.
  1119. ** Applications that define a custom xFileControl method should use opcodes
  1120. ** greater than 100 to avoid conflicts. VFS implementations should
  1121. ** return [SQLITE_NOTFOUND] for file control opcodes that they do not
  1122. ** recognize.
  1123. **
  1124. ** The xSectorSize() method returns the sector size of the
  1125. ** device that underlies the file. The sector size is the
  1126. ** minimum write that can be performed without disturbing
  1127. ** other bytes in the file. The xDeviceCharacteristics()
  1128. ** method returns a bit vector describing behaviors of the
  1129. ** underlying device:
  1130. **
  1131. ** <ul>
  1132. ** <li> [SQLITE_IOCAP_ATOMIC]
  1133. ** <li> [SQLITE_IOCAP_ATOMIC512]
  1134. ** <li> [SQLITE_IOCAP_ATOMIC1K]
  1135. ** <li> [SQLITE_IOCAP_ATOMIC2K]
  1136. ** <li> [SQLITE_IOCAP_ATOMIC4K]
  1137. ** <li> [SQLITE_IOCAP_ATOMIC8K]
  1138. ** <li> [SQLITE_IOCAP_ATOMIC16K]
  1139. ** <li> [SQLITE_IOCAP_ATOMIC32K]
  1140. ** <li> [SQLITE_IOCAP_ATOMIC64K]
  1141. ** <li> [SQLITE_IOCAP_SAFE_APPEND]
  1142. ** <li> [SQLITE_IOCAP_SEQUENTIAL]
  1143. ** </ul>
  1144. **
  1145. ** The SQLITE_IOCAP_ATOMIC property means that all writes of
  1146. ** any size are atomic. The SQLITE_IOCAP_ATOMICnnn values
  1147. ** mean that writes of blocks that are nnn bytes in size and
  1148. ** are aligned to an address which is an integer multiple of
  1149. ** nnn are atomic. The SQLITE_IOCAP_SAFE_APPEND value means
  1150. ** that when data is appended to a file, the data is appended
  1151. ** first then the size of the file is extended, never the other
  1152. ** way around. The SQLITE_IOCAP_SEQUENTIAL property means that
  1153. ** information is written to disk in the same order as calls
  1154. ** to xWrite().
  1155. **
  1156. ** If xRead() returns SQLITE_IOERR_SHORT_READ it must also fill
  1157. ** in the unread portions of the buffer with zeros. A VFS that
  1158. ** fails to zero-fill short reads might seem to work. However,
  1159. ** failure to zero-fill short reads will eventually lead to
  1160. ** database corruption.
  1161. */
  1162. typedef struct sqlite3_io_methods sqlite3_io_methods;
  1163. struct sqlite3_io_methods {
  1164. int iVersion;
  1165. int (*xClose)(sqlite3_file*);
  1166. int (*xRead)(sqlite3_file*, void*, int iAmt, sqlite3_int64 iOfst);
  1167. int (*xWrite)(sqlite3_file*, const void*, int iAmt, sqlite3_int64 iOfst);
  1168. int (*xTruncate)(sqlite3_file*, sqlite3_int64 size);
  1169. int (*xSync)(sqlite3_file*, int flags);
  1170. int (*xFileSize)(sqlite3_file*, sqlite3_int64 *pSize);
  1171. int (*xLock)(sqlite3_file*, int);
  1172. int (*xUnlock)(sqlite3_file*, int);
  1173. int (*xCheckReservedLock)(sqlite3_file*, int *pResOut);
  1174. int (*xFileControl)(sqlite3_file*, int op, void *pArg);
  1175. int (*xSectorSize)(sqlite3_file*);
  1176. int (*xDeviceCharacteristics)(sqlite3_file*);
  1177. /* Methods above are valid for version 1 */
  1178. int (*xShmMap)(sqlite3_file*, int iPg, int pgsz, int, void volatile**);
  1179. int (*xShmLock)(sqlite3_file*, int offset, int n, int flags);
  1180. void (*xShmBarrier)(sqlite3_file*);
  1181. int (*xShmUnmap)(sqlite3_file*, int deleteFlag);
  1182. /* Methods above are valid for version 2 */
  1183. /* Additional methods may be added in future releases */
  1184. };
  1185. /*
  1186. ** CAPI3REF: Standard File Control Opcodes
  1187. **
  1188. ** These integer constants are opcodes for the xFileControl method
  1189. ** of the [sqlite3_io_methods] object and for the [sqlite3_file_control()]
  1190. ** interface.
  1191. **
  1192. ** The [SQLITE_FCNTL_LOCKSTATE] opcode is used for debugging. This
  1193. ** opcode causes the xFileControl method to write the current state of
  1194. ** the lock (one of [SQLITE_LOCK_NONE], [SQLITE_LOCK_SHARED],
  1195. ** [SQLITE_LOCK_RESERVED], [SQLITE_LOCK_PENDING], or [SQLITE_LOCK_EXCLUSIVE])
  1196. ** into an integer that the pArg argument points to. This capability
  1197. ** is used during testing and only needs to be supported when SQLITE_TEST
  1198. ** is defined.
  1199. **
  1200. ** The [SQLITE_FCNTL_SIZE_HINT] opcode is used by SQLite to give the VFS
  1201. ** layer a hint of how large the database file will grow to be during the
  1202. ** current transaction. This hint is not guaranteed to be accurate but it
  1203. ** is often close. The underlying VFS might choose to preallocate database
  1204. ** file space based on this hint in order to help writes to the database
  1205. ** file run faster.
  1206. **
  1207. ** The [SQLITE_FCNTL_CHUNK_SIZE] opcode is used to request that the VFS
  1208. ** extends and truncates the database file in chunks of a size specified
  1209. ** by the user. The fourth argument to [sqlite3_file_control()] should
  1210. ** point to an integer (type int) containing the new chunk-size to use
  1211. ** for the nominated database. Allocating database file space in large
  1212. ** chunks (say 1MB at a time), may reduce file-system fragmentation and
  1213. ** improve performance on some systems.
  1214. **
  1215. ** The [SQLITE_FCNTL_FILE_POINTER] opcode is used to obtain a pointer
  1216. ** to the [sqlite3_file] object associated with a particular database
  1217. ** connection. See the [sqlite3_file_control()] documentation for
  1218. ** additional information.
  1219. **
  1220. ** ^(The [SQLITE_FCNTL_SYNC_OMITTED] opcode is generated internally by
  1221. ** SQLite and sent to all VFSes in place of a call to the xSync method
  1222. ** when the database connection has [PRAGMA synchronous] set to OFF.)^
  1223. ** Some specialized VFSes need this signal in order to operate correctly
  1224. ** when [PRAGMA synchronous | PRAGMA synchronous=OFF] is set, but most
  1225. ** VFSes do not need this signal and should silently ignore this opcode.
  1226. ** Applications should not call [sqlite3_file_control()] with this
  1227. ** opcode as doing so may disrupt the operation of the specialized VFSes
  1228. ** that do require it.
  1229. **
  1230. ** ^The [SQLITE_FCNTL_WIN32_AV_RETRY] opcode is used to configure automatic
  1231. ** retry counts and intervals for certain disk I/O operations for the
  1232. ** windows [VFS] in order to provide robustness in the presence of
  1233. ** anti-virus programs. By default, the windows VFS will retry file read,
  1234. ** file write, and file delete operations up to 10 times, with a delay
  1235. ** of 25 milliseconds before the first retry and with the delay increasing
  1236. ** by an additional 25 milliseconds with each subsequent retry. This
  1237. ** opcode allows these two values (10 retries and 25 milliseconds of delay)
  1238. ** to be adjusted. The values are changed for all database connections
  1239. ** within the same process. The argument is a pointer to an array of two
  1240. ** integers where the first integer i the new retry count and the second
  1241. ** integer is the delay. If either integer is negative, then the setting
  1242. ** is not changed but instead the prior value of that setting is written
  1243. ** into the array entry, allowing the current retry settings to be
  1244. ** interrogated. The zDbName parameter is ignored.
  1245. **
  1246. ** ^The [SQLITE_FCNTL_PERSIST_WAL] opcode is used to set or query the
  1247. ** persistent [WAL | Write AHead Log] setting. By default, the auxiliary
  1248. ** write ahead log and shared memory files used for transaction control
  1249. ** are automatically deleted when the latest connection to the database
  1250. ** closes. Setting persistent WAL mode causes those files to persist after
  1251. ** close. Persisting the files is useful when other processes that do not
  1252. ** have write permission on the directory containing the database file want
  1253. ** to read the database file, as the WAL and shared memory files must exist
  1254. ** in order for the database to be readable. The fourth parameter to
  1255. ** [sqlite3_file_control()] for this opcode should be a pointer to an integer.
  1256. ** That integer is 0 to disable persistent WAL mode or 1 to enable persistent
  1257. ** WAL mode. If the integer is -1, then it is overwritten with the current
  1258. ** WAL persistence setting.
  1259. **
  1260. ** ^The [SQLITE_FCNTL_POWERSAFE_OVERWRITE] opcode is used to set or query the
  1261. ** persistent "powersafe-overwrite" or "PSOW" setting. The PSOW setting
  1262. ** determines the [SQLITE_IOCAP_POWERSAFE_OVERWRITE] bit of the
  1263. ** xDeviceCharacteristics methods. The fourth parameter to
  1264. ** [sqlite3_file_control()] for this opcode should be a pointer to an integer.
  1265. ** That integer is 0 to disable zero-damage mode or 1 to enable zero-damage
  1266. ** mode. If the integer is -1, then it is overwritten with the current
  1267. ** zero-damage mode setting.
  1268. **
  1269. ** ^The [SQLITE_FCNTL_OVERWRITE] opcode is invoked by SQLite after opening
  1270. ** a write transaction to indicate that, unless it is rolled back for some
  1271. ** reason, the entire database file will be overwritten by the current
  1272. ** transaction. This is used by VACUUM operations.
  1273. **
  1274. ** ^The [SQLITE_FCNTL_VFSNAME] opcode can be used to obtain the names of
  1275. ** all [VFSes] in the VFS stack. The names are of all VFS shims and the
  1276. ** final bottom-level VFS are written into memory obtained from
  1277. ** [sqlite3_malloc()] and the result is stored in the char* variable
  1278. ** that the fourth parameter of [sqlite3_file_control()] points to.
  1279. ** The caller is responsible for freeing the memory when done. As with
  1280. ** all file-control actions, there is no guarantee that this will actually
  1281. ** do anything. Callers should initialize the char* variable to a NULL
  1282. ** pointer in case this file-control is not implemented. This file-control
  1283. ** is intended for diagnostic use only.
  1284. */
  1285. #define SQLITE_FCNTL_LOCKSTATE 1
  1286. #define SQLITE_GET_LOCKPROXYFILE 2
  1287. #define SQLITE_SET_LOCKPROXYFILE 3
  1288. #define SQLITE_LAST_ERRNO 4
  1289. #define SQLITE_FCNTL_SIZE_HINT 5
  1290. #define SQLITE_FCNTL_CHUNK_SIZE 6
  1291. #define SQLITE_FCNTL_FILE_POINTER 7
  1292. #define SQLITE_FCNTL_SYNC_OMITTED 8
  1293. #define SQLITE_FCNTL_WIN32_AV_RETRY 9
  1294. #define SQLITE_FCNTL_PERSIST_WAL 10
  1295. #define SQLITE_FCNTL_OVERWRITE 11
  1296. #define SQLITE_FCNTL_VFSNAME 12
  1297. #define SQLITE_FCNTL_POWERSAFE_OVERWRITE 13
  1298. /*
  1299. ** CAPI3REF: Mutex Handle
  1300. **
  1301. ** The mutex module within SQLite defines [sqlite3_mutex] to be an
  1302. ** abstract type for a mutex object. The SQLite core never looks
  1303. ** at the internal representation of an [sqlite3_mutex]. It only
  1304. ** deals with pointers to the [sqlite3_mutex] object.
  1305. **
  1306. ** Mutexes are created using [sqlite3_mutex_alloc()].
  1307. */
  1308. typedef struct sqlite3_mutex sqlite3_mutex;
  1309. /*
  1310. ** CAPI3REF: OS Interface Object
  1311. **
  1312. ** An instance of the sqlite3_vfs object defines the interface between
  1313. ** the SQLite core and the underlying operating system. The "vfs"
  1314. ** in the name of the object stands for "virtual file system". See
  1315. ** the [VFS | VFS documentation] for further information.
  1316. **
  1317. ** The value of the iVersion field is initially 1 but may be larger in
  1318. ** future versions of SQLite. Additional fields may be appended to this
  1319. ** object when the iVersion value is increased. Note that the structure
  1320. ** of the sqlite3_vfs object changes in the transaction between
  1321. ** SQLite version 3.5.9 and 3.6.0 and yet the iVersion field was not
  1322. ** modified.
  1323. **
  1324. ** The szOsFile field is the size of the subclassed [sqlite3_file]
  1325. ** structure used by this VFS. mxPathname is the maximum length of
  1326. ** a pathname in this VFS.
  1327. **
  1328. ** Registered sqlite3_vfs objects are kept on a linked list formed by
  1329. ** the pNext pointer. The [sqlite3_vfs_register()]
  1330. ** and [sqlite3_vfs_unregister()] interfaces manage this list
  1331. ** in a thread-safe way. The [sqlite3_vfs_find()] interface
  1332. ** searches the list. Neither the application code nor the VFS
  1333. ** implementation should use the pNext pointer.
  1334. **
  1335. ** The pNext field is the only field in the sqlite3_vfs
  1336. ** structure that SQLite will ever modify. SQLite will only access
  1337. ** or modify this field while holding a particular static mutex.
  1338. ** The application should never modify anything within the sqlite3_vfs
  1339. ** object once the object has been registered.
  1340. **
  1341. ** The zName field holds the name of the VFS module. The name must
  1342. ** be unique across all VFS modules.
  1343. **
  1344. ** [[sqlite3_vfs.xOpen]]
  1345. ** ^SQLite guarantees that the zFilename parameter to xOpen
  1346. ** is either a NULL pointer or string obtained
  1347. ** from xFullPathname() with an optional suffix added.
  1348. ** ^If a suffix is added to the zFilename parameter, it will
  1349. ** consist of a single "-" character followed by no more than
  1350. ** 11 alphanumeric and/or "-" characters.
  1351. ** ^SQLite further guarantees that
  1352. ** the string will be valid and unchanged until xClose() is
  1353. ** called. Because of the previous sentence,
  1354. ** the [sqlite3_file] can safely store a pointer to the
  1355. ** filename if it needs to remember the filename for some reason.
  1356. ** If the zFilename parameter to xOpen is a NULL pointer then xOpen
  1357. ** must invent its own temporary name for the file. ^Whenever the
  1358. ** xFilename parameter is NULL it will also be the case that the
  1359. ** flags parameter will include [SQLITE_OPEN_DELETEONCLOSE].
  1360. **
  1361. ** The flags argument to xOpen() includes all bits set in
  1362. ** the flags argument to [sqlite3_open_v2()]. Or if [sqlite3_open()]
  1363. ** or [sqlite3_open16()] is used, then flags includes at least
  1364. ** [SQLITE_OPEN_READWRITE] | [SQLITE_OPEN_CREATE].
  1365. ** If xOpen() opens a file read-only then it sets *pOutFlags to
  1366. ** include [SQLITE_OPEN_READONLY]. Other bits in *pOutFlags may be set.
  1367. **
  1368. ** ^(SQLite will also add one of the following flags to the xOpen()
  1369. ** call, depending on the object being opened:
  1370. **
  1371. ** <ul>
  1372. ** <li> [SQLITE_OPEN_MAIN_DB]
  1373. ** <li> [SQLITE_OPEN_MAIN_JOURNAL]
  1374. ** <li> [SQLITE_OPEN_TEMP_DB]
  1375. ** <li> [SQLITE_OPEN_TEMP_JOURNAL]
  1376. ** <li> [SQLITE_OPEN_TRANSIENT_DB]
  1377. ** <li> [SQLITE_OPEN_SUBJOURNAL]
  1378. ** <li> [SQLITE_OPEN_MASTER_JOURNAL]
  1379. ** <li> [SQLITE_OPEN_WAL]
  1380. ** </ul>)^
  1381. **
  1382. ** The file I/O implementation can use the object type flags to
  1383. ** change the way it deals with files. For example, an application
  1384. ** that does not care about crash recovery or rollback might make
  1385. ** the open of a journal file a no-op. Writes to this journal would
  1386. ** also be no-ops, and any attempt to read the journal would return
  1387. ** SQLITE_IOERR. Or the implementation might recognize that a database
  1388. ** file will be doing page-aligned sector reads and writes in a random
  1389. ** order and set up its I/O subsystem accordingly.
  1390. **
  1391. ** SQLite might also add one of the following flags to the xOpen method:
  1392. **
  1393. ** <ul>
  1394. ** <li> [SQLITE_OPEN_DELETEONCLOSE]
  1395. ** <li> [SQLITE_OPEN_EXCLUSIVE]
  1396. ** </ul>
  1397. **
  1398. ** The [SQLITE_OPEN_DELETEONCLOSE] flag means the file should be
  1399. ** deleted when it is closed. ^The [SQLITE_OPEN_DELETEONCLOSE]
  1400. ** will be set for TEMP databases and their journals, transient
  1401. ** databases, and subjournals.
  1402. **
  1403. ** ^The [SQLITE_OPEN_EXCLUSIVE] flag is always used in conjunction
  1404. ** with the [SQLITE_OPEN_CREATE] flag, which are both directly
  1405. ** analogous to the O_EXCL and O_CREAT flags of the POSIX open()
  1406. ** API. The SQLITE_OPEN_EXCLUSIVE flag, when paired with the
  1407. ** SQLITE_OPEN_CREATE, is used to indicate that file should always
  1408. ** be created, and that it is an error if it already exists.
  1409. ** It is <i>not</i> used to indicate the file should be opened
  1410. ** for exclusive access.
  1411. **
  1412. ** ^At least szOsFile bytes of memory are allocated by SQLite
  1413. ** to hold the [sqlite3_file] structure passed as the third
  1414. ** argument to xOpen. The xOpen method does not have to
  1415. ** allocate the structure; it should just fill it in. Note that
  1416. ** the xOpen method must set the sqlite3_file.pMethods to either
  1417. ** a valid [sqlite3_io_methods] object or to NULL. xOpen must do
  1418. ** this even if the open fails. SQLite expects that the sqlite3_file.pMethods
  1419. ** element will be valid after xOpen returns regardless of the success
  1420. ** or failure of the xOpen call.
  1421. **
  1422. ** [[sqlite3_vfs.xAccess]]
  1423. ** ^The flags argument to xAccess() may be [SQLITE_ACCESS_EXISTS]
  1424. ** to test for the existence of a file, or [SQLITE_ACCESS_READWRITE] to
  1425. ** test whether a file is readable and writable, or [SQLITE_ACCESS_READ]
  1426. ** to test whether a file is at least readable. The file can be a
  1427. ** directory.
  1428. **
  1429. ** ^SQLite will always allocate at least mxPathname+1 bytes for the
  1430. ** output buffer xFullPathname. The exact size of the output buffer
  1431. ** is also passed as a parameter to both methods. If the output buffer
  1432. ** is not large enough, [SQLITE_CANTOPEN] should be returned. Since this is
  1433. ** handled as a fatal error by SQLite, vfs implementations should endeavor
  1434. ** to prevent this by setting mxPathname to a sufficiently large value.
  1435. **
  1436. ** The xRandomness(), xSleep(), xCurrentTime(), and xCurrentTimeInt64()
  1437. ** interfaces are not strictly a part of the filesystem, but they are
  1438. ** included in the VFS structure for completeness.
  1439. ** The xRandomness() function attempts to return nBytes bytes
  1440. ** of good-quality randomness into zOut. The return value is
  1441. ** the actual number of bytes of randomness obtained.
  1442. ** The xSleep() method causes the calling thread to sleep for at
  1443. ** least the number of microseconds given. ^The xCurrentTime()
  1444. ** method returns a Julian Day Number for the current date and time as
  1445. ** a floating point value.
  1446. ** ^The xCurrentTimeInt64() method returns, as an integer, the Julian
  1447. ** Day Number multiplied by 86400000 (the number of milliseconds in
  1448. ** a 24-hour day).
  1449. ** ^SQLite will use the xCurrentTimeInt64() method to get the current
  1450. ** date and time if that method is available (if iVersion is 2 or
  1451. ** greater and the function pointer is not NULL) and will fall back
  1452. ** to xCurrentTime() if xCurrentTimeInt64() is unavailable.
  1453. **
  1454. ** ^The xSetSystemCall(), xGetSystemCall(), and xNestSystemCall() interfaces
  1455. ** are not used by the SQLite core. These optional interfaces are provided
  1456. ** by some VFSes to facilitate testing of the VFS code. By overriding
  1457. ** system calls with functions under its control, a test program can
  1458. ** simulate faults and error conditions that would otherwise be difficult
  1459. ** or impossible to induce. The set of system calls that can be overridden
  1460. ** varies from one VFS to another, and from one version of the same VFS to the
  1461. ** next. Applications that use these interfaces must be prepared for any
  1462. ** or all of these interfaces to be NULL or for their behavior to change
  1463. ** from one release to the next. Applications must not attempt to access
  1464. ** any of these methods if the iVersion of the VFS is less than 3.
  1465. */
  1466. typedef struct sqlite3_vfs sqlite3_vfs;
  1467. typedef void (*sqlite3_syscall_ptr)(void);
  1468. struct sqlite3_vfs {
  1469. int iVersion; /* Structure version number (currently 3) */
  1470. int szOsFile; /* Size of subclassed sqlite3_file */
  1471. int mxPathname; /* Maximum file pathname length */
  1472. sqlite3_vfs *pNext; /* Next registered VFS */
  1473. const char *zName; /* Name of this virtual file system */
  1474. void *pAppData; /* Pointer to application-specific data */
  1475. int (*xOpen)(sqlite3_vfs*, const char *zName, sqlite3_file*,
  1476. int flags, int *pOutFlags);
  1477. int (*xDelete)(sqlite3_vfs*, const char *zName, int syncDir);
  1478. int (*xAccess)(sqlite3_vfs*, const char *zName, int flags, int *pResOut);
  1479. int (*xFullPathname)(sqlite3_vfs*, const char *zName, int nOut, char *zOut);
  1480. void *(*xDlOpen)(sqlite3_vfs*, const char *zFilename);
  1481. void (*xDlError)(sqlite3_vfs*, int nByte, char *zErrMsg);
  1482. void (*(*xDlSym)(sqlite3_vfs*,void*, const char *zSymbol))(void);
  1483. void (*xDlClose)(sqlite3_vfs*, void*);
  1484. int (*xRandomness)(sqlite3_vfs*, int nByte, char *zOut);
  1485. int (*xSleep)(sqlite3_vfs*, int microseconds);
  1486. int (*xCurrentTime)(sqlite3_vfs*, double*);
  1487. int (*xGetLastError)(sqlite3_vfs*, int, char *);
  1488. /*
  1489. ** The methods above are in version 1 of the sqlite_vfs object
  1490. ** definition. Those that follow are added in version 2 or later
  1491. */
  1492. int (*xCurrentTimeInt64)(sqlite3_vfs*, sqlite3_int64*);
  1493. /*
  1494. ** The methods above are in versions 1 and 2 of the sqlite_vfs object.
  1495. ** Those below are for version 3 and greater.
  1496. */
  1497. int (*xSetSystemCall)(sqlite3_vfs*, const char *zName, sqlite3_syscall_ptr);
  1498. sqlite3_syscall_ptr (*xGetSystemCall)(sqlite3_vfs*, const char *zName);
  1499. const char *(*xNextSystemCall)(sqlite3_vfs*, const char *zName);
  1500. /*
  1501. ** The methods above are in versions 1 through 3 of the sqlite_vfs object.
  1502. ** New fields may be appended in figure versions. The iVersion
  1503. ** value will increment whenever this happens.
  1504. */
  1505. };
  1506. /*
  1507. ** CAPI3REF: Flags for the xAccess VFS method
  1508. **
  1509. ** These integer constants can be used as the third parameter to
  1510. ** the xAccess method of an [sqlite3_vfs] object. They determine
  1511. ** what kind of permissions the xAccess method is looking for.
  1512. ** With SQLITE_ACCESS_EXISTS, the xAccess method
  1513. ** simply checks whether the file exists.
  1514. ** With SQLITE_ACCESS_READWRITE, the xAccess method
  1515. ** checks whether the named directory is both readable and writable
  1516. ** (in other words, if files can be added, removed, and renamed within
  1517. ** the directory).
  1518. ** The SQLITE_ACCESS_READWRITE constant is currently used only by the
  1519. ** [temp_store_directory pragma], though this could change in a future
  1520. ** release of SQLite.
  1521. ** With SQLITE_ACCESS_READ, the xAccess method
  1522. ** checks whether the file is readable. The SQLITE_ACCESS_READ constant is
  1523. ** currently unused, though it might be used in a future release of
  1524. ** SQLite.
  1525. */
  1526. #define SQLITE_ACCESS_EXISTS 0
  1527. #define SQLITE_ACCESS_READWRITE 1 /* Used by PRAGMA temp_store_directory */
  1528. #define SQLITE_ACCESS_READ 2 /* Unused */
  1529. /*
  1530. ** CAPI3REF: Flags for the xShmLock VFS method
  1531. **
  1532. ** These integer constants define the various locking operations
  1533. ** allowed by the xShmLock method of [sqlite3_io_methods]. The
  1534. ** following are the only legal combinations of flags to the
  1535. ** xShmLock method:
  1536. **
  1537. ** <ul>
  1538. ** <li> SQLITE_SHM_LOCK | SQLITE_SHM_SHARED
  1539. ** <li> SQLITE_SHM_LOCK | SQLITE_SHM_EXCLUSIVE
  1540. ** <li> SQLITE_SHM_UNLOCK | SQLITE_SHM_SHARED
  1541. ** <li> SQLITE_SHM_UNLOCK | SQLITE_SHM_EXCLUSIVE
  1542. ** </ul>
  1543. **
  1544. ** When unlocking, the same SHARED or EXCLUSIVE flag must be supplied as
  1545. ** was given no the corresponding lock.
  1546. **
  1547. ** The xShmLock method can transition between unlocked and SHARED or
  1548. ** between unlocked and EXCLUSIVE. It cannot transition between SHARED
  1549. ** and EXCLUSIVE.
  1550. */
  1551. #define SQLITE_SHM_UNLOCK 1
  1552. #define SQLITE_SHM_LOCK 2
  1553. #define SQLITE_SHM_SHARED 4
  1554. #define SQLITE_SHM_EXCLUSIVE 8
  1555. /*
  1556. ** CAPI3REF: Maximum xShmLock index
  1557. **
  1558. ** The xShmLock method on [sqlite3_io_methods] may use values
  1559. ** between 0 and this upper bound as its "offset" argument.
  1560. ** The SQLite core will never attempt to acquire or release a
  1561. ** lock outside of this range
  1562. */
  1563. #define SQLITE_SHM_NLOCK 8
  1564. /*
  1565. ** CAPI3REF: Initialize The SQLite Library
  1566. **
  1567. ** ^The sqlite3_initialize() routine initializes the
  1568. ** SQLite library. ^The sqlite3_shutdown() routine
  1569. ** deallocates any resources that were allocated by sqlite3_initialize().
  1570. ** These routines are designed to aid in process initialization and
  1571. ** shutdown on embedded systems. Workstation applications using
  1572. ** SQLite normally do not need to invoke either of these routines.
  1573. **
  1574. ** A call to sqlite3_initialize() is an "effective" call if it is
  1575. ** the first time sqlite3_initialize() is invoked during the lifetime of
  1576. ** the process, or if it is the first time sqlite3_initialize() is invoked
  1577. ** following a call to sqlite3_shutdown(). ^(Only an effective call
  1578. ** of sqlite3_initialize() does any initialization. All other calls
  1579. ** are harmless no-ops.)^
  1580. **
  1581. ** A call to sqlite3_shutdown() is an "effective" call if it is the first
  1582. ** call to sqlite3_shutdown() since the last sqlite3_initialize(). ^(Only
  1583. ** an effective call to sqlite3_shutdown() does any deinitialization.
  1584. ** All other valid calls to sqlite3_shutdown() are harmless no-ops.)^
  1585. **
  1586. ** The sqlite3_initialize() interface is threadsafe, but sqlite3_shutdown()
  1587. ** is not. The sqlite3_shutdown() interface must only be called from a
  1588. ** single thread. All open [database connections] must be closed and all
  1589. ** other SQLite resources must be deallocated prior to invoking
  1590. ** sqlite3_shutdown().
  1591. **
  1592. ** Among other things, ^sqlite3_initialize() will invoke
  1593. ** sqlite3_os_init(). Similarly, ^sqlite3_shutdown()
  1594. ** will invoke sqlite3_os_end().
  1595. **
  1596. ** ^The sqlite3_initialize() routine returns [SQLITE_OK] on success.
  1597. ** ^If for some reason, sqlite3_initialize() is unable to initialize
  1598. ** the library (perhaps it is unable to allocate a needed resource such
  1599. ** as a mutex) it returns an [error code] other than [SQLITE_OK].
  1600. **
  1601. ** ^The sqlite3_initialize() routine is called internally by many other
  1602. ** SQLite interfaces so that an application usually does not need to
  1603. ** invoke sqlite3_initialize() directly. For example, [sqlite3_open()]
  1604. ** calls sqlite3_initialize() so the SQLite library will be automatically
  1605. ** initialized when [sqlite3_open()] is called if it has not be initialized
  1606. ** already. ^However, if SQLite is compiled with the [SQLITE_OMIT_AUTOINIT]
  1607. ** compile-time option, then the automatic calls to sqlite3_initialize()
  1608. ** are omitted and the application must call sqlite3_initialize() directly
  1609. ** prior to using any other SQLite interface. For maximum portability,
  1610. ** it is recommended that applications always invoke sqlite3_initialize()
  1611. ** directly prior to using any other SQLite interface. Future releases
  1612. ** of SQLite may require this. In other words, the behavior exhibited
  1613. ** when SQLite is compiled with [SQLITE_OMIT_AUTOINIT] might become the
  1614. ** default behavior in some future release of SQLite.
  1615. **
  1616. ** The sqlite3_os_init() routine does operating-system specific
  1617. ** initialization of the SQLite library. The sqlite3_os_end()
  1618. ** routine undoes the effect of sqlite3_os_init(). Typical tasks
  1619. ** performed by these routines include allocation or deallocation
  1620. ** of static resources, initialization of global variables,
  1621. ** setting up a default [sqlite3_vfs] module, or setting up
  1622. ** a default configuration using [sqlite3_config()].
  1623. **
  1624. ** The application should never invoke either sqlite3_os_init()
  1625. ** or sqlite3_os_end() directly. The application should only invoke
  1626. ** sqlite3_initialize() and sqlite3_shutdown(). The sqlite3_os_init()
  1627. ** interface is called automatically by sqlite3_initialize() and
  1628. ** sqlite3_os_end() is called by sqlite3_shutdown(). Appropriate
  1629. ** implementations for sqlite3_os_init() and sqlite3_os_end()
  1630. ** are built into SQLite when it is compiled for Unix, Windows, or OS/2.
  1631. ** When [custom builds | built for other platforms]
  1632. ** (using the [SQLITE_OS_OTHER=1] compile-time
  1633. ** option) the application must supply a suitable implementation for
  1634. ** sqlite3_os_init() and sqlite3_os_end(). An application-supplied
  1635. ** implementation of sqlite3_os_init() or sqlite3_os_end()
  1636. ** must return [SQLITE_OK] on success and some other [error code] upon
  1637. ** failure.
  1638. */
  1639. SQLITE_API int sqlite3_initialize(void);
  1640. SQLITE_API int sqlite3_shutdown(void);
  1641. SQLITE_API int sqlite3_os_init(void);
  1642. SQLITE_API int sqlite3_os_end(void);
  1643. /*
  1644. ** CAPI3REF: Configuring The SQLite Library
  1645. **
  1646. ** The sqlite3_config() interface is used to make global configuration
  1647. ** changes to SQLite in order to tune SQLite to the specific needs of
  1648. ** the application. The default configuration is recommended for most
  1649. ** applications and so this routine is usually not necessary. It is
  1650. ** provided to support rare applications with unusual needs.
  1651. **
  1652. ** The sqlite3_config() interface is not threadsafe. The application
  1653. ** must insure that no other SQLite interfaces are invoked by other
  1654. ** threads while sqlite3_config() is running. Furthermore, sqlite3_config()
  1655. ** may only be invoked prior to library initialization using
  1656. ** [sqlite3_initialize()] or after shutdown by [sqlite3_shutdown()].
  1657. ** ^If sqlite3_config() is called after [sqlite3_initialize()] and before
  1658. ** [sqlite3_shutdown()] then it will return SQLITE_MISUSE.
  1659. ** Note, however, that ^sqlite3_config() can be called as part of the
  1660. ** implementation of an application-defined [sqlite3_os_init()].
  1661. **
  1662. ** The first argument to sqlite3_config() is an integer
  1663. ** [configuration option] that determines
  1664. ** what property of SQLite is to be configured. Subsequent arguments
  1665. ** vary depending on the [configuration option]
  1666. ** in the first argument.
  1667. **
  1668. ** ^When a configuration option is set, sqlite3_config() returns [SQLITE_OK].
  1669. ** ^If the option is unknown or SQLite is unable to set the option
  1670. ** then this routine returns a non-zero [error code].
  1671. */
  1672. SQLITE_API int sqlite3_config(int, ...);
  1673. /*
  1674. ** CAPI3REF: Configure database connections
  1675. **
  1676. ** The sqlite3_db_config() interface is used to make configuration
  1677. ** changes to a [database connection]. The interface is similar to
  1678. ** [sqlite3_config()] except that the changes apply to a single
  1679. ** [database connection] (specified in the first argument).
  1680. **
  1681. ** The second argument to sqlite3_db_config(D,V,...) is the
  1682. ** [SQLITE_DBCONFIG_LOOKASIDE | configuration verb] - an integer code
  1683. ** that indicates what aspect of the [database connection] is being configured.
  1684. ** Subsequent arguments vary depending on the configuration verb.
  1685. **
  1686. ** ^Calls to sqlite3_db_config() return SQLITE_OK if and only if
  1687. ** the call is considered successful.
  1688. */
  1689. SQLITE_API int sqlite3_db_config(sqlite3*, int op, ...);
  1690. /*
  1691. ** CAPI3REF: Memory Allocation Routines
  1692. **
  1693. ** An instance of this object defines the interface between SQLite
  1694. ** and low-level memory allocation routines.
  1695. **
  1696. ** This object is used in only one place in the SQLite interface.
  1697. ** A pointer to an instance of this object is the argument to
  1698. ** [sqlite3_config()] when the configuration option is
  1699. ** [SQLITE_CONFIG_MALLOC] or [SQLITE_CONFIG_GETMALLOC].
  1700. ** By creating an instance of this object
  1701. ** and passing it to [sqlite3_config]([SQLITE_CONFIG_MALLOC])
  1702. ** during configuration, an application can specify an alternative
  1703. ** memory allocation subsystem for SQLite to use for all of its
  1704. ** dynamic memory needs.
  1705. **
  1706. ** Note that SQLite comes with several [built-in memory allocators]
  1707. ** that are perfectly adequate for the overwhelming majority of applications
  1708. ** and that this object is only useful to a tiny minority of applications
  1709. ** with specialized memory allocation requirements. This object is
  1710. ** also used during testing of SQLite in order to specify an alternative
  1711. ** memory allocator that simulates memory out-of-memory conditions in
  1712. ** order to verify that SQLite recovers gracefully from such
  1713. ** conditions.
  1714. **
  1715. ** The xMalloc, xRealloc, and xFree methods must work like the
  1716. ** malloc(), realloc() and free() functions from the standard C library.
  1717. ** ^SQLite guarantees that the second argument to
  1718. ** xRealloc is always a value returned by a prior call to xRoundup.
  1719. **
  1720. ** xSize should return the allocated size of a memory allocation
  1721. ** previously obtained from xMalloc or xRealloc. The allocated size
  1722. ** is always at least as big as the requested size but may be larger.
  1723. **
  1724. ** The xRoundup method returns what would be the allocated size of
  1725. ** a memory allocation given a particular requested size. Most memory
  1726. ** allocators round up memory allocations at least to the next multiple
  1727. ** of 8. Some allocators round up to a larger multiple or to a power of 2.
  1728. ** Every memory allocation request coming in through [sqlite3_malloc()]
  1729. ** or [sqlite3_realloc()] first calls xRoundup. If xRoundup returns 0,
  1730. ** that causes the corresponding memory allocation to fail.
  1731. **
  1732. ** The xInit method initializes the memory allocator. (For example,
  1733. ** it might allocate any require mutexes or initialize internal data
  1734. ** structures. The xShutdown method is invoked (indirectly) by
  1735. ** [sqlite3_shutdown()] and should deallocate any resources acquired
  1736. ** by xInit. The pAppData pointer is used as the only parameter to
  1737. ** xInit and xShutdown.
  1738. **
  1739. ** SQLite holds the [SQLITE_MUTEX_STATIC_MASTER] mutex when it invokes
  1740. ** the xInit method, so the xInit method need not be threadsafe. The
  1741. ** xShutdown method is only called from [sqlite3_shutdown()] so it does
  1742. ** not need to be threadsafe either. For all other methods, SQLite
  1743. ** holds the [SQLITE_MUTEX_STATIC_MEM] mutex as long as the
  1744. ** [SQLITE_CONFIG_MEMSTATUS] configuration option is turned on (which
  1745. ** it is by default) and so the methods are automatically serialized.
  1746. ** However, if [SQLITE_CONFIG_MEMSTATUS] is disabled, then the other
  1747. ** methods must be threadsafe or else make their own arrangements for
  1748. ** serialization.
  1749. **
  1750. ** SQLite will never invoke xInit() more than once without an intervening
  1751. ** call to xShutdown().
  1752. */
  1753. typedef struct sqlite3_mem_methods sqlite3_mem_methods;
  1754. struct sqlite3_mem_methods {
  1755. void *(*xMalloc)(int); /* Memory allocation function */
  1756. void (*xFree)(void*); /* Free a prior allocation */
  1757. void *(*xRealloc)(void*,int); /* Resize an allocation */
  1758. int (*xSize)(void*); /* Return the size of an allocation */
  1759. int (*xRoundup)(int); /* Round up request size to allocation size */
  1760. int (*xInit)(void*); /* Initialize the memory allocator */
  1761. void (*xShutdown)(void*); /* Deinitialize the memory allocator */
  1762. void *pAppData; /* Argument to xInit() and xShutdown() */
  1763. };
  1764. /*
  1765. ** CAPI3REF: Configuration Options
  1766. ** KEYWORDS: {configuration option}
  1767. **
  1768. ** These constants are the available integer configuration options that
  1769. ** can be passed as the first argument to the [sqlite3_config()] interface.
  1770. **
  1771. ** New configuration options may be added in future releases of SQLite.
  1772. ** Existing configuration options might be discontinued. Applications
  1773. ** should check the return code from [sqlite3_config()] to make sure that
  1774. ** the call worked. The [sqlite3_config()] interface will return a
  1775. ** non-zero [error code] if a discontinued or unsupported configuration option
  1776. ** is invoked.
  1777. **
  1778. ** <dl>
  1779. ** [[SQLITE_CONFIG_SINGLETHREAD]] <dt>SQLITE_CONFIG_SINGLETHREAD</dt>
  1780. ** <dd>There are no arguments to this option. ^This option sets the
  1781. ** [threading mode] to Single-thread. In other words, it disables
  1782. ** all mutexing and puts SQLite into a mode where it can only be used
  1783. ** by a single thread. ^If SQLite is compiled with
  1784. ** the [SQLITE_THREADSAFE | SQLITE_THREADSAFE=0] compile-time option then
  1785. ** it is not possible to change the [threading mode] from its default
  1786. ** value of Single-thread and so [sqlite3_config()] will return
  1787. ** [SQLITE_ERROR] if called with the SQLITE_CONFIG_SINGLETHREAD
  1788. ** configuration option.</dd>
  1789. **
  1790. ** [[SQLITE_CONFIG_MULTITHREAD]] <dt>SQLITE_CONFIG_MULTITHREAD</dt>
  1791. ** <dd>There are no arguments to this option. ^This option sets the
  1792. ** [threading mode] to Multi-thread. In other words, it disables
  1793. ** mutexing on [database connection] and [prepared statement] objects.
  1794. ** The application is responsible for serializing access to
  1795. ** [database connections] and [prepared statements]. But other mutexes
  1796. ** are enabled so that SQLite will be safe to use in a multi-threaded
  1797. ** environment as long as no two threads attempt to use the same
  1798. ** [database connection] at the same time. ^If SQLite is compiled with
  1799. ** the [SQLITE_THREADSAFE | SQLITE_THREADSAFE=0] compile-time option then
  1800. ** it is not possible to set the Multi-thread [threading mode] and
  1801. ** [sqlite3_config()] will return [SQLITE_ERROR] if called with the
  1802. ** SQLITE_CONFIG_MULTITHREAD configuration option.</dd>
  1803. **
  1804. ** [[SQLITE_CONFIG_SERIALIZED]] <dt>SQLITE_CONFIG_SERIALIZED</dt>
  1805. ** <dd>There are no arguments to this option. ^This option sets the
  1806. ** [threading mode] to Serialized. In other words, this option enables
  1807. ** all mutexes including the recursive
  1808. ** mutexes on [database connection] and [prepared statement] objects.
  1809. ** In this mode (which is the default when SQLite is compiled with
  1810. ** [SQLITE_THREADSAFE=1]) the SQLite library will itself serialize access
  1811. ** to [database connections] and [prepared statements] so that the
  1812. ** application is free to use the same [database connection] or the
  1813. ** same [prepared statement] in different threads at the same time.
  1814. ** ^If SQLite is compiled with
  1815. ** the [SQLITE_THREADSAFE | SQLITE_THREADSAFE=0] compile-time option then
  1816. ** it is not possible to set the Serialized [threading mode] and
  1817. ** [sqlite3_config()] will return [SQLITE_ERROR] if called with the
  1818. ** SQLITE_CONFIG_SERIALIZED configuration option.</dd>
  1819. **
  1820. ** [[SQLITE_CONFIG_MALLOC]] <dt>SQLITE_CONFIG_MALLOC</dt>
  1821. ** <dd> ^(This option takes a single argument which is a pointer to an
  1822. ** instance of the [sqlite3_mem_methods] structure. The argument specifies
  1823. ** alternative low-level memory allocation routines to be used in place of
  1824. ** the memory allocation routines built into SQLite.)^ ^SQLite makes
  1825. ** its own private copy of the content of the [sqlite3_mem_methods] structure
  1826. ** before the [sqlite3_config()] call returns.</dd>
  1827. **
  1828. ** [[SQLITE_CONFIG_GETMALLOC]] <dt>SQLITE_CONFIG_GETMALLOC</dt>
  1829. ** <dd> ^(This option takes a single argument which is a pointer to an
  1830. ** instance of the [sqlite3_mem_methods] structure. The [sqlite3_mem_methods]
  1831. ** structure is filled with the currently defined memory allocation routines.)^
  1832. ** This option can be used to overload the default memory allocation
  1833. ** routines with a wrapper that simulations memory allocation failure or
  1834. ** tracks memory usage, for example. </dd>
  1835. **
  1836. ** [[SQLITE_CONFIG_MEMSTATUS]] <dt>SQLITE_CONFIG_MEMSTATUS</dt>
  1837. ** <dd> ^This option takes single argument of type int, interpreted as a
  1838. ** boolean, which enables or disables the collection of memory allocation
  1839. ** statistics. ^(When memory allocation statistics are disabled, the
  1840. ** following SQLite interfaces become non-operational:
  1841. ** <ul>
  1842. ** <li> [sqlite3_memory_used()]
  1843. ** <li> [sqlite3_memory_highwater()]
  1844. ** <li> [sqlite3_soft_heap_limit64()]
  1845. ** <li> [sqlite3_status()]
  1846. ** </ul>)^
  1847. ** ^Memory allocation statistics are enabled by default unless SQLite is
  1848. ** compiled with [SQLITE_DEFAULT_MEMSTATUS]=0 in which case memory
  1849. ** allocation statistics are disabled by default.
  1850. ** </dd>
  1851. **
  1852. ** [[SQLITE_CONFIG_SCRATCH]] <dt>SQLITE_CONFIG_SCRATCH</dt>
  1853. ** <dd> ^This option specifies a static memory buffer that SQLite can use for
  1854. ** scratch memory. There are three arguments: A pointer an 8-byte
  1855. ** aligned memory buffer from which the scratch allocations will be
  1856. ** drawn, the size of each scratch allocation (sz),
  1857. ** and the maximum number of scratch allocations (N). The sz
  1858. ** argument must be a multiple of 16.
  1859. ** The first argument must be a pointer to an 8-byte aligned buffer
  1860. ** of at least sz*N bytes of memory.
  1861. ** ^SQLite will use no more than two scratch buffers per thread. So
  1862. ** N should be set to twice the expected maximum number of threads.
  1863. ** ^SQLite will never require a scratch buffer that is more than 6
  1864. ** times the database page size. ^If SQLite needs needs additional
  1865. ** scratch memory beyond what is provided by this configuration option, then
  1866. ** [sqlite3_malloc()] will be used to obtain the memory needed.</dd>
  1867. **
  1868. ** [[SQLITE_CONFIG_PAGECACHE]] <dt>SQLITE_CONFIG_PAGECACHE</dt>
  1869. ** <dd> ^This option specifies a static memory buffer that SQLite can use for
  1870. ** the database page cache with the default page cache implementation.
  1871. ** This configuration should not be used if an application-define page
  1872. ** cache implementation is loaded using the SQLITE_CONFIG_PCACHE2 option.
  1873. ** There are three arguments to this option: A pointer to 8-byte aligned
  1874. ** memory, the size of each page buffer (sz), and the number of pages (N).
  1875. ** The sz argument should be the size of the largest database page
  1876. ** (a power of two between 512 and 32768) plus a little extra for each
  1877. ** page header. ^The page header size is 20 to 40 bytes depending on
  1878. ** the host architecture. ^It is harmless, apart from the wasted memory,
  1879. ** to make sz a little too large. The first
  1880. ** argument should point to an allocation of at least sz*N bytes of memory.
  1881. ** ^SQLite will use the memory provided by the first argument to satisfy its
  1882. ** memory needs for the first N pages that it adds to cache. ^If additional
  1883. ** page cache memory is needed beyond what is provided by this option, then
  1884. ** SQLite goes to [sqlite3_malloc()] for the additional storage space.
  1885. ** The pointer in the first argument must
  1886. ** be aligned to an 8-byte boundary or subsequent behavior of SQLite
  1887. ** will be undefined.</dd>
  1888. **
  1889. ** [[SQLITE_CONFIG_HEAP]] <dt>SQLITE_CONFIG_HEAP</dt>
  1890. ** <dd> ^This option specifies a static memory buffer that SQLite will use
  1891. ** for all of its dynamic memory allocation needs beyond those provided
  1892. ** for by [SQLITE_CONFIG_SCRATCH] and [SQLITE_CONFIG_PAGECACHE].
  1893. ** There are three arguments: An 8-byte aligned pointer to the memory,
  1894. ** the number of bytes in the memory buffer, and the minimum allocation size.
  1895. ** ^If the first pointer (the memory pointer) is NULL, then SQLite reverts
  1896. ** to using its default memory allocator (the system malloc() implementation),
  1897. ** undoing any prior invocation of [SQLITE_CONFIG_MALLOC]. ^If the
  1898. ** memory pointer is not NULL and either [SQLITE_ENABLE_MEMSYS3] or
  1899. ** [SQLITE_ENABLE_MEMSYS5] are defined, then the alternative memory
  1900. ** allocator is engaged to handle all of SQLites memory allocation needs.
  1901. ** The first pointer (the memory pointer) must be aligned to an 8-byte
  1902. ** boundary or subsequent behavior of SQLite will be undefined.
  1903. ** The minimum allocation size is capped at 2**12. Reasonable values
  1904. ** for the minimum allocation size are 2**5 through 2**8.</dd>
  1905. **
  1906. ** [[SQLITE_CONFIG_MUTEX]] <dt>SQLITE_CONFIG_MUTEX</dt>
  1907. ** <dd> ^(This option takes a single argument which is a pointer to an
  1908. ** instance of the [sqlite3_mutex_methods] structure. The argument specifies
  1909. ** alternative low-level mutex routines to be used in place
  1910. ** the mutex routines built into SQLite.)^ ^SQLite makes a copy of the
  1911. ** content of the [sqlite3_mutex_methods] structure before the call to
  1912. ** [sqlite3_config()] returns. ^If SQLite is compiled with
  1913. ** the [SQLITE_THREADSAFE | SQLITE_THREADSAFE=0] compile-time option then
  1914. ** the entire mutexing subsystem is omitted from the build and hence calls to
  1915. ** [sqlite3_config()] with the SQLITE_CONFIG_MUTEX configuration option will
  1916. ** return [SQLITE_ERROR].</dd>
  1917. **
  1918. ** [[SQLITE_CONFIG_GETMUTEX]] <dt>SQLITE_CONFIG_GETMUTEX</dt>
  1919. ** <dd> ^(This option takes a single argument which is a pointer to an
  1920. ** instance of the [sqlite3_mutex_methods] structure. The
  1921. ** [sqlite3_mutex_methods]
  1922. ** structure is filled with the currently defined mutex routines.)^
  1923. ** This option can be used to overload the default mutex allocation
  1924. ** routines with a wrapper used to track mutex usage for performance
  1925. ** profiling or testing, for example. ^If SQLite is compiled with
  1926. ** the [SQLITE_THREADSAFE | SQLITE_THREADSAFE=0] compile-time option then
  1927. ** the entire mutexing subsystem is omitted from the build and hence calls to
  1928. ** [sqlite3_config()] with the SQLITE_CONFIG_GETMUTEX configuration option will
  1929. ** return [SQLITE_ERROR].</dd>
  1930. **
  1931. ** [[SQLITE_CONFIG_LOOKASIDE]] <dt>SQLITE_CONFIG_LOOKASIDE</dt>
  1932. ** <dd> ^(This option takes two arguments that determine the default
  1933. ** memory allocation for the lookaside memory allocator on each
  1934. ** [database connection]. The first argument is the
  1935. ** size of each lookaside buffer slot and the second is the number of
  1936. ** slots allocated to each database connection.)^ ^(This option sets the
  1937. ** <i>default</i> lookaside size. The [SQLITE_DBCONFIG_LOOKASIDE]
  1938. ** verb to [sqlite3_db_config()] can be used to change the lookaside
  1939. ** configuration on individual connections.)^ </dd>
  1940. **
  1941. ** [[SQLITE_CONFIG_PCACHE2]] <dt>SQLITE_CONFIG_PCACHE2</dt>
  1942. ** <dd> ^(This option takes a single argument which is a pointer to
  1943. ** an [sqlite3_pcache_methods2] object. This object specifies the interface
  1944. ** to a custom page cache implementation.)^ ^SQLite makes a copy of the
  1945. ** object and uses it for page cache memory allocations.</dd>
  1946. **
  1947. ** [[SQLITE_CONFIG_GETPCACHE2]] <dt>SQLITE_CONFIG_GETPCACHE2</dt>
  1948. ** <dd> ^(This option takes a single argument which is a pointer to an
  1949. ** [sqlite3_pcache_methods2] object. SQLite copies of the current
  1950. ** page cache implementation into that object.)^ </dd>
  1951. **
  1952. ** [[SQLITE_CONFIG_LOG]] <dt>SQLITE_CONFIG_LOG</dt>
  1953. ** <dd> ^The SQLITE_CONFIG_LOG option takes two arguments: a pointer to a
  1954. ** function with a call signature of void(*)(void*,int,const char*),
  1955. ** and a pointer to void. ^If the function pointer is not NULL, it is
  1956. ** invoked by [sqlite3_log()] to process each logging event. ^If the
  1957. ** function pointer is NULL, the [sqlite3_log()] interface becomes a no-op.
  1958. ** ^The void pointer that is the second argument to SQLITE_CONFIG_LOG is
  1959. ** passed through as the first parameter to the application-defined logger
  1960. ** function whenever that function is invoked. ^The second parameter to
  1961. ** the logger function is a copy of the first parameter to the corresponding
  1962. ** [sqlite3_log()] call and is intended to be a [result code] or an
  1963. ** [extended result code]. ^The third parameter passed to the logger is
  1964. ** log message after formatting via [sqlite3_snprintf()].
  1965. ** The SQLite logging interface is not reentrant; the logger function
  1966. ** supplied by the application must not invoke any SQLite interface.
  1967. ** In a multi-threaded application, the application-defined logger
  1968. ** function must be threadsafe. </dd>
  1969. **
  1970. ** [[SQLITE_CONFIG_URI]] <dt>SQLITE_CONFIG_URI
  1971. ** <dd> This option takes a single argument of type int. If non-zero, then
  1972. ** URI handling is globally enabled. If the parameter is zero, then URI handling
  1973. ** is globally disabled. If URI handling is globally enabled, all filenames
  1974. ** passed to [sqlite3_open()], [sqlite3_open_v2()], [sqlite3_open16()] or
  1975. ** specified as part of [ATTACH] commands are interpreted as URIs, regardless
  1976. ** of whether or not the [SQLITE_OPEN_URI] flag is set when the database
  1977. ** connection is opened. If it is globally disabled, filenames are
  1978. ** only interpreted as URIs if the SQLITE_OPEN_URI flag is set when the
  1979. ** database connection is opened. By default, URI handling is globally
  1980. ** disabled. The default value may be changed by compiling with the
  1981. ** [SQLITE_USE_URI] symbol defined.
  1982. **
  1983. ** [[SQLITE_CONFIG_PCACHE]] [[SQLITE_CONFIG_GETPCACHE]]
  1984. ** <dt>SQLITE_CONFIG_PCACHE and SQLITE_CONFNIG_GETPCACHE
  1985. ** <dd> These options are obsolete and should not be used by new code.
  1986. ** They are retained for backwards compatibility but are now no-ops.
  1987. ** </dl>
  1988. */
  1989. #define SQLITE_CONFIG_SINGLETHREAD 1 /* nil */
  1990. #define SQLITE_CONFIG_MULTITHREAD 2 /* nil */
  1991. #define SQLITE_CONFIG_SERIALIZED 3 /* nil */
  1992. #define SQLITE_CONFIG_MALLOC 4 /* sqlite3_mem_methods* */
  1993. #define SQLITE_CONFIG_GETMALLOC 5 /* sqlite3_mem_methods* */
  1994. #define SQLITE_CONFIG_SCRATCH 6 /* void*, int sz, int N */
  1995. #define SQLITE_CONFIG_PAGECACHE 7 /* void*, int sz, int N */
  1996. #define SQLITE_CONFIG_HEAP 8 /* void*, int nByte, int min */
  1997. #define SQLITE_CONFIG_MEMSTATUS 9 /* boolean */
  1998. #define SQLITE_CONFIG_MUTEX 10 /* sqlite3_mutex_methods* */
  1999. #define SQLITE_CONFIG_GETMUTEX 11 /* sqlite3_mutex_methods* */
  2000. /* previously SQLITE_CONFIG_CHUNKALLOC 12 which is now unused. */
  2001. #define SQLITE_CONFIG_LOOKASIDE 13 /* int int */
  2002. #define SQLITE_CONFIG_PCACHE 14 /* no-op */
  2003. #define SQLITE_CONFIG_GETPCACHE 15 /* no-op */
  2004. #define SQLITE_CONFIG_LOG 16 /* xFunc, void* */
  2005. #define SQLITE_CONFIG_URI 17 /* int */
  2006. #define SQLITE_CONFIG_PCACHE2 18 /* sqlite3_pcache_methods2* */
  2007. #define SQLITE_CONFIG_GETPCACHE2 19 /* sqlite3_pcache_methods2* */
  2008. /*
  2009. ** CAPI3REF: Database Connection Configuration Options
  2010. **
  2011. ** These constants are the available integer configuration options that
  2012. ** can be passed as the second argument to the [sqlite3_db_config()] interface.
  2013. **
  2014. ** New configuration options may be added in future releases of SQLite.
  2015. ** Existing configuration options might be discontinued. Applications
  2016. ** should check the return code from [sqlite3_db_config()] to make sure that
  2017. ** the call worked. ^The [sqlite3_db_config()] interface will return a
  2018. ** non-zero [error code] if a discontinued or unsupported configuration option
  2019. ** is invoked.
  2020. **
  2021. ** <dl>
  2022. ** <dt>SQLITE_DBCONFIG_LOOKASIDE</dt>
  2023. ** <dd> ^This option takes three additional arguments that determine the
  2024. ** [lookaside memory allocator] configuration for the [database connection].
  2025. ** ^The first argument (the third parameter to [sqlite3_db_config()] is a
  2026. ** pointer to a memory buffer to use for lookaside memory.
  2027. ** ^The first argument after the SQLITE_DBCONFIG_LOOKASIDE verb
  2028. ** may be NULL in which case SQLite will allocate the
  2029. ** lookaside buffer itself using [sqlite3_malloc()]. ^The second argument is the
  2030. ** size of each lookaside buffer slot. ^The third argument is the number of
  2031. ** slots. The size of the buffer in the first argument must be greater than
  2032. ** or equal to the product of the second and third arguments. The buffer
  2033. ** must be aligned to an 8-byte boundary. ^If the second argument to
  2034. ** SQLITE_DBCONFIG_LOOKASIDE is not a multiple of 8, it is internally
  2035. ** rounded down to the next smaller multiple of 8. ^(The lookaside memory
  2036. ** configuration for a database connection can only be changed when that
  2037. ** connection is not currently using lookaside memory, or in other words
  2038. ** when the "current value" returned by
  2039. ** [sqlite3_db_status](D,[SQLITE_CONFIG_LOOKASIDE],...) is zero.
  2040. ** Any attempt to change the lookaside memory configuration when lookaside
  2041. ** memory is in use leaves the configuration unchanged and returns
  2042. ** [SQLITE_BUSY].)^</dd>
  2043. **
  2044. ** <dt>SQLITE_DBCONFIG_ENABLE_FKEY</dt>
  2045. ** <dd> ^This option is used to enable or disable the enforcement of
  2046. ** [foreign key constraints]. There should be two additional arguments.
  2047. ** The first argument is an integer which is 0 to disable FK enforcement,
  2048. ** positive to enable FK enforcement or negative to leave FK enforcement
  2049. ** unchanged. The second parameter is a pointer to an integer into which
  2050. ** is written 0 or 1 to indicate whether FK enforcement is off or on
  2051. ** following this call. The second parameter may be a NULL pointer, in
  2052. ** which case the FK enforcement setting is not reported back. </dd>
  2053. **
  2054. ** <dt>SQLITE_DBCONFIG_ENABLE_TRIGGER</dt>
  2055. ** <dd> ^This option is used to enable or disable [CREATE TRIGGER | triggers].
  2056. ** There should be two additional arguments.
  2057. ** The first argument is an integer which is 0 to disable triggers,
  2058. ** positive to enable triggers or negative to leave the setting unchanged.
  2059. ** The second parameter is a pointer to an integer into which
  2060. ** is written 0 or 1 to indicate whether triggers are disabled or enabled
  2061. ** following this call. The second parameter may be a NULL pointer, in
  2062. ** which case the trigger setting is not reported back. </dd>
  2063. **
  2064. ** </dl>
  2065. */
  2066. #define SQLITE_DBCONFIG_LOOKASIDE 1001 /* void* int int */
  2067. #define SQLITE_DBCONFIG_ENABLE_FKEY 1002 /* int int* */
  2068. #define SQLITE_DBCONFIG_ENABLE_TRIGGER 1003 /* int int* */
  2069. /*
  2070. ** CAPI3REF: Enable Or Disable Extended Result Codes
  2071. **
  2072. ** ^The sqlite3_extended_result_codes() routine enables or disables the
  2073. ** [extended result codes] feature of SQLite. ^The extended result
  2074. ** codes are disabled by default for historical compatibility.
  2075. */
  2076. SQLITE_API int sqlite3_extended_result_codes(sqlite3*, int onoff);
  2077. /*
  2078. ** CAPI3REF: Last Insert Rowid
  2079. **
  2080. ** ^Each entry in an SQLite table has a unique 64-bit signed
  2081. ** integer key called the [ROWID | "rowid"]. ^The rowid is always available
  2082. ** as an undeclared column named ROWID, OID, or _ROWID_ as long as those
  2083. ** names are not also used by explicitly declared columns. ^If
  2084. ** the table has a column of type [INTEGER PRIMARY KEY] then that column
  2085. ** is another alias for the rowid.
  2086. **
  2087. ** ^This routine returns the [rowid] of the most recent
  2088. ** successful [INSERT] into the database from the [database connection]
  2089. ** in the first argument. ^As of SQLite version 3.7.7, this routines
  2090. ** records the last insert rowid of both ordinary tables and [virtual tables].
  2091. ** ^If no successful [INSERT]s
  2092. ** have ever occurred on that database connection, zero is returned.
  2093. **
  2094. ** ^(If an [INSERT] occurs within a trigger or within a [virtual table]
  2095. ** method, then this routine will return the [rowid] of the inserted
  2096. ** row as long as the trigger or virtual table method is running.
  2097. ** But once the trigger or virtual table method ends, the value returned
  2098. ** by this routine reverts to what it was before the trigger or virtual
  2099. ** table method began.)^
  2100. **
  2101. ** ^An [INSERT] that fails due to a constraint violation is not a
  2102. ** successful [INSERT] and does not change the value returned by this
  2103. ** routine. ^Thus INSERT OR FAIL, INSERT OR IGNORE, INSERT OR ROLLBACK,
  2104. ** and INSERT OR ABORT make no changes to the return value of this
  2105. ** routine when their insertion fails. ^(When INSERT OR REPLACE
  2106. ** encounters a constraint violation, it does not fail. The
  2107. ** INSERT continues to completion after deleting rows that caused
  2108. ** the constraint problem so INSERT OR REPLACE will always change
  2109. ** the return value of this interface.)^
  2110. **
  2111. ** ^For the purposes of this routine, an [INSERT] is considered to
  2112. ** be successful even if it is subsequently rolled back.
  2113. **
  2114. ** This function is accessible to SQL statements via the
  2115. ** [last_insert_rowid() SQL function].
  2116. **
  2117. ** If a separate thread performs a new [INSERT] on the same
  2118. ** database connection while the [sqlite3_last_insert_rowid()]
  2119. ** function is running and thus changes the last insert [rowid],
  2120. ** then the value returned by [sqlite3_last_insert_rowid()] is
  2121. ** unpredictable and might not equal either the old or the new
  2122. ** last insert [rowid].
  2123. */
  2124. SQLITE_API sqlite3_int64 sqlite3_last_insert_rowid(sqlite3*);
  2125. /*
  2126. ** CAPI3REF: Count The Number Of Rows Modified
  2127. **
  2128. ** ^This function returns the number of database rows that were changed
  2129. ** or inserted or deleted by the most recently completed SQL statement
  2130. ** on the [database connection] specified by the first parameter.
  2131. ** ^(Only changes that are directly specified by the [INSERT], [UPDATE],
  2132. ** or [DELETE] statement are counted. Auxiliary changes caused by
  2133. ** triggers or [foreign key actions] are not counted.)^ Use the
  2134. ** [sqlite3_total_changes()] function to find the total number of changes
  2135. ** including changes caused by triggers and foreign key actions.
  2136. **
  2137. ** ^Changes to a view that are simulated by an [INSTEAD OF trigger]
  2138. ** are not counted. Only real table changes are counted.
  2139. **
  2140. ** ^(A "row change" is a change to a single row of a single table
  2141. ** caused by an INSERT, DELETE, or UPDATE statement. Rows that
  2142. ** are changed as side effects of [REPLACE] constraint resolution,
  2143. ** rollback, ABORT processing, [DROP TABLE], or by any other
  2144. ** mechanisms do not count as direct row changes.)^
  2145. **
  2146. ** A "trigger context" is a scope of execution that begins and
  2147. ** ends with the script of a [CREATE TRIGGER | trigger].
  2148. ** Most SQL statements are
  2149. ** evaluated outside of any trigger. This is the "top level"
  2150. ** trigger context. If a trigger fires from the top level, a
  2151. ** new trigger context is entered for the duration of that one
  2152. ** trigger. Subtriggers create subcontexts for their duration.
  2153. **
  2154. ** ^Calling [sqlite3_exec()] or [sqlite3_step()] recursively does
  2155. ** not create a new trigger context.
  2156. **
  2157. ** ^This function returns the number of direct row changes in the
  2158. ** most recent INSERT, UPDATE, or DELETE statement within the same
  2159. ** trigger context.
  2160. **
  2161. ** ^Thus, when called from the top level, this function returns the
  2162. ** number of changes in the most recent INSERT, UPDATE, or DELETE
  2163. ** that also occurred at the top level. ^(Within the body of a trigger,
  2164. ** the sqlite3_changes() interface can be called to find the number of
  2165. ** changes in the most recently completed INSERT, UPDATE, or DELETE
  2166. ** statement within the body of the same trigger.
  2167. ** However, the number returned does not include changes
  2168. ** caused by subtriggers since those have their own context.)^
  2169. **
  2170. ** See also the [sqlite3_total_changes()] interface, the
  2171. ** [count_changes pragma], and the [changes() SQL function].
  2172. **
  2173. ** If a separate thread makes changes on the same database connection
  2174. ** while [sqlite3_changes()] is running then the value returned
  2175. ** is unpredictable and not meaningful.
  2176. */
  2177. SQLITE_API int sqlite3_changes(sqlite3*);
  2178. /*
  2179. ** CAPI3REF: Total Number Of Rows Modified
  2180. **
  2181. ** ^This function returns the number of row changes caused by [INSERT],
  2182. ** [UPDATE] or [DELETE] statements since the [database connection] was opened.
  2183. ** ^(The count returned by sqlite3_total_changes() includes all changes
  2184. ** from all [CREATE TRIGGER | trigger] contexts and changes made by
  2185. ** [foreign key actions]. However,
  2186. ** the count does not include changes used to implement [REPLACE] constraints,
  2187. ** do rollbacks or ABORT processing, or [DROP TABLE] processing. The
  2188. ** count does not include rows of views that fire an [INSTEAD OF trigger],
  2189. ** though if the INSTEAD OF trigger makes changes of its own, those changes
  2190. ** are counted.)^
  2191. ** ^The sqlite3_total_changes() function counts the changes as soon as
  2192. ** the statement that makes them is completed (when the statement handle
  2193. ** is passed to [sqlite3_reset()] or [sqlite3_finalize()]).
  2194. **
  2195. ** See also the [sqlite3_changes()] interface, the
  2196. ** [count_changes pragma], and the [total_changes() SQL function].
  2197. **
  2198. ** If a separate thread makes changes on the same database connection
  2199. ** while [sqlite3_total_changes()] is running then the value
  2200. ** returned is unpredictable and not meaningful.
  2201. */
  2202. SQLITE_API int sqlite3_total_changes(sqlite3*);
  2203. /*
  2204. ** CAPI3REF: Interrupt A Long-Running Query
  2205. **
  2206. ** ^This function causes any pending database operation to abort and
  2207. ** return at its earliest opportunity. This routine is typically
  2208. ** called in response to a user action such as pressing "Cancel"
  2209. ** or Ctrl-C where the user wants a long query operation to halt
  2210. ** immediately.
  2211. **
  2212. ** ^It is safe to call this routine from a thread different from the
  2213. ** thread that is currently running the database operation. But it
  2214. ** is not safe to call this routine with a [database connection] that
  2215. ** is closed or might close before sqlite3_interrupt() returns.
  2216. **
  2217. ** ^If an SQL operation is very nearly finished at the time when
  2218. ** sqlite3_interrupt() is called, then it might not have an opportunity
  2219. ** to be interrupted and might continue to completion.
  2220. **
  2221. ** ^An SQL operation that is interrupted will return [SQLITE_INTERRUPT].
  2222. ** ^If the interrupted SQL operation is an INSERT, UPDATE, or DELETE
  2223. ** that is inside an explicit transaction, then the entire transaction
  2224. ** will be rolled back automatically.
  2225. **
  2226. ** ^The sqlite3_interrupt(D) call is in effect until all currently running
  2227. ** SQL statements on [database connection] D complete. ^Any new SQL statements
  2228. ** that are started after the sqlite3_interrupt() call and before the
  2229. ** running statements reaches zero are interrupted as if they had been
  2230. ** running prior to the sqlite3_interrupt() call. ^New SQL statements
  2231. ** that are started after the running statement count reaches zero are
  2232. ** not effected by the sqlite3_interrupt().
  2233. ** ^A call to sqlite3_interrupt(D) that occurs when there are no running
  2234. ** SQL statements is a no-op and has no effect on SQL statements
  2235. ** that are started after the sqlite3_interrupt() call returns.
  2236. **
  2237. ** If the database connection closes while [sqlite3_interrupt()]
  2238. ** is running then bad things will likely happen.
  2239. */
  2240. SQLITE_API void sqlite3_interrupt(sqlite3*);
  2241. /*
  2242. ** CAPI3REF: Determine If An SQL Statement Is Complete
  2243. **
  2244. ** These routines are useful during command-line input to determine if the
  2245. ** currently entered text seems to form a complete SQL statement or
  2246. ** if additional input is needed before sending the text into
  2247. ** SQLite for parsing. ^These routines return 1 if the input string
  2248. ** appears to be a complete SQL statement. ^A statement is judged to be
  2249. ** complete if it ends with a semicolon token and is not a prefix of a
  2250. ** well-formed CREATE TRIGGER statement. ^Semicolons that are embedded within
  2251. ** string literals or quoted identifier names or comments are not
  2252. ** independent tokens (they are part of the token in which they are
  2253. ** embedded) and thus do not count as a statement terminator. ^Whitespace
  2254. ** and comments that follow the final semicolon are ignored.
  2255. **
  2256. ** ^These routines return 0 if the statement is incomplete. ^If a
  2257. ** memory allocation fails, then SQLITE_NOMEM is returned.
  2258. **
  2259. ** ^These routines do not parse the SQL statements thus
  2260. ** will not detect syntactically incorrect SQL.
  2261. **
  2262. ** ^(If SQLite has not been initialized using [sqlite3_initialize()] prior
  2263. ** to invoking sqlite3_complete16() then sqlite3_initialize() is invoked
  2264. ** automatically by sqlite3_complete16(). If that initialization fails,
  2265. ** then the return value from sqlite3_complete16() will be non-zero
  2266. ** regardless of whether or not the input SQL is complete.)^
  2267. **
  2268. ** The input to [sqlite3_complete()] must be a zero-terminated
  2269. ** UTF-8 string.
  2270. **
  2271. ** The input to [sqlite3_complete16()] must be a zero-terminated
  2272. ** UTF-16 string in native byte order.
  2273. */
  2274. SQLITE_API int sqlite3_complete(const char *sql);
  2275. SQLITE_API int sqlite3_complete16(const void *sql);
  2276. /*
  2277. ** CAPI3REF: Register A Callback To Handle SQLITE_BUSY Errors
  2278. **
  2279. ** ^This routine sets a callback function that might be invoked whenever
  2280. ** an attempt is made to open a database table that another thread
  2281. ** or process has locked.
  2282. **
  2283. ** ^If the busy callback is NULL, then [SQLITE_BUSY] or [SQLITE_IOERR_BLOCKED]
  2284. ** is returned immediately upon encountering the lock. ^If the busy callback
  2285. ** is not NULL, then the callback might be invoked with two arguments.
  2286. **
  2287. ** ^The first argument to the busy handler is a copy of the void* pointer which
  2288. ** is the third argument to sqlite3_busy_handler(). ^The second argument to
  2289. ** the busy handler callback is the number of times that the busy handler has
  2290. ** been invoked for this locking event. ^If the
  2291. ** busy callback returns 0, then no additional attempts are made to
  2292. ** access the database and [SQLITE_BUSY] or [SQLITE_IOERR_BLOCKED] is returned.
  2293. ** ^If the callback returns non-zero, then another attempt
  2294. ** is made to open the database for reading and the cycle repeats.
  2295. **
  2296. ** The presence of a busy handler does not guarantee that it will be invoked
  2297. ** when there is lock contention. ^If SQLite determines that invoking the busy
  2298. ** handler could result in a deadlock, it will go ahead and return [SQLITE_BUSY]
  2299. ** or [SQLITE_IOERR_BLOCKED] instead of invoking the busy handler.
  2300. ** Consider a scenario where one process is holding a read lock that
  2301. ** it is trying to promote to a reserved lock and
  2302. ** a second process is holding a reserved lock that it is trying
  2303. ** to promote to an exclusive lock. The first process cannot proceed
  2304. ** because it is blocked by the second and the second process cannot
  2305. ** proceed because it is blocked by the first. If both processes
  2306. ** invoke the busy handlers, neither will make any progress. Therefore,
  2307. ** SQLite returns [SQLITE_BUSY] for the first process, hoping that this
  2308. ** will induce the first process to release its read lock and allow
  2309. ** the second process to proceed.
  2310. **
  2311. ** ^The default busy callback is NULL.
  2312. **
  2313. ** ^The [SQLITE_BUSY] error is converted to [SQLITE_IOERR_BLOCKED]
  2314. ** when SQLite is in the middle of a large transaction where all the
  2315. ** changes will not fit into the in-memory cache. SQLite will
  2316. ** already hold a RESERVED lock on the database file, but it needs
  2317. ** to promote this lock to EXCLUSIVE so that it can spill cache
  2318. ** pages into the database file without harm to concurrent
  2319. ** readers. ^If it is unable to promote the lock, then the in-memory
  2320. ** cache will be left in an inconsistent state and so the error
  2321. ** code is promoted from the relatively benign [SQLITE_BUSY] to
  2322. ** the more severe [SQLITE_IOERR_BLOCKED]. ^This error code promotion
  2323. ** forces an automatic rollback of the changes. See the
  2324. ** <a href="/cvstrac/wiki?p=CorruptionFollowingBusyError">
  2325. ** CorruptionFollowingBusyError</a> wiki page for a discussion of why
  2326. ** this is important.
  2327. **
  2328. ** ^(There can only be a single busy handler defined for each
  2329. ** [database connection]. Setting a new busy handler clears any
  2330. ** previously set handler.)^ ^Note that calling [sqlite3_busy_timeout()]
  2331. ** will also set or clear the busy handler.
  2332. **
  2333. ** The busy callback should not take any actions which modify the
  2334. ** database connection that invoked the busy handler. Any such actions
  2335. ** result in undefined behavior.
  2336. **
  2337. ** A busy handler must not close the database connection
  2338. ** or [prepared statement] that invoked the busy handler.
  2339. */
  2340. SQLITE_API int sqlite3_busy_handler(sqlite3*, int(*)(void*,int), void*);
  2341. /*
  2342. ** CAPI3REF: Set A Busy Timeout
  2343. **
  2344. ** ^This routine sets a [sqlite3_busy_handler | busy handler] that sleeps
  2345. ** for a specified amount of time when a table is locked. ^The handler
  2346. ** will sleep multiple times until at least "ms" milliseconds of sleeping
  2347. ** have accumulated. ^After at least "ms" milliseconds of sleeping,
  2348. ** the handler returns 0 which causes [sqlite3_step()] to return
  2349. ** [SQLITE_BUSY] or [SQLITE_IOERR_BLOCKED].
  2350. **
  2351. ** ^Calling this routine with an argument less than or equal to zero
  2352. ** turns off all busy handlers.
  2353. **
  2354. ** ^(There can only be a single busy handler for a particular
  2355. ** [database connection] any any given moment. If another busy handler
  2356. ** was defined (using [sqlite3_busy_handler()]) prior to calling
  2357. ** this routine, that other busy handler is cleared.)^
  2358. */
  2359. SQLITE_API int sqlite3_busy_timeout(sqlite3*, int ms);
  2360. /*
  2361. ** CAPI3REF: Convenience Routines For Running Queries
  2362. **
  2363. ** This is a legacy interface that is preserved for backwards compatibility.
  2364. ** Use of this interface is not recommended.
  2365. **
  2366. ** Definition: A <b>result table</b> is memory data structure created by the
  2367. ** [sqlite3_get_table()] interface. A result table records the
  2368. ** complete query results from one or more queries.
  2369. **
  2370. ** The table conceptually has a number of rows and columns. But
  2371. ** these numbers are not part of the result table itself. These
  2372. ** numbers are obtained separately. Let N be the number of rows
  2373. ** and M be the number of columns.
  2374. **
  2375. ** A result table is an array of pointers to zero-terminated UTF-8 strings.
  2376. ** There are (N+1)*M elements in the array. The first M pointers point
  2377. ** to zero-terminated strings that contain the names of the columns.
  2378. ** The remaining entries all point to query results. NULL values result
  2379. ** in NULL pointers. All other values are in their UTF-8 zero-terminated
  2380. ** string representation as returned by [sqlite3_column_text()].
  2381. **
  2382. ** A result table might consist of one or more memory allocations.
  2383. ** It is not safe to pass a result table directly to [sqlite3_free()].
  2384. ** A result table should be deallocated using [sqlite3_free_table()].
  2385. **
  2386. ** ^(As an example of the result table format, suppose a query result
  2387. ** is as follows:
  2388. **
  2389. ** <blockquote><pre>
  2390. ** Name | Age
  2391. ** -----------------------
  2392. ** Alice | 43
  2393. ** Bob | 28
  2394. ** Cindy | 21
  2395. ** </pre></blockquote>
  2396. **
  2397. ** There are two column (M==2) and three rows (N==3). Thus the
  2398. ** result table has 8 entries. Suppose the result table is stored
  2399. ** in an array names azResult. Then azResult holds this content:
  2400. **
  2401. ** <blockquote><pre>
  2402. ** azResult&#91;0] = "Name";
  2403. ** azResult&#91;1] = "Age";
  2404. ** azResult&#91;2] = "Alice";
  2405. ** azResult&#91;3] = "43";
  2406. ** azResult&#91;4] = "Bob";
  2407. ** azResult&#91;5] = "28";
  2408. ** azResult&#91;6] = "Cindy";
  2409. ** azResult&#91;7] = "21";
  2410. ** </pre></blockquote>)^
  2411. **
  2412. ** ^The sqlite3_get_table() function evaluates one or more
  2413. ** semicolon-separated SQL statements in the zero-terminated UTF-8
  2414. ** string of its 2nd parameter and returns a result table to the
  2415. ** pointer given in its 3rd parameter.
  2416. **
  2417. ** After the application has finished with the result from sqlite3_get_table(),
  2418. ** it must pass the result table pointer to sqlite3_free_table() in order to
  2419. ** release the memory that was malloced. Because of the way the
  2420. ** [sqlite3_malloc()] happens within sqlite3_get_table(), the calling
  2421. ** function must not try to call [sqlite3_free()] directly. Only
  2422. ** [sqlite3_free_table()] is able to release the memory properly and safely.
  2423. **
  2424. ** The sqlite3_get_table() interface is implemented as a wrapper around
  2425. ** [sqlite3_exec()]. The sqlite3_get_table() routine does not have access
  2426. ** to any internal data structures of SQLite. It uses only the public
  2427. ** interface defined here. As a consequence, errors that occur in the
  2428. ** wrapper layer outside of the internal [sqlite3_exec()] call are not
  2429. ** reflected in subsequent calls to [sqlite3_errcode()] or
  2430. ** [sqlite3_errmsg()].
  2431. */
  2432. SQLITE_API int sqlite3_get_table(
  2433. sqlite3 *db, /* An open database */
  2434. const char *zSql, /* SQL to be evaluated */
  2435. char ***pazResult, /* Results of the query */
  2436. int *pnRow, /* Number of result rows written here */
  2437. int *pnColumn, /* Number of result columns written here */
  2438. char **pzErrmsg /* Error msg written here */
  2439. );
  2440. SQLITE_API void sqlite3_free_table(char **result);
  2441. /*
  2442. ** CAPI3REF: Formatted String Printing Functions
  2443. **
  2444. ** These routines are work-alikes of the "printf()" family of functions
  2445. ** from the standard C library.
  2446. **
  2447. ** ^The sqlite3_mprintf() and sqlite3_vmprintf() routines write their
  2448. ** results into memory obtained from [sqlite3_malloc()].
  2449. ** The strings returned by these two routines should be
  2450. ** released by [sqlite3_free()]. ^Both routines return a
  2451. ** NULL pointer if [sqlite3_malloc()] is unable to allocate enough
  2452. ** memory to hold the resulting string.
  2453. **
  2454. ** ^(The sqlite3_snprintf() routine is similar to "snprintf()" from
  2455. ** the standard C library. The result is written into the
  2456. ** buffer supplied as the second parameter whose size is given by
  2457. ** the first parameter. Note that the order of the
  2458. ** first two parameters is reversed from snprintf().)^ This is an
  2459. ** historical accident that cannot be fixed without breaking
  2460. ** backwards compatibility. ^(Note also that sqlite3_snprintf()
  2461. ** returns a pointer to its buffer instead of the number of
  2462. ** characters actually written into the buffer.)^ We admit that
  2463. ** the number of characters written would be a more useful return
  2464. ** value but we cannot change the implementation of sqlite3_snprintf()
  2465. ** now without breaking compatibility.
  2466. **
  2467. ** ^As long as the buffer size is greater than zero, sqlite3_snprintf()
  2468. ** guarantees that the buffer is always zero-terminated. ^The first
  2469. ** parameter "n" is the total size of the buffer, including space for
  2470. ** the zero terminator. So the longest string that can be completely
  2471. ** written will be n-1 characters.
  2472. **
  2473. ** ^The sqlite3_vsnprintf() routine is a varargs version of sqlite3_snprintf().
  2474. **
  2475. ** These routines all implement some additional formatting
  2476. ** options that are useful for constructing SQL statements.
  2477. ** All of the usual printf() formatting options apply. In addition, there
  2478. ** is are "%q", "%Q", and "%z" options.
  2479. **
  2480. ** ^(The %q option works like %s in that it substitutes a nul-terminated
  2481. ** string from the argument list. But %q also doubles every '\'' character.
  2482. ** %q is designed for use inside a string literal.)^ By doubling each '\''
  2483. ** character it escapes that character and allows it to be inserted into
  2484. ** the string.
  2485. **
  2486. ** For example, assume the string variable zText contains text as follows:
  2487. **
  2488. ** <blockquote><pre>
  2489. ** char *zText = "It's a happy day!";
  2490. ** </pre></blockquote>
  2491. **
  2492. ** One can use this text in an SQL statement as follows:
  2493. **
  2494. ** <blockquote><pre>
  2495. ** char *zSQL = sqlite3_mprintf("INSERT INTO table VALUES('%q')", zText);
  2496. ** sqlite3_exec(db, zSQL, 0, 0, 0);
  2497. ** sqlite3_free(zSQL);
  2498. ** </pre></blockquote>
  2499. **
  2500. ** Because the %q format string is used, the '\'' character in zText
  2501. ** is escaped and the SQL generated is as follows:
  2502. **
  2503. ** <blockquote><pre>
  2504. ** INSERT INTO table1 VALUES('It''s a happy day!')
  2505. ** </pre></blockquote>
  2506. **
  2507. ** This is correct. Had we used %s instead of %q, the generated SQL
  2508. ** would have looked like this:
  2509. **
  2510. ** <blockquote><pre>
  2511. ** INSERT INTO table1 VALUES('It's a happy day!');
  2512. ** </pre></blockquote>
  2513. **
  2514. ** This second example is an SQL syntax error. As a general rule you should
  2515. ** always use %q instead of %s when inserting text into a string literal.
  2516. **
  2517. ** ^(The %Q option works like %q except it also adds single quotes around
  2518. ** the outside of the total string. Additionally, if the parameter in the
  2519. ** argument list is a NULL pointer, %Q substitutes the text "NULL" (without
  2520. ** single quotes).)^ So, for example, one could say:
  2521. **
  2522. ** <blockquote><pre>
  2523. ** char *zSQL = sqlite3_mprintf("INSERT INTO table VALUES(%Q)", zText);
  2524. ** sqlite3_exec(db, zSQL, 0, 0, 0);
  2525. ** sqlite3_free(zSQL);
  2526. ** </pre></blockquote>
  2527. **
  2528. ** The code above will render a correct SQL statement in the zSQL
  2529. ** variable even if the zText variable is a NULL pointer.
  2530. **
  2531. ** ^(The "%z" formatting option works like "%s" but with the
  2532. ** addition that after the string has been read and copied into
  2533. ** the result, [sqlite3_free()] is called on the input string.)^
  2534. */
  2535. SQLITE_API char *sqlite3_mprintf(const char*,...);
  2536. SQLITE_API char *sqlite3_vmprintf(const char*, va_list);
  2537. SQLITE_API char *sqlite3_snprintf(int,char*,const char*, ...);
  2538. SQLITE_API char *sqlite3_vsnprintf(int,char*,const char*, va_list);
  2539. /*
  2540. ** CAPI3REF: Memory Allocation Subsystem
  2541. **
  2542. ** The SQLite core uses these three routines for all of its own
  2543. ** internal memory allocation needs. "Core" in the previous sentence
  2544. ** does not include operating-system specific VFS implementation. The
  2545. ** Windows VFS uses native malloc() and free() for some operations.
  2546. **
  2547. ** ^The sqlite3_malloc() routine returns a pointer to a block
  2548. ** of memory at least N bytes in length, where N is the parameter.
  2549. ** ^If sqlite3_malloc() is unable to obtain sufficient free
  2550. ** memory, it returns a NULL pointer. ^If the parameter N to
  2551. ** sqlite3_malloc() is zero or negative then sqlite3_malloc() returns
  2552. ** a NULL pointer.
  2553. **
  2554. ** ^Calling sqlite3_free() with a pointer previously returned
  2555. ** by sqlite3_malloc() or sqlite3_realloc() releases that memory so
  2556. ** that it might be reused. ^The sqlite3_free() routine is
  2557. ** a no-op if is called with a NULL pointer. Passing a NULL pointer
  2558. ** to sqlite3_free() is harmless. After being freed, memory
  2559. ** should neither be read nor written. Even reading previously freed
  2560. ** memory might result in a segmentation fault or other severe error.
  2561. ** Memory corruption, a segmentation fault, or other severe error
  2562. ** might result if sqlite3_free() is called with a non-NULL pointer that
  2563. ** was not obtained from sqlite3_malloc() or sqlite3_realloc().
  2564. **
  2565. ** ^(The sqlite3_realloc() interface attempts to resize a
  2566. ** prior memory allocation to be at least N bytes, where N is the
  2567. ** second parameter. The memory allocation to be resized is the first
  2568. ** parameter.)^ ^ If the first parameter to sqlite3_realloc()
  2569. ** is a NULL pointer then its behavior is identical to calling
  2570. ** sqlite3_malloc(N) where N is the second parameter to sqlite3_realloc().
  2571. ** ^If the second parameter to sqlite3_realloc() is zero or
  2572. ** negative then the behavior is exactly the same as calling
  2573. ** sqlite3_free(P) where P is the first parameter to sqlite3_realloc().
  2574. ** ^sqlite3_realloc() returns a pointer to a memory allocation
  2575. ** of at least N bytes in size or NULL if sufficient memory is unavailable.
  2576. ** ^If M is the size of the prior allocation, then min(N,M) bytes
  2577. ** of the prior allocation are copied into the beginning of buffer returned
  2578. ** by sqlite3_realloc() and the prior allocation is freed.
  2579. ** ^If sqlite3_realloc() returns NULL, then the prior allocation
  2580. ** is not freed.
  2581. **
  2582. ** ^The memory returned by sqlite3_malloc() and sqlite3_realloc()
  2583. ** is always aligned to at least an 8 byte boundary, or to a
  2584. ** 4 byte boundary if the [SQLITE_4_BYTE_ALIGNED_MALLOC] compile-time
  2585. ** option is used.
  2586. **
  2587. ** In SQLite version 3.5.0 and 3.5.1, it was possible to define
  2588. ** the SQLITE_OMIT_MEMORY_ALLOCATION which would cause the built-in
  2589. ** implementation of these routines to be omitted. That capability
  2590. ** is no longer provided. Only built-in memory allocators can be used.
  2591. **
  2592. ** The Windows OS interface layer calls
  2593. ** the system malloc() and free() directly when converting
  2594. ** filenames between the UTF-8 encoding used by SQLite
  2595. ** and whatever filename encoding is used by the particular Windows
  2596. ** installation. Memory allocation errors are detected, but
  2597. ** they are reported back as [SQLITE_CANTOPEN] or
  2598. ** [SQLITE_IOERR] rather than [SQLITE_NOMEM].
  2599. **
  2600. ** The pointer arguments to [sqlite3_free()] and [sqlite3_realloc()]
  2601. ** must be either NULL or else pointers obtained from a prior
  2602. ** invocation of [sqlite3_malloc()] or [sqlite3_realloc()] that have
  2603. ** not yet been released.
  2604. **
  2605. ** The application must not read or write any part of
  2606. ** a block of memory after it has been released using
  2607. ** [sqlite3_free()] or [sqlite3_realloc()].
  2608. */
  2609. SQLITE_API void *sqlite3_malloc(int);
  2610. SQLITE_API void *sqlite3_realloc(void*, int);
  2611. SQLITE_API void sqlite3_free(void*);
  2612. /*
  2613. ** CAPI3REF: Memory Allocator Statistics
  2614. **
  2615. ** SQLite provides these two interfaces for reporting on the status
  2616. ** of the [sqlite3_malloc()], [sqlite3_free()], and [sqlite3_realloc()]
  2617. ** routines, which form the built-in memory allocation subsystem.
  2618. **
  2619. ** ^The [sqlite3_memory_used()] routine returns the number of bytes
  2620. ** of memory currently outstanding (malloced but not freed).
  2621. ** ^The [sqlite3_memory_highwater()] routine returns the maximum
  2622. ** value of [sqlite3_memory_used()] since the high-water mark
  2623. ** was last reset. ^The values returned by [sqlite3_memory_used()] and
  2624. ** [sqlite3_memory_highwater()] include any overhead
  2625. ** added by SQLite in its implementation of [sqlite3_malloc()],
  2626. ** but not overhead added by the any underlying system library
  2627. ** routines that [sqlite3_malloc()] may call.
  2628. **
  2629. ** ^The memory high-water mark is reset to the current value of
  2630. ** [sqlite3_memory_used()] if and only if the parameter to
  2631. ** [sqlite3_memory_highwater()] is true. ^The value returned
  2632. ** by [sqlite3_memory_highwater(1)] is the high-water mark
  2633. ** prior to the reset.
  2634. */
  2635. SQLITE_API sqlite3_int64 sqlite3_memory_used(void);
  2636. SQLITE_API sqlite3_int64 sqlite3_memory_highwater(int resetFlag);
  2637. /*
  2638. ** CAPI3REF: Pseudo-Random Number Generator
  2639. **
  2640. ** SQLite contains a high-quality pseudo-random number generator (PRNG) used to
  2641. ** select random [ROWID | ROWIDs] when inserting new records into a table that
  2642. ** already uses the largest possible [ROWID]. The PRNG is also used for
  2643. ** the build-in random() and randomblob() SQL functions. This interface allows
  2644. ** applications to access the same PRNG for other purposes.
  2645. **
  2646. ** ^A call to this routine stores N bytes of randomness into buffer P.
  2647. **
  2648. ** ^The first time this routine is invoked (either internally or by
  2649. ** the application) the PRNG is seeded using randomness obtained
  2650. ** from the xRandomness method of the default [sqlite3_vfs] object.
  2651. ** ^On all subsequent invocations, the pseudo-randomness is generated
  2652. ** internally and without recourse to the [sqlite3_vfs] xRandomness
  2653. ** method.
  2654. */
  2655. SQLITE_API void sqlite3_randomness(int N, void *P);
  2656. /*
  2657. ** CAPI3REF: Compile-Time Authorization Callbacks
  2658. **
  2659. ** ^This routine registers an authorizer callback with a particular
  2660. ** [database connection], supplied in the first argument.
  2661. ** ^The authorizer callback is invoked as SQL statements are being compiled
  2662. ** by [sqlite3_prepare()] or its variants [sqlite3_prepare_v2()],
  2663. ** [sqlite3_prepare16()] and [sqlite3_prepare16_v2()]. ^At various
  2664. ** points during the compilation process, as logic is being created
  2665. ** to perform various actions, the authorizer callback is invoked to
  2666. ** see if those actions are allowed. ^The authorizer callback should
  2667. ** return [SQLITE_OK] to allow the action, [SQLITE_IGNORE] to disallow the
  2668. ** specific action but allow the SQL statement to continue to be
  2669. ** compiled, or [SQLITE_DENY] to cause the entire SQL statement to be
  2670. ** rejected with an error. ^If the authorizer callback returns
  2671. ** any value other than [SQLITE_IGNORE], [SQLITE_OK], or [SQLITE_DENY]
  2672. ** then the [sqlite3_prepare_v2()] or equivalent call that triggered
  2673. ** the authorizer will fail with an error message.
  2674. **
  2675. ** When the callback returns [SQLITE_OK], that means the operation
  2676. ** requested is ok. ^When the callback returns [SQLITE_DENY], the
  2677. ** [sqlite3_prepare_v2()] or equivalent call that triggered the
  2678. ** authorizer will fail with an error message explaining that
  2679. ** access is denied.
  2680. **
  2681. ** ^The first parameter to the authorizer callback is a copy of the third
  2682. ** parameter to the sqlite3_set_authorizer() interface. ^The second parameter
  2683. ** to the callback is an integer [SQLITE_COPY | action code] that specifies
  2684. ** the particular action to be authorized. ^The third through sixth parameters
  2685. ** to the callback are zero-terminated strings that contain additional
  2686. ** details about the action to be authorized.
  2687. **
  2688. ** ^If the action code is [SQLITE_READ]
  2689. ** and the callback returns [SQLITE_IGNORE] then the
  2690. ** [prepared statement] statement is constructed to substitute
  2691. ** a NULL value in place of the table column that would have
  2692. ** been read if [SQLITE_OK] had been returned. The [SQLITE_IGNORE]
  2693. ** return can be used to deny an untrusted user access to individual
  2694. ** columns of a table.
  2695. ** ^If the action code is [SQLITE_DELETE] and the callback returns
  2696. ** [SQLITE_IGNORE] then the [DELETE] operation proceeds but the
  2697. ** [truncate optimization] is disabled and all rows are deleted individually.
  2698. **
  2699. ** An authorizer is used when [sqlite3_prepare | preparing]
  2700. ** SQL statements from an untrusted source, to ensure that the SQL statements
  2701. ** do not try to access data they are not allowed to see, or that they do not
  2702. ** try to execute malicious statements that damage the database. For
  2703. ** example, an application may allow a user to enter arbitrary
  2704. ** SQL queries for evaluation by a database. But the application does
  2705. ** not want the user to be able to make arbitrary changes to the
  2706. ** database. An authorizer could then be put in place while the
  2707. ** user-entered SQL is being [sqlite3_prepare | prepared] that
  2708. ** disallows everything except [SELECT] statements.
  2709. **
  2710. ** Applications that need to process SQL from untrusted sources
  2711. ** might also consider lowering resource limits using [sqlite3_limit()]
  2712. ** and limiting database size using the [max_page_count] [PRAGMA]
  2713. ** in addition to using an authorizer.
  2714. **
  2715. ** ^(Only a single authorizer can be in place on a database connection
  2716. ** at a time. Each call to sqlite3_set_authorizer overrides the
  2717. ** previous call.)^ ^Disable the authorizer by installing a NULL callback.
  2718. ** The authorizer is disabled by default.
  2719. **
  2720. ** The authorizer callback must not do anything that will modify
  2721. ** the database connection that invoked the authorizer callback.
  2722. ** Note that [sqlite3_prepare_v2()] and [sqlite3_step()] both modify their
  2723. ** database connections for the meaning of "modify" in this paragraph.
  2724. **
  2725. ** ^When [sqlite3_prepare_v2()] is used to prepare a statement, the
  2726. ** statement might be re-prepared during [sqlite3_step()] due to a
  2727. ** schema change. Hence, the application should ensure that the
  2728. ** correct authorizer callback remains in place during the [sqlite3_step()].
  2729. **
  2730. ** ^Note that the authorizer callback is invoked only during
  2731. ** [sqlite3_prepare()] or its variants. Authorization is not
  2732. ** performed during statement evaluation in [sqlite3_step()], unless
  2733. ** as stated in the previous paragraph, sqlite3_step() invokes
  2734. ** sqlite3_prepare_v2() to reprepare a statement after a schema change.
  2735. */
  2736. SQLITE_API int sqlite3_set_authorizer(
  2737. sqlite3*,
  2738. int (*xAuth)(void*,int,const char*,const char*,const char*,const char*),
  2739. void *pUserData
  2740. );
  2741. /*
  2742. ** CAPI3REF: Authorizer Return Codes
  2743. **
  2744. ** The [sqlite3_set_authorizer | authorizer callback function] must
  2745. ** return either [SQLITE_OK] or one of these two constants in order
  2746. ** to signal SQLite whether or not the action is permitted. See the
  2747. ** [sqlite3_set_authorizer | authorizer documentation] for additional
  2748. ** information.
  2749. **
  2750. ** Note that SQLITE_IGNORE is also used as a [SQLITE_ROLLBACK | return code]
  2751. ** from the [sqlite3_vtab_on_conflict()] interface.
  2752. */
  2753. #define SQLITE_DENY 1 /* Abort the SQL statement with an error */
  2754. #define SQLITE_IGNORE 2 /* Don't allow access, but don't generate an error */
  2755. /*
  2756. ** CAPI3REF: Authorizer Action Codes
  2757. **
  2758. ** The [sqlite3_set_authorizer()] interface registers a callback function
  2759. ** that is invoked to authorize certain SQL statement actions. The
  2760. ** second parameter to the callback is an integer code that specifies
  2761. ** what action is being authorized. These are the integer action codes that
  2762. ** the authorizer callback may be passed.
  2763. **
  2764. ** These action code values signify what kind of operation is to be
  2765. ** authorized. The 3rd and 4th parameters to the authorization
  2766. ** callback function will be parameters or NULL depending on which of these
  2767. ** codes is used as the second parameter. ^(The 5th parameter to the
  2768. ** authorizer callback is the name of the database ("main", "temp",
  2769. ** etc.) if applicable.)^ ^The 6th parameter to the authorizer callback
  2770. ** is the name of the inner-most trigger or view that is responsible for
  2771. ** the access attempt or NULL if this access attempt is directly from
  2772. ** top-level SQL code.
  2773. */
  2774. /******************************************* 3rd ************ 4th ***********/
  2775. #define SQLITE_CREATE_INDEX 1 /* Index Name Table Name */
  2776. #define SQLITE_CREATE_TABLE 2 /* Table Name NULL */
  2777. #define SQLITE_CREATE_TEMP_INDEX 3 /* Index Name Table Name */
  2778. #define SQLITE_CREATE_TEMP_TABLE 4 /* Table Name NULL */
  2779. #define SQLITE_CREATE_TEMP_TRIGGER 5 /* Trigger Name Table Name */
  2780. #define SQLITE_CREATE_TEMP_VIEW 6 /* View Name NULL */
  2781. #define SQLITE_CREATE_TRIGGER 7 /* Trigger Name Table Name */
  2782. #define SQLITE_CREATE_VIEW 8 /* View Name NULL */
  2783. #define SQLITE_DELETE 9 /* Table Name NULL */
  2784. #define SQLITE_DROP_INDEX 10 /* Index Name Table Name */
  2785. #define SQLITE_DROP_TABLE 11 /* Table Name NULL */
  2786. #define SQLITE_DROP_TEMP_INDEX 12 /* Index Name Table Name */
  2787. #define SQLITE_DROP_TEMP_TABLE 13 /* Table Name NULL */
  2788. #define SQLITE_DROP_TEMP_TRIGGER 14 /* Trigger Name Table Name */
  2789. #define SQLITE_DROP_TEMP_VIEW 15 /* View Name NULL */
  2790. #define SQLITE_DROP_TRIGGER 16 /* Trigger Name Table Name */
  2791. #define SQLITE_DROP_VIEW 17 /* View Name NULL */
  2792. #define SQLITE_INSERT 18 /* Table Name NULL */
  2793. #define SQLITE_PRAGMA 19 /* Pragma Name 1st arg or NULL */
  2794. #define SQLITE_READ 20 /* Table Name Column Name */
  2795. #define SQLITE_SELECT 21 /* NULL NULL */
  2796. #define SQLITE_TRANSACTION 22 /* Operation NULL */
  2797. #define SQLITE_UPDATE 23 /* Table Name Column Name */
  2798. #define SQLITE_ATTACH 24 /* Filename NULL */
  2799. #define SQLITE_DETACH 25 /* Database Name NULL */
  2800. #define SQLITE_ALTER_TABLE 26 /* Database Name Table Name */
  2801. #define SQLITE_REINDEX 27 /* Index Name NULL */
  2802. #define SQLITE_ANALYZE 28 /* Table Name NULL */
  2803. #define SQLITE_CREATE_VTABLE 29 /* Table Name Module Name */
  2804. #define SQLITE_DROP_VTABLE 30 /* Table Name Module Name */
  2805. #define SQLITE_FUNCTION 31 /* NULL Function Name */
  2806. #define SQLITE_SAVEPOINT 32 /* Operation Savepoint Name */
  2807. #define SQLITE_COPY 0 /* No longer used */
  2808. /*
  2809. ** CAPI3REF: Tracing And Profiling Functions
  2810. **
  2811. ** These routines register callback functions that can be used for
  2812. ** tracing and profiling the execution of SQL statements.
  2813. **
  2814. ** ^The callback function registered by sqlite3_trace() is invoked at
  2815. ** various times when an SQL statement is being run by [sqlite3_step()].
  2816. ** ^The sqlite3_trace() callback is invoked with a UTF-8 rendering of the
  2817. ** SQL statement text as the statement first begins executing.
  2818. ** ^(Additional sqlite3_trace() callbacks might occur
  2819. ** as each triggered subprogram is entered. The callbacks for triggers
  2820. ** contain a UTF-8 SQL comment that identifies the trigger.)^
  2821. **
  2822. ** ^The callback function registered by sqlite3_profile() is invoked
  2823. ** as each SQL statement finishes. ^The profile callback contains
  2824. ** the original statement text and an estimate of wall-clock time
  2825. ** of how long that statement took to run. ^The profile callback
  2826. ** time is in units of nanoseconds, however the current implementation
  2827. ** is only capable of millisecond resolution so the six least significant
  2828. ** digits in the time are meaningless. Future versions of SQLite
  2829. ** might provide greater resolution on the profiler callback. The
  2830. ** sqlite3_profile() function is considered experimental and is
  2831. ** subject to change in future versions of SQLite.
  2832. */
  2833. SQLITE_API void *sqlite3_trace(sqlite3*, void(*xTrace)(void*,const char*), void*);
  2834. SQLITE_API SQLITE_EXPERIMENTAL void *sqlite3_profile(sqlite3*,
  2835. void(*xProfile)(void*,const char*,sqlite3_uint64), void*);
  2836. /*
  2837. ** CAPI3REF: Query Progress Callbacks
  2838. **
  2839. ** ^The sqlite3_progress_handler(D,N,X,P) interface causes the callback
  2840. ** function X to be invoked periodically during long running calls to
  2841. ** [sqlite3_exec()], [sqlite3_step()] and [sqlite3_get_table()] for
  2842. ** database connection D. An example use for this
  2843. ** interface is to keep a GUI updated during a large query.
  2844. **
  2845. ** ^The parameter P is passed through as the only parameter to the
  2846. ** callback function X. ^The parameter N is the number of
  2847. ** [virtual machine instructions] that are evaluated between successive
  2848. ** invocations of the callback X.
  2849. **
  2850. ** ^Only a single progress handler may be defined at one time per
  2851. ** [database connection]; setting a new progress handler cancels the
  2852. ** old one. ^Setting parameter X to NULL disables the progress handler.
  2853. ** ^The progress handler is also disabled by setting N to a value less
  2854. ** than 1.
  2855. **
  2856. ** ^If the progress callback returns non-zero, the operation is
  2857. ** interrupted. This feature can be used to implement a
  2858. ** "Cancel" button on a GUI progress dialog box.
  2859. **
  2860. ** The progress handler callback must not do anything that will modify
  2861. ** the database connection that invoked the progress handler.
  2862. ** Note that [sqlite3_prepare_v2()] and [sqlite3_step()] both modify their
  2863. ** database connections for the meaning of "modify" in this paragraph.
  2864. **
  2865. */
  2866. SQLITE_API void sqlite3_progress_handler(sqlite3*, int, int(*)(void*), void*);
  2867. /*
  2868. ** CAPI3REF: Opening A New Database Connection
  2869. **
  2870. ** ^These routines open an SQLite database file as specified by the
  2871. ** filename argument. ^The filename argument is interpreted as UTF-8 for
  2872. ** sqlite3_open() and sqlite3_open_v2() and as UTF-16 in the native byte
  2873. ** order for sqlite3_open16(). ^(A [database connection] handle is usually
  2874. ** returned in *ppDb, even if an error occurs. The only exception is that
  2875. ** if SQLite is unable to allocate memory to hold the [sqlite3] object,
  2876. ** a NULL will be written into *ppDb instead of a pointer to the [sqlite3]
  2877. ** object.)^ ^(If the database is opened (and/or created) successfully, then
  2878. ** [SQLITE_OK] is returned. Otherwise an [error code] is returned.)^ ^The
  2879. ** [sqlite3_errmsg()] or [sqlite3_errmsg16()] routines can be used to obtain
  2880. ** an English language description of the error following a failure of any
  2881. ** of the sqlite3_open() routines.
  2882. **
  2883. ** ^The default encoding for the database will be UTF-8 if
  2884. ** sqlite3_open() or sqlite3_open_v2() is called and
  2885. ** UTF-16 in the native byte order if sqlite3_open16() is used.
  2886. **
  2887. ** Whether or not an error occurs when it is opened, resources
  2888. ** associated with the [database connection] handle should be released by
  2889. ** passing it to [sqlite3_close()] when it is no longer required.
  2890. **
  2891. ** The sqlite3_open_v2() interface works like sqlite3_open()
  2892. ** except that it accepts two additional parameters for additional control
  2893. ** over the new database connection. ^(The flags parameter to
  2894. ** sqlite3_open_v2() can take one of
  2895. ** the following three values, optionally combined with the
  2896. ** [SQLITE_OPEN_NOMUTEX], [SQLITE_OPEN_FULLMUTEX], [SQLITE_OPEN_SHAREDCACHE],
  2897. ** [SQLITE_OPEN_PRIVATECACHE], and/or [SQLITE_OPEN_URI] flags:)^
  2898. **
  2899. ** <dl>
  2900. ** ^(<dt>[SQLITE_OPEN_READONLY]</dt>
  2901. ** <dd>The database is opened in read-only mode. If the database does not
  2902. ** already exist, an error is returned.</dd>)^
  2903. **
  2904. ** ^(<dt>[SQLITE_OPEN_READWRITE]</dt>
  2905. ** <dd>The database is opened for reading and writing if possible, or reading
  2906. ** only if the file is write protected by the operating system. In either
  2907. ** case the database must already exist, otherwise an error is returned.</dd>)^
  2908. **
  2909. ** ^(<dt>[SQLITE_OPEN_READWRITE] | [SQLITE_OPEN_CREATE]</dt>
  2910. ** <dd>The database is opened for reading and writing, and is created if
  2911. ** it does not already exist. This is the behavior that is always used for
  2912. ** sqlite3_open() and sqlite3_open16().</dd>)^
  2913. ** </dl>
  2914. **
  2915. ** If the 3rd parameter to sqlite3_open_v2() is not one of the
  2916. ** combinations shown above optionally combined with other
  2917. ** [SQLITE_OPEN_READONLY | SQLITE_OPEN_* bits]
  2918. ** then the behavior is undefined.
  2919. **
  2920. ** ^If the [SQLITE_OPEN_NOMUTEX] flag is set, then the database connection
  2921. ** opens in the multi-thread [threading mode] as long as the single-thread
  2922. ** mode has not been set at compile-time or start-time. ^If the
  2923. ** [SQLITE_OPEN_FULLMUTEX] flag is set then the database connection opens
  2924. ** in the serialized [threading mode] unless single-thread was
  2925. ** previously selected at compile-time or start-time.
  2926. ** ^The [SQLITE_OPEN_SHAREDCACHE] flag causes the database connection to be
  2927. ** eligible to use [shared cache mode], regardless of whether or not shared
  2928. ** cache is enabled using [sqlite3_enable_shared_cache()]. ^The
  2929. ** [SQLITE_OPEN_PRIVATECACHE] flag causes the database connection to not
  2930. ** participate in [shared cache mode] even if it is enabled.
  2931. **
  2932. ** ^The fourth parameter to sqlite3_open_v2() is the name of the
  2933. ** [sqlite3_vfs] object that defines the operating system interface that
  2934. ** the new database connection should use. ^If the fourth parameter is
  2935. ** a NULL pointer then the default [sqlite3_vfs] object is used.
  2936. **
  2937. ** ^If the filename is ":memory:", then a private, temporary in-memory database
  2938. ** is created for the connection. ^This in-memory database will vanish when
  2939. ** the database connection is closed. Future versions of SQLite might
  2940. ** make use of additional special filenames that begin with the ":" character.
  2941. ** It is recommended that when a database filename actually does begin with
  2942. ** a ":" character you should prefix the filename with a pathname such as
  2943. ** "./" to avoid ambiguity.
  2944. **
  2945. ** ^If the filename is an empty string, then a private, temporary
  2946. ** on-disk database will be created. ^This private database will be
  2947. ** automatically deleted as soon as the database connection is closed.
  2948. **
  2949. ** [[URI filenames in sqlite3_open()]] <h3>URI Filenames</h3>
  2950. **
  2951. ** ^If [URI filename] interpretation is enabled, and the filename argument
  2952. ** begins with "file:", then the filename is interpreted as a URI. ^URI
  2953. ** filename interpretation is enabled if the [SQLITE_OPEN_URI] flag is
  2954. ** set in the fourth argument to sqlite3_open_v2(), or if it has
  2955. ** been enabled globally using the [SQLITE_CONFIG_URI] option with the
  2956. ** [sqlite3_config()] method or by the [SQLITE_USE_URI] compile-time option.
  2957. ** As of SQLite version 3.7.7, URI filename interpretation is turned off
  2958. ** by default, but future releases of SQLite might enable URI filename
  2959. ** interpretation by default. See "[URI filenames]" for additional
  2960. ** information.
  2961. **
  2962. ** URI filenames are parsed according to RFC 3986. ^If the URI contains an
  2963. ** authority, then it must be either an empty string or the string
  2964. ** "localhost". ^If the authority is not an empty string or "localhost", an
  2965. ** error is returned to the caller. ^The fragment component of a URI, if
  2966. ** present, is ignored.
  2967. **
  2968. ** ^SQLite uses the path component of the URI as the name of the disk file
  2969. ** which contains the database. ^If the path begins with a '/' character,
  2970. ** then it is interpreted as an absolute path. ^If the path does not begin
  2971. ** with a '/' (meaning that the authority section is omitted from the URI)
  2972. ** then the path is interpreted as a relative path.
  2973. ** ^On windows, the first component of an absolute path
  2974. ** is a drive specification (e.g. "C:").
  2975. **
  2976. ** [[core URI query parameters]]
  2977. ** The query component of a URI may contain parameters that are interpreted
  2978. ** either by SQLite itself, or by a [VFS | custom VFS implementation].
  2979. ** SQLite interprets the following three query parameters:
  2980. **
  2981. ** <ul>
  2982. ** <li> <b>vfs</b>: ^The "vfs" parameter may be used to specify the name of
  2983. ** a VFS object that provides the operating system interface that should
  2984. ** be used to access the database file on disk. ^If this option is set to
  2985. ** an empty string the default VFS object is used. ^Specifying an unknown
  2986. ** VFS is an error. ^If sqlite3_open_v2() is used and the vfs option is
  2987. ** present, then the VFS specified by the option takes precedence over
  2988. ** the value passed as the fourth parameter to sqlite3_open_v2().
  2989. **
  2990. ** <li> <b>mode</b>: ^(The mode parameter may be set to either "ro", "rw" or
  2991. ** "rwc". Attempting to set it to any other value is an error)^.
  2992. ** ^If "ro" is specified, then the database is opened for read-only
  2993. ** access, just as if the [SQLITE_OPEN_READONLY] flag had been set in the
  2994. ** third argument to sqlite3_prepare_v2(). ^If the mode option is set to
  2995. ** "rw", then the database is opened for read-write (but not create)
  2996. ** access, as if SQLITE_OPEN_READWRITE (but not SQLITE_OPEN_CREATE) had
  2997. ** been set. ^Value "rwc" is equivalent to setting both
  2998. ** SQLITE_OPEN_READWRITE and SQLITE_OPEN_CREATE. ^If sqlite3_open_v2() is
  2999. ** used, it is an error to specify a value for the mode parameter that is
  3000. ** less restrictive than that specified by the flags passed as the third
  3001. ** parameter.
  3002. **
  3003. ** <li> <b>cache</b>: ^The cache parameter may be set to either "shared" or
  3004. ** "private". ^Setting it to "shared" is equivalent to setting the
  3005. ** SQLITE_OPEN_SHAREDCACHE bit in the flags argument passed to
  3006. ** sqlite3_open_v2(). ^Setting the cache parameter to "private" is
  3007. ** equivalent to setting the SQLITE_OPEN_PRIVATECACHE bit.
  3008. ** ^If sqlite3_open_v2() is used and the "cache" parameter is present in
  3009. ** a URI filename, its value overrides any behaviour requested by setting
  3010. ** SQLITE_OPEN_PRIVATECACHE or SQLITE_OPEN_SHAREDCACHE flag.
  3011. ** </ul>
  3012. **
  3013. ** ^Specifying an unknown parameter in the query component of a URI is not an
  3014. ** error. Future versions of SQLite might understand additional query
  3015. ** parameters. See "[query parameters with special meaning to SQLite]" for
  3016. ** additional information.
  3017. **
  3018. ** [[URI filename examples]] <h3>URI filename examples</h3>
  3019. **
  3020. ** <table border="1" align=center cellpadding=5>
  3021. ** <tr><th> URI filenames <th> Results
  3022. ** <tr><td> file:data.db <td>
  3023. ** Open the file "data.db" in the current directory.
  3024. ** <tr><td> file:/home/fred/data.db<br>
  3025. ** file:///home/fred/data.db <br>
  3026. ** file://localhost/home/fred/data.db <br> <td>
  3027. ** Open the database file "/home/fred/data.db".
  3028. ** <tr><td> file://darkstar/home/fred/data.db <td>
  3029. ** An error. "darkstar" is not a recognized authority.
  3030. ** <tr><td style="white-space:nowrap">
  3031. ** file:///C:/Documents%20and%20Settings/fred/Desktop/data.db
  3032. ** <td> Windows only: Open the file "data.db" on fred's desktop on drive
  3033. ** C:. Note that the %20 escaping in this example is not strictly
  3034. ** necessary - space characters can be used literally
  3035. ** in URI filenames.
  3036. ** <tr><td> file:data.db?mode=ro&cache=private <td>
  3037. ** Open file "data.db" in the current directory for read-only access.
  3038. ** Regardless of whether or not shared-cache mode is enabled by
  3039. ** default, use a private cache.
  3040. ** <tr><td> file:/home/fred/data.db?vfs=unix-nolock <td>
  3041. ** Open file "/home/fred/data.db". Use the special VFS "unix-nolock".
  3042. ** <tr><td> file:data.db?mode=readonly <td>
  3043. ** An error. "readonly" is not a valid option for the "mode" parameter.
  3044. ** </table>
  3045. **
  3046. ** ^URI hexadecimal escape sequences (%HH) are supported within the path and
  3047. ** query components of a URI. A hexadecimal escape sequence consists of a
  3048. ** percent sign - "%" - followed by exactly two hexadecimal digits
  3049. ** specifying an octet value. ^Before the path or query components of a
  3050. ** URI filename are interpreted, they are encoded using UTF-8 and all
  3051. ** hexadecimal escape sequences replaced by a single byte containing the
  3052. ** corresponding octet. If this process generates an invalid UTF-8 encoding,
  3053. ** the results are undefined.
  3054. **
  3055. ** <b>Note to Windows users:</b> The encoding used for the filename argument
  3056. ** of sqlite3_open() and sqlite3_open_v2() must be UTF-8, not whatever
  3057. ** codepage is currently defined. Filenames containing international
  3058. ** characters must be converted to UTF-8 prior to passing them into
  3059. ** sqlite3_open() or sqlite3_open_v2().
  3060. */
  3061. SQLITE_API int sqlite3_open(
  3062. const char *filename, /* Database filename (UTF-8) */
  3063. sqlite3 **ppDb /* OUT: SQLite db handle */
  3064. );
  3065. SQLITE_API int sqlite3_open16(
  3066. const void *filename, /* Database filename (UTF-16) */
  3067. sqlite3 **ppDb /* OUT: SQLite db handle */
  3068. );
  3069. SQLITE_API int sqlite3_open_v2(
  3070. const char *filename, /* Database filename (UTF-8) */
  3071. sqlite3 **ppDb, /* OUT: SQLite db handle */
  3072. int flags, /* Flags */
  3073. const char *zVfs /* Name of VFS module to use */
  3074. );
  3075. /*
  3076. ** CAPI3REF: Obtain Values For URI Parameters
  3077. **
  3078. ** These are utility routines, useful to VFS implementations, that check
  3079. ** to see if a database file was a URI that contained a specific query
  3080. ** parameter, and if so obtains the value of that query parameter.
  3081. **
  3082. ** If F is the database filename pointer passed into the xOpen() method of
  3083. ** a VFS implementation when the flags parameter to xOpen() has one or
  3084. ** more of the [SQLITE_OPEN_URI] or [SQLITE_OPEN_MAIN_DB] bits set and
  3085. ** P is the name of the query parameter, then
  3086. ** sqlite3_uri_parameter(F,P) returns the value of the P
  3087. ** parameter if it exists or a NULL pointer if P does not appear as a
  3088. ** query parameter on F. If P is a query parameter of F
  3089. ** has no explicit value, then sqlite3_uri_parameter(F,P) returns
  3090. ** a pointer to an empty string.
  3091. **
  3092. ** The sqlite3_uri_boolean(F,P,B) routine assumes that P is a boolean
  3093. ** parameter and returns true (1) or false (0) according to the value
  3094. ** of P. The value of P is true if it is "yes" or "true" or "on" or
  3095. ** a non-zero number and is false otherwise. If P is not a query parameter
  3096. ** on F then sqlite3_uri_boolean(F,P,B) returns (B!=0).
  3097. **
  3098. ** The sqlite3_uri_int64(F,P,D) routine converts the value of P into a
  3099. ** 64-bit signed integer and returns that integer, or D if P does not
  3100. ** exist. If the value of P is something other than an integer, then
  3101. ** zero is returned.
  3102. **
  3103. ** If F is a NULL pointer, then sqlite3_uri_parameter(F,P) returns NULL and
  3104. ** sqlite3_uri_boolean(F,P,B) returns B. If F is not a NULL pointer and
  3105. ** is not a database file pathname pointer that SQLite passed into the xOpen
  3106. ** VFS method, then the behavior of this routine is undefined and probably
  3107. ** undesirable.
  3108. */
  3109. SQLITE_API const char *sqlite3_uri_parameter(const char *zFilename, const char *zParam);
  3110. SQLITE_API int sqlite3_uri_boolean(const char *zFile, const char *zParam, int bDefault);
  3111. SQLITE_API sqlite3_int64 sqlite3_uri_int64(const char*, const char*, sqlite3_int64);
  3112. /*
  3113. ** CAPI3REF: Error Codes And Messages
  3114. **
  3115. ** ^The sqlite3_errcode() interface returns the numeric [result code] or
  3116. ** [extended result code] for the most recent failed sqlite3_* API call
  3117. ** associated with a [database connection]. If a prior API call failed
  3118. ** but the most recent API call succeeded, the return value from
  3119. ** sqlite3_errcode() is undefined. ^The sqlite3_extended_errcode()
  3120. ** interface is the same except that it always returns the
  3121. ** [extended result code] even when extended result codes are
  3122. ** disabled.
  3123. **
  3124. ** ^The sqlite3_errmsg() and sqlite3_errmsg16() return English-language
  3125. ** text that describes the error, as either UTF-8 or UTF-16 respectively.
  3126. ** ^(Memory to hold the error message string is managed internally.
  3127. ** The application does not need to worry about freeing the result.
  3128. ** However, the error string might be overwritten or deallocated by
  3129. ** subsequent calls to other SQLite interface functions.)^
  3130. **
  3131. ** When the serialized [threading mode] is in use, it might be the
  3132. ** case that a second error occurs on a separate thread in between
  3133. ** the time of the first error and the call to these interfaces.
  3134. ** When that happens, the second error will be reported since these
  3135. ** interfaces always report the most recent result. To avoid
  3136. ** this, each thread can obtain exclusive use of the [database connection] D
  3137. ** by invoking [sqlite3_mutex_enter]([sqlite3_db_mutex](D)) before beginning
  3138. ** to use D and invoking [sqlite3_mutex_leave]([sqlite3_db_mutex](D)) after
  3139. ** all calls to the interfaces listed here are completed.
  3140. **
  3141. ** If an interface fails with SQLITE_MISUSE, that means the interface
  3142. ** was invoked incorrectly by the application. In that case, the
  3143. ** error code and message may or may not be set.
  3144. */
  3145. SQLITE_API int sqlite3_errcode(sqlite3 *db);
  3146. SQLITE_API int sqlite3_extended_errcode(sqlite3 *db);
  3147. SQLITE_API const char *sqlite3_errmsg(sqlite3*);
  3148. SQLITE_API const void *sqlite3_errmsg16(sqlite3*);
  3149. /*
  3150. ** CAPI3REF: SQL Statement Object
  3151. ** KEYWORDS: {prepared statement} {prepared statements}
  3152. **
  3153. ** An instance of this object represents a single SQL statement.
  3154. ** This object is variously known as a "prepared statement" or a
  3155. ** "compiled SQL statement" or simply as a "statement".
  3156. **
  3157. ** The life of a statement object goes something like this:
  3158. **
  3159. ** <ol>
  3160. ** <li> Create the object using [sqlite3_prepare_v2()] or a related
  3161. ** function.
  3162. ** <li> Bind values to [host parameters] using the sqlite3_bind_*()
  3163. ** interfaces.
  3164. ** <li> Run the SQL by calling [sqlite3_step()] one or more times.
  3165. ** <li> Reset the statement using [sqlite3_reset()] then go back
  3166. ** to step 2. Do this zero or more times.
  3167. ** <li> Destroy the object using [sqlite3_finalize()].
  3168. ** </ol>
  3169. **
  3170. ** Refer to documentation on individual methods above for additional
  3171. ** information.
  3172. */
  3173. typedef struct sqlite3_stmt sqlite3_stmt;
  3174. /*
  3175. ** CAPI3REF: Run-time Limits
  3176. **
  3177. ** ^(This interface allows the size of various constructs to be limited
  3178. ** on a connection by connection basis. The first parameter is the
  3179. ** [database connection] whose limit is to be set or queried. The
  3180. ** second parameter is one of the [limit categories] that define a
  3181. ** class of constructs to be size limited. The third parameter is the
  3182. ** new limit for that construct.)^
  3183. **
  3184. ** ^If the new limit is a negative number, the limit is unchanged.
  3185. ** ^(For each limit category SQLITE_LIMIT_<i>NAME</i> there is a
  3186. ** [limits | hard upper bound]
  3187. ** set at compile-time by a C preprocessor macro called
  3188. ** [limits | SQLITE_MAX_<i>NAME</i>].
  3189. ** (The "_LIMIT_" in the name is changed to "_MAX_".))^
  3190. ** ^Attempts to increase a limit above its hard upper bound are
  3191. ** silently truncated to the hard upper bound.
  3192. **
  3193. ** ^Regardless of whether or not the limit was changed, the
  3194. ** [sqlite3_limit()] interface returns the prior value of the limit.
  3195. ** ^Hence, to find the current value of a limit without changing it,
  3196. ** simply invoke this interface with the third parameter set to -1.
  3197. **
  3198. ** Run-time limits are intended for use in applications that manage
  3199. ** both their own internal database and also databases that are controlled
  3200. ** by untrusted external sources. An example application might be a
  3201. ** web browser that has its own databases for storing history and
  3202. ** separate databases controlled by JavaScript applications downloaded
  3203. ** off the Internet. The internal databases can be given the
  3204. ** large, default limits. Databases managed by external sources can
  3205. ** be given much smaller limits designed to prevent a denial of service
  3206. ** attack. Developers might also want to use the [sqlite3_set_authorizer()]
  3207. ** interface to further control untrusted SQL. The size of the database
  3208. ** created by an untrusted script can be contained using the
  3209. ** [max_page_count] [PRAGMA].
  3210. **
  3211. ** New run-time limit categories may be added in future releases.
  3212. */
  3213. SQLITE_API int sqlite3_limit(sqlite3*, int id, int newVal);
  3214. /*
  3215. ** CAPI3REF: Run-Time Limit Categories
  3216. ** KEYWORDS: {limit category} {*limit categories}
  3217. **
  3218. ** These constants define various performance limits
  3219. ** that can be lowered at run-time using [sqlite3_limit()].
  3220. ** The synopsis of the meanings of the various limits is shown below.
  3221. ** Additional information is available at [limits | Limits in SQLite].
  3222. **
  3223. ** <dl>
  3224. ** [[SQLITE_LIMIT_LENGTH]] ^(<dt>SQLITE_LIMIT_LENGTH</dt>
  3225. ** <dd>The maximum size of any string or BLOB or table row, in bytes.<dd>)^
  3226. **
  3227. ** [[SQLITE_LIMIT_SQL_LENGTH]] ^(<dt>SQLITE_LIMIT_SQL_LENGTH</dt>
  3228. ** <dd>The maximum length of an SQL statement, in bytes.</dd>)^
  3229. **
  3230. ** [[SQLITE_LIMIT_COLUMN]] ^(<dt>SQLITE_LIMIT_COLUMN</dt>
  3231. ** <dd>The maximum number of columns in a table definition or in the
  3232. ** result set of a [SELECT] or the maximum number of columns in an index
  3233. ** or in an ORDER BY or GROUP BY clause.</dd>)^
  3234. **
  3235. ** [[SQLITE_LIMIT_EXPR_DEPTH]] ^(<dt>SQLITE_LIMIT_EXPR_DEPTH</dt>
  3236. ** <dd>The maximum depth of the parse tree on any expression.</dd>)^
  3237. **
  3238. ** [[SQLITE_LIMIT_COMPOUND_SELECT]] ^(<dt>SQLITE_LIMIT_COMPOUND_SELECT</dt>
  3239. ** <dd>The maximum number of terms in a compound SELECT statement.</dd>)^
  3240. **
  3241. ** [[SQLITE_LIMIT_VDBE_OP]] ^(<dt>SQLITE_LIMIT_VDBE_OP</dt>
  3242. ** <dd>The maximum number of instructions in a virtual machine program
  3243. ** used to implement an SQL statement. This limit is not currently
  3244. ** enforced, though that might be added in some future release of
  3245. ** SQLite.</dd>)^
  3246. **
  3247. ** [[SQLITE_LIMIT_FUNCTION_ARG]] ^(<dt>SQLITE_LIMIT_FUNCTION_ARG</dt>
  3248. ** <dd>The maximum number of arguments on a function.</dd>)^
  3249. **
  3250. ** [[SQLITE_LIMIT_ATTACHED]] ^(<dt>SQLITE_LIMIT_ATTACHED</dt>
  3251. ** <dd>The maximum number of [ATTACH | attached databases].)^</dd>
  3252. **
  3253. ** [[SQLITE_LIMIT_LIKE_PATTERN_LENGTH]]
  3254. ** ^(<dt>SQLITE_LIMIT_LIKE_PATTERN_LENGTH</dt>
  3255. ** <dd>The maximum length of the pattern argument to the [LIKE] or
  3256. ** [GLOB] operators.</dd>)^
  3257. **
  3258. ** [[SQLITE_LIMIT_VARIABLE_NUMBER]]
  3259. ** ^(<dt>SQLITE_LIMIT_VARIABLE_NUMBER</dt>
  3260. ** <dd>The maximum index number of any [parameter] in an SQL statement.)^
  3261. **
  3262. ** [[SQLITE_LIMIT_TRIGGER_DEPTH]] ^(<dt>SQLITE_LIMIT_TRIGGER_DEPTH</dt>
  3263. ** <dd>The maximum depth of recursion for triggers.</dd>)^
  3264. ** </dl>
  3265. */
  3266. #define SQLITE_LIMIT_LENGTH 0
  3267. #define SQLITE_LIMIT_SQL_LENGTH 1
  3268. #define SQLITE_LIMIT_COLUMN 2
  3269. #define SQLITE_LIMIT_EXPR_DEPTH 3
  3270. #define SQLITE_LIMIT_COMPOUND_SELECT 4
  3271. #define SQLITE_LIMIT_VDBE_OP 5
  3272. #define SQLITE_LIMIT_FUNCTION_ARG 6
  3273. #define SQLITE_LIMIT_ATTACHED 7
  3274. #define SQLITE_LIMIT_LIKE_PATTERN_LENGTH 8
  3275. #define SQLITE_LIMIT_VARIABLE_NUMBER 9
  3276. #define SQLITE_LIMIT_TRIGGER_DEPTH 10
  3277. /*
  3278. ** CAPI3REF: Compiling An SQL Statement
  3279. ** KEYWORDS: {SQL statement compiler}
  3280. **
  3281. ** To execute an SQL query, it must first be compiled into a byte-code
  3282. ** program using one of these routines.
  3283. **
  3284. ** The first argument, "db", is a [database connection] obtained from a
  3285. ** prior successful call to [sqlite3_open()], [sqlite3_open_v2()] or
  3286. ** [sqlite3_open16()]. The database connection must not have been closed.
  3287. **
  3288. ** The second argument, "zSql", is the statement to be compiled, encoded
  3289. ** as either UTF-8 or UTF-16. The sqlite3_prepare() and sqlite3_prepare_v2()
  3290. ** interfaces use UTF-8, and sqlite3_prepare16() and sqlite3_prepare16_v2()
  3291. ** use UTF-16.
  3292. **
  3293. ** ^If the nByte argument is less than zero, then zSql is read up to the
  3294. ** first zero terminator. ^If nByte is non-negative, then it is the maximum
  3295. ** number of bytes read from zSql. ^When nByte is non-negative, the
  3296. ** zSql string ends at either the first '\000' or '\u0000' character or
  3297. ** the nByte-th byte, whichever comes first. If the caller knows
  3298. ** that the supplied string is nul-terminated, then there is a small
  3299. ** performance advantage to be gained by passing an nByte parameter that
  3300. ** is equal to the number of bytes in the input string <i>including</i>
  3301. ** the nul-terminator bytes as this saves SQLite from having to
  3302. ** make a copy of the input string.
  3303. **
  3304. ** ^If pzTail is not NULL then *pzTail is made to point to the first byte
  3305. ** past the end of the first SQL statement in zSql. These routines only
  3306. ** compile the first statement in zSql, so *pzTail is left pointing to
  3307. ** what remains uncompiled.
  3308. **
  3309. ** ^*ppStmt is left pointing to a compiled [prepared statement] that can be
  3310. ** executed using [sqlite3_step()]. ^If there is an error, *ppStmt is set
  3311. ** to NULL. ^If the input text contains no SQL (if the input is an empty
  3312. ** string or a comment) then *ppStmt is set to NULL.
  3313. ** The calling procedure is responsible for deleting the compiled
  3314. ** SQL statement using [sqlite3_finalize()] after it has finished with it.
  3315. ** ppStmt may not be NULL.
  3316. **
  3317. ** ^On success, the sqlite3_prepare() family of routines return [SQLITE_OK];
  3318. ** otherwise an [error code] is returned.
  3319. **
  3320. ** The sqlite3_prepare_v2() and sqlite3_prepare16_v2() interfaces are
  3321. ** recommended for all new programs. The two older interfaces are retained
  3322. ** for backwards compatibility, but their use is discouraged.
  3323. ** ^In the "v2" interfaces, the prepared statement
  3324. ** that is returned (the [sqlite3_stmt] object) contains a copy of the
  3325. ** original SQL text. This causes the [sqlite3_step()] interface to
  3326. ** behave differently in three ways:
  3327. **
  3328. ** <ol>
  3329. ** <li>
  3330. ** ^If the database schema changes, instead of returning [SQLITE_SCHEMA] as it
  3331. ** always used to do, [sqlite3_step()] will automatically recompile the SQL
  3332. ** statement and try to run it again.
  3333. ** </li>
  3334. **
  3335. ** <li>
  3336. ** ^When an error occurs, [sqlite3_step()] will return one of the detailed
  3337. ** [error codes] or [extended error codes]. ^The legacy behavior was that
  3338. ** [sqlite3_step()] would only return a generic [SQLITE_ERROR] result code
  3339. ** and the application would have to make a second call to [sqlite3_reset()]
  3340. ** in order to find the underlying cause of the problem. With the "v2" prepare
  3341. ** interfaces, the underlying reason for the error is returned immediately.
  3342. ** </li>
  3343. **
  3344. ** <li>
  3345. ** ^If the specific value bound to [parameter | host parameter] in the
  3346. ** WHERE clause might influence the choice of query plan for a statement,
  3347. ** then the statement will be automatically recompiled, as if there had been
  3348. ** a schema change, on the first [sqlite3_step()] call following any change
  3349. ** to the [sqlite3_bind_text | bindings] of that [parameter].
  3350. ** ^The specific value of WHERE-clause [parameter] might influence the
  3351. ** choice of query plan if the parameter is the left-hand side of a [LIKE]
  3352. ** or [GLOB] operator or if the parameter is compared to an indexed column
  3353. ** and the [SQLITE_ENABLE_STAT3] compile-time option is enabled.
  3354. ** the
  3355. ** </li>
  3356. ** </ol>
  3357. */
  3358. SQLITE_API int sqlite3_prepare(
  3359. sqlite3 *db, /* Database handle */
  3360. const char *zSql, /* SQL statement, UTF-8 encoded */
  3361. int nByte, /* Maximum length of zSql in bytes. */
  3362. sqlite3_stmt **ppStmt, /* OUT: Statement handle */
  3363. const char **pzTail /* OUT: Pointer to unused portion of zSql */
  3364. );
  3365. SQLITE_API int sqlite3_prepare_v2(
  3366. sqlite3 *db, /* Database handle */
  3367. const char *zSql, /* SQL statement, UTF-8 encoded */
  3368. int nByte, /* Maximum length of zSql in bytes. */
  3369. sqlite3_stmt **ppStmt, /* OUT: Statement handle */
  3370. const char **pzTail /* OUT: Pointer to unused portion of zSql */
  3371. );
  3372. SQLITE_API int sqlite3_prepare16(
  3373. sqlite3 *db, /* Database handle */
  3374. const void *zSql, /* SQL statement, UTF-16 encoded */
  3375. int nByte, /* Maximum length of zSql in bytes. */
  3376. sqlite3_stmt **ppStmt, /* OUT: Statement handle */
  3377. const void **pzTail /* OUT: Pointer to unused portion of zSql */
  3378. );
  3379. SQLITE_API int sqlite3_prepare16_v2(
  3380. sqlite3 *db, /* Database handle */
  3381. const void *zSql, /* SQL statement, UTF-16 encoded */
  3382. int nByte, /* Maximum length of zSql in bytes. */
  3383. sqlite3_stmt **ppStmt, /* OUT: Statement handle */
  3384. const void **pzTail /* OUT: Pointer to unused portion of zSql */
  3385. );
  3386. /*
  3387. ** CAPI3REF: Retrieving Statement SQL
  3388. **
  3389. ** ^This interface can be used to retrieve a saved copy of the original
  3390. ** SQL text used to create a [prepared statement] if that statement was
  3391. ** compiled using either [sqlite3_prepare_v2()] or [sqlite3_prepare16_v2()].
  3392. */
  3393. SQLITE_API const char *sqlite3_sql(sqlite3_stmt *pStmt);
  3394. /*
  3395. ** CAPI3REF: Determine If An SQL Statement Writes The Database
  3396. **
  3397. ** ^The sqlite3_stmt_readonly(X) interface returns true (non-zero) if
  3398. ** and only if the [prepared statement] X makes no direct changes to
  3399. ** the content of the database file.
  3400. **
  3401. ** Note that [application-defined SQL functions] or
  3402. ** [virtual tables] might change the database indirectly as a side effect.
  3403. ** ^(For example, if an application defines a function "eval()" that
  3404. ** calls [sqlite3_exec()], then the following SQL statement would
  3405. ** change the database file through side-effects:
  3406. **
  3407. ** <blockquote><pre>
  3408. ** SELECT eval('DELETE FROM t1') FROM t2;
  3409. ** </pre></blockquote>
  3410. **
  3411. ** But because the [SELECT] statement does not change the database file
  3412. ** directly, sqlite3_stmt_readonly() would still return true.)^
  3413. **
  3414. ** ^Transaction control statements such as [BEGIN], [COMMIT], [ROLLBACK],
  3415. ** [SAVEPOINT], and [RELEASE] cause sqlite3_stmt_readonly() to return true,
  3416. ** since the statements themselves do not actually modify the database but
  3417. ** rather they control the timing of when other statements modify the
  3418. ** database. ^The [ATTACH] and [DETACH] statements also cause
  3419. ** sqlite3_stmt_readonly() to return true since, while those statements
  3420. ** change the configuration of a database connection, they do not make
  3421. ** changes to the content of the database files on disk.
  3422. */
  3423. SQLITE_API int sqlite3_stmt_readonly(sqlite3_stmt *pStmt);
  3424. /*
  3425. ** CAPI3REF: Determine If A Prepared Statement Has Been Reset
  3426. **
  3427. ** ^The sqlite3_stmt_busy(S) interface returns true (non-zero) if the
  3428. ** [prepared statement] S has been stepped at least once using
  3429. ** [sqlite3_step(S)] but has not run to completion and/or has not
  3430. ** been reset using [sqlite3_reset(S)]. ^The sqlite3_stmt_busy(S)
  3431. ** interface returns false if S is a NULL pointer. If S is not a
  3432. ** NULL pointer and is not a pointer to a valid [prepared statement]
  3433. ** object, then the behavior is undefined and probably undesirable.
  3434. **
  3435. ** This interface can be used in combination [sqlite3_next_stmt()]
  3436. ** to locate all prepared statements associated with a database
  3437. ** connection that are in need of being reset. This can be used,
  3438. ** for example, in diagnostic routines to search for prepared
  3439. ** statements that are holding a transaction open.
  3440. */
  3441. SQLITE_API int sqlite3_stmt_busy(sqlite3_stmt*);
  3442. /*
  3443. ** CAPI3REF: Dynamically Typed Value Object
  3444. ** KEYWORDS: {protected sqlite3_value} {unprotected sqlite3_value}
  3445. **
  3446. ** SQLite uses the sqlite3_value object to represent all values
  3447. ** that can be stored in a database table. SQLite uses dynamic typing
  3448. ** for the values it stores. ^Values stored in sqlite3_value objects
  3449. ** can be integers, floating point values, strings, BLOBs, or NULL.
  3450. **
  3451. ** An sqlite3_value object may be either "protected" or "unprotected".
  3452. ** Some interfaces require a protected sqlite3_value. Other interfaces
  3453. ** will accept either a protected or an unprotected sqlite3_value.
  3454. ** Every interface that accepts sqlite3_value arguments specifies
  3455. ** whether or not it requires a protected sqlite3_value.
  3456. **
  3457. ** The terms "protected" and "unprotected" refer to whether or not
  3458. ** a mutex is held. An internal mutex is held for a protected
  3459. ** sqlite3_value object but no mutex is held for an unprotected
  3460. ** sqlite3_value object. If SQLite is compiled to be single-threaded
  3461. ** (with [SQLITE_THREADSAFE=0] and with [sqlite3_threadsafe()] returning 0)
  3462. ** or if SQLite is run in one of reduced mutex modes
  3463. ** [SQLITE_CONFIG_SINGLETHREAD] or [SQLITE_CONFIG_MULTITHREAD]
  3464. ** then there is no distinction between protected and unprotected
  3465. ** sqlite3_value objects and they can be used interchangeably. However,
  3466. ** for maximum code portability it is recommended that applications
  3467. ** still make the distinction between protected and unprotected
  3468. ** sqlite3_value objects even when not strictly required.
  3469. **
  3470. ** ^The sqlite3_value objects that are passed as parameters into the
  3471. ** implementation of [application-defined SQL functions] are protected.
  3472. ** ^The sqlite3_value object returned by
  3473. ** [sqlite3_column_value()] is unprotected.
  3474. ** Unprotected sqlite3_value objects may only be used with
  3475. ** [sqlite3_result_value()] and [sqlite3_bind_value()].
  3476. ** The [sqlite3_value_blob | sqlite3_value_type()] family of
  3477. ** interfaces require protected sqlite3_value objects.
  3478. */
  3479. typedef struct Mem sqlite3_value;
  3480. /*
  3481. ** CAPI3REF: SQL Function Context Object
  3482. **
  3483. ** The context in which an SQL function executes is stored in an
  3484. ** sqlite3_context object. ^A pointer to an sqlite3_context object
  3485. ** is always first parameter to [application-defined SQL functions].
  3486. ** The application-defined SQL function implementation will pass this
  3487. ** pointer through into calls to [sqlite3_result_int | sqlite3_result()],
  3488. ** [sqlite3_aggregate_context()], [sqlite3_user_data()],
  3489. ** [sqlite3_context_db_handle()], [sqlite3_get_auxdata()],
  3490. ** and/or [sqlite3_set_auxdata()].
  3491. */
  3492. typedef struct sqlite3_context sqlite3_context;
  3493. /*
  3494. ** CAPI3REF: Binding Values To Prepared Statements
  3495. ** KEYWORDS: {host parameter} {host parameters} {host parameter name}
  3496. ** KEYWORDS: {SQL parameter} {SQL parameters} {parameter binding}
  3497. **
  3498. ** ^(In the SQL statement text input to [sqlite3_prepare_v2()] and its variants,
  3499. ** literals may be replaced by a [parameter] that matches one of following
  3500. ** templates:
  3501. **
  3502. ** <ul>
  3503. ** <li> ?
  3504. ** <li> ?NNN
  3505. ** <li> :VVV
  3506. ** <li> @VVV
  3507. ** <li> $VVV
  3508. ** </ul>
  3509. **
  3510. ** In the templates above, NNN represents an integer literal,
  3511. ** and VVV represents an alphanumeric identifier.)^ ^The values of these
  3512. ** parameters (also called "host parameter names" or "SQL parameters")
  3513. ** can be set using the sqlite3_bind_*() routines defined here.
  3514. **
  3515. ** ^The first argument to the sqlite3_bind_*() routines is always
  3516. ** a pointer to the [sqlite3_stmt] object returned from
  3517. ** [sqlite3_prepare_v2()] or its variants.
  3518. **
  3519. ** ^The second argument is the index of the SQL parameter to be set.
  3520. ** ^The leftmost SQL parameter has an index of 1. ^When the same named
  3521. ** SQL parameter is used more than once, second and subsequent
  3522. ** occurrences have the same index as the first occurrence.
  3523. ** ^The index for named parameters can be looked up using the
  3524. ** [sqlite3_bind_parameter_index()] API if desired. ^The index
  3525. ** for "?NNN" parameters is the value of NNN.
  3526. ** ^The NNN value must be between 1 and the [sqlite3_limit()]
  3527. ** parameter [SQLITE_LIMIT_VARIABLE_NUMBER] (default value: 999).
  3528. **
  3529. ** ^The third argument is the value to bind to the parameter.
  3530. **
  3531. ** ^(In those routines that have a fourth argument, its value is the
  3532. ** number of bytes in the parameter. To be clear: the value is the
  3533. ** number of <u>bytes</u> in the value, not the number of characters.)^
  3534. ** ^If the fourth parameter is negative, the length of the string is
  3535. ** the number of bytes up to the first zero terminator.
  3536. ** If a non-negative fourth parameter is provided to sqlite3_bind_text()
  3537. ** or sqlite3_bind_text16() then that parameter must be the byte offset
  3538. ** where the NUL terminator would occur assuming the string were NUL
  3539. ** terminated. If any NUL characters occur at byte offsets less than
  3540. ** the value of the fourth parameter then the resulting string value will
  3541. ** contain embedded NULs. The result of expressions involving strings
  3542. ** with embedded NULs is undefined.
  3543. **
  3544. ** ^The fifth argument to sqlite3_bind_blob(), sqlite3_bind_text(), and
  3545. ** sqlite3_bind_text16() is a destructor used to dispose of the BLOB or
  3546. ** string after SQLite has finished with it. ^The destructor is called
  3547. ** to dispose of the BLOB or string even if the call to sqlite3_bind_blob(),
  3548. ** sqlite3_bind_text(), or sqlite3_bind_text16() fails.
  3549. ** ^If the fifth argument is
  3550. ** the special value [SQLITE_STATIC], then SQLite assumes that the
  3551. ** information is in static, unmanaged space and does not need to be freed.
  3552. ** ^If the fifth argument has the value [SQLITE_TRANSIENT], then
  3553. ** SQLite makes its own private copy of the data immediately, before
  3554. ** the sqlite3_bind_*() routine returns.
  3555. **
  3556. ** ^The sqlite3_bind_zeroblob() routine binds a BLOB of length N that
  3557. ** is filled with zeroes. ^A zeroblob uses a fixed amount of memory
  3558. ** (just an integer to hold its size) while it is being processed.
  3559. ** Zeroblobs are intended to serve as placeholders for BLOBs whose
  3560. ** content is later written using
  3561. ** [sqlite3_blob_open | incremental BLOB I/O] routines.
  3562. ** ^A negative value for the zeroblob results in a zero-length BLOB.
  3563. **
  3564. ** ^If any of the sqlite3_bind_*() routines are called with a NULL pointer
  3565. ** for the [prepared statement] or with a prepared statement for which
  3566. ** [sqlite3_step()] has been called more recently than [sqlite3_reset()],
  3567. ** then the call will return [SQLITE_MISUSE]. If any sqlite3_bind_()
  3568. ** routine is passed a [prepared statement] that has been finalized, the
  3569. ** result is undefined and probably harmful.
  3570. **
  3571. ** ^Bindings are not cleared by the [sqlite3_reset()] routine.
  3572. ** ^Unbound parameters are interpreted as NULL.
  3573. **
  3574. ** ^The sqlite3_bind_* routines return [SQLITE_OK] on success or an
  3575. ** [error code] if anything goes wrong.
  3576. ** ^[SQLITE_RANGE] is returned if the parameter
  3577. ** index is out of range. ^[SQLITE_NOMEM] is returned if malloc() fails.
  3578. **
  3579. ** See also: [sqlite3_bind_parameter_count()],
  3580. ** [sqlite3_bind_parameter_name()], and [sqlite3_bind_parameter_index()].
  3581. */
  3582. SQLITE_API int sqlite3_bind_blob(sqlite3_stmt*, int, const void*, int n, void(*)(void*));
  3583. SQLITE_API int sqlite3_bind_double(sqlite3_stmt*, int, double);
  3584. SQLITE_API int sqlite3_bind_int(sqlite3_stmt*, int, int);
  3585. SQLITE_API int sqlite3_bind_int64(sqlite3_stmt*, int, sqlite3_int64);
  3586. SQLITE_API int sqlite3_bind_null(sqlite3_stmt*, int);
  3587. SQLITE_API int sqlite3_bind_text(sqlite3_stmt*, int, const char*, int n, void(*)(void*));
  3588. SQLITE_API int sqlite3_bind_text16(sqlite3_stmt*, int, const void*, int, void(*)(void*));
  3589. SQLITE_API int sqlite3_bind_value(sqlite3_stmt*, int, const sqlite3_value*);
  3590. SQLITE_API int sqlite3_bind_zeroblob(sqlite3_stmt*, int, int n);
  3591. /*
  3592. ** CAPI3REF: Number Of SQL Parameters
  3593. **
  3594. ** ^This routine can be used to find the number of [SQL parameters]
  3595. ** in a [prepared statement]. SQL parameters are tokens of the
  3596. ** form "?", "?NNN", ":AAA", "$AAA", or "@AAA" that serve as
  3597. ** placeholders for values that are [sqlite3_bind_blob | bound]
  3598. ** to the parameters at a later time.
  3599. **
  3600. ** ^(This routine actually returns the index of the largest (rightmost)
  3601. ** parameter. For all forms except ?NNN, this will correspond to the
  3602. ** number of unique parameters. If parameters of the ?NNN form are used,
  3603. ** there may be gaps in the list.)^
  3604. **
  3605. ** See also: [sqlite3_bind_blob|sqlite3_bind()],
  3606. ** [sqlite3_bind_parameter_name()], and
  3607. ** [sqlite3_bind_parameter_index()].
  3608. */
  3609. SQLITE_API int sqlite3_bind_parameter_count(sqlite3_stmt*);
  3610. /*
  3611. ** CAPI3REF: Name Of A Host Parameter
  3612. **
  3613. ** ^The sqlite3_bind_parameter_name(P,N) interface returns
  3614. ** the name of the N-th [SQL parameter] in the [prepared statement] P.
  3615. ** ^(SQL parameters of the form "?NNN" or ":AAA" or "@AAA" or "$AAA"
  3616. ** have a name which is the string "?NNN" or ":AAA" or "@AAA" or "$AAA"
  3617. ** respectively.
  3618. ** In other words, the initial ":" or "$" or "@" or "?"
  3619. ** is included as part of the name.)^
  3620. ** ^Parameters of the form "?" without a following integer have no name
  3621. ** and are referred to as "nameless" or "anonymous parameters".
  3622. **
  3623. ** ^The first host parameter has an index of 1, not 0.
  3624. **
  3625. ** ^If the value N is out of range or if the N-th parameter is
  3626. ** nameless, then NULL is returned. ^The returned string is
  3627. ** always in UTF-8 encoding even if the named parameter was
  3628. ** originally specified as UTF-16 in [sqlite3_prepare16()] or
  3629. ** [sqlite3_prepare16_v2()].
  3630. **
  3631. ** See also: [sqlite3_bind_blob|sqlite3_bind()],
  3632. ** [sqlite3_bind_parameter_count()], and
  3633. ** [sqlite3_bind_parameter_index()].
  3634. */
  3635. SQLITE_API const char *sqlite3_bind_parameter_name(sqlite3_stmt*, int);
  3636. /*
  3637. ** CAPI3REF: Index Of A Parameter With A Given Name
  3638. **
  3639. ** ^Return the index of an SQL parameter given its name. ^The
  3640. ** index value returned is suitable for use as the second
  3641. ** parameter to [sqlite3_bind_blob|sqlite3_bind()]. ^A zero
  3642. ** is returned if no matching parameter is found. ^The parameter
  3643. ** name must be given in UTF-8 even if the original statement
  3644. ** was prepared from UTF-16 text using [sqlite3_prepare16_v2()].
  3645. **
  3646. ** See also: [sqlite3_bind_blob|sqlite3_bind()],
  3647. ** [sqlite3_bind_parameter_count()], and
  3648. ** [sqlite3_bind_parameter_index()].
  3649. */
  3650. SQLITE_API int sqlite3_bind_parameter_index(sqlite3_stmt*, const char *zName);
  3651. /*
  3652. ** CAPI3REF: Reset All Bindings On A Prepared Statement
  3653. **
  3654. ** ^Contrary to the intuition of many, [sqlite3_reset()] does not reset
  3655. ** the [sqlite3_bind_blob | bindings] on a [prepared statement].
  3656. ** ^Use this routine to reset all host parameters to NULL.
  3657. */
  3658. SQLITE_API int sqlite3_clear_bindings(sqlite3_stmt*);
  3659. /*
  3660. ** CAPI3REF: Number Of Columns In A Result Set
  3661. **
  3662. ** ^Return the number of columns in the result set returned by the
  3663. ** [prepared statement]. ^This routine returns 0 if pStmt is an SQL
  3664. ** statement that does not return data (for example an [UPDATE]).
  3665. **
  3666. ** See also: [sqlite3_data_count()]
  3667. */
  3668. SQLITE_API int sqlite3_column_count(sqlite3_stmt *pStmt);
  3669. /*
  3670. ** CAPI3REF: Column Names In A Result Set
  3671. **
  3672. ** ^These routines return the name assigned to a particular column
  3673. ** in the result set of a [SELECT] statement. ^The sqlite3_column_name()
  3674. ** interface returns a pointer to a zero-terminated UTF-8 string
  3675. ** and sqlite3_column_name16() returns a pointer to a zero-terminated
  3676. ** UTF-16 string. ^The first parameter is the [prepared statement]
  3677. ** that implements the [SELECT] statement. ^The second parameter is the
  3678. ** column number. ^The leftmost column is number 0.
  3679. **
  3680. ** ^The returned string pointer is valid until either the [prepared statement]
  3681. ** is destroyed by [sqlite3_finalize()] or until the statement is automatically
  3682. ** reprepared by the first call to [sqlite3_step()] for a particular run
  3683. ** or until the next call to
  3684. ** sqlite3_column_name() or sqlite3_column_name16() on the same column.
  3685. **
  3686. ** ^If sqlite3_malloc() fails during the processing of either routine
  3687. ** (for example during a conversion from UTF-8 to UTF-16) then a
  3688. ** NULL pointer is returned.
  3689. **
  3690. ** ^The name of a result column is the value of the "AS" clause for
  3691. ** that column, if there is an AS clause. If there is no AS clause
  3692. ** then the name of the column is unspecified and may change from
  3693. ** one release of SQLite to the next.
  3694. */
  3695. SQLITE_API const char *sqlite3_column_name(sqlite3_stmt*, int N);
  3696. SQLITE_API const void *sqlite3_column_name16(sqlite3_stmt*, int N);
  3697. /*
  3698. ** CAPI3REF: Source Of Data In A Query Result
  3699. **
  3700. ** ^These routines provide a means to determine the database, table, and
  3701. ** table column that is the origin of a particular result column in
  3702. ** [SELECT] statement.
  3703. ** ^The name of the database or table or column can be returned as
  3704. ** either a UTF-8 or UTF-16 string. ^The _database_ routines return
  3705. ** the database name, the _table_ routines return the table name, and
  3706. ** the origin_ routines return the column name.
  3707. ** ^The returned string is valid until the [prepared statement] is destroyed
  3708. ** using [sqlite3_finalize()] or until the statement is automatically
  3709. ** reprepared by the first call to [sqlite3_step()] for a particular run
  3710. ** or until the same information is requested
  3711. ** again in a different encoding.
  3712. **
  3713. ** ^The names returned are the original un-aliased names of the
  3714. ** database, table, and column.
  3715. **
  3716. ** ^The first argument to these interfaces is a [prepared statement].
  3717. ** ^These functions return information about the Nth result column returned by
  3718. ** the statement, where N is the second function argument.
  3719. ** ^The left-most column is column 0 for these routines.
  3720. **
  3721. ** ^If the Nth column returned by the statement is an expression or
  3722. ** subquery and is not a column value, then all of these functions return
  3723. ** NULL. ^These routine might also return NULL if a memory allocation error
  3724. ** occurs. ^Otherwise, they return the name of the attached database, table,
  3725. ** or column that query result column was extracted from.
  3726. **
  3727. ** ^As with all other SQLite APIs, those whose names end with "16" return
  3728. ** UTF-16 encoded strings and the other functions return UTF-8.
  3729. **
  3730. ** ^These APIs are only available if the library was compiled with the
  3731. ** [SQLITE_ENABLE_COLUMN_METADATA] C-preprocessor symbol.
  3732. **
  3733. ** If two or more threads call one or more of these routines against the same
  3734. ** prepared statement and column at the same time then the results are
  3735. ** undefined.
  3736. **
  3737. ** If two or more threads call one or more
  3738. ** [sqlite3_column_database_name | column metadata interfaces]
  3739. ** for the same [prepared statement] and result column
  3740. ** at the same time then the results are undefined.
  3741. */
  3742. SQLITE_API const char *sqlite3_column_database_name(sqlite3_stmt*,int);
  3743. SQLITE_API const void *sqlite3_column_database_name16(sqlite3_stmt*,int);
  3744. SQLITE_API const char *sqlite3_column_table_name(sqlite3_stmt*,int);
  3745. SQLITE_API const void *sqlite3_column_table_name16(sqlite3_stmt*,int);
  3746. SQLITE_API const char *sqlite3_column_origin_name(sqlite3_stmt*,int);
  3747. SQLITE_API const void *sqlite3_column_origin_name16(sqlite3_stmt*,int);
  3748. /*
  3749. ** CAPI3REF: Declared Datatype Of A Query Result
  3750. **
  3751. ** ^(The first parameter is a [prepared statement].
  3752. ** If this statement is a [SELECT] statement and the Nth column of the
  3753. ** returned result set of that [SELECT] is a table column (not an
  3754. ** expression or subquery) then the declared type of the table
  3755. ** column is returned.)^ ^If the Nth column of the result set is an
  3756. ** expression or subquery, then a NULL pointer is returned.
  3757. ** ^The returned string is always UTF-8 encoded.
  3758. **
  3759. ** ^(For example, given the database schema:
  3760. **
  3761. ** CREATE TABLE t1(c1 VARIANT);
  3762. **
  3763. ** and the following statement to be compiled:
  3764. **
  3765. ** SELECT c1 + 1, c1 FROM t1;
  3766. **
  3767. ** this routine would return the string "VARIANT" for the second result
  3768. ** column (i==1), and a NULL pointer for the first result column (i==0).)^
  3769. **
  3770. ** ^SQLite uses dynamic run-time typing. ^So just because a column
  3771. ** is declared to contain a particular type does not mean that the
  3772. ** data stored in that column is of the declared type. SQLite is
  3773. ** strongly typed, but the typing is dynamic not static. ^Type
  3774. ** is associated with individual values, not with the containers
  3775. ** used to hold those values.
  3776. */
  3777. SQLITE_API const char *sqlite3_column_decltype(sqlite3_stmt*,int);
  3778. SQLITE_API const void *sqlite3_column_decltype16(sqlite3_stmt*,int);
  3779. /*
  3780. ** CAPI3REF: Evaluate An SQL Statement
  3781. **
  3782. ** After a [prepared statement] has been prepared using either
  3783. ** [sqlite3_prepare_v2()] or [sqlite3_prepare16_v2()] or one of the legacy
  3784. ** interfaces [sqlite3_prepare()] or [sqlite3_prepare16()], this function
  3785. ** must be called one or more times to evaluate the statement.
  3786. **
  3787. ** The details of the behavior of the sqlite3_step() interface depend
  3788. ** on whether the statement was prepared using the newer "v2" interface
  3789. ** [sqlite3_prepare_v2()] and [sqlite3_prepare16_v2()] or the older legacy
  3790. ** interface [sqlite3_prepare()] and [sqlite3_prepare16()]. The use of the
  3791. ** new "v2" interface is recommended for new applications but the legacy
  3792. ** interface will continue to be supported.
  3793. **
  3794. ** ^In the legacy interface, the return value will be either [SQLITE_BUSY],
  3795. ** [SQLITE_DONE], [SQLITE_ROW], [SQLITE_ERROR], or [SQLITE_MISUSE].
  3796. ** ^With the "v2" interface, any of the other [result codes] or
  3797. ** [extended result codes] might be returned as well.
  3798. **
  3799. ** ^[SQLITE_BUSY] means that the database engine was unable to acquire the
  3800. ** database locks it needs to do its job. ^If the statement is a [COMMIT]
  3801. ** or occurs outside of an explicit transaction, then you can retry the
  3802. ** statement. If the statement is not a [COMMIT] and occurs within an
  3803. ** explicit transaction then you should rollback the transaction before
  3804. ** continuing.
  3805. **
  3806. ** ^[SQLITE_DONE] means that the statement has finished executing
  3807. ** successfully. sqlite3_step() should not be called again on this virtual
  3808. ** machine without first calling [sqlite3_reset()] to reset the virtual
  3809. ** machine back to its initial state.
  3810. **
  3811. ** ^If the SQL statement being executed returns any data, then [SQLITE_ROW]
  3812. ** is returned each time a new row of data is ready for processing by the
  3813. ** caller. The values may be accessed using the [column access functions].
  3814. ** sqlite3_step() is called again to retrieve the next row of data.
  3815. **
  3816. ** ^[SQLITE_ERROR] means that a run-time error (such as a constraint
  3817. ** violation) has occurred. sqlite3_step() should not be called again on
  3818. ** the VM. More information may be found by calling [sqlite3_errmsg()].
  3819. ** ^With the legacy interface, a more specific error code (for example,
  3820. ** [SQLITE_INTERRUPT], [SQLITE_SCHEMA], [SQLITE_CORRUPT], and so forth)
  3821. ** can be obtained by calling [sqlite3_reset()] on the
  3822. ** [prepared statement]. ^In the "v2" interface,
  3823. ** the more specific error code is returned directly by sqlite3_step().
  3824. **
  3825. ** [SQLITE_MISUSE] means that the this routine was called inappropriately.
  3826. ** Perhaps it was called on a [prepared statement] that has
  3827. ** already been [sqlite3_finalize | finalized] or on one that had
  3828. ** previously returned [SQLITE_ERROR] or [SQLITE_DONE]. Or it could
  3829. ** be the case that the same database connection is being used by two or
  3830. ** more threads at the same moment in time.
  3831. **
  3832. ** For all versions of SQLite up to and including 3.6.23.1, a call to
  3833. ** [sqlite3_reset()] was required after sqlite3_step() returned anything
  3834. ** other than [SQLITE_ROW] before any subsequent invocation of
  3835. ** sqlite3_step(). Failure to reset the prepared statement using
  3836. ** [sqlite3_reset()] would result in an [SQLITE_MISUSE] return from
  3837. ** sqlite3_step(). But after version 3.6.23.1, sqlite3_step() began
  3838. ** calling [sqlite3_reset()] automatically in this circumstance rather
  3839. ** than returning [SQLITE_MISUSE]. This is not considered a compatibility
  3840. ** break because any application that ever receives an SQLITE_MISUSE error
  3841. ** is broken by definition. The [SQLITE_OMIT_AUTORESET] compile-time option
  3842. ** can be used to restore the legacy behavior.
  3843. **
  3844. ** <b>Goofy Interface Alert:</b> In the legacy interface, the sqlite3_step()
  3845. ** API always returns a generic error code, [SQLITE_ERROR], following any
  3846. ** error other than [SQLITE_BUSY] and [SQLITE_MISUSE]. You must call
  3847. ** [sqlite3_reset()] or [sqlite3_finalize()] in order to find one of the
  3848. ** specific [error codes] that better describes the error.
  3849. ** We admit that this is a goofy design. The problem has been fixed
  3850. ** with the "v2" interface. If you prepare all of your SQL statements
  3851. ** using either [sqlite3_prepare_v2()] or [sqlite3_prepare16_v2()] instead
  3852. ** of the legacy [sqlite3_prepare()] and [sqlite3_prepare16()] interfaces,
  3853. ** then the more specific [error codes] are returned directly
  3854. ** by sqlite3_step(). The use of the "v2" interface is recommended.
  3855. */
  3856. SQLITE_API int sqlite3_step(sqlite3_stmt*);
  3857. /*
  3858. ** CAPI3REF: Number of columns in a result set
  3859. **
  3860. ** ^The sqlite3_data_count(P) interface returns the number of columns in the
  3861. ** current row of the result set of [prepared statement] P.
  3862. ** ^If prepared statement P does not have results ready to return
  3863. ** (via calls to the [sqlite3_column_int | sqlite3_column_*()] of
  3864. ** interfaces) then sqlite3_data_count(P) returns 0.
  3865. ** ^The sqlite3_data_count(P) routine also returns 0 if P is a NULL pointer.
  3866. ** ^The sqlite3_data_count(P) routine returns 0 if the previous call to
  3867. ** [sqlite3_step](P) returned [SQLITE_DONE]. ^The sqlite3_data_count(P)
  3868. ** will return non-zero if previous call to [sqlite3_step](P) returned
  3869. ** [SQLITE_ROW], except in the case of the [PRAGMA incremental_vacuum]
  3870. ** where it always returns zero since each step of that multi-step
  3871. ** pragma returns 0 columns of data.
  3872. **
  3873. ** See also: [sqlite3_column_count()]
  3874. */
  3875. SQLITE_API int sqlite3_data_count(sqlite3_stmt *pStmt);
  3876. /*
  3877. ** CAPI3REF: Fundamental Datatypes
  3878. ** KEYWORDS: SQLITE_TEXT
  3879. **
  3880. ** ^(Every value in SQLite has one of five fundamental datatypes:
  3881. **
  3882. ** <ul>
  3883. ** <li> 64-bit signed integer
  3884. ** <li> 64-bit IEEE floating point number
  3885. ** <li> string
  3886. ** <li> BLOB
  3887. ** <li> NULL
  3888. ** </ul>)^
  3889. **
  3890. ** These constants are codes for each of those types.
  3891. **
  3892. ** Note that the SQLITE_TEXT constant was also used in SQLite version 2
  3893. ** for a completely different meaning. Software that links against both
  3894. ** SQLite version 2 and SQLite version 3 should use SQLITE3_TEXT, not
  3895. ** SQLITE_TEXT.
  3896. */
  3897. #define SQLITE_INTEGER 1
  3898. #define SQLITE_FLOAT 2
  3899. #define SQLITE_BLOB 4
  3900. #define SQLITE_NULL 5
  3901. #ifdef SQLITE_TEXT
  3902. # undef SQLITE_TEXT
  3903. #else
  3904. # define SQLITE_TEXT 3
  3905. #endif
  3906. #define SQLITE3_TEXT 3
  3907. /*
  3908. ** CAPI3REF: Result Values From A Query
  3909. ** KEYWORDS: {column access functions}
  3910. **
  3911. ** These routines form the "result set" interface.
  3912. **
  3913. ** ^These routines return information about a single column of the current
  3914. ** result row of a query. ^In every case the first argument is a pointer
  3915. ** to the [prepared statement] that is being evaluated (the [sqlite3_stmt*]
  3916. ** that was returned from [sqlite3_prepare_v2()] or one of its variants)
  3917. ** and the second argument is the index of the column for which information
  3918. ** should be returned. ^The leftmost column of the result set has the index 0.
  3919. ** ^The number of columns in the result can be determined using
  3920. ** [sqlite3_column_count()].
  3921. **
  3922. ** If the SQL statement does not currently point to a valid row, or if the
  3923. ** column index is out of range, the result is undefined.
  3924. ** These routines may only be called when the most recent call to
  3925. ** [sqlite3_step()] has returned [SQLITE_ROW] and neither
  3926. ** [sqlite3_reset()] nor [sqlite3_finalize()] have been called subsequently.
  3927. ** If any of these routines are called after [sqlite3_reset()] or
  3928. ** [sqlite3_finalize()] or after [sqlite3_step()] has returned
  3929. ** something other than [SQLITE_ROW], the results are undefined.
  3930. ** If [sqlite3_step()] or [sqlite3_reset()] or [sqlite3_finalize()]
  3931. ** are called from a different thread while any of these routines
  3932. ** are pending, then the results are undefined.
  3933. **
  3934. ** ^The sqlite3_column_type() routine returns the
  3935. ** [SQLITE_INTEGER | datatype code] for the initial data type
  3936. ** of the result column. ^The returned value is one of [SQLITE_INTEGER],
  3937. ** [SQLITE_FLOAT], [SQLITE_TEXT], [SQLITE_BLOB], or [SQLITE_NULL]. The value
  3938. ** returned by sqlite3_column_type() is only meaningful if no type
  3939. ** conversions have occurred as described below. After a type conversion,
  3940. ** the value returned by sqlite3_column_type() is undefined. Future
  3941. ** versions of SQLite may change the behavior of sqlite3_column_type()
  3942. ** following a type conversion.
  3943. **
  3944. ** ^If the result is a BLOB or UTF-8 string then the sqlite3_column_bytes()
  3945. ** routine returns the number of bytes in that BLOB or string.
  3946. ** ^If the result is a UTF-16 string, then sqlite3_column_bytes() converts
  3947. ** the string to UTF-8 and then returns the number of bytes.
  3948. ** ^If the result is a numeric value then sqlite3_column_bytes() uses
  3949. ** [sqlite3_snprintf()] to convert that value to a UTF-8 string and returns
  3950. ** the number of bytes in that string.
  3951. ** ^If the result is NULL, then sqlite3_column_bytes() returns zero.
  3952. **
  3953. ** ^If the result is a BLOB or UTF-16 string then the sqlite3_column_bytes16()
  3954. ** routine returns the number of bytes in that BLOB or string.
  3955. ** ^If the result is a UTF-8 string, then sqlite3_column_bytes16() converts
  3956. ** the string to UTF-16 and then returns the number of bytes.
  3957. ** ^If the result is a numeric value then sqlite3_column_bytes16() uses
  3958. ** [sqlite3_snprintf()] to convert that value to a UTF-16 string and returns
  3959. ** the number of bytes in that string.
  3960. ** ^If the result is NULL, then sqlite3_column_bytes16() returns zero.
  3961. **
  3962. ** ^The values returned by [sqlite3_column_bytes()] and
  3963. ** [sqlite3_column_bytes16()] do not include the zero terminators at the end
  3964. ** of the string. ^For clarity: the values returned by
  3965. ** [sqlite3_column_bytes()] and [sqlite3_column_bytes16()] are the number of
  3966. ** bytes in the string, not the number of characters.
  3967. **
  3968. ** ^Strings returned by sqlite3_column_text() and sqlite3_column_text16(),
  3969. ** even empty strings, are always zero-terminated. ^The return
  3970. ** value from sqlite3_column_blob() for a zero-length BLOB is a NULL pointer.
  3971. **
  3972. ** ^The object returned by [sqlite3_column_value()] is an
  3973. ** [unprotected sqlite3_value] object. An unprotected sqlite3_value object
  3974. ** may only be used with [sqlite3_bind_value()] and [sqlite3_result_value()].
  3975. ** If the [unprotected sqlite3_value] object returned by
  3976. ** [sqlite3_column_value()] is used in any other way, including calls
  3977. ** to routines like [sqlite3_value_int()], [sqlite3_value_text()],
  3978. ** or [sqlite3_value_bytes()], then the behavior is undefined.
  3979. **
  3980. ** These routines attempt to convert the value where appropriate. ^For
  3981. ** example, if the internal representation is FLOAT and a text result
  3982. ** is requested, [sqlite3_snprintf()] is used internally to perform the
  3983. ** conversion automatically. ^(The following table details the conversions
  3984. ** that are applied:
  3985. **
  3986. ** <blockquote>
  3987. ** <table border="1">
  3988. ** <tr><th> Internal<br>Type <th> Requested<br>Type <th> Conversion
  3989. **
  3990. ** <tr><td> NULL <td> INTEGER <td> Result is 0
  3991. ** <tr><td> NULL <td> FLOAT <td> Result is 0.0
  3992. ** <tr><td> NULL <td> TEXT <td> Result is NULL pointer
  3993. ** <tr><td> NULL <td> BLOB <td> Result is NULL pointer
  3994. ** <tr><td> INTEGER <td> FLOAT <td> Convert from integer to float
  3995. ** <tr><td> INTEGER <td> TEXT <td> ASCII rendering of the integer
  3996. ** <tr><td> INTEGER <td> BLOB <td> Same as INTEGER->TEXT
  3997. ** <tr><td> FLOAT <td> INTEGER <td> Convert from float to integer
  3998. ** <tr><td> FLOAT <td> TEXT <td> ASCII rendering of the float
  3999. ** <tr><td> FLOAT <td> BLOB <td> Same as FLOAT->TEXT
  4000. ** <tr><td> TEXT <td> INTEGER <td> Use atoi()
  4001. ** <tr><td> TEXT <td> FLOAT <td> Use atof()
  4002. ** <tr><td> TEXT <td> BLOB <td> No change
  4003. ** <tr><td> BLOB <td> INTEGER <td> Convert to TEXT then use atoi()
  4004. ** <tr><td> BLOB <td> FLOAT <td> Convert to TEXT then use atof()
  4005. ** <tr><td> BLOB <td> TEXT <td> Add a zero terminator if needed
  4006. ** </table>
  4007. ** </blockquote>)^
  4008. **
  4009. ** The table above makes reference to standard C library functions atoi()
  4010. ** and atof(). SQLite does not really use these functions. It has its
  4011. ** own equivalent internal routines. The atoi() and atof() names are
  4012. ** used in the table for brevity and because they are familiar to most
  4013. ** C programmers.
  4014. **
  4015. ** Note that when type conversions occur, pointers returned by prior
  4016. ** calls to sqlite3_column_blob(), sqlite3_column_text(), and/or
  4017. ** sqlite3_column_text16() may be invalidated.
  4018. ** Type conversions and pointer invalidations might occur
  4019. ** in the following cases:
  4020. **
  4021. ** <ul>
  4022. ** <li> The initial content is a BLOB and sqlite3_column_text() or
  4023. ** sqlite3_column_text16() is called. A zero-terminator might
  4024. ** need to be added to the string.</li>
  4025. ** <li> The initial content is UTF-8 text and sqlite3_column_bytes16() or
  4026. ** sqlite3_column_text16() is called. The content must be converted
  4027. ** to UTF-16.</li>
  4028. ** <li> The initial content is UTF-16 text and sqlite3_column_bytes() or
  4029. ** sqlite3_column_text() is called. The content must be converted
  4030. ** to UTF-8.</li>
  4031. ** </ul>
  4032. **
  4033. ** ^Conversions between UTF-16be and UTF-16le are always done in place and do
  4034. ** not invalidate a prior pointer, though of course the content of the buffer
  4035. ** that the prior pointer references will have been modified. Other kinds
  4036. ** of conversion are done in place when it is possible, but sometimes they
  4037. ** are not possible and in those cases prior pointers are invalidated.
  4038. **
  4039. ** The safest and easiest to remember policy is to invoke these routines
  4040. ** in one of the following ways:
  4041. **
  4042. ** <ul>
  4043. ** <li>sqlite3_column_text() followed by sqlite3_column_bytes()</li>
  4044. ** <li>sqlite3_column_blob() followed by sqlite3_column_bytes()</li>
  4045. ** <li>sqlite3_column_text16() followed by sqlite3_column_bytes16()</li>
  4046. ** </ul>
  4047. **
  4048. ** In other words, you should call sqlite3_column_text(),
  4049. ** sqlite3_column_blob(), or sqlite3_column_text16() first to force the result
  4050. ** into the desired format, then invoke sqlite3_column_bytes() or
  4051. ** sqlite3_column_bytes16() to find the size of the result. Do not mix calls
  4052. ** to sqlite3_column_text() or sqlite3_column_blob() with calls to
  4053. ** sqlite3_column_bytes16(), and do not mix calls to sqlite3_column_text16()
  4054. ** with calls to sqlite3_column_bytes().
  4055. **
  4056. ** ^The pointers returned are valid until a type conversion occurs as
  4057. ** described above, or until [sqlite3_step()] or [sqlite3_reset()] or
  4058. ** [sqlite3_finalize()] is called. ^The memory space used to hold strings
  4059. ** and BLOBs is freed automatically. Do <b>not</b> pass the pointers returned
  4060. ** [sqlite3_column_blob()], [sqlite3_column_text()], etc. into
  4061. ** [sqlite3_free()].
  4062. **
  4063. ** ^(If a memory allocation error occurs during the evaluation of any
  4064. ** of these routines, a default value is returned. The default value
  4065. ** is either the integer 0, the floating point number 0.0, or a NULL
  4066. ** pointer. Subsequent calls to [sqlite3_errcode()] will return
  4067. ** [SQLITE_NOMEM].)^
  4068. */
  4069. SQLITE_API const void *sqlite3_column_blob(sqlite3_stmt*, int iCol);
  4070. SQLITE_API int sqlite3_column_bytes(sqlite3_stmt*, int iCol);
  4071. SQLITE_API int sqlite3_column_bytes16(sqlite3_stmt*, int iCol);
  4072. SQLITE_API double sqlite3_column_double(sqlite3_stmt*, int iCol);
  4073. SQLITE_API int sqlite3_column_int(sqlite3_stmt*, int iCol);
  4074. SQLITE_API sqlite3_int64 sqlite3_column_int64(sqlite3_stmt*, int iCol);
  4075. SQLITE_API const unsigned char *sqlite3_column_text(sqlite3_stmt*, int iCol);
  4076. SQLITE_API const void *sqlite3_column_text16(sqlite3_stmt*, int iCol);
  4077. SQLITE_API int sqlite3_column_type(sqlite3_stmt*, int iCol);
  4078. SQLITE_API sqlite3_value *sqlite3_column_value(sqlite3_stmt*, int iCol);
  4079. /*
  4080. ** CAPI3REF: Destroy A Prepared Statement Object
  4081. **
  4082. ** ^The sqlite3_finalize() function is called to delete a [prepared statement].
  4083. ** ^If the most recent evaluation of the statement encountered no errors
  4084. ** or if the statement is never been evaluated, then sqlite3_finalize() returns
  4085. ** SQLITE_OK. ^If the most recent evaluation of statement S failed, then
  4086. ** sqlite3_finalize(S) returns the appropriate [error code] or
  4087. ** [extended error code].
  4088. **
  4089. ** ^The sqlite3_finalize(S) routine can be called at any point during
  4090. ** the life cycle of [prepared statement] S:
  4091. ** before statement S is ever evaluated, after
  4092. ** one or more calls to [sqlite3_reset()], or after any call
  4093. ** to [sqlite3_step()] regardless of whether or not the statement has
  4094. ** completed execution.
  4095. **
  4096. ** ^Invoking sqlite3_finalize() on a NULL pointer is a harmless no-op.
  4097. **
  4098. ** The application must finalize every [prepared statement] in order to avoid
  4099. ** resource leaks. It is a grievous error for the application to try to use
  4100. ** a prepared statement after it has been finalized. Any use of a prepared
  4101. ** statement after it has been finalized can result in undefined and
  4102. ** undesirable behavior such as segfaults and heap corruption.
  4103. */
  4104. SQLITE_API int sqlite3_finalize(sqlite3_stmt *pStmt);
  4105. /*
  4106. ** CAPI3REF: Reset A Prepared Statement Object
  4107. **
  4108. ** The sqlite3_reset() function is called to reset a [prepared statement]
  4109. ** object back to its initial state, ready to be re-executed.
  4110. ** ^Any SQL statement variables that had values bound to them using
  4111. ** the [sqlite3_bind_blob | sqlite3_bind_*() API] retain their values.
  4112. ** Use [sqlite3_clear_bindings()] to reset the bindings.
  4113. **
  4114. ** ^The [sqlite3_reset(S)] interface resets the [prepared statement] S
  4115. ** back to the beginning of its program.
  4116. **
  4117. ** ^If the most recent call to [sqlite3_step(S)] for the
  4118. ** [prepared statement] S returned [SQLITE_ROW] or [SQLITE_DONE],
  4119. ** or if [sqlite3_step(S)] has never before been called on S,
  4120. ** then [sqlite3_reset(S)] returns [SQLITE_OK].
  4121. **
  4122. ** ^If the most recent call to [sqlite3_step(S)] for the
  4123. ** [prepared statement] S indicated an error, then
  4124. ** [sqlite3_reset(S)] returns an appropriate [error code].
  4125. **
  4126. ** ^The [sqlite3_reset(S)] interface does not change the values
  4127. ** of any [sqlite3_bind_blob|bindings] on the [prepared statement] S.
  4128. */
  4129. SQLITE_API int sqlite3_reset(sqlite3_stmt *pStmt);
  4130. /*
  4131. ** CAPI3REF: Create Or Redefine SQL Functions
  4132. ** KEYWORDS: {function creation routines}
  4133. ** KEYWORDS: {application-defined SQL function}
  4134. ** KEYWORDS: {application-defined SQL functions}
  4135. **
  4136. ** ^These functions (collectively known as "function creation routines")
  4137. ** are used to add SQL functions or aggregates or to redefine the behavior
  4138. ** of existing SQL functions or aggregates. The only differences between
  4139. ** these routines are the text encoding expected for
  4140. ** the second parameter (the name of the function being created)
  4141. ** and the presence or absence of a destructor callback for
  4142. ** the application data pointer.
  4143. **
  4144. ** ^The first parameter is the [database connection] to which the SQL
  4145. ** function is to be added. ^If an application uses more than one database
  4146. ** connection then application-defined SQL functions must be added
  4147. ** to each database connection separately.
  4148. **
  4149. ** ^The second parameter is the name of the SQL function to be created or
  4150. ** redefined. ^The length of the name is limited to 255 bytes in a UTF-8
  4151. ** representation, exclusive of the zero-terminator. ^Note that the name
  4152. ** length limit is in UTF-8 bytes, not characters nor UTF-16 bytes.
  4153. ** ^Any attempt to create a function with a longer name
  4154. ** will result in [SQLITE_MISUSE] being returned.
  4155. **
  4156. ** ^The third parameter (nArg)
  4157. ** is the number of arguments that the SQL function or
  4158. ** aggregate takes. ^If this parameter is -1, then the SQL function or
  4159. ** aggregate may take any number of arguments between 0 and the limit
  4160. ** set by [sqlite3_limit]([SQLITE_LIMIT_FUNCTION_ARG]). If the third
  4161. ** parameter is less than -1 or greater than 127 then the behavior is
  4162. ** undefined.
  4163. **
  4164. ** ^The fourth parameter, eTextRep, specifies what
  4165. ** [SQLITE_UTF8 | text encoding] this SQL function prefers for
  4166. ** its parameters. Every SQL function implementation must be able to work
  4167. ** with UTF-8, UTF-16le, or UTF-16be. But some implementations may be
  4168. ** more efficient with one encoding than another. ^An application may
  4169. ** invoke sqlite3_create_function() or sqlite3_create_function16() multiple
  4170. ** times with the same function but with different values of eTextRep.
  4171. ** ^When multiple implementations of the same function are available, SQLite
  4172. ** will pick the one that involves the least amount of data conversion.
  4173. ** If there is only a single implementation which does not care what text
  4174. ** encoding is used, then the fourth argument should be [SQLITE_ANY].
  4175. **
  4176. ** ^(The fifth parameter is an arbitrary pointer. The implementation of the
  4177. ** function can gain access to this pointer using [sqlite3_user_data()].)^
  4178. **
  4179. ** ^The sixth, seventh and eighth parameters, xFunc, xStep and xFinal, are
  4180. ** pointers to C-language functions that implement the SQL function or
  4181. ** aggregate. ^A scalar SQL function requires an implementation of the xFunc
  4182. ** callback only; NULL pointers must be passed as the xStep and xFinal
  4183. ** parameters. ^An aggregate SQL function requires an implementation of xStep
  4184. ** and xFinal and NULL pointer must be passed for xFunc. ^To delete an existing
  4185. ** SQL function or aggregate, pass NULL pointers for all three function
  4186. ** callbacks.
  4187. **
  4188. ** ^(If the ninth parameter to sqlite3_create_function_v2() is not NULL,
  4189. ** then it is destructor for the application data pointer.
  4190. ** The destructor is invoked when the function is deleted, either by being
  4191. ** overloaded or when the database connection closes.)^
  4192. ** ^The destructor is also invoked if the call to
  4193. ** sqlite3_create_function_v2() fails.
  4194. ** ^When the destructor callback of the tenth parameter is invoked, it
  4195. ** is passed a single argument which is a copy of the application data
  4196. ** pointer which was the fifth parameter to sqlite3_create_function_v2().
  4197. **
  4198. ** ^It is permitted to register multiple implementations of the same
  4199. ** functions with the same name but with either differing numbers of
  4200. ** arguments or differing preferred text encodings. ^SQLite will use
  4201. ** the implementation that most closely matches the way in which the
  4202. ** SQL function is used. ^A function implementation with a non-negative
  4203. ** nArg parameter is a better match than a function implementation with
  4204. ** a negative nArg. ^A function where the preferred text encoding
  4205. ** matches the database encoding is a better
  4206. ** match than a function where the encoding is different.
  4207. ** ^A function where the encoding difference is between UTF16le and UTF16be
  4208. ** is a closer match than a function where the encoding difference is
  4209. ** between UTF8 and UTF16.
  4210. **
  4211. ** ^Built-in functions may be overloaded by new application-defined functions.
  4212. **
  4213. ** ^An application-defined function is permitted to call other
  4214. ** SQLite interfaces. However, such calls must not
  4215. ** close the database connection nor finalize or reset the prepared
  4216. ** statement in which the function is running.
  4217. */
  4218. SQLITE_API int sqlite3_create_function(
  4219. sqlite3 *db,
  4220. const char *zFunctionName,
  4221. int nArg,
  4222. int eTextRep,
  4223. void *pApp,
  4224. void (*xFunc)(sqlite3_context*,int,sqlite3_value**),
  4225. void (*xStep)(sqlite3_context*,int,sqlite3_value**),
  4226. void (*xFinal)(sqlite3_context*)
  4227. );
  4228. SQLITE_API int sqlite3_create_function16(
  4229. sqlite3 *db,
  4230. const void *zFunctionName,
  4231. int nArg,
  4232. int eTextRep,
  4233. void *pApp,
  4234. void (*xFunc)(sqlite3_context*,int,sqlite3_value**),
  4235. void (*xStep)(sqlite3_context*,int,sqlite3_value**),
  4236. void (*xFinal)(sqlite3_context*)
  4237. );
  4238. SQLITE_API int sqlite3_create_function_v2(
  4239. sqlite3 *db,
  4240. const char *zFunctionName,
  4241. int nArg,
  4242. int eTextRep,
  4243. void *pApp,
  4244. void (*xFunc)(sqlite3_context*,int,sqlite3_value**),
  4245. void (*xStep)(sqlite3_context*,int,sqlite3_value**),
  4246. void (*xFinal)(sqlite3_context*),
  4247. void(*xDestroy)(void*)
  4248. );
  4249. /*
  4250. ** CAPI3REF: Text Encodings
  4251. **
  4252. ** These constant define integer codes that represent the various
  4253. ** text encodings supported by SQLite.
  4254. */
  4255. #define SQLITE_UTF8 1
  4256. #define SQLITE_UTF16LE 2
  4257. #define SQLITE_UTF16BE 3
  4258. #define SQLITE_UTF16 4 /* Use native byte order */
  4259. #define SQLITE_ANY 5 /* sqlite3_create_function only */
  4260. #define SQLITE_UTF16_ALIGNED 8 /* sqlite3_create_collation only */
  4261. /*
  4262. ** CAPI3REF: Deprecated Functions
  4263. ** DEPRECATED
  4264. **
  4265. ** These functions are [deprecated]. In order to maintain
  4266. ** backwards compatibility with older code, these functions continue
  4267. ** to be supported. However, new applications should avoid
  4268. ** the use of these functions. To help encourage people to avoid
  4269. ** using these functions, we are not going to tell you what they do.
  4270. */
  4271. #ifndef SQLITE_OMIT_DEPRECATED
  4272. SQLITE_API SQLITE_DEPRECATED int sqlite3_aggregate_count(sqlite3_context*);
  4273. SQLITE_API SQLITE_DEPRECATED int sqlite3_expired(sqlite3_stmt*);
  4274. SQLITE_API SQLITE_DEPRECATED int sqlite3_transfer_bindings(sqlite3_stmt*, sqlite3_stmt*);
  4275. SQLITE_API SQLITE_DEPRECATED int sqlite3_global_recover(void);
  4276. SQLITE_API SQLITE_DEPRECATED void sqlite3_thread_cleanup(void);
  4277. SQLITE_API SQLITE_DEPRECATED int sqlite3_memory_alarm(void(*)(void*,sqlite3_int64,int),void*,sqlite3_int64);
  4278. #endif
  4279. /*
  4280. ** CAPI3REF: Obtaining SQL Function Parameter Values
  4281. **
  4282. ** The C-language implementation of SQL functions and aggregates uses
  4283. ** this set of interface routines to access the parameter values on
  4284. ** the function or aggregate.
  4285. **
  4286. ** The xFunc (for scalar functions) or xStep (for aggregates) parameters
  4287. ** to [sqlite3_create_function()] and [sqlite3_create_function16()]
  4288. ** define callbacks that implement the SQL functions and aggregates.
  4289. ** The 3rd parameter to these callbacks is an array of pointers to
  4290. ** [protected sqlite3_value] objects. There is one [sqlite3_value] object for
  4291. ** each parameter to the SQL function. These routines are used to
  4292. ** extract values from the [sqlite3_value] objects.
  4293. **
  4294. ** These routines work only with [protected sqlite3_value] objects.
  4295. ** Any attempt to use these routines on an [unprotected sqlite3_value]
  4296. ** object results in undefined behavior.
  4297. **
  4298. ** ^These routines work just like the corresponding [column access functions]
  4299. ** except that these routines take a single [protected sqlite3_value] object
  4300. ** pointer instead of a [sqlite3_stmt*] pointer and an integer column number.
  4301. **
  4302. ** ^The sqlite3_value_text16() interface extracts a UTF-16 string
  4303. ** in the native byte-order of the host machine. ^The
  4304. ** sqlite3_value_text16be() and sqlite3_value_text16le() interfaces
  4305. ** extract UTF-16 strings as big-endian and little-endian respectively.
  4306. **
  4307. ** ^(The sqlite3_value_numeric_type() interface attempts to apply
  4308. ** numeric affinity to the value. This means that an attempt is
  4309. ** made to convert the value to an integer or floating point. If
  4310. ** such a conversion is possible without loss of information (in other
  4311. ** words, if the value is a string that looks like a number)
  4312. ** then the conversion is performed. Otherwise no conversion occurs.
  4313. ** The [SQLITE_INTEGER | datatype] after conversion is returned.)^
  4314. **
  4315. ** Please pay particular attention to the fact that the pointer returned
  4316. ** from [sqlite3_value_blob()], [sqlite3_value_text()], or
  4317. ** [sqlite3_value_text16()] can be invalidated by a subsequent call to
  4318. ** [sqlite3_value_bytes()], [sqlite3_value_bytes16()], [sqlite3_value_text()],
  4319. ** or [sqlite3_value_text16()].
  4320. **
  4321. ** These routines must be called from the same thread as
  4322. ** the SQL function that supplied the [sqlite3_value*] parameters.
  4323. */
  4324. SQLITE_API const void *sqlite3_value_blob(sqlite3_value*);
  4325. SQLITE_API int sqlite3_value_bytes(sqlite3_value*);
  4326. SQLITE_API int sqlite3_value_bytes16(sqlite3_value*);
  4327. SQLITE_API double sqlite3_value_double(sqlite3_value*);
  4328. SQLITE_API int sqlite3_value_int(sqlite3_value*);
  4329. SQLITE_API sqlite3_int64 sqlite3_value_int64(sqlite3_value*);
  4330. SQLITE_API const unsigned char *sqlite3_value_text(sqlite3_value*);
  4331. SQLITE_API const void *sqlite3_value_text16(sqlite3_value*);
  4332. SQLITE_API const void *sqlite3_value_text16le(sqlite3_value*);
  4333. SQLITE_API const void *sqlite3_value_text16be(sqlite3_value*);
  4334. SQLITE_API int sqlite3_value_type(sqlite3_value*);
  4335. SQLITE_API int sqlite3_value_numeric_type(sqlite3_value*);
  4336. /*
  4337. ** CAPI3REF: Obtain Aggregate Function Context
  4338. **
  4339. ** Implementations of aggregate SQL functions use this
  4340. ** routine to allocate memory for storing their state.
  4341. **
  4342. ** ^The first time the sqlite3_aggregate_context(C,N) routine is called
  4343. ** for a particular aggregate function, SQLite
  4344. ** allocates N of memory, zeroes out that memory, and returns a pointer
  4345. ** to the new memory. ^On second and subsequent calls to
  4346. ** sqlite3_aggregate_context() for the same aggregate function instance,
  4347. ** the same buffer is returned. Sqlite3_aggregate_context() is normally
  4348. ** called once for each invocation of the xStep callback and then one
  4349. ** last time when the xFinal callback is invoked. ^(When no rows match
  4350. ** an aggregate query, the xStep() callback of the aggregate function
  4351. ** implementation is never called and xFinal() is called exactly once.
  4352. ** In those cases, sqlite3_aggregate_context() might be called for the
  4353. ** first time from within xFinal().)^
  4354. **
  4355. ** ^The sqlite3_aggregate_context(C,N) routine returns a NULL pointer if N is
  4356. ** less than or equal to zero or if a memory allocate error occurs.
  4357. **
  4358. ** ^(The amount of space allocated by sqlite3_aggregate_context(C,N) is
  4359. ** determined by the N parameter on first successful call. Changing the
  4360. ** value of N in subsequent call to sqlite3_aggregate_context() within
  4361. ** the same aggregate function instance will not resize the memory
  4362. ** allocation.)^
  4363. **
  4364. ** ^SQLite automatically frees the memory allocated by
  4365. ** sqlite3_aggregate_context() when the aggregate query concludes.
  4366. **
  4367. ** The first parameter must be a copy of the
  4368. ** [sqlite3_context | SQL function context] that is the first parameter
  4369. ** to the xStep or xFinal callback routine that implements the aggregate
  4370. ** function.
  4371. **
  4372. ** This routine must be called from the same thread in which
  4373. ** the aggregate SQL function is running.
  4374. */
  4375. SQLITE_API void *sqlite3_aggregate_context(sqlite3_context*, int nBytes);
  4376. /*
  4377. ** CAPI3REF: User Data For Functions
  4378. **
  4379. ** ^The sqlite3_user_data() interface returns a copy of
  4380. ** the pointer that was the pUserData parameter (the 5th parameter)
  4381. ** of the [sqlite3_create_function()]
  4382. ** and [sqlite3_create_function16()] routines that originally
  4383. ** registered the application defined function.
  4384. **
  4385. ** This routine must be called from the same thread in which
  4386. ** the application-defined function is running.
  4387. */
  4388. SQLITE_API void *sqlite3_user_data(sqlite3_context*);
  4389. /*
  4390. ** CAPI3REF: Database Connection For Functions
  4391. **
  4392. ** ^The sqlite3_context_db_handle() interface returns a copy of
  4393. ** the pointer to the [database connection] (the 1st parameter)
  4394. ** of the [sqlite3_create_function()]
  4395. ** and [sqlite3_create_function16()] routines that originally
  4396. ** registered the application defined function.
  4397. */
  4398. SQLITE_API sqlite3 *sqlite3_context_db_handle(sqlite3_context*);
  4399. /*
  4400. ** CAPI3REF: Function Auxiliary Data
  4401. **
  4402. ** The following two functions may be used by scalar SQL functions to
  4403. ** associate metadata with argument values. If the same value is passed to
  4404. ** multiple invocations of the same SQL function during query execution, under
  4405. ** some circumstances the associated metadata may be preserved. This may
  4406. ** be used, for example, to add a regular-expression matching scalar
  4407. ** function. The compiled version of the regular expression is stored as
  4408. ** metadata associated with the SQL value passed as the regular expression
  4409. ** pattern. The compiled regular expression can be reused on multiple
  4410. ** invocations of the same function so that the original pattern string
  4411. ** does not need to be recompiled on each invocation.
  4412. **
  4413. ** ^The sqlite3_get_auxdata() interface returns a pointer to the metadata
  4414. ** associated by the sqlite3_set_auxdata() function with the Nth argument
  4415. ** value to the application-defined function. ^If no metadata has been ever
  4416. ** been set for the Nth argument of the function, or if the corresponding
  4417. ** function parameter has changed since the meta-data was set,
  4418. ** then sqlite3_get_auxdata() returns a NULL pointer.
  4419. **
  4420. ** ^The sqlite3_set_auxdata() interface saves the metadata
  4421. ** pointed to by its 3rd parameter as the metadata for the N-th
  4422. ** argument of the application-defined function. Subsequent
  4423. ** calls to sqlite3_get_auxdata() might return this data, if it has
  4424. ** not been destroyed.
  4425. ** ^If it is not NULL, SQLite will invoke the destructor
  4426. ** function given by the 4th parameter to sqlite3_set_auxdata() on
  4427. ** the metadata when the corresponding function parameter changes
  4428. ** or when the SQL statement completes, whichever comes first.
  4429. **
  4430. ** SQLite is free to call the destructor and drop metadata on any
  4431. ** parameter of any function at any time. ^The only guarantee is that
  4432. ** the destructor will be called before the metadata is dropped.
  4433. **
  4434. ** ^(In practice, metadata is preserved between function calls for
  4435. ** expressions that are constant at compile time. This includes literal
  4436. ** values and [parameters].)^
  4437. **
  4438. ** These routines must be called from the same thread in which
  4439. ** the SQL function is running.
  4440. */
  4441. SQLITE_API void *sqlite3_get_auxdata(sqlite3_context*, int N);
  4442. SQLITE_API void sqlite3_set_auxdata(sqlite3_context*, int N, void*, void (*)(void*));
  4443. /*
  4444. ** CAPI3REF: Constants Defining Special Destructor Behavior
  4445. **
  4446. ** These are special values for the destructor that is passed in as the
  4447. ** final argument to routines like [sqlite3_result_blob()]. ^If the destructor
  4448. ** argument is SQLITE_STATIC, it means that the content pointer is constant
  4449. ** and will never change. It does not need to be destroyed. ^The
  4450. ** SQLITE_TRANSIENT value means that the content will likely change in
  4451. ** the near future and that SQLite should make its own private copy of
  4452. ** the content before returning.
  4453. **
  4454. ** The typedef is necessary to work around problems in certain
  4455. ** C++ compilers. See ticket #2191.
  4456. */
  4457. typedef void (*sqlite3_destructor_type)(void*);
  4458. #define SQLITE_STATIC ((sqlite3_destructor_type)0)
  4459. #define SQLITE_TRANSIENT ((sqlite3_destructor_type)-1)
  4460. /*
  4461. ** CAPI3REF: Setting The Result Of An SQL Function
  4462. **
  4463. ** These routines are used by the xFunc or xFinal callbacks that
  4464. ** implement SQL functions and aggregates. See
  4465. ** [sqlite3_create_function()] and [sqlite3_create_function16()]
  4466. ** for additional information.
  4467. **
  4468. ** These functions work very much like the [parameter binding] family of
  4469. ** functions used to bind values to host parameters in prepared statements.
  4470. ** Refer to the [SQL parameter] documentation for additional information.
  4471. **
  4472. ** ^The sqlite3_result_blob() interface sets the result from
  4473. ** an application-defined function to be the BLOB whose content is pointed
  4474. ** to by the second parameter and which is N bytes long where N is the
  4475. ** third parameter.
  4476. **
  4477. ** ^The sqlite3_result_zeroblob() interfaces set the result of
  4478. ** the application-defined function to be a BLOB containing all zero
  4479. ** bytes and N bytes in size, where N is the value of the 2nd parameter.
  4480. **
  4481. ** ^The sqlite3_result_double() interface sets the result from
  4482. ** an application-defined function to be a floating point value specified
  4483. ** by its 2nd argument.
  4484. **
  4485. ** ^The sqlite3_result_error() and sqlite3_result_error16() functions
  4486. ** cause the implemented SQL function to throw an exception.
  4487. ** ^SQLite uses the string pointed to by the
  4488. ** 2nd parameter of sqlite3_result_error() or sqlite3_result_error16()
  4489. ** as the text of an error message. ^SQLite interprets the error
  4490. ** message string from sqlite3_result_error() as UTF-8. ^SQLite
  4491. ** interprets the string from sqlite3_result_error16() as UTF-16 in native
  4492. ** byte order. ^If the third parameter to sqlite3_result_error()
  4493. ** or sqlite3_result_error16() is negative then SQLite takes as the error
  4494. ** message all text up through the first zero character.
  4495. ** ^If the third parameter to sqlite3_result_error() or
  4496. ** sqlite3_result_error16() is non-negative then SQLite takes that many
  4497. ** bytes (not characters) from the 2nd parameter as the error message.
  4498. ** ^The sqlite3_result_error() and sqlite3_result_error16()
  4499. ** routines make a private copy of the error message text before
  4500. ** they return. Hence, the calling function can deallocate or
  4501. ** modify the text after they return without harm.
  4502. ** ^The sqlite3_result_error_code() function changes the error code
  4503. ** returned by SQLite as a result of an error in a function. ^By default,
  4504. ** the error code is SQLITE_ERROR. ^A subsequent call to sqlite3_result_error()
  4505. ** or sqlite3_result_error16() resets the error code to SQLITE_ERROR.
  4506. **
  4507. ** ^The sqlite3_result_toobig() interface causes SQLite to throw an error
  4508. ** indicating that a string or BLOB is too long to represent.
  4509. **
  4510. ** ^The sqlite3_result_nomem() interface causes SQLite to throw an error
  4511. ** indicating that a memory allocation failed.
  4512. **
  4513. ** ^The sqlite3_result_int() interface sets the return value
  4514. ** of the application-defined function to be the 32-bit signed integer
  4515. ** value given in the 2nd argument.
  4516. ** ^The sqlite3_result_int64() interface sets the return value
  4517. ** of the application-defined function to be the 64-bit signed integer
  4518. ** value given in the 2nd argument.
  4519. **
  4520. ** ^The sqlite3_result_null() interface sets the return value
  4521. ** of the application-defined function to be NULL.
  4522. **
  4523. ** ^The sqlite3_result_text(), sqlite3_result_text16(),
  4524. ** sqlite3_result_text16le(), and sqlite3_result_text16be() interfaces
  4525. ** set the return value of the application-defined function to be
  4526. ** a text string which is represented as UTF-8, UTF-16 native byte order,
  4527. ** UTF-16 little endian, or UTF-16 big endian, respectively.
  4528. ** ^SQLite takes the text result from the application from
  4529. ** the 2nd parameter of the sqlite3_result_text* interfaces.
  4530. ** ^If the 3rd parameter to the sqlite3_result_text* interfaces
  4531. ** is negative, then SQLite takes result text from the 2nd parameter
  4532. ** through the first zero character.
  4533. ** ^If the 3rd parameter to the sqlite3_result_text* interfaces
  4534. ** is non-negative, then as many bytes (not characters) of the text
  4535. ** pointed to by the 2nd parameter are taken as the application-defined
  4536. ** function result. If the 3rd parameter is non-negative, then it
  4537. ** must be the byte offset into the string where the NUL terminator would
  4538. ** appear if the string where NUL terminated. If any NUL characters occur
  4539. ** in the string at a byte offset that is less than the value of the 3rd
  4540. ** parameter, then the resulting string will contain embedded NULs and the
  4541. ** result of expressions operating on strings with embedded NULs is undefined.
  4542. ** ^If the 4th parameter to the sqlite3_result_text* interfaces
  4543. ** or sqlite3_result_blob is a non-NULL pointer, then SQLite calls that
  4544. ** function as the destructor on the text or BLOB result when it has
  4545. ** finished using that result.
  4546. ** ^If the 4th parameter to the sqlite3_result_text* interfaces or to
  4547. ** sqlite3_result_blob is the special constant SQLITE_STATIC, then SQLite
  4548. ** assumes that the text or BLOB result is in constant space and does not
  4549. ** copy the content of the parameter nor call a destructor on the content
  4550. ** when it has finished using that result.
  4551. ** ^If the 4th parameter to the sqlite3_result_text* interfaces
  4552. ** or sqlite3_result_blob is the special constant SQLITE_TRANSIENT
  4553. ** then SQLite makes a copy of the result into space obtained from
  4554. ** from [sqlite3_malloc()] before it returns.
  4555. **
  4556. ** ^The sqlite3_result_value() interface sets the result of
  4557. ** the application-defined function to be a copy the
  4558. ** [unprotected sqlite3_value] object specified by the 2nd parameter. ^The
  4559. ** sqlite3_result_value() interface makes a copy of the [sqlite3_value]
  4560. ** so that the [sqlite3_value] specified in the parameter may change or
  4561. ** be deallocated after sqlite3_result_value() returns without harm.
  4562. ** ^A [protected sqlite3_value] object may always be used where an
  4563. ** [unprotected sqlite3_value] object is required, so either
  4564. ** kind of [sqlite3_value] object can be used with this interface.
  4565. **
  4566. ** If these routines are called from within the different thread
  4567. ** than the one containing the application-defined function that received
  4568. ** the [sqlite3_context] pointer, the results are undefined.
  4569. */
  4570. SQLITE_API void sqlite3_result_blob(sqlite3_context*, const void*, int, void(*)(void*));
  4571. SQLITE_API void sqlite3_result_double(sqlite3_context*, double);
  4572. SQLITE_API void sqlite3_result_error(sqlite3_context*, const char*, int);
  4573. SQLITE_API void sqlite3_result_error16(sqlite3_context*, const void*, int);
  4574. SQLITE_API void sqlite3_result_error_toobig(sqlite3_context*);
  4575. SQLITE_API void sqlite3_result_error_nomem(sqlite3_context*);
  4576. SQLITE_API void sqlite3_result_error_code(sqlite3_context*, int);
  4577. SQLITE_API void sqlite3_result_int(sqlite3_context*, int);
  4578. SQLITE_API void sqlite3_result_int64(sqlite3_context*, sqlite3_int64);
  4579. SQLITE_API void sqlite3_result_null(sqlite3_context*);
  4580. SQLITE_API void sqlite3_result_text(sqlite3_context*, const char*, int, void(*)(void*));
  4581. SQLITE_API void sqlite3_result_text16(sqlite3_context*, const void*, int, void(*)(void*));
  4582. SQLITE_API void sqlite3_result_text16le(sqlite3_context*, const void*, int,void(*)(void*));
  4583. SQLITE_API void sqlite3_result_text16be(sqlite3_context*, const void*, int,void(*)(void*));
  4584. SQLITE_API void sqlite3_result_value(sqlite3_context*, sqlite3_value*);
  4585. SQLITE_API void sqlite3_result_zeroblob(sqlite3_context*, int n);
  4586. /*
  4587. ** CAPI3REF: Define New Collating Sequences
  4588. **
  4589. ** ^These functions add, remove, or modify a [collation] associated
  4590. ** with the [database connection] specified as the first argument.
  4591. **
  4592. ** ^The name of the collation is a UTF-8 string
  4593. ** for sqlite3_create_collation() and sqlite3_create_collation_v2()
  4594. ** and a UTF-16 string in native byte order for sqlite3_create_collation16().
  4595. ** ^Collation names that compare equal according to [sqlite3_strnicmp()] are
  4596. ** considered to be the same name.
  4597. **
  4598. ** ^(The third argument (eTextRep) must be one of the constants:
  4599. ** <ul>
  4600. ** <li> [SQLITE_UTF8],
  4601. ** <li> [SQLITE_UTF16LE],
  4602. ** <li> [SQLITE_UTF16BE],
  4603. ** <li> [SQLITE_UTF16], or
  4604. ** <li> [SQLITE_UTF16_ALIGNED].
  4605. ** </ul>)^
  4606. ** ^The eTextRep argument determines the encoding of strings passed
  4607. ** to the collating function callback, xCallback.
  4608. ** ^The [SQLITE_UTF16] and [SQLITE_UTF16_ALIGNED] values for eTextRep
  4609. ** force strings to be UTF16 with native byte order.
  4610. ** ^The [SQLITE_UTF16_ALIGNED] value for eTextRep forces strings to begin
  4611. ** on an even byte address.
  4612. **
  4613. ** ^The fourth argument, pArg, is an application data pointer that is passed
  4614. ** through as the first argument to the collating function callback.
  4615. **
  4616. ** ^The fifth argument, xCallback, is a pointer to the collating function.
  4617. ** ^Multiple collating functions can be registered using the same name but
  4618. ** with different eTextRep parameters and SQLite will use whichever
  4619. ** function requires the least amount of data transformation.
  4620. ** ^If the xCallback argument is NULL then the collating function is
  4621. ** deleted. ^When all collating functions having the same name are deleted,
  4622. ** that collation is no longer usable.
  4623. **
  4624. ** ^The collating function callback is invoked with a copy of the pArg
  4625. ** application data pointer and with two strings in the encoding specified
  4626. ** by the eTextRep argument. The collating function must return an
  4627. ** integer that is negative, zero, or positive
  4628. ** if the first string is less than, equal to, or greater than the second,
  4629. ** respectively. A collating function must always return the same answer
  4630. ** given the same inputs. If two or more collating functions are registered
  4631. ** to the same collation name (using different eTextRep values) then all
  4632. ** must give an equivalent answer when invoked with equivalent strings.
  4633. ** The collating function must obey the following properties for all
  4634. ** strings A, B, and C:
  4635. **
  4636. ** <ol>
  4637. ** <li> If A==B then B==A.
  4638. ** <li> If A==B and B==C then A==C.
  4639. ** <li> If A&lt;B THEN B&gt;A.
  4640. ** <li> If A&lt;B and B&lt;C then A&lt;C.
  4641. ** </ol>
  4642. **
  4643. ** If a collating function fails any of the above constraints and that
  4644. ** collating function is registered and used, then the behavior of SQLite
  4645. ** is undefined.
  4646. **
  4647. ** ^The sqlite3_create_collation_v2() works like sqlite3_create_collation()
  4648. ** with the addition that the xDestroy callback is invoked on pArg when
  4649. ** the collating function is deleted.
  4650. ** ^Collating functions are deleted when they are overridden by later
  4651. ** calls to the collation creation functions or when the
  4652. ** [database connection] is closed using [sqlite3_close()].
  4653. **
  4654. ** ^The xDestroy callback is <u>not</u> called if the
  4655. ** sqlite3_create_collation_v2() function fails. Applications that invoke
  4656. ** sqlite3_create_collation_v2() with a non-NULL xDestroy argument should
  4657. ** check the return code and dispose of the application data pointer
  4658. ** themselves rather than expecting SQLite to deal with it for them.
  4659. ** This is different from every other SQLite interface. The inconsistency
  4660. ** is unfortunate but cannot be changed without breaking backwards
  4661. ** compatibility.
  4662. **
  4663. ** See also: [sqlite3_collation_needed()] and [sqlite3_collation_needed16()].
  4664. */
  4665. SQLITE_API int sqlite3_create_collation(
  4666. sqlite3*,
  4667. const char *zName,
  4668. int eTextRep,
  4669. void *pArg,
  4670. int(*xCompare)(void*,int,const void*,int,const void*)
  4671. );
  4672. SQLITE_API int sqlite3_create_collation_v2(
  4673. sqlite3*,
  4674. const char *zName,
  4675. int eTextRep,
  4676. void *pArg,
  4677. int(*xCompare)(void*,int,const void*,int,const void*),
  4678. void(*xDestroy)(void*)
  4679. );
  4680. SQLITE_API int sqlite3_create_collation16(
  4681. sqlite3*,
  4682. const void *zName,
  4683. int eTextRep,
  4684. void *pArg,
  4685. int(*xCompare)(void*,int,const void*,int,const void*)
  4686. );
  4687. /*
  4688. ** CAPI3REF: Collation Needed Callbacks
  4689. **
  4690. ** ^To avoid having to register all collation sequences before a database
  4691. ** can be used, a single callback function may be registered with the
  4692. ** [database connection] to be invoked whenever an undefined collation
  4693. ** sequence is required.
  4694. **
  4695. ** ^If the function is registered using the sqlite3_collation_needed() API,
  4696. ** then it is passed the names of undefined collation sequences as strings
  4697. ** encoded in UTF-8. ^If sqlite3_collation_needed16() is used,
  4698. ** the names are passed as UTF-16 in machine native byte order.
  4699. ** ^A call to either function replaces the existing collation-needed callback.
  4700. **
  4701. ** ^(When the callback is invoked, the first argument passed is a copy
  4702. ** of the second argument to sqlite3_collation_needed() or
  4703. ** sqlite3_collation_needed16(). The second argument is the database
  4704. ** connection. The third argument is one of [SQLITE_UTF8], [SQLITE_UTF16BE],
  4705. ** or [SQLITE_UTF16LE], indicating the most desirable form of the collation
  4706. ** sequence function required. The fourth parameter is the name of the
  4707. ** required collation sequence.)^
  4708. **
  4709. ** The callback function should register the desired collation using
  4710. ** [sqlite3_create_collation()], [sqlite3_create_collation16()], or
  4711. ** [sqlite3_create_collation_v2()].
  4712. */
  4713. SQLITE_API int sqlite3_collation_needed(
  4714. sqlite3*,
  4715. void*,
  4716. void(*)(void*,sqlite3*,int eTextRep,const char*)
  4717. );
  4718. SQLITE_API int sqlite3_collation_needed16(
  4719. sqlite3*,
  4720. void*,
  4721. void(*)(void*,sqlite3*,int eTextRep,const void*)
  4722. );
  4723. #ifdef SQLITE_HAS_CODEC
  4724. /*
  4725. ** Specify the key for an encrypted database. This routine should be
  4726. ** called right after sqlite3_open().
  4727. **
  4728. ** The code to implement this API is not available in the public release
  4729. ** of SQLite.
  4730. */
  4731. SQLITE_API int sqlite3_key(
  4732. sqlite3 *db, /* Database to be rekeyed */
  4733. const void *pKey, int nKey /* The key */
  4734. );
  4735. /*
  4736. ** Change the key on an open database. If the current database is not
  4737. ** encrypted, this routine will encrypt it. If pNew==0 or nNew==0, the
  4738. ** database is decrypted.
  4739. **
  4740. ** The code to implement this API is not available in the public release
  4741. ** of SQLite.
  4742. */
  4743. SQLITE_API int sqlite3_rekey(
  4744. sqlite3 *db, /* Database to be rekeyed */
  4745. const void *pKey, int nKey /* The new key */
  4746. );
  4747. /*
  4748. ** Specify the activation key for a SEE database. Unless
  4749. ** activated, none of the SEE routines will work.
  4750. */
  4751. SQLITE_API void sqlite3_activate_see(
  4752. const char *zPassPhrase /* Activation phrase */
  4753. );
  4754. #endif
  4755. #ifdef SQLITE_ENABLE_CEROD
  4756. /*
  4757. ** Specify the activation key for a CEROD database. Unless
  4758. ** activated, none of the CEROD routines will work.
  4759. */
  4760. SQLITE_API void sqlite3_activate_cerod(
  4761. const char *zPassPhrase /* Activation phrase */
  4762. );
  4763. #endif
  4764. /*
  4765. ** CAPI3REF: Suspend Execution For A Short Time
  4766. **
  4767. ** The sqlite3_sleep() function causes the current thread to suspend execution
  4768. ** for at least a number of milliseconds specified in its parameter.
  4769. **
  4770. ** If the operating system does not support sleep requests with
  4771. ** millisecond time resolution, then the time will be rounded up to
  4772. ** the nearest second. The number of milliseconds of sleep actually
  4773. ** requested from the operating system is returned.
  4774. **
  4775. ** ^SQLite implements this interface by calling the xSleep()
  4776. ** method of the default [sqlite3_vfs] object. If the xSleep() method
  4777. ** of the default VFS is not implemented correctly, or not implemented at
  4778. ** all, then the behavior of sqlite3_sleep() may deviate from the description
  4779. ** in the previous paragraphs.
  4780. */
  4781. SQLITE_API int sqlite3_sleep(int);
  4782. /*
  4783. ** CAPI3REF: Name Of The Folder Holding Temporary Files
  4784. **
  4785. ** ^(If this global variable is made to point to a string which is
  4786. ** the name of a folder (a.k.a. directory), then all temporary files
  4787. ** created by SQLite when using a built-in [sqlite3_vfs | VFS]
  4788. ** will be placed in that directory.)^ ^If this variable
  4789. ** is a NULL pointer, then SQLite performs a search for an appropriate
  4790. ** temporary file directory.
  4791. **
  4792. ** It is not safe to read or modify this variable in more than one
  4793. ** thread at a time. It is not safe to read or modify this variable
  4794. ** if a [database connection] is being used at the same time in a separate
  4795. ** thread.
  4796. ** It is intended that this variable be set once
  4797. ** as part of process initialization and before any SQLite interface
  4798. ** routines have been called and that this variable remain unchanged
  4799. ** thereafter.
  4800. **
  4801. ** ^The [temp_store_directory pragma] may modify this variable and cause
  4802. ** it to point to memory obtained from [sqlite3_malloc]. ^Furthermore,
  4803. ** the [temp_store_directory pragma] always assumes that any string
  4804. ** that this variable points to is held in memory obtained from
  4805. ** [sqlite3_malloc] and the pragma may attempt to free that memory
  4806. ** using [sqlite3_free].
  4807. ** Hence, if this variable is modified directly, either it should be
  4808. ** made NULL or made to point to memory obtained from [sqlite3_malloc]
  4809. ** or else the use of the [temp_store_directory pragma] should be avoided.
  4810. */
  4811. SQLITE_API char *sqlite3_temp_directory;
  4812. /*
  4813. ** CAPI3REF: Test For Auto-Commit Mode
  4814. ** KEYWORDS: {autocommit mode}
  4815. **
  4816. ** ^The sqlite3_get_autocommit() interface returns non-zero or
  4817. ** zero if the given database connection is or is not in autocommit mode,
  4818. ** respectively. ^Autocommit mode is on by default.
  4819. ** ^Autocommit mode is disabled by a [BEGIN] statement.
  4820. ** ^Autocommit mode is re-enabled by a [COMMIT] or [ROLLBACK].
  4821. **
  4822. ** If certain kinds of errors occur on a statement within a multi-statement
  4823. ** transaction (errors including [SQLITE_FULL], [SQLITE_IOERR],
  4824. ** [SQLITE_NOMEM], [SQLITE_BUSY], and [SQLITE_INTERRUPT]) then the
  4825. ** transaction might be rolled back automatically. The only way to
  4826. ** find out whether SQLite automatically rolled back the transaction after
  4827. ** an error is to use this function.
  4828. **
  4829. ** If another thread changes the autocommit status of the database
  4830. ** connection while this routine is running, then the return value
  4831. ** is undefined.
  4832. */
  4833. SQLITE_API int sqlite3_get_autocommit(sqlite3*);
  4834. /*
  4835. ** CAPI3REF: Find The Database Handle Of A Prepared Statement
  4836. **
  4837. ** ^The sqlite3_db_handle interface returns the [database connection] handle
  4838. ** to which a [prepared statement] belongs. ^The [database connection]
  4839. ** returned by sqlite3_db_handle is the same [database connection]
  4840. ** that was the first argument
  4841. ** to the [sqlite3_prepare_v2()] call (or its variants) that was used to
  4842. ** create the statement in the first place.
  4843. */
  4844. SQLITE_API sqlite3 *sqlite3_db_handle(sqlite3_stmt*);
  4845. /*
  4846. ** CAPI3REF: Return The Filename For A Database Connection
  4847. **
  4848. ** ^The sqlite3_db_filename(D,N) interface returns a pointer to a filename
  4849. ** associated with database N of connection D. ^The main database file
  4850. ** has the name "main". If there is no attached database N on the database
  4851. ** connection D, or if database N is a temporary or in-memory database, then
  4852. ** a NULL pointer is returned.
  4853. **
  4854. ** ^The filename returned by this function is the output of the
  4855. ** xFullPathname method of the [VFS]. ^In other words, the filename
  4856. ** will be an absolute pathname, even if the filename used
  4857. ** to open the database originally was a URI or relative pathname.
  4858. */
  4859. SQLITE_API const char *sqlite3_db_filename(sqlite3 *db, const char *zDbName);
  4860. /*
  4861. ** CAPI3REF: Find the next prepared statement
  4862. **
  4863. ** ^This interface returns a pointer to the next [prepared statement] after
  4864. ** pStmt associated with the [database connection] pDb. ^If pStmt is NULL
  4865. ** then this interface returns a pointer to the first prepared statement
  4866. ** associated with the database connection pDb. ^If no prepared statement
  4867. ** satisfies the conditions of this routine, it returns NULL.
  4868. **
  4869. ** The [database connection] pointer D in a call to
  4870. ** [sqlite3_next_stmt(D,S)] must refer to an open database
  4871. ** connection and in particular must not be a NULL pointer.
  4872. */
  4873. SQLITE_API sqlite3_stmt *sqlite3_next_stmt(sqlite3 *pDb, sqlite3_stmt *pStmt);
  4874. /*
  4875. ** CAPI3REF: Commit And Rollback Notification Callbacks
  4876. **
  4877. ** ^The sqlite3_commit_hook() interface registers a callback
  4878. ** function to be invoked whenever a transaction is [COMMIT | committed].
  4879. ** ^Any callback set by a previous call to sqlite3_commit_hook()
  4880. ** for the same database connection is overridden.
  4881. ** ^The sqlite3_rollback_hook() interface registers a callback
  4882. ** function to be invoked whenever a transaction is [ROLLBACK | rolled back].
  4883. ** ^Any callback set by a previous call to sqlite3_rollback_hook()
  4884. ** for the same database connection is overridden.
  4885. ** ^The pArg argument is passed through to the callback.
  4886. ** ^If the callback on a commit hook function returns non-zero,
  4887. ** then the commit is converted into a rollback.
  4888. **
  4889. ** ^The sqlite3_commit_hook(D,C,P) and sqlite3_rollback_hook(D,C,P) functions
  4890. ** return the P argument from the previous call of the same function
  4891. ** on the same [database connection] D, or NULL for
  4892. ** the first call for each function on D.
  4893. **
  4894. ** The commit and rollback hook callbacks are not reentrant.
  4895. ** The callback implementation must not do anything that will modify
  4896. ** the database connection that invoked the callback. Any actions
  4897. ** to modify the database connection must be deferred until after the
  4898. ** completion of the [sqlite3_step()] call that triggered the commit
  4899. ** or rollback hook in the first place.
  4900. ** Note that running any other SQL statements, including SELECT statements,
  4901. ** or merely calling [sqlite3_prepare_v2()] and [sqlite3_step()] will modify
  4902. ** the database connections for the meaning of "modify" in this paragraph.
  4903. **
  4904. ** ^Registering a NULL function disables the callback.
  4905. **
  4906. ** ^When the commit hook callback routine returns zero, the [COMMIT]
  4907. ** operation is allowed to continue normally. ^If the commit hook
  4908. ** returns non-zero, then the [COMMIT] is converted into a [ROLLBACK].
  4909. ** ^The rollback hook is invoked on a rollback that results from a commit
  4910. ** hook returning non-zero, just as it would be with any other rollback.
  4911. **
  4912. ** ^For the purposes of this API, a transaction is said to have been
  4913. ** rolled back if an explicit "ROLLBACK" statement is executed, or
  4914. ** an error or constraint causes an implicit rollback to occur.
  4915. ** ^The rollback callback is not invoked if a transaction is
  4916. ** automatically rolled back because the database connection is closed.
  4917. **
  4918. ** See also the [sqlite3_update_hook()] interface.
  4919. */
  4920. SQLITE_API void *sqlite3_commit_hook(sqlite3*, int(*)(void*), void*);
  4921. SQLITE_API void *sqlite3_rollback_hook(sqlite3*, void(*)(void *), void*);
  4922. /*
  4923. ** CAPI3REF: Data Change Notification Callbacks
  4924. **
  4925. ** ^The sqlite3_update_hook() interface registers a callback function
  4926. ** with the [database connection] identified by the first argument
  4927. ** to be invoked whenever a row is updated, inserted or deleted.
  4928. ** ^Any callback set by a previous call to this function
  4929. ** for the same database connection is overridden.
  4930. **
  4931. ** ^The second argument is a pointer to the function to invoke when a
  4932. ** row is updated, inserted or deleted.
  4933. ** ^The first argument to the callback is a copy of the third argument
  4934. ** to sqlite3_update_hook().
  4935. ** ^The second callback argument is one of [SQLITE_INSERT], [SQLITE_DELETE],
  4936. ** or [SQLITE_UPDATE], depending on the operation that caused the callback
  4937. ** to be invoked.
  4938. ** ^The third and fourth arguments to the callback contain pointers to the
  4939. ** database and table name containing the affected row.
  4940. ** ^The final callback parameter is the [rowid] of the row.
  4941. ** ^In the case of an update, this is the [rowid] after the update takes place.
  4942. **
  4943. ** ^(The update hook is not invoked when internal system tables are
  4944. ** modified (i.e. sqlite_master and sqlite_sequence).)^
  4945. **
  4946. ** ^In the current implementation, the update hook
  4947. ** is not invoked when duplication rows are deleted because of an
  4948. ** [ON CONFLICT | ON CONFLICT REPLACE] clause. ^Nor is the update hook
  4949. ** invoked when rows are deleted using the [truncate optimization].
  4950. ** The exceptions defined in this paragraph might change in a future
  4951. ** release of SQLite.
  4952. **
  4953. ** The update hook implementation must not do anything that will modify
  4954. ** the database connection that invoked the update hook. Any actions
  4955. ** to modify the database connection must be deferred until after the
  4956. ** completion of the [sqlite3_step()] call that triggered the update hook.
  4957. ** Note that [sqlite3_prepare_v2()] and [sqlite3_step()] both modify their
  4958. ** database connections for the meaning of "modify" in this paragraph.
  4959. **
  4960. ** ^The sqlite3_update_hook(D,C,P) function
  4961. ** returns the P argument from the previous call
  4962. ** on the same [database connection] D, or NULL for
  4963. ** the first call on D.
  4964. **
  4965. ** See also the [sqlite3_commit_hook()] and [sqlite3_rollback_hook()]
  4966. ** interfaces.
  4967. */
  4968. SQLITE_API void *sqlite3_update_hook(
  4969. sqlite3*,
  4970. void(*)(void *,int ,char const *,char const *,sqlite3_int64),
  4971. void*
  4972. );
  4973. /*
  4974. ** CAPI3REF: Enable Or Disable Shared Pager Cache
  4975. ** KEYWORDS: {shared cache}
  4976. **
  4977. ** ^(This routine enables or disables the sharing of the database cache
  4978. ** and schema data structures between [database connection | connections]
  4979. ** to the same database. Sharing is enabled if the argument is true
  4980. ** and disabled if the argument is false.)^
  4981. **
  4982. ** ^Cache sharing is enabled and disabled for an entire process.
  4983. ** This is a change as of SQLite version 3.5.0. In prior versions of SQLite,
  4984. ** sharing was enabled or disabled for each thread separately.
  4985. **
  4986. ** ^(The cache sharing mode set by this interface effects all subsequent
  4987. ** calls to [sqlite3_open()], [sqlite3_open_v2()], and [sqlite3_open16()].
  4988. ** Existing database connections continue use the sharing mode
  4989. ** that was in effect at the time they were opened.)^
  4990. **
  4991. ** ^(This routine returns [SQLITE_OK] if shared cache was enabled or disabled
  4992. ** successfully. An [error code] is returned otherwise.)^
  4993. **
  4994. ** ^Shared cache is disabled by default. But this might change in
  4995. ** future releases of SQLite. Applications that care about shared
  4996. ** cache setting should set it explicitly.
  4997. **
  4998. ** See Also: [SQLite Shared-Cache Mode]
  4999. */
  5000. SQLITE_API int sqlite3_enable_shared_cache(int);
  5001. /*
  5002. ** CAPI3REF: Attempt To Free Heap Memory
  5003. **
  5004. ** ^The sqlite3_release_memory() interface attempts to free N bytes
  5005. ** of heap memory by deallocating non-essential memory allocations
  5006. ** held by the database library. Memory used to cache database
  5007. ** pages to improve performance is an example of non-essential memory.
  5008. ** ^sqlite3_release_memory() returns the number of bytes actually freed,
  5009. ** which might be more or less than the amount requested.
  5010. ** ^The sqlite3_release_memory() routine is a no-op returning zero
  5011. ** if SQLite is not compiled with [SQLITE_ENABLE_MEMORY_MANAGEMENT].
  5012. **
  5013. ** See also: [sqlite3_db_release_memory()]
  5014. */
  5015. SQLITE_API int sqlite3_release_memory(int);
  5016. /*
  5017. ** CAPI3REF: Free Memory Used By A Database Connection
  5018. **
  5019. ** ^The sqlite3_db_release_memory(D) interface attempts to free as much heap
  5020. ** memory as possible from database connection D. Unlike the
  5021. ** [sqlite3_release_memory()] interface, this interface is effect even
  5022. ** when then [SQLITE_ENABLE_MEMORY_MANAGEMENT] compile-time option is
  5023. ** omitted.
  5024. **
  5025. ** See also: [sqlite3_release_memory()]
  5026. */
  5027. SQLITE_API int sqlite3_db_release_memory(sqlite3*);
  5028. /*
  5029. ** CAPI3REF: Impose A Limit On Heap Size
  5030. **
  5031. ** ^The sqlite3_soft_heap_limit64() interface sets and/or queries the
  5032. ** soft limit on the amount of heap memory that may be allocated by SQLite.
  5033. ** ^SQLite strives to keep heap memory utilization below the soft heap
  5034. ** limit by reducing the number of pages held in the page cache
  5035. ** as heap memory usages approaches the limit.
  5036. ** ^The soft heap limit is "soft" because even though SQLite strives to stay
  5037. ** below the limit, it will exceed the limit rather than generate
  5038. ** an [SQLITE_NOMEM] error. In other words, the soft heap limit
  5039. ** is advisory only.
  5040. **
  5041. ** ^The return value from sqlite3_soft_heap_limit64() is the size of
  5042. ** the soft heap limit prior to the call, or negative in the case of an
  5043. ** error. ^If the argument N is negative
  5044. ** then no change is made to the soft heap limit. Hence, the current
  5045. ** size of the soft heap limit can be determined by invoking
  5046. ** sqlite3_soft_heap_limit64() with a negative argument.
  5047. **
  5048. ** ^If the argument N is zero then the soft heap limit is disabled.
  5049. **
  5050. ** ^(The soft heap limit is not enforced in the current implementation
  5051. ** if one or more of following conditions are true:
  5052. **
  5053. ** <ul>
  5054. ** <li> The soft heap limit is set to zero.
  5055. ** <li> Memory accounting is disabled using a combination of the
  5056. ** [sqlite3_config]([SQLITE_CONFIG_MEMSTATUS],...) start-time option and
  5057. ** the [SQLITE_DEFAULT_MEMSTATUS] compile-time option.
  5058. ** <li> An alternative page cache implementation is specified using
  5059. ** [sqlite3_config]([SQLITE_CONFIG_PCACHE2],...).
  5060. ** <li> The page cache allocates from its own memory pool supplied
  5061. ** by [sqlite3_config]([SQLITE_CONFIG_PAGECACHE],...) rather than
  5062. ** from the heap.
  5063. ** </ul>)^
  5064. **
  5065. ** Beginning with SQLite version 3.7.3, the soft heap limit is enforced
  5066. ** regardless of whether or not the [SQLITE_ENABLE_MEMORY_MANAGEMENT]
  5067. ** compile-time option is invoked. With [SQLITE_ENABLE_MEMORY_MANAGEMENT],
  5068. ** the soft heap limit is enforced on every memory allocation. Without
  5069. ** [SQLITE_ENABLE_MEMORY_MANAGEMENT], the soft heap limit is only enforced
  5070. ** when memory is allocated by the page cache. Testing suggests that because
  5071. ** the page cache is the predominate memory user in SQLite, most
  5072. ** applications will achieve adequate soft heap limit enforcement without
  5073. ** the use of [SQLITE_ENABLE_MEMORY_MANAGEMENT].
  5074. **
  5075. ** The circumstances under which SQLite will enforce the soft heap limit may
  5076. ** changes in future releases of SQLite.
  5077. */
  5078. SQLITE_API sqlite3_int64 sqlite3_soft_heap_limit64(sqlite3_int64 N);
  5079. /*
  5080. ** CAPI3REF: Deprecated Soft Heap Limit Interface
  5081. ** DEPRECATED
  5082. **
  5083. ** This is a deprecated version of the [sqlite3_soft_heap_limit64()]
  5084. ** interface. This routine is provided for historical compatibility
  5085. ** only. All new applications should use the
  5086. ** [sqlite3_soft_heap_limit64()] interface rather than this one.
  5087. */
  5088. SQLITE_API SQLITE_DEPRECATED void sqlite3_soft_heap_limit(int N);
  5089. /*
  5090. ** CAPI3REF: Extract Metadata About A Column Of A Table
  5091. **
  5092. ** ^This routine returns metadata about a specific column of a specific
  5093. ** database table accessible using the [database connection] handle
  5094. ** passed as the first function argument.
  5095. **
  5096. ** ^The column is identified by the second, third and fourth parameters to
  5097. ** this function. ^The second parameter is either the name of the database
  5098. ** (i.e. "main", "temp", or an attached database) containing the specified
  5099. ** table or NULL. ^If it is NULL, then all attached databases are searched
  5100. ** for the table using the same algorithm used by the database engine to
  5101. ** resolve unqualified table references.
  5102. **
  5103. ** ^The third and fourth parameters to this function are the table and column
  5104. ** name of the desired column, respectively. Neither of these parameters
  5105. ** may be NULL.
  5106. **
  5107. ** ^Metadata is returned by writing to the memory locations passed as the 5th
  5108. ** and subsequent parameters to this function. ^Any of these arguments may be
  5109. ** NULL, in which case the corresponding element of metadata is omitted.
  5110. **
  5111. ** ^(<blockquote>
  5112. ** <table border="1">
  5113. ** <tr><th> Parameter <th> Output<br>Type <th> Description
  5114. **
  5115. ** <tr><td> 5th <td> const char* <td> Data type
  5116. ** <tr><td> 6th <td> const char* <td> Name of default collation sequence
  5117. ** <tr><td> 7th <td> int <td> True if column has a NOT NULL constraint
  5118. ** <tr><td> 8th <td> int <td> True if column is part of the PRIMARY KEY
  5119. ** <tr><td> 9th <td> int <td> True if column is [AUTOINCREMENT]
  5120. ** </table>
  5121. ** </blockquote>)^
  5122. **
  5123. ** ^The memory pointed to by the character pointers returned for the
  5124. ** declaration type and collation sequence is valid only until the next
  5125. ** call to any SQLite API function.
  5126. **
  5127. ** ^If the specified table is actually a view, an [error code] is returned.
  5128. **
  5129. ** ^If the specified column is "rowid", "oid" or "_rowid_" and an
  5130. ** [INTEGER PRIMARY KEY] column has been explicitly declared, then the output
  5131. ** parameters are set for the explicitly declared column. ^(If there is no
  5132. ** explicitly declared [INTEGER PRIMARY KEY] column, then the output
  5133. ** parameters are set as follows:
  5134. **
  5135. ** <pre>
  5136. ** data type: "INTEGER"
  5137. ** collation sequence: "BINARY"
  5138. ** not null: 0
  5139. ** primary key: 1
  5140. ** auto increment: 0
  5141. ** </pre>)^
  5142. **
  5143. ** ^(This function may load one or more schemas from database files. If an
  5144. ** error occurs during this process, or if the requested table or column
  5145. ** cannot be found, an [error code] is returned and an error message left
  5146. ** in the [database connection] (to be retrieved using sqlite3_errmsg()).)^
  5147. **
  5148. ** ^This API is only available if the library was compiled with the
  5149. ** [SQLITE_ENABLE_COLUMN_METADATA] C-preprocessor symbol defined.
  5150. */
  5151. SQLITE_API int sqlite3_table_column_metadata(
  5152. sqlite3 *db, /* Connection handle */
  5153. const char *zDbName, /* Database name or NULL */
  5154. const char *zTableName, /* Table name */
  5155. const char *zColumnName, /* Column name */
  5156. char const **pzDataType, /* OUTPUT: Declared data type */
  5157. char const **pzCollSeq, /* OUTPUT: Collation sequence name */
  5158. int *pNotNull, /* OUTPUT: True if NOT NULL constraint exists */
  5159. int *pPrimaryKey, /* OUTPUT: True if column part of PK */
  5160. int *pAutoinc /* OUTPUT: True if column is auto-increment */
  5161. );
  5162. /*
  5163. ** CAPI3REF: Load An Extension
  5164. **
  5165. ** ^This interface loads an SQLite extension library from the named file.
  5166. **
  5167. ** ^The sqlite3_load_extension() interface attempts to load an
  5168. ** SQLite extension library contained in the file zFile.
  5169. **
  5170. ** ^The entry point is zProc.
  5171. ** ^zProc may be 0, in which case the name of the entry point
  5172. ** defaults to "sqlite3_extension_init".
  5173. ** ^The sqlite3_load_extension() interface returns
  5174. ** [SQLITE_OK] on success and [SQLITE_ERROR] if something goes wrong.
  5175. ** ^If an error occurs and pzErrMsg is not 0, then the
  5176. ** [sqlite3_load_extension()] interface shall attempt to
  5177. ** fill *pzErrMsg with error message text stored in memory
  5178. ** obtained from [sqlite3_malloc()]. The calling function
  5179. ** should free this memory by calling [sqlite3_free()].
  5180. **
  5181. ** ^Extension loading must be enabled using
  5182. ** [sqlite3_enable_load_extension()] prior to calling this API,
  5183. ** otherwise an error will be returned.
  5184. **
  5185. ** See also the [load_extension() SQL function].
  5186. */
  5187. SQLITE_API int sqlite3_load_extension(
  5188. sqlite3 *db, /* Load the extension into this database connection */
  5189. const char *zFile, /* Name of the shared library containing extension */
  5190. const char *zProc, /* Entry point. Derived from zFile if 0 */
  5191. char **pzErrMsg /* Put error message here if not 0 */
  5192. );
  5193. /*
  5194. ** CAPI3REF: Enable Or Disable Extension Loading
  5195. **
  5196. ** ^So as not to open security holes in older applications that are
  5197. ** unprepared to deal with extension loading, and as a means of disabling
  5198. ** extension loading while evaluating user-entered SQL, the following API
  5199. ** is provided to turn the [sqlite3_load_extension()] mechanism on and off.
  5200. **
  5201. ** ^Extension loading is off by default. See ticket #1863.
  5202. ** ^Call the sqlite3_enable_load_extension() routine with onoff==1
  5203. ** to turn extension loading on and call it with onoff==0 to turn
  5204. ** it back off again.
  5205. */
  5206. SQLITE_API int sqlite3_enable_load_extension(sqlite3 *db, int onoff);
  5207. /*
  5208. ** CAPI3REF: Automatically Load Statically Linked Extensions
  5209. **
  5210. ** ^This interface causes the xEntryPoint() function to be invoked for
  5211. ** each new [database connection] that is created. The idea here is that
  5212. ** xEntryPoint() is the entry point for a statically linked SQLite extension
  5213. ** that is to be automatically loaded into all new database connections.
  5214. **
  5215. ** ^(Even though the function prototype shows that xEntryPoint() takes
  5216. ** no arguments and returns void, SQLite invokes xEntryPoint() with three
  5217. ** arguments and expects and integer result as if the signature of the
  5218. ** entry point where as follows:
  5219. **
  5220. ** <blockquote><pre>
  5221. ** &nbsp; int xEntryPoint(
  5222. ** &nbsp; sqlite3 *db,
  5223. ** &nbsp; const char **pzErrMsg,
  5224. ** &nbsp; const struct sqlite3_api_routines *pThunk
  5225. ** &nbsp; );
  5226. ** </pre></blockquote>)^
  5227. **
  5228. ** If the xEntryPoint routine encounters an error, it should make *pzErrMsg
  5229. ** point to an appropriate error message (obtained from [sqlite3_mprintf()])
  5230. ** and return an appropriate [error code]. ^SQLite ensures that *pzErrMsg
  5231. ** is NULL before calling the xEntryPoint(). ^SQLite will invoke
  5232. ** [sqlite3_free()] on *pzErrMsg after xEntryPoint() returns. ^If any
  5233. ** xEntryPoint() returns an error, the [sqlite3_open()], [sqlite3_open16()],
  5234. ** or [sqlite3_open_v2()] call that provoked the xEntryPoint() will fail.
  5235. **
  5236. ** ^Calling sqlite3_auto_extension(X) with an entry point X that is already
  5237. ** on the list of automatic extensions is a harmless no-op. ^No entry point
  5238. ** will be called more than once for each database connection that is opened.
  5239. **
  5240. ** See also: [sqlite3_reset_auto_extension()].
  5241. */
  5242. SQLITE_API int sqlite3_auto_extension(void (*xEntryPoint)(void));
  5243. /*
  5244. ** CAPI3REF: Reset Automatic Extension Loading
  5245. **
  5246. ** ^This interface disables all automatic extensions previously
  5247. ** registered using [sqlite3_auto_extension()].
  5248. */
  5249. SQLITE_API void sqlite3_reset_auto_extension(void);
  5250. /*
  5251. ** The interface to the virtual-table mechanism is currently considered
  5252. ** to be experimental. The interface might change in incompatible ways.
  5253. ** If this is a problem for you, do not use the interface at this time.
  5254. **
  5255. ** When the virtual-table mechanism stabilizes, we will declare the
  5256. ** interface fixed, support it indefinitely, and remove this comment.
  5257. */
  5258. /*
  5259. ** Structures used by the virtual table interface
  5260. */
  5261. typedef struct sqlite3_vtab sqlite3_vtab;
  5262. typedef struct sqlite3_index_info sqlite3_index_info;
  5263. typedef struct sqlite3_vtab_cursor sqlite3_vtab_cursor;
  5264. typedef struct sqlite3_module sqlite3_module;
  5265. /*
  5266. ** CAPI3REF: Virtual Table Object
  5267. ** KEYWORDS: sqlite3_module {virtual table module}
  5268. **
  5269. ** This structure, sometimes called a "virtual table module",
  5270. ** defines the implementation of a [virtual tables].
  5271. ** This structure consists mostly of methods for the module.
  5272. **
  5273. ** ^A virtual table module is created by filling in a persistent
  5274. ** instance of this structure and passing a pointer to that instance
  5275. ** to [sqlite3_create_module()] or [sqlite3_create_module_v2()].
  5276. ** ^The registration remains valid until it is replaced by a different
  5277. ** module or until the [database connection] closes. The content
  5278. ** of this structure must not change while it is registered with
  5279. ** any database connection.
  5280. */
  5281. struct sqlite3_module {
  5282. int iVersion;
  5283. int (*xCreate)(sqlite3*, void *pAux,
  5284. int argc, const char *const*argv,
  5285. sqlite3_vtab **ppVTab, char**);
  5286. int (*xConnect)(sqlite3*, void *pAux,
  5287. int argc, const char *const*argv,
  5288. sqlite3_vtab **ppVTab, char**);
  5289. int (*xBestIndex)(sqlite3_vtab *pVTab, sqlite3_index_info*);
  5290. int (*xDisconnect)(sqlite3_vtab *pVTab);
  5291. int (*xDestroy)(sqlite3_vtab *pVTab);
  5292. int (*xOpen)(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCursor);
  5293. int (*xClose)(sqlite3_vtab_cursor*);
  5294. int (*xFilter)(sqlite3_vtab_cursor*, int idxNum, const char *idxStr,
  5295. int argc, sqlite3_value **argv);
  5296. int (*xNext)(sqlite3_vtab_cursor*);
  5297. int (*xEof)(sqlite3_vtab_cursor*);
  5298. int (*xColumn)(sqlite3_vtab_cursor*, sqlite3_context*, int);
  5299. int (*xRowid)(sqlite3_vtab_cursor*, sqlite3_int64 *pRowid);
  5300. int (*xUpdate)(sqlite3_vtab *, int, sqlite3_value **, sqlite3_int64 *);
  5301. int (*xBegin)(sqlite3_vtab *pVTab);
  5302. int (*xSync)(sqlite3_vtab *pVTab);
  5303. int (*xCommit)(sqlite3_vtab *pVTab);
  5304. int (*xRollback)(sqlite3_vtab *pVTab);
  5305. int (*xFindFunction)(sqlite3_vtab *pVtab, int nArg, const char *zName,
  5306. void (**pxFunc)(sqlite3_context*,int,sqlite3_value**),
  5307. void **ppArg);
  5308. int (*xRename)(sqlite3_vtab *pVtab, const char *zNew);
  5309. /* The methods above are in version 1 of the sqlite_module object. Those
  5310. ** below are for version 2 and greater. */
  5311. int (*xSavepoint)(sqlite3_vtab *pVTab, int);
  5312. int (*xRelease)(sqlite3_vtab *pVTab, int);
  5313. int (*xRollbackTo)(sqlite3_vtab *pVTab, int);
  5314. };
  5315. /*
  5316. ** CAPI3REF: Virtual Table Indexing Information
  5317. ** KEYWORDS: sqlite3_index_info
  5318. **
  5319. ** The sqlite3_index_info structure and its substructures is used as part
  5320. ** of the [virtual table] interface to
  5321. ** pass information into and receive the reply from the [xBestIndex]
  5322. ** method of a [virtual table module]. The fields under **Inputs** are the
  5323. ** inputs to xBestIndex and are read-only. xBestIndex inserts its
  5324. ** results into the **Outputs** fields.
  5325. **
  5326. ** ^(The aConstraint[] array records WHERE clause constraints of the form:
  5327. **
  5328. ** <blockquote>column OP expr</blockquote>
  5329. **
  5330. ** where OP is =, &lt;, &lt;=, &gt;, or &gt;=.)^ ^(The particular operator is
  5331. ** stored in aConstraint[].op using one of the
  5332. ** [SQLITE_INDEX_CONSTRAINT_EQ | SQLITE_INDEX_CONSTRAINT_ values].)^
  5333. ** ^(The index of the column is stored in
  5334. ** aConstraint[].iColumn.)^ ^(aConstraint[].usable is TRUE if the
  5335. ** expr on the right-hand side can be evaluated (and thus the constraint
  5336. ** is usable) and false if it cannot.)^
  5337. **
  5338. ** ^The optimizer automatically inverts terms of the form "expr OP column"
  5339. ** and makes other simplifications to the WHERE clause in an attempt to
  5340. ** get as many WHERE clause terms into the form shown above as possible.
  5341. ** ^The aConstraint[] array only reports WHERE clause terms that are
  5342. ** relevant to the particular virtual table being queried.
  5343. **
  5344. ** ^Information about the ORDER BY clause is stored in aOrderBy[].
  5345. ** ^Each term of aOrderBy records a column of the ORDER BY clause.
  5346. **
  5347. ** The [xBestIndex] method must fill aConstraintUsage[] with information
  5348. ** about what parameters to pass to xFilter. ^If argvIndex>0 then
  5349. ** the right-hand side of the corresponding aConstraint[] is evaluated
  5350. ** and becomes the argvIndex-th entry in argv. ^(If aConstraintUsage[].omit
  5351. ** is true, then the constraint is assumed to be fully handled by the
  5352. ** virtual table and is not checked again by SQLite.)^
  5353. **
  5354. ** ^The idxNum and idxPtr values are recorded and passed into the
  5355. ** [xFilter] method.
  5356. ** ^[sqlite3_free()] is used to free idxPtr if and only if
  5357. ** needToFreeIdxPtr is true.
  5358. **
  5359. ** ^The orderByConsumed means that output from [xFilter]/[xNext] will occur in
  5360. ** the correct order to satisfy the ORDER BY clause so that no separate
  5361. ** sorting step is required.
  5362. **
  5363. ** ^The estimatedCost value is an estimate of the cost of doing the
  5364. ** particular lookup. A full scan of a table with N entries should have
  5365. ** a cost of N. A binary search of a table of N entries should have a
  5366. ** cost of approximately log(N).
  5367. */
  5368. struct sqlite3_index_info {
  5369. /* Inputs */
  5370. int nConstraint; /* Number of entries in aConstraint */
  5371. struct sqlite3_index_constraint {
  5372. int iColumn; /* Column on left-hand side of constraint */
  5373. unsigned char op; /* Constraint operator */
  5374. unsigned char usable; /* True if this constraint is usable */
  5375. int iTermOffset; /* Used internally - xBestIndex should ignore */
  5376. } *aConstraint; /* Table of WHERE clause constraints */
  5377. int nOrderBy; /* Number of terms in the ORDER BY clause */
  5378. struct sqlite3_index_orderby {
  5379. int iColumn; /* Column number */
  5380. unsigned char desc; /* True for DESC. False for ASC. */
  5381. } *aOrderBy; /* The ORDER BY clause */
  5382. /* Outputs */
  5383. struct sqlite3_index_constraint_usage {
  5384. int argvIndex; /* if >0, constraint is part of argv to xFilter */
  5385. unsigned char omit; /* Do not code a test for this constraint */
  5386. } *aConstraintUsage;
  5387. int idxNum; /* Number used to identify the index */
  5388. char *idxStr; /* String, possibly obtained from sqlite3_malloc */
  5389. int needToFreeIdxStr; /* Free idxStr using sqlite3_free() if true */
  5390. int orderByConsumed; /* True if output is already ordered */
  5391. double estimatedCost; /* Estimated cost of using this index */
  5392. };
  5393. /*
  5394. ** CAPI3REF: Virtual Table Constraint Operator Codes
  5395. **
  5396. ** These macros defined the allowed values for the
  5397. ** [sqlite3_index_info].aConstraint[].op field. Each value represents
  5398. ** an operator that is part of a constraint term in the wHERE clause of
  5399. ** a query that uses a [virtual table].
  5400. */
  5401. #define SQLITE_INDEX_CONSTRAINT_EQ 2
  5402. #define SQLITE_INDEX_CONSTRAINT_GT 4
  5403. #define SQLITE_INDEX_CONSTRAINT_LE 8
  5404. #define SQLITE_INDEX_CONSTRAINT_LT 16
  5405. #define SQLITE_INDEX_CONSTRAINT_GE 32
  5406. #define SQLITE_INDEX_CONSTRAINT_MATCH 64
  5407. /*
  5408. ** CAPI3REF: Register A Virtual Table Implementation
  5409. **
  5410. ** ^These routines are used to register a new [virtual table module] name.
  5411. ** ^Module names must be registered before
  5412. ** creating a new [virtual table] using the module and before using a
  5413. ** preexisting [virtual table] for the module.
  5414. **
  5415. ** ^The module name is registered on the [database connection] specified
  5416. ** by the first parameter. ^The name of the module is given by the
  5417. ** second parameter. ^The third parameter is a pointer to
  5418. ** the implementation of the [virtual table module]. ^The fourth
  5419. ** parameter is an arbitrary client data pointer that is passed through
  5420. ** into the [xCreate] and [xConnect] methods of the virtual table module
  5421. ** when a new virtual table is be being created or reinitialized.
  5422. **
  5423. ** ^The sqlite3_create_module_v2() interface has a fifth parameter which
  5424. ** is a pointer to a destructor for the pClientData. ^SQLite will
  5425. ** invoke the destructor function (if it is not NULL) when SQLite
  5426. ** no longer needs the pClientData pointer. ^The destructor will also
  5427. ** be invoked if the call to sqlite3_create_module_v2() fails.
  5428. ** ^The sqlite3_create_module()
  5429. ** interface is equivalent to sqlite3_create_module_v2() with a NULL
  5430. ** destructor.
  5431. */
  5432. SQLITE_API int sqlite3_create_module(
  5433. sqlite3 *db, /* SQLite connection to register module with */
  5434. const char *zName, /* Name of the module */
  5435. const sqlite3_module *p, /* Methods for the module */
  5436. void *pClientData /* Client data for xCreate/xConnect */
  5437. );
  5438. SQLITE_API int sqlite3_create_module_v2(
  5439. sqlite3 *db, /* SQLite connection to register module with */
  5440. const char *zName, /* Name of the module */
  5441. const sqlite3_module *p, /* Methods for the module */
  5442. void *pClientData, /* Client data for xCreate/xConnect */
  5443. void(*xDestroy)(void*) /* Module destructor function */
  5444. );
  5445. /*
  5446. ** CAPI3REF: Virtual Table Instance Object
  5447. ** KEYWORDS: sqlite3_vtab
  5448. **
  5449. ** Every [virtual table module] implementation uses a subclass
  5450. ** of this object to describe a particular instance
  5451. ** of the [virtual table]. Each subclass will
  5452. ** be tailored to the specific needs of the module implementation.
  5453. ** The purpose of this superclass is to define certain fields that are
  5454. ** common to all module implementations.
  5455. **
  5456. ** ^Virtual tables methods can set an error message by assigning a
  5457. ** string obtained from [sqlite3_mprintf()] to zErrMsg. The method should
  5458. ** take care that any prior string is freed by a call to [sqlite3_free()]
  5459. ** prior to assigning a new string to zErrMsg. ^After the error message
  5460. ** is delivered up to the client application, the string will be automatically
  5461. ** freed by sqlite3_free() and the zErrMsg field will be zeroed.
  5462. */
  5463. struct sqlite3_vtab {
  5464. const sqlite3_module *pModule; /* The module for this virtual table */
  5465. int nRef; /* NO LONGER USED */
  5466. char *zErrMsg; /* Error message from sqlite3_mprintf() */
  5467. /* Virtual table implementations will typically add additional fields */
  5468. };
  5469. /*
  5470. ** CAPI3REF: Virtual Table Cursor Object
  5471. ** KEYWORDS: sqlite3_vtab_cursor {virtual table cursor}
  5472. **
  5473. ** Every [virtual table module] implementation uses a subclass of the
  5474. ** following structure to describe cursors that point into the
  5475. ** [virtual table] and are used
  5476. ** to loop through the virtual table. Cursors are created using the
  5477. ** [sqlite3_module.xOpen | xOpen] method of the module and are destroyed
  5478. ** by the [sqlite3_module.xClose | xClose] method. Cursors are used
  5479. ** by the [xFilter], [xNext], [xEof], [xColumn], and [xRowid] methods
  5480. ** of the module. Each module implementation will define
  5481. ** the content of a cursor structure to suit its own needs.
  5482. **
  5483. ** This superclass exists in order to define fields of the cursor that
  5484. ** are common to all implementations.
  5485. */
  5486. struct sqlite3_vtab_cursor {
  5487. sqlite3_vtab *pVtab; /* Virtual table of this cursor */
  5488. /* Virtual table implementations will typically add additional fields */
  5489. };
  5490. /*
  5491. ** CAPI3REF: Declare The Schema Of A Virtual Table
  5492. **
  5493. ** ^The [xCreate] and [xConnect] methods of a
  5494. ** [virtual table module] call this interface
  5495. ** to declare the format (the names and datatypes of the columns) of
  5496. ** the virtual tables they implement.
  5497. */
  5498. SQLITE_API int sqlite3_declare_vtab(sqlite3*, const char *zSQL);
  5499. /*
  5500. ** CAPI3REF: Overload A Function For A Virtual Table
  5501. **
  5502. ** ^(Virtual tables can provide alternative implementations of functions
  5503. ** using the [xFindFunction] method of the [virtual table module].
  5504. ** But global versions of those functions
  5505. ** must exist in order to be overloaded.)^
  5506. **
  5507. ** ^(This API makes sure a global version of a function with a particular
  5508. ** name and number of parameters exists. If no such function exists
  5509. ** before this API is called, a new function is created.)^ ^The implementation
  5510. ** of the new function always causes an exception to be thrown. So
  5511. ** the new function is not good for anything by itself. Its only
  5512. ** purpose is to be a placeholder function that can be overloaded
  5513. ** by a [virtual table].
  5514. */
  5515. SQLITE_API int sqlite3_overload_function(sqlite3*, const char *zFuncName, int nArg);
  5516. /*
  5517. ** The interface to the virtual-table mechanism defined above (back up
  5518. ** to a comment remarkably similar to this one) is currently considered
  5519. ** to be experimental. The interface might change in incompatible ways.
  5520. ** If this is a problem for you, do not use the interface at this time.
  5521. **
  5522. ** When the virtual-table mechanism stabilizes, we will declare the
  5523. ** interface fixed, support it indefinitely, and remove this comment.
  5524. */
  5525. /*
  5526. ** CAPI3REF: A Handle To An Open BLOB
  5527. ** KEYWORDS: {BLOB handle} {BLOB handles}
  5528. **
  5529. ** An instance of this object represents an open BLOB on which
  5530. ** [sqlite3_blob_open | incremental BLOB I/O] can be performed.
  5531. ** ^Objects of this type are created by [sqlite3_blob_open()]
  5532. ** and destroyed by [sqlite3_blob_close()].
  5533. ** ^The [sqlite3_blob_read()] and [sqlite3_blob_write()] interfaces
  5534. ** can be used to read or write small subsections of the BLOB.
  5535. ** ^The [sqlite3_blob_bytes()] interface returns the size of the BLOB in bytes.
  5536. */
  5537. typedef struct sqlite3_blob sqlite3_blob;
  5538. /*
  5539. ** CAPI3REF: Open A BLOB For Incremental I/O
  5540. **
  5541. ** ^(This interfaces opens a [BLOB handle | handle] to the BLOB located
  5542. ** in row iRow, column zColumn, table zTable in database zDb;
  5543. ** in other words, the same BLOB that would be selected by:
  5544. **
  5545. ** <pre>
  5546. ** SELECT zColumn FROM zDb.zTable WHERE [rowid] = iRow;
  5547. ** </pre>)^
  5548. **
  5549. ** ^If the flags parameter is non-zero, then the BLOB is opened for read
  5550. ** and write access. ^If it is zero, the BLOB is opened for read access.
  5551. ** ^It is not possible to open a column that is part of an index or primary
  5552. ** key for writing. ^If [foreign key constraints] are enabled, it is
  5553. ** not possible to open a column that is part of a [child key] for writing.
  5554. **
  5555. ** ^Note that the database name is not the filename that contains
  5556. ** the database but rather the symbolic name of the database that
  5557. ** appears after the AS keyword when the database is connected using [ATTACH].
  5558. ** ^For the main database file, the database name is "main".
  5559. ** ^For TEMP tables, the database name is "temp".
  5560. **
  5561. ** ^(On success, [SQLITE_OK] is returned and the new [BLOB handle] is written
  5562. ** to *ppBlob. Otherwise an [error code] is returned and *ppBlob is set
  5563. ** to be a null pointer.)^
  5564. ** ^This function sets the [database connection] error code and message
  5565. ** accessible via [sqlite3_errcode()] and [sqlite3_errmsg()] and related
  5566. ** functions. ^Note that the *ppBlob variable is always initialized in a
  5567. ** way that makes it safe to invoke [sqlite3_blob_close()] on *ppBlob
  5568. ** regardless of the success or failure of this routine.
  5569. **
  5570. ** ^(If the row that a BLOB handle points to is modified by an
  5571. ** [UPDATE], [DELETE], or by [ON CONFLICT] side-effects
  5572. ** then the BLOB handle is marked as "expired".
  5573. ** This is true if any column of the row is changed, even a column
  5574. ** other than the one the BLOB handle is open on.)^
  5575. ** ^Calls to [sqlite3_blob_read()] and [sqlite3_blob_write()] for
  5576. ** an expired BLOB handle fail with a return code of [SQLITE_ABORT].
  5577. ** ^(Changes written into a BLOB prior to the BLOB expiring are not
  5578. ** rolled back by the expiration of the BLOB. Such changes will eventually
  5579. ** commit if the transaction continues to completion.)^
  5580. **
  5581. ** ^Use the [sqlite3_blob_bytes()] interface to determine the size of
  5582. ** the opened blob. ^The size of a blob may not be changed by this
  5583. ** interface. Use the [UPDATE] SQL command to change the size of a
  5584. ** blob.
  5585. **
  5586. ** ^The [sqlite3_bind_zeroblob()] and [sqlite3_result_zeroblob()] interfaces
  5587. ** and the built-in [zeroblob] SQL function can be used, if desired,
  5588. ** to create an empty, zero-filled blob in which to read or write using
  5589. ** this interface.
  5590. **
  5591. ** To avoid a resource leak, every open [BLOB handle] should eventually
  5592. ** be released by a call to [sqlite3_blob_close()].
  5593. */
  5594. SQLITE_API int sqlite3_blob_open(
  5595. sqlite3*,
  5596. const char *zDb,
  5597. const char *zTable,
  5598. const char *zColumn,
  5599. sqlite3_int64 iRow,
  5600. int flags,
  5601. sqlite3_blob **ppBlob
  5602. );
  5603. /*
  5604. ** CAPI3REF: Move a BLOB Handle to a New Row
  5605. **
  5606. ** ^This function is used to move an existing blob handle so that it points
  5607. ** to a different row of the same database table. ^The new row is identified
  5608. ** by the rowid value passed as the second argument. Only the row can be
  5609. ** changed. ^The database, table and column on which the blob handle is open
  5610. ** remain the same. Moving an existing blob handle to a new row can be
  5611. ** faster than closing the existing handle and opening a new one.
  5612. **
  5613. ** ^(The new row must meet the same criteria as for [sqlite3_blob_open()] -
  5614. ** it must exist and there must be either a blob or text value stored in
  5615. ** the nominated column.)^ ^If the new row is not present in the table, or if
  5616. ** it does not contain a blob or text value, or if another error occurs, an
  5617. ** SQLite error code is returned and the blob handle is considered aborted.
  5618. ** ^All subsequent calls to [sqlite3_blob_read()], [sqlite3_blob_write()] or
  5619. ** [sqlite3_blob_reopen()] on an aborted blob handle immediately return
  5620. ** SQLITE_ABORT. ^Calling [sqlite3_blob_bytes()] on an aborted blob handle
  5621. ** always returns zero.
  5622. **
  5623. ** ^This function sets the database handle error code and message.
  5624. */
  5625. SQLITE_API SQLITE_EXPERIMENTAL int sqlite3_blob_reopen(sqlite3_blob *, sqlite3_int64);
  5626. /*
  5627. ** CAPI3REF: Close A BLOB Handle
  5628. **
  5629. ** ^Closes an open [BLOB handle].
  5630. **
  5631. ** ^Closing a BLOB shall cause the current transaction to commit
  5632. ** if there are no other BLOBs, no pending prepared statements, and the
  5633. ** database connection is in [autocommit mode].
  5634. ** ^If any writes were made to the BLOB, they might be held in cache
  5635. ** until the close operation if they will fit.
  5636. **
  5637. ** ^(Closing the BLOB often forces the changes
  5638. ** out to disk and so if any I/O errors occur, they will likely occur
  5639. ** at the time when the BLOB is closed. Any errors that occur during
  5640. ** closing are reported as a non-zero return value.)^
  5641. **
  5642. ** ^(The BLOB is closed unconditionally. Even if this routine returns
  5643. ** an error code, the BLOB is still closed.)^
  5644. **
  5645. ** ^Calling this routine with a null pointer (such as would be returned
  5646. ** by a failed call to [sqlite3_blob_open()]) is a harmless no-op.
  5647. */
  5648. SQLITE_API int sqlite3_blob_close(sqlite3_blob *);
  5649. /*
  5650. ** CAPI3REF: Return The Size Of An Open BLOB
  5651. **
  5652. ** ^Returns the size in bytes of the BLOB accessible via the
  5653. ** successfully opened [BLOB handle] in its only argument. ^The
  5654. ** incremental blob I/O routines can only read or overwriting existing
  5655. ** blob content; they cannot change the size of a blob.
  5656. **
  5657. ** This routine only works on a [BLOB handle] which has been created
  5658. ** by a prior successful call to [sqlite3_blob_open()] and which has not
  5659. ** been closed by [sqlite3_blob_close()]. Passing any other pointer in
  5660. ** to this routine results in undefined and probably undesirable behavior.
  5661. */
  5662. SQLITE_API int sqlite3_blob_bytes(sqlite3_blob *);
  5663. /*
  5664. ** CAPI3REF: Read Data From A BLOB Incrementally
  5665. **
  5666. ** ^(This function is used to read data from an open [BLOB handle] into a
  5667. ** caller-supplied buffer. N bytes of data are copied into buffer Z
  5668. ** from the open BLOB, starting at offset iOffset.)^
  5669. **
  5670. ** ^If offset iOffset is less than N bytes from the end of the BLOB,
  5671. ** [SQLITE_ERROR] is returned and no data is read. ^If N or iOffset is
  5672. ** less than zero, [SQLITE_ERROR] is returned and no data is read.
  5673. ** ^The size of the blob (and hence the maximum value of N+iOffset)
  5674. ** can be determined using the [sqlite3_blob_bytes()] interface.
  5675. **
  5676. ** ^An attempt to read from an expired [BLOB handle] fails with an
  5677. ** error code of [SQLITE_ABORT].
  5678. **
  5679. ** ^(On success, sqlite3_blob_read() returns SQLITE_OK.
  5680. ** Otherwise, an [error code] or an [extended error code] is returned.)^
  5681. **
  5682. ** This routine only works on a [BLOB handle] which has been created
  5683. ** by a prior successful call to [sqlite3_blob_open()] and which has not
  5684. ** been closed by [sqlite3_blob_close()]. Passing any other pointer in
  5685. ** to this routine results in undefined and probably undesirable behavior.
  5686. **
  5687. ** See also: [sqlite3_blob_write()].
  5688. */
  5689. SQLITE_API int sqlite3_blob_read(sqlite3_blob *, void *Z, int N, int iOffset);
  5690. /*
  5691. ** CAPI3REF: Write Data Into A BLOB Incrementally
  5692. **
  5693. ** ^This function is used to write data into an open [BLOB handle] from a
  5694. ** caller-supplied buffer. ^N bytes of data are copied from the buffer Z
  5695. ** into the open BLOB, starting at offset iOffset.
  5696. **
  5697. ** ^If the [BLOB handle] passed as the first argument was not opened for
  5698. ** writing (the flags parameter to [sqlite3_blob_open()] was zero),
  5699. ** this function returns [SQLITE_READONLY].
  5700. **
  5701. ** ^This function may only modify the contents of the BLOB; it is
  5702. ** not possible to increase the size of a BLOB using this API.
  5703. ** ^If offset iOffset is less than N bytes from the end of the BLOB,
  5704. ** [SQLITE_ERROR] is returned and no data is written. ^If N is
  5705. ** less than zero [SQLITE_ERROR] is returned and no data is written.
  5706. ** The size of the BLOB (and hence the maximum value of N+iOffset)
  5707. ** can be determined using the [sqlite3_blob_bytes()] interface.
  5708. **
  5709. ** ^An attempt to write to an expired [BLOB handle] fails with an
  5710. ** error code of [SQLITE_ABORT]. ^Writes to the BLOB that occurred
  5711. ** before the [BLOB handle] expired are not rolled back by the
  5712. ** expiration of the handle, though of course those changes might
  5713. ** have been overwritten by the statement that expired the BLOB handle
  5714. ** or by other independent statements.
  5715. **
  5716. ** ^(On success, sqlite3_blob_write() returns SQLITE_OK.
  5717. ** Otherwise, an [error code] or an [extended error code] is returned.)^
  5718. **
  5719. ** This routine only works on a [BLOB handle] which has been created
  5720. ** by a prior successful call to [sqlite3_blob_open()] and which has not
  5721. ** been closed by [sqlite3_blob_close()]. Passing any other pointer in
  5722. ** to this routine results in undefined and probably undesirable behavior.
  5723. **
  5724. ** See also: [sqlite3_blob_read()].
  5725. */
  5726. SQLITE_API int sqlite3_blob_write(sqlite3_blob *, const void *z, int n, int iOffset);
  5727. /*
  5728. ** CAPI3REF: Virtual File System Objects
  5729. **
  5730. ** A virtual filesystem (VFS) is an [sqlite3_vfs] object
  5731. ** that SQLite uses to interact
  5732. ** with the underlying operating system. Most SQLite builds come with a
  5733. ** single default VFS that is appropriate for the host computer.
  5734. ** New VFSes can be registered and existing VFSes can be unregistered.
  5735. ** The following interfaces are provided.
  5736. **
  5737. ** ^The sqlite3_vfs_find() interface returns a pointer to a VFS given its name.
  5738. ** ^Names are case sensitive.
  5739. ** ^Names are zero-terminated UTF-8 strings.
  5740. ** ^If there is no match, a NULL pointer is returned.
  5741. ** ^If zVfsName is NULL then the default VFS is returned.
  5742. **
  5743. ** ^New VFSes are registered with sqlite3_vfs_register().
  5744. ** ^Each new VFS becomes the default VFS if the makeDflt flag is set.
  5745. ** ^The same VFS can be registered multiple times without injury.
  5746. ** ^To make an existing VFS into the default VFS, register it again
  5747. ** with the makeDflt flag set. If two different VFSes with the
  5748. ** same name are registered, the behavior is undefined. If a
  5749. ** VFS is registered with a name that is NULL or an empty string,
  5750. ** then the behavior is undefined.
  5751. **
  5752. ** ^Unregister a VFS with the sqlite3_vfs_unregister() interface.
  5753. ** ^(If the default VFS is unregistered, another VFS is chosen as
  5754. ** the default. The choice for the new VFS is arbitrary.)^
  5755. */
  5756. SQLITE_API sqlite3_vfs *sqlite3_vfs_find(const char *zVfsName);
  5757. SQLITE_API int sqlite3_vfs_register(sqlite3_vfs*, int makeDflt);
  5758. SQLITE_API int sqlite3_vfs_unregister(sqlite3_vfs*);
  5759. /*
  5760. ** CAPI3REF: Mutexes
  5761. **
  5762. ** The SQLite core uses these routines for thread
  5763. ** synchronization. Though they are intended for internal
  5764. ** use by SQLite, code that links against SQLite is
  5765. ** permitted to use any of these routines.
  5766. **
  5767. ** The SQLite source code contains multiple implementations
  5768. ** of these mutex routines. An appropriate implementation
  5769. ** is selected automatically at compile-time. ^(The following
  5770. ** implementations are available in the SQLite core:
  5771. **
  5772. ** <ul>
  5773. ** <li> SQLITE_MUTEX_OS2
  5774. ** <li> SQLITE_MUTEX_PTHREADS
  5775. ** <li> SQLITE_MUTEX_W32
  5776. ** <li> SQLITE_MUTEX_NOOP
  5777. ** </ul>)^
  5778. **
  5779. ** ^The SQLITE_MUTEX_NOOP implementation is a set of routines
  5780. ** that does no real locking and is appropriate for use in
  5781. ** a single-threaded application. ^The SQLITE_MUTEX_OS2,
  5782. ** SQLITE_MUTEX_PTHREADS, and SQLITE_MUTEX_W32 implementations
  5783. ** are appropriate for use on OS/2, Unix, and Windows.
  5784. **
  5785. ** ^(If SQLite is compiled with the SQLITE_MUTEX_APPDEF preprocessor
  5786. ** macro defined (with "-DSQLITE_MUTEX_APPDEF=1"), then no mutex
  5787. ** implementation is included with the library. In this case the
  5788. ** application must supply a custom mutex implementation using the
  5789. ** [SQLITE_CONFIG_MUTEX] option of the sqlite3_config() function
  5790. ** before calling sqlite3_initialize() or any other public sqlite3_
  5791. ** function that calls sqlite3_initialize().)^
  5792. **
  5793. ** ^The sqlite3_mutex_alloc() routine allocates a new
  5794. ** mutex and returns a pointer to it. ^If it returns NULL
  5795. ** that means that a mutex could not be allocated. ^SQLite
  5796. ** will unwind its stack and return an error. ^(The argument
  5797. ** to sqlite3_mutex_alloc() is one of these integer constants:
  5798. **
  5799. ** <ul>
  5800. ** <li> SQLITE_MUTEX_FAST
  5801. ** <li> SQLITE_MUTEX_RECURSIVE
  5802. ** <li> SQLITE_MUTEX_STATIC_MASTER
  5803. ** <li> SQLITE_MUTEX_STATIC_MEM
  5804. ** <li> SQLITE_MUTEX_STATIC_MEM2
  5805. ** <li> SQLITE_MUTEX_STATIC_PRNG
  5806. ** <li> SQLITE_MUTEX_STATIC_LRU
  5807. ** <li> SQLITE_MUTEX_STATIC_LRU2
  5808. ** </ul>)^
  5809. **
  5810. ** ^The first two constants (SQLITE_MUTEX_FAST and SQLITE_MUTEX_RECURSIVE)
  5811. ** cause sqlite3_mutex_alloc() to create
  5812. ** a new mutex. ^The new mutex is recursive when SQLITE_MUTEX_RECURSIVE
  5813. ** is used but not necessarily so when SQLITE_MUTEX_FAST is used.
  5814. ** The mutex implementation does not need to make a distinction
  5815. ** between SQLITE_MUTEX_RECURSIVE and SQLITE_MUTEX_FAST if it does
  5816. ** not want to. ^SQLite will only request a recursive mutex in
  5817. ** cases where it really needs one. ^If a faster non-recursive mutex
  5818. ** implementation is available on the host platform, the mutex subsystem
  5819. ** might return such a mutex in response to SQLITE_MUTEX_FAST.
  5820. **
  5821. ** ^The other allowed parameters to sqlite3_mutex_alloc() (anything other
  5822. ** than SQLITE_MUTEX_FAST and SQLITE_MUTEX_RECURSIVE) each return
  5823. ** a pointer to a static preexisting mutex. ^Six static mutexes are
  5824. ** used by the current version of SQLite. Future versions of SQLite
  5825. ** may add additional static mutexes. Static mutexes are for internal
  5826. ** use by SQLite only. Applications that use SQLite mutexes should
  5827. ** use only the dynamic mutexes returned by SQLITE_MUTEX_FAST or
  5828. ** SQLITE_MUTEX_RECURSIVE.
  5829. **
  5830. ** ^Note that if one of the dynamic mutex parameters (SQLITE_MUTEX_FAST
  5831. ** or SQLITE_MUTEX_RECURSIVE) is used then sqlite3_mutex_alloc()
  5832. ** returns a different mutex on every call. ^But for the static
  5833. ** mutex types, the same mutex is returned on every call that has
  5834. ** the same type number.
  5835. **
  5836. ** ^The sqlite3_mutex_free() routine deallocates a previously
  5837. ** allocated dynamic mutex. ^SQLite is careful to deallocate every
  5838. ** dynamic mutex that it allocates. The dynamic mutexes must not be in
  5839. ** use when they are deallocated. Attempting to deallocate a static
  5840. ** mutex results in undefined behavior. ^SQLite never deallocates
  5841. ** a static mutex.
  5842. **
  5843. ** ^The sqlite3_mutex_enter() and sqlite3_mutex_try() routines attempt
  5844. ** to enter a mutex. ^If another thread is already within the mutex,
  5845. ** sqlite3_mutex_enter() will block and sqlite3_mutex_try() will return
  5846. ** SQLITE_BUSY. ^The sqlite3_mutex_try() interface returns [SQLITE_OK]
  5847. ** upon successful entry. ^(Mutexes created using
  5848. ** SQLITE_MUTEX_RECURSIVE can be entered multiple times by the same thread.
  5849. ** In such cases the,
  5850. ** mutex must be exited an equal number of times before another thread
  5851. ** can enter.)^ ^(If the same thread tries to enter any other
  5852. ** kind of mutex more than once, the behavior is undefined.
  5853. ** SQLite will never exhibit
  5854. ** such behavior in its own use of mutexes.)^
  5855. **
  5856. ** ^(Some systems (for example, Windows 95) do not support the operation
  5857. ** implemented by sqlite3_mutex_try(). On those systems, sqlite3_mutex_try()
  5858. ** will always return SQLITE_BUSY. The SQLite core only ever uses
  5859. ** sqlite3_mutex_try() as an optimization so this is acceptable behavior.)^
  5860. **
  5861. ** ^The sqlite3_mutex_leave() routine exits a mutex that was
  5862. ** previously entered by the same thread. ^(The behavior
  5863. ** is undefined if the mutex is not currently entered by the
  5864. ** calling thread or is not currently allocated. SQLite will
  5865. ** never do either.)^
  5866. **
  5867. ** ^If the argument to sqlite3_mutex_enter(), sqlite3_mutex_try(), or
  5868. ** sqlite3_mutex_leave() is a NULL pointer, then all three routines
  5869. ** behave as no-ops.
  5870. **
  5871. ** See also: [sqlite3_mutex_held()] and [sqlite3_mutex_notheld()].
  5872. */
  5873. SQLITE_API sqlite3_mutex *sqlite3_mutex_alloc(int);
  5874. SQLITE_API void sqlite3_mutex_free(sqlite3_mutex*);
  5875. SQLITE_API void sqlite3_mutex_enter(sqlite3_mutex*);
  5876. SQLITE_API int sqlite3_mutex_try(sqlite3_mutex*);
  5877. SQLITE_API void sqlite3_mutex_leave(sqlite3_mutex*);
  5878. /*
  5879. ** CAPI3REF: Mutex Methods Object
  5880. **
  5881. ** An instance of this structure defines the low-level routines
  5882. ** used to allocate and use mutexes.
  5883. **
  5884. ** Usually, the default mutex implementations provided by SQLite are
  5885. ** sufficient, however the user has the option of substituting a custom
  5886. ** implementation for specialized deployments or systems for which SQLite
  5887. ** does not provide a suitable implementation. In this case, the user
  5888. ** creates and populates an instance of this structure to pass
  5889. ** to sqlite3_config() along with the [SQLITE_CONFIG_MUTEX] option.
  5890. ** Additionally, an instance of this structure can be used as an
  5891. ** output variable when querying the system for the current mutex
  5892. ** implementation, using the [SQLITE_CONFIG_GETMUTEX] option.
  5893. **
  5894. ** ^The xMutexInit method defined by this structure is invoked as
  5895. ** part of system initialization by the sqlite3_initialize() function.
  5896. ** ^The xMutexInit routine is called by SQLite exactly once for each
  5897. ** effective call to [sqlite3_initialize()].
  5898. **
  5899. ** ^The xMutexEnd method defined by this structure is invoked as
  5900. ** part of system shutdown by the sqlite3_shutdown() function. The
  5901. ** implementation of this method is expected to release all outstanding
  5902. ** resources obtained by the mutex methods implementation, especially
  5903. ** those obtained by the xMutexInit method. ^The xMutexEnd()
  5904. ** interface is invoked exactly once for each call to [sqlite3_shutdown()].
  5905. **
  5906. ** ^(The remaining seven methods defined by this structure (xMutexAlloc,
  5907. ** xMutexFree, xMutexEnter, xMutexTry, xMutexLeave, xMutexHeld and
  5908. ** xMutexNotheld) implement the following interfaces (respectively):
  5909. **
  5910. ** <ul>
  5911. ** <li> [sqlite3_mutex_alloc()] </li>
  5912. ** <li> [sqlite3_mutex_free()] </li>
  5913. ** <li> [sqlite3_mutex_enter()] </li>
  5914. ** <li> [sqlite3_mutex_try()] </li>
  5915. ** <li> [sqlite3_mutex_leave()] </li>
  5916. ** <li> [sqlite3_mutex_held()] </li>
  5917. ** <li> [sqlite3_mutex_notheld()] </li>
  5918. ** </ul>)^
  5919. **
  5920. ** The only difference is that the public sqlite3_XXX functions enumerated
  5921. ** above silently ignore any invocations that pass a NULL pointer instead
  5922. ** of a valid mutex handle. The implementations of the methods defined
  5923. ** by this structure are not required to handle this case, the results
  5924. ** of passing a NULL pointer instead of a valid mutex handle are undefined
  5925. ** (i.e. it is acceptable to provide an implementation that segfaults if
  5926. ** it is passed a NULL pointer).
  5927. **
  5928. ** The xMutexInit() method must be threadsafe. ^It must be harmless to
  5929. ** invoke xMutexInit() multiple times within the same process and without
  5930. ** intervening calls to xMutexEnd(). Second and subsequent calls to
  5931. ** xMutexInit() must be no-ops.
  5932. **
  5933. ** ^xMutexInit() must not use SQLite memory allocation ([sqlite3_malloc()]
  5934. ** and its associates). ^Similarly, xMutexAlloc() must not use SQLite memory
  5935. ** allocation for a static mutex. ^However xMutexAlloc() may use SQLite
  5936. ** memory allocation for a fast or recursive mutex.
  5937. **
  5938. ** ^SQLite will invoke the xMutexEnd() method when [sqlite3_shutdown()] is
  5939. ** called, but only if the prior call to xMutexInit returned SQLITE_OK.
  5940. ** If xMutexInit fails in any way, it is expected to clean up after itself
  5941. ** prior to returning.
  5942. */
  5943. typedef struct sqlite3_mutex_methods sqlite3_mutex_methods;
  5944. struct sqlite3_mutex_methods {
  5945. int (*xMutexInit)(void);
  5946. int (*xMutexEnd)(void);
  5947. sqlite3_mutex *(*xMutexAlloc)(int);
  5948. void (*xMutexFree)(sqlite3_mutex *);
  5949. void (*xMutexEnter)(sqlite3_mutex *);
  5950. int (*xMutexTry)(sqlite3_mutex *);
  5951. void (*xMutexLeave)(sqlite3_mutex *);
  5952. int (*xMutexHeld)(sqlite3_mutex *);
  5953. int (*xMutexNotheld)(sqlite3_mutex *);
  5954. };
  5955. /*
  5956. ** CAPI3REF: Mutex Verification Routines
  5957. **
  5958. ** The sqlite3_mutex_held() and sqlite3_mutex_notheld() routines
  5959. ** are intended for use inside assert() statements. ^The SQLite core
  5960. ** never uses these routines except inside an assert() and applications
  5961. ** are advised to follow the lead of the core. ^The SQLite core only
  5962. ** provides implementations for these routines when it is compiled
  5963. ** with the SQLITE_DEBUG flag. ^External mutex implementations
  5964. ** are only required to provide these routines if SQLITE_DEBUG is
  5965. ** defined and if NDEBUG is not defined.
  5966. **
  5967. ** ^These routines should return true if the mutex in their argument
  5968. ** is held or not held, respectively, by the calling thread.
  5969. **
  5970. ** ^The implementation is not required to provide versions of these
  5971. ** routines that actually work. If the implementation does not provide working
  5972. ** versions of these routines, it should at least provide stubs that always
  5973. ** return true so that one does not get spurious assertion failures.
  5974. **
  5975. ** ^If the argument to sqlite3_mutex_held() is a NULL pointer then
  5976. ** the routine should return 1. This seems counter-intuitive since
  5977. ** clearly the mutex cannot be held if it does not exist. But
  5978. ** the reason the mutex does not exist is because the build is not
  5979. ** using mutexes. And we do not want the assert() containing the
  5980. ** call to sqlite3_mutex_held() to fail, so a non-zero return is
  5981. ** the appropriate thing to do. ^The sqlite3_mutex_notheld()
  5982. ** interface should also return 1 when given a NULL pointer.
  5983. */
  5984. #ifndef NDEBUG
  5985. SQLITE_API int sqlite3_mutex_held(sqlite3_mutex*);
  5986. SQLITE_API int sqlite3_mutex_notheld(sqlite3_mutex*);
  5987. #endif
  5988. /*
  5989. ** CAPI3REF: Mutex Types
  5990. **
  5991. ** The [sqlite3_mutex_alloc()] interface takes a single argument
  5992. ** which is one of these integer constants.
  5993. **
  5994. ** The set of static mutexes may change from one SQLite release to the
  5995. ** next. Applications that override the built-in mutex logic must be
  5996. ** prepared to accommodate additional static mutexes.
  5997. */
  5998. #define SQLITE_MUTEX_FAST 0
  5999. #define SQLITE_MUTEX_RECURSIVE 1
  6000. #define SQLITE_MUTEX_STATIC_MASTER 2
  6001. #define SQLITE_MUTEX_STATIC_MEM 3 /* sqlite3_malloc() */
  6002. #define SQLITE_MUTEX_STATIC_MEM2 4 /* NOT USED */
  6003. #define SQLITE_MUTEX_STATIC_OPEN 4 /* sqlite3BtreeOpen() */
  6004. #define SQLITE_MUTEX_STATIC_PRNG 5 /* sqlite3_random() */
  6005. #define SQLITE_MUTEX_STATIC_LRU 6 /* lru page list */
  6006. #define SQLITE_MUTEX_STATIC_LRU2 7 /* NOT USED */
  6007. #define SQLITE_MUTEX_STATIC_PMEM 7 /* sqlite3PageMalloc() */
  6008. /*
  6009. ** CAPI3REF: Retrieve the mutex for a database connection
  6010. **
  6011. ** ^This interface returns a pointer the [sqlite3_mutex] object that
  6012. ** serializes access to the [database connection] given in the argument
  6013. ** when the [threading mode] is Serialized.
  6014. ** ^If the [threading mode] is Single-thread or Multi-thread then this
  6015. ** routine returns a NULL pointer.
  6016. */
  6017. SQLITE_API sqlite3_mutex *sqlite3_db_mutex(sqlite3*);
  6018. /*
  6019. ** CAPI3REF: Low-Level Control Of Database Files
  6020. **
  6021. ** ^The [sqlite3_file_control()] interface makes a direct call to the
  6022. ** xFileControl method for the [sqlite3_io_methods] object associated
  6023. ** with a particular database identified by the second argument. ^The
  6024. ** name of the database is "main" for the main database or "temp" for the
  6025. ** TEMP database, or the name that appears after the AS keyword for
  6026. ** databases that are added using the [ATTACH] SQL command.
  6027. ** ^A NULL pointer can be used in place of "main" to refer to the
  6028. ** main database file.
  6029. ** ^The third and fourth parameters to this routine
  6030. ** are passed directly through to the second and third parameters of
  6031. ** the xFileControl method. ^The return value of the xFileControl
  6032. ** method becomes the return value of this routine.
  6033. **
  6034. ** ^The SQLITE_FCNTL_FILE_POINTER value for the op parameter causes
  6035. ** a pointer to the underlying [sqlite3_file] object to be written into
  6036. ** the space pointed to by the 4th parameter. ^The SQLITE_FCNTL_FILE_POINTER
  6037. ** case is a short-circuit path which does not actually invoke the
  6038. ** underlying sqlite3_io_methods.xFileControl method.
  6039. **
  6040. ** ^If the second parameter (zDbName) does not match the name of any
  6041. ** open database file, then SQLITE_ERROR is returned. ^This error
  6042. ** code is not remembered and will not be recalled by [sqlite3_errcode()]
  6043. ** or [sqlite3_errmsg()]. The underlying xFileControl method might
  6044. ** also return SQLITE_ERROR. There is no way to distinguish between
  6045. ** an incorrect zDbName and an SQLITE_ERROR return from the underlying
  6046. ** xFileControl method.
  6047. **
  6048. ** See also: [SQLITE_FCNTL_LOCKSTATE]
  6049. */
  6050. SQLITE_API int sqlite3_file_control(sqlite3*, const char *zDbName, int op, void*);
  6051. /*
  6052. ** CAPI3REF: Testing Interface
  6053. **
  6054. ** ^The sqlite3_test_control() interface is used to read out internal
  6055. ** state of SQLite and to inject faults into SQLite for testing
  6056. ** purposes. ^The first parameter is an operation code that determines
  6057. ** the number, meaning, and operation of all subsequent parameters.
  6058. **
  6059. ** This interface is not for use by applications. It exists solely
  6060. ** for verifying the correct operation of the SQLite library. Depending
  6061. ** on how the SQLite library is compiled, this interface might not exist.
  6062. **
  6063. ** The details of the operation codes, their meanings, the parameters
  6064. ** they take, and what they do are all subject to change without notice.
  6065. ** Unlike most of the SQLite API, this function is not guaranteed to
  6066. ** operate consistently from one release to the next.
  6067. */
  6068. SQLITE_API int sqlite3_test_control(int op, ...);
  6069. /*
  6070. ** CAPI3REF: Testing Interface Operation Codes
  6071. **
  6072. ** These constants are the valid operation code parameters used
  6073. ** as the first argument to [sqlite3_test_control()].
  6074. **
  6075. ** These parameters and their meanings are subject to change
  6076. ** without notice. These values are for testing purposes only.
  6077. ** Applications should not use any of these parameters or the
  6078. ** [sqlite3_test_control()] interface.
  6079. */
  6080. #define SQLITE_TESTCTRL_FIRST 5
  6081. #define SQLITE_TESTCTRL_PRNG_SAVE 5
  6082. #define SQLITE_TESTCTRL_PRNG_RESTORE 6
  6083. #define SQLITE_TESTCTRL_PRNG_RESET 7
  6084. #define SQLITE_TESTCTRL_BITVEC_TEST 8
  6085. #define SQLITE_TESTCTRL_FAULT_INSTALL 9
  6086. #define SQLITE_TESTCTRL_BENIGN_MALLOC_HOOKS 10
  6087. #define SQLITE_TESTCTRL_PENDING_BYTE 11
  6088. #define SQLITE_TESTCTRL_ASSERT 12
  6089. #define SQLITE_TESTCTRL_ALWAYS 13
  6090. #define SQLITE_TESTCTRL_RESERVE 14
  6091. #define SQLITE_TESTCTRL_OPTIMIZATIONS 15
  6092. #define SQLITE_TESTCTRL_ISKEYWORD 16
  6093. #define SQLITE_TESTCTRL_SCRATCHMALLOC 17
  6094. #define SQLITE_TESTCTRL_LOCALTIME_FAULT 18
  6095. #define SQLITE_TESTCTRL_EXPLAIN_STMT 19
  6096. #define SQLITE_TESTCTRL_LAST 19
  6097. /*
  6098. ** CAPI3REF: SQLite Runtime Status
  6099. **
  6100. ** ^This interface is used to retrieve runtime status information
  6101. ** about the performance of SQLite, and optionally to reset various
  6102. ** highwater marks. ^The first argument is an integer code for
  6103. ** the specific parameter to measure. ^(Recognized integer codes
  6104. ** are of the form [status parameters | SQLITE_STATUS_...].)^
  6105. ** ^The current value of the parameter is returned into *pCurrent.
  6106. ** ^The highest recorded value is returned in *pHighwater. ^If the
  6107. ** resetFlag is true, then the highest record value is reset after
  6108. ** *pHighwater is written. ^(Some parameters do not record the highest
  6109. ** value. For those parameters
  6110. ** nothing is written into *pHighwater and the resetFlag is ignored.)^
  6111. ** ^(Other parameters record only the highwater mark and not the current
  6112. ** value. For these latter parameters nothing is written into *pCurrent.)^
  6113. **
  6114. ** ^The sqlite3_status() routine returns SQLITE_OK on success and a
  6115. ** non-zero [error code] on failure.
  6116. **
  6117. ** This routine is threadsafe but is not atomic. This routine can be
  6118. ** called while other threads are running the same or different SQLite
  6119. ** interfaces. However the values returned in *pCurrent and
  6120. ** *pHighwater reflect the status of SQLite at different points in time
  6121. ** and it is possible that another thread might change the parameter
  6122. ** in between the times when *pCurrent and *pHighwater are written.
  6123. **
  6124. ** See also: [sqlite3_db_status()]
  6125. */
  6126. SQLITE_API int sqlite3_status(int op, int *pCurrent, int *pHighwater, int resetFlag);
  6127. /*
  6128. ** CAPI3REF: Status Parameters
  6129. ** KEYWORDS: {status parameters}
  6130. **
  6131. ** These integer constants designate various run-time status parameters
  6132. ** that can be returned by [sqlite3_status()].
  6133. **
  6134. ** <dl>
  6135. ** [[SQLITE_STATUS_MEMORY_USED]] ^(<dt>SQLITE_STATUS_MEMORY_USED</dt>
  6136. ** <dd>This parameter is the current amount of memory checked out
  6137. ** using [sqlite3_malloc()], either directly or indirectly. The
  6138. ** figure includes calls made to [sqlite3_malloc()] by the application
  6139. ** and internal memory usage by the SQLite library. Scratch memory
  6140. ** controlled by [SQLITE_CONFIG_SCRATCH] and auxiliary page-cache
  6141. ** memory controlled by [SQLITE_CONFIG_PAGECACHE] is not included in
  6142. ** this parameter. The amount returned is the sum of the allocation
  6143. ** sizes as reported by the xSize method in [sqlite3_mem_methods].</dd>)^
  6144. **
  6145. ** [[SQLITE_STATUS_MALLOC_SIZE]] ^(<dt>SQLITE_STATUS_MALLOC_SIZE</dt>
  6146. ** <dd>This parameter records the largest memory allocation request
  6147. ** handed to [sqlite3_malloc()] or [sqlite3_realloc()] (or their
  6148. ** internal equivalents). Only the value returned in the
  6149. ** *pHighwater parameter to [sqlite3_status()] is of interest.
  6150. ** The value written into the *pCurrent parameter is undefined.</dd>)^
  6151. **
  6152. ** [[SQLITE_STATUS_MALLOC_COUNT]] ^(<dt>SQLITE_STATUS_MALLOC_COUNT</dt>
  6153. ** <dd>This parameter records the number of separate memory allocations
  6154. ** currently checked out.</dd>)^
  6155. **
  6156. ** [[SQLITE_STATUS_PAGECACHE_USED]] ^(<dt>SQLITE_STATUS_PAGECACHE_USED</dt>
  6157. ** <dd>This parameter returns the number of pages used out of the
  6158. ** [pagecache memory allocator] that was configured using
  6159. ** [SQLITE_CONFIG_PAGECACHE]. The
  6160. ** value returned is in pages, not in bytes.</dd>)^
  6161. **
  6162. ** [[SQLITE_STATUS_PAGECACHE_OVERFLOW]]
  6163. ** ^(<dt>SQLITE_STATUS_PAGECACHE_OVERFLOW</dt>
  6164. ** <dd>This parameter returns the number of bytes of page cache
  6165. ** allocation which could not be satisfied by the [SQLITE_CONFIG_PAGECACHE]
  6166. ** buffer and where forced to overflow to [sqlite3_malloc()]. The
  6167. ** returned value includes allocations that overflowed because they
  6168. ** where too large (they were larger than the "sz" parameter to
  6169. ** [SQLITE_CONFIG_PAGECACHE]) and allocations that overflowed because
  6170. ** no space was left in the page cache.</dd>)^
  6171. **
  6172. ** [[SQLITE_STATUS_PAGECACHE_SIZE]] ^(<dt>SQLITE_STATUS_PAGECACHE_SIZE</dt>
  6173. ** <dd>This parameter records the largest memory allocation request
  6174. ** handed to [pagecache memory allocator]. Only the value returned in the
  6175. ** *pHighwater parameter to [sqlite3_status()] is of interest.
  6176. ** The value written into the *pCurrent parameter is undefined.</dd>)^
  6177. **
  6178. ** [[SQLITE_STATUS_SCRATCH_USED]] ^(<dt>SQLITE_STATUS_SCRATCH_USED</dt>
  6179. ** <dd>This parameter returns the number of allocations used out of the
  6180. ** [scratch memory allocator] configured using
  6181. ** [SQLITE_CONFIG_SCRATCH]. The value returned is in allocations, not
  6182. ** in bytes. Since a single thread may only have one scratch allocation
  6183. ** outstanding at time, this parameter also reports the number of threads
  6184. ** using scratch memory at the same time.</dd>)^
  6185. **
  6186. ** [[SQLITE_STATUS_SCRATCH_OVERFLOW]] ^(<dt>SQLITE_STATUS_SCRATCH_OVERFLOW</dt>
  6187. ** <dd>This parameter returns the number of bytes of scratch memory
  6188. ** allocation which could not be satisfied by the [SQLITE_CONFIG_SCRATCH]
  6189. ** buffer and where forced to overflow to [sqlite3_malloc()]. The values
  6190. ** returned include overflows because the requested allocation was too
  6191. ** larger (that is, because the requested allocation was larger than the
  6192. ** "sz" parameter to [SQLITE_CONFIG_SCRATCH]) and because no scratch buffer
  6193. ** slots were available.
  6194. ** </dd>)^
  6195. **
  6196. ** [[SQLITE_STATUS_SCRATCH_SIZE]] ^(<dt>SQLITE_STATUS_SCRATCH_SIZE</dt>
  6197. ** <dd>This parameter records the largest memory allocation request
  6198. ** handed to [scratch memory allocator]. Only the value returned in the
  6199. ** *pHighwater parameter to [sqlite3_status()] is of interest.
  6200. ** The value written into the *pCurrent parameter is undefined.</dd>)^
  6201. **
  6202. ** [[SQLITE_STATUS_PARSER_STACK]] ^(<dt>SQLITE_STATUS_PARSER_STACK</dt>
  6203. ** <dd>This parameter records the deepest parser stack. It is only
  6204. ** meaningful if SQLite is compiled with [YYTRACKMAXSTACKDEPTH].</dd>)^
  6205. ** </dl>
  6206. **
  6207. ** New status parameters may be added from time to time.
  6208. */
  6209. #define SQLITE_STATUS_MEMORY_USED 0
  6210. #define SQLITE_STATUS_PAGECACHE_USED 1
  6211. #define SQLITE_STATUS_PAGECACHE_OVERFLOW 2
  6212. #define SQLITE_STATUS_SCRATCH_USED 3
  6213. #define SQLITE_STATUS_SCRATCH_OVERFLOW 4
  6214. #define SQLITE_STATUS_MALLOC_SIZE 5
  6215. #define SQLITE_STATUS_PARSER_STACK 6
  6216. #define SQLITE_STATUS_PAGECACHE_SIZE 7
  6217. #define SQLITE_STATUS_SCRATCH_SIZE 8
  6218. #define SQLITE_STATUS_MALLOC_COUNT 9
  6219. /*
  6220. ** CAPI3REF: Database Connection Status
  6221. **
  6222. ** ^This interface is used to retrieve runtime status information
  6223. ** about a single [database connection]. ^The first argument is the
  6224. ** database connection object to be interrogated. ^The second argument
  6225. ** is an integer constant, taken from the set of
  6226. ** [SQLITE_DBSTATUS options], that
  6227. ** determines the parameter to interrogate. The set of
  6228. ** [SQLITE_DBSTATUS options] is likely
  6229. ** to grow in future releases of SQLite.
  6230. **
  6231. ** ^The current value of the requested parameter is written into *pCur
  6232. ** and the highest instantaneous value is written into *pHiwtr. ^If
  6233. ** the resetFlg is true, then the highest instantaneous value is
  6234. ** reset back down to the current value.
  6235. **
  6236. ** ^The sqlite3_db_status() routine returns SQLITE_OK on success and a
  6237. ** non-zero [error code] on failure.
  6238. **
  6239. ** See also: [sqlite3_status()] and [sqlite3_stmt_status()].
  6240. */
  6241. SQLITE_API int sqlite3_db_status(sqlite3*, int op, int *pCur, int *pHiwtr, int resetFlg);
  6242. /*
  6243. ** CAPI3REF: Status Parameters for database connections
  6244. ** KEYWORDS: {SQLITE_DBSTATUS options}
  6245. **
  6246. ** These constants are the available integer "verbs" that can be passed as
  6247. ** the second argument to the [sqlite3_db_status()] interface.
  6248. **
  6249. ** New verbs may be added in future releases of SQLite. Existing verbs
  6250. ** might be discontinued. Applications should check the return code from
  6251. ** [sqlite3_db_status()] to make sure that the call worked.
  6252. ** The [sqlite3_db_status()] interface will return a non-zero error code
  6253. ** if a discontinued or unsupported verb is invoked.
  6254. **
  6255. ** <dl>
  6256. ** [[SQLITE_DBSTATUS_LOOKASIDE_USED]] ^(<dt>SQLITE_DBSTATUS_LOOKASIDE_USED</dt>
  6257. ** <dd>This parameter returns the number of lookaside memory slots currently
  6258. ** checked out.</dd>)^
  6259. **
  6260. ** [[SQLITE_DBSTATUS_LOOKASIDE_HIT]] ^(<dt>SQLITE_DBSTATUS_LOOKASIDE_HIT</dt>
  6261. ** <dd>This parameter returns the number malloc attempts that were
  6262. ** satisfied using lookaside memory. Only the high-water value is meaningful;
  6263. ** the current value is always zero.)^
  6264. **
  6265. ** [[SQLITE_DBSTATUS_LOOKASIDE_MISS_SIZE]]
  6266. ** ^(<dt>SQLITE_DBSTATUS_LOOKASIDE_MISS_SIZE</dt>
  6267. ** <dd>This parameter returns the number malloc attempts that might have
  6268. ** been satisfied using lookaside memory but failed due to the amount of
  6269. ** memory requested being larger than the lookaside slot size.
  6270. ** Only the high-water value is meaningful;
  6271. ** the current value is always zero.)^
  6272. **
  6273. ** [[SQLITE_DBSTATUS_LOOKASIDE_MISS_FULL]]
  6274. ** ^(<dt>SQLITE_DBSTATUS_LOOKASIDE_MISS_FULL</dt>
  6275. ** <dd>This parameter returns the number malloc attempts that might have
  6276. ** been satisfied using lookaside memory but failed due to all lookaside
  6277. ** memory already being in use.
  6278. ** Only the high-water value is meaningful;
  6279. ** the current value is always zero.)^
  6280. **
  6281. ** [[SQLITE_DBSTATUS_CACHE_USED]] ^(<dt>SQLITE_DBSTATUS_CACHE_USED</dt>
  6282. ** <dd>This parameter returns the approximate number of of bytes of heap
  6283. ** memory used by all pager caches associated with the database connection.)^
  6284. ** ^The highwater mark associated with SQLITE_DBSTATUS_CACHE_USED is always 0.
  6285. **
  6286. ** [[SQLITE_DBSTATUS_SCHEMA_USED]] ^(<dt>SQLITE_DBSTATUS_SCHEMA_USED</dt>
  6287. ** <dd>This parameter returns the approximate number of of bytes of heap
  6288. ** memory used to store the schema for all databases associated
  6289. ** with the connection - main, temp, and any [ATTACH]-ed databases.)^
  6290. ** ^The full amount of memory used by the schemas is reported, even if the
  6291. ** schema memory is shared with other database connections due to
  6292. ** [shared cache mode] being enabled.
  6293. ** ^The highwater mark associated with SQLITE_DBSTATUS_SCHEMA_USED is always 0.
  6294. **
  6295. ** [[SQLITE_DBSTATUS_STMT_USED]] ^(<dt>SQLITE_DBSTATUS_STMT_USED</dt>
  6296. ** <dd>This parameter returns the approximate number of of bytes of heap
  6297. ** and lookaside memory used by all prepared statements associated with
  6298. ** the database connection.)^
  6299. ** ^The highwater mark associated with SQLITE_DBSTATUS_STMT_USED is always 0.
  6300. ** </dd>
  6301. **
  6302. ** [[SQLITE_DBSTATUS_CACHE_HIT]] ^(<dt>SQLITE_DBSTATUS_CACHE_HIT</dt>
  6303. ** <dd>This parameter returns the number of pager cache hits that have
  6304. ** occurred.)^ ^The highwater mark associated with SQLITE_DBSTATUS_CACHE_HIT
  6305. ** is always 0.
  6306. ** </dd>
  6307. **
  6308. ** [[SQLITE_DBSTATUS_CACHE_MISS]] ^(<dt>SQLITE_DBSTATUS_CACHE_MISS</dt>
  6309. ** <dd>This parameter returns the number of pager cache misses that have
  6310. ** occurred.)^ ^The highwater mark associated with SQLITE_DBSTATUS_CACHE_MISS
  6311. ** is always 0.
  6312. ** </dd>
  6313. ** </dl>
  6314. */
  6315. #define SQLITE_DBSTATUS_LOOKASIDE_USED 0
  6316. #define SQLITE_DBSTATUS_CACHE_USED 1
  6317. #define SQLITE_DBSTATUS_SCHEMA_USED 2
  6318. #define SQLITE_DBSTATUS_STMT_USED 3
  6319. #define SQLITE_DBSTATUS_LOOKASIDE_HIT 4
  6320. #define SQLITE_DBSTATUS_LOOKASIDE_MISS_SIZE 5
  6321. #define SQLITE_DBSTATUS_LOOKASIDE_MISS_FULL 6
  6322. #define SQLITE_DBSTATUS_CACHE_HIT 7
  6323. #define SQLITE_DBSTATUS_CACHE_MISS 8
  6324. #define SQLITE_DBSTATUS_MAX 8 /* Largest defined DBSTATUS */
  6325. /*
  6326. ** CAPI3REF: Prepared Statement Status
  6327. **
  6328. ** ^(Each prepared statement maintains various
  6329. ** [SQLITE_STMTSTATUS counters] that measure the number
  6330. ** of times it has performed specific operations.)^ These counters can
  6331. ** be used to monitor the performance characteristics of the prepared
  6332. ** statements. For example, if the number of table steps greatly exceeds
  6333. ** the number of table searches or result rows, that would tend to indicate
  6334. ** that the prepared statement is using a full table scan rather than
  6335. ** an index.
  6336. **
  6337. ** ^(This interface is used to retrieve and reset counter values from
  6338. ** a [prepared statement]. The first argument is the prepared statement
  6339. ** object to be interrogated. The second argument
  6340. ** is an integer code for a specific [SQLITE_STMTSTATUS counter]
  6341. ** to be interrogated.)^
  6342. ** ^The current value of the requested counter is returned.
  6343. ** ^If the resetFlg is true, then the counter is reset to zero after this
  6344. ** interface call returns.
  6345. **
  6346. ** See also: [sqlite3_status()] and [sqlite3_db_status()].
  6347. */
  6348. SQLITE_API int sqlite3_stmt_status(sqlite3_stmt*, int op,int resetFlg);
  6349. /*
  6350. ** CAPI3REF: Status Parameters for prepared statements
  6351. ** KEYWORDS: {SQLITE_STMTSTATUS counter} {SQLITE_STMTSTATUS counters}
  6352. **
  6353. ** These preprocessor macros define integer codes that name counter
  6354. ** values associated with the [sqlite3_stmt_status()] interface.
  6355. ** The meanings of the various counters are as follows:
  6356. **
  6357. ** <dl>
  6358. ** [[SQLITE_STMTSTATUS_FULLSCAN_STEP]] <dt>SQLITE_STMTSTATUS_FULLSCAN_STEP</dt>
  6359. ** <dd>^This is the number of times that SQLite has stepped forward in
  6360. ** a table as part of a full table scan. Large numbers for this counter
  6361. ** may indicate opportunities for performance improvement through
  6362. ** careful use of indices.</dd>
  6363. **
  6364. ** [[SQLITE_STMTSTATUS_SORT]] <dt>SQLITE_STMTSTATUS_SORT</dt>
  6365. ** <dd>^This is the number of sort operations that have occurred.
  6366. ** A non-zero value in this counter may indicate an opportunity to
  6367. ** improvement performance through careful use of indices.</dd>
  6368. **
  6369. ** [[SQLITE_STMTSTATUS_AUTOINDEX]] <dt>SQLITE_STMTSTATUS_AUTOINDEX</dt>
  6370. ** <dd>^This is the number of rows inserted into transient indices that
  6371. ** were created automatically in order to help joins run faster.
  6372. ** A non-zero value in this counter may indicate an opportunity to
  6373. ** improvement performance by adding permanent indices that do not
  6374. ** need to be reinitialized each time the statement is run.</dd>
  6375. ** </dl>
  6376. */
  6377. #define SQLITE_STMTSTATUS_FULLSCAN_STEP 1
  6378. #define SQLITE_STMTSTATUS_SORT 2
  6379. #define SQLITE_STMTSTATUS_AUTOINDEX 3
  6380. /*
  6381. ** CAPI3REF: Custom Page Cache Object
  6382. **
  6383. ** The sqlite3_pcache type is opaque. It is implemented by
  6384. ** the pluggable module. The SQLite core has no knowledge of
  6385. ** its size or internal structure and never deals with the
  6386. ** sqlite3_pcache object except by holding and passing pointers
  6387. ** to the object.
  6388. **
  6389. ** See [sqlite3_pcache_methods2] for additional information.
  6390. */
  6391. typedef struct sqlite3_pcache sqlite3_pcache;
  6392. /*
  6393. ** CAPI3REF: Custom Page Cache Object
  6394. **
  6395. ** The sqlite3_pcache_page object represents a single page in the
  6396. ** page cache. The page cache will allocate instances of this
  6397. ** object. Various methods of the page cache use pointers to instances
  6398. ** of this object as parameters or as their return value.
  6399. **
  6400. ** See [sqlite3_pcache_methods2] for additional information.
  6401. */
  6402. typedef struct sqlite3_pcache_page sqlite3_pcache_page;
  6403. struct sqlite3_pcache_page {
  6404. void *pBuf; /* The content of the page */
  6405. void *pExtra; /* Extra information associated with the page */
  6406. };
  6407. /*
  6408. ** CAPI3REF: Application Defined Page Cache.
  6409. ** KEYWORDS: {page cache}
  6410. **
  6411. ** ^(The [sqlite3_config]([SQLITE_CONFIG_PCACHE2], ...) interface can
  6412. ** register an alternative page cache implementation by passing in an
  6413. ** instance of the sqlite3_pcache_methods2 structure.)^
  6414. ** In many applications, most of the heap memory allocated by
  6415. ** SQLite is used for the page cache.
  6416. ** By implementing a
  6417. ** custom page cache using this API, an application can better control
  6418. ** the amount of memory consumed by SQLite, the way in which
  6419. ** that memory is allocated and released, and the policies used to
  6420. ** determine exactly which parts of a database file are cached and for
  6421. ** how long.
  6422. **
  6423. ** The alternative page cache mechanism is an
  6424. ** extreme measure that is only needed by the most demanding applications.
  6425. ** The built-in page cache is recommended for most uses.
  6426. **
  6427. ** ^(The contents of the sqlite3_pcache_methods2 structure are copied to an
  6428. ** internal buffer by SQLite within the call to [sqlite3_config]. Hence
  6429. ** the application may discard the parameter after the call to
  6430. ** [sqlite3_config()] returns.)^
  6431. **
  6432. ** [[the xInit() page cache method]]
  6433. ** ^(The xInit() method is called once for each effective
  6434. ** call to [sqlite3_initialize()])^
  6435. ** (usually only once during the lifetime of the process). ^(The xInit()
  6436. ** method is passed a copy of the sqlite3_pcache_methods2.pArg value.)^
  6437. ** The intent of the xInit() method is to set up global data structures
  6438. ** required by the custom page cache implementation.
  6439. ** ^(If the xInit() method is NULL, then the
  6440. ** built-in default page cache is used instead of the application defined
  6441. ** page cache.)^
  6442. **
  6443. ** [[the xShutdown() page cache method]]
  6444. ** ^The xShutdown() method is called by [sqlite3_shutdown()].
  6445. ** It can be used to clean up
  6446. ** any outstanding resources before process shutdown, if required.
  6447. ** ^The xShutdown() method may be NULL.
  6448. **
  6449. ** ^SQLite automatically serializes calls to the xInit method,
  6450. ** so the xInit method need not be threadsafe. ^The
  6451. ** xShutdown method is only called from [sqlite3_shutdown()] so it does
  6452. ** not need to be threadsafe either. All other methods must be threadsafe
  6453. ** in multithreaded applications.
  6454. **
  6455. ** ^SQLite will never invoke xInit() more than once without an intervening
  6456. ** call to xShutdown().
  6457. **
  6458. ** [[the xCreate() page cache methods]]
  6459. ** ^SQLite invokes the xCreate() method to construct a new cache instance.
  6460. ** SQLite will typically create one cache instance for each open database file,
  6461. ** though this is not guaranteed. ^The
  6462. ** first parameter, szPage, is the size in bytes of the pages that must
  6463. ** be allocated by the cache. ^szPage will always a power of two. ^The
  6464. ** second parameter szExtra is a number of bytes of extra storage
  6465. ** associated with each page cache entry. ^The szExtra parameter will
  6466. ** a number less than 250. SQLite will use the
  6467. ** extra szExtra bytes on each page to store metadata about the underlying
  6468. ** database page on disk. The value passed into szExtra depends
  6469. ** on the SQLite version, the target platform, and how SQLite was compiled.
  6470. ** ^The third argument to xCreate(), bPurgeable, is true if the cache being
  6471. ** created will be used to cache database pages of a file stored on disk, or
  6472. ** false if it is used for an in-memory database. The cache implementation
  6473. ** does not have to do anything special based with the value of bPurgeable;
  6474. ** it is purely advisory. ^On a cache where bPurgeable is false, SQLite will
  6475. ** never invoke xUnpin() except to deliberately delete a page.
  6476. ** ^In other words, calls to xUnpin() on a cache with bPurgeable set to
  6477. ** false will always have the "discard" flag set to true.
  6478. ** ^Hence, a cache created with bPurgeable false will
  6479. ** never contain any unpinned pages.
  6480. **
  6481. ** [[the xCachesize() page cache method]]
  6482. ** ^(The xCachesize() method may be called at any time by SQLite to set the
  6483. ** suggested maximum cache-size (number of pages stored by) the cache
  6484. ** instance passed as the first argument. This is the value configured using
  6485. ** the SQLite "[PRAGMA cache_size]" command.)^ As with the bPurgeable
  6486. ** parameter, the implementation is not required to do anything with this
  6487. ** value; it is advisory only.
  6488. **
  6489. ** [[the xPagecount() page cache methods]]
  6490. ** The xPagecount() method must return the number of pages currently
  6491. ** stored in the cache, both pinned and unpinned.
  6492. **
  6493. ** [[the xFetch() page cache methods]]
  6494. ** The xFetch() method locates a page in the cache and returns a pointer to
  6495. ** an sqlite3_pcache_page object associated with that page, or a NULL pointer.
  6496. ** The pBuf element of the returned sqlite3_pcache_page object will be a
  6497. ** pointer to a buffer of szPage bytes used to store the content of a
  6498. ** single database page. The pExtra element of sqlite3_pcache_page will be
  6499. ** a pointer to the szExtra bytes of extra storage that SQLite has requested
  6500. ** for each entry in the page cache.
  6501. **
  6502. ** The page to be fetched is determined by the key. ^The minimum key value
  6503. ** is 1. After it has been retrieved using xFetch, the page is considered
  6504. ** to be "pinned".
  6505. **
  6506. ** If the requested page is already in the page cache, then the page cache
  6507. ** implementation must return a pointer to the page buffer with its content
  6508. ** intact. If the requested page is not already in the cache, then the
  6509. ** cache implementation should use the value of the createFlag
  6510. ** parameter to help it determined what action to take:
  6511. **
  6512. ** <table border=1 width=85% align=center>
  6513. ** <tr><th> createFlag <th> Behaviour when page is not already in cache
  6514. ** <tr><td> 0 <td> Do not allocate a new page. Return NULL.
  6515. ** <tr><td> 1 <td> Allocate a new page if it easy and convenient to do so.
  6516. ** Otherwise return NULL.
  6517. ** <tr><td> 2 <td> Make every effort to allocate a new page. Only return
  6518. ** NULL if allocating a new page is effectively impossible.
  6519. ** </table>
  6520. **
  6521. ** ^(SQLite will normally invoke xFetch() with a createFlag of 0 or 1. SQLite
  6522. ** will only use a createFlag of 2 after a prior call with a createFlag of 1
  6523. ** failed.)^ In between the to xFetch() calls, SQLite may
  6524. ** attempt to unpin one or more cache pages by spilling the content of
  6525. ** pinned pages to disk and synching the operating system disk cache.
  6526. **
  6527. ** [[the xUnpin() page cache method]]
  6528. ** ^xUnpin() is called by SQLite with a pointer to a currently pinned page
  6529. ** as its second argument. If the third parameter, discard, is non-zero,
  6530. ** then the page must be evicted from the cache.
  6531. ** ^If the discard parameter is
  6532. ** zero, then the page may be discarded or retained at the discretion of
  6533. ** page cache implementation. ^The page cache implementation
  6534. ** may choose to evict unpinned pages at any time.
  6535. **
  6536. ** The cache must not perform any reference counting. A single
  6537. ** call to xUnpin() unpins the page regardless of the number of prior calls
  6538. ** to xFetch().
  6539. **
  6540. ** [[the xRekey() page cache methods]]
  6541. ** The xRekey() method is used to change the key value associated with the
  6542. ** page passed as the second argument. If the cache
  6543. ** previously contains an entry associated with newKey, it must be
  6544. ** discarded. ^Any prior cache entry associated with newKey is guaranteed not
  6545. ** to be pinned.
  6546. **
  6547. ** When SQLite calls the xTruncate() method, the cache must discard all
  6548. ** existing cache entries with page numbers (keys) greater than or equal
  6549. ** to the value of the iLimit parameter passed to xTruncate(). If any
  6550. ** of these pages are pinned, they are implicitly unpinned, meaning that
  6551. ** they can be safely discarded.
  6552. **
  6553. ** [[the xDestroy() page cache method]]
  6554. ** ^The xDestroy() method is used to delete a cache allocated by xCreate().
  6555. ** All resources associated with the specified cache should be freed. ^After
  6556. ** calling the xDestroy() method, SQLite considers the [sqlite3_pcache*]
  6557. ** handle invalid, and will not use it with any other sqlite3_pcache_methods2
  6558. ** functions.
  6559. **
  6560. ** [[the xShrink() page cache method]]
  6561. ** ^SQLite invokes the xShrink() method when it wants the page cache to
  6562. ** free up as much of heap memory as possible. The page cache implementation
  6563. ** is not obligated to free any memory, but well-behaved implementations should
  6564. ** do their best.
  6565. */
  6566. typedef struct sqlite3_pcache_methods2 sqlite3_pcache_methods2;
  6567. struct sqlite3_pcache_methods2 {
  6568. int iVersion;
  6569. void *pArg;
  6570. int (*xInit)(void*);
  6571. void (*xShutdown)(void*);
  6572. sqlite3_pcache *(*xCreate)(int szPage, int szExtra, int bPurgeable);
  6573. void (*xCachesize)(sqlite3_pcache*, int nCachesize);
  6574. int (*xPagecount)(sqlite3_pcache*);
  6575. sqlite3_pcache_page *(*xFetch)(sqlite3_pcache*, unsigned key, int createFlag);
  6576. void (*xUnpin)(sqlite3_pcache*, sqlite3_pcache_page*, int discard);
  6577. void (*xRekey)(sqlite3_pcache*, sqlite3_pcache_page*,
  6578. unsigned oldKey, unsigned newKey);
  6579. void (*xTruncate)(sqlite3_pcache*, unsigned iLimit);
  6580. void (*xDestroy)(sqlite3_pcache*);
  6581. void (*xShrink)(sqlite3_pcache*);
  6582. };
  6583. /*
  6584. ** This is the obsolete pcache_methods object that has now been replaced
  6585. ** by sqlite3_pcache_methods2. This object is not used by SQLite. It is
  6586. ** retained in the header file for backwards compatibility only.
  6587. */
  6588. typedef struct sqlite3_pcache_methods sqlite3_pcache_methods;
  6589. struct sqlite3_pcache_methods {
  6590. void *pArg;
  6591. int (*xInit)(void*);
  6592. void (*xShutdown)(void*);
  6593. sqlite3_pcache *(*xCreate)(int szPage, int bPurgeable);
  6594. void (*xCachesize)(sqlite3_pcache*, int nCachesize);
  6595. int (*xPagecount)(sqlite3_pcache*);
  6596. void *(*xFetch)(sqlite3_pcache*, unsigned key, int createFlag);
  6597. void (*xUnpin)(sqlite3_pcache*, void*, int discard);
  6598. void (*xRekey)(sqlite3_pcache*, void*, unsigned oldKey, unsigned newKey);
  6599. void (*xTruncate)(sqlite3_pcache*, unsigned iLimit);
  6600. void (*xDestroy)(sqlite3_pcache*);
  6601. };
  6602. /*
  6603. ** CAPI3REF: Online Backup Object
  6604. **
  6605. ** The sqlite3_backup object records state information about an ongoing
  6606. ** online backup operation. ^The sqlite3_backup object is created by
  6607. ** a call to [sqlite3_backup_init()] and is destroyed by a call to
  6608. ** [sqlite3_backup_finish()].
  6609. **
  6610. ** See Also: [Using the SQLite Online Backup API]
  6611. */
  6612. typedef struct sqlite3_backup sqlite3_backup;
  6613. /*
  6614. ** CAPI3REF: Online Backup API.
  6615. **
  6616. ** The backup API copies the content of one database into another.
  6617. ** It is useful either for creating backups of databases or
  6618. ** for copying in-memory databases to or from persistent files.
  6619. **
  6620. ** See Also: [Using the SQLite Online Backup API]
  6621. **
  6622. ** ^SQLite holds a write transaction open on the destination database file
  6623. ** for the duration of the backup operation.
  6624. ** ^The source database is read-locked only while it is being read;
  6625. ** it is not locked continuously for the entire backup operation.
  6626. ** ^Thus, the backup may be performed on a live source database without
  6627. ** preventing other database connections from
  6628. ** reading or writing to the source database while the backup is underway.
  6629. **
  6630. ** ^(To perform a backup operation:
  6631. ** <ol>
  6632. ** <li><b>sqlite3_backup_init()</b> is called once to initialize the
  6633. ** backup,
  6634. ** <li><b>sqlite3_backup_step()</b> is called one or more times to transfer
  6635. ** the data between the two databases, and finally
  6636. ** <li><b>sqlite3_backup_finish()</b> is called to release all resources
  6637. ** associated with the backup operation.
  6638. ** </ol>)^
  6639. ** There should be exactly one call to sqlite3_backup_finish() for each
  6640. ** successful call to sqlite3_backup_init().
  6641. **
  6642. ** [[sqlite3_backup_init()]] <b>sqlite3_backup_init()</b>
  6643. **
  6644. ** ^The D and N arguments to sqlite3_backup_init(D,N,S,M) are the
  6645. ** [database connection] associated with the destination database
  6646. ** and the database name, respectively.
  6647. ** ^The database name is "main" for the main database, "temp" for the
  6648. ** temporary database, or the name specified after the AS keyword in
  6649. ** an [ATTACH] statement for an attached database.
  6650. ** ^The S and M arguments passed to
  6651. ** sqlite3_backup_init(D,N,S,M) identify the [database connection]
  6652. ** and database name of the source database, respectively.
  6653. ** ^The source and destination [database connections] (parameters S and D)
  6654. ** must be different or else sqlite3_backup_init(D,N,S,M) will fail with
  6655. ** an error.
  6656. **
  6657. ** ^If an error occurs within sqlite3_backup_init(D,N,S,M), then NULL is
  6658. ** returned and an error code and error message are stored in the
  6659. ** destination [database connection] D.
  6660. ** ^The error code and message for the failed call to sqlite3_backup_init()
  6661. ** can be retrieved using the [sqlite3_errcode()], [sqlite3_errmsg()], and/or
  6662. ** [sqlite3_errmsg16()] functions.
  6663. ** ^A successful call to sqlite3_backup_init() returns a pointer to an
  6664. ** [sqlite3_backup] object.
  6665. ** ^The [sqlite3_backup] object may be used with the sqlite3_backup_step() and
  6666. ** sqlite3_backup_finish() functions to perform the specified backup
  6667. ** operation.
  6668. **
  6669. ** [[sqlite3_backup_step()]] <b>sqlite3_backup_step()</b>
  6670. **
  6671. ** ^Function sqlite3_backup_step(B,N) will copy up to N pages between
  6672. ** the source and destination databases specified by [sqlite3_backup] object B.
  6673. ** ^If N is negative, all remaining source pages are copied.
  6674. ** ^If sqlite3_backup_step(B,N) successfully copies N pages and there
  6675. ** are still more pages to be copied, then the function returns [SQLITE_OK].
  6676. ** ^If sqlite3_backup_step(B,N) successfully finishes copying all pages
  6677. ** from source to destination, then it returns [SQLITE_DONE].
  6678. ** ^If an error occurs while running sqlite3_backup_step(B,N),
  6679. ** then an [error code] is returned. ^As well as [SQLITE_OK] and
  6680. ** [SQLITE_DONE], a call to sqlite3_backup_step() may return [SQLITE_READONLY],
  6681. ** [SQLITE_NOMEM], [SQLITE_BUSY], [SQLITE_LOCKED], or an
  6682. ** [SQLITE_IOERR_ACCESS | SQLITE_IOERR_XXX] extended error code.
  6683. **
  6684. ** ^(The sqlite3_backup_step() might return [SQLITE_READONLY] if
  6685. ** <ol>
  6686. ** <li> the destination database was opened read-only, or
  6687. ** <li> the destination database is using write-ahead-log journaling
  6688. ** and the destination and source page sizes differ, or
  6689. ** <li> the destination database is an in-memory database and the
  6690. ** destination and source page sizes differ.
  6691. ** </ol>)^
  6692. **
  6693. ** ^If sqlite3_backup_step() cannot obtain a required file-system lock, then
  6694. ** the [sqlite3_busy_handler | busy-handler function]
  6695. ** is invoked (if one is specified). ^If the
  6696. ** busy-handler returns non-zero before the lock is available, then
  6697. ** [SQLITE_BUSY] is returned to the caller. ^In this case the call to
  6698. ** sqlite3_backup_step() can be retried later. ^If the source
  6699. ** [database connection]
  6700. ** is being used to write to the source database when sqlite3_backup_step()
  6701. ** is called, then [SQLITE_LOCKED] is returned immediately. ^Again, in this
  6702. ** case the call to sqlite3_backup_step() can be retried later on. ^(If
  6703. ** [SQLITE_IOERR_ACCESS | SQLITE_IOERR_XXX], [SQLITE_NOMEM], or
  6704. ** [SQLITE_READONLY] is returned, then
  6705. ** there is no point in retrying the call to sqlite3_backup_step(). These
  6706. ** errors are considered fatal.)^ The application must accept
  6707. ** that the backup operation has failed and pass the backup operation handle
  6708. ** to the sqlite3_backup_finish() to release associated resources.
  6709. **
  6710. ** ^The first call to sqlite3_backup_step() obtains an exclusive lock
  6711. ** on the destination file. ^The exclusive lock is not released until either
  6712. ** sqlite3_backup_finish() is called or the backup operation is complete
  6713. ** and sqlite3_backup_step() returns [SQLITE_DONE]. ^Every call to
  6714. ** sqlite3_backup_step() obtains a [shared lock] on the source database that
  6715. ** lasts for the duration of the sqlite3_backup_step() call.
  6716. ** ^Because the source database is not locked between calls to
  6717. ** sqlite3_backup_step(), the source database may be modified mid-way
  6718. ** through the backup process. ^If the source database is modified by an
  6719. ** external process or via a database connection other than the one being
  6720. ** used by the backup operation, then the backup will be automatically
  6721. ** restarted by the next call to sqlite3_backup_step(). ^If the source
  6722. ** database is modified by the using the same database connection as is used
  6723. ** by the backup operation, then the backup database is automatically
  6724. ** updated at the same time.
  6725. **
  6726. ** [[sqlite3_backup_finish()]] <b>sqlite3_backup_finish()</b>
  6727. **
  6728. ** When sqlite3_backup_step() has returned [SQLITE_DONE], or when the
  6729. ** application wishes to abandon the backup operation, the application
  6730. ** should destroy the [sqlite3_backup] by passing it to sqlite3_backup_finish().
  6731. ** ^The sqlite3_backup_finish() interfaces releases all
  6732. ** resources associated with the [sqlite3_backup] object.
  6733. ** ^If sqlite3_backup_step() has not yet returned [SQLITE_DONE], then any
  6734. ** active write-transaction on the destination database is rolled back.
  6735. ** The [sqlite3_backup] object is invalid
  6736. ** and may not be used following a call to sqlite3_backup_finish().
  6737. **
  6738. ** ^The value returned by sqlite3_backup_finish is [SQLITE_OK] if no
  6739. ** sqlite3_backup_step() errors occurred, regardless or whether or not
  6740. ** sqlite3_backup_step() completed.
  6741. ** ^If an out-of-memory condition or IO error occurred during any prior
  6742. ** sqlite3_backup_step() call on the same [sqlite3_backup] object, then
  6743. ** sqlite3_backup_finish() returns the corresponding [error code].
  6744. **
  6745. ** ^A return of [SQLITE_BUSY] or [SQLITE_LOCKED] from sqlite3_backup_step()
  6746. ** is not a permanent error and does not affect the return value of
  6747. ** sqlite3_backup_finish().
  6748. **
  6749. ** [[sqlite3_backup__remaining()]] [[sqlite3_backup_pagecount()]]
  6750. ** <b>sqlite3_backup_remaining() and sqlite3_backup_pagecount()</b>
  6751. **
  6752. ** ^Each call to sqlite3_backup_step() sets two values inside
  6753. ** the [sqlite3_backup] object: the number of pages still to be backed
  6754. ** up and the total number of pages in the source database file.
  6755. ** The sqlite3_backup_remaining() and sqlite3_backup_pagecount() interfaces
  6756. ** retrieve these two values, respectively.
  6757. **
  6758. ** ^The values returned by these functions are only updated by
  6759. ** sqlite3_backup_step(). ^If the source database is modified during a backup
  6760. ** operation, then the values are not updated to account for any extra
  6761. ** pages that need to be updated or the size of the source database file
  6762. ** changing.
  6763. **
  6764. ** <b>Concurrent Usage of Database Handles</b>
  6765. **
  6766. ** ^The source [database connection] may be used by the application for other
  6767. ** purposes while a backup operation is underway or being initialized.
  6768. ** ^If SQLite is compiled and configured to support threadsafe database
  6769. ** connections, then the source database connection may be used concurrently
  6770. ** from within other threads.
  6771. **
  6772. ** However, the application must guarantee that the destination
  6773. ** [database connection] is not passed to any other API (by any thread) after
  6774. ** sqlite3_backup_init() is called and before the corresponding call to
  6775. ** sqlite3_backup_finish(). SQLite does not currently check to see
  6776. ** if the application incorrectly accesses the destination [database connection]
  6777. ** and so no error code is reported, but the operations may malfunction
  6778. ** nevertheless. Use of the destination database connection while a
  6779. ** backup is in progress might also also cause a mutex deadlock.
  6780. **
  6781. ** If running in [shared cache mode], the application must
  6782. ** guarantee that the shared cache used by the destination database
  6783. ** is not accessed while the backup is running. In practice this means
  6784. ** that the application must guarantee that the disk file being
  6785. ** backed up to is not accessed by any connection within the process,
  6786. ** not just the specific connection that was passed to sqlite3_backup_init().
  6787. **
  6788. ** The [sqlite3_backup] object itself is partially threadsafe. Multiple
  6789. ** threads may safely make multiple concurrent calls to sqlite3_backup_step().
  6790. ** However, the sqlite3_backup_remaining() and sqlite3_backup_pagecount()
  6791. ** APIs are not strictly speaking threadsafe. If they are invoked at the
  6792. ** same time as another thread is invoking sqlite3_backup_step() it is
  6793. ** possible that they return invalid values.
  6794. */
  6795. SQLITE_API sqlite3_backup *sqlite3_backup_init(
  6796. sqlite3 *pDest, /* Destination database handle */
  6797. const char *zDestName, /* Destination database name */
  6798. sqlite3 *pSource, /* Source database handle */
  6799. const char *zSourceName /* Source database name */
  6800. );
  6801. SQLITE_API int sqlite3_backup_step(sqlite3_backup *p, int nPage);
  6802. SQLITE_API int sqlite3_backup_finish(sqlite3_backup *p);
  6803. SQLITE_API int sqlite3_backup_remaining(sqlite3_backup *p);
  6804. SQLITE_API int sqlite3_backup_pagecount(sqlite3_backup *p);
  6805. /*
  6806. ** CAPI3REF: Unlock Notification
  6807. **
  6808. ** ^When running in shared-cache mode, a database operation may fail with
  6809. ** an [SQLITE_LOCKED] error if the required locks on the shared-cache or
  6810. ** individual tables within the shared-cache cannot be obtained. See
  6811. ** [SQLite Shared-Cache Mode] for a description of shared-cache locking.
  6812. ** ^This API may be used to register a callback that SQLite will invoke
  6813. ** when the connection currently holding the required lock relinquishes it.
  6814. ** ^This API is only available if the library was compiled with the
  6815. ** [SQLITE_ENABLE_UNLOCK_NOTIFY] C-preprocessor symbol defined.
  6816. **
  6817. ** See Also: [Using the SQLite Unlock Notification Feature].
  6818. **
  6819. ** ^Shared-cache locks are released when a database connection concludes
  6820. ** its current transaction, either by committing it or rolling it back.
  6821. **
  6822. ** ^When a connection (known as the blocked connection) fails to obtain a
  6823. ** shared-cache lock and SQLITE_LOCKED is returned to the caller, the
  6824. ** identity of the database connection (the blocking connection) that
  6825. ** has locked the required resource is stored internally. ^After an
  6826. ** application receives an SQLITE_LOCKED error, it may call the
  6827. ** sqlite3_unlock_notify() method with the blocked connection handle as
  6828. ** the first argument to register for a callback that will be invoked
  6829. ** when the blocking connections current transaction is concluded. ^The
  6830. ** callback is invoked from within the [sqlite3_step] or [sqlite3_close]
  6831. ** call that concludes the blocking connections transaction.
  6832. **
  6833. ** ^(If sqlite3_unlock_notify() is called in a multi-threaded application,
  6834. ** there is a chance that the blocking connection will have already
  6835. ** concluded its transaction by the time sqlite3_unlock_notify() is invoked.
  6836. ** If this happens, then the specified callback is invoked immediately,
  6837. ** from within the call to sqlite3_unlock_notify().)^
  6838. **
  6839. ** ^If the blocked connection is attempting to obtain a write-lock on a
  6840. ** shared-cache table, and more than one other connection currently holds
  6841. ** a read-lock on the same table, then SQLite arbitrarily selects one of
  6842. ** the other connections to use as the blocking connection.
  6843. **
  6844. ** ^(There may be at most one unlock-notify callback registered by a
  6845. ** blocked connection. If sqlite3_unlock_notify() is called when the
  6846. ** blocked connection already has a registered unlock-notify callback,
  6847. ** then the new callback replaces the old.)^ ^If sqlite3_unlock_notify() is
  6848. ** called with a NULL pointer as its second argument, then any existing
  6849. ** unlock-notify callback is canceled. ^The blocked connections
  6850. ** unlock-notify callback may also be canceled by closing the blocked
  6851. ** connection using [sqlite3_close()].
  6852. **
  6853. ** The unlock-notify callback is not reentrant. If an application invokes
  6854. ** any sqlite3_xxx API functions from within an unlock-notify callback, a
  6855. ** crash or deadlock may be the result.
  6856. **
  6857. ** ^Unless deadlock is detected (see below), sqlite3_unlock_notify() always
  6858. ** returns SQLITE_OK.
  6859. **
  6860. ** <b>Callback Invocation Details</b>
  6861. **
  6862. ** When an unlock-notify callback is registered, the application provides a
  6863. ** single void* pointer that is passed to the callback when it is invoked.
  6864. ** However, the signature of the callback function allows SQLite to pass
  6865. ** it an array of void* context pointers. The first argument passed to
  6866. ** an unlock-notify callback is a pointer to an array of void* pointers,
  6867. ** and the second is the number of entries in the array.
  6868. **
  6869. ** When a blocking connections transaction is concluded, there may be
  6870. ** more than one blocked connection that has registered for an unlock-notify
  6871. ** callback. ^If two or more such blocked connections have specified the
  6872. ** same callback function, then instead of invoking the callback function
  6873. ** multiple times, it is invoked once with the set of void* context pointers
  6874. ** specified by the blocked connections bundled together into an array.
  6875. ** This gives the application an opportunity to prioritize any actions
  6876. ** related to the set of unblocked database connections.
  6877. **
  6878. ** <b>Deadlock Detection</b>
  6879. **
  6880. ** Assuming that after registering for an unlock-notify callback a
  6881. ** database waits for the callback to be issued before taking any further
  6882. ** action (a reasonable assumption), then using this API may cause the
  6883. ** application to deadlock. For example, if connection X is waiting for
  6884. ** connection Y's transaction to be concluded, and similarly connection
  6885. ** Y is waiting on connection X's transaction, then neither connection
  6886. ** will proceed and the system may remain deadlocked indefinitely.
  6887. **
  6888. ** To avoid this scenario, the sqlite3_unlock_notify() performs deadlock
  6889. ** detection. ^If a given call to sqlite3_unlock_notify() would put the
  6890. ** system in a deadlocked state, then SQLITE_LOCKED is returned and no
  6891. ** unlock-notify callback is registered. The system is said to be in
  6892. ** a deadlocked state if connection A has registered for an unlock-notify
  6893. ** callback on the conclusion of connection B's transaction, and connection
  6894. ** B has itself registered for an unlock-notify callback when connection
  6895. ** A's transaction is concluded. ^Indirect deadlock is also detected, so
  6896. ** the system is also considered to be deadlocked if connection B has
  6897. ** registered for an unlock-notify callback on the conclusion of connection
  6898. ** C's transaction, where connection C is waiting on connection A. ^Any
  6899. ** number of levels of indirection are allowed.
  6900. **
  6901. ** <b>The "DROP TABLE" Exception</b>
  6902. **
  6903. ** When a call to [sqlite3_step()] returns SQLITE_LOCKED, it is almost
  6904. ** always appropriate to call sqlite3_unlock_notify(). There is however,
  6905. ** one exception. When executing a "DROP TABLE" or "DROP INDEX" statement,
  6906. ** SQLite checks if there are any currently executing SELECT statements
  6907. ** that belong to the same connection. If there are, SQLITE_LOCKED is
  6908. ** returned. In this case there is no "blocking connection", so invoking
  6909. ** sqlite3_unlock_notify() results in the unlock-notify callback being
  6910. ** invoked immediately. If the application then re-attempts the "DROP TABLE"
  6911. ** or "DROP INDEX" query, an infinite loop might be the result.
  6912. **
  6913. ** One way around this problem is to check the extended error code returned
  6914. ** by an sqlite3_step() call. ^(If there is a blocking connection, then the
  6915. ** extended error code is set to SQLITE_LOCKED_SHAREDCACHE. Otherwise, in
  6916. ** the special "DROP TABLE/INDEX" case, the extended error code is just
  6917. ** SQLITE_LOCKED.)^
  6918. */
  6919. SQLITE_API int sqlite3_unlock_notify(
  6920. sqlite3 *pBlocked, /* Waiting connection */
  6921. void (*xNotify)(void **apArg, int nArg), /* Callback function to invoke */
  6922. void *pNotifyArg /* Argument to pass to xNotify */
  6923. );
  6924. /*
  6925. ** CAPI3REF: String Comparison
  6926. **
  6927. ** ^The [sqlite3_strnicmp()] API allows applications and extensions to
  6928. ** compare the contents of two buffers containing UTF-8 strings in a
  6929. ** case-independent fashion, using the same definition of case independence
  6930. ** that SQLite uses internally when comparing identifiers.
  6931. */
  6932. SQLITE_API int sqlite3_strnicmp(const char *, const char *, int);
  6933. /*
  6934. ** CAPI3REF: Error Logging Interface
  6935. **
  6936. ** ^The [sqlite3_log()] interface writes a message into the error log
  6937. ** established by the [SQLITE_CONFIG_LOG] option to [sqlite3_config()].
  6938. ** ^If logging is enabled, the zFormat string and subsequent arguments are
  6939. ** used with [sqlite3_snprintf()] to generate the final output string.
  6940. **
  6941. ** The sqlite3_log() interface is intended for use by extensions such as
  6942. ** virtual tables, collating functions, and SQL functions. While there is
  6943. ** nothing to prevent an application from calling sqlite3_log(), doing so
  6944. ** is considered bad form.
  6945. **
  6946. ** The zFormat string must not be NULL.
  6947. **
  6948. ** To avoid deadlocks and other threading problems, the sqlite3_log() routine
  6949. ** will not use dynamically allocated memory. The log message is stored in
  6950. ** a fixed-length buffer on the stack. If the log message is longer than
  6951. ** a few hundred characters, it will be truncated to the length of the
  6952. ** buffer.
  6953. */
  6954. SQLITE_API void sqlite3_log(int iErrCode, const char *zFormat, ...);
  6955. /*
  6956. ** CAPI3REF: Write-Ahead Log Commit Hook
  6957. **
  6958. ** ^The [sqlite3_wal_hook()] function is used to register a callback that
  6959. ** will be invoked each time a database connection commits data to a
  6960. ** [write-ahead log] (i.e. whenever a transaction is committed in
  6961. ** [journal_mode | journal_mode=WAL mode]).
  6962. **
  6963. ** ^The callback is invoked by SQLite after the commit has taken place and
  6964. ** the associated write-lock on the database released, so the implementation
  6965. ** may read, write or [checkpoint] the database as required.
  6966. **
  6967. ** ^The first parameter passed to the callback function when it is invoked
  6968. ** is a copy of the third parameter passed to sqlite3_wal_hook() when
  6969. ** registering the callback. ^The second is a copy of the database handle.
  6970. ** ^The third parameter is the name of the database that was written to -
  6971. ** either "main" or the name of an [ATTACH]-ed database. ^The fourth parameter
  6972. ** is the number of pages currently in the write-ahead log file,
  6973. ** including those that were just committed.
  6974. **
  6975. ** The callback function should normally return [SQLITE_OK]. ^If an error
  6976. ** code is returned, that error will propagate back up through the
  6977. ** SQLite code base to cause the statement that provoked the callback
  6978. ** to report an error, though the commit will have still occurred. If the
  6979. ** callback returns [SQLITE_ROW] or [SQLITE_DONE], or if it returns a value
  6980. ** that does not correspond to any valid SQLite error code, the results
  6981. ** are undefined.
  6982. **
  6983. ** A single database handle may have at most a single write-ahead log callback
  6984. ** registered at one time. ^Calling [sqlite3_wal_hook()] replaces any
  6985. ** previously registered write-ahead log callback. ^Note that the
  6986. ** [sqlite3_wal_autocheckpoint()] interface and the
  6987. ** [wal_autocheckpoint pragma] both invoke [sqlite3_wal_hook()] and will
  6988. ** those overwrite any prior [sqlite3_wal_hook()] settings.
  6989. */
  6990. SQLITE_API void *sqlite3_wal_hook(
  6991. sqlite3*,
  6992. int(*)(void *,sqlite3*,const char*,int),
  6993. void*
  6994. );
  6995. /*
  6996. ** CAPI3REF: Configure an auto-checkpoint
  6997. **
  6998. ** ^The [sqlite3_wal_autocheckpoint(D,N)] is a wrapper around
  6999. ** [sqlite3_wal_hook()] that causes any database on [database connection] D
  7000. ** to automatically [checkpoint]
  7001. ** after committing a transaction if there are N or
  7002. ** more frames in the [write-ahead log] file. ^Passing zero or
  7003. ** a negative value as the nFrame parameter disables automatic
  7004. ** checkpoints entirely.
  7005. **
  7006. ** ^The callback registered by this function replaces any existing callback
  7007. ** registered using [sqlite3_wal_hook()]. ^Likewise, registering a callback
  7008. ** using [sqlite3_wal_hook()] disables the automatic checkpoint mechanism
  7009. ** configured by this function.
  7010. **
  7011. ** ^The [wal_autocheckpoint pragma] can be used to invoke this interface
  7012. ** from SQL.
  7013. **
  7014. ** ^Every new [database connection] defaults to having the auto-checkpoint
  7015. ** enabled with a threshold of 1000 or [SQLITE_DEFAULT_WAL_AUTOCHECKPOINT]
  7016. ** pages. The use of this interface
  7017. ** is only necessary if the default setting is found to be suboptimal
  7018. ** for a particular application.
  7019. */
  7020. SQLITE_API int sqlite3_wal_autocheckpoint(sqlite3 *db, int N);
  7021. /*
  7022. ** CAPI3REF: Checkpoint a database
  7023. **
  7024. ** ^The [sqlite3_wal_checkpoint(D,X)] interface causes database named X
  7025. ** on [database connection] D to be [checkpointed]. ^If X is NULL or an
  7026. ** empty string, then a checkpoint is run on all databases of
  7027. ** connection D. ^If the database connection D is not in
  7028. ** [WAL | write-ahead log mode] then this interface is a harmless no-op.
  7029. **
  7030. ** ^The [wal_checkpoint pragma] can be used to invoke this interface
  7031. ** from SQL. ^The [sqlite3_wal_autocheckpoint()] interface and the
  7032. ** [wal_autocheckpoint pragma] can be used to cause this interface to be
  7033. ** run whenever the WAL reaches a certain size threshold.
  7034. **
  7035. ** See also: [sqlite3_wal_checkpoint_v2()]
  7036. */
  7037. SQLITE_API int sqlite3_wal_checkpoint(sqlite3 *db, const char *zDb);
  7038. /*
  7039. ** CAPI3REF: Checkpoint a database
  7040. **
  7041. ** Run a checkpoint operation on WAL database zDb attached to database
  7042. ** handle db. The specific operation is determined by the value of the
  7043. ** eMode parameter:
  7044. **
  7045. ** <dl>
  7046. ** <dt>SQLITE_CHECKPOINT_PASSIVE<dd>
  7047. ** Checkpoint as many frames as possible without waiting for any database
  7048. ** readers or writers to finish. Sync the db file if all frames in the log
  7049. ** are checkpointed. This mode is the same as calling
  7050. ** sqlite3_wal_checkpoint(). The busy-handler callback is never invoked.
  7051. **
  7052. ** <dt>SQLITE_CHECKPOINT_FULL<dd>
  7053. ** This mode blocks (calls the busy-handler callback) until there is no
  7054. ** database writer and all readers are reading from the most recent database
  7055. ** snapshot. It then checkpoints all frames in the log file and syncs the
  7056. ** database file. This call blocks database writers while it is running,
  7057. ** but not database readers.
  7058. **
  7059. ** <dt>SQLITE_CHECKPOINT_RESTART<dd>
  7060. ** This mode works the same way as SQLITE_CHECKPOINT_FULL, except after
  7061. ** checkpointing the log file it blocks (calls the busy-handler callback)
  7062. ** until all readers are reading from the database file only. This ensures
  7063. ** that the next client to write to the database file restarts the log file
  7064. ** from the beginning. This call blocks database writers while it is running,
  7065. ** but not database readers.
  7066. ** </dl>
  7067. **
  7068. ** If pnLog is not NULL, then *pnLog is set to the total number of frames in
  7069. ** the log file before returning. If pnCkpt is not NULL, then *pnCkpt is set to
  7070. ** the total number of checkpointed frames (including any that were already
  7071. ** checkpointed when this function is called). *pnLog and *pnCkpt may be
  7072. ** populated even if sqlite3_wal_checkpoint_v2() returns other than SQLITE_OK.
  7073. ** If no values are available because of an error, they are both set to -1
  7074. ** before returning to communicate this to the caller.
  7075. **
  7076. ** All calls obtain an exclusive "checkpoint" lock on the database file. If
  7077. ** any other process is running a checkpoint operation at the same time, the
  7078. ** lock cannot be obtained and SQLITE_BUSY is returned. Even if there is a
  7079. ** busy-handler configured, it will not be invoked in this case.
  7080. **
  7081. ** The SQLITE_CHECKPOINT_FULL and RESTART modes also obtain the exclusive
  7082. ** "writer" lock on the database file. If the writer lock cannot be obtained
  7083. ** immediately, and a busy-handler is configured, it is invoked and the writer
  7084. ** lock retried until either the busy-handler returns 0 or the lock is
  7085. ** successfully obtained. The busy-handler is also invoked while waiting for
  7086. ** database readers as described above. If the busy-handler returns 0 before
  7087. ** the writer lock is obtained or while waiting for database readers, the
  7088. ** checkpoint operation proceeds from that point in the same way as
  7089. ** SQLITE_CHECKPOINT_PASSIVE - checkpointing as many frames as possible
  7090. ** without blocking any further. SQLITE_BUSY is returned in this case.
  7091. **
  7092. ** If parameter zDb is NULL or points to a zero length string, then the
  7093. ** specified operation is attempted on all WAL databases. In this case the
  7094. ** values written to output parameters *pnLog and *pnCkpt are undefined. If
  7095. ** an SQLITE_BUSY error is encountered when processing one or more of the
  7096. ** attached WAL databases, the operation is still attempted on any remaining
  7097. ** attached databases and SQLITE_BUSY is returned to the caller. If any other
  7098. ** error occurs while processing an attached database, processing is abandoned
  7099. ** and the error code returned to the caller immediately. If no error
  7100. ** (SQLITE_BUSY or otherwise) is encountered while processing the attached
  7101. ** databases, SQLITE_OK is returned.
  7102. **
  7103. ** If database zDb is the name of an attached database that is not in WAL
  7104. ** mode, SQLITE_OK is returned and both *pnLog and *pnCkpt set to -1. If
  7105. ** zDb is not NULL (or a zero length string) and is not the name of any
  7106. ** attached database, SQLITE_ERROR is returned to the caller.
  7107. */
  7108. SQLITE_API int sqlite3_wal_checkpoint_v2(
  7109. sqlite3 *db, /* Database handle */
  7110. const char *zDb, /* Name of attached database (or NULL) */
  7111. int eMode, /* SQLITE_CHECKPOINT_* value */
  7112. int *pnLog, /* OUT: Size of WAL log in frames */
  7113. int *pnCkpt /* OUT: Total number of frames checkpointed */
  7114. );
  7115. /*
  7116. ** CAPI3REF: Checkpoint operation parameters
  7117. **
  7118. ** These constants can be used as the 3rd parameter to
  7119. ** [sqlite3_wal_checkpoint_v2()]. See the [sqlite3_wal_checkpoint_v2()]
  7120. ** documentation for additional information about the meaning and use of
  7121. ** each of these values.
  7122. */
  7123. #define SQLITE_CHECKPOINT_PASSIVE 0
  7124. #define SQLITE_CHECKPOINT_FULL 1
  7125. #define SQLITE_CHECKPOINT_RESTART 2
  7126. /*
  7127. ** CAPI3REF: Virtual Table Interface Configuration
  7128. **
  7129. ** This function may be called by either the [xConnect] or [xCreate] method
  7130. ** of a [virtual table] implementation to configure
  7131. ** various facets of the virtual table interface.
  7132. **
  7133. ** If this interface is invoked outside the context of an xConnect or
  7134. ** xCreate virtual table method then the behavior is undefined.
  7135. **
  7136. ** At present, there is only one option that may be configured using
  7137. ** this function. (See [SQLITE_VTAB_CONSTRAINT_SUPPORT].) Further options
  7138. ** may be added in the future.
  7139. */
  7140. SQLITE_API int sqlite3_vtab_config(sqlite3*, int op, ...);
  7141. /*
  7142. ** CAPI3REF: Virtual Table Configuration Options
  7143. **
  7144. ** These macros define the various options to the
  7145. ** [sqlite3_vtab_config()] interface that [virtual table] implementations
  7146. ** can use to customize and optimize their behavior.
  7147. **
  7148. ** <dl>
  7149. ** <dt>SQLITE_VTAB_CONSTRAINT_SUPPORT
  7150. ** <dd>Calls of the form
  7151. ** [sqlite3_vtab_config](db,SQLITE_VTAB_CONSTRAINT_SUPPORT,X) are supported,
  7152. ** where X is an integer. If X is zero, then the [virtual table] whose
  7153. ** [xCreate] or [xConnect] method invoked [sqlite3_vtab_config()] does not
  7154. ** support constraints. In this configuration (which is the default) if
  7155. ** a call to the [xUpdate] method returns [SQLITE_CONSTRAINT], then the entire
  7156. ** statement is rolled back as if [ON CONFLICT | OR ABORT] had been
  7157. ** specified as part of the users SQL statement, regardless of the actual
  7158. ** ON CONFLICT mode specified.
  7159. **
  7160. ** If X is non-zero, then the virtual table implementation guarantees
  7161. ** that if [xUpdate] returns [SQLITE_CONSTRAINT], it will do so before
  7162. ** any modifications to internal or persistent data structures have been made.
  7163. ** If the [ON CONFLICT] mode is ABORT, FAIL, IGNORE or ROLLBACK, SQLite
  7164. ** is able to roll back a statement or database transaction, and abandon
  7165. ** or continue processing the current SQL statement as appropriate.
  7166. ** If the ON CONFLICT mode is REPLACE and the [xUpdate] method returns
  7167. ** [SQLITE_CONSTRAINT], SQLite handles this as if the ON CONFLICT mode
  7168. ** had been ABORT.
  7169. **
  7170. ** Virtual table implementations that are required to handle OR REPLACE
  7171. ** must do so within the [xUpdate] method. If a call to the
  7172. ** [sqlite3_vtab_on_conflict()] function indicates that the current ON
  7173. ** CONFLICT policy is REPLACE, the virtual table implementation should
  7174. ** silently replace the appropriate rows within the xUpdate callback and
  7175. ** return SQLITE_OK. Or, if this is not possible, it may return
  7176. ** SQLITE_CONSTRAINT, in which case SQLite falls back to OR ABORT
  7177. ** constraint handling.
  7178. ** </dl>
  7179. */
  7180. #define SQLITE_VTAB_CONSTRAINT_SUPPORT 1
  7181. /*
  7182. ** CAPI3REF: Determine The Virtual Table Conflict Policy
  7183. **
  7184. ** This function may only be called from within a call to the [xUpdate] method
  7185. ** of a [virtual table] implementation for an INSERT or UPDATE operation. ^The
  7186. ** value returned is one of [SQLITE_ROLLBACK], [SQLITE_IGNORE], [SQLITE_FAIL],
  7187. ** [SQLITE_ABORT], or [SQLITE_REPLACE], according to the [ON CONFLICT] mode
  7188. ** of the SQL statement that triggered the call to the [xUpdate] method of the
  7189. ** [virtual table].
  7190. */
  7191. SQLITE_API int sqlite3_vtab_on_conflict(sqlite3 *);
  7192. /*
  7193. ** CAPI3REF: Conflict resolution modes
  7194. **
  7195. ** These constants are returned by [sqlite3_vtab_on_conflict()] to
  7196. ** inform a [virtual table] implementation what the [ON CONFLICT] mode
  7197. ** is for the SQL statement being evaluated.
  7198. **
  7199. ** Note that the [SQLITE_IGNORE] constant is also used as a potential
  7200. ** return value from the [sqlite3_set_authorizer()] callback and that
  7201. ** [SQLITE_ABORT] is also a [result code].
  7202. */
  7203. #define SQLITE_ROLLBACK 1
  7204. /* #define SQLITE_IGNORE 2 // Also used by sqlite3_authorizer() callback */
  7205. #define SQLITE_FAIL 3
  7206. /* #define SQLITE_ABORT 4 // Also an error code */
  7207. #define SQLITE_REPLACE 5
  7208. /*
  7209. ** Undo the hack that converts floating point types to integer for
  7210. ** builds on processors without floating point support.
  7211. */
  7212. #ifdef SQLITE_OMIT_FLOATING_POINT
  7213. # undef double
  7214. #endif
  7215. #if 0
  7216. } /* End of the 'extern "C"' block */
  7217. #endif
  7218. #endif
  7219. /*
  7220. ** 2010 August 30
  7221. **
  7222. ** The author disclaims copyright to this source code. In place of
  7223. ** a legal notice, here is a blessing:
  7224. **
  7225. ** May you do good and not evil.
  7226. ** May you find forgiveness for yourself and forgive others.
  7227. ** May you share freely, never taking more than you give.
  7228. **
  7229. *************************************************************************
  7230. */
  7231. #ifndef _SQLITE3RTREE_H_
  7232. #define _SQLITE3RTREE_H_
  7233. #if 0
  7234. extern "C" {
  7235. #endif
  7236. typedef struct sqlite3_rtree_geometry sqlite3_rtree_geometry;
  7237. /*
  7238. ** Register a geometry callback named zGeom that can be used as part of an
  7239. ** R-Tree geometry query as follows:
  7240. **
  7241. ** SELECT ... FROM <rtree> WHERE <rtree col> MATCH $zGeom(... params ...)
  7242. */
  7243. SQLITE_API int sqlite3_rtree_geometry_callback(
  7244. sqlite3 *db,
  7245. const char *zGeom,
  7246. int (*xGeom)(sqlite3_rtree_geometry *, int nCoord, double *aCoord, int *pRes),
  7247. void *pContext
  7248. );
  7249. /*
  7250. ** A pointer to a structure of the following type is passed as the first
  7251. ** argument to callbacks registered using rtree_geometry_callback().
  7252. */
  7253. struct sqlite3_rtree_geometry {
  7254. void *pContext; /* Copy of pContext passed to s_r_g_c() */
  7255. int nParam; /* Size of array aParam[] */
  7256. double *aParam; /* Parameters passed to SQL geom function */
  7257. void *pUser; /* Callback implementation user data */
  7258. void (*xDelUser)(void *); /* Called by SQLite to clean up pUser */
  7259. };
  7260. #if 0
  7261. } /* end of the 'extern "C"' block */
  7262. #endif
  7263. #endif /* ifndef _SQLITE3RTREE_H_ */
  7264. /************** End of sqlite3.h *********************************************/
  7265. /************** Continuing where we left off in sqliteInt.h ******************/
  7266. /************** Include hash.h in the middle of sqliteInt.h ******************/
  7267. /************** Begin file hash.h ********************************************/
  7268. /*
  7269. ** 2001 September 22
  7270. **
  7271. ** The author disclaims copyright to this source code. In place of
  7272. ** a legal notice, here is a blessing:
  7273. **
  7274. ** May you do good and not evil.
  7275. ** May you find forgiveness for yourself and forgive others.
  7276. ** May you share freely, never taking more than you give.
  7277. **
  7278. *************************************************************************
  7279. ** This is the header file for the generic hash-table implemenation
  7280. ** used in SQLite.
  7281. */
  7282. #ifndef _SQLITE_HASH_H_
  7283. #define _SQLITE_HASH_H_
  7284. /* Forward declarations of structures. */
  7285. typedef struct Hash Hash;
  7286. typedef struct HashElem HashElem;
  7287. /* A complete hash table is an instance of the following structure.
  7288. ** The internals of this structure are intended to be opaque -- client
  7289. ** code should not attempt to access or modify the fields of this structure
  7290. ** directly. Change this structure only by using the routines below.
  7291. ** However, some of the "procedures" and "functions" for modifying and
  7292. ** accessing this structure are really macros, so we can't really make
  7293. ** this structure opaque.
  7294. **
  7295. ** All elements of the hash table are on a single doubly-linked list.
  7296. ** Hash.first points to the head of this list.
  7297. **
  7298. ** There are Hash.htsize buckets. Each bucket points to a spot in
  7299. ** the global doubly-linked list. The contents of the bucket are the
  7300. ** element pointed to plus the next _ht.count-1 elements in the list.
  7301. **
  7302. ** Hash.htsize and Hash.ht may be zero. In that case lookup is done
  7303. ** by a linear search of the global list. For small tables, the
  7304. ** Hash.ht table is never allocated because if there are few elements
  7305. ** in the table, it is faster to do a linear search than to manage
  7306. ** the hash table.
  7307. */
  7308. struct Hash {
  7309. unsigned int htsize; /* Number of buckets in the hash table */
  7310. unsigned int count; /* Number of entries in this table */
  7311. HashElem *first; /* The first element of the array */
  7312. struct _ht { /* the hash table */
  7313. int count; /* Number of entries with this hash */
  7314. HashElem *chain; /* Pointer to first entry with this hash */
  7315. } *ht;
  7316. };
  7317. /* Each element in the hash table is an instance of the following
  7318. ** structure. All elements are stored on a single doubly-linked list.
  7319. **
  7320. ** Again, this structure is intended to be opaque, but it can't really
  7321. ** be opaque because it is used by macros.
  7322. */
  7323. struct HashElem {
  7324. HashElem *next, *prev; /* Next and previous elements in the table */
  7325. void *data; /* Data associated with this element */
  7326. const char *pKey; int nKey; /* Key associated with this element */
  7327. };
  7328. /*
  7329. ** Access routines. To delete, insert a NULL pointer.
  7330. */
  7331. SQLITE_PRIVATE void sqlite3HashInit(Hash*);
  7332. SQLITE_PRIVATE void *sqlite3HashInsert(Hash*, const char *pKey, int nKey, void *pData);
  7333. SQLITE_PRIVATE void *sqlite3HashFind(const Hash*, const char *pKey, int nKey);
  7334. SQLITE_PRIVATE void sqlite3HashClear(Hash*);
  7335. /*
  7336. ** Macros for looping over all elements of a hash table. The idiom is
  7337. ** like this:
  7338. **
  7339. ** Hash h;
  7340. ** HashElem *p;
  7341. ** ...
  7342. ** for(p=sqliteHashFirst(&h); p; p=sqliteHashNext(p)){
  7343. ** SomeStructure *pData = sqliteHashData(p);
  7344. ** // do something with pData
  7345. ** }
  7346. */
  7347. #define sqliteHashFirst(H) ((H)->first)
  7348. #define sqliteHashNext(E) ((E)->next)
  7349. #define sqliteHashData(E) ((E)->data)
  7350. /* #define sqliteHashKey(E) ((E)->pKey) // NOT USED */
  7351. /* #define sqliteHashKeysize(E) ((E)->nKey) // NOT USED */
  7352. /*
  7353. ** Number of entries in a hash table
  7354. */
  7355. /* #define sqliteHashCount(H) ((H)->count) // NOT USED */
  7356. #endif /* _SQLITE_HASH_H_ */
  7357. /************** End of hash.h ************************************************/
  7358. /************** Continuing where we left off in sqliteInt.h ******************/
  7359. /************** Include parse.h in the middle of sqliteInt.h *****************/
  7360. /************** Begin file parse.h *******************************************/
  7361. #define TK_SEMI 1
  7362. #define TK_EXPLAIN 2
  7363. #define TK_QUERY 3
  7364. #define TK_PLAN 4
  7365. #define TK_BEGIN 5
  7366. #define TK_TRANSACTION 6
  7367. #define TK_DEFERRED 7
  7368. #define TK_IMMEDIATE 8
  7369. #define TK_EXCLUSIVE 9
  7370. #define TK_COMMIT 10
  7371. #define TK_END 11
  7372. #define TK_ROLLBACK 12
  7373. #define TK_SAVEPOINT 13
  7374. #define TK_RELEASE 14
  7375. #define TK_TO 15
  7376. #define TK_TABLE 16
  7377. #define TK_CREATE 17
  7378. #define TK_IF 18
  7379. #define TK_NOT 19
  7380. #define TK_EXISTS 20
  7381. #define TK_TEMP 21
  7382. #define TK_LP 22
  7383. #define TK_RP 23
  7384. #define TK_AS 24
  7385. #define TK_COMMA 25
  7386. #define TK_ID 26
  7387. #define TK_INDEXED 27
  7388. #define TK_ABORT 28
  7389. #define TK_ACTION 29
  7390. #define TK_AFTER 30
  7391. #define TK_ANALYZE 31
  7392. #define TK_ASC 32
  7393. #define TK_ATTACH 33
  7394. #define TK_BEFORE 34
  7395. #define TK_BY 35
  7396. #define TK_CASCADE 36
  7397. #define TK_CAST 37
  7398. #define TK_COLUMNKW 38
  7399. #define TK_CONFLICT 39
  7400. #define TK_DATABASE 40
  7401. #define TK_DESC 41
  7402. #define TK_DETACH 42
  7403. #define TK_EACH 43
  7404. #define TK_FAIL 44
  7405. #define TK_FOR 45
  7406. #define TK_IGNORE 46
  7407. #define TK_INITIALLY 47
  7408. #define TK_INSTEAD 48
  7409. #define TK_LIKE_KW 49
  7410. #define TK_MATCH 50
  7411. #define TK_NO 51
  7412. #define TK_KEY 52
  7413. #define TK_OF 53
  7414. #define TK_OFFSET 54
  7415. #define TK_PRAGMA 55
  7416. #define TK_RAISE 56
  7417. #define TK_REPLACE 57
  7418. #define TK_RESTRICT 58
  7419. #define TK_ROW 59
  7420. #define TK_TRIGGER 60
  7421. #define TK_VACUUM 61
  7422. #define TK_VIEW 62
  7423. #define TK_VIRTUAL 63
  7424. #define TK_REINDEX 64
  7425. #define TK_RENAME 65
  7426. #define TK_CTIME_KW 66
  7427. #define TK_ANY 67
  7428. #define TK_OR 68
  7429. #define TK_AND 69
  7430. #define TK_IS 70
  7431. #define TK_BETWEEN 71
  7432. #define TK_IN 72
  7433. #define TK_ISNULL 73
  7434. #define TK_NOTNULL 74
  7435. #define TK_NE 75
  7436. #define TK_EQ 76
  7437. #define TK_GT 77
  7438. #define TK_LE 78
  7439. #define TK_LT 79
  7440. #define TK_GE 80
  7441. #define TK_ESCAPE 81
  7442. #define TK_BITAND 82
  7443. #define TK_BITOR 83
  7444. #define TK_LSHIFT 84
  7445. #define TK_RSHIFT 85
  7446. #define TK_PLUS 86
  7447. #define TK_MINUS 87
  7448. #define TK_STAR 88
  7449. #define TK_SLASH 89
  7450. #define TK_REM 90
  7451. #define TK_CONCAT 91
  7452. #define TK_COLLATE 92
  7453. #define TK_BITNOT 93
  7454. #define TK_STRING 94
  7455. #define TK_JOIN_KW 95
  7456. #define TK_CONSTRAINT 96
  7457. #define TK_DEFAULT 97
  7458. #define TK_NULL 98
  7459. #define TK_PRIMARY 99
  7460. #define TK_UNIQUE 100
  7461. #define TK_CHECK 101
  7462. #define TK_REFERENCES 102
  7463. #define TK_AUTOINCR 103
  7464. #define TK_ON 104
  7465. #define TK_INSERT 105
  7466. #define TK_DELETE 106
  7467. #define TK_UPDATE 107
  7468. #define TK_SET 108
  7469. #define TK_DEFERRABLE 109
  7470. #define TK_FOREIGN 110
  7471. #define TK_DROP 111
  7472. #define TK_UNION 112
  7473. #define TK_ALL 113
  7474. #define TK_EXCEPT 114
  7475. #define TK_INTERSECT 115
  7476. #define TK_SELECT 116
  7477. #define TK_DISTINCT 117
  7478. #define TK_DOT 118
  7479. #define TK_FROM 119
  7480. #define TK_JOIN 120
  7481. #define TK_USING 121
  7482. #define TK_ORDER 122
  7483. #define TK_GROUP 123
  7484. #define TK_HAVING 124
  7485. #define TK_LIMIT 125
  7486. #define TK_WHERE 126
  7487. #define TK_INTO 127
  7488. #define TK_VALUES 128
  7489. #define TK_INTEGER 129
  7490. #define TK_FLOAT 130
  7491. #define TK_BLOB 131
  7492. #define TK_REGISTER 132
  7493. #define TK_VARIABLE 133
  7494. #define TK_CASE 134
  7495. #define TK_WHEN 135
  7496. #define TK_THEN 136
  7497. #define TK_ELSE 137
  7498. #define TK_INDEX 138
  7499. #define TK_ALTER 139
  7500. #define TK_ADD 140
  7501. #define TK_TO_TEXT 141
  7502. #define TK_TO_BLOB 142
  7503. #define TK_TO_NUMERIC 143
  7504. #define TK_TO_INT 144
  7505. #define TK_TO_REAL 145
  7506. #define TK_ISNOT 146
  7507. #define TK_END_OF_FILE 147
  7508. #define TK_ILLEGAL 148
  7509. #define TK_SPACE 149
  7510. #define TK_UNCLOSED_STRING 150
  7511. #define TK_FUNCTION 151
  7512. #define TK_COLUMN 152
  7513. #define TK_AGG_FUNCTION 153
  7514. #define TK_AGG_COLUMN 154
  7515. #define TK_CONST_FUNC 155
  7516. #define TK_UMINUS 156
  7517. #define TK_UPLUS 157
  7518. /************** End of parse.h ***********************************************/
  7519. /************** Continuing where we left off in sqliteInt.h ******************/
  7520. #include <stdio.h>
  7521. #include <stdlib.h>
  7522. #include <string.h>
  7523. #include <assert.h>
  7524. #include <stddef.h>
  7525. /*
  7526. ** If compiling for a processor that lacks floating point support,
  7527. ** substitute integer for floating-point
  7528. */
  7529. #ifdef SQLITE_OMIT_FLOATING_POINT
  7530. # define double sqlite_int64
  7531. # define float sqlite_int64
  7532. # define LONGDOUBLE_TYPE sqlite_int64
  7533. # ifndef SQLITE_BIG_DBL
  7534. # define SQLITE_BIG_DBL (((sqlite3_int64)1)<<50)
  7535. # endif
  7536. # define SQLITE_OMIT_DATETIME_FUNCS 1
  7537. # define SQLITE_OMIT_TRACE 1
  7538. # undef SQLITE_MIXED_ENDIAN_64BIT_FLOAT
  7539. # undef SQLITE_HAVE_ISNAN
  7540. #endif
  7541. #ifndef SQLITE_BIG_DBL
  7542. # define SQLITE_BIG_DBL (1e99)
  7543. #endif
  7544. /*
  7545. ** OMIT_TEMPDB is set to 1 if SQLITE_OMIT_TEMPDB is defined, or 0
  7546. ** afterward. Having this macro allows us to cause the C compiler
  7547. ** to omit code used by TEMP tables without messy #ifndef statements.
  7548. */
  7549. #ifdef SQLITE_OMIT_TEMPDB
  7550. #define OMIT_TEMPDB 1
  7551. #else
  7552. #define OMIT_TEMPDB 0
  7553. #endif
  7554. /*
  7555. ** The "file format" number is an integer that is incremented whenever
  7556. ** the VDBE-level file format changes. The following macros define the
  7557. ** the default file format for new databases and the maximum file format
  7558. ** that the library can read.
  7559. */
  7560. #define SQLITE_MAX_FILE_FORMAT 4
  7561. #ifndef SQLITE_DEFAULT_FILE_FORMAT
  7562. # define SQLITE_DEFAULT_FILE_FORMAT 4
  7563. #endif
  7564. /*
  7565. ** Determine whether triggers are recursive by default. This can be
  7566. ** changed at run-time using a pragma.
  7567. */
  7568. #ifndef SQLITE_DEFAULT_RECURSIVE_TRIGGERS
  7569. # define SQLITE_DEFAULT_RECURSIVE_TRIGGERS 0
  7570. #endif
  7571. /*
  7572. ** Provide a default value for SQLITE_TEMP_STORE in case it is not specified
  7573. ** on the command-line
  7574. */
  7575. #ifndef SQLITE_TEMP_STORE
  7576. # define SQLITE_TEMP_STORE 1
  7577. #endif
  7578. /*
  7579. ** GCC does not define the offsetof() macro so we'll have to do it
  7580. ** ourselves.
  7581. */
  7582. #ifndef offsetof
  7583. #define offsetof(STRUCTURE,FIELD) ((int)((char*)&((STRUCTURE*)0)->FIELD))
  7584. #endif
  7585. /*
  7586. ** Check to see if this machine uses EBCDIC. (Yes, believe it or
  7587. ** not, there are still machines out there that use EBCDIC.)
  7588. */
  7589. #if 'A' == '\301'
  7590. # define SQLITE_EBCDIC 1
  7591. #else
  7592. # define SQLITE_ASCII 1
  7593. #endif
  7594. /*
  7595. ** Integers of known sizes. These typedefs might change for architectures
  7596. ** where the sizes very. Preprocessor macros are available so that the
  7597. ** types can be conveniently redefined at compile-type. Like this:
  7598. **
  7599. ** cc '-DUINTPTR_TYPE=long long int' ...
  7600. */
  7601. #ifndef UINT32_TYPE
  7602. # ifdef HAVE_UINT32_T
  7603. # define UINT32_TYPE uint32_t
  7604. # else
  7605. # define UINT32_TYPE unsigned int
  7606. # endif
  7607. #endif
  7608. #ifndef UINT16_TYPE
  7609. # ifdef HAVE_UINT16_T
  7610. # define UINT16_TYPE uint16_t
  7611. # else
  7612. # define UINT16_TYPE unsigned short int
  7613. # endif
  7614. #endif
  7615. #ifndef INT16_TYPE
  7616. # ifdef HAVE_INT16_T
  7617. # define INT16_TYPE int16_t
  7618. # else
  7619. # define INT16_TYPE short int
  7620. # endif
  7621. #endif
  7622. #ifndef UINT8_TYPE
  7623. # ifdef HAVE_UINT8_T
  7624. # define UINT8_TYPE uint8_t
  7625. # else
  7626. # define UINT8_TYPE unsigned char
  7627. # endif
  7628. #endif
  7629. #ifndef INT8_TYPE
  7630. # ifdef HAVE_INT8_T
  7631. # define INT8_TYPE int8_t
  7632. # else
  7633. # define INT8_TYPE signed char
  7634. # endif
  7635. #endif
  7636. #ifndef LONGDOUBLE_TYPE
  7637. # define LONGDOUBLE_TYPE long double
  7638. #endif
  7639. typedef sqlite_int64 i64; /* 8-byte signed integer */
  7640. typedef sqlite_uint64 u64; /* 8-byte unsigned integer */
  7641. typedef UINT32_TYPE u32; /* 4-byte unsigned integer */
  7642. typedef UINT16_TYPE u16; /* 2-byte unsigned integer */
  7643. typedef INT16_TYPE i16; /* 2-byte signed integer */
  7644. typedef UINT8_TYPE u8; /* 1-byte unsigned integer */
  7645. typedef INT8_TYPE i8; /* 1-byte signed integer */
  7646. /*
  7647. ** SQLITE_MAX_U32 is a u64 constant that is the maximum u64 value
  7648. ** that can be stored in a u32 without loss of data. The value
  7649. ** is 0x00000000ffffffff. But because of quirks of some compilers, we
  7650. ** have to specify the value in the less intuitive manner shown:
  7651. */
  7652. #define SQLITE_MAX_U32 ((((u64)1)<<32)-1)
  7653. /*
  7654. ** The datatype used to store estimates of the number of rows in a
  7655. ** table or index. This is an unsigned integer type. For 99.9% of
  7656. ** the world, a 32-bit integer is sufficient. But a 64-bit integer
  7657. ** can be used at compile-time if desired.
  7658. */
  7659. #ifdef SQLITE_64BIT_STATS
  7660. typedef u64 tRowcnt; /* 64-bit only if requested at compile-time */
  7661. #else
  7662. typedef u32 tRowcnt; /* 32-bit is the default */
  7663. #endif
  7664. /*
  7665. ** Macros to determine whether the machine is big or little endian,
  7666. ** evaluated at runtime.
  7667. */
  7668. #ifdef SQLITE_AMALGAMATION
  7669. SQLITE_PRIVATE const int sqlite3one = 1;
  7670. #else
  7671. SQLITE_PRIVATE const int sqlite3one;
  7672. #endif
  7673. #if defined(i386) || defined(__i386__) || defined(_M_IX86)\
  7674. || defined(__x86_64) || defined(__x86_64__)
  7675. # define SQLITE_BIGENDIAN 0
  7676. # define SQLITE_LITTLEENDIAN 1
  7677. # define SQLITE_UTF16NATIVE SQLITE_UTF16LE
  7678. #else
  7679. # define SQLITE_BIGENDIAN (*(char *)(&sqlite3one)==0)
  7680. # define SQLITE_LITTLEENDIAN (*(char *)(&sqlite3one)==1)
  7681. # define SQLITE_UTF16NATIVE (SQLITE_BIGENDIAN?SQLITE_UTF16BE:SQLITE_UTF16LE)
  7682. #endif
  7683. /*
  7684. ** Constants for the largest and smallest possible 64-bit signed integers.
  7685. ** These macros are designed to work correctly on both 32-bit and 64-bit
  7686. ** compilers.
  7687. */
  7688. #define LARGEST_INT64 (0xffffffff|(((i64)0x7fffffff)<<32))
  7689. #define SMALLEST_INT64 (((i64)-1) - LARGEST_INT64)
  7690. /*
  7691. ** Round up a number to the next larger multiple of 8. This is used
  7692. ** to force 8-byte alignment on 64-bit architectures.
  7693. */
  7694. #define ROUND8(x) (((x)+7)&~7)
  7695. /*
  7696. ** Round down to the nearest multiple of 8
  7697. */
  7698. #define ROUNDDOWN8(x) ((x)&~7)
  7699. /*
  7700. ** Assert that the pointer X is aligned to an 8-byte boundary. This
  7701. ** macro is used only within assert() to verify that the code gets
  7702. ** all alignment restrictions correct.
  7703. **
  7704. ** Except, if SQLITE_4_BYTE_ALIGNED_MALLOC is defined, then the
  7705. ** underlying malloc() implemention might return us 4-byte aligned
  7706. ** pointers. In that case, only verify 4-byte alignment.
  7707. */
  7708. #ifdef SQLITE_4_BYTE_ALIGNED_MALLOC
  7709. # define EIGHT_BYTE_ALIGNMENT(X) ((((char*)(X) - (char*)0)&3)==0)
  7710. #else
  7711. # define EIGHT_BYTE_ALIGNMENT(X) ((((char*)(X) - (char*)0)&7)==0)
  7712. #endif
  7713. /*
  7714. ** An instance of the following structure is used to store the busy-handler
  7715. ** callback for a given sqlite handle.
  7716. **
  7717. ** The sqlite.busyHandler member of the sqlite struct contains the busy
  7718. ** callback for the database handle. Each pager opened via the sqlite
  7719. ** handle is passed a pointer to sqlite.busyHandler. The busy-handler
  7720. ** callback is currently invoked only from within pager.c.
  7721. */
  7722. typedef struct BusyHandler BusyHandler;
  7723. struct BusyHandler {
  7724. int (*xFunc)(void *,int); /* The busy callback */
  7725. void *pArg; /* First arg to busy callback */
  7726. int nBusy; /* Incremented with each busy call */
  7727. };
  7728. /*
  7729. ** Name of the master database table. The master database table
  7730. ** is a special table that holds the names and attributes of all
  7731. ** user tables and indices.
  7732. */
  7733. #define MASTER_NAME "sqlite_master"
  7734. #define TEMP_MASTER_NAME "sqlite_temp_master"
  7735. /*
  7736. ** The root-page of the master database table.
  7737. */
  7738. #define MASTER_ROOT 1
  7739. /*
  7740. ** The name of the schema table.
  7741. */
  7742. #define SCHEMA_TABLE(x) ((!OMIT_TEMPDB)&&(x==1)?TEMP_MASTER_NAME:MASTER_NAME)
  7743. /*
  7744. ** A convenience macro that returns the number of elements in
  7745. ** an array.
  7746. */
  7747. #define ArraySize(X) ((int)(sizeof(X)/sizeof(X[0])))
  7748. /*
  7749. ** The following value as a destructor means to use sqlite3DbFree().
  7750. ** This is an internal extension to SQLITE_STATIC and SQLITE_TRANSIENT.
  7751. */
  7752. #define SQLITE_DYNAMIC ((sqlite3_destructor_type)sqlite3DbFree)
  7753. /*
  7754. ** When SQLITE_OMIT_WSD is defined, it means that the target platform does
  7755. ** not support Writable Static Data (WSD) such as global and static variables.
  7756. ** All variables must either be on the stack or dynamically allocated from
  7757. ** the heap. When WSD is unsupported, the variable declarations scattered
  7758. ** throughout the SQLite code must become constants instead. The SQLITE_WSD
  7759. ** macro is used for this purpose. And instead of referencing the variable
  7760. ** directly, we use its constant as a key to lookup the run-time allocated
  7761. ** buffer that holds real variable. The constant is also the initializer
  7762. ** for the run-time allocated buffer.
  7763. **
  7764. ** In the usual case where WSD is supported, the SQLITE_WSD and GLOBAL
  7765. ** macros become no-ops and have zero performance impact.
  7766. */
  7767. #ifdef SQLITE_OMIT_WSD
  7768. #define SQLITE_WSD const
  7769. #define GLOBAL(t,v) (*(t*)sqlite3_wsd_find((void*)&(v), sizeof(v)))
  7770. #define sqlite3GlobalConfig GLOBAL(struct Sqlite3Config, sqlite3Config)
  7771. SQLITE_API int sqlite3_wsd_init(int N, int J);
  7772. SQLITE_API void *sqlite3_wsd_find(void *K, int L);
  7773. #else
  7774. #define SQLITE_WSD
  7775. #define GLOBAL(t,v) v
  7776. #define sqlite3GlobalConfig sqlite3Config
  7777. #endif
  7778. /*
  7779. ** The following macros are used to suppress compiler warnings and to
  7780. ** make it clear to human readers when a function parameter is deliberately
  7781. ** left unused within the body of a function. This usually happens when
  7782. ** a function is called via a function pointer. For example the
  7783. ** implementation of an SQL aggregate step callback may not use the
  7784. ** parameter indicating the number of arguments passed to the aggregate,
  7785. ** if it knows that this is enforced elsewhere.
  7786. **
  7787. ** When a function parameter is not used at all within the body of a function,
  7788. ** it is generally named "NotUsed" or "NotUsed2" to make things even clearer.
  7789. ** However, these macros may also be used to suppress warnings related to
  7790. ** parameters that may or may not be used depending on compilation options.
  7791. ** For example those parameters only used in assert() statements. In these
  7792. ** cases the parameters are named as per the usual conventions.
  7793. */
  7794. #define UNUSED_PARAMETER(x) (void)(x)
  7795. #define UNUSED_PARAMETER2(x,y) UNUSED_PARAMETER(x),UNUSED_PARAMETER(y)
  7796. /*
  7797. ** Forward references to structures
  7798. */
  7799. typedef struct AggInfo AggInfo;
  7800. typedef struct AuthContext AuthContext;
  7801. typedef struct AutoincInfo AutoincInfo;
  7802. typedef struct Bitvec Bitvec;
  7803. typedef struct CollSeq CollSeq;
  7804. typedef struct Column Column;
  7805. typedef struct Db Db;
  7806. typedef struct Schema Schema;
  7807. typedef struct Expr Expr;
  7808. typedef struct ExprList ExprList;
  7809. typedef struct ExprSpan ExprSpan;
  7810. typedef struct FKey FKey;
  7811. typedef struct FuncDestructor FuncDestructor;
  7812. typedef struct FuncDef FuncDef;
  7813. typedef struct FuncDefHash FuncDefHash;
  7814. typedef struct IdList IdList;
  7815. typedef struct Index Index;
  7816. typedef struct IndexSample IndexSample;
  7817. typedef struct KeyClass KeyClass;
  7818. typedef struct KeyInfo KeyInfo;
  7819. typedef struct Lookaside Lookaside;
  7820. typedef struct LookasideSlot LookasideSlot;
  7821. typedef struct Module Module;
  7822. typedef struct NameContext NameContext;
  7823. typedef struct Parse Parse;
  7824. typedef struct RowSet RowSet;
  7825. typedef struct Savepoint Savepoint;
  7826. typedef struct Select Select;
  7827. typedef struct SrcList SrcList;
  7828. typedef struct StrAccum StrAccum;
  7829. typedef struct Table Table;
  7830. typedef struct TableLock TableLock;
  7831. typedef struct Token Token;
  7832. typedef struct Trigger Trigger;
  7833. typedef struct TriggerPrg TriggerPrg;
  7834. typedef struct TriggerStep TriggerStep;
  7835. typedef struct UnpackedRecord UnpackedRecord;
  7836. typedef struct VTable VTable;
  7837. typedef struct VtabCtx VtabCtx;
  7838. typedef struct Walker Walker;
  7839. typedef struct WherePlan WherePlan;
  7840. typedef struct WhereInfo WhereInfo;
  7841. typedef struct WhereLevel WhereLevel;
  7842. /*
  7843. ** Defer sourcing vdbe.h and btree.h until after the "u8" and
  7844. ** "BusyHandler" typedefs. vdbe.h also requires a few of the opaque
  7845. ** pointer types (i.e. FuncDef) defined above.
  7846. */
  7847. /************** Include btree.h in the middle of sqliteInt.h *****************/
  7848. /************** Begin file btree.h *******************************************/
  7849. /*
  7850. ** 2001 September 15
  7851. **
  7852. ** The author disclaims copyright to this source code. In place of
  7853. ** a legal notice, here is a blessing:
  7854. **
  7855. ** May you do good and not evil.
  7856. ** May you find forgiveness for yourself and forgive others.
  7857. ** May you share freely, never taking more than you give.
  7858. **
  7859. *************************************************************************
  7860. ** This header file defines the interface that the sqlite B-Tree file
  7861. ** subsystem. See comments in the source code for a detailed description
  7862. ** of what each interface routine does.
  7863. */
  7864. #ifndef _BTREE_H_
  7865. #define _BTREE_H_
  7866. /* TODO: This definition is just included so other modules compile. It
  7867. ** needs to be revisited.
  7868. */
  7869. #define SQLITE_N_BTREE_META 10
  7870. /*
  7871. ** If defined as non-zero, auto-vacuum is enabled by default. Otherwise
  7872. ** it must be turned on for each database using "PRAGMA auto_vacuum = 1".
  7873. */
  7874. #ifndef SQLITE_DEFAULT_AUTOVACUUM
  7875. #define SQLITE_DEFAULT_AUTOVACUUM 0
  7876. #endif
  7877. #define BTREE_AUTOVACUUM_NONE 0 /* Do not do auto-vacuum */
  7878. #define BTREE_AUTOVACUUM_FULL 1 /* Do full auto-vacuum */
  7879. #define BTREE_AUTOVACUUM_INCR 2 /* Incremental vacuum */
  7880. /*
  7881. ** Forward declarations of structure
  7882. */
  7883. typedef struct Btree Btree;
  7884. typedef struct BtCursor BtCursor;
  7885. typedef struct BtShared BtShared;
  7886. SQLITE_PRIVATE int sqlite3BtreeOpen(
  7887. sqlite3_vfs *pVfs, /* VFS to use with this b-tree */
  7888. const char *zFilename, /* Name of database file to open */
  7889. sqlite3 *db, /* Associated database connection */
  7890. Btree **ppBtree, /* Return open Btree* here */
  7891. int flags, /* Flags */
  7892. int vfsFlags /* Flags passed through to VFS open */
  7893. );
  7894. /* The flags parameter to sqlite3BtreeOpen can be the bitwise or of the
  7895. ** following values.
  7896. **
  7897. ** NOTE: These values must match the corresponding PAGER_ values in
  7898. ** pager.h.
  7899. */
  7900. #define BTREE_OMIT_JOURNAL 1 /* Do not create or use a rollback journal */
  7901. #define BTREE_NO_READLOCK 2 /* Omit readlocks on readonly files */
  7902. #define BTREE_MEMORY 4 /* This is an in-memory DB */
  7903. #define BTREE_SINGLE 8 /* The file contains at most 1 b-tree */
  7904. #define BTREE_UNORDERED 16 /* Use of a hash implementation is OK */
  7905. SQLITE_PRIVATE int sqlite3BtreeClose(Btree*);
  7906. SQLITE_PRIVATE int sqlite3BtreeSetCacheSize(Btree*,int);
  7907. SQLITE_PRIVATE int sqlite3BtreeSetSafetyLevel(Btree*,int,int,int);
  7908. SQLITE_PRIVATE int sqlite3BtreeSyncDisabled(Btree*);
  7909. SQLITE_PRIVATE int sqlite3BtreeSetPageSize(Btree *p, int nPagesize, int nReserve, int eFix);
  7910. SQLITE_PRIVATE int sqlite3BtreeGetPageSize(Btree*);
  7911. SQLITE_PRIVATE int sqlite3BtreeMaxPageCount(Btree*,int);
  7912. SQLITE_PRIVATE u32 sqlite3BtreeLastPage(Btree*);
  7913. SQLITE_PRIVATE int sqlite3BtreeSecureDelete(Btree*,int);
  7914. SQLITE_PRIVATE int sqlite3BtreeGetReserve(Btree*);
  7915. SQLITE_PRIVATE int sqlite3BtreeSetAutoVacuum(Btree *, int);
  7916. SQLITE_PRIVATE int sqlite3BtreeGetAutoVacuum(Btree *);
  7917. SQLITE_PRIVATE int sqlite3BtreeBeginTrans(Btree*,int);
  7918. SQLITE_PRIVATE int sqlite3BtreeCommitPhaseOne(Btree*, const char *zMaster);
  7919. SQLITE_PRIVATE int sqlite3BtreeCommitPhaseTwo(Btree*, int);
  7920. SQLITE_PRIVATE int sqlite3BtreeCommit(Btree*);
  7921. SQLITE_PRIVATE int sqlite3BtreeRollback(Btree*);
  7922. SQLITE_PRIVATE int sqlite3BtreeBeginStmt(Btree*,int);
  7923. SQLITE_PRIVATE int sqlite3BtreeCreateTable(Btree*, int*, int flags);
  7924. SQLITE_PRIVATE int sqlite3BtreeIsInTrans(Btree*);
  7925. SQLITE_PRIVATE int sqlite3BtreeIsInReadTrans(Btree*);
  7926. SQLITE_PRIVATE int sqlite3BtreeIsInBackup(Btree*);
  7927. SQLITE_PRIVATE void *sqlite3BtreeSchema(Btree *, int, void(*)(void *));
  7928. SQLITE_PRIVATE int sqlite3BtreeSchemaLocked(Btree *pBtree);
  7929. SQLITE_PRIVATE int sqlite3BtreeLockTable(Btree *pBtree, int iTab, u8 isWriteLock);
  7930. SQLITE_PRIVATE int sqlite3BtreeSavepoint(Btree *, int, int);
  7931. SQLITE_PRIVATE const char *sqlite3BtreeGetFilename(Btree *);
  7932. SQLITE_PRIVATE const char *sqlite3BtreeGetJournalname(Btree *);
  7933. SQLITE_PRIVATE int sqlite3BtreeCopyFile(Btree *, Btree *);
  7934. SQLITE_PRIVATE int sqlite3BtreeIncrVacuum(Btree *);
  7935. /* The flags parameter to sqlite3BtreeCreateTable can be the bitwise OR
  7936. ** of the flags shown below.
  7937. **
  7938. ** Every SQLite table must have either BTREE_INTKEY or BTREE_BLOBKEY set.
  7939. ** With BTREE_INTKEY, the table key is a 64-bit integer and arbitrary data
  7940. ** is stored in the leaves. (BTREE_INTKEY is used for SQL tables.) With
  7941. ** BTREE_BLOBKEY, the key is an arbitrary BLOB and no content is stored
  7942. ** anywhere - the key is the content. (BTREE_BLOBKEY is used for SQL
  7943. ** indices.)
  7944. */
  7945. #define BTREE_INTKEY 1 /* Table has only 64-bit signed integer keys */
  7946. #define BTREE_BLOBKEY 2 /* Table has keys only - no data */
  7947. SQLITE_PRIVATE int sqlite3BtreeDropTable(Btree*, int, int*);
  7948. SQLITE_PRIVATE int sqlite3BtreeClearTable(Btree*, int, int*);
  7949. SQLITE_PRIVATE void sqlite3BtreeTripAllCursors(Btree*, int);
  7950. SQLITE_PRIVATE void sqlite3BtreeGetMeta(Btree *pBtree, int idx, u32 *pValue);
  7951. SQLITE_PRIVATE int sqlite3BtreeUpdateMeta(Btree*, int idx, u32 value);
  7952. /*
  7953. ** The second parameter to sqlite3BtreeGetMeta or sqlite3BtreeUpdateMeta
  7954. ** should be one of the following values. The integer values are assigned
  7955. ** to constants so that the offset of the corresponding field in an
  7956. ** SQLite database header may be found using the following formula:
  7957. **
  7958. ** offset = 36 + (idx * 4)
  7959. **
  7960. ** For example, the free-page-count field is located at byte offset 36 of
  7961. ** the database file header. The incr-vacuum-flag field is located at
  7962. ** byte offset 64 (== 36+4*7).
  7963. */
  7964. #define BTREE_FREE_PAGE_COUNT 0
  7965. #define BTREE_SCHEMA_VERSION 1
  7966. #define BTREE_FILE_FORMAT 2
  7967. #define BTREE_DEFAULT_CACHE_SIZE 3
  7968. #define BTREE_LARGEST_ROOT_PAGE 4
  7969. #define BTREE_TEXT_ENCODING 5
  7970. #define BTREE_USER_VERSION 6
  7971. #define BTREE_INCR_VACUUM 7
  7972. SQLITE_PRIVATE int sqlite3BtreeCursor(
  7973. Btree*, /* BTree containing table to open */
  7974. int iTable, /* Index of root page */
  7975. int wrFlag, /* 1 for writing. 0 for read-only */
  7976. struct KeyInfo*, /* First argument to compare function */
  7977. BtCursor *pCursor /* Space to write cursor structure */
  7978. );
  7979. SQLITE_PRIVATE int sqlite3BtreeCursorSize(void);
  7980. SQLITE_PRIVATE void sqlite3BtreeCursorZero(BtCursor*);
  7981. SQLITE_PRIVATE int sqlite3BtreeCloseCursor(BtCursor*);
  7982. SQLITE_PRIVATE int sqlite3BtreeMovetoUnpacked(
  7983. BtCursor*,
  7984. UnpackedRecord *pUnKey,
  7985. i64 intKey,
  7986. int bias,
  7987. int *pRes
  7988. );
  7989. SQLITE_PRIVATE int sqlite3BtreeCursorHasMoved(BtCursor*, int*);
  7990. SQLITE_PRIVATE int sqlite3BtreeDelete(BtCursor*);
  7991. SQLITE_PRIVATE int sqlite3BtreeInsert(BtCursor*, const void *pKey, i64 nKey,
  7992. const void *pData, int nData,
  7993. int nZero, int bias, int seekResult);
  7994. SQLITE_PRIVATE int sqlite3BtreeFirst(BtCursor*, int *pRes);
  7995. SQLITE_PRIVATE int sqlite3BtreeLast(BtCursor*, int *pRes);
  7996. SQLITE_PRIVATE int sqlite3BtreeNext(BtCursor*, int *pRes);
  7997. SQLITE_PRIVATE int sqlite3BtreeEof(BtCursor*);
  7998. SQLITE_PRIVATE int sqlite3BtreePrevious(BtCursor*, int *pRes);
  7999. SQLITE_PRIVATE int sqlite3BtreeKeySize(BtCursor*, i64 *pSize);
  8000. SQLITE_PRIVATE int sqlite3BtreeKey(BtCursor*, u32 offset, u32 amt, void*);
  8001. SQLITE_PRIVATE const void *sqlite3BtreeKeyFetch(BtCursor*, int *pAmt);
  8002. SQLITE_PRIVATE const void *sqlite3BtreeDataFetch(BtCursor*, int *pAmt);
  8003. SQLITE_PRIVATE int sqlite3BtreeDataSize(BtCursor*, u32 *pSize);
  8004. SQLITE_PRIVATE int sqlite3BtreeData(BtCursor*, u32 offset, u32 amt, void*);
  8005. SQLITE_PRIVATE void sqlite3BtreeSetCachedRowid(BtCursor*, sqlite3_int64);
  8006. SQLITE_PRIVATE sqlite3_int64 sqlite3BtreeGetCachedRowid(BtCursor*);
  8007. SQLITE_PRIVATE char *sqlite3BtreeIntegrityCheck(Btree*, int *aRoot, int nRoot, int, int*);
  8008. SQLITE_PRIVATE struct Pager *sqlite3BtreePager(Btree*);
  8009. SQLITE_PRIVATE int sqlite3BtreePutData(BtCursor*, u32 offset, u32 amt, void*);
  8010. SQLITE_PRIVATE void sqlite3BtreeCacheOverflow(BtCursor *);
  8011. SQLITE_PRIVATE void sqlite3BtreeClearCursor(BtCursor *);
  8012. SQLITE_PRIVATE int sqlite3BtreeSetVersion(Btree *pBt, int iVersion);
  8013. #ifndef NDEBUG
  8014. SQLITE_PRIVATE int sqlite3BtreeCursorIsValid(BtCursor*);
  8015. #endif
  8016. #ifndef SQLITE_OMIT_BTREECOUNT
  8017. SQLITE_PRIVATE int sqlite3BtreeCount(BtCursor *, i64 *);
  8018. #endif
  8019. #ifdef SQLITE_TEST
  8020. SQLITE_PRIVATE int sqlite3BtreeCursorInfo(BtCursor*, int*, int);
  8021. SQLITE_PRIVATE void sqlite3BtreeCursorList(Btree*);
  8022. #endif
  8023. #ifndef SQLITE_OMIT_WAL
  8024. SQLITE_PRIVATE int sqlite3BtreeCheckpoint(Btree*, int, int *, int *);
  8025. #endif
  8026. /*
  8027. ** If we are not using shared cache, then there is no need to
  8028. ** use mutexes to access the BtShared structures. So make the
  8029. ** Enter and Leave procedures no-ops.
  8030. */
  8031. #ifndef SQLITE_OMIT_SHARED_CACHE
  8032. SQLITE_PRIVATE void sqlite3BtreeEnter(Btree*);
  8033. SQLITE_PRIVATE void sqlite3BtreeEnterAll(sqlite3*);
  8034. #else
  8035. # define sqlite3BtreeEnter(X)
  8036. # define sqlite3BtreeEnterAll(X)
  8037. #endif
  8038. #if !defined(SQLITE_OMIT_SHARED_CACHE) && SQLITE_THREADSAFE
  8039. SQLITE_PRIVATE int sqlite3BtreeSharable(Btree*);
  8040. SQLITE_PRIVATE void sqlite3BtreeLeave(Btree*);
  8041. SQLITE_PRIVATE void sqlite3BtreeEnterCursor(BtCursor*);
  8042. SQLITE_PRIVATE void sqlite3BtreeLeaveCursor(BtCursor*);
  8043. SQLITE_PRIVATE void sqlite3BtreeLeaveAll(sqlite3*);
  8044. #ifndef NDEBUG
  8045. /* These routines are used inside assert() statements only. */
  8046. SQLITE_PRIVATE int sqlite3BtreeHoldsMutex(Btree*);
  8047. SQLITE_PRIVATE int sqlite3BtreeHoldsAllMutexes(sqlite3*);
  8048. SQLITE_PRIVATE int sqlite3SchemaMutexHeld(sqlite3*,int,Schema*);
  8049. #endif
  8050. #else
  8051. # define sqlite3BtreeSharable(X) 0
  8052. # define sqlite3BtreeLeave(X)
  8053. # define sqlite3BtreeEnterCursor(X)
  8054. # define sqlite3BtreeLeaveCursor(X)
  8055. # define sqlite3BtreeLeaveAll(X)
  8056. # define sqlite3BtreeHoldsMutex(X) 1
  8057. # define sqlite3BtreeHoldsAllMutexes(X) 1
  8058. # define sqlite3SchemaMutexHeld(X,Y,Z) 1
  8059. #endif
  8060. #endif /* _BTREE_H_ */
  8061. /************** End of btree.h ***********************************************/
  8062. /************** Continuing where we left off in sqliteInt.h ******************/
  8063. /************** Include vdbe.h in the middle of sqliteInt.h ******************/
  8064. /************** Begin file vdbe.h ********************************************/
  8065. /*
  8066. ** 2001 September 15
  8067. **
  8068. ** The author disclaims copyright to this source code. In place of
  8069. ** a legal notice, here is a blessing:
  8070. **
  8071. ** May you do good and not evil.
  8072. ** May you find forgiveness for yourself and forgive others.
  8073. ** May you share freely, never taking more than you give.
  8074. **
  8075. *************************************************************************
  8076. ** Header file for the Virtual DataBase Engine (VDBE)
  8077. **
  8078. ** This header defines the interface to the virtual database engine
  8079. ** or VDBE. The VDBE implements an abstract machine that runs a
  8080. ** simple program to access and modify the underlying database.
  8081. */
  8082. #ifndef _SQLITE_VDBE_H_
  8083. #define _SQLITE_VDBE_H_
  8084. /* #include <stdio.h> */
  8085. /*
  8086. ** A single VDBE is an opaque structure named "Vdbe". Only routines
  8087. ** in the source file sqliteVdbe.c are allowed to see the insides
  8088. ** of this structure.
  8089. */
  8090. typedef struct Vdbe Vdbe;
  8091. /*
  8092. ** The names of the following types declared in vdbeInt.h are required
  8093. ** for the VdbeOp definition.
  8094. */
  8095. typedef struct VdbeFunc VdbeFunc;
  8096. typedef struct Mem Mem;
  8097. typedef struct SubProgram SubProgram;
  8098. /*
  8099. ** A single instruction of the virtual machine has an opcode
  8100. ** and as many as three operands. The instruction is recorded
  8101. ** as an instance of the following structure:
  8102. */
  8103. struct VdbeOp {
  8104. u8 opcode; /* What operation to perform */
  8105. signed char p4type; /* One of the P4_xxx constants for p4 */
  8106. u8 opflags; /* Mask of the OPFLG_* flags in opcodes.h */
  8107. u8 p5; /* Fifth parameter is an unsigned character */
  8108. int p1; /* First operand */
  8109. int p2; /* Second parameter (often the jump destination) */
  8110. int p3; /* The third parameter */
  8111. union { /* fourth parameter */
  8112. int i; /* Integer value if p4type==P4_INT32 */
  8113. void *p; /* Generic pointer */
  8114. char *z; /* Pointer to data for string (char array) types */
  8115. i64 *pI64; /* Used when p4type is P4_INT64 */
  8116. double *pReal; /* Used when p4type is P4_REAL */
  8117. FuncDef *pFunc; /* Used when p4type is P4_FUNCDEF */
  8118. VdbeFunc *pVdbeFunc; /* Used when p4type is P4_VDBEFUNC */
  8119. CollSeq *pColl; /* Used when p4type is P4_COLLSEQ */
  8120. Mem *pMem; /* Used when p4type is P4_MEM */
  8121. VTable *pVtab; /* Used when p4type is P4_VTAB */
  8122. KeyInfo *pKeyInfo; /* Used when p4type is P4_KEYINFO */
  8123. int *ai; /* Used when p4type is P4_INTARRAY */
  8124. SubProgram *pProgram; /* Used when p4type is P4_SUBPROGRAM */
  8125. int (*xAdvance)(BtCursor *, int *);
  8126. } p4;
  8127. #ifdef SQLITE_DEBUG
  8128. char *zComment; /* Comment to improve readability */
  8129. #endif
  8130. #ifdef VDBE_PROFILE
  8131. int cnt; /* Number of times this instruction was executed */
  8132. u64 cycles; /* Total time spent executing this instruction */
  8133. #endif
  8134. };
  8135. typedef struct VdbeOp VdbeOp;
  8136. /*
  8137. ** A sub-routine used to implement a trigger program.
  8138. */
  8139. struct SubProgram {
  8140. VdbeOp *aOp; /* Array of opcodes for sub-program */
  8141. int nOp; /* Elements in aOp[] */
  8142. int nMem; /* Number of memory cells required */
  8143. int nCsr; /* Number of cursors required */
  8144. int nOnce; /* Number of OP_Once instructions */
  8145. void *token; /* id that may be used to recursive triggers */
  8146. SubProgram *pNext; /* Next sub-program already visited */
  8147. };
  8148. /*
  8149. ** A smaller version of VdbeOp used for the VdbeAddOpList() function because
  8150. ** it takes up less space.
  8151. */
  8152. struct VdbeOpList {
  8153. u8 opcode; /* What operation to perform */
  8154. signed char p1; /* First operand */
  8155. signed char p2; /* Second parameter (often the jump destination) */
  8156. signed char p3; /* Third parameter */
  8157. };
  8158. typedef struct VdbeOpList VdbeOpList;
  8159. /*
  8160. ** Allowed values of VdbeOp.p4type
  8161. */
  8162. #define P4_NOTUSED 0 /* The P4 parameter is not used */
  8163. #define P4_DYNAMIC (-1) /* Pointer to a string obtained from sqliteMalloc() */
  8164. #define P4_STATIC (-2) /* Pointer to a static string */
  8165. #define P4_COLLSEQ (-4) /* P4 is a pointer to a CollSeq structure */
  8166. #define P4_FUNCDEF (-5) /* P4 is a pointer to a FuncDef structure */
  8167. #define P4_KEYINFO (-6) /* P4 is a pointer to a KeyInfo structure */
  8168. #define P4_VDBEFUNC (-7) /* P4 is a pointer to a VdbeFunc structure */
  8169. #define P4_MEM (-8) /* P4 is a pointer to a Mem* structure */
  8170. #define P4_TRANSIENT 0 /* P4 is a pointer to a transient string */
  8171. #define P4_VTAB (-10) /* P4 is a pointer to an sqlite3_vtab structure */
  8172. #define P4_MPRINTF (-11) /* P4 is a string obtained from sqlite3_mprintf() */
  8173. #define P4_REAL (-12) /* P4 is a 64-bit floating point value */
  8174. #define P4_INT64 (-13) /* P4 is a 64-bit signed integer */
  8175. #define P4_INT32 (-14) /* P4 is a 32-bit signed integer */
  8176. #define P4_INTARRAY (-15) /* P4 is a vector of 32-bit integers */
  8177. #define P4_SUBPROGRAM (-18) /* P4 is a pointer to a SubProgram structure */
  8178. #define P4_ADVANCE (-19) /* P4 is a pointer to BtreeNext() or BtreePrev() */
  8179. /* When adding a P4 argument using P4_KEYINFO, a copy of the KeyInfo structure
  8180. ** is made. That copy is freed when the Vdbe is finalized. But if the
  8181. ** argument is P4_KEYINFO_HANDOFF, the passed in pointer is used. It still
  8182. ** gets freed when the Vdbe is finalized so it still should be obtained
  8183. ** from a single sqliteMalloc(). But no copy is made and the calling
  8184. ** function should *not* try to free the KeyInfo.
  8185. */
  8186. #define P4_KEYINFO_HANDOFF (-16)
  8187. #define P4_KEYINFO_STATIC (-17)
  8188. /*
  8189. ** The Vdbe.aColName array contains 5n Mem structures, where n is the
  8190. ** number of columns of data returned by the statement.
  8191. */
  8192. #define COLNAME_NAME 0
  8193. #define COLNAME_DECLTYPE 1
  8194. #define COLNAME_DATABASE 2
  8195. #define COLNAME_TABLE 3
  8196. #define COLNAME_COLUMN 4
  8197. #ifdef SQLITE_ENABLE_COLUMN_METADATA
  8198. # define COLNAME_N 5 /* Number of COLNAME_xxx symbols */
  8199. #else
  8200. # ifdef SQLITE_OMIT_DECLTYPE
  8201. # define COLNAME_N 1 /* Store only the name */
  8202. # else
  8203. # define COLNAME_N 2 /* Store the name and decltype */
  8204. # endif
  8205. #endif
  8206. /*
  8207. ** The following macro converts a relative address in the p2 field
  8208. ** of a VdbeOp structure into a negative number so that
  8209. ** sqlite3VdbeAddOpList() knows that the address is relative. Calling
  8210. ** the macro again restores the address.
  8211. */
  8212. #define ADDR(X) (-1-(X))
  8213. /*
  8214. ** The makefile scans the vdbe.c source file and creates the "opcodes.h"
  8215. ** header file that defines a number for each opcode used by the VDBE.
  8216. */
  8217. /************** Include opcodes.h in the middle of vdbe.h ********************/
  8218. /************** Begin file opcodes.h *****************************************/
  8219. /* Automatically generated. Do not edit */
  8220. /* See the mkopcodeh.awk script for details */
  8221. #define OP_Goto 1
  8222. #define OP_Gosub 2
  8223. #define OP_Return 3
  8224. #define OP_Yield 4
  8225. #define OP_HaltIfNull 5
  8226. #define OP_Halt 6
  8227. #define OP_Integer 7
  8228. #define OP_Int64 8
  8229. #define OP_Real 130 /* same as TK_FLOAT */
  8230. #define OP_String8 94 /* same as TK_STRING */
  8231. #define OP_String 9
  8232. #define OP_Null 10
  8233. #define OP_Blob 11
  8234. #define OP_Variable 12
  8235. #define OP_Move 13
  8236. #define OP_Copy 14
  8237. #define OP_SCopy 15
  8238. #define OP_ResultRow 16
  8239. #define OP_Concat 91 /* same as TK_CONCAT */
  8240. #define OP_Add 86 /* same as TK_PLUS */
  8241. #define OP_Subtract 87 /* same as TK_MINUS */
  8242. #define OP_Multiply 88 /* same as TK_STAR */
  8243. #define OP_Divide 89 /* same as TK_SLASH */
  8244. #define OP_Remainder 90 /* same as TK_REM */
  8245. #define OP_CollSeq 17
  8246. #define OP_Function 18
  8247. #define OP_BitAnd 82 /* same as TK_BITAND */
  8248. #define OP_BitOr 83 /* same as TK_BITOR */
  8249. #define OP_ShiftLeft 84 /* same as TK_LSHIFT */
  8250. #define OP_ShiftRight 85 /* same as TK_RSHIFT */
  8251. #define OP_AddImm 20
  8252. #define OP_MustBeInt 21
  8253. #define OP_RealAffinity 22
  8254. #define OP_ToText 141 /* same as TK_TO_TEXT */
  8255. #define OP_ToBlob 142 /* same as TK_TO_BLOB */
  8256. #define OP_ToNumeric 143 /* same as TK_TO_NUMERIC*/
  8257. #define OP_ToInt 144 /* same as TK_TO_INT */
  8258. #define OP_ToReal 145 /* same as TK_TO_REAL */
  8259. #define OP_Eq 76 /* same as TK_EQ */
  8260. #define OP_Ne 75 /* same as TK_NE */
  8261. #define OP_Lt 79 /* same as TK_LT */
  8262. #define OP_Le 78 /* same as TK_LE */
  8263. #define OP_Gt 77 /* same as TK_GT */
  8264. #define OP_Ge 80 /* same as TK_GE */
  8265. #define OP_Permutation 23
  8266. #define OP_Compare 24
  8267. #define OP_Jump 25
  8268. #define OP_And 69 /* same as TK_AND */
  8269. #define OP_Or 68 /* same as TK_OR */
  8270. #define OP_Not 19 /* same as TK_NOT */
  8271. #define OP_BitNot 93 /* same as TK_BITNOT */
  8272. #define OP_Once 26
  8273. #define OP_If 27
  8274. #define OP_IfNot 28
  8275. #define OP_IsNull 73 /* same as TK_ISNULL */
  8276. #define OP_NotNull 74 /* same as TK_NOTNULL */
  8277. #define OP_Column 29
  8278. #define OP_Affinity 30
  8279. #define OP_MakeRecord 31
  8280. #define OP_Count 32
  8281. #define OP_Savepoint 33
  8282. #define OP_AutoCommit 34
  8283. #define OP_Transaction 35
  8284. #define OP_ReadCookie 36
  8285. #define OP_SetCookie 37
  8286. #define OP_VerifyCookie 38
  8287. #define OP_OpenRead 39
  8288. #define OP_OpenWrite 40
  8289. #define OP_OpenAutoindex 41
  8290. #define OP_OpenEphemeral 42
  8291. #define OP_SorterOpen 43
  8292. #define OP_OpenPseudo 44
  8293. #define OP_Close 45
  8294. #define OP_SeekLt 46
  8295. #define OP_SeekLe 47
  8296. #define OP_SeekGe 48
  8297. #define OP_SeekGt 49
  8298. #define OP_Seek 50
  8299. #define OP_NotFound 51
  8300. #define OP_Found 52
  8301. #define OP_IsUnique 53
  8302. #define OP_NotExists 54
  8303. #define OP_Sequence 55
  8304. #define OP_NewRowid 56
  8305. #define OP_Insert 57
  8306. #define OP_InsertInt 58
  8307. #define OP_Delete 59
  8308. #define OP_ResetCount 60
  8309. #define OP_SorterCompare 61
  8310. #define OP_SorterData 62
  8311. #define OP_RowKey 63
  8312. #define OP_RowData 64
  8313. #define OP_Rowid 65
  8314. #define OP_NullRow 66
  8315. #define OP_Last 67
  8316. #define OP_SorterSort 70
  8317. #define OP_Sort 71
  8318. #define OP_Rewind 72
  8319. #define OP_SorterNext 81
  8320. #define OP_Prev 92
  8321. #define OP_Next 95
  8322. #define OP_SorterInsert 96
  8323. #define OP_IdxInsert 97
  8324. #define OP_IdxDelete 98
  8325. #define OP_IdxRowid 99
  8326. #define OP_IdxLT 100
  8327. #define OP_IdxGE 101
  8328. #define OP_Destroy 102
  8329. #define OP_Clear 103
  8330. #define OP_CreateIndex 104
  8331. #define OP_CreateTable 105
  8332. #define OP_ParseSchema 106
  8333. #define OP_LoadAnalysis 107
  8334. #define OP_DropTable 108
  8335. #define OP_DropIndex 109
  8336. #define OP_DropTrigger 110
  8337. #define OP_IntegrityCk 111
  8338. #define OP_RowSetAdd 112
  8339. #define OP_RowSetRead 113
  8340. #define OP_RowSetTest 114
  8341. #define OP_Program 115
  8342. #define OP_Param 116
  8343. #define OP_FkCounter 117
  8344. #define OP_FkIfZero 118
  8345. #define OP_MemMax 119
  8346. #define OP_IfPos 120
  8347. #define OP_IfNeg 121
  8348. #define OP_IfZero 122
  8349. #define OP_AggStep 123
  8350. #define OP_AggFinal 124
  8351. #define OP_Checkpoint 125
  8352. #define OP_JournalMode 126
  8353. #define OP_Vacuum 127
  8354. #define OP_IncrVacuum 128
  8355. #define OP_Expire 129
  8356. #define OP_TableLock 131
  8357. #define OP_VBegin 132
  8358. #define OP_VCreate 133
  8359. #define OP_VDestroy 134
  8360. #define OP_VOpen 135
  8361. #define OP_VFilter 136
  8362. #define OP_VColumn 137
  8363. #define OP_VNext 138
  8364. #define OP_VRename 139
  8365. #define OP_VUpdate 140
  8366. #define OP_Pagecount 146
  8367. #define OP_MaxPgcnt 147
  8368. #define OP_Trace 148
  8369. #define OP_Noop 149
  8370. #define OP_Explain 150
  8371. /* Properties such as "out2" or "jump" that are specified in
  8372. ** comments following the "case" for each opcode in the vdbe.c
  8373. ** are encoded into bitvectors as follows:
  8374. */
  8375. #define OPFLG_JUMP 0x0001 /* jump: P2 holds jmp target */
  8376. #define OPFLG_OUT2_PRERELEASE 0x0002 /* out2-prerelease: */
  8377. #define OPFLG_IN1 0x0004 /* in1: P1 is an input */
  8378. #define OPFLG_IN2 0x0008 /* in2: P2 is an input */
  8379. #define OPFLG_IN3 0x0010 /* in3: P3 is an input */
  8380. #define OPFLG_OUT2 0x0020 /* out2: P2 is an output */
  8381. #define OPFLG_OUT3 0x0040 /* out3: P3 is an output */
  8382. #define OPFLG_INITIALIZER {\
  8383. /* 0 */ 0x00, 0x01, 0x01, 0x04, 0x04, 0x10, 0x00, 0x02,\
  8384. /* 8 */ 0x02, 0x02, 0x02, 0x02, 0x02, 0x00, 0x24, 0x24,\
  8385. /* 16 */ 0x00, 0x00, 0x00, 0x24, 0x04, 0x05, 0x04, 0x00,\
  8386. /* 24 */ 0x00, 0x01, 0x01, 0x05, 0x05, 0x00, 0x00, 0x00,\
  8387. /* 32 */ 0x02, 0x00, 0x00, 0x00, 0x02, 0x10, 0x00, 0x00,\
  8388. /* 40 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x11, 0x11,\
  8389. /* 48 */ 0x11, 0x11, 0x08, 0x11, 0x11, 0x11, 0x11, 0x02,\
  8390. /* 56 */ 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,\
  8391. /* 64 */ 0x00, 0x02, 0x00, 0x01, 0x4c, 0x4c, 0x01, 0x01,\
  8392. /* 72 */ 0x01, 0x05, 0x05, 0x15, 0x15, 0x15, 0x15, 0x15,\
  8393. /* 80 */ 0x15, 0x01, 0x4c, 0x4c, 0x4c, 0x4c, 0x4c, 0x4c,\
  8394. /* 88 */ 0x4c, 0x4c, 0x4c, 0x4c, 0x01, 0x24, 0x02, 0x01,\
  8395. /* 96 */ 0x08, 0x08, 0x00, 0x02, 0x01, 0x01, 0x02, 0x00,\
  8396. /* 104 */ 0x02, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,\
  8397. /* 112 */ 0x0c, 0x45, 0x15, 0x01, 0x02, 0x00, 0x01, 0x08,\
  8398. /* 120 */ 0x05, 0x05, 0x05, 0x00, 0x00, 0x00, 0x02, 0x00,\
  8399. /* 128 */ 0x01, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00,\
  8400. /* 136 */ 0x01, 0x00, 0x01, 0x00, 0x00, 0x04, 0x04, 0x04,\
  8401. /* 144 */ 0x04, 0x04, 0x02, 0x02, 0x00, 0x00, 0x00,}
  8402. /************** End of opcodes.h *********************************************/
  8403. /************** Continuing where we left off in vdbe.h ***********************/
  8404. /*
  8405. ** Prototypes for the VDBE interface. See comments on the implementation
  8406. ** for a description of what each of these routines does.
  8407. */
  8408. SQLITE_PRIVATE Vdbe *sqlite3VdbeCreate(sqlite3*);
  8409. SQLITE_PRIVATE int sqlite3VdbeAddOp0(Vdbe*,int);
  8410. SQLITE_PRIVATE int sqlite3VdbeAddOp1(Vdbe*,int,int);
  8411. SQLITE_PRIVATE int sqlite3VdbeAddOp2(Vdbe*,int,int,int);
  8412. SQLITE_PRIVATE int sqlite3VdbeAddOp3(Vdbe*,int,int,int,int);
  8413. SQLITE_PRIVATE int sqlite3VdbeAddOp4(Vdbe*,int,int,int,int,const char *zP4,int);
  8414. SQLITE_PRIVATE int sqlite3VdbeAddOp4Int(Vdbe*,int,int,int,int,int);
  8415. SQLITE_PRIVATE int sqlite3VdbeAddOpList(Vdbe*, int nOp, VdbeOpList const *aOp);
  8416. SQLITE_PRIVATE void sqlite3VdbeAddParseSchemaOp(Vdbe*,int,char*);
  8417. SQLITE_PRIVATE void sqlite3VdbeChangeP1(Vdbe*, u32 addr, int P1);
  8418. SQLITE_PRIVATE void sqlite3VdbeChangeP2(Vdbe*, u32 addr, int P2);
  8419. SQLITE_PRIVATE void sqlite3VdbeChangeP3(Vdbe*, u32 addr, int P3);
  8420. SQLITE_PRIVATE void sqlite3VdbeChangeP5(Vdbe*, u8 P5);
  8421. SQLITE_PRIVATE void sqlite3VdbeJumpHere(Vdbe*, int addr);
  8422. SQLITE_PRIVATE void sqlite3VdbeChangeToNoop(Vdbe*, int addr);
  8423. SQLITE_PRIVATE void sqlite3VdbeChangeP4(Vdbe*, int addr, const char *zP4, int N);
  8424. SQLITE_PRIVATE void sqlite3VdbeUsesBtree(Vdbe*, int);
  8425. SQLITE_PRIVATE VdbeOp *sqlite3VdbeGetOp(Vdbe*, int);
  8426. SQLITE_PRIVATE int sqlite3VdbeMakeLabel(Vdbe*);
  8427. SQLITE_PRIVATE void sqlite3VdbeRunOnlyOnce(Vdbe*);
  8428. SQLITE_PRIVATE void sqlite3VdbeDelete(Vdbe*);
  8429. SQLITE_PRIVATE void sqlite3VdbeDeleteObject(sqlite3*,Vdbe*);
  8430. SQLITE_PRIVATE void sqlite3VdbeMakeReady(Vdbe*,Parse*);
  8431. SQLITE_PRIVATE int sqlite3VdbeFinalize(Vdbe*);
  8432. SQLITE_PRIVATE void sqlite3VdbeResolveLabel(Vdbe*, int);
  8433. SQLITE_PRIVATE int sqlite3VdbeCurrentAddr(Vdbe*);
  8434. #ifdef SQLITE_DEBUG
  8435. SQLITE_PRIVATE int sqlite3VdbeAssertMayAbort(Vdbe *, int);
  8436. SQLITE_PRIVATE void sqlite3VdbeTrace(Vdbe*,FILE*);
  8437. #endif
  8438. SQLITE_PRIVATE void sqlite3VdbeResetStepResult(Vdbe*);
  8439. SQLITE_PRIVATE void sqlite3VdbeRewind(Vdbe*);
  8440. SQLITE_PRIVATE int sqlite3VdbeReset(Vdbe*);
  8441. SQLITE_PRIVATE void sqlite3VdbeSetNumCols(Vdbe*,int);
  8442. SQLITE_PRIVATE int sqlite3VdbeSetColName(Vdbe*, int, int, const char *, void(*)(void*));
  8443. SQLITE_PRIVATE void sqlite3VdbeCountChanges(Vdbe*);
  8444. SQLITE_PRIVATE sqlite3 *sqlite3VdbeDb(Vdbe*);
  8445. SQLITE_PRIVATE void sqlite3VdbeSetSql(Vdbe*, const char *z, int n, int);
  8446. SQLITE_PRIVATE void sqlite3VdbeSwap(Vdbe*,Vdbe*);
  8447. SQLITE_PRIVATE VdbeOp *sqlite3VdbeTakeOpArray(Vdbe*, int*, int*);
  8448. SQLITE_PRIVATE sqlite3_value *sqlite3VdbeGetValue(Vdbe*, int, u8);
  8449. SQLITE_PRIVATE void sqlite3VdbeSetVarmask(Vdbe*, int);
  8450. #ifndef SQLITE_OMIT_TRACE
  8451. SQLITE_PRIVATE char *sqlite3VdbeExpandSql(Vdbe*, const char*);
  8452. #endif
  8453. SQLITE_PRIVATE void sqlite3VdbeRecordUnpack(KeyInfo*,int,const void*,UnpackedRecord*);
  8454. SQLITE_PRIVATE int sqlite3VdbeRecordCompare(int,const void*,UnpackedRecord*);
  8455. SQLITE_PRIVATE UnpackedRecord *sqlite3VdbeAllocUnpackedRecord(KeyInfo *, char *, int, char **);
  8456. #ifndef SQLITE_OMIT_TRIGGER
  8457. SQLITE_PRIVATE void sqlite3VdbeLinkSubProgram(Vdbe *, SubProgram *);
  8458. #endif
  8459. #ifndef NDEBUG
  8460. SQLITE_PRIVATE void sqlite3VdbeComment(Vdbe*, const char*, ...);
  8461. # define VdbeComment(X) sqlite3VdbeComment X
  8462. SQLITE_PRIVATE void sqlite3VdbeNoopComment(Vdbe*, const char*, ...);
  8463. # define VdbeNoopComment(X) sqlite3VdbeNoopComment X
  8464. #else
  8465. # define VdbeComment(X)
  8466. # define VdbeNoopComment(X)
  8467. #endif
  8468. #endif
  8469. /************** End of vdbe.h ************************************************/
  8470. /************** Continuing where we left off in sqliteInt.h ******************/
  8471. /************** Include pager.h in the middle of sqliteInt.h *****************/
  8472. /************** Begin file pager.h *******************************************/
  8473. /*
  8474. ** 2001 September 15
  8475. **
  8476. ** The author disclaims copyright to this source code. In place of
  8477. ** a legal notice, here is a blessing:
  8478. **
  8479. ** May you do good and not evil.
  8480. ** May you find forgiveness for yourself and forgive others.
  8481. ** May you share freely, never taking more than you give.
  8482. **
  8483. *************************************************************************
  8484. ** This header file defines the interface that the sqlite page cache
  8485. ** subsystem. The page cache subsystem reads and writes a file a page
  8486. ** at a time and provides a journal for rollback.
  8487. */
  8488. #ifndef _PAGER_H_
  8489. #define _PAGER_H_
  8490. /*
  8491. ** Default maximum size for persistent journal files. A negative
  8492. ** value means no limit. This value may be overridden using the
  8493. ** sqlite3PagerJournalSizeLimit() API. See also "PRAGMA journal_size_limit".
  8494. */
  8495. #ifndef SQLITE_DEFAULT_JOURNAL_SIZE_LIMIT
  8496. #define SQLITE_DEFAULT_JOURNAL_SIZE_LIMIT -1
  8497. #endif
  8498. /*
  8499. ** The type used to represent a page number. The first page in a file
  8500. ** is called page 1. 0 is used to represent "not a page".
  8501. */
  8502. typedef u32 Pgno;
  8503. /*
  8504. ** Each open file is managed by a separate instance of the "Pager" structure.
  8505. */
  8506. typedef struct Pager Pager;
  8507. /*
  8508. ** Handle type for pages.
  8509. */
  8510. typedef struct PgHdr DbPage;
  8511. /*
  8512. ** Page number PAGER_MJ_PGNO is never used in an SQLite database (it is
  8513. ** reserved for working around a windows/posix incompatibility). It is
  8514. ** used in the journal to signify that the remainder of the journal file
  8515. ** is devoted to storing a master journal name - there are no more pages to
  8516. ** roll back. See comments for function writeMasterJournal() in pager.c
  8517. ** for details.
  8518. */
  8519. #define PAGER_MJ_PGNO(x) ((Pgno)((PENDING_BYTE/((x)->pageSize))+1))
  8520. /*
  8521. ** Allowed values for the flags parameter to sqlite3PagerOpen().
  8522. **
  8523. ** NOTE: These values must match the corresponding BTREE_ values in btree.h.
  8524. */
  8525. #define PAGER_OMIT_JOURNAL 0x0001 /* Do not use a rollback journal */
  8526. #define PAGER_NO_READLOCK 0x0002 /* Omit readlocks on readonly files */
  8527. #define PAGER_MEMORY 0x0004 /* In-memory database */
  8528. /*
  8529. ** Valid values for the second argument to sqlite3PagerLockingMode().
  8530. */
  8531. #define PAGER_LOCKINGMODE_QUERY -1
  8532. #define PAGER_LOCKINGMODE_NORMAL 0
  8533. #define PAGER_LOCKINGMODE_EXCLUSIVE 1
  8534. /*
  8535. ** Numeric constants that encode the journalmode.
  8536. */
  8537. #define PAGER_JOURNALMODE_QUERY (-1) /* Query the value of journalmode */
  8538. #define PAGER_JOURNALMODE_DELETE 0 /* Commit by deleting journal file */
  8539. #define PAGER_JOURNALMODE_PERSIST 1 /* Commit by zeroing journal header */
  8540. #define PAGER_JOURNALMODE_OFF 2 /* Journal omitted. */
  8541. #define PAGER_JOURNALMODE_TRUNCATE 3 /* Commit by truncating journal */
  8542. #define PAGER_JOURNALMODE_MEMORY 4 /* In-memory journal file */
  8543. #define PAGER_JOURNALMODE_WAL 5 /* Use write-ahead logging */
  8544. /*
  8545. ** The remainder of this file contains the declarations of the functions
  8546. ** that make up the Pager sub-system API. See source code comments for
  8547. ** a detailed description of each routine.
  8548. */
  8549. /* Open and close a Pager connection. */
  8550. SQLITE_PRIVATE int sqlite3PagerOpen(
  8551. sqlite3_vfs*,
  8552. Pager **ppPager,
  8553. const char*,
  8554. int,
  8555. int,
  8556. int,
  8557. void(*)(DbPage*)
  8558. );
  8559. SQLITE_PRIVATE int sqlite3PagerClose(Pager *pPager);
  8560. SQLITE_PRIVATE int sqlite3PagerReadFileheader(Pager*, int, unsigned char*);
  8561. /* Functions used to configure a Pager object. */
  8562. SQLITE_PRIVATE void sqlite3PagerSetBusyhandler(Pager*, int(*)(void *), void *);
  8563. SQLITE_PRIVATE int sqlite3PagerSetPagesize(Pager*, u32*, int);
  8564. SQLITE_PRIVATE int sqlite3PagerMaxPageCount(Pager*, int);
  8565. SQLITE_PRIVATE void sqlite3PagerSetCachesize(Pager*, int);
  8566. SQLITE_PRIVATE void sqlite3PagerShrink(Pager*);
  8567. SQLITE_PRIVATE void sqlite3PagerSetSafetyLevel(Pager*,int,int,int);
  8568. SQLITE_PRIVATE int sqlite3PagerLockingMode(Pager *, int);
  8569. SQLITE_PRIVATE int sqlite3PagerSetJournalMode(Pager *, int);
  8570. SQLITE_PRIVATE int sqlite3PagerGetJournalMode(Pager*);
  8571. SQLITE_PRIVATE int sqlite3PagerOkToChangeJournalMode(Pager*);
  8572. SQLITE_PRIVATE i64 sqlite3PagerJournalSizeLimit(Pager *, i64);
  8573. SQLITE_PRIVATE sqlite3_backup **sqlite3PagerBackupPtr(Pager*);
  8574. /* Functions used to obtain and release page references. */
  8575. SQLITE_PRIVATE int sqlite3PagerAcquire(Pager *pPager, Pgno pgno, DbPage **ppPage, int clrFlag);
  8576. #define sqlite3PagerGet(A,B,C) sqlite3PagerAcquire(A,B,C,0)
  8577. SQLITE_PRIVATE DbPage *sqlite3PagerLookup(Pager *pPager, Pgno pgno);
  8578. SQLITE_PRIVATE void sqlite3PagerRef(DbPage*);
  8579. SQLITE_PRIVATE void sqlite3PagerUnref(DbPage*);
  8580. /* Operations on page references. */
  8581. SQLITE_PRIVATE int sqlite3PagerWrite(DbPage*);
  8582. SQLITE_PRIVATE void sqlite3PagerDontWrite(DbPage*);
  8583. SQLITE_PRIVATE int sqlite3PagerMovepage(Pager*,DbPage*,Pgno,int);
  8584. SQLITE_PRIVATE int sqlite3PagerPageRefcount(DbPage*);
  8585. SQLITE_PRIVATE void *sqlite3PagerGetData(DbPage *);
  8586. SQLITE_PRIVATE void *sqlite3PagerGetExtra(DbPage *);
  8587. /* Functions used to manage pager transactions and savepoints. */
  8588. SQLITE_PRIVATE void sqlite3PagerPagecount(Pager*, int*);
  8589. SQLITE_PRIVATE int sqlite3PagerBegin(Pager*, int exFlag, int);
  8590. SQLITE_PRIVATE int sqlite3PagerCommitPhaseOne(Pager*,const char *zMaster, int);
  8591. SQLITE_PRIVATE int sqlite3PagerExclusiveLock(Pager*);
  8592. SQLITE_PRIVATE int sqlite3PagerSync(Pager *pPager);
  8593. SQLITE_PRIVATE int sqlite3PagerCommitPhaseTwo(Pager*);
  8594. SQLITE_PRIVATE int sqlite3PagerRollback(Pager*);
  8595. SQLITE_PRIVATE int sqlite3PagerOpenSavepoint(Pager *pPager, int n);
  8596. SQLITE_PRIVATE int sqlite3PagerSavepoint(Pager *pPager, int op, int iSavepoint);
  8597. SQLITE_PRIVATE int sqlite3PagerSharedLock(Pager *pPager);
  8598. SQLITE_PRIVATE int sqlite3PagerCheckpoint(Pager *pPager, int, int*, int*);
  8599. SQLITE_PRIVATE int sqlite3PagerWalSupported(Pager *pPager);
  8600. SQLITE_PRIVATE int sqlite3PagerWalCallback(Pager *pPager);
  8601. SQLITE_PRIVATE int sqlite3PagerOpenWal(Pager *pPager, int *pisOpen);
  8602. SQLITE_PRIVATE int sqlite3PagerCloseWal(Pager *pPager);
  8603. /* Functions used to query pager state and configuration. */
  8604. SQLITE_PRIVATE u8 sqlite3PagerIsreadonly(Pager*);
  8605. SQLITE_PRIVATE int sqlite3PagerRefcount(Pager*);
  8606. SQLITE_PRIVATE int sqlite3PagerMemUsed(Pager*);
  8607. SQLITE_PRIVATE const char *sqlite3PagerFilename(Pager*);
  8608. SQLITE_PRIVATE const sqlite3_vfs *sqlite3PagerVfs(Pager*);
  8609. SQLITE_PRIVATE sqlite3_file *sqlite3PagerFile(Pager*);
  8610. SQLITE_PRIVATE const char *sqlite3PagerJournalname(Pager*);
  8611. SQLITE_PRIVATE int sqlite3PagerNosync(Pager*);
  8612. SQLITE_PRIVATE void *sqlite3PagerTempSpace(Pager*);
  8613. SQLITE_PRIVATE int sqlite3PagerIsMemdb(Pager*);
  8614. SQLITE_PRIVATE void sqlite3PagerCacheStat(Pager *, int, int, int *);
  8615. SQLITE_PRIVATE void sqlite3PagerClearCache(Pager *);
  8616. /* Functions used to truncate the database file. */
  8617. SQLITE_PRIVATE void sqlite3PagerTruncateImage(Pager*,Pgno);
  8618. #if defined(SQLITE_HAS_CODEC) && !defined(SQLITE_OMIT_WAL)
  8619. SQLITE_PRIVATE void *sqlite3PagerCodec(DbPage *);
  8620. #endif
  8621. /* Functions to support testing and debugging. */
  8622. #if !defined(NDEBUG) || defined(SQLITE_TEST)
  8623. SQLITE_PRIVATE Pgno sqlite3PagerPagenumber(DbPage*);
  8624. SQLITE_PRIVATE int sqlite3PagerIswriteable(DbPage*);
  8625. #endif
  8626. #ifdef SQLITE_TEST
  8627. SQLITE_PRIVATE int *sqlite3PagerStats(Pager*);
  8628. SQLITE_PRIVATE void sqlite3PagerRefdump(Pager*);
  8629. void disable_simulated_io_errors(void);
  8630. void enable_simulated_io_errors(void);
  8631. #else
  8632. # define disable_simulated_io_errors()
  8633. # define enable_simulated_io_errors()
  8634. #endif
  8635. #endif /* _PAGER_H_ */
  8636. /************** End of pager.h ***********************************************/
  8637. /************** Continuing where we left off in sqliteInt.h ******************/
  8638. /************** Include pcache.h in the middle of sqliteInt.h ****************/
  8639. /************** Begin file pcache.h ******************************************/
  8640. /*
  8641. ** 2008 August 05
  8642. **
  8643. ** The author disclaims copyright to this source code. In place of
  8644. ** a legal notice, here is a blessing:
  8645. **
  8646. ** May you do good and not evil.
  8647. ** May you find forgiveness for yourself and forgive others.
  8648. ** May you share freely, never taking more than you give.
  8649. **
  8650. *************************************************************************
  8651. ** This header file defines the interface that the sqlite page cache
  8652. ** subsystem.
  8653. */
  8654. #ifndef _PCACHE_H_
  8655. typedef struct PgHdr PgHdr;
  8656. typedef struct PCache PCache;
  8657. /*
  8658. ** Every page in the cache is controlled by an instance of the following
  8659. ** structure.
  8660. */
  8661. struct PgHdr {
  8662. sqlite3_pcache_page *pPage; /* Pcache object page handle */
  8663. void *pData; /* Page data */
  8664. void *pExtra; /* Extra content */
  8665. PgHdr *pDirty; /* Transient list of dirty pages */
  8666. Pgno pgno; /* Page number for this page */
  8667. Pager *pPager; /* The pager this page is part of */
  8668. #ifdef SQLITE_CHECK_PAGES
  8669. u32 pageHash; /* Hash of page content */
  8670. #endif
  8671. u16 flags; /* PGHDR flags defined below */
  8672. /**********************************************************************
  8673. ** Elements above are public. All that follows is private to pcache.c
  8674. ** and should not be accessed by other modules.
  8675. */
  8676. i16 nRef; /* Number of users of this page */
  8677. PCache *pCache; /* Cache that owns this page */
  8678. PgHdr *pDirtyNext; /* Next element in list of dirty pages */
  8679. PgHdr *pDirtyPrev; /* Previous element in list of dirty pages */
  8680. };
  8681. /* Bit values for PgHdr.flags */
  8682. #define PGHDR_DIRTY 0x002 /* Page has changed */
  8683. #define PGHDR_NEED_SYNC 0x004 /* Fsync the rollback journal before
  8684. ** writing this page to the database */
  8685. #define PGHDR_NEED_READ 0x008 /* Content is unread */
  8686. #define PGHDR_REUSE_UNLIKELY 0x010 /* A hint that reuse is unlikely */
  8687. #define PGHDR_DONT_WRITE 0x020 /* Do not write content to disk */
  8688. /* Initialize and shutdown the page cache subsystem */
  8689. SQLITE_PRIVATE int sqlite3PcacheInitialize(void);
  8690. SQLITE_PRIVATE void sqlite3PcacheShutdown(void);
  8691. /* Page cache buffer management:
  8692. ** These routines implement SQLITE_CONFIG_PAGECACHE.
  8693. */
  8694. SQLITE_PRIVATE void sqlite3PCacheBufferSetup(void *, int sz, int n);
  8695. /* Create a new pager cache.
  8696. ** Under memory stress, invoke xStress to try to make pages clean.
  8697. ** Only clean and unpinned pages can be reclaimed.
  8698. */
  8699. SQLITE_PRIVATE void sqlite3PcacheOpen(
  8700. int szPage, /* Size of every page */
  8701. int szExtra, /* Extra space associated with each page */
  8702. int bPurgeable, /* True if pages are on backing store */
  8703. int (*xStress)(void*, PgHdr*), /* Call to try to make pages clean */
  8704. void *pStress, /* Argument to xStress */
  8705. PCache *pToInit /* Preallocated space for the PCache */
  8706. );
  8707. /* Modify the page-size after the cache has been created. */
  8708. SQLITE_PRIVATE void sqlite3PcacheSetPageSize(PCache *, int);
  8709. /* Return the size in bytes of a PCache object. Used to preallocate
  8710. ** storage space.
  8711. */
  8712. SQLITE_PRIVATE int sqlite3PcacheSize(void);
  8713. /* One release per successful fetch. Page is pinned until released.
  8714. ** Reference counted.
  8715. */
  8716. SQLITE_PRIVATE int sqlite3PcacheFetch(PCache*, Pgno, int createFlag, PgHdr**);
  8717. SQLITE_PRIVATE void sqlite3PcacheRelease(PgHdr*);
  8718. SQLITE_PRIVATE void sqlite3PcacheDrop(PgHdr*); /* Remove page from cache */
  8719. SQLITE_PRIVATE void sqlite3PcacheMakeDirty(PgHdr*); /* Make sure page is marked dirty */
  8720. SQLITE_PRIVATE void sqlite3PcacheMakeClean(PgHdr*); /* Mark a single page as clean */
  8721. SQLITE_PRIVATE void sqlite3PcacheCleanAll(PCache*); /* Mark all dirty list pages as clean */
  8722. /* Change a page number. Used by incr-vacuum. */
  8723. SQLITE_PRIVATE void sqlite3PcacheMove(PgHdr*, Pgno);
  8724. /* Remove all pages with pgno>x. Reset the cache if x==0 */
  8725. SQLITE_PRIVATE void sqlite3PcacheTruncate(PCache*, Pgno x);
  8726. /* Get a list of all dirty pages in the cache, sorted by page number */
  8727. SQLITE_PRIVATE PgHdr *sqlite3PcacheDirtyList(PCache*);
  8728. /* Reset and close the cache object */
  8729. SQLITE_PRIVATE void sqlite3PcacheClose(PCache*);
  8730. /* Clear flags from pages of the page cache */
  8731. SQLITE_PRIVATE void sqlite3PcacheClearSyncFlags(PCache *);
  8732. /* Discard the contents of the cache */
  8733. SQLITE_PRIVATE void sqlite3PcacheClear(PCache*);
  8734. /* Return the total number of outstanding page references */
  8735. SQLITE_PRIVATE int sqlite3PcacheRefCount(PCache*);
  8736. /* Increment the reference count of an existing page */
  8737. SQLITE_PRIVATE void sqlite3PcacheRef(PgHdr*);
  8738. SQLITE_PRIVATE int sqlite3PcachePageRefcount(PgHdr*);
  8739. /* Return the total number of pages stored in the cache */
  8740. SQLITE_PRIVATE int sqlite3PcachePagecount(PCache*);
  8741. #if defined(SQLITE_CHECK_PAGES) || defined(SQLITE_DEBUG)
  8742. /* Iterate through all dirty pages currently stored in the cache. This
  8743. ** interface is only available if SQLITE_CHECK_PAGES is defined when the
  8744. ** library is built.
  8745. */
  8746. SQLITE_PRIVATE void sqlite3PcacheIterateDirty(PCache *pCache, void (*xIter)(PgHdr *));
  8747. #endif
  8748. /* Set and get the suggested cache-size for the specified pager-cache.
  8749. **
  8750. ** If no global maximum is configured, then the system attempts to limit
  8751. ** the total number of pages cached by purgeable pager-caches to the sum
  8752. ** of the suggested cache-sizes.
  8753. */
  8754. SQLITE_PRIVATE void sqlite3PcacheSetCachesize(PCache *, int);
  8755. #ifdef SQLITE_TEST
  8756. SQLITE_PRIVATE int sqlite3PcacheGetCachesize(PCache *);
  8757. #endif
  8758. /* Free up as much memory as possible from the page cache */
  8759. SQLITE_PRIVATE void sqlite3PcacheShrink(PCache*);
  8760. #ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
  8761. /* Try to return memory used by the pcache module to the main memory heap */
  8762. SQLITE_PRIVATE int sqlite3PcacheReleaseMemory(int);
  8763. #endif
  8764. #ifdef SQLITE_TEST
  8765. SQLITE_PRIVATE void sqlite3PcacheStats(int*,int*,int*,int*);
  8766. #endif
  8767. SQLITE_PRIVATE void sqlite3PCacheSetDefault(void);
  8768. #endif /* _PCACHE_H_ */
  8769. /************** End of pcache.h **********************************************/
  8770. /************** Continuing where we left off in sqliteInt.h ******************/
  8771. /************** Include os.h in the middle of sqliteInt.h ********************/
  8772. /************** Begin file os.h **********************************************/
  8773. /*
  8774. ** 2001 September 16
  8775. **
  8776. ** The author disclaims copyright to this source code. In place of
  8777. ** a legal notice, here is a blessing:
  8778. **
  8779. ** May you do good and not evil.
  8780. ** May you find forgiveness for yourself and forgive others.
  8781. ** May you share freely, never taking more than you give.
  8782. **
  8783. ******************************************************************************
  8784. **
  8785. ** This header file (together with is companion C source-code file
  8786. ** "os.c") attempt to abstract the underlying operating system so that
  8787. ** the SQLite library will work on both POSIX and windows systems.
  8788. **
  8789. ** This header file is #include-ed by sqliteInt.h and thus ends up
  8790. ** being included by every source file.
  8791. */
  8792. #ifndef _SQLITE_OS_H_
  8793. #define _SQLITE_OS_H_
  8794. /*
  8795. ** Figure out if we are dealing with Unix, Windows, or some other
  8796. ** operating system. After the following block of preprocess macros,
  8797. ** all of SQLITE_OS_UNIX, SQLITE_OS_WIN, SQLITE_OS_OS2, and SQLITE_OS_OTHER
  8798. ** will defined to either 1 or 0. One of the four will be 1. The other
  8799. ** three will be 0.
  8800. */
  8801. #if defined(SQLITE_OS_OTHER)
  8802. # if SQLITE_OS_OTHER==1
  8803. # undef SQLITE_OS_UNIX
  8804. # define SQLITE_OS_UNIX 0
  8805. # undef SQLITE_OS_WIN
  8806. # define SQLITE_OS_WIN 0
  8807. # undef SQLITE_OS_OS2
  8808. # define SQLITE_OS_OS2 0
  8809. # else
  8810. # undef SQLITE_OS_OTHER
  8811. # endif
  8812. #endif
  8813. #if !defined(SQLITE_OS_UNIX) && !defined(SQLITE_OS_OTHER)
  8814. # define SQLITE_OS_OTHER 0
  8815. # ifndef SQLITE_OS_WIN
  8816. # if defined(_WIN32) || defined(WIN32) || defined(__CYGWIN__) || defined(__MINGW32__) || defined(__BORLANDC__)
  8817. # define SQLITE_OS_WIN 1
  8818. # define SQLITE_OS_UNIX 0
  8819. # define SQLITE_OS_OS2 0
  8820. # elif defined(__EMX__) || defined(_OS2) || defined(OS2) || defined(_OS2_) || defined(__OS2__)
  8821. # define SQLITE_OS_WIN 0
  8822. # define SQLITE_OS_UNIX 0
  8823. # define SQLITE_OS_OS2 1
  8824. # else
  8825. # define SQLITE_OS_WIN 0
  8826. # define SQLITE_OS_UNIX 1
  8827. # define SQLITE_OS_OS2 0
  8828. # endif
  8829. # else
  8830. # define SQLITE_OS_UNIX 0
  8831. # define SQLITE_OS_OS2 0
  8832. # endif
  8833. #else
  8834. # ifndef SQLITE_OS_WIN
  8835. # define SQLITE_OS_WIN 0
  8836. # endif
  8837. #endif
  8838. /*
  8839. ** Define the maximum size of a temporary filename
  8840. */
  8841. #if SQLITE_OS_WIN
  8842. # include <windows.h>
  8843. # define SQLITE_TEMPNAME_SIZE (MAX_PATH+50)
  8844. #elif SQLITE_OS_OS2
  8845. # if (__GNUC__ > 3 || __GNUC__ == 3 && __GNUC_MINOR__ >= 3) && defined(OS2_HIGH_MEMORY)
  8846. # include <os2safe.h> /* has to be included before os2.h for linking to work */
  8847. # endif
  8848. # define INCL_DOSDATETIME
  8849. # define INCL_DOSFILEMGR
  8850. # define INCL_DOSERRORS
  8851. # define INCL_DOSMISC
  8852. # define INCL_DOSPROCESS
  8853. # define INCL_DOSMODULEMGR
  8854. # define INCL_DOSSEMAPHORES
  8855. # include <os2.h>
  8856. # include <uconv.h>
  8857. # define SQLITE_TEMPNAME_SIZE (CCHMAXPATHCOMP)
  8858. #else
  8859. # define SQLITE_TEMPNAME_SIZE 200
  8860. #endif
  8861. /*
  8862. ** Determine if we are dealing with Windows NT.
  8863. */
  8864. #if defined(_WIN32_WINNT)
  8865. # define SQLITE_OS_WINNT 1
  8866. #else
  8867. # define SQLITE_OS_WINNT 0
  8868. #endif
  8869. /*
  8870. ** Determine if we are dealing with WindowsCE - which has a much
  8871. ** reduced API.
  8872. */
  8873. #if defined(_WIN32_WCE)
  8874. # define SQLITE_OS_WINCE 1
  8875. #else
  8876. # define SQLITE_OS_WINCE 0
  8877. #endif
  8878. /* If the SET_FULLSYNC macro is not defined above, then make it
  8879. ** a no-op
  8880. */
  8881. #ifndef SET_FULLSYNC
  8882. # define SET_FULLSYNC(x,y)
  8883. #endif
  8884. /*
  8885. ** The default size of a disk sector
  8886. */
  8887. #ifndef SQLITE_DEFAULT_SECTOR_SIZE
  8888. # define SQLITE_DEFAULT_SECTOR_SIZE 4096
  8889. #endif
  8890. /*
  8891. ** Temporary files are named starting with this prefix followed by 16 random
  8892. ** alphanumeric characters, and no file extension. They are stored in the
  8893. ** OS's standard temporary file directory, and are deleted prior to exit.
  8894. ** If sqlite is being embedded in another program, you may wish to change the
  8895. ** prefix to reflect your program's name, so that if your program exits
  8896. ** prematurely, old temporary files can be easily identified. This can be done
  8897. ** using -DSQLITE_TEMP_FILE_PREFIX=myprefix_ on the compiler command line.
  8898. **
  8899. ** 2006-10-31: The default prefix used to be "sqlite_". But then
  8900. ** Mcafee started using SQLite in their anti-virus product and it
  8901. ** started putting files with the "sqlite" name in the c:/temp folder.
  8902. ** This annoyed many windows users. Those users would then do a
  8903. ** Google search for "sqlite", find the telephone numbers of the
  8904. ** developers and call to wake them up at night and complain.
  8905. ** For this reason, the default name prefix is changed to be "sqlite"
  8906. ** spelled backwards. So the temp files are still identified, but
  8907. ** anybody smart enough to figure out the code is also likely smart
  8908. ** enough to know that calling the developer will not help get rid
  8909. ** of the file.
  8910. */
  8911. #ifndef SQLITE_TEMP_FILE_PREFIX
  8912. # define SQLITE_TEMP_FILE_PREFIX "etilqs_"
  8913. #endif
  8914. /*
  8915. ** The following values may be passed as the second argument to
  8916. ** sqlite3OsLock(). The various locks exhibit the following semantics:
  8917. **
  8918. ** SHARED: Any number of processes may hold a SHARED lock simultaneously.
  8919. ** RESERVED: A single process may hold a RESERVED lock on a file at
  8920. ** any time. Other processes may hold and obtain new SHARED locks.
  8921. ** PENDING: A single process may hold a PENDING lock on a file at
  8922. ** any one time. Existing SHARED locks may persist, but no new
  8923. ** SHARED locks may be obtained by other processes.
  8924. ** EXCLUSIVE: An EXCLUSIVE lock precludes all other locks.
  8925. **
  8926. ** PENDING_LOCK may not be passed directly to sqlite3OsLock(). Instead, a
  8927. ** process that requests an EXCLUSIVE lock may actually obtain a PENDING
  8928. ** lock. This can be upgraded to an EXCLUSIVE lock by a subsequent call to
  8929. ** sqlite3OsLock().
  8930. */
  8931. #define NO_LOCK 0
  8932. #define SHARED_LOCK 1
  8933. #define RESERVED_LOCK 2
  8934. #define PENDING_LOCK 3
  8935. #define EXCLUSIVE_LOCK 4
  8936. /*
  8937. ** File Locking Notes: (Mostly about windows but also some info for Unix)
  8938. **
  8939. ** We cannot use LockFileEx() or UnlockFileEx() on Win95/98/ME because
  8940. ** those functions are not available. So we use only LockFile() and
  8941. ** UnlockFile().
  8942. **
  8943. ** LockFile() prevents not just writing but also reading by other processes.
  8944. ** A SHARED_LOCK is obtained by locking a single randomly-chosen
  8945. ** byte out of a specific range of bytes. The lock byte is obtained at
  8946. ** random so two separate readers can probably access the file at the
  8947. ** same time, unless they are unlucky and choose the same lock byte.
  8948. ** An EXCLUSIVE_LOCK is obtained by locking all bytes in the range.
  8949. ** There can only be one writer. A RESERVED_LOCK is obtained by locking
  8950. ** a single byte of the file that is designated as the reserved lock byte.
  8951. ** A PENDING_LOCK is obtained by locking a designated byte different from
  8952. ** the RESERVED_LOCK byte.
  8953. **
  8954. ** On WinNT/2K/XP systems, LockFileEx() and UnlockFileEx() are available,
  8955. ** which means we can use reader/writer locks. When reader/writer locks
  8956. ** are used, the lock is placed on the same range of bytes that is used
  8957. ** for probabilistic locking in Win95/98/ME. Hence, the locking scheme
  8958. ** will support two or more Win95 readers or two or more WinNT readers.
  8959. ** But a single Win95 reader will lock out all WinNT readers and a single
  8960. ** WinNT reader will lock out all other Win95 readers.
  8961. **
  8962. ** The following #defines specify the range of bytes used for locking.
  8963. ** SHARED_SIZE is the number of bytes available in the pool from which
  8964. ** a random byte is selected for a shared lock. The pool of bytes for
  8965. ** shared locks begins at SHARED_FIRST.
  8966. **
  8967. ** The same locking strategy and
  8968. ** byte ranges are used for Unix. This leaves open the possiblity of having
  8969. ** clients on win95, winNT, and unix all talking to the same shared file
  8970. ** and all locking correctly. To do so would require that samba (or whatever
  8971. ** tool is being used for file sharing) implements locks correctly between
  8972. ** windows and unix. I'm guessing that isn't likely to happen, but by
  8973. ** using the same locking range we are at least open to the possibility.
  8974. **
  8975. ** Locking in windows is manditory. For this reason, we cannot store
  8976. ** actual data in the bytes used for locking. The pager never allocates
  8977. ** the pages involved in locking therefore. SHARED_SIZE is selected so
  8978. ** that all locks will fit on a single page even at the minimum page size.
  8979. ** PENDING_BYTE defines the beginning of the locks. By default PENDING_BYTE
  8980. ** is set high so that we don't have to allocate an unused page except
  8981. ** for very large databases. But one should test the page skipping logic
  8982. ** by setting PENDING_BYTE low and running the entire regression suite.
  8983. **
  8984. ** Changing the value of PENDING_BYTE results in a subtly incompatible
  8985. ** file format. Depending on how it is changed, you might not notice
  8986. ** the incompatibility right away, even running a full regression test.
  8987. ** The default location of PENDING_BYTE is the first byte past the
  8988. ** 1GB boundary.
  8989. **
  8990. */
  8991. #ifdef SQLITE_OMIT_WSD
  8992. # define PENDING_BYTE (0x40000000)
  8993. #else
  8994. # define PENDING_BYTE sqlite3PendingByte
  8995. #endif
  8996. #define RESERVED_BYTE (PENDING_BYTE+1)
  8997. #define SHARED_FIRST (PENDING_BYTE+2)
  8998. #define SHARED_SIZE 510
  8999. /*
  9000. ** Wrapper around OS specific sqlite3_os_init() function.
  9001. */
  9002. SQLITE_PRIVATE int sqlite3OsInit(void);
  9003. /*
  9004. ** Functions for accessing sqlite3_file methods
  9005. */
  9006. SQLITE_PRIVATE int sqlite3OsClose(sqlite3_file*);
  9007. SQLITE_PRIVATE int sqlite3OsRead(sqlite3_file*, void*, int amt, i64 offset);
  9008. SQLITE_PRIVATE int sqlite3OsWrite(sqlite3_file*, const void*, int amt, i64 offset);
  9009. SQLITE_PRIVATE int sqlite3OsTruncate(sqlite3_file*, i64 size);
  9010. SQLITE_PRIVATE int sqlite3OsSync(sqlite3_file*, int);
  9011. SQLITE_PRIVATE int sqlite3OsFileSize(sqlite3_file*, i64 *pSize);
  9012. SQLITE_PRIVATE int sqlite3OsLock(sqlite3_file*, int);
  9013. SQLITE_PRIVATE int sqlite3OsUnlock(sqlite3_file*, int);
  9014. SQLITE_PRIVATE int sqlite3OsCheckReservedLock(sqlite3_file *id, int *pResOut);
  9015. SQLITE_PRIVATE int sqlite3OsFileControl(sqlite3_file*,int,void*);
  9016. SQLITE_PRIVATE void sqlite3OsFileControlHint(sqlite3_file*,int,void*);
  9017. #define SQLITE_FCNTL_DB_UNCHANGED 0xca093fa0
  9018. SQLITE_PRIVATE int sqlite3OsSectorSize(sqlite3_file *id);
  9019. SQLITE_PRIVATE int sqlite3OsDeviceCharacteristics(sqlite3_file *id);
  9020. SQLITE_PRIVATE int sqlite3OsShmMap(sqlite3_file *,int,int,int,void volatile **);
  9021. SQLITE_PRIVATE int sqlite3OsShmLock(sqlite3_file *id, int, int, int);
  9022. SQLITE_PRIVATE void sqlite3OsShmBarrier(sqlite3_file *id);
  9023. SQLITE_PRIVATE int sqlite3OsShmUnmap(sqlite3_file *id, int);
  9024. /*
  9025. ** Functions for accessing sqlite3_vfs methods
  9026. */
  9027. SQLITE_PRIVATE int sqlite3OsOpen(sqlite3_vfs *, const char *, sqlite3_file*, int, int *);
  9028. SQLITE_PRIVATE int sqlite3OsDelete(sqlite3_vfs *, const char *, int);
  9029. SQLITE_PRIVATE int sqlite3OsAccess(sqlite3_vfs *, const char *, int, int *pResOut);
  9030. SQLITE_PRIVATE int sqlite3OsFullPathname(sqlite3_vfs *, const char *, int, char *);
  9031. #ifndef SQLITE_OMIT_LOAD_EXTENSION
  9032. SQLITE_PRIVATE void *sqlite3OsDlOpen(sqlite3_vfs *, const char *);
  9033. SQLITE_PRIVATE void sqlite3OsDlError(sqlite3_vfs *, int, char *);
  9034. SQLITE_PRIVATE void (*sqlite3OsDlSym(sqlite3_vfs *, void *, const char *))(void);
  9035. SQLITE_PRIVATE void sqlite3OsDlClose(sqlite3_vfs *, void *);
  9036. #endif /* SQLITE_OMIT_LOAD_EXTENSION */
  9037. SQLITE_PRIVATE int sqlite3OsRandomness(sqlite3_vfs *, int, char *);
  9038. SQLITE_PRIVATE int sqlite3OsSleep(sqlite3_vfs *, int);
  9039. SQLITE_PRIVATE int sqlite3OsCurrentTimeInt64(sqlite3_vfs *, sqlite3_int64*);
  9040. /*
  9041. ** Convenience functions for opening and closing files using
  9042. ** sqlite3_malloc() to obtain space for the file-handle structure.
  9043. */
  9044. SQLITE_PRIVATE int sqlite3OsOpenMalloc(sqlite3_vfs *, const char *, sqlite3_file **, int,int*);
  9045. SQLITE_PRIVATE int sqlite3OsCloseFree(sqlite3_file *);
  9046. #endif /* _SQLITE_OS_H_ */
  9047. /************** End of os.h **************************************************/
  9048. /************** Continuing where we left off in sqliteInt.h ******************/
  9049. /************** Include mutex.h in the middle of sqliteInt.h *****************/
  9050. /************** Begin file mutex.h *******************************************/
  9051. /*
  9052. ** 2007 August 28
  9053. **
  9054. ** The author disclaims copyright to this source code. In place of
  9055. ** a legal notice, here is a blessing:
  9056. **
  9057. ** May you do good and not evil.
  9058. ** May you find forgiveness for yourself and forgive others.
  9059. ** May you share freely, never taking more than you give.
  9060. **
  9061. *************************************************************************
  9062. **
  9063. ** This file contains the common header for all mutex implementations.
  9064. ** The sqliteInt.h header #includes this file so that it is available
  9065. ** to all source files. We break it out in an effort to keep the code
  9066. ** better organized.
  9067. **
  9068. ** NOTE: source files should *not* #include this header file directly.
  9069. ** Source files should #include the sqliteInt.h file and let that file
  9070. ** include this one indirectly.
  9071. */
  9072. /*
  9073. ** Figure out what version of the code to use. The choices are
  9074. **
  9075. ** SQLITE_MUTEX_OMIT No mutex logic. Not even stubs. The
  9076. ** mutexes implemention cannot be overridden
  9077. ** at start-time.
  9078. **
  9079. ** SQLITE_MUTEX_NOOP For single-threaded applications. No
  9080. ** mutual exclusion is provided. But this
  9081. ** implementation can be overridden at
  9082. ** start-time.
  9083. **
  9084. ** SQLITE_MUTEX_PTHREADS For multi-threaded applications on Unix.
  9085. **
  9086. ** SQLITE_MUTEX_W32 For multi-threaded applications on Win32.
  9087. **
  9088. ** SQLITE_MUTEX_OS2 For multi-threaded applications on OS/2.
  9089. */
  9090. #if !SQLITE_THREADSAFE
  9091. # define SQLITE_MUTEX_OMIT
  9092. #endif
  9093. #if SQLITE_THREADSAFE && !defined(SQLITE_MUTEX_NOOP)
  9094. # if SQLITE_OS_UNIX
  9095. # define SQLITE_MUTEX_PTHREADS
  9096. # elif SQLITE_OS_WIN
  9097. # define SQLITE_MUTEX_W32
  9098. # elif SQLITE_OS_OS2
  9099. # define SQLITE_MUTEX_OS2
  9100. # else
  9101. # define SQLITE_MUTEX_NOOP
  9102. # endif
  9103. #endif
  9104. #ifdef SQLITE_MUTEX_OMIT
  9105. /*
  9106. ** If this is a no-op implementation, implement everything as macros.
  9107. */
  9108. #define sqlite3_mutex_alloc(X) ((sqlite3_mutex*)8)
  9109. #define sqlite3_mutex_free(X)
  9110. #define sqlite3_mutex_enter(X)
  9111. #define sqlite3_mutex_try(X) SQLITE_OK
  9112. #define sqlite3_mutex_leave(X)
  9113. #define sqlite3_mutex_held(X) ((void)(X),1)
  9114. #define sqlite3_mutex_notheld(X) ((void)(X),1)
  9115. #define sqlite3MutexAlloc(X) ((sqlite3_mutex*)8)
  9116. #define sqlite3MutexInit() SQLITE_OK
  9117. #define sqlite3MutexEnd()
  9118. #define MUTEX_LOGIC(X)
  9119. #else
  9120. #define MUTEX_LOGIC(X) X
  9121. #endif /* defined(SQLITE_MUTEX_OMIT) */
  9122. /************** End of mutex.h ***********************************************/
  9123. /************** Continuing where we left off in sqliteInt.h ******************/
  9124. /*
  9125. ** Each database file to be accessed by the system is an instance
  9126. ** of the following structure. There are normally two of these structures
  9127. ** in the sqlite.aDb[] array. aDb[0] is the main database file and
  9128. ** aDb[1] is the database file used to hold temporary tables. Additional
  9129. ** databases may be attached.
  9130. */
  9131. struct Db {
  9132. char *zName; /* Name of this database */
  9133. Btree *pBt; /* The B*Tree structure for this database file */
  9134. u8 inTrans; /* 0: not writable. 1: Transaction. 2: Checkpoint */
  9135. u8 safety_level; /* How aggressive at syncing data to disk */
  9136. Schema *pSchema; /* Pointer to database schema (possibly shared) */
  9137. };
  9138. /*
  9139. ** An instance of the following structure stores a database schema.
  9140. **
  9141. ** Most Schema objects are associated with a Btree. The exception is
  9142. ** the Schema for the TEMP databaes (sqlite3.aDb[1]) which is free-standing.
  9143. ** In shared cache mode, a single Schema object can be shared by multiple
  9144. ** Btrees that refer to the same underlying BtShared object.
  9145. **
  9146. ** Schema objects are automatically deallocated when the last Btree that
  9147. ** references them is destroyed. The TEMP Schema is manually freed by
  9148. ** sqlite3_close().
  9149. *
  9150. ** A thread must be holding a mutex on the corresponding Btree in order
  9151. ** to access Schema content. This implies that the thread must also be
  9152. ** holding a mutex on the sqlite3 connection pointer that owns the Btree.
  9153. ** For a TEMP Schema, only the connection mutex is required.
  9154. */
  9155. struct Schema {
  9156. int schema_cookie; /* Database schema version number for this file */
  9157. int iGeneration; /* Generation counter. Incremented with each change */
  9158. Hash tblHash; /* All tables indexed by name */
  9159. Hash idxHash; /* All (named) indices indexed by name */
  9160. Hash trigHash; /* All triggers indexed by name */
  9161. Hash fkeyHash; /* All foreign keys by referenced table name */
  9162. Table *pSeqTab; /* The sqlite_sequence table used by AUTOINCREMENT */
  9163. u8 file_format; /* Schema format version for this file */
  9164. u8 enc; /* Text encoding used by this database */
  9165. u16 flags; /* Flags associated with this schema */
  9166. int cache_size; /* Number of pages to use in the cache */
  9167. };
  9168. /*
  9169. ** These macros can be used to test, set, or clear bits in the
  9170. ** Db.pSchema->flags field.
  9171. */
  9172. #define DbHasProperty(D,I,P) (((D)->aDb[I].pSchema->flags&(P))==(P))
  9173. #define DbHasAnyProperty(D,I,P) (((D)->aDb[I].pSchema->flags&(P))!=0)
  9174. #define DbSetProperty(D,I,P) (D)->aDb[I].pSchema->flags|=(P)
  9175. #define DbClearProperty(D,I,P) (D)->aDb[I].pSchema->flags&=~(P)
  9176. /*
  9177. ** Allowed values for the DB.pSchema->flags field.
  9178. **
  9179. ** The DB_SchemaLoaded flag is set after the database schema has been
  9180. ** read into internal hash tables.
  9181. **
  9182. ** DB_UnresetViews means that one or more views have column names that
  9183. ** have been filled out. If the schema changes, these column names might
  9184. ** changes and so the view will need to be reset.
  9185. */
  9186. #define DB_SchemaLoaded 0x0001 /* The schema has been loaded */
  9187. #define DB_UnresetViews 0x0002 /* Some views have defined column names */
  9188. #define DB_Empty 0x0004 /* The file is empty (length 0 bytes) */
  9189. /*
  9190. ** The number of different kinds of things that can be limited
  9191. ** using the sqlite3_limit() interface.
  9192. */
  9193. #define SQLITE_N_LIMIT (SQLITE_LIMIT_TRIGGER_DEPTH+1)
  9194. /*
  9195. ** Lookaside malloc is a set of fixed-size buffers that can be used
  9196. ** to satisfy small transient memory allocation requests for objects
  9197. ** associated with a particular database connection. The use of
  9198. ** lookaside malloc provides a significant performance enhancement
  9199. ** (approx 10%) by avoiding numerous malloc/free requests while parsing
  9200. ** SQL statements.
  9201. **
  9202. ** The Lookaside structure holds configuration information about the
  9203. ** lookaside malloc subsystem. Each available memory allocation in
  9204. ** the lookaside subsystem is stored on a linked list of LookasideSlot
  9205. ** objects.
  9206. **
  9207. ** Lookaside allocations are only allowed for objects that are associated
  9208. ** with a particular database connection. Hence, schema information cannot
  9209. ** be stored in lookaside because in shared cache mode the schema information
  9210. ** is shared by multiple database connections. Therefore, while parsing
  9211. ** schema information, the Lookaside.bEnabled flag is cleared so that
  9212. ** lookaside allocations are not used to construct the schema objects.
  9213. */
  9214. struct Lookaside {
  9215. u16 sz; /* Size of each buffer in bytes */
  9216. u8 bEnabled; /* False to disable new lookaside allocations */
  9217. u8 bMalloced; /* True if pStart obtained from sqlite3_malloc() */
  9218. int nOut; /* Number of buffers currently checked out */
  9219. int mxOut; /* Highwater mark for nOut */
  9220. int anStat[3]; /* 0: hits. 1: size misses. 2: full misses */
  9221. LookasideSlot *pFree; /* List of available buffers */
  9222. void *pStart; /* First byte of available memory space */
  9223. void *pEnd; /* First byte past end of available space */
  9224. };
  9225. struct LookasideSlot {
  9226. LookasideSlot *pNext; /* Next buffer in the list of free buffers */
  9227. };
  9228. /*
  9229. ** A hash table for function definitions.
  9230. **
  9231. ** Hash each FuncDef structure into one of the FuncDefHash.a[] slots.
  9232. ** Collisions are on the FuncDef.pHash chain.
  9233. */
  9234. struct FuncDefHash {
  9235. FuncDef *a[23]; /* Hash table for functions */
  9236. };
  9237. /*
  9238. ** Each database connection is an instance of the following structure.
  9239. **
  9240. ** The sqlite.lastRowid records the last insert rowid generated by an
  9241. ** insert statement. Inserts on views do not affect its value. Each
  9242. ** trigger has its own context, so that lastRowid can be updated inside
  9243. ** triggers as usual. The previous value will be restored once the trigger
  9244. ** exits. Upon entering a before or instead of trigger, lastRowid is no
  9245. ** longer (since after version 2.8.12) reset to -1.
  9246. **
  9247. ** The sqlite.nChange does not count changes within triggers and keeps no
  9248. ** context. It is reset at start of sqlite3_exec.
  9249. ** The sqlite.lsChange represents the number of changes made by the last
  9250. ** insert, update, or delete statement. It remains constant throughout the
  9251. ** length of a statement and is then updated by OP_SetCounts. It keeps a
  9252. ** context stack just like lastRowid so that the count of changes
  9253. ** within a trigger is not seen outside the trigger. Changes to views do not
  9254. ** affect the value of lsChange.
  9255. ** The sqlite.csChange keeps track of the number of current changes (since
  9256. ** the last statement) and is used to update sqlite_lsChange.
  9257. **
  9258. ** The member variables sqlite.errCode, sqlite.zErrMsg and sqlite.zErrMsg16
  9259. ** store the most recent error code and, if applicable, string. The
  9260. ** internal function sqlite3Error() is used to set these variables
  9261. ** consistently.
  9262. */
  9263. struct sqlite3 {
  9264. sqlite3_vfs *pVfs; /* OS Interface */
  9265. int nDb; /* Number of backends currently in use */
  9266. Db *aDb; /* All backends */
  9267. int flags; /* Miscellaneous flags. See below */
  9268. unsigned int openFlags; /* Flags passed to sqlite3_vfs.xOpen() */
  9269. int errCode; /* Most recent error code (SQLITE_*) */
  9270. int errMask; /* & result codes with this before returning */
  9271. u8 autoCommit; /* The auto-commit flag. */
  9272. u8 temp_store; /* 1: file 2: memory 0: default */
  9273. u8 mallocFailed; /* True if we have seen a malloc failure */
  9274. u8 dfltLockMode; /* Default locking-mode for attached dbs */
  9275. signed char nextAutovac; /* Autovac setting after VACUUM if >=0 */
  9276. u8 suppressErr; /* Do not issue error messages if true */
  9277. u8 vtabOnConflict; /* Value to return for s3_vtab_on_conflict() */
  9278. int nextPagesize; /* Pagesize after VACUUM if >0 */
  9279. int nTable; /* Number of tables in the database */
  9280. CollSeq *pDfltColl; /* The default collating sequence (BINARY) */
  9281. i64 lastRowid; /* ROWID of most recent insert (see above) */
  9282. u32 magic; /* Magic number for detect library misuse */
  9283. int nChange; /* Value returned by sqlite3_changes() */
  9284. int nTotalChange; /* Value returned by sqlite3_total_changes() */
  9285. sqlite3_mutex *mutex; /* Connection mutex */
  9286. int aLimit[SQLITE_N_LIMIT]; /* Limits */
  9287. struct sqlite3InitInfo { /* Information used during initialization */
  9288. int iDb; /* When back is being initialized */
  9289. int newTnum; /* Rootpage of table being initialized */
  9290. u8 busy; /* TRUE if currently initializing */
  9291. u8 orphanTrigger; /* Last statement is orphaned TEMP trigger */
  9292. } init;
  9293. int nExtension; /* Number of loaded extensions */
  9294. void **aExtension; /* Array of shared library handles */
  9295. struct Vdbe *pVdbe; /* List of active virtual machines */
  9296. int activeVdbeCnt; /* Number of VDBEs currently executing */
  9297. int writeVdbeCnt; /* Number of active VDBEs that are writing */
  9298. int vdbeExecCnt; /* Number of nested calls to VdbeExec() */
  9299. void (*xTrace)(void*,const char*); /* Trace function */
  9300. void *pTraceArg; /* Argument to the trace function */
  9301. void (*xProfile)(void*,const char*,u64); /* Profiling function */
  9302. void *pProfileArg; /* Argument to profile function */
  9303. void *pCommitArg; /* Argument to xCommitCallback() */
  9304. int (*xCommitCallback)(void*); /* Invoked at every commit. */
  9305. void *pRollbackArg; /* Argument to xRollbackCallback() */
  9306. void (*xRollbackCallback)(void*); /* Invoked at every commit. */
  9307. void *pUpdateArg;
  9308. void (*xUpdateCallback)(void*,int, const char*,const char*,sqlite_int64);
  9309. #ifndef SQLITE_OMIT_WAL
  9310. int (*xWalCallback)(void *, sqlite3 *, const char *, int);
  9311. void *pWalArg;
  9312. #endif
  9313. void(*xCollNeeded)(void*,sqlite3*,int eTextRep,const char*);
  9314. void(*xCollNeeded16)(void*,sqlite3*,int eTextRep,const void*);
  9315. void *pCollNeededArg;
  9316. sqlite3_value *pErr; /* Most recent error message */
  9317. char *zErrMsg; /* Most recent error message (UTF-8 encoded) */
  9318. char *zErrMsg16; /* Most recent error message (UTF-16 encoded) */
  9319. union {
  9320. volatile int isInterrupted; /* True if sqlite3_interrupt has been called */
  9321. double notUsed1; /* Spacer */
  9322. } u1;
  9323. Lookaside lookaside; /* Lookaside malloc configuration */
  9324. #ifndef SQLITE_OMIT_AUTHORIZATION
  9325. int (*xAuth)(void*,int,const char*,const char*,const char*,const char*);
  9326. /* Access authorization function */
  9327. void *pAuthArg; /* 1st argument to the access auth function */
  9328. #endif
  9329. #ifndef SQLITE_OMIT_PROGRESS_CALLBACK
  9330. int (*xProgress)(void *); /* The progress callback */
  9331. void *pProgressArg; /* Argument to the progress callback */
  9332. int nProgressOps; /* Number of opcodes for progress callback */
  9333. #endif
  9334. #ifndef SQLITE_OMIT_VIRTUALTABLE
  9335. Hash aModule; /* populated by sqlite3_create_module() */
  9336. VtabCtx *pVtabCtx; /* Context for active vtab connect/create */
  9337. VTable **aVTrans; /* Virtual tables with open transactions */
  9338. int nVTrans; /* Allocated size of aVTrans */
  9339. VTable *pDisconnect; /* Disconnect these in next sqlite3_prepare() */
  9340. #endif
  9341. FuncDefHash aFunc; /* Hash table of connection functions */
  9342. Hash aCollSeq; /* All collating sequences */
  9343. BusyHandler busyHandler; /* Busy callback */
  9344. int busyTimeout; /* Busy handler timeout, in msec */
  9345. Db aDbStatic[2]; /* Static space for the 2 default backends */
  9346. Savepoint *pSavepoint; /* List of active savepoints */
  9347. int nSavepoint; /* Number of non-transaction savepoints */
  9348. int nStatement; /* Number of nested statement-transactions */
  9349. u8 isTransactionSavepoint; /* True if the outermost savepoint is a TS */
  9350. i64 nDeferredCons; /* Net deferred constraints this transaction. */
  9351. int *pnBytesFreed; /* If not NULL, increment this in DbFree() */
  9352. #ifdef SQLITE_ENABLE_UNLOCK_NOTIFY
  9353. /* The following variables are all protected by the STATIC_MASTER
  9354. ** mutex, not by sqlite3.mutex. They are used by code in notify.c.
  9355. **
  9356. ** When X.pUnlockConnection==Y, that means that X is waiting for Y to
  9357. ** unlock so that it can proceed.
  9358. **
  9359. ** When X.pBlockingConnection==Y, that means that something that X tried
  9360. ** tried to do recently failed with an SQLITE_LOCKED error due to locks
  9361. ** held by Y.
  9362. */
  9363. sqlite3 *pBlockingConnection; /* Connection that caused SQLITE_LOCKED */
  9364. sqlite3 *pUnlockConnection; /* Connection to watch for unlock */
  9365. void *pUnlockArg; /* Argument to xUnlockNotify */
  9366. void (*xUnlockNotify)(void **, int); /* Unlock notify callback */
  9367. sqlite3 *pNextBlocked; /* Next in list of all blocked connections */
  9368. #endif
  9369. };
  9370. /*
  9371. ** A macro to discover the encoding of a database.
  9372. */
  9373. #define ENC(db) ((db)->aDb[0].pSchema->enc)
  9374. /*
  9375. ** Possible values for the sqlite3.flags.
  9376. */
  9377. #define SQLITE_VdbeTrace 0x00000100 /* True to trace VDBE execution */
  9378. #define SQLITE_InternChanges 0x00000200 /* Uncommitted Hash table changes */
  9379. #define SQLITE_FullColNames 0x00000400 /* Show full column names on SELECT */
  9380. #define SQLITE_ShortColNames 0x00000800 /* Show short columns names */
  9381. #define SQLITE_CountRows 0x00001000 /* Count rows changed by INSERT, */
  9382. /* DELETE, or UPDATE and return */
  9383. /* the count using a callback. */
  9384. #define SQLITE_NullCallback 0x00002000 /* Invoke the callback once if the */
  9385. /* result set is empty */
  9386. #define SQLITE_SqlTrace 0x00004000 /* Debug print SQL as it executes */
  9387. #define SQLITE_VdbeListing 0x00008000 /* Debug listings of VDBE programs */
  9388. #define SQLITE_WriteSchema 0x00010000 /* OK to update SQLITE_MASTER */
  9389. #define SQLITE_NoReadlock 0x00020000 /* Readlocks are omitted when
  9390. ** accessing read-only databases */
  9391. #define SQLITE_IgnoreChecks 0x00040000 /* Do not enforce check constraints */
  9392. #define SQLITE_ReadUncommitted 0x0080000 /* For shared-cache mode */
  9393. #define SQLITE_LegacyFileFmt 0x00100000 /* Create new databases in format 1 */
  9394. #define SQLITE_FullFSync 0x00200000 /* Use full fsync on the backend */
  9395. #define SQLITE_CkptFullFSync 0x00400000 /* Use full fsync for checkpoint */
  9396. #define SQLITE_RecoveryMode 0x00800000 /* Ignore schema errors */
  9397. #define SQLITE_ReverseOrder 0x01000000 /* Reverse unordered SELECTs */
  9398. #define SQLITE_RecTriggers 0x02000000 /* Enable recursive triggers */
  9399. #define SQLITE_ForeignKeys 0x04000000 /* Enforce foreign key constraints */
  9400. #define SQLITE_AutoIndex 0x08000000 /* Enable automatic indexes */
  9401. #define SQLITE_PreferBuiltin 0x10000000 /* Preference to built-in funcs */
  9402. #define SQLITE_LoadExtension 0x20000000 /* Enable load_extension */
  9403. #define SQLITE_EnableTrigger 0x40000000 /* True to enable triggers */
  9404. /*
  9405. ** Bits of the sqlite3.flags field that are used by the
  9406. ** sqlite3_test_control(SQLITE_TESTCTRL_OPTIMIZATIONS,...) interface.
  9407. ** These must be the low-order bits of the flags field.
  9408. */
  9409. #define SQLITE_QueryFlattener 0x01 /* Disable query flattening */
  9410. #define SQLITE_ColumnCache 0x02 /* Disable the column cache */
  9411. #define SQLITE_IndexSort 0x04 /* Disable indexes for sorting */
  9412. #define SQLITE_IndexSearch 0x08 /* Disable indexes for searching */
  9413. #define SQLITE_IndexCover 0x10 /* Disable index covering table */
  9414. #define SQLITE_GroupByOrder 0x20 /* Disable GROUPBY cover of ORDERBY */
  9415. #define SQLITE_FactorOutConst 0x40 /* Disable factoring out constants */
  9416. #define SQLITE_IdxRealAsInt 0x80 /* Store REAL as INT in indices */
  9417. #define SQLITE_DistinctOpt 0x80 /* DISTINCT using indexes */
  9418. #define SQLITE_OptMask 0xff /* Mask of all disablable opts */
  9419. /*
  9420. ** Possible values for the sqlite.magic field.
  9421. ** The numbers are obtained at random and have no special meaning, other
  9422. ** than being distinct from one another.
  9423. */
  9424. #define SQLITE_MAGIC_OPEN 0xa029a697 /* Database is open */
  9425. #define SQLITE_MAGIC_CLOSED 0x9f3c2d33 /* Database is closed */
  9426. #define SQLITE_MAGIC_SICK 0x4b771290 /* Error and awaiting close */
  9427. #define SQLITE_MAGIC_BUSY 0xf03b7906 /* Database currently in use */
  9428. #define SQLITE_MAGIC_ERROR 0xb5357930 /* An SQLITE_MISUSE error occurred */
  9429. /*
  9430. ** Each SQL function is defined by an instance of the following
  9431. ** structure. A pointer to this structure is stored in the sqlite.aFunc
  9432. ** hash table. When multiple functions have the same name, the hash table
  9433. ** points to a linked list of these structures.
  9434. */
  9435. struct FuncDef {
  9436. i16 nArg; /* Number of arguments. -1 means unlimited */
  9437. u8 iPrefEnc; /* Preferred text encoding (SQLITE_UTF8, 16LE, 16BE) */
  9438. u8 flags; /* Some combination of SQLITE_FUNC_* */
  9439. void *pUserData; /* User data parameter */
  9440. FuncDef *pNext; /* Next function with same name */
  9441. void (*xFunc)(sqlite3_context*,int,sqlite3_value**); /* Regular function */
  9442. void (*xStep)(sqlite3_context*,int,sqlite3_value**); /* Aggregate step */
  9443. void (*xFinalize)(sqlite3_context*); /* Aggregate finalizer */
  9444. char *zName; /* SQL name of the function. */
  9445. FuncDef *pHash; /* Next with a different name but the same hash */
  9446. FuncDestructor *pDestructor; /* Reference counted destructor function */
  9447. };
  9448. /*
  9449. ** This structure encapsulates a user-function destructor callback (as
  9450. ** configured using create_function_v2()) and a reference counter. When
  9451. ** create_function_v2() is called to create a function with a destructor,
  9452. ** a single object of this type is allocated. FuncDestructor.nRef is set to
  9453. ** the number of FuncDef objects created (either 1 or 3, depending on whether
  9454. ** or not the specified encoding is SQLITE_ANY). The FuncDef.pDestructor
  9455. ** member of each of the new FuncDef objects is set to point to the allocated
  9456. ** FuncDestructor.
  9457. **
  9458. ** Thereafter, when one of the FuncDef objects is deleted, the reference
  9459. ** count on this object is decremented. When it reaches 0, the destructor
  9460. ** is invoked and the FuncDestructor structure freed.
  9461. */
  9462. struct FuncDestructor {
  9463. int nRef;
  9464. void (*xDestroy)(void *);
  9465. void *pUserData;
  9466. };
  9467. /*
  9468. ** Possible values for FuncDef.flags
  9469. */
  9470. #define SQLITE_FUNC_LIKE 0x01 /* Candidate for the LIKE optimization */
  9471. #define SQLITE_FUNC_CASE 0x02 /* Case-sensitive LIKE-type function */
  9472. #define SQLITE_FUNC_EPHEM 0x04 /* Ephemeral. Delete with VDBE */
  9473. #define SQLITE_FUNC_NEEDCOLL 0x08 /* sqlite3GetFuncCollSeq() might be called */
  9474. #define SQLITE_FUNC_PRIVATE 0x10 /* Allowed for internal use only */
  9475. #define SQLITE_FUNC_COUNT 0x20 /* Built-in count(*) aggregate */
  9476. #define SQLITE_FUNC_COALESCE 0x40 /* Built-in coalesce() or ifnull() function */
  9477. /*
  9478. ** The following three macros, FUNCTION(), LIKEFUNC() and AGGREGATE() are
  9479. ** used to create the initializers for the FuncDef structures.
  9480. **
  9481. ** FUNCTION(zName, nArg, iArg, bNC, xFunc)
  9482. ** Used to create a scalar function definition of a function zName
  9483. ** implemented by C function xFunc that accepts nArg arguments. The
  9484. ** value passed as iArg is cast to a (void*) and made available
  9485. ** as the user-data (sqlite3_user_data()) for the function. If
  9486. ** argument bNC is true, then the SQLITE_FUNC_NEEDCOLL flag is set.
  9487. **
  9488. ** AGGREGATE(zName, nArg, iArg, bNC, xStep, xFinal)
  9489. ** Used to create an aggregate function definition implemented by
  9490. ** the C functions xStep and xFinal. The first four parameters
  9491. ** are interpreted in the same way as the first 4 parameters to
  9492. ** FUNCTION().
  9493. **
  9494. ** LIKEFUNC(zName, nArg, pArg, flags)
  9495. ** Used to create a scalar function definition of a function zName
  9496. ** that accepts nArg arguments and is implemented by a call to C
  9497. ** function likeFunc. Argument pArg is cast to a (void *) and made
  9498. ** available as the function user-data (sqlite3_user_data()). The
  9499. ** FuncDef.flags variable is set to the value passed as the flags
  9500. ** parameter.
  9501. */
  9502. #define FUNCTION(zName, nArg, iArg, bNC, xFunc) \
  9503. {nArg, SQLITE_UTF8, bNC*SQLITE_FUNC_NEEDCOLL, \
  9504. SQLITE_INT_TO_PTR(iArg), 0, xFunc, 0, 0, #zName, 0, 0}
  9505. #define STR_FUNCTION(zName, nArg, pArg, bNC, xFunc) \
  9506. {nArg, SQLITE_UTF8, bNC*SQLITE_FUNC_NEEDCOLL, \
  9507. pArg, 0, xFunc, 0, 0, #zName, 0, 0}
  9508. #define LIKEFUNC(zName, nArg, arg, flags) \
  9509. {nArg, SQLITE_UTF8, flags, (void *)arg, 0, likeFunc, 0, 0, #zName, 0, 0}
  9510. #define AGGREGATE(zName, nArg, arg, nc, xStep, xFinal) \
  9511. {nArg, SQLITE_UTF8, nc*SQLITE_FUNC_NEEDCOLL, \
  9512. SQLITE_INT_TO_PTR(arg), 0, 0, xStep,xFinal,#zName,0,0}
  9513. /*
  9514. ** All current savepoints are stored in a linked list starting at
  9515. ** sqlite3.pSavepoint. The first element in the list is the most recently
  9516. ** opened savepoint. Savepoints are added to the list by the vdbe
  9517. ** OP_Savepoint instruction.
  9518. */
  9519. struct Savepoint {
  9520. char *zName; /* Savepoint name (nul-terminated) */
  9521. i64 nDeferredCons; /* Number of deferred fk violations */
  9522. Savepoint *pNext; /* Parent savepoint (if any) */
  9523. };
  9524. /*
  9525. ** The following are used as the second parameter to sqlite3Savepoint(),
  9526. ** and as the P1 argument to the OP_Savepoint instruction.
  9527. */
  9528. #define SAVEPOINT_BEGIN 0
  9529. #define SAVEPOINT_RELEASE 1
  9530. #define SAVEPOINT_ROLLBACK 2
  9531. /*
  9532. ** Each SQLite module (virtual table definition) is defined by an
  9533. ** instance of the following structure, stored in the sqlite3.aModule
  9534. ** hash table.
  9535. */
  9536. struct Module {
  9537. const sqlite3_module *pModule; /* Callback pointers */
  9538. const char *zName; /* Name passed to create_module() */
  9539. void *pAux; /* pAux passed to create_module() */
  9540. void (*xDestroy)(void *); /* Module destructor function */
  9541. };
  9542. /*
  9543. ** information about each column of an SQL table is held in an instance
  9544. ** of this structure.
  9545. */
  9546. struct Column {
  9547. char *zName; /* Name of this column */
  9548. Expr *pDflt; /* Default value of this column */
  9549. char *zDflt; /* Original text of the default value */
  9550. char *zType; /* Data type for this column */
  9551. char *zColl; /* Collating sequence. If NULL, use the default */
  9552. u8 notNull; /* True if there is a NOT NULL constraint */
  9553. u8 isPrimKey; /* True if this column is part of the PRIMARY KEY */
  9554. char affinity; /* One of the SQLITE_AFF_... values */
  9555. #ifndef SQLITE_OMIT_VIRTUALTABLE
  9556. u8 isHidden; /* True if this column is 'hidden' */
  9557. #endif
  9558. };
  9559. /*
  9560. ** A "Collating Sequence" is defined by an instance of the following
  9561. ** structure. Conceptually, a collating sequence consists of a name and
  9562. ** a comparison routine that defines the order of that sequence.
  9563. **
  9564. ** There may two separate implementations of the collation function, one
  9565. ** that processes text in UTF-8 encoding (CollSeq.xCmp) and another that
  9566. ** processes text encoded in UTF-16 (CollSeq.xCmp16), using the machine
  9567. ** native byte order. When a collation sequence is invoked, SQLite selects
  9568. ** the version that will require the least expensive encoding
  9569. ** translations, if any.
  9570. **
  9571. ** The CollSeq.pUser member variable is an extra parameter that passed in
  9572. ** as the first argument to the UTF-8 comparison function, xCmp.
  9573. ** CollSeq.pUser16 is the equivalent for the UTF-16 comparison function,
  9574. ** xCmp16.
  9575. **
  9576. ** If both CollSeq.xCmp and CollSeq.xCmp16 are NULL, it means that the
  9577. ** collating sequence is undefined. Indices built on an undefined
  9578. ** collating sequence may not be read or written.
  9579. */
  9580. struct CollSeq {
  9581. char *zName; /* Name of the collating sequence, UTF-8 encoded */
  9582. u8 enc; /* Text encoding handled by xCmp() */
  9583. void *pUser; /* First argument to xCmp() */
  9584. int (*xCmp)(void*,int, const void*, int, const void*);
  9585. void (*xDel)(void*); /* Destructor for pUser */
  9586. };
  9587. /*
  9588. ** A sort order can be either ASC or DESC.
  9589. */
  9590. #define SQLITE_SO_ASC 0 /* Sort in ascending order */
  9591. #define SQLITE_SO_DESC 1 /* Sort in ascending order */
  9592. /*
  9593. ** Column affinity types.
  9594. **
  9595. ** These used to have mnemonic name like 'i' for SQLITE_AFF_INTEGER and
  9596. ** 't' for SQLITE_AFF_TEXT. But we can save a little space and improve
  9597. ** the speed a little by numbering the values consecutively.
  9598. **
  9599. ** But rather than start with 0 or 1, we begin with 'a'. That way,
  9600. ** when multiple affinity types are concatenated into a string and
  9601. ** used as the P4 operand, they will be more readable.
  9602. **
  9603. ** Note also that the numeric types are grouped together so that testing
  9604. ** for a numeric type is a single comparison.
  9605. */
  9606. #define SQLITE_AFF_TEXT 'a'
  9607. #define SQLITE_AFF_NONE 'b'
  9608. #define SQLITE_AFF_NUMERIC 'c'
  9609. #define SQLITE_AFF_INTEGER 'd'
  9610. #define SQLITE_AFF_REAL 'e'
  9611. #define sqlite3IsNumericAffinity(X) ((X)>=SQLITE_AFF_NUMERIC)
  9612. /*
  9613. ** The SQLITE_AFF_MASK values masks off the significant bits of an
  9614. ** affinity value.
  9615. */
  9616. #define SQLITE_AFF_MASK 0x67
  9617. /*
  9618. ** Additional bit values that can be ORed with an affinity without
  9619. ** changing the affinity.
  9620. */
  9621. #define SQLITE_JUMPIFNULL 0x08 /* jumps if either operand is NULL */
  9622. #define SQLITE_STOREP2 0x10 /* Store result in reg[P2] rather than jump */
  9623. #define SQLITE_NULLEQ 0x80 /* NULL=NULL */
  9624. /*
  9625. ** An object of this type is created for each virtual table present in
  9626. ** the database schema.
  9627. **
  9628. ** If the database schema is shared, then there is one instance of this
  9629. ** structure for each database connection (sqlite3*) that uses the shared
  9630. ** schema. This is because each database connection requires its own unique
  9631. ** instance of the sqlite3_vtab* handle used to access the virtual table
  9632. ** implementation. sqlite3_vtab* handles can not be shared between
  9633. ** database connections, even when the rest of the in-memory database
  9634. ** schema is shared, as the implementation often stores the database
  9635. ** connection handle passed to it via the xConnect() or xCreate() method
  9636. ** during initialization internally. This database connection handle may
  9637. ** then be used by the virtual table implementation to access real tables
  9638. ** within the database. So that they appear as part of the callers
  9639. ** transaction, these accesses need to be made via the same database
  9640. ** connection as that used to execute SQL operations on the virtual table.
  9641. **
  9642. ** All VTable objects that correspond to a single table in a shared
  9643. ** database schema are initially stored in a linked-list pointed to by
  9644. ** the Table.pVTable member variable of the corresponding Table object.
  9645. ** When an sqlite3_prepare() operation is required to access the virtual
  9646. ** table, it searches the list for the VTable that corresponds to the
  9647. ** database connection doing the preparing so as to use the correct
  9648. ** sqlite3_vtab* handle in the compiled query.
  9649. **
  9650. ** When an in-memory Table object is deleted (for example when the
  9651. ** schema is being reloaded for some reason), the VTable objects are not
  9652. ** deleted and the sqlite3_vtab* handles are not xDisconnect()ed
  9653. ** immediately. Instead, they are moved from the Table.pVTable list to
  9654. ** another linked list headed by the sqlite3.pDisconnect member of the
  9655. ** corresponding sqlite3 structure. They are then deleted/xDisconnected
  9656. ** next time a statement is prepared using said sqlite3*. This is done
  9657. ** to avoid deadlock issues involving multiple sqlite3.mutex mutexes.
  9658. ** Refer to comments above function sqlite3VtabUnlockList() for an
  9659. ** explanation as to why it is safe to add an entry to an sqlite3.pDisconnect
  9660. ** list without holding the corresponding sqlite3.mutex mutex.
  9661. **
  9662. ** The memory for objects of this type is always allocated by
  9663. ** sqlite3DbMalloc(), using the connection handle stored in VTable.db as
  9664. ** the first argument.
  9665. */
  9666. struct VTable {
  9667. sqlite3 *db; /* Database connection associated with this table */
  9668. Module *pMod; /* Pointer to module implementation */
  9669. sqlite3_vtab *pVtab; /* Pointer to vtab instance */
  9670. int nRef; /* Number of pointers to this structure */
  9671. u8 bConstraint; /* True if constraints are supported */
  9672. int iSavepoint; /* Depth of the SAVEPOINT stack */
  9673. VTable *pNext; /* Next in linked list (see above) */
  9674. };
  9675. /*
  9676. ** Each SQL table is represented in memory by an instance of the
  9677. ** following structure.
  9678. **
  9679. ** Table.zName is the name of the table. The case of the original
  9680. ** CREATE TABLE statement is stored, but case is not significant for
  9681. ** comparisons.
  9682. **
  9683. ** Table.nCol is the number of columns in this table. Table.aCol is a
  9684. ** pointer to an array of Column structures, one for each column.
  9685. **
  9686. ** If the table has an INTEGER PRIMARY KEY, then Table.iPKey is the index of
  9687. ** the column that is that key. Otherwise Table.iPKey is negative. Note
  9688. ** that the datatype of the PRIMARY KEY must be INTEGER for this field to
  9689. ** be set. An INTEGER PRIMARY KEY is used as the rowid for each row of
  9690. ** the table. If a table has no INTEGER PRIMARY KEY, then a random rowid
  9691. ** is generated for each row of the table. TF_HasPrimaryKey is set if
  9692. ** the table has any PRIMARY KEY, INTEGER or otherwise.
  9693. **
  9694. ** Table.tnum is the page number for the root BTree page of the table in the
  9695. ** database file. If Table.iDb is the index of the database table backend
  9696. ** in sqlite.aDb[]. 0 is for the main database and 1 is for the file that
  9697. ** holds temporary tables and indices. If TF_Ephemeral is set
  9698. ** then the table is stored in a file that is automatically deleted
  9699. ** when the VDBE cursor to the table is closed. In this case Table.tnum
  9700. ** refers VDBE cursor number that holds the table open, not to the root
  9701. ** page number. Transient tables are used to hold the results of a
  9702. ** sub-query that appears instead of a real table name in the FROM clause
  9703. ** of a SELECT statement.
  9704. */
  9705. struct Table {
  9706. char *zName; /* Name of the table or view */
  9707. int iPKey; /* If not negative, use aCol[iPKey] as the primary key */
  9708. int nCol; /* Number of columns in this table */
  9709. Column *aCol; /* Information about each column */
  9710. Index *pIndex; /* List of SQL indexes on this table. */
  9711. int tnum; /* Root BTree node for this table (see note above) */
  9712. tRowcnt nRowEst; /* Estimated rows in table - from sqlite_stat1 table */
  9713. Select *pSelect; /* NULL for tables. Points to definition if a view. */
  9714. u16 nRef; /* Number of pointers to this Table */
  9715. u8 tabFlags; /* Mask of TF_* values */
  9716. u8 keyConf; /* What to do in case of uniqueness conflict on iPKey */
  9717. FKey *pFKey; /* Linked list of all foreign keys in this table */
  9718. char *zColAff; /* String defining the affinity of each column */
  9719. #ifndef SQLITE_OMIT_CHECK
  9720. Expr *pCheck; /* The AND of all CHECK constraints */
  9721. #endif
  9722. #ifndef SQLITE_OMIT_ALTERTABLE
  9723. int addColOffset; /* Offset in CREATE TABLE stmt to add a new column */
  9724. #endif
  9725. #ifndef SQLITE_OMIT_VIRTUALTABLE
  9726. VTable *pVTable; /* List of VTable objects. */
  9727. int nModuleArg; /* Number of arguments to the module */
  9728. char **azModuleArg; /* Text of all module args. [0] is module name */
  9729. #endif
  9730. Trigger *pTrigger; /* List of triggers stored in pSchema */
  9731. Schema *pSchema; /* Schema that contains this table */
  9732. Table *pNextZombie; /* Next on the Parse.pZombieTab list */
  9733. };
  9734. /*
  9735. ** Allowed values for Tabe.tabFlags.
  9736. */
  9737. #define TF_Readonly 0x01 /* Read-only system table */
  9738. #define TF_Ephemeral 0x02 /* An ephemeral table */
  9739. #define TF_HasPrimaryKey 0x04 /* Table has a primary key */
  9740. #define TF_Autoincrement 0x08 /* Integer primary key is autoincrement */
  9741. #define TF_Virtual 0x10 /* Is a virtual table */
  9742. #define TF_NeedMetadata 0x20 /* aCol[].zType and aCol[].pColl missing */
  9743. /*
  9744. ** Test to see whether or not a table is a virtual table. This is
  9745. ** done as a macro so that it will be optimized out when virtual
  9746. ** table support is omitted from the build.
  9747. */
  9748. #ifndef SQLITE_OMIT_VIRTUALTABLE
  9749. # define IsVirtual(X) (((X)->tabFlags & TF_Virtual)!=0)
  9750. # define IsHiddenColumn(X) ((X)->isHidden)
  9751. #else
  9752. # define IsVirtual(X) 0
  9753. # define IsHiddenColumn(X) 0
  9754. #endif
  9755. /*
  9756. ** Each foreign key constraint is an instance of the following structure.
  9757. **
  9758. ** A foreign key is associated with two tables. The "from" table is
  9759. ** the table that contains the REFERENCES clause that creates the foreign
  9760. ** key. The "to" table is the table that is named in the REFERENCES clause.
  9761. ** Consider this example:
  9762. **
  9763. ** CREATE TABLE ex1(
  9764. ** a INTEGER PRIMARY KEY,
  9765. ** b INTEGER CONSTRAINT fk1 REFERENCES ex2(x)
  9766. ** );
  9767. **
  9768. ** For foreign key "fk1", the from-table is "ex1" and the to-table is "ex2".
  9769. **
  9770. ** Each REFERENCES clause generates an instance of the following structure
  9771. ** which is attached to the from-table. The to-table need not exist when
  9772. ** the from-table is created. The existence of the to-table is not checked.
  9773. */
  9774. struct FKey {
  9775. Table *pFrom; /* Table containing the REFERENCES clause (aka: Child) */
  9776. FKey *pNextFrom; /* Next foreign key in pFrom */
  9777. char *zTo; /* Name of table that the key points to (aka: Parent) */
  9778. FKey *pNextTo; /* Next foreign key on table named zTo */
  9779. FKey *pPrevTo; /* Previous foreign key on table named zTo */
  9780. int nCol; /* Number of columns in this key */
  9781. /* EV: R-30323-21917 */
  9782. u8 isDeferred; /* True if constraint checking is deferred till COMMIT */
  9783. u8 aAction[2]; /* ON DELETE and ON UPDATE actions, respectively */
  9784. Trigger *apTrigger[2]; /* Triggers for aAction[] actions */
  9785. struct sColMap { /* Mapping of columns in pFrom to columns in zTo */
  9786. int iFrom; /* Index of column in pFrom */
  9787. char *zCol; /* Name of column in zTo. If 0 use PRIMARY KEY */
  9788. } aCol[1]; /* One entry for each of nCol column s */
  9789. };
  9790. /*
  9791. ** SQLite supports many different ways to resolve a constraint
  9792. ** error. ROLLBACK processing means that a constraint violation
  9793. ** causes the operation in process to fail and for the current transaction
  9794. ** to be rolled back. ABORT processing means the operation in process
  9795. ** fails and any prior changes from that one operation are backed out,
  9796. ** but the transaction is not rolled back. FAIL processing means that
  9797. ** the operation in progress stops and returns an error code. But prior
  9798. ** changes due to the same operation are not backed out and no rollback
  9799. ** occurs. IGNORE means that the particular row that caused the constraint
  9800. ** error is not inserted or updated. Processing continues and no error
  9801. ** is returned. REPLACE means that preexisting database rows that caused
  9802. ** a UNIQUE constraint violation are removed so that the new insert or
  9803. ** update can proceed. Processing continues and no error is reported.
  9804. **
  9805. ** RESTRICT, SETNULL, and CASCADE actions apply only to foreign keys.
  9806. ** RESTRICT is the same as ABORT for IMMEDIATE foreign keys and the
  9807. ** same as ROLLBACK for DEFERRED keys. SETNULL means that the foreign
  9808. ** key is set to NULL. CASCADE means that a DELETE or UPDATE of the
  9809. ** referenced table row is propagated into the row that holds the
  9810. ** foreign key.
  9811. **
  9812. ** The following symbolic values are used to record which type
  9813. ** of action to take.
  9814. */
  9815. #define OE_None 0 /* There is no constraint to check */
  9816. #define OE_Rollback 1 /* Fail the operation and rollback the transaction */
  9817. #define OE_Abort 2 /* Back out changes but do no rollback transaction */
  9818. #define OE_Fail 3 /* Stop the operation but leave all prior changes */
  9819. #define OE_Ignore 4 /* Ignore the error. Do not do the INSERT or UPDATE */
  9820. #define OE_Replace 5 /* Delete existing record, then do INSERT or UPDATE */
  9821. #define OE_Restrict 6 /* OE_Abort for IMMEDIATE, OE_Rollback for DEFERRED */
  9822. #define OE_SetNull 7 /* Set the foreign key value to NULL */
  9823. #define OE_SetDflt 8 /* Set the foreign key value to its default */
  9824. #define OE_Cascade 9 /* Cascade the changes */
  9825. #define OE_Default 99 /* Do whatever the default action is */
  9826. /*
  9827. ** An instance of the following structure is passed as the first
  9828. ** argument to sqlite3VdbeKeyCompare and is used to control the
  9829. ** comparison of the two index keys.
  9830. */
  9831. struct KeyInfo {
  9832. sqlite3 *db; /* The database connection */
  9833. u8 enc; /* Text encoding - one of the SQLITE_UTF* values */
  9834. u16 nField; /* Number of entries in aColl[] */
  9835. u8 *aSortOrder; /* Sort order for each column. May be NULL */
  9836. CollSeq *aColl[1]; /* Collating sequence for each term of the key */
  9837. };
  9838. /*
  9839. ** An instance of the following structure holds information about a
  9840. ** single index record that has already been parsed out into individual
  9841. ** values.
  9842. **
  9843. ** A record is an object that contains one or more fields of data.
  9844. ** Records are used to store the content of a table row and to store
  9845. ** the key of an index. A blob encoding of a record is created by
  9846. ** the OP_MakeRecord opcode of the VDBE and is disassembled by the
  9847. ** OP_Column opcode.
  9848. **
  9849. ** This structure holds a record that has already been disassembled
  9850. ** into its constituent fields.
  9851. */
  9852. struct UnpackedRecord {
  9853. KeyInfo *pKeyInfo; /* Collation and sort-order information */
  9854. u16 nField; /* Number of entries in apMem[] */
  9855. u8 flags; /* Boolean settings. UNPACKED_... below */
  9856. i64 rowid; /* Used by UNPACKED_PREFIX_SEARCH */
  9857. Mem *aMem; /* Values */
  9858. };
  9859. /*
  9860. ** Allowed values of UnpackedRecord.flags
  9861. */
  9862. #define UNPACKED_INCRKEY 0x01 /* Make this key an epsilon larger */
  9863. #define UNPACKED_PREFIX_MATCH 0x02 /* A prefix match is considered OK */
  9864. #define UNPACKED_PREFIX_SEARCH 0x04 /* Ignore final (rowid) field */
  9865. /*
  9866. ** Each SQL index is represented in memory by an
  9867. ** instance of the following structure.
  9868. **
  9869. ** The columns of the table that are to be indexed are described
  9870. ** by the aiColumn[] field of this structure. For example, suppose
  9871. ** we have the following table and index:
  9872. **
  9873. ** CREATE TABLE Ex1(c1 int, c2 int, c3 text);
  9874. ** CREATE INDEX Ex2 ON Ex1(c3,c1);
  9875. **
  9876. ** In the Table structure describing Ex1, nCol==3 because there are
  9877. ** three columns in the table. In the Index structure describing
  9878. ** Ex2, nColumn==2 since 2 of the 3 columns of Ex1 are indexed.
  9879. ** The value of aiColumn is {2, 0}. aiColumn[0]==2 because the
  9880. ** first column to be indexed (c3) has an index of 2 in Ex1.aCol[].
  9881. ** The second column to be indexed (c1) has an index of 0 in
  9882. ** Ex1.aCol[], hence Ex2.aiColumn[1]==0.
  9883. **
  9884. ** The Index.onError field determines whether or not the indexed columns
  9885. ** must be unique and what to do if they are not. When Index.onError=OE_None,
  9886. ** it means this is not a unique index. Otherwise it is a unique index
  9887. ** and the value of Index.onError indicate the which conflict resolution
  9888. ** algorithm to employ whenever an attempt is made to insert a non-unique
  9889. ** element.
  9890. */
  9891. struct Index {
  9892. char *zName; /* Name of this index */
  9893. int nColumn; /* Number of columns in the table used by this index */
  9894. int *aiColumn; /* Which columns are used by this index. 1st is 0 */
  9895. tRowcnt *aiRowEst; /* Result of ANALYZE: Est. rows selected by each column */
  9896. Table *pTable; /* The SQL table being indexed */
  9897. int tnum; /* Page containing root of this index in database file */
  9898. u8 onError; /* OE_Abort, OE_Ignore, OE_Replace, or OE_None */
  9899. u8 autoIndex; /* True if is automatically created (ex: by UNIQUE) */
  9900. u8 bUnordered; /* Use this index for == or IN queries only */
  9901. char *zColAff; /* String defining the affinity of each column */
  9902. Index *pNext; /* The next index associated with the same table */
  9903. Schema *pSchema; /* Schema containing this index */
  9904. u8 *aSortOrder; /* Array of size Index.nColumn. True==DESC, False==ASC */
  9905. char **azColl; /* Array of collation sequence names for index */
  9906. #ifdef SQLITE_ENABLE_STAT3
  9907. int nSample; /* Number of elements in aSample[] */
  9908. tRowcnt avgEq; /* Average nEq value for key values not in aSample */
  9909. IndexSample *aSample; /* Samples of the left-most key */
  9910. #endif
  9911. };
  9912. /*
  9913. ** Each sample stored in the sqlite_stat3 table is represented in memory
  9914. ** using a structure of this type. See documentation at the top of the
  9915. ** analyze.c source file for additional information.
  9916. */
  9917. struct IndexSample {
  9918. union {
  9919. char *z; /* Value if eType is SQLITE_TEXT or SQLITE_BLOB */
  9920. double r; /* Value if eType is SQLITE_FLOAT */
  9921. i64 i; /* Value if eType is SQLITE_INTEGER */
  9922. } u;
  9923. u8 eType; /* SQLITE_NULL, SQLITE_INTEGER ... etc. */
  9924. int nByte; /* Size in byte of text or blob. */
  9925. tRowcnt nEq; /* Est. number of rows where the key equals this sample */
  9926. tRowcnt nLt; /* Est. number of rows where key is less than this sample */
  9927. tRowcnt nDLt; /* Est. number of distinct keys less than this sample */
  9928. };
  9929. /*
  9930. ** Each token coming out of the lexer is an instance of
  9931. ** this structure. Tokens are also used as part of an expression.
  9932. **
  9933. ** Note if Token.z==0 then Token.dyn and Token.n are undefined and
  9934. ** may contain random values. Do not make any assumptions about Token.dyn
  9935. ** and Token.n when Token.z==0.
  9936. */
  9937. struct Token {
  9938. const char *z; /* Text of the token. Not NULL-terminated! */
  9939. unsigned int n; /* Number of characters in this token */
  9940. };
  9941. /*
  9942. ** An instance of this structure contains information needed to generate
  9943. ** code for a SELECT that contains aggregate functions.
  9944. **
  9945. ** If Expr.op==TK_AGG_COLUMN or TK_AGG_FUNCTION then Expr.pAggInfo is a
  9946. ** pointer to this structure. The Expr.iColumn field is the index in
  9947. ** AggInfo.aCol[] or AggInfo.aFunc[] of information needed to generate
  9948. ** code for that node.
  9949. **
  9950. ** AggInfo.pGroupBy and AggInfo.aFunc.pExpr point to fields within the
  9951. ** original Select structure that describes the SELECT statement. These
  9952. ** fields do not need to be freed when deallocating the AggInfo structure.
  9953. */
  9954. struct AggInfo {
  9955. u8 directMode; /* Direct rendering mode means take data directly
  9956. ** from source tables rather than from accumulators */
  9957. u8 useSortingIdx; /* In direct mode, reference the sorting index rather
  9958. ** than the source table */
  9959. int sortingIdx; /* Cursor number of the sorting index */
  9960. int sortingIdxPTab; /* Cursor number of pseudo-table */
  9961. ExprList *pGroupBy; /* The group by clause */
  9962. int nSortingColumn; /* Number of columns in the sorting index */
  9963. struct AggInfo_col { /* For each column used in source tables */
  9964. Table *pTab; /* Source table */
  9965. int iTable; /* Cursor number of the source table */
  9966. int iColumn; /* Column number within the source table */
  9967. int iSorterColumn; /* Column number in the sorting index */
  9968. int iMem; /* Memory location that acts as accumulator */
  9969. Expr *pExpr; /* The original expression */
  9970. } *aCol;
  9971. int nColumn; /* Number of used entries in aCol[] */
  9972. int nColumnAlloc; /* Number of slots allocated for aCol[] */
  9973. int nAccumulator; /* Number of columns that show through to the output.
  9974. ** Additional columns are used only as parameters to
  9975. ** aggregate functions */
  9976. struct AggInfo_func { /* For each aggregate function */
  9977. Expr *pExpr; /* Expression encoding the function */
  9978. FuncDef *pFunc; /* The aggregate function implementation */
  9979. int iMem; /* Memory location that acts as accumulator */
  9980. int iDistinct; /* Ephemeral table used to enforce DISTINCT */
  9981. } *aFunc;
  9982. int nFunc; /* Number of entries in aFunc[] */
  9983. int nFuncAlloc; /* Number of slots allocated for aFunc[] */
  9984. };
  9985. /*
  9986. ** The datatype ynVar is a signed integer, either 16-bit or 32-bit.
  9987. ** Usually it is 16-bits. But if SQLITE_MAX_VARIABLE_NUMBER is greater
  9988. ** than 32767 we have to make it 32-bit. 16-bit is preferred because
  9989. ** it uses less memory in the Expr object, which is a big memory user
  9990. ** in systems with lots of prepared statements. And few applications
  9991. ** need more than about 10 or 20 variables. But some extreme users want
  9992. ** to have prepared statements with over 32767 variables, and for them
  9993. ** the option is available (at compile-time).
  9994. */
  9995. #if SQLITE_MAX_VARIABLE_NUMBER<=32767
  9996. typedef i16 ynVar;
  9997. #else
  9998. typedef int ynVar;
  9999. #endif
  10000. /*
  10001. ** Each node of an expression in the parse tree is an instance
  10002. ** of this structure.
  10003. **
  10004. ** Expr.op is the opcode. The integer parser token codes are reused
  10005. ** as opcodes here. For example, the parser defines TK_GE to be an integer
  10006. ** code representing the ">=" operator. This same integer code is reused
  10007. ** to represent the greater-than-or-equal-to operator in the expression
  10008. ** tree.
  10009. **
  10010. ** If the expression is an SQL literal (TK_INTEGER, TK_FLOAT, TK_BLOB,
  10011. ** or TK_STRING), then Expr.token contains the text of the SQL literal. If
  10012. ** the expression is a variable (TK_VARIABLE), then Expr.token contains the
  10013. ** variable name. Finally, if the expression is an SQL function (TK_FUNCTION),
  10014. ** then Expr.token contains the name of the function.
  10015. **
  10016. ** Expr.pRight and Expr.pLeft are the left and right subexpressions of a
  10017. ** binary operator. Either or both may be NULL.
  10018. **
  10019. ** Expr.x.pList is a list of arguments if the expression is an SQL function,
  10020. ** a CASE expression or an IN expression of the form "<lhs> IN (<y>, <z>...)".
  10021. ** Expr.x.pSelect is used if the expression is a sub-select or an expression of
  10022. ** the form "<lhs> IN (SELECT ...)". If the EP_xIsSelect bit is set in the
  10023. ** Expr.flags mask, then Expr.x.pSelect is valid. Otherwise, Expr.x.pList is
  10024. ** valid.
  10025. **
  10026. ** An expression of the form ID or ID.ID refers to a column in a table.
  10027. ** For such expressions, Expr.op is set to TK_COLUMN and Expr.iTable is
  10028. ** the integer cursor number of a VDBE cursor pointing to that table and
  10029. ** Expr.iColumn is the column number for the specific column. If the
  10030. ** expression is used as a result in an aggregate SELECT, then the
  10031. ** value is also stored in the Expr.iAgg column in the aggregate so that
  10032. ** it can be accessed after all aggregates are computed.
  10033. **
  10034. ** If the expression is an unbound variable marker (a question mark
  10035. ** character '?' in the original SQL) then the Expr.iTable holds the index
  10036. ** number for that variable.
  10037. **
  10038. ** If the expression is a subquery then Expr.iColumn holds an integer
  10039. ** register number containing the result of the subquery. If the
  10040. ** subquery gives a constant result, then iTable is -1. If the subquery
  10041. ** gives a different answer at different times during statement processing
  10042. ** then iTable is the address of a subroutine that computes the subquery.
  10043. **
  10044. ** If the Expr is of type OP_Column, and the table it is selecting from
  10045. ** is a disk table or the "old.*" pseudo-table, then pTab points to the
  10046. ** corresponding table definition.
  10047. **
  10048. ** ALLOCATION NOTES:
  10049. **
  10050. ** Expr objects can use a lot of memory space in database schema. To
  10051. ** help reduce memory requirements, sometimes an Expr object will be
  10052. ** truncated. And to reduce the number of memory allocations, sometimes
  10053. ** two or more Expr objects will be stored in a single memory allocation,
  10054. ** together with Expr.zToken strings.
  10055. **
  10056. ** If the EP_Reduced and EP_TokenOnly flags are set when
  10057. ** an Expr object is truncated. When EP_Reduced is set, then all
  10058. ** the child Expr objects in the Expr.pLeft and Expr.pRight subtrees
  10059. ** are contained within the same memory allocation. Note, however, that
  10060. ** the subtrees in Expr.x.pList or Expr.x.pSelect are always separately
  10061. ** allocated, regardless of whether or not EP_Reduced is set.
  10062. */
  10063. struct Expr {
  10064. u8 op; /* Operation performed by this node */
  10065. char affinity; /* The affinity of the column or 0 if not a column */
  10066. u16 flags; /* Various flags. EP_* See below */
  10067. union {
  10068. char *zToken; /* Token value. Zero terminated and dequoted */
  10069. int iValue; /* Non-negative integer value if EP_IntValue */
  10070. } u;
  10071. /* If the EP_TokenOnly flag is set in the Expr.flags mask, then no
  10072. ** space is allocated for the fields below this point. An attempt to
  10073. ** access them will result in a segfault or malfunction.
  10074. *********************************************************************/
  10075. Expr *pLeft; /* Left subnode */
  10076. Expr *pRight; /* Right subnode */
  10077. union {
  10078. ExprList *pList; /* Function arguments or in "<expr> IN (<expr-list)" */
  10079. Select *pSelect; /* Used for sub-selects and "<expr> IN (<select>)" */
  10080. } x;
  10081. CollSeq *pColl; /* The collation type of the column or 0 */
  10082. /* If the EP_Reduced flag is set in the Expr.flags mask, then no
  10083. ** space is allocated for the fields below this point. An attempt to
  10084. ** access them will result in a segfault or malfunction.
  10085. *********************************************************************/
  10086. int iTable; /* TK_COLUMN: cursor number of table holding column
  10087. ** TK_REGISTER: register number
  10088. ** TK_TRIGGER: 1 -> new, 0 -> old */
  10089. ynVar iColumn; /* TK_COLUMN: column index. -1 for rowid.
  10090. ** TK_VARIABLE: variable number (always >= 1). */
  10091. i16 iAgg; /* Which entry in pAggInfo->aCol[] or ->aFunc[] */
  10092. i16 iRightJoinTable; /* If EP_FromJoin, the right table of the join */
  10093. u8 flags2; /* Second set of flags. EP2_... */
  10094. u8 op2; /* If a TK_REGISTER, the original value of Expr.op */
  10095. AggInfo *pAggInfo; /* Used by TK_AGG_COLUMN and TK_AGG_FUNCTION */
  10096. Table *pTab; /* Table for TK_COLUMN expressions. */
  10097. #if SQLITE_MAX_EXPR_DEPTH>0
  10098. int nHeight; /* Height of the tree headed by this node */
  10099. #endif
  10100. };
  10101. /*
  10102. ** The following are the meanings of bits in the Expr.flags field.
  10103. */
  10104. #define EP_FromJoin 0x0001 /* Originated in ON or USING clause of a join */
  10105. #define EP_Agg 0x0002 /* Contains one or more aggregate functions */
  10106. #define EP_Resolved 0x0004 /* IDs have been resolved to COLUMNs */
  10107. #define EP_Error 0x0008 /* Expression contains one or more errors */
  10108. #define EP_Distinct 0x0010 /* Aggregate function with DISTINCT keyword */
  10109. #define EP_VarSelect 0x0020 /* pSelect is correlated, not constant */
  10110. #define EP_DblQuoted 0x0040 /* token.z was originally in "..." */
  10111. #define EP_InfixFunc 0x0080 /* True for an infix function: LIKE, GLOB, etc */
  10112. #define EP_ExpCollate 0x0100 /* Collating sequence specified explicitly */
  10113. #define EP_FixedDest 0x0200 /* Result needed in a specific register */
  10114. #define EP_IntValue 0x0400 /* Integer value contained in u.iValue */
  10115. #define EP_xIsSelect 0x0800 /* x.pSelect is valid (otherwise x.pList is) */
  10116. #define EP_Hint 0x1000 /* Optimizer hint. Not required for correctness */
  10117. #define EP_Reduced 0x2000 /* Expr struct is EXPR_REDUCEDSIZE bytes only */
  10118. #define EP_TokenOnly 0x4000 /* Expr struct is EXPR_TOKENONLYSIZE bytes only */
  10119. #define EP_Static 0x8000 /* Held in memory not obtained from malloc() */
  10120. /*
  10121. ** The following are the meanings of bits in the Expr.flags2 field.
  10122. */
  10123. #define EP2_MallocedToken 0x0001 /* Need to sqlite3DbFree() Expr.zToken */
  10124. #define EP2_Irreducible 0x0002 /* Cannot EXPRDUP_REDUCE this Expr */
  10125. /*
  10126. ** The pseudo-routine sqlite3ExprSetIrreducible sets the EP2_Irreducible
  10127. ** flag on an expression structure. This flag is used for VV&A only. The
  10128. ** routine is implemented as a macro that only works when in debugging mode,
  10129. ** so as not to burden production code.
  10130. */
  10131. #ifdef SQLITE_DEBUG
  10132. # define ExprSetIrreducible(X) (X)->flags2 |= EP2_Irreducible
  10133. #else
  10134. # define ExprSetIrreducible(X)
  10135. #endif
  10136. /*
  10137. ** These macros can be used to test, set, or clear bits in the
  10138. ** Expr.flags field.
  10139. */
  10140. #define ExprHasProperty(E,P) (((E)->flags&(P))==(P))
  10141. #define ExprHasAnyProperty(E,P) (((E)->flags&(P))!=0)
  10142. #define ExprSetProperty(E,P) (E)->flags|=(P)
  10143. #define ExprClearProperty(E,P) (E)->flags&=~(P)
  10144. /*
  10145. ** Macros to determine the number of bytes required by a normal Expr
  10146. ** struct, an Expr struct with the EP_Reduced flag set in Expr.flags
  10147. ** and an Expr struct with the EP_TokenOnly flag set.
  10148. */
  10149. #define EXPR_FULLSIZE sizeof(Expr) /* Full size */
  10150. #define EXPR_REDUCEDSIZE offsetof(Expr,iTable) /* Common features */
  10151. #define EXPR_TOKENONLYSIZE offsetof(Expr,pLeft) /* Fewer features */
  10152. /*
  10153. ** Flags passed to the sqlite3ExprDup() function. See the header comment
  10154. ** above sqlite3ExprDup() for details.
  10155. */
  10156. #define EXPRDUP_REDUCE 0x0001 /* Used reduced-size Expr nodes */
  10157. /*
  10158. ** A list of expressions. Each expression may optionally have a
  10159. ** name. An expr/name combination can be used in several ways, such
  10160. ** as the list of "expr AS ID" fields following a "SELECT" or in the
  10161. ** list of "ID = expr" items in an UPDATE. A list of expressions can
  10162. ** also be used as the argument to a function, in which case the a.zName
  10163. ** field is not used.
  10164. */
  10165. struct ExprList {
  10166. int nExpr; /* Number of expressions on the list */
  10167. int nAlloc; /* Number of entries allocated below */
  10168. int iECursor; /* VDBE Cursor associated with this ExprList */
  10169. struct ExprList_item {
  10170. Expr *pExpr; /* The list of expressions */
  10171. char *zName; /* Token associated with this expression */
  10172. char *zSpan; /* Original text of the expression */
  10173. u8 sortOrder; /* 1 for DESC or 0 for ASC */
  10174. u8 done; /* A flag to indicate when processing is finished */
  10175. u16 iOrderByCol; /* For ORDER BY, column number in result set */
  10176. u16 iAlias; /* Index into Parse.aAlias[] for zName */
  10177. } *a; /* One entry for each expression */
  10178. };
  10179. /*
  10180. ** An instance of this structure is used by the parser to record both
  10181. ** the parse tree for an expression and the span of input text for an
  10182. ** expression.
  10183. */
  10184. struct ExprSpan {
  10185. Expr *pExpr; /* The expression parse tree */
  10186. const char *zStart; /* First character of input text */
  10187. const char *zEnd; /* One character past the end of input text */
  10188. };
  10189. /*
  10190. ** An instance of this structure can hold a simple list of identifiers,
  10191. ** such as the list "a,b,c" in the following statements:
  10192. **
  10193. ** INSERT INTO t(a,b,c) VALUES ...;
  10194. ** CREATE INDEX idx ON t(a,b,c);
  10195. ** CREATE TRIGGER trig BEFORE UPDATE ON t(a,b,c) ...;
  10196. **
  10197. ** The IdList.a.idx field is used when the IdList represents the list of
  10198. ** column names after a table name in an INSERT statement. In the statement
  10199. **
  10200. ** INSERT INTO t(a,b,c) ...
  10201. **
  10202. ** If "a" is the k-th column of table "t", then IdList.a[0].idx==k.
  10203. */
  10204. struct IdList {
  10205. struct IdList_item {
  10206. char *zName; /* Name of the identifier */
  10207. int idx; /* Index in some Table.aCol[] of a column named zName */
  10208. } *a;
  10209. int nId; /* Number of identifiers on the list */
  10210. int nAlloc; /* Number of entries allocated for a[] below */
  10211. };
  10212. /*
  10213. ** The bitmask datatype defined below is used for various optimizations.
  10214. **
  10215. ** Changing this from a 64-bit to a 32-bit type limits the number of
  10216. ** tables in a join to 32 instead of 64. But it also reduces the size
  10217. ** of the library by 738 bytes on ix86.
  10218. */
  10219. typedef u64 Bitmask;
  10220. /*
  10221. ** The number of bits in a Bitmask. "BMS" means "BitMask Size".
  10222. */
  10223. #define BMS ((int)(sizeof(Bitmask)*8))
  10224. /*
  10225. ** The following structure describes the FROM clause of a SELECT statement.
  10226. ** Each table or subquery in the FROM clause is a separate element of
  10227. ** the SrcList.a[] array.
  10228. **
  10229. ** With the addition of multiple database support, the following structure
  10230. ** can also be used to describe a particular table such as the table that
  10231. ** is modified by an INSERT, DELETE, or UPDATE statement. In standard SQL,
  10232. ** such a table must be a simple name: ID. But in SQLite, the table can
  10233. ** now be identified by a database name, a dot, then the table name: ID.ID.
  10234. **
  10235. ** The jointype starts out showing the join type between the current table
  10236. ** and the next table on the list. The parser builds the list this way.
  10237. ** But sqlite3SrcListShiftJoinType() later shifts the jointypes so that each
  10238. ** jointype expresses the join between the table and the previous table.
  10239. **
  10240. ** In the colUsed field, the high-order bit (bit 63) is set if the table
  10241. ** contains more than 63 columns and the 64-th or later column is used.
  10242. */
  10243. struct SrcList {
  10244. i16 nSrc; /* Number of tables or subqueries in the FROM clause */
  10245. i16 nAlloc; /* Number of entries allocated in a[] below */
  10246. struct SrcList_item {
  10247. char *zDatabase; /* Name of database holding this table */
  10248. char *zName; /* Name of the table */
  10249. char *zAlias; /* The "B" part of a "A AS B" phrase. zName is the "A" */
  10250. Table *pTab; /* An SQL table corresponding to zName */
  10251. Select *pSelect; /* A SELECT statement used in place of a table name */
  10252. int addrFillSub; /* Address of subroutine to manifest a subquery */
  10253. int regReturn; /* Register holding return address of addrFillSub */
  10254. u8 jointype; /* Type of join between this able and the previous */
  10255. u8 notIndexed; /* True if there is a NOT INDEXED clause */
  10256. u8 isCorrelated; /* True if sub-query is correlated */
  10257. #ifndef SQLITE_OMIT_EXPLAIN
  10258. u8 iSelectId; /* If pSelect!=0, the id of the sub-select in EQP */
  10259. #endif
  10260. int iCursor; /* The VDBE cursor number used to access this table */
  10261. Expr *pOn; /* The ON clause of a join */
  10262. IdList *pUsing; /* The USING clause of a join */
  10263. Bitmask colUsed; /* Bit N (1<<N) set if column N of pTab is used */
  10264. char *zIndex; /* Identifier from "INDEXED BY <zIndex>" clause */
  10265. Index *pIndex; /* Index structure corresponding to zIndex, if any */
  10266. } a[1]; /* One entry for each identifier on the list */
  10267. };
  10268. /*
  10269. ** Permitted values of the SrcList.a.jointype field
  10270. */
  10271. #define JT_INNER 0x0001 /* Any kind of inner or cross join */
  10272. #define JT_CROSS 0x0002 /* Explicit use of the CROSS keyword */
  10273. #define JT_NATURAL 0x0004 /* True for a "natural" join */
  10274. #define JT_LEFT 0x0008 /* Left outer join */
  10275. #define JT_RIGHT 0x0010 /* Right outer join */
  10276. #define JT_OUTER 0x0020 /* The "OUTER" keyword is present */
  10277. #define JT_ERROR 0x0040 /* unknown or unsupported join type */
  10278. /*
  10279. ** A WherePlan object holds information that describes a lookup
  10280. ** strategy.
  10281. **
  10282. ** This object is intended to be opaque outside of the where.c module.
  10283. ** It is included here only so that that compiler will know how big it
  10284. ** is. None of the fields in this object should be used outside of
  10285. ** the where.c module.
  10286. **
  10287. ** Within the union, pIdx is only used when wsFlags&WHERE_INDEXED is true.
  10288. ** pTerm is only used when wsFlags&WHERE_MULTI_OR is true. And pVtabIdx
  10289. ** is only used when wsFlags&WHERE_VIRTUALTABLE is true. It is never the
  10290. ** case that more than one of these conditions is true.
  10291. */
  10292. struct WherePlan {
  10293. u32 wsFlags; /* WHERE_* flags that describe the strategy */
  10294. u32 nEq; /* Number of == constraints */
  10295. double nRow; /* Estimated number of rows (for EQP) */
  10296. union {
  10297. Index *pIdx; /* Index when WHERE_INDEXED is true */
  10298. struct WhereTerm *pTerm; /* WHERE clause term for OR-search */
  10299. sqlite3_index_info *pVtabIdx; /* Virtual table index to use */
  10300. } u;
  10301. };
  10302. /*
  10303. ** For each nested loop in a WHERE clause implementation, the WhereInfo
  10304. ** structure contains a single instance of this structure. This structure
  10305. ** is intended to be private the the where.c module and should not be
  10306. ** access or modified by other modules.
  10307. **
  10308. ** The pIdxInfo field is used to help pick the best index on a
  10309. ** virtual table. The pIdxInfo pointer contains indexing
  10310. ** information for the i-th table in the FROM clause before reordering.
  10311. ** All the pIdxInfo pointers are freed by whereInfoFree() in where.c.
  10312. ** All other information in the i-th WhereLevel object for the i-th table
  10313. ** after FROM clause ordering.
  10314. */
  10315. struct WhereLevel {
  10316. WherePlan plan; /* query plan for this element of the FROM clause */
  10317. int iLeftJoin; /* Memory cell used to implement LEFT OUTER JOIN */
  10318. int iTabCur; /* The VDBE cursor used to access the table */
  10319. int iIdxCur; /* The VDBE cursor used to access pIdx */
  10320. int addrBrk; /* Jump here to break out of the loop */
  10321. int addrNxt; /* Jump here to start the next IN combination */
  10322. int addrCont; /* Jump here to continue with the next loop cycle */
  10323. int addrFirst; /* First instruction of interior of the loop */
  10324. u8 iFrom; /* Which entry in the FROM clause */
  10325. u8 op, p5; /* Opcode and P5 of the opcode that ends the loop */
  10326. int p1, p2; /* Operands of the opcode used to ends the loop */
  10327. union { /* Information that depends on plan.wsFlags */
  10328. struct {
  10329. int nIn; /* Number of entries in aInLoop[] */
  10330. struct InLoop {
  10331. int iCur; /* The VDBE cursor used by this IN operator */
  10332. int addrInTop; /* Top of the IN loop */
  10333. } *aInLoop; /* Information about each nested IN operator */
  10334. } in; /* Used when plan.wsFlags&WHERE_IN_ABLE */
  10335. } u;
  10336. /* The following field is really not part of the current level. But
  10337. ** we need a place to cache virtual table index information for each
  10338. ** virtual table in the FROM clause and the WhereLevel structure is
  10339. ** a convenient place since there is one WhereLevel for each FROM clause
  10340. ** element.
  10341. */
  10342. sqlite3_index_info *pIdxInfo; /* Index info for n-th source table */
  10343. };
  10344. /*
  10345. ** Flags appropriate for the wctrlFlags parameter of sqlite3WhereBegin()
  10346. ** and the WhereInfo.wctrlFlags member.
  10347. */
  10348. #define WHERE_ORDERBY_NORMAL 0x0000 /* No-op */
  10349. #define WHERE_ORDERBY_MIN 0x0001 /* ORDER BY processing for min() func */
  10350. #define WHERE_ORDERBY_MAX 0x0002 /* ORDER BY processing for max() func */
  10351. #define WHERE_ONEPASS_DESIRED 0x0004 /* Want to do one-pass UPDATE/DELETE */
  10352. #define WHERE_DUPLICATES_OK 0x0008 /* Ok to return a row more than once */
  10353. #define WHERE_OMIT_OPEN_CLOSE 0x0010 /* Table cursors are already open */
  10354. #define WHERE_FORCE_TABLE 0x0020 /* Do not use an index-only search */
  10355. #define WHERE_ONETABLE_ONLY 0x0040 /* Only code the 1st table in pTabList */
  10356. #define WHERE_AND_ONLY 0x0080 /* Don't use indices for OR terms */
  10357. /*
  10358. ** The WHERE clause processing routine has two halves. The
  10359. ** first part does the start of the WHERE loop and the second
  10360. ** half does the tail of the WHERE loop. An instance of
  10361. ** this structure is returned by the first half and passed
  10362. ** into the second half to give some continuity.
  10363. */
  10364. struct WhereInfo {
  10365. Parse *pParse; /* Parsing and code generating context */
  10366. u16 wctrlFlags; /* Flags originally passed to sqlite3WhereBegin() */
  10367. u8 okOnePass; /* Ok to use one-pass algorithm for UPDATE or DELETE */
  10368. u8 untestedTerms; /* Not all WHERE terms resolved by outer loop */
  10369. u8 eDistinct;
  10370. SrcList *pTabList; /* List of tables in the join */
  10371. int iTop; /* The very beginning of the WHERE loop */
  10372. int iContinue; /* Jump here to continue with next record */
  10373. int iBreak; /* Jump here to break out of the loop */
  10374. int nLevel; /* Number of nested loop */
  10375. struct WhereClause *pWC; /* Decomposition of the WHERE clause */
  10376. double savedNQueryLoop; /* pParse->nQueryLoop outside the WHERE loop */
  10377. double nRowOut; /* Estimated number of output rows */
  10378. WhereLevel a[1]; /* Information about each nest loop in WHERE */
  10379. };
  10380. #define WHERE_DISTINCT_UNIQUE 1
  10381. #define WHERE_DISTINCT_ORDERED 2
  10382. /*
  10383. ** A NameContext defines a context in which to resolve table and column
  10384. ** names. The context consists of a list of tables (the pSrcList) field and
  10385. ** a list of named expression (pEList). The named expression list may
  10386. ** be NULL. The pSrc corresponds to the FROM clause of a SELECT or
  10387. ** to the table being operated on by INSERT, UPDATE, or DELETE. The
  10388. ** pEList corresponds to the result set of a SELECT and is NULL for
  10389. ** other statements.
  10390. **
  10391. ** NameContexts can be nested. When resolving names, the inner-most
  10392. ** context is searched first. If no match is found, the next outer
  10393. ** context is checked. If there is still no match, the next context
  10394. ** is checked. This process continues until either a match is found
  10395. ** or all contexts are check. When a match is found, the nRef member of
  10396. ** the context containing the match is incremented.
  10397. **
  10398. ** Each subquery gets a new NameContext. The pNext field points to the
  10399. ** NameContext in the parent query. Thus the process of scanning the
  10400. ** NameContext list corresponds to searching through successively outer
  10401. ** subqueries looking for a match.
  10402. */
  10403. struct NameContext {
  10404. Parse *pParse; /* The parser */
  10405. SrcList *pSrcList; /* One or more tables used to resolve names */
  10406. ExprList *pEList; /* Optional list of named expressions */
  10407. int nRef; /* Number of names resolved by this context */
  10408. int nErr; /* Number of errors encountered while resolving names */
  10409. u8 allowAgg; /* Aggregate functions allowed here */
  10410. u8 hasAgg; /* True if aggregates are seen */
  10411. u8 isCheck; /* True if resolving names in a CHECK constraint */
  10412. int nDepth; /* Depth of subquery recursion. 1 for no recursion */
  10413. AggInfo *pAggInfo; /* Information about aggregates at this level */
  10414. NameContext *pNext; /* Nex