sqlite
[Ported from github.com/sqlite/sqlite] Fix bugs in and improve the interface to the ".check" command of the CLI.
Brought by drakestapleton
sqlite / README.md
| 1 | <h1 align="center">SQLite Source Repository</h1> |
| 2 | |
| 3 | This repository contains the complete source code for the |
| 4 | [SQLite database engine](https://sqlite.org/) going back |
| 5 | to 2000-05-29. The tree includes many tests and some |
| 6 | documentation, though additional tests and most documentation |
| 7 | are managed separately. |
| 8 | |
| 9 | See the [on-line documentation](https://sqlite.org/) for more information |
| 10 | about what SQLite is and how it works from a user's perspective. This |
| 11 | README file is about the source code that goes into building SQLite, |
| 12 | not about how SQLite is used. |
| 13 | |
| 14 | ## Version Control |
| 15 | |
| 16 | SQLite sources are managed using |
| 17 | [Fossil](https://fossil-scm.org/), a distributed version control system |
| 18 | that was specifically designed and written to support SQLite development. |
| 19 | The [Fossil repository](https://sqlite.org/src/timeline) contains the urtext. |
| 20 | |
| 21 | If you are reading this on GitHub or some other Git repository or service, |
| 22 | then you are looking at a mirror. The names of check-ins and |
| 23 | other artifacts in a Git mirror are different from the official |
| 24 | names for those objects. The official names for check-ins are |
| 25 | found in a footer on the check-in comment for authorized mirrors. |
| 26 | The official check-in name can also be seen in the `manifest.uuid` file |
| 27 | in the root of the tree. Always use the official name, not the |
| 28 | Git-name, when communicating about an SQLite check-in. |
| 29 | |
| 30 | If you pulled your SQLite source code from a secondary source and want to |
| 31 | verify its integrity, there are hints on how to do that in the |
| 32 | [Verifying Code Authenticity](#vauth) section below. |
| 33 | |
| 34 | ## Contacting The SQLite Developers |
| 35 | |
| 36 | The preferred way to ask questions or make comments about SQLite is to |
| 37 | visit the [SQLite Forum](https://sqlite.org/forum) at |
| 38 | <https://sqlite.org/forum/>. Anonymous postings are permitted. |
| 39 | Do <u>not</u> send bug reports to the main SQLite Forum. |
| 40 | To report bugs, post a message on the [SQLite bugs |
| 41 | forum](https://sqlite.org/bugs) at <https://sqlite.org/bugs>. |
| 42 | |
| 43 | If you think you have found a bug that has security implications, report |
| 44 | it to the SQLite bugs forum anyhow. If you really don't want to do that, |
| 45 | you can instead send a private email to drh at sqlite dot org. However, |
| 46 | unsolicited messages sent to that email address are liable to be |
| 47 | copy/pasted onto the SQLite bugs forum anyhow. |
| 48 | |
| 49 | ## Public Domain |
| 50 | |
| 51 | The SQLite source code is in the public domain. See |
| 52 | <https://sqlite.org/copyright.html> for details. |
| 53 | |
| 54 | Because SQLite is in the public domain, we do not normally accept pull |
| 55 | requests, because if we did take a pull request, the changes in that |
| 56 | pull request might carry a copyright and the SQLite source code would |
| 57 | then no longer be fully in the public domain. |
| 58 | |
| 59 | ## Obtaining The SQLite Source Code |
| 60 | |
| 61 | Source code tarballs or ZIP archives are available at: |
| 62 | |
| 63 | * [Latest trunk check-in](https://sqlite.org/src/rchvdwnld/trunk). |
| 64 | |
| 65 | * [Latest release](https://sqlite.org/src/rchvdwnld/release) |
| 66 | |
| 67 | * For other check-ins, browse the |
| 68 | [project timeline](https://sqlite.org/src/timeline?y=ci) and |
| 69 | click on the check-in hash of the check-in you want to download. |
| 70 | On the resulting "info" page, click one of the options to the |
| 71 | right of the "**Downloads:**" label in the "**Overview**" section |
| 72 | near the top. |
| 73 | |
| 74 | To access sources directly using [Fossil](https://fossil-scm.org/home), |
| 75 | first install Fossil version 2.0 or later. |
| 76 | Source tarballs and precompiled binaries for Fossil are available at |
| 77 | <https://fossil-scm.org/home/uv/download.html>. Fossil is |
| 78 | a stand-alone program. To install, simply download or build the single |
| 79 | executable file and put that file someplace on your $PATH or %PATH%. |
| 80 | Then run commands like this: |
| 81 | |
| 82 | mkdir -p ~/sqlite |
| 83 | cd ~/sqlite |
| 84 | fossil open https://sqlite.org/src |
| 85 | |
| 86 | The initial "fossil open" command will take two or three minutes. Afterwards, |
| 87 | you can do fast, bandwidth-efficient updates to whatever versions of SQLite you |
| 88 | like. Some examples: |
| 89 | |
| 90 | fossil update trunk ;# latest trunk check-in |
| 91 | fossil update release ;# latest official release |
| 92 | fossil update trunk:2024-01-01 ;# First trunk check-in after 2024-01-01 |
| 93 | fossil update version-3.39.0 ;# Version 3.39.0 |
| 94 | |
| 95 | Or type "fossil ui" to get a web-based user interface. |
| 96 | |
| 97 | ## Compiling for Unix-like systems |
| 98 | |
| 99 | First create a directory in which to place |
| 100 | the build products. It is recommended, but not required, that the |
| 101 | build directory be separate from the source directory. Cd into the |
| 102 | build directory and then from the build directory run the configure |
| 103 | script found at the root of the source tree. Then run "make". |
| 104 | See the [compile-for-unix.md](doc/compile-for-unix.md) document for |
| 105 | more detail. |
| 106 | |
| 107 | For example: |
| 108 | |
| 109 | apt install gcc make tcl-dev ;# Install the necessary build tools |
| 110 | tar xzf sqlite.tar.gz ;# Unpack the source tree into "sqlite" |
| 111 | mkdir bld ;# Build happens in a sibling directory |
| 112 | cd bld ;# Change to the build directory |
| 113 | ../sqlite/configure ;# Run the configure script |
| 114 | make sqlite3 ;# The "sqlite3" command-line tool |
| 115 | make sqlite3.c ;# The "amalgamation" source file |
| 116 | make sqldiff ;# The "sqldiff" command-line tool |
| 117 | #### Targets below require tcl-dev #### |
| 118 | make tclextension-install ;# Install the SQLite TCL extension |
| 119 | make test ;# Run development tests |
| 120 | make releasetest ;# Run full release tests |
| 121 | |
| 122 | See the makefile for additional targets. For debugging builds, the |
| 123 | core developers typically run "configure" with options like this: |
| 124 | |
| 125 | ../sqlite/configure --all --debug CFLAGS='-O0 -g' |
| 126 | |
| 127 | For release builds, the core developers usually do: |
| 128 | |
| 129 | ../sqlite/configure --all |
| 130 | |
| 131 | Core deliverables (sqlite3.c, sqlite3) can be built without a TCL, but |
| 132 | many makefile targets require a "tclsh" TCL interpreter version 8.6 |
| 133 | or later. The "tclextension-install" target and the test targets that follow |
| 134 | all require TCL development libraries too. ("apt install tcl-dev"). It is |
| 135 | helpful, but is not required, to install the SQLite TCL extension (the |
| 136 | "tclextension-install" target) prior to running tests. The "releasetest" |
| 137 | target has additional requirements, such as "valgrind". |
| 138 | |
| 139 | On "make" command-lines, one can add "OPTIONS=..." to specify additional |
| 140 | compile-time options over and above those set by ./configure. For example, |
| 141 | to compile with the SQLITE_OMIT_DEPRECATED compile-time option, one could say: |
| 142 | |
| 143 | ./configure --all |
| 144 | make OPTIONS=-DSQLITE_OMIT_DEPRECATED sqlite3 |
| 145 | |
| 146 | The configure script uses [autosetup](https://msteveb.github.io/autosetup/). |
| 147 | If the configure script does not work out for you, there is a generic |
| 148 | makefile named "Makefile.linux-gcc" in the top directory of the source tree |
| 149 | that you can copy and edit to suit your needs. Comments on the generic |
| 150 | makefile show what changes are needed. |
| 151 | |
| 152 | ## Compiling for Windows Using MSVC |
| 153 | |
| 154 | On Windows, everything can be compiled with MSVC. |
| 155 | You will also need a working installation of TCL if you want to run tests, |
| 156 | though TCL is not required if you just want to build SQLite itself. |
| 157 | See the [compile-for-windows.md](doc/compile-for-windows.md) document for |
| 158 | additional information about how to install MSVC and TCL and configure your |
| 159 | build environment. |
| 160 | |
| 161 | If you want to run tests, you need to let SQLite know the location of your |
| 162 | TCL library, using a command like this: |
| 163 | |
| 164 | set TCLDIR=c:\Tcl |
| 165 | |
| 166 | SQLite itself does not contain any TCL code, but it does use TCL to run |
| 167 | tests. You may need to install TCL development libraries in order to |
| 168 | successfully complete some makefile targets. It is helpful, but is not |
| 169 | required, to install the SQLite TCL extension (the "tclextension-install" |
| 170 | target) prior to running tests. |
| 171 | |
| 172 | The source tree contains a "make.bat" file that allows the same "make" |
| 173 | commands of Unix to work on Windows. In the following, you can substitute |
| 174 | "nmake /f Makefile.msc" in place of "make", if you prefer to avoid this BAT |
| 175 | file: |
| 176 | |
| 177 | make sqlite3.exe |
| 178 | make sqlite3.c |
| 179 | make sqldiff.exe |
| 180 | #### Targets below require TCL development libraries #### |
| 181 | make tclextension-install |
| 182 | make test |
| 183 | make releasetest |
| 184 | |
| 185 | There are many other makefile targets. See comments in Makefile.msc for |
| 186 | details. |
| 187 | |
| 188 | As with the unix Makefile, the OPTIONS=... argument can be passed on the nmake |
| 189 | command-line to enable new compile-time options. For example: |
| 190 | |
| 191 | make OPTIONS=-DSQLITE_OMIT_DEPRECATED sqlite3.exe |
| 192 | |
| 193 | ## Source Tree Map |
| 194 | |
| 195 | * **src/** - This directory contains the primary source code for the |
| 196 | SQLite core. For historical reasons, C-code used for testing is |
| 197 | also found here. Source files intended for testing begin with "`test`". |
| 198 | The `tclsqlite3.c` and `tclsqlite3.h` files are the TCL interface |
| 199 | for SQLite and are also not part of the core. |
| 200 | |
| 201 | * **test/** - This directory and its subdirectories contains code used |
| 202 | for testing. Files that end in "`.test`" are TCL scripts that run |
| 203 | tests using an augmented TCL interpreter named "testfixture". Use |
| 204 | a command like "`make testfixture`" to build that |
| 205 | augmented TCL interpreter, then run individual tests using commands like |
| 206 | "`testfixture test/main.test`". This test/ subdirectory also contains |
| 207 | additional C code modules and scripts for other kinds of testing. |
| 208 | |
| 209 | * **tool/** - This directory contains programs and scripts used to |
| 210 | build some of the machine-generated code that goes into the SQLite |
| 211 | core, as well as to build and run tests and perform diagnostics. |
| 212 | The source code to [the Lemon parser generator](./doc/lemon.html) is |
| 213 | found here. There are also TCL scripts used to build and/or transform |
| 214 | source code files. For example, the tool/mksqlite3h.tcl script reads |
| 215 | the src/sqlite.h.in file and uses it as a template to construct |
| 216 | the deliverable "sqlite3.h" file that defines the SQLite interface. |
| 217 | |
| 218 | * **ext/** - Various extensions to SQLite are found under this |
| 219 | directory. For example, the FTS5 subsystem is in "ext/fts5/". |
| 220 | Some of these extensions (ex: FTS3/4, FTS5, RTREE) might get built |
| 221 | into the SQLite amalgamation, but not all of them. The |
| 222 | "ext/misc/" subdirectory contains an assortment of one-file extensions, |
| 223 | many of which are omitted from the SQLite core, but which are included |
| 224 | in the [SQLite CLI](https://sqlite.org/cli.html). |
| 225 | |
| 226 | * **doc/** - Some documentation files about SQLite internals are found |
| 227 | here. Note, however, that the primary documentation designed for |
| 228 | application developers and users of SQLite is in a completely separate |
| 229 | repository. Note also that the primary API documentation is derived |
| 230 | from specially constructed comments in the src/sqlite.h.in file. |
| 231 | |
| 232 | ### Generated Source Code Files |
| 233 | |
| 234 | Several of the C-language source files used by SQLite are generated from |
| 235 | other sources rather than being typed in manually by a programmer. This |
| 236 | section will summarize those automatically-generated files. To create all |
| 237 | of the automatically-generated files, simply run "make target_source". |
| 238 | The "target_source" make target will create a subdirectory "tsrc/" and |
| 239 | fill it with all the source files needed to build SQLite, both |
| 240 | manually-edited files and automatically-generated files. |
| 241 | |
| 242 | The SQLite interface is defined by the **sqlite3.h** header file, which is |
| 243 | generated from src/sqlite.h.in, ./manifest.uuid, and ./VERSION. The |
| 244 | [Tcl script](https://www.tcl.tk) at tool/mksqlite3h.tcl does the conversion. |
| 245 | The manifest.uuid file contains the SHA3 hash of the particular check-in |
| 246 | and is used to generate the SQLITE\_SOURCE\_ID macro. The VERSION file |
| 247 | contains the current SQLite version number. The sqlite3.h header is really |
| 248 | just a copy of src/sqlite.h.in with the source-id and version number inserted |
| 249 | at just the right spots. Note that comment text in the sqlite3.h file is |
| 250 | used to generate much of the SQLite API documentation. The Tcl scripts |
| 251 | used to generate that documentation are in a separate source repository. |
| 252 | |
| 253 | The SQL language parser is **parse.c** which is generated from a grammar in |
| 254 | the src/parse.y file. The conversion of "parse.y" into "parse.c" is done |
| 255 | by the [lemon](./doc/lemon.html) LALR(1) parser generator. The source code |
| 256 | for lemon is at tool/lemon.c. Lemon uses the tool/lempar.c file as a |
| 257 | template for generating its parser. |
| 258 | Lemon also generates the **parse.h** header file, at the same time it |
| 259 | generates parse.c. |
| 260 | |
| 261 | The **opcodes.h** header file contains macros that define the numbers |
| 262 | corresponding to opcodes in the "VDBE" virtual machine. The opcodes.h |
| 263 | file is generated by scanning the src/vdbe.c source file. The |
| 264 | Tcl script at ./mkopcodeh.tcl does this scan and generates opcodes.h. |
| 265 | A second Tcl script, ./mkopcodec.tcl, then scans opcodes.h to generate |
| 266 | the **opcodes.c** source file, which contains a reverse mapping from |
| 267 | opcode-number to opcode-name that is used for EXPLAIN output. |
| 268 | |
| 269 | The **keywordhash.h** header file contains the definition of a hash table |
| 270 | that maps SQL language keywords (ex: "CREATE", "SELECT", "INDEX", etc.) into |
| 271 | the numeric codes used by the parse.c parser. The keywordhash.h file is |
| 272 | generated by a C-language program at tool mkkeywordhash.c. |
| 273 | |
| 274 | The **pragma.h** header file contains various definitions used to parse |
| 275 | and implement the PRAGMA statements. The header is generated by a |
| 276 | script **tool/mkpragmatab.tcl**. If you want to add a new PRAGMA, edit |
| 277 | the **tool/mkpragmatab.tcl** file to insert the information needed by the |
| 278 | parser for your new PRAGMA, then run the script to regenerate the |
| 279 | **pragma.h** header file. |
| 280 | |
| 281 | ### The Amalgamation |
| 282 | |
| 283 | All of the individual C source code and header files (both manually-edited |
| 284 | and automatically-generated) can be combined into a single big source file |
| 285 | **sqlite3.c** called "the amalgamation". The amalgamation is the recommended |
| 286 | way of using SQLite in a larger application. Combining all individual |
| 287 | source code files into a single big source code file allows the C compiler |
| 288 | to perform more cross-procedure analysis and generate better code. SQLite |
| 289 | runs about 5% faster when compiled from the amalgamation versus when compiled |
| 290 | from individual source files. |
| 291 | |
| 292 | The amalgamation is generated from the tool/mksqlite3c.tcl Tcl script. |
| 293 | First, all of the individual source files must be gathered into the tsrc/ |
| 294 | subdirectory (using the equivalent of "make target_source") then the |
| 295 | tool/mksqlite3c.tcl script is run to copy them all together in just the |
| 296 | right order while resolving internal "#include" references. |
| 297 | |
| 298 | The amalgamation source file is more than 200K lines long. Some symbolic |
| 299 | debuggers (most notably MSVC) are unable to deal with files longer than 64K |
| 300 | lines. To work around this, a separate Tcl script, tool/split-sqlite3c.tcl, |
| 301 | can be run on the amalgamation to break it up into a single small C file |
| 302 | called **sqlite3-all.c** that does #include on about seven other files |
| 303 | named **sqlite3-1.c**, **sqlite3-2.c**, ..., **sqlite3-7.c**. In this way, |
| 304 | all of the source code is contained within a single translation unit so |
| 305 | that the compiler can do extra cross-procedure optimization, but no |
| 306 | individual source file exceeds 32K lines in length. |
| 307 | |
| 308 | ## How It All Fits Together |
| 309 | |
| 310 | SQLite is modular in design. |
| 311 | See the [architectural description](https://sqlite.org/arch.html) |
| 312 | for details. Other documents that are useful in |
| 313 | helping to understand how SQLite works include the |
| 314 | [file format](https://sqlite.org/fileformat2.html) description, |
| 315 | the [virtual machine](https://sqlite.org/opcode.html) that runs |
| 316 | prepared statements, the description of |
| 317 | [how transactions work](https://sqlite.org/atomiccommit.html), and |
| 318 | the [overview of the query planner](https://sqlite.org/optoverview.html). |
| 319 | |
| 320 | Decades of effort have gone into optimizing SQLite, both |
| 321 | for small size and high performance. And optimizations tend to result in |
| 322 | complex code. So there is a lot of complexity in the current SQLite |
| 323 | implementation. It will not be the easiest library in the world to hack. |
| 324 | |
| 325 | ### Key source code files |
| 326 | |
| 327 | * **sqlite.h.in** - This file defines the public interface to the SQLite |
| 328 | library. Readers will need to be familiar with this interface before |
| 329 | trying to understand how the library works internally. This file is |
| 330 | really a template that is transformed into the "sqlite3.h" deliverable |
| 331 | using a script invoked by the makefile. |
| 332 | |
| 333 | * **sqliteInt.h** - this header file defines many of the data objects |
| 334 | used internally by SQLite. In addition to "sqliteInt.h", some |
| 335 | subsystems inside of sQLite have their own header files. These internal |
| 336 | interfaces are not for use by applications. They can and do change |
| 337 | from one release of SQLite to the next. |
| 338 | |
| 339 | * **parse.y** - This file describes the LALR(1) grammar that SQLite uses |
| 340 | to parse SQL statements, and the actions that are taken at each step |
| 341 | in the parsing process. The file is processed by the |
| 342 | [Lemon Parser Generator](./doc/lemon.html) to produce the actual C code |
| 343 | used for parsing. |
| 344 | |
| 345 | * **vdbe.c** - This file implements the virtual machine that runs |
| 346 | prepared statements. There are various helper files whose names |
| 347 | begin with "vdbe". The VDBE has access to the vdbeInt.h header file |
| 348 | which defines internal data objects. The rest of SQLite interacts |
| 349 | with the VDBE through an interface defined by vdbe.h. |
| 350 | |
| 351 | * **where.c** - This file (together with its helper files named |
| 352 | by "where*.c") analyzes the WHERE clause and generates |
| 353 | virtual machine code to run queries efficiently. This file is |
| 354 | sometimes called the "query optimizer". It has its own private |
| 355 | header file, whereInt.h, that defines data objects used internally. |
| 356 | |
| 357 | * **btree.c** - This file contains the implementation of the B-Tree |
| 358 | storage engine used by SQLite. The interface to the rest of the system |
| 359 | is defined by "btree.h". The "btreeInt.h" header defines objects |
| 360 | used internally by btree.c and not published to the rest of the system. |
| 361 | |
| 362 | * **pager.c** - This file contains the "pager" implementation, the |
| 363 | module that implements transactions. The "pager.h" header file |
| 364 | defines the interface between pager.c and the rest of the system. |
| 365 | |
| 366 | * **os_unix.c** and **os_win.c** - These two files implement the interface |
| 367 | between SQLite and the underlying operating system using the run-time |
| 368 | pluggable VFS interface. |
| 369 | |
| 370 | * **shell.c.in** - This file is not part of the core SQLite library. This |
| 371 | is the file that, when linked against sqlite3.a, generates the |
| 372 | "sqlite3.exe" command-line shell. The "shell.c.in" file is transformed |
| 373 | into "shell.c" as part of the build process. |
| 374 | |
| 375 | * **tclsqlite.c** - This file implements the Tcl bindings for SQLite. It |
| 376 | is not part of the core SQLite library. But as most of the tests in this |
| 377 | repository are written in Tcl, the Tcl language bindings are important. |
| 378 | |
| 379 | * **test\*.c** - Files in the src/ folder that begin with "test" go into |
| 380 | building the "testfixture.exe" program. The testfixture.exe program is |
| 381 | an enhanced Tcl shell. The testfixture.exe program runs scripts in the |
| 382 | test/ folder to validate the core SQLite code. The testfixture program |
| 383 | (and some other test programs too) is built and run when you type |
| 384 | "make test". |
| 385 | |
| 386 | * **VERSION**, **manifest**, **manifest.tags**, and **manifest.uuid** - |
| 387 | These files define the current SQLite version number. The "VERSION" file |
| 388 | is human generated, but the "manifest", "manifest.tags", and |
| 389 | "manifest.uuid" files are automatically generated by the |
| 390 | [Fossil version control system](https://fossil-scm.org/). |
| 391 | |
| 392 | There are many other source files. Each has a succinct header comment that |
| 393 | describes its purpose and role within the larger system. |
| 394 | |
| 395 | <a name="vauth"></a> |
| 396 | ## Verifying Code Authenticity |
| 397 | |
| 398 | The `manifest` file at the root directory of the source tree |
| 399 | contains either a SHA3-256 hash or a SHA1 hash |
| 400 | for every source file in the repository. |
| 401 | The name of the version of the entire source tree is just the |
| 402 | SHA3-256 hash of the `manifest` file itself, possibly with the |
| 403 | last line of that file omitted if the last line begins with |
| 404 | "`# Remove this line`". |
| 405 | The `manifest.uuid` file should contain the SHA3-256 hash of the |
| 406 | `manifest` file. If all of the above hash comparisons are correct, then |
| 407 | you can be confident that your source tree is authentic and unadulterated. |
| 408 | Details on the format for the `manifest` files are available |
| 409 | [on the Fossil website](https://fossil-scm.org/home/doc/trunk/www/fileformat.wiki#manifest). |
| 410 | |
| 411 | The process of checking source code authenticity is automated by the |
| 412 | makefile: |
| 413 | |
| 414 | > make verify-source |
| 415 | |
| 416 | Using the makefile to verify source integrity is good for detecting |
| 417 | accidental changes to the source tree, but malicious changes could be |
| 418 | hidden by also modifying the makefiles. |
| 419 | |
| 420 | ## Contacts |
| 421 | |
| 422 | The main SQLite website is [https://sqlite.org/](https://sqlite.org/) |
| 423 | with geographically distributed backups at |
| 424 | [https://www2.sqlite.org/](https://www2.sqlite.org) and |
| 425 | [https://www3.sqlite.org/](https://www3.sqlite.org). |
| 426 | |
| 427 | Contact the SQLite developers through the |
| 428 | [SQLite Forum](https://sqlite.org/forum/). In an emergency, you |
| 429 | can send private email to the lead developer at drh at sqlite dot org. |