PageRenderTime 53ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 2ms

/security/nss/lib/sqlite/sqlite3.c

http://github.com/zpao/v8monkey
C | 10962 lines | 3602 code | 518 blank | 6842 comment | 55 complexity | 53baa8167f00ffeea2662d0515f72037 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.6.22. By combining all the individual C code files into this
  4. ** single large file, the entire code can be compiled as a one 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% are 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 maximum number of attached databases. This must be between 0
  183. ** and 30. The upper bound on 30 is because a 32-bit integer bitmap
  184. ** is used internally to track attached databases.
  185. */
  186. #ifndef SQLITE_MAX_ATTACHED
  187. # define SQLITE_MAX_ATTACHED 10
  188. #endif
  189. /*
  190. ** The maximum value of a ?nnn wildcard that the parser will accept.
  191. */
  192. #ifndef SQLITE_MAX_VARIABLE_NUMBER
  193. # define SQLITE_MAX_VARIABLE_NUMBER 999
  194. #endif
  195. /* Maximum page size. The upper bound on this value is 32768. This a limit
  196. ** imposed by the necessity of storing the value in a 2-byte unsigned integer
  197. ** and the fact that the page size must be a power of 2.
  198. **
  199. ** If this limit is changed, then the compiled library is technically
  200. ** incompatible with an SQLite library compiled with a different limit. If
  201. ** a process operating on a database with a page-size of 65536 bytes
  202. ** crashes, then an instance of SQLite compiled with the default page-size
  203. ** limit will not be able to rollback the aborted transaction. This could
  204. ** lead to database corruption.
  205. */
  206. #ifndef SQLITE_MAX_PAGE_SIZE
  207. # define SQLITE_MAX_PAGE_SIZE 32768
  208. #endif
  209. /*
  210. ** The default size of a database page.
  211. */
  212. #ifndef SQLITE_DEFAULT_PAGE_SIZE
  213. # define SQLITE_DEFAULT_PAGE_SIZE 1024
  214. #endif
  215. #if SQLITE_DEFAULT_PAGE_SIZE>SQLITE_MAX_PAGE_SIZE
  216. # undef SQLITE_DEFAULT_PAGE_SIZE
  217. # define SQLITE_DEFAULT_PAGE_SIZE SQLITE_MAX_PAGE_SIZE
  218. #endif
  219. /*
  220. ** Ordinarily, if no value is explicitly provided, SQLite creates databases
  221. ** with page size SQLITE_DEFAULT_PAGE_SIZE. However, based on certain
  222. ** device characteristics (sector-size and atomic write() support),
  223. ** SQLite may choose a larger value. This constant is the maximum value
  224. ** SQLite will choose on its own.
  225. */
  226. #ifndef SQLITE_MAX_DEFAULT_PAGE_SIZE
  227. # define SQLITE_MAX_DEFAULT_PAGE_SIZE 8192
  228. #endif
  229. #if SQLITE_MAX_DEFAULT_PAGE_SIZE>SQLITE_MAX_PAGE_SIZE
  230. # undef SQLITE_MAX_DEFAULT_PAGE_SIZE
  231. # define SQLITE_MAX_DEFAULT_PAGE_SIZE SQLITE_MAX_PAGE_SIZE
  232. #endif
  233. /*
  234. ** Maximum number of pages in one database file.
  235. **
  236. ** This is really just the default value for the max_page_count pragma.
  237. ** This value can be lowered (or raised) at run-time using that the
  238. ** max_page_count macro.
  239. */
  240. #ifndef SQLITE_MAX_PAGE_COUNT
  241. # define SQLITE_MAX_PAGE_COUNT 1073741823
  242. #endif
  243. /*
  244. ** Maximum length (in bytes) of the pattern in a LIKE or GLOB
  245. ** operator.
  246. */
  247. #ifndef SQLITE_MAX_LIKE_PATTERN_LENGTH
  248. # define SQLITE_MAX_LIKE_PATTERN_LENGTH 50000
  249. #endif
  250. /*
  251. ** Maximum depth of recursion for triggers.
  252. **
  253. ** A value of 1 means that a trigger program will not be able to itself
  254. ** fire any triggers. A value of 0 means that no trigger programs at all
  255. ** may be executed.
  256. */
  257. #ifndef SQLITE_MAX_TRIGGER_DEPTH
  258. # define SQLITE_MAX_TRIGGER_DEPTH 1000
  259. #endif
  260. /************** End of sqliteLimit.h *****************************************/
  261. /************** Continuing where we left off in sqliteInt.h ******************/
  262. /* Disable nuisance warnings on Borland compilers */
  263. #if defined(__BORLANDC__)
  264. #pragma warn -rch /* unreachable code */
  265. #pragma warn -ccc /* Condition is always true or false */
  266. #pragma warn -aus /* Assigned value is never used */
  267. #pragma warn -csu /* Comparing signed and unsigned */
  268. #pragma warn -spa /* Suspicious pointer arithmetic */
  269. #endif
  270. /* Needed for various definitions... */
  271. #ifndef _GNU_SOURCE
  272. # define _GNU_SOURCE
  273. #endif
  274. /*
  275. ** Include standard header files as necessary
  276. */
  277. #ifdef HAVE_STDINT_H
  278. #include <stdint.h>
  279. #endif
  280. #ifdef HAVE_INTTYPES_H
  281. #include <inttypes.h>
  282. #endif
  283. #define SQLITE_INDEX_SAMPLES 10
  284. /*
  285. ** This macro is used to "hide" some ugliness in casting an int
  286. ** value to a ptr value under the MSVC 64-bit compiler. Casting
  287. ** non 64-bit values to ptr types results in a "hard" error with
  288. ** the MSVC 64-bit compiler which this attempts to avoid.
  289. **
  290. ** A simple compiler pragma or casting sequence could not be found
  291. ** to correct this in all situations, so this macro was introduced.
  292. **
  293. ** It could be argued that the intptr_t type could be used in this
  294. ** case, but that type is not available on all compilers, or
  295. ** requires the #include of specific headers which differs between
  296. ** platforms.
  297. **
  298. ** Ticket #3860: The llvm-gcc-4.2 compiler from Apple chokes on
  299. ** the ((void*)&((char*)0)[X]) construct. But MSVC chokes on ((void*)(X)).
  300. ** So we have to define the macros in different ways depending on the
  301. ** compiler.
  302. */
  303. #if defined(__GNUC__)
  304. # if defined(HAVE_STDINT_H)
  305. # define SQLITE_INT_TO_PTR(X) ((void*)(intptr_t)(X))
  306. # define SQLITE_PTR_TO_INT(X) ((int)(intptr_t)(X))
  307. # else
  308. # define SQLITE_INT_TO_PTR(X) ((void*)(X))
  309. # define SQLITE_PTR_TO_INT(X) ((int)(X))
  310. # endif
  311. #else
  312. # define SQLITE_INT_TO_PTR(X) ((void*)&((char*)0)[X])
  313. # define SQLITE_PTR_TO_INT(X) ((int)(((char*)X)-(char*)0))
  314. #endif
  315. /*
  316. ** The SQLITE_THREADSAFE macro must be defined as either 0 or 1.
  317. ** Older versions of SQLite used an optional THREADSAFE macro.
  318. ** We support that for legacy
  319. */
  320. #if !defined(SQLITE_THREADSAFE)
  321. #if defined(THREADSAFE)
  322. # define SQLITE_THREADSAFE THREADSAFE
  323. #else
  324. # define SQLITE_THREADSAFE 1
  325. #endif
  326. #endif
  327. /*
  328. ** The SQLITE_DEFAULT_MEMSTATUS macro must be defined as either 0 or 1.
  329. ** It determines whether or not the features related to
  330. ** SQLITE_CONFIG_MEMSTATUS are available by default or not. This value can
  331. ** be overridden at runtime using the sqlite3_config() API.
  332. */
  333. #if !defined(SQLITE_DEFAULT_MEMSTATUS)
  334. # define SQLITE_DEFAULT_MEMSTATUS 1
  335. #endif
  336. /*
  337. ** Exactly one of the following macros must be defined in order to
  338. ** specify which memory allocation subsystem to use.
  339. **
  340. ** SQLITE_SYSTEM_MALLOC // Use normal system malloc()
  341. ** SQLITE_MEMDEBUG // Debugging version of system malloc()
  342. ** SQLITE_MEMORY_SIZE // internal allocator #1
  343. ** SQLITE_MMAP_HEAP_SIZE // internal mmap() allocator
  344. ** SQLITE_POW2_MEMORY_SIZE // internal power-of-two allocator
  345. **
  346. ** If none of the above are defined, then set SQLITE_SYSTEM_MALLOC as
  347. ** the default.
  348. */
  349. #if defined(SQLITE_SYSTEM_MALLOC)+defined(SQLITE_MEMDEBUG)+\
  350. defined(SQLITE_MEMORY_SIZE)+defined(SQLITE_MMAP_HEAP_SIZE)+\
  351. defined(SQLITE_POW2_MEMORY_SIZE)>1
  352. # error "At most one of the following compile-time configuration options\
  353. is allows: SQLITE_SYSTEM_MALLOC, SQLITE_MEMDEBUG, SQLITE_MEMORY_SIZE,\
  354. SQLITE_MMAP_HEAP_SIZE, SQLITE_POW2_MEMORY_SIZE"
  355. #endif
  356. #if defined(SQLITE_SYSTEM_MALLOC)+defined(SQLITE_MEMDEBUG)+\
  357. defined(SQLITE_MEMORY_SIZE)+defined(SQLITE_MMAP_HEAP_SIZE)+\
  358. defined(SQLITE_POW2_MEMORY_SIZE)==0
  359. # define SQLITE_SYSTEM_MALLOC 1
  360. #endif
  361. /*
  362. ** If SQLITE_MALLOC_SOFT_LIMIT is not zero, then try to keep the
  363. ** sizes of memory allocations below this value where possible.
  364. */
  365. #if !defined(SQLITE_MALLOC_SOFT_LIMIT)
  366. # define SQLITE_MALLOC_SOFT_LIMIT 1024
  367. #endif
  368. /*
  369. ** We need to define _XOPEN_SOURCE as follows in order to enable
  370. ** recursive mutexes on most Unix systems. But Mac OS X is different.
  371. ** The _XOPEN_SOURCE define causes problems for Mac OS X we are told,
  372. ** so it is omitted there. See ticket #2673.
  373. **
  374. ** Later we learn that _XOPEN_SOURCE is poorly or incorrectly
  375. ** implemented on some systems. So we avoid defining it at all
  376. ** if it is already defined or if it is unneeded because we are
  377. ** not doing a threadsafe build. Ticket #2681.
  378. **
  379. ** See also ticket #2741.
  380. */
  381. #if !defined(_XOPEN_SOURCE) && !defined(__DARWIN__) && !defined(__APPLE__) && SQLITE_THREADSAFE
  382. # define _XOPEN_SOURCE 500 /* Needed to enable pthread recursive mutexes */
  383. #endif
  384. /*
  385. ** The TCL headers are only needed when compiling the TCL bindings.
  386. */
  387. #if defined(SQLITE_TCL) || defined(TCLSH)
  388. # include <tcl.h>
  389. #endif
  390. /*
  391. ** Many people are failing to set -DNDEBUG=1 when compiling SQLite.
  392. ** Setting NDEBUG makes the code smaller and run faster. So the following
  393. ** lines are added to automatically set NDEBUG unless the -DSQLITE_DEBUG=1
  394. ** option is set. Thus NDEBUG becomes an opt-in rather than an opt-out
  395. ** feature.
  396. */
  397. #if !defined(NDEBUG) && !defined(SQLITE_DEBUG)
  398. # define NDEBUG 1
  399. #endif
  400. /*
  401. ** The testcase() macro is used to aid in coverage testing. When
  402. ** doing coverage testing, the condition inside the argument to
  403. ** testcase() must be evaluated both true and false in order to
  404. ** get full branch coverage. The testcase() macro is inserted
  405. ** to help ensure adequate test coverage in places where simple
  406. ** condition/decision coverage is inadequate. For example, testcase()
  407. ** can be used to make sure boundary values are tested. For
  408. ** bitmask tests, testcase() can be used to make sure each bit
  409. ** is significant and used at least once. On switch statements
  410. ** where multiple cases go to the same block of code, testcase()
  411. ** can insure that all cases are evaluated.
  412. **
  413. */
  414. #ifdef SQLITE_COVERAGE_TEST
  415. SQLITE_PRIVATE void sqlite3Coverage(int);
  416. # define testcase(X) if( X ){ sqlite3Coverage(__LINE__); }
  417. #else
  418. # define testcase(X)
  419. #endif
  420. /*
  421. ** The TESTONLY macro is used to enclose variable declarations or
  422. ** other bits of code that are needed to support the arguments
  423. ** within testcase() and assert() macros.
  424. */
  425. #if !defined(NDEBUG) || defined(SQLITE_COVERAGE_TEST)
  426. # define TESTONLY(X) X
  427. #else
  428. # define TESTONLY(X)
  429. #endif
  430. /*
  431. ** Sometimes we need a small amount of code such as a variable initialization
  432. ** to setup for a later assert() statement. We do not want this code to
  433. ** appear when assert() is disabled. The following macro is therefore
  434. ** used to contain that setup code. The "VVA" acronym stands for
  435. ** "Verification, Validation, and Accreditation". In other words, the
  436. ** code within VVA_ONLY() will only run during verification processes.
  437. */
  438. #ifndef NDEBUG
  439. # define VVA_ONLY(X) X
  440. #else
  441. # define VVA_ONLY(X)
  442. #endif
  443. /*
  444. ** The ALWAYS and NEVER macros surround boolean expressions which
  445. ** are intended to always be true or false, respectively. Such
  446. ** expressions could be omitted from the code completely. But they
  447. ** are included in a few cases in order to enhance the resilience
  448. ** of SQLite to unexpected behavior - to make the code "self-healing"
  449. ** or "ductile" rather than being "brittle" and crashing at the first
  450. ** hint of unplanned behavior.
  451. **
  452. ** In other words, ALWAYS and NEVER are added for defensive code.
  453. **
  454. ** When doing coverage testing ALWAYS and NEVER are hard-coded to
  455. ** be true and false so that the unreachable code then specify will
  456. ** not be counted as untested code.
  457. */
  458. #if defined(SQLITE_COVERAGE_TEST)
  459. # define ALWAYS(X) (1)
  460. # define NEVER(X) (0)
  461. #elif !defined(NDEBUG)
  462. # define ALWAYS(X) ((X)?1:(assert(0),0))
  463. # define NEVER(X) ((X)?(assert(0),1):0)
  464. #else
  465. # define ALWAYS(X) (X)
  466. # define NEVER(X) (X)
  467. #endif
  468. /*
  469. ** The macro unlikely() is a hint that surrounds a boolean
  470. ** expression that is usually false. Macro likely() surrounds
  471. ** a boolean expression that is usually true. GCC is able to
  472. ** use these hints to generate better code, sometimes.
  473. */
  474. #if defined(__GNUC__) && 0
  475. # define likely(X) __builtin_expect((X),1)
  476. # define unlikely(X) __builtin_expect((X),0)
  477. #else
  478. # define likely(X) !!(X)
  479. # define unlikely(X) !!(X)
  480. #endif
  481. /************** Include sqlite3.h in the middle of sqliteInt.h ***************/
  482. /************** Begin file sqlite3.h *****************************************/
  483. /*
  484. ** 2001 September 15
  485. **
  486. ** The author disclaims copyright to this source code. In place of
  487. ** a legal notice, here is a blessing:
  488. **
  489. ** May you do good and not evil.
  490. ** May you find forgiveness for yourself and forgive others.
  491. ** May you share freely, never taking more than you give.
  492. **
  493. *************************************************************************
  494. ** This header file defines the interface that the SQLite library
  495. ** presents to client programs. If a C-function, structure, datatype,
  496. ** or constant definition does not appear in this file, then it is
  497. ** not a published API of SQLite, is subject to change without
  498. ** notice, and should not be referenced by programs that use SQLite.
  499. **
  500. ** Some of the definitions that are in this file are marked as
  501. ** "experimental". Experimental interfaces are normally new
  502. ** features recently added to SQLite. We do not anticipate changes
  503. ** to experimental interfaces but reserve the right to make minor changes
  504. ** if experience from use "in the wild" suggest such changes are prudent.
  505. **
  506. ** The official C-language API documentation for SQLite is derived
  507. ** from comments in this file. This file is the authoritative source
  508. ** on how SQLite interfaces are suppose to operate.
  509. **
  510. ** The name of this file under configuration management is "sqlite.h.in".
  511. ** The makefile makes some minor changes to this file (such as inserting
  512. ** the version number) and changes its name to "sqlite3.h" as
  513. ** part of the build process.
  514. */
  515. #ifndef _SQLITE3_H_
  516. #define _SQLITE3_H_
  517. #include <stdarg.h> /* Needed for the definition of va_list */
  518. /*
  519. ** Make sure we can call this stuff from C++.
  520. */
  521. #if 0
  522. extern "C" {
  523. #endif
  524. /*
  525. ** Add the ability to override 'extern'
  526. */
  527. #ifndef SQLITE_EXTERN
  528. # define SQLITE_EXTERN extern
  529. #endif
  530. #ifndef SQLITE_API
  531. # define SQLITE_API
  532. #endif
  533. /*
  534. ** These no-op macros are used in front of interfaces to mark those
  535. ** interfaces as either deprecated or experimental. New applications
  536. ** should not use deprecated interfaces - they are support for backwards
  537. ** compatibility only. Application writers should be aware that
  538. ** experimental interfaces are subject to change in point releases.
  539. **
  540. ** These macros used to resolve to various kinds of compiler magic that
  541. ** would generate warning messages when they were used. But that
  542. ** compiler magic ended up generating such a flurry of bug reports
  543. ** that we have taken it all out and gone back to using simple
  544. ** noop macros.
  545. */
  546. #define SQLITE_DEPRECATED
  547. #define SQLITE_EXPERIMENTAL
  548. /*
  549. ** Ensure these symbols were not defined by some previous header file.
  550. */
  551. #ifdef SQLITE_VERSION
  552. # undef SQLITE_VERSION
  553. #endif
  554. #ifdef SQLITE_VERSION_NUMBER
  555. # undef SQLITE_VERSION_NUMBER
  556. #endif
  557. /*
  558. ** CAPI3REF: Compile-Time Library Version Numbers
  559. **
  560. ** ^(The [SQLITE_VERSION] C preprocessor macro in the sqlite3.h header
  561. ** evaluates to a string literal that is the SQLite version in the
  562. ** format "X.Y.Z" where X is the major version number (always 3 for
  563. ** SQLite3) and Y is the minor version number and Z is the release number.)^
  564. ** ^(The [SQLITE_VERSION_NUMBER] C preprocessor macro resolves to an integer
  565. ** with the value (X*1000000 + Y*1000 + Z) where X, Y, and Z are the same
  566. ** numbers used in [SQLITE_VERSION].)^
  567. ** The SQLITE_VERSION_NUMBER for any given release of SQLite will also
  568. ** be larger than the release from which it is derived. Either Y will
  569. ** be held constant and Z will be incremented or else Y will be incremented
  570. ** and Z will be reset to zero.
  571. **
  572. ** Since version 3.6.18, SQLite source code has been stored in the
  573. ** <a href="http://www.fossil-scm.org/">Fossil configuration management
  574. ** system</a>. ^The SQLITE_SOURCE_ID macro evalutes to
  575. ** a string which identifies a particular check-in of SQLite
  576. ** within its configuration management system. ^The SQLITE_SOURCE_ID
  577. ** string contains the date and time of the check-in (UTC) and an SHA1
  578. ** hash of the entire source tree.
  579. **
  580. ** See also: [sqlite3_libversion()],
  581. ** [sqlite3_libversion_number()], [sqlite3_sourceid()],
  582. ** [sqlite_version()] and [sqlite_source_id()].
  583. */
  584. #define SQLITE_VERSION "3.6.22"
  585. #define SQLITE_VERSION_NUMBER 3006022
  586. #define SQLITE_SOURCE_ID "2010-01-05 15:30:36 28d0d7710761114a44a1a3a425a6883c661f06e7"
  587. /*
  588. ** CAPI3REF: Run-Time Library Version Numbers
  589. ** KEYWORDS: sqlite3_version
  590. **
  591. ** These interfaces provide the same information as the [SQLITE_VERSION],
  592. ** [SQLITE_VERSION_NUMBER], and [SQLITE_SOURCE_ID] C preprocessor macros
  593. ** but are associated with the library instead of the header file. ^(Cautious
  594. ** programmers might include assert() statements in their application to
  595. ** verify that values returned by these interfaces match the macros in
  596. ** the header, and thus insure that the application is
  597. ** compiled with matching library and header files.
  598. **
  599. ** <blockquote><pre>
  600. ** assert( sqlite3_libversion_number()==SQLITE_VERSION_NUMBER );
  601. ** assert( strcmp(sqlite3_sourceid(),SQLITE_SOURCE_ID)==0 );
  602. ** assert( strcmp(sqlite3_libversion(),SQLITE_VERSION)==0 );
  603. ** </pre></blockquote>)^
  604. **
  605. ** ^The sqlite3_version[] string constant contains the text of [SQLITE_VERSION]
  606. ** macro. ^The sqlite3_libversion() function returns a pointer to the
  607. ** to the sqlite3_version[] string constant. The sqlite3_libversion()
  608. ** function is provided for use in DLLs since DLL users usually do not have
  609. ** direct access to string constants within the DLL. ^The
  610. ** sqlite3_libversion_number() function returns an integer equal to
  611. ** [SQLITE_VERSION_NUMBER]. ^The sqlite3_sourceid() function a pointer
  612. ** to a string constant whose value is the same as the [SQLITE_SOURCE_ID]
  613. ** C preprocessor macro.
  614. **
  615. ** See also: [sqlite_version()] and [sqlite_source_id()].
  616. */
  617. SQLITE_API const char sqlite3_version[] = SQLITE_VERSION;
  618. SQLITE_API const char *sqlite3_libversion(void);
  619. SQLITE_API const char *sqlite3_sourceid(void);
  620. SQLITE_API int sqlite3_libversion_number(void);
  621. /*
  622. ** CAPI3REF: Test To See If The Library Is Threadsafe
  623. **
  624. ** ^The sqlite3_threadsafe() function returns zero if and only if
  625. ** SQLite was compiled mutexing code omitted due to the
  626. ** [SQLITE_THREADSAFE] compile-time option being set to 0.
  627. **
  628. ** SQLite can be compiled with or without mutexes. When
  629. ** the [SQLITE_THREADSAFE] C preprocessor macro is 1 or 2, mutexes
  630. ** are enabled and SQLite is threadsafe. When the
  631. ** [SQLITE_THREADSAFE] macro is 0,
  632. ** the mutexes are omitted. Without the mutexes, it is not safe
  633. ** to use SQLite concurrently from more than one thread.
  634. **
  635. ** Enabling mutexes incurs a measurable performance penalty.
  636. ** So if speed is of utmost importance, it makes sense to disable
  637. ** the mutexes. But for maximum safety, mutexes should be enabled.
  638. ** ^The default behavior is for mutexes to be enabled.
  639. **
  640. ** This interface can be used by an application to make sure that the
  641. ** version of SQLite that it is linking against was compiled with
  642. ** the desired setting of the [SQLITE_THREADSAFE] macro.
  643. **
  644. ** This interface only reports on the compile-time mutex setting
  645. ** of the [SQLITE_THREADSAFE] flag. If SQLite is compiled with
  646. ** SQLITE_THREADSAFE=1 or =2 then mutexes are enabled by default but
  647. ** can be fully or partially disabled using a call to [sqlite3_config()]
  648. ** with the verbs [SQLITE_CONFIG_SINGLETHREAD], [SQLITE_CONFIG_MULTITHREAD],
  649. ** or [SQLITE_CONFIG_MUTEX]. ^(The return value of the
  650. ** sqlite3_threadsafe() function shows only the compile-time setting of
  651. ** thread safety, not any run-time changes to that setting made by
  652. ** sqlite3_config(). In other words, the return value from sqlite3_threadsafe()
  653. ** is unchanged by calls to sqlite3_config().)^
  654. **
  655. ** See the [threading mode] documentation for additional information.
  656. */
  657. SQLITE_API int sqlite3_threadsafe(void);
  658. /*
  659. ** CAPI3REF: Database Connection Handle
  660. ** KEYWORDS: {database connection} {database connections}
  661. **
  662. ** Each open SQLite database is represented by a pointer to an instance of
  663. ** the opaque structure named "sqlite3". It is useful to think of an sqlite3
  664. ** pointer as an object. The [sqlite3_open()], [sqlite3_open16()], and
  665. ** [sqlite3_open_v2()] interfaces are its constructors, and [sqlite3_close()]
  666. ** is its destructor. There are many other interfaces (such as
  667. ** [sqlite3_prepare_v2()], [sqlite3_create_function()], and
  668. ** [sqlite3_busy_timeout()] to name but three) that are methods on an
  669. ** sqlite3 object.
  670. */
  671. typedef struct sqlite3 sqlite3;
  672. /*
  673. ** CAPI3REF: 64-Bit Integer Types
  674. ** KEYWORDS: sqlite_int64 sqlite_uint64
  675. **
  676. ** Because there is no cross-platform way to specify 64-bit integer types
  677. ** SQLite includes typedefs for 64-bit signed and unsigned integers.
  678. **
  679. ** The sqlite3_int64 and sqlite3_uint64 are the preferred type definitions.
  680. ** The sqlite_int64 and sqlite_uint64 types are supported for backwards
  681. ** compatibility only.
  682. **
  683. ** ^The sqlite3_int64 and sqlite_int64 types can store integer values
  684. ** between -9223372036854775808 and +9223372036854775807 inclusive. ^The
  685. ** sqlite3_uint64 and sqlite_uint64 types can store integer values
  686. ** between 0 and +18446744073709551615 inclusive.
  687. */
  688. #ifdef SQLITE_INT64_TYPE
  689. typedef SQLITE_INT64_TYPE sqlite_int64;
  690. typedef unsigned SQLITE_INT64_TYPE sqlite_uint64;
  691. #elif defined(_MSC_VER) || defined(__BORLANDC__)
  692. typedef __int64 sqlite_int64;
  693. typedef unsigned __int64 sqlite_uint64;
  694. #else
  695. typedef long long int sqlite_int64;
  696. typedef unsigned long long int sqlite_uint64;
  697. #endif
  698. typedef sqlite_int64 sqlite3_int64;
  699. typedef sqlite_uint64 sqlite3_uint64;
  700. /*
  701. ** If compiling for a processor that lacks floating point support,
  702. ** substitute integer for floating-point.
  703. */
  704. #ifdef SQLITE_OMIT_FLOATING_POINT
  705. # define double sqlite3_int64
  706. #endif
  707. /*
  708. ** CAPI3REF: Closing A Database Connection
  709. **
  710. ** ^The sqlite3_close() routine is the destructor for the [sqlite3] object.
  711. ** ^Calls to sqlite3_close() return SQLITE_OK if the [sqlite3] object is
  712. ** successfullly destroyed and all associated resources are deallocated.
  713. **
  714. ** Applications must [sqlite3_finalize | finalize] all [prepared statements]
  715. ** and [sqlite3_blob_close | close] all [BLOB handles] associated with
  716. ** the [sqlite3] object prior to attempting to close the object. ^If
  717. ** sqlite3_close() is called on a [database connection] that still has
  718. ** outstanding [prepared statements] or [BLOB handles], then it returns
  719. ** SQLITE_BUSY.
  720. **
  721. ** ^If [sqlite3_close()] is invoked while a transaction is open,
  722. ** the transaction is automatically rolled back.
  723. **
  724. ** The C parameter to [sqlite3_close(C)] must be either a NULL
  725. ** pointer or an [sqlite3] object pointer obtained
  726. ** from [sqlite3_open()], [sqlite3_open16()], or
  727. ** [sqlite3_open_v2()], and not previously closed.
  728. ** ^Calling sqlite3_close() with a NULL pointer argument is a
  729. ** harmless no-op.
  730. */
  731. SQLITE_API int sqlite3_close(sqlite3 *);
  732. /*
  733. ** The type for a callback function.
  734. ** This is legacy and deprecated. It is included for historical
  735. ** compatibility and is not documented.
  736. */
  737. typedef int (*sqlite3_callback)(void*,int,char**, char**);
  738. /*
  739. ** CAPI3REF: One-Step Query Execution Interface
  740. **
  741. ** The sqlite3_exec() interface is a convenience wrapper around
  742. ** [sqlite3_prepare_v2()], [sqlite3_step()], and [sqlite3_finalize()],
  743. ** that allows an application to run multiple statements of SQL
  744. ** without having to use a lot of C code.
  745. **
  746. ** ^The sqlite3_exec() interface runs zero or more UTF-8 encoded,
  747. ** semicolon-separate SQL statements passed into its 2nd argument,
  748. ** in the context of the [database connection] passed in as its 1st
  749. ** argument. ^If the callback function of the 3rd argument to
  750. ** sqlite3_exec() is not NULL, then it is invoked for each result row
  751. ** coming out of the evaluated SQL statements. ^The 4th argument to
  752. ** to sqlite3_exec() is relayed through to the 1st argument of each
  753. ** callback invocation. ^If the callback pointer to sqlite3_exec()
  754. ** is NULL, then no callback is ever invoked and result rows are
  755. ** ignored.
  756. **
  757. ** ^If an error occurs while evaluating the SQL statements passed into
  758. ** sqlite3_exec(), then execution of the current statement stops and
  759. ** subsequent statements are skipped. ^If the 5th parameter to sqlite3_exec()
  760. ** is not NULL then any error message is written into memory obtained
  761. ** from [sqlite3_malloc()] and passed back through the 5th parameter.
  762. ** To avoid memory leaks, the application should invoke [sqlite3_free()]
  763. ** on error message strings returned through the 5th parameter of
  764. ** of sqlite3_exec() after the error message string is no longer needed.
  765. ** ^If the 5th parameter to sqlite3_exec() is not NULL and no errors
  766. ** occur, then sqlite3_exec() sets the pointer in its 5th parameter to
  767. ** NULL before returning.
  768. **
  769. ** ^If an sqlite3_exec() callback returns non-zero, the sqlite3_exec()
  770. ** routine returns SQLITE_ABORT without invoking the callback again and
  771. ** without running any subsequent SQL statements.
  772. **
  773. ** ^The 2nd argument to the sqlite3_exec() callback function is the
  774. ** number of columns in the result. ^The 3rd argument to the sqlite3_exec()
  775. ** callback is an array of pointers to strings obtained as if from
  776. ** [sqlite3_column_text()], one for each column. ^If an element of a
  777. ** result row is NULL then the corresponding string pointer for the
  778. ** sqlite3_exec() callback is a NULL pointer. ^The 4th argument to the
  779. ** sqlite3_exec() callback is an array of pointers to strings where each
  780. ** entry represents the name of corresponding result column as obtained
  781. ** from [sqlite3_column_name()].
  782. **
  783. ** ^If the 2nd parameter to sqlite3_exec() is a NULL pointer, a pointer
  784. ** to an empty string, or a pointer that contains only whitespace and/or
  785. ** SQL comments, then no SQL statements are evaluated and the database
  786. ** is not changed.
  787. **
  788. ** Restrictions:
  789. **
  790. ** <ul>
  791. ** <li> The application must insure that the 1st parameter to sqlite3_exec()
  792. ** is a valid and open [database connection].
  793. ** <li> The application must not close [database connection] specified by
  794. ** the 1st parameter to sqlite3_exec() while sqlite3_exec() is running.
  795. ** <li> The application must not modify the SQL statement text passed into
  796. ** the 2nd parameter of sqlite3_exec() while sqlite3_exec() is running.
  797. ** </ul>
  798. */
  799. SQLITE_API int sqlite3_exec(
  800. sqlite3*, /* An open database */
  801. const char *sql, /* SQL to be evaluated */
  802. int (*callback)(void*,int,char**,char**), /* Callback function */
  803. void *, /* 1st argument to callback */
  804. char **errmsg /* Error msg written here */
  805. );
  806. /*
  807. ** CAPI3REF: Result Codes
  808. ** KEYWORDS: SQLITE_OK {error code} {error codes}
  809. ** KEYWORDS: {result code} {result codes}
  810. **
  811. ** Many SQLite functions return an integer result code from the set shown
  812. ** here in order to indicates success or failure.
  813. **
  814. ** New error codes may be added in future versions of SQLite.
  815. **
  816. ** See also: [SQLITE_IOERR_READ | extended result codes]
  817. */
  818. #define SQLITE_OK 0 /* Successful result */
  819. /* beginning-of-error-codes */
  820. #define SQLITE_ERROR 1 /* SQL error or missing database */
  821. #define SQLITE_INTERNAL 2 /* Internal logic error in SQLite */
  822. #define SQLITE_PERM 3 /* Access permission denied */
  823. #define SQLITE_ABORT 4 /* Callback routine requested an abort */
  824. #define SQLITE_BUSY 5 /* The database file is locked */
  825. #define SQLITE_LOCKED 6 /* A table in the database is locked */
  826. #define SQLITE_NOMEM 7 /* A malloc() failed */
  827. #define SQLITE_READONLY 8 /* Attempt to write a readonly database */
  828. #define SQLITE_INTERRUPT 9 /* Operation terminated by sqlite3_interrupt()*/
  829. #define SQLITE_IOERR 10 /* Some kind of disk I/O error occurred */
  830. #define SQLITE_CORRUPT 11 /* The database disk image is malformed */
  831. #define SQLITE_NOTFOUND 12 /* NOT USED. Table or record not found */
  832. #define SQLITE_FULL 13 /* Insertion failed because database is full */
  833. #define SQLITE_CANTOPEN 14 /* Unable to open the database file */
  834. #define SQLITE_PROTOCOL 15 /* NOT USED. Database lock protocol error */
  835. #define SQLITE_EMPTY 16 /* Database is empty */
  836. #define SQLITE_SCHEMA 17 /* The database schema changed */
  837. #define SQLITE_TOOBIG 18 /* String or BLOB exceeds size limit */
  838. #define SQLITE_CONSTRAINT 19 /* Abort due to constraint violation */
  839. #define SQLITE_MISMATCH 20 /* Data type mismatch */
  840. #define SQLITE_MISUSE 21 /* Library used incorrectly */
  841. #define SQLITE_NOLFS 22 /* Uses OS features not supported on host */
  842. #define SQLITE_AUTH 23 /* Authorization denied */
  843. #define SQLITE_FORMAT 24 /* Auxiliary database format error */
  844. #define SQLITE_RANGE 25 /* 2nd parameter to sqlite3_bind out of range */
  845. #define SQLITE_NOTADB 26 /* File opened that is not a database file */
  846. #define SQLITE_ROW 100 /* sqlite3_step() has another row ready */
  847. #define SQLITE_DONE 101 /* sqlite3_step() has finished executing */
  848. /* end-of-error-codes */
  849. /*
  850. ** CAPI3REF: Extended Result Codes
  851. ** KEYWORDS: {extended error code} {extended error codes}
  852. ** KEYWORDS: {extended result code} {extended result codes}
  853. **
  854. ** In its default configuration, SQLite API routines return one of 26 integer
  855. ** [SQLITE_OK | result codes]. However, experience has shown that many of
  856. ** these result codes are too coarse-grained. They do not provide as
  857. ** much information about problems as programmers might like. In an effort to
  858. ** address this, newer versions of SQLite (version 3.3.8 and later) include
  859. ** support for additional result codes that provide more detailed information
  860. ** about errors. The extended result codes are enabled or disabled
  861. ** on a per database connection basis using the
  862. ** [sqlite3_extended_result_codes()] API.
  863. **
  864. ** Some of the available extended result codes are listed here.
  865. ** One may expect the number of extended result codes will be expand
  866. ** over time. Software that uses extended result codes should expect
  867. ** to see new result codes in future releases of SQLite.
  868. **
  869. ** The SQLITE_OK result code will never be extended. It will always
  870. ** be exactly zero.
  871. */
  872. #define SQLITE_IOERR_READ (SQLITE_IOERR | (1<<8))
  873. #define SQLITE_IOERR_SHORT_READ (SQLITE_IOERR | (2<<8))
  874. #define SQLITE_IOERR_WRITE (SQLITE_IOERR | (3<<8))
  875. #define SQLITE_IOERR_FSYNC (SQLITE_IOERR | (4<<8))
  876. #define SQLITE_IOERR_DIR_FSYNC (SQLITE_IOERR | (5<<8))
  877. #define SQLITE_IOERR_TRUNCATE (SQLITE_IOERR | (6<<8))
  878. #define SQLITE_IOERR_FSTAT (SQLITE_IOERR | (7<<8))
  879. #define SQLITE_IOERR_UNLOCK (SQLITE_IOERR | (8<<8))
  880. #define SQLITE_IOERR_RDLOCK (SQLITE_IOERR | (9<<8))
  881. #define SQLITE_IOERR_DELETE (SQLITE_IOERR | (10<<8))
  882. #define SQLITE_IOERR_BLOCKED (SQLITE_IOERR | (11<<8))
  883. #define SQLITE_IOERR_NOMEM (SQLITE_IOERR | (12<<8))
  884. #define SQLITE_IOERR_ACCESS (SQLITE_IOERR | (13<<8))
  885. #define SQLITE_IOERR_CHECKRESERVEDLOCK (SQLITE_IOERR | (14<<8))
  886. #define SQLITE_IOERR_LOCK (SQLITE_IOERR | (15<<8))
  887. #define SQLITE_IOERR_CLOSE (SQLITE_IOERR | (16<<8))
  888. #define SQLITE_IOERR_DIR_CLOSE (SQLITE_IOERR | (17<<8))
  889. #define SQLITE_LOCKED_SHAREDCACHE (SQLITE_LOCKED | (1<<8) )
  890. /*
  891. ** CAPI3REF: Flags For File Open Operations
  892. **
  893. ** These bit values are intended for use in the
  894. ** 3rd parameter to the [sqlite3_open_v2()] interface and
  895. ** in the 4th parameter to the xOpen method of the
  896. ** [sqlite3_vfs] object.
  897. */
  898. #define SQLITE_OPEN_READONLY 0x00000001 /* Ok for sqlite3_open_v2() */
  899. #define SQLITE_OPEN_READWRITE 0x00000002 /* Ok for sqlite3_open_v2() */
  900. #define SQLITE_OPEN_CREATE 0x00000004 /* Ok for sqlite3_open_v2() */
  901. #define SQLITE_OPEN_DELETEONCLOSE 0x00000008 /* VFS only */
  902. #define SQLITE_OPEN_EXCLUSIVE 0x00000010 /* VFS only */
  903. #define SQLITE_OPEN_MAIN_DB 0x00000100 /* VFS only */
  904. #define SQLITE_OPEN_TEMP_DB 0x00000200 /* VFS only */
  905. #define SQLITE_OPEN_TRANSIENT_DB 0x00000400 /* VFS only */
  906. #define SQLITE_OPEN_MAIN_JOURNAL 0x00000800 /* VFS only */
  907. #define SQLITE_OPEN_TEMP_JOURNAL 0x00001000 /* VFS only */
  908. #define SQLITE_OPEN_SUBJOURNAL 0x00002000 /* VFS only */
  909. #define SQLITE_OPEN_MASTER_JOURNAL 0x00004000 /* VFS only */
  910. #define SQLITE_OPEN_NOMUTEX 0x00008000 /* Ok for sqlite3_open_v2() */
  911. #define SQLITE_OPEN_FULLMUTEX 0x00010000 /* Ok for sqlite3_open_v2() */
  912. #define SQLITE_OPEN_SHAREDCACHE 0x00020000 /* Ok for sqlite3_open_v2() */
  913. #define SQLITE_OPEN_PRIVATECACHE 0x00040000 /* Ok for sqlite3_open_v2() */
  914. /*
  915. ** CAPI3REF: Device Characteristics
  916. **
  917. ** The xDeviceCapabilities method of the [sqlite3_io_methods]
  918. ** object returns an integer which is a vector of the these
  919. ** bit values expressing I/O characteristics of the mass storage
  920. ** device that holds the file that the [sqlite3_io_methods]
  921. ** refers to.
  922. **
  923. ** The SQLITE_IOCAP_ATOMIC property means that all writes of
  924. ** any size are atomic. The SQLITE_IOCAP_ATOMICnnn values
  925. ** mean that writes of blocks that are nnn bytes in size and
  926. ** are aligned to an address which is an integer multiple of
  927. ** nnn are atomic. The SQLITE_IOCAP_SAFE_APPEND value means
  928. ** that when data is appended to a file, the data is appended
  929. ** first then the size of the file is extended, never the other
  930. ** way around. The SQLITE_IOCAP_SEQUENTIAL property means that
  931. ** information is written to disk in the same order as calls
  932. ** to xWrite().
  933. */
  934. #define SQLITE_IOCAP_ATOMIC 0x00000001
  935. #define SQLITE_IOCAP_ATOMIC512 0x00000002
  936. #define SQLITE_IOCAP_ATOMIC1K 0x00000004
  937. #define SQLITE_IOCAP_ATOMIC2K 0x00000008
  938. #define SQLITE_IOCAP_ATOMIC4K 0x00000010
  939. #define SQLITE_IOCAP_ATOMIC8K 0x00000020
  940. #define SQLITE_IOCAP_ATOMIC16K 0x00000040
  941. #define SQLITE_IOCAP_ATOMIC32K 0x00000080
  942. #define SQLITE_IOCAP_ATOMIC64K 0x00000100
  943. #define SQLITE_IOCAP_SAFE_APPEND 0x00000200
  944. #define SQLITE_IOCAP_SEQUENTIAL 0x00000400
  945. /*
  946. ** CAPI3REF: File Locking Levels
  947. **
  948. ** SQLite uses one of these integer values as the second
  949. ** argument to calls it makes to the xLock() and xUnlock() methods
  950. ** of an [sqlite3_io_methods] object.
  951. */
  952. #define SQLITE_LOCK_NONE 0
  953. #define SQLITE_LOCK_SHARED 1
  954. #define SQLITE_LOCK_RESERVED 2
  955. #define SQLITE_LOCK_PENDING 3
  956. #define SQLITE_LOCK_EXCLUSIVE 4
  957. /*
  958. ** CAPI3REF: Synchronization Type Flags
  959. **
  960. ** When SQLite invokes the xSync() method of an
  961. ** [sqlite3_io_methods] object it uses a combination of
  962. ** these integer values as the second argument.
  963. **
  964. ** When the SQLITE_SYNC_DATAONLY flag is used, it means that the
  965. ** sync operation only needs to flush data to mass storage. Inode
  966. ** information need not be flushed. If the lower four bits of the flag
  967. ** equal SQLITE_SYNC_NORMAL, that means to use normal fsync() semantics.
  968. ** If the lower four bits equal SQLITE_SYNC_FULL, that means
  969. ** to use Mac OS X style fullsync instead of fsync().
  970. */
  971. #define SQLITE_SYNC_NORMAL 0x00002
  972. #define SQLITE_SYNC_FULL 0x00003
  973. #define SQLITE_SYNC_DATAONLY 0x00010
  974. /*
  975. ** CAPI3REF: OS Interface Open File Handle
  976. **
  977. ** An [sqlite3_file] object represents an open file in the
  978. ** [sqlite3_vfs | OS interface layer]. Individual OS interface
  979. ** implementations will
  980. ** want to subclass this object by appending additional fields
  981. ** for their own use. The pMethods entry is a pointer to an
  982. ** [sqlite3_io_methods] object that defines methods for performing
  983. ** I/O operations on the open file.
  984. */
  985. typedef struct sqlite3_file sqlite3_file;
  986. struct sqlite3_file {
  987. const struct sqlite3_io_methods *pMethods; /* Methods for an open file */
  988. };
  989. /*
  990. ** CAPI3REF: OS Interface File Virtual Methods Object
  991. **
  992. ** Every file opened by the [sqlite3_vfs] xOpen method populates an
  993. ** [sqlite3_file] object (or, more commonly, a subclass of the
  994. ** [sqlite3_file] object) with a pointer to an instance of this object.
  995. ** This object defines the methods used to perform various operations
  996. ** against the open file represented by the [sqlite3_file] object.
  997. **
  998. ** If the xOpen method sets the sqlite3_file.pMethods element
  999. ** to a non-NULL pointer, then the sqlite3_io_methods.xClose method
  1000. ** may be invoked even if the xOpen reported that it failed. The
  1001. ** only way to prevent a call to xClose following a failed xOpen
  1002. ** is for the xOpen to set the sqlite3_file.pMethods element to NULL.
  1003. **
  1004. ** The flags argument to xSync may be one of [SQLITE_SYNC_NORMAL] or
  1005. ** [SQLITE_SYNC_FULL]. The first choice is the normal fsync().
  1006. ** The second choice is a Mac OS X style fullsync. The [SQLITE_SYNC_DATAONLY]
  1007. ** flag may be ORed in to indicate that only the data of the file
  1008. ** and not its inode needs to be synced.
  1009. **
  1010. ** The integer values to xLock() and xUnlock() are one of
  1011. ** <ul>
  1012. ** <li> [SQLITE_LOCK_NONE],
  1013. ** <li> [SQLITE_LOCK_SHARED],
  1014. ** <li> [SQLITE_LOCK_RESERVED],
  1015. ** <li> [SQLITE_LOCK_PENDING], or
  1016. ** <li> [SQLITE_LOCK_EXCLUSIVE].
  1017. ** </ul>
  1018. ** xLock() increases the lock. xUnlock() decreases the lock.
  1019. ** The xCheckReservedLock() method checks whether any database connection,
  1020. ** either in this process or in some other process, is holding a RESERVED,
  1021. ** PENDING, or EXCLUSIVE lock on the file. It returns true
  1022. ** if such a lock exists and false otherwise.
  1023. **
  1024. ** The xFileControl() method is a generic interface that allows custom
  1025. ** VFS implementations to directly control an open file using the
  1026. ** [sqlite3_file_control()] interface. The second "op" argument is an
  1027. ** integer opcode. The third argument is a generic pointer intended to
  1028. ** point to a structure that may contain arguments or space in which to
  1029. ** write return values. Potential uses for xFileControl() might be
  1030. ** functions to enable blocking locks with timeouts, to change the
  1031. ** locking strategy (for example to use dot-file locks), to inquire
  1032. ** about the status of a lock, or to break stale locks. The SQLite
  1033. ** core reserves all opcodes less than 100 for its own use.
  1034. ** A [SQLITE_FCNTL_LOCKSTATE | list of opcodes] less than 100 is available.
  1035. ** Applications that define a custom xFileControl method should use opcodes
  1036. ** greater than 100 to avoid conflicts.
  1037. **
  1038. ** The xSectorSize() method returns the sector size of the
  1039. ** device that underlies the file. The sector size is the
  1040. ** minimum write that can be performed without disturbing
  1041. ** other bytes in the file. The xDeviceCharacteristics()
  1042. ** method returns a bit vector describing behaviors of the
  1043. ** underlying device:
  1044. **
  1045. ** <ul>
  1046. ** <li> [SQLITE_IOCAP_ATOMIC]
  1047. ** <li> [SQLITE_IOCAP_ATOMIC512]
  1048. ** <li> [SQLITE_IOCAP_ATOMIC1K]
  1049. ** <li> [SQLITE_IOCAP_ATOMIC2K]
  1050. ** <li> [SQLITE_IOCAP_ATOMIC4K]
  1051. ** <li> [SQLITE_IOCAP_ATOMIC8K]
  1052. ** <li> [SQLITE_IOCAP_ATOMIC16K]
  1053. ** <li> [SQLITE_IOCAP_ATOMIC32K]
  1054. ** <li> [SQLITE_IOCAP_ATOMIC64K]
  1055. ** <li> [SQLITE_IOCAP_SAFE_APPEND]
  1056. ** <li> [SQLITE_IOCAP_SEQUENTIAL]
  1057. ** </ul>
  1058. **
  1059. ** The SQLITE_IOCAP_ATOMIC property means that all writes of
  1060. ** any size are atomic. The SQLITE_IOCAP_ATOMICnnn values
  1061. ** mean that writes of blocks that are nnn bytes in size and
  1062. ** are aligned to an address which is an integer multiple of
  1063. ** nnn are atomic. The SQLITE_IOCAP_SAFE_APPEND value means
  1064. ** that when data is appended to a file, the data is appended
  1065. ** first then the size of the file is extended, never the other
  1066. ** way around. The SQLITE_IOCAP_SEQUENTIAL property means that
  1067. ** information is written to disk in the same order as calls
  1068. ** to xWrite().
  1069. **
  1070. ** If xRead() returns SQLITE_IOERR_SHORT_READ it must also fill
  1071. ** in the unread portions of the buffer with zeros. A VFS that
  1072. ** fails to zero-fill short reads might seem to work. However,
  1073. ** failure to zero-fill short reads will eventually lead to
  1074. ** database corruption.
  1075. */
  1076. typedef struct sqlite3_io_methods sqlite3_io_methods;
  1077. struct sqlite3_io_methods {
  1078. int iVersion;
  1079. int (*xClose)(sqlite3_file*);
  1080. int (*xRead)(sqlite3_file*, void*, int iAmt, sqlite3_int64 iOfst);
  1081. int (*xWrite)(sqlite3_file*, const void*, int iAmt, sqlite3_int64 iOfst);
  1082. int (*xTruncate)(sqlite3_file*, sqlite3_int64 size);
  1083. int (*xSync)(sqlite3_file*, int flags);
  1084. int (*xFileSize)(sqlite3_file*, sqlite3_int64 *pSize);
  1085. int (*xLock)(sqlite3_file*, int);
  1086. int (*xUnlock)(sqlite3_file*, int);
  1087. int (*xCheckReservedLock)(sqlite3_file*, int *pResOut);
  1088. int (*xFileControl)(sqlite3_file*, int op, void *pArg);
  1089. int (*xSectorSize)(sqlite3_file*);
  1090. int (*xDeviceCharacteristics)(sqlite3_file*);
  1091. /* Additional methods may be added in future releases */
  1092. };
  1093. /*
  1094. ** CAPI3REF: Standard File Control Opcodes
  1095. **
  1096. ** These integer constants are opcodes for the xFileControl method
  1097. ** of the [sqlite3_io_methods] object and for the [sqlite3_file_control()]
  1098. ** interface.
  1099. **
  1100. ** The [SQLITE_FCNTL_LOCKSTATE] opcode is used for debugging. This
  1101. ** opcode causes the xFileControl method to write the current state of
  1102. ** the lock (one of [SQLITE_LOCK_NONE], [SQLITE_LOCK_SHARED],
  1103. ** [SQLITE_LOCK_RESERVED], [SQLITE_LOCK_PENDING], or [SQLITE_LOCK_EXCLUSIVE])
  1104. ** into an integer that the pArg argument points to. This capability
  1105. ** is used during testing and only needs to be supported when SQLITE_TEST
  1106. ** is defined.
  1107. */
  1108. #define SQLITE_FCNTL_LOCKSTATE 1
  1109. #define SQLITE_GET_LOCKPROXYFILE 2
  1110. #define SQLITE_SET_LOCKPROXYFILE 3
  1111. #define SQLITE_LAST_ERRNO 4
  1112. /*
  1113. ** CAPI3REF: Mutex Handle
  1114. **
  1115. ** The mutex module within SQLite defines [sqlite3_mutex] to be an
  1116. ** abstract type for a mutex object. The SQLite core never looks
  1117. ** at the internal representation of an [sqlite3_mutex]. It only
  1118. ** deals with pointers to the [sqlite3_mutex] object.
  1119. **
  1120. ** Mutexes are created using [sqlite3_mutex_alloc()].
  1121. */
  1122. typedef struct sqlite3_mutex sqlite3_mutex;
  1123. /*
  1124. ** CAPI3REF: OS Interface Object
  1125. **
  1126. ** An instance of the sqlite3_vfs object defines the interface between
  1127. ** the SQLite core and the underlying operating system. The "vfs"
  1128. ** in the name of the object stands for "virtual file system".
  1129. **
  1130. ** The value of the iVersion field is initially 1 but may be larger in
  1131. ** future versions of SQLite. Additional fields may be appended to this
  1132. ** object when the iVersion value is increased. Note that the structure
  1133. ** of the sqlite3_vfs object changes in the transaction between
  1134. ** SQLite version 3.5.9 and 3.6.0 and yet the iVersion field was not
  1135. ** modified.
  1136. **
  1137. ** The szOsFile field is the size of the subclassed [sqlite3_file]
  1138. ** structure used by this VFS. mxPathname is the maximum length of
  1139. ** a pathname in this VFS.
  1140. **
  1141. ** Registered sqlite3_vfs objects are kept on a linked list formed by
  1142. ** the pNext pointer. The [sqlite3_vfs_register()]
  1143. ** and [sqlite3_vfs_unregister()] interfaces manage this list
  1144. ** in a thread-safe way. The [sqlite3_vfs_find()] interface
  1145. ** searches the list. Neither the application code nor the VFS
  1146. ** implementation should use the pNext pointer.
  1147. **
  1148. ** The pNext field is the only field in the sqlite3_vfs
  1149. ** structure that SQLite will ever modify. SQLite will only access
  1150. ** or modify this field while holding a particular static mutex.
  1151. ** The application should never modify anything within the sqlite3_vfs
  1152. ** object once the object has been registered.
  1153. **
  1154. ** The zName field holds the name of the VFS module. The name must
  1155. ** be unique across all VFS modules.
  1156. **
  1157. ** SQLite will guarantee that the zFilename parameter to xOpen
  1158. ** is either a NULL pointer or string obtained
  1159. ** from xFullPathname(). SQLite further guarantees that
  1160. ** the string will be valid and unchanged until xClose() is
  1161. ** called. Because of the previous sentence,
  1162. ** the [sqlite3_file] can safely store a pointer to the
  1163. ** filename if it needs to remember the filename for some reason.
  1164. ** If the zFilename parameter is xOpen is a NULL pointer then xOpen
  1165. ** must invent its own temporary name for the file. Whenever the
  1166. ** xFilename parameter is NULL it will also be the case that the
  1167. ** flags parameter will include [SQLITE_OPEN_DELETEONCLOSE].
  1168. **
  1169. ** The flags argument to xOpen() includes all bits set in
  1170. ** the flags argument to [sqlite3_open_v2()]. Or if [sqlite3_open()]
  1171. ** or [sqlite3_open16()] is used, then flags includes at least
  1172. ** [SQLITE_OPEN_READWRITE] | [SQLITE_OPEN_CREATE].
  1173. ** If xOpen() opens a file read-only then it sets *pOutFlags to
  1174. ** include [SQLITE_OPEN_READONLY]. Other bits in *pOutFlags may be set.
  1175. **
  1176. ** SQLite will also add one of the following flags to the xOpen()
  1177. ** call, depending on the object being opened:
  1178. **
  1179. ** <ul>
  1180. ** <li> [SQLITE_OPEN_MAIN_DB]
  1181. ** <li> [SQLITE_OPEN_MAIN_JOURNAL]
  1182. ** <li> [SQLITE_OPEN_TEMP_DB]
  1183. ** <li> [SQLITE_OPEN_TEMP_JOURNAL]
  1184. ** <li> [SQLITE_OPEN_TRANSIENT_DB]
  1185. ** <li> [SQLITE_OPEN_SUBJOURNAL]
  1186. ** <li> [SQLITE_OPEN_MASTER_JOURNAL]
  1187. ** </ul>
  1188. **
  1189. ** The file I/O implementation can use the object type flags to
  1190. ** change the way it deals with files. For example, an application
  1191. ** that does not care about crash recovery or rollback might make
  1192. ** the open of a journal file a no-op. Writes to this journal would
  1193. ** also be no-ops, and any attempt to read the journal would return
  1194. ** SQLITE_IOERR. Or the implementation might recognize that a database
  1195. ** file will be doing page-aligned sector reads and writes in a random
  1196. ** order and set up its I/O subsystem accordingly.
  1197. **
  1198. ** SQLite might also add one of the following flags to the xOpen method:
  1199. **
  1200. ** <ul>
  1201. ** <li> [SQLITE_OPEN_DELETEONCLOSE]
  1202. ** <li> [SQLITE_OPEN_EXCLUSIVE]
  1203. ** </ul>
  1204. **
  1205. ** The [SQLITE_OPEN_DELETEONCLOSE] flag means the file should be
  1206. ** deleted when it is closed. The [SQLITE_OPEN_DELETEONCLOSE]
  1207. ** will be set for TEMP databases, journals and for subjournals.
  1208. **
  1209. ** The [SQLITE_OPEN_EXCLUSIVE] flag is always used in conjunction
  1210. ** with the [SQLITE_OPEN_CREATE] flag, which are both directly
  1211. ** analogous to the O_EXCL and O_CREAT flags of the POSIX open()
  1212. ** API. The SQLITE_OPEN_EXCLUSIVE flag, when paired with the
  1213. ** SQLITE_OPEN_CREATE, is used to indicate that file should always
  1214. ** be created, and that it is an error if it already exists.
  1215. ** It is <i>not</i> used to indicate the file should be opened
  1216. ** for exclusive access.
  1217. **
  1218. ** At least szOsFile bytes of memory are allocated by SQLite
  1219. ** to hold the [sqlite3_file] structure passed as the third
  1220. ** argument to xOpen. The xOpen method does not have to
  1221. ** allocate the structure; it should just fill it in. Note that
  1222. ** the xOpen method must set the sqlite3_file.pMethods to either
  1223. ** a valid [sqlite3_io_methods] object or to NULL. xOpen must do
  1224. ** this even if the open fails. SQLite expects that the sqlite3_file.pMethods
  1225. ** element will be valid after xOpen returns regardless of the success
  1226. ** or failure of the xOpen call.
  1227. **
  1228. ** The flags argument to xAccess() may be [SQLITE_ACCESS_EXISTS]
  1229. ** to test for the existence of a file, or [SQLITE_ACCESS_READWRITE] to
  1230. ** test whether a file is readable and writable, or [SQLITE_ACCESS_READ]
  1231. ** to test whether a file is at least readable. The file can be a
  1232. ** directory.
  1233. **
  1234. ** SQLite will always allocate at least mxPathname+1 bytes for the
  1235. ** output buffer xFullPathname. The exact size of the output buffer
  1236. ** is also passed as a parameter to both methods. If the output buffer
  1237. ** is not large enough, [SQLITE_CANTOPEN] should be returned. Since this is
  1238. ** handled as a fatal error by SQLite, vfs implementations should endeavor
  1239. ** to prevent this by setting mxPathname to a sufficiently large value.
  1240. **
  1241. ** The xRandomness(), xSleep(), and xCurrentTime() interfaces
  1242. ** are not strictly a part of the filesystem, but they are
  1243. ** included in the VFS structure for completeness.
  1244. ** The xRandomness() function attempts to return nBytes bytes
  1245. ** of good-quality randomness into zOut. The return value is
  1246. ** the actual number of bytes of randomness obtained.
  1247. ** The xSleep() method causes the calling thread to sleep for at
  1248. ** least the number of microseconds given. The xCurrentTime()
  1249. ** method returns a Julian Day Number for the current date and time.
  1250. **
  1251. */
  1252. typedef struct sqlite3_vfs sqlite3_vfs;
  1253. struct sqlite3_vfs {
  1254. int iVersion; /* Structure version number */
  1255. int szOsFile; /* Size of subclassed sqlite3_file */
  1256. int mxPathname; /* Maximum file pathname length */
  1257. sqlite3_vfs *pNext; /* Next registered VFS */
  1258. const char *zName; /* Name of this virtual file system */
  1259. void *pAppData; /* Pointer to application-specific data */
  1260. int (*xOpen)(sqlite3_vfs*, const char *zName, sqlite3_file*,
  1261. int flags, int *pOutFlags);
  1262. int (*xDelete)(sqlite3_vfs*, const char *zName, int syncDir);
  1263. int (*xAccess)(sqlite3_vfs*, const char *zName, int flags, int *pResOut);
  1264. int (*xFullPathname)(sqlite3_vfs*, const char *zName, int nOut, char *zOut);
  1265. void *(*xDlOpen)(sqlite3_vfs*, const char *zFilename);
  1266. void (*xDlError)(sqlite3_vfs*, int nByte, char *zErrMsg);
  1267. void (*(*xDlSym)(sqlite3_vfs*,void*, const char *zSymbol))(void);
  1268. void (*xDlClose)(sqlite3_vfs*, void*);
  1269. int (*xRandomness)(sqlite3_vfs*, int nByte, char *zOut);
  1270. int (*xSleep)(sqlite3_vfs*, int microseconds);
  1271. int (*xCurrentTime)(sqlite3_vfs*, double*);
  1272. int (*xGetLastError)(sqlite3_vfs*, int, char *);
  1273. /* New fields may be appended in figure versions. The iVersion
  1274. ** value will increment whenever this happens. */
  1275. };
  1276. /*
  1277. ** CAPI3REF: Flags for the xAccess VFS method
  1278. **
  1279. ** These integer constants can be used as the third parameter to
  1280. ** the xAccess method of an [sqlite3_vfs] object. They determine
  1281. ** what kind of permissions the xAccess method is looking for.
  1282. ** With SQLITE_ACCESS_EXISTS, the xAccess method
  1283. ** simply checks whether the file exists.
  1284. ** With SQLITE_ACCESS_READWRITE, the xAccess method
  1285. ** checks whether the file is both readable and writable.
  1286. ** With SQLITE_ACCESS_READ, the xAccess method
  1287. ** checks whether the file is readable.
  1288. */
  1289. #define SQLITE_ACCESS_EXISTS 0
  1290. #define SQLITE_ACCESS_READWRITE 1
  1291. #define SQLITE_ACCESS_READ 2
  1292. /*
  1293. ** CAPI3REF: Initialize The SQLite Library
  1294. **
  1295. ** ^The sqlite3_initialize() routine initializes the
  1296. ** SQLite library. ^The sqlite3_shutdown() routine
  1297. ** deallocates any resources that were allocated by sqlite3_initialize().
  1298. ** These routines are designed to aid in process initialization and
  1299. ** shutdown on embedded systems. Workstation applications using
  1300. ** SQLite normally do not need to invoke either of these routines.
  1301. **
  1302. ** A call to sqlite3_initialize() is an "effective" call if it is
  1303. ** the first time sqlite3_initialize() is invoked during the lifetime of
  1304. ** the process, or if it is the first time sqlite3_initialize() is invoked
  1305. ** following a call to sqlite3_shutdown(). ^(Only an effective call
  1306. ** of sqlite3_initialize() does any initialization. All other calls
  1307. ** are harmless no-ops.)^
  1308. **
  1309. ** A call to sqlite3_shutdown() is an "effective" call if it is the first
  1310. ** call to sqlite3_shutdown() since the last sqlite3_initialize(). ^(Only
  1311. ** an effective call to sqlite3_shutdown() does any deinitialization.
  1312. ** All other valid calls to sqlite3_shutdown() are harmless no-ops.)^
  1313. **
  1314. ** The sqlite3_initialize() interface is threadsafe, but sqlite3_shutdown()
  1315. ** is not. The sqlite3_shutdown() interface must only be called from a
  1316. ** single thread. All open [database connections] must be closed and all
  1317. ** other SQLite resources must be deallocated prior to invoking
  1318. ** sqlite3_shutdown().
  1319. **
  1320. ** Among other things, ^sqlite3_initialize() will invoke
  1321. ** sqlite3_os_init(). Similarly, ^sqlite3_shutdown()
  1322. ** will invoke sqlite3_os_end().
  1323. **
  1324. ** ^The sqlite3_initialize() routine returns [SQLITE_OK] on success.
  1325. ** ^If for some reason, sqlite3_initialize() is unable to initialize
  1326. ** the library (perhaps it is unable to allocate a needed resource such
  1327. ** as a mutex) it returns an [error code] other than [SQLITE_OK].
  1328. **
  1329. ** ^The sqlite3_initialize() routine is called internally by many other
  1330. ** SQLite interfaces so that an application usually does not need to
  1331. ** invoke sqlite3_initialize() directly. For example, [sqlite3_open()]
  1332. ** calls sqlite3_initialize() so the SQLite library will be automatically
  1333. ** initialized when [sqlite3_open()] is called if it has not be initialized
  1334. ** already. ^However, if SQLite is compiled with the [SQLITE_OMIT_AUTOINIT]
  1335. ** compile-time option, then the automatic calls to sqlite3_initialize()
  1336. ** are omitted and the application must call sqlite3_initialize() directly
  1337. ** prior to using any other SQLite interface. For maximum portability,
  1338. ** it is recommended that applications always invoke sqlite3_initialize()
  1339. ** directly prior to using any other SQLite interface. Future releases
  1340. ** of SQLite may require this. In other words, the behavior exhibited
  1341. ** when SQLite is compiled with [SQLITE_OMIT_AUTOINIT] might become the
  1342. ** default behavior in some future release of SQLite.
  1343. **
  1344. ** The sqlite3_os_init() routine does operating-system specific
  1345. ** initialization of the SQLite library. The sqlite3_os_end()
  1346. ** routine undoes the effect of sqlite3_os_init(). Typical tasks
  1347. ** performed by these routines include allocation or deallocation
  1348. ** of static resources, initialization of global variables,
  1349. ** setting up a default [sqlite3_vfs] module, or setting up
  1350. ** a default configuration using [sqlite3_config()].
  1351. **
  1352. ** The application should never invoke either sqlite3_os_init()
  1353. ** or sqlite3_os_end() directly. The application should only invoke
  1354. ** sqlite3_initialize() and sqlite3_shutdown(). The sqlite3_os_init()
  1355. ** interface is called automatically by sqlite3_initialize() and
  1356. ** sqlite3_os_end() is called by sqlite3_shutdown(). Appropriate
  1357. ** implementations for sqlite3_os_init() and sqlite3_os_end()
  1358. ** are built into SQLite when it is compiled for Unix, Windows, or OS/2.
  1359. ** When [custom builds | built for other platforms]
  1360. ** (using the [SQLITE_OS_OTHER=1] compile-time
  1361. ** option) the application must supply a suitable implementation for
  1362. ** sqlite3_os_init() and sqlite3_os_end(). An application-supplied
  1363. ** implementation of sqlite3_os_init() or sqlite3_os_end()
  1364. ** must return [SQLITE_OK] on success and some other [error code] upon
  1365. ** failure.
  1366. */
  1367. SQLITE_API int sqlite3_initialize(void);
  1368. SQLITE_API int sqlite3_shutdown(void);
  1369. SQLITE_API int sqlite3_os_init(void);
  1370. SQLITE_API int sqlite3_os_end(void);
  1371. /*
  1372. ** CAPI3REF: Configuring The SQLite Library
  1373. ** EXPERIMENTAL
  1374. **
  1375. ** The sqlite3_config() interface is used to make global configuration
  1376. ** changes to SQLite in order to tune SQLite to the specific needs of
  1377. ** the application. The default configuration is recommended for most
  1378. ** applications and so this routine is usually not necessary. It is
  1379. ** provided to support rare applications with unusual needs.
  1380. **
  1381. ** The sqlite3_config() interface is not threadsafe. The application
  1382. ** must insure that no other SQLite interfaces are invoked by other
  1383. ** threads while sqlite3_config() is running. Furthermore, sqlite3_config()
  1384. ** may only be invoked prior to library initialization using
  1385. ** [sqlite3_initialize()] or after shutdown by [sqlite3_shutdown()].
  1386. ** ^If sqlite3_config() is called after [sqlite3_initialize()] and before
  1387. ** [sqlite3_shutdown()] then it will return SQLITE_MISUSE.
  1388. ** Note, however, that ^sqlite3_config() can be called as part of the
  1389. ** implementation of an application-defined [sqlite3_os_init()].
  1390. **
  1391. ** The first argument to sqlite3_config() is an integer
  1392. ** [SQLITE_CONFIG_SINGLETHREAD | configuration option] that determines
  1393. ** what property of SQLite is to be configured. Subsequent arguments
  1394. ** vary depending on the [SQLITE_CONFIG_SINGLETHREAD | configuration option]
  1395. ** in the first argument.
  1396. **
  1397. ** ^When a configuration option is set, sqlite3_config() returns [SQLITE_OK].
  1398. ** ^If the option is unknown or SQLite is unable to set the option
  1399. ** then this routine returns a non-zero [error code].
  1400. */
  1401. SQLITE_API SQLITE_EXPERIMENTAL int sqlite3_config(int, ...);
  1402. /*
  1403. ** CAPI3REF: Configure database connections
  1404. ** EXPERIMENTAL
  1405. **
  1406. ** The sqlite3_db_config() interface is used to make configuration
  1407. ** changes to a [database connection]. The interface is similar to
  1408. ** [sqlite3_config()] except that the changes apply to a single
  1409. ** [database connection] (specified in the first argument). The
  1410. ** sqlite3_db_config() interface should only be used immediately after
  1411. ** the database connection is created using [sqlite3_open()],
  1412. ** [sqlite3_open16()], or [sqlite3_open_v2()].
  1413. **
  1414. ** The second argument to sqlite3_db_config(D,V,...) is the
  1415. ** configuration verb - an integer code that indicates what
  1416. ** aspect of the [database connection] is being configured.
  1417. ** The only choice for this value is [SQLITE_DBCONFIG_LOOKASIDE].
  1418. ** New verbs are likely to be added in future releases of SQLite.
  1419. ** Additional arguments depend on the verb.
  1420. **
  1421. ** ^Calls to sqlite3_db_config() return SQLITE_OK if and only if
  1422. ** the call is considered successful.
  1423. */
  1424. SQLITE_API SQLITE_EXPERIMENTAL int sqlite3_db_config(sqlite3*, int op, ...);
  1425. /*
  1426. ** CAPI3REF: Memory Allocation Routines
  1427. ** EXPERIMENTAL
  1428. **
  1429. ** An instance of this object defines the interface between SQLite
  1430. ** and low-level memory allocation routines.
  1431. **
  1432. ** This object is used in only one place in the SQLite interface.
  1433. ** A pointer to an instance of this object is the argument to
  1434. ** [sqlite3_config()] when the configuration option is
  1435. ** [SQLITE_CONFIG_MALLOC] or [SQLITE_CONFIG_GETMALLOC].
  1436. ** By creating an instance of this object
  1437. ** and passing it to [sqlite3_config]([SQLITE_CONFIG_MALLOC])
  1438. ** during configuration, an application can specify an alternative
  1439. ** memory allocation subsystem for SQLite to use for all of its
  1440. ** dynamic memory needs.
  1441. **
  1442. ** Note that SQLite comes with several [built-in memory allocators]
  1443. ** that are perfectly adequate for the overwhelming majority of applications
  1444. ** and that this object is only useful to a tiny minority of applications
  1445. ** with specialized memory allocation requirements. This object is
  1446. ** also used during testing of SQLite in order to specify an alternative
  1447. ** memory allocator that simulates memory out-of-memory conditions in
  1448. ** order to verify that SQLite recovers gracefully from such
  1449. ** conditions.
  1450. **
  1451. ** The xMalloc and xFree methods must work like the
  1452. ** malloc() and free() functions from the standard C library.
  1453. ** The xRealloc method must work like realloc() from the standard C library
  1454. ** with the exception that if the second argument to xRealloc is zero,
  1455. ** xRealloc must be a no-op - it must not perform any allocation or
  1456. ** deallocation. ^SQLite guarantees that the second argument to
  1457. ** xRealloc is always a value returned by a prior call to xRoundup.
  1458. ** And so in cases where xRoundup always returns a positive number,
  1459. ** xRealloc can perform exactly as the standard library realloc() and
  1460. ** still be in compliance with this specification.
  1461. **
  1462. ** xSize should return the allocated size of a memory allocation
  1463. ** previously obtained from xMalloc or xRealloc. The allocated size
  1464. ** is always at least as big as the requested size but may be larger.
  1465. **
  1466. ** The xRoundup method returns what would be the allocated size of
  1467. ** a memory allocation given a particular requested size. Most memory
  1468. ** allocators round up memory allocations at least to the next multiple
  1469. ** of 8. Some allocators round up to a larger multiple or to a power of 2.
  1470. ** Every memory allocation request coming in through [sqlite3_malloc()]
  1471. ** or [sqlite3_realloc()] first calls xRoundup. If xRoundup returns 0,
  1472. ** that causes the corresponding memory allocation to fail.
  1473. **
  1474. ** The xInit method initializes the memory allocator. (For example,
  1475. ** it might allocate any require mutexes or initialize internal data
  1476. ** structures. The xShutdown method is invoked (indirectly) by
  1477. ** [sqlite3_shutdown()] and should deallocate any resources acquired
  1478. ** by xInit. The pAppData pointer is used as the only parameter to
  1479. ** xInit and xShutdown.
  1480. **
  1481. ** SQLite holds the [SQLITE_MUTEX_STATIC_MASTER] mutex when it invokes
  1482. ** the xInit method, so the xInit method need not be threadsafe. The
  1483. ** xShutdown method is only called from [sqlite3_shutdown()] so it does
  1484. ** not need to be threadsafe either. For all other methods, SQLite
  1485. ** holds the [SQLITE_MUTEX_STATIC_MEM] mutex as long as the
  1486. ** [SQLITE_CONFIG_MEMSTATUS] configuration option is turned on (which
  1487. ** it is by default) and so the methods are automatically serialized.
  1488. ** However, if [SQLITE_CONFIG_MEMSTATUS] is disabled, then the other
  1489. ** methods must be threadsafe or else make their own arrangements for
  1490. ** serialization.
  1491. **
  1492. ** SQLite will never invoke xInit() more than once without an intervening
  1493. ** call to xShutdown().
  1494. */
  1495. typedef struct sqlite3_mem_methods sqlite3_mem_methods;
  1496. struct sqlite3_mem_methods {
  1497. void *(*xMalloc)(int); /* Memory allocation function */
  1498. void (*xFree)(void*); /* Free a prior allocation */
  1499. void *(*xRealloc)(void*,int); /* Resize an allocation */
  1500. int (*xSize)(void*); /* Return the size of an allocation */
  1501. int (*xRoundup)(int); /* Round up request size to allocation size */
  1502. int (*xInit)(void*); /* Initialize the memory allocator */
  1503. void (*xShutdown)(void*); /* Deinitialize the memory allocator */
  1504. void *pAppData; /* Argument to xInit() and xShutdown() */
  1505. };
  1506. /*
  1507. ** CAPI3REF: Configuration Options
  1508. ** EXPERIMENTAL
  1509. **
  1510. ** These constants are the available integer configuration options that
  1511. ** can be passed as the first argument to the [sqlite3_config()] interface.
  1512. **
  1513. ** New configuration options may be added in future releases of SQLite.
  1514. ** Existing configuration options might be discontinued. Applications
  1515. ** should check the return code from [sqlite3_config()] to make sure that
  1516. ** the call worked. The [sqlite3_config()] interface will return a
  1517. ** non-zero [error code] if a discontinued or unsupported configuration option
  1518. ** is invoked.
  1519. **
  1520. ** <dl>
  1521. ** <dt>SQLITE_CONFIG_SINGLETHREAD</dt>
  1522. ** <dd>There are no arguments to this option. ^This option sets the
  1523. ** [threading mode] to Single-thread. In other words, it disables
  1524. ** all mutexing and puts SQLite into a mode where it can only be used
  1525. ** by a single thread. ^If SQLite is compiled with
  1526. ** the [SQLITE_THREADSAFE | SQLITE_THREADSAFE=0] compile-time option then
  1527. ** it is not possible to change the [threading mode] from its default
  1528. ** value of Single-thread and so [sqlite3_config()] will return
  1529. ** [SQLITE_ERROR] if called with the SQLITE_CONFIG_SINGLETHREAD
  1530. ** configuration option.</dd>
  1531. **
  1532. ** <dt>SQLITE_CONFIG_MULTITHREAD</dt>
  1533. ** <dd>There are no arguments to this option. ^This option sets the
  1534. ** [threading mode] to Multi-thread. In other words, it disables
  1535. ** mutexing on [database connection] and [prepared statement] objects.
  1536. ** The application is responsible for serializing access to
  1537. ** [database connections] and [prepared statements]. But other mutexes
  1538. ** are enabled so that SQLite will be safe to use in a multi-threaded
  1539. ** environment as long as no two threads attempt to use the same
  1540. ** [database connection] at the same time. ^If SQLite is compiled with
  1541. ** the [SQLITE_THREADSAFE | SQLITE_THREADSAFE=0] compile-time option then
  1542. ** it is not possible to set the Multi-thread [threading mode] and
  1543. ** [sqlite3_config()] will return [SQLITE_ERROR] if called with the
  1544. ** SQLITE_CONFIG_MULTITHREAD configuration option.</dd>
  1545. **
  1546. ** <dt>SQLITE_CONFIG_SERIALIZED</dt>
  1547. ** <dd>There are no arguments to this option. ^This option sets the
  1548. ** [threading mode] to Serialized. In other words, this option enables
  1549. ** all mutexes including the recursive
  1550. ** mutexes on [database connection] and [prepared statement] objects.
  1551. ** In this mode (which is the default when SQLite is compiled with
  1552. ** [SQLITE_THREADSAFE=1]) the SQLite library will itself serialize access
  1553. ** to [database connections] and [prepared statements] so that the
  1554. ** application is free to use the same [database connection] or the
  1555. ** same [prepared statement] in different threads at the same time.
  1556. ** ^If SQLite is compiled with
  1557. ** the [SQLITE_THREADSAFE | SQLITE_THREADSAFE=0] compile-time option then
  1558. ** it is not possible to set the Serialized [threading mode] and
  1559. ** [sqlite3_config()] will return [SQLITE_ERROR] if called with the
  1560. ** SQLITE_CONFIG_SERIALIZED configuration option.</dd>
  1561. **
  1562. ** <dt>SQLITE_CONFIG_MALLOC</dt>
  1563. ** <dd> ^(This option takes a single argument which is a pointer to an
  1564. ** instance of the [sqlite3_mem_methods] structure. The argument specifies
  1565. ** alternative low-level memory allocation routines to be used in place of
  1566. ** the memory allocation routines built into SQLite.)^ ^SQLite makes
  1567. ** its own private copy of the content of the [sqlite3_mem_methods] structure
  1568. ** before the [sqlite3_config()] call returns.</dd>
  1569. **
  1570. ** <dt>SQLITE_CONFIG_GETMALLOC</dt>
  1571. ** <dd> ^(This option takes a single argument which is a pointer to an
  1572. ** instance of the [sqlite3_mem_methods] structure. The [sqlite3_mem_methods]
  1573. ** structure is filled with the currently defined memory allocation routines.)^
  1574. ** This option can be used to overload the default memory allocation
  1575. ** routines with a wrapper that simulations memory allocation failure or
  1576. ** tracks memory usage, for example. </dd>
  1577. **
  1578. ** <dt>SQLITE_CONFIG_MEMSTATUS</dt>
  1579. ** <dd> ^This option takes single argument of type int, interpreted as a
  1580. ** boolean, which enables or disables the collection of memory allocation
  1581. ** statistics. ^(When memory allocation statistics are disabled, the
  1582. ** following SQLite interfaces become non-operational:
  1583. ** <ul>
  1584. ** <li> [sqlite3_memory_used()]
  1585. ** <li> [sqlite3_memory_highwater()]
  1586. ** <li> [sqlite3_soft_heap_limit()]
  1587. ** <li> [sqlite3_status()]
  1588. ** </ul>)^
  1589. ** ^Memory allocation statistics are enabled by default unless SQLite is
  1590. ** compiled with [SQLITE_DEFAULT_MEMSTATUS]=0 in which case memory
  1591. ** allocation statistics are disabled by default.
  1592. ** </dd>
  1593. **
  1594. ** <dt>SQLITE_CONFIG_SCRATCH</dt>
  1595. ** <dd> ^This option specifies a static memory buffer that SQLite can use for
  1596. ** scratch memory. There are three arguments: A pointer an 8-byte
  1597. ** aligned memory buffer from which the scrach allocations will be
  1598. ** drawn, the size of each scratch allocation (sz),
  1599. ** and the maximum number of scratch allocations (N). The sz
  1600. ** argument must be a multiple of 16. The sz parameter should be a few bytes
  1601. ** larger than the actual scratch space required due to internal overhead.
  1602. ** The first argument must be a pointer to an 8-byte aligned buffer
  1603. ** of at least sz*N bytes of memory.
  1604. ** ^SQLite will use no more than one scratch buffer per thread. So
  1605. ** N should be set to the expected maximum number of threads. ^SQLite will
  1606. ** never require a scratch buffer that is more than 6 times the database
  1607. ** page size. ^If SQLite needs needs additional scratch memory beyond
  1608. ** what is provided by this configuration option, then
  1609. ** [sqlite3_malloc()] will be used to obtain the memory needed.</dd>
  1610. **
  1611. ** <dt>SQLITE_CONFIG_PAGECACHE</dt>
  1612. ** <dd> ^This option specifies a static memory buffer that SQLite can use for
  1613. ** the database page cache with the default page cache implemenation.
  1614. ** This configuration should not be used if an application-define page
  1615. ** cache implementation is loaded using the SQLITE_CONFIG_PCACHE option.
  1616. ** There are three arguments to this option: A pointer to 8-byte aligned
  1617. ** memory, the size of each page buffer (sz), and the number of pages (N).
  1618. ** The sz argument should be the size of the largest database page
  1619. ** (a power of two between 512 and 32768) plus a little extra for each
  1620. ** page header. ^The page header size is 20 to 40 bytes depending on
  1621. ** the host architecture. ^It is harmless, apart from the wasted memory,
  1622. ** to make sz a little too large. The first
  1623. ** argument should point to an allocation of at least sz*N bytes of memory.
  1624. ** ^SQLite will use the memory provided by the first argument to satisfy its
  1625. ** memory needs for the first N pages that it adds to cache. ^If additional
  1626. ** page cache memory is needed beyond what is provided by this option, then
  1627. ** SQLite goes to [sqlite3_malloc()] for the additional storage space.
  1628. ** ^The implementation might use one or more of the N buffers to hold
  1629. ** memory accounting information. The pointer in the first argument must
  1630. ** be aligned to an 8-byte boundary or subsequent behavior of SQLite
  1631. ** will be undefined.</dd>
  1632. **
  1633. ** <dt>SQLITE_CONFIG_HEAP</dt>
  1634. ** <dd> ^This option specifies a static memory buffer that SQLite will use
  1635. ** for all of its dynamic memory allocation needs beyond those provided
  1636. ** for by [SQLITE_CONFIG_SCRATCH] and [SQLITE_CONFIG_PAGECACHE].
  1637. ** There are three arguments: An 8-byte aligned pointer to the memory,
  1638. ** the number of bytes in the memory buffer, and the minimum allocation size.
  1639. ** ^If the first pointer (the memory pointer) is NULL, then SQLite reverts
  1640. ** to using its default memory allocator (the system malloc() implementation),
  1641. ** undoing any prior invocation of [SQLITE_CONFIG_MALLOC]. ^If the
  1642. ** memory pointer is not NULL and either [SQLITE_ENABLE_MEMSYS3] or
  1643. ** [SQLITE_ENABLE_MEMSYS5] are defined, then the alternative memory
  1644. ** allocator is engaged to handle all of SQLites memory allocation needs.
  1645. ** The first pointer (the memory pointer) must be aligned to an 8-byte
  1646. ** boundary or subsequent behavior of SQLite will be undefined.</dd>
  1647. **
  1648. ** <dt>SQLITE_CONFIG_MUTEX</dt>
  1649. ** <dd> ^(This option takes a single argument which is a pointer to an
  1650. ** instance of the [sqlite3_mutex_methods] structure. The argument specifies
  1651. ** alternative low-level mutex routines to be used in place
  1652. ** the mutex routines built into SQLite.)^ ^SQLite makes a copy of the
  1653. ** content of the [sqlite3_mutex_methods] structure before the call to
  1654. ** [sqlite3_config()] returns. ^If SQLite is compiled with
  1655. ** the [SQLITE_THREADSAFE | SQLITE_THREADSAFE=0] compile-time option then
  1656. ** the entire mutexing subsystem is omitted from the build and hence calls to
  1657. ** [sqlite3_config()] with the SQLITE_CONFIG_MUTEX configuration option will
  1658. ** return [SQLITE_ERROR].</dd>
  1659. **
  1660. ** <dt>SQLITE_CONFIG_GETMUTEX</dt>
  1661. ** <dd> ^(This option takes a single argument which is a pointer to an
  1662. ** instance of the [sqlite3_mutex_methods] structure. The
  1663. ** [sqlite3_mutex_methods]
  1664. ** structure is filled with the currently defined mutex routines.)^
  1665. ** This option can be used to overload the default mutex allocation
  1666. ** routines with a wrapper used to track mutex usage for performance
  1667. ** profiling or testing, for example. ^If SQLite is compiled with
  1668. ** the [SQLITE_THREADSAFE | SQLITE_THREADSAFE=0] compile-time option then
  1669. ** the entire mutexing subsystem is omitted from the build and hence calls to
  1670. ** [sqlite3_config()] with the SQLITE_CONFIG_GETMUTEX configuration option will
  1671. ** return [SQLITE_ERROR].</dd>
  1672. **
  1673. ** <dt>SQLITE_CONFIG_LOOKASIDE</dt>
  1674. ** <dd> ^(This option takes two arguments that determine the default
  1675. ** memory allocation for the lookaside memory allocator on each
  1676. ** [database connection]. The first argument is the
  1677. ** size of each lookaside buffer slot and the second is the number of
  1678. ** slots allocated to each database connection.)^ ^(This option sets the
  1679. ** <i>default</i> lookaside size. The [SQLITE_DBCONFIG_LOOKASIDE]
  1680. ** verb to [sqlite3_db_config()] can be used to change the lookaside
  1681. ** configuration on individual connections.)^ </dd>
  1682. **
  1683. ** <dt>SQLITE_CONFIG_PCACHE</dt>
  1684. ** <dd> ^(This option takes a single argument which is a pointer to
  1685. ** an [sqlite3_pcache_methods] object. This object specifies the interface
  1686. ** to a custom page cache implementation.)^ ^SQLite makes a copy of the
  1687. ** object and uses it for page cache memory allocations.</dd>
  1688. **
  1689. ** <dt>SQLITE_CONFIG_GETPCACHE</dt>
  1690. ** <dd> ^(This option takes a single argument which is a pointer to an
  1691. ** [sqlite3_pcache_methods] object. SQLite copies of the current
  1692. ** page cache implementation into that object.)^ </dd>
  1693. **
  1694. ** </dl>
  1695. */
  1696. #define SQLITE_CONFIG_SINGLETHREAD 1 /* nil */
  1697. #define SQLITE_CONFIG_MULTITHREAD 2 /* nil */
  1698. #define SQLITE_CONFIG_SERIALIZED 3 /* nil */
  1699. #define SQLITE_CONFIG_MALLOC 4 /* sqlite3_mem_methods* */
  1700. #define SQLITE_CONFIG_GETMALLOC 5 /* sqlite3_mem_methods* */
  1701. #define SQLITE_CONFIG_SCRATCH 6 /* void*, int sz, int N */
  1702. #define SQLITE_CONFIG_PAGECACHE 7 /* void*, int sz, int N */
  1703. #define SQLITE_CONFIG_HEAP 8 /* void*, int nByte, int min */
  1704. #define SQLITE_CONFIG_MEMSTATUS 9 /* boolean */
  1705. #define SQLITE_CONFIG_MUTEX 10 /* sqlite3_mutex_methods* */
  1706. #define SQLITE_CONFIG_GETMUTEX 11 /* sqlite3_mutex_methods* */
  1707. /* previously SQLITE_CONFIG_CHUNKALLOC 12 which is now unused. */
  1708. #define SQLITE_CONFIG_LOOKASIDE 13 /* int int */
  1709. #define SQLITE_CONFIG_PCACHE 14 /* sqlite3_pcache_methods* */
  1710. #define SQLITE_CONFIG_GETPCACHE 15 /* sqlite3_pcache_methods* */
  1711. /*
  1712. ** CAPI3REF: Configuration Options
  1713. ** EXPERIMENTAL
  1714. **
  1715. ** These constants are the available integer configuration options that
  1716. ** can be passed as the second argument to the [sqlite3_db_config()] interface.
  1717. **
  1718. ** New configuration options may be added in future releases of SQLite.
  1719. ** Existing configuration options might be discontinued. Applications
  1720. ** should check the return code from [sqlite3_db_config()] to make sure that
  1721. ** the call worked. ^The [sqlite3_db_config()] interface will return a
  1722. ** non-zero [error code] if a discontinued or unsupported configuration option
  1723. ** is invoked.
  1724. **
  1725. ** <dl>
  1726. ** <dt>SQLITE_DBCONFIG_LOOKASIDE</dt>
  1727. ** <dd> ^This option takes three additional arguments that determine the
  1728. ** [lookaside memory allocator] configuration for the [database connection].
  1729. ** ^The first argument (the third parameter to [sqlite3_db_config()] is a
  1730. ** pointer to an memory buffer to use for lookaside memory.
  1731. ** ^The first argument after the SQLITE_DBCONFIG_LOOKASIDE verb
  1732. ** may be NULL in which case SQLite will allocate the
  1733. ** lookaside buffer itself using [sqlite3_malloc()]. ^The second argument is the
  1734. ** size of each lookaside buffer slot. ^The third argument is the number of
  1735. ** slots. The size of the buffer in the first argument must be greater than
  1736. ** or equal to the product of the second and third arguments. The buffer
  1737. ** must be aligned to an 8-byte boundary. ^If the second argument to
  1738. ** SQLITE_DBCONFIG_LOOKASIDE is not a multiple of 8, it is internally
  1739. ** rounded down to the next smaller
  1740. ** multiple of 8. See also: [SQLITE_CONFIG_LOOKASIDE]</dd>
  1741. **
  1742. ** </dl>
  1743. */
  1744. #define SQLITE_DBCONFIG_LOOKASIDE 1001 /* void* int int */
  1745. /*
  1746. ** CAPI3REF: Enable Or Disable Extended Result Codes
  1747. **
  1748. ** ^The sqlite3_extended_result_codes() routine enables or disables the
  1749. ** [extended result codes] feature of SQLite. ^The extended result
  1750. ** codes are disabled by default for historical compatibility.
  1751. */
  1752. SQLITE_API int sqlite3_extended_result_codes(sqlite3*, int onoff);
  1753. /*
  1754. ** CAPI3REF: Last Insert Rowid
  1755. **
  1756. ** ^Each entry in an SQLite table has a unique 64-bit signed
  1757. ** integer key called the [ROWID | "rowid"]. ^The rowid is always available
  1758. ** as an undeclared column named ROWID, OID, or _ROWID_ as long as those
  1759. ** names are not also used by explicitly declared columns. ^If
  1760. ** the table has a column of type [INTEGER PRIMARY KEY] then that column
  1761. ** is another alias for the rowid.
  1762. **
  1763. ** ^This routine returns the [rowid] of the most recent
  1764. ** successful [INSERT] into the database from the [database connection]
  1765. ** in the first argument. ^If no successful [INSERT]s
  1766. ** have ever occurred on that database connection, zero is returned.
  1767. **
  1768. ** ^(If an [INSERT] occurs within a trigger, then the [rowid] of the inserted
  1769. ** row is returned by this routine as long as the trigger is running.
  1770. ** But once the trigger terminates, the value returned by this routine
  1771. ** reverts to the last value inserted before the trigger fired.)^
  1772. **
  1773. ** ^An [INSERT] that fails due to a constraint violation is not a
  1774. ** successful [INSERT] and does not change the value returned by this
  1775. ** routine. ^Thus INSERT OR FAIL, INSERT OR IGNORE, INSERT OR ROLLBACK,
  1776. ** and INSERT OR ABORT make no changes to the return value of this
  1777. ** routine when their insertion fails. ^(When INSERT OR REPLACE
  1778. ** encounters a constraint violation, it does not fail. The
  1779. ** INSERT continues to completion after deleting rows that caused
  1780. ** the constraint problem so INSERT OR REPLACE will always change
  1781. ** the return value of this interface.)^
  1782. **
  1783. ** ^For the purposes of this routine, an [INSERT] is considered to
  1784. ** be successful even if it is subsequently rolled back.
  1785. **
  1786. ** This function is accessible to SQL statements via the
  1787. ** [last_insert_rowid() SQL function].
  1788. **
  1789. ** If a separate thread performs a new [INSERT] on the same
  1790. ** database connection while the [sqlite3_last_insert_rowid()]
  1791. ** function is running and thus changes the last insert [rowid],
  1792. ** then the value returned by [sqlite3_last_insert_rowid()] is
  1793. ** unpredictable and might not equal either the old or the new
  1794. ** last insert [rowid].
  1795. */
  1796. SQLITE_API sqlite3_int64 sqlite3_last_insert_rowid(sqlite3*);
  1797. /*
  1798. ** CAPI3REF: Count The Number Of Rows Modified
  1799. **
  1800. ** ^This function returns the number of database rows that were changed
  1801. ** or inserted or deleted by the most recently completed SQL statement
  1802. ** on the [database connection] specified by the first parameter.
  1803. ** ^(Only changes that are directly specified by the [INSERT], [UPDATE],
  1804. ** or [DELETE] statement are counted. Auxiliary changes caused by
  1805. ** triggers or [foreign key actions] are not counted.)^ Use the
  1806. ** [sqlite3_total_changes()] function to find the total number of changes
  1807. ** including changes caused by triggers and foreign key actions.
  1808. **
  1809. ** ^Changes to a view that are simulated by an [INSTEAD OF trigger]
  1810. ** are not counted. Only real table changes are counted.
  1811. **
  1812. ** ^(A "row change" is a change to a single row of a single table
  1813. ** caused by an INSERT, DELETE, or UPDATE statement. Rows that
  1814. ** are changed as side effects of [REPLACE] constraint resolution,
  1815. ** rollback, ABORT processing, [DROP TABLE], or by any other
  1816. ** mechanisms do not count as direct row changes.)^
  1817. **
  1818. ** A "trigger context" is a scope of execution that begins and
  1819. ** ends with the script of a [CREATE TRIGGER | trigger].
  1820. ** Most SQL statements are
  1821. ** evaluated outside of any trigger. This is the "top level"
  1822. ** trigger context. If a trigger fires from the top level, a
  1823. ** new trigger context is entered for the duration of that one
  1824. ** trigger. Subtriggers create subcontexts for their duration.
  1825. **
  1826. ** ^Calling [sqlite3_exec()] or [sqlite3_step()] recursively does
  1827. ** not create a new trigger context.
  1828. **
  1829. ** ^This function returns the number of direct row changes in the
  1830. ** most recent INSERT, UPDATE, or DELETE statement within the same
  1831. ** trigger context.
  1832. **
  1833. ** ^Thus, when called from the top level, this function returns the
  1834. ** number of changes in the most recent INSERT, UPDATE, or DELETE
  1835. ** that also occurred at the top level. ^(Within the body of a trigger,
  1836. ** the sqlite3_changes() interface can be called to find the number of
  1837. ** changes in the most recently completed INSERT, UPDATE, or DELETE
  1838. ** statement within the body of the same trigger.
  1839. ** However, the number returned does not include changes
  1840. ** caused by subtriggers since those have their own context.)^
  1841. **
  1842. ** See also the [sqlite3_total_changes()] interface, the
  1843. ** [count_changes pragma], and the [changes() SQL function].
  1844. **
  1845. ** If a separate thread makes changes on the same database connection
  1846. ** while [sqlite3_changes()] is running then the value returned
  1847. ** is unpredictable and not meaningful.
  1848. */
  1849. SQLITE_API int sqlite3_changes(sqlite3*);
  1850. /*
  1851. ** CAPI3REF: Total Number Of Rows Modified
  1852. **
  1853. ** ^This function returns the number of row changes caused by [INSERT],
  1854. ** [UPDATE] or [DELETE] statements since the [database connection] was opened.
  1855. ** ^(The count returned by sqlite3_total_changes() includes all changes
  1856. ** from all [CREATE TRIGGER | trigger] contexts and changes made by
  1857. ** [foreign key actions]. However,
  1858. ** the count does not include changes used to implement [REPLACE] constraints,
  1859. ** do rollbacks or ABORT processing, or [DROP TABLE] processing. The
  1860. ** count does not include rows of views that fire an [INSTEAD OF trigger],
  1861. ** though if the INSTEAD OF trigger makes changes of its own, those changes
  1862. ** are counted.)^
  1863. ** ^The sqlite3_total_changes() function counts the changes as soon as
  1864. ** the statement that makes them is completed (when the statement handle
  1865. ** is passed to [sqlite3_reset()] or [sqlite3_finalize()]).
  1866. **
  1867. ** See also the [sqlite3_changes()] interface, the
  1868. ** [count_changes pragma], and the [total_changes() SQL function].
  1869. **
  1870. ** If a separate thread makes changes on the same database connection
  1871. ** while [sqlite3_total_changes()] is running then the value
  1872. ** returned is unpredictable and not meaningful.
  1873. */
  1874. SQLITE_API int sqlite3_total_changes(sqlite3*);
  1875. /*
  1876. ** CAPI3REF: Interrupt A Long-Running Query
  1877. **
  1878. ** ^This function causes any pending database operation to abort and
  1879. ** return at its earliest opportunity. This routine is typically
  1880. ** called in response to a user action such as pressing "Cancel"
  1881. ** or Ctrl-C where the user wants a long query operation to halt
  1882. ** immediately.
  1883. **
  1884. ** ^It is safe to call this routine from a thread different from the
  1885. ** thread that is currently running the database operation. But it
  1886. ** is not safe to call this routine with a [database connection] that
  1887. ** is closed or might close before sqlite3_interrupt() returns.
  1888. **
  1889. ** ^If an SQL operation is very nearly finished at the time when
  1890. ** sqlite3_interrupt() is called, then it might not have an opportunity
  1891. ** to be interrupted and might continue to completion.
  1892. **
  1893. ** ^An SQL operation that is interrupted will return [SQLITE_INTERRUPT].
  1894. ** ^If the interrupted SQL operation is an INSERT, UPDATE, or DELETE
  1895. ** that is inside an explicit transaction, then the entire transaction
  1896. ** will be rolled back automatically.
  1897. **
  1898. ** ^The sqlite3_interrupt(D) call is in effect until all currently running
  1899. ** SQL statements on [database connection] D complete. ^Any new SQL statements
  1900. ** that are started after the sqlite3_interrupt() call and before the
  1901. ** running statements reaches zero are interrupted as if they had been
  1902. ** running prior to the sqlite3_interrupt() call. ^New SQL statements
  1903. ** that are started after the running statement count reaches zero are
  1904. ** not effected by the sqlite3_interrupt().
  1905. ** ^A call to sqlite3_interrupt(D) that occurs when there are no running
  1906. ** SQL statements is a no-op and has no effect on SQL statements
  1907. ** that are started after the sqlite3_interrupt() call returns.
  1908. **
  1909. ** If the database connection closes while [sqlite3_interrupt()]
  1910. ** is running then bad things will likely happen.
  1911. */
  1912. SQLITE_API void sqlite3_interrupt(sqlite3*);
  1913. /*
  1914. ** CAPI3REF: Determine If An SQL Statement Is Complete
  1915. **
  1916. ** These routines are useful during command-line input to determine if the
  1917. ** currently entered text seems to form a complete SQL statement or
  1918. ** if additional input is needed before sending the text into
  1919. ** SQLite for parsing. ^These routines return 1 if the input string
  1920. ** appears to be a complete SQL statement. ^A statement is judged to be
  1921. ** complete if it ends with a semicolon token and is not a prefix of a
  1922. ** well-formed CREATE TRIGGER statement. ^Semicolons that are embedded within
  1923. ** string literals or quoted identifier names or comments are not
  1924. ** independent tokens (they are part of the token in which they are
  1925. ** embedded) and thus do not count as a statement terminator. ^Whitespace
  1926. ** and comments that follow the final semicolon are ignored.
  1927. **
  1928. ** ^These routines return 0 if the statement is incomplete. ^If a
  1929. ** memory allocation fails, then SQLITE_NOMEM is returned.
  1930. **
  1931. ** ^These routines do not parse the SQL statements thus
  1932. ** will not detect syntactically incorrect SQL.
  1933. **
  1934. ** ^(If SQLite has not been initialized using [sqlite3_initialize()] prior
  1935. ** to invoking sqlite3_complete16() then sqlite3_initialize() is invoked
  1936. ** automatically by sqlite3_complete16(). If that initialization fails,
  1937. ** then the return value from sqlite3_complete16() will be non-zero
  1938. ** regardless of whether or not the input SQL is complete.)^
  1939. **
  1940. ** The input to [sqlite3_complete()] must be a zero-terminated
  1941. ** UTF-8 string.
  1942. **
  1943. ** The input to [sqlite3_complete16()] must be a zero-terminated
  1944. ** UTF-16 string in native byte order.
  1945. */
  1946. SQLITE_API int sqlite3_complete(const char *sql);
  1947. SQLITE_API int sqlite3_complete16(const void *sql);
  1948. /*
  1949. ** CAPI3REF: Register A Callback To Handle SQLITE_BUSY Errors
  1950. **
  1951. ** ^This routine sets a callback function that might be invoked whenever
  1952. ** an attempt is made to open a database table that another thread
  1953. ** or process has locked.
  1954. **
  1955. ** ^If the busy callback is NULL, then [SQLITE_BUSY] or [SQLITE_IOERR_BLOCKED]
  1956. ** is returned immediately upon encountering the lock. ^If the busy callback
  1957. ** is not NULL, then the callback might be invoked with two arguments.
  1958. **
  1959. ** ^The first argument to the busy handler is a copy of the void* pointer which
  1960. ** is the third argument to sqlite3_busy_handler(). ^The second argument to
  1961. ** the busy handler callback is the number of times that the busy handler has
  1962. ** been invoked for this locking event. ^If the
  1963. ** busy callback returns 0, then no additional attempts are made to
  1964. ** access the database and [SQLITE_BUSY] or [SQLITE_IOERR_BLOCKED] is returned.
  1965. ** ^If the callback returns non-zero, then another attempt
  1966. ** is made to open the database for reading and the cycle repeats.
  1967. **
  1968. ** The presence of a busy handler does not guarantee that it will be invoked
  1969. ** when there is lock contention. ^If SQLite determines that invoking the busy
  1970. ** handler could result in a deadlock, it will go ahead and return [SQLITE_BUSY]
  1971. ** or [SQLITE_IOERR_BLOCKED] instead of invoking the busy handler.
  1972. ** Consider a scenario where one process is holding a read lock that
  1973. ** it is trying to promote to a reserved lock and
  1974. ** a second process is holding a reserved lock that it is trying
  1975. ** to promote to an exclusive lock. The first process cannot proceed
  1976. ** because it is blocked by the second and the second process cannot
  1977. ** proceed because it is blocked by the first. If both processes
  1978. ** invoke the busy handlers, neither will make any progress. Therefore,
  1979. ** SQLite returns [SQLITE_BUSY] for the first process, hoping that this
  1980. ** will induce the first process to release its read lock and allow
  1981. ** the second process to proceed.
  1982. **
  1983. ** ^The default busy callback is NULL.
  1984. **
  1985. ** ^The [SQLITE_BUSY] error is converted to [SQLITE_IOERR_BLOCKED]
  1986. ** when SQLite is in the middle of a large transaction where all the
  1987. ** changes will not fit into the in-memory cache. SQLite will
  1988. ** already hold a RESERVED lock on the database file, but it needs
  1989. ** to promote this lock to EXCLUSIVE so that it can spill cache
  1990. ** pages into the database file without harm to concurrent
  1991. ** readers. ^If it is unable to promote the lock, then the in-memory
  1992. ** cache will be left in an inconsistent state and so the error
  1993. ** code is promoted from the relatively benign [SQLITE_BUSY] to
  1994. ** the more severe [SQLITE_IOERR_BLOCKED]. ^This error code promotion
  1995. ** forces an automatic rollback of the changes. See the
  1996. ** <a href="/cvstrac/wiki?p=CorruptionFollowingBusyError">
  1997. ** CorruptionFollowingBusyError</a> wiki page for a discussion of why
  1998. ** this is important.
  1999. **
  2000. ** ^(There can only be a single busy handler defined for each
  2001. ** [database connection]. Setting a new busy handler clears any
  2002. ** previously set handler.)^ ^Note that calling [sqlite3_busy_timeout()]
  2003. ** will also set or clear the busy handler.
  2004. **
  2005. ** The busy callback should not take any actions which modify the
  2006. ** database connection that invoked the busy handler. Any such actions
  2007. ** result in undefined behavior.
  2008. **
  2009. ** A busy handler must not close the database connection
  2010. ** or [prepared statement] that invoked the busy handler.
  2011. */
  2012. SQLITE_API int sqlite3_busy_handler(sqlite3*, int(*)(void*,int), void*);
  2013. /*
  2014. ** CAPI3REF: Set A Busy Timeout
  2015. **
  2016. ** ^This routine sets a [sqlite3_busy_handler | busy handler] that sleeps
  2017. ** for a specified amount of time when a table is locked. ^The handler
  2018. ** will sleep multiple times until at least "ms" milliseconds of sleeping
  2019. ** have accumulated. ^After at least "ms" milliseconds of sleeping,
  2020. ** the handler returns 0 which causes [sqlite3_step()] to return
  2021. ** [SQLITE_BUSY] or [SQLITE_IOERR_BLOCKED].
  2022. **
  2023. ** ^Calling this routine with an argument less than or equal to zero
  2024. ** turns off all busy handlers.
  2025. **
  2026. ** ^(There can only be a single busy handler for a particular
  2027. ** [database connection] any any given moment. If another busy handler
  2028. ** was defined (using [sqlite3_busy_handler()]) prior to calling
  2029. ** this routine, that other busy handler is cleared.)^
  2030. */
  2031. SQLITE_API int sqlite3_busy_timeout(sqlite3*, int ms);
  2032. /*
  2033. ** CAPI3REF: Convenience Routines For Running Queries
  2034. **
  2035. ** Definition: A <b>result table</b> is memory data structure created by the
  2036. ** [sqlite3_get_table()] interface. A result table records the
  2037. ** complete query results from one or more queries.
  2038. **
  2039. ** The table conceptually has a number of rows and columns. But
  2040. ** these numbers are not part of the result table itself. These
  2041. ** numbers are obtained separately. Let N be the number of rows
  2042. ** and M be the number of columns.
  2043. **
  2044. ** A result table is an array of pointers to zero-terminated UTF-8 strings.
  2045. ** There are (N+1)*M elements in the array. The first M pointers point
  2046. ** to zero-terminated strings that contain the names of the columns.
  2047. ** The remaining entries all point to query results. NULL values result
  2048. ** in NULL pointers. All other values are in their UTF-8 zero-terminated
  2049. ** string representation as returned by [sqlite3_column_text()].
  2050. **
  2051. ** A result table might consist of one or more memory allocations.
  2052. ** It is not safe to pass a result table directly to [sqlite3_free()].
  2053. ** A result table should be deallocated using [sqlite3_free_table()].
  2054. **
  2055. ** As an example of the result table format, suppose a query result
  2056. ** is as follows:
  2057. **
  2058. ** <blockquote><pre>
  2059. ** Name | Age
  2060. ** -----------------------
  2061. ** Alice | 43
  2062. ** Bob | 28
  2063. ** Cindy | 21
  2064. ** </pre></blockquote>
  2065. **
  2066. ** There are two column (M==2) and three rows (N==3). Thus the
  2067. ** result table has 8 entries. Suppose the result table is stored
  2068. ** in an array names azResult. Then azResult holds this content:
  2069. **
  2070. ** <blockquote><pre>
  2071. ** azResult&#91;0] = "Name";
  2072. ** azResult&#91;1] = "Age";
  2073. ** azResult&#91;2] = "Alice";
  2074. ** azResult&#91;3] = "43";
  2075. ** azResult&#91;4] = "Bob";
  2076. ** azResult&#91;5] = "28";
  2077. ** azResult&#91;6] = "Cindy";
  2078. ** azResult&#91;7] = "21";
  2079. ** </pre></blockquote>
  2080. **
  2081. ** ^The sqlite3_get_table() function evaluates one or more
  2082. ** semicolon-separated SQL statements in the zero-terminated UTF-8
  2083. ** string of its 2nd parameter and returns a result table to the
  2084. ** pointer given in its 3rd parameter.
  2085. **
  2086. ** After the application has finished with the result from sqlite3_get_table(),
  2087. ** it should pass the result table pointer to sqlite3_free_table() in order to
  2088. ** release the memory that was malloced. Because of the way the
  2089. ** [sqlite3_malloc()] happens within sqlite3_get_table(), the calling
  2090. ** function must not try to call [sqlite3_free()] directly. Only
  2091. ** [sqlite3_free_table()] is able to release the memory properly and safely.
  2092. **
  2093. ** ^(The sqlite3_get_table() interface is implemented as a wrapper around
  2094. ** [sqlite3_exec()]. The sqlite3_get_table() routine does not have access
  2095. ** to any internal data structures of SQLite. It uses only the public
  2096. ** interface defined here. As a consequence, errors that occur in the
  2097. ** wrapper layer outside of the internal [sqlite3_exec()] call are not
  2098. ** reflected in subsequent calls to [sqlite3_errcode()] or
  2099. ** [sqlite3_errmsg()].)^
  2100. */
  2101. SQLITE_API int sqlite3_get_table(
  2102. sqlite3 *db, /* An open database */
  2103. const char *zSql, /* SQL to be evaluated */
  2104. char ***pazResult, /* Results of the query */
  2105. int *pnRow, /* Number of result rows written here */
  2106. int *pnColumn, /* Number of result columns written here */
  2107. char **pzErrmsg /* Error msg written here */
  2108. );
  2109. SQLITE_API void sqlite3_free_table(char **result);
  2110. /*
  2111. ** CAPI3REF: Formatted String Printing Functions
  2112. **
  2113. ** These routines are work-alikes of the "printf()" family of functions
  2114. ** from the standard C library.
  2115. **
  2116. ** ^The sqlite3_mprintf() and sqlite3_vmprintf() routines write their
  2117. ** results into memory obtained from [sqlite3_malloc()].
  2118. ** The strings returned by these two routines should be
  2119. ** released by [sqlite3_free()]. ^Both routines return a
  2120. ** NULL pointer if [sqlite3_malloc()] is unable to allocate enough
  2121. ** memory to hold the resulting string.
  2122. **
  2123. ** ^(In sqlite3_snprintf() routine is similar to "snprintf()" from
  2124. ** the standard C library. The result is written into the
  2125. ** buffer supplied as the second parameter whose size is given by
  2126. ** the first parameter. Note that the order of the
  2127. ** first two parameters is reversed from snprintf().)^ This is an
  2128. ** historical accident that cannot be fixed without breaking
  2129. ** backwards compatibility. ^(Note also that sqlite3_snprintf()
  2130. ** returns a pointer to its buffer instead of the number of
  2131. ** characters actually written into the buffer.)^ We admit that
  2132. ** the number of characters written would be a more useful return
  2133. ** value but we cannot change the implementation of sqlite3_snprintf()
  2134. ** now without breaking compatibility.
  2135. **
  2136. ** ^As long as the buffer size is greater than zero, sqlite3_snprintf()
  2137. ** guarantees that the buffer is always zero-terminated. ^The first
  2138. ** parameter "n" is the total size of the buffer, including space for
  2139. ** the zero terminator. So the longest string that can be completely
  2140. ** written will be n-1 characters.
  2141. **
  2142. ** These routines all implement some additional formatting
  2143. ** options that are useful for constructing SQL statements.
  2144. ** All of the usual printf() formatting options apply. In addition, there
  2145. ** is are "%q", "%Q", and "%z" options.
  2146. **
  2147. ** ^(The %q option works like %s in that it substitutes a null-terminated
  2148. ** string from the argument list. But %q also doubles every '\'' character.
  2149. ** %q is designed for use inside a string literal.)^ By doubling each '\''
  2150. ** character it escapes that character and allows it to be inserted into
  2151. ** the string.
  2152. **
  2153. ** For example, assume the string variable zText contains text as follows:
  2154. **
  2155. ** <blockquote><pre>
  2156. ** char *zText = "It's a happy day!";
  2157. ** </pre></blockquote>
  2158. **
  2159. ** One can use this text in an SQL statement as follows:
  2160. **
  2161. ** <blockquote><pre>
  2162. ** char *zSQL = sqlite3_mprintf("INSERT INTO table VALUES('%q')", zText);
  2163. ** sqlite3_exec(db, zSQL, 0, 0, 0);
  2164. ** sqlite3_free(zSQL);
  2165. ** </pre></blockquote>
  2166. **
  2167. ** Because the %q format string is used, the '\'' character in zText
  2168. ** is escaped and the SQL generated is as follows:
  2169. **
  2170. ** <blockquote><pre>
  2171. ** INSERT INTO table1 VALUES('It''s a happy day!')
  2172. ** </pre></blockquote>
  2173. **
  2174. ** This is correct. Had we used %s instead of %q, the generated SQL
  2175. ** would have looked like this:
  2176. **
  2177. ** <blockquote><pre>
  2178. ** INSERT INTO table1 VALUES('It's a happy day!');
  2179. ** </pre></blockquote>
  2180. **
  2181. ** This second example is an SQL syntax error. As a general rule you should
  2182. ** always use %q instead of %s when inserting text into a string literal.
  2183. **
  2184. ** ^(The %Q option works like %q except it also adds single quotes around
  2185. ** the outside of the total string. Additionally, if the parameter in the
  2186. ** argument list is a NULL pointer, %Q substitutes the text "NULL" (without
  2187. ** single quotes).)^ So, for example, one could say:
  2188. **
  2189. ** <blockquote><pre>
  2190. ** char *zSQL = sqlite3_mprintf("INSERT INTO table VALUES(%Q)", zText);
  2191. ** sqlite3_exec(db, zSQL, 0, 0, 0);
  2192. ** sqlite3_free(zSQL);
  2193. ** </pre></blockquote>
  2194. **
  2195. ** The code above will render a correct SQL statement in the zSQL
  2196. ** variable even if the zText variable is a NULL pointer.
  2197. **
  2198. ** ^(The "%z" formatting option works like "%s" but with the
  2199. ** addition that after the string has been read and copied into
  2200. ** the result, [sqlite3_free()] is called on the input string.)^
  2201. */
  2202. SQLITE_API char *sqlite3_mprintf(const char*,...);
  2203. SQLITE_API char *sqlite3_vmprintf(const char*, va_list);
  2204. SQLITE_API char *sqlite3_snprintf(int,char*,const char*, ...);
  2205. /*
  2206. ** CAPI3REF: Memory Allocation Subsystem
  2207. **
  2208. ** The SQLite core uses these three routines for all of its own
  2209. ** internal memory allocation needs. "Core" in the previous sentence
  2210. ** does not include operating-system specific VFS implementation. The
  2211. ** Windows VFS uses native malloc() and free() for some operations.
  2212. **
  2213. ** ^The sqlite3_malloc() routine returns a pointer to a block
  2214. ** of memory at least N bytes in length, where N is the parameter.
  2215. ** ^If sqlite3_malloc() is unable to obtain sufficient free
  2216. ** memory, it returns a NULL pointer. ^If the parameter N to
  2217. ** sqlite3_malloc() is zero or negative then sqlite3_malloc() returns
  2218. ** a NULL pointer.
  2219. **
  2220. ** ^Calling sqlite3_free() with a pointer previously returned
  2221. ** by sqlite3_malloc() or sqlite3_realloc() releases that memory so
  2222. ** that it might be reused. ^The sqlite3_free() routine is
  2223. ** a no-op if is called with a NULL pointer. Passing a NULL pointer
  2224. ** to sqlite3_free() is harmless. After being freed, memory
  2225. ** should neither be read nor written. Even reading previously freed
  2226. ** memory might result in a segmentation fault or other severe error.
  2227. ** Memory corruption, a segmentation fault, or other severe error
  2228. ** might result if sqlite3_free() is called with a non-NULL pointer that
  2229. ** was not obtained from sqlite3_malloc() or sqlite3_realloc().
  2230. **
  2231. ** ^(The sqlite3_realloc() interface attempts to resize a
  2232. ** prior memory allocation to be at least N bytes, where N is the
  2233. ** second parameter. The memory allocation to be resized is the first
  2234. ** parameter.)^ ^ If the first parameter to sqlite3_realloc()
  2235. ** is a NULL pointer then its behavior is identical to calling
  2236. ** sqlite3_malloc(N) where N is the second parameter to sqlite3_realloc().
  2237. ** ^If the second parameter to sqlite3_realloc() is zero or
  2238. ** negative then the behavior is exactly the same as calling
  2239. ** sqlite3_free(P) where P is the first parameter to sqlite3_realloc().
  2240. ** ^sqlite3_realloc() returns a pointer to a memory allocation
  2241. ** of at least N bytes in size or NULL if sufficient memory is unavailable.
  2242. ** ^If M is the size of the prior allocation, then min(N,M) bytes
  2243. ** of the prior allocation are copied into the beginning of buffer returned
  2244. ** by sqlite3_realloc() and the prior allocation is freed.
  2245. ** ^If sqlite3_realloc() returns NULL, then the prior allocation
  2246. ** is not freed.
  2247. **
  2248. ** ^The memory returned by sqlite3_malloc() and sqlite3_realloc()
  2249. ** is always aligned to at least an 8 byte boundary.
  2250. **
  2251. ** In SQLite version 3.5.0 and 3.5.1, it was possible to define
  2252. ** the SQLITE_OMIT_MEMORY_ALLOCATION which would cause the built-in
  2253. ** implementation of these routines to be omitted. That capability
  2254. ** is no longer provided. Only built-in memory allocators can be used.
  2255. **
  2256. ** The Windows OS interface layer calls
  2257. ** the system malloc() and free() directly when converting
  2258. ** filenames between the UTF-8 encoding used by SQLite
  2259. ** and whatever filename encoding is used by the particular Windows
  2260. ** installation. Memory allocation errors are detected, but
  2261. ** they are reported back as [SQLITE_CANTOPEN] or
  2262. ** [SQLITE_IOERR] rather than [SQLITE_NOMEM].
  2263. **
  2264. ** The pointer arguments to [sqlite3_free()] and [sqlite3_realloc()]
  2265. ** must be either NULL or else pointers obtained from a prior
  2266. ** invocation of [sqlite3_malloc()] or [sqlite3_realloc()] that have
  2267. ** not yet been released.
  2268. **
  2269. ** The application must not read or write any part of
  2270. ** a block of memory after it has been released using
  2271. ** [sqlite3_free()] or [sqlite3_realloc()].
  2272. */
  2273. SQLITE_API void *sqlite3_malloc(int);
  2274. SQLITE_API void *sqlite3_realloc(void*, int);
  2275. SQLITE_API void sqlite3_free(void*);
  2276. /*
  2277. ** CAPI3REF: Memory Allocator Statistics
  2278. **
  2279. ** SQLite provides these two interfaces for reporting on the status
  2280. ** of the [sqlite3_malloc()], [sqlite3_free()], and [sqlite3_realloc()]
  2281. ** routines, which form the built-in memory allocation subsystem.
  2282. **
  2283. ** ^The [sqlite3_memory_used()] routine returns the number of bytes
  2284. ** of memory currently outstanding (malloced but not freed).
  2285. ** ^The [sqlite3_memory_highwater()] routine returns the maximum
  2286. ** value of [sqlite3_memory_used()] since the high-water mark
  2287. ** was last reset. ^The values returned by [sqlite3_memory_used()] and
  2288. ** [sqlite3_memory_highwater()] include any overhead
  2289. ** added by SQLite in its implementation of [sqlite3_malloc()],
  2290. ** but not overhead added by the any underlying system library
  2291. ** routines that [sqlite3_malloc()] may call.
  2292. **
  2293. ** ^The memory high-water mark is reset to the current value of
  2294. ** [sqlite3_memory_used()] if and only if the parameter to
  2295. ** [sqlite3_memory_highwater()] is true. ^The value returned
  2296. ** by [sqlite3_memory_highwater(1)] is the high-water mark
  2297. ** prior to the reset.
  2298. */
  2299. SQLITE_API sqlite3_int64 sqlite3_memory_used(void);
  2300. SQLITE_API sqlite3_int64 sqlite3_memory_highwater(int resetFlag);
  2301. /*
  2302. ** CAPI3REF: Pseudo-Random Number Generator
  2303. **
  2304. ** SQLite contains a high-quality pseudo-random number generator (PRNG) used to
  2305. ** select random [ROWID | ROWIDs] when inserting new records into a table that
  2306. ** already uses the largest possible [ROWID]. The PRNG is also used for
  2307. ** the build-in random() and randomblob() SQL functions. This interface allows
  2308. ** applications to access the same PRNG for other purposes.
  2309. **
  2310. ** ^A call to this routine stores N bytes of randomness into buffer P.
  2311. **
  2312. ** ^The first time this routine is invoked (either internally or by
  2313. ** the application) the PRNG is seeded using randomness obtained
  2314. ** from the xRandomness method of the default [sqlite3_vfs] object.
  2315. ** ^On all subsequent invocations, the pseudo-randomness is generated
  2316. ** internally and without recourse to the [sqlite3_vfs] xRandomness
  2317. ** method.
  2318. */
  2319. SQLITE_API void sqlite3_randomness(int N, void *P);
  2320. /*
  2321. ** CAPI3REF: Compile-Time Authorization Callbacks
  2322. **
  2323. ** ^This routine registers a authorizer callback with a particular
  2324. ** [database connection], supplied in the first argument.
  2325. ** ^The authorizer callback is invoked as SQL statements are being compiled
  2326. ** by [sqlite3_prepare()] or its variants [sqlite3_prepare_v2()],
  2327. ** [sqlite3_prepare16()] and [sqlite3_prepare16_v2()]. ^At various
  2328. ** points during the compilation process, as logic is being created
  2329. ** to perform various actions, the authorizer callback is invoked to
  2330. ** see if those actions are allowed. ^The authorizer callback should
  2331. ** return [SQLITE_OK] to allow the action, [SQLITE_IGNORE] to disallow the
  2332. ** specific action but allow the SQL statement to continue to be
  2333. ** compiled, or [SQLITE_DENY] to cause the entire SQL statement to be
  2334. ** rejected with an error. ^If the authorizer callback returns
  2335. ** any value other than [SQLITE_IGNORE], [SQLITE_OK], or [SQLITE_DENY]
  2336. ** then the [sqlite3_prepare_v2()] or equivalent call that triggered
  2337. ** the authorizer will fail with an error message.
  2338. **
  2339. ** When the callback returns [SQLITE_OK], that means the operation
  2340. ** requested is ok. ^When the callback returns [SQLITE_DENY], the
  2341. ** [sqlite3_prepare_v2()] or equivalent call that triggered the
  2342. ** authorizer will fail with an error message explaining that
  2343. ** access is denied.
  2344. **
  2345. ** ^The first parameter to the authorizer callback is a copy of the third
  2346. ** parameter to the sqlite3_set_authorizer() interface. ^The second parameter
  2347. ** to the callback is an integer [SQLITE_COPY | action code] that specifies
  2348. ** the particular action to be authorized. ^The third through sixth parameters
  2349. ** to the callback are zero-terminated strings that contain additional
  2350. ** details about the action to be authorized.
  2351. **
  2352. ** ^If the action code is [SQLITE_READ]
  2353. ** and the callback returns [SQLITE_IGNORE] then the
  2354. ** [prepared statement] statement is constructed to substitute
  2355. ** a NULL value in place of the table column that would have
  2356. ** been read if [SQLITE_OK] had been returned. The [SQLITE_IGNORE]
  2357. ** return can be used to deny an untrusted user access to individual
  2358. ** columns of a table.
  2359. ** ^If the action code is [SQLITE_DELETE] and the callback returns
  2360. ** [SQLITE_IGNORE] then the [DELETE] operation proceeds but the
  2361. ** [truncate optimization] is disabled and all rows are deleted individually.
  2362. **
  2363. ** An authorizer is used when [sqlite3_prepare | preparing]
  2364. ** SQL statements from an untrusted source, to ensure that the SQL statements
  2365. ** do not try to access data they are not allowed to see, or that they do not
  2366. ** try to execute malicious statements that damage the database. For
  2367. ** example, an application may allow a user to enter arbitrary
  2368. ** SQL queries for evaluation by a database. But the application does
  2369. ** not want the user to be able to make arbitrary changes to the
  2370. ** database. An authorizer could then be put in place while the
  2371. ** user-entered SQL is being [sqlite3_prepare | prepared] that
  2372. ** disallows everything except [SELECT] statements.
  2373. **
  2374. ** Applications that need to process SQL from untrusted sources
  2375. ** might also consider lowering resource limits using [sqlite3_limit()]
  2376. ** and limiting database size using the [max_page_count] [PRAGMA]
  2377. ** in addition to using an authorizer.
  2378. **
  2379. ** ^(Only a single authorizer can be in place on a database connection
  2380. ** at a time. Each call to sqlite3_set_authorizer overrides the
  2381. ** previous call.)^ ^Disable the authorizer by installing a NULL callback.
  2382. ** The authorizer is disabled by default.
  2383. **
  2384. ** The authorizer callback must not do anything that will modify
  2385. ** the database connection that invoked the authorizer callback.
  2386. ** Note that [sqlite3_prepare_v2()] and [sqlite3_step()] both modify their
  2387. ** database connections for the meaning of "modify" in this paragraph.
  2388. **
  2389. ** ^When [sqlite3_prepare_v2()] is used to prepare a statement, the
  2390. ** statement might be re-prepared during [sqlite3_step()] due to a
  2391. ** schema change. Hence, the application should ensure that the
  2392. ** correct authorizer callback remains in place during the [sqlite3_step()].
  2393. **
  2394. ** ^Note that the authorizer callback is invoked only during
  2395. ** [sqlite3_prepare()] or its variants. Authorization is not
  2396. ** performed during statement evaluation in [sqlite3_step()], unless
  2397. ** as stated in the previous paragraph, sqlite3_step() invokes
  2398. ** sqlite3_prepare_v2() to reprepare a statement after a schema change.
  2399. */
  2400. SQLITE_API int sqlite3_set_authorizer(
  2401. sqlite3*,
  2402. int (*xAuth)(void*,int,const char*,const char*,const char*,const char*),
  2403. void *pUserData
  2404. );
  2405. /*
  2406. ** CAPI3REF: Authorizer Return Codes
  2407. **
  2408. ** The [sqlite3_set_authorizer | authorizer callback function] must
  2409. ** return either [SQLITE_OK] or one of these two constants in order
  2410. ** to signal SQLite whether or not the action is permitted. See the
  2411. ** [sqlite3_set_authorizer | authorizer documentation] for additional
  2412. ** information.
  2413. */
  2414. #define SQLITE_DENY 1 /* Abort the SQL statement with an error */
  2415. #define SQLITE_IGNORE 2 /* Don't allow access, but don't generate an error */
  2416. /*
  2417. ** CAPI3REF: Authorizer Action Codes
  2418. **
  2419. ** The [sqlite3_set_authorizer()] interface registers a callback function
  2420. ** that is invoked to authorize certain SQL statement actions. The
  2421. ** second parameter to the callback is an integer code that specifies
  2422. ** what action is being authorized. These are the integer action codes that
  2423. ** the authorizer callback may be passed.
  2424. **
  2425. ** These action code values signify what kind of operation is to be
  2426. ** authorized. The 3rd and 4th parameters to the authorization
  2427. ** callback function will be parameters or NULL depending on which of these
  2428. ** codes is used as the second parameter. ^(The 5th parameter to the
  2429. ** authorizer callback is the name of the database ("main", "temp",
  2430. ** etc.) if applicable.)^ ^The 6th parameter to the authorizer callback
  2431. ** is the name of the inner-most trigger or view that is responsible for
  2432. ** the access attempt or NULL if this access attempt is directly from
  2433. ** top-level SQL code.
  2434. */
  2435. /******************************************* 3rd ************ 4th ***********/
  2436. #define SQLITE_CREATE_INDEX 1 /* Index Name Table Name */
  2437. #define SQLITE_CREATE_TABLE 2 /* Table Name NULL */
  2438. #define SQLITE_CREATE_TEMP_INDEX 3 /* Index Name Table Name */
  2439. #define SQLITE_CREATE_TEMP_TABLE 4 /* Table Name NULL */
  2440. #define SQLITE_CREATE_TEMP_TRIGGER 5 /* Trigger Name Table Name */
  2441. #define SQLITE_CREATE_TEMP_VIEW 6 /* View Name NULL */
  2442. #define SQLITE_CREATE_TRIGGER 7 /* Trigger Name Table Name */
  2443. #define SQLITE_CREATE_VIEW 8 /* View Name NULL */
  2444. #define SQLITE_DELETE 9 /* Table Name NULL */
  2445. #define SQLITE_DROP_INDEX 10 /* Index Name Table Name */
  2446. #define SQLITE_DROP_TABLE 11 /* Table Name NULL */
  2447. #define SQLITE_DROP_TEMP_INDEX 12 /* Index Name Table Name */
  2448. #define SQLITE_DROP_TEMP_TABLE 13 /* Table Name NULL */
  2449. #define SQLITE_DROP_TEMP_TRIGGER 14 /* Trigger Name Table Name */
  2450. #define SQLITE_DROP_TEMP_VIEW 15 /* View Name NULL */
  2451. #define SQLITE_DROP_TRIGGER 16 /* Trigger Name Table Name */
  2452. #define SQLITE_DROP_VIEW 17 /* View Name NULL */
  2453. #define SQLITE_INSERT 18 /* Table Name NULL */
  2454. #define SQLITE_PRAGMA 19 /* Pragma Name 1st arg or NULL */
  2455. #define SQLITE_READ 20 /* Table Name Column Name */
  2456. #define SQLITE_SELECT 21 /* NULL NULL */
  2457. #define SQLITE_TRANSACTION 22 /* Operation NULL */
  2458. #define SQLITE_UPDATE 23 /* Table Name Column Name */
  2459. #define SQLITE_ATTACH 24 /* Filename NULL */
  2460. #define SQLITE_DETACH 25 /* Database Name NULL */
  2461. #define SQLITE_ALTER_TABLE 26 /* Database Name Table Name */
  2462. #define SQLITE_REINDEX 27 /* Index Name NULL */
  2463. #define SQLITE_ANALYZE 28 /* Table Name NULL */
  2464. #define SQLITE_CREATE_VTABLE 29 /* Table Name Module Name */
  2465. #define SQLITE_DROP_VTABLE 30 /* Table Name Module Name */
  2466. #define SQLITE_FUNCTION 31 /* NULL Function Name */
  2467. #define SQLITE_SAVEPOINT 32 /* Operation Savepoint Name */
  2468. #define SQLITE_COPY 0 /* No longer used */
  2469. /*
  2470. ** CAPI3REF: Tracing And Profiling Functions
  2471. ** EXPERIMENTAL
  2472. **
  2473. ** These routines register callback functions that can be used for
  2474. ** tracing and profiling the execution of SQL statements.
  2475. **
  2476. ** ^The callback function registered by sqlite3_trace() is invoked at
  2477. ** various times when an SQL statement is being run by [sqlite3_step()].
  2478. ** ^The sqlite3_trace() callback is invoked with a UTF-8 rendering of the
  2479. ** SQL statement text as the statement first begins executing.
  2480. ** ^(Additional sqlite3_trace() callbacks might occur
  2481. ** as each triggered subprogram is entered. The callbacks for triggers
  2482. ** contain a UTF-8 SQL comment that identifies the trigger.)^
  2483. **
  2484. ** ^The callback function registered by sqlite3_profile() is invoked
  2485. ** as each SQL statement finishes. ^The profile callback contains
  2486. ** the original statement text and an estimate of wall-clock time
  2487. ** of how long that statement took to run.
  2488. */
  2489. SQLITE_API SQLITE_EXPERIMENTAL void *sqlite3_trace(sqlite3*, void(*xTrace)(void*,const char*), void*);
  2490. SQLITE_API SQLITE_EXPERIMENTAL void *sqlite3_profile(sqlite3*,
  2491. void(*xProfile)(void*,const char*,sqlite3_uint64), void*);
  2492. /*
  2493. ** CAPI3REF: Query Progress Callbacks
  2494. **
  2495. ** ^This routine configures a callback function - the
  2496. ** progress callback - that is invoked periodically during long
  2497. ** running calls to [sqlite3_exec()], [sqlite3_step()] and
  2498. ** [sqlite3_get_table()]. An example use for this
  2499. ** interface is to keep a GUI updated during a large query.
  2500. **
  2501. ** ^If the progress callback returns non-zero, the operation is
  2502. ** interrupted. This feature can be used to implement a
  2503. ** "Cancel" button on a GUI progress dialog box.
  2504. **
  2505. ** The progress handler must not do anything that will modify
  2506. ** the database connection that invoked the progress handler.
  2507. ** Note that [sqlite3_prepare_v2()] and [sqlite3_step()] both modify their
  2508. ** database connections for the meaning of "modify" in this paragraph.
  2509. **
  2510. */
  2511. SQLITE_API void sqlite3_progress_handler(sqlite3*, int, int(*)(void*), void*);
  2512. /*
  2513. ** CAPI3REF: Opening A New Database Connection
  2514. **
  2515. ** ^These routines open an SQLite database file whose name is given by the
  2516. ** filename argument. ^The filename argument is interpreted as UTF-8 for
  2517. ** sqlite3_open() and sqlite3_open_v2() and as UTF-16 in the native byte
  2518. ** order for sqlite3_open16(). ^(A [database connection] handle is usually
  2519. ** returned in *ppDb, even if an error occurs. The only exception is that
  2520. ** if SQLite is unable to allocate memory to hold the [sqlite3] object,
  2521. ** a NULL will be written into *ppDb instead of a pointer to the [sqlite3]
  2522. ** object.)^ ^(If the database is opened (and/or created) successfully, then
  2523. ** [SQLITE_OK] is returned. Otherwise an [error code] is returned.)^ ^The
  2524. ** [sqlite3_errmsg()] or [sqlite3_errmsg16()] routines can be used to obtain
  2525. ** an English language description of the error following a failure of any
  2526. ** of the sqlite3_open() routines.
  2527. **
  2528. ** ^The default encoding for the database will be UTF-8 if
  2529. ** sqlite3_open() or sqlite3_open_v2() is called and
  2530. ** UTF-16 in the native byte order if sqlite3_open16() is used.
  2531. **
  2532. ** Whether or not an error occurs when it is opened, resources
  2533. ** associated with the [database connection] handle should be released by
  2534. ** passing it to [sqlite3_close()] when it is no longer required.
  2535. **
  2536. ** The sqlite3_open_v2() interface works like sqlite3_open()
  2537. ** except that it accepts two additional parameters for additional control
  2538. ** over the new database connection. ^(The flags parameter to
  2539. ** sqlite3_open_v2() can take one of
  2540. ** the following three values, optionally combined with the
  2541. ** [SQLITE_OPEN_NOMUTEX], [SQLITE_OPEN_FULLMUTEX], [SQLITE_OPEN_SHAREDCACHE],
  2542. ** and/or [SQLITE_OPEN_PRIVATECACHE] flags:)^
  2543. **
  2544. ** <dl>
  2545. ** ^(<dt>[SQLITE_OPEN_READONLY]</dt>
  2546. ** <dd>The database is opened in read-only mode. If the database does not
  2547. ** already exist, an error is returned.</dd>)^
  2548. **
  2549. ** ^(<dt>[SQLITE_OPEN_READWRITE]</dt>
  2550. ** <dd>The database is opened for reading and writing if possible, or reading
  2551. ** only if the file is write protected by the operating system. In either
  2552. ** case the database must already exist, otherwise an error is returned.</dd>)^
  2553. **
  2554. ** ^(<dt>[SQLITE_OPEN_READWRITE] | [SQLITE_OPEN_CREATE]</dt>
  2555. ** <dd>The database is opened for reading and writing, and is creates it if
  2556. ** it does not already exist. This is the behavior that is always used for
  2557. ** sqlite3_open() and sqlite3_open16().</dd>)^
  2558. ** </dl>
  2559. **
  2560. ** If the 3rd parameter to sqlite3_open_v2() is not one of the
  2561. ** combinations shown above or one of the combinations shown above combined
  2562. ** with the [SQLITE_OPEN_NOMUTEX], [SQLITE_OPEN_FULLMUTEX],
  2563. ** [SQLITE_OPEN_SHAREDCACHE] and/or [SQLITE_OPEN_SHAREDCACHE] flags,
  2564. ** then the behavior is undefined.
  2565. **
  2566. ** ^If the [SQLITE_OPEN_NOMUTEX] flag is set, then the database connection
  2567. ** opens in the multi-thread [threading mode] as long as the single-thread
  2568. ** mode has not been set at compile-time or start-time. ^If the
  2569. ** [SQLITE_OPEN_FULLMUTEX] flag is set then the database connection opens
  2570. ** in the serialized [threading mode] unless single-thread was
  2571. ** previously selected at compile-time or start-time.
  2572. ** ^The [SQLITE_OPEN_SHAREDCACHE] flag causes the database connection to be
  2573. ** eligible to use [shared cache mode], regardless of whether or not shared
  2574. ** cache is enabled using [sqlite3_enable_shared_cache()]. ^The
  2575. ** [SQLITE_OPEN_PRIVATECACHE] flag causes the database connection to not
  2576. ** participate in [shared cache mode] even if it is enabled.
  2577. **
  2578. ** ^If the filename is ":memory:", then a private, temporary in-memory database
  2579. ** is created for the connection. ^This in-memory database will vanish when
  2580. ** the database connection is closed. Future versions of SQLite might
  2581. ** make use of additional special filenames that begin with the ":" character.
  2582. ** It is recommended that when a database filename actually does begin with
  2583. ** a ":" character you should prefix the filename with a pathname such as
  2584. ** "./" to avoid ambiguity.
  2585. **
  2586. ** ^If the filename is an empty string, then a private, temporary
  2587. ** on-disk database will be created. ^This private database will be
  2588. ** automatically deleted as soon as the database connection is closed.
  2589. **
  2590. ** ^The fourth parameter to sqlite3_open_v2() is the name of the
  2591. ** [sqlite3_vfs] object that defines the operating system interface that
  2592. ** the new database connection should use. ^If the fourth parameter is
  2593. ** a NULL pointer then the default [sqlite3_vfs] object is used.
  2594. **
  2595. ** <b>Note to Windows users:</b> The encoding used for the filename argument
  2596. ** of sqlite3_open() and sqlite3_open_v2() must be UTF-8, not whatever
  2597. ** codepage is currently defined. Filenames containing international
  2598. ** characters must be converted to UTF-8 prior to passing them into
  2599. ** sqlite3_open() or sqlite3_open_v2().
  2600. */
  2601. SQLITE_API int sqlite3_open(
  2602. const char *filename, /* Database filename (UTF-8) */
  2603. sqlite3 **ppDb /* OUT: SQLite db handle */
  2604. );
  2605. SQLITE_API int sqlite3_open16(
  2606. const void *filename, /* Database filename (UTF-16) */
  2607. sqlite3 **ppDb /* OUT: SQLite db handle */
  2608. );
  2609. SQLITE_API int sqlite3_open_v2(
  2610. const char *filename, /* Database filename (UTF-8) */
  2611. sqlite3 **ppDb, /* OUT: SQLite db handle */
  2612. int flags, /* Flags */
  2613. const char *zVfs /* Name of VFS module to use */
  2614. );
  2615. /*
  2616. ** CAPI3REF: Error Codes And Messages
  2617. **
  2618. ** ^The sqlite3_errcode() interface returns the numeric [result code] or
  2619. ** [extended result code] for the most recent failed sqlite3_* API call
  2620. ** associated with a [database connection]. If a prior API call failed
  2621. ** but the most recent API call succeeded, the return value from
  2622. ** sqlite3_errcode() is undefined. ^The sqlite3_extended_errcode()
  2623. ** interface is the same except that it always returns the
  2624. ** [extended result code] even when extended result codes are
  2625. ** disabled.
  2626. **
  2627. ** ^The sqlite3_errmsg() and sqlite3_errmsg16() return English-language
  2628. ** text that describes the error, as either UTF-8 or UTF-16 respectively.
  2629. ** ^(Memory to hold the error message string is managed internally.
  2630. ** The application does not need to worry about freeing the result.
  2631. ** However, the error string might be overwritten or deallocated by
  2632. ** subsequent calls to other SQLite interface functions.)^
  2633. **
  2634. ** When the serialized [threading mode] is in use, it might be the
  2635. ** case that a second error occurs on a separate thread in between
  2636. ** the time of the first error and the call to these interfaces.
  2637. ** When that happens, the second error will be reported since these
  2638. ** interfaces always report the most recent result. To avoid
  2639. ** this, each thread can obtain exclusive use of the [database connection] D
  2640. ** by invoking [sqlite3_mutex_enter]([sqlite3_db_mutex](D)) before beginning
  2641. ** to use D and invoking [sqlite3_mutex_leave]([sqlite3_db_mutex](D)) after
  2642. ** all calls to the interfaces listed here are completed.
  2643. **
  2644. ** If an interface fails with SQLITE_MISUSE, that means the interface
  2645. ** was invoked incorrectly by the application. In that case, the
  2646. ** error code and message may or may not be set.
  2647. */
  2648. SQLITE_API int sqlite3_errcode(sqlite3 *db);
  2649. SQLITE_API int sqlite3_extended_errcode(sqlite3 *db);
  2650. SQLITE_API const char *sqlite3_errmsg(sqlite3*);
  2651. SQLITE_API const void *sqlite3_errmsg16(sqlite3*);
  2652. /*
  2653. ** CAPI3REF: SQL Statement Object
  2654. ** KEYWORDS: {prepared statement} {prepared statements}
  2655. **
  2656. ** An instance of this object represents a single SQL statement.
  2657. ** This object is variously known as a "prepared statement" or a
  2658. ** "compiled SQL statement" or simply as a "statement".
  2659. **
  2660. ** The life of a statement object goes something like this:
  2661. **
  2662. ** <ol>
  2663. ** <li> Create the object using [sqlite3_prepare_v2()] or a related
  2664. ** function.
  2665. ** <li> Bind values to [host parameters] using the sqlite3_bind_*()
  2666. ** interfaces.
  2667. ** <li> Run the SQL by calling [sqlite3_step()] one or more times.
  2668. ** <li> Reset the statement using [sqlite3_reset()] then go back
  2669. ** to step 2. Do this zero or more times.
  2670. ** <li> Destroy the object using [sqlite3_finalize()].
  2671. ** </ol>
  2672. **
  2673. ** Refer to documentation on individual methods above for additional
  2674. ** information.
  2675. */
  2676. typedef struct sqlite3_stmt sqlite3_stmt;
  2677. /*
  2678. ** CAPI3REF: Run-time Limits
  2679. **
  2680. ** ^(This interface allows the size of various constructs to be limited
  2681. ** on a connection by connection basis. The first parameter is the
  2682. ** [database connection] whose limit is to be set or queried. The
  2683. ** second parameter is one of the [limit categories] that define a
  2684. ** class of constructs to be size limited. The third parameter is the
  2685. ** new limit for that construct. The function returns the old limit.)^
  2686. **
  2687. ** ^If the new limit is a negative number, the limit is unchanged.
  2688. ** ^(For the limit category of SQLITE_LIMIT_XYZ there is a
  2689. ** [limits | hard upper bound]
  2690. ** set by a compile-time C preprocessor macro named
  2691. ** [limits | SQLITE_MAX_XYZ].
  2692. ** (The "_LIMIT_" in the name is changed to "_MAX_".))^
  2693. ** ^Attempts to increase a limit above its hard upper bound are
  2694. ** silently truncated to the hard upper bound.
  2695. **
  2696. ** Run-time limits are intended for use in applications that manage
  2697. ** both their own internal database and also databases that are controlled
  2698. ** by untrusted external sources. An example application might be a
  2699. ** web browser that has its own databases for storing history and
  2700. ** separate databases controlled by JavaScript applications downloaded
  2701. ** off the Internet. The internal databases can be given the
  2702. ** large, default limits. Databases managed by external sources can
  2703. ** be given much smaller limits designed to prevent a denial of service
  2704. ** attack. Developers might also want to use the [sqlite3_set_authorizer()]
  2705. ** interface to further control untrusted SQL. The size of the database
  2706. ** created by an untrusted script can be contained using the
  2707. ** [max_page_count] [PRAGMA].
  2708. **
  2709. ** New run-time limit categories may be added in future releases.
  2710. */
  2711. SQLITE_API int sqlite3_limit(sqlite3*, int id, int newVal);
  2712. /*
  2713. ** CAPI3REF: Run-Time Limit Categories
  2714. ** KEYWORDS: {limit category} {*limit categories}
  2715. **
  2716. ** These constants define various performance limits
  2717. ** that can be lowered at run-time using [sqlite3_limit()].
  2718. ** The synopsis of the meanings of the various limits is shown below.
  2719. ** Additional information is available at [limits | Limits in SQLite].
  2720. **
  2721. ** <dl>
  2722. ** ^(<dt>SQLITE_LIMIT_LENGTH</dt>
  2723. ** <dd>The maximum size of any string or BLOB or table row.<dd>)^
  2724. **
  2725. ** ^(<dt>SQLITE_LIMIT_SQL_LENGTH</dt>
  2726. ** <dd>The maximum length of an SQL statement, in bytes.</dd>)^
  2727. **
  2728. ** ^(<dt>SQLITE_LIMIT_COLUMN</dt>
  2729. ** <dd>The maximum number of columns in a table definition or in the
  2730. ** result set of a [SELECT] or the maximum number of columns in an index
  2731. ** or in an ORDER BY or GROUP BY clause.</dd>)^
  2732. **
  2733. ** ^(<dt>SQLITE_LIMIT_EXPR_DEPTH</dt>
  2734. ** <dd>The maximum depth of the parse tree on any expression.</dd>)^
  2735. **
  2736. ** ^(<dt>SQLITE_LIMIT_COMPOUND_SELECT</dt>
  2737. ** <dd>The maximum number of terms in a compound SELECT statement.</dd>)^
  2738. **
  2739. ** ^(<dt>SQLITE_LIMIT_VDBE_OP</dt>
  2740. ** <dd>The maximum number of instructions in a virtual machine program
  2741. ** used to implement an SQL statement.</dd>)^
  2742. **
  2743. ** ^(<dt>SQLITE_LIMIT_FUNCTION_ARG</dt>
  2744. ** <dd>The maximum number of arguments on a function.</dd>)^
  2745. **
  2746. ** ^(<dt>SQLITE_LIMIT_ATTACHED</dt>
  2747. ** <dd>The maximum number of [ATTACH | attached databases].)^</dd>
  2748. **
  2749. ** ^(<dt>SQLITE_LIMIT_LIKE_PATTERN_LENGTH</dt>
  2750. ** <dd>The maximum length of the pattern argument to the [LIKE] or
  2751. ** [GLOB] operators.</dd>)^
  2752. **
  2753. ** ^(<dt>SQLITE_LIMIT_VARIABLE_NUMBER</dt>
  2754. ** <dd>The maximum number of variables in an SQL statement that can
  2755. ** be bound.</dd>)^
  2756. **
  2757. ** ^(<dt>SQLITE_LIMIT_TRIGGER_DEPTH</dt>
  2758. ** <dd>The maximum depth of recursion for triggers.</dd>)^
  2759. ** </dl>
  2760. */
  2761. #define SQLITE_LIMIT_LENGTH 0
  2762. #define SQLITE_LIMIT_SQL_LENGTH 1
  2763. #define SQLITE_LIMIT_COLUMN 2
  2764. #define SQLITE_LIMIT_EXPR_DEPTH 3
  2765. #define SQLITE_LIMIT_COMPOUND_SELECT 4
  2766. #define SQLITE_LIMIT_VDBE_OP 5
  2767. #define SQLITE_LIMIT_FUNCTION_ARG 6
  2768. #define SQLITE_LIMIT_ATTACHED 7
  2769. #define SQLITE_LIMIT_LIKE_PATTERN_LENGTH 8
  2770. #define SQLITE_LIMIT_VARIABLE_NUMBER 9
  2771. #define SQLITE_LIMIT_TRIGGER_DEPTH 10
  2772. /*
  2773. ** CAPI3REF: Compiling An SQL Statement
  2774. ** KEYWORDS: {SQL statement compiler}
  2775. **
  2776. ** To execute an SQL query, it must first be compiled into a byte-code
  2777. ** program using one of these routines.
  2778. **
  2779. ** The first argument, "db", is a [database connection] obtained from a
  2780. ** prior successful call to [sqlite3_open()], [sqlite3_open_v2()] or
  2781. ** [sqlite3_open16()]. The database connection must not have been closed.
  2782. **
  2783. ** The second argument, "zSql", is the statement to be compiled, encoded
  2784. ** as either UTF-8 or UTF-16. The sqlite3_prepare() and sqlite3_prepare_v2()
  2785. ** interfaces use UTF-8, and sqlite3_prepare16() and sqlite3_prepare16_v2()
  2786. ** use UTF-16.
  2787. **
  2788. ** ^If the nByte argument is less than zero, then zSql is read up to the
  2789. ** first zero terminator. ^If nByte is non-negative, then it is the maximum
  2790. ** number of bytes read from zSql. ^When nByte is non-negative, the
  2791. ** zSql string ends at either the first '\000' or '\u0000' character or
  2792. ** the nByte-th byte, whichever comes first. If the caller knows
  2793. ** that the supplied string is nul-terminated, then there is a small
  2794. ** performance advantage to be gained by passing an nByte parameter that
  2795. ** is equal to the number of bytes in the input string <i>including</i>
  2796. ** the nul-terminator bytes.
  2797. **
  2798. ** ^If pzTail is not NULL then *pzTail is made to point to the first byte
  2799. ** past the end of the first SQL statement in zSql. These routines only
  2800. ** compile the first statement in zSql, so *pzTail is left pointing to
  2801. ** what remains uncompiled.
  2802. **
  2803. ** ^*ppStmt is left pointing to a compiled [prepared statement] that can be
  2804. ** executed using [sqlite3_step()]. ^If there is an error, *ppStmt is set
  2805. ** to NULL. ^If the input text contains no SQL (if the input is an empty
  2806. ** string or a comment) then *ppStmt is set to NULL.
  2807. ** The calling procedure is responsible for deleting the compiled
  2808. ** SQL statement using [sqlite3_finalize()] after it has finished with it.
  2809. ** ppStmt may not be NULL.
  2810. **
  2811. ** ^On success, the sqlite3_prepare() family of routines return [SQLITE_OK];
  2812. ** otherwise an [error code] is returned.
  2813. **
  2814. ** The sqlite3_prepare_v2() and sqlite3_prepare16_v2() interfaces are
  2815. ** recommended for all new programs. The two older interfaces are retained
  2816. ** for backwards compatibility, but their use is discouraged.
  2817. ** ^In the "v2" interfaces, the prepared statement
  2818. ** that is returned (the [sqlite3_stmt] object) contains a copy of the
  2819. ** original SQL text. This causes the [sqlite3_step()] interface to
  2820. ** behave differently in three ways:
  2821. **
  2822. ** <ol>
  2823. ** <li>
  2824. ** ^If the database schema changes, instead of returning [SQLITE_SCHEMA] as it
  2825. ** always used to do, [sqlite3_step()] will automatically recompile the SQL
  2826. ** statement and try to run it again. ^If the schema has changed in
  2827. ** a way that makes the statement no longer valid, [sqlite3_step()] will still
  2828. ** return [SQLITE_SCHEMA]. But unlike the legacy behavior, [SQLITE_SCHEMA] is
  2829. ** now a fatal error. Calling [sqlite3_prepare_v2()] again will not make the
  2830. ** error go away. Note: use [sqlite3_errmsg()] to find the text
  2831. ** of the parsing error that results in an [SQLITE_SCHEMA] return.
  2832. ** </li>
  2833. **
  2834. ** <li>
  2835. ** ^When an error occurs, [sqlite3_step()] will return one of the detailed
  2836. ** [error codes] or [extended error codes]. ^The legacy behavior was that
  2837. ** [sqlite3_step()] would only return a generic [SQLITE_ERROR] result code
  2838. ** and the application would have to make a second call to [sqlite3_reset()]
  2839. ** in order to find the underlying cause of the problem. With the "v2" prepare
  2840. ** interfaces, the underlying reason for the error is returned immediately.
  2841. ** </li>
  2842. **
  2843. ** <li>
  2844. ** ^If the value of a [parameter | host parameter] in the WHERE clause might
  2845. ** change the query plan for a statement, then the statement may be
  2846. ** automatically recompiled (as if there had been a schema change) on the first
  2847. ** [sqlite3_step()] call following any change to the
  2848. ** [sqlite3_bind_text | bindings] of the [parameter].
  2849. ** </li>
  2850. ** </ol>
  2851. */
  2852. SQLITE_API int sqlite3_prepare(
  2853. sqlite3 *db, /* Database handle */
  2854. const char *zSql, /* SQL statement, UTF-8 encoded */
  2855. int nByte, /* Maximum length of zSql in bytes. */
  2856. sqlite3_stmt **ppStmt, /* OUT: Statement handle */
  2857. const char **pzTail /* OUT: Pointer to unused portion of zSql */
  2858. );
  2859. SQLITE_API int sqlite3_prepare_v2(
  2860. sqlite3 *db, /* Database handle */
  2861. const char *zSql, /* SQL statement, UTF-8 encoded */
  2862. int nByte, /* Maximum length of zSql in bytes. */
  2863. sqlite3_stmt **ppStmt, /* OUT: Statement handle */
  2864. const char **pzTail /* OUT: Pointer to unused portion of zSql */
  2865. );
  2866. SQLITE_API int sqlite3_prepare16(
  2867. sqlite3 *db, /* Database handle */
  2868. const void *zSql, /* SQL statement, UTF-16 encoded */
  2869. int nByte, /* Maximum length of zSql in bytes. */
  2870. sqlite3_stmt **ppStmt, /* OUT: Statement handle */
  2871. const void **pzTail /* OUT: Pointer to unused portion of zSql */
  2872. );
  2873. SQLITE_API int sqlite3_prepare16_v2(
  2874. sqlite3 *db, /* Database handle */
  2875. const void *zSql, /* SQL statement, UTF-16 encoded */
  2876. int nByte, /* Maximum length of zSql in bytes. */
  2877. sqlite3_stmt **ppStmt, /* OUT: Statement handle */
  2878. const void **pzTail /* OUT: Pointer to unused portion of zSql */
  2879. );
  2880. /*
  2881. ** CAPI3REF: Retrieving Statement SQL
  2882. **
  2883. ** ^This interface can be used to retrieve a saved copy of the original
  2884. ** SQL text used to create a [prepared statement] if that statement was
  2885. ** compiled using either [sqlite3_prepare_v2()] or [sqlite3_prepare16_v2()].
  2886. */
  2887. SQLITE_API const char *sqlite3_sql(sqlite3_stmt *pStmt);
  2888. /*
  2889. ** CAPI3REF: Dynamically Typed Value Object
  2890. ** KEYWORDS: {protected sqlite3_value} {unprotected sqlite3_value}
  2891. **
  2892. ** SQLite uses the sqlite3_value object to represent all values
  2893. ** that can be stored in a database table. SQLite uses dynamic typing
  2894. ** for the values it stores. ^Values stored in sqlite3_value objects
  2895. ** can be integers, floating point values, strings, BLOBs, or NULL.
  2896. **
  2897. ** An sqlite3_value object may be either "protected" or "unprotected".
  2898. ** Some interfaces require a protected sqlite3_value. Other interfaces
  2899. ** will accept either a protected or an unprotected sqlite3_value.
  2900. ** Every interface that accepts sqlite3_value arguments specifies
  2901. ** whether or not it requires a protected sqlite3_value.
  2902. **
  2903. ** The terms "protected" and "unprotected" refer to whether or not
  2904. ** a mutex is held. A internal mutex is held for a protected
  2905. ** sqlite3_value object but no mutex is held for an unprotected
  2906. ** sqlite3_value object. If SQLite is compiled to be single-threaded
  2907. ** (with [SQLITE_THREADSAFE=0] and with [sqlite3_threadsafe()] returning 0)
  2908. ** or if SQLite is run in one of reduced mutex modes
  2909. ** [SQLITE_CONFIG_SINGLETHREAD] or [SQLITE_CONFIG_MULTITHREAD]
  2910. ** then there is no distinction between protected and unprotected
  2911. ** sqlite3_value objects and they can be used interchangeably. However,
  2912. ** for maximum code portability it is recommended that applications
  2913. ** still make the distinction between between protected and unprotected
  2914. ** sqlite3_value objects even when not strictly required.
  2915. **
  2916. ** ^The sqlite3_value objects that are passed as parameters into the
  2917. ** implementation of [application-defined SQL functions] are protected.
  2918. ** ^The sqlite3_value object returned by
  2919. ** [sqlite3_column_value()] is unprotected.
  2920. ** Unprotected sqlite3_value objects may only be used with
  2921. ** [sqlite3_result_value()] and [sqlite3_bind_value()].
  2922. ** The [sqlite3_value_blob | sqlite3_value_type()] family of
  2923. ** interfaces require protected sqlite3_value objects.
  2924. */
  2925. typedef struct Mem sqlite3_value;
  2926. /*
  2927. ** CAPI3REF: SQL Function Context Object
  2928. **
  2929. ** The context in which an SQL function executes is stored in an
  2930. ** sqlite3_context object. ^A pointer to an sqlite3_context object
  2931. ** is always first parameter to [application-defined SQL functions].
  2932. ** The application-defined SQL function implementation will pass this
  2933. ** pointer through into calls to [sqlite3_result_int | sqlite3_result()],
  2934. ** [sqlite3_aggregate_context()], [sqlite3_user_data()],
  2935. ** [sqlite3_context_db_handle()], [sqlite3_get_auxdata()],
  2936. ** and/or [sqlite3_set_auxdata()].
  2937. */
  2938. typedef struct sqlite3_context sqlite3_context;
  2939. /*
  2940. ** CAPI3REF: Binding Values To Prepared Statements
  2941. ** KEYWORDS: {host parameter} {host parameters} {host parameter name}
  2942. ** KEYWORDS: {SQL parameter} {SQL parameters} {parameter binding}
  2943. **
  2944. ** ^(In the SQL statement text input to [sqlite3_prepare_v2()] and its variants,
  2945. ** literals may be replaced by a [parameter] that matches one of following
  2946. ** templates:
  2947. **
  2948. ** <ul>
  2949. ** <li> ?
  2950. ** <li> ?NNN
  2951. ** <li> :VVV
  2952. ** <li> @VVV
  2953. ** <li> $VVV
  2954. ** </ul>
  2955. **
  2956. ** In the templates above, NNN represents an integer literal,
  2957. ** and VVV represents an alphanumeric identifer.)^ ^The values of these
  2958. ** parameters (also called "host parameter names" or "SQL parameters")
  2959. ** can be set using the sqlite3_bind_*() routines defined here.
  2960. **
  2961. ** ^The first argument to the sqlite3_bind_*() routines is always
  2962. ** a pointer to the [sqlite3_stmt] object returned from
  2963. ** [sqlite3_prepare_v2()] or its variants.
  2964. **
  2965. ** ^The second argument is the index of the SQL parameter to be set.
  2966. ** ^The leftmost SQL parameter has an index of 1. ^When the same named
  2967. ** SQL parameter is used more than once, second and subsequent
  2968. ** occurrences have the same index as the first occurrence.
  2969. ** ^The index for named parameters can be looked up using the
  2970. ** [sqlite3_bind_parameter_index()] API if desired. ^The index
  2971. ** for "?NNN" parameters is the value of NNN.
  2972. ** ^The NNN value must be between 1 and the [sqlite3_limit()]
  2973. ** parameter [SQLITE_LIMIT_VARIABLE_NUMBER] (default value: 999).
  2974. **
  2975. ** ^The third argument is the value to bind to the parameter.
  2976. **
  2977. ** ^(In those routines that have a fourth argument, its value is the
  2978. ** number of bytes in the parameter. To be clear: the value is the
  2979. ** number of <u>bytes</u> in the value, not the number of characters.)^
  2980. ** ^If the fourth parameter is negative, the length of the string is
  2981. ** the number of bytes up to the first zero terminator.
  2982. **
  2983. ** ^The fifth argument to sqlite3_bind_blob(), sqlite3_bind_text(), and
  2984. ** sqlite3_bind_text16() is a destructor used to dispose of the BLOB or
  2985. ** string after SQLite has finished with it. ^If the fifth argument is
  2986. ** the special value [SQLITE_STATIC], then SQLite assumes that the
  2987. ** information is in static, unmanaged space and does not need to be freed.
  2988. ** ^If the fifth argument has the value [SQLITE_TRANSIENT], then
  2989. ** SQLite makes its own private copy of the data immediately, before
  2990. ** the sqlite3_bind_*() routine returns.
  2991. **
  2992. ** ^The sqlite3_bind_zeroblob() routine binds a BLOB of length N that
  2993. ** is filled with zeroes. ^A zeroblob uses a fixed amount of memory
  2994. ** (just an integer to hold its size) while it is being processed.
  2995. ** Zeroblobs are intended to serve as placeholders for BLOBs whose
  2996. ** content is later written using
  2997. ** [sqlite3_blob_open | incremental BLOB I/O] routines.
  2998. ** ^A negative value for the zeroblob results in a zero-length BLOB.
  2999. **
  3000. ** ^If any of the sqlite3_bind_*() routines are called with a NULL pointer
  3001. ** for the [prepared statement] or with a prepared statement for which
  3002. ** [sqlite3_step()] has been called more recently than [sqlite3_reset()],
  3003. ** then the call will return [SQLITE_MISUSE]. If any sqlite3_bind_()
  3004. ** routine is passed a [prepared statement] that has been finalized, the
  3005. ** result is undefined and probably harmful.
  3006. **
  3007. ** ^Bindings are not cleared by the [sqlite3_reset()] routine.
  3008. ** ^Unbound parameters are interpreted as NULL.
  3009. **
  3010. ** ^The sqlite3_bind_* routines return [SQLITE_OK] on success or an
  3011. ** [error code] if anything goes wrong.
  3012. ** ^[SQLITE_RANGE] is returned if the parameter
  3013. ** index is out of range. ^[SQLITE_NOMEM] is returned if malloc() fails.
  3014. **
  3015. ** See also: [sqlite3_bind_parameter_count()],
  3016. ** [sqlite3_bind_parameter_name()], and [sqlite3_bind_parameter_index()].
  3017. */
  3018. SQLITE_API int sqlite3_bind_blob(sqlite3_stmt*, int, const void*, int n, void(*)(void*));
  3019. SQLITE_API int sqlite3_bind_double(sqlite3_stmt*, int, double);
  3020. SQLITE_API int sqlite3_bind_int(sqlite3_stmt*, int, int);
  3021. SQLITE_API int sqlite3_bind_int64(sqlite3_stmt*, int, sqlite3_int64);
  3022. SQLITE_API int sqlite3_bind_null(sqlite3_stmt*, int);
  3023. SQLITE_API int sqlite3_bind_text(sqlite3_stmt*, int, const char*, int n, void(*)(void*));
  3024. SQLITE_API int sqlite3_bind_text16(sqlite3_stmt*, int, const void*, int, void(*)(void*));
  3025. SQLITE_API int sqlite3_bind_value(sqlite3_stmt*, int, const sqlite3_value*);
  3026. SQLITE_API int sqlite3_bind_zeroblob(sqlite3_stmt*, int, int n);
  3027. /*
  3028. ** CAPI3REF: Number Of SQL Parameters
  3029. **
  3030. ** ^This routine can be used to find the number of [SQL parameters]
  3031. ** in a [prepared statement]. SQL parameters are tokens of the
  3032. ** form "?", "?NNN", ":AAA", "$AAA", or "@AAA" that serve as
  3033. ** placeholders for values that are [sqlite3_bind_blob | bound]
  3034. ** to the parameters at a later time.
  3035. **
  3036. ** ^(This routine actually returns the index of the largest (rightmost)
  3037. ** parameter. For all forms except ?NNN, this will correspond to the
  3038. ** number of unique parameters. If parameters of the ?NNN form are used,
  3039. ** there may be gaps in the list.)^
  3040. **
  3041. ** See also: [sqlite3_bind_blob|sqlite3_bind()],
  3042. ** [sqlite3_bind_parameter_name()], and
  3043. ** [sqlite3_bind_parameter_index()].
  3044. */
  3045. SQLITE_API int sqlite3_bind_parameter_count(sqlite3_stmt*);
  3046. /*
  3047. ** CAPI3REF: Name Of A Host Parameter
  3048. **
  3049. ** ^The sqlite3_bind_parameter_name(P,N) interface returns
  3050. ** the name of the N-th [SQL parameter] in the [prepared statement] P.
  3051. ** ^(SQL parameters of the form "?NNN" or ":AAA" or "@AAA" or "$AAA"
  3052. ** have a name which is the string "?NNN" or ":AAA" or "@AAA" or "$AAA"
  3053. ** respectively.
  3054. ** In other words, the initial ":" or "$" or "@" or "?"
  3055. ** is included as part of the name.)^
  3056. ** ^Parameters of the form "?" without a following integer have no name
  3057. ** and are referred to as "nameless" or "anonymous parameters".
  3058. **
  3059. ** ^The first host parameter has an index of 1, not 0.
  3060. **
  3061. ** ^If the value N is out of range or if the N-th parameter is
  3062. ** nameless, then NULL is returned. ^The returned string is
  3063. ** always in UTF-8 encoding even if the named parameter was
  3064. ** originally specified as UTF-16 in [sqlite3_prepare16()] or
  3065. ** [sqlite3_prepare16_v2()].
  3066. **
  3067. ** See also: [sqlite3_bind_blob|sqlite3_bind()],
  3068. ** [sqlite3_bind_parameter_count()], and
  3069. ** [sqlite3_bind_parameter_index()].
  3070. */
  3071. SQLITE_API const char *sqlite3_bind_parameter_name(sqlite3_stmt*, int);
  3072. /*
  3073. ** CAPI3REF: Index Of A Parameter With A Given Name
  3074. **
  3075. ** ^Return the index of an SQL parameter given its name. ^The
  3076. ** index value returned is suitable for use as the second
  3077. ** parameter to [sqlite3_bind_blob|sqlite3_bind()]. ^A zero
  3078. ** is returned if no matching parameter is found. ^The parameter
  3079. ** name must be given in UTF-8 even if the original statement
  3080. ** was prepared from UTF-16 text using [sqlite3_prepare16_v2()].
  3081. **
  3082. ** See also: [sqlite3_bind_blob|sqlite3_bind()],
  3083. ** [sqlite3_bind_parameter_count()], and
  3084. ** [sqlite3_bind_parameter_index()].
  3085. */
  3086. SQLITE_API int sqlite3_bind_parameter_index(sqlite3_stmt*, const char *zName);
  3087. /*
  3088. ** CAPI3REF: Reset All Bindings On A Prepared Statement
  3089. **
  3090. ** ^Contrary to the intuition of many, [sqlite3_reset()] does not reset
  3091. ** the [sqlite3_bind_blob | bindings] on a [prepared statement].
  3092. ** ^Use this routine to reset all host parameters to NULL.
  3093. */
  3094. SQLITE_API int sqlite3_clear_bindings(sqlite3_stmt*);
  3095. /*
  3096. ** CAPI3REF: Number Of Columns In A Result Set
  3097. **
  3098. ** ^Return the number of columns in the result set returned by the
  3099. ** [prepared statement]. ^This routine returns 0 if pStmt is an SQL
  3100. ** statement that does not return data (for example an [UPDATE]).
  3101. */
  3102. SQLITE_API int sqlite3_column_count(sqlite3_stmt *pStmt);
  3103. /*
  3104. ** CAPI3REF: Column Names In A Result Set
  3105. **
  3106. ** ^These routines return the name assigned to a particular column
  3107. ** in the result set of a [SELECT] statement. ^The sqlite3_column_name()
  3108. ** interface returns a pointer to a zero-terminated UTF-8 string
  3109. ** and sqlite3_column_name16() returns a pointer to a zero-terminated
  3110. ** UTF-16 string. ^The first parameter is the [prepared statement]
  3111. ** that implements the [SELECT] statement. ^The second parameter is the
  3112. ** column number. ^The leftmost column is number 0.
  3113. **
  3114. ** ^The returned string pointer is valid until either the [prepared statement]
  3115. ** is destroyed by [sqlite3_finalize()] or until the next call to
  3116. ** sqlite3_column_name() or sqlite3_column_name16() on the same column.
  3117. **
  3118. ** ^If sqlite3_malloc() fails during the processing of either routine
  3119. ** (for example during a conversion from UTF-8 to UTF-16) then a
  3120. ** NULL pointer is returned.
  3121. **
  3122. ** ^The name of a result column is the value of the "AS" clause for
  3123. ** that column, if there is an AS clause. If there is no AS clause
  3124. ** then the name of the column is unspecified and may change from
  3125. ** one release of SQLite to the next.
  3126. */
  3127. SQLITE_API const char *sqlite3_column_name(sqlite3_stmt*, int N);
  3128. SQLITE_API const void *sqlite3_column_name16(sqlite3_stmt*, int N);
  3129. /*
  3130. ** CAPI3REF: Source Of Data In A Query Result
  3131. **
  3132. ** ^These routines provide a means to determine the database, table, and
  3133. ** table column that is the origin of a particular result column in
  3134. ** [SELECT] statement.
  3135. ** ^The name of the database or table or column can be returned as
  3136. ** either a UTF-8 or UTF-16 string. ^The _database_ routines return
  3137. ** the database name, the _table_ routines return the table name, and
  3138. ** the origin_ routines return the column name.
  3139. ** ^The returned string is valid until the [prepared statement] is destroyed
  3140. ** using [sqlite3_finalize()] or until the same information is requested
  3141. ** again in a different encoding.
  3142. **
  3143. ** ^The names returned are the original un-aliased names of the
  3144. ** database, table, and column.
  3145. **
  3146. ** ^The first argument to these interfaces is a [prepared statement].
  3147. ** ^These functions return information about the Nth result column returned by
  3148. ** the statement, where N is the second function argument.
  3149. ** ^The left-most column is column 0 for these routines.
  3150. **
  3151. ** ^If the Nth column returned by the statement is an expression or
  3152. ** subquery and is not a column value, then all of these functions return
  3153. ** NULL. ^These routine might also return NULL if a memory allocation error
  3154. ** occurs. ^Otherwise, they return the name of the attached database, table,
  3155. ** or column that query result column was extracted from.
  3156. **
  3157. ** ^As with all other SQLite APIs, those whose names end with "16" return
  3158. ** UTF-16 encoded strings and the other functions return UTF-8.
  3159. **
  3160. ** ^These APIs are only available if the library was compiled with the
  3161. ** [SQLITE_ENABLE_COLUMN_METADATA] C-preprocessor symbol.
  3162. **
  3163. ** If two or more threads call one or more of these routines against the same
  3164. ** prepared statement and column at the same time then the results are
  3165. ** undefined.
  3166. **
  3167. ** If two or more threads call one or more
  3168. ** [sqlite3_column_database_name | column metadata interfaces]
  3169. ** for the same [prepared statement] and result column
  3170. ** at the same time then the results are undefined.
  3171. */
  3172. SQLITE_API const char *sqlite3_column_database_name(sqlite3_stmt*,int);
  3173. SQLITE_API const void *sqlite3_column_database_name16(sqlite3_stmt*,int);
  3174. SQLITE_API const char *sqlite3_column_table_name(sqlite3_stmt*,int);
  3175. SQLITE_API const void *sqlite3_column_table_name16(sqlite3_stmt*,int);
  3176. SQLITE_API const char *sqlite3_column_origin_name(sqlite3_stmt*,int);
  3177. SQLITE_API const void *sqlite3_column_origin_name16(sqlite3_stmt*,int);
  3178. /*
  3179. ** CAPI3REF: Declared Datatype Of A Query Result
  3180. **
  3181. ** ^(The first parameter is a [prepared statement].
  3182. ** If this statement is a [SELECT] statement and the Nth column of the
  3183. ** returned result set of that [SELECT] is a table column (not an
  3184. ** expression or subquery) then the declared type of the table
  3185. ** column is returned.)^ ^If the Nth column of the result set is an
  3186. ** expression or subquery, then a NULL pointer is returned.
  3187. ** ^The returned string is always UTF-8 encoded.
  3188. **
  3189. ** ^(For example, given the database schema:
  3190. **
  3191. ** CREATE TABLE t1(c1 VARIANT);
  3192. **
  3193. ** and the following statement to be compiled:
  3194. **
  3195. ** SELECT c1 + 1, c1 FROM t1;
  3196. **
  3197. ** this routine would return the string "VARIANT" for the second result
  3198. ** column (i==1), and a NULL pointer for the first result column (i==0).)^
  3199. **
  3200. ** ^SQLite uses dynamic run-time typing. ^So just because a column
  3201. ** is declared to contain a particular type does not mean that the
  3202. ** data stored in that column is of the declared type. SQLite is
  3203. ** strongly typed, but the typing is dynamic not static. ^Type
  3204. ** is associated with individual values, not with the containers
  3205. ** used to hold those values.
  3206. */
  3207. SQLITE_API const char *sqlite3_column_decltype(sqlite3_stmt*,int);
  3208. SQLITE_API const void *sqlite3_column_decltype16(sqlite3_stmt*,int);
  3209. /*
  3210. ** CAPI3REF: Evaluate An SQL Statement
  3211. **
  3212. ** After a [prepared statement] has been prepared using either
  3213. ** [sqlite3_prepare_v2()] or [sqlite3_prepare16_v2()] or one of the legacy
  3214. ** interfaces [sqlite3_prepare()] or [sqlite3_prepare16()], this function
  3215. ** must be called one or more times to evaluate the statement.
  3216. **
  3217. ** The details of the behavior of the sqlite3_step() interface depend
  3218. ** on whether the statement was prepared using the newer "v2" interface
  3219. ** [sqlite3_prepare_v2()] and [sqlite3_prepare16_v2()] or the older legacy
  3220. ** interface [sqlite3_prepare()] and [sqlite3_prepare16()]. The use of the
  3221. ** new "v2" interface is recommended for new applications but the legacy
  3222. ** interface will continue to be supported.
  3223. **
  3224. ** ^In the legacy interface, the return value will be either [SQLITE_BUSY],
  3225. ** [SQLITE_DONE], [SQLITE_ROW], [SQLITE_ERROR], or [SQLITE_MISUSE].
  3226. ** ^With the "v2" interface, any of the other [result codes] or
  3227. ** [extended result codes] might be returned as well.
  3228. **
  3229. ** ^[SQLITE_BUSY] means that the database engine was unable to acquire the
  3230. ** database locks it needs to do its job. ^If the statement is a [COMMIT]
  3231. ** or occurs outside of an explicit transaction, then you can retry the
  3232. ** statement. If the statement is not a [COMMIT] and occurs within a
  3233. ** explicit transaction then you should rollback the transaction before
  3234. ** continuing.
  3235. **
  3236. ** ^[SQLITE_DONE] means that the statement has finished executing
  3237. ** successfully. sqlite3_step() should not be called again on this virtual
  3238. ** machine without first calling [sqlite3_reset()] to reset the virtual
  3239. ** machine back to its initial state.
  3240. **
  3241. ** ^If the SQL statement being executed returns any data, then [SQLITE_ROW]
  3242. ** is returned each time a new row of data is ready for processing by the
  3243. ** caller. The values may be accessed using the [column access functions].
  3244. ** sqlite3_step() is called again to retrieve the next row of data.
  3245. **
  3246. ** ^[SQLITE_ERROR] means that a run-time error (such as a constraint
  3247. ** violation) has occurred. sqlite3_step() should not be called again on
  3248. ** the VM. More information may be found by calling [sqlite3_errmsg()].
  3249. ** ^With the legacy interface, a more specific error code (for example,
  3250. ** [SQLITE_INTERRUPT], [SQLITE_SCHEMA], [SQLITE_CORRUPT], and so forth)
  3251. ** can be obtained by calling [sqlite3_reset()] on the
  3252. ** [prepared statement]. ^In the "v2" interface,
  3253. ** the more specific error code is returned directly by sqlite3_step().
  3254. **
  3255. ** [SQLITE_MISUSE] means that the this routine was called inappropriately.
  3256. ** Perhaps it was called on a [prepared statement] that has
  3257. ** already been [sqlite3_finalize | finalized] or on one that had
  3258. ** previously returned [SQLITE_ERROR] or [SQLITE_DONE]. Or it could
  3259. ** be the case that the same database connection is being used by two or
  3260. ** more threads at the same moment in time.
  3261. **
  3262. ** <b>Goofy Interface Alert:</b> In the legacy interface, the sqlite3_step()
  3263. ** API always returns a generic error code, [SQLITE_ERROR], following any
  3264. ** error other than [SQLITE_BUSY] and [SQLITE_MISUSE]. You must call
  3265. ** [sqlite3_reset()] or [sqlite3_finalize()] in order to find one of the
  3266. ** specific [error codes] that better describes the error.
  3267. ** We admit that this is a goofy design. The problem has been fixed
  3268. ** with the "v2" interface. If you prepare all of your SQL statements
  3269. ** using either [sqlite3_prepare_v2()] or [sqlite3_prepare16_v2()] instead
  3270. ** of the legacy [sqlite3_prepare()] and [sqlite3_prepare16()] interfaces,
  3271. ** then the more specific [error codes] are returned directly
  3272. ** by sqlite3_step(). The use of the "v2" interface is recommended.
  3273. */
  3274. SQLITE_API int sqlite3_step(sqlite3_stmt*);
  3275. /*
  3276. ** CAPI3REF: Number of columns in a result set
  3277. **
  3278. ** ^The sqlite3_data_count(P) the number of columns in the
  3279. ** of the result set of [prepared statement] P.
  3280. */
  3281. SQLITE_API int sqlite3_data_count(sqlite3_stmt *pStmt);
  3282. /*
  3283. ** CAPI3REF: Fundamental Datatypes
  3284. ** KEYWORDS: SQLITE_TEXT
  3285. **
  3286. ** ^(Every value in SQLite has one of five fundamental datatypes:
  3287. **
  3288. ** <ul>
  3289. ** <li> 64-bit signed integer
  3290. ** <li> 64-bit IEEE floating point number
  3291. ** <li> string
  3292. ** <li> BLOB
  3293. ** <li> NULL
  3294. ** </ul>)^
  3295. **
  3296. ** These constants are codes for each of those types.
  3297. **
  3298. ** Note that the SQLITE_TEXT constant was also used in SQLite version 2
  3299. ** for a completely different meaning. Software that links against both
  3300. ** SQLite version 2 and SQLite version 3 should use SQLITE3_TEXT, not
  3301. ** SQLITE_TEXT.
  3302. */
  3303. #define SQLITE_INTEGER 1
  3304. #define SQLITE_FLOAT 2
  3305. #define SQLITE_BLOB 4
  3306. #define SQLITE_NULL 5
  3307. #ifdef SQLITE_TEXT
  3308. # undef SQLITE_TEXT
  3309. #else
  3310. # define SQLITE_TEXT 3
  3311. #endif
  3312. #define SQLITE3_TEXT 3
  3313. /*
  3314. ** CAPI3REF: Result Values From A Query
  3315. ** KEYWORDS: {column access functions}
  3316. **
  3317. ** These routines form the "result set" interface.
  3318. **
  3319. ** ^These routines return information about a single column of the current
  3320. ** result row of a query. ^In every case the first argument is a pointer
  3321. ** to the [prepared statement] that is being evaluated (the [sqlite3_stmt*]
  3322. ** that was returned from [sqlite3_prepare_v2()] or one of its variants)
  3323. ** and the second argument is the index of the column for which information
  3324. ** should be returned. ^The leftmost column of the result set has the index 0.
  3325. ** ^The number of columns in the result can be determined using
  3326. ** [sqlite3_column_count()].
  3327. **
  3328. ** If the SQL statement does not currently point to a valid row, or if the
  3329. ** column index is out of range, the result is undefined.
  3330. ** These routines may only be called when the most recent call to
  3331. ** [sqlite3_step()] has returned [SQLITE_ROW] and neither
  3332. ** [sqlite3_reset()] nor [sqlite3_finalize()] have been called subsequently.
  3333. ** If any of these routines are called after [sqlite3_reset()] or
  3334. ** [sqlite3_finalize()] or after [sqlite3_step()] has returned
  3335. ** something other than [SQLITE_ROW], the results are undefined.
  3336. ** If [sqlite3_step()] or [sqlite3_reset()] or [sqlite3_finalize()]
  3337. ** are called from a different thread while any of these routines
  3338. ** are pending, then the results are undefined.
  3339. **
  3340. ** ^The sqlite3_column_type() routine returns the
  3341. ** [SQLITE_INTEGER | datatype code] for the initial data type
  3342. ** of the result column. ^The returned value is one of [SQLITE_INTEGER],
  3343. ** [SQLITE_FLOAT], [SQLITE_TEXT], [SQLITE_BLOB], or [SQLITE_NULL]. The value
  3344. ** returned by sqlite3_column_type() is only meaningful if no type
  3345. ** conversions have occurred as described below. After a type conversion,
  3346. ** the value returned by sqlite3_column_type() is undefined. Future
  3347. ** versions of SQLite may change the behavior of sqlite3_column_type()
  3348. ** following a type conversion.
  3349. **
  3350. ** ^If the result is a BLOB or UTF-8 string then the sqlite3_column_bytes()
  3351. ** routine returns the number of bytes in that BLOB or string.
  3352. ** ^If the result is a UTF-16 string, then sqlite3_column_bytes() converts
  3353. ** the string to UTF-8 and then returns the number of bytes.
  3354. ** ^If the result is a numeric value then sqlite3_column_bytes() uses
  3355. ** [sqlite3_snprintf()] to convert that value to a UTF-8 string and returns
  3356. ** the number of bytes in that string.
  3357. ** ^The value returned does not include the zero terminator at the end
  3358. ** of the string. ^For clarity: the value returned is the number of
  3359. ** bytes in the string, not the number of characters.
  3360. **
  3361. ** ^Strings returned by sqlite3_column_text() and sqlite3_column_text16(),
  3362. ** even empty strings, are always zero terminated. ^The return
  3363. ** value from sqlite3_column_blob() for a zero-length BLOB is an arbitrary
  3364. ** pointer, possibly even a NULL pointer.
  3365. **
  3366. ** ^The sqlite3_column_bytes16() routine is similar to sqlite3_column_bytes()
  3367. ** but leaves the result in UTF-16 in native byte order instead of UTF-8.
  3368. ** ^The zero terminator is not included in this count.
  3369. **
  3370. ** ^The object returned by [sqlite3_column_value()] is an
  3371. ** [unprotected sqlite3_value] object. An unprotected sqlite3_value object
  3372. ** may only be used with [sqlite3_bind_value()] and [sqlite3_result_value()].
  3373. ** If the [unprotected sqlite3_value] object returned by
  3374. ** [sqlite3_column_value()] is used in any other way, including calls
  3375. ** to routines like [sqlite3_value_int()], [sqlite3_value_text()],
  3376. ** or [sqlite3_value_bytes()], then the behavior is undefined.
  3377. **
  3378. ** These routines attempt to convert the value where appropriate. ^For
  3379. ** example, if the internal representation is FLOAT and a text result
  3380. ** is requested, [sqlite3_snprintf()] is used internally to perform the
  3381. ** conversion automatically. ^(The following table details the conversions
  3382. ** that are applied:
  3383. **
  3384. ** <blockquote>
  3385. ** <table border="1">
  3386. ** <tr><th> Internal<br>Type <th> Requested<br>Type <th> Conversion
  3387. **
  3388. ** <tr><td> NULL <td> INTEGER <td> Result is 0
  3389. ** <tr><td> NULL <td> FLOAT <td> Result is 0.0
  3390. ** <tr><td> NULL <td> TEXT <td> Result is NULL pointer
  3391. ** <tr><td> NULL <td> BLOB <td> Result is NULL pointer
  3392. ** <tr><td> INTEGER <td> FLOAT <td> Convert from integer to float
  3393. ** <tr><td> INTEGER <td> TEXT <td> ASCII rendering of the integer
  3394. ** <tr><td> INTEGER <td> BLOB <td> Same as INTEGER->TEXT
  3395. ** <tr><td> FLOAT <td> INTEGER <td> Convert from float to integer
  3396. ** <tr><td> FLOAT <td> TEXT <td> ASCII rendering of the float
  3397. ** <tr><td> FLOAT <td> BLOB <td> Same as FLOAT->TEXT
  3398. ** <tr><td> TEXT <td> INTEGER <td> Use atoi()
  3399. ** <tr><td> TEXT <td> FLOAT <td> Use atof()
  3400. ** <tr><td> TEXT <td> BLOB <td> No change
  3401. ** <tr><td> BLOB <td> INTEGER <td> Convert to TEXT then use atoi()
  3402. ** <tr><td> BLOB <td> FLOAT <td> Convert to TEXT then use atof()
  3403. ** <tr><td> BLOB <td> TEXT <td> Add a zero terminator if needed
  3404. ** </table>
  3405. ** </blockquote>)^
  3406. **
  3407. ** The table above makes reference to standard C library functions atoi()
  3408. ** and atof(). SQLite does not really use these functions. It has its
  3409. ** own equivalent internal routines. The atoi() and atof() names are
  3410. ** used in the table for brevity and because they are familiar to most
  3411. ** C programmers.
  3412. **
  3413. ** ^Note that when type conversions occur, pointers returned by prior
  3414. ** calls to sqlite3_column_blob(), sqlite3_column_text(), and/or
  3415. ** sqlite3_column_text16() may be invalidated.
  3416. ** ^(Type conversions and pointer invalidations might occur
  3417. ** in the following cases:
  3418. **
  3419. ** <ul>
  3420. ** <li> The initial content is a BLOB and sqlite3_column_text() or
  3421. ** sqlite3_column_text16() is called. A zero-terminator might
  3422. ** need to be added to the string.</li>
  3423. ** <li> The initial content is UTF-8 text and sqlite3_column_bytes16() or
  3424. ** sqlite3_column_text16() is called. The content must be converted
  3425. ** to UTF-16.</li>
  3426. ** <li> The initial content is UTF-16 text and sqlite3_column_bytes() or
  3427. ** sqlite3_column_text() is called. The content must be converted
  3428. ** to UTF-8.</li>
  3429. ** </ul>)^
  3430. **
  3431. ** ^Conversions between UTF-16be and UTF-16le are always done in place and do
  3432. ** not invalidate a prior pointer, though of course the content of the buffer
  3433. ** that the prior pointer points to will have been modified. Other kinds
  3434. ** of conversion are done in place when it is possible, but sometimes they
  3435. ** are not possible and in those cases prior pointers are invalidated.
  3436. **
  3437. ** ^(The safest and easiest to remember policy is to invoke these routines
  3438. ** in one of the following ways:
  3439. **
  3440. ** <ul>
  3441. ** <li>sqlite3_column_text() followed by sqlite3_column_bytes()</li>
  3442. ** <li>sqlite3_column_blob() followed by sqlite3_column_bytes()</li>
  3443. ** <li>sqlite3_column_text16() followed by sqlite3_column_bytes16()</li>
  3444. ** </ul>)^
  3445. **
  3446. ** In other words, you should call sqlite3_column_text(),
  3447. ** sqlite3_column_blob(), or sqlite3_column_text16() first to force the result
  3448. ** into the desired format, then invoke sqlite3_column_bytes() or
  3449. ** sqlite3_column_bytes16() to find the size of the result. Do not mix calls
  3450. ** to sqlite3_column_text() or sqlite3_column_blob() with calls to
  3451. ** sqlite3_column_bytes16(), and do not mix calls to sqlite3_column_text16()
  3452. ** with calls to sqlite3_column_bytes().
  3453. **
  3454. ** ^The pointers returned are valid until a type conversion occurs as
  3455. ** described above, or until [sqlite3_step()] or [sqlite3_reset()] or
  3456. ** [sqlite3_finalize()] is called. ^The memory space used to hold strings
  3457. ** and BLOBs is freed automatically. Do <b>not</b> pass the pointers returned
  3458. ** [sqlite3_column_blob()], [sqlite3_column_text()], etc. into
  3459. ** [sqlite3_free()].
  3460. **
  3461. ** ^(If a memory allocation error occurs during the evaluation of any
  3462. ** of these routines, a default value is returned. The default value
  3463. ** is either the integer 0, the floating point number 0.0, or a NULL
  3464. ** pointer. Subsequent calls to [sqlite3_errcode()] will return
  3465. ** [SQLITE_NOMEM].)^
  3466. */
  3467. SQLITE_API const void *sqlite3_column_blob(sqlite3_stmt*, int iCol);
  3468. SQLITE_API int sqlite3_column_bytes(sqlite3_stmt*, int iCol);
  3469. SQLITE_API int sqlite3_column_bytes16(sqlite3_stmt*, int iCol);
  3470. SQLITE_API double sqlite3_column_double(sqlite3_stmt*, int iCol);
  3471. SQLITE_API int sqlite3_column_int(sqlite3_stmt*, int iCol);
  3472. SQLITE_API sqlite3_int64 sqlite3_column_int64(sqlite3_stmt*, int iCol);
  3473. SQLITE_API const unsigned char *sqlite3_column_text(sqlite3_stmt*, int iCol);
  3474. SQLITE_API const void *sqlite3_column_text16(sqlite3_stmt*, int iCol);
  3475. SQLITE_API int sqlite3_column_type(sqlite3_stmt*, int iCol);
  3476. SQLITE_API sqlite3_value *sqlite3_column_value(sqlite3_stmt*, int iCol);
  3477. /*
  3478. ** CAPI3REF: Destroy A Prepared Statement Object
  3479. **
  3480. ** ^The sqlite3_finalize() function is called to delete a [prepared statement].
  3481. ** ^If the statement was executed successfully or not executed at all, then
  3482. ** SQLITE_OK is returned. ^If execution of the statement failed then an
  3483. ** [error code] or [extended error code] is returned.
  3484. **
  3485. ** ^This routine can be called at any point during the execution of the
  3486. ** [prepared statement]. ^If the virtual machine has not
  3487. ** completed execution when this routine is called, that is like
  3488. ** encountering an error or an [sqlite3_interrupt | interrupt].
  3489. ** ^Incomplete updates may be rolled back and transactions canceled,
  3490. ** depending on the circumstances, and the
  3491. ** [error code] returned will be [SQLITE_ABORT].
  3492. */
  3493. SQLITE_API int sqlite3_finalize(sqlite3_stmt *pStmt);
  3494. /*
  3495. ** CAPI3REF: Reset A Prepared Statement Object
  3496. **
  3497. ** The sqlite3_reset() function is called to reset a [prepared statement]
  3498. ** object back to its initial state, ready to be re-executed.
  3499. ** ^Any SQL statement variables that had values bound to them using
  3500. ** the [sqlite3_bind_blob | sqlite3_bind_*() API] retain their values.
  3501. ** Use [sqlite3_clear_bindings()] to reset the bindings.
  3502. **
  3503. ** ^The [sqlite3_reset(S)] interface resets the [prepared statement] S
  3504. ** back to the beginning of its program.
  3505. **
  3506. ** ^If the most recent call to [sqlite3_step(S)] for the
  3507. ** [prepared statement] S returned [SQLITE_ROW] or [SQLITE_DONE],
  3508. ** or if [sqlite3_step(S)] has never before been called on S,
  3509. ** then [sqlite3_reset(S)] returns [SQLITE_OK].
  3510. **
  3511. ** ^If the most recent call to [sqlite3_step(S)] for the
  3512. ** [prepared statement] S indicated an error, then
  3513. ** [sqlite3_reset(S)] returns an appropriate [error code].
  3514. **
  3515. ** ^The [sqlite3_reset(S)] interface does not change the values
  3516. ** of any [sqlite3_bind_blob|bindings] on the [prepared statement] S.
  3517. */
  3518. SQLITE_API int sqlite3_reset(sqlite3_stmt *pStmt);
  3519. /*
  3520. ** CAPI3REF: Create Or Redefine SQL Functions
  3521. ** KEYWORDS: {function creation routines}
  3522. ** KEYWORDS: {application-defined SQL function}
  3523. ** KEYWORDS: {application-defined SQL functions}
  3524. **
  3525. ** ^These two functions (collectively known as "function creation routines")
  3526. ** are used to add SQL functions or aggregates or to redefine the behavior
  3527. ** of existing SQL functions or aggregates. The only difference between the
  3528. ** two is that the second parameter, the name of the (scalar) function or
  3529. ** aggregate, is encoded in UTF-8 for sqlite3_create_function() and UTF-16
  3530. ** for sqlite3_create_function16().
  3531. **
  3532. ** ^The first parameter is the [database connection] to which the SQL
  3533. ** function is to be added. ^If an application uses more than one database
  3534. ** connection then application-defined SQL functions must be added
  3535. ** to each database connection separately.
  3536. **
  3537. ** The second parameter is the name of the SQL function to be created or
  3538. ** redefined. ^The length of the name is limited to 255 bytes, exclusive of
  3539. ** the zero-terminator. Note that the name length limit is in bytes, not
  3540. ** characters. ^Any attempt to create a function with a longer name
  3541. ** will result in [SQLITE_ERROR] being returned.
  3542. **
  3543. ** ^The third parameter (nArg)
  3544. ** is the number of arguments that the SQL function or
  3545. ** aggregate takes. ^If this parameter is -1, then the SQL function or
  3546. ** aggregate may take any number of arguments between 0 and the limit
  3547. ** set by [sqlite3_limit]([SQLITE_LIMIT_FUNCTION_ARG]). If the third
  3548. ** parameter is less than -1 or greater than 127 then the behavior is
  3549. ** undefined.
  3550. **
  3551. ** The fourth parameter, eTextRep, specifies what
  3552. ** [SQLITE_UTF8 | text encoding] this SQL function prefers for
  3553. ** its parameters. Any SQL function implementation should be able to work
  3554. ** work with UTF-8, UTF-16le, or UTF-16be. But some implementations may be
  3555. ** more efficient with one encoding than another. ^An application may
  3556. ** invoke sqlite3_create_function() or sqlite3_create_function16() multiple
  3557. ** times with the same function but with different values of eTextRep.
  3558. ** ^When multiple implementations of the same function are available, SQLite
  3559. ** will pick the one that involves the least amount of data conversion.
  3560. ** If there is only a single implementation which does not care what text
  3561. ** encoding is used, then the fourth argument should be [SQLITE_ANY].
  3562. **
  3563. ** ^(The fifth parameter is an arbitrary pointer. The implementation of the
  3564. ** function can gain access to this pointer using [sqlite3_user_data()].)^
  3565. **
  3566. ** The seventh, eighth and ninth parameters, xFunc, xStep and xFinal, are
  3567. ** pointers to C-language functions that implement the SQL function or
  3568. ** aggregate. ^A scalar SQL function requires an implementation of the xFunc
  3569. ** callback only; NULL pointers should be passed as the xStep and xFinal
  3570. ** parameters. ^An aggregate SQL function requires an implementation of xStep
  3571. ** and xFinal and NULL should be passed for xFunc. ^To delete an existing
  3572. ** SQL function or aggregate, pass NULL for all three function callbacks.
  3573. **
  3574. ** ^It is permitted to register multiple implementations of the same
  3575. ** functions with the same name but with either differing numbers of
  3576. ** arguments or differing preferred text encodings. ^SQLite will use
  3577. ** the implementation that most closely matches the way in which the
  3578. ** SQL function is used. ^A function implementation with a non-negative
  3579. ** nArg parameter is a better match than a function implementation with
  3580. ** a negative nArg. ^A function where the preferred text encoding
  3581. ** matches the database encoding is a better
  3582. ** match than a function where the encoding is different.
  3583. ** ^A function where the encoding difference is between UTF16le and UTF16be
  3584. ** is a closer match than a function where the encoding difference is
  3585. ** between UTF8 and UTF16.
  3586. **
  3587. ** ^Built-in functions may be overloaded by new application-defined functions.
  3588. ** ^The first application-defined function with a given name overrides all
  3589. ** built-in functions in the same [database connection] with the same name.
  3590. ** ^Subsequent application-defined functions of the same name only override
  3591. ** prior application-defined functions that are an exact match for the
  3592. ** number of parameters and preferred encoding.
  3593. **
  3594. ** ^An application-defined function is permitted to call other
  3595. ** SQLite interfaces. However, such calls must not
  3596. ** close the database connection nor finalize or reset the prepared
  3597. ** statement in which the function is running.
  3598. */
  3599. SQLITE_API int sqlite3_create_function(
  3600. sqlite3 *db,
  3601. const char *zFunctionName,
  3602. int nArg,
  3603. int eTextRep,
  3604. void *pApp,
  3605. void (*xFunc)(sqlite3_context*,int,sqlite3_value**),
  3606. void (*xStep)(sqlite3_context*,int,sqlite3_value**),
  3607. void (*xFinal)(sqlite3_context*)
  3608. );
  3609. SQLITE_API int sqlite3_create_function16(
  3610. sqlite3 *db,
  3611. const void *zFunctionName,
  3612. int nArg,
  3613. int eTextRep,
  3614. void *pApp,
  3615. void (*xFunc)(sqlite3_context*,int,sqlite3_value**),
  3616. void (*xStep)(sqlite3_context*,int,sqlite3_value**),
  3617. void (*xFinal)(sqlite3_context*)
  3618. );
  3619. /*
  3620. ** CAPI3REF: Text Encodings
  3621. **
  3622. ** These constant define integer codes that represent the various
  3623. ** text encodings supported by SQLite.
  3624. */
  3625. #define SQLITE_UTF8 1
  3626. #define SQLITE_UTF16LE 2
  3627. #define SQLITE_UTF16BE 3
  3628. #define SQLITE_UTF16 4 /* Use native byte order */
  3629. #define SQLITE_ANY 5 /* sqlite3_create_function only */
  3630. #define SQLITE_UTF16_ALIGNED 8 /* sqlite3_create_collation only */
  3631. /*
  3632. ** CAPI3REF: Deprecated Functions
  3633. ** DEPRECATED
  3634. **
  3635. ** These functions are [deprecated]. In order to maintain
  3636. ** backwards compatibility with older code, these functions continue
  3637. ** to be supported. However, new applications should avoid
  3638. ** the use of these functions. To help encourage people to avoid
  3639. ** using these functions, we are not going to tell you what they do.
  3640. */
  3641. #ifndef SQLITE_OMIT_DEPRECATED
  3642. SQLITE_API SQLITE_DEPRECATED int sqlite3_aggregate_count(sqlite3_context*);
  3643. SQLITE_API SQLITE_DEPRECATED int sqlite3_expired(sqlite3_stmt*);
  3644. SQLITE_API SQLITE_DEPRECATED int sqlite3_transfer_bindings(sqlite3_stmt*, sqlite3_stmt*);
  3645. SQLITE_API SQLITE_DEPRECATED int sqlite3_global_recover(void);
  3646. SQLITE_API SQLITE_DEPRECATED void sqlite3_thread_cleanup(void);
  3647. SQLITE_API SQLITE_DEPRECATED int sqlite3_memory_alarm(void(*)(void*,sqlite3_int64,int),void*,sqlite3_int64);
  3648. #endif
  3649. /*
  3650. ** CAPI3REF: Obtaining SQL Function Parameter Values
  3651. **
  3652. ** The C-language implementation of SQL functions and aggregates uses
  3653. ** this set of interface routines to access the parameter values on
  3654. ** the function or aggregate.
  3655. **
  3656. ** The xFunc (for scalar functions) or xStep (for aggregates) parameters
  3657. ** to [sqlite3_create_function()] and [sqlite3_create_function16()]
  3658. ** define callbacks that implement the SQL functions and aggregates.
  3659. ** The 4th parameter to these callbacks is an array of pointers to
  3660. ** [protected sqlite3_value] objects. There is one [sqlite3_value] object for
  3661. ** each parameter to the SQL function. These routines are used to
  3662. ** extract values from the [sqlite3_value] objects.
  3663. **
  3664. ** These routines work only with [protected sqlite3_value] objects.
  3665. ** Any attempt to use these routines on an [unprotected sqlite3_value]
  3666. ** object results in undefined behavior.
  3667. **
  3668. ** ^These routines work just like the corresponding [column access functions]
  3669. ** except that these routines take a single [protected sqlite3_value] object
  3670. ** pointer instead of a [sqlite3_stmt*] pointer and an integer column number.
  3671. **
  3672. ** ^The sqlite3_value_text16() interface extracts a UTF-16 string
  3673. ** in the native byte-order of the host machine. ^The
  3674. ** sqlite3_value_text16be() and sqlite3_value_text16le() interfaces
  3675. ** extract UTF-16 strings as big-endian and little-endian respectively.
  3676. **
  3677. ** ^(The sqlite3_value_numeric_type() interface attempts to apply
  3678. ** numeric affinity to the value. This means that an attempt is
  3679. ** made to convert the value to an integer or floating point. If
  3680. ** such a conversion is possible without loss of information (in other
  3681. ** words, if the value is a string that looks like a number)
  3682. ** then the conversion is performed. Otherwise no conversion occurs.
  3683. ** The [SQLITE_INTEGER | datatype] after conversion is returned.)^
  3684. **
  3685. ** Please pay particular attention to the fact that the pointer returned
  3686. ** from [sqlite3_value_blob()], [sqlite3_value_text()], or
  3687. ** [sqlite3_value_text16()] can be invalidated by a subsequent call to
  3688. ** [sqlite3_value_bytes()], [sqlite3_value_bytes16()], [sqlite3_value_text()],
  3689. ** or [sqlite3_value_text16()].
  3690. **
  3691. ** These routines must be called from the same thread as
  3692. ** the SQL function that supplied the [sqlite3_value*] parameters.
  3693. */
  3694. SQLITE_API const void *sqlite3_value_blob(sqlite3_value*);
  3695. SQLITE_API int sqlite3_value_bytes(sqlite3_value*);
  3696. SQLITE_API int sqlite3_value_bytes16(sqlite3_value*);
  3697. SQLITE_API double sqlite3_value_double(sqlite3_value*);
  3698. SQLITE_API int sqlite3_value_int(sqlite3_value*);
  3699. SQLITE_API sqlite3_int64 sqlite3_value_int64(sqlite3_value*);
  3700. SQLITE_API const unsigned char *sqlite3_value_text(sqlite3_value*);
  3701. SQLITE_API const void *sqlite3_value_text16(sqlite3_value*);
  3702. SQLITE_API const void *sqlite3_value_text16le(sqlite3_value*);
  3703. SQLITE_API const void *sqlite3_value_text16be(sqlite3_value*);
  3704. SQLITE_API int sqlite3_value_type(sqlite3_value*);
  3705. SQLITE_API int sqlite3_value_numeric_type(sqlite3_value*);
  3706. /*
  3707. ** CAPI3REF: Obtain Aggregate Function Context
  3708. **
  3709. ** Implementions of aggregate SQL functions use this
  3710. ** routine to allocate memory for storing their state.
  3711. **
  3712. ** ^The first time the sqlite3_aggregate_context(C,N) routine is called
  3713. ** for a particular aggregate function, SQLite
  3714. ** allocates N of memory, zeroes out that memory, and returns a pointer
  3715. ** to the new memory. ^On second and subsequent calls to
  3716. ** sqlite3_aggregate_context() for the same aggregate function instance,
  3717. ** the same buffer is returned. Sqlite3_aggregate_context() is normally
  3718. ** called once for each invocation of the xStep callback and then one
  3719. ** last time when the xFinal callback is invoked. ^(When no rows match
  3720. ** an aggregate query, the xStep() callback of the aggregate function
  3721. ** implementation is never called and xFinal() is called exactly once.
  3722. ** In those cases, sqlite3_aggregate_context() might be called for the
  3723. ** first time from within xFinal().)^
  3724. **
  3725. ** ^The sqlite3_aggregate_context(C,N) routine returns a NULL pointer if N is
  3726. ** less than or equal to zero or if a memory allocate error occurs.
  3727. **
  3728. ** ^(The amount of space allocated by sqlite3_aggregate_context(C,N) is
  3729. ** determined by the N parameter on first successful call. Changing the
  3730. ** value of N in subsequent call to sqlite3_aggregate_context() within
  3731. ** the same aggregate function instance will not resize the memory
  3732. ** allocation.)^
  3733. **
  3734. ** ^SQLite automatically frees the memory allocated by
  3735. ** sqlite3_aggregate_context() when the aggregate query concludes.
  3736. **
  3737. ** The first parameter must be a copy of the
  3738. ** [sqlite3_context | SQL function context] that is the first parameter
  3739. ** to the xStep or xFinal callback routine that implements the aggregate
  3740. ** function.
  3741. **
  3742. ** This routine must be called from the same thread in which
  3743. ** the aggregate SQL function is running.
  3744. */
  3745. SQLITE_API void *sqlite3_aggregate_context(sqlite3_context*, int nBytes);
  3746. /*
  3747. ** CAPI3REF: User Data For Functions
  3748. **
  3749. ** ^The sqlite3_user_data() interface returns a copy of
  3750. ** the pointer that was the pUserData parameter (the 5th parameter)
  3751. ** of the [sqlite3_create_function()]
  3752. ** and [sqlite3_create_function16()] routines that originally
  3753. ** registered the application defined function.
  3754. **
  3755. ** This routine must be called from the same thread in which
  3756. ** the application-defined function is running.
  3757. */
  3758. SQLITE_API void *sqlite3_user_data(sqlite3_context*);
  3759. /*
  3760. ** CAPI3REF: Database Connection For Functions
  3761. **
  3762. ** ^The sqlite3_context_db_handle() interface returns a copy of
  3763. ** the pointer to the [database connection] (the 1st parameter)
  3764. ** of the [sqlite3_create_function()]
  3765. ** and [sqlite3_create_function16()] routines that originally
  3766. ** registered the application defined function.
  3767. */
  3768. SQLITE_API sqlite3 *sqlite3_context_db_handle(sqlite3_context*);
  3769. /*
  3770. ** CAPI3REF: Function Auxiliary Data
  3771. **
  3772. ** The following two functions may be used by scalar SQL functions to
  3773. ** associate metadata with argument values. If the same value is passed to
  3774. ** multiple invocations of the same SQL function during query execution, under
  3775. ** some circumstances the associated metadata may be preserved. This may
  3776. ** be used, for example, to add a regular-expression matching scalar
  3777. ** function. The compiled version of the regular expression is stored as
  3778. ** metadata associated with the SQL value passed as the regular expression
  3779. ** pattern. The compiled regular expression can be reused on multiple
  3780. ** invocations of the same function so that the original pattern string
  3781. ** does not need to be recompiled on each invocation.
  3782. **
  3783. ** ^The sqlite3_get_auxdata() interface returns a pointer to the metadata
  3784. ** associated by the sqlite3_set_auxdata() function with the Nth argument
  3785. ** value to the application-defined function. ^If no metadata has been ever
  3786. ** been set for the Nth argument of the function, or if the corresponding
  3787. ** function parameter has changed since the meta-data was set,
  3788. ** then sqlite3_get_auxdata() returns a NULL pointer.
  3789. **
  3790. ** ^The sqlite3_set_auxdata() interface saves the metadata
  3791. ** pointed to by its 3rd parameter as the metadata for the N-th
  3792. ** argument of the application-defined function. Subsequent
  3793. ** calls to sqlite3_get_auxdata() might return this data, if it has
  3794. ** not been destroyed.
  3795. ** ^If it is not NULL, SQLite will invoke the destructor
  3796. ** function given by the 4th parameter to sqlite3_set_auxdata() on
  3797. ** the metadata when the corresponding function parameter changes
  3798. ** or when the SQL statement completes, whichever comes first.
  3799. **
  3800. ** SQLite is free to call the destructor and drop metadata on any
  3801. ** parameter of any function at any time. ^The only guarantee is that
  3802. ** the destructor will be called before the metadata is dropped.
  3803. **
  3804. ** ^(In practice, metadata is preserved between function calls for
  3805. ** expressions that are constant at compile time. This includes literal
  3806. ** values and [parameters].)^
  3807. **
  3808. ** These routines must be called from the same thread in which
  3809. ** the SQL function is running.
  3810. */
  3811. SQLITE_API void *sqlite3_get_auxdata(sqlite3_context*, int N);
  3812. SQLITE_API void sqlite3_set_auxdata(sqlite3_context*, int N, void*, void (*)(void*));
  3813. /*
  3814. ** CAPI3REF: Constants Defining Special Destructor Behavior
  3815. **
  3816. ** These are special values for the destructor that is passed in as the
  3817. ** final argument to routines like [sqlite3_result_blob()]. ^If the destructor
  3818. ** argument is SQLITE_STATIC, it means that the content pointer is constant
  3819. ** and will never change. It does not need to be destroyed. ^The
  3820. ** SQLITE_TRANSIENT value means that the content will likely change in
  3821. ** the near future and that SQLite should make its own private copy of
  3822. ** the content before returning.
  3823. **
  3824. ** The typedef is necessary to work around problems in certain
  3825. ** C++ compilers. See ticket #2191.
  3826. */
  3827. typedef void (*sqlite3_destructor_type)(void*);
  3828. #define SQLITE_STATIC ((sqlite3_destructor_type)0)
  3829. #define SQLITE_TRANSIENT ((sqlite3_destructor_type)-1)
  3830. /*
  3831. ** CAPI3REF: Setting The Result Of An SQL Function
  3832. **
  3833. ** These routines are used by the xFunc or xFinal callbacks that
  3834. ** implement SQL functions and aggregates. See
  3835. ** [sqlite3_create_function()] and [sqlite3_create_function16()]
  3836. ** for additional information.
  3837. **
  3838. ** These functions work very much like the [parameter binding] family of
  3839. ** functions used to bind values to host parameters in prepared statements.
  3840. ** Refer to the [SQL parameter] documentation for additional information.
  3841. **
  3842. ** ^The sqlite3_result_blob() interface sets the result from
  3843. ** an application-defined function to be the BLOB whose content is pointed
  3844. ** to by the second parameter and which is N bytes long where N is the
  3845. ** third parameter.
  3846. **
  3847. ** ^The sqlite3_result_zeroblob() interfaces set the result of
  3848. ** the application-defined function to be a BLOB containing all zero
  3849. ** bytes and N bytes in size, where N is the value of the 2nd parameter.
  3850. **
  3851. ** ^The sqlite3_result_double() interface sets the result from
  3852. ** an application-defined function to be a floating point value specified
  3853. ** by its 2nd argument.
  3854. **
  3855. ** ^The sqlite3_result_error() and sqlite3_result_error16() functions
  3856. ** cause the implemented SQL function to throw an exception.
  3857. ** ^SQLite uses the string pointed to by the
  3858. ** 2nd parameter of sqlite3_result_error() or sqlite3_result_error16()
  3859. ** as the text of an error message. ^SQLite interprets the error
  3860. ** message string from sqlite3_result_error() as UTF-8. ^SQLite
  3861. ** interprets the string from sqlite3_result_error16() as UTF-16 in native
  3862. ** byte order. ^If the third parameter to sqlite3_result_error()
  3863. ** or sqlite3_result_error16() is negative then SQLite takes as the error
  3864. ** message all text up through the first zero character.
  3865. ** ^If the third parameter to sqlite3_result_error() or
  3866. ** sqlite3_result_error16() is non-negative then SQLite takes that many
  3867. ** bytes (not characters) from the 2nd parameter as the error message.
  3868. ** ^The sqlite3_result_error() and sqlite3_result_error16()
  3869. ** routines make a private copy of the error message text before
  3870. ** they return. Hence, the calling function can deallocate or
  3871. ** modify the text after they return without harm.
  3872. ** ^The sqlite3_result_error_code() function changes the error code
  3873. ** returned by SQLite as a result of an error in a function. ^By default,
  3874. ** the error code is SQLITE_ERROR. ^A subsequent call to sqlite3_result_error()
  3875. ** or sqlite3_result_error16() resets the error code to SQLITE_ERROR.
  3876. **
  3877. ** ^The sqlite3_result_toobig() interface causes SQLite to throw an error
  3878. ** indicating that a string or BLOB is too long to represent.
  3879. **
  3880. ** ^The sqlite3_result_nomem() interface causes SQLite to throw an error
  3881. ** indicating that a memory allocation failed.
  3882. **
  3883. ** ^The sqlite3_result_int() interface sets the return value
  3884. ** of the application-defined function to be the 32-bit signed integer
  3885. ** value given in the 2nd argument.
  3886. ** ^The sqlite3_result_int64() interface sets the return value
  3887. ** of the application-defined function to be the 64-bit signed integer
  3888. ** value given in the 2nd argument.
  3889. **
  3890. ** ^The sqlite3_result_null() interface sets the return value
  3891. ** of the application-defined function to be NULL.
  3892. **
  3893. ** ^The sqlite3_result_text(), sqlite3_result_text16(),
  3894. ** sqlite3_result_text16le(), and sqlite3_result_text16be() interfaces
  3895. ** set the return value of the application-defined function to be
  3896. ** a text string which is represented as UTF-8, UTF-16 native byte order,
  3897. ** UTF-16 little endian, or UTF-16 big endian, respectively.
  3898. ** ^SQLite takes the text result from the application from
  3899. ** the 2nd parameter of the sqlite3_result_text* interfaces.
  3900. ** ^If the 3rd parameter to the sqlite3_result_text* interfaces
  3901. ** is negative, then SQLite takes result text from the 2nd parameter
  3902. ** through the first zero character.
  3903. ** ^If the 3rd parameter to the sqlite3_result_text* interfaces
  3904. ** is non-negative, then as many bytes (not characters) of the text
  3905. ** pointed to by the 2nd parameter are taken as the application-defined
  3906. ** function result.
  3907. ** ^If the 4th parameter to the sqlite3_result_text* interfaces
  3908. ** or sqlite3_result_blob is a non-NULL pointer, then SQLite calls that
  3909. ** function as the destructor on the text or BLOB result when it has
  3910. ** finished using that result.
  3911. ** ^If the 4th parameter to the sqlite3_result_text* interfaces or to
  3912. ** sqlite3_result_blob is the special constant SQLITE_STATIC, then SQLite
  3913. ** assumes that the text or BLOB result is in constant space and does not
  3914. ** copy the content of the parameter nor call a destructor on the content
  3915. ** when it has finished using that result.
  3916. ** ^If the 4th parameter to the sqlite3_result_text* interfaces
  3917. ** or sqlite3_result_blob is the special constant SQLITE_TRANSIENT
  3918. ** then SQLite makes a copy of the result into space obtained from
  3919. ** from [sqlite3_malloc()] before it returns.
  3920. **
  3921. ** ^The sqlite3_result_value() interface sets the result of
  3922. ** the application-defined function to be a copy the
  3923. ** [unprotected sqlite3_value] object specified by the 2nd parameter. ^The
  3924. ** sqlite3_result_value() interface makes a copy of the [sqlite3_value]
  3925. ** so that the [sqlite3_value] specified in the parameter may change or
  3926. ** be deallocated after sqlite3_result_value() returns without harm.
  3927. ** ^A [protected sqlite3_value] object may always be used where an
  3928. ** [unprotected sqlite3_value] object is required, so either
  3929. ** kind of [sqlite3_value] object can be used with this interface.
  3930. **
  3931. ** If these routines are called from within the different thread
  3932. ** than the one containing the application-defined function that received
  3933. ** the [sqlite3_context] pointer, the results are undefined.
  3934. */
  3935. SQLITE_API void sqlite3_result_blob(sqlite3_context*, const void*, int, void(*)(void*));
  3936. SQLITE_API void sqlite3_result_double(sqlite3_context*, double);
  3937. SQLITE_API void sqlite3_result_error(sqlite3_context*, const char*, int);
  3938. SQLITE_API void sqlite3_result_error16(sqlite3_context*, const void*, int);
  3939. SQLITE_API void sqlite3_result_error_toobig(sqlite3_context*);
  3940. SQLITE_API void sqlite3_result_error_nomem(sqlite3_context*);
  3941. SQLITE_API void sqlite3_result_error_code(sqlite3_context*, int);
  3942. SQLITE_API void sqlite3_result_int(sqlite3_context*, int);
  3943. SQLITE_API void sqlite3_result_int64(sqlite3_context*, sqlite3_int64);
  3944. SQLITE_API void sqlite3_result_null(sqlite3_context*);
  3945. SQLITE_API void sqlite3_result_text(sqlite3_context*, const char*, int, void(*)(void*));
  3946. SQLITE_API void sqlite3_result_text16(sqlite3_context*, const void*, int, void(*)(void*));
  3947. SQLITE_API void sqlite3_result_text16le(sqlite3_context*, const void*, int,void(*)(void*));
  3948. SQLITE_API void sqlite3_result_text16be(sqlite3_context*, const void*, int,void(*)(void*));
  3949. SQLITE_API void sqlite3_result_value(sqlite3_context*, sqlite3_value*);
  3950. SQLITE_API void sqlite3_result_zeroblob(sqlite3_context*, int n);
  3951. /*
  3952. ** CAPI3REF: Define New Collating Sequences
  3953. **
  3954. ** These functions are used to add new collation sequences to the
  3955. ** [database connection] specified as the first argument.
  3956. **
  3957. ** ^The name of the new collation sequence is specified as a UTF-8 string
  3958. ** for sqlite3_create_collation() and sqlite3_create_collation_v2()
  3959. ** and a UTF-16 string for sqlite3_create_collation16(). ^In all cases
  3960. ** the name is passed as the second function argument.
  3961. **
  3962. ** ^The third argument may be one of the constants [SQLITE_UTF8],
  3963. ** [SQLITE_UTF16LE], or [SQLITE_UTF16BE], indicating that the user-supplied
  3964. ** routine expects to be passed pointers to strings encoded using UTF-8,
  3965. ** UTF-16 little-endian, or UTF-16 big-endian, respectively. ^The
  3966. ** third argument might also be [SQLITE_UTF16] to indicate that the routine
  3967. ** expects pointers to be UTF-16 strings in the native byte order, or the
  3968. ** argument can be [SQLITE_UTF16_ALIGNED] if the
  3969. ** the routine expects pointers to 16-bit word aligned strings
  3970. ** of UTF-16 in the native byte order.
  3971. **
  3972. ** A pointer to the user supplied routine must be passed as the fifth
  3973. ** argument. ^If it is NULL, this is the same as deleting the collation
  3974. ** sequence (so that SQLite cannot call it anymore).
  3975. ** ^Each time the application supplied function is invoked, it is passed
  3976. ** as its first parameter a copy of the void* passed as the fourth argument
  3977. ** to sqlite3_create_collation() or sqlite3_create_collation16().
  3978. **
  3979. ** ^The remaining arguments to the application-supplied routine are two strings,
  3980. ** each represented by a (length, data) pair and encoded in the encoding
  3981. ** that was passed as the third argument when the collation sequence was
  3982. ** registered. The application defined collation routine should
  3983. ** return negative, zero or positive if the first string is less than,
  3984. ** equal to, or greater than the second string. i.e. (STRING1 - STRING2).
  3985. **
  3986. ** ^The sqlite3_create_collation_v2() works like sqlite3_create_collation()
  3987. ** except that it takes an extra argument which is a destructor for
  3988. ** the collation. ^The destructor is called when the collation is
  3989. ** destroyed and is passed a copy of the fourth parameter void* pointer
  3990. ** of the sqlite3_create_collation_v2().
  3991. ** ^Collations are destroyed when they are overridden by later calls to the
  3992. ** collation creation functions or when the [database connection] is closed
  3993. ** using [sqlite3_close()].
  3994. **
  3995. ** See also: [sqlite3_collation_needed()] and [sqlite3_collation_needed16()].
  3996. */
  3997. SQLITE_API int sqlite3_create_collation(
  3998. sqlite3*,
  3999. const char *zName,
  4000. int eTextRep,
  4001. void*,
  4002. int(*xCompare)(void*,int,const void*,int,const void*)
  4003. );
  4004. SQLITE_API int sqlite3_create_collation_v2(
  4005. sqlite3*,
  4006. const char *zName,
  4007. int eTextRep,
  4008. void*,
  4009. int(*xCompare)(void*,int,const void*,int,const void*),
  4010. void(*xDestroy)(void*)
  4011. );
  4012. SQLITE_API int sqlite3_create_collation16(
  4013. sqlite3*,
  4014. const void *zName,
  4015. int eTextRep,
  4016. void*,
  4017. int(*xCompare)(void*,int,const void*,int,const void*)
  4018. );
  4019. /*
  4020. ** CAPI3REF: Collation Needed Callbacks
  4021. **
  4022. ** ^To avoid having to register all collation sequences before a database
  4023. ** can be used, a single callback function may be registered with the
  4024. ** [database connection] to be invoked whenever an undefined collation
  4025. ** sequence is required.
  4026. **
  4027. ** ^If the function is registered using the sqlite3_collation_needed() API,
  4028. ** then it is passed the names of undefined collation sequences as strings
  4029. ** encoded in UTF-8. ^If sqlite3_collation_needed16() is used,
  4030. ** the names are passed as UTF-16 in machine native byte order.
  4031. ** ^A call to either function replaces the existing collation-needed callback.
  4032. **
  4033. ** ^(When the callback is invoked, the first argument passed is a copy
  4034. ** of the second argument to sqlite3_collation_needed() or
  4035. ** sqlite3_collation_needed16(). The second argument is the database
  4036. ** connection. The third argument is one of [SQLITE_UTF8], [SQLITE_UTF16BE],
  4037. ** or [SQLITE_UTF16LE], indicating the most desirable form of the collation
  4038. ** sequence function required. The fourth parameter is the name of the
  4039. ** required collation sequence.)^
  4040. **
  4041. ** The callback function should register the desired collation using
  4042. ** [sqlite3_create_collation()], [sqlite3_create_collation16()], or
  4043. ** [sqlite3_create_collation_v2()].
  4044. */
  4045. SQLITE_API int sqlite3_collation_needed(
  4046. sqlite3*,
  4047. void*,
  4048. void(*)(void*,sqlite3*,int eTextRep,const char*)
  4049. );
  4050. SQLITE_API int sqlite3_collation_needed16(
  4051. sqlite3*,
  4052. void*,
  4053. void(*)(void*,sqlite3*,int eTextRep,const void*)
  4054. );
  4055. /*
  4056. ** Specify the key for an encrypted database. This routine should be
  4057. ** called right after sqlite3_open().
  4058. **
  4059. ** The code to implement this API is not available in the public release
  4060. ** of SQLite.
  4061. */
  4062. SQLITE_API int sqlite3_key(
  4063. sqlite3 *db, /* Database to be rekeyed */
  4064. const void *pKey, int nKey /* The key */
  4065. );
  4066. /*
  4067. ** Change the key on an open database. If the current database is not
  4068. ** encrypted, this routine will encrypt it. If pNew==0 or nNew==0, the
  4069. ** database is decrypted.
  4070. **
  4071. ** The code to implement this API is not available in the public release
  4072. ** of SQLite.
  4073. */
  4074. SQLITE_API int sqlite3_rekey(
  4075. sqlite3 *db, /* Database to be rekeyed */
  4076. const void *pKey, int nKey /* The new key */
  4077. );
  4078. /*
  4079. ** CAPI3REF: Suspend Execution For A Short Time
  4080. **
  4081. ** ^The sqlite3_sleep() function causes the current thread to suspend execution
  4082. ** for at least a number of milliseconds specified in its parameter.
  4083. **
  4084. ** ^If the operating system does not support sleep requests with
  4085. ** millisecond time resolution, then the time will be rounded up to
  4086. ** the nearest second. ^The number of milliseconds of sleep actually
  4087. ** requested from the operating system is returned.
  4088. **
  4089. ** ^SQLite implements this interface by calling the xSleep()
  4090. ** method of the default [sqlite3_vfs] object.
  4091. */
  4092. SQLITE_API int sqlite3_sleep(int);
  4093. /*
  4094. ** CAPI3REF: Name Of The Folder Holding Temporary Files
  4095. **
  4096. ** ^(If this global variable is made to point to a string which is
  4097. ** the name of a folder (a.k.a. directory), then all temporary files
  4098. ** created by SQLite when using a built-in [sqlite3_vfs | VFS]
  4099. ** will be placed in that directory.)^ ^If this variable
  4100. ** is a NULL pointer, then SQLite performs a search for an appropriate
  4101. ** temporary file directory.
  4102. **
  4103. ** It is not safe to read or modify this variable in more than one
  4104. ** thread at a time. It is not safe to read or modify this variable
  4105. ** if a [database connection] is being used at the same time in a separate
  4106. ** thread.
  4107. ** It is intended that this variable be set once
  4108. ** as part of process initialization and before any SQLite interface
  4109. ** routines have been called and that this variable remain unchanged
  4110. ** thereafter.
  4111. **
  4112. ** ^The [temp_store_directory pragma] may modify this variable and cause
  4113. ** it to point to memory obtained from [sqlite3_malloc]. ^Furthermore,
  4114. ** the [temp_store_directory pragma] always assumes that any string
  4115. ** that this variable points to is held in memory obtained from
  4116. ** [sqlite3_malloc] and the pragma may attempt to free that memory
  4117. ** using [sqlite3_free].
  4118. ** Hence, if this variable is modified directly, either it should be
  4119. ** made NULL or made to point to memory obtained from [sqlite3_malloc]
  4120. ** or else the use of the [temp_store_directory pragma] should be avoided.
  4121. */
  4122. SQLITE_API char *sqlite3_temp_directory;
  4123. /*
  4124. ** CAPI3REF: Test For Auto-Commit Mode
  4125. ** KEYWORDS: {autocommit mode}
  4126. **
  4127. ** ^The sqlite3_get_autocommit() interface returns non-zero or
  4128. ** zero if the given database connection is or is not in autocommit mode,
  4129. ** respectively. ^Autocommit mode is on by default.
  4130. ** ^Autocommit mode is disabled by a [BEGIN] statement.
  4131. ** ^Autocommit mode is re-enabled by a [COMMIT] or [ROLLBACK].
  4132. **
  4133. ** If certain kinds of errors occur on a statement within a multi-statement
  4134. ** transaction (errors including [SQLITE_FULL], [SQLITE_IOERR],
  4135. ** [SQLITE_NOMEM], [SQLITE_BUSY], and [SQLITE_INTERRUPT]) then the
  4136. ** transaction might be rolled back automatically. The only way to
  4137. ** find out whether SQLite automatically rolled back the transaction after
  4138. ** an error is to use this function.
  4139. **
  4140. ** If another thread changes the autocommit status of the database
  4141. ** connection while this routine is running, then the return value
  4142. ** is undefined.
  4143. */
  4144. SQLITE_API int sqlite3_get_autocommit(sqlite3*);
  4145. /*
  4146. ** CAPI3REF: Find The Database Handle Of A Prepared Statement
  4147. **
  4148. ** ^The sqlite3_db_handle interface returns the [database connection] handle
  4149. ** to which a [prepared statement] belongs. ^The [database connection]
  4150. ** returned by sqlite3_db_handle is the same [database connection]
  4151. ** that was the first argument
  4152. ** to the [sqlite3_prepare_v2()] call (or its variants) that was used to
  4153. ** create the statement in the first place.
  4154. */
  4155. SQLITE_API sqlite3 *sqlite3_db_handle(sqlite3_stmt*);
  4156. /*
  4157. ** CAPI3REF: Find the next prepared statement
  4158. **
  4159. ** ^This interface returns a pointer to the next [prepared statement] after
  4160. ** pStmt associated with the [database connection] pDb. ^If pStmt is NULL
  4161. ** then this interface returns a pointer to the first prepared statement
  4162. ** associated with the database connection pDb. ^If no prepared statement
  4163. ** satisfies the conditions of this routine, it returns NULL.
  4164. **
  4165. ** The [database connection] pointer D in a call to
  4166. ** [sqlite3_next_stmt(D,S)] must refer to an open database
  4167. ** connection and in particular must not be a NULL pointer.
  4168. */
  4169. SQLITE_API sqlite3_stmt *sqlite3_next_stmt(sqlite3 *pDb, sqlite3_stmt *pStmt);
  4170. /*
  4171. ** CAPI3REF: Commit And Rollback Notification Callbacks
  4172. **
  4173. ** ^The sqlite3_commit_hook() interface registers a callback
  4174. ** function to be invoked whenever a transaction is [COMMIT | committed].
  4175. ** ^Any callback set by a previous call to sqlite3_commit_hook()
  4176. ** for the same database connection is overridden.
  4177. ** ^The sqlite3_rollback_hook() interface registers a callback
  4178. ** function to be invoked whenever a transaction is [ROLLBACK | rolled back].
  4179. ** ^Any callback set by a previous call to sqlite3_rollback_hook()
  4180. ** for the same database connection is overridden.
  4181. ** ^The pArg argument is passed through to the callback.
  4182. ** ^If the callback on a commit hook function returns non-zero,
  4183. ** then the commit is converted into a rollback.
  4184. **
  4185. ** ^The sqlite3_commit_hook(D,C,P) and sqlite3_rollback_hook(D,C,P) functions
  4186. ** return the P argument from the previous call of the same function
  4187. ** on the same [database connection] D, or NULL for
  4188. ** the first call for each function on D.
  4189. **
  4190. ** The callback implementation must not do anything that will modify
  4191. ** the database connection that invoked the callback. Any actions
  4192. ** to modify the database connection must be deferred until after the
  4193. ** completion of the [sqlite3_step()] call that triggered the commit
  4194. ** or rollback hook in the first place.
  4195. ** Note that [sqlite3_prepare_v2()] and [sqlite3_step()] both modify their
  4196. ** database connections for the meaning of "modify" in this paragraph.
  4197. **
  4198. ** ^Registering a NULL function disables the callback.
  4199. **
  4200. ** ^When the commit hook callback routine returns zero, the [COMMIT]
  4201. ** operation is allowed to continue normally. ^If the commit hook
  4202. ** returns non-zero, then the [COMMIT] is converted into a [ROLLBACK].
  4203. ** ^The rollback hook is invoked on a rollback that results from a commit
  4204. ** hook returning non-zero, just as it would be with any other rollback.
  4205. **
  4206. ** ^For the purposes of this API, a transaction is said to have been
  4207. ** rolled back if an explicit "ROLLBACK" statement is executed, or
  4208. ** an error or constraint causes an implicit rollback to occur.
  4209. ** ^The rollback callback is not invoked if a transaction is
  4210. ** automatically rolled back because the database connection is closed.
  4211. ** ^The rollback callback is not invoked if a transaction is
  4212. ** rolled back because a commit callback returned non-zero.
  4213. **
  4214. ** See also the [sqlite3_update_hook()] interface.
  4215. */
  4216. SQLITE_API void *sqlite3_commit_hook(sqlite3*, int(*)(void*), void*);
  4217. SQLITE_API void *sqlite3_rollback_hook(sqlite3*, void(*)(void *), void*);
  4218. /*
  4219. ** CAPI3REF: Data Change Notification Callbacks
  4220. **
  4221. ** ^The sqlite3_update_hook() interface registers a callback function
  4222. ** with the [database connection] identified by the first argument
  4223. ** to be invoked whenever a row is updated, inserted or deleted.
  4224. ** ^Any callback set by a previous call to this function
  4225. ** for the same database connection is overridden.
  4226. **
  4227. ** ^The second argument is a pointer to the function to invoke when a
  4228. ** row is updated, inserted or deleted.
  4229. ** ^The first argument to the callback is a copy of the third argument
  4230. ** to sqlite3_update_hook().
  4231. ** ^The second callback argument is one of [SQLITE_INSERT], [SQLITE_DELETE],
  4232. ** or [SQLITE_UPDATE], depending on the operation that caused the callback
  4233. ** to be invoked.
  4234. ** ^The third and fourth arguments to the callback contain pointers to the
  4235. ** database and table name containing the affected row.
  4236. ** ^The final callback parameter is the [rowid] of the row.
  4237. ** ^In the case of an update, this is the [rowid] after the update takes place.
  4238. **
  4239. ** ^(The update hook is not invoked when internal system tables are
  4240. ** modified (i.e. sqlite_master and sqlite_sequence).)^
  4241. **
  4242. ** ^In the current implementation, the update hook
  4243. ** is not invoked when duplication rows are deleted because of an
  4244. ** [ON CONFLICT | ON CONFLICT REPLACE] clause. ^Nor is the update hook
  4245. ** invoked when rows are deleted using the [truncate optimization].
  4246. ** The exceptions defined in this paragraph might change in a future
  4247. ** release of SQLite.
  4248. **
  4249. ** The update hook implementation must not do anything that will modify
  4250. ** the database connection that invoked the update hook. Any actions
  4251. ** to modify the database connection must be deferred until after the
  4252. ** completion of the [sqlite3_step()] call that triggered the update hook.
  4253. ** Note that [sqlite3_prepare_v2()] and [sqlite3_step()] both modify their
  4254. ** database connections for the meaning of "modify" in this paragraph.
  4255. **
  4256. ** ^The sqlite3_update_hook(D,C,P) function
  4257. ** returns the P argument from the previous call
  4258. ** on the same [database connection] D, or NULL for
  4259. ** the first call on D.
  4260. **
  4261. ** See also the [sqlite3_commit_hook()] and [sqlite3_rollback_hook()]
  4262. ** interfaces.
  4263. */
  4264. SQLITE_API void *sqlite3_update_hook(
  4265. sqlite3*,
  4266. void(*)(void *,int ,char const *,char const *,sqlite3_int64),
  4267. void*
  4268. );
  4269. /*
  4270. ** CAPI3REF: Enable Or Disable Shared Pager Cache
  4271. ** KEYWORDS: {shared cache}
  4272. **
  4273. ** ^(This routine enables or disables the sharing of the database cache
  4274. ** and schema data structures between [database connection | connections]
  4275. ** to the same database. Sharing is enabled if the argument is true
  4276. ** and disabled if the argument is false.)^
  4277. **
  4278. ** ^Cache sharing is enabled and disabled for an entire process.
  4279. ** This is a change as of SQLite version 3.5.0. In prior versions of SQLite,
  4280. ** sharing was enabled or disabled for each thread separately.
  4281. **
  4282. ** ^(The cache sharing mode set by this interface effects all subsequent
  4283. ** calls to [sqlite3_open()], [sqlite3_open_v2()], and [sqlite3_open16()].
  4284. ** Existing database connections continue use the sharing mode
  4285. ** that was in effect at the time they were opened.)^
  4286. **
  4287. ** ^(This routine returns [SQLITE_OK] if shared cache was enabled or disabled
  4288. ** successfully. An [error code] is returned otherwise.)^
  4289. **
  4290. ** ^Shared cache is disabled by default. But this might change in
  4291. ** future releases of SQLite. Applications that care about shared
  4292. ** cache setting should set it explicitly.
  4293. **
  4294. ** See Also: [SQLite Shared-Cache Mode]
  4295. */
  4296. SQLITE_API int sqlite3_enable_shared_cache(int);
  4297. /*
  4298. ** CAPI3REF: Attempt To Free Heap Memory
  4299. **
  4300. ** ^The sqlite3_release_memory() interface attempts to free N bytes
  4301. ** of heap memory by deallocating non-essential memory allocations
  4302. ** held by the database library. Memory used to cache database
  4303. ** pages to improve performance is an example of non-essential memory.
  4304. ** ^sqlite3_release_memory() returns the number of bytes actually freed,
  4305. ** which might be more or less than the amount requested.
  4306. */
  4307. SQLITE_API int sqlite3_release_memory(int);
  4308. /*
  4309. ** CAPI3REF: Impose A Limit On Heap Size
  4310. **
  4311. ** ^The sqlite3_soft_heap_limit() interface places a "soft" limit
  4312. ** on the amount of heap memory that may be allocated by SQLite.
  4313. ** ^If an internal allocation is requested that would exceed the
  4314. ** soft heap limit, [sqlite3_release_memory()] is invoked one or
  4315. ** more times to free up some space before the allocation is performed.
  4316. **
  4317. ** ^The limit is called "soft" because if [sqlite3_release_memory()]
  4318. ** cannot free sufficient memory to prevent the limit from being exceeded,
  4319. ** the memory is allocated anyway and the current operation proceeds.
  4320. **
  4321. ** ^A negative or zero value for N means that there is no soft heap limit and
  4322. ** [sqlite3_release_memory()] will only be called when memory is exhausted.
  4323. ** ^The default value for the soft heap limit is zero.
  4324. **
  4325. ** ^(SQLite makes a best effort to honor the soft heap limit.
  4326. ** But if the soft heap limit cannot be honored, execution will
  4327. ** continue without error or notification.)^ This is why the limit is
  4328. ** called a "soft" limit. It is advisory only.
  4329. **
  4330. ** Prior to SQLite version 3.5.0, this routine only constrained the memory
  4331. ** allocated by a single thread - the same thread in which this routine
  4332. ** runs. Beginning with SQLite version 3.5.0, the soft heap limit is
  4333. ** applied to all threads. The value specified for the soft heap limit
  4334. ** is an upper bound on the total memory allocation for all threads. In
  4335. ** version 3.5.0 there is no mechanism for limiting the heap usage for
  4336. ** individual threads.
  4337. */
  4338. SQLITE_API void sqlite3_soft_heap_limit(int);
  4339. /*
  4340. ** CAPI3REF: Extract Metadata About A Column Of A Table
  4341. **
  4342. ** ^This routine returns metadata about a specific column of a specific
  4343. ** database table accessible using the [database connection] handle
  4344. ** passed as the first function argument.
  4345. **
  4346. ** ^The column is identified by the second, third and fourth parameters to
  4347. ** this function. ^The second parameter is either the name of the database
  4348. ** (i.e. "main", "temp", or an attached database) containing the specified
  4349. ** table or NULL. ^If it is NULL, then all attached databases are searched
  4350. ** for the table using the same algorithm used by the database engine to
  4351. ** resolve unqualified table references.
  4352. **
  4353. ** ^The third and fourth parameters to this function are the table and column
  4354. ** name of the desired column, respectively. Neither of these parameters
  4355. ** may be NULL.
  4356. **
  4357. ** ^Metadata is returned by writing to the memory locations passed as the 5th
  4358. ** and subsequent parameters to this function. ^Any of these arguments may be
  4359. ** NULL, in which case the corresponding element of metadata is omitted.
  4360. **
  4361. ** ^(<blockquote>
  4362. ** <table border="1">
  4363. ** <tr><th> Parameter <th> Output<br>Type <th> Description
  4364. **
  4365. ** <tr><td> 5th <td> const char* <td> Data type
  4366. ** <tr><td> 6th <td> const char* <td> Name of default collation sequence
  4367. ** <tr><td> 7th <td> int <td> True if column has a NOT NULL constraint
  4368. ** <tr><td> 8th <td> int <td> True if column is part of the PRIMARY KEY
  4369. ** <tr><td> 9th <td> int <td> True if column is [AUTOINCREMENT]
  4370. ** </table>
  4371. ** </blockquote>)^
  4372. **
  4373. ** ^The memory pointed to by the character pointers returned for the
  4374. ** declaration type and collation sequence is valid only until the next
  4375. ** call to any SQLite API function.
  4376. **
  4377. ** ^If the specified table is actually a view, an [error code] is returned.
  4378. **
  4379. ** ^If the specified column is "rowid", "oid" or "_rowid_" and an
  4380. ** [INTEGER PRIMARY KEY] column has been explicitly declared, then the output
  4381. ** parameters are set for the explicitly declared column. ^(If there is no
  4382. ** explicitly declared [INTEGER PRIMARY KEY] column, then the output
  4383. ** parameters are set as follows:
  4384. **
  4385. ** <pre>
  4386. ** data type: "INTEGER"
  4387. ** collation sequence: "BINARY"
  4388. ** not null: 0
  4389. ** primary key: 1
  4390. ** auto increment: 0
  4391. ** </pre>)^
  4392. **
  4393. ** ^(This function may load one or more schemas from database files. If an
  4394. ** error occurs during this process, or if the requested table or column
  4395. ** cannot be found, an [error code] is returned and an error message left
  4396. ** in the [database connection] (to be retrieved using sqlite3_errmsg()).)^
  4397. **
  4398. ** ^This API is only available if the library was compiled with the
  4399. ** [SQLITE_ENABLE_COLUMN_METADATA] C-preprocessor symbol defined.
  4400. */
  4401. SQLITE_API int sqlite3_table_column_metadata(
  4402. sqlite3 *db, /* Connection handle */
  4403. const char *zDbName, /* Database name or NULL */
  4404. const char *zTableName, /* Table name */
  4405. const char *zColumnName, /* Column name */
  4406. char const **pzDataType, /* OUTPUT: Declared data type */
  4407. char const **pzCollSeq, /* OUTPUT: Collation sequence name */
  4408. int *pNotNull, /* OUTPUT: True if NOT NULL constraint exists */
  4409. int *pPrimaryKey, /* OUTPUT: True if column part of PK */
  4410. int *pAutoinc /* OUTPUT: True if column is auto-increment */
  4411. );
  4412. /*
  4413. ** CAPI3REF: Load An Extension
  4414. **
  4415. ** ^This interface loads an SQLite extension library from the named file.
  4416. **
  4417. ** ^The sqlite3_load_extension() interface attempts to load an
  4418. ** SQLite extension library contained in the file zFile.
  4419. **
  4420. ** ^The entry point is zProc.
  4421. ** ^zProc may be 0, in which case the name of the entry point
  4422. ** defaults to "sqlite3_extension_init".
  4423. ** ^The sqlite3_load_extension() interface returns
  4424. ** [SQLITE_OK] on success and [SQLITE_ERROR] if something goes wrong.
  4425. ** ^If an error occurs and pzErrMsg is not 0, then the
  4426. ** [sqlite3_load_extension()] interface shall attempt to
  4427. ** fill *pzErrMsg with error message text stored in memory
  4428. ** obtained from [sqlite3_malloc()]. The calling function
  4429. ** should free this memory by calling [sqlite3_free()].
  4430. **
  4431. ** ^Extension loading must be enabled using
  4432. ** [sqlite3_enable_load_extension()] prior to calling this API,
  4433. ** otherwise an error will be returned.
  4434. **
  4435. ** See also the [load_extension() SQL function].
  4436. */
  4437. SQLITE_API int sqlite3_load_extension(
  4438. sqlite3 *db, /* Load the extension into this database connection */
  4439. const char *zFile, /* Name of the shared library containing extension */
  4440. const char *zProc, /* Entry point. Derived from zFile if 0 */
  4441. char **pzErrMsg /* Put error message here if not 0 */
  4442. );
  4443. /*
  4444. ** CAPI3REF: Enable Or Disable Extension Loading
  4445. **
  4446. ** ^So as not to open security holes in older applications that are
  4447. ** unprepared to deal with extension loading, and as a means of disabling
  4448. ** extension loading while evaluating user-entered SQL, the following API
  4449. ** is provided to turn the [sqlite3_load_extension()] mechanism on and off.
  4450. **
  4451. ** ^Extension loading is off by default. See ticket #1863.
  4452. ** ^Call the sqlite3_enable_load_extension() routine with onoff==1
  4453. ** to turn extension loading on and call it with onoff==0 to turn
  4454. ** it back off again.
  4455. */
  4456. SQLITE_API int sqlite3_enable_load_extension(sqlite3 *db, int onoff);
  4457. /*
  4458. ** CAPI3REF: Automatically Load An Extensions
  4459. **
  4460. ** ^This API can be invoked at program startup in order to register
  4461. ** one or more statically linked extensions that will be available
  4462. ** to all new [database connections].
  4463. **
  4464. ** ^(This routine stores a pointer to the extension entry point
  4465. ** in an array that is obtained from [sqlite3_malloc()]. That memory
  4466. ** is deallocated by [sqlite3_reset_auto_extension()].)^
  4467. **
  4468. ** ^This function registers an extension entry point that is
  4469. ** automatically invoked whenever a new [database connection]
  4470. ** is opened using [sqlite3_open()], [sqlite3_open16()],
  4471. ** or [sqlite3_open_v2()].
  4472. ** ^Duplicate extensions are detected so calling this routine
  4473. ** multiple times with the same extension is harmless.
  4474. ** ^Automatic extensions apply across all threads.
  4475. */
  4476. SQLITE_API int sqlite3_auto_extension(void (*xEntryPoint)(void));
  4477. /*
  4478. ** CAPI3REF: Reset Automatic Extension Loading
  4479. **
  4480. ** ^(This function disables all previously registered automatic
  4481. ** extensions. It undoes the effect of all prior
  4482. ** [sqlite3_auto_extension()] calls.)^
  4483. **
  4484. ** ^This function disables automatic extensions in all threads.
  4485. */
  4486. SQLITE_API void sqlite3_reset_auto_extension(void);
  4487. /*
  4488. ****** EXPERIMENTAL - subject to change without notice **************
  4489. **
  4490. ** The interface to the virtual-table mechanism is currently considered
  4491. ** to be experimental. The interface might change in incompatible ways.
  4492. ** If this is a problem for you, do not use the interface at this time.
  4493. **
  4494. ** When the virtual-table mechanism stabilizes, we will declare the
  4495. ** interface fixed, support it indefinitely, and remove this comment.
  4496. */
  4497. /*
  4498. ** Structures used by the virtual table interface
  4499. */
  4500. typedef struct sqlite3_vtab sqlite3_vtab;
  4501. typedef struct sqlite3_index_info sqlite3_index_info;
  4502. typedef struct sqlite3_vtab_cursor sqlite3_vtab_cursor;
  4503. typedef struct sqlite3_module sqlite3_module;
  4504. /*
  4505. ** CAPI3REF: Virtual Table Object
  4506. ** KEYWORDS: sqlite3_module {virtual table module}
  4507. ** EXPERIMENTAL
  4508. **
  4509. ** This structure, sometimes called a a "virtual table module",
  4510. ** defines the implementation of a [virtual tables].
  4511. ** This structure consists mostly of methods for the module.
  4512. **
  4513. ** ^A virtual table module is created by filling in a persistent
  4514. ** instance of this structure and passing a pointer to that instance
  4515. ** to [sqlite3_create_module()] or [sqlite3_create_module_v2()].
  4516. ** ^The registration remains valid until it is replaced by a different
  4517. ** module or until the [database connection] closes. The content
  4518. ** of this structure must not change while it is registered with
  4519. ** any database connection.
  4520. */
  4521. struct sqlite3_module {
  4522. int iVersion;
  4523. int (*xCreate)(sqlite3*, void *pAux,
  4524. int argc, const char *const*argv,
  4525. sqlite3_vtab **ppVTab, char**);
  4526. int (*xConnect)(sqlite3*, void *pAux,
  4527. int argc, const char *const*argv,
  4528. sqlite3_vtab **ppVTab, char**);
  4529. int (*xBestIndex)(sqlite3_vtab *pVTab, sqlite3_index_info*);
  4530. int (*xDisconnect)(sqlite3_vtab *pVTab);
  4531. int (*xDestroy)(sqlite3_vtab *pVTab);
  4532. int (*xOpen)(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCursor);
  4533. int (*xClose)(sqlite3_vtab_cursor*);
  4534. int (*xFilter)(sqlite3_vtab_cursor*, int idxNum, const char *idxStr,
  4535. int argc, sqlite3_value **argv);
  4536. int (*xNext)(sqlite3_vtab_cursor*);
  4537. int (*xEof)(sqlite3_vtab_cursor*);
  4538. int (*xColumn)(sqlite3_vtab_cursor*, sqlite3_context*, int);
  4539. int (*xRowid)(sqlite3_vtab_cursor*, sqlite3_int64 *pRowid);
  4540. int (*xUpdate)(sqlite3_vtab *, int, sqlite3_value **, sqlite3_int64 *);
  4541. int (*xBegin)(sqlite3_vtab *pVTab);
  4542. int (*xSync)(sqlite3_vtab *pVTab);
  4543. int (*xCommit)(sqlite3_vtab *pVTab);
  4544. int (*xRollback)(sqlite3_vtab *pVTab);
  4545. int (*xFindFunction)(sqlite3_vtab *pVtab, int nArg, const char *zName,
  4546. void (**pxFunc)(sqlite3_context*,int,sqlite3_value**),
  4547. void **ppArg);
  4548. int (*xRename)(sqlite3_vtab *pVtab, const char *zNew);
  4549. };
  4550. /*
  4551. ** CAPI3REF: Virtual Table Indexing Information
  4552. ** KEYWORDS: sqlite3_index_info
  4553. ** EXPERIMENTAL
  4554. **
  4555. ** The sqlite3_index_info structure and its substructures is used to
  4556. ** pass information into and receive the reply from the [xBestIndex]
  4557. ** method of a [virtual table module]. The fields under **Inputs** are the
  4558. ** inputs to xBestIndex and are read-only. xBestIndex inserts its
  4559. ** results into the **Outputs** fields.
  4560. **
  4561. ** ^(The aConstraint[] array records WHERE clause constraints of the form:
  4562. **
  4563. ** <pre>column OP expr</pre>
  4564. **
  4565. ** where OP is =, &lt;, &lt;=, &gt;, or &gt;=.)^ ^(The particular operator is
  4566. ** stored in aConstraint[].op.)^ ^(The index of the column is stored in
  4567. ** aConstraint[].iColumn.)^ ^(aConstraint[].usable is TRUE if the
  4568. ** expr on the right-hand side can be evaluated (and thus the constraint
  4569. ** is usable) and false if it cannot.)^
  4570. **
  4571. ** ^The optimizer automatically inverts terms of the form "expr OP column"
  4572. ** and makes other simplifications to the WHERE clause in an attempt to
  4573. ** get as many WHERE clause terms into the form shown above as possible.
  4574. ** ^The aConstraint[] array only reports WHERE clause terms that are
  4575. ** relevant to the particular virtual table being queried.
  4576. **
  4577. ** ^Information about the ORDER BY clause is stored in aOrderBy[].
  4578. ** ^Each term of aOrderBy records a column of the ORDER BY clause.
  4579. **
  4580. ** The [xBestIndex] method must fill aConstraintUsage[] with information
  4581. ** about what parameters to pass to xFilter. ^If argvIndex>0 then
  4582. ** the right-hand side of the corresponding aConstraint[] is evaluated
  4583. ** and becomes the argvIndex-th entry in argv. ^(If aConstraintUsage[].omit
  4584. ** is true, then the constraint is assumed to be fully handled by the
  4585. ** virtual table and is not checked again by SQLite.)^
  4586. **
  4587. ** ^The idxNum and idxPtr values are recorded and passed into the
  4588. ** [xFilter] method.
  4589. ** ^[sqlite3_free()] is used to free idxPtr if and only if
  4590. ** needToFreeIdxPtr is true.
  4591. **
  4592. ** ^The orderByConsumed means that output from [xFilter]/[xNext] will occur in
  4593. ** the correct order to satisfy the ORDER BY clause so that no separate
  4594. ** sorting step is required.
  4595. **
  4596. ** ^The estimatedCost value is an estimate of the cost of doing the
  4597. ** particular lookup. A full scan of a table with N entries should have
  4598. ** a cost of N. A binary search of a table of N entries should have a
  4599. ** cost of approximately log(N).
  4600. */
  4601. struct sqlite3_index_info {
  4602. /* Inputs */
  4603. int nConstraint; /* Number of entries in aConstraint */
  4604. struct sqlite3_index_constraint {
  4605. int iColumn; /* Column on left-hand side of constraint */
  4606. unsigned char op; /* Constraint operator */
  4607. unsigned char usable; /* True if this constraint is usable */
  4608. int iTermOffset; /* Used internally - xBestIndex should ignore */
  4609. } *aConstraint; /* Table of WHERE clause constraints */
  4610. int nOrderBy; /* Number of terms in the ORDER BY clause */
  4611. struct sqlite3_index_orderby {
  4612. int iColumn; /* Column number */
  4613. unsigned char desc; /* True for DESC. False for ASC. */
  4614. } *aOrderBy; /* The ORDER BY clause */
  4615. /* Outputs */
  4616. struct sqlite3_index_constraint_usage {
  4617. int argvIndex; /* if >0, constraint is part of argv to xFilter */
  4618. unsigned char omit; /* Do not code a test for this constraint */
  4619. } *aConstraintUsage;
  4620. int idxNum; /* Number used to identify the index */
  4621. char *idxStr; /* String, possibly obtained from sqlite3_malloc */
  4622. int needToFreeIdxStr; /* Free idxStr using sqlite3_free() if true */
  4623. int orderByConsumed; /* True if output is already ordered */
  4624. double estimatedCost; /* Estimated cost of using this index */
  4625. };
  4626. #define SQLITE_INDEX_CONSTRAINT_EQ 2
  4627. #define SQLITE_INDEX_CONSTRAINT_GT 4
  4628. #define SQLITE_INDEX_CONSTRAINT_LE 8
  4629. #define SQLITE_INDEX_CONSTRAINT_LT 16
  4630. #define SQLITE_INDEX_CONSTRAINT_GE 32
  4631. #define SQLITE_INDEX_CONSTRAINT_MATCH 64
  4632. /*
  4633. ** CAPI3REF: Register A Virtual Table Implementation
  4634. ** EXPERIMENTAL
  4635. **
  4636. ** ^These routines are used to register a new [virtual table module] name.
  4637. ** ^Module names must be registered before
  4638. ** creating a new [virtual table] using the module and before using a
  4639. ** preexisting [virtual table] for the module.
  4640. **
  4641. ** ^The module name is registered on the [database connection] specified
  4642. ** by the first parameter. ^The name of the module is given by the
  4643. ** second parameter. ^The third parameter is a pointer to
  4644. ** the implementation of the [virtual table module]. ^The fourth
  4645. ** parameter is an arbitrary client data pointer that is passed through
  4646. ** into the [xCreate] and [xConnect] methods of the virtual table module
  4647. ** when a new virtual table is be being created or reinitialized.
  4648. **
  4649. ** ^The sqlite3_create_module_v2() interface has a fifth parameter which
  4650. ** is a pointer to a destructor for the pClientData. ^SQLite will
  4651. ** invoke the destructor function (if it is not NULL) when SQLite
  4652. ** no longer needs the pClientData pointer. ^The sqlite3_create_module()
  4653. ** interface is equivalent to sqlite3_create_module_v2() with a NULL
  4654. ** destructor.
  4655. */
  4656. SQLITE_API SQLITE_EXPERIMENTAL int sqlite3_create_module(
  4657. sqlite3 *db, /* SQLite connection to register module with */
  4658. const char *zName, /* Name of the module */
  4659. const sqlite3_module *p, /* Methods for the module */
  4660. void *pClientData /* Client data for xCreate/xConnect */
  4661. );
  4662. SQLITE_API SQLITE_EXPERIMENTAL int sqlite3_create_module_v2(
  4663. sqlite3 *db, /* SQLite connection to register module with */
  4664. const char *zName, /* Name of the module */
  4665. const sqlite3_module *p, /* Methods for the module */
  4666. void *pClientData, /* Client data for xCreate/xConnect */
  4667. void(*xDestroy)(void*) /* Module destructor function */
  4668. );
  4669. /*
  4670. ** CAPI3REF: Virtual Table Instance Object
  4671. ** KEYWORDS: sqlite3_vtab
  4672. ** EXPERIMENTAL
  4673. **
  4674. ** Every [virtual table module] implementation uses a subclass
  4675. ** of this object to describe a particular instance
  4676. ** of the [virtual table]. Each subclass will
  4677. ** be tailored to the specific needs of the module implementation.
  4678. ** The purpose of this superclass is to define certain fields that are
  4679. ** common to all module implementations.
  4680. **
  4681. ** ^Virtual tables methods can set an error message by assigning a
  4682. ** string obtained from [sqlite3_mprintf()] to zErrMsg. The method should
  4683. ** take care that any prior string is freed by a call to [sqlite3_free()]
  4684. ** prior to assigning a new string to zErrMsg. ^After the error message
  4685. ** is delivered up to the client application, the string will be automatically
  4686. ** freed by sqlite3_free() and the zErrMsg field will be zeroed.
  4687. */
  4688. struct sqlite3_vtab {
  4689. const sqlite3_module *pModule; /* The module for this virtual table */
  4690. int nRef; /* NO LONGER USED */
  4691. char *zErrMsg; /* Error message from sqlite3_mprintf() */
  4692. /* Virtual table implementations will typically add additional fields */
  4693. };
  4694. /*
  4695. ** CAPI3REF: Virtual Table Cursor Object
  4696. ** KEYWORDS: sqlite3_vtab_cursor {virtual table cursor}
  4697. ** EXPERIMENTAL
  4698. **
  4699. ** Every [virtual table module] implementation uses a subclass of the
  4700. ** following structure to describe cursors that point into the
  4701. ** [virtual table] and are used
  4702. ** to loop through the virtual table. Cursors are created using the
  4703. ** [sqlite3_module.xOpen | xOpen] method of the module and are destroyed
  4704. ** by the [sqlite3_module.xClose | xClose] method. Cursors are used
  4705. ** by the [xFilter], [xNext], [xEof], [xColumn], and [xRowid] methods
  4706. ** of the module. Each module implementation will define
  4707. ** the content of a cursor structure to suit its own needs.
  4708. **
  4709. ** This superclass exists in order to define fields of the cursor that
  4710. ** are common to all implementations.
  4711. */
  4712. struct sqlite3_vtab_cursor {
  4713. sqlite3_vtab *pVtab; /* Virtual table of this cursor */
  4714. /* Virtual table implementations will typically add additional fields */
  4715. };
  4716. /*
  4717. ** CAPI3REF: Declare The Schema Of A Virtual Table
  4718. ** EXPERIMENTAL
  4719. **
  4720. ** ^The [xCreate] and [xConnect] methods of a
  4721. ** [virtual table module] call this interface
  4722. ** to declare the format (the names and datatypes of the columns) of
  4723. ** the virtual tables they implement.
  4724. */
  4725. SQLITE_API SQLITE_EXPERIMENTAL int sqlite3_declare_vtab(sqlite3*, const char *zSQL);
  4726. /*
  4727. ** CAPI3REF: Overload A Function For A Virtual Table
  4728. ** EXPERIMENTAL
  4729. **
  4730. ** ^(Virtual tables can provide alternative implementations of functions
  4731. ** using the [xFindFunction] method of the [virtual table module].
  4732. ** But global versions of those functions
  4733. ** must exist in order to be overloaded.)^
  4734. **
  4735. ** ^(This API makes sure a global version of a function with a particular
  4736. ** name and number of parameters exists. If no such function exists
  4737. ** before this API is called, a new function is created.)^ ^The implementation
  4738. ** of the new function always causes an exception to be thrown. So
  4739. ** the new function is not good for anything by itself. Its only
  4740. ** purpose is to be a placeholder function that can be overloaded
  4741. ** by a [virtual table].
  4742. */
  4743. SQLITE_API SQLITE_EXPERIMENTAL int sqlite3_overload_function(sqlite3*, const char *zFuncName, int nArg);
  4744. /*
  4745. ** The interface to the virtual-table mechanism defined above (back up
  4746. ** to a comment remarkably similar to this one) is currently considered
  4747. ** to be experimental. The interface might change in incompatible ways.
  4748. ** If this is a problem for you, do not use the interface at this time.
  4749. **
  4750. ** When the virtual-table mechanism stabilizes, we will declare the
  4751. ** interface fixed, support it indefinitely, and remove this comment.
  4752. **
  4753. ****** EXPERIMENTAL - subject to change without notice **************
  4754. */
  4755. /*
  4756. ** CAPI3REF: A Handle To An Open BLOB
  4757. ** KEYWORDS: {BLOB handle} {BLOB handles}
  4758. **
  4759. ** An instance of this object represents an open BLOB on which
  4760. ** [sqlite3_blob_open | incremental BLOB I/O] can be performed.
  4761. ** ^Objects of this type are created by [sqlite3_blob_open()]
  4762. ** and destroyed by [sqlite3_blob_close()].
  4763. ** ^The [sqlite3_blob_read()] and [sqlite3_blob_write()] interfaces
  4764. ** can be used to read or write small subsections of the BLOB.
  4765. ** ^The [sqlite3_blob_bytes()] interface returns the size of the BLOB in bytes.
  4766. */
  4767. typedef struct sqlite3_blob sqlite3_blob;
  4768. /*
  4769. ** CAPI3REF: Open A BLOB For Incremental I/O
  4770. **
  4771. ** ^(This interfaces opens a [BLOB handle | handle] to the BLOB located
  4772. ** in row iRow, column zColumn, table zTable in database zDb;
  4773. ** in other words, the same BLOB that would be selected by:
  4774. **
  4775. ** <pre>
  4776. ** SELECT zColumn FROM zDb.zTable WHERE [rowid] = iRow;
  4777. ** </pre>)^
  4778. **
  4779. ** ^If the flags parameter is non-zero, then the BLOB is opened for read
  4780. ** and write access. ^If it is zero, the BLOB is opened for read access.
  4781. ** ^It is not possible to open a column that is part of an index or primary
  4782. ** key for writing. ^If [foreign key constraints] are enabled, it is
  4783. ** not possible to open a column that is part of a [child key] for writing.
  4784. **
  4785. ** ^Note that the database name is not the filename that contains
  4786. ** the database but rather the symbolic name of the database that
  4787. ** appears after the AS keyword when the database is connected using [ATTACH].
  4788. ** ^For the main database file, the database name is "main".
  4789. ** ^For TEMP tables, the database name is "temp".
  4790. **
  4791. ** ^(On success, [SQLITE_OK] is returned and the new [BLOB handle] is written
  4792. ** to *ppBlob. Otherwise an [error code] is returned and *ppBlob is set
  4793. ** to be a null pointer.)^
  4794. ** ^This function sets the [database connection] error code and message
  4795. ** accessible via [sqlite3_errcode()] and [sqlite3_errmsg()] and related
  4796. ** functions. ^Note that the *ppBlob variable is always initialized in a
  4797. ** way that makes it safe to invoke [sqlite3_blob_close()] on *ppBlob
  4798. ** regardless of the success or failure of this routine.
  4799. **
  4800. ** ^(If the row that a BLOB handle points to is modified by an
  4801. ** [UPDATE], [DELETE], or by [ON CONFLICT] side-effects
  4802. ** then the BLOB handle is marked as "expired".
  4803. ** This is true if any column of the row is changed, even a column
  4804. ** other than the one the BLOB handle is open on.)^
  4805. ** ^Calls to [sqlite3_blob_read()] and [sqlite3_blob_write()] for
  4806. ** a expired BLOB handle fail with an return code of [SQLITE_ABORT].
  4807. ** ^(Changes written into a BLOB prior to the BLOB expiring are not
  4808. ** rolled back by the expiration of the BLOB. Such changes will eventually
  4809. ** commit if the transaction continues to completion.)^
  4810. **
  4811. ** ^Use the [sqlite3_blob_bytes()] interface to determine the size of
  4812. ** the opened blob. ^The size of a blob may not be changed by this
  4813. ** interface. Use the [UPDATE] SQL command to change the size of a
  4814. ** blob.
  4815. **
  4816. ** ^The [sqlite3_bind_zeroblob()] and [sqlite3_result_zeroblob()] interfaces
  4817. ** and the built-in [zeroblob] SQL function can be used, if desired,
  4818. ** to create an empty, zero-filled blob in which to read or write using
  4819. ** this interface.
  4820. **
  4821. ** To avoid a resource leak, every open [BLOB handle] should eventually
  4822. ** be released by a call to [sqlite3_blob_close()].
  4823. */
  4824. SQLITE_API int sqlite3_blob_open(
  4825. sqlite3*,
  4826. const char *zDb,
  4827. const char *zTable,
  4828. const char *zColumn,
  4829. sqlite3_int64 iRow,
  4830. int flags,
  4831. sqlite3_blob **ppBlob
  4832. );
  4833. /*
  4834. ** CAPI3REF: Close A BLOB Handle
  4835. **
  4836. ** ^Closes an open [BLOB handle].
  4837. **
  4838. ** ^Closing a BLOB shall cause the current transaction to commit
  4839. ** if there are no other BLOBs, no pending prepared statements, and the
  4840. ** database connection is in [autocommit mode].
  4841. ** ^If any writes were made to the BLOB, they might be held in cache
  4842. ** until the close operation if they will fit.
  4843. **
  4844. ** ^(Closing the BLOB often forces the changes
  4845. ** out to disk and so if any I/O errors occur, they will likely occur
  4846. ** at the time when the BLOB is closed. Any errors that occur during
  4847. ** closing are reported as a non-zero return value.)^
  4848. **
  4849. ** ^(The BLOB is closed unconditionally. Even if this routine returns
  4850. ** an error code, the BLOB is still closed.)^
  4851. **
  4852. ** ^Calling this routine with a null pointer (such as would be returned
  4853. ** by a failed call to [sqlite3_blob_open()]) is a harmless no-op.
  4854. */
  4855. SQLITE_API int sqlite3_blob_close(sqlite3_blob *);
  4856. /*
  4857. ** CAPI3REF: Return The Size Of An Open BLOB
  4858. **
  4859. ** ^Returns the size in bytes of the BLOB accessible via the
  4860. ** successfully opened [BLOB handle] in its only argument. ^The
  4861. ** incremental blob I/O routines can only read or overwriting existing
  4862. ** blob content; they cannot change the size of a blob.
  4863. **
  4864. ** This routine only works on a [BLOB handle] which has been created
  4865. ** by a prior successful call to [sqlite3_blob_open()] and which has not
  4866. ** been closed by [sqlite3_blob_close()]. Passing any other pointer in
  4867. ** to this routine results in undefined and probably undesirable behavior.
  4868. */
  4869. SQLITE_API int sqlite3_blob_bytes(sqlite3_blob *);
  4870. /*
  4871. ** CAPI3REF: Read Data From A BLOB Incrementally
  4872. **
  4873. ** ^(This function is used to read data from an open [BLOB handle] into a
  4874. ** caller-supplied buffer. N bytes of data are copied into buffer Z
  4875. ** from the open BLOB, starting at offset iOffset.)^
  4876. **
  4877. ** ^If offset iOffset is less than N bytes from the end of the BLOB,
  4878. ** [SQLITE_ERROR] is returned and no data is read. ^If N or iOffset is
  4879. ** less than zero, [SQLITE_ERROR] is returned and no data is read.
  4880. ** ^The size of the blob (and hence the maximum value of N+iOffset)
  4881. ** can be determined using the [sqlite3_blob_bytes()] interface.
  4882. **
  4883. ** ^An attempt to read from an expired [BLOB handle] fails with an
  4884. ** error code of [SQLITE_ABORT].
  4885. **
  4886. ** ^(On success, sqlite3_blob_read() returns SQLITE_OK.
  4887. ** Otherwise, an [error code] or an [extended error code] is returned.)^
  4888. **
  4889. ** This routine only works on a [BLOB handle] which has been created
  4890. ** by a prior successful call to [sqlite3_blob_open()] and which has not
  4891. ** been closed by [sqlite3_blob_close()]. Passing any other pointer in
  4892. ** to this routine results in undefined and probably undesirable behavior.
  4893. **
  4894. ** See also: [sqlite3_blob_write()].
  4895. */
  4896. SQLITE_API int sqlite3_blob_read(sqlite3_blob *, void *Z, int N, int iOffset);
  4897. /*
  4898. ** CAPI3REF: Write Data Into A BLOB Incrementally
  4899. **
  4900. ** ^This function is used to write data into an open [BLOB handle] from a
  4901. ** caller-supplied buffer. ^N bytes of data are copied from the buffer Z
  4902. ** into the open BLOB, starting at offset iOffset.
  4903. **
  4904. ** ^If the [BLOB handle] passed as the first argument was not opened for
  4905. ** writing (the flags parameter to [sqlite3_blob_open()] was zero),
  4906. ** this function returns [SQLITE_READONLY].
  4907. **
  4908. ** ^This function may only modify the contents of the BLOB; it is
  4909. ** not possible to increase the size of a BLOB using this API.
  4910. ** ^If offset iOffset is less than N bytes from the end of the BLOB,
  4911. ** [SQLITE_ERROR] is returned and no data is written. ^If N is
  4912. ** less than zero [SQLITE_ERROR] is returned and no data is written.
  4913. ** The size of the BLOB (and hence the maximum value of N+iOffset)
  4914. ** can be determined using the [sqlite3_blob_bytes()] interface.
  4915. **
  4916. ** ^An attempt to write to an expired [BLOB handle] fails with an
  4917. ** error code of [SQLITE_ABORT]. ^Writes to the BLOB that occurred
  4918. ** before the [BLOB handle] expired are not rolled back by the
  4919. ** expiration of the handle, though of course those changes might
  4920. ** have been overwritten by the statement that expired the BLOB handle
  4921. ** or by other independent statements.
  4922. **
  4923. ** ^(On success, sqlite3_blob_write() returns SQLITE_OK.
  4924. ** Otherwise, an [error code] or an [extended error code] is returned.)^
  4925. **
  4926. ** This routine only works on a [BLOB handle] which has been created
  4927. ** by a prior successful call to [sqlite3_blob_open()] and which has not
  4928. ** been closed by [sqlite3_blob_close()]. Passing any other pointer in
  4929. ** to this routine results in undefined and probably undesirable behavior.
  4930. **
  4931. ** See also: [sqlite3_blob_read()].
  4932. */
  4933. SQLITE_API int sqlite3_blob_write(sqlite3_blob *, const void *z, int n, int iOffset);
  4934. /*
  4935. ** CAPI3REF: Virtual File System Objects
  4936. **
  4937. ** A virtual filesystem (VFS) is an [sqlite3_vfs] object
  4938. ** that SQLite uses to interact
  4939. ** with the underlying operating system. Most SQLite builds come with a
  4940. ** single default VFS that is appropriate for the host computer.
  4941. ** New VFSes can be registered and existing VFSes can be unregistered.
  4942. ** The following interfaces are provided.
  4943. **
  4944. ** ^The sqlite3_vfs_find() interface returns a pointer to a VFS given its name.
  4945. ** ^Names are case sensitive.
  4946. ** ^Names are zero-terminated UTF-8 strings.
  4947. ** ^If there is no match, a NULL pointer is returned.
  4948. ** ^If zVfsName is NULL then the default VFS is returned.
  4949. **
  4950. ** ^New VFSes are registered with sqlite3_vfs_register().
  4951. ** ^Each new VFS becomes the default VFS if the makeDflt flag is set.
  4952. ** ^The same VFS can be registered multiple times without injury.
  4953. ** ^To make an existing VFS into the default VFS, register it again
  4954. ** with the makeDflt flag set. If two different VFSes with the
  4955. ** same name are registered, the behavior is undefined. If a
  4956. ** VFS is registered with a name that is NULL or an empty string,
  4957. ** then the behavior is undefined.
  4958. **
  4959. ** ^Unregister a VFS with the sqlite3_vfs_unregister() interface.
  4960. ** ^(If the default VFS is unregistered, another VFS is chosen as
  4961. ** the default. The choice for the new VFS is arbitrary.)^
  4962. */
  4963. SQLITE_API sqlite3_vfs *sqlite3_vfs_find(const char *zVfsName);
  4964. SQLITE_API int sqlite3_vfs_register(sqlite3_vfs*, int makeDflt);
  4965. SQLITE_API int sqlite3_vfs_unregister(sqlite3_vfs*);
  4966. /*
  4967. ** CAPI3REF: Mutexes
  4968. **
  4969. ** The SQLite core uses these routines for thread
  4970. ** synchronization. Though they are intended for internal
  4971. ** use by SQLite, code that links against SQLite is
  4972. ** permitted to use any of these routines.
  4973. **
  4974. ** The SQLite source code contains multiple implementations
  4975. ** of these mutex routines. An appropriate implementation
  4976. ** is selected automatically at compile-time. ^(The following
  4977. ** implementations are available in the SQLite core:
  4978. **
  4979. ** <ul>
  4980. ** <li> SQLITE_MUTEX_OS2
  4981. ** <li> SQLITE_MUTEX_PTHREAD
  4982. ** <li> SQLITE_MUTEX_W32
  4983. ** <li> SQLITE_MUTEX_NOOP
  4984. ** </ul>)^
  4985. **
  4986. ** ^The SQLITE_MUTEX_NOOP implementation is a set of routines
  4987. ** that does no real locking and is appropriate for use in
  4988. ** a single-threaded application. ^The SQLITE_MUTEX_OS2,
  4989. ** SQLITE_MUTEX_PTHREAD, and SQLITE_MUTEX_W32 implementations
  4990. ** are appropriate for use on OS/2, Unix, and Windows.
  4991. **
  4992. ** ^(If SQLite is compiled with the SQLITE_MUTEX_APPDEF preprocessor
  4993. ** macro defined (with "-DSQLITE_MUTEX_APPDEF=1"), then no mutex
  4994. ** implementation is included with the library. In this case the
  4995. ** application must supply a custom mutex implementation using the
  4996. ** [SQLITE_CONFIG_MUTEX] option of the sqlite3_config() function
  4997. ** before calling sqlite3_initialize() or any other public sqlite3_
  4998. ** function that calls sqlite3_initialize().)^
  4999. **
  5000. ** ^The sqlite3_mutex_alloc() routine allocates a new
  5001. ** mutex and returns a pointer to it. ^If it returns NULL
  5002. ** that means that a mutex could not be allocated. ^SQLite
  5003. ** will unwind its stack and return an error. ^(The argument
  5004. ** to sqlite3_mutex_alloc() is one of these integer constants:
  5005. **
  5006. ** <ul>
  5007. ** <li> SQLITE_MUTEX_FAST
  5008. ** <li> SQLITE_MUTEX_RECURSIVE
  5009. ** <li> SQLITE_MUTEX_STATIC_MASTER
  5010. ** <li> SQLITE_MUTEX_STATIC_MEM
  5011. ** <li> SQLITE_MUTEX_STATIC_MEM2
  5012. ** <li> SQLITE_MUTEX_STATIC_PRNG
  5013. ** <li> SQLITE_MUTEX_STATIC_LRU
  5014. ** <li> SQLITE_MUTEX_STATIC_LRU2
  5015. ** </ul>)^
  5016. **
  5017. ** ^The first two constants (SQLITE_MUTEX_FAST and SQLITE_MUTEX_RECURSIVE)
  5018. ** cause sqlite3_mutex_alloc() to create
  5019. ** a new mutex. ^The new mutex is recursive when SQLITE_MUTEX_RECURSIVE
  5020. ** is used but not necessarily so when SQLITE_MUTEX_FAST is used.
  5021. ** The mutex implementation does not need to make a distinction
  5022. ** between SQLITE_MUTEX_RECURSIVE and SQLITE_MUTEX_FAST if it does
  5023. ** not want to. ^SQLite will only request a recursive mutex in
  5024. ** cases where it really needs one. ^If a faster non-recursive mutex
  5025. ** implementation is available on the host platform, the mutex subsystem
  5026. ** might return such a mutex in response to SQLITE_MUTEX_FAST.
  5027. **
  5028. ** ^The other allowed parameters to sqlite3_mutex_alloc() (anything other
  5029. ** than SQLITE_MUTEX_FAST and SQLITE_MUTEX_RECURSIVE) each return
  5030. ** a pointer to a static preexisting mutex. ^Six static mutexes are
  5031. ** used by the current version of SQLite. Future versions of SQLite
  5032. ** may add additional static mutexes. Static mutexes are for internal
  5033. ** use by SQLite only. Applications that use SQLite mutexes should
  5034. ** use only the dynamic mutexes returned by SQLITE_MUTEX_FAST or
  5035. ** SQLITE_MUTEX_RECURSIVE.
  5036. **
  5037. ** ^Note that if one of the dynamic mutex parameters (SQLITE_MUTEX_FAST
  5038. ** or SQLITE_MUTEX_RECURSIVE) is used then sqlite3_mutex_alloc()
  5039. ** returns a different mutex on every call. ^But for the static
  5040. ** mutex types, the same mutex is returned on every call that has
  5041. ** the same type number.
  5042. **
  5043. ** ^The sqlite3_mutex_free() routine deallocates a previously
  5044. ** allocated dynamic mutex. ^SQLite is careful to deallocate every
  5045. ** dynamic mutex that it allocates. The dynamic mutexes must not be in
  5046. ** use when they are deallocated. Attempting to deallocate a static
  5047. ** mutex results in undefined behavior. ^SQLite never deallocates
  5048. ** a static mutex.
  5049. **
  5050. ** ^The sqlite3_mutex_enter() and sqlite3_mutex_try() routines attempt
  5051. ** to enter a mutex. ^If another thread is already within the mutex,
  5052. ** sqlite3_mutex_enter() will block and sqlite3_mutex_try() will return
  5053. ** SQLITE_BUSY. ^The sqlite3_mutex_try() interface returns [SQLITE_OK]
  5054. ** upon successful entry. ^(Mutexes created using
  5055. ** SQLITE_MUTEX_RECURSIVE can be entered multiple times by the same thread.
  5056. ** In such cases the,
  5057. ** mutex must be exited an equal number of times before another thread
  5058. ** can enter.)^ ^(If the same thread tries to enter any other
  5059. ** kind of mutex more than once, the behavior is undefined.
  5060. ** SQLite will never exhibit
  5061. ** such behavior in its own use of mutexes.)^
  5062. **
  5063. ** ^(Some systems (for example, Windows 95) do not support the operation
  5064. ** implemented by sqlite3_mutex_try(). On those systems, sqlite3_mutex_try()
  5065. ** will always return SQLITE_BUSY. The SQLite core only ever uses
  5066. ** sqlite3_mutex_try() as an optimization so this is acceptable behavior.)^
  5067. **
  5068. ** ^The sqlite3_mutex_leave() routine exits a mutex that was
  5069. ** previously entered by the same thread. ^(The behavior
  5070. ** is undefined if the mutex is not currently entered by the
  5071. ** calling thread or is not currently allocated. SQLite will
  5072. ** never do either.)^
  5073. **
  5074. ** ^If the argument to sqlite3_mutex_enter(), sqlite3_mutex_try(), or
  5075. ** sqlite3_mutex_leave() is a NULL pointer, then all three routines
  5076. ** behave as no-ops.
  5077. **
  5078. ** See also: [sqlite3_mutex_held()] and [sqlite3_mutex_notheld()].
  5079. */
  5080. SQLITE_API sqlite3_mutex *sqlite3_mutex_alloc(int);
  5081. SQLITE_API void sqlite3_mutex_free(sqlite3_mutex*);
  5082. SQLITE_API void sqlite3_mutex_enter(sqlite3_mutex*);
  5083. SQLITE_API int sqlite3_mutex_try(sqlite3_mutex*);
  5084. SQLITE_API void sqlite3_mutex_leave(sqlite3_mutex*);
  5085. /*
  5086. ** CAPI3REF: Mutex Methods Object
  5087. ** EXPERIMENTAL
  5088. **
  5089. ** An instance of this structure defines the low-level routines
  5090. ** used to allocate and use mutexes.
  5091. **
  5092. ** Usually, the default mutex implementations provided by SQLite are
  5093. ** sufficient, however the user has the option of substituting a custom
  5094. ** implementation for specialized deployments or systems for which SQLite
  5095. ** does not provide a suitable implementation. In this case, the user
  5096. ** creates and populates an instance of this structure to pass
  5097. ** to sqlite3_config() along with the [SQLITE_CONFIG_MUTEX] option.
  5098. ** Additionally, an instance of this structure can be used as an
  5099. ** output variable when querying the system for the current mutex
  5100. ** implementation, using the [SQLITE_CONFIG_GETMUTEX] option.
  5101. **
  5102. ** ^The xMutexInit method defined by this structure is invoked as
  5103. ** part of system initialization by the sqlite3_initialize() function.
  5104. ** ^The xMutexInit routine is calle by SQLite exactly once for each
  5105. ** effective call to [sqlite3_initialize()].
  5106. **
  5107. ** ^The xMutexEnd method defined by this structure is invoked as
  5108. ** part of system shutdown by the sqlite3_shutdown() function. The
  5109. ** implementation of this method is expected to release all outstanding
  5110. ** resources obtained by the mutex methods implementation, especially
  5111. ** those obtained by the xMutexInit method. ^The xMutexEnd()
  5112. ** interface is invoked exactly once for each call to [sqlite3_shutdown()].
  5113. **
  5114. ** ^(The remaining seven methods defined by this structure (xMutexAlloc,
  5115. ** xMutexFree, xMutexEnter, xMutexTry, xMutexLeave, xMutexHeld and
  5116. ** xMutexNotheld) implement the following interfaces (respectively):
  5117. **
  5118. ** <ul>
  5119. ** <li> [sqlite3_mutex_alloc()] </li>
  5120. ** <li> [sqlite3_mutex_free()] </li>
  5121. ** <li> [sqlite3_mutex_enter()] </li>
  5122. ** <li> [sqlite3_mutex_try()] </li>
  5123. ** <li> [sqlite3_mutex_leave()] </li>
  5124. ** <li> [sqlite3_mutex_held()] </li>
  5125. ** <li> [sqlite3_mutex_notheld()] </li>
  5126. ** </ul>)^
  5127. **
  5128. ** The only difference is that the public sqlite3_XXX functions enumerated
  5129. ** above silently ignore any invocations that pass a NULL pointer instead
  5130. ** of a valid mutex handle. The implementations of the methods defined
  5131. ** by this structure are not required to handle this case, the results
  5132. ** of passing a NULL pointer instead of a valid mutex handle are undefined
  5133. ** (i.e. it is acceptable to provide an implementation that segfaults if
  5134. ** it is passed a NULL pointer).
  5135. **
  5136. ** The xMutexInit() method must be threadsafe. ^It must be harmless to
  5137. ** invoke xMutexInit() mutiple times within the same process and without
  5138. ** intervening calls to xMutexEnd(). Second and subsequent calls to
  5139. ** xMutexInit() must be no-ops.
  5140. **
  5141. ** ^xMutexInit() must not use SQLite memory allocation ([sqlite3_malloc()]
  5142. ** and its associates). ^Similarly, xMutexAlloc() must not use SQLite memory
  5143. ** allocation for a static mutex. ^However xMutexAlloc() may use SQLite
  5144. ** memory allocation for a fast or recursive mutex.
  5145. **
  5146. ** ^SQLite will invoke the xMutexEnd() method when [sqlite3_shutdown()] is
  5147. ** called, but only if the prior call to xMutexInit returned SQLITE_OK.
  5148. ** If xMutexInit fails in any way, it is expected to clean up after itself
  5149. ** prior to returning.
  5150. */
  5151. typedef struct sqlite3_mutex_methods sqlite3_mutex_methods;
  5152. struct sqlite3_mutex_methods {
  5153. int (*xMutexInit)(void);
  5154. int (*xMutexEnd)(void);
  5155. sqlite3_mutex *(*xMutexAlloc)(int);
  5156. void (*xMutexFree)(sqlite3_mutex *);
  5157. void (*xMutexEnter)(sqlite3_mutex *);
  5158. int (*xMutexTry)(sqlite3_mutex *);
  5159. void (*xMutexLeave)(sqlite3_mutex *);
  5160. int (*xMutexHeld)(sqlite3_mutex *);
  5161. int (*xMutexNotheld)(sqlite3_mutex *);
  5162. };
  5163. /*
  5164. ** CAPI3REF: Mutex Verification Routines
  5165. **
  5166. ** The sqlite3_mutex_held() and sqlite3_mutex_notheld() routines
  5167. ** are intended for use inside assert() statements. ^The SQLite core
  5168. ** never uses these routines except inside an assert() and applications
  5169. ** are advised to follow the lead of the core. ^The SQLite core only
  5170. ** provides implementations for these routines when it is compiled
  5171. ** with the SQLITE_DEBUG flag. ^External mutex implementations
  5172. ** are only required to provide these routines if SQLITE_DEBUG is
  5173. ** defined and if NDEBUG is not defined.
  5174. **
  5175. ** ^These routines should return true if the mutex in their argument
  5176. ** is held or not held, respectively, by the calling thread.
  5177. **
  5178. ** ^The implementation is not required to provided versions of these
  5179. ** routines that actually work. If the implementation does not provide working
  5180. ** versions of these routines, it should at least provide stubs that always
  5181. ** return true so that one does not get spurious assertion failures.
  5182. **
  5183. ** ^If the argument to sqlite3_mutex_held() is a NULL pointer then
  5184. ** the routine should return 1. This seems counter-intuitive since
  5185. ** clearly the mutex cannot be held if it does not exist. But the
  5186. ** the reason the mutex does not exist is because the build is not
  5187. ** using mutexes. And we do not want the assert() containing the
  5188. ** call to sqlite3_mutex_held() to fail, so a non-zero return is
  5189. ** the appropriate thing to do. ^The sqlite3_mutex_notheld()
  5190. ** interface should also return 1 when given a NULL pointer.
  5191. */
  5192. #ifndef NDEBUG
  5193. SQLITE_API int sqlite3_mutex_held(sqlite3_mutex*);
  5194. SQLITE_API int sqlite3_mutex_notheld(sqlite3_mutex*);
  5195. #endif
  5196. /*
  5197. ** CAPI3REF: Mutex Types
  5198. **
  5199. ** The [sqlite3_mutex_alloc()] interface takes a single argument
  5200. ** which is one of these integer constants.
  5201. **
  5202. ** The set of static mutexes may change from one SQLite release to the
  5203. ** next. Applications that override the built-in mutex logic must be
  5204. ** prepared to accommodate additional static mutexes.
  5205. */
  5206. #define SQLITE_MUTEX_FAST 0
  5207. #define SQLITE_MUTEX_RECURSIVE 1
  5208. #define SQLITE_MUTEX_STATIC_MASTER 2
  5209. #define SQLITE_MUTEX_STATIC_MEM 3 /* sqlite3_malloc() */
  5210. #define SQLITE_MUTEX_STATIC_MEM2 4 /* NOT USED */
  5211. #define SQLITE_MUTEX_STATIC_OPEN 4 /* sqlite3BtreeOpen() */
  5212. #define SQLITE_MUTEX_STATIC_PRNG 5 /* sqlite3_random() */
  5213. #define SQLITE_MUTEX_STATIC_LRU 6 /* lru page list */
  5214. #define SQLITE_MUTEX_STATIC_LRU2 7 /* lru page list */
  5215. /*
  5216. ** CAPI3REF: Retrieve the mutex for a database connection
  5217. **
  5218. ** ^This interface returns a pointer the [sqlite3_mutex] object that
  5219. ** serializes access to the [database connection] given in the argument
  5220. ** when the [threading mode] is Serialized.
  5221. ** ^If the [threading mode] is Single-thread or Multi-thread then this
  5222. ** routine returns a NULL pointer.
  5223. */
  5224. SQLITE_API sqlite3_mutex *sqlite3_db_mutex(sqlite3*);
  5225. /*
  5226. ** CAPI3REF: Low-Level Control Of Database Files
  5227. **
  5228. ** ^The [sqlite3_file_control()] interface makes a direct call to the
  5229. ** xFileControl method for the [sqlite3_io_methods] object associated
  5230. ** with a particular database identified by the second argument. ^The
  5231. ** name of the database "main" for the main database or "temp" for the
  5232. ** TEMP database, or the name that appears after the AS keyword for
  5233. ** databases that are added using the [ATTACH] SQL command.
  5234. ** ^A NULL pointer can be used in place of "main" to refer to the
  5235. ** main database file.
  5236. ** ^The third and fourth parameters to this routine
  5237. ** are passed directly through to the second and third parameters of
  5238. ** the xFileControl method. ^The return value of the xFileControl
  5239. ** method becomes the return value of this routine.
  5240. **
  5241. ** ^If the second parameter (zDbName) does not match the name of any
  5242. ** open database file, then SQLITE_ERROR is returned. ^This error
  5243. ** code is not remembered and will not be recalled by [sqlite3_errcode()]
  5244. ** or [sqlite3_errmsg()]. The underlying xFileControl method might
  5245. ** also return SQLITE_ERROR. There is no way to distinguish between
  5246. ** an incorrect zDbName and an SQLITE_ERROR return from the underlying
  5247. ** xFileControl method.
  5248. **
  5249. ** See also: [SQLITE_FCNTL_LOCKSTATE]
  5250. */
  5251. SQLITE_API int sqlite3_file_control(sqlite3*, const char *zDbName, int op, void*);
  5252. /*
  5253. ** CAPI3REF: Testing Interface
  5254. **
  5255. ** ^The sqlite3_test_control() interface is used to read out internal
  5256. ** state of SQLite and to inject faults into SQLite for testing
  5257. ** purposes. ^The first parameter is an operation code that determines
  5258. ** the number, meaning, and operation of all subsequent parameters.
  5259. **
  5260. ** This interface is not for use by applications. It exists solely
  5261. ** for verifying the correct operation of the SQLite library. Depending
  5262. ** on how the SQLite library is compiled, this interface might not exist.
  5263. **
  5264. ** The details of the operation codes, their meanings, the parameters
  5265. ** they take, and what they do are all subject to change without notice.
  5266. ** Unlike most of the SQLite API, this function is not guaranteed to
  5267. ** operate consistently from one release to the next.
  5268. */
  5269. SQLITE_API int sqlite3_test_control(int op, ...);
  5270. /*
  5271. ** CAPI3REF: Testing Interface Operation Codes
  5272. **
  5273. ** These constants are the valid operation code parameters used
  5274. ** as the first argument to [sqlite3_test_control()].
  5275. **
  5276. ** These parameters and their meanings are subject to change
  5277. ** without notice. These values are for testing purposes only.
  5278. ** Applications should not use any of these parameters or the
  5279. ** [sqlite3_test_control()] interface.
  5280. */
  5281. #define SQLITE_TESTCTRL_FIRST 5
  5282. #define SQLITE_TESTCTRL_PRNG_SAVE 5
  5283. #define SQLITE_TESTCTRL_PRNG_RESTORE 6
  5284. #define SQLITE_TESTCTRL_PRNG_RESET 7
  5285. #define SQLITE_TESTCTRL_BITVEC_TEST 8
  5286. #define SQLITE_TESTCTRL_FAULT_INSTALL 9
  5287. #define SQLITE_TESTCTRL_BENIGN_MALLOC_HOOKS 10
  5288. #define SQLITE_TESTCTRL_PENDING_BYTE 11
  5289. #define SQLITE_TESTCTRL_ASSERT 12
  5290. #define SQLITE_TESTCTRL_ALWAYS 13
  5291. #define SQLITE_TESTCTRL_RESERVE 14
  5292. #define SQLITE_TESTCTRL_OPTIMIZATIONS 15
  5293. #define SQLITE_TESTCTRL_ISKEYWORD 16
  5294. #define SQLITE_TESTCTRL_LAST 16
  5295. /*
  5296. ** CAPI3REF: SQLite Runtime Status
  5297. ** EXPERIMENTAL
  5298. **
  5299. ** ^This interface is used to retrieve runtime status information
  5300. ** about the preformance of SQLite, and optionally to reset various
  5301. ** highwater marks. ^The first argument is an integer code for
  5302. ** the specific parameter to measure. ^(Recognized integer codes
  5303. ** are of the form [SQLITE_STATUS_MEMORY_USED | SQLITE_STATUS_...].)^
  5304. ** ^The current value of the parameter is returned into *pCurrent.
  5305. ** ^The highest recorded value is returned in *pHighwater. ^If the
  5306. ** resetFlag is true, then the highest record value is reset after
  5307. ** *pHighwater is written. ^(Some parameters do not record the highest
  5308. ** value. For those parameters
  5309. ** nothing is written into *pHighwater and the resetFlag is ignored.)^
  5310. ** ^(Other parameters record only the highwater mark and not the current
  5311. ** value. For these latter parameters nothing is written into *pCurrent.)^
  5312. **
  5313. ** ^The sqlite3_db_status() routine returns SQLITE_OK on success and a
  5314. ** non-zero [error code] on failure.
  5315. **
  5316. ** This routine is threadsafe but is not atomic. This routine can be
  5317. ** called while other threads are running the same or different SQLite
  5318. ** interfaces. However the values returned in *pCurrent and
  5319. ** *pHighwater reflect the status of SQLite at different points in time
  5320. ** and it is possible that another thread might change the parameter
  5321. ** in between the times when *pCurrent and *pHighwater are written.
  5322. **
  5323. ** See also: [sqlite3_db_status()]
  5324. */
  5325. SQLITE_API SQLITE_EXPERIMENTAL int sqlite3_status(int op, int *pCurrent, int *pHighwater, int resetFlag);
  5326. /*
  5327. ** CAPI3REF: Status Parameters
  5328. ** EXPERIMENTAL
  5329. **
  5330. ** These integer constants designate various run-time status parameters
  5331. ** that can be returned by [sqlite3_status()].
  5332. **
  5333. ** <dl>
  5334. ** ^(<dt>SQLITE_STATUS_MEMORY_USED</dt>
  5335. ** <dd>This parameter is the current amount of memory checked out
  5336. ** using [sqlite3_malloc()], either directly or indirectly. The
  5337. ** figure includes calls made to [sqlite3_malloc()] by the application
  5338. ** and internal memory usage by the SQLite library. Scratch memory
  5339. ** controlled by [SQLITE_CONFIG_SCRATCH] and auxiliary page-cache
  5340. ** memory controlled by [SQLITE_CONFIG_PAGECACHE] is not included in
  5341. ** this parameter. The amount returned is the sum of the allocation
  5342. ** sizes as reported by the xSize method in [sqlite3_mem_methods].</dd>)^
  5343. **
  5344. ** ^(<dt>SQLITE_STATUS_MALLOC_SIZE</dt>
  5345. ** <dd>This parameter records the largest memory allocation request
  5346. ** handed to [sqlite3_malloc()] or [sqlite3_realloc()] (or their
  5347. ** internal equivalents). Only the value returned in the
  5348. ** *pHighwater parameter to [sqlite3_status()] is of interest.
  5349. ** The value written into the *pCurrent parameter is undefined.</dd>)^
  5350. **
  5351. ** ^(<dt>SQLITE_STATUS_PAGECACHE_USED</dt>
  5352. ** <dd>This parameter returns the number of pages used out of the
  5353. ** [pagecache memory allocator] that was configured using
  5354. ** [SQLITE_CONFIG_PAGECACHE]. The
  5355. ** value returned is in pages, not in bytes.</dd>)^
  5356. **
  5357. ** ^(<dt>SQLITE_STATUS_PAGECACHE_OVERFLOW</dt>
  5358. ** <dd>This parameter returns the number of bytes of page cache
  5359. ** allocation which could not be statisfied by the [SQLITE_CONFIG_PAGECACHE]
  5360. ** buffer and where forced to overflow to [sqlite3_malloc()]. The
  5361. ** returned value includes allocations that overflowed because they
  5362. ** where too large (they were larger than the "sz" parameter to
  5363. ** [SQLITE_CONFIG_PAGECACHE]) and allocations that overflowed because
  5364. ** no space was left in the page cache.</dd>)^
  5365. **
  5366. ** ^(<dt>SQLITE_STATUS_PAGECACHE_SIZE</dt>
  5367. ** <dd>This parameter records the largest memory allocation request
  5368. ** handed to [pagecache memory allocator]. Only the value returned in the
  5369. ** *pHighwater parameter to [sqlite3_status()] is of interest.
  5370. ** The value written into the *pCurrent parameter is undefined.</dd>)^
  5371. **
  5372. ** ^(<dt>SQLITE_STATUS_SCRATCH_USED</dt>
  5373. ** <dd>This parameter returns the number of allocations used out of the
  5374. ** [scratch memory allocator] configured using
  5375. ** [SQLITE_CONFIG_SCRATCH]. The value returned is in allocations, not
  5376. ** in bytes. Since a single thread may only have one scratch allocation
  5377. ** outstanding at time, this parameter also reports the number of threads
  5378. ** using scratch memory at the same time.</dd>)^
  5379. **
  5380. ** ^(<dt>SQLITE_STATUS_SCRATCH_OVERFLOW</dt>
  5381. ** <dd>This parameter returns the number of bytes of scratch memory
  5382. ** allocation which could not be statisfied by the [SQLITE_CONFIG_SCRATCH]
  5383. ** buffer and where forced to overflow to [sqlite3_malloc()]. The values
  5384. ** returned include overflows because the requested allocation was too
  5385. ** larger (that is, because the requested allocation was larger than the
  5386. ** "sz" parameter to [SQLITE_CONFIG_SCRATCH]) and because no scratch buffer
  5387. ** slots were available.
  5388. ** </dd>)^
  5389. **
  5390. ** ^(<dt>SQLITE_STATUS_SCRATCH_SIZE</dt>
  5391. ** <dd>This parameter records the largest memory allocation request
  5392. ** handed to [scratch memory allocator]. Only the value returned in the
  5393. ** *pHighwater parameter to [sqlite3_status()] is of interest.
  5394. ** The value written into the *pCurrent parameter is undefined.</dd>)^
  5395. **
  5396. ** ^(<dt>SQLITE_STATUS_PARSER_STACK</dt>
  5397. ** <dd>This parameter records the deepest parser stack. It is only
  5398. ** meaningful if SQLite is compiled with [YYTRACKMAXSTACKDEPTH].</dd>)^
  5399. ** </dl>
  5400. **
  5401. ** New status parameters may be added from time to time.
  5402. */
  5403. #define SQLITE_STATUS_MEMORY_USED 0
  5404. #define SQLITE_STATUS_PAGECACHE_USED 1
  5405. #define SQLITE_STATUS_PAGECACHE_OVERFLOW 2
  5406. #define SQLITE_STATUS_SCRATCH_USED 3
  5407. #define SQLITE_STATUS_SCRATCH_OVERFLOW 4
  5408. #define SQLITE_STATUS_MALLOC_SIZE 5
  5409. #define SQLITE_STATUS_PARSER_STACK 6
  5410. #define SQLITE_STATUS_PAGECACHE_SIZE 7
  5411. #define SQLITE_STATUS_SCRATCH_SIZE 8
  5412. /*
  5413. ** CAPI3REF: Database Connection Status
  5414. ** EXPERIMENTAL
  5415. **
  5416. ** ^This interface is used to retrieve runtime status information
  5417. ** about a single [database connection]. ^The first argument is the
  5418. ** database connection object to be interrogated. ^The second argument
  5419. ** is the parameter to interrogate. ^Currently, the only allowed value
  5420. ** for the second parameter is [SQLITE_DBSTATUS_LOOKASIDE_USED].
  5421. ** Additional options will likely appear in future releases of SQLite.
  5422. **
  5423. ** ^The current value of the requested parameter is written into *pCur
  5424. ** and the highest instantaneous value is written into *pHiwtr. ^If
  5425. ** the resetFlg is true, then the highest instantaneous value is
  5426. ** reset back down to the current value.
  5427. **
  5428. ** See also: [sqlite3_status()] and [sqlite3_stmt_status()].
  5429. */
  5430. SQLITE_API SQLITE_EXPERIMENTAL int sqlite3_db_status(sqlite3*, int op, int *pCur, int *pHiwtr, int resetFlg);
  5431. /*
  5432. ** CAPI3REF: Status Parameters for database connections
  5433. ** EXPERIMENTAL
  5434. **
  5435. ** These constants are the available integer "verbs" that can be passed as
  5436. ** the second argument to the [sqlite3_db_status()] interface.
  5437. **
  5438. ** New verbs may be added in future releases of SQLite. Existing verbs
  5439. ** might be discontinued. Applications should check the return code from
  5440. ** [sqlite3_db_status()] to make sure that the call worked.
  5441. ** The [sqlite3_db_status()] interface will return a non-zero error code
  5442. ** if a discontinued or unsupported verb is invoked.
  5443. **
  5444. ** <dl>
  5445. ** ^(<dt>SQLITE_DBSTATUS_LOOKASIDE_USED</dt>
  5446. ** <dd>This parameter returns the number of lookaside memory slots currently
  5447. ** checked out.</dd>)^
  5448. ** </dl>
  5449. */
  5450. #define SQLITE_DBSTATUS_LOOKASIDE_USED 0
  5451. /*
  5452. ** CAPI3REF: Prepared Statement Status
  5453. ** EXPERIMENTAL
  5454. **
  5455. ** ^(Each prepared statement maintains various
  5456. ** [SQLITE_STMTSTATUS_SORT | counters] that measure the number
  5457. ** of times it has performed specific operations.)^ These counters can
  5458. ** be used to monitor the performance characteristics of the prepared
  5459. ** statements. For example, if the number of table steps greatly exceeds
  5460. ** the number of table searches or result rows, that would tend to indicate
  5461. ** that the prepared statement is using a full table scan rather than
  5462. ** an index.
  5463. **
  5464. ** ^(This interface is used to retrieve and reset counter values from
  5465. ** a [prepared statement]. The first argument is the prepared statement
  5466. ** object to be interrogated. The second argument
  5467. ** is an integer code for a specific [SQLITE_STMTSTATUS_SORT | counter]
  5468. ** to be interrogated.)^
  5469. ** ^The current value of the requested counter is returned.
  5470. ** ^If the resetFlg is true, then the counter is reset to zero after this
  5471. ** interface call returns.
  5472. **
  5473. ** See also: [sqlite3_status()] and [sqlite3_db_status()].
  5474. */
  5475. SQLITE_API SQLITE_EXPERIMENTAL int sqlite3_stmt_status(sqlite3_stmt*, int op,int resetFlg);
  5476. /*
  5477. ** CAPI3REF: Status Parameters for prepared statements
  5478. ** EXPERIMENTAL
  5479. **
  5480. ** These preprocessor macros define integer codes that name counter
  5481. ** values associated with the [sqlite3_stmt_status()] interface.
  5482. ** The meanings of the various counters are as follows:
  5483. **
  5484. ** <dl>
  5485. ** <dt>SQLITE_STMTSTATUS_FULLSCAN_STEP</dt>
  5486. ** <dd>^This is the number of times that SQLite has stepped forward in
  5487. ** a table as part of a full table scan. Large numbers for this counter
  5488. ** may indicate opportunities for performance improvement through
  5489. ** careful use of indices.</dd>
  5490. **
  5491. ** <dt>SQLITE_STMTSTATUS_SORT</dt>
  5492. ** <dd>^This is the number of sort operations that have occurred.
  5493. ** A non-zero value in this counter may indicate an opportunity to
  5494. ** improvement performance through careful use of indices.</dd>
  5495. **
  5496. ** </dl>
  5497. */
  5498. #define SQLITE_STMTSTATUS_FULLSCAN_STEP 1
  5499. #define SQLITE_STMTSTATUS_SORT 2
  5500. /*
  5501. ** CAPI3REF: Custom Page Cache Object
  5502. ** EXPERIMENTAL
  5503. **
  5504. ** The sqlite3_pcache type is opaque. It is implemented by
  5505. ** the pluggable module. The SQLite core has no knowledge of
  5506. ** its size or internal structure and never deals with the
  5507. ** sqlite3_pcache object except by holding and passing pointers
  5508. ** to the object.
  5509. **
  5510. ** See [sqlite3_pcache_methods] for additional information.
  5511. */
  5512. typedef struct sqlite3_pcache sqlite3_pcache;
  5513. /*
  5514. ** CAPI3REF: Application Defined Page Cache.
  5515. ** KEYWORDS: {page cache}
  5516. ** EXPERIMENTAL
  5517. **
  5518. ** ^(The [sqlite3_config]([SQLITE_CONFIG_PCACHE], ...) interface can
  5519. ** register an alternative page cache implementation by passing in an
  5520. ** instance of the sqlite3_pcache_methods structure.)^ The majority of the
  5521. ** heap memory used by SQLite is used by the page cache to cache data read
  5522. ** from, or ready to be written to, the database file. By implementing a
  5523. ** custom page cache using this API, an application can control more
  5524. ** precisely the amount of memory consumed by SQLite, the way in which
  5525. ** that memory is allocated and released, and the policies used to
  5526. ** determine exactly which parts of a database file are cached and for
  5527. ** how long.
  5528. **
  5529. ** ^(The contents of the sqlite3_pcache_methods structure are copied to an
  5530. ** internal buffer by SQLite within the call to [sqlite3_config]. Hence
  5531. ** the application may discard the parameter after the call to
  5532. ** [sqlite3_config()] returns.)^
  5533. **
  5534. ** ^The xInit() method is called once for each call to [sqlite3_initialize()]
  5535. ** (usually only once during the lifetime of the process). ^(The xInit()
  5536. ** method is passed a copy of the sqlite3_pcache_methods.pArg value.)^
  5537. ** ^The xInit() method can set up up global structures and/or any mutexes
  5538. ** required by the custom page cache implementation.
  5539. **
  5540. ** ^The xShutdown() method is called from within [sqlite3_shutdown()],
  5541. ** if the application invokes this API. It can be used to clean up
  5542. ** any outstanding resources before process shutdown, if required.
  5543. **
  5544. ** ^SQLite holds a [SQLITE_MUTEX_RECURSIVE] mutex when it invokes
  5545. ** the xInit method, so the xInit method need not be threadsafe. ^The
  5546. ** xShutdown method is only called from [sqlite3_shutdown()] so it does
  5547. ** not need to be threadsafe either. All other methods must be threadsafe
  5548. ** in multithreaded applications.
  5549. **
  5550. ** ^SQLite will never invoke xInit() more than once without an intervening
  5551. ** call to xShutdown().
  5552. **
  5553. ** ^The xCreate() method is used to construct a new cache instance. SQLite
  5554. ** will typically create one cache instance for each open database file,
  5555. ** though this is not guaranteed. ^The
  5556. ** first parameter, szPage, is the size in bytes of the pages that must
  5557. ** be allocated by the cache. ^szPage will not be a power of two. ^szPage
  5558. ** will the page size of the database file that is to be cached plus an
  5559. ** increment (here called "R") of about 100 or 200. ^SQLite will use the
  5560. ** extra R bytes on each page to store metadata about the underlying
  5561. ** database page on disk. The value of R depends
  5562. ** on the SQLite version, the target platform, and how SQLite was compiled.
  5563. ** ^R is constant for a particular build of SQLite. ^The second argument to
  5564. ** xCreate(), bPurgeable, is true if the cache being created will
  5565. ** be used to cache database pages of a file stored on disk, or
  5566. ** false if it is used for an in-memory database. ^The cache implementation
  5567. ** does not have to do anything special based with the value of bPurgeable;
  5568. ** it is purely advisory. ^On a cache where bPurgeable is false, SQLite will
  5569. ** never invoke xUnpin() except to deliberately delete a page.
  5570. ** ^In other words, a cache created with bPurgeable set to false will
  5571. ** never contain any unpinned pages.
  5572. **
  5573. ** ^(The xCachesize() method may be called at any time by SQLite to set the
  5574. ** suggested maximum cache-size (number of pages stored by) the cache
  5575. ** instance passed as the first argument. This is the value configured using
  5576. ** the SQLite "[PRAGMA cache_size]" command.)^ ^As with the bPurgeable
  5577. ** parameter, the implementation is not required to do anything with this
  5578. ** value; it is advisory only.
  5579. **
  5580. ** ^The xPagecount() method should return the number of pages currently
  5581. ** stored in the cache.
  5582. **
  5583. ** ^The xFetch() method is used to fetch a page and return a pointer to it.
  5584. ** ^A 'page', in this context, is a buffer of szPage bytes aligned at an
  5585. ** 8-byte boundary. ^The page to be fetched is determined by the key. ^The
  5586. ** mimimum key value is 1. After it has been retrieved using xFetch, the page
  5587. ** is considered to be "pinned".
  5588. **
  5589. ** ^If the requested page is already in the page cache, then the page cache
  5590. ** implementation must return a pointer to the page buffer with its content
  5591. ** intact. ^(If the requested page is not already in the cache, then the
  5592. ** behavior of the cache implementation is determined by the value of the
  5593. ** createFlag parameter passed to xFetch, according to the following table:
  5594. **
  5595. ** <table border=1 width=85% align=center>
  5596. ** <tr><th> createFlag <th> Behaviour when page is not already in cache
  5597. ** <tr><td> 0 <td> Do not allocate a new page. Return NULL.
  5598. ** <tr><td> 1 <td> Allocate a new page if it easy and convenient to do so.
  5599. ** Otherwise return NULL.
  5600. ** <tr><td> 2 <td> Make every effort to allocate a new page. Only return
  5601. ** NULL if allocating a new page is effectively impossible.
  5602. ** </table>)^
  5603. **
  5604. ** SQLite will normally invoke xFetch() with a createFlag of 0 or 1. If
  5605. ** a call to xFetch() with createFlag==1 returns NULL, then SQLite will
  5606. ** attempt to unpin one or more cache pages by spilling the content of
  5607. ** pinned pages to disk and synching the operating system disk cache. After
  5608. ** attempting to unpin pages, the xFetch() method will be invoked again with
  5609. ** a createFlag of 2.
  5610. **
  5611. ** ^xUnpin() is called by SQLite with a pointer to a currently pinned page
  5612. ** as its second argument. ^(If the third parameter, discard, is non-zero,
  5613. ** then the page should be evicted from the cache. In this case SQLite
  5614. ** assumes that the next time the page is retrieved from the cache using
  5615. ** the xFetch() method, it will be zeroed.)^ ^If the discard parameter is
  5616. ** zero, then the page is considered to be unpinned. ^The cache implementation
  5617. ** may choose to evict unpinned pages at any time.
  5618. **
  5619. ** ^(The cache is not required to perform any reference counting. A single
  5620. ** call to xUnpin() unpins the page regardless of the number of prior calls
  5621. ** to xFetch().)^
  5622. **
  5623. ** ^The xRekey() method is used to change the key value associated with the
  5624. ** page passed as the second argument from oldKey to newKey. ^If the cache
  5625. ** previously contains an entry associated with newKey, it should be
  5626. ** discarded. ^Any prior cache entry associated with newKey is guaranteed not
  5627. ** to be pinned.
  5628. **
  5629. ** ^When SQLite calls the xTruncate() method, the cache must discard all
  5630. ** existing cache entries with page numbers (keys) greater than or equal
  5631. ** to the value of the iLimit parameter passed to xTruncate(). ^If any
  5632. ** of these pages are pinned, they are implicitly unpinned, meaning that
  5633. ** they can be safely discarded.
  5634. **
  5635. ** ^The xDestroy() method is used to delete a cache allocated by xCreate().
  5636. ** All resources associated with the specified cache should be freed. ^After
  5637. ** calling the xDestroy() method, SQLite considers the [sqlite3_pcache*]
  5638. ** handle invalid, and will not use it with any other sqlite3_pcache_methods
  5639. ** functions.
  5640. */
  5641. typedef struct sqlite3_pcache_methods sqlite3_pcache_methods;
  5642. struct sqlite3_pcache_methods {
  5643. void *pArg;
  5644. int (*xInit)(void*);
  5645. void (*xShutdown)(void*);
  5646. sqlite3_pcache *(*xCreate)(int szPage, int bPurgeable);
  5647. void (*xCachesize)(sqlite3_pcache*, int nCachesize);
  5648. int (*xPagecount)(sqlite3_pcache*);
  5649. void *(*xFetch)(sqlite3_pcache*, unsigned key, int createFlag);
  5650. void (*xUnpin)(sqlite3_pcache*, void*, int discard);
  5651. void (*xRekey)(sqlite3_pcache*, void*, unsigned oldKey, unsigned newKey);
  5652. void (*xTruncate)(sqlite3_pcache*, unsigned iLimit);
  5653. void (*xDestroy)(sqlite3_pcache*);
  5654. };
  5655. /*
  5656. ** CAPI3REF: Online Backup Object
  5657. ** EXPERIMENTAL
  5658. **
  5659. ** The sqlite3_backup object records state information about an ongoing
  5660. ** online backup operation. ^The sqlite3_backup object is created by
  5661. ** a call to [sqlite3_backup_init()] and is destroyed by a call to
  5662. ** [sqlite3_backup_finish()].
  5663. **
  5664. ** See Also: [Using the SQLite Online Backup API]
  5665. */
  5666. typedef struct sqlite3_backup sqlite3_backup;
  5667. /*
  5668. ** CAPI3REF: Online Backup API.
  5669. ** EXPERIMENTAL
  5670. **
  5671. ** The backup API copies the content of one database into another.
  5672. ** It is useful either for creating backups of databases or
  5673. ** for copying in-memory databases to or from persistent files.
  5674. **
  5675. ** See Also: [Using the SQLite Online Backup API]
  5676. **
  5677. ** ^Exclusive access is required to the destination database for the
  5678. ** duration of the operation. ^However the source database is only
  5679. ** read-locked while it is actually being read; it is not locked
  5680. ** continuously for the entire backup operation. ^Thus, the backup may be
  5681. ** performed on a live source database without preventing other users from
  5682. ** reading or writing to the source database while the backup is underway.
  5683. **
  5684. ** ^(To perform a backup operation:
  5685. ** <ol>
  5686. ** <li><b>sqlite3_backup_init()</b> is called once to initialize the
  5687. ** backup,
  5688. ** <li><b>sqlite3_backup_step()</b> is called one or more times to transfer
  5689. ** the data between the two databases, and finally
  5690. ** <li><b>sqlite3_backup_finish()</b> is called to release all resources
  5691. ** associated with the backup operation.
  5692. ** </ol>)^
  5693. ** There should be exactly one call to sqlite3_backup_finish() for each
  5694. ** successful call to sqlite3_backup_init().
  5695. **
  5696. ** <b>sqlite3_backup_init()</b>
  5697. **
  5698. ** ^The D and N arguments to sqlite3_backup_init(D,N,S,M) are the
  5699. ** [database connection] associated with the destination database
  5700. ** and the database name, respectively.
  5701. ** ^The database name is "main" for the main database, "temp" for the
  5702. ** temporary database, or the name specified after the AS keyword in
  5703. ** an [ATTACH] statement for an attached database.
  5704. ** ^The S and M arguments passed to
  5705. ** sqlite3_backup_init(D,N,S,M) identify the [database connection]
  5706. ** and database name of the source database, respectively.
  5707. ** ^The source and destination [database connections] (parameters S and D)
  5708. ** must be different or else sqlite3_backup_init(D,N,S,M) will file with
  5709. ** an error.
  5710. **
  5711. ** ^If an error occurs within sqlite3_backup_init(D,N,S,M), then NULL is
  5712. ** returned and an error code and error message are store3d in the
  5713. ** destination [database connection] D.
  5714. ** ^The error code and message for the failed call to sqlite3_backup_init()
  5715. ** can be retrieved using the [sqlite3_errcode()], [sqlite3_errmsg()], and/or
  5716. ** [sqlite3_errmsg16()] functions.
  5717. ** ^A successful call to sqlite3_backup_init() returns a pointer to an
  5718. ** [sqlite3_backup] object.
  5719. ** ^The [sqlite3_backup] object may be used with the sqlite3_backup_step() and
  5720. ** sqlite3_backup_finish() functions to perform the specified backup
  5721. ** operation.
  5722. **
  5723. ** <b>sqlite3_backup_step()</b>
  5724. **
  5725. ** ^Function sqlite3_backup_step(B,N) will copy up to N pages between
  5726. ** the source and destination databases specified by [sqlite3_backup] object B.
  5727. ** ^If N is negative, all remaining source pages are copied.
  5728. ** ^If sqlite3_backup_step(B,N) successfully copies N pages and there
  5729. ** are still more pages to be copied, then the function resturns [SQLITE_OK].
  5730. ** ^If sqlite3_backup_step(B,N) successfully finishes copying all pages
  5731. ** from source to destination, then it returns [SQLITE_DONE].
  5732. ** ^If an error occurs while running sqlite3_backup_step(B,N),
  5733. ** then an [error code] is returned. ^As well as [SQLITE_OK] and
  5734. ** [SQLITE_DONE], a call to sqlite3_backup_step() may return [SQLITE_READONLY],
  5735. ** [SQLITE_NOMEM], [SQLITE_BUSY], [SQLITE_LOCKED], or an
  5736. ** [SQLITE_IOERR_ACCESS | SQLITE_IOERR_XXX] extended error code.
  5737. **
  5738. ** ^The sqlite3_backup_step() might return [SQLITE_READONLY] if the destination
  5739. ** database was opened read-only or if
  5740. ** the destination is an in-memory database with a different page size
  5741. ** from the source database.
  5742. **
  5743. ** ^If sqlite3_backup_step() cannot obtain a required file-system lock, then
  5744. ** the [sqlite3_busy_handler | busy-handler function]
  5745. ** is invoked (if one is specified). ^If the
  5746. ** busy-handler returns non-zero before the lock is available, then
  5747. ** [SQLITE_BUSY] is returned to the caller. ^In this case the call to
  5748. ** sqlite3_backup_step() can be retried later. ^If the source
  5749. ** [database connection]
  5750. ** is being used to write to the source database when sqlite3_backup_step()
  5751. ** is called, then [SQLITE_LOCKED] is returned immediately. ^Again, in this
  5752. ** case the call to sqlite3_backup_step() can be retried later on. ^(If
  5753. ** [SQLITE_IOERR_ACCESS | SQLITE_IOERR_XXX], [SQLITE_NOMEM], or
  5754. ** [SQLITE_READONLY] is returned, then
  5755. ** there is no point in retrying the call to sqlite3_backup_step(). These
  5756. ** errors are considered fatal.)^ The application must accept
  5757. ** that the backup operation has failed and pass the backup operation handle
  5758. ** to the sqlite3_backup_finish() to release associated resources.
  5759. **
  5760. ** ^The first call to sqlite3_backup_step() obtains an exclusive lock
  5761. ** on the destination file. ^The exclusive lock is not released until either
  5762. ** sqlite3_backup_finish() is called or the backup operation is complete
  5763. ** and sqlite3_backup_step() returns [SQLITE_DONE]. ^Every call to
  5764. ** sqlite3_backup_step() obtains a [shared lock] on the source database that
  5765. ** lasts for the duration of the sqlite3_backup_step() call.
  5766. ** ^Because the source database is not locked between calls to
  5767. ** sqlite3_backup_step(), the source database may be modified mid-way
  5768. ** through the backup process. ^If the source database is modified by an
  5769. ** external process or via a database connection other than the one being
  5770. ** used by the backup operation, then the backup will be automatically
  5771. ** restarted by the next call to sqlite3_backup_step(). ^If the source
  5772. ** database is modified by the using the same database connection as is used
  5773. ** by the backup operation, then the backup database is automatically
  5774. ** updated at the same time.
  5775. **
  5776. ** <b>sqlite3_backup_finish()</b>
  5777. **
  5778. ** When sqlite3_backup_step() has returned [SQLITE_DONE], or when the
  5779. ** application wishes to abandon the backup operation, the application
  5780. ** should destroy the [sqlite3_backup] by passing it to sqlite3_backup_finish().
  5781. ** ^The sqlite3_backup_finish() interfaces releases all
  5782. ** resources associated with the [sqlite3_backup] object.
  5783. ** ^If sqlite3_backup_step() has not yet returned [SQLITE_DONE], then any
  5784. ** active write-transaction on the destination database is rolled back.
  5785. ** The [sqlite3_backup] object is invalid
  5786. ** and may not be used following a call to sqlite3_backup_finish().
  5787. **
  5788. ** ^The value returned by sqlite3_backup_finish is [SQLITE_OK] if no
  5789. ** sqlite3_backup_step() errors occurred, regardless or whether or not
  5790. ** sqlite3_backup_step() completed.
  5791. ** ^If an out-of-memory condition or IO error occurred during any prior
  5792. ** sqlite3_backup_step() call on the same [sqlite3_backup] object, then
  5793. ** sqlite3_backup_finish() returns the corresponding [error code].
  5794. **
  5795. ** ^A return of [SQLITE_BUSY] or [SQLITE_LOCKED] from sqlite3_backup_step()
  5796. ** is not a permanent error and does not affect the return value of
  5797. ** sqlite3_backup_finish().
  5798. **
  5799. ** <b>sqlite3_backup_remaining(), sqlite3_backup_pagecount()</b>
  5800. **
  5801. ** ^Each call to sqlite3_backup_step() sets two values inside
  5802. ** the [sqlite3_backup] object: the number of pages still to be backed
  5803. ** up and the total number of pages in the source databae file.
  5804. ** The sqlite3_backup_remaining() and sqlite3_backup_pagecount() interfaces
  5805. ** retrieve these two values, respectively.
  5806. **
  5807. ** ^The values returned by these functions are only updated by
  5808. ** sqlite3_backup_step(). ^If the source database is modified during a backup
  5809. ** operation, then the values are not updated to account for any extra
  5810. ** pages that need to be updated or the size of the source database file
  5811. ** changing.
  5812. **
  5813. ** <b>Concurrent Usage of Database Handles</b>
  5814. **
  5815. ** ^The source [database connection] may be used by the application for other
  5816. ** purposes while a backup operation is underway or being initialized.
  5817. ** ^If SQLite is compiled and configured to support threadsafe database
  5818. ** connections, then the source database connection may be used concurrently
  5819. ** from within other threads.
  5820. **
  5821. ** However, the application must guarantee that the destination
  5822. ** [database connection] is not passed to any other API (by any thread) after
  5823. ** sqlite3_backup_init() is called and before the corresponding call to
  5824. ** sqlite3_backup_finish(). SQLite does not currently check to see
  5825. ** if the application incorrectly accesses the destination [database connection]
  5826. ** and so no error code is reported, but the operations may malfunction
  5827. ** nevertheless. Use of the destination database connection while a
  5828. ** backup is in progress might also also cause a mutex deadlock.
  5829. **
  5830. ** If running in [shared cache mode], the application must
  5831. ** guarantee that the shared cache used by the destination database
  5832. ** is not accessed while the backup is running. In practice this means
  5833. ** that the application must guarantee that the disk file being
  5834. ** backed up to is not accessed by any connection within the process,
  5835. ** not just the specific connection that was passed to sqlite3_backup_init().
  5836. **
  5837. ** The [sqlite3_backup] object itself is partially threadsafe. Multiple
  5838. ** threads may safely make multiple concurrent calls to sqlite3_backup_step().
  5839. ** However, the sqlite3_backup_remaining() and sqlite3_backup_pagecount()
  5840. ** APIs are not strictly speaking threadsafe. If they are invoked at the
  5841. ** same time as another thread is invoking sqlite3_backup_step() it is
  5842. ** possible that they return invalid values.
  5843. */
  5844. SQLITE_API sqlite3_backup *sqlite3_backup_init(
  5845. sqlite3 *pDest, /* Destination database handle */
  5846. const char *zDestName, /* Destination database name */
  5847. sqlite3 *pSource, /* Source database handle */
  5848. const char *zSourceName /* Source database name */
  5849. );
  5850. SQLITE_API int sqlite3_backup_step(sqlite3_backup *p, int nPage);
  5851. SQLITE_API int sqlite3_backup_finish(sqlite3_backup *p);
  5852. SQLITE_API int sqlite3_backup_remaining(sqlite3_backup *p);
  5853. SQLITE_API int sqlite3_backup_pagecount(sqlite3_backup *p);
  5854. /*
  5855. ** CAPI3REF: Unlock Notification
  5856. ** EXPERIMENTAL
  5857. **
  5858. ** ^When running in shared-cache mode, a database operation may fail with
  5859. ** an [SQLITE_LOCKED] error if the required locks on the shared-cache or
  5860. ** individual tables within the shared-cache cannot be obtained. See
  5861. ** [SQLite Shared-Cache Mode] for a description of shared-cache locking.
  5862. ** ^This API may be used to register a callback that SQLite will invoke
  5863. ** when the connection currently holding the required lock relinquishes it.
  5864. ** ^This API is only available if the library was compiled with the
  5865. ** [SQLITE_ENABLE_UNLOCK_NOTIFY] C-preprocessor symbol defined.
  5866. **
  5867. ** See Also: [Using the SQLite Unlock Notification Feature].
  5868. **
  5869. ** ^Shared-cache locks are released when a database connection concludes
  5870. ** its current transaction, either by committing it or rolling it back.
  5871. **
  5872. ** ^When a connection (known as the blocked connection) fails to obtain a
  5873. ** shared-cache lock and SQLITE_LOCKED is returned to the caller, the
  5874. ** identity of the database connection (the blocking connection) that
  5875. ** has locked the required resource is stored internally. ^After an
  5876. ** application receives an SQLITE_LOCKED error, it may call the
  5877. ** sqlite3_unlock_notify() method with the blocked connection handle as
  5878. ** the first argument to register for a callback that will be invoked
  5879. ** when the blocking connections current transaction is concluded. ^The
  5880. ** callback is invoked from within the [sqlite3_step] or [sqlite3_close]
  5881. ** call that concludes the blocking connections transaction.
  5882. **
  5883. ** ^(If sqlite3_unlock_notify() is called in a multi-threaded application,
  5884. ** there is a chance that the blocking connection will have already
  5885. ** concluded its transaction by the time sqlite3_unlock_notify() is invoked.
  5886. ** If this happens, then the specified callback is invoked immediately,
  5887. ** from within the call to sqlite3_unlock_notify().)^
  5888. **
  5889. ** ^If the blocked connection is attempting to obtain a write-lock on a
  5890. ** shared-cache table, and more than one other connection currently holds
  5891. ** a read-lock on the same table, then SQLite arbitrarily selects one of
  5892. ** the other connections to use as the blocking connection.
  5893. **
  5894. ** ^(There may be at most one unlock-notify callback registered by a
  5895. ** blocked connection. If sqlite3_unlock_notify() is called when the
  5896. ** blocked connection already has a registered unlock-notify callback,
  5897. ** then the new callback replaces the old.)^ ^If sqlite3_unlock_notify() is
  5898. ** called with a NULL pointer as its second argument, then any existing
  5899. ** unlock-notify callback is cancelled. ^The blocked connections
  5900. ** unlock-notify callback may also be canceled by closing the blocked
  5901. ** connection using [sqlite3_close()].
  5902. **
  5903. ** The unlock-notify callback is not reentrant. If an application invokes
  5904. ** any sqlite3_xxx API functions from within an unlock-notify callback, a
  5905. ** crash or deadlock may be the result.
  5906. **
  5907. ** ^Unless deadlock is detected (see below), sqlite3_unlock_notify() always
  5908. ** returns SQLITE_OK.
  5909. **
  5910. ** <b>Callback Invocation Details</b>
  5911. **
  5912. ** When an unlock-notify callback is registered, the application provides a
  5913. ** single void* pointer that is passed to the callback when it is invoked.
  5914. ** However, the signature of the callback function allows SQLite to pass
  5915. ** it an array of void* context pointers. The first argument passed to
  5916. ** an unlock-notify callback is a pointer to an array of void* pointers,
  5917. ** and the second is the number of entries in the array.
  5918. **
  5919. ** When a blocking connections transaction is concluded, there may be
  5920. ** more than one blocked connection that has registered for an unlock-notify
  5921. ** callback. ^If two or more such blocked connections have specified the
  5922. ** same callback function, then instead of invoking the callback function
  5923. ** multiple times, it is invoked once with the set of void* context pointers
  5924. ** specified by the blocked connections bundled together into an array.
  5925. ** This gives the application an opportunity to prioritize any actions
  5926. ** related to the set of unblocked database connections.
  5927. **
  5928. ** <b>Deadlock Detection</b>
  5929. **
  5930. ** Assuming that after registering for an unlock-notify callback a
  5931. ** database waits for the callback to be issued before taking any further
  5932. ** action (a reasonable assumption), then using this API may cause the
  5933. ** application to deadlock. For example, if connection X is waiting for
  5934. ** connection Y's transaction to be concluded, and similarly connection
  5935. ** Y is waiting on connection X's transaction, then neither connection
  5936. ** will proceed and the system may remain deadlocked indefinitely.
  5937. **
  5938. ** To avoid this scenario, the sqlite3_unlock_notify() performs deadlock
  5939. ** detection. ^If a given call to sqlite3_unlock_notify() would put the
  5940. ** system in a deadlocked state, then SQLITE_LOCKED is returned and no
  5941. ** unlock-notify callback is registered. The system is said to be in
  5942. ** a deadlocked state if connection A has registered for an unlock-notify
  5943. ** callback on the conclusion of connection B's transaction, and connection
  5944. ** B has itself registered for an unlock-notify callback when connection
  5945. ** A's transaction is concluded. ^Indirect deadlock is also detected, so
  5946. ** the system is also considered to be deadlocked if connection B has
  5947. ** registered for an unlock-notify callback on the conclusion of connection
  5948. ** C's transaction, where connection C is waiting on connection A. ^Any
  5949. ** number of levels of indirection are allowed.
  5950. **
  5951. ** <b>The "DROP TABLE" Exception</b>
  5952. **
  5953. ** When a call to [sqlite3_step()] returns SQLITE_LOCKED, it is almost
  5954. ** always appropriate to call sqlite3_unlock_notify(). There is however,
  5955. ** one exception. When executing a "DROP TABLE" or "DROP INDEX" statement,
  5956. ** SQLite checks if there are any currently executing SELECT statements
  5957. ** that belong to the same connection. If there are, SQLITE_LOCKED is
  5958. ** returned. In this case there is no "blocking connection", so invoking
  5959. ** sqlite3_unlock_notify() results in the unlock-notify callback being
  5960. ** invoked immediately. If the application then re-attempts the "DROP TABLE"
  5961. ** or "DROP INDEX" query, an infinite loop might be the result.
  5962. **
  5963. ** One way around this problem is to check the extended error code returned
  5964. ** by an sqlite3_step() call. ^(If there is a blocking connection, then the
  5965. ** extended error code is set to SQLITE_LOCKED_SHAREDCACHE. Otherwise, in
  5966. ** the special "DROP TABLE/INDEX" case, the extended error code is just
  5967. ** SQLITE_LOCKED.)^
  5968. */
  5969. SQLITE_API int sqlite3_unlock_notify(
  5970. sqlite3 *pBlocked, /* Waiting connection */
  5971. void (*xNotify)(void **apArg, int nArg), /* Callback function to invoke */
  5972. void *pNotifyArg /* Argument to pass to xNotify */
  5973. );
  5974. /*
  5975. ** CAPI3REF: String Comparison
  5976. ** EXPERIMENTAL
  5977. **
  5978. ** ^The [sqlite3_strnicmp()] API allows applications and extensions to
  5979. ** compare the contents of two buffers containing UTF-8 strings in a
  5980. ** case-indendent fashion, using the same definition of case independence
  5981. ** that SQLite uses internally when comparing identifiers.
  5982. */
  5983. SQLITE_API int sqlite3_strnicmp(const char *, const char *, int);
  5984. /*
  5985. ** Undo the hack that converts floating point types to integer for
  5986. ** builds on processors without floating point support.
  5987. */
  5988. #ifdef SQLITE_OMIT_FLOATING_POINT
  5989. # undef double
  5990. #endif
  5991. #if 0
  5992. } /* End of the 'extern "C"' block */
  5993. #endif
  5994. #endif
  5995. /************** End of sqlite3.h *********************************************/
  5996. /************** Continuing where we left off in sqliteInt.h ******************/
  5997. /************** Include hash.h in the middle of sqliteInt.h ******************/
  5998. /************** Begin file hash.h ********************************************/
  5999. /*
  6000. ** 2001 September 22
  6001. **
  6002. ** The author disclaims copyright to this source code. In place of
  6003. ** a legal notice, here is a blessing:
  6004. **
  6005. ** May you do good and not evil.
  6006. ** May you find forgiveness for yourself and forgive others.
  6007. ** May you share freely, never taking more than you give.
  6008. **
  6009. *************************************************************************
  6010. ** This is the header file for the generic hash-table implemenation
  6011. ** used in SQLite.
  6012. */
  6013. #ifndef _SQLITE_HASH_H_
  6014. #define _SQLITE_HASH_H_
  6015. /* Forward declarations of structures. */
  6016. typedef struct Hash Hash;
  6017. typedef struct HashElem HashElem;
  6018. /* A complete hash table is an instance of the following structure.
  6019. ** The internals of this structure are intended to be opaque -- client
  6020. ** code should not attempt to access or modify the fields of this structure
  6021. ** directly. Change this structure only by using the routines below.
  6022. ** However, some of the "procedures" and "functions" for modifying and
  6023. ** accessing this structure are really macros, so we can't really make
  6024. ** this structure opaque.
  6025. **
  6026. ** All elements of the hash table are on a single doubly-linked list.
  6027. ** Hash.first points to the head of this list.
  6028. **
  6029. ** There are Hash.htsize buckets. Each bucket points to a spot in
  6030. ** the global doubly-linked list. The contents of the bucket are the
  6031. ** element pointed to plus the next _ht.count-1 elements in the list.
  6032. **
  6033. ** Hash.htsize and Hash.ht may be zero. In that case lookup is done
  6034. ** by a linear search of the global list. For small tables, the
  6035. ** Hash.ht table is never allocated because if there are few elements
  6036. ** in the table, it is faster to do a linear search than to manage
  6037. ** the hash table.
  6038. */
  6039. struct Hash {
  6040. unsigned int htsize; /* Number of buckets in the hash table */
  6041. unsigned int count; /* Number of entries in this table */
  6042. HashElem *first; /* The first element of the array */
  6043. struct _ht { /* the hash table */
  6044. int count; /* Number of entries with this hash */
  6045. HashElem *chain; /* Pointer to first entry with this hash */
  6046. } *ht;
  6047. };
  6048. /* Each element in the hash table is an instance of the following
  6049. ** structure. All elements are stored on a single doubly-linked list.
  6050. **
  6051. ** Again, this structure is intended to be opaque, but it can't really
  6052. ** be opaque because it is used by macros.
  6053. */
  6054. struct HashElem {
  6055. HashElem *next, *prev; /* Next and previous elements in the table */
  6056. void *data; /* Data associated with this element */
  6057. const char *pKey; int nKey; /* Key associated with this element */
  6058. };
  6059. /*
  6060. ** Access routines. To delete, insert a NULL pointer.
  6061. */
  6062. SQLITE_PRIVATE void sqlite3HashInit(Hash*);
  6063. SQLITE_PRIVATE void *sqlite3HashInsert(Hash*, const char *pKey, int nKey, void *pData);
  6064. SQLITE_PRIVATE void *sqlite3HashFind(const Hash*, const char *pKey, int nKey);
  6065. SQLITE_PRIVATE void sqlite3HashClear(Hash*);
  6066. /*
  6067. ** Macros for looping over all elements of a hash table. The idiom is
  6068. ** like this:
  6069. **
  6070. ** Hash h;
  6071. ** HashElem *p;
  6072. ** ...
  6073. ** for(p=sqliteHashFirst(&h); p; p=sqliteHashNext(p)){
  6074. ** SomeStructure *pData = sqliteHashData(p);
  6075. ** // do something with pData
  6076. ** }
  6077. */
  6078. #define sqliteHashFirst(H) ((H)->first)
  6079. #define sqliteHashNext(E) ((E)->next)
  6080. #define sqliteHashData(E) ((E)->data)
  6081. /* #define sqliteHashKey(E) ((E)->pKey) // NOT USED */
  6082. /* #define sqliteHashKeysize(E) ((E)->nKey) // NOT USED */
  6083. /*
  6084. ** Number of entries in a hash table
  6085. */
  6086. /* #define sqliteHashCount(H) ((H)->count) // NOT USED */
  6087. #endif /* _SQLITE_HASH_H_ */
  6088. /************** End of hash.h ************************************************/
  6089. /************** Continuing where we left off in sqliteInt.h ******************/
  6090. /************** Include parse.h in the middle of sqliteInt.h *****************/
  6091. /************** Begin file parse.h *******************************************/
  6092. #define TK_SEMI 1
  6093. #define TK_EXPLAIN 2
  6094. #define TK_QUERY 3
  6095. #define TK_PLAN 4
  6096. #define TK_BEGIN 5
  6097. #define TK_TRANSACTION 6
  6098. #define TK_DEFERRED 7
  6099. #define TK_IMMEDIATE 8
  6100. #define TK_EXCLUSIVE 9
  6101. #define TK_COMMIT 10
  6102. #define TK_END 11
  6103. #define TK_ROLLBACK 12
  6104. #define TK_SAVEPOINT 13
  6105. #define TK_RELEASE 14
  6106. #define TK_TO 15
  6107. #define TK_TABLE 16
  6108. #define TK_CREATE 17
  6109. #define TK_IF 18
  6110. #define TK_NOT 19
  6111. #define TK_EXISTS 20
  6112. #define TK_TEMP 21
  6113. #define TK_LP 22
  6114. #define TK_RP 23
  6115. #define TK_AS 24
  6116. #define TK_COMMA 25
  6117. #define TK_ID 26
  6118. #define TK_INDEXED 27
  6119. #define TK_ABORT 28
  6120. #define TK_ACTION 29
  6121. #define TK_AFTER 30
  6122. #define TK_ANALYZE 31
  6123. #define TK_ASC 32
  6124. #define TK_ATTACH 33
  6125. #define TK_BEFORE 34
  6126. #define TK_BY 35
  6127. #define TK_CASCADE 36
  6128. #define TK_CAST 37
  6129. #define TK_COLUMNKW 38
  6130. #define TK_CONFLICT 39
  6131. #define TK_DATABASE 40
  6132. #define TK_DESC 41
  6133. #define TK_DETACH 42
  6134. #define TK_EACH 43
  6135. #define TK_FAIL 44
  6136. #define TK_FOR 45
  6137. #define TK_IGNORE 46
  6138. #define TK_INITIALLY 47
  6139. #define TK_INSTEAD 48
  6140. #define TK_LIKE_KW 49
  6141. #define TK_MATCH 50
  6142. #define TK_NO 51
  6143. #define TK_KEY 52
  6144. #define TK_OF 53
  6145. #define TK_OFFSET 54
  6146. #define TK_PRAGMA 55
  6147. #define TK_RAISE 56
  6148. #define TK_REPLACE 57
  6149. #define TK_RESTRICT 58
  6150. #define TK_ROW 59
  6151. #define TK_TRIGGER 60
  6152. #define TK_VACUUM 61
  6153. #define TK_VIEW 62
  6154. #define TK_VIRTUAL 63
  6155. #define TK_REINDEX 64
  6156. #define TK_RENAME 65
  6157. #define TK_CTIME_KW 66
  6158. #define TK_ANY 67
  6159. #define TK_OR 68
  6160. #define TK_AND 69
  6161. #define TK_IS 70
  6162. #define TK_BETWEEN 71
  6163. #define TK_IN 72
  6164. #define TK_ISNULL 73
  6165. #define TK_NOTNULL 74
  6166. #define TK_NE 75
  6167. #define TK_EQ 76
  6168. #define TK_GT 77
  6169. #define TK_LE 78
  6170. #define TK_LT 79
  6171. #define TK_GE 80
  6172. #define TK_ESCAPE 81
  6173. #define TK_BITAND 82
  6174. #define TK_BITOR 83
  6175. #define TK_LSHIFT 84
  6176. #define TK_RSHIFT 85
  6177. #define TK_PLUS 86
  6178. #define TK_MINUS 87
  6179. #define TK_STAR 88
  6180. #define TK_SLASH 89
  6181. #define TK_REM 90
  6182. #define TK_CONCAT 91
  6183. #define TK_COLLATE 92
  6184. #define TK_BITNOT 93
  6185. #define TK_STRING 94
  6186. #define TK_JOIN_KW 95
  6187. #define TK_CONSTRAINT 96
  6188. #define TK_DEFAULT 97
  6189. #define TK_NULL 98
  6190. #define TK_PRIMARY 99
  6191. #define TK_UNIQUE 100
  6192. #define TK_CHECK 101
  6193. #define TK_REFERENCES 102
  6194. #define TK_AUTOINCR 103
  6195. #define TK_ON 104
  6196. #define TK_INSERT 105
  6197. #define TK_DELETE 106
  6198. #define TK_UPDATE 107
  6199. #define TK_SET 108
  6200. #define TK_DEFERRABLE 109
  6201. #define TK_FOREIGN 110
  6202. #define TK_DROP 111
  6203. #define TK_UNION 112
  6204. #define TK_ALL 113
  6205. #define TK_EXCEPT 114
  6206. #define TK_INTERSECT 115
  6207. #define TK_SELECT 116
  6208. #define TK_DISTINCT 117
  6209. #define TK_DOT 118
  6210. #define TK_FROM 119
  6211. #define TK_JOIN 120
  6212. #define TK_USING 121
  6213. #define TK_ORDER 122
  6214. #define TK_GROUP 123
  6215. #define TK_HAVING 124
  6216. #define TK_LIMIT 125
  6217. #define TK_WHERE 126
  6218. #define TK_INTO 127
  6219. #define TK_VALUES 128
  6220. #define TK_INTEGER 129
  6221. #define TK_FLOAT 130
  6222. #define TK_BLOB 131
  6223. #define TK_REGISTER 132
  6224. #define TK_VARIABLE 133
  6225. #define TK_CASE 134
  6226. #define TK_WHEN 135
  6227. #define TK_THEN 136
  6228. #define TK_ELSE 137
  6229. #define TK_INDEX 138
  6230. #define TK_ALTER 139
  6231. #define TK_ADD 140
  6232. #define TK_TO_TEXT 141
  6233. #define TK_TO_BLOB 142
  6234. #define TK_TO_NUMERIC 143
  6235. #define TK_TO_INT 144
  6236. #define TK_TO_REAL 145
  6237. #define TK_ISNOT 146
  6238. #define TK_END_OF_FILE 147
  6239. #define TK_ILLEGAL 148
  6240. #define TK_SPACE 149
  6241. #define TK_UNCLOSED_STRING 150
  6242. #define TK_FUNCTION 151
  6243. #define TK_COLUMN 152
  6244. #define TK_AGG_FUNCTION 153
  6245. #define TK_AGG_COLUMN 154
  6246. #define TK_CONST_FUNC 155
  6247. #define TK_UMINUS 156
  6248. #define TK_UPLUS 157
  6249. /************** End of parse.h ***********************************************/
  6250. /************** Continuing where we left off in sqliteInt.h ******************/
  6251. #include <stdio.h>
  6252. #include <stdlib.h>
  6253. #include <string.h>
  6254. #include <assert.h>
  6255. #include <stddef.h>
  6256. /*
  6257. ** If compiling for a processor that lacks floating point support,
  6258. ** substitute integer for floating-point
  6259. */
  6260. #ifdef SQLITE_OMIT_FLOATING_POINT
  6261. # define double sqlite_int64
  6262. # define LONGDOUBLE_TYPE sqlite_int64
  6263. # ifndef SQLITE_BIG_DBL
  6264. # define SQLITE_BIG_DBL (((sqlite3_int64)1)<<50)
  6265. # endif
  6266. # define SQLITE_OMIT_DATETIME_FUNCS 1
  6267. # define SQLITE_OMIT_TRACE 1
  6268. # undef SQLITE_MIXED_ENDIAN_64BIT_FLOAT
  6269. # undef SQLITE_HAVE_ISNAN
  6270. #endif
  6271. #ifndef SQLITE_BIG_DBL
  6272. # define SQLITE_BIG_DBL (1e99)
  6273. #endif
  6274. /*
  6275. ** OMIT_TEMPDB is set to 1 if SQLITE_OMIT_TEMPDB is defined, or 0
  6276. ** afterward. Having this macro allows us to cause the C compiler
  6277. ** to omit code used by TEMP tables without messy #ifndef statements.
  6278. */
  6279. #ifdef SQLITE_OMIT_TEMPDB
  6280. #define OMIT_TEMPDB 1
  6281. #else
  6282. #define OMIT_TEMPDB 0
  6283. #endif
  6284. /*
  6285. ** If the following macro is set to 1, then NULL values are considered
  6286. ** distinct when determining whether or not two entries are the same
  6287. ** in a UNIQUE index. This is the way PostgreSQL, Oracle, DB2, MySQL,
  6288. ** OCELOT, and Firebird all work. The SQL92 spec explicitly says this
  6289. ** is the way things are suppose to work.
  6290. **
  6291. ** If the following macro is set to 0, the NULLs are indistinct for
  6292. ** a UNIQUE index. In this mode, you can only have a single NULL entry
  6293. ** for a column declared UNIQUE. This is the way Informix and SQL Server
  6294. ** work.
  6295. */
  6296. #define NULL_DISTINCT_FOR_UNIQUE 1
  6297. /*
  6298. ** The "file format" number is an integer that is incremented whenever
  6299. ** the VDBE-level file format changes. The following macros define the
  6300. ** the default file format for new databases and the maximum file format
  6301. ** that the library can read.
  6302. */
  6303. #define SQLITE_MAX_FILE_FORMAT 4
  6304. #ifndef SQLITE_DEFAULT_FILE_FORMAT
  6305. # define SQLITE_DEFAULT_FILE_FORMAT 1
  6306. #endif
  6307. #ifndef SQLITE_DEFAULT_RECURSIVE_TRIGGERS
  6308. # define SQLITE_DEFAULT_RECURSIVE_TRIGGERS 0
  6309. #endif
  6310. /*
  6311. ** Provide a default value for SQLITE_TEMP_STORE in case it is not specified
  6312. ** on the command-line
  6313. */
  6314. #ifndef SQLITE_TEMP_STORE
  6315. # define SQLITE_TEMP_STORE 1
  6316. #endif
  6317. /*
  6318. ** GCC does not define the offsetof() macro so we'll have to do it
  6319. ** ourselves.
  6320. */
  6321. #ifndef offsetof
  6322. #define offsetof(STRUCTURE,FIELD) ((int)((char*)&((STRUCTURE*)0)->FIELD))
  6323. #endif
  6324. /*
  6325. ** Check to see if this machine uses EBCDIC. (Yes, believe it or
  6326. ** not, there are still machines out there that use EBCDIC.)
  6327. */
  6328. #if 'A' == '\301'
  6329. # define SQLITE_EBCDIC 1
  6330. #else
  6331. # define SQLITE_ASCII 1
  6332. #endif
  6333. /*
  6334. ** Integers of known sizes. These typedefs might change for architectures
  6335. ** where the sizes very. Preprocessor macros are available so that the
  6336. ** types can be conveniently redefined at compile-type. Like this:
  6337. **
  6338. ** cc '-DUINTPTR_TYPE=long long int' ...
  6339. */
  6340. #ifndef UINT32_TYPE
  6341. # ifdef HAVE_UINT32_T
  6342. # define UINT32_TYPE uint32_t
  6343. # else
  6344. # define UINT32_TYPE unsigned int
  6345. # endif
  6346. #endif
  6347. #ifndef UINT16_TYPE
  6348. # ifdef HAVE_UINT16_T
  6349. # define UINT16_TYPE uint16_t
  6350. # else
  6351. # define UINT16_TYPE unsigned short int
  6352. # endif
  6353. #endif
  6354. #ifndef INT16_TYPE
  6355. # ifdef HAVE_INT16_T
  6356. # define INT16_TYPE int16_t
  6357. # else
  6358. # define INT16_TYPE short int
  6359. # endif
  6360. #endif
  6361. #ifndef UINT8_TYPE
  6362. # ifdef HAVE_UINT8_T
  6363. # define UINT8_TYPE uint8_t
  6364. # else
  6365. # define UINT8_TYPE unsigned char
  6366. # endif
  6367. #endif
  6368. #ifndef INT8_TYPE
  6369. # ifdef HAVE_INT8_T
  6370. # define INT8_TYPE int8_t
  6371. # else
  6372. # define INT8_TYPE signed char
  6373. # endif
  6374. #endif
  6375. #ifndef LONGDOUBLE_TYPE
  6376. # define LONGDOUBLE_TYPE long double
  6377. #endif
  6378. typedef sqlite_int64 i64; /* 8-byte signed integer */
  6379. typedef sqlite_uint64 u64; /* 8-byte unsigned integer */
  6380. typedef UINT32_TYPE u32; /* 4-byte unsigned integer */
  6381. typedef UINT16_TYPE u16; /* 2-byte unsigned integer */
  6382. typedef INT16_TYPE i16; /* 2-byte signed integer */
  6383. typedef UINT8_TYPE u8; /* 1-byte unsigned integer */
  6384. typedef INT8_TYPE i8; /* 1-byte signed integer */
  6385. /*
  6386. ** SQLITE_MAX_U32 is a u64 constant that is the maximum u64 value
  6387. ** that can be stored in a u32 without loss of data. The value
  6388. ** is 0x00000000ffffffff. But because of quirks of some compilers, we
  6389. ** have to specify the value in the less intuitive manner shown:
  6390. */
  6391. #define SQLITE_MAX_U32 ((((u64)1)<<32)-1)
  6392. /*
  6393. ** Macros to determine whether the machine is big or little endian,
  6394. ** evaluated at runtime.
  6395. */
  6396. #ifdef SQLITE_AMALGAMATION
  6397. SQLITE_PRIVATE const int sqlite3one = 1;
  6398. #else
  6399. SQLITE_PRIVATE const int sqlite3one;
  6400. #endif
  6401. #if defined(i386) || defined(__i386__) || defined(_M_IX86)\
  6402. || defined(__x86_64) || defined(__x86_64__)
  6403. # define SQLITE_BIGENDIAN 0
  6404. # define SQLITE_LITTLEENDIAN 1
  6405. # define SQLITE_UTF16NATIVE SQLITE_UTF16LE
  6406. #else
  6407. # define SQLITE_BIGENDIAN (*(char *)(&sqlite3one)==0)
  6408. # define SQLITE_LITTLEENDIAN (*(char *)(&sqlite3one)==1)
  6409. # define SQLITE_UTF16NATIVE (SQLITE_BIGENDIAN?SQLITE_UTF16BE:SQLITE_UTF16LE)
  6410. #endif
  6411. /*
  6412. ** Constants for the largest and smallest possible 64-bit signed integers.
  6413. ** These macros are designed to work correctly on both 32-bit and 64-bit
  6414. ** compilers.
  6415. */
  6416. #define LARGEST_INT64 (0xffffffff|(((i64)0x7fffffff)<<32))
  6417. #define SMALLEST_INT64 (((i64)-1) - LARGEST_INT64)
  6418. /*
  6419. ** Round up a number to the next larger multiple of 8. This is used
  6420. ** to force 8-byte alignment on 64-bit architectures.
  6421. */
  6422. #define ROUND8(x) (((x)+7)&~7)
  6423. /*
  6424. ** Round down to the nearest multiple of 8
  6425. */
  6426. #define ROUNDDOWN8(x) ((x)&~7)
  6427. /*
  6428. ** Assert that the pointer X is aligned to an 8-byte boundary. This
  6429. ** macro is used only within assert() to verify that the code gets
  6430. ** all alignment restrictions correct.
  6431. **
  6432. ** Except, if SQLITE_4_BYTE_ALIGNED_MALLOC is defined, then the
  6433. ** underlying malloc() implemention might return us 4-byte aligned
  6434. ** pointers. In that case, only verify 4-byte alignment.
  6435. */
  6436. #ifdef SQLITE_4_BYTE_ALIGNED_MALLOC
  6437. # define EIGHT_BYTE_ALIGNMENT(X) ((((char*)(X) - (char*)0)&3)==0)
  6438. #else
  6439. # define EIGHT_BYTE_ALIGNMENT(X) ((((char*)(X) - (char*)0)&7)==0)
  6440. #endif
  6441. /*
  6442. ** An instance of the following structure is used to store the busy-handler
  6443. ** callback for a given sqlite handle.
  6444. **
  6445. ** The sqlite.busyHandler member of the sqlite struct contains the busy
  6446. ** callback for the database handle. Each pager opened via the sqlite
  6447. ** handle is passed a pointer to sqlite.busyHandler. The busy-handler
  6448. ** callback is currently invoked only from within pager.c.
  6449. */
  6450. typedef struct BusyHandler BusyHandler;
  6451. struct BusyHandler {
  6452. int (*xFunc)(void *,int); /* The busy callback */
  6453. void *pArg; /* First arg to busy callback */
  6454. int nBusy; /* Incremented with each busy call */
  6455. };
  6456. /*
  6457. ** Name of the master database table. The master database table
  6458. ** is a special table that holds the names and attributes of all
  6459. ** user tables and indices.
  6460. */
  6461. #define MASTER_NAME "sqlite_master"
  6462. #define TEMP_MASTER_NAME "sqlite_temp_master"
  6463. /*
  6464. ** The root-page of the master database table.
  6465. */
  6466. #define MASTER_ROOT 1
  6467. /*
  6468. ** The name of the schema table.
  6469. */
  6470. #define SCHEMA_TABLE(x) ((!OMIT_TEMPDB)&&(x==1)?TEMP_MASTER_NAME:MASTER_NAME)
  6471. /*
  6472. ** A convenience macro that returns the number of elements in
  6473. ** an array.
  6474. */
  6475. #define ArraySize(X) ((int)(sizeof(X)/sizeof(X[0])))
  6476. /*
  6477. ** The following value as a destructor means to use sqlite3DbFree().
  6478. ** This is an internal extension to SQLITE_STATIC and SQLITE_TRANSIENT.
  6479. */
  6480. #define SQLITE_DYNAMIC ((sqlite3_destructor_type)sqlite3DbFree)
  6481. /*
  6482. ** When SQLITE_OMIT_WSD is defined, it means that the target platform does
  6483. ** not support Writable Static Data (WSD) such as global and static variables.
  6484. ** All variables must either be on the stack or dynamically allocated from
  6485. ** the heap. When WSD is unsupported, the variable declarations scattered
  6486. ** throughout the SQLite code must become constants instead. The SQLITE_WSD
  6487. ** macro is used for this purpose. And instead of referencing the variable
  6488. ** directly, we use its constant as a key to lookup the run-time allocated
  6489. ** buffer that holds real variable. The constant is also the initializer
  6490. ** for the run-time allocated buffer.
  6491. **
  6492. ** In the usual case where WSD is supported, the SQLITE_WSD and GLOBAL
  6493. ** macros become no-ops and have zero performance impact.
  6494. */
  6495. #ifdef SQLITE_OMIT_WSD
  6496. #define SQLITE_WSD const
  6497. #define GLOBAL(t,v) (*(t*)sqlite3_wsd_find((void*)&(v), sizeof(v)))
  6498. #define sqlite3GlobalConfig GLOBAL(struct Sqlite3Config, sqlite3Config)
  6499. SQLITE_API int sqlite3_wsd_init(int N, int J);
  6500. SQLITE_API void *sqlite3_wsd_find(void *K, int L);
  6501. #else
  6502. #define SQLITE_WSD
  6503. #define GLOBAL(t,v) v
  6504. #define sqlite3GlobalConfig sqlite3Config
  6505. #endif
  6506. /*
  6507. ** The following macros are used to suppress compiler warnings and to
  6508. ** make it clear to human readers when a function parameter is deliberately
  6509. ** left unused within the body of a function. This usually happens when
  6510. ** a function is called via a function pointer. For example the
  6511. ** implementation of an SQL aggregate step callback may not use the
  6512. ** parameter indicating the number of arguments passed to the aggregate,
  6513. ** if it knows that this is enforced elsewhere.
  6514. **
  6515. ** When a function parameter is not used at all within the body of a function,
  6516. ** it is generally named "NotUsed" or "NotUsed2" to make things even clearer.
  6517. ** However, these macros may also be used to suppress warnings related to
  6518. ** parameters that may or may not be used depending on compilation options.
  6519. ** For example those parameters only used in assert() statements. In these
  6520. ** cases the parameters are named as per the usual conventions.
  6521. */
  6522. #define UNUSED_PARAMETER(x) (void)(x)
  6523. #define UNUSED_PARAMETER2(x,y) UNUSED_PARAMETER(x),UNUSED_PARAMETER(y)
  6524. /*
  6525. ** Forward references to structures
  6526. */
  6527. typedef struct AggInfo AggInfo;
  6528. typedef struct AuthContext AuthContext;
  6529. typedef struct AutoincInfo AutoincInfo;
  6530. typedef struct Bitvec Bitvec;
  6531. typedef struct RowSet RowSet;
  6532. typedef struct CollSeq CollSeq;
  6533. typedef struct Column Column;
  6534. typedef struct Db Db;
  6535. typedef struct Schema Schema;
  6536. typedef struct Expr Expr;
  6537. typedef struct ExprList ExprList;
  6538. typedef struct ExprSpan ExprSpan;
  6539. typedef struct FKey FKey;
  6540. typedef struct FuncDef FuncDef;
  6541. typedef struct FuncDefHash FuncDefHash;
  6542. typedef struct IdList IdList;
  6543. typedef struct Index Index;
  6544. typedef struct IndexSample IndexSample;
  6545. typedef struct KeyClass KeyClass;
  6546. typedef struct KeyInfo KeyInfo;
  6547. typedef struct Lookaside Lookaside;
  6548. typedef struct LookasideSlot LookasideSlot;
  6549. typedef struct Module Module;
  6550. typedef struct NameContext NameContext;
  6551. typedef struct Parse Parse;
  6552. typedef struct Savepoint Savepoint;
  6553. typedef struct Select Select;
  6554. typedef struct SrcList SrcList;
  6555. typedef struct StrAccum StrAccum;
  6556. typedef struct Table Table;
  6557. typedef struct TableLock TableLock;
  6558. typedef struct Token Token;
  6559. typedef struct TriggerPrg TriggerPrg;
  6560. typedef struct TriggerStep TriggerStep;
  6561. typedef struct Trigger Trigger;
  6562. typedef struct UnpackedRecord UnpackedRecord;
  6563. typedef struct VTable VTable;
  6564. typedef struct Walker Walker;
  6565. typedef struct WherePlan WherePlan;
  6566. typedef struct WhereInfo WhereInfo;
  6567. typedef struct WhereLevel WhereLevel;
  6568. /*
  6569. ** Defer sourcing vdbe.h and btree.h until after the "u8" and
  6570. ** "BusyHandler" typedefs. vdbe.h also requires a few of the opaque
  6571. ** pointer types (i.e. FuncDef) defined above.
  6572. */
  6573. /************** Include btree.h in the middle of sqliteInt.h *****************/
  6574. /************** Begin file btree.h *******************************************/
  6575. /*
  6576. ** 2001 September 15
  6577. **
  6578. ** The author disclaims copyright to this source code. In place of
  6579. ** a legal notice, here is a blessing:
  6580. **
  6581. ** May you do good and not evil.
  6582. ** May you find forgiveness for yourself and forgive others.
  6583. ** May you share freely, never taking more than you give.
  6584. **
  6585. *************************************************************************
  6586. ** This header file defines the interface that the sqlite B-Tree file
  6587. ** subsystem. See comments in the source code for a detailed description
  6588. ** of what each interface routine does.
  6589. */
  6590. #ifndef _BTREE_H_
  6591. #define _BTREE_H_
  6592. /* TODO: This definition is just included so other modules compile. It
  6593. ** needs to be revisited.
  6594. */
  6595. #define SQLITE_N_BTREE_META 10
  6596. /*
  6597. ** If defined as non-zero, auto-vacuum is enabled by default. Otherwise
  6598. ** it must be turned on for each database using "PRAGMA auto_vacuum = 1".
  6599. */
  6600. #ifndef SQLITE_DEFAULT_AUTOVACUUM
  6601. #define SQLITE_DEFAULT_AUTOVACUUM 0
  6602. #endif
  6603. #define BTREE_AUTOVACUUM_NONE 0 /* Do not do auto-vacuum */
  6604. #define BTREE_AUTOVACUUM_FULL 1 /* Do full auto-vacuum */
  6605. #define BTREE_AUTOVACUUM_INCR 2 /* Incremental vacuum */
  6606. /*
  6607. ** Forward declarations of structure
  6608. */
  6609. typedef struct Btree Btree;
  6610. typedef struct BtCursor BtCursor;
  6611. typedef struct BtShared BtShared;
  6612. typedef struct BtreeMutexArray BtreeMutexArray;
  6613. /*
  6614. ** This structure records all of the Btrees that need to hold
  6615. ** a mutex before we enter sqlite3VdbeExec(). The Btrees are
  6616. ** are placed in aBtree[] in order of aBtree[]->pBt. That way,
  6617. ** we can always lock and unlock them all quickly.
  6618. */
  6619. struct BtreeMutexArray {
  6620. int nMutex;
  6621. Btree *aBtree[SQLITE_MAX_ATTACHED+1];
  6622. };
  6623. SQLITE_PRIVATE int sqlite3BtreeOpen(
  6624. const char *zFilename, /* Name of database file to open */
  6625. sqlite3 *db, /* Associated database connection */
  6626. Btree **ppBtree, /* Return open Btree* here */
  6627. int flags, /* Flags */
  6628. int vfsFlags /* Flags passed through to VFS open */
  6629. );
  6630. /* The flags parameter to sqlite3BtreeOpen can be the bitwise or of the
  6631. ** following values.
  6632. **
  6633. ** NOTE: These values must match the corresponding PAGER_ values in
  6634. ** pager.h.
  6635. */
  6636. #define BTREE_OMIT_JOURNAL 1 /* Do not use journal. No argument */
  6637. #define BTREE_NO_READLOCK 2 /* Omit readlocks on readonly files */
  6638. #define BTREE_MEMORY 4 /* In-memory DB. No argument */
  6639. #define BTREE_READONLY 8 /* Open the database in read-only mode */
  6640. #define BTREE_READWRITE 16 /* Open for both reading and writing */
  6641. #define BTREE_CREATE 32 /* Create the database if it does not exist */
  6642. SQLITE_PRIVATE int sqlite3BtreeClose(Btree*);
  6643. SQLITE_PRIVATE int sqlite3BtreeSetCacheSize(Btree*,int);
  6644. SQLITE_PRIVATE int sqlite3BtreeSetSafetyLevel(Btree*,int,int);
  6645. SQLITE_PRIVATE int sqlite3BtreeSyncDisabled(Btree*);
  6646. SQLITE_PRIVATE int sqlite3BtreeSetPageSize(Btree *p, int nPagesize, int nReserve, int eFix);
  6647. SQLITE_PRIVATE int sqlite3BtreeGetPageSize(Btree*);
  6648. SQLITE_PRIVATE int sqlite3BtreeMaxPageCount(Btree*,int);
  6649. SQLITE_PRIVATE int sqlite3BtreeGetReserve(Btree*);
  6650. SQLITE_PRIVATE int sqlite3BtreeSetAutoVacuum(Btree *, int);
  6651. SQLITE_PRIVATE int sqlite3BtreeGetAutoVacuum(Btree *);
  6652. SQLITE_PRIVATE int sqlite3BtreeBeginTrans(Btree*,int);
  6653. SQLITE_PRIVATE int sqlite3BtreeCommitPhaseOne(Btree*, const char *zMaster);
  6654. SQLITE_PRIVATE int sqlite3BtreeCommitPhaseTwo(Btree*);
  6655. SQLITE_PRIVATE int sqlite3BtreeCommit(Btree*);
  6656. SQLITE_PRIVATE int sqlite3BtreeRollback(Btree*);
  6657. SQLITE_PRIVATE int sqlite3BtreeBeginStmt(Btree*,int);
  6658. SQLITE_PRIVATE int sqlite3BtreeCreateTable(Btree*, int*, int flags);
  6659. SQLITE_PRIVATE int sqlite3BtreeIsInTrans(Btree*);
  6660. SQLITE_PRIVATE int sqlite3BtreeIsInReadTrans(Btree*);
  6661. SQLITE_PRIVATE int sqlite3BtreeIsInBackup(Btree*);
  6662. SQLITE_PRIVATE void *sqlite3BtreeSchema(Btree *, int, void(*)(void *));
  6663. SQLITE_PRIVATE int sqlite3BtreeSchemaLocked(Btree *pBtree);
  6664. SQLITE_PRIVATE int sqlite3BtreeLockTable(Btree *pBtree, int iTab, u8 isWriteLock);
  6665. SQLITE_PRIVATE int sqlite3BtreeSavepoint(Btree *, int, int);
  6666. SQLITE_PRIVATE const char *sqlite3BtreeGetFilename(Btree *);
  6667. SQLITE_PRIVATE const char *sqlite3BtreeGetJournalname(Btree *);
  6668. SQLITE_PRIVATE int sqlite3BtreeCopyFile(Btree *, Btree *);
  6669. SQLITE_PRIVATE int sqlite3BtreeIncrVacuum(Btree *);
  6670. /* The flags parameter to sqlite3BtreeCreateTable can be the bitwise OR
  6671. ** of the following flags:
  6672. */
  6673. #define BTREE_INTKEY 1 /* Table has only 64-bit signed integer keys */
  6674. #define BTREE_ZERODATA 2 /* Table has keys only - no data */
  6675. #define BTREE_LEAFDATA 4 /* Data stored in leaves only. Implies INTKEY */
  6676. SQLITE_PRIVATE int sqlite3BtreeDropTable(Btree*, int, int*);
  6677. SQLITE_PRIVATE int sqlite3BtreeClearTable(Btree*, int, int*);
  6678. SQLITE_PRIVATE void sqlite3BtreeTripAllCursors(Btree*, int);
  6679. SQLITE_PRIVATE void sqlite3BtreeGetMeta(Btree *pBtree, int idx, u32 *pValue);
  6680. SQLITE_PRIVATE int sqlite3BtreeUpdateMeta(Btree*, int idx, u32 value);
  6681. /*
  6682. ** The second parameter to sqlite3BtreeGetMeta or sqlite3BtreeUpdateMeta
  6683. ** should be one of the following values. The integer values are assigned
  6684. ** to constants so that the offset of the corresponding field in an
  6685. ** SQLite database header may be found using the following formula:
  6686. **
  6687. ** offset = 36 + (idx * 4)
  6688. **
  6689. ** For example, the free-page-count field is located at byte offset 36 of
  6690. ** the database file header. The incr-vacuum-flag field is located at
  6691. ** byte offset 64 (== 36+4*7).
  6692. */
  6693. #define BTREE_FREE_PAGE_COUNT 0
  6694. #define BTREE_SCHEMA_VERSION 1
  6695. #define BTREE_FILE_FORMAT 2
  6696. #define BTREE_DEFAULT_CACHE_SIZE 3
  6697. #define BTREE_LARGEST_ROOT_PAGE 4
  6698. #define BTREE_TEXT_ENCODING 5
  6699. #define BTREE_USER_VERSION 6
  6700. #define BTREE_INCR_VACUUM 7
  6701. SQLITE_PRIVATE int sqlite3BtreeCursor(
  6702. Btree*, /* BTree containing table to open */
  6703. int iTable, /* Index of root page */
  6704. int wrFlag, /* 1 for writing. 0 for read-only */
  6705. struct KeyInfo*, /* First argument to compare function */
  6706. BtCursor *pCursor /* Space to write cursor structure */
  6707. );
  6708. SQLITE_PRIVATE int sqlite3BtreeCursorSize(void);
  6709. SQLITE_PRIVATE void sqlite3BtreeCursorZero(BtCursor*);
  6710. SQLITE_PRIVATE int sqlite3BtreeCloseCursor(BtCursor*);
  6711. SQLITE_PRIVATE int sqlite3BtreeMovetoUnpacked(
  6712. BtCursor*,
  6713. UnpackedRecord *pUnKey,
  6714. i64 intKey,
  6715. int bias,
  6716. int *pRes
  6717. );
  6718. SQLITE_PRIVATE int sqlite3BtreeCursorHasMoved(BtCursor*, int*);
  6719. SQLITE_PRIVATE int sqlite3BtreeDelete(BtCursor*);
  6720. SQLITE_PRIVATE int sqlite3BtreeInsert(BtCursor*, const void *pKey, i64 nKey,
  6721. const void *pData, int nData,
  6722. int nZero, int bias, int seekResult);
  6723. SQLITE_PRIVATE int sqlite3BtreeFirst(BtCursor*, int *pRes);
  6724. SQLITE_PRIVATE int sqlite3BtreeLast(BtCursor*, int *pRes);
  6725. SQLITE_PRIVATE int sqlite3BtreeNext(BtCursor*, int *pRes);
  6726. SQLITE_PRIVATE int sqlite3BtreeEof(BtCursor*);
  6727. SQLITE_PRIVATE int sqlite3BtreePrevious(BtCursor*, int *pRes);
  6728. SQLITE_PRIVATE int sqlite3BtreeKeySize(BtCursor*, i64 *pSize);
  6729. SQLITE_PRIVATE int sqlite3BtreeKey(BtCursor*, u32 offset, u32 amt, void*);
  6730. SQLITE_PRIVATE const void *sqlite3BtreeKeyFetch(BtCursor*, int *pAmt);
  6731. SQLITE_PRIVATE const void *sqlite3BtreeDataFetch(BtCursor*, int *pAmt);
  6732. SQLITE_PRIVATE int sqlite3BtreeDataSize(BtCursor*, u32 *pSize);
  6733. SQLITE_PRIVATE int sqlite3BtreeData(BtCursor*, u32 offset, u32 amt, void*);
  6734. SQLITE_PRIVATE void sqlite3BtreeSetCachedRowid(BtCursor*, sqlite3_int64);
  6735. SQLITE_PRIVATE sqlite3_int64 sqlite3BtreeGetCachedRowid(BtCursor*);
  6736. SQLITE_PRIVATE char *sqlite3BtreeIntegrityCheck(Btree*, int *aRoot, int nRoot, int, int*);
  6737. SQLITE_PRIVATE struct Pager *sqlite3BtreePager(Btree*);
  6738. SQLITE_PRIVATE int sqlite3BtreePutData(BtCursor*, u32 offset, u32 amt, void*);
  6739. SQLITE_PRIVATE void sqlite3BtreeCacheOverflow(BtCursor *);
  6740. SQLITE_PRIVATE void sqlite3BtreeClearCursor(BtCursor *);
  6741. #ifndef NDEBUG
  6742. SQLITE_PRIVATE int sqlite3BtreeCursorIsValid(BtCursor*);
  6743. #endif
  6744. #ifndef SQLITE_OMIT_BTREECOUNT
  6745. SQLITE_PRIVATE int sqlite3BtreeCount(BtCursor *, i64 *);
  6746. #endif
  6747. #ifdef SQLITE_TEST
  6748. SQLITE_PRIVATE int sqlite3BtreeCursorInfo(BtCursor*, int*, int);
  6749. SQLITE_PRIVATE void sqlite3BtreeCursorList(Btree*);
  6750. #endif
  6751. /*
  6752. ** If we are not using shared cache, then there is no need to
  6753. ** use mutexes to access the BtShared structures. So make the
  6754. ** Enter and Leave procedures no-ops.
  6755. */
  6756. #ifndef SQLITE_OMIT_SHARED_CACHE
  6757. SQLITE_PRIVATE void sqlite3BtreeEnter(Btree*);
  6758. SQLITE_PRIVATE void sqlite3BtreeEnterAll(sqlite3*);
  6759. #else
  6760. # define sqlite3BtreeEnter(X)
  6761. # define sqlite3BtreeEnterAll(X)
  6762. #endif
  6763. #if !defined(SQLITE_OMIT_SHARED_CACHE) && SQLITE_THREADSAFE
  6764. SQLITE_PRIVATE void sqlite3BtreeLeave(Btree*);
  6765. SQLITE_PRIVATE void sqlite3BtreeEnterCursor(BtCursor*);
  6766. SQLITE_PRIVATE void sqlite3BtreeLeaveCursor(BtCursor*);
  6767. SQLITE_PRIVATE void sqlite3BtreeLeaveAll(sqlite3*);
  6768. SQLITE_PRIVATE void sqlite3BtreeMutexArrayEnter(BtreeMutexArray*);
  6769. SQLITE_PRIVATE void sqlite3BtreeMutexArrayLeave(BtreeMutexArray*);
  6770. SQLITE_PRIVATE void sqlite3BtreeMutexArrayInsert(BtreeMutexArray*, Btree*);
  6771. #ifndef NDEBUG
  6772. /* These routines are used inside assert() statements only. */
  6773. SQLITE_PRIVATE int sqlite3BtreeHoldsMutex(Btree*);
  6774. SQLITE_PRIVATE int sqlite3BtreeHoldsAllMutexes(sqlite3*);
  6775. #endif
  6776. #else
  6777. # define sqlite3BtreeLeave(X)
  6778. # define sqlite3BtreeEnterCursor(X)
  6779. # define sqlite3BtreeLeaveCursor(X)
  6780. # define sqlite3BtreeLeaveAll(X)
  6781. # define sqlite3BtreeMutexArrayEnter(X)
  6782. # define sqlite3BtreeMutexArrayLeave(X)
  6783. # define sqlite3BtreeMutexArrayInsert(X,Y)
  6784. # define sqlite3BtreeHoldsMutex(X) 1
  6785. # define sqlite3BtreeHoldsAllMutexes(X) 1
  6786. #endif
  6787. #endif /* _BTREE_H_ */
  6788. /************** End of btree.h ***********************************************/
  6789. /************** Continuing where we left off in sqliteInt.h ******************/
  6790. /************** Include vdbe.h in the middle of sqliteInt.h ******************/
  6791. /************** Begin file vdbe.h ********************************************/
  6792. /*
  6793. ** 2001 September 15
  6794. **
  6795. ** The author disclaims copyright to this source code. In place of
  6796. ** a legal notice, here is a blessing:
  6797. **
  6798. ** May you do good and not evil.
  6799. ** May you find forgiveness for yourself and forgive others.
  6800. ** May you share freely, never taking more than you give.
  6801. **
  6802. *************************************************************************
  6803. ** Header file for the Virtual DataBase Engine (VDBE)
  6804. **
  6805. ** This header defines the interface to the virtual database engine
  6806. ** or VDBE. The VDBE implements an abstract machine that runs a
  6807. ** simple program to access and modify the underlying database.
  6808. */
  6809. #ifndef _SQLITE_VDBE_H_
  6810. #define _SQLITE_VDBE_H_
  6811. /*
  6812. ** A single VDBE is an opaque structure named "Vdbe". Only routines
  6813. ** in the source file sqliteVdbe.c are allowed to see the insides
  6814. ** of this structure.
  6815. */
  6816. typedef struct Vdbe Vdbe;
  6817. /*
  6818. ** The names of the following types declared in vdbeInt.h are required
  6819. ** for the VdbeOp definition.
  6820. */
  6821. typedef struct VdbeFunc VdbeFunc;
  6822. typedef struct Mem Mem;
  6823. typedef struct SubProgram SubProgram;
  6824. /*
  6825. ** A single instruction of the virtual machine has an opcode
  6826. ** and as many as three operands. The instruction is recorded
  6827. ** as an instance of the following structure:
  6828. */
  6829. struct VdbeOp {
  6830. u8 opcode; /* What operation to perform */
  6831. signed char p4type; /* One of the P4_xxx constants for p4 */
  6832. u8 opflags; /* Mask of the OPFLG_* flags in opcodes.h */
  6833. u8 p5; /* Fifth parameter is an unsigned character */
  6834. int p1; /* First operand */
  6835. int p2; /* Second parameter (often the jump destination) */
  6836. int p3; /* The third parameter */
  6837. union { /* fourth parameter */
  6838. int i; /* Integer value if p4type==P4_INT32 */
  6839. void *p; /* Generic pointer */
  6840. char *z; /* Pointer to data for string (char array) types */
  6841. i64 *pI64; /* Used when p4type is P4_INT64 */
  6842. double *pReal; /* Used when p4type is P4_REAL */
  6843. FuncDef *pFunc; /* Used when p4type is P4_FUNCDEF */
  6844. VdbeFunc *pVdbeFunc; /* Used when p4type is P4_VDBEFUNC */
  6845. CollSeq *pColl; /* Used when p4type is P4_COLLSEQ */
  6846. Mem *pMem; /* Used when p4type is P4_MEM */
  6847. VTable *pVtab; /* Used when p4type is P4_VTAB */
  6848. KeyInfo *pKeyInfo; /* Used when p4type is P4_KEYINFO */
  6849. int *ai; /* Used when p4type is P4_INTARRAY */
  6850. SubProgram *pProgram; /* Used when p4type is P4_SUBPROGRAM */
  6851. } p4;
  6852. #ifdef SQLITE_DEBUG
  6853. char *zComment; /* Comment to improve readability */
  6854. #endif
  6855. #ifdef VDBE_PROFILE
  6856. int cnt; /* Number of times this instruction was executed */
  6857. u64 cycles; /* Total time spent executing this instruction */
  6858. #endif
  6859. };
  6860. typedef struct VdbeOp VdbeOp;
  6861. /*
  6862. ** A sub-routine used to implement a trigger program.
  6863. */
  6864. struct SubProgram {
  6865. VdbeOp *aOp; /* Array of opcodes for sub-program */
  6866. int nOp; /* Elements in aOp[] */
  6867. int nMem; /* Number of memory cells required */
  6868. int nCsr; /* Number of cursors required */
  6869. int nRef; /* Number of pointers to this structure */
  6870. void *token; /* id that may be used to recursive triggers */
  6871. };
  6872. /*
  6873. ** A smaller version of VdbeOp used for the VdbeAddOpList() function because
  6874. ** it takes up less space.
  6875. */
  6876. struct VdbeOpList {
  6877. u8 opcode; /* What operation to perform */
  6878. signed char p1; /* First operand */
  6879. signed char p2; /* Second parameter (often the jump destination) */
  6880. signed char p3; /* Third parameter */
  6881. };
  6882. typedef struct VdbeOpList VdbeOpList;
  6883. /*
  6884. ** Allowed values of VdbeOp.p4type
  6885. */
  6886. #define P4_NOTUSED 0 /* The P4 parameter is not used */
  6887. #define P4_DYNAMIC (-1) /* Pointer to a string obtained from sqliteMalloc() */
  6888. #define P4_STATIC (-2) /* Pointer to a static string */
  6889. #define P4_COLLSEQ (-4) /* P4 is a pointer to a CollSeq structure */
  6890. #define P4_FUNCDEF (-5) /* P4 is a pointer to a FuncDef structure */
  6891. #define P4_KEYINFO (-6) /* P4 is a pointer to a KeyInfo structure */
  6892. #define P4_VDBEFUNC (-7) /* P4 is a pointer to a VdbeFunc structure */
  6893. #define P4_MEM (-8) /* P4 is a pointer to a Mem* structure */
  6894. #define P4_TRANSIENT (-9) /* P4 is a pointer to a transient string */
  6895. #define P4_VTAB (-10) /* P4 is a pointer to an sqlite3_vtab structure */
  6896. #define P4_MPRINTF (-11) /* P4 is a string obtained from sqlite3_mprintf() */
  6897. #define P4_REAL (-12) /* P4 is a 64-bit floating point value */
  6898. #define P4_INT64 (-13) /* P4 is a 64-bit signed integer */
  6899. #define P4_INT32 (-14) /* P4 is a 32-bit signed integer */
  6900. #define P4_INTARRAY (-15) /* P4 is a vector of 32-bit integers */
  6901. #define P4_SUBPROGRAM (-18) /* P4 is a pointer to a SubProgram structure */
  6902. /* When adding a P4 argument using P4_KEYINFO, a copy of the KeyInfo structure
  6903. ** is made. That copy is freed when the Vdbe is finalized. But if the
  6904. ** argument is P4_KEYINFO_HANDOFF, the passed in pointer is used. It still
  6905. ** gets freed when the Vdbe is finalized so it still should be obtained
  6906. ** from a single sqliteMalloc(). But no copy is made and the calling
  6907. ** function should *not* try to free the KeyInfo.
  6908. */
  6909. #define P4_KEYINFO_HANDOFF (-16)
  6910. #define P4_KEYINFO_STATIC (-17)
  6911. /*
  6912. ** The Vdbe.aColName array contains 5n Mem structures, where n is the
  6913. ** number of columns of data returned by the statement.
  6914. */
  6915. #define COLNAME_NAME 0
  6916. #define COLNAME_DECLTYPE 1
  6917. #define COLNAME_DATABASE 2
  6918. #define COLNAME_TABLE 3
  6919. #define COLNAME_COLUMN 4
  6920. #ifdef SQLITE_ENABLE_COLUMN_METADATA
  6921. # define COLNAME_N 5 /* Number of COLNAME_xxx symbols */
  6922. #else
  6923. # ifdef SQLITE_OMIT_DECLTYPE
  6924. # define COLNAME_N 1 /* Store only the name */
  6925. # else
  6926. # define COLNAME_N 2 /* Store the name and decltype */
  6927. # endif
  6928. #endif
  6929. /*
  6930. ** The following macro converts a relative address in the p2 field
  6931. ** of a VdbeOp structure into a negative number so that
  6932. ** sqlite3VdbeAddOpList() knows that the address is relative. Calling
  6933. ** the macro again restores the address.
  6934. */
  6935. #define ADDR(X) (-1-(X))
  6936. /*
  6937. ** The makefile scans the vdbe.c source file and creates the "opcodes.h"
  6938. ** header file that defines a number for each opcode used by the VDBE.
  6939. */
  6940. /************** Include opcodes.h in the middle of vdbe.h ********************/
  6941. /************** Begin file opcodes.h *****************************************/
  6942. /* Automatically generated. Do not edit */
  6943. /* See the mkopcodeh.awk script for details */
  6944. #define OP_Goto 1
  6945. #define OP_Gosub 2
  6946. #define OP_Return 3
  6947. #define OP_Yield 4
  6948. #define OP_HaltIfNull 5
  6949. #define OP_Halt 6
  6950. #define OP_Integer 7
  6951. #define OP_Int64 8
  6952. #define OP_Real 130 /* same as TK_FLOAT */
  6953. #define OP_String8 94 /* same as TK_STRING */
  6954. #define OP_String 9
  6955. #define OP_Null 10
  6956. #define OP_Blob 11
  6957. #define OP_Variable 12
  6958. #define OP_Move 13
  6959. #define OP_Copy 14
  6960. #define OP_SCopy 15
  6961. #define OP_ResultRow 16
  6962. #define OP_Concat 91 /* same as TK_CONCAT */
  6963. #define OP_Add 86 /* same as TK_PLUS */
  6964. #define OP_Subtract 87 /* same as TK_MINUS */
  6965. #define OP_Multiply 88 /* same as TK_STAR */
  6966. #define OP_Divide 89 /* same as TK_SLASH */
  6967. #define OP_Remainder 90 /* same as TK_REM */
  6968. #define OP_CollSeq 17
  6969. #define OP_Function 18
  6970. #define OP_BitAnd 82 /* same as TK_BITAND */
  6971. #define OP_BitOr 83 /* same as TK_BITOR */
  6972. #define OP_ShiftLeft 84 /* same as TK_LSHIFT */
  6973. #define OP_ShiftRight 85 /* same as TK_RSHIFT */
  6974. #define OP_AddImm 20
  6975. #define OP_MustBeInt 21
  6976. #define OP_RealAffinity 22
  6977. #define OP_ToText 141 /* same as TK_TO_TEXT */
  6978. #define OP_ToBlob 142 /* same as TK_TO_BLOB */
  6979. #define OP_ToNumeric 143 /* same as TK_TO_NUMERIC*/
  6980. #define OP_ToInt 144 /* same as TK_TO_INT */
  6981. #define OP_ToReal 145 /* same as TK_TO_REAL */
  6982. #define OP_Eq 76 /* same as TK_EQ */
  6983. #define OP_Ne 75 /* same as TK_NE */
  6984. #define OP_Lt 79 /* same as TK_LT */
  6985. #define OP_Le 78 /* same as TK_LE */
  6986. #define OP_Gt 77 /* same as TK_GT */
  6987. #define OP_Ge 80 /* same as TK_GE */
  6988. #define OP_Permutation 23
  6989. #define OP_Compare 24
  6990. #define OP_Jump 25
  6991. #define OP_And 69 /* same as TK_AND */
  6992. #define OP_Or 68 /* same as TK_OR */
  6993. #define OP_Not 19 /* same as TK_NOT */
  6994. #define OP_BitNot 93 /* same as TK_BITNOT */
  6995. #define OP_If 26
  6996. #define OP_IfNot 27
  6997. #define OP_IsNull 73 /* same as TK_ISNULL */
  6998. #define OP_NotNull 74 /* same as TK_NOTNULL */
  6999. #define OP_Column 28
  7000. #define OP_Affinity 29
  7001. #define OP_MakeRecord 30
  7002. #define OP_Count 31
  7003. #define OP_Savepoint 32
  7004. #define OP_AutoCommit 33
  7005. #define OP_Transaction 34
  7006. #define OP_ReadCookie 35
  7007. #define OP_SetCookie 36
  7008. #define OP_VerifyCookie 37
  7009. #define OP_OpenRead 38
  7010. #define OP_OpenWrite 39
  7011. #define OP_OpenEphemeral 40
  7012. #define OP_OpenPseudo 41
  7013. #define OP_Close 42
  7014. #define OP_SeekLt 43
  7015. #define OP_SeekLe 44
  7016. #define OP_SeekGe 45
  7017. #define OP_SeekGt 46
  7018. #define OP_Seek 47
  7019. #define OP_NotFound 48
  7020. #define OP_Found 49
  7021. #define OP_IsUnique 50
  7022. #define OP_NotExists 51
  7023. #define OP_Sequence 52
  7024. #define OP_NewRowid 53
  7025. #define OP_Insert 54
  7026. #define OP_InsertInt 55
  7027. #define OP_Delete 56
  7028. #define OP_ResetCount 57
  7029. #define OP_RowKey 58
  7030. #define OP_RowData 59
  7031. #define OP_Rowid 60
  7032. #define OP_NullRow 61
  7033. #define OP_Last 62
  7034. #define OP_Sort 63
  7035. #define OP_Rewind 64
  7036. #define OP_Prev 65
  7037. #define OP_Next 66
  7038. #define OP_IdxInsert 67
  7039. #define OP_IdxDelete 70
  7040. #define OP_IdxRowid 71
  7041. #define OP_IdxLT 72
  7042. #define OP_IdxGE 81
  7043. #define OP_Destroy 92
  7044. #define OP_Clear 95
  7045. #define OP_CreateIndex 96
  7046. #define OP_CreateTable 97
  7047. #define OP_ParseSchema 98
  7048. #define OP_LoadAnalysis 99
  7049. #define OP_DropTable 100
  7050. #define OP_DropIndex 101
  7051. #define OP_DropTrigger 102
  7052. #define OP_IntegrityCk 103
  7053. #define OP_RowSetAdd 104
  7054. #define OP_RowSetRead 105
  7055. #define OP_RowSetTest 106
  7056. #define OP_Program 107
  7057. #define OP_Param 108
  7058. #define OP_FkCounter 109
  7059. #define OP_FkIfZero 110
  7060. #define OP_MemMax 111
  7061. #define OP_IfPos 112
  7062. #define OP_IfNeg 113
  7063. #define OP_IfZero 114
  7064. #define OP_AggStep 115
  7065. #define OP_AggFinal 116
  7066. #define OP_Vacuum 117
  7067. #define OP_IncrVacuum 118
  7068. #define OP_Expire 119
  7069. #define OP_TableLock 120
  7070. #define OP_VBegin 121
  7071. #define OP_VCreate 122
  7072. #define OP_VDestroy 123
  7073. #define OP_VOpen 124
  7074. #define OP_VFilter 125
  7075. #define OP_VColumn 126
  7076. #define OP_VNext 127
  7077. #define OP_VRename 128
  7078. #define OP_VUpdate 129
  7079. #define OP_Pagecount 131
  7080. #define OP_Trace 132
  7081. #define OP_Noop 133
  7082. #define OP_Explain 134
  7083. /* The following opcode values are never used */
  7084. #define OP_NotUsed_135 135
  7085. #define OP_NotUsed_136 136
  7086. #define OP_NotUsed_137 137
  7087. #define OP_NotUsed_138 138
  7088. #define OP_NotUsed_139 139
  7089. #define OP_NotUsed_140 140
  7090. /* Properties such as "out2" or "jump" that are specified in
  7091. ** comments following the "case" for each opcode in the vdbe.c
  7092. ** are encoded into bitvectors as follows:
  7093. */
  7094. #define OPFLG_JUMP 0x0001 /* jump: P2 holds jmp target */
  7095. #define OPFLG_OUT2_PRERELEASE 0x0002 /* out2-prerelease: */
  7096. #define OPFLG_IN1 0x0004 /* in1: P1 is an input */
  7097. #define OPFLG_IN2 0x0008 /* in2: P2 is an input */
  7098. #define OPFLG_IN3 0x0010 /* in3: P3 is an input */
  7099. #define OPFLG_OUT2 0x0020 /* out2: P2 is an output */
  7100. #define OPFLG_OUT3 0x0040 /* out3: P3 is an output */
  7101. #define OPFLG_INITIALIZER {\
  7102. /* 0 */ 0x00, 0x01, 0x05, 0x04, 0x04, 0x10, 0x00, 0x02,\
  7103. /* 8 */ 0x02, 0x02, 0x02, 0x02, 0x00, 0x00, 0x24, 0x24,\
  7104. /* 16 */ 0x00, 0x00, 0x00, 0x24, 0x04, 0x05, 0x04, 0x00,\
  7105. /* 24 */ 0x00, 0x01, 0x05, 0x05, 0x00, 0x00, 0x00, 0x02,\
  7106. /* 32 */ 0x00, 0x00, 0x00, 0x02, 0x10, 0x00, 0x00, 0x00,\
  7107. /* 40 */ 0x00, 0x00, 0x00, 0x11, 0x11, 0x11, 0x11, 0x08,\
  7108. /* 48 */ 0x11, 0x11, 0x11, 0x11, 0x02, 0x02, 0x00, 0x00,\
  7109. /* 56 */ 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x01, 0x01,\
  7110. /* 64 */ 0x01, 0x01, 0x01, 0x08, 0x4c, 0x4c, 0x00, 0x02,\
  7111. /* 72 */ 0x01, 0x05, 0x05, 0x15, 0x15, 0x15, 0x15, 0x15,\
  7112. /* 80 */ 0x15, 0x01, 0x4c, 0x4c, 0x4c, 0x4c, 0x4c, 0x4c,\
  7113. /* 88 */ 0x4c, 0x4c, 0x4c, 0x4c, 0x02, 0x24, 0x02, 0x00,\
  7114. /* 96 */ 0x02, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,\
  7115. /* 104 */ 0x0c, 0x45, 0x15, 0x01, 0x02, 0x00, 0x01, 0x08,\
  7116. /* 112 */ 0x05, 0x05, 0x05, 0x00, 0x00, 0x00, 0x01, 0x00,\
  7117. /* 120 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x01,\
  7118. /* 128 */ 0x00, 0x00, 0x02, 0x02, 0x00, 0x00, 0x00, 0x00,\
  7119. /* 136 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x04, 0x04,\
  7120. /* 144 */ 0x04, 0x04,}
  7121. /************** End of opcodes.h *********************************************/
  7122. /************** Continuing where we left off in vdbe.h ***********************/
  7123. /*
  7124. ** Prototypes for the VDBE interface. See comments on the implementation
  7125. ** for a description of what each of these routines does.
  7126. */
  7127. SQLITE_PRIVATE Vdbe *sqlite3VdbeCreate(sqlite3*);
  7128. SQLITE_PRIVATE int sqlite3VdbeAddOp0(Vdbe*,int);
  7129. SQLITE_PRIVATE int sqlite3VdbeAddOp1(Vdbe*,int,int);
  7130. SQLITE_PRIVATE int sqlite3VdbeAddOp2(Vdbe*,int,int,int);
  7131. SQLITE_PRIVATE int sqlite3VdbeAddOp3(Vdbe*,int,int,int,int);
  7132. SQLITE_PRIVATE int sqlite3VdbeAddOp4(Vdbe*,int,int,int,int,const char *zP4,int);
  7133. SQLITE_PRIVATE int sqlite3VdbeAddOp4Int(Vdbe*,int,int,int,int,int);
  7134. SQLITE_PRIVATE int sqlite3VdbeAddOpList(Vdbe*, int nOp, VdbeOpList const *aOp);
  7135. SQLITE_PRIVATE void sqlite3VdbeChangeP1(Vdbe*, int addr, int P1);
  7136. SQLITE_PRIVATE void sqlite3VdbeChangeP2(Vdbe*, int addr, int P2);
  7137. SQLITE_PRIVATE void sqlite3VdbeChangeP3(Vdbe*, int addr, int P3);
  7138. SQLITE_PRIVATE void sqlite3VdbeChangeP5(Vdbe*, u8 P5);
  7139. SQLITE_PRIVATE void sqlite3VdbeJumpHere(Vdbe*, int addr);
  7140. SQLITE_PRIVATE void sqlite3VdbeChangeToNoop(Vdbe*, int addr, int N);
  7141. SQLITE_PRIVATE void sqlite3VdbeChangeP4(Vdbe*, int addr, const char *zP4, int N);
  7142. SQLITE_PRIVATE void sqlite3VdbeUsesBtree(Vdbe*, int);
  7143. SQLITE_PRIVATE VdbeOp *sqlite3VdbeGetOp(Vdbe*, int);
  7144. SQLITE_PRIVATE int sqlite3VdbeMakeLabel(Vdbe*);
  7145. SQLITE_PRIVATE void sqlite3VdbeDelete(Vdbe*);
  7146. SQLITE_PRIVATE void sqlite3VdbeMakeReady(Vdbe*,int,int,int,int,int,int);
  7147. SQLITE_PRIVATE int sqlite3VdbeFinalize(Vdbe*);
  7148. SQLITE_PRIVATE void sqlite3VdbeResolveLabel(Vdbe*, int);
  7149. SQLITE_PRIVATE int sqlite3VdbeCurrentAddr(Vdbe*);
  7150. #ifdef SQLITE_DEBUG
  7151. SQLITE_PRIVATE int sqlite3VdbeAssertMayAbort(Vdbe *, int);
  7152. SQLITE_PRIVATE void sqlite3VdbeTrace(Vdbe*,FILE*);
  7153. #endif
  7154. SQLITE_PRIVATE void sqlite3VdbeResetStepResult(Vdbe*);
  7155. SQLITE_PRIVATE int sqlite3VdbeReset(Vdbe*);
  7156. SQLITE_PRIVATE void sqlite3VdbeSetNumCols(Vdbe*,int);
  7157. SQLITE_PRIVATE int sqlite3VdbeSetColName(Vdbe*, int, int, const char *, void(*)(void*));
  7158. SQLITE_PRIVATE void sqlite3VdbeCountChanges(Vdbe*);
  7159. SQLITE_PRIVATE sqlite3 *sqlite3VdbeDb(Vdbe*);
  7160. SQLITE_PRIVATE void sqlite3VdbeSetSql(Vdbe*, const char *z, int n, int);
  7161. SQLITE_PRIVATE void sqlite3VdbeSwap(Vdbe*,Vdbe*);
  7162. SQLITE_PRIVATE VdbeOp *sqlite3VdbeTakeOpArray(Vdbe*, int*, int*);
  7163. SQLITE_PRIVATE void sqlite3VdbeProgramDelete(sqlite3 *, SubProgram *, int);
  7164. SQLITE_PRIVATE sqlite3_value *sqlite3VdbeGetValue(Vdbe*, int, u8);
  7165. SQLITE_PRIVATE void sqlite3VdbeSetVarmask(Vdbe*, int);
  7166. #ifndef SQLITE_OMIT_TRACE
  7167. SQLITE_PRIVATE char *sqlite3VdbeExpandSql(Vdbe*, const char*);
  7168. #endif
  7169. SQLITE_PRIVATE UnpackedRecord *sqlite3VdbeRecordUnpack(KeyInfo*,int,const void*,char*,int);
  7170. SQLITE_PRIVATE void sqlite3VdbeDeleteUnpackedRecord(UnpackedRecord*);
  7171. SQLITE_PRIVATE int sqlite3VdbeRecordCompare(int,const void*,UnpackedRecord*);
  7172. #ifndef NDEBUG
  7173. SQLITE_PRIVATE void sqlite3VdbeComment(Vdbe*, const char*, ...);
  7174. # define VdbeComment(X) sqlite3VdbeComment X
  7175. SQLITE_PRIVATE void sqlite3VdbeNoopComment(Vdbe*, const char*, ...);
  7176. # define VdbeNoopComment(X) sqlite3VdbeNoopComment X
  7177. #else
  7178. # define VdbeComment(X)
  7179. # define VdbeNoopComment(X)
  7180. #endif
  7181. #endif
  7182. /************** End of vdbe.h ************************************************/
  7183. /************** Continuing where we left off in sqliteInt.h ******************/
  7184. /************** Include pager.h in the middle of sqliteInt.h *****************/
  7185. /************** Begin file pager.h *******************************************/
  7186. /*
  7187. ** 2001 September 15
  7188. **
  7189. ** The author disclaims copyright to this source code. In place of
  7190. ** a legal notice, here is a blessing:
  7191. **
  7192. ** May you do good and not evil.
  7193. ** May you find forgiveness for yourself and forgive others.
  7194. ** May you share freely, never taking more than you give.
  7195. **
  7196. *************************************************************************
  7197. ** This header file defines the interface that the sqlite page cache
  7198. ** subsystem. The page cache subsystem reads and writes a file a page
  7199. ** at a time and provides a journal for rollback.
  7200. */
  7201. #ifndef _PAGER_H_
  7202. #define _PAGER_H_
  7203. /*
  7204. ** Default maximum size for persistent journal files. A negative
  7205. ** value means no limit. This value may be overridden using the
  7206. ** sqlite3PagerJournalSizeLimit() API. See also "PRAGMA journal_size_limit".
  7207. */
  7208. #ifndef SQLITE_DEFAULT_JOURNAL_SIZE_LIMIT
  7209. #define SQLITE_DEFAULT_JOURNAL_SIZE_LIMIT -1
  7210. #endif
  7211. /*
  7212. ** The type used to represent a page number. The first page in a file
  7213. ** is called page 1. 0 is used to represent "not a page".
  7214. */
  7215. typedef u32 Pgno;
  7216. /*
  7217. ** Each open file is managed by a separate instance of the "Pager" structure.
  7218. */
  7219. typedef struct Pager Pager;
  7220. /*
  7221. ** Handle type for pages.
  7222. */
  7223. typedef struct PgHdr DbPage;
  7224. /*
  7225. ** Page number PAGER_MJ_PGNO is never used in an SQLite database (it is
  7226. ** reserved for working around a windows/posix incompatibility). It is
  7227. ** used in the journal to signify that the remainder of the journal file
  7228. ** is devoted to storing a master journal name - there are no more pages to
  7229. ** roll back. See comments for function writeMasterJournal() in pager.c
  7230. ** for details.
  7231. */
  7232. #define PAGER_MJ_PGNO(x) ((Pgno)((PENDING_BYTE/((x)->pageSize))+1))
  7233. /*
  7234. ** Allowed values for the flags parameter to sqlite3PagerOpen().
  7235. **
  7236. ** NOTE: These values must match the corresponding BTREE_ values in btree.h.
  7237. */
  7238. #define PAGER_OMIT_JOURNAL 0x0001 /* Do not use a rollback journal */
  7239. #define PAGER_NO_READLOCK 0x0002 /* Omit readlocks on readonly files */
  7240. /*
  7241. ** Valid values for the second argument to sqlite3PagerLockingMode().
  7242. */
  7243. #define PAGER_LOCKINGMODE_QUERY -1
  7244. #define PAGER_LOCKINGMODE_NORMAL 0
  7245. #define PAGER_LOCKINGMODE_EXCLUSIVE 1
  7246. /*
  7247. ** Valid values for the second argument to sqlite3PagerJournalMode().
  7248. */
  7249. #define PAGER_JOURNALMODE_QUERY -1
  7250. #define PAGER_JOURNALMODE_DELETE 0 /* Commit by deleting journal file */
  7251. #define PAGER_JOURNALMODE_PERSIST 1 /* Commit by zeroing journal header */
  7252. #define PAGER_JOURNALMODE_OFF 2 /* Journal omitted. */
  7253. #define PAGER_JOURNALMODE_TRUNCATE 3 /* Commit by truncating journal */
  7254. #define PAGER_JOURNALMODE_MEMORY 4 /* In-memory journal file */
  7255. /*
  7256. ** The remainder of this file contains the declarations of the functions
  7257. ** that make up the Pager sub-system API. See source code comments for
  7258. ** a detailed description of each routine.
  7259. */
  7260. /* Open and close a Pager connection. */
  7261. SQLITE_PRIVATE int sqlite3PagerOpen(
  7262. sqlite3_vfs*,
  7263. Pager **ppPager,
  7264. const char*,
  7265. int,
  7266. int,
  7267. int,
  7268. void(*)(DbPage*)
  7269. );
  7270. SQLITE_PRIVATE int sqlite3PagerClose(Pager *pPager);
  7271. SQLITE_PRIVATE int sqlite3PagerReadFileheader(Pager*, int, unsigned char*);
  7272. /* Functions used to configure a Pager object. */
  7273. SQLITE_PRIVATE void sqlite3PagerSetBusyhandler(Pager*, int(*)(void *), void *);
  7274. SQLITE_PRIVATE int sqlite3PagerSetPagesize(Pager*, u16*, int);
  7275. SQLITE_PRIVATE int sqlite3PagerMaxPageCount(Pager*, int);
  7276. SQLITE_PRIVATE void sqlite3PagerSetCachesize(Pager*, int);
  7277. SQLITE_PRIVATE void sqlite3PagerSetSafetyLevel(Pager*,int,int);
  7278. SQLITE_PRIVATE int sqlite3PagerLockingMode(Pager *, int);
  7279. SQLITE_PRIVATE int sqlite3PagerJournalMode(Pager *, int);
  7280. SQLITE_PRIVATE i64 sqlite3PagerJournalSizeLimit(Pager *, i64);
  7281. SQLITE_PRIVATE sqlite3_backup **sqlite3PagerBackupPtr(Pager*);
  7282. /* Functions used to obtain and release page references. */
  7283. SQLITE_PRIVATE int sqlite3PagerAcquire(Pager *pPager, Pgno pgno, DbPage **ppPage, int clrFlag);
  7284. #define sqlite3PagerGet(A,B,C) sqlite3PagerAcquire(A,B,C,0)
  7285. SQLITE_PRIVATE DbPage *sqlite3PagerLookup(Pager *pPager, Pgno pgno);
  7286. SQLITE_PRIVATE void sqlite3PagerRef(DbPage*);
  7287. SQLITE_PRIVATE void sqlite3PagerUnref(DbPage*);
  7288. /* Operations on page references. */
  7289. SQLITE_PRIVATE int sqlite3PagerWrite(DbPage*);
  7290. SQLITE_PRIVATE void sqlite3PagerDontWrite(DbPage*);
  7291. SQLITE_PRIVATE int sqlite3PagerMovepage(Pager*,DbPage*,Pgno,int);
  7292. SQLITE_PRIVATE int sqlite3PagerPageRefcount(DbPage*);
  7293. SQLITE_PRIVATE void *sqlite3PagerGetData(DbPage *);
  7294. SQLITE_PRIVATE void *sqlite3PagerGetExtra(DbPage *);
  7295. /* Functions used to manage pager transactions and savepoints. */
  7296. SQLITE_PRIVATE int sqlite3PagerPagecount(Pager*, int*);
  7297. SQLITE_PRIVATE int sqlite3PagerBegin(Pager*, int exFlag, int);
  7298. SQLITE_PRIVATE int sqlite3PagerCommitPhaseOne(Pager*,const char *zMaster, int);
  7299. SQLITE_PRIVATE int sqlite3PagerSync(Pager *pPager);
  7300. SQLITE_PRIVATE int sqlite3PagerCommitPhaseTwo(Pager*);
  7301. SQLITE_PRIVATE int sqlite3PagerRollback(Pager*);
  7302. SQLITE_PRIVATE int sqlite3PagerOpenSavepoint(Pager *pPager, int n);
  7303. SQLITE_PRIVATE int sqlite3PagerSavepoint(Pager *pPager, int op, int iSavepoint);
  7304. SQLITE_PRIVATE int sqlite3PagerSharedLock(Pager *pPager);
  7305. /* Functions used to query pager state and configuration. */
  7306. SQLITE_PRIVATE u8 sqlite3PagerIsreadonly(Pager*);
  7307. SQLITE_PRIVATE int sqlite3PagerRefcount(Pager*);
  7308. SQLITE_PRIVATE const char *sqlite3PagerFilename(Pager*);
  7309. SQLITE_PRIVATE const sqlite3_vfs *sqlite3PagerVfs(Pager*);
  7310. SQLITE_PRIVATE sqlite3_file *sqlite3PagerFile(Pager*);
  7311. SQLITE_PRIVATE const char *sqlite3PagerJournalname(Pager*);
  7312. SQLITE_PRIVATE int sqlite3PagerNosync(Pager*);
  7313. SQLITE_PRIVATE void *sqlite3PagerTempSpace(Pager*);
  7314. SQLITE_PRIVATE int sqlite3PagerIsMemdb(Pager*);
  7315. /* Functions used to truncate the database file. */
  7316. SQLITE_PRIVATE void sqlite3PagerTruncateImage(Pager*,Pgno);
  7317. /* Functions to support testing and debugging. */
  7318. #if !defined(NDEBUG) || defined(SQLITE_TEST)
  7319. SQLITE_PRIVATE Pgno sqlite3PagerPagenumber(DbPage*);
  7320. SQLITE_PRIVATE int sqlite3PagerIswriteable(DbPage*);
  7321. #endif
  7322. #ifdef SQLITE_TEST
  7323. SQLITE_PRIVATE int *sqlite3PagerStats(Pager*);
  7324. SQLITE_PRIVATE void sqlite3PagerRefdump(Pager*);
  7325. void disable_simulated_io_errors(void);
  7326. void enable_simulated_io_errors(void);
  7327. #else
  7328. # define disable_simulated_io_errors()
  7329. # define enable_simulated_io_errors()
  7330. #endif
  7331. #endif /* _PAGER_H_ */
  7332. /************** End of pager.h ***********************************************/
  7333. /************** Continuing where we left off in sqliteInt.h ******************/
  7334. /************** Include pcache.h in the middle of sqliteInt.h ****************/
  7335. /************** Begin file pcache.h ******************************************/
  7336. /*
  7337. ** 2008 August 05
  7338. **
  7339. ** The author disclaims copyright to this source code. In place of
  7340. ** a legal notice, here is a blessing:
  7341. **
  7342. ** May you do good and not evil.
  7343. ** May you find forgiveness for yourself and forgive others.
  7344. ** May you share freely, never taking more than you give.
  7345. **
  7346. *************************************************************************
  7347. ** This header file defines the interface that the sqlite page cache
  7348. ** subsystem.
  7349. */
  7350. #ifndef _PCACHE_H_
  7351. typedef struct PgHdr PgHdr;
  7352. typedef struct PCache PCache;
  7353. /*
  7354. ** Every page in the cache is controlled by an instance of the following
  7355. ** structure.
  7356. */
  7357. struct PgHdr {
  7358. void *pData; /* Content of this page */
  7359. void *pExtra; /* Extra content */
  7360. PgHdr *pDirty; /* Transient list of dirty pages */
  7361. Pgno pgno; /* Page number for this page */
  7362. Pager *pPager; /* The pager this page is part of */
  7363. #ifdef SQLITE_CHECK_PAGES
  7364. u32 pageHash; /* Hash of page content */
  7365. #endif
  7366. u16 flags; /* PGHDR flags defined below */
  7367. /**********************************************************************
  7368. ** Elements above are public. All that follows is private to pcache.c
  7369. ** and should not be accessed by other modules.
  7370. */
  7371. i16 nRef; /* Number of users of this page */
  7372. PCache *pCache; /* Cache that owns this page */
  7373. PgHdr *pDirtyNext; /* Next element in list of dirty pages */
  7374. PgHdr *pDirtyPrev; /* Previous element in list of dirty pages */
  7375. };
  7376. /* Bit values for PgHdr.flags */
  7377. #define PGHDR_DIRTY 0x002 /* Page has changed */
  7378. #define PGHDR_NEED_SYNC 0x004 /* Fsync the rollback journal before
  7379. ** writing this page to the database */
  7380. #define PGHDR_NEED_READ 0x008 /* Content is unread */
  7381. #define PGHDR_REUSE_UNLIKELY 0x010 /* A hint that reuse is unlikely */
  7382. #define PGHDR_DONT_WRITE 0x020 /* Do not write content to disk */
  7383. /* Initialize and shutdown the page cache subsystem */
  7384. SQLITE_PRIVATE int sqlite3PcacheInitialize(void);
  7385. SQLITE_PRIVATE void sqlite3PcacheShutdown(void);
  7386. /* Page cache buffer management:
  7387. ** These routines implement SQLITE_CONFIG_PAGECACHE.
  7388. */
  7389. SQLITE_PRIVATE void sqlite3PCacheBufferSetup(void *, int sz, int n);
  7390. /* Create a new pager cache.
  7391. ** Under memory stress, invoke xStress to try to make pages clean.
  7392. ** Only clean and unpinned pages can be reclaimed.
  7393. */
  7394. SQLITE_PRIVATE void sqlite3PcacheOpen(
  7395. int szPage, /* Size of every page */
  7396. int szExtra, /* Extra space associated with each page */
  7397. int bPurgeable, /* True if pages are on backing store */
  7398. int (*xStress)(void*, PgHdr*), /* Call to try to make pages clean */
  7399. void *pStress, /* Argument to xStress */
  7400. PCache *pToInit /* Preallocated space for the PCache */
  7401. );
  7402. /* Modify the page-size after the cache has been created. */
  7403. SQLITE_PRIVATE void sqlite3PcacheSetPageSize(PCache *, int);
  7404. /* Return the size in bytes of a PCache object. Used to preallocate
  7405. ** storage space.
  7406. */
  7407. SQLITE_PRIVATE int sqlite3PcacheSize(void);
  7408. /* One release per successful fetch. Page is pinned until released.
  7409. ** Reference counted.
  7410. */
  7411. SQLITE_PRIVATE int sqlite3PcacheFetch(PCache*, Pgno, int createFlag, PgHdr**);
  7412. SQLITE_PRIVATE void sqlite3PcacheRelease(PgHdr*);
  7413. SQLITE_PRIVATE void sqlite3PcacheDrop(PgHdr*); /* Remove page from cache */
  7414. SQLITE_PRIVATE void sqlite3PcacheMakeDirty(PgHdr*); /* Make sure page is marked dirty */
  7415. SQLITE_PRIVATE void sqlite3PcacheMakeClean(PgHdr*); /* Mark a single page as clean */
  7416. SQLITE_PRIVATE void sqlite3PcacheCleanAll(PCache*); /* Mark all dirty list pages as clean */
  7417. /* Change a page number. Used by incr-vacuum. */
  7418. SQLITE_PRIVATE void sqlite3PcacheMove(PgHdr*, Pgno);
  7419. /* Remove all pages with pgno>x. Reset the cache if x==0 */
  7420. SQLITE_PRIVATE void sqlite3PcacheTruncate(PCache*, Pgno x);
  7421. /* Get a list of all dirty pages in the cache, sorted by page number */
  7422. SQLITE_PRIVATE PgHdr *sqlite3PcacheDirtyList(PCache*);
  7423. /* Reset and close the cache object */
  7424. SQLITE_PRIVATE void sqlite3PcacheClose(PCache*);
  7425. /* Clear flags from pages of the page cache */
  7426. SQLITE_PRIVATE void sqlite3PcacheClearSyncFlags(PCache *);
  7427. /* Discard the contents of the cache */
  7428. SQLITE_PRIVATE void sqlite3PcacheClear(PCache*);
  7429. /* Return the total number of outstanding page references */
  7430. SQLITE_PRIVATE int sqlite3PcacheRefCount(PCache*);
  7431. /* Increment the reference count of an existing page */
  7432. SQLITE_PRIVATE void sqlite3PcacheRef(PgHdr*);
  7433. SQLITE_PRIVATE int sqlite3PcachePageRefcount(PgHdr*);
  7434. /* Return the total number of pages stored in the cache */
  7435. SQLITE_PRIVATE int sqlite3PcachePagecount(PCache*);
  7436. #if defined(SQLITE_CHECK_PAGES) || defined(SQLITE_DEBUG)
  7437. /* Iterate through all dirty pages currently stored in the cache. This
  7438. ** interface is only available if SQLITE_CHECK_PAGES is defined when the
  7439. ** library is built.
  7440. */
  7441. SQLITE_PRIVATE void sqlite3PcacheIterateDirty(PCache *pCache, void (*xIter)(PgHdr *));
  7442. #endif
  7443. /* Set and get the suggested cache-size for the specified pager-cache.
  7444. **
  7445. ** If no global maximum is configured, then the system attempts to limit
  7446. ** the total number of pages cached by purgeable pager-caches to the sum
  7447. ** of the suggested cache-sizes.
  7448. */
  7449. SQLITE_PRIVATE void sqlite3PcacheSetCachesize(PCache *, int);
  7450. #ifdef SQLITE_TEST
  7451. SQLITE_PRIVATE int sqlite3PcacheGetCachesize(PCache *);
  7452. #endif
  7453. #ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
  7454. /* Try to return memory used by the pcache module to the main memory heap */
  7455. SQLITE_PRIVATE int sqlite3PcacheReleaseMemory(int);
  7456. #endif
  7457. #ifdef SQLITE_TEST
  7458. SQLITE_PRIVATE void sqlite3PcacheStats(int*,int*,int*,int*);
  7459. #endif
  7460. SQLITE_PRIVATE void sqlite3PCacheSetDefault(void);
  7461. #endif /* _PCACHE_H_ */
  7462. /************** End of pcache.h **********************************************/
  7463. /************** Continuing where we left off in sqliteInt.h ******************/
  7464. /************** Include os.h in the middle of sqliteInt.h ********************/
  7465. /************** Begin file os.h **********************************************/
  7466. /*
  7467. ** 2001 September 16
  7468. **
  7469. ** The author disclaims copyright to this source code. In place of
  7470. ** a legal notice, here is a blessing:
  7471. **
  7472. ** May you do good and not evil.
  7473. ** May you find forgiveness for yourself and forgive others.
  7474. ** May you share freely, never taking more than you give.
  7475. **
  7476. ******************************************************************************
  7477. **
  7478. ** This header file (together with is companion C source-code file
  7479. ** "os.c") attempt to abstract the underlying operating system so that
  7480. ** the SQLite library will work on both POSIX and windows systems.
  7481. **
  7482. ** This header file is #include-ed by sqliteInt.h and thus ends up
  7483. ** being included by every source file.
  7484. */
  7485. #ifndef _SQLITE_OS_H_
  7486. #define _SQLITE_OS_H_
  7487. /*
  7488. ** Figure out if we are dealing with Unix, Windows, or some other
  7489. ** operating system. After the following block of preprocess macros,
  7490. ** all of SQLITE_OS_UNIX, SQLITE_OS_WIN, SQLITE_OS_OS2, and SQLITE_OS_OTHER
  7491. ** will defined to either 1 or 0. One of the four will be 1. The other
  7492. ** three will be 0.
  7493. */
  7494. #if defined(SQLITE_OS_OTHER)
  7495. # if SQLITE_OS_OTHER==1
  7496. # undef SQLITE_OS_UNIX
  7497. # define SQLITE_OS_UNIX 0
  7498. # undef SQLITE_OS_WIN
  7499. # define SQLITE_OS_WIN 0
  7500. # undef SQLITE_OS_OS2
  7501. # define SQLITE_OS_OS2 0
  7502. # else
  7503. # undef SQLITE_OS_OTHER
  7504. # endif
  7505. #endif
  7506. #if !defined(SQLITE_OS_UNIX) && !defined(SQLITE_OS_OTHER)
  7507. # define SQLITE_OS_OTHER 0
  7508. # ifndef SQLITE_OS_WIN
  7509. # if defined(_WIN32) || defined(WIN32) || defined(__CYGWIN__) || defined(__MINGW32__) || defined(__BORLANDC__)
  7510. # define SQLITE_OS_WIN 1
  7511. # define SQLITE_OS_UNIX 0
  7512. # define SQLITE_OS_OS2 0
  7513. # elif defined(__EMX__) || defined(_OS2) || defined(OS2) || defined(_OS2_) || defined(__OS2__)
  7514. # define SQLITE_OS_WIN 0
  7515. # define SQLITE_OS_UNIX 0
  7516. # define SQLITE_OS_OS2 1
  7517. # else
  7518. # define SQLITE_OS_WIN 0
  7519. # define SQLITE_OS_UNIX 1
  7520. # define SQLITE_OS_OS2 0
  7521. # endif
  7522. # else
  7523. # define SQLITE_OS_UNIX 0
  7524. # define SQLITE_OS_OS2 0
  7525. # endif
  7526. #else
  7527. # ifndef SQLITE_OS_WIN
  7528. # define SQLITE_OS_WIN 0
  7529. # endif
  7530. #endif
  7531. /*
  7532. ** Determine if we are dealing with WindowsCE - which has a much
  7533. ** reduced API.
  7534. */
  7535. #if defined(_WIN32_WCE)
  7536. # define SQLITE_OS_WINCE 1
  7537. #else
  7538. # define SQLITE_OS_WINCE 0
  7539. #endif
  7540. /*
  7541. ** Define the maximum size of a temporary filename
  7542. */
  7543. #if SQLITE_OS_WIN
  7544. # include <windows.h>
  7545. # define SQLITE_TEMPNAME_SIZE (MAX_PATH+50)
  7546. #elif SQLITE_OS_OS2
  7547. # if (__GNUC__ > 3 || __GNUC__ == 3 && __GNUC_MINOR__ >= 3) && defined(OS2_HIGH_MEMORY)
  7548. # include <os2safe.h> /* has to be included before os2.h for linking to work */
  7549. # endif
  7550. # define INCL_DOSDATETIME
  7551. # define INCL_DOSFILEMGR
  7552. # define INCL_DOSERRORS
  7553. # define INCL_DOSMISC
  7554. # define INCL_DOSPROCESS
  7555. # define INCL_DOSMODULEMGR
  7556. # define INCL_DOSSEMAPHORES
  7557. # include <os2.h>
  7558. # include <uconv.h>
  7559. # define SQLITE_TEMPNAME_SIZE (CCHMAXPATHCOMP)
  7560. #else
  7561. # define SQLITE_TEMPNAME_SIZE 200
  7562. #endif
  7563. /* If the SET_FULLSYNC macro is not defined above, then make it
  7564. ** a no-op
  7565. */
  7566. #ifndef SET_FULLSYNC
  7567. # define SET_FULLSYNC(x,y)
  7568. #endif
  7569. /*
  7570. ** The default size of a disk sector
  7571. */
  7572. #ifndef SQLITE_DEFAULT_SECTOR_SIZE
  7573. # define SQLITE_DEFAULT_SECTOR_SIZE 512
  7574. #endif
  7575. /*
  7576. ** Temporary files are named starting with this prefix followed by 16 random
  7577. ** alphanumeric characters, and no file extension. They are stored in the
  7578. ** OS's standard temporary file directory, and are deleted prior to exit.
  7579. ** If sqlite is being embedded in another program, you may wish to change the
  7580. ** prefix to reflect your program's name, so that if your program exits
  7581. ** prematurely, old temporary files can be easily identified. This can be done
  7582. ** using -DSQLITE_TEMP_FILE_PREFIX=myprefix_ on the compiler command line.
  7583. **
  7584. ** 2006-10-31: The default prefix used to be "sqlite_". But then
  7585. ** Mcafee started using SQLite in their anti-virus product and it
  7586. ** started putting files with the "sqlite" name in the c:/temp folder.
  7587. ** This annoyed many windows users. Those users would then do a
  7588. ** Google search for "sqlite", find the telephone numbers of the
  7589. ** developers and call to wake them up at night and complain.
  7590. ** For this reason, the default name prefix is changed to be "sqlite"
  7591. ** spelled backwards. So the temp files are still identified, but
  7592. ** anybody smart enough to figure out the code is also likely smart
  7593. ** enough to know that calling the developer will not help get rid
  7594. ** of the file.
  7595. */
  7596. #ifndef SQLITE_TEMP_FILE_PREFIX
  7597. # define SQLITE_TEMP_FILE_PREFIX "etilqs_"
  7598. #endif
  7599. /*
  7600. ** The following values may be passed as the second argument to
  7601. ** sqlite3OsLock(). The various locks exhibit the following semantics:
  7602. **
  7603. ** SHARED: Any number of processes may hold a SHARED lock simultaneously.
  7604. ** RESERVED: A single process may hold a RESERVED lock on a file at
  7605. ** any time. Other processes may hold and obtain new SHARED locks.
  7606. ** PENDING: A single process may hold a PENDING lock on a file at
  7607. ** any one time. Existing SHARED locks may persist, but no new
  7608. ** SHARED locks may be obtained by other processes.
  7609. ** EXCLUSIVE: An EXCLUSIVE lock precludes all other locks.
  7610. **
  7611. ** PENDING_LOCK may not be passed directly to sqlite3OsLock(). Instead, a
  7612. ** process that requests an EXCLUSIVE lock may actually obtain a PENDING
  7613. ** lock. This can be upgraded to an EXCLUSIVE lock by a subsequent call to
  7614. ** sqlite3OsLock().
  7615. */
  7616. #define NO_LOCK 0
  7617. #define SHARED_LOCK 1
  7618. #define RESERVED_LOCK 2
  7619. #define PENDING_LOCK 3
  7620. #define EXCLUSIVE_LOCK 4
  7621. /*
  7622. ** File Locking Notes: (Mostly about windows but also some info for Unix)
  7623. **
  7624. ** We cannot use LockFileEx() or UnlockFileEx() on Win95/98/ME because
  7625. ** those functions are not available. So we use only LockFile() and
  7626. ** UnlockFile().
  7627. **
  7628. ** LockFile() prevents not just writing but also reading by other processes.
  7629. ** A SHARED_LOCK is obtained by locking a single randomly-chosen
  7630. ** byte out of a specific range of bytes. The lock byte is obtained at
  7631. ** random so two separate readers can probably access the file at the
  7632. ** same time, unless they are unlucky and choose the same lock byte.
  7633. ** An EXCLUSIVE_LOCK is obtained by locking all bytes in the range.
  7634. ** There can only be one writer. A RESERVED_LOCK is obtained by locking
  7635. ** a single byte of the file that is designated as the reserved lock byte.
  7636. ** A PENDING_LOCK is obtained by locking a designated byte different from
  7637. ** the RESERVED_LOCK byte.
  7638. **
  7639. ** On WinNT/2K/XP systems, LockFileEx() and UnlockFileEx() are available,
  7640. ** which means we can use reader/writer locks. When reader/writer locks
  7641. ** are used, the lock is placed on the same range of bytes that is used
  7642. ** for probabilistic locking in Win95/98/ME. Hence, the locking scheme
  7643. ** will support two or more Win95 readers or two or more WinNT readers.
  7644. ** But a single Win95 reader will lock out all WinNT readers and a single
  7645. ** WinNT reader will lock out all other Win95 readers.
  7646. **
  7647. ** The following #defines specify the range of bytes used for locking.
  7648. ** SHARED_SIZE is the number of bytes available in the pool from which
  7649. ** a random byte is selected for a shared lock. The pool of bytes for
  7650. ** shared locks begins at SHARED_FIRST.
  7651. **
  7652. ** The same locking strategy and
  7653. ** byte ranges are used for Unix. This leaves open the possiblity of having
  7654. ** clients on win95, winNT, and unix all talking to the same shared file
  7655. ** and all locking correctly. To do so would require that samba (or whatever
  7656. ** tool is being used for file sharing) implements locks correctly between
  7657. ** windows and unix. I'm guessing that isn't likely to happen, but by
  7658. ** using the same locking range we are at least open to the possibility.
  7659. **
  7660. ** Locking in windows is manditory. For this reason, we cannot store
  7661. ** actual data in the bytes used for locking. The pager never allocates
  7662. ** the pages involved in locking therefore. SHARED_SIZE is selected so
  7663. ** that all locks will fit on a single page even at the minimum page size.
  7664. ** PENDING_BYTE defines the beginning of the locks. By default PENDING_BYTE
  7665. ** is set high so that we don't have to allocate an unused page except
  7666. ** for very large databases. But one should test the page skipping logic
  7667. ** by setting PENDING_BYTE low and running the entire regression suite.
  7668. **
  7669. ** Changing the value of PENDING_BYTE results in a subtly incompatible
  7670. ** file format. Depending on how it is changed, you might not notice
  7671. ** the incompatibility right away, even running a full regression test.
  7672. ** The default location of PENDING_BYTE is the first byte past the
  7673. ** 1GB boundary.
  7674. **
  7675. */
  7676. #define PENDING_BYTE sqlite3PendingByte
  7677. #define RESERVED_BYTE (PENDING_BYTE+1)
  7678. #define SHARED_FIRST (PENDING_BYTE+2)
  7679. #define SHARED_SIZE 510
  7680. /*
  7681. ** Wrapper around OS specific sqlite3_os_init() function.
  7682. */
  7683. SQLITE_PRIVATE int sqlite3OsInit(void);
  7684. /*
  7685. ** Functions for accessing sqlite3_file methods
  7686. */
  7687. SQLITE_PRIVATE int sqlite3OsClose(sqlite3_file*);
  7688. SQLITE_PRIVATE int sqlite3OsRead(sqlite3_file*, void*, int amt, i64 offset);
  7689. SQLITE_PRIVATE int sqlite3OsWrite(sqlite3_file*, const void*, int amt, i64 offset);
  7690. SQLITE_PRIVATE int sqlite3OsTruncate(sqlite3_file*, i64 size);
  7691. SQLITE_PRIVATE int sqlite3OsSync(sqlite3_file*, int);
  7692. SQLITE_PRIVATE int sqlite3OsFileSize(sqlite3_file*, i64 *pSize);
  7693. SQLITE_PRIVATE int sqlite3OsLock(sqlite3_file*, int);
  7694. SQLITE_PRIVATE int sqlite3OsUnlock(sqlite3_file*, int);
  7695. SQLITE_PRIVATE int sqlite3OsCheckReservedLock(sqlite3_file *id, int *pResOut);
  7696. SQLITE_PRIVATE int sqlite3OsFileControl(sqlite3_file*,int,void*);
  7697. #define SQLITE_FCNTL_DB_UNCHANGED 0xca093fa0
  7698. SQLITE_PRIVATE int sqlite3OsSectorSize(sqlite3_file *id);
  7699. SQLITE_PRIVATE int sqlite3OsDeviceCharacteristics(sqlite3_file *id);
  7700. /*
  7701. ** Functions for accessing sqlite3_vfs methods
  7702. */
  7703. SQLITE_PRIVATE int sqlite3OsOpen(sqlite3_vfs *, const char *, sqlite3_file*, int, int *);
  7704. SQLITE_PRIVATE int sqlite3OsDelete(sqlite3_vfs *, const char *, int);
  7705. SQLITE_PRIVATE int sqlite3OsAccess(sqlite3_vfs *, const char *, int, int *pResOut);
  7706. SQLITE_PRIVATE int sqlite3OsFullPathname(sqlite3_vfs *, const char *, int, char *);
  7707. #ifndef SQLITE_OMIT_LOAD_EXTENSION
  7708. SQLITE_PRIVATE void *sqlite3OsDlOpen(sqlite3_vfs *, const char *);
  7709. SQLITE_PRIVATE void sqlite3OsDlError(sqlite3_vfs *, int, char *);
  7710. SQLITE_PRIVATE void (*sqlite3OsDlSym(sqlite3_vfs *, void *, const char *))(void);
  7711. SQLITE_PRIVATE void sqlite3OsDlClose(sqlite3_vfs *, void *);
  7712. #endif /* SQLITE_OMIT_LOAD_EXTENSION */
  7713. SQLITE_PRIVATE int sqlite3OsRandomness(sqlite3_vfs *, int, char *);
  7714. SQLITE_PRIVATE int sqlite3OsSleep(sqlite3_vfs *, int);
  7715. SQLITE_PRIVATE int sqlite3OsCurrentTime(sqlite3_vfs *, double*);
  7716. /*
  7717. ** Convenience functions for opening and closing files using
  7718. ** sqlite3_malloc() to obtain space for the file-handle structure.
  7719. */
  7720. SQLITE_PRIVATE int sqlite3OsOpenMalloc(sqlite3_vfs *, const char *, sqlite3_file **, int,int*);
  7721. SQLITE_PRIVATE int sqlite3OsCloseFree(sqlite3_file *);
  7722. #endif /* _SQLITE_OS_H_ */
  7723. /************** End of os.h **************************************************/
  7724. /************** Continuing where we left off in sqliteInt.h ******************/
  7725. /************** Include mutex.h in the middle of sqliteInt.h *****************/
  7726. /************** Begin file mutex.h *******************************************/
  7727. /*
  7728. ** 2007 August 28
  7729. **
  7730. ** The author disclaims copyright to this source code. In place of
  7731. ** a legal notice, here is a blessing:
  7732. **
  7733. ** May you do good and not evil.
  7734. ** May you find forgiveness for yourself and forgive others.
  7735. ** May you share freely, never taking more than you give.
  7736. **
  7737. *************************************************************************
  7738. **
  7739. ** This file contains the common header for all mutex implementations.
  7740. ** The sqliteInt.h header #includes this file so that it is available
  7741. ** to all source files. We break it out in an effort to keep the code
  7742. ** better organized.
  7743. **
  7744. ** NOTE: source files should *not* #include this header file directly.
  7745. ** Source files should #include the sqliteInt.h file and let that file
  7746. ** include this one indirectly.
  7747. */
  7748. /*
  7749. ** Figure out what version of the code to use. The choices are
  7750. **
  7751. ** SQLITE_MUTEX_OMIT No mutex logic. Not even stubs. The
  7752. ** mutexes implemention cannot be overridden
  7753. ** at start-time.
  7754. **
  7755. ** SQLITE_MUTEX_NOOP For single-threaded applications. No
  7756. ** mutual exclusion is provided. But this
  7757. ** implementation can be overridden at
  7758. ** start-time.
  7759. **
  7760. ** SQLITE_MUTEX_PTHREADS For multi-threaded applications on Unix.
  7761. **
  7762. ** SQLITE_MUTEX_W32 For multi-threaded applications on Win32.
  7763. **
  7764. ** SQLITE_MUTEX_OS2 For multi-threaded applications on OS/2.
  7765. */
  7766. #if !SQLITE_THREADSAFE
  7767. # define SQLITE_MUTEX_OMIT
  7768. #endif
  7769. #if SQLITE_THREADSAFE && !defined(SQLITE_MUTEX_NOOP)
  7770. # if SQLITE_OS_UNIX
  7771. # define SQLITE_MUTEX_PTHREADS
  7772. # elif SQLITE_OS_WIN
  7773. # define SQLITE_MUTEX_W32
  7774. # elif SQLITE_OS_OS2
  7775. # define SQLITE_MUTEX_OS2
  7776. # else
  7777. # define SQLITE_MUTEX_NOOP
  7778. # endif
  7779. #endif
  7780. #ifdef SQLITE_MUTEX_OMIT
  7781. /*
  7782. ** If this is a no-op implementation, implement everything as macros.
  7783. */
  7784. #define sqlite3_mutex_alloc(X) ((sqlite3_mutex*)8)
  7785. #define sqlite3_mutex_free(X)
  7786. #define sqlite3_mutex_enter(X)
  7787. #define sqlite3_mutex_try(X) SQLITE_OK
  7788. #define sqlite3_mutex_leave(X)
  7789. #define sqlite3_mutex_held(X) 1
  7790. #define sqlite3_mutex_notheld(X) 1
  7791. #define sqlite3MutexAlloc(X) ((sqlite3_mutex*)8)
  7792. #define sqlite3MutexInit() SQLITE_OK
  7793. #define sqlite3MutexEnd()
  7794. #endif /* defined(SQLITE_MUTEX_OMIT) */
  7795. /************** End of mutex.h ***********************************************/
  7796. /************** Continuing where we left off in sqliteInt.h ******************/
  7797. /*
  7798. ** Each database file to be accessed by the system is an instance
  7799. ** of the following structure. There are normally two of these structures
  7800. ** in the sqlite.aDb[] array. aDb[0] is the main database file and
  7801. ** aDb[1] is the database file used to hold temporary tables. Additional
  7802. ** databases may be attached.
  7803. */
  7804. struct Db {
  7805. char *zName; /* Name of this database */
  7806. Btree *pBt; /* The B*Tree structure for this database file */
  7807. u8 inTrans; /* 0: not writable. 1: Transaction. 2: Checkpoint */
  7808. u8 safety_level; /* How aggressive at syncing data to disk */
  7809. Schema *pSchema; /* Pointer to database schema (possibly shared) */
  7810. };
  7811. /*
  7812. ** An instance of the following structure stores a database schema.
  7813. **
  7814. ** If there are no virtual tables configured in this schema, the
  7815. ** Schema.db variable is set to NULL. After the first virtual table
  7816. ** has been added, it is set to point to the database connection
  7817. ** used to create the connection. Once a virtual table has been
  7818. ** added to the Schema structure and the Schema.db variable populated,
  7819. ** only that database connection may use the Schema to prepare
  7820. ** statements.
  7821. */
  7822. struct Schema {
  7823. int schema_cookie; /* Database schema version number for this file */
  7824. Hash tblHash; /* All tables indexed by name */
  7825. Hash idxHash; /* All (named) indices indexed by name */
  7826. Hash trigHash; /* All triggers indexed by name */
  7827. Hash fkeyHash; /* All foreign keys by referenced table name */
  7828. Table *pSeqTab; /* The sqlite_sequence table used by AUTOINCREMENT */
  7829. u8 file_format; /* Schema format version for this file */
  7830. u8 enc; /* Text encoding used by this database */
  7831. u16 flags; /* Flags associated with this schema */
  7832. int cache_size; /* Number of pages to use in the cache */
  7833. #ifndef SQLITE_OMIT_VIRTUALTABLE
  7834. sqlite3 *db; /* "Owner" connection. See comment above */
  7835. #endif
  7836. };
  7837. /*
  7838. ** These macros can be used to test, set, or clear bits in the
  7839. ** Db.flags field.
  7840. */
  7841. #define DbHasProperty(D,I,P) (((D)->aDb[I].pSchema->flags&(P))==(P))
  7842. #define DbHasAnyProperty(D,I,P) (((D)->aDb[I].pSchema->flags&(P))!=0)
  7843. #define DbSetProperty(D,I,P) (D)->aDb[I].pSchema->flags|=(P)
  7844. #define DbClearProperty(D,I,P) (D)->aDb[I].pSchema->flags&=~(P)
  7845. /*
  7846. ** Allowed values for the DB.flags field.
  7847. **
  7848. ** The DB_SchemaLoaded flag is set after the database schema has been
  7849. ** read into internal hash tables.
  7850. **
  7851. ** DB_UnresetViews means that one or more views have column names that
  7852. ** have been filled out. If the schema changes, these column names might
  7853. ** changes and so the view will need to be reset.
  7854. */
  7855. #define DB_SchemaLoaded 0x0001 /* The schema has been loaded */
  7856. #define DB_UnresetViews 0x0002 /* Some views have defined column names */
  7857. #define DB_Empty 0x0004 /* The file is empty (length 0 bytes) */
  7858. /*
  7859. ** The number of different kinds of things that can be limited
  7860. ** using the sqlite3_limit() interface.
  7861. */
  7862. #define SQLITE_N_LIMIT (SQLITE_LIMIT_TRIGGER_DEPTH+1)
  7863. /*
  7864. ** Lookaside malloc is a set of fixed-size buffers that can be used
  7865. ** to satisfy small transient memory allocation requests for objects
  7866. ** associated with a particular database connection. The use of
  7867. ** lookaside malloc provides a significant performance enhancement
  7868. ** (approx 10%) by avoiding numerous malloc/free requests while parsing
  7869. ** SQL statements.
  7870. **
  7871. ** The Lookaside structure holds configuration information about the
  7872. ** lookaside malloc subsystem. Each available memory allocation in
  7873. ** the lookaside subsystem is stored on a linked list of LookasideSlot
  7874. ** objects.
  7875. **
  7876. ** Lookaside allocations are only allowed for objects that are associated
  7877. ** with a particular database connection. Hence, schema information cannot
  7878. ** be stored in lookaside because in shared cache mode the schema information
  7879. ** is shared by multiple database connections. Therefore, while parsing
  7880. ** schema information, the Lookaside.bEnabled flag is cleared so that
  7881. ** lookaside allocations are not used to construct the schema objects.
  7882. */
  7883. struct Lookaside {
  7884. u16 sz; /* Size of each buffer in bytes */
  7885. u8 bEnabled; /* False to disable new lookaside allocations */
  7886. u8 bMalloced; /* True if pStart obtained from sqlite3_malloc() */
  7887. int nOut; /* Number of buffers currently checked out */
  7888. int mxOut; /* Highwater mark for nOut */
  7889. LookasideSlot *pFree; /* List of available buffers */
  7890. void *pStart; /* First byte of available memory space */
  7891. void *pEnd; /* First byte past end of available space */
  7892. };
  7893. struct LookasideSlot {
  7894. LookasideSlot *pNext; /* Next buffer in the list of free buffers */
  7895. };
  7896. /*
  7897. ** A hash table for function definitions.
  7898. **
  7899. ** Hash each FuncDef structure into one of the FuncDefHash.a[] slots.
  7900. ** Collisions are on the FuncDef.pHash chain.
  7901. */
  7902. struct FuncDefHash {
  7903. FuncDef *a[23]; /* Hash table for functions */
  7904. };
  7905. /*
  7906. ** Each database is an instance of the following structure.
  7907. **
  7908. ** The sqlite.lastRowid records the last insert rowid generated by an
  7909. ** insert statement. Inserts on views do not affect its value. Each
  7910. ** trigger has its own context, so that lastRowid can be updated inside
  7911. ** triggers as usual. The previous value will be restored once the trigger
  7912. ** exits. Upon entering a before or instead of trigger, lastRowid is no
  7913. ** longer (since after version 2.8.12) reset to -1.
  7914. **
  7915. ** The sqlite.nChange does not count changes within triggers and keeps no
  7916. ** context. It is reset at start of sqlite3_exec.
  7917. ** The sqlite.lsChange represents the number of changes made by the last
  7918. ** insert, update, or delete statement. It remains constant throughout the
  7919. ** length of a statement and is then updated by OP_SetCounts. It keeps a
  7920. ** context stack just like lastRowid so that the count of changes
  7921. ** within a trigger is not seen outside the trigger. Changes to views do not
  7922. ** affect the value of lsChange.
  7923. ** The sqlite.csChange keeps track of the number of current changes (since
  7924. ** the last statement) and is used to update sqlite_lsChange.
  7925. **
  7926. ** The member variables sqlite.errCode, sqlite.zErrMsg and sqlite.zErrMsg16
  7927. ** store the most recent error code and, if applicable, string. The
  7928. ** internal function sqlite3Error() is used to set these variables
  7929. ** consistently.
  7930. */
  7931. struct sqlite3 {
  7932. sqlite3_vfs *pVfs; /* OS Interface */
  7933. int nDb; /* Number of backends currently in use */
  7934. Db *aDb; /* All backends */
  7935. int flags; /* Miscellaneous flags. See below */
  7936. int openFlags; /* Flags passed to sqlite3_vfs.xOpen() */
  7937. int errCode; /* Most recent error code (SQLITE_*) */
  7938. int errMask; /* & result codes with this before returning */
  7939. u8 autoCommit; /* The auto-commit flag. */
  7940. u8 temp_store; /* 1: file 2: memory 0: default */
  7941. u8 mallocFailed; /* True if we have seen a malloc failure */
  7942. u8 dfltLockMode; /* Default locking-mode for attached dbs */
  7943. u8 dfltJournalMode; /* Default journal mode for attached dbs */
  7944. signed char nextAutovac; /* Autovac setting after VACUUM if >=0 */
  7945. int nextPagesize; /* Pagesize after VACUUM if >0 */
  7946. int nTable; /* Number of tables in the database */
  7947. CollSeq *pDfltColl; /* The default collating sequence (BINARY) */
  7948. i64 lastRowid; /* ROWID of most recent insert (see above) */
  7949. u32 magic; /* Magic number for detect library misuse */
  7950. int nChange; /* Value returned by sqlite3_changes() */
  7951. int nTotalChange; /* Value returned by sqlite3_total_changes() */
  7952. sqlite3_mutex *mutex; /* Connection mutex */
  7953. int aLimit[SQLITE_N_LIMIT]; /* Limits */
  7954. struct sqlite3InitInfo { /* Information used during initialization */
  7955. int iDb; /* When back is being initialized */
  7956. int newTnum; /* Rootpage of table being initialized */
  7957. u8 busy; /* TRUE if currently initializing */
  7958. u8 orphanTrigger; /* Last statement is orphaned TEMP trigger */
  7959. } init;
  7960. int nExtension; /* Number of loaded extensions */
  7961. void **aExtension; /* Array of shared library handles */
  7962. struct Vdbe *pVdbe; /* List of active virtual machines */
  7963. int activeVdbeCnt; /* Number of VDBEs currently executing */
  7964. int writeVdbeCnt; /* Number of active VDBEs that are writing */
  7965. void (*xTrace)(void*,const char*); /* Trace function */
  7966. void *pTraceArg; /* Argument to the trace function */
  7967. void (*xProfile)(void*,const char*,u64); /* Profiling function */
  7968. void *pProfileArg; /* Argument to profile function */
  7969. void *pCommitArg; /* Argument to xCommitCallback() */
  7970. int (*xCommitCallback)(void*); /* Invoked at every commit. */
  7971. void *pRollbackArg; /* Argument to xRollbackCallback() */
  7972. void (*xRollbackCallback)(void*); /* Invoked at every commit. */
  7973. void *pUpdateArg;
  7974. void (*xUpdateCallback)(void*,int, const char*,const char*,sqlite_int64);
  7975. void(*xCollNeeded)(void*,sqlite3*,int eTextRep,const char*);
  7976. void(*xCollNeeded16)(void*,sqlite3*,int eTextRep,const void*);
  7977. void *pCollNeededArg;
  7978. sqlite3_value *pErr; /* Most recent error message */
  7979. char *zErrMsg; /* Most recent error message (UTF-8 encoded) */
  7980. char *zErrMsg16; /* Most recent error message (UTF-16 encoded) */
  7981. union {
  7982. volatile int isInterrupted; /* True if sqlite3_interrupt has been called */
  7983. double notUsed1; /* Spacer */
  7984. } u1;
  7985. Lookaside lookaside; /* Lookaside malloc configuration */
  7986. #ifndef SQLITE_OMIT_AUTHORIZATION
  7987. int (*xAuth)(void*,int,const char*,const char*,const char*,const char*);
  7988. /* Access authorization function */
  7989. void *pAuthArg; /* 1st argument to the access auth function */
  7990. #endif
  7991. #ifndef SQLITE_OMIT_PROGRESS_CALLBACK
  7992. int (*xProgress)(void *); /* The progress callback */
  7993. void *pProgressArg; /* Argument to the progress callback */
  7994. int nProgressOps; /* Number of opcodes for progress callback */
  7995. #endif
  7996. #ifndef SQLITE_OMIT_VIRTUALTABLE
  7997. Hash aModule; /* populated by sqlite3_create_module() */
  7998. Table *pVTab; /* vtab with active Connect/Create method */
  7999. VTable **aVTrans; /* Virtual tables with open transactions */
  8000. int nVTrans; /* Allocated size of aVTrans */
  8001. VTable *pDisconnect; /* Disconnect these in next sqlite3_prepare() */
  8002. #endif
  8003. FuncDefHash aFunc; /* Hash table of connection functions */
  8004. Hash aCollSeq; /* All collating sequences */
  8005. BusyHandler busyHandler; /* Busy callback */
  8006. int busyTimeout; /* Busy handler timeout, in msec */
  8007. Db aDbStatic[2]; /* Static space for the 2 default backends */
  8008. Savepoint *pSavepoint; /* List of active savepoints */
  8009. int nSavepoint; /* Number of non-transaction savepoints */
  8010. int nStatement; /* Number of nested statement-transactions */
  8011. u8 isTransactionSavepoint; /* True if the outermost savepoint is a TS */
  8012. i64 nDeferredCons; /* Net deferred constraints this transaction. */
  8013. #ifdef SQLITE_ENABLE_UNLOCK_NOTIFY
  8014. /* The following variables are all protected by the STATIC_MASTER
  8015. ** mutex, not by sqlite3.mutex. They are used by code in notify.c.
  8016. **
  8017. ** When X.pUnlockConnection==Y, that means that X is waiting for Y to
  8018. ** unlock so that it can proceed.
  8019. **
  8020. ** When X.pBlockingConnection==Y, that means that something that X tried
  8021. ** tried to do recently failed with an SQLITE_LOCKED error due to locks
  8022. ** held by Y.
  8023. */
  8024. sqlite3 *pBlockingConnection; /* Connection that caused SQLITE_LOCKED */
  8025. sqlite3 *pUnlockConnection; /* Connection to watch for unlock */
  8026. void *pUnlockArg; /* Argument to xUnlockNotify */
  8027. void (*xUnlockNotify)(void **, int); /* Unlock notify callback */
  8028. sqlite3 *pNextBlocked; /* Next in list of all blocked connections */
  8029. #endif
  8030. };
  8031. /*
  8032. ** A macro to discover the encoding of a database.
  8033. */
  8034. #define ENC(db) ((db)->aDb[0].pSchema->enc)
  8035. /*
  8036. ** Possible values for the sqlite3.flags.
  8037. */
  8038. #define SQLITE_VdbeTrace 0x00000100 /* True to trace VDBE execution */
  8039. #define SQLITE_InternChanges 0x00000200 /* Uncommitted Hash table changes */
  8040. #define SQLITE_FullColNames 0x00000400 /* Show full column names on SELECT */
  8041. #define SQLITE_ShortColNames 0x00000800 /* Show short columns names */
  8042. #define SQLITE_CountRows 0x00001000 /* Count rows changed by INSERT, */
  8043. /* DELETE, or UPDATE and return */
  8044. /* the count using a callback. */
  8045. #define SQLITE_NullCallback 0x00002000 /* Invoke the callback once if the */
  8046. /* result set is empty */
  8047. #define SQLITE_SqlTrace 0x00004000 /* Debug print SQL as it executes */
  8048. #define SQLITE_VdbeListing 0x00008000 /* Debug listings of VDBE programs */
  8049. #define SQLITE_WriteSchema 0x00010000 /* OK to update SQLITE_MASTER */
  8050. #define SQLITE_NoReadlock 0x00020000 /* Readlocks are omitted when
  8051. ** accessing read-only databases */
  8052. #define SQLITE_IgnoreChecks 0x00040000 /* Do not enforce check constraints */
  8053. #define SQLITE_ReadUncommitted 0x0080000 /* For shared-cache mode */
  8054. #define SQLITE_LegacyFileFmt 0x00100000 /* Create new databases in format 1 */
  8055. #define SQLITE_FullFSync 0x00200000 /* Use full fsync on the backend */
  8056. #define SQLITE_LoadExtension 0x00400000 /* Enable load_extension */
  8057. #define SQLITE_RecoveryMode 0x00800000 /* Ignore schema errors */
  8058. #define SQLITE_ReverseOrder 0x01000000 /* Reverse unordered SELECTs */
  8059. #define SQLITE_RecTriggers 0x02000000 /* Enable recursive triggers */
  8060. #define SQLITE_ForeignKeys 0x04000000 /* Enforce foreign key constraints */
  8061. /*
  8062. ** Bits of the sqlite3.flags field that are used by the
  8063. ** sqlite3_test_control(SQLITE_TESTCTRL_OPTIMIZATIONS,...) interface.
  8064. ** These must be the low-order bits of the flags field.
  8065. */
  8066. #define SQLITE_QueryFlattener 0x01 /* Disable query flattening */
  8067. #define SQLITE_ColumnCache 0x02 /* Disable the column cache */
  8068. #define SQLITE_IndexSort 0x04 /* Disable indexes for sorting */
  8069. #define SQLITE_IndexSearch 0x08 /* Disable indexes for searching */
  8070. #define SQLITE_IndexCover 0x10 /* Disable index covering table */
  8071. #define SQLITE_OptMask 0x1f /* Mask of all disablable opts */
  8072. /*
  8073. ** Possible values for the sqlite.magic field.
  8074. ** The numbers are obtained at random and have no special meaning, other
  8075. ** than being distinct from one another.
  8076. */
  8077. #define SQLITE_MAGIC_OPEN 0xa029a697 /* Database is open */
  8078. #define SQLITE_MAGIC_CLOSED 0x9f3c2d33 /* Database is closed */
  8079. #define SQLITE_MAGIC_SICK 0x4b771290 /* Error and awaiting close */
  8080. #define SQLITE_MAGIC_BUSY 0xf03b7906 /* Database currently in use */
  8081. #define SQLITE_MAGIC_ERROR 0xb5357930 /* An SQLITE_MISUSE error occurred */
  8082. /*
  8083. ** Each SQL function is defined by an instance of the following
  8084. ** structure. A pointer to this structure is stored in the sqlite.aFunc
  8085. ** hash table. When multiple functions have the same name, the hash table
  8086. ** points to a linked list of these structures.
  8087. */
  8088. struct FuncDef {
  8089. i16 nArg; /* Number of arguments. -1 means unlimited */
  8090. u8 iPrefEnc; /* Preferred text encoding (SQLITE_UTF8, 16LE, 16BE) */
  8091. u8 flags; /* Some combination of SQLITE_FUNC_* */
  8092. void *pUserData; /* User data parameter */
  8093. FuncDef *pNext; /* Next function with same name */
  8094. void (*xFunc)(sqlite3_context*,int,sqlite3_value**); /* Regular function */
  8095. void (*xStep)(sqlite3_context*,int,sqlite3_value**); /* Aggregate step */
  8096. void (*xFinalize)(sqlite3_context*); /* Aggregate finalizer */
  8097. char *zName; /* SQL name of the function. */
  8098. FuncDef *pHash; /* Next with a different name but the same hash */
  8099. };
  8100. /*
  8101. ** Possible values for FuncDef.flags
  8102. */
  8103. #define SQLITE_FUNC_LIKE 0x01 /* Candidate for the LIKE optimization */
  8104. #define SQLITE_FUNC_CASE 0x02 /* Case-sensitive LIKE-type function */
  8105. #define SQLITE_FUNC_EPHEM 0x04 /* Ephemeral. Delete with VDBE */
  8106. #define SQLITE_FUNC_NEEDCOLL 0x08 /* sqlite3GetFuncCollSeq() might be called */
  8107. #define SQLITE_FUNC_PRIVATE 0x10 /* Allowed for internal use only */
  8108. #define SQLITE_FUNC_COUNT 0x20 /* Built-in count(*) aggregate */
  8109. #define SQLITE_FUNC_COALESCE 0x40 /* Built-in coalesce() or ifnull() function */
  8110. /*
  8111. ** The following three macros, FUNCTION(), LIKEFUNC() and AGGREGATE() are
  8112. ** used to create the initializers for the FuncDef structures.
  8113. **
  8114. ** FUNCTION(zName, nArg, iArg, bNC, xFunc)
  8115. ** Used to create a scalar function definition of a function zName
  8116. ** implemented by C function xFunc that accepts nArg arguments. The
  8117. ** value passed as iArg is cast to a (void*) and made available
  8118. ** as the user-data (sqlite3_user_data()) for the function. If
  8119. ** argument bNC is true, then the SQLITE_FUNC_NEEDCOLL flag is set.
  8120. **
  8121. ** AGGREGATE(zName, nArg, iArg, bNC, xStep, xFinal)
  8122. ** Used to create an aggregate function definition implemented by
  8123. ** the C functions xStep and xFinal. The first four parameters
  8124. ** are interpreted in the same way as the first 4 parameters to
  8125. ** FUNCTION().
  8126. **
  8127. ** LIKEFUNC(zName, nArg, pArg, flags)
  8128. ** Used to create a scalar function definition of a function zName
  8129. ** that accepts nArg arguments and is implemented by a call to C
  8130. ** function likeFunc. Argument pArg is cast to a (void *) and made
  8131. ** available as the function user-data (sqlite3_user_data()). The
  8132. ** FuncDef.flags variable is set to the value passed as the flags
  8133. ** parameter.
  8134. */
  8135. #define FUNCTION(zName, nArg, iArg, bNC, xFunc) \
  8136. {nArg, SQLITE_UTF8, bNC*SQLITE_FUNC_NEEDCOLL, \
  8137. SQLITE_INT_TO_PTR(iArg), 0, xFunc, 0, 0, #zName, 0}
  8138. #define STR_FUNCTION(zName, nArg, pArg, bNC, xFunc) \
  8139. {nArg, SQLITE_UTF8, bNC*SQLITE_FUNC_NEEDCOLL, \
  8140. pArg, 0, xFunc, 0, 0, #zName, 0}
  8141. #define LIKEFUNC(zName, nArg, arg, flags) \
  8142. {nArg, SQLITE_UTF8, flags, (void *)arg, 0, likeFunc, 0, 0, #zName, 0}
  8143. #define AGGREGATE(zName, nArg, arg, nc, xStep, xFinal) \
  8144. {nArg, SQLITE_UTF8, nc*SQLITE_FUNC_NEEDCOLL, \
  8145. SQLITE_INT_TO_PTR(arg), 0, 0, xStep,xFinal,#zName,0}
  8146. /*
  8147. ** All current savepoints are stored in a linked list starting at
  8148. ** sqlite3.pSavepoint. The first element in the list is the most recently
  8149. ** opened savepoint. Savepoints are added to the list by the vdbe
  8150. ** OP_Savepoint instruction.
  8151. */
  8152. struct Savepoint {
  8153. char *zName; /* Savepoint name (nul-terminated) */
  8154. i64 nDeferredCons; /* Number of deferred fk violations */
  8155. Savepoint *pNext; /* Parent savepoint (if any) */
  8156. };
  8157. /*
  8158. ** The following are used as the second parameter to sqlite3Savepoint(),
  8159. ** and as the P1 argument to the OP_Savepoint instruction.
  8160. */
  8161. #define SAVEPOINT_BEGIN 0
  8162. #define SAVEPOINT_RELEASE 1
  8163. #define SAVEPOINT_ROLLBACK 2
  8164. /*
  8165. ** Each SQLite module (virtual table definition) is defined by an
  8166. ** instance of the following structure, stored in the sqlite3.aModule
  8167. ** hash table.
  8168. */
  8169. struct Module {
  8170. const sqlite3_module *pModule; /* Callback pointers */
  8171. const char *zName; /* Name passed to create_module() */
  8172. void *pAux; /* pAux passed to create_module() */
  8173. void (*xDestroy)(void *); /* Module destructor function */
  8174. };
  8175. /*
  8176. ** information about each column of an SQL table is held in an instance
  8177. ** of this structure.
  8178. */
  8179. struct Column {
  8180. char *zName; /* Name of this column */
  8181. Expr *pDflt; /* Default value of this column */
  8182. char *zDflt; /* Original text of the default value */
  8183. char *zType; /* Data type for this column */
  8184. char *zColl; /* Collating sequence. If NULL, use the default */
  8185. u8 notNull; /* True if there is a NOT NULL constraint */
  8186. u8 isPrimKey; /* True if this column is part of the PRIMARY KEY */
  8187. char affinity; /* One of the SQLITE_AFF_... values */
  8188. #ifndef SQLITE_OMIT_VIRTUALTABLE
  8189. u8 isHidden; /* True if this column is 'hidden' */
  8190. #endif
  8191. };
  8192. /*
  8193. ** A "Collating Sequence" is defined by an instance of the following
  8194. ** structure. Conceptually, a collating sequence consists of a name and
  8195. ** a comparison routine that defines the order of that sequence.
  8196. **
  8197. ** There may two separate implementations of the collation function, one
  8198. ** that processes text in UTF-8 encoding (CollSeq.xCmp) and another that
  8199. ** processes text encoded in UTF-16 (CollSeq.xCmp16), using the machine
  8200. ** native byte order. When a collation sequence is invoked, SQLite selects
  8201. ** the version that will require the least expensive encoding
  8202. ** translations, if any.
  8203. **
  8204. ** The CollSeq.pUser member variable is an extra parameter that passed in
  8205. ** as the first argument to the UTF-8 comparison function, xCmp.
  8206. ** CollSeq.pUser16 is the equivalent for the UTF-16 comparison function,
  8207. ** xCmp16.
  8208. **
  8209. ** If both CollSeq.xCmp and CollSeq.xCmp16 are NULL, it means that the
  8210. ** collating sequence is undefined. Indices built on an undefined
  8211. ** collating sequence may not be read or written.
  8212. */
  8213. struct CollSeq {
  8214. char *zName; /* Name of the collating sequence, UTF-8 encoded */
  8215. u8 enc; /* Text encoding handled by xCmp() */
  8216. u8 type; /* One of the SQLITE_COLL_... values below */
  8217. void *pUser; /* First argument to xCmp() */
  8218. int (*xCmp)(void*,int, const void*, int, const void*);
  8219. void (*xDel)(void*); /* Destructor for pUser */
  8220. };
  8221. /*
  8222. ** Allowed values of CollSeq.type:
  8223. */
  8224. #define SQLITE_COLL_BINARY 1 /* The default memcmp() collating sequence */
  8225. #define SQLITE_COLL_NOCASE 2 /* The built-in NOCASE collating sequence */
  8226. #define SQLITE_COLL_REVERSE 3 /* The built-in REVERSE collating sequence */
  8227. #define SQLITE_COLL_USER 0 /* Any other user-defined collating sequence */
  8228. /*
  8229. ** A sort order can be either ASC or DESC.
  8230. */
  8231. #define SQLITE_SO_ASC 0 /* Sort in ascending order */
  8232. #define SQLITE_SO_DESC 1 /* Sort in ascending order */
  8233. /*
  8234. ** Column affinity types.
  8235. **
  8236. ** These used to have mnemonic name like 'i' for SQLITE_AFF_INTEGER and
  8237. ** 't' for SQLITE_AFF_TEXT. But we can save a little space and improve
  8238. ** the speed a little by numbering the values consecutively.
  8239. **
  8240. ** But rather than start with 0 or 1, we begin with 'a'. That way,
  8241. ** when multiple affinity types are concatenated into a string and
  8242. ** used as the P4 operand, they will be more readable.
  8243. **
  8244. ** Note also that the numeric types are grouped together so that testing
  8245. ** for a numeric type is a single comparison.
  8246. */
  8247. #define SQLITE_AFF_TEXT 'a'
  8248. #define SQLITE_AFF_NONE 'b'
  8249. #define SQLITE_AFF_NUMERIC 'c'
  8250. #define SQLITE_AFF_INTEGER 'd'
  8251. #define SQLITE_AFF_REAL 'e'
  8252. #define sqlite3IsNumericAffinity(X) ((X)>=SQLITE_AFF_NUMERIC)
  8253. /*
  8254. ** The SQLITE_AFF_MASK values masks off the significant bits of an
  8255. ** affinity value.
  8256. */
  8257. #define SQLITE_AFF_MASK 0x67
  8258. /*
  8259. ** Additional bit values that can be ORed with an affinity without
  8260. ** changing the affinity.
  8261. */
  8262. #define SQLITE_JUMPIFNULL 0x08 /* jumps if either operand is NULL */
  8263. #define SQLITE_STOREP2 0x10 /* Store result in reg[P2] rather than jump */
  8264. #define SQLITE_NULLEQ 0x80 /* NULL=NULL */
  8265. /*
  8266. ** An object of this type is created for each virtual table present in
  8267. ** the database schema.
  8268. **
  8269. ** If the database schema is shared, then there is one instance of this
  8270. ** structure for each database connection (sqlite3*) that uses the shared
  8271. ** schema. This is because each database connection requires its own unique
  8272. ** instance of the sqlite3_vtab* handle used to access the virtual table
  8273. ** implementation. sqlite3_vtab* handles can not be shared between
  8274. ** database connections, even when the rest of the in-memory database
  8275. ** schema is shared, as the implementation often stores the database
  8276. ** connection handle passed to it via the xConnect() or xCreate() method
  8277. ** during initialization internally. This database connection handle may
  8278. ** then used by the virtual table implementation to access real tables
  8279. ** within the database. So that they appear as part of the callers
  8280. ** transaction, these accesses need to be made via the same database
  8281. ** connection as that used to execute SQL operations on the virtual table.
  8282. **
  8283. ** All VTable objects that correspond to a single table in a shared
  8284. ** database schema are initially stored in a linked-list pointed to by
  8285. ** the Table.pVTable member variable of the corresponding Table object.
  8286. ** When an sqlite3_prepare() operation is required to access the virtual
  8287. ** table, it searches the list for the VTable that corresponds to the
  8288. ** database connection doing the preparing so as to use the correct
  8289. ** sqlite3_vtab* handle in the compiled query.
  8290. **
  8291. ** When an in-memory Table object is deleted (for example when the
  8292. ** schema is being reloaded for some reason), the VTable objects are not
  8293. ** deleted and the sqlite3_vtab* handles are not xDisconnect()ed
  8294. ** immediately. Instead, they are moved from the Table.pVTable list to
  8295. ** another linked list headed by the sqlite3.pDisconnect member of the
  8296. ** corresponding sqlite3 structure. They are then deleted/xDisconnected
  8297. ** next time a statement is prepared using said sqlite3*. This is done
  8298. ** to avoid deadlock issues involving multiple sqlite3.mutex mutexes.
  8299. ** Refer to comments above function sqlite3VtabUnlockList() for an
  8300. ** explanation as to why it is safe to add an entry to an sqlite3.pDisconnect
  8301. ** list without holding the corresponding sqlite3.mutex mutex.
  8302. **
  8303. ** The memory for objects of this type is always allocated by
  8304. ** sqlite3DbMalloc(), using the connection handle stored in VTable.db as
  8305. ** the first argument.
  8306. */
  8307. struct VTable {
  8308. sqlite3 *db; /* Database connection associated with this table */
  8309. Module *pMod; /* Pointer to module implementation */
  8310. sqlite3_vtab *pVtab; /* Pointer to vtab instance */
  8311. int nRef; /* Number of pointers to this structure */
  8312. VTable *pNext; /* Next in linked list (see above) */
  8313. };
  8314. /*
  8315. ** Each SQL table is represented in memory by an instance of the
  8316. ** following structure.
  8317. **
  8318. ** Table.zName is the name of the table. The case of the original
  8319. ** CREATE TABLE statement is stored, but case is not significant for
  8320. ** comparisons.
  8321. **
  8322. ** Table.nCol is the number of columns in this table. Table.aCol is a
  8323. ** pointer to an array of Column structures, one for each column.
  8324. **
  8325. ** If the table has an INTEGER PRIMARY KEY, then Table.iPKey is the index of
  8326. ** the column that is that key. Otherwise Table.iPKey is negative. Note
  8327. ** that the datatype of the PRIMARY KEY must be INTEGER for this field to
  8328. ** be set. An INTEGER PRIMARY KEY is used as the rowid for each row of
  8329. ** the table. If a table has no INTEGER PRIMARY KEY, then a random rowid
  8330. ** is generated for each row of the table. TF_HasPrimaryKey is set if
  8331. ** the table has any PRIMARY KEY, INTEGER or otherwise.
  8332. **
  8333. ** Table.tnum is the page number for the root BTree page of the table in the
  8334. ** database file. If Table.iDb is the index of the database table backend
  8335. ** in sqlite.aDb[]. 0 is for the main database and 1 is for the file that
  8336. ** holds temporary tables and indices. If TF_Ephemeral is set
  8337. ** then the table is stored in a file that is automatically deleted
  8338. ** when the VDBE cursor to the table is closed. In this case Table.tnum
  8339. ** refers VDBE cursor number that holds the table open, not to the root
  8340. ** page number. Transient tables are used to hold the results of a
  8341. ** sub-query that appears instead of a real table name in the FROM clause
  8342. ** of a SELECT statement.
  8343. */
  8344. struct Table {
  8345. sqlite3 *dbMem; /* DB connection used for lookaside allocations. */
  8346. char *zName; /* Name of the table or view */
  8347. int iPKey; /* If not negative, use aCol[iPKey] as the primary key */
  8348. int nCol; /* Number of columns in this table */
  8349. Column *aCol; /* Information about each column */
  8350. Index *pIndex; /* List of SQL indexes on this table. */
  8351. int tnum; /* Root BTree node for this table (see note above) */
  8352. Select *pSelect; /* NULL for tables. Points to definition if a view. */
  8353. u16 nRef; /* Number of pointers to this Table */
  8354. u8 tabFlags; /* Mask of TF_* values */
  8355. u8 keyConf; /* What to do in case of uniqueness conflict on iPKey */
  8356. FKey *pFKey; /* Linked list of all foreign keys in this table */
  8357. char *zColAff; /* String defining the affinity of each column */
  8358. #ifndef SQLITE_OMIT_CHECK
  8359. Expr *pCheck; /* The AND of all CHECK constraints */
  8360. #endif
  8361. #ifndef SQLITE_OMIT_ALTERTABLE
  8362. int addColOffset; /* Offset in CREATE TABLE stmt to add a new column */
  8363. #endif
  8364. #ifndef SQLITE_OMIT_VIRTUALTABLE
  8365. VTable *pVTable; /* List of VTable objects. */
  8366. int nModuleArg; /* Number of arguments to the module */
  8367. char **azModuleArg; /* Text of all module args. [0] is module name */
  8368. #endif
  8369. Trigger *pTrigger; /* List of triggers stored in pSchema */
  8370. Schema *pSchema; /* Schema that contains this table */
  8371. Table *pNextZombie; /* Next on the Parse.pZombieTab list */
  8372. };
  8373. /*
  8374. ** Allowed values for Tabe.tabFlags.
  8375. */
  8376. #define TF_Readonly 0x01 /* Read-only system table */
  8377. #define TF_Ephemeral 0x02 /* An ephemeral table */
  8378. #define TF_HasPrimaryKey 0x04 /* Table has a primary key */
  8379. #define TF_Autoincrement 0x08 /* Integer primary key is autoincrement */
  8380. #define TF_Virtual 0x10 /* Is a virtual table */
  8381. #define TF_NeedMetadata 0x20 /* aCol[].zType and aCol[].pColl missing */
  8382. /*
  8383. ** Test to see whether or not a table is a virtual table. This is
  8384. ** done as a macro so that it will be optimized out when virtual
  8385. ** table support is omitted from the build.
  8386. */
  8387. #ifndef SQLITE_OMIT_VIRTUALTABLE
  8388. # define IsVirtual(X) (((X)->tabFlags & TF_Virtual)!=0)
  8389. # define IsHiddenColumn(X) ((X)->isHidden)
  8390. #else
  8391. # define IsVirtual(X) 0
  8392. # define IsHiddenColumn(X) 0
  8393. #endif
  8394. /*
  8395. ** Each foreign key constraint is an instance of the following structure.
  8396. **
  8397. ** A foreign key is associated with two tables. The "from" table is
  8398. ** the table that contains the REFERENCES clause that creates the foreign
  8399. ** key. The "to" table is the table that is named in the REFERENCES clause.
  8400. ** Consider this example:
  8401. **
  8402. ** CREATE TABLE ex1(
  8403. ** a INTEGER PRIMARY KEY,
  8404. ** b INTEGER CONSTRAINT fk1 REFERENCES ex2(x)
  8405. ** );
  8406. **
  8407. ** For foreign key "fk1", the from-table is "ex1" and the to-table is "ex2".
  8408. **
  8409. ** Each REFERENCES clause generates an instance of the following structure
  8410. ** which is attached to the from-table. The to-table need not exist when
  8411. ** the from-table is created. The existence of the to-table is not checked.
  8412. */
  8413. struct FKey {
  8414. Table *pFrom; /* Table containing the REFERENCES clause (aka: Child) */
  8415. FKey *pNextFrom; /* Next foreign key in pFrom */
  8416. char *zTo; /* Name of table that the key points to (aka: Parent) */
  8417. FKey *pNextTo; /* Next foreign key on table named zTo */
  8418. FKey *pPrevTo; /* Previous foreign key on table named zTo */
  8419. int nCol; /* Number of columns in this key */
  8420. /* EV: R-30323-21917 */
  8421. u8 isDeferred; /* True if constraint checking is deferred till COMMIT */
  8422. u8 aAction[2]; /* ON DELETE and ON UPDATE actions, respectively */
  8423. Trigger *apTrigger[2]; /* Triggers for aAction[] actions */
  8424. struct sColMap { /* Mapping of columns in pFrom to columns in zTo */
  8425. int iFrom; /* Index of column in pFrom */
  8426. char *zCol; /* Name of column in zTo. If 0 use PRIMARY KEY */
  8427. } aCol[1]; /* One entry for each of nCol column s */
  8428. };
  8429. /*
  8430. ** SQLite supports many different ways to resolve a constraint
  8431. ** error. ROLLBACK processing means that a constraint violation
  8432. ** causes the operation in process to fail and for the current transaction
  8433. ** to be rolled back. ABORT processing means the operation in process
  8434. ** fails and any prior changes from that one operation are backed out,
  8435. ** but the transaction is not rolled back. FAIL processing means that
  8436. ** the operation in progress stops and returns an error code. But prior
  8437. ** changes due to the same operation are not backed out and no rollback
  8438. ** occurs. IGNORE means that the particular row that caused the constraint
  8439. ** error is not inserted or updated. Processing continues and no error
  8440. ** is returned. REPLACE means that preexisting database rows that caused
  8441. ** a UNIQUE constraint violation are removed so that the new insert or
  8442. ** update can proceed. Processing continues and no error is reported.
  8443. **
  8444. ** RESTRICT, SETNULL, and CASCADE actions apply only to foreign keys.
  8445. ** RESTRICT is the same as ABORT for IMMEDIATE foreign keys and the
  8446. ** same as ROLLBACK for DEFERRED keys. SETNULL means that the foreign
  8447. ** key is set to NULL. CASCADE means that a DELETE or UPDATE of the
  8448. ** referenced table row is propagated into the row that holds the
  8449. ** foreign key.
  8450. **
  8451. ** The following symbolic values are used to record which type
  8452. ** of action to take.
  8453. */
  8454. #define OE_None 0 /* There is no constraint to check */
  8455. #define OE_Rollback 1 /* Fail the operation and rollback the transaction */
  8456. #define OE_Abort 2 /* Back out changes but do no rollback transaction */
  8457. #define OE_Fail 3 /* Stop the operation but leave all prior changes */
  8458. #define OE_Ignore 4 /* Ignore the error. Do not do the INSERT or UPDATE */
  8459. #define OE_Replace 5 /* Delete existing record, then do INSERT or UPDATE */
  8460. #define OE_Restrict 6 /* OE_Abort for IMMEDIATE, OE_Rollback for DEFERRED */
  8461. #define OE_SetNull 7 /* Set the foreign key value to NULL */
  8462. #define OE_SetDflt 8 /* Set the foreign key value to its default */
  8463. #define OE_Cascade 9 /* Cascade the changes */
  8464. #define OE_Default 99 /* Do whatever the default action is */
  8465. /*
  8466. ** An instance of the following structure is passed as the first
  8467. ** argument to sqlite3VdbeKeyCompare and is used to control the
  8468. ** comparison of the two index keys.
  8469. */
  8470. struct KeyInfo {
  8471. sqlite3 *db; /* The database connection */
  8472. u8 enc; /* Text encoding - one of the TEXT_Utf* values */
  8473. u16 nField; /* Number of entries in aColl[] */
  8474. u8 *aSortOrder; /* If defined an aSortOrder[i] is true, sort DESC */
  8475. CollSeq *aColl[1]; /* Collating sequence for each term of the key */
  8476. };
  8477. /*
  8478. ** An instance of the following structure holds information about a
  8479. ** single index record that has already been parsed out into individual
  8480. ** values.
  8481. **
  8482. ** A record is an object that contains one or more fields of data.
  8483. ** Records are used to store the content of a table row and to store
  8484. ** the key of an index. A blob encoding of a record is created by
  8485. ** the OP_MakeRecord opcode of the VDBE and is disassembled by the
  8486. ** OP_Column opcode.
  8487. **
  8488. ** This structure holds a record that has already been disassembled
  8489. ** into its constituent fields.
  8490. */
  8491. struct UnpackedRecord {
  8492. KeyInfo *pKeyInfo; /* Collation and sort-order information */
  8493. u16 nField; /* Number of entries in apMem[] */
  8494. u16 flags; /* Boolean settings. UNPACKED_... below */
  8495. i64 rowid; /* Used by UNPACKED_PREFIX_SEARCH */
  8496. Mem *aMem; /* Values */
  8497. };
  8498. /*
  8499. ** Allowed values of UnpackedRecord.flags
  8500. */
  8501. #define UNPACKED_NEED_FREE 0x0001 /* Memory is from sqlite3Malloc() */
  8502. #define UNPACKED_NEED_DESTROY 0x0002 /* apMem[]s should all be destroyed */
  8503. #define UNPACKED_IGNORE_ROWID 0x0004 /* Ignore trailing rowid on key1 */
  8504. #define UNPACKED_INCRKEY 0x0008 /* Make this key an epsilon larger */
  8505. #define UNPACKED_PREFIX_MATCH 0x0010 /* A prefix match is considered OK */
  8506. #define UNPACKED_PREFIX_SEARCH 0x0020 /* A prefix match is considered OK */
  8507. /*
  8508. ** Each SQL index is represented in memory by an
  8509. ** instance of the following structure.
  8510. **
  8511. ** The columns of the table that are to be indexed are described
  8512. ** by the aiColumn[] field of this structure. For example, suppose
  8513. ** we have the following table and index:
  8514. **
  8515. ** CREATE TABLE Ex1(c1 int, c2 int, c3 text);
  8516. ** CREATE INDEX Ex2 ON Ex1(c3,c1);
  8517. **
  8518. ** In the Table structure describing Ex1, nCol==3 because there are
  8519. ** three columns in the table. In the Index structure describing
  8520. ** Ex2, nColumn==2 since 2 of the 3 columns of Ex1 are indexed.
  8521. ** The value of aiColumn is {2, 0}. aiColumn[0]==2 because the
  8522. ** first column to be indexed (c3) has an index of 2 in Ex1.aCol[].
  8523. ** The second column to be indexed (c1) has an index of 0 in
  8524. ** Ex1.aCol[], hence Ex2.aiColumn[1]==0.
  8525. **
  8526. ** The Index.onError field determines whether or not the indexed columns
  8527. ** must be unique and what to do if they are not. When Index.onError=OE_None,
  8528. ** it means this is not a unique index. Otherwise it is a unique index
  8529. ** and the value of Index.onError indicate the which conflict resolution
  8530. ** algorithm to employ whenever an attempt is made to insert a non-unique
  8531. ** element.
  8532. */
  8533. struct Index {
  8534. char *zName; /* Name of this index */
  8535. int nColumn; /* Number of columns in the table used by this index */
  8536. int *aiColumn; /* Which columns are used by this index. 1st is 0 */
  8537. unsigned *aiRowEst; /* Result of ANALYZE: Est. rows selected by each column */
  8538. Table *pTable; /* The SQL table being indexed */
  8539. int tnum; /* Page containing root of this index in database file */
  8540. u8 onError; /* OE_Abort, OE_Ignore, OE_Replace, or OE_None */
  8541. u8 autoIndex; /* True if is automatically created (ex: by UNIQUE) */
  8542. char *zColAff; /* String defining the affinity of each column */
  8543. Index *pNext; /* The next index associated with the same table */
  8544. Schema *pSchema; /* Schema containing this index */
  8545. u8 *aSortOrder; /* Array of size Index.nColumn. True==DESC, False==ASC */
  8546. char **azColl; /* Array of collation sequence names for index */
  8547. IndexSample *aSample; /* Array of SQLITE_INDEX_SAMPLES samples */
  8548. };
  8549. /*
  8550. ** Each sample stored in the sqlite_stat2 table is represented in memory
  8551. ** using a structure of this type.
  8552. */
  8553. struct IndexSample {
  8554. union {
  8555. char *z; /* Value if eType is SQLITE_TEXT or SQLITE_BLOB */
  8556. double r; /* Value if eType is SQLITE_FLOAT or SQLITE_INTEGER */
  8557. } u;
  8558. u8 eType; /* SQLITE_NULL, SQLITE_INTEGER ... etc. */
  8559. u8 nByte; /* Size in byte of text or blob. */
  8560. };
  8561. /*
  8562. ** Each token coming out of the lexer is an instance of
  8563. ** this structure. Tokens are also used as part of an expression.
  8564. **
  8565. ** Note if Token.z==0 then Token.dyn and Token.n are undefined and
  8566. ** may contain random values. Do not make any assumptions about Token.dyn
  8567. ** and Token.n when Token.z==0.
  8568. */
  8569. struct Token {
  8570. const char *z; /* Text of the token. Not NULL-terminated! */
  8571. unsigned int n; /* Number of characters in this token */
  8572. };
  8573. /*
  8574. ** An instance of this structure contains information needed to generate
  8575. ** code for a SELECT that contains aggregate functions.
  8576. **
  8577. ** If Expr.op==TK_AGG_COLUMN or TK_AGG_FUNCTION then Expr.pAggInfo is a
  8578. ** pointer to this structure. The Expr.iColumn field is the index in
  8579. ** AggInfo.aCol[] or AggInfo.aFunc[] of information needed to generate
  8580. ** code for that node.
  8581. **
  8582. ** AggInfo.pGroupBy and AggInfo.aFunc.pExpr point to fields within the
  8583. ** original Select structure that describes the SELECT statement. These
  8584. ** fields do not need to be freed when deallocating the AggInfo structure.
  8585. */
  8586. struct AggInfo {
  8587. u8 directMode; /* Direct rendering mode means take data directly
  8588. ** from source tables rather than from accumulators */
  8589. u8 useSortingIdx; /* In direct mode, reference the sorting index rather
  8590. ** than the source table */
  8591. int sortingIdx; /* Cursor number of the sorting index */
  8592. ExprList *pGroupBy; /* The group by clause */
  8593. int nSortingColumn; /* Number of columns in the sorting index */
  8594. struct AggInfo_col { /* For each column used in source tables */
  8595. Table *pTab; /* Source table */
  8596. int iTable; /* Cursor number of the source table */
  8597. int iColumn; /* Column number within the source table */
  8598. int iSorterColumn; /* Column number in the sorting index */
  8599. int iMem; /* Memory location that acts as accumulator */
  8600. Expr *pExpr; /* The original expression */
  8601. } *aCol;
  8602. int nColumn; /* Number of used entries in aCol[] */
  8603. int nColumnAlloc; /* Number of slots allocated for aCol[] */
  8604. int nAccumulator; /* Number of columns that show through to the output.
  8605. ** Additional columns are used only as parameters to
  8606. ** aggregate functions */
  8607. struct AggInfo_func { /* For each aggregate function */
  8608. Expr *pExpr; /* Expression encoding the function */
  8609. FuncDef *pFunc; /* The aggregate function implementation */
  8610. int iMem; /* Memory location that acts as accumulator */
  8611. int iDistinct; /* Ephemeral table used to enforce DISTINCT */
  8612. } *aFunc;
  8613. int nFunc; /* Number of entries in aFunc[] */
  8614. int nFuncAlloc; /* Number of slots allocated for aFunc[] */
  8615. };
  8616. /*
  8617. ** The datatype ynVar is a signed integer, either 16-bit or 32-bit.
  8618. ** Usually it is 16-bits. But if SQLITE_MAX_VARIABLE_NUMBER is greater
  8619. ** than 32767 we have to make it 32-bit. 16-bit is preferred because
  8620. ** it uses less memory in the Expr object, which is a big memory user
  8621. ** in systems with lots of prepared statements. And few applications
  8622. ** need more than about 10 or 20 variables. But some extreme users want
  8623. ** to have prepared statements with over 32767 variables, and for them
  8624. ** the option is available (at compile-time).
  8625. */
  8626. #if SQLITE_MAX_VARIABLE_NUMBER<=32767
  8627. typedef i16 ynVar;
  8628. #else
  8629. typedef int ynVar;
  8630. #endif
  8631. /*
  8632. ** Each node of an expression in the parse tree is an instance
  8633. ** of this structure.
  8634. **
  8635. ** Expr.op is the opcode. The integer parser token codes are reused
  8636. ** as opcodes here. For example, the parser defines TK_GE to be an integer
  8637. ** code representing the ">=" operator. This same integer code is reused
  8638. ** to represent the greater-than-or-equal-to operator in the expression
  8639. ** tree.
  8640. **
  8641. ** If the expression is an SQL literal (TK_INTEGER, TK_FLOAT, TK_BLOB,
  8642. ** or TK_STRING), then Expr.token contains the text of the SQL literal. If
  8643. ** the expression is a variable (TK_VARIABLE), then Expr.token contains the
  8644. ** variable name. Finally, if the expression is an SQL function (TK_FUNCTION),
  8645. ** then Expr.token contains the name of the function.
  8646. **
  8647. ** Expr.pRight and Expr.pLeft are the left and right subexpressions of a
  8648. ** binary operator. Either or both may be NULL.
  8649. **
  8650. ** Expr.x.pList is a list of arguments if the expression is an SQL function,
  8651. ** a CASE expression or an IN expression of the form "<lhs> IN (<y>, <z>...)".
  8652. ** Expr.x.pSelect is used if the expression is a sub-select or an expression of
  8653. ** the form "<lhs> IN (SELECT ...)". If the EP_xIsSelect bit is set in the
  8654. ** Expr.flags mask, then Expr.x.pSelect is valid. Otherwise, Expr.x.pList is
  8655. ** valid.
  8656. **
  8657. ** An expression of the form ID or ID.ID refers to a column in a table.
  8658. ** For such expressions, Expr.op is set to TK_COLUMN and Expr.iTable is
  8659. ** the integer cursor number of a VDBE cursor pointing to that table and
  8660. ** Expr.iColumn is the column number for the specific column. If the
  8661. ** expression is used as a result in an aggregate SELECT, then the
  8662. ** value is also stored in the Expr.iAgg column in the aggregate so that
  8663. ** it can be accessed after all aggregates are computed.
  8664. **
  8665. ** If the expression is an unbound variable marker (a question mark
  8666. ** character '?' in the original SQL) then the Expr.iTable holds the index
  8667. ** number for that variable.
  8668. **
  8669. ** If the expression is a subquery then Expr.iColumn holds an integer
  8670. ** register number containing the result of the subquery. If the
  8671. ** subquery gives a constant result, then iTable is -1. If the subquery
  8672. ** gives a different answer at different times during statement processing
  8673. ** then iTable is the address of a subroutine that computes the subquery.
  8674. **
  8675. ** If the Expr is of type OP_Column, and the table it is selecting from
  8676. ** is a disk table or the "old.*" pseudo-table, then pTab points to the
  8677. ** corresponding table definition.
  8678. **
  8679. ** ALLOCATION NOTES:
  8680. **
  8681. ** Expr objects can use a lot of memory space in database schema. To
  8682. ** help reduce memory requirements, sometimes an Expr object will be
  8683. ** truncated. And to reduce the number of memory allocations, sometimes
  8684. ** two or more Expr objects will be stored in a single memory allocation,
  8685. ** together with Expr.zToken strings.
  8686. **
  8687. ** If the EP_Reduced and EP_TokenOnly flags are set when
  8688. ** an Expr object is truncated. When EP_Reduced is set, then all
  8689. ** the child Expr objects in the Expr.pLeft and Expr.pRight subtrees
  8690. ** are contained within the same memory allocation. Note, however, that
  8691. ** the subtrees in Expr.x.pList or Expr.x.pSelect are always separately
  8692. ** allocated, regardless of whether or not EP_Reduced is set.
  8693. */
  8694. struct Expr {
  8695. u8 op; /* Operation performed by this node */
  8696. char affinity; /* The affinity of the column or 0 if not a column */
  8697. u16 flags; /* Various flags. EP_* See below */
  8698. union {
  8699. char *zToken; /* Token value. Zero terminated and dequoted */
  8700. int iValue; /* Integer value if EP_IntValue */
  8701. } u;
  8702. /* If the EP_TokenOnly flag is set in the Expr.flags mask, then no
  8703. ** space is allocated for the fields below this point. An attempt to
  8704. ** access them will result in a segfault or malfunction.
  8705. *********************************************************************/
  8706. Expr *pLeft; /* Left subnode */
  8707. Expr *pRight; /* Right subnode */
  8708. union {
  8709. ExprList *pList; /* Function arguments or in "<expr> IN (<expr-list)" */
  8710. Select *pSelect; /* Used for sub-selects and "<expr> IN (<select>)" */
  8711. } x;
  8712. CollSeq *pColl; /* The collation type of the column or 0 */
  8713. /* If the EP_Reduced flag is set in the Expr.flags mask, then no
  8714. ** space is allocated for the fields below this point. An attempt to
  8715. ** access them will result in a segfault or malfunction.
  8716. *********************************************************************/
  8717. int iTable; /* TK_COLUMN: cursor number of table holding column
  8718. ** TK_REGISTER: register number
  8719. ** TK_TRIGGER: 1 -> new, 0 -> old */
  8720. ynVar iColumn; /* TK_COLUMN: column index. -1 for rowid.
  8721. ** TK_VARIABLE: variable number (always >= 1). */
  8722. i16 iAgg; /* Which entry in pAggInfo->aCol[] or ->aFunc[] */
  8723. i16 iRightJoinTable; /* If EP_FromJoin, the right table of the join */
  8724. u8 flags2; /* Second set of flags. EP2_... */
  8725. u8 op2; /* If a TK_REGISTER, the original value of Expr.op */
  8726. AggInfo *pAggInfo; /* Used by TK_AGG_COLUMN and TK_AGG_FUNCTION */
  8727. Table *pTab; /* Table for TK_COLUMN expressions. */
  8728. #if SQLITE_MAX_EXPR_DEPTH>0
  8729. int nHeight; /* Height of the tree headed by this node */
  8730. #endif
  8731. };
  8732. /*
  8733. ** The following are the meanings of bits in the Expr.flags field.
  8734. */
  8735. #define EP_FromJoin 0x0001 /* Originated in ON or USING clause of a join */
  8736. #define EP_Agg 0x0002 /* Contains one or more aggregate functions */
  8737. #define EP_Resolved 0x0004 /* IDs have been resolved to COLUMNs */
  8738. #define EP_Error 0x0008 /* Expression contains one or more errors */
  8739. #define EP_Distinct 0x0010 /* Aggregate function with DISTINCT keyword */
  8740. #define EP_VarSelect 0x0020 /* pSelect is correlated, not constant */
  8741. #define EP_DblQuoted 0x0040 /* token.z was originally in "..." */
  8742. #define EP_InfixFunc 0x0080 /* True for an infix function: LIKE, GLOB, etc */
  8743. #define EP_ExpCollate 0x0100 /* Collating sequence specified explicitly */
  8744. #define EP_FixedDest 0x0200 /* Result needed in a specific register */
  8745. #define EP_IntValue 0x0400 /* Integer value contained in u.iValue */
  8746. #define EP_xIsSelect 0x0800 /* x.pSelect is valid (otherwise x.pList is) */
  8747. #define EP_Reduced 0x1000 /* Expr struct is EXPR_REDUCEDSIZE bytes only */
  8748. #define EP_TokenOnly 0x2000 /* Expr struct is EXPR_TOKENONLYSIZE bytes only */
  8749. #define EP_Static 0x4000 /* Held in memory not obtained from malloc() */
  8750. /*
  8751. ** The following are the meanings of bits in the Expr.flags2 field.
  8752. */
  8753. #define EP2_MallocedToken 0x0001 /* Need to sqlite3DbFree() Expr.zToken */
  8754. #define EP2_Irreducible 0x0002 /* Cannot EXPRDUP_REDUCE this Expr */
  8755. /*
  8756. ** The pseudo-routine sqlite3ExprSetIrreducible sets the EP2_Irreducible
  8757. ** flag on an expression structure. This flag is used for VV&A only. The
  8758. ** routine is implemented as a macro that only works when in debugging mode,
  8759. ** so as not to burden production code.
  8760. */
  8761. #ifdef SQLITE_DEBUG
  8762. # define ExprSetIrreducible(X) (X)->flags2 |= EP2_Irreducible
  8763. #else
  8764. # define ExprSetIrreducible(X)
  8765. #endif
  8766. /*
  8767. ** These macros can be used to test, set, or clear bits in the
  8768. ** Expr.flags field.
  8769. */
  8770. #define ExprHasProperty(E,P) (((E)->flags&(P))==(P))
  8771. #define ExprHasAnyProperty(E,P) (((E)->flags&(P))!=0)
  8772. #define ExprSetProperty(E,P) (E)->flags|=(P)
  8773. #define ExprClearProperty(E,P) (E)->flags&=~(P)
  8774. /*
  8775. ** Macros to determine the number of bytes required by a normal Expr
  8776. ** struct, an Expr struct with the EP_Reduced flag set in Expr.flags
  8777. ** and an Expr struct with the EP_TokenOnly flag set.
  8778. */
  8779. #define EXPR_FULLSIZE sizeof(Expr) /* Full size */
  8780. #define EXPR_REDUCEDSIZE offsetof(Expr,iTable) /* Common features */
  8781. #define EXPR_TOKENONLYSIZE offsetof(Expr,pLeft) /* Fewer features */
  8782. /*
  8783. ** Flags passed to the sqlite3ExprDup() function. See the header comment
  8784. ** above sqlite3ExprDup() for details.
  8785. */
  8786. #define EXPRDUP_REDUCE 0x0001 /* Used reduced-size Expr nodes */
  8787. /*
  8788. ** A list of expressions. Each expression may optionally have a
  8789. ** name. An expr/name combination can be used in several ways, such
  8790. ** as the list of "expr AS ID" fields following a "SELECT" or in the
  8791. ** list of "ID = expr" items in an UPDATE. A list of expressions can
  8792. ** also be used as the argument to a function, in which case the a.zName
  8793. ** field is not used.
  8794. */
  8795. struct ExprList {
  8796. int nExpr; /* Number of expressions on the list */
  8797. int nAlloc; /* Number of entries allocated below */
  8798. int iECursor; /* VDBE Cursor associated with this ExprList */
  8799. struct ExprList_item {
  8800. Expr *pExpr; /* The list of expressions */
  8801. char *zName; /* Token associated with this expression */
  8802. char *zSpan; /* Original text of the expression */
  8803. u8 sortOrder; /* 1 for DESC or 0 for ASC */
  8804. u8 done; /* A flag to indicate when processing is finished */
  8805. u16 iCol; /* For ORDER BY, column number in result set */
  8806. u16 iAlias; /* Index into Parse.aAlias[] for zName */
  8807. } *a; /* One entry for each expression */
  8808. };
  8809. /*
  8810. ** An instance of this structure is used by the parser to record both
  8811. ** the parse tree for an expression and the span of input text for an
  8812. ** expression.
  8813. */
  8814. struct ExprSpan {
  8815. Expr *pExpr; /* The expression parse tree */
  8816. const char *zStart; /* First character of input text */
  8817. const char *zEnd; /* One character past the end of input text */
  8818. };
  8819. /*
  8820. ** An instance of this structure can hold a simple list of identifiers,
  8821. ** such as the list "a,b,c" in the following statements:
  8822. **
  8823. ** INSERT INTO t(a,b,c) VALUES ...;
  8824. ** CREATE INDEX idx ON t(a,b,c);
  8825. ** CREATE TRIGGER trig BEFORE UPDATE ON t(a,b,c) ...;
  8826. **
  8827. ** The IdList.a.idx field is used when the IdList represents the list of
  8828. ** column names after a table name in an INSERT statement. In the statement
  8829. **
  8830. ** INSERT INTO t(a,b,c) ...
  8831. **
  8832. ** If "a" is the k-th column of table "t", then IdList.a[0].idx==k.
  8833. */
  8834. struct IdList {
  8835. struct IdList_item {
  8836. char *zName; /* Name of the identifier */
  8837. int idx; /* Index in some Table.aCol[] of a column named zName */
  8838. } *a;
  8839. int nId; /* Number of identifiers on the list */
  8840. int nAlloc; /* Number of entries allocated for a[] below */
  8841. };
  8842. /*
  8843. ** The bitmask datatype defined below is used for various optimizations.
  8844. **
  8845. ** Changing this from a 64-bit to a 32-bit type limits the number of
  8846. ** tables in a join to 32 instead of 64. But it also reduces the size
  8847. ** of the library by 738 bytes on ix86.
  8848. */
  8849. typedef u64 Bitmask;
  8850. /*
  8851. ** The number of bits in a Bitmask. "BMS" means "BitMask Size".
  8852. */
  8853. #define BMS ((int)(sizeof(Bitmask)*8))
  8854. /*
  8855. ** The following structure describes the FROM clause of a SELECT statement.
  8856. ** Each table or subquery in the FROM clause is a separate element of
  8857. ** the SrcList.a[] array.
  8858. **
  8859. ** With the addition of multiple database support, the following structure
  8860. ** can also be used to describe a particular table such as the table that
  8861. ** is modified by an INSERT, DELETE, or UPDATE statement. In standard SQL,
  8862. ** such a table must be a simple name: ID. But in SQLite, the table can
  8863. ** now be identified by a database name, a dot, then the table name: ID.ID.
  8864. **
  8865. ** The jointype starts out showing the join type between the current table
  8866. ** and the next table on the list. The parser builds the list this way.
  8867. ** But sqlite3SrcListShiftJoinType() later shifts the jointypes so that each
  8868. ** jointype expresses the join between the table and the previous table.
  8869. */
  8870. struct SrcList {
  8871. i16 nSrc; /* Number of tables or subqueries in the FROM clause */
  8872. i16 nAlloc; /* Number of entries allocated in a[] below */
  8873. struct SrcList_item {
  8874. char *zDatabase; /* Name of database holding this table */
  8875. char *zName; /* Name of the table */
  8876. char *zAlias; /* The "B" part of a "A AS B" phrase. zName is the "A" */
  8877. Table *pTab; /* An SQL table corresponding to zName */
  8878. Select *pSelect; /* A SELECT statement used in place of a table name */
  8879. u8 isPopulated; /* Temporary table associated with SELECT is populated */
  8880. u8 jointype; /* Type of join between this able and the previous */
  8881. u8 notIndexed; /* True if there is a NOT INDEXED clause */
  8882. int iCursor; /* The VDBE cursor number used to access this table */
  8883. Expr *pOn; /* The ON clause of a join */
  8884. IdList *pUsing; /* The USING clause of a join */
  8885. Bitmask colUsed; /* Bit N (1<<N) set if column N of pTab is used */
  8886. char *zIndex; /* Identifier from "INDEXED BY <zIndex>" clause */
  8887. Index *pIndex; /* Index structure corresponding to zIndex, if any */
  8888. } a[1]; /* One entry for each identifier on the list */
  8889. };
  8890. /*
  8891. ** Permitted values of the SrcList.a.jointype field
  8892. */
  8893. #define JT_INNER 0x0001 /* Any kind of inner or cross join */
  8894. #define JT_CROSS 0x0002 /* Explicit use of the CROSS keyword */
  8895. #define JT_NATURAL 0x0004 /* True for a "natural" join */
  8896. #define JT_LEFT 0x0008 /* Left outer join */
  8897. #define JT_RIGHT 0x0010 /* Right outer join */
  8898. #define JT_OUTER 0x0020 /* The "OUTER" keyword is present */
  8899. #define JT_ERROR 0x0040 /* unknown or unsupported join type */
  8900. /*
  8901. ** A WherePlan object holds information that describes a lookup
  8902. ** strategy.
  8903. **
  8904. ** This object is intended to be opaque outside of the where.c module.
  8905. ** It is included here only so that that compiler will know how big it
  8906. ** is. None of the fields in this object should be used outside of
  8907. ** the where.c module.
  8908. **
  8909. ** Within the union, pIdx is only used when wsFlags&WHERE_INDEXED is true.
  8910. ** pTerm is only used when wsFlags&WHERE_MULTI_OR is true. And pVtabIdx
  8911. ** is only used when wsFlags&WHERE_VIRTUALTABLE is true. It is never the
  8912. ** case that more than one of these conditions is true.
  8913. */
  8914. struct WherePlan {
  8915. u32 wsFlags; /* WHERE_* flags that describe the strategy */
  8916. u32 nEq; /* Number of == constraints */
  8917. union {
  8918. Index *pIdx; /* Index when WHERE_INDEXED is true */
  8919. struct WhereTerm *pTerm; /* WHERE clause term for OR-search */
  8920. sqlite3_index_info *pVtabIdx; /* Virtual table index to use */
  8921. } u;
  8922. };
  8923. /*
  8924. ** For each nested loop in a WHERE clause implementation, the WhereInfo
  8925. ** structure contains a single instance of this structure. This structure
  8926. ** is intended to be private the the where.c module and should not be
  8927. ** access or modified by other modules.
  8928. **
  8929. ** The pIdxInfo field is used to help pick the best index on a
  8930. ** virtual table. The pIdxInfo pointer contains indexing
  8931. ** information for the i-th table in the FROM clause before reordering.
  8932. ** All the pIdxInfo pointers are freed by whereInfoFree() in where.c.
  8933. ** All other information in the i-th WhereLevel object for the i-th table
  8934. ** after FROM clause ordering.
  8935. */
  8936. struct WhereLevel {
  8937. WherePlan plan; /* query plan for this element of the FROM clause */
  8938. int iLeftJoin; /* Memory cell used to implement LEFT OUTER JOIN */
  8939. int iTabCur; /* The VDBE cursor used to access the table */
  8940. int iIdxCur; /* The VDBE cursor used to access pIdx */
  8941. int addrBrk; /* Jump here to break out of the loop */
  8942. int addrNxt; /* Jump here to start the next IN combination */
  8943. int addrCont; /* Jump here to continue with the next loop cycle */
  8944. int addrFirst; /* First instruction of interior of the loop */
  8945. u8 iFrom; /* Which entry in the FROM clause */
  8946. u8 op, p5; /* Opcode and P5 of the opcode that ends the loop */
  8947. int p1, p2; /* Operands of the opcode used to ends the loop */
  8948. union { /* Information that depends on plan.wsFlags */
  8949. struct {
  8950. int nIn; /* Number of entries in aInLoop[] */
  8951. struct InLoop {
  8952. int iCur; /* The VDBE cursor used by this IN operator */
  8953. int addrInTop; /* Top of the IN loop */
  8954. } *aInLoop; /* Information about each nested IN operator */
  8955. } in; /* Used when plan.wsFlags&WHERE_IN_ABLE */
  8956. } u;
  8957. /* The following field is really not part of the current level. But
  8958. ** we need a place to cache virtual table index information for each
  8959. ** virtual table in the FROM clause and the WhereLevel structure is
  8960. ** a convenient place since there is one WhereLevel for each FROM clause
  8961. ** element.
  8962. */
  8963. sqlite3_index_info *pIdxInfo; /* Index info for n-th source table */
  8964. };
  8965. /*
  8966. ** Flags appropriate for the wctrlFlags parameter of sqlite3WhereBegin()
  8967. ** and the WhereInfo.wctrlFlags member.
  8968. */
  8969. #define WHERE_ORDERBY_NORMAL 0x0000 /* No-op */
  8970. #define WHERE_ORDERBY_MIN 0x0001 /* ORDER BY processing for min() func */
  8971. #define WHERE_ORDERBY_MAX 0x0002 /* ORDER BY processing for max() func */
  8972. #define WHERE_ONEPASS_DESIRED 0x0004 /* Want to do one-pass UPDATE/DELETE */
  8973. #define WHERE_DUPLICATES_OK 0x0008 /* Ok to return a row more than once */
  8974. #define WHERE_OMIT_OPEN 0x0010 /* Table cursor are already open */
  8975. #define WHERE_OMIT_CLOSE 0x0020 /* Omit close of table & index cursors */
  8976. #define WHERE_FORCE_TABLE 0x0040 /* Do not use an index-only search */
  8977. #define WHERE_ONETABLE_ONLY 0x0080 /* Only code the 1st table in pTabList */
  8978. /*
  8979. ** The WHERE clause processing routine has two halves. The
  8980. ** first part does the start of the WHERE loop and the second
  8981. ** half does the tail of the WHERE loop. An instance of
  8982. ** this structure is returned by the first half and passed
  8983. ** into the second half to give some continuity.
  8984. */
  8985. struct WhereInfo {
  8986. Parse *pParse; /* Parsing and code generating context */
  8987. u16 wctrlFlags; /* Flags originally passed to sqlite3WhereBegin() */
  8988. u8 okOnePass; /* Ok to use one-pass algorithm for UPDATE or DELETE */
  8989. u8 untestedTerms; /* Not all WHERE terms resolved by outer loop */
  8990. SrcList *pTabList; /* List of tables in the join */
  8991. int iTop; /* The very beginning of the WHERE loop */
  8992. int iContinue; /* Jump here to continue with next record */
  8993. int iBreak; /* Jump here to break out of the loop */
  8994. int nLevel; /* Number of nested loop */
  8995. struct WhereClause *pWC; /* Decomposition of the WHERE clause */
  8996. WhereLevel a[1]; /* Information about each nest loop in WHERE */
  8997. };
  8998. /*
  8999. ** A NameContext defines a context in which to resolve table and column
  9000. ** names. The context consists of a list of tables (the pSrcList) field and
  9001. ** a list of named expression (pEList). The named expression list may
  9002. ** be NULL. The pSrc corresponds to the FROM clause of a SELECT or
  9003. ** to the table being operated on by INSERT, UPDATE, or DELETE. The
  9004. ** pEList corresponds to the result set of a SELECT and is NULL for
  9005. ** other statements.
  9006. **
  9007. ** NameContexts can be nested. When resolving names, the inner-most
  9008. ** context is searched first. If no match is found, the next outer
  9009. ** context is checked. If there is still no match, the next context
  9010. ** is checked. This process continues until either a match is found
  9011. ** or all contexts are check. When a match is found, the nRef member of
  9012. ** the context containing the match is incremented.
  9013. **
  9014. ** Each subquery gets a new NameContext. The pNext field points to the
  9015. ** NameContext in the parent query. Thus the process of scanning the
  9016. ** NameContext list corresponds to searching through successively outer
  9017. ** subqueries looking for a match.
  9018. */
  9019. struct NameContext {
  9020. Parse *pParse; /* The parser */
  9021. SrcList *pSrcList; /* One or more tables used to resolve names */
  9022. ExprList *pEList; /* Optional list of named expressions */
  9023. int nRef; /* Number of names resolved by this context */
  9024. int nErr; /* Number of errors encountered while resolving names */
  9025. u8 allowAgg; /* Aggregate functions allowed here */
  9026. u8 hasAgg; /* True if aggregates are seen */
  9027. u8 isCheck; /* True if resolving names in a CHECK constraint */
  9028. int nDepth; /* Depth of subquery recursion. 1 for no recursion */
  9029. AggInfo *pAggInfo; /* Information about aggregates at this level */
  9030. NameContext *pNext; /* Next outer name context. NULL for outermost */
  9031. };
  9032. /*
  9033. ** An instance of the following structure contains all information
  9034. ** needed to generate code for a single SELECT statement.
  9035. **
  9036. ** nLimit is set to -1 if there is no LIMIT clause. nOffset is set to 0.
  9037. ** If there is a LIMIT clause, the parser sets nLimit to the value of the
  9038. ** limit and nOffset to the value of the offset (or 0 if there is not
  9039. ** offset). But later on, nLimit and nOffset become the memory locations
  9040. ** in the VDBE that record the limit and offset counters.
  9041. **
  9042. ** addrOpenEphm[] entries contain the address of OP_OpenEphemeral opcodes.
  9043. ** These addresses must be stored so that we can go back and fill in
  9044. ** the P4_KEYINFO and P2 parameters later. Neither the KeyInfo nor
  9045. ** the number of columns in P2 can be computed at the same time
  9046. ** as the OP_OpenEphm instruction is coded because not
  9047. ** enough information about the compound query is known at that point.
  9048. ** The KeyInfo for addrOpenTran[0] and [1] contains collating sequences
  9049. ** for the result set. The KeyInfo for addrOpenTran[2] contains collating
  9050. ** sequences for the ORDER BY clause.
  9051. */
  9052. struct Select {
  9053. ExprList *pEList; /* The fields of the result */
  9054. u8 op; /* One of: TK_UNION TK_ALL TK_INTERSECT TK_EXCEPT */
  9055. char affinity; /* MakeRecord with this affinity for SRT_Set */
  9056. u16 selFlags; /* Various SF_* values */
  9057. SrcList *pSrc; /* The FROM clause */
  9058. Expr *pWhere; /* The WHERE clause */
  9059. ExprList *pGroupBy; /* The GROUP BY clause */
  9060. Expr *pHaving; /* The HAVING clause */
  9061. ExprList *pOrderBy; /* The ORDER BY clause */
  9062. Select *pPrior; /* Prior select in a compound select statement */
  9063. Select *pNext; /* Next select to the left in a compound */
  9064. Select *pRightmost; /* Right-most select in a compound select statement */
  9065. Expr *pLimit; /* LIMIT expression. NULL means not used. */
  9066. Expr *pOffset; /* OFFSET expression. NULL means not used. */
  9067. int iLimit, iOffset; /* Memory registers holding LIMIT & OFFSET counters */
  9068. int addrOpenEphm[3]; /* OP_OpenEphem opcodes related to this select */
  9069. };
  9070. /*
  9071. ** Allowed values for Select.selFlags. The "SF" prefix stands for
  9072. ** "Select Flag".
  9073. */
  9074. #define SF_Distinct 0x0001 /* Output should be DISTINCT */
  9075. #define SF_Resolved 0x0002 /* Identifiers have been resolved */
  9076. #define SF_Aggregate 0x0004 /* Contains aggregate functions */
  9077. #define SF_UsesEphemeral 0x0008 /* Uses the OpenEphemeral opcode */
  9078. #define SF_Expanded 0x0010 /* sqlite3SelectExpand() called on this */
  9079. #define SF_HasTypeInfo 0x0020 /* FROM subqueries have Table metadata */
  9080. /*
  9081. ** The results of a select can be distributed in several ways. The
  9082. ** "SRT" prefix means "SELECT Result Type".
  9083. */
  9084. #define SRT_Union 1 /* Store result as keys in an index */
  9085. #define SRT_Except 2 /* Remove result from a UNION index */
  9086. #define SRT_Exists 3 /* Store 1 if the result is not empty */
  9087. #define SRT_Discard 4 /* Do not save the results anywhere */
  9088. /* The ORDER BY clause is ignored for all of the above */
  9089. #define IgnorableOrderby(X) ((X->eDest)<=SRT_Discard)
  9090. #define SRT_Output 5 /* Output each row of result */
  9091. #define SRT_Mem 6 /* Store result in a memory cell */
  9092. #define SRT_Set 7 /* Store results as keys in an index */
  9093. #define SRT_Table 8 /* Store result as data with an automatic rowid */
  9094. #define SRT_EphemTab 9 /* Create transient tab and store like SRT_Table */
  9095. #define SRT_Coroutine 10 /* Generate a single row of result */
  9096. /*
  9097. ** A structure used to customize the behavior of sqlite3Select(). See
  9098. ** comments above sqlite3Select() for details.
  9099. */
  9100. typedef struct SelectDest SelectDest;
  9101. struct SelectDest {
  9102. u8 eDest; /* How to dispose of the results */
  9103. u8 affinity; /* Affinity used when eDest==SRT_Set */
  9104. int iParm; /* A parameter used by the eDest disposal method */
  9105. int iMem; /* Base register where results are written */
  9106. int nMem; /* Number of registers allocated */
  9107. };
  9108. /*
  9109. ** During code generation of statements that do inserts into AUTOINCREMENT
  9110. ** tables, the following information is attached to the Table.u.autoInc.p
  9111. ** pointer of each autoincrement table to record some side information that
  9112. ** the code generator needs. We have to keep per-table autoincrement
  9113. ** information in case inserts are down within triggers. Triggers do not
  9114. ** normally coordinate their activities, but we do need to coordinate the
  9115. ** loading and saving of autoincrement information.
  9116. */
  9117. struct AutoincInfo {
  9118. AutoincInfo *pNext; /* Next info block in a list of them all */
  9119. Table *pTab; /* Table this info block refers to */
  9120. int iDb; /* Index in sqlite3.aDb[] of database holding pTab */
  9121. int regCtr; /* Memory register holding the rowid counter */
  9122. };
  9123. /*
  9124. ** Size of the column cache
  9125. */
  9126. #ifndef SQLITE_N_COLCACHE
  9127. # define SQLITE_N_COLCACHE 10
  9128. #endif
  9129. /*
  9130. ** At least one instance of the following structure is created for each
  9131. ** trigger that may be fired while parsing an INSERT, UPDATE or DELETE
  9132. ** statement. All such objects are stored in the linked list headed at
  9133. ** Parse.pTriggerPrg and deleted once statement compilation has been
  9134. ** completed.
  9135. **
  9136. ** A Vdbe sub-program that implements the body and WHEN clause of trigger
  9137. ** TriggerPrg.pTrigger, assuming a default ON CONFLICT clause of
  9138. ** TriggerPrg.orconf, is stored in the TriggerPrg.pProgram variable.
  9139. ** The Parse.pTriggerPrg list never contains two entries with the same
  9140. ** values for both pTrigger and orconf.
  9141. **
  9142. ** The TriggerPrg.aColmask[0] variable is set to a mask of old.* columns
  9143. ** accessed (or set to 0 for triggers fired as a result of INSERT
  9144. ** statements). Similarly, the TriggerPrg.aColmask[1] variable is set to
  9145. ** a mask of new.* columns used by the program.
  9146. */
  9147. struct TriggerPrg {
  9148. Trigger *pTrigger; /* Trigger this program was coded from */
  9149. int orconf; /* Default ON CONFLICT policy */
  9150. SubProgram *pProgram; /* Program implementing pTrigger/orconf */
  9151. u32 aColmask[2]; /* Masks of old.*, new.* columns accessed */
  9152. TriggerPrg *pNext; /* Next entry in Parse.pTriggerPrg list */
  9153. };
  9154. /*
  9155. ** An SQL parser context. A copy of this structure is passed through
  9156. ** the parser and down into all the parser action routine in order to
  9157. ** carry around information that is global to the entire parse.
  9158. **
  9159. ** The structure is divided into two parts. When the parser and code
  9160. ** generate call themselves recursively, the first part of the structure
  9161. ** is constant but the second part is reset at the beginning and end of
  9162. ** each recursion.
  9163. **
  9164. ** The nTableLock and aTableLock variables are only used if the shared-cache
  9165. ** feature is enabled (if sqlite3Tsd()->useSharedData is true). They are
  9166. ** used to store the set of table-locks required by the statement being
  9167. ** compiled. Function sqlite3TableLock() is used to add entries to the
  9168. ** list.
  9169. */
  9170. struct Parse {
  9171. sqlite3 *db; /* The main database structure */
  9172. int rc; /* Return code from execution */
  9173. char *zErrMsg; /* An error message */
  9174. Vdbe *pVdbe; /* An engine for executing database bytecode */
  9175. u8 colNamesSet; /* TRUE after OP_ColumnName has been issued to pVdbe */
  9176. u8 nameClash; /* A permanent table name clashes with temp table name */
  9177. u8 checkSchema; /* Causes schema cookie check after an error */
  9178. u8 nested; /* Number of nested calls to the parser/code generator */
  9179. u8 parseError; /* True after a parsing error. Ticket #1794 */
  9180. u8 nTempReg; /* Number of temporary registers in aTempReg[] */
  9181. u8 nTempInUse; /* Number of aTempReg[] currently checked out */
  9182. int aTempReg[8]; /* Holding area for temporary registers */
  9183. int nRangeReg; /* Size of the temporary register block */
  9184. int iRangeReg; /* First register in temporary register block */
  9185. int nErr; /* Number of errors seen */
  9186. int nTab; /* Number of previously allocated VDBE cursors */
  9187. int nMem; /* Number of memory cells used so far */
  9188. int nSet; /* Number of sets used so far */
  9189. int ckBase; /* Base register of data during check constraints */
  9190. int iCacheLevel; /* ColCache valid when aColCache[].iLevel<=iCacheLevel */
  9191. int iCacheCnt; /* Counter used to generate aColCache[].lru values */
  9192. u8 nColCache; /* Number of entries in the column cache */
  9193. u8 iColCache; /* Next entry of the cache to replace */
  9194. struct yColCache {
  9195. int iTable; /* Table cursor number */
  9196. int iColumn; /* Table column number */
  9197. u8 tempReg; /* iReg is a temp register that needs to be freed */
  9198. int iLevel; /* Nesting level */
  9199. int iReg; /* Reg with value of this column. 0 means none. */
  9200. int lru; /* Least recently used entry has the smallest value */
  9201. } aColCache[SQLITE_N_COLCACHE]; /* One for each column cache entry */
  9202. u32 writeMask; /* Start a write transaction on these databases */
  9203. u32 cookieMask; /* Bitmask of schema verified databases */
  9204. u8 isMultiWrite; /* True if statement may affect/insert multiple rows */
  9205. u8 mayAbort; /* True if statement may throw an ABORT exception */
  9206. int cookieGoto; /* Address of OP_Goto to cookie verifier subroutine */
  9207. int cookieValue[SQLITE_MAX_ATTACHED+2]; /* Values of cookies to verify */
  9208. #ifndef SQLITE_OMIT_SHARED_CACHE
  9209. int nTableLock; /* Number of locks in aTableLock */
  9210. TableLock *aTableLock; /* Required table locks for shared-cache mode */
  9211. #endif
  9212. int regRowid; /* Register holding rowid of CREATE TABLE entry */
  9213. int regRoot; /* Register holding root page number for new objects */
  9214. AutoincInfo *pAinc; /* Information about AUTOINCREMENT counters */
  9215. int nMaxArg; /* Max args passed to user function by sub-program */
  9216. /* Information used while coding trigger programs. */
  9217. Parse *pToplevel; /* Parse structure for main program (or NULL) */
  9218. Table *pTriggerTab; /* Table triggers are being coded for */
  9219. u32 oldmask; /* Mask of old.* columns referenced */
  9220. u32 newmask; /* Mask of new.* columns referenced */
  9221. u8 eTriggerOp; /* TK_UPDATE, TK_INSERT or TK_DELETE */
  9222. u8 eOrconf; /* Default ON CONFLICT policy for trigger steps */
  9223. u8 disableTriggers; /* True to disable triggers */
  9224. /* Above is constant between recursions. Below is reset before and after
  9225. ** each recursion */
  9226. int nVar; /* Number of '?' variables seen in the SQL so far */
  9227. int nVarExpr; /* Number of used slots in apVarExpr[] */
  9228. int nVarExprAlloc; /* Number of allocated slots in apVarExpr[] */
  9229. Expr **apVarExpr; /* Pointers to :aaa and $aaaa wildcard expressions */
  9230. Vdbe *pReprepare; /* VM being reprepared (sqlite3Reprepare()) */
  9231. int nAlias; /* Number of aliased result set columns */
  9232. int nAliasAlloc; /* Number of allocated slots for aAlias[] */
  9233. int *aAlias; /* Register used to hold aliased result */
  9234. u8 explain; /* True if the EXPLAIN flag is found on the query */
  9235. Token sNameToken; /* Token with unqualified schema object name */
  9236. Token sLastToken; /* The last token parsed */
  9237. const char *zTail; /* All SQL text past the last semicolon parsed */
  9238. Table *pNewTable; /* A table being constructed by CREATE TABLE */
  9239. Trigger *pNewTrigger; /* Trigger under construct by a CREATE TRIGGER */
  9240. const char *zAuthContext; /* The 6th parameter to db->xAuth callbacks */
  9241. #ifndef SQLITE_OMIT_VIRTUALTABLE
  9242. Token sArg; /* Complete text of a module argument */
  9243. u8 declareVtab; /* True if inside sqlite3_declare_vtab() */
  9244. int nVtabLock; /* Number of virtual tables to lock */
  9245. Table **apVtabLock; /* Pointer to virtual tables needing locking */
  9246. #endif
  9247. int nHeight; /* Expression tree height of current sub-select */
  9248. Table *pZombieTab; /* List of Table objects to delete after code gen */
  9249. TriggerPrg *pTriggerPrg; /* Linked list of coded triggers */
  9250. };
  9251. #ifdef SQLITE_OMIT_VIRTUALTABLE
  9252. #define IN_DECLARE_VTAB 0
  9253. #else
  9254. #define IN_DECLARE_VTAB (pParse->declareVtab)
  9255. #endif
  9256. /*
  9257. ** An instance of the following structure can be declared on a stack and used
  9258. ** to save the Parse.zAuthContext value so that it can be restored later.
  9259. */
  9260. struct AuthContext {
  9261. const char *zAuthContext; /* Put saved Parse.zAuthContext here */
  9262. Parse *pParse; /* The Parse structure */
  9263. };
  9264. /*
  9265. ** Bitfield flags for P5 value in OP_Insert and OP_Delete
  9266. */
  9267. #define OPFLAG_NCHANGE 0x01 /* Set to update db->nChange */
  9268. #define OPFLAG_LASTROWID 0x02 /* Set to update db->lastRowid */
  9269. #define OPFLAG_ISUPDATE 0x04 /* This OP_Insert is an sql UPDATE */
  9270. #define OPFLAG_APPEND 0x08 /* This is likely to be an append */
  9271. #define OPFLAG_USESEEKRESULT 0x10 /* Try to avoid a seek in BtreeInsert() */
  9272. #define OPFLAG_CLEARCACHE 0x20 /* Clear pseudo-table cache in OP_Column */
  9273. /*
  9274. * Each trigger present in the database schema is stored as an instance of
  9275. * struct Trigger.
  9276. *
  9277. * Pointers to instances of struct Trigger are stored in two ways.
  9278. * 1. In the "trigHash" hash table (part of the sqlite3* that represents the
  9279. * database). This allows Trigger structures to be retrieved by name.
  9280. * 2. All triggers associated with a single table form a linked list, using the
  9281. * pNext member of struct Trigger. A pointer to the first element of the
  9282. * linked list is stored as the "pTrigger" member of the associated
  9283. * struct Table.
  9284. *
  9285. * The "step_list" member points to the first element of a linked list
  9286. * containing the SQL statements specified as the trigger program.
  9287. */
  9288. struct Trigger {
  9289. char *zName; /* The name of the trigger */
  9290. char *table; /* The table or view to which the trigger applies */
  9291. u8 op; /* One of TK_DELETE, TK_UPDATE, TK_INSERT */
  9292. u8 tr_tm; /* One of TRIGGER_BEFORE, TRIGGER_AFTER */
  9293. Expr *pWhen; /* The WHEN clause of the expression (may be NULL) */
  9294. IdList *pColumns; /* If this is an UPDATE OF <column-list> trigger,
  9295. the <column-list> is stored here */
  9296. Schema *pSchema; /* Schema containing the trigger */
  9297. Schema *pTabSchema; /* Schema containing the table */
  9298. TriggerStep *step_list; /* Link list of trigger program steps */
  9299. Trigger *pNext; /* Next trigger associated with the table */
  9300. };
  9301. /*
  9302. ** A trigger is either a BEFORE or an AFTER trigger. The following constants
  9303. ** determine which.
  9304. **
  9305. ** If there are multiple triggers, you might of some BEFORE and some AFTER.
  9306. ** In that cases, the constants below can be ORed together.
  9307. */
  9308. #define TRIGGER_BEFORE 1
  9309. #define TRIGGER_AFTER 2
  9310. /*
  9311. * An instance of struct TriggerStep is used to store a single SQL statement
  9312. * that is a part of a trigger-program.
  9313. *
  9314. * Instances of struct TriggerStep are stored in a singly linked list (linked
  9315. * using the "pNext" member) referenced by the "step_list" member of the
  9316. * associated struct Trigger instance. The first element of the linked list is
  9317. * the first step of the trigger-program.
  9318. *
  9319. * The "op" member indicates whether this is a "DELETE", "INSERT", "UPDATE" or
  9320. * "SELECT" statement. The meanings of the other members is determined by the
  9321. * value of "op" as follows:
  9322. *
  9323. * (op == TK_INSERT)
  9324. * orconf -> stores the ON CONFLICT algorithm
  9325. * pSelect -> If this is an INSERT INTO ... SELECT ... statement, then
  9326. * this stores a pointer to the SELECT statement. Otherwise NULL.
  9327. * target -> A token holding the quoted name of the table to insert into.
  9328. * pExprList -> If this is an INSERT INTO ... VALUES ... statement, then
  9329. * this stores values to be inserted. Otherwise NULL.
  9330. * pIdList -> If this is an INSERT INTO ... (<column-names>) VALUES ...
  9331. * statement, then this stores the column-names to be
  9332. * inserted into.
  9333. *
  9334. * (op == TK_DELETE)
  9335. * target -> A token holding the quoted name of the table to delete from.
  9336. * pWhere -> The WHERE clause of the DELETE statement if one is specified.
  9337. * Otherwise NULL.
  9338. *
  9339. * (op == TK_UPDATE)
  9340. * target -> A token holding the quoted name of the table to update rows of.
  9341. * pWhere -> The WHERE clause of the UPDATE statement if one is specified.
  9342. * Otherwise NULL.
  9343. * pExprList -> A list of the columns to update and the expressions to update
  9344. * them to. See sqlite3Update() documentation of "pChanges"
  9345. * argument.
  9346. *
  9347. */
  9348. struct TriggerStep {
  9349. u8 op; /* One of TK_DELETE, TK_UPDATE, TK_INSERT, TK_SELECT */
  9350. u8 orconf; /* OE_Rollback etc. */
  9351. Trigger *pTrig; /* The trigger that this step is a part of */
  9352. Select *pSelect; /* SELECT statment or RHS of INSERT INTO .. SELECT ... */
  9353. Token target; /* Target table for DELETE, UPDATE, INSERT */
  9354. Expr *pWhere; /* The WHERE clause for DELETE or UPDATE steps */
  9355. ExprList *pExprList; /* SET clause for UPDATE. VALUES clause for INSERT */
  9356. IdList *pIdList; /* Column names for INSERT */
  9357. TriggerStep *pNext; /* Next in the link-list */
  9358. TriggerStep *pLast; /* Last element in link-list. Valid for 1st elem only */
  9359. };
  9360. /*
  9361. ** The following structure contains information used by the sqliteFix...
  9362. ** routines as they walk the parse tree to make database references
  9363. ** explicit.
  9364. */
  9365. typedef struct DbFixer DbFixer;
  9366. struct DbFixer {
  9367. Parse *pParse; /* The parsing context. Error messages written here */
  9368. const char *zDb; /* Make sure all objects are contained in this database */
  9369. const char *zType; /* Type of the container - used for error messages */
  9370. const Token *pName; /* Name of the container - used for error messages */
  9371. };
  9372. /*
  9373. ** An objected used to accumulate the text of a string where we
  9374. ** do not necessarily know how big the string will be in the end.
  9375. */
  9376. struct StrAccum {
  9377. sqlite3 *db; /* Optional database for lookaside. Can be NULL */
  9378. char *zBase; /* A base allocation. Not from malloc. */
  9379. char *zText; /* The string collected so far */
  9380. int nChar; /* Length of the string so far */
  9381. int nAlloc; /* Amount of space allocated in zText */
  9382. int mxAlloc; /* Maximum allowed string length */
  9383. u8 mallocFailed; /* Becomes true if any memory allocation fails */
  9384. u8 useMalloc; /* True if zText is enlargeable using realloc */
  9385. u8 tooBig; /* Becomes true if string size exceeds limits */
  9386. };
  9387. /*
  9388. ** A pointer to this structure is used to communicate information
  9389. ** from sqlite3Init and OP_ParseSchema into the sqlite3InitCallback.
  9390. */
  9391. typedef struct {
  9392. sqlite3 *db; /* The database being initialized */
  9393. int iDb; /* 0 for main database. 1 for TEMP, 2.. for ATTACHed */
  9394. char **pzErrMsg; /* Error message stored here */
  9395. int rc; /* Result code stored here */
  9396. } InitData;
  9397. /*
  9398. ** Structure containing global configuration data for the SQLite library.
  9399. **
  9400. ** This structure also contains some state information.
  9401. */
  9402. struct Sqlite3Config {
  9403. int bMemstat; /* True to enable memory status */
  9404. int bCoreMutex; /* True to enable core mutexing */
  9405. int bFullMutex; /* True to enable full mutexing */
  9406. int mxStrlen; /* Maximum string length */
  9407. int szLookaside; /* Default lookaside buffer size */
  9408. int nLookaside; /* Default lookaside buffer count */
  9409. sqlite3_mem_methods m; /* Low-level memory allocation interface */
  9410. sqlite3_mutex_methods mutex; /* Low-level mutex interface */
  9411. sqlite3_pcache_methods pcache; /* Low-level page-cache interface */
  9412. void *pHeap; /* Heap storage space */
  9413. int nHeap; /* Size of pHeap[] */
  9414. int mnReq, mxReq; /* Min and max heap requests sizes */
  9415. void *pScratch; /* Scratch memory */
  9416. int szScratch; /* Size of each scratch buffer */
  9417. int nScratch; /* Number of scratch buffers */
  9418. void *pPage; /* Page cache memory */
  9419. int szPage; /* Size of each page in pPage[] */
  9420. int nPage; /* Number of pages in pPage[] */
  9421. int mxParserStack; /* maximum depth of the parser stack */
  9422. int sharedCacheEnabled; /* true if shared-cache mode enabled */
  9423. /* The above might be initialized to non-zero. The following need to always
  9424. ** initially be zero, however. */
  9425. int isInit; /* True after initialization has finished */
  9426. int inProgress; /* True while initialization in progress */
  9427. int isMutexInit; /* True after mutexes are initialized */
  9428. int isMallocInit; /* True after malloc is initialized */
  9429. int isPCacheInit; /* True after malloc is initialized */
  9430. sqlite3_mutex *pInitMutex; /* Mutex used by sqlite3_initialize() */
  9431. int nRefInitMutex; /* Number of users of pInitMutex */
  9432. };
  9433. /*
  9434. ** Context pointer passed down through the tree-walk.
  9435. */
  9436. struct Walker {
  9437. int (*xExprCallback)(Walker*, Expr*); /* Callback for expressions */
  9438. int (*xSelectCallback)(Walker*,Select*); /* Callback for SELECTs */
  9439. Parse *pParse; /* Parser context. */
  9440. union { /* Extra data for callback */
  9441. NameContext *pNC; /* Naming context */
  9442. int i; /* Integer value */
  9443. } u;
  9444. };
  9445. /* Forward declarations */
  9446. SQLITE_PRIVATE int sqlite3WalkExpr(Walker*, Expr*);
  9447. SQLITE_PRIVATE int sqlite3WalkExprList(Walker*, ExprList*);
  9448. SQLITE_PRIVATE int sqlite3WalkSelect(Walker*, Select*);
  9449. SQLITE_PRIVATE int sqlite3WalkSelectExpr(Walker*, Select*);
  9450. SQLITE_PRIVATE int sqlite3WalkSelectFrom(Walker*, Select*);
  9451. /*
  9452. ** Return code from the parse-tree walking primitives and their
  9453. ** callbacks.
  9454. */
  9455. #define WRC_Continue 0 /* Continue down into children */
  9456. #define WRC_Prune 1 /* Omit children but continue walking siblings */
  9457. #define WRC_Abort 2 /* Abandon the tree walk */
  9458. /*
  9459. ** Assuming zIn points to the first byte of a UTF-8 character,
  9460. ** advance zIn to point to the first byte of the next UTF-8 character.
  9461. */
  9462. #define SQLITE_SKIP_UTF8(zIn) { \
  9463. if( (*(zIn++))>=0xc0 ){ \
  9464. while( (*zIn & 0xc0)==0x80 ){ zIn++; } \
  9465. } \
  9466. }
  9467. /*
  9468. ** The SQLITE_CORRUPT_BKPT macro can be either a constant (for production
  9469. ** builds) or a function call (for debugging). If it is a function call,
  9470. ** it allows the operator to set a breakpoint at the spot where database
  9471. ** corruption is first detected.
  9472. */
  9473. #ifdef SQLITE_DEBUG
  9474. SQLITE_PRIVATE int sqlite3Corrupt(void);
  9475. # define SQLITE_CORRUPT_BKPT sqlite3Corrupt()
  9476. #else
  9477. # define SQLITE_CORRUPT_BKPT SQLITE_CORRUPT
  9478. #endif
  9479. /*
  9480. ** The ctype.h header is needed for non-ASCII systems. It is also
  9481. ** needed by FTS3 when FTS3 is included in the amalgamation.
  9482. */
  9483. #if !defined(SQLITE_ASCII) || \
  9484. (defined(SQLITE_ENABLE_FTS3) && defined(SQLITE_AMALGAMATION))
  9485. # include <ctype.h>
  9486. #endif
  9487. /*
  9488. ** The following macros mimic the standard library functions toupper(),
  9489. ** isspace(), isalnum(), isdigit() and isxdigit(), respectively. The
  9490. ** sqlite versions only work for ASCII characters, regardless of locale.
  9491. */
  9492. #ifdef SQLITE_ASCII
  9493. # define sqlite3Toupper(x) ((x)&~(sqlite3CtypeMap[(unsigned char)(x)]&0x20))
  9494. # define sqlite3Isspace(x) (sqlite3CtypeMap[(unsigned char)(x)]&0x01)
  9495. # define sqlite3Isalnum(x) (sqlite3CtypeMap[(unsigned char)(x)]&0x06)
  9496. # define sqlite3Isalpha(x) (sqlite3CtypeMap[(unsigned char)(x)]&0x02)
  9497. # define sqlite3Isdigit(x) (sqlite3CtypeMap[(unsigned char)(x)]&0x04)
  9498. # define sqlite3Isxdigit(x) (sqlite3CtypeMap[(unsigned char)(x)]&0x08)
  9499. # define sqlite3Tolower(x) (sqlite3UpperToLower[(unsigned char)(x)])
  9500. #else
  9501. # define sqlite3Toupper(x) toupper((unsigned char)(x))
  9502. # define sqlite3Isspace(x) isspace((unsigned char)(x))
  9503. # define sqlite3Isalnum(x) isalnum((unsigned char)(x))
  9504. # define sqlite3Isalpha(x) isalpha((unsigned char)(x))
  9505. # define sqlite3Isdigit(x) isdigit((unsigned char)(x))
  9506. # define sqlite3Isxdigit(x) isxdigit((unsigned char)(x))
  9507. # define sqlite3Tolower(x) tolower((unsigned char)(x))
  9508. #endif
  9509. /*
  9510. ** Internal function prototypes
  9511. */
  9512. SQLITE_PRIVATE int sqlite3StrICmp(const char *, const char *);
  9513. SQLITE_PRIVATE int sqlite3IsNumber(const char*, int*, u8);
  9514. SQLITE_PRIVATE int sqlite3Strlen30(const char*);
  9515. #define sqlite3StrNICmp sqlite3_strnicmp
  9516. SQLITE_PRIVATE int sqlite3MallocInit(void);
  9517. SQLITE_PRIVATE void sqlite3MallocEnd(void);
  9518. SQLITE_PRIVATE void *sqlite3Malloc(int);
  9519. SQLITE_PRIVATE void *sqlite3MallocZero(int);
  9520. SQLITE_PRIVATE void *sqlite3DbMallocZero(sqlite3*, int);
  9521. SQLITE_PRIVATE void *sqlite3DbMallocRaw(sqlite3*, int);
  9522. SQLITE_PRIVATE char *sqlite3DbStrDup(sqlite3*,const char*);
  9523. SQLITE_PRIVATE char *sqlite3DbStrNDup(sqlite3*,const char*, int);
  9524. SQLITE_PRIVATE void *sqlite3Realloc(void*, int);
  9525. SQLITE_PRIVATE void *sqlite3DbReallocOrFree(sqlite3 *, void *, int);
  9526. SQLITE_PRIVATE void *sqlite3DbRealloc(sqlite3 *, void *, int);
  9527. SQLITE_PRIVATE void sqlite3DbFree(sqlite3*, void*);
  9528. SQLITE_PRIVATE int sqlite3MallocSize(void*);
  9529. SQLITE_PRIVATE int sqlite3DbMallocSize(sqlite3*, void*);
  9530. SQLITE_PRIVATE void *sqlite3ScratchMalloc(int);
  9531. SQLITE_PRIVATE void sqlite3ScratchFree(void*);
  9532. SQLITE_PRIVATE void *sqlite3PageMalloc(int);
  9533. SQLITE_PRIVATE void sqlite3PageFree(void*);
  9534. SQLITE_PRIVATE void sqlite3MemSetDefault(void);
  9535. SQLITE_PRIVATE void sqlite3BenignMallocHooks(void (*)(void), void (*)(void));
  9536. SQLITE_PRIVATE int sqlite3MemoryAlarm(void (*)(void*, sqlite3_int64, int), void*, sqlite3_int64);
  9537. /*
  9538. ** On systems with ample stack space and that support alloca(), make
  9539. ** use of alloca() to obtain space for large automatic objects. By default,
  9540. ** obtain space from malloc().
  9541. **
  9542. ** The alloca() routine never returns NULL. This will cause code paths
  9543. ** that deal with sqlite3StackAlloc() failures to be unreachable.
  9544. */
  9545. #ifdef SQLITE_USE_ALLOCA
  9546. # define sqlite3StackAllocRaw(D,N) alloca(N)
  9547. # define sqlite3StackAllocZero(D,N) memset(alloca(N), 0, N)
  9548. # define sqlite3StackFree(D,P)
  9549. #else
  9550. # define sqlite3StackAllocRaw(D,N) sqlite3DbMallocRaw(D,N)
  9551. # define sqlite3StackAllocZero(D,N) sqlite3DbMallocZero(D,N)
  9552. # define sqlite3StackFree(D,P) sqlite3DbFree(D,P)
  9553. #endif
  9554. #ifdef SQLITE_ENABLE_MEMSYS3
  9555. SQLITE_PRIVATE const sqlite3_mem_methods *sqlite3MemGetMemsys3(void);
  9556. #endif
  9557. #ifdef SQLITE_ENABLE_MEMSYS5
  9558. SQLITE_PRIVATE const sqlite3_mem_methods *sqlite3MemGetMemsys5(void);
  9559. #endif
  9560. #ifndef SQLITE_MUTEX_OMIT
  9561. SQLITE_PRIVATE sqlite3_mutex_methods *sqlite3DefaultMutex(void);
  9562. SQLITE_PRIVATE sqlite3_mutex *sqlite3MutexAlloc(int);
  9563. SQLITE_PRIVATE int sqlite3MutexInit(void);
  9564. SQLITE_PRIVATE int sqlite3MutexEnd(void);
  9565. #endif
  9566. SQLITE_PRIVATE int sqlite3StatusValue(int);
  9567. SQLITE_PRIVATE void sqlite3StatusAdd(int, int);
  9568. SQLITE_PRIVATE void sqlite3StatusSet(int, int);
  9569. SQLITE_PRIVATE int sqlite3IsNaN(double);
  9570. SQLITE_PRIVATE void sqlite3VXPrintf(StrAccum*, int, const char*, va_list);
  9571. #ifndef SQLITE_OMIT_TRACE
  9572. SQLITE_PRIVATE void sqlite3XPrintf(StrAccum*, const char*, ...);
  9573. #endif
  9574. SQLITE_PRIVATE char *sqlite3MPrintf(sqlite3*,const char*, ...);
  9575. SQLITE_PRIVATE char *sqlite3VMPrintf(sqlite3*,const char*, va_list);
  9576. SQLITE_PRIVATE char *sqlite3MAppendf(sqlite3*,char*,const char*,...);
  9577. #if defined(SQLITE_TEST) || defined(SQLITE_DEBUG)
  9578. SQLITE_PRIVATE void sqlite3DebugPrintf(const char*, ...);
  9579. #endif
  9580. #if defined(SQLITE_TEST)
  9581. SQLITE_PRIVATE void *sqlite3TestTextToPtr(const char*);
  9582. #endif
  9583. SQLITE_PRIVATE void sqlite3SetString(char **, sqlite3*, const char*, ...);
  9584. SQLITE_PRIVATE void sqlite3ErrorMsg(Parse*, const char*, ...);
  9585. SQLITE_PRIVATE void sqlite3ErrorClear(Parse*);
  9586. SQLITE_PRIVATE int sqlite3Dequote(char*);
  9587. SQLITE_PRIVATE int sqlite3KeywordCode(const unsigned char*, int);
  9588. SQLITE_PRIVATE int sqlite3RunParser(Parse*, const char*, char **);
  9589. SQLITE_PRIVATE void sqlite3FinishCoding(Parse*);
  9590. SQLITE_PRIVATE int sqlite3GetTempReg(Parse*);
  9591. SQLITE_PRIVATE void sqlite3ReleaseTempReg(Parse*,int);
  9592. SQLITE_PRIVATE int sqlite3GetTempRange(Parse*,int);
  9593. SQLITE_PRIVATE void sqlite3ReleaseTempRange(Parse*,int,int);
  9594. SQLITE_PRIVATE Expr *sqlite3ExprAlloc(sqlite3*,int,const Token*,int);
  9595. SQLITE_PRIVATE Expr *sqlite3Expr(sqlite3*,int,const char*);
  9596. SQLITE_PRIVATE void sqlite3ExprAttachSubtrees(sqlite3*,Expr*,Expr*,Expr*);
  9597. SQLITE_PRIVATE Expr *sqlite3PExpr(Parse*, int, Expr*, Expr*, const Token*);
  9598. SQLITE_PRIVATE Expr *sqlite3ExprAnd(sqlite3*,Expr*, Expr*);
  9599. SQLITE_PRIVATE Expr *sqlite3ExprFunction(Parse*,ExprList*, Token*);
  9600. SQLITE_PRIVATE void sqlite3ExprAssignVarNumber(Parse*, Expr*);
  9601. SQLITE_PRIVATE void sqlite3ExprDelete(sqlite3*, Expr*);
  9602. SQLITE_PRIVATE ExprList *sqlite3ExprListAppend(Parse*,ExprList*,Expr*);
  9603. SQLITE_PRIVATE void sqlite3ExprListSetName(Parse*,ExprList*,Token*,int);
  9604. SQLITE_PRIVATE void sqlite3ExprListSetSpan(Parse*,ExprList*,ExprSpan*);
  9605. SQLITE_PRIVATE void sqlite3ExprListDelete(sqlite3*, ExprList*);
  9606. SQLITE_PRIVATE int sqlite3Init(sqlite3*, char**);
  9607. SQLITE_PRIVATE int sqlite3InitCallback(void*, int, char**, char**);
  9608. SQLITE_PRIVATE void sqlite3Pragma(Parse*,Token*,Token*,Token*,int);
  9609. SQLITE_PRIVATE void sqlite3ResetInternalSchema(sqlite3*, int);
  9610. SQLITE_PRIVATE void sqlite3BeginParse(Parse*,int);
  9611. SQLITE_PRIVATE void sqlite3CommitInternalChanges(sqlite3*);
  9612. SQLITE_PRIVATE Table *sqlite3ResultSetOfSelect(Parse*,Select*);
  9613. SQLITE_PRIVATE void sqlite3OpenMasterTable(Parse *, int);
  9614. SQLITE_PRIVATE void sqlite3StartTable(Parse*,Token*,Token*,int,int,int,int);
  9615. SQLITE_PRIVATE void sqlite3AddColumn(Parse*,Token*);
  9616. SQLITE_PRIVATE void sqlite3AddNotNull(Parse*, int);
  9617. SQLITE_PRIVATE void sqlite3AddPrimaryKey(Parse*, ExprList*, int, int, int);
  9618. SQLITE_PRIVATE void sqlite3AddCheckConstraint(Parse*, Expr*);
  9619. SQLITE_PRIVATE void sqlite3AddColumnType(Parse*,Token*);
  9620. SQLITE_PRIVATE void sqlite3AddDefaultValue(Parse*,ExprSpan*);
  9621. SQLITE_PRIVATE void sqlite3AddCollateType(Parse*, Token*);
  9622. SQLITE_PRIVATE void sqlite3EndTable(Parse*,Token*,Token*,Select*);
  9623. SQLITE_PRIVATE Bitvec *sqlite3BitvecCreate(u32);
  9624. SQLITE_PRIVATE int sqlite3BitvecTest(Bitvec*, u32);
  9625. SQLITE_PRIVATE int sqlite3BitvecSet(Bitvec*, u32);
  9626. SQLITE_PRIVATE void sqlite3BitvecClear(Bitvec*, u32, void*);
  9627. SQLITE_PRIVATE void sqlite3BitvecDestroy(Bitvec*);
  9628. SQLITE_PRIVATE u32 sqlite3BitvecSize(Bitvec*);
  9629. SQLITE_PRIVATE int sqlite3BitvecBuiltinTest(int,int*);
  9630. SQLITE_PRIVATE RowSet *sqlite3RowSetInit(sqlite3*, void*, unsigned int);
  9631. SQLITE_PRIVATE void sqlite3RowSetClear(RowSet*);
  9632. SQLITE_PRIVATE void sqlite3RowSetInsert(RowSet*, i64);
  9633. SQLITE_PRIVATE int sqlite3RowSetTest(RowSet*, u8 iBatch, i64);
  9634. SQLITE_PRIVATE int sqlite3RowSetNext(RowSet*, i64*);
  9635. SQLITE_PRIVATE void sqlite3CreateView(Parse*,Token*,Token*,Token*,Select*,int,int);
  9636. #if !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_VIRTUALTABLE)
  9637. SQLITE_PRIVATE int sqlite3ViewGetColumnNames(Parse*,Table*);
  9638. #else
  9639. # define sqlite3ViewGetColumnNames(A,B) 0
  9640. #endif
  9641. SQLITE_PRIVATE void sqlite3DropTable(Parse*, SrcList*, int, int);
  9642. SQLITE_PRIVATE void sqlite3DeleteTable(Table*);
  9643. #ifndef SQLITE_OMIT_AUTOINCREMENT
  9644. SQLITE_PRIVATE void sqlite3AutoincrementBegin(Parse *pParse);
  9645. SQLITE_PRIVATE void sqlite3AutoincrementEnd(Parse *pParse);
  9646. #else
  9647. # define sqlite3AutoincrementBegin(X)
  9648. # define sqlite3AutoincrementEnd(X)
  9649. #endif
  9650. SQLITE_PRIVATE void sqlite3Insert(Parse*, SrcList*, ExprList*, Select*, IdList*, int);
  9651. SQLITE_PRIVATE void *sqlite3ArrayAllocate(sqlite3*,void*,int,int,int*,int*,int*);
  9652. SQLITE_PRIVATE IdList *sqlite3IdListAppend(sqlite3*, IdList*, Token*);
  9653. SQLITE_PRIVATE int sqlite3IdListIndex(IdList*,const char*);
  9654. SQLITE_PRIVATE SrcList *sqlite3SrcListEnlarge(sqlite3*, SrcList*, int, int);
  9655. SQLITE_PRIVATE SrcList *sqlite3SrcListAppend(sqlite3*, SrcList*, Token*, Token*);
  9656. SQLITE_PRIVATE SrcList *sqlite3SrcListAppendFromTerm(Parse*, SrcList*, Token*, Token*,
  9657. Token*, Select*, Expr*, IdList*);
  9658. SQLITE_PRIVATE void sqlite3SrcListIndexedBy(Parse *, SrcList *, Token *);
  9659. SQLITE_PRIVATE int sqlite3IndexedByLookup(Parse *, struct SrcList_item *);
  9660. SQLITE_PRIVATE void sqlite3SrcListShiftJoinType(SrcList*);
  9661. SQLITE_PRIVATE void sqlite3SrcListAssignCursors(Parse*, SrcList*);
  9662. SQLITE_PRIVATE void sqlite3IdListDelete(sqlite3*, IdList*);
  9663. SQLITE_PRIVATE void sqlite3SrcListDelete(sqlite3*, SrcList*);
  9664. SQLITE_PRIVATE Index *sqlite3CreateIndex(Parse*,Token*,Token*,SrcList*,ExprList*,int,Token*,
  9665. Token*, int, int);
  9666. SQLITE_PRIVATE void sqlite3DropIndex(Parse*, SrcList*, int);
  9667. SQLITE_PRIVATE int sqlite3Select(Parse*, Select*, SelectDest*);
  9668. SQLITE_PRIVATE Select *sqlite3SelectNew(Parse*,ExprList*,SrcList*,Expr*,ExprList*,
  9669. Expr*,ExprList*,int,Expr*,Expr*);
  9670. SQLITE_PRIVATE void sqlite3SelectDelete(sqlite3*, Select*);
  9671. SQLITE_PRIVATE Table *sqlite3SrcListLookup(Parse*, SrcList*);
  9672. SQLITE_PRIVATE int sqlite3IsReadOnly(Parse*, Table*, int);
  9673. SQLITE_PRIVATE void sqlite3OpenTable(Parse*, int iCur, int iDb, Table*, int);
  9674. #if defined(SQLITE_ENABLE_UPDATE_DELETE_LIMIT) && !defined(SQLITE_OMIT_SUBQUERY)
  9675. SQLITE_PRIVATE Expr *sqlite3LimitWhere(Parse *, SrcList *, Expr *, ExprList *, Expr *, Expr *, char *);
  9676. #endif
  9677. SQLITE_PRIVATE void sqlite3DeleteFrom(Parse*, SrcList*, Expr*);
  9678. SQLITE_PRIVATE void sqlite3Update(Parse*, SrcList*, ExprList*, Expr*, int);
  9679. SQLITE_PRIVATE WhereInfo *sqlite3WhereBegin(Parse*, SrcList*, Expr*, ExprList**, u16);
  9680. SQLITE_PRIVATE void sqlite3WhereEnd(WhereInfo*);
  9681. SQLITE_PRIVATE int sqlite3ExprCodeGetColumn(Parse*, Table*, int, int, int);
  9682. SQLITE_PRIVATE void sqlite3ExprCodeMove(Parse*, int, int, int);
  9683. SQLITE_PRIVATE void sqlite3ExprCodeCopy(Parse*, int, int, int);
  9684. SQLITE_PRIVATE void sqlite3ExprCacheStore(Parse*, int, int, int);
  9685. SQLITE_PRIVATE void sqlite3ExprCachePush(Parse*);
  9686. SQLITE_PRIVATE void sqlite3ExprCachePop(Parse*, int);
  9687. SQLITE_PRIVATE void sqlite3ExprCacheRemove(Parse*, int, int);
  9688. SQLITE_PRIVATE void sqlite3ExprCacheClear(Parse*);
  9689. SQLITE_PRIVATE void sqlite3ExprCacheAffinityChange(Parse*, int, int);
  9690. SQLITE_PRIVATE void sqlite3ExprHardCopy(Parse*,int,int);
  9691. SQLITE_PRIVATE int sqlite3ExprCode(Parse*, Expr*, int);
  9692. SQLITE_PRIVATE int sqlite3ExprCodeTemp(Parse*, Expr*, int*);
  9693. SQLITE_PRIVATE int sqlite3ExprCodeTarget(Parse*, Expr*, int);
  9694. SQLITE_PRIVATE int sqlite3ExprCodeAndCache(Parse*, Expr*, int);
  9695. SQLITE_PRIVATE void sqlite3ExprCodeConstants(Parse*, Expr*);
  9696. SQLITE_PRIVATE int sqlite3ExprCodeExprList(Parse*, ExprList*, int, int);
  9697. SQLITE_PRIVATE void sqlite3ExprIfTrue(Parse*, Expr*, int, int);
  9698. SQLITE_PRIVATE void sqlite3ExprIfFalse(Parse*, Expr*, int, int);
  9699. SQLITE_PRIVATE Table *sqlite3FindTable(sqlite3*,const char*, const char*);
  9700. SQLITE_PRIVATE Table *sqlite3LocateTable(Parse*,int isView,const char*, const char*);
  9701. SQLITE_PRIVATE Index *sqlite3FindIndex(sqlite3*,const char*, const char*);
  9702. SQLITE_PRIVATE void sqlite3UnlinkAndDeleteTable(sqlite3*,int,const char*);
  9703. SQLITE_PRIVATE void sqlite3UnlinkAndDeleteIndex(sqlite3*,int,const char*);
  9704. SQLITE_PRIVATE void sqlite3Vacuum(Parse*);
  9705. SQLITE_PRIVATE int sqlite3RunVacuum(char**, sqlite3*);
  9706. SQLITE_PRIVATE char *sqlite3NameFromToken(sqlite3*, Token*);
  9707. SQLITE_PRIVATE int sqlite3ExprCompare(Expr*, Expr*);
  9708. SQLITE_PRIVATE void sqlite3ExprAnalyzeAggregates(NameContext*, Expr*);
  9709. SQLITE_PRIVATE void sqlite3ExprAnalyzeAggList(NameContext*,ExprList*);
  9710. SQLITE_PRIVATE Vdbe *sqlite3GetVdbe(Parse*);
  9711. SQLITE_PRIVATE void sqlite3PrngSaveState(void);
  9712. SQLITE_PRIVATE void sqlite3PrngRestoreState(void);
  9713. SQLITE_PRIVATE void sqlite3PrngResetState(void);
  9714. SQLITE_PRIVATE void sqlite3RollbackAll(sqlite3*);
  9715. SQLITE_PRIVATE void sqlite3CodeVerifySchema(Parse*, int);
  9716. SQLITE_PRIVATE void sqlite3BeginTransaction(Parse*, int);
  9717. SQLITE_PRIVATE void sqlite3CommitTransaction(Parse*);
  9718. SQLITE_PRIVATE void sqlite3RollbackTransaction(Parse*);
  9719. SQLITE_PRIVATE void sqlite3Savepoint(Parse*, int, Token*);
  9720. SQLITE_PRIVATE void sqlite3CloseSavepoints(sqlite3 *);
  9721. SQLITE_PRIVATE int sqlite3ExprIsConstant(Expr*);
  9722. SQLITE_PRIVATE int sqlite3ExprIsConstantNotJoin(Expr*);
  9723. SQLITE_PRIVATE int sqlite3ExprIsConstantOrFunction(Expr*);
  9724. SQLITE_PRIVATE int sqlite3ExprIsInteger(Expr*, int*);
  9725. SQLITE_PRIVATE int sqlite3ExprCanBeNull(const Expr*);
  9726. SQLITE_PRIVATE void sqlite3ExprCodeIsNullJump(Vdbe*, const Expr*, int, int);
  9727. SQLITE_PRIVATE int sqlite3ExprNeedsNoAffinityChange(const Expr*, char);
  9728. SQLITE_PRIVATE int sqlite3IsRowid(const char*);
  9729. SQLITE_PRIVATE void sqlite3GenerateRowDelete(Parse*, Table*, int, int, int, Trigger *, int);
  9730. SQLITE_PRIVATE void sqlite3GenerateRowIndexDelete(Parse*, Table*, int, int*);
  9731. SQLITE_PRIVATE int sqlite3GenerateIndexKey(Parse*, Index*, int, int, int);
  9732. SQLITE_PRIVATE void sqlite3GenerateConstraintChecks(Parse*,Table*,int,int,
  9733. int*,int,int,int,int,int*);
  9734. SQLITE_PRIVATE void sqlite3CompleteInsertion(Parse*, Table*, int, int, int*, int, int, int);
  9735. SQLITE_PRIVATE int sqlite3OpenTableAndIndices(Parse*, Table*, int, int);
  9736. SQLITE_PRIVATE void sqlite3BeginWriteOperation(Parse*, int, int);
  9737. SQLITE_PRIVATE void sqlite3MultiWrite(Parse*);
  9738. SQLITE_PRIVATE void sqlite3MayAbort(Parse*);
  9739. SQLITE_PRIVATE void sqlite3HaltConstraint(Parse*, int, char*, int);
  9740. SQLITE_PRIVATE Expr *sqlite3ExprDup(sqlite3*,Expr*,int);
  9741. SQLITE_PRIVATE ExprList *sqlite3ExprListDup(sqlite3*,ExprList*,int);
  9742. SQLITE_PRIVATE SrcList *sqlite3SrcListDup(sqlite3*,SrcList*,int);
  9743. SQLITE_PRIVATE IdList *sqlite3IdListDup(sqlite3*,IdList*);
  9744. SQLITE_PRIVATE Select *sqlite3SelectDup(sqlite3*,Select*,int);
  9745. SQLITE_PRIVATE void sqlite3FuncDefInsert(FuncDefHash*, FuncDef*);
  9746. SQLITE_PRIVATE FuncDef *sqlite3FindFunction(sqlite3*,const char*,int,int,u8,int);
  9747. SQLITE_PRIVATE void sqlite3RegisterBuiltinFunctions(sqlite3*);
  9748. SQLITE_PRIVATE void sqlite3RegisterDateTimeFunctions(void);
  9749. SQLITE_PRIVATE void sqlite3RegisterGlobalFunctions(void);
  9750. #ifdef SQLITE_DEBUG
  9751. SQLITE_PRIVATE int sqlite3SafetyOn(sqlite3*);
  9752. SQLITE_PRIVATE int sqlite3SafetyOff(sqlite3*);
  9753. #else
  9754. # define sqlite3SafetyOn(A) 0
  9755. # define sqlite3SafetyOff(A) 0
  9756. #endif
  9757. SQLITE_PRIVATE int sqlite3SafetyCheckOk(sqlite3*);
  9758. SQLITE_PRIVATE int sqlite3SafetyCheckSickOrOk(sqlite3*);
  9759. SQLITE_PRIVATE void sqlite3ChangeCookie(Parse*, int);
  9760. #if !defined(SQLITE_OMIT_VIEW) && !defined(SQLITE_OMIT_TRIGGER)
  9761. SQLITE_PRIVATE void sqlite3MaterializeView(Parse*, Table*, Expr*, int);
  9762. #endif
  9763. #ifndef SQLITE_OMIT_TRIGGER
  9764. SQLITE_PRIVATE void sqlite3BeginTrigger(Parse*, Token*,Token*,int,int,IdList*,SrcList*,
  9765. Expr*,int, int);
  9766. SQLITE_PRIVATE void sqlite3FinishTrigger(Parse*, TriggerStep*, Token*);
  9767. SQLITE_PRIVATE void sqlite3DropTrigger(Parse*, SrcList*, int);
  9768. SQLITE_PRIVATE void sqlite3DropTriggerPtr(Parse*, Trigger*);
  9769. SQLITE_PRIVATE Trigger *sqlite3TriggersExist(Parse *, Table*, int, ExprList*, int *pMask);
  9770. SQLITE_PRIVATE Trigger *sqlite3TriggerList(Parse *, Table *);
  9771. SQLITE_PRIVATE void sqlite3CodeRowTrigger(Parse*, Trigger *, int, ExprList*, int, Table *,
  9772. int, int, int);
  9773. SQLITE_PRIVATE void sqlite3CodeRowTriggerDirect(Parse *, Trigger *, Table *, int, int, int);
  9774. void sqliteViewTriggers(Parse*, Table*, Expr*, int, ExprList*);
  9775. SQLITE_PRIVATE void sqlite3DeleteTriggerStep(sqlite3*, TriggerStep*);
  9776. SQLITE_PRIVATE TriggerStep *sqlite3TriggerSelectStep(sqlite3*,Select*);
  9777. SQLITE_PRIVATE TriggerStep *sqlite3TriggerInsertStep(sqlite3*,Token*, IdList*,
  9778. ExprList*,Select*,u8);
  9779. SQLITE_PRIVATE TriggerStep *sqlite3TriggerUpdateStep(sqlite3*,Token*,ExprList*, Expr*, u8);
  9780. SQLITE_PRIVATE TriggerStep *sqlite3TriggerDeleteStep(sqlite3*,Token*, Expr*);
  9781. SQLITE_PRIVATE void sqlite3DeleteTrigger(sqlite3*, Trigger*);
  9782. SQLITE_PRIVATE void sqlite3UnlinkAndDeleteTrigger(sqlite3*,int,const char*);
  9783. SQLITE_PRIVATE u32 sqlite3TriggerColmask(Parse*,Trigger*,ExprList*,int,int,Table*,int);
  9784. # define sqlite3ParseToplevel(p) ((p)->pToplevel ? (p)->pToplevel : (p))
  9785. #else
  9786. # define sqlite3TriggersExist(B,C,D,E,F) 0
  9787. # define sqlite3DeleteTrigger(A,B)
  9788. # define sqlite3DropTriggerPtr(A,B)
  9789. # define sqlite3UnlinkAndDeleteTrigger(A,B,C)
  9790. # define sqlite3CodeRowTrigger(A,B,C,D,E,F,G,H,I)
  9791. # define sqlite3CodeRowTriggerDirect(A,B,C,D,E,F)
  9792. # define sqlite3TriggerList(X, Y) 0
  9793. # define sqlite3ParseToplevel(p) p
  9794. # define sqlite3TriggerColmask(A,B,C,D,E,F,G) 0
  9795. #endif
  9796. SQLITE_PRIVATE int sqlite3JoinType(Parse*, Token*, Token*, Token*);
  9797. SQLITE_PRIVATE void sqlite3CreateForeignKey(Parse*, ExprList*, Token*, ExprList*, int);
  9798. SQLITE_PRIVATE void sqlite3DeferForeignKey(Parse*, int);
  9799. #ifndef SQLITE_OMIT_AUTHORIZATION
  9800. SQLITE_PRIVATE void sqlite3AuthRead(Parse*,Expr*,Schema*,SrcList*);
  9801. SQLITE_PRIVATE int sqlite3AuthCheck(Parse*,int, const char*, const char*, const char*);
  9802. SQLITE_PRIVATE void sqlite3AuthContextPush(Parse*, AuthContext*, const char*);
  9803. SQLITE_PRIVATE void sqlite3AuthContextPop(AuthContext*);
  9804. SQLITE_PRIVATE int sqlite3AuthReadCol(Parse*, const char *, const char *, int);
  9805. #else
  9806. # define sqlite3AuthRead(a,b,c,d)
  9807. # define sqlite3AuthCheck(a,b,c,d,e) SQLITE_OK
  9808. # define sqlite3AuthContextPush(a,b,c)
  9809. # define sqlite3AuthContextPop(a) ((void)(a))
  9810. #endif
  9811. SQLITE_PRIVATE void sqlite3Attach(Parse*, Expr*, Expr*, Expr*);
  9812. SQLITE_PRIVATE void sqlite3Detach(Parse*, Expr*);
  9813. SQLITE_PRIVATE int sqlite3BtreeFactory(sqlite3 *db, const char *zFilename,
  9814. int omitJournal, int nCache, int flags, Btree **ppBtree);
  9815. SQLITE_PRIVATE int sqlite3FixInit(DbFixer*, Parse*, int, const char*, const Token*);
  9816. SQLITE_PRIVATE int sqlite3FixSrcList(DbFixer*, SrcList*);
  9817. SQLITE_PRIVATE int sqlite3FixSelect(DbFixer*, Select*);
  9818. SQLITE_PRIVATE int sqlite3FixExpr(DbFixer*, Expr*);
  9819. SQLITE_PRIVATE int sqlite3FixExprList(DbFixer*, ExprList*);
  9820. SQLITE_PRIVATE int sqlite3FixTriggerStep(DbFixer*, TriggerStep*);
  9821. SQLITE_PRIVATE int sqlite3AtoF(const char *z, double*);
  9822. SQLITE_PRIVATE int sqlite3GetInt32(const char *, int*);
  9823. SQLITE_PRIVATE int sqlite3FitsIn64Bits(const char *, int);
  9824. SQLITE_PRIVATE int sqlite3Utf16ByteLen(const void *pData, int nChar);
  9825. SQLITE_PRIVATE int sqlite3Utf8CharLen(const char *pData, int nByte);
  9826. SQLITE_PRIVATE int sqlite3Utf8Read(const u8*, const u8**);
  9827. /*
  9828. ** Routines to read and write variable-length integers. These used to
  9829. ** be defined locally, but now we use the varint routines in the util.c
  9830. ** file. Code should use the MACRO forms below, as the Varint32 versions
  9831. ** are coded to assume the single byte case is already handled (which
  9832. ** the MACRO form does).
  9833. */
  9834. SQLITE_PRIVATE int sqlite3PutVarint(unsigned char*, u64);
  9835. SQLITE_PRIVATE int sqlite3PutVarint32(unsigned char*, u32);
  9836. SQLITE_PRIVATE u8 sqlite3GetVarint(const unsigned char *, u64 *);
  9837. SQLITE_PRIVATE u8 sqlite3GetVarint32(const unsigned char *, u32 *);
  9838. SQLITE_PRIVATE int sqlite3VarintLen(u64 v);
  9839. /*
  9840. ** The header of a record consists of a sequence variable-length integers.
  9841. ** These integers are almost always small and are encoded as a single byte.
  9842. ** The following macros take advantage this fact to provide a fast encode
  9843. ** and decode of the integers in a record header. It is faster for the common
  9844. ** case where the integer is a single byte. It is a little slower when the
  9845. ** integer is two or more bytes. But overall it is faster.
  9846. **
  9847. ** The following expressions are equivalent:
  9848. **
  9849. ** x = sqlite3GetVarint32( A, &B );
  9850. ** x = sqlite3PutVarint32( A, B );
  9851. **
  9852. ** x = getVarint32( A, B );
  9853. ** x = putVarint32( A, B );
  9854. **
  9855. */
  9856. #define getVarint32(A,B) (u8)((*(A)<(u8)0x80) ? ((B) = (u32)*(A)),1 : sqlite3GetVarint32((A), (u32 *)&(B)))
  9857. #define putVarint32(A,B) (u8)(((u32)(B)<(u32)0x80) ? (*(A) = (unsigned char)(B)),1 : sqlite3PutVarint32((A), (B)))
  9858. #define getVarint sqlite3GetVarint
  9859. #define putVarint sqlite3PutVarint
  9860. SQLITE_PRIVATE const char *sqlite3IndexAffinityStr(Vdbe *, Index *);
  9861. SQLITE_PRIVATE void sqlite3TableAffinityStr(Vdbe *, Table *);
  9862. SQLITE_PRIVATE char sqlite3CompareAffinity(Expr *pExpr, char aff2);
  9863. SQLITE_PRIVATE int sqlite3IndexAffinityOk(Expr *pExpr, char idx_affinity);
  9864. SQLITE_PRIVATE char sqlite3ExprAffinity(Expr *pExpr);
  9865. SQLITE_PRIVATE int sqlite3Atoi64(const char*, i64*);
  9866. SQLITE_PRIVATE void sqlite3Error(sqlite3*, int, const char*,...);
  9867. SQLITE_PRIVATE void *sqlite3HexToBlob(sqlite3*, const char *z, int n);
  9868. SQLITE_PRIVATE int sqlite3TwoPartName(Parse *, Token *, Token *, Token **);
  9869. SQLITE_PRIVATE const char *sqlite3ErrStr(int);
  9870. SQLITE_PRIVATE int sqlite3ReadSchema(Parse *pParse);
  9871. SQLITE_PRIVATE CollSeq *sqlite3FindCollSeq(sqlite3*,u8 enc, const char*,int);
  9872. SQLITE_PRIVATE CollSeq *sqlite3LocateCollSeq(Parse *pParse, const char*zName);
  9873. SQLITE_PRIVATE CollSeq *sqlite3ExprCollSeq(Parse *pParse, Expr *pExpr);
  9874. SQLITE_PRIVATE Expr *sqlite3ExprSetColl(Parse *pParse, Expr *, Token *);
  9875. SQLITE_PRIVATE int sqlite3CheckCollSeq(Parse *, CollSeq *);
  9876. SQLITE_PRIVATE int sqlite3CheckObjectName(Parse *, const char *);
  9877. SQLITE_PRIVATE void sqlite3VdbeSetChanges(sqlite3 *, int);
  9878. SQLITE_PRIVATE const void *sqlite3ValueText(sqlite3_value*, u8);
  9879. SQLITE_PRIVATE int sqlite3ValueBytes(sqlite3_value*, u8);
  9880. SQLITE_PRIVATE void sqlite3ValueSetStr(sqlite3_value*, int, const void *,u8,
  9881. void(*)(void*));
  9882. SQLITE_PRIVATE void sqlite3ValueFree(sqlite3_value*);
  9883. SQLITE_PRIVATE sqlite3_value *sqlite3ValueNew(sqlite3 *);
  9884. SQLITE_PRIVATE char *sqlite3Utf16to8(sqlite3 *, const void*, int);
  9885. #ifdef SQLITE_ENABLE_STAT2
  9886. SQLITE_PRIVATE char *sqlite3Utf8to16(sqlite3 *, u8, char *, int, int *);
  9887. #endif
  9888. SQLITE_PRIVATE int sqlite3ValueFromExpr(sqlite3 *, Expr *, u8, u8, sqlite3_value **);
  9889. SQLITE_PRIVATE void sqlite3ValueApplyAffinity(sqlite3_value *, u8, u8);
  9890. #ifndef SQLITE_AMALGAMATION
  9891. SQLITE_PRIVATE const unsigned char sqlite3OpcodeProperty[];
  9892. SQLITE_PRIVATE const unsigned char sqlite3UpperToLower[];
  9893. SQLITE_PRIVATE const unsigned char sqlite3CtypeMap[];
  9894. SQLITE_PRIVATE SQLITE_WSD struct Sqlite3Config sqlite3Config;
  9895. SQLITE_PRIVATE SQLITE_WSD FuncDefHash sqlite3GlobalFunctions;
  9896. SQLITE_PRIVATE int sqlite3PendingByte;
  9897. #endif
  9898. SQLITE_PRIVATE void sqlite3RootPageMoved(Db*, int, int);
  9899. SQLITE_PRIVATE void sqlite3Reindex(Parse*, Token*, Token*);
  9900. SQLITE_PRIVATE void sqlite3AlterFunctions(sqlite3*);
  9901. SQLITE_PRIVATE void sqlite3AlterRenameTable(Parse*, SrcList*, Token*);
  9902. SQLITE_PRIVATE int sqlite3GetToken(const unsigned char *, int *);
  9903. SQLITE_PRIVATE void sqlite3NestedParse(Parse*, const char*, ...);
  9904. SQLITE_PRIVATE void sqlite3ExpirePreparedStatements(sqlite3*);
  9905. SQLITE_PRIVATE int sqlite3CodeSubselect(Parse *, Expr *, int, int);
  9906. SQLITE_PRIVATE void sqlite3SelectPrep(Parse*, Select*, NameContext*);
  9907. SQLITE_PRIVATE int sqlite3ResolveExprNames(NameContext*, Expr*);
  9908. SQLITE_PRIVATE void sqlite3ResolveSelectNames(Parse*, Select*, NameContext*);
  9909. SQLITE_PRIVATE int sqlite3ResolveOrderGroupBy(Parse*, Select*, ExprList*, const char*);
  9910. SQLITE_PRIVATE void sqlite3ColumnDefault(Vdbe *, Table *, int, int);
  9911. SQLITE_PRIVATE void sqlite3AlterFinishAddColumn(Parse *, Token *);
  9912. SQLITE_PRIVATE void sqlite3AlterBeginAddColumn(Parse *, SrcList *);
  9913. SQLITE_PRIVATE CollSeq *sqlite3GetCollSeq(sqlite3*, u8, CollSeq *, const char*);
  9914. SQLITE_PRIVATE char sqlite3AffinityType(const char*);
  9915. SQLITE_PRIVATE void sqlite3Analyze(Parse*, Token*, Token*);
  9916. SQLITE_PRIVATE int sqlite3InvokeBusyHandler(BusyHandler*);
  9917. SQLITE_PRIVATE int sqlite3FindDb(sqlite3*, Token*);
  9918. SQLITE_PRIVATE int sqlite3FindDbName(sqlite3 *, const char *);
  9919. SQLITE_PRIVATE int sqlite3AnalysisLoad(sqlite3*,int iDB);
  9920. SQLITE_PRIVATE void sqlite3DeleteIndexSamples(Index*);
  9921. SQLITE_PRIVATE void sqlite3DefaultRowEst(Index*);
  9922. SQLITE_PRIVATE void sqlite3RegisterLikeFunctions(sqlite3*, int);
  9923. SQLITE_PRIVATE int sqlite3IsLikeFunction(sqlite3*,Expr*,int*,char*);
  9924. SQLITE_PRIVATE void sqlite3MinimumFileFormat(Parse*, int, int);
  9925. SQLITE_PRIVATE void sqlite3SchemaFree(void *);
  9926. SQLITE_PRIVATE Schema *sqlite3SchemaGet(sqlite3 *, Btree *);
  9927. SQLITE_PRIVATE int sqlite3SchemaToIndex(sqlite3 *db, Schema *);
  9928. SQLITE_PRIVATE KeyInfo *sqlite3IndexKeyinfo(Parse *, Index *);
  9929. SQLITE_PRIVATE int sqlite3CreateFunc(sqlite3 *, const char *, int, int, void *,
  9930. void (*)(sqlite3_context*,int,sqlite3_value **),
  9931. void (*)(sqlite3_context*,int,sqlite3_value **), void (*)(sqlite3_context*));
  9932. SQLITE_PRIVATE int sqlite3ApiExit(sqlite3 *db, int);
  9933. SQLITE_PRIVATE int sqlite3OpenTempDatabase(Parse *);
  9934. SQLITE_PRIVATE void sqlite3StrAccumInit(StrAccum*, char*, int, int);
  9935. SQLITE_PRIVATE void sqlite3StrAccumAppend(StrAccum*,const char*,int);
  9936. SQLITE_PRIVATE char *sqlite3StrAccumFinish(StrAccum*);
  9937. SQLITE_PRIVATE void sqlite3StrAccumReset(StrAccum*);
  9938. SQLITE_PRIVATE void sqlite3SelectDestInit(SelectDest*,int,int);
  9939. SQLITE_PRIVATE Expr *sqlite3CreateColumnExpr(sqlite3 *, SrcList *, int, int);
  9940. SQLITE_PRIVATE void sqlite3BackupRestart(sqlite3_backup *);
  9941. SQLITE_PRIVATE void sqlite3BackupUpdate(sqlite3_backup *, Pgno, const u8 *);
  9942. /*
  9943. ** The interface to the LEMON-generated parser
  9944. */
  9945. SQLITE_PRIVATE void *sqlite3ParserAlloc(void*(*)(size_t));
  9946. SQLITE_PRIVATE void sqlite3ParserFree(void*, void(*)(void*));
  9947. SQLITE_PRIVATE void sqlite3Parser(void*, int, Token, Parse*);
  9948. #ifdef YYTRACKMAXSTACKDEPTH
  9949. SQLITE_PRIVATE int sqlite3ParserStackPeak(void*);
  9950. #endif
  9951. SQLITE_PRIVATE void sqlite3AutoLoadExtensions(sqlite3*);
  9952. #ifndef SQLITE_OMIT_LOAD_EXTENSION
  9953. SQLITE_PRIVATE void sqlite3CloseExtensions(sqlite3*);
  9954. #else
  9955. # define sqlite3CloseExtensions(X)
  9956. #endif
  9957. #ifndef SQLITE_OMIT_SHARED_CACHE
  9958. SQLITE_PRIVATE void sqlite3TableLock(Parse *, int, int, u8, const char *);
  9959. #else
  9960. #define sqlite3TableLock(v,w,x,y,z)
  9961. #endif
  9962. #ifdef SQLITE_TEST
  9963. SQLITE_PRIVATE int sqlite3Utf8To8(unsigned char*);
  9964. #endif
  9965. #ifdef SQLITE_OMIT_VIRTUALTABLE
  9966. # define sqlite3VtabClear(Y)
  9967. # define sqlite3VtabSync(X,Y) SQLITE_OK
  9968. # define sqlite3VtabRollback(X)
  9969. # define sqlite3VtabCommit(X)
  9970. # define sqlite3VtabInSync(db) 0
  9971. # define sqlite3VtabLock(X)
  9972. # define sqlite3VtabUnlock(X)
  9973. # define sqlite3VtabUnlockList(X)
  9974. #else
  9975. SQLITE_PRIVATE void sqlite3VtabClear(Table*);
  9976. SQLITE_PRIVATE int sqlite3VtabSync(sqlite3 *db, char **);
  9977. SQLITE_PRIVATE int sqlite3VtabRollback(sqlite3 *db);
  9978. SQLITE_PRIVATE int sqlite3VtabCommit(sqlite3 *db);
  9979. SQLITE_PRIVATE void sqlite3VtabLock(VTable *);
  9980. SQLITE_PRIVATE void sqlite3VtabUnlock(VTable *);
  9981. SQLITE_PRIVATE void sqlite3VtabUnlockList(sqlite3*);
  9982. # define sqlite3VtabInSync(db) ((db)->nVTrans>0 && (db)->aVTrans==0)
  9983. #endif
  9984. SQLITE_PRIVATE void sqlite3VtabMakeWritable(Parse*,Table*);
  9985. SQLITE_PRIVATE void sqlite3VtabBeginParse(Parse*, Token*, Token*, Token*);
  9986. SQLITE_PRIVATE void sqlite3VtabFinishParse(Parse*, Token*);
  9987. SQLITE_PRIVATE void sqlite3VtabArgInit(Parse*);
  9988. SQLITE_PRIVATE void sqlite3VtabArgExtend(Parse*, Token*);
  9989. SQLITE_PRIVATE int sqlite3VtabCallCreate(sqlite3*, int, const char *, char **);
  9990. SQLITE_PRIVATE int sqlite3VtabCallConnect(Parse*, Table*);
  9991. SQLITE_PRIVATE int sqlite3VtabCallDestroy(sqlite3*, int, const char *);
  9992. SQLITE_PRIVATE int sqlite3VtabBegin(sqlite3 *, VTable *);
  9993. SQLITE_PRIVATE FuncDef *sqlite3VtabOverloadFunction(sqlite3 *,FuncDef*, int nArg, Expr*);
  9994. SQLITE_PRIVATE void sqlite3InvalidFunction(sqlite3_context*,int,sqlite3_value**);
  9995. SQLITE_PRIVATE int sqlite3VdbeParameterIndex(Vdbe*, const char*, int);
  9996. SQLITE_PRIVATE int sqlite3TransferBindings(sqlite3_stmt *, sqlite3_stmt *);
  9997. SQLITE_PRIVATE int sqlite3Reprepare(Vdbe*);
  9998. SQLITE_PRIVATE void sqlite3ExprListCheckLength(Parse*, ExprList*, const char*);
  9999. SQLITE_PRIVATE CollSeq *sqlite3BinaryCompareCollSeq(Parse *, Expr *, Expr *);
  10000. SQLITE_PRIVATE int sqlite3TempInMemory(const sqlite3*);
  10001. SQLITE_PRIVATE VTable *sqlite3GetVTable(sqlite3*, Table*);
  10002. /* Declarations for functions in fkey.c. All of these are replaced by
  10003. ** no-op macros if OMIT_FOREIGN_KEY is defined. In this case no foreign
  10004. ** key functionality is available. If OMIT_TRIGGER is defined but
  10005. ** OMIT_FOREIGN_KEY is not, only some of the functions are no-oped. In
  10006. ** this case foreign keys are parsed, but no other functionality is
  10007. ** provided (enforcement of FK constraints requires the triggers sub-system).
  10008. */
  10009. #if !defined(SQLITE_OMIT_FOREIGN_KEY) && !defined(SQLITE_OMIT_TRIGGER)
  10010. SQLITE_PRIVATE void sqlite3FkCheck(Parse*, Table*, int, int);
  10011. SQLITE_PRIVATE void sqlite3FkDropTable(Parse*, SrcList *, Table*);
  10012. SQLITE_PRIVATE void sqlite3FkActions(Parse*, Table*, ExprList*, int);
  10013. SQLITE_PRIVATE int sqlite3FkRequired(Parse*, Table*, int*, int);
  10014. SQLITE_PRIVATE u32 sqlite3FkOldmask(Parse*, Table*);
  10015. SQLITE_PRIVATE FKey *sqlite3FkReferences(Table *);
  10016. #else
  10017. #define sqlite3FkActions(a,b,c,d)
  10018. #define sqlite3FkCheck(a,b,c,d)
  10019. #define sqlite3FkDropTable(a,b,c)
  10020. #define sqlite3FkOldmask(a,b) 0
  10021. #define sqlite3FkRequired(a,b,c,d) 0
  10022. #endif
  10023. #ifndef SQLITE_OMIT_FOREIGN_KEY
  10024. SQLITE_PRIVATE void sqlite3FkDelete(Table*);
  10025. #else
  10026. #define sqlite3FkDelete(a)
  10027. #endif
  10028. /*
  10029. ** Available fault injectors. Should be numbered beginning with 0.
  10030. */
  10031. #define SQLITE_FAULTINJECTOR_MALLOC 0
  10032. #define SQLITE_FAULTINJECTOR_COUNT 1
  10033. /*
  10034. ** The interface to the code in fault.c used for identifying "benign"
  10035. ** malloc failures. This is only present if SQLITE_OMIT_BUILTIN_TEST
  10036. ** is not defined.
  10037. */
  10038. #ifndef SQLITE_OMIT_BUILTIN_TEST
  10039. SQLITE_PRIVATE void sqlite3BeginBenignMalloc(void);
  10040. SQLITE_PRIVATE void sqlite3EndBenignMalloc(void);
  10041. #else
  10042. #define sqlite3BeginBenignMalloc()
  10043. #define sqlite3EndBenignMalloc()
  10044. #endif
  10045. #define IN_INDEX_ROWID 1
  10046. #define IN_INDEX_EPH 2
  10047. #define IN_INDEX_INDEX 3
  10048. SQLITE_PRIVATE int sqlite3FindInIndex(Parse *, Expr *, int*);
  10049. #ifdef SQLITE_ENABLE_ATOMIC_WRITE
  10050. SQLITE_PRIVATE int sqlite3JournalOpen(sqlite3_vfs *, const char *, sqlite3_file *, int, int);
  10051. SQLITE_PRIVATE int sqlite3JournalSize(sqlite3_vfs *);
  10052. SQLITE_PRIVATE int sqlite3JournalCreate(sqlite3_file *);
  10053. #else
  10054. #define sqlite3JournalSize(pVfs) ((pVfs)->szOsFile)
  10055. #endif
  10056. SQLITE_PRIVATE void sqlite3MemJournalOpen(sqlite3_file *);
  10057. SQLITE_PRIVATE int sqlite3MemJournalSize(void);
  10058. SQLITE_PRIVATE int sqlite3IsMemJournal(sqlite3_file *);
  10059. #if SQLITE_MAX_EXPR_DEPTH>0
  10060. SQLITE_PRIVATE void sqlite3ExprSetHeight(Parse *pParse, Expr *p);
  10061. SQLITE_PRIVATE int sqlite3SelectExprHeight(Select *);
  10062. SQLITE_PRIVATE int sqlite3ExprCheckHeight(Parse*, int);
  10063. #else
  10064. #define sqlite3ExprSetHeight(x,y)
  10065. #define sqlite3SelectExprHeight(x) 0
  10066. #define sqlite3ExprCheckHeight(x,y)
  10067. #endif
  10068. SQLITE_PRIVATE u32 sqlite3Get4byte(const u8*);
  10069. SQLITE_PRIVATE void sqlite3Put4byte(u8*, u32);
  10070. #ifdef SQLITE_ENABLE_UNLOCK_NOTIFY
  10071. SQLITE_PRIVATE void sqlite3ConnectionBlocked(sqlite3 *, sqlite3 *);
  10072. SQLITE_PRIVATE void sqlite3ConnectionUnlocked(sqlite3 *db);
  10073. SQLITE_PRIVATE void sqlite3ConnectionClosed(sqlite3 *db);
  10074. #else
  10075. #define sqlite3ConnectionBlocked(x,y)
  10076. #define sqlite3ConnectionUnlocked(x)
  10077. #define sqlite3ConnectionClosed(x)
  10078. #endif
  10079. #ifdef SQLITE_DEBUG
  10080. SQLITE_PRIVATE void sqlite3ParserTrace(FILE*, char *);
  10081. #endif
  10082. /*
  10083. ** If the SQLITE_ENABLE IOTRACE exists then the global variable
  10084. ** sqlite3IoTrace is a pointer to a printf-like routine used to
  10085. ** print I/O tracing messages.
  10086. */
  10087. #ifdef SQLITE_ENABLE_IOTRACE
  10088. # define IOTRACE(A) if( sqlite3IoTrace ){ sqlite3IoTrace A; }
  10089. SQLITE_PRIVATE void sqlite3VdbeIOTraceSql(Vdbe*);
  10090. SQLITE_PRIVATE void (*sqlite3IoTrace)(const char*,...);
  10091. #else
  10092. # define IOTRACE(A)
  10093. # define sqlite3VdbeIOTraceSql(X)
  10094. #endif
  10095. #endif
  10096. /************** End of sqliteInt.h *******************************************/
  10097. /************** Begin file global.c ******************************************/
  10098. /*
  10099. ** 2008 June 13
  10100. **
  10101. ** The author disclaims copyright to this source code. In place of
  10102. ** a legal notice, here is a blessing:
  10103. **
  10104. ** May you do good and not evil.
  10105. ** May you find forgiveness for yourself and forgive others.
  10106. ** May you share freely, never taking more than you give.
  10107. **
  10108. *************************************************************************
  10109. **
  10110. ** This file contains definitions of global variables and contants.
  10111. */
  10112. /* An array to map all upper-case characters into their corresponding
  10113. ** lower-case character.
  10114. **
  10115. ** SQLite only considers US-ASCII (or EBCDIC) characters. We do not
  10116. ** handle case conversions for the UTF character set since the tables
  10117. ** involved are nearly as big or bigger than SQLite itself.
  10118. */
  10119. SQLITE_PRIVATE const unsigned char sqlite3UpperToLower[] = {
  10120. #ifdef SQLITE_ASCII
  10121. 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17,
  10122. 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35,
  10123. 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53,
  10124. 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 97, 98, 99,100,101,102,103,
  10125. 104,105,106,107,108,109,110,111,112,113,114,115,116,117,118,119,120,121,
  10126. 122, 91, 92, 93, 94, 95, 96, 97, 98, 99,100,101,102,103,104,105,106,107,
  10127. 108,109,110,111,112,113,114,115,116,117,118,119,120,121,122,123,124,125,
  10128. 126,127,128,129,130,131,132,133,134,135,136,137,138,139,140,141,142,143,
  10129. 144,145,146,147,148,149,150,151,152,153,154,155,156,157,158,159,160,161,
  10130. 162,163,164,165,166,167,168,169,170,171,172,173,174,175,176,177,178,179,
  10131. 180,181,182,183,184,185,186,187,188,189,190,191,192,193,194,195,196,197,
  10132. 198,199,200,201,202,203,204,205,206,207,208,209,210,211,212,213,214,215,
  10133. 216,217,218,219,220,221,222,223,224,225,226,227,228,229,230,231,232,233,
  10134. 234,235,236,237,238,239,240,241,242,243,244,245,246,247,248,249,250,251,
  10135. 252,253,254,255
  10136. #endif
  10137. #ifdef SQLITE_EBCDIC
  10138. 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, /* 0x */
  10139. 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, /* 1x */
  10140. 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, /* 2x */
  10141. 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, /* 3x */
  10142. 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, /* 4x */
  10143. 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, /* 5x */
  10144. 96, 97, 66, 67, 68, 69, 70, 71, 72, 73,106,107,108,109,110,111, /* 6x */
  10145. 112, 81, 82, 83, 84, 85, 86, 87, 88, 89,122,123,124,125,126,127, /* 7x */
  10146. 128,129,130,131,132,133,134,135,136,137,138,139,140,141,142,143, /* 8x */
  10147. 144,145,146,147,148,149,150,151,152,153,154,155,156,157,156,159, /* 9x */
  10148. 160,161,162,163,164,165,166,167,168,169,170,171,140,141,142,175, /* Ax */
  10149. 176,177,178,179,180,181,182,183,184,185,186,187,188,189,190,191, /* Bx */
  10150. 192,129,130,131,132,133,134,135,136,137,202,203,204,205,206,207, /* Cx */
  10151. 208,145,146,147,148,149,150,151,152,153,218,219,220,221,222,223, /* Dx */
  10152. 224,225,162,163,164,165,166,167,168,169,232,203,204,205,206,207, /* Ex */
  10153. 239,240,241,242,243,244,245,246,247,248,249,219,220,221,222,255, /* Fx */
  10154. #endif
  10155. };
  10156. /*
  10157. ** The following 256 byte lookup table is used to support SQLites built-in
  10158. ** equivalents to the following standard library functions:
  10159. **
  10160. ** isspace() 0x01
  10161. ** isalpha() 0x02
  10162. ** isdigit() 0x04
  10163. ** isalnum() 0x06
  10164. ** isxdigit() 0x08
  10165. ** toupper() 0x20
  10166. ** SQLite identifier character 0x40
  10167. **
  10168. ** Bit 0x20 is set if the mapped character requires translation to upper
  10169. ** case. i.e. if the character is a lower-case ASCII character.
  10170. ** If x is a lower-case ASCII character, then its upper-case equivalent
  10171. ** is (x - 0x20). Therefore toupper() can be implemented as:
  10172. **
  10173. ** (x & ~(map[x]&0x20))
  10174. **
  10175. ** Standard function tolower() is implemented using the sqlite3UpperToLower[]
  10176. ** array. tolower() is used more often than toupper() by SQLite.
  10177. **
  10178. ** Bit 0x40 is set if the character non-alphanumeric and can be used in an
  10179. ** SQLite identifier. Identifiers are alphanumerics, "_", "$", and any
  10180. ** non-ASCII UTF character. Hence the test for whether or not a character is
  10181. ** part of an identifier is 0x46.
  10182. **
  10183. ** SQLite's versions are identical to the standard versions assuming a
  10184. ** locale of "C". They are implemented as macros in sqliteInt.h.
  10185. */
  10186. #ifdef SQLITE_ASCII
  10187. SQLITE_PRIVATE const unsigned char sqlite3CtypeMap[256] = {
  10188. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 00..07 ........ */
  10189. 0x00, 0x01, 0x01, 0x01, 0x01, 0x01, 0x00, 0x00, /* 08..0f ........ */
  10190. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 10..17 ........ */
  10191. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 18..1f ........ */
  10192. 0x01, 0x00, 0x00, 0x00, 0x40, 0x00, 0x00, 0x00, /* 20..27 !"#$%&' */
  10193. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 28..2f ()*+,-./ */
  10194. 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, /* 30..37 01234567 */
  10195. 0x0c, 0x0c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 38..3f 89:;<=>? */
  10196. 0x00, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x02, /* 40..47 @ABCDEFG */
  10197. 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, /* 48..4f HIJKLMNO */
  10198. 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, /* 50..57 PQRSTUVW */
  10199. 0x02, 0x02, 0x02, 0x00, 0x00, 0x00, 0x00, 0x40, /* 58..5f XYZ[\]^_ */
  10200. 0x00, 0x2a, 0x2a, 0x2a, 0x2a, 0x2a, 0x2a, 0x22, /* 60..67 `abcdefg */
  10201. 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, /* 68..6f hijklmno */
  10202. 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, /* 70..77 pqrstuvw */
  10203. 0x22, 0x22, 0x22, 0x00, 0x00, 0x00, 0x00, 0x00, /* 78..7f xyz{|}~. */
  10204. 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, /* 80..87 ........ */
  10205. 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, /* 88..8f ........ */
  10206. 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, /* 90..97 ........ */
  10207. 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, /* 98..9f ........ */
  10208. 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, /* a0..a7 ........ */
  10209. 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, /* a8..af ........ */
  10210. 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, /* b0..b7 ........ */
  10211. 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, /* b8..bf ........ */
  10212. 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, /* c0..c7 ........ */
  10213. 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, /* c8..cf ........ */
  10214. 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, /* d0..d7 ........ */
  10215. 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, /* d8..df ........ */
  10216. 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, /* e0..e7 ........ */
  10217. 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, /* e8..ef ........ */
  10218. 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, /* f0..f7 ........ */
  10219. 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40 /* f8..ff ........ */
  10220. };
  10221. #endif
  10222. /*
  10223. ** The following singleton contains the global configuration for
  10224. ** the SQLite library.
  10225. */
  10226. SQLITE_PRIVATE SQLITE_WSD struct Sqlite3Config sqlite3Config = {
  10227. SQLITE_DEFAULT_MEMSTATUS, /* bMemstat */
  10228. 1, /* bCoreMutex */
  10229. SQLITE_THREADSAFE==1, /* bFullMutex */
  10230. 0x7ffffffe, /* mxStrlen */
  10231. 100, /* szLookaside */
  10232. 500, /* nLookaside */
  10233. {0,0,0,0,0,0,0,0}, /* m */
  10234. {0,0,0,0,0,0,0,0,0}, /* mutex */
  10235. {0,0,0,0,0,0,0,0,0,0,0}, /* pcache */
  10236. (void*)0, /* pHeap */
  10237. 0, /* nHeap */
  10238. 0, 0, /* mnHeap, mxHeap */
  10239. (void*)0, /* pScratch */
  10240. 0, /* szScratch */
  10241. 0, /* nScratch */
  10242. (void*)0, /* pPage */
  10243. 0, /* szPage */
  10244. 0, /* nPage */
  10245. 0, /* mxParserStack */
  10246. 0, /* sharedCacheEnabled */
  10247. /* All the rest should always be initialized to zero */
  10248. 0, /* isInit */
  10249. 0, /* inProgress */
  10250. 0, /* isMutexInit */
  10251. 0, /* isMallocInit */
  10252. 0, /* isPCacheInit */
  10253. 0, /* pInitMutex */
  10254. 0, /* nRefInitMutex */
  10255. };
  10256. /*
  10257. ** Hash table for global functions - functions common to all
  10258. ** database connections. After initialization, this table is
  10259. ** read-only.
  10260. */
  10261. SQLITE_PRIVATE SQLITE_WSD FuncDefHash sqlite3GlobalFunctions;
  10262. /*
  10263. ** The value of the "pending" byte must be 0x40000000 (1 byte past the
  10264. ** 1-gibabyte boundary) in a compatible database. SQLite never uses
  10265. ** the database page that contains the pending byte. It never attempts
  10266. ** to read or write that page. The pending byte page is set assign
  10267. ** for use by the VFS layers as space for managing file locks.
  10268. **
  10269. ** During testing, it is often desirable to move the pending byte to
  10270. ** a different position in the file. This allows code that has to
  10271. ** deal with the pending byte to run on files that are much smaller
  10272. ** than 1 GiB. The sqlite3_test_control() interface can be used to
  10273. ** move the pending byte.
  10274. **
  10275. ** IMPORTANT: Changing the pending byte to any value other than
  10276. ** 0x40000000 results in an incompatible database file format!
  10277. ** Changing the pending byte during operating results in undefined
  10278. ** and dileterious behavior.
  10279. */
  10280. SQLITE_PRIVATE int sqlite3PendingByte = 0x40000000;
  10281. /*
  10282. ** Properties of opcodes. The OPFLG_INITIALIZER macro is
  10283. ** created by mkopcodeh.awk during compilation. Data is obtained
  10284. ** from the comments following the "case OP_xxxx:" statements in
  10285. ** the vdbe.c file.
  10286. */
  10287. SQLITE_PRIVATE const unsigned char sqlite3OpcodeProperty[] = OPFLG_INITIALIZER;
  10288. /************** End of global.c **********************************************/
  10289. /************** Begin file status.c ******************************************/
  10290. /*
  10291. ** 2008 June 18
  10292. **
  10293. ** The author disclaims copyright to this source code. In place of
  10294. ** a legal notice, here is a blessing:
  10295. **
  10296. ** May you do good and not evil.
  10297. ** May you find forgiveness for yourself and forgive others.
  10298. ** May you share freely, never taking more than you give.
  10299. **
  10300. *************************************************************************
  10301. **
  10302. ** This module implements the sqlite3_status() interface and related
  10303. ** functionality.
  10304. */
  10305. /*
  10306. ** Variables in which to record status information.
  10307. */
  10308. typedef struct sqlite3StatType sqlite3StatType;
  10309. static SQLITE_WSD struct sqlite3StatType {
  10310. int nowValue[9]; /* Current value */
  10311. int mxValue[9]; /* Maximum value */
  10312. } sqlite3Stat = { {0,}, {0,} };
  10313. /* The "wsdStat" macro will resolve to the status information
  10314. ** state vector. If writable static data is unsupported on the target,
  10315. ** we have to locate the state vector at run-time. In the more common
  10316. ** case where writable static data is supported, wsdStat can refer directly
  10317. ** to the "sqlite3Stat" state vector declared above.
  10318. */
  10319. #ifdef SQLITE_OMIT_WSD
  10320. # define wsdStatInit sqlite3StatType *x = &GLOBAL(sqlite3StatType,sqlite3Stat)
  10321. # define wsdStat x[0]
  10322. #else
  10323. # define wsdStatInit
  10324. # define wsdStat sqlite3Stat
  10325. #endif
  10326. /*
  10327. ** Return the current value of a status parameter.
  10328. */
  10329. SQLITE_PRIVATE int sqlite3StatusValue(int op){
  10330. wsdStatInit;
  10331. assert( op>=0 && op<ArraySize(wsdStat.nowValue) );
  10332. return wsdStat.nowValue[op];
  10333. }
  10334. /*
  10335. ** Add N to the value of a status record. It is assumed that the
  10336. ** caller holds appropriate locks.
  10337. */
  10338. SQLITE_PRIVATE void sqlite3StatusAdd(int op, int N){
  10339. wsdStatInit;
  10340. assert( op>=0 && op<ArraySize(wsdStat.nowValue) );
  10341. wsdStat.nowValue[op] += N;
  10342. if( wsdStat.nowValue[op]>wsdStat.mxValue[op] ){
  10343. wsdStat.mxValue[op] = wsdStat.nowValue[op];
  10344. }
  10345. }
  10346. /*
  10347. ** Set the value of a status to X.
  10348. */
  10349. SQLITE_PRIVATE void sqlite3StatusSet(int op, int X){
  10350. wsdStatInit;
  10351. assert( op>=0 && op<ArraySize(wsdStat.nowValue) );
  10352. wsdStat.nowValue[op] = X;
  10353. if( wsdStat.nowValue[op]>wsdStat.mxValue[op] ){
  10354. wsdStat.mxValue[op] = wsdStat.nowValue[op];
  10355. }
  10356. }
  10357. /*
  10358. ** Query status information.
  10359. **
  10360. ** This implementation assumes that reading or writing an aligned
  10361. ** 32-bit integer is an atomic operation. If that assumption is not true,
  10362. ** then this routine is not threadsafe.
  10363. */
  10364. SQLITE_API int sqlite3_status(int op, int *pCurrent, int *pHighwater, int resetFlag){
  10365. wsdStatInit;
  10366. if( op<0 || op>=ArraySize(wsdStat.nowValue) ){
  10367. return SQLITE_MISUSE;
  10368. }
  10369. *pCurrent = wsdStat.nowValue[op];
  10370. *pHighwater = wsdStat.mxValue[op];
  10371. if( resetFlag ){
  10372. wsdStat.mxValue[op] = wsdStat.nowValue[op];
  10373. }
  10374. return SQLITE_OK;
  10375. }
  10376. /*
  10377. ** Query status information for a single database connection
  10378. */
  10379. SQLITE_API int sqlite3_db_status(
  10380. sqlite3 *db, /* The database connection whose status is desired */
  10381. int op, /* Status verb */
  10382. int *pCurrent, /* Write current value here */
  10383. int *pHighwater, /* Write high-water mark here */
  10384. int resetFlag /* Reset high-water mark if true */
  10385. ){
  10386. switch( op ){
  10387. case SQLITE_DBSTATUS_LOOKASIDE_USED: {
  10388. *pCurrent = db->lookaside.nOut;
  10389. *pHighwater = db->lookaside.mxOut;
  10390. if( resetFlag ){
  10391. db->lookaside.mxOut = db->lookaside.nOut;
  10392. }
  10393. break;
  10394. }
  10395. default: {
  10396. return SQLITE_ERROR;
  10397. }
  10398. }
  10399. return SQLITE_OK;
  10400. }
  10401. /************** End of status.c **********************************************/
  10402. /************** Begin file date.c ********************************************/
  10403. /*
  10404. ** 2003 October 31
  10405. **
  10406. ** The author disclaims copyright to this source code. In place of
  10407. ** a legal notice, here is a blessing:
  10408. **
  10409. ** May you do good and not evil.
  10410. ** May you find forgiveness for yourself and forgive others.
  10411. ** May you share freely, never taking more than you give.
  10412. **
  10413. *************************************************************************
  10414. ** This file contains the C functions that implement date and time
  10415. ** functions for SQLite.
  10416. **
  10417. ** There is only one exported symbol in this file - the function
  10418. ** sqlite3RegisterDateTimeFunctions() found at the bottom of the file.
  10419. ** All other code has file scope.
  10420. **
  10421. ** SQLite processes all times and dates as Julian Day numbers. The
  10422. ** dates and times are stored as the number of days since noon
  10423. ** in Greenwich on November 24, 4714 B.C. according to the Gregorian
  10424. ** calendar system.
  10425. **
  10426. ** 1970-01-01 00:00:00 is JD 2440587.5
  10427. ** 2000-01-01 00:00:00 is JD 2451544.5
  10428. **
  10429. ** This implemention requires years to be expressed as a 4-digit number
  10430. ** which means that only dates between 0000-01-01 and 9999-12-31 can
  10431. ** be represented, even though julian day numbers allow a much wider
  10432. ** range of dates.
  10433. **
  10434. ** The Gregorian calendar system is used for all dates and times,
  10435. ** even those that predate the Gregorian calendar. Historians usually
  10436. ** use the Julian calendar for dates prior to 1582-10-15 and for some
  10437. ** dates afterwards, depending on locale. Beware of this difference.
  10438. **
  10439. ** The conversion algorithms are implemented based on descriptions
  10440. ** in the following text:
  10441. **
  10442. ** Jean Meeus
  10443. ** Astronomical Algorithms, 2nd Edition, 1998
  10444. **