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