1. /******************************************************************************
  2. ** This file is an amalgamation of many separate C source files from SQLite
  3. ** version 3.14.1. By combining all the individual C code files into this
  4. ** single large file, the entire code can be compiled as a single translation
  5. ** unit. This allows many compilers to do optimizations that would not be
  6. ** possible if the files were compiled separately. Performance improvements
  7. ** of 5% or more are commonly seen when SQLite is compiled as a single
  8. ** translation unit.
  9. **
  10. ** This file is all you need to compile SQLite. To use SQLite in other
  11. ** programs, you need this file and the "sqlite3.h" header file that defines
  12. ** the programming interface to the SQLite library. (If you do not have
  13. ** the "sqlite3.h" header file at hand, you will find a copy embedded within
  14. ** the text of this file. Search for "Begin file sqlite3.h" to find the start
  15. ** of the embedded sqlite3.h header file.) Additional code files may be needed
  16. ** if you want a wrapper to interface SQLite with your choice of programming
  17. ** language. The code for the "sqlite3" command-line shell is also in a
  18. ** separate file. This file contains only code for the core SQLite library.
  19. */
  20. #define SQLITE_CORE 1 //定义内核参数
  21. #define SQLITE_AMALGAMATION 1 //定义合并参数(为1),版本为合并后的源代码
  22. #ifndef SQLITE_PRIVATE //定义SQLITE代码的私有部分(即static)
  23. # define SQLITE_PRIVATE static
  24. #endif
  25. /************** Begin file sqliteInt.h ***************************************/
  26. /*
  27. ** 2001 September 15
  28. **
  29. ** The author disclaims copyright to this source code. In place of
  30. ** a legal notice, here is a blessing:
  31. **
  32. ** May you do good and not evil.
  33. ** May you find forgiveness for yourself and forgive others.
  34. ** May you share freely, never taking more than you give.
  35. **
  36. *************************************************************************
  37. ** Internal interface definitions for SQLite.
  38. **
  39. */
  40. #ifndef SQLITEINT_H //定义SQLITE内部接口,定义头文件
  41. #define SQLITEINT_H
  42. ……
  43. /************** Include msvc.h in the middle of sqliteInt.h ******************/
  44. ……
  45. /************** Continuing where we left off in sqliteInt.h ******************/
  46.  
  47. /*
  48. ** 如果基础操作系统支持,可用#define开启大于2G的单个文件支持
  49. ** These #defines should enable >2GB file support on POSIX if the
  50. ** underlying operating system supports it. If the OS lacks
  51. ** large file support, or if the OS is windows, these should be no-ops.
  52. **
  53. ** _LARGEFILE_SOURCE宏必须在任何#include前使用
  54. ** Ticket #2739: The _LARGEFILE_SOURCE macro must appear before any
  55. ** system #includes. Hence, this block of code must be the very first
  56. ** code in all source files.
  57. **
  58. ** 在编译命令行使用SQLITE_DISABLE_LFS可禁止大文件支持
  59. ** Large file support can be disabled using the -DSQLITE_DISABLE_LFS switch
  60. ** on the compiler command line. This is necessary if you are compiling
  61. ** on a recent machine (ex: Red Hat 7.2) but you want your code to work
  62. ** on an older machine (ex: Red Hat 6.0). If you compile on Red Hat 7.2
  63. ** without this option, LFS is enable. But LFS does not exist in the kernel
  64. ** in Red Hat 6.0, so the code won't work. Hence, for maximum binary
  65. ** portability you should omit LFS.
  66. **
  67. ** The previous paragraph was written in 2005. (This paragraph is written
  68. ** on 2008-11-28.) These days, all Linux kernels support large files, so
  69. ** you should probably leave LFS enabled. But some embedded platforms might
  70. ** lack LFS in which case the SQLITE_DISABLE_LFS macro might still be useful.
  71. **
  72. ** Similar is true for Mac OS X. LFS is only supported on Mac OS X 9 and later.
  73. */
  74. #ifndef SQLITE_DISABLE_LFS //如果没禁止大文件支持,则定义相关常量
  75. # define _LARGE_FILE
  76. # ifndef _FILE_OFFSET_BITS
  77. # define _FILE_OFFSET_BITS
  78. # endif
  79. # define _LARGEFILE_SOURCE
  80. #endif
  81.  
  82. /* What version of GCC is being used. 0 means GCC is not being used */
  83. #ifdef __GNUC__
  84. # define GCC_VERSION (__GNUC__*+__GNUC_MINOR__*+__GNUC_PATCHLEVEL__)
  85. #else
  86. # define GCC_VERSION
  87. #endif
  88.  
  89. /* Needed for various definitions... */
  90. #if defined(__GNUC__) && !defined(_GNU_SOURCE)
  91. # define _GNU_SOURCE
  92. #endif
  93.  
  94. #if defined(__OpenBSD__) && !defined(_BSD_SOURCE)
  95. # define _BSD_SOURCE
  96. #endif
  97.  
  98. /*
  99. ** For MinGW, check to see if we can include the header file containing its
  100. ** version information, among other things. Normally, this internal MinGW
  101. ** header file would [only] be included automatically by other MinGW header
  102. ** files; however, the contained version information is now required by this
  103. ** header file to work around binary compatibility issues (see below) and
  104. ** this is the only known way to reliably obtain it. This entire #if block
  105. ** would be completely unnecessary if there was any other way of detecting
  106. ** MinGW via their preprocessor (e.g. if they customized their GCC to define
  107. ** some MinGW-specific macros). When compiling for MinGW, either the
  108. ** _HAVE_MINGW_H or _HAVE__MINGW_H (note the extra underscore) macro must be
  109. ** defined; otherwise, detection of conditions specific to MinGW will be
  110. ** disabled.
  111. */
  112. #if defined(_HAVE_MINGW_H)
  113. # include "mingw.h"
  114. #elif defined(_HAVE__MINGW_H)
  115. # include "_mingw.h"
  116. #endif
  117.  
  118. /*
  119. ** For MinGW version 4.x (and higher), check to see if the _USE_32BIT_TIME_T
  120. ** define is required to maintain binary compatibility with the MSVC runtime
  121. ** library in use (e.g. for Windows XP).
  122. */
  123. #if !defined(_USE_32BIT_TIME_T) && !defined(_USE_64BIT_TIME_T) && \
  124. defined(_WIN32) && !defined(_WIN64) && \
  125. defined(__MINGW_MAJOR_VERSION) && __MINGW_MAJOR_VERSION >= && \
  126. defined(__MSVCRT__)
  127. # define _USE_32BIT_TIME_T
  128. #endif
  129.  
  130. /* The public SQLite interface. The _FILE_OFFSET_BITS macro must appear
  131. ** first in QNX. Also, the _USE_32BIT_TIME_T macro must appear first for
  132. ** MinGW.
  133. */
  134. /************** Include sqlite3.h in the middle of sqliteInt.h ***************/
  135. /************** Begin file sqlite3.h *****************************************/
  136. // 在sqliteInt.h中包含sqlite3.h
  137. /*
  138. ** 2001 September 15
  139. **
  140. ** The author disclaims copyright to this source code. In place of
  141. ** a legal notice, here is a blessing:
  142. **
  143. ** May you do good and not evil.
  144. ** May you find forgiveness for yourself and forgive others.
  145. ** May you share freely, never taking more than you give.
  146. **
  147. *************************************************************************
  148. ** sqlite库的客户端接口,如果在这个文件中没有出现过某个C函数、结构、数据类型、或常量定义
  149. ** 那么它是不公开的SQLITE的API,不会声明随时有可能改变,也不能做为使用SQLITE开发的参考
  150. ** This header file defines the interface that the SQLite library
  151. ** presents to client programs. If a C-function, structure, datatype,
  152. ** or constant definition does not appear in this file, then it is
  153. ** not a published API of SQLite, is subject to change without
  154. ** notice, and should not be referenced by programs that use SQLite.
  155. ** 有些定义被标明experimental(实验性的),这些接口不久会被加入SQLITE
  156. ** 虽然不希望改变实验性接口,会保留较小改变的权力,使用in the wild标明的地方要谨慎改变
  157. **
  158. ** Some of the definitions that are in this file are marked as
  159. ** "experimental". Experimental interfaces are normally new
  160. ** features recently added to SQLite. We do not anticipate changes
  161. ** to experimental interfaces but reserve the right to make minor changes
  162. ** if experience from use "in the wild" suggest such changes are prudent.
  163. ** SQLITE的官方C语言API文档从注解生成,这个文件在SQLITE接口操作方面具有权威
  164. **
  165. ** The official C-language API documentation for SQLite is derived
  166. ** from comments in this file. This file is the authoritative source
  167. ** on how SQLite interfaces are supposed to operate.
  168. ** 构造管理文件是sqlite.h.in,makefile对这个文件(比如嵌入式版本中)做较小改动,build过程中其名改为sqlite3.h
  169. **
  170. ** The name of this file under configuration management is "sqlite.h.in".
  171. ** The makefile makes some minor changes to this file (such as inserting
  172. ** the version number) and changes its name to "sqlite3.h" as
  173. ** part of the build process.
  174. */
  175. #ifndef SQLITE3_H
  176. #define SQLITE3_H
  177. #include <stdarg.h> /* Needed for the definition of va_list * SQLITE接口需要va_list定义 */
  178.  
  179. /*
  180. ** Make sure we can call this stuff from C++.
  181. */
  182. /*
  183. ** extern声明的函数和变量可以在本模块或其他模块中使用。
  184. ** extern "C"包含双重含义,其一:被它修饰的目标是“extern”的;其二:被它修饰的目标是“C”的。
  185. ** extern "C"仅被使用在C++调用C程序情况,C不能使用。
  186. ** #if 0把它屏蔽了,如果使用C++编译器,可以打开该选项
  187. ** 比如test.cpp(C++源码文件)需要调用myc.h这个C头文件中用extern声明的函数,可以如下书写:
  188. **
  189. ** extern "C"
  190. ** {
  191. ** #include "myc.h"
  192. ** }
  193. */
  194. #if 0
  195. extern "C" {
  196. #endif
  197.  
  198. /*
  199. ** Provide the ability to override linkage features of the interface.
  200. */
  201. //定义extern的宏,可使用SQLITE_EXTERN来完成extern功能
  202. #ifndef SQLITE_EXTERN
  203. # define SQLITE_EXTERN extern
  204. #endif
  205. //定义SQLITE_API宏
  206. #ifndef SQLITE_API
  207. # define SQLITE_API __declspec(dllexport)
  208. #endif
  209. #ifndef SQLITE_CDECL
  210. # define SQLITE_CDECL
  211. #endif
  212. #ifndef SQLITE_APICALL
  213. # define SQLITE_APICALL
  214. #endif
  215. #ifndef SQLITE_STDCALL
  216. # define SQLITE_STDCALL SQLITE_APICALL
  217. #endif
  218. #ifndef SQLITE_CALLBACK
  219. # define SQLITE_CALLBACK
  220. #endif
  221. #ifndef SQLITE_SYSAPI
  222. # define SQLITE_SYSAPI
  223. #endif
  224.  
  225. /*
  226. ** no-op宏经常在接口前标记那些实验性的和不推荐的接口。
  227. ** 新应用程序最好不使用不推荐的接口,它们支持向后兼容。程序员必须意识到实验性接口会在某个版本中改变
  228. ** These no-op macros are used in front of interfaces to mark those
  229. ** interfaces as either deprecated or experimental. New applications
  230. ** should not use deprecated interfaces - they are supported for backwards
  231. ** compatibility only. Application writers should be aware that
  232. ** experimental interfaces are subject to change in point releases.
  233. ** 这些宏在他们需要时,能实现编译器魔法compiler magic警告信息,编译器魔法最终产生BUG报告,编译器会试着使用noop宏。
  234. **
  235. ** These macros used to resolve to various kinds of compiler magic that
  236. ** would generate warning messages when they were used. But that
  237. ** compiler magic ended up generating such a flurry of bug reports
  238. ** that we have taken it all out and gone back to using simple
  239. ** noop macros.
  240. */
  241. #define SQLITE_DEPRECATED
  242. #define SQLITE_EXPERIMENTAL
  243.  
  244. /*
  245. ** Ensure these symbols were not defined by some previous header file.
  246. */
  247. // 如果SQLITE_VERSION、SQLITE_VERSION_NUMBER标志已经定义,则取消
  248. #ifdef SQLITE_VERSION
  249. # undef SQLITE_VERSION
  250. #endif
  251. #ifdef SQLITE_VERSION_NUMBER
  252. # undef SQLITE_VERSION_NUMBER
  253. #endif
  254.  
  255. /*
  256. ** CAPI3REF: Compile-Time Library Version Numbers
  257. **
  258. ** SQLITE_VERSION 宏为版本号,X.Y.Z的SQLITE版本号中,X是主版本号,Y是次版本号,Z是发行号
  259. ** ^(The [SQLITE_VERSION] C preprocessor macro in the sqlite3.h header
  260. ** evaluates to a string literal that is the SQLite version in the
  261. ** format "X.Y.Z" where X is the major version number (always 3 for
  262. ** SQLite3) and Y is the minor version number and Z is the release number.)^
  263. ** ^(The [SQLITE_VERSION_NUMBER] C preprocessor macro resolves to an integer
  264. ** with the value (X*1000000 + Y*1000 + Z) where X, Y, and Z are the same
  265. ** numbers used in [SQLITE_VERSION].)^
  266. **
  267. ** SQLITE_VERSION_NUMBER根据SQLITE_VERSION中版本号计算一个整数(X*1000000 + Y*1000 + Z)
  268. ** The SQLITE_VERSION_NUMBER for any given release of SQLite will also
  269. ** be larger than the release from which it is derived. Either Y will
  270. ** be held constant and Z will be incremented or else Y will be incremented
  271. ** and Z will be reset to zero.
  272. **
  273. ** Since version 3.6.18, SQLite source code has been stored in the
  274. ** <a href="http://www.fossil-scm.org/">Fossil configuration management
  275. ** system</a>. ^The SQLITE_SOURCE_ID macro evaluates to
  276. ** a string which identifies a particular check-in of SQLite
  277. ** within its configuration management system. ^The SQLITE_SOURCE_ID
  278. ** string contains the date and time of the check-in (UTC) and an SHA1
  279. ** hash of the entire source tree.
  280. ** SQLITE_SOURCE_ID 为一个字符串,为SQLITE配置管理系统中的check-in详情包含check-in的日期和时间以及整个源码树的SHA1哈希
  281. **
  282. ** See also: [sqlite3_libversion()],
  283. ** [sqlite3_libversion_number()], [sqlite3_sourceid()],
  284. ** [sqlite_version()] and [sqlite_source_id()].
  285. */
  286. //定义版本号,并计算SQLITE_VERSION_NUMBER
  287. #define SQLITE_VERSION "3.14.1"
  288. #define SQLITE_VERSION_NUMBER 3014001
  289. #define SQLITE_SOURCE_ID "2016-08-11 18:53:32 a12d8059770df4bca59e321c266410344242bf7b"
  290.  
  291. /*
  292. ** CAPI3REF: Run-Time Library Version Numbers
  293. ** KEYWORDS: sqlite3_version, sqlite3_sourceid
  294. **
  295. ** 这些接口通过库而不是头文件提供了SQLITE_VERSION、SQLITE_VERSION_NUMBER以**及SQLITE_SOURCE_ID宏的相同信息
  296. ** These interfaces provide the same information as the [SQLITE_VERSION],
  297. ** [SQLITE_VERSION_NUMBER], and [SQLITE_SOURCE_ID] C preprocessor macros
  298. ** but are associated with the library instead of the header file. ^(Cautious
  299. ** programmers might include assert() statements in their application to
  300. ** verify that values returned by these interfaces match the macros in
  301. ** the header, and thus ensure that the application is
  302. ** compiled with matching library and header files.
  303. **
  304. ** 可小心地通过包含以下的assert()声明,在应用程序核实匹配头文件中宏的这些接口,以确保应用程序使用相匹配的库和头文件编译
  305. ** <blockquote><pre>
  306. ** assert( sqlite3_libversion_number()==SQLITE_VERSION_NUMBER );
  307. ** assert( strcmp(sqlite3_sourceid(),SQLITE_SOURCE_ID)==0 );
  308. ** assert( strcmp(sqlite3_libversion(),SQLITE_VERSION)==0 );
  309. ** </pre></blockquote>)^
  310. **
  311. ** ^The sqlite3_version[] string constant contains the text of [SQLITE_VERSION]
  312. ** macro. ^The sqlite3_libversion() function returns a pointer to the
  313. ** to the sqlite3_version[] string constant. The sqlite3_libversion()
  314. ** function is provided for use in DLLs since DLL users usually do not have
  315. ** direct access to string constants within the DLL. ^The
  316. ** sqlite3_libversion_number() function returns an integer equal to
  317. ** [SQLITE_VERSION_NUMBER]. ^The sqlite3_sourceid() function returns
  318. ** a pointer to a string constant whose value is the same as the
  319. ** [SQLITE_SOURCE_ID] C preprocessor macro.
  320. **
  321. ** See also: [sqlite_version()] and [sqlite_source_id()].
  322. */
  323. SQLITE_API const char sqlite3_version[] = SQLITE_VERSION;
  324. SQLITE_API const char *SQLITE_STDCALL sqlite3_libversion(void);
  325. SQLITE_API const char *SQLITE_STDCALL sqlite3_sourceid(void);
  326. SQLITE_API int SQLITE_STDCALL sqlite3_libversion_number(void);
  327. //SQLITE_VERSION 宏定义了版本号,在本源码包中定义为"3.14.1"
  328. //sqlite3_version[]为前面定义的SQLITE_VERSION宏的内容,即版本号
  329. //sqlite3_libversion()返回指向sqlite3_version[]字符数组常量的指针
  330. //sqlite3_sourceid()返回一个指向SQLITE_SOURCE_ID宏内容的指针
  331. //sqlite3_libversion_number()返回SQLITE_VERSION_NUMBER宏定义的版本号
  332.  
  333. /*
  334. ** CAPI3REF: Run-Time Library Compilation Options Diagnostics
  335. **
  336. ** ^The sqlite3_compileoption_used() function returns 0 or 1
  337. ** indicating whether the specified option was defined at
  338. ** compile time. ^The SQLITE_ prefix may be omitted from the
  339. ** option name passed to sqlite3_compileoption_used().
  340. ** sqlite3_compileoption_used()返回0和1,指示编译时是否有定义的选项
  341. **
  342. ** sqlite3_compileoption_get()允许正在起作用的编译时定义的选项列表,返回N次编译时的选项字符串
  343. ** ^The sqlite3_compileoption_get() function allows iterating
  344. ** over the list of options that were defined at compile time by
  345. ** returning the N-th compile time option string. ^If N is out of range,
  346. ** sqlite3_compileoption_get() returns a NULL pointer. ^The SQLITE_
  347. ** prefix is omitted from any strings returned by
  348. ** sqlite3_compileoption_get().
  349. **
  350. ** 如果 N过界,sqlite3_compileoption_get()返回NULL指针
  351. ** ^Support for the diagnostic functions sqlite3_compileoption_used()
  352. ** and sqlite3_compileoption_get() may be omitted by specifying the
  353. ** [SQLITE_OMIT_COMPILEOPTION_DIAGS] option at compile time.
  354. **
  355. ** 编译时定义SQLITE_OMIT_COMPILEOPTION_DIAGS选项,将忽略sqlite3_compileoption_used()和 sqlite3_compileoption_get()这2个诊断函数
  356. ** See also: SQL functions [sqlite_compileoption_used()] and
  357. ** [sqlite_compileoption_get()] and the [compile_options pragma].
  358. */
  359. #ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS
  360. SQLITE_API int SQLITE_STDCALL sqlite3_compileoption_used(const char *zOptName);
  361. SQLITE_API const char *SQLITE_STDCALL sqlite3_compileoption_get(int N);
  362. #endif
  363.  
  364. /*
  365. ** 库线程安全
  366. ** CAPI3REF: Test To See If The Library Is Threadsafe
  367. **
  368. ** SQLITE_THREADSAFE预处理宏编译时选项设为0,则忽略SQLITE的互斥代码,此时,sqlite3_threadsafe()返回0
  369. ** ^The sqlite3_threadsafe() function returns zero if and only if
  370. ** SQLite was compiled with mutexing code omitted due to the
  371. ** [SQLITE_THREADSAFE] compile-time option being set to 0.
  372. ** SQLITE可在有互斥和没有互斥情况下编译,当SQLITE_THREADSAFE宏是1或2**时,互斥被允许,SQLITE是线程安全的。
  373. ** 该宏为0时,不使用互斥,超过一个线程同时使用SQLite是不安全的
  374. **
  375. ** SQLite can be compiled with or without mutexes. When
  376. ** the [SQLITE_THREADSAFE] C preprocessor macro is 1 or 2, mutexes
  377. ** are enabled and SQLite is threadsafe. When the
  378. ** [SQLITE_THREADSAFE] macro is 0,
  379. ** the mutexes are omitted. Without the mutexes, it is not safe
  380. ** to use SQLite concurrently from more than one thread.
  381. ** 允许互斥,将会产生一些可预见的后果。如果速度是第一位的,最好是禁止互斥,对于最好安全性而言,互斥要开启,默认的行为是互斥开启。
  382. **
  383. ** Enabling mutexes incurs a measurable performance penalty.
  384. ** So if speed is of utmost importance, it makes sense to disable
  385. ** the mutexes. But for maximum safety, mutexes should be enabled.
  386. ** ^The default behavior is for mutexes to be enabled.
  387. ** 这些接口被应用程序使用,确认该SQLITE版本编译链接时是否使用sqlite_threadsafe宏
  388. **
  389. ** This interface can be used by an application to make sure that the
  390. ** version of SQLite that it is linking against was compiled with
  391. ** the desired setting of the [SQLITE_THREADSAFE] macro.
  392. ** 该接口仅在编译时,互斥设置了SQLITE_THREADSAFE标志时才报告,如果SQLITE使用SQLITE_THREADSAFE=1或=2的方式编译,互斥被允许
  393. ** 但通过SQLITE_CONFIG_SINGLETHREAD、SQLITE_CONFIG_MULTITHREAD能部分或完全禁止对sqlite3_config()的调用
  394. **
  395. ** This interface only reports on the compile-time mutex setting
  396. ** of the [SQLITE_THREADSAFE] flag. If SQLite is compiled with
  397. ** SQLITE_THREADSAFE=1 or =2 then mutexes are enabled by default but
  398. ** can be fully or partially disabled using a call to [sqlite3_config()]
  399. ** with the verbs [SQLITE_CONFIG_SINGLETHREAD], [SQLITE_CONFIG_MULTITHREAD],
  400. ** or [SQLITE_CONFIG_SERIALIZED].
  401. ** sqlite3_threadsafe()函数的返回值仅指示编译时设置了线程安全不能被sqlite3_config()可在运行时改变
  402. ** ^(The return value of the sqlite3_threadsafe() function shows only the compile-time setting of
  403. ** thread safety, not any run-time changes to that setting made by
  404. ** sqlite3_config(). In other words, the return value from sqlite3_threadsafe()
  405. ** is unchanged by calls to sqlite3_config().)^
  406. ** 调用sqlite3_config()不能改变sqlite3_threadsafe()返回值
  407. **
  408. ** See the [threading mode] documentation for additional information.
  409. */
  410. SQLITE_API int SQLITE_STDCALL sqlite3_threadsafe(void);
  411.  
  412. /*
  413. ** 数据库连接句柄
  414. ** CAPI3REF: Database Connection Handle
  415. ** KEYWORDS: {database connection} {database connections}
  416. ** 关键字:{database connection} {database connections}
  417. ** 每个打开的SQLite数据库做为一个指针出现,该指针指向隐藏的sqlite3结构的实例,建议将sqlite3指针视为对象
  418. ** sqlite3_open()、sqlite3_open16()、sqlite3_open_v2()接口是这个对象的构造器,sqlite3_close()是析构器
  419. **
  420. ** Each open SQLite database is represented by a pointer to an instance of
  421. ** the opaque structure named "sqlite3". It is useful to think of an sqlite3
  422. ** pointer as an object. The [sqlite3_open()], [sqlite3_open16()], and
  423. ** [sqlite3_open_v2()] interfaces are its constructors, and [sqlite3_close()]
  424. ** and [sqlite3_close_v2()] are its destructors.
  425. ** 还有一些其它接口sqlite3_prepare_v2()、sqlite3_create_function()、sqlite3_busy_timeout()为sqlite3对象的方法
  426. **
  427. ** There are many other interfaces (such as
  428. ** [sqlite3_prepare_v2()], [sqlite3_create_function()], and
  429. ** [sqlite3_busy_timeout()] to name but three) that are methods on an
  430. ** sqlite3 object.
  431. */
  432. typedef struct sqlite3 sqlite3;
  433.  
  434. /*
  435. ** CAPI3REF: 64-Bit Integer Types
  436. ** KEYWORDS: sqlite_int64 sqlite_uint64
  437. ** 64位整数类型关键字:sqlite_int64 sqlite_uint64
  438. **
  439. ** Because there is no cross-platform way to specify 64-bit integer types
  440. ** SQLite includes typedefs for 64-bit signed and unsigned integers.
  441. ** 没有跨平台的方法定义64位整数,SQLite包括64位有符号和无符号整数
  442. **
  443. ** The sqlite3_int64 and sqlite3_uint64 are the preferred type definitions.
  444. ** sqlite3_int64和sqlite3_uint64是首选类型,sqlite_int64和sqlite_uint64仅支持向后兼容性
  445. ** The sqlite_int64 and sqlite_uint64 types are supported for backwards
  446. ** compatibility only.
  447. **
  448. ** sqlite3_int64和sqlite_int64类型的范围在-922337203685477580和+9223372036854775807之间
  449. ** sqlite3_uint64和sqlite_uint64在0和+18446744073709551615之间
  450. **
  451. ** ^The sqlite3_int64 and sqlite_int64 types can store integer values
  452. ** between -9223372036854775808 and +9223372036854775807 inclusive. ^The
  453. ** sqlite3_uint64 and sqlite_uint64 types can store integer values
  454. ** between 0 and +18446744073709551615 inclusive.
  455. */
  456. //以下根据前面定义的宏,定义sqlite_int64、sqlite_uint64、sqlite3_int64、sqlite_uint64实际使用的类型
  457. #ifdef SQLITE_INT64_TYPE
  458. typedef SQLITE_INT64_TYPE sqlite_int64;
  459. typedef unsigned SQLITE_INT64_TYPE sqlite_uint64;
  460. #elif defined(_MSC_VER) || defined(__BORLANDC__)
  461. typedef __int64 sqlite_int64;
  462. typedef unsigned __int64 sqlite_uint64;
  463. #else
  464. typedef long long int sqlite_int64;
  465. typedef unsigned long long int sqlite_uint64;
  466. #endif
  467. typedef sqlite_int64 sqlite3_int64;
  468. typedef sqlite_uint64 sqlite3_uint64;
  469.  
  470. /*
  471. ** 如果处理器没有符点支持,则用sqlite3_int64整数替代
  472. ** If compiling for a processor that lacks floating point support,
  473. ** substitute integer for floating-point.
  474. */
  475. #ifdef SQLITE_OMIT_FLOATING_POINT
  476. # define double sqlite3_int64
  477. #endif
  478.  
  479. /*
  480. ** CAPI3REF: Closing A Database Connection
  481. ** DESTRUCTOR: sqlite3
  482. **
  483. ** 关闭数据库连接,如果sqlite3对象成功卸载,所有相关资源被释放sqlite3sqlite3_close()返回SQLITE_OK
  484. **
  485. ** ^The sqlite3_close() and sqlite3_close_v2() routines are destructors
  486. ** for the [sqlite3] object.
  487. ** ^Calls to sqlite3_close() and sqlite3_close_v2() return [SQLITE_OK] if
  488. ** the [sqlite3] object is successfully destroyed and all associated
  489. ** resources are deallocated.
  490. **
  491. ** ^If the database connection is associated with unfinalized prepared
  492. ** statements or unfinished sqlite3_backup objects then sqlite3_close()
  493. ** will leave the database connection open and return [SQLITE_BUSY].
  494. ** ^If sqlite3_close_v2() is called with unfinalized prepared statements
  495. ** and/or unfinished sqlite3_backups, then the database connection becomes
  496. ** an unusable "zombie" which will automatically be deallocated when the
  497. ** last prepared statement is finalized or the last sqlite3_backup is
  498. ** finished. The sqlite3_close_v2() interface is intended for use with
  499. ** host languages that are garbage collected, and where the order in which
  500. ** destructors are called is arbitrary.
  501. **
  502. ** 应用程序必须在关闭sqlite3对象前,[sqlite3_finalize | finalize]所有的与该对象相关的[prepared statements]
  503. ** 必须[sqlite3_blob_close | close]所有的与该对象相关的[BLOB handles] (BLOB大二进制句柄)
  504. **
  505. ** Applications should [sqlite3_finalize | finalize] all [prepared statements],
  506. ** [sqlite3_blob_close | close] all [BLOB handles], and
  507. ** [sqlite3_backup_finish | finish] all [sqlite3_backup] objects associated
  508. ** with the [sqlite3] object prior to attempting to close the object. ^If
  509. ** sqlite3_close_v2() is called on a [database connection] that still has
  510. ** outstanding [prepared statements], [BLOB handles], and/or
  511. ** [sqlite3_backup] objects then it returns [SQLITE_OK] and the deallocation
  512. ** of resources is deferred until all [prepared statements], [BLOB handles],
  513. ** and [sqlite3_backup] objects are also destroyed.
  514. **
  515. ** 如果sqlite3_close()在[database connection]数据库连接被调用,该数据库连接中仍有显式的[prepared statements][BLOB handles],则返回SQLITE_BUSY
  516. **
  517. ** 当事务打开时,调用sqlite3_close(),事务自动回滚
  518. **
  519. ** ^If an [sqlite3] object is destroyed while a transaction is open,
  520. ** the transaction is automatically rolled back.
  521. **
  522. ** sqlite3_close(C)的C参数可以是NULL指针或从sqlite3_open()、sqlite3_**open16()、sqlite3_open_v2()获取的[sqlite3]对象指针
  523. ** 使用NULL参数调用sqlite3_close()为没负作用的空操作
  524. **
  525. ** The C parameter to [sqlite3_close(C)] and [sqlite3_close_v2(C)]
  526. ** must be either a NULL
  527. ** pointer or an [sqlite3] object pointer obtained
  528. ** from [sqlite3_open()], [sqlite3_open16()], or
  529. ** [sqlite3_open_v2()], and not previously closed.
  530. ** ^Calling sqlite3_close() or sqlite3_close_v2() with a NULL pointer
  531. ** argument is a harmless no-op.
  532. */
  533. SQLITE_API int SQLITE_STDCALL sqlite3_close(sqlite3*);
  534. SQLITE_API int SQLITE_STDCALL sqlite3_close_v2(sqlite3*);
  535.  
  536. /*
  537. ** The type for a callback function.
  538. ** This is legacy and deprecated. It is included for historical
  539. ** compatibility and is not documented.
  540. */
  541. //回调函数,不赞成这种旧版本遗留下来的机制,它被包含进来,为了历史兼容性,没有相关证实
  542. typedef int (*sqlite3_callback)(void*,int,char**, char**);
  543.  
  544. /*
  545. ** 查询执行接口
  546. ** CAPI3REF: One-Step Query Execution Interface
  547. ** METHOD: sqlite3
  548. **
  549. ** The sqlite3_exec() interface is a convenience wrapper around
  550. ** [sqlite3_prepare_v2()], [sqlite3_step()], and [sqlite3_finalize()],
  551. ** that allows an application to run multiple statements of SQL
  552. ** without having to use a lot of C code.
  553. **
  554. ** sqlite3_exec()接口包装了[sqlite3_prepare_v2()], [sqlite3_step()], and [sqlite3_finalize()]
  555. ** 方便使用,允许应用程序执行多语句的SQL,无使用大量C代码
  556. **
  557. ** sqlite3_exec()符合UTF8编码要求,用分号分隔的SQL语句为它的第二个参数
  558. ** database connection数据库连接为第一个参数,做为第三个参数的回调函数如果非空,则SQL执行结果的每一行都会调用该函数
  559. **
  560. ** ^The sqlite3_exec() interface runs zero or more UTF-8 encoded,
  561. ** semicolon-separate SQL statements passed into its 2nd argument,
  562. ** in the context of the [database connection] passed in as its 1st
  563. ** argument. ^If the callback function of the 3rd argument to
  564. ** sqlite3_exec() is not NULL, then it is invoked for each result row
  565. ** coming out of the evaluated SQL statements. ^The 4th argument to
  566. ** sqlite3_exec() is relayed through to the 1st argument of each
  567. ** callback invocation. ^If the callback pointer to sqlite3_exec()
  568. ** is NULL, then no callback is ever invoked and result rows are
  569. ** ignored.
  570. **
  571. ** ^If an error occurs while evaluating the SQL statements passed into
  572. ** sqlite3_exec(), then execution of the current statement stops and
  573. ** subsequent statements are skipped. ^If the 5th parameter to sqlite3_exec()
  574. ** is not NULL then any error message is written into memory obtained
  575. ** from [sqlite3_malloc()] and passed back through the 5th parameter.
  576. ** To avoid memory leaks, the application should invoke [sqlite3_free()]
  577. ** on error message strings returned through the 5th parameter of
  578. ** sqlite3_exec() after the error message string is no longer needed.
  579. ** ^If the 5th parameter to sqlite3_exec() is not NULL and no errors
  580. ** occur, then sqlite3_exec() sets the pointer in its 5th parameter to
  581. ** NULL before returning.
  582. **
  583. ** ^If an sqlite3_exec() callback returns non-zero, the sqlite3_exec()
  584. ** routine returns SQLITE_ABORT without invoking the callback again and
  585. ** without running any subsequent SQL statements.
  586. **
  587. ** ^The 2nd argument to the sqlite3_exec() callback function is the
  588. ** number of columns in the result. ^The 3rd argument to the sqlite3_exec()
  589. ** callback is an array of pointers to strings obtained as if from
  590. ** [sqlite3_column_text()], one for each column. ^If an element of a
  591. ** result row is NULL then the corresponding string pointer for the
  592. ** sqlite3_exec() callback is a NULL pointer. ^The 4th argument to the
  593. ** sqlite3_exec() callback is an array of pointers to strings where each
  594. ** entry represents the name of corresponding result column as obtained
  595. ** from [sqlite3_column_name()].
  596. **
  597. ** ^If the 2nd parameter to sqlite3_exec() is a NULL pointer, a pointer
  598. ** to an empty string, or a pointer that contains only whitespace and/or
  599. ** SQL comments, then no SQL statements are evaluated and the database
  600. ** is not changed.
  601. **
  602. ** Restrictions:
  603. **
  604. ** <ul>
  605. ** <li> The application must ensure that the 1st parameter to sqlite3_exec()
  606. ** is a valid and open [database connection].
  607. ** <li> The application must not close the [database connection] specified by
  608. ** the 1st parameter to sqlite3_exec() while sqlite3_exec() is running.
  609. ** <li> The application must not modify the SQL statement text passed into
  610. ** the 2nd parameter of sqlite3_exec() while sqlite3_exec() is running.
  611. ** </ul>
  612. */
  613. SQLITE_API int SQLITE_STDCALL sqlite3_exec(
  614. sqlite3*, /* An open database */
  615. const char *sql, /* SQL to be evaluated */
  616. int (*callback)(void*,int,char**,char**), /* Callback function */
  617. void *, /* 1st argument to callback */
  618. char **errmsg /* Error msg written here */
  619. );
  1. /************** Continuing where we left off in sqliteInt.h ******************/
  2.  
  3. /*
  4. ** Include the configuration header output by 'configure' if we're using the
  5. ** autoconf-based build
  6. */
  7. #ifdef _HAVE_SQLITE_CONFIG_H //如果我们使用autoconf-based自动配置基础创建,则include "config.h"配置文件
  8. #include "config.h"
  9. #endif
  10.  
  11. /************** Include sqliteLimit.h in the middle of sqliteInt.h ***********/
  12. /************** Begin file sqliteLimit.h *************************************/
  13. /*
  14. ** 2007 May 7
  15. **
  16. ** The author disclaims copyright to this source code. In place of
  17. ** a legal notice, here is a blessing:
  18. **
  19. ** May you do good and not evil.
  20. ** May you find forgiveness for yourself and forgive others.
  21. ** May you share freely, never taking more than you give.
  22. **
  23. *************************************************************************
  24. **
  25. ** This file defines various limits of what SQLite can process.
  26. */
  27.  
  28. /*
  29. ** The maximum length of a TEXT or BLOB in bytes. This also
  30. ** limits the size of a row in a table or index.
  31. **
  32. ** The hard limit is the ability of a 32-bit signed integer
  33. ** to count the size: 2^31-1 or 2147483647.
  34. */
  35. #ifndef SQLITE_MAX_LENGTH //最大32位有符号整数,定义BLOB或TEXT字段的最大允许字节数为1000000000
  36. # define SQLITE_MAX_LENGTH
  37. #endif
  38.  
  39. /*
  40. ** This is the maximum number of
  41. **
  42. ** 定义以下这些项的最大值:
  43. ** 表中的列、索引中的列、视图的列、update的set从句的数量、select
  44. ** 返回的字段数、GROUP BY 和ORDER BY的字段数、INSERT的values从句
  45. **
  46. ** * Columns in a table
  47. ** * Columns in an index
  48. ** * Columns in a view
  49. ** * Terms in the SET clause of an UPDATE statement
  50. ** * Terms in the result set of a SELECT statement
  51. ** * Terms in the GROUP BY or ORDER BY clauses of a SELECT statement.
  52. ** * Terms in the VALUES clause of an INSERT statement
  53. **
  54. ** The hard upper limit here is 32676. Most database people will
  55. ** tell you that in a well-normalized database, you usually should
  56. ** not have more than a dozen or so columns in any table. And if
  57. ** that is the case, there is no point in having more than a few
  58. ** dozen values in any of the other situations described above.
  59. */
  60. #ifndef SQLITE_MAX_COLUMN //定义最大列数为2000
  61. # define SQLITE_MAX_COLUMN
  62. #endif
  63.  
  64. /*
  65. ** The maximum length of a single SQL statement in bytes.
  66. **
  67. ** It used to be the case that setting this value to zero would
  68. ** turn the limit off. That is no longer true. It is not possible
  69. ** to turn this limit off.
  70. */
  71. #ifndef SQLITE_MAX_SQL_LENGTH //定义单个sql语句最长字节数为1000000000
  72. # define SQLITE_MAX_SQL_LENGTH
  73. #endif
  74.  
  75. /*
  76. ** The maximum depth of an expression tree. This is limited to
  77. ** some extent by SQLITE_MAX_SQL_LENGTH. But sometime you might
  78. ** want to place more severe limits on the complexity of an
  79. ** expression.
  80. **
  81. ** A value of 0 used to mean that the limit was not enforced.
  82. ** But that is no longer true. The limit is now strictly enforced
  83. ** at all times.
  84. */
  85. #ifndef SQLITE_MAX_EXPR_DEPTH //定义解释树的最大深度为1000,为0即不限制
  86. # define SQLITE_MAX_EXPR_DEPTH
  87. #endif
  88.  
  89. /*
  90. ** The maximum number of terms in a compound SELECT statement.
  91. ** The code generator for compound SELECT statements does one
  92. ** level of recursion for each term. A stack overflow can result
  93. ** if the number of terms is too large. In practice, most SQL
  94. ** never has more than 3 or 4 terms. Use a value of 0 to disable
  95. ** any limit on the number of terms in a compount SELECT.
  96. */
  97. #ifndef SQLITE_MAX_COMPOUND_SELECT //定义复合select语句的最大项数为500,为0表示不限制
  98. # define SQLITE_MAX_COMPOUND_SELECT
  99. #endif
  100.  
  101. /*
  102. ** The maximum number of opcodes in a VDBE program.
  103. ** Not currently enforced.
  104. */
  105. #ifndef SQLITE_MAX_VDBE_OP //定义VDBE程序的操作符数量,最大为25000
  106. # define SQLITE_MAX_VDBE_OP
  107. #endif
  108.  
  109. /*
  110. ** The maximum number of arguments to an SQL function.
  111. */
  112. #ifndef SQLITE_MAX_FUNCTION_ARG //定义SQL函数的最大参数数量为127
  113. # define SQLITE_MAX_FUNCTION_ARG
  114. #endif
  115.  
  116. /*
  117. ** The suggested maximum number of in-memory pages to use for
  118. ** the main database table and for temporary tables.
  119. **
  120. ** IMPLEMENTATION-OF: R-30185-15359 The default suggested cache size is -2000,
  121. ** which means the cache size is limited to 2048000 bytes of memory.
  122. ** IMPLEMENTATION-OF: R-48205-43578 The default suggested cache size can be
  123. ** altered using the SQLITE_DEFAULT_CACHE_SIZE compile-time options.
  124. */
  125. #ifndef SQLITE_DEFAULT_CACHE_SIZE //主数据库表最大缓冲内存页数(使用),即最大内存大小
  126. # define SQLITE_DEFAULT_CACHE_SIZE
  127. #endif
  128.  
  129. /*
  130. ** The default number of frames to accumulate in the log file before
  131. ** checkpointing the database in WAL mode.
  132. */
  133. #ifndef SQLITE_DEFAULT_WAL_AUTOCHECKPOINT
  134. # define SQLITE_DEFAULT_WAL_AUTOCHECKPOINT
  135. #endif
  136.  
  137. /*
  138. ** The maximum number of attached databases. This must be between 0
  139. ** and 125. The upper bound of 125 is because the attached databases are
  140. ** counted using a signed 8-bit integer which has a maximum value of 127
  141. ** and we have to allow 2 extra counts for the "main" and "temp" databases.
  142. */
  143. #ifndef SQLITE_MAX_ATTACHED //附加数据库的数目(最大)<0-30之间>
  144. # define SQLITE_MAX_ATTACHED
  145. #endif
  146.  
  147. /*
  148. ** The maximum value of a ?nnn wildcard that the parser will accept.
  149. */
  150. #ifndef SQLITE_MAX_VARIABLE_NUMBER //定义解析器能接受的通配符(匹配符参数)的最大值
  151. # define SQLITE_MAX_VARIABLE_NUMBER
  152. #endif
  153.  
  154. /* Maximum page size. The upper bound on this value is 65536. This a limit
  155. ** imposed by the use of 16-bit offsets within each page.
  156. **
  157. ** Earlier versions of SQLite allowed the user to change this value at
  158. ** compile time. This is no longer permitted, on the grounds that it creates
  159. ** a library that is technically incompatible with an SQLite library
  160. ** compiled with a different limit. If a process operating on a database
  161. ** with a page-size of 65536 bytes crashes, then an instance of SQLite
  162. ** compiled with the default page-size limit will not be able to rollback
  163. ** the aborted transaction. This could lead to database corruption.
  164. */
  165. #ifdef SQLITE_MAX_PAGE_SIZE //定义最大页面大小
  166. # undef SQLITE_MAX_PAGE_SIZE
  167. #endif
  168. #define SQLITE_MAX_PAGE_SIZE 65536
  169.  
  170. /*
  171. ** The default size of a database page.
  172. */
  173. #ifndef SQLITE_DEFAULT_PAGE_SIZE //定义数据库页面的默认大小
  174. # define SQLITE_DEFAULT_PAGE_SIZE
  175. #endif
  176. #if SQLITE_DEFAULT_PAGE_SIZE>SQLITE_MAX_PAGE_SIZE
  177. # undef SQLITE_DEFAULT_PAGE_SIZE
  178. # define SQLITE_DEFAULT_PAGE_SIZE SQLITE_MAX_PAGE_SIZE
  179. #endif
  180.  
  181. /*
  182. ** Ordinarily, if no value is explicitly provided, SQLite creates databases
  183. ** with page size SQLITE_DEFAULT_PAGE_SIZE. However, based on certain
  184. ** device characteristics (sector-size and atomic write() support),
  185. ** SQLite may choose a larger value. This constant is the maximum value
  186. ** SQLite will choose on its own.
  187. */
  188. #ifndef SQLITE_MAX_DEFAULT_PAGE_SIZE //定义数据库页面的最大默认大小
  189. # define SQLITE_MAX_DEFAULT_PAGE_SIZE
  190. #endif
  191. #if SQLITE_MAX_DEFAULT_PAGE_SIZE>SQLITE_MAX_PAGE_SIZE
  192. # undef SQLITE_MAX_DEFAULT_PAGE_SIZE
  193. # define SQLITE_MAX_DEFAULT_PAGE_SIZE SQLITE_MAX_PAGE_SIZE
  194. #endif
  195.  
  196. /*
  197. ** Maximum number of pages in one database file.
  198. **
  199. ** This is really just the default value for the max_page_count pragma.
  200. ** This value can be lowered (or raised) at run-time using that the
  201. ** max_page_count macro.
  202. */
  203. #ifndef SQLITE_MAX_PAGE_COUNT //定义单个数据库文件的最大页数
  204. # define SQLITE_MAX_PAGE_COUNT
  205. #endif
  206.  
  207. /*
  208. ** Maximum length (in bytes) of the pattern in a LIKE or GLOB
  209. ** operator.
  210. */
  211. #ifndef SQLITE_MAX_LIKE_PATTERN_LENGTH //定义LIKE or GLOB模式的最大长度(字节)
  212. # define SQLITE_MAX_LIKE_PATTERN_LENGTH
  213. #endif
  214.  
  215. /*
  216. ** Maximum depth of recursion for triggers.
  217. **
  218. ** A value of 1 means that a trigger program will not be able to itself
  219. ** fire any triggers. A value of 0 means that no trigger programs at all
  220. ** may be executed.
  221. */
  222. #ifndef SQLITE_MAX_TRIGGER_DEPTH //定义触发器程序的递归深度
  223. # define SQLITE_MAX_TRIGGER_DEPTH
  224. #endif
  225.  
  226. /************** End of sqliteLimit.h *****************************************/
  227. /************** SQLITE限制参数定义完毕 ***************************************/
  228.  
  229. /************** Continuing where we left off in sqliteInt.h ******************/
  230.  
  231. /* Disable nuisance warnings on Borland compilers
  232. ** 禁止Borland编译器的警告
  233. */
  234. #if defined(__BORLANDC__)
  235. #pragma warn -rch /* unreachable code */
  236. #pragma warn -ccc /* Condition is always true or false */
  237. #pragma warn -aus /* Assigned value is never used */
  238. #pragma warn -csu /* Comparing signed and unsigned */
  239. #pragma warn -spa /* Suspicious pointer arithmetic */
  240. #endif
  241.  
  242. /*
  243. ** Include standard header files as necessary
  244. ** 包含必要的头文件
  245. */
  246. #ifdef HAVE_STDINT_H
  247. #include <stdint.h>
  248. #endif
  249. #ifdef HAVE_INTTYPES_H
  250. #include <inttypes.h>
  251. #endif
  252.  
  253. /*
  254. ** 下列宏完成指针转整数和整数转指针
  255. ** The following macros are used to cast pointers to integers and
  256. ** integers to pointers. The way you do this varies from one compiler
  257. ** to the next, so we have developed the following set of #if statements
  258. ** to generate appropriate macros for a wide range of compilers.
  259. **
  260. ** The correct "ANSI" way to do this is to use the intptr_t type.
  261. ** Unfortunately, that typedef is not available on all compilers, or
  262. ** if it is available, it requires an #include of specific headers
  263. ** that vary from one machine to the next.
  264. **
  265. ** Ticket #3860: The llvm-gcc-4.2 compiler from Apple chokes on
  266. ** the ((void*)&((char*)0)[X]) construct. But MSVC chokes on ((void*)(X)).
  267. ** So we have to define the macros in different ways depending on the
  268. ** compiler.
  269. */
  270. #if defined(__PTRDIFF_TYPE__) /* This case should work for GCC */
  271. # define SQLITE_INT_TO_PTR(X) ((void*)(__PTRDIFF_TYPE__)(X))
  272. # define SQLITE_PTR_TO_INT(X) ((int)(__PTRDIFF_TYPE__)(X))
  273. #elif !defined(__GNUC__) /* Works for compilers other than LLVM */
  274. # define SQLITE_INT_TO_PTR(X) (()[X])
  275. # define SQLITE_PTR_TO_INT(X) (())
  276. #elif defined(HAVE_STDINT_H) /* Use this case if we have ANSI headers */
  277. # define SQLITE_INT_TO_PTR(X) ((void*)(intptr_t)(X))
  278. # define SQLITE_PTR_TO_INT(X) ((int)(intptr_t)(X))
  279. #else /* Generates a warning - but it always works */
  280. # define SQLITE_INT_TO_PTR(X) ((void*)(X))
  281. # define SQLITE_PTR_TO_INT(X) ((int)(X))
  282. #endif
  283.  
  284. /*
  285. ** A macro to hint to the compiler that a function should not be
  286. ** inlined.
  287. */
  288. #if defined(__GNUC__)
  289. # define SQLITE_NOINLINE __attribute__((noinline))
  290. #elif defined(_MSC_VER) && _MSC_VER>=1310
  291. # define SQLITE_NOINLINE __declspec(noinline)
  292. #else
  293. # define SQLITE_NOINLINE
  294. #endif
  295.  
  296. /*
  297. ** Make sure that the compiler intrinsics we desire are enabled when
  298. ** compiling with an appropriate version of MSVC unless prevented by
  299. ** the SQLITE_DISABLE_INTRINSIC define.
  300. */
  301. #if !defined(SQLITE_DISABLE_INTRINSIC)
  302. #
  303. # if !defined(_WIN32_WCE)
  304. # include <intrin.h>
  305. # pragma intrinsic(_byteswap_ushort)
  306. # pragma intrinsic(_byteswap_ulong)
  307. # pragma intrinsic(_ReadWriteBarrier)
  308. # else
  309. # include <cmnintrin.h>
  310. # endif
  311. # endif
  312. #endif
  313.  
  314. /*
  315. ** SQLITE_THREADSAFE线程安全宏被定义为0、1或2
  316. ** The SQLITE_THREADSAFE macro must be defined as 0, 1, or 2.
  317. ** 0 means mutexes are permanently disable and the library is never
  318. ** threadsafe. 1 means the library is serialized which is the highest
  319. ** level of threadsafety. 2 means the library is multithreaded - multiple
  320. ** threads can use SQLite as long as no two threads try to use the same
  321. ** database connection at the same time.
  322. **
  323. ** Older versions of SQLite used an optional THREADSAFE macro.
  324. ** We support that for legacy.
  325. */
  326. #if !defined(SQLITE_THREADSAFE)
  327. # if defined(THREADSAFE)
  328. # define SQLITE_THREADSAFE THREADSAFE
  329. # else
  330. # define SQLITE_THREADSAFE /* IMP: R-07272-22309 */
  331. # endif
  332. #endif
  333.  
  334. /*
  335. ** Powersafe overwrite is on by default. But can be turned off using
  336. ** the -DSQLITE_POWERSAFE_OVERWRITE=0 command-line option.
  337. */
  338. #ifndef SQLITE_POWERSAFE_OVERWRITE
  339. # define SQLITE_POWERSAFE_OVERWRITE
  340. #endif
  341.  
  342. /*
  343. ** EVIDENCE-OF: R-25715-37072 Memory allocation statistics are enabled by
  344. ** default unless SQLite is compiled with SQLITE_DEFAULT_MEMSTATUS=0 in
  345. ** which case memory allocation statistics are disabled by default.
  346. */
  347. #if !defined(SQLITE_DEFAULT_MEMSTATUS) //SQLITE_DEFAULT_MEMSTATUS宏被定义为0或1,在运行时可使用sqlite3_config() API修改该值
  348. # define SQLITE_DEFAULT_MEMSTATUS
  349. #endif
  350.  
  351. /*
  352. ** Exactly one of the following macros must be defined in order to
  353. ** specify which memory allocation subsystem to use.
  354. **
  355. ** 该宏不一定要被定义,使用SQLITE_SYSTEM_MALLOC标准内存分配系统malloc()还是malloc()的SQLITE_MEMDEBUG调试版本
  356. **
  357. ** SQLITE_SYSTEM_MALLOC // Use normal system malloc()
  358. ** SQLITE_WIN32_MALLOC // Use Win32 native heap API
  359. ** SQLITE_ZERO_MALLOC // Use a stub allocator that always fails
  360. ** SQLITE_MEMDEBUG // Debugging version of system malloc()
  361. **
  362. ** On Windows, if the SQLITE_WIN32_MALLOC_VALIDATE macro is defined and the
  363. ** assert() macro is enabled, each call into the Win32 native heap subsystem
  364. ** will cause HeapValidate to be called. If heap validation should fail, an
  365. ** assertion will be triggered.
  366. **
  367. ** If none of the above are defined, then set SQLITE_SYSTEM_MALLOC as
  368. ** the default.
  369. */
  370. // SQLITE_SYSTEM_MALLOC和SQLITE_MEMDEBUG不能同时被定义
  371. #if defined(SQLITE_SYSTEM_MALLOC) \
  372. + defined(SQLITE_WIN32_MALLOC) \
  373. + defined(SQLITE_ZERO_MALLOC) \
  374. + defined(SQLITE_MEMDEBUG)>
  375. # error "Two or more of the following compile-time configuration options\
  376. are defined but at most one is allowed:\
  377. SQLITE_SYSTEM_MALLOC, SQLITE_WIN32_MALLOC, SQLITE_MEMDEBUG,\
  378. SQLITE_ZERO_MALLOC"
  379. #endif
  380. // 默认使用SQLITE_SYSTEM_MALLOC标准
  381. #if defined(SQLITE_SYSTEM_MALLOC) \
  382. + defined(SQLITE_WIN32_MALLOC) \
  383. + defined(SQLITE_ZERO_MALLOC) \
  384. + defined(SQLITE_MEMDEBUG)==
  385. # define SQLITE_SYSTEM_MALLOC
  386. #endif
  387.  
  388. /*
  389. ** SQLITE_MALLOC_SOFT_LIMIT非0,则试图把分配的内存控制在这些值以内
  390. ** If SQLITE_MALLOC_SOFT_LIMIT is not zero, then try to keep the
  391. ** sizes of memory allocations below this value where possible.
  392. */
  393. #if !defined(SQLITE_MALLOC_SOFT_LIMIT)
  394. # define SQLITE_MALLOC_SOFT_LIMIT
  395. #endif
  396.  
  397. /*
  398. ** We need to define _XOPEN_SOURCE as follows in order to enable
  399. ** recursive mutexes on most Unix systems and fchmod() on OpenBSD.
  400. ** But _XOPEN_SOURCE define causes problems for Mac OS X, so omit
  401. ** it.
  402. */
  403. // 在大多数UNIX系统中,我们需要定义_XOPEN_SOURCE允许递归互斥
  404. // 对于Mac OS X, _XOPEN_SOURCE导致一些问题发生
  405. #if !defined(_XOPEN_SOURCE) && !defined(__DARWIN__) && !defined(__APPLE__)
  406. # define _XOPEN_SOURCE
  407. #endif
  408.  
  409. /*
  410. ** NDEBUG and SQLITE_DEBUG are opposites. It should always be true that
  411. ** defined(NDEBUG)==!defined(SQLITE_DEBUG). If this is not currently true,
  412. ** make it true by defining or undefining NDEBUG.
  413. **
  414. ** Setting NDEBUG makes the code smaller and faster by disabling the
  415. ** assert() statements in the code. So we want the default action
  416. ** to be for NDEBUG to be set and NDEBUG to be undefined only if SQLITE_DEBUG
  417. ** is set. Thus NDEBUG becomes an opt-in rather than an opt-out
  418. ** feature.
  419. */
  420. // NDEBUG设置能使代码更小,且运行地更快
  421. #if !defined(NDEBUG) && !defined(SQLITE_DEBUG)
  422. # define NDEBUG
  423. #endif
  424. #if defined(NDEBUG) && defined(SQLITE_DEBUG)
  425. # undef NDEBUG
  426. #endif
  427.  
  428. /*
  429. ** Enable SQLITE_ENABLE_EXPLAIN_COMMENTS if SQLITE_DEBUG is turned on.
  430. */
  431. #if !defined(SQLITE_ENABLE_EXPLAIN_COMMENTS) && defined(SQLITE_DEBUG)
  432. # define SQLITE_ENABLE_EXPLAIN_COMMENTS
  433. #endif
  434.  
  435. /*
  436. ** The testcase() macro is used to aid in coverage testing. When
  437. ** doing coverage testing, the condition inside the argument to
  438. ** testcase() must be evaluated both true and false in order to
  439. ** get full branch coverage. The testcase() macro is inserted
  440. ** to help ensure adequate test coverage in places where simple
  441. ** condition/decision coverage is inadequate. For example, testcase()
  442. ** can be used to make sure boundary values are tested. For
  443. ** bitmask tests, testcase() can be used to make sure each bit
  444. ** is significant and used at least once. On switch statements
  445. ** where multiple cases go to the same block of code, testcase()
  446. ** can insure that all cases are evaluated.
  447. **
  448. */
  449. // 在覆盖测试时使用testcase()宏
  450. #ifdef SQLITE_COVERAGE_TEST
  451. SQLITE_PRIVATE void sqlite3Coverage(int);
  452. # define testcase(X) if( X ){ sqlite3Coverage(__LINE__); }
  453. #else
  454. # define testcase(X)
  455. #endif
  456.  
  457. /*
  458. ** The TESTONLY macro is used to enclose variable declarations or
  459. ** other bits of code that are needed to support the arguments
  460. ** within testcase() and assert() macros.
  461. */
  462. // TESTONLY将变量声明或需要使用testcase()和assert()宏的参数的代码片断包围
  463. #if !defined(NDEBUG) || defined(SQLITE_COVERAGE_TEST)
  464. # define TESTONLY(X) X
  465. #else
  466. # define TESTONLY(X)
  467. #endif
  468.  
  469. /*
  470. ** Sometimes we need a small amount of code such as a variable initialization
  471. ** to setup for a later assert() statement. We do not want this code to
  472. ** appear when assert() is disabled. The following macro is therefore
  473. ** used to contain that setup code. The "VVA" acronym stands for
  474. ** "Verification, Validation, and Accreditation". In other words, the
  475. ** code within VVA_ONLY() will only run during verification processes.
  476. */
  477. // 在写一段代码如变量初始化时,需要设置assert()语句进行验证,当assert()被禁止时,我们不希望看到这些代码,包括VVA_ONLY()的代码仅在验证后才运行
  478. #ifndef NDEBUG
  479. # define VVA_ONLY(X) X
  480. #else
  481. # define VVA_ONLY(X)
  482. #endif
  483.  
  484. /*
  485. ** ALWAYS和NEVER宏环绕boolean表达式,分别为true或false,代码中的这些表达式能被完全忽略,但他们在少数情况下被用于提高SQLITE异常恢复能力,使代码自修复
  486. ** The ALWAYS and NEVER macros surround boolean expressions which
  487. ** are intended to always be true or false, respectively. Such
  488. ** expressions could be omitted from the code completely. But they
  489. ** are included in a few cases in order to enhance the resilience
  490. ** of SQLite to unexpected behavior - to make the code "self-healing"
  491. ** or "ductile" rather than being "brittle" and crashing at the first
  492. ** hint of unplanned behavior.
  493. **
  494. ** ALWAYS和NEVER可被用于防御代。当做覆盖测试时,ALWAYS和NEVER被硬编码为true和false,无法访问的代码,不能算作未经测试的代码
  495. **
  496. ** In other words, ALWAYS and NEVER are added for defensive code.
  497. **
  498. ** When doing coverage testing ALWAYS and NEVER are hard-coded to
  499. ** be true and false so that the unreachable code they specify will
  500. ** not be counted as untested code.
  501. */
  502. #if defined(SQLITE_COVERAGE_TEST) || defined(SQLITE_MUTATION_TEST)
  503. # define ALWAYS(X) ()
  504. # define NEVER(X) ()
  505. #elif !defined(NDEBUG)
  506. # define ALWAYS(X) ((X)?:(assert(),))
  507. # define NEVER(X) ((X)?(assert(),):)
  508. #else
  509. # define ALWAYS(X) (X)
  510. # define NEVER(X) (X)
  511. #endif
  512.  
  513. /*
  514. ** Some malloc failures are only possible if SQLITE_TEST_REALLOC_STRESS is
  515. ** defined. We need to defend against those failures when testing with
  516. ** SQLITE_TEST_REALLOC_STRESS, but we don't want the unreachable branches
  517. ** during a normal build. The following macro can be used to disable tests
  518. ** that are always false except when SQLITE_TEST_REALLOC_STRESS is set.
  519. */
  520. #if defined(SQLITE_TEST_REALLOC_STRESS)
  521. # define ONLY_IF_REALLOC_STRESS(X) (X)
  522. #elif !defined(NDEBUG)
  523. # define ONLY_IF_REALLOC_STRESS(X) ((X)?(assert(),):)
  524. #else
  525. # define ONLY_IF_REALLOC_STRESS(X) ()
  526. #endif
  527.  
  528. /*
  529. ** Declarations used for tracing the operating system interfaces.
  530. */
  531. #if defined(SQLITE_FORCE_OS_TRACE) || defined(SQLITE_TEST) || \
  532. (defined(SQLITE_DEBUG) && SQLITE_OS_WIN)
  533. extern int sqlite3OSTrace;
  534. # define OSTRACE(X) if( sqlite3OSTrace ) sqlite3DebugPrintf X
  535. # define SQLITE_HAVE_OS_TRACE
  536. #else
  537. # define OSTRACE(X)
  538. # undef SQLITE_HAVE_OS_TRACE
  539. #endif
  540.  
  541. /*
  542. ** Is the sqlite3ErrName() function needed in the build? Currently,
  543. ** it is needed by "mutex_w32.c" (when debugging), "os_win.c" (when
  544. ** OSTRACE is enabled), and by several "test*.c" files (which are
  545. ** compiled using SQLITE_TEST).
  546. */
  547. #if defined(SQLITE_HAVE_OS_TRACE) || defined(SQLITE_TEST) || \
  548. (defined(SQLITE_DEBUG) && SQLITE_OS_WIN)
  549. # define SQLITE_NEED_ERR_NAME
  550. #else
  551. # undef SQLITE_NEED_ERR_NAME
  552. #endif
  553.  
  554. /*
  555. ** SQLITE_ENABLE_EXPLAIN_COMMENTS is incompatible with SQLITE_OMIT_EXPLAIN
  556. */
  557. #ifdef SQLITE_OMIT_EXPLAIN
  558. # undef SQLITE_ENABLE_EXPLAIN_COMMENTS
  559. #endif
  560.  
  561. /*
  562. ** Return true (non-zero) if the input is an integer that is too large
  563. ** to fit in 32-bits. This macro is used inside of various testcase()
  564. ** macros to verify that we have tested SQLite for large-file support.
  565. */
  566. #define IS_BIG_INT(X) (((X)&~(i64)0xffffffff)!=0)
  567.  
  568. /*
  569. ** 对于包围的boolean表达式,unlikely()为false,likely()为true
  570. ** The macro unlikely() is a hint that surrounds a boolean
  571. ** expression that is usually false. Macro likely() surrounds
  572. ** a boolean expression that is usually true. These hints could,
  573. ** in theory, be used by the compiler to generate better code, but
  574. ** currently they are just comments for human readers.
  575. */
  576. #define likely(X) (X)
  577. #define unlikely(X) (X)

SQLite源程序分析之sqlite3.c的更多相关文章

  1. SQLite源程序分析之回叫机制

    1.SQL访问数据库非常方便,只需简单的三个函数: sqlite3_open(char* szDbFileName, sqlite3 ** db) sqlite3_exec(sqlite3 *db, ...

  2. SQLite3源程序分析之查询处理及优化

    前言 查询处理及优化是关系数据库得以流行的根本原因,也是关系数据库系统最核心的技术之一.SQLite的查询处理模块很精致,而且很容易移植到不支持SQL的存储引擎(Berkeley DB最新的版本已经将 ...

  3. SQLite3源程序分析之虚拟机

    前言 最早的虚拟机可追溯到IBM的VM/370,到上个世纪90年代,在计算机程序设计语言领域又出现一件革命性的事情——Java语言的出现,它与c++最大的不同在于它必须在Java虚拟机上运行.Java ...

  4. SQLite3源程序分析之分析器的生成

    1.概述 Lemon是一个LALR(1)文法分析器生成工具,与bison和yacc类似,是一个可以独立于SQLite使用的开源的分析器生成工具.而且它使用与yacc(bison)不同的语法规则,可以减 ...

  5. Swift - 操作SQLite数据库(引用SQLite3库)

    SQLite轻量级数据库在移动应用中使用非常普遍,但是目前的库是C编写的,为了方便使用,对SQLite相关的操作用Swift进行了封装.这个封装代码使用了一个开源项目SQLiteDB,地址是:http ...

  6. python模块分析之sqlite3数据库

    SQLite作为一种应用广泛的文件式关系型数据库,python操作sqlite主要有两种方式,原生SQL语句和ORM映射工具. SQLAlchemy连接SQLITE SQLAlchemy是一款优秀的p ...

  7. Android SQLite性能分析

    作为Android预置的数据库模块,对SQLite的深入理解是很有必要的,能够从中找到一些优化的方向. 这里对SQLite的性能和内存进行了一些測试分析.对照了不同操作的运行性能和内存占用的情况,粗略 ...

  8. Sqlite数据库sqlite3命令小记

    SQLite库包含一个名字叫做sqlite3的命令行,它可以让用户手工输入并执行面向SQLite数据库的SQL命令.本文档提供一个样使用sqlite3的简要说明. 开始 启动sqlite3程序,仅仅需 ...

  9. [原]SQLite的学习系列之获取数据库版本二

    本系列文章主要是使用C++语言来调用其API,达到管中窥豹的目的.另外本文使用的开发环境为mac + clion,并且基于SQLite 3.7.14来进行开发. 一.去下载sqlite-amalgam ...

随机推荐

  1. Windows Phone Toolkit 的 DatePicker 控件本地化的问题

    用到 The Windows Phone Toolkit 里的 DatePicker 控件,但是多语言的时候出现了问题: 手机设置为中文,虽然月份跟星期有效,但是 Title 却还是默认的语言:CHO ...

  2. React-Native学习系列(一)

    近段时间一直在忙,所以博客也没有更新,这两天我翻了一下写的这几篇博客,感觉写的都很片面,所以,我想重新写一个系列教程,从最基础的开始,来让大家更容易学会React-Native. 这个系列大部分只介绍 ...

  3. LINQ to SQL语句(17)之对象加载

    对象加载 延迟加载 在查询某对象时,实际上你只查询该对象.不会同时自动获取这个对象.这就是延迟加载. 例如,您可能需要查看客户数据和订单数据.你最初不一定需要检索与每个客户有关的所有订单数据.其优点是 ...

  4. 【C#进阶系列】27 I/O限制的异步操作

    上一章讲到了用线程池,任务,并行类的函数,PLINQ等各种方式进行基于线程池的计算限制异步操作. 而本章讲的是如何异步执行I/O限制操作,允许将任务交给硬件设备来处理,期间完全不占用线程和CPU资源. ...

  5. bootstrap表格分页

    <script src="~/Scripts/jquery.min.js"></script> <script src="~/Scripts ...

  6. AngularJs最简单解决跨域问题案例

    AngularJs最简单解决跨域问题案例 2016-05-20 09:18 82人阅读 评论(0) 收藏 举报  分类: javascript(1)  作者:白狼 出处:http://www.mank ...

  7. Less配置环境

    一.安装Sublime 插件 1.安装Less插件: ctrl+shift+p>install Package>输入less按Enter 2.安装Less2CSS插件:ctrl+shift ...

  8. Oracle研究专题:Oracle系统安装与配置

    最近开始研究Oracle数据库,盖因公司的系统要么Oracle要么是mysql吧. 作为一个IT工作者,没有碰过Oracle是一件很匪夷所思得事情. 想到过去几年,乃至接触IT行业开始就只有玩过sql ...

  9. Android中使用ExpandableListView实现好友分组

    一个视图显示垂直滚动两级列表中的条目.这不同于列表视图,允许两个层次,类似于QQ的好友分组.要实现这个效果的整体思路为: 1.要给ExpandableListView 设置适配器,那么必须先设置数据源 ...

  10. unable to boot the simulator,无法启动模拟器已解决

    突然模拟器报错:unable to boot the simulator(无法启动模拟器) 试了好几种解决办法,删除所有的模拟器重启以后再添加,删除钥匙串登陆中的证书,重新安装Xcode都不行 最后通 ...