156e3767eSdrh<h1 align="center">SQLite Source Repository</h1> 256e3767eSdrh 328d46eedSdrhThis repository contains the complete source code for the 428d46eedSdrh[SQLite database engine](https://sqlite.org/). Some test scripts 528d46eedSdrhare also included. However, many other test scripts 656e3767eSdrhand most of the documentation are managed separately. 756e3767eSdrh 83756d59bSdrh## Version Control 96295ff1dSdrh 106295ff1dSdrhSQLite sources are managed using the 116295ff1dSdrh[Fossil](https://www.fossil-scm.org/), a distributed version control system 123756d59bSdrhthat was specifically designed and written to support SQLite development. 13faeeddf0SdrhThe [Fossil repository](https://sqlite.org/src/timeline) contains the urtext. 143756d59bSdrh 153756d59bSdrhIf you are reading this on GitHub or some other Git repository or service, 163756d59bSdrhthen you are looking at a mirror. The names of check-ins and 173756d59bSdrhother artifacts in a Git mirror are different from the official 18*bc33519dSlarrybrnames for those objects. The official names for check-ins are 193756d59bSdrhfound in a footer on the check-in comment for authorized mirrors. 203756d59bSdrhThe official check-in name can also be seen in the `manifest.uuid` file 213756d59bSdrhin the root of the tree. Always use the official name, not the 223756d59bSdrhGit-name, when communicating about an SQLite check-in. 233756d59bSdrh 243756d59bSdrhIf you pulled your SQLite source code from a secondary source and want to 253756d59bSdrhverify its integrity, there are hints on how to do that in the 263756d59bSdrh[Verifying Code Authenticity](#vauth) section below. 273756d59bSdrh 283756d59bSdrh## Obtaining The Code 293756d59bSdrh 306295ff1dSdrhIf you do not want to use Fossil, you can download tarballs or ZIP 314f46b58aSdrharchives or [SQLite archives](https://sqlite.org/cli.html#sqlar) as follows: 326295ff1dSdrh 33*bc33519dSlarrybr * Latest trunk check-in as 34d6ec7a6aSdrh [Tarball](https://www.sqlite.org/src/tarball/sqlite.tar.gz), 35d6ec7a6aSdrh [ZIP-archive](https://www.sqlite.org/src/zip/sqlite.zip), or 36d6ec7a6aSdrh [SQLite-archive](https://www.sqlite.org/src/sqlar/sqlite.sqlar). 376295ff1dSdrh 38d6ec7a6aSdrh * Latest release as 39d6ec7a6aSdrh [Tarball](https://www.sqlite.org/src/tarball/sqlite.tar.gz?r=release), 40d6ec7a6aSdrh [ZIP-archive](https://www.sqlite.org/src/zip/sqlite.zip?r=release), or 41d6ec7a6aSdrh [SQLite-archive](https://www.sqlite.org/src/sqlar/sqlite.sqlar?r=release). 426295ff1dSdrh 436295ff1dSdrh * For other check-ins, substitute an appropriate branch name or 4440f827a6Sdrh tag or hash prefix in place of "release" in the URLs of the previous 456295ff1dSdrh bullet. Or browse the [timeline](https://www.sqlite.org/src/timeline) 466295ff1dSdrh to locate the check-in desired, click on its information page link, 476295ff1dSdrh then click on the "Tarball" or "ZIP Archive" links on the information 486295ff1dSdrh page. 496295ff1dSdrh 506295ff1dSdrhIf you do want to use Fossil to check out the source tree, 516295ff1dSdrhfirst install Fossil version 2.0 or later. 526295ff1dSdrh(Source tarballs and precompiled binaries available 537ff64dc2Sdrh[here](https://www.fossil-scm.org/fossil/uv/download.html). Fossil is 547ff64dc2Sdrha stand-alone program. To install, simply download or build the single 557ff64dc2Sdrhexecutable file and put that file someplace on your $PATH.) 566295ff1dSdrhThen run commands like this: 576295ff1dSdrh 587ac08e8bSdrh mkdir -p ~/sqlite ~/Fossils 596295ff1dSdrh cd ~/sqlite 607ac08e8bSdrh fossil clone https://www.sqlite.org/src ~/Fossils/sqlite.fossil 617ac08e8bSdrh fossil open ~/Fossils/sqlite.fossil 626295ff1dSdrh 636295ff1dSdrhAfter setting up a repository using the steps above, you can always 64*bc33519dSlarrybrupdate to the latest version using: 656295ff1dSdrh 666295ff1dSdrh fossil update trunk ;# latest trunk check-in 676295ff1dSdrh fossil update release ;# latest official release 686295ff1dSdrh 696295ff1dSdrhOr type "fossil ui" to get a web-based user interface. 706295ff1dSdrh 71e427fe52Slarrybr## Compiling for Unix-like systems 7256e3767eSdrh 7356e3767eSdrhFirst create a directory in which to place 7456e3767eSdrhthe build products. It is recommended, but not required, that the 7556e3767eSdrhbuild directory be separate from the source directory. Cd into the 7656e3767eSdrhbuild directory and then from the build directory run the configure 7756e3767eSdrhscript found at the root of the source tree. Then run "make". 7856e3767eSdrh 7956e3767eSdrhFor example: 8056e3767eSdrh 8156e3767eSdrh tar xzf sqlite.tar.gz ;# Unpack the source tree into "sqlite" 8256e3767eSdrh mkdir bld ;# Build will occur in a sibling directory 8356e3767eSdrh cd bld ;# Change to the build directory 8456e3767eSdrh ../sqlite/configure ;# Run the configure script 8556e3767eSdrh make ;# Run the makefile. 8656e3767eSdrh make sqlite3.c ;# Build the "amalgamation" source file 873f40d40aSmistachkin make test ;# Run some tests (requires Tcl) 8856e3767eSdrh 8956e3767eSdrhSee the makefile for additional targets. 9056e3767eSdrh 9156e3767eSdrhThe configure script uses autoconf 2.61 and libtool. If the configure 9256e3767eSdrhscript does not work out for you, there is a generic makefile named 9356e3767eSdrh"Makefile.linux-gcc" in the top directory of the source tree that you 9456e3767eSdrhcan copy and edit to suit your needs. Comments on the generic makefile 9556e3767eSdrhshow what changes are needed. 9656e3767eSdrh 97e427fe52Slarrybr## Using MSVC for Windows systems 983f40d40aSmistachkin 993f40d40aSmistachkinOn Windows, all applicable build products can be compiled with MSVC. 1003f40d40aSmistachkinFirst open the command prompt window associated with the desired compiler 1013f40d40aSmistachkinversion (e.g. "Developer Command Prompt for VS2013"). Next, use NMAKE 1023f40d40aSmistachkinwith the provided "Makefile.msc" to build one of the supported targets. 1033f40d40aSmistachkin 104e427fe52SlarrybrFor example, from the parent directory of the source subtree named "sqlite": 1053f40d40aSmistachkin 1063f40d40aSmistachkin mkdir bld 1073f40d40aSmistachkin cd bld 108e427fe52Slarrybr nmake /f ..\sqlite\Makefile.msc TOP=..\sqlite 109e427fe52Slarrybr nmake /f ..\sqlite\Makefile.msc sqlite3.c TOP=..\sqlite 110e427fe52Slarrybr nmake /f ..\sqlite\Makefile.msc sqlite3.dll TOP=..\sqlite 111e427fe52Slarrybr nmake /f ..\sqlite\Makefile.msc sqlite3.exe TOP=..\sqlite 112e427fe52Slarrybr nmake /f ..\sqlite\Makefile.msc test TOP=..\sqlite 1133f40d40aSmistachkin 1143f40d40aSmistachkinThere are several build options that can be set via the NMAKE command 1153f40d40aSmistachkinline. For example, to build for WinRT, simply add "FOR_WINRT=1" argument 1163f40d40aSmistachkinto the "sqlite3.dll" command line above. When debugging into the SQLite 1173f40d40aSmistachkincode, adding the "DEBUG=1" argument to one of the above command lines is 1183f40d40aSmistachkinrecommended. 1193f40d40aSmistachkin 120dd31b4e7SdrhSQLite does not require [Tcl](http://www.tcl.tk/) to run, but a Tcl installation 121dd31b4e7Sdrhis required by the makefiles (including those for MSVC). SQLite contains 122dd31b4e7Sdrha lot of generated code and Tcl is used to do much of that code generation. 12356e3767eSdrh 12456e3767eSdrh## Source Code Tour 12556e3767eSdrh 1267ff64dc2SdrhMost of the core source files are in the **src/** subdirectory. The 1277ff64dc2Sdrh**src/** folder also contains files used to build the "testfixture" test 12867d5bf91Sdrhharness. The names of the source files used by "testfixture" all begin 1297ff64dc2Sdrhwith "test". 1307ff64dc2SdrhThe **src/** also contains the "shell.c" file 1317ff64dc2Sdrhwhich is the main program for the "sqlite3.exe" 1327ff64dc2Sdrh[command-line shell](https://sqlite.org/cli.html) and 1337ff64dc2Sdrhthe "tclsqlite.c" file which implements the 1342034353dSmistachkin[Tcl bindings](https://sqlite.org/tclsqlite.html) for SQLite. 1357ff64dc2Sdrh(Historical note: SQLite began as a Tcl 13656e3767eSdrhextension and only later escaped to the wild as an independent library.) 13756e3767eSdrh 13856e3767eSdrhTest scripts and programs are found in the **test/** subdirectory. 139*bc33519dSlarrybrAdditional test code is found in other source repositories. 1407ff64dc2SdrhSee [How SQLite Is Tested](http://www.sqlite.org/testing.html) for 1417ff64dc2Sdrhadditional information. 14256e3767eSdrh 14356e3767eSdrhThe **ext/** subdirectory contains code for extensions. The 14456e3767eSdrhFull-text search engine is in **ext/fts3**. The R-Tree engine is in 14556e3767eSdrh**ext/rtree**. The **ext/misc** subdirectory contains a number of 14656e3767eSdrhsmaller, single-file extensions, such as a REGEXP operator. 14756e3767eSdrh 14856e3767eSdrhThe **tool/** subdirectory contains various scripts and programs used 14956e3767eSdrhfor building generated source code files or for testing or for generating 15056e3767eSdrhaccessory programs such as "sqlite3_analyzer(.exe)". 15156e3767eSdrh 15256e3767eSdrh### Generated Source Code Files 15356e3767eSdrh 15456e3767eSdrhSeveral of the C-language source files used by SQLite are generated from 15556e3767eSdrhother sources rather than being typed in manually by a programmer. This 15656e3767eSdrhsection will summarize those automatically-generated files. To create all 15756e3767eSdrhof the automatically-generated files, simply run "make target_source". 15856e3767eSdrhThe "target_source" make target will create a subdirectory "tsrc/" and 15956e3767eSdrhfill it with all the source files needed to build SQLite, both 16056e3767eSdrhmanually-edited files and automatically-generated files. 16156e3767eSdrh 16256e3767eSdrhThe SQLite interface is defined by the **sqlite3.h** header file, which is 16356e3767eSdrhgenerated from src/sqlite.h.in, ./manifest.uuid, and ./VERSION. The 164dd31b4e7Sdrh[Tcl script](http://www.tcl.tk) at tool/mksqlite3h.tcl does the conversion. 1657ff64dc2SdrhThe manifest.uuid file contains the SHA3 hash of the particular check-in 166dd31b4e7Sdrhand is used to generate the SQLITE\_SOURCE\_ID macro. The VERSION file 167dd31b4e7Sdrhcontains the current SQLite version number. The sqlite3.h header is really 168dd31b4e7Sdrhjust a copy of src/sqlite.h.in with the source-id and version number inserted 169dd31b4e7Sdrhat just the right spots. Note that comment text in the sqlite3.h file is 170dd31b4e7Sdrhused to generate much of the SQLite API documentation. The Tcl scripts 171dd31b4e7Sdrhused to generate that documentation are in a separate source repository. 17256e3767eSdrh 173*bc33519dSlarrybrThe SQL language parser is **parse.c** which is generated from a grammar in 17456e3767eSdrhthe src/parse.y file. The conversion of "parse.y" into "parse.c" is done 17556e3767eSdrhby the [lemon](./doc/lemon.html) LALR(1) parser generator. The source code 17614d5ff42Sdrhfor lemon is at tool/lemon.c. Lemon uses the tool/lempar.c file as a 17714d5ff42Sdrhtemplate for generating its parser. 17856e3767eSdrhLemon also generates the **parse.h** header file, at the same time it 1797bbdc3c5Sdrhgenerates parse.c. 18056e3767eSdrh 18156e3767eSdrhThe **opcodes.h** header file contains macros that define the numbers 18256e3767eSdrhcorresponding to opcodes in the "VDBE" virtual machine. The opcodes.h 183*bc33519dSlarrybrfile is generated by scanning the src/vdbe.c source file. The 1842034353dSmistachkinTcl script at ./mkopcodeh.tcl does this scan and generates opcodes.h. 1852034353dSmistachkinA second Tcl script, ./mkopcodec.tcl, then scans opcodes.h to generate 18656e3767eSdrhthe **opcodes.c** source file, which contains a reverse mapping from 18756e3767eSdrhopcode-number to opcode-name that is used for EXPLAIN output. 18856e3767eSdrh 18956e3767eSdrhThe **keywordhash.h** header file contains the definition of a hash table 19056e3767eSdrhthat maps SQL language keywords (ex: "CREATE", "SELECT", "INDEX", etc.) into 19156e3767eSdrhthe numeric codes used by the parse.c parser. The keywordhash.h file is 19256e3767eSdrhgenerated by a C-language program at tool mkkeywordhash.c. 19356e3767eSdrh 194764fb303SdrhThe **pragma.h** header file contains various definitions used to parse 1957ff64dc2Sdrhand implement the PRAGMA statements. The header is generated by a 1967ff64dc2Sdrhscript **tool/mkpragmatab.tcl**. If you want to add a new PRAGMA, edit 1977ff64dc2Sdrhthe **tool/mkpragmatab.tcl** file to insert the information needed by the 1987ff64dc2Sdrhparser for your new PRAGMA, then run the script to regenerate the 1997ff64dc2Sdrh**pragma.h** header file. 2007ff64dc2Sdrh 20156e3767eSdrh### The Amalgamation 20256e3767eSdrh 20356e3767eSdrhAll of the individual C source code and header files (both manually-edited 20456e3767eSdrhand automatically-generated) can be combined into a single big source file 20556e3767eSdrh**sqlite3.c** called "the amalgamation". The amalgamation is the recommended 20656e3767eSdrhway of using SQLite in a larger application. Combining all individual 20756e3767eSdrhsource code files into a single big source code file allows the C compiler 20856e3767eSdrhto perform more cross-procedure analysis and generate better code. SQLite 20956e3767eSdrhruns about 5% faster when compiled from the amalgamation versus when compiled 21056e3767eSdrhfrom individual source files. 21156e3767eSdrh 2123f40d40aSmistachkinThe amalgamation is generated from the tool/mksqlite3c.tcl Tcl script. 21356e3767eSdrhFirst, all of the individual source files must be gathered into the tsrc/ 21456e3767eSdrhsubdirectory (using the equivalent of "make target_source") then the 21556e3767eSdrhtool/mksqlite3c.tcl script is run to copy them all together in just the 21656e3767eSdrhright order while resolving internal "#include" references. 21756e3767eSdrh 2187ff64dc2SdrhThe amalgamation source file is more than 200K lines long. Some symbolic 21956e3767eSdrhdebuggers (most notably MSVC) are unable to deal with files longer than 64K 2203f40d40aSmistachkinlines. To work around this, a separate Tcl script, tool/split-sqlite3c.tcl, 22156e3767eSdrhcan be run on the amalgamation to break it up into a single small C file 222b5008252Sdrhcalled **sqlite3-all.c** that does #include on about seven other files 223b5008252Sdrhnamed **sqlite3-1.c**, **sqlite3-2.c**, ..., **sqlite3-7.c**. In this way, 22456e3767eSdrhall of the source code is contained within a single translation unit so 22556e3767eSdrhthat the compiler can do extra cross-procedure optimization, but no 22656e3767eSdrhindividual source file exceeds 32K lines in length. 22756e3767eSdrh 22856e3767eSdrh## How It All Fits Together 22956e3767eSdrh 23056e3767eSdrhSQLite is modular in design. 23156e3767eSdrhSee the [architectural description](http://www.sqlite.org/arch.html) 23256e3767eSdrhfor details. Other documents that are useful in 23356e3767eSdrh(helping to understand how SQLite works include the 23456e3767eSdrh[file format](http://www.sqlite.org/fileformat2.html) description, 2357ff64dc2Sdrhthe [virtual machine](http://www.sqlite.org/opcode.html) that runs 23656e3767eSdrhprepared statements, the description of 23756e3767eSdrh[how transactions work](http://www.sqlite.org/atomiccommit.html), and 23856e3767eSdrhthe [overview of the query planner](http://www.sqlite.org/optoverview.html). 23956e3767eSdrh 240*bc33519dSlarrybrYears of effort have gone into optimizing SQLite, both 24156e3767eSdrhfor small size and high performance. And optimizations tend to result in 2427ff64dc2Sdrhcomplex code. So there is a lot of complexity in the current SQLite 2437ff64dc2Sdrhimplementation. It will not be the easiest library in the world to hack. 24456e3767eSdrh 24556e3767eSdrhKey files: 24656e3767eSdrh 2475fea9858Sdrh * **sqlite.h.in** - This file defines the public interface to the SQLite 24856e3767eSdrh library. Readers will need to be familiar with this interface before 24956e3767eSdrh trying to understand how the library works internally. 25056e3767eSdrh 25156e3767eSdrh * **sqliteInt.h** - this header file defines many of the data objects 252b5008252Sdrh used internally by SQLite. In addition to "sqliteInt.h", some 253b5008252Sdrh subsystems have their own header files. 25456e3767eSdrh 255a8686396Sdrh * **parse.y** - This file describes the LALR(1) grammar that SQLite uses 2565fea9858Sdrh to parse SQL statements, and the actions that are taken at each step 25756e3767eSdrh in the parsing process. 25856e3767eSdrh 25956e3767eSdrh * **vdbe.c** - This file implements the virtual machine that runs 26056e3767eSdrh prepared statements. There are various helper files whose names 26156e3767eSdrh begin with "vdbe". The VDBE has access to the vdbeInt.h header file 26256e3767eSdrh which defines internal data objects. The rest of SQLite interacts 26356e3767eSdrh with the VDBE through an interface defined by vdbe.h. 26456e3767eSdrh 265b5008252Sdrh * **where.c** - This file (together with its helper files named 266b5008252Sdrh by "where*.c") analyzes the WHERE clause and generates 26756e3767eSdrh virtual machine code to run queries efficiently. This file is 26856e3767eSdrh sometimes called the "query optimizer". It has its own private 26956e3767eSdrh header file, whereInt.h, that defines data objects used internally. 27056e3767eSdrh 27156e3767eSdrh * **btree.c** - This file contains the implementation of the B-Tree 272b5008252Sdrh storage engine used by SQLite. The interface to the rest of the system 273b5008252Sdrh is defined by "btree.h". The "btreeInt.h" header defines objects 274b5008252Sdrh used internally by btree.c and not published to the rest of the system. 27556e3767eSdrh 27656e3767eSdrh * **pager.c** - This file contains the "pager" implementation, the 277b5008252Sdrh module that implements transactions. The "pager.h" header file 278b5008252Sdrh defines the interface between pager.c and the rest of the system. 27956e3767eSdrh 28056e3767eSdrh * **os_unix.c** and **os_win.c** - These two files implement the interface 28156e3767eSdrh between SQLite and the underlying operating system using the run-time 28256e3767eSdrh pluggable VFS interface. 28356e3767eSdrh 284b5008252Sdrh * **shell.c.in** - This file is not part of the core SQLite library. This 2855fea9858Sdrh is the file that, when linked against sqlite3.a, generates the 286b5008252Sdrh "sqlite3.exe" command-line shell. The "shell.c.in" file is transformed 287b5008252Sdrh into "shell.c" as part of the build process. 2885fea9858Sdrh 2895fea9858Sdrh * **tclsqlite.c** - This file implements the Tcl bindings for SQLite. It 2905fea9858Sdrh is not part of the core SQLite library. But as most of the tests in this 2915fea9858Sdrh repository are written in Tcl, the Tcl language bindings are important. 2925fea9858Sdrh 293b5008252Sdrh * **test*.c** - Files in the src/ folder that begin with "test" go into 294b5008252Sdrh building the "testfixture.exe" program. The testfixture.exe program is 2952034353dSmistachkin an enhanced Tcl shell. The testfixture.exe program runs scripts in the 296b5008252Sdrh test/ folder to validate the core SQLite code. The testfixture program 297*bc33519dSlarrybr (and some other test programs too) is built and run when you type 298b5008252Sdrh "make test". 299b5008252Sdrh 300b5008252Sdrh * **ext/misc/json1.c** - This file implements the various JSON functions 301*bc33519dSlarrybr that are built into SQLite. 302b5008252Sdrh 303e1869a91SdrhThere are many other source files. Each has a succinct header comment that 3045fea9858Sdrhdescribes its purpose and role within the larger system. 3055fea9858Sdrh 3063756d59bSdrh<a name="vauth"></a> 3073756d59bSdrh## Verifying Code Authenticity 3083756d59bSdrh 30958eaf763SdrhThe `manifest` file at the root directory of the source tree 3103756d59bSdrhcontains either a SHA3-256 hash (for newer files) or a SHA1 hash (for 31158eaf763Sdrholder files) for every source file in the repository. 3128b6dfc47SdrhThe name of the version of the entire source tree is just the 3138b6dfc47SdrhSHA3-256 hash of the `manifest` file itself, possibly with the 3148b6dfc47Sdrhlast line of that file omitted if the last line begins with 3158b6dfc47Sdrh"`# Remove this line`". 3168b6dfc47SdrhThe `manifest.uuid` file should contain the SHA3-256 hash of the 3173756d59bSdrh`manifest` file. If all of the above hash comparisons are correct, then 3183756d59bSdrhyou can be confident that your source tree is authentic and unadulterated. 31956e3767eSdrh 32040f827a6SdrhThe format of the `manifest` file should be mostly self-explanatory, but 32140f827a6Sdrhif you want details, they are available 32240f827a6Sdrh[here](https://fossil-scm.org/fossil/doc/trunk/www/fileformat.wiki#manifest). 32340f827a6Sdrh 32456e3767eSdrh## Contacts 32556e3767eSdrh 32640f827a6SdrhThe main SQLite website is [http://www.sqlite.org/](http://www.sqlite.org/) 3277ff64dc2Sdrhwith geographically distributed backups at 32856e3767eSdrh[http://www2.sqlite.org/](http://www2.sqlite.org) and 32956e3767eSdrh[http://www3.sqlite.org/](http://www3.sqlite.org). 330