Strengthen the sqlite3VdbeMemAboutToChange() run-time verification mechanism
[sqlite.git] / README.md
blob8c8cc95c74c048a6d0d89209669dbcdb2381b849
1 <h1 align="center">SQLite Source Repository</h1>
3 This repository contains the complete source code for the SQLite database
4 engine.  Some test scripts are also included.  However, many other test scripts
5 and most of the documentation are managed separately.
7 SQLite [does not use Git](https://sqlite.org/whynotgit.html).
8 If you are reading this on GitHub, then you are looking at an
9 unofficial mirror. See <https://sqlite.org/src> for the official
10 repository.
12 ## Obtaining The Code
14 SQLite sources are managed using the
15 [Fossil](https://www.fossil-scm.org/), a distributed version control system
16 that was specifically designed to support SQLite development.
17 If you do not want to use Fossil, you can download tarballs or ZIP
18 archives or [SQLite archives](https://sqlite.org/cli.html#sqlar) as follows:
20   *  Lastest trunk check-in as
21      [Tarball](https://www.sqlite.org/src/tarball/sqlite.tar.gz),
22      [ZIP-archive](https://www.sqlite.org/src/zip/sqlite.zip), or
23      [SQLite-archive](https://www.sqlite.org/src/sqlar/sqlite.sqlar).
25   *  Latest release as
26      [Tarball](https://www.sqlite.org/src/tarball/sqlite.tar.gz?r=release),
27      [ZIP-archive](https://www.sqlite.org/src/zip/sqlite.zip?r=release), or
28      [SQLite-archive](https://www.sqlite.org/src/sqlar/sqlite.sqlar?r=release).
30   *  For other check-ins, substitute an appropriate branch name or
31      tag or hash prefix for "release" in the URLs of the previous
32      bullet.  Or browse the [timeline](https://www.sqlite.org/src/timeline)
33      to locate the check-in desired, click on its information page link,
34      then click on the "Tarball" or "ZIP Archive" links on the information
35      page.
37 If you do want to use Fossil to check out the source tree, 
38 first install Fossil version 2.0 or later.
39 (Source tarballs and precompiled binaries available
40 [here](https://www.fossil-scm.org/fossil/uv/download.html).  Fossil is
41 a stand-alone program.  To install, simply download or build the single 
42 executable file and put that file someplace on your $PATH.)
43 Then run commands like this:
45         mkdir ~/sqlite
46         cd ~/sqlite
47         fossil clone https://www.sqlite.org/src sqlite.fossil
48         fossil open sqlite.fossil
49     
50 After setting up a repository using the steps above, you can always
51 update to the lastest version using:
53         fossil update trunk   ;# latest trunk check-in
54         fossil update release ;# latest official release
56 Or type "fossil ui" to get a web-based user interface.
58 ## Compiling
60 First create a directory in which to place
61 the build products.  It is recommended, but not required, that the
62 build directory be separate from the source directory.  Cd into the
63 build directory and then from the build directory run the configure
64 script found at the root of the source tree.  Then run "make".
66 For example:
68         tar xzf sqlite.tar.gz    ;#  Unpack the source tree into "sqlite"
69         mkdir bld                ;#  Build will occur in a sibling directory
70         cd bld                   ;#  Change to the build directory
71         ../sqlite/configure      ;#  Run the configure script
72         make                     ;#  Run the makefile.
73         make sqlite3.c           ;#  Build the "amalgamation" source file
74         make test                ;#  Run some tests (requires Tcl)
76 See the makefile for additional targets.
78 The configure script uses autoconf 2.61 and libtool.  If the configure
79 script does not work out for you, there is a generic makefile named
80 "Makefile.linux-gcc" in the top directory of the source tree that you
81 can copy and edit to suit your needs.  Comments on the generic makefile
82 show what changes are needed.
84 ## Using MSVC
86 On Windows, all applicable build products can be compiled with MSVC.
87 First open the command prompt window associated with the desired compiler
88 version (e.g. "Developer Command Prompt for VS2013").  Next, use NMAKE
89 with the provided "Makefile.msc" to build one of the supported targets.
91 For example:
93         mkdir bld
94         cd bld
95         nmake /f Makefile.msc TOP=..\sqlite
96         nmake /f Makefile.msc sqlite3.c TOP=..\sqlite
97         nmake /f Makefile.msc sqlite3.dll TOP=..\sqlite
98         nmake /f Makefile.msc sqlite3.exe TOP=..\sqlite
99         nmake /f Makefile.msc test TOP=..\sqlite
101 There are several build options that can be set via the NMAKE command
102 line.  For example, to build for WinRT, simply add "FOR_WINRT=1" argument
103 to the "sqlite3.dll" command line above.  When debugging into the SQLite
104 code, adding the "DEBUG=1" argument to one of the above command lines is
105 recommended.
107 SQLite does not require [Tcl](http://www.tcl.tk/) to run, but a Tcl installation
108 is required by the makefiles (including those for MSVC).  SQLite contains
109 a lot of generated code and Tcl is used to do much of that code generation.
111 ## Source Code Tour
113 Most of the core source files are in the **src/** subdirectory.  The
114 **src/** folder also contains files used to build the "testfixture" test
115 harness. The names of the source files used by "testfixture" all begin
116 with "test".
117 The **src/** also contains the "shell.c" file
118 which is the main program for the "sqlite3.exe"
119 [command-line shell](https://sqlite.org/cli.html) and
120 the "tclsqlite.c" file which implements the
121 [Tcl bindings](https://sqlite.org/tclsqlite.html) for SQLite.
122 (Historical note:  SQLite began as a Tcl
123 extension and only later escaped to the wild as an independent library.)
125 Test scripts and programs are found in the **test/** subdirectory.
126 Addtional test code is found in other source repositories.
127 See [How SQLite Is Tested](http://www.sqlite.org/testing.html) for
128 additional information.
130 The **ext/** subdirectory contains code for extensions.  The
131 Full-text search engine is in **ext/fts3**.  The R-Tree engine is in
132 **ext/rtree**.  The **ext/misc** subdirectory contains a number of
133 smaller, single-file extensions, such as a REGEXP operator.
135 The **tool/** subdirectory contains various scripts and programs used
136 for building generated source code files or for testing or for generating
137 accessory programs such as "sqlite3_analyzer(.exe)".
139 ### Generated Source Code Files
141 Several of the C-language source files used by SQLite are generated from
142 other sources rather than being typed in manually by a programmer.  This
143 section will summarize those automatically-generated files.  To create all
144 of the automatically-generated files, simply run "make target&#95;source".
145 The "target&#95;source" make target will create a subdirectory "tsrc/" and
146 fill it with all the source files needed to build SQLite, both
147 manually-edited files and automatically-generated files.
149 The SQLite interface is defined by the **sqlite3.h** header file, which is
150 generated from src/sqlite.h.in, ./manifest.uuid, and ./VERSION.  The
151 [Tcl script](http://www.tcl.tk) at tool/mksqlite3h.tcl does the conversion.
152 The manifest.uuid file contains the SHA3 hash of the particular check-in
153 and is used to generate the SQLITE\_SOURCE\_ID macro.  The VERSION file
154 contains the current SQLite version number.  The sqlite3.h header is really
155 just a copy of src/sqlite.h.in with the source-id and version number inserted
156 at just the right spots. Note that comment text in the sqlite3.h file is
157 used to generate much of the SQLite API documentation.  The Tcl scripts
158 used to generate that documentation are in a separate source repository.
160 The SQL language parser is **parse.c** which is generate from a grammar in
161 the src/parse.y file.  The conversion of "parse.y" into "parse.c" is done
162 by the [lemon](./doc/lemon.html) LALR(1) parser generator.  The source code
163 for lemon is at tool/lemon.c.  Lemon uses the tool/lempar.c file as a
164 template for generating its parser.
166 Lemon also generates the **parse.h** header file, at the same time it
167 generates parse.c. But the parse.h header file is
168 modified further (to add additional symbols) using the ./addopcodes.tcl
169 Tcl script.
171 The **opcodes.h** header file contains macros that define the numbers
172 corresponding to opcodes in the "VDBE" virtual machine.  The opcodes.h
173 file is generated by the scanning the src/vdbe.c source file.  The
174 Tcl script at ./mkopcodeh.tcl does this scan and generates opcodes.h.
175 A second Tcl script, ./mkopcodec.tcl, then scans opcodes.h to generate
176 the **opcodes.c** source file, which contains a reverse mapping from
177 opcode-number to opcode-name that is used for EXPLAIN output.
179 The **keywordhash.h** header file contains the definition of a hash table
180 that maps SQL language keywords (ex: "CREATE", "SELECT", "INDEX", etc.) into
181 the numeric codes used by the parse.c parser.  The keywordhash.h file is
182 generated by a C-language program at tool mkkeywordhash.c.
184 The **pragma.h** header file contains various definitions used to parse
185 and implement the PRAGMA statements.  The header is generated by a
186 script **tool/mkpragmatab.tcl**. If you want to add a new PRAGMA, edit
187 the **tool/mkpragmatab.tcl** file to insert the information needed by the
188 parser for your new PRAGMA, then run the script to regenerate the
189 **pragma.h** header file.
191 ### The Amalgamation
193 All of the individual C source code and header files (both manually-edited
194 and automatically-generated) can be combined into a single big source file
195 **sqlite3.c** called "the amalgamation".  The amalgamation is the recommended
196 way of using SQLite in a larger application.  Combining all individual
197 source code files into a single big source code file allows the C compiler
198 to perform more cross-procedure analysis and generate better code.  SQLite
199 runs about 5% faster when compiled from the amalgamation versus when compiled
200 from individual source files.
202 The amalgamation is generated from the tool/mksqlite3c.tcl Tcl script.
203 First, all of the individual source files must be gathered into the tsrc/
204 subdirectory (using the equivalent of "make target_source") then the
205 tool/mksqlite3c.tcl script is run to copy them all together in just the
206 right order while resolving internal "#include" references.
208 The amalgamation source file is more than 200K lines long.  Some symbolic
209 debuggers (most notably MSVC) are unable to deal with files longer than 64K
210 lines.  To work around this, a separate Tcl script, tool/split-sqlite3c.tcl,
211 can be run on the amalgamation to break it up into a single small C file
212 called **sqlite3-all.c** that does #include on about seven other files
213 named **sqlite3-1.c**, **sqlite3-2.c**, ..., **sqlite3-7.c**.  In this way,
214 all of the source code is contained within a single translation unit so
215 that the compiler can do extra cross-procedure optimization, but no
216 individual source file exceeds 32K lines in length.
218 ## How It All Fits Together
220 SQLite is modular in design.
221 See the [architectural description](http://www.sqlite.org/arch.html)
222 for details. Other documents that are useful in
223 (helping to understand how SQLite works include the
224 [file format](http://www.sqlite.org/fileformat2.html) description,
225 the [virtual machine](http://www.sqlite.org/opcode.html) that runs
226 prepared statements, the description of
227 [how transactions work](http://www.sqlite.org/atomiccommit.html), and
228 the [overview of the query planner](http://www.sqlite.org/optoverview.html).
230 Years of effort have gone into optimizating SQLite, both
231 for small size and high performance.  And optimizations tend to result in
232 complex code.  So there is a lot of complexity in the current SQLite
233 implementation.  It will not be the easiest library in the world to hack.
235 Key files:
237   *  **sqlite.h.in** - This file defines the public interface to the SQLite
238      library.  Readers will need to be familiar with this interface before
239      trying to understand how the library works internally.
241   *  **sqliteInt.h** - this header file defines many of the data objects
242      used internally by SQLite.  In addition to "sqliteInt.h", some
243      subsystems have their own header files.
245   *  **parse.y** - This file describes the LALR(1) grammar that SQLite uses
246      to parse SQL statements, and the actions that are taken at each step
247      in the parsing process.
249   *  **vdbe.c** - This file implements the virtual machine that runs
250      prepared statements.  There are various helper files whose names
251      begin with "vdbe".  The VDBE has access to the vdbeInt.h header file
252      which defines internal data objects.  The rest of SQLite interacts
253      with the VDBE through an interface defined by vdbe.h.
255   *  **where.c** - This file (together with its helper files named
256      by "where*.c") analyzes the WHERE clause and generates
257      virtual machine code to run queries efficiently.  This file is
258      sometimes called the "query optimizer".  It has its own private
259      header file, whereInt.h, that defines data objects used internally.
261   *  **btree.c** - This file contains the implementation of the B-Tree
262      storage engine used by SQLite.  The interface to the rest of the system
263      is defined by "btree.h".  The "btreeInt.h" header defines objects
264      used internally by btree.c and not published to the rest of the system.
266   *  **pager.c** - This file contains the "pager" implementation, the
267      module that implements transactions.  The "pager.h" header file
268      defines the interface between pager.c and the rest of the system.
270   *  **os_unix.c** and **os_win.c** - These two files implement the interface
271      between SQLite and the underlying operating system using the run-time
272      pluggable VFS interface.
274   *  **shell.c.in** - This file is not part of the core SQLite library.  This
275      is the file that, when linked against sqlite3.a, generates the
276      "sqlite3.exe" command-line shell.  The "shell.c.in" file is transformed
277      into "shell.c" as part of the build process.
279   *  **tclsqlite.c** - This file implements the Tcl bindings for SQLite.  It
280      is not part of the core SQLite library.  But as most of the tests in this
281      repository are written in Tcl, the Tcl language bindings are important.
283   *  **test*.c** - Files in the src/ folder that begin with "test" go into
284      building the "testfixture.exe" program.  The testfixture.exe program is
285      an enhanced Tcl shell.  The testfixture.exe program runs scripts in the
286      test/ folder to validate the core SQLite code.  The testfixture program
287      (and some other test programs too) is build and run when you type
288      "make test".
290   *  **ext/misc/json1.c** - This file implements the various JSON functions
291      that are build into SQLite.
293 There are many other source files.  Each has a succinct header comment that
294 describes its purpose and role within the larger system.
297 ## Contacts
299 The main SQLite webpage is [http://www.sqlite.org/](http://www.sqlite.org/)
300 with geographically distributed backups at
301 [http://www2.sqlite.org/](http://www2.sqlite.org) and
302 [http://www3.sqlite.org/](http://www3.sqlite.org).