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 include. However, many other test scripts
5 and most of the documentation are managed separately.
9 First create a directory in which to place
10 the build products. It is recommended, but not required, that the
11 build directory be separate from the source directory. Cd into the
12 build directory and then from the build directory run the configure
13 script found at the root of the source tree. Then run "make".
17 tar xzf sqlite.tar.gz ;# Unpack the source tree into "sqlite"
18 mkdir bld ;# Build will occur in a sibling directory
19 cd bld ;# Change to the build directory
20 ../sqlite/configure ;# Run the configure script
21 make ;# Run the makefile.
22 make sqlite3.c ;# Build the "amalgamation" source file
23 make test ;# Run some tests (requires Tcl)
25 See the makefile for additional targets.
27 The configure script uses autoconf 2.61 and libtool. If the configure
28 script does not work out for you, there is a generic makefile named
29 "Makefile.linux-gcc" in the top directory of the source tree that you
30 can copy and edit to suit your needs. Comments on the generic makefile
31 show what changes are needed.
35 On Windows, all applicable build products can be compiled with MSVC.
36 First open the command prompt window associated with the desired compiler
37 version (e.g. "Developer Command Prompt for VS2013"). Next, use NMAKE
38 with the provided "Makefile.msc" to build one of the supported targets.
44 nmake /f Makefile.msc TOP=..\sqlite
45 nmake /f Makefile.msc sqlite3.c TOP=..\sqlite
46 nmake /f Makefile.msc sqlite3.dll TOP=..\sqlite
47 nmake /f Makefile.msc sqlite3.exe TOP=..\sqlite
48 nmake /f Makefile.msc test TOP=..\sqlite
50 There are several build options that can be set via the NMAKE command
51 line. For example, to build for WinRT, simply add "FOR_WINRT=1" argument
52 to the "sqlite3.dll" command line above. When debugging into the SQLite
53 code, adding the "DEBUG=1" argument to one of the above command lines is
56 SQLite does not require Tcl to run, but a Tcl installation is required
57 by the makefiles (including those for MSVC). SQLite contains a lot of
58 generated code and Tcl is used to do much of that code generation. The
59 makefiles also require AWK.
63 Most of the core source files are in the **src/** subdirectory. But
64 src/ also contains files used to build the "testfixture" test harness;
65 those file all begin with "test". And src/ contains the "shell.c" file
66 which is the main program for the "sqlite3.exe" command-line shell and
67 the "tclsqlite.c" file which implements the bindings to SQLite from the
68 Tcl programming language. (Historical note: SQLite began as a Tcl
69 extension and only later escaped to the wild as an independent library.)
71 Test scripts and programs are found in the **test/** subdirectory.
72 There are other test suites for SQLite (see
73 [How SQLite Is Tested](http://www.sqlite.org/testing.html))
74 but those other test suites are
75 in separate source repositories.
77 The **ext/** subdirectory contains code for extensions. The
78 Full-text search engine is in **ext/fts3**. The R-Tree engine is in
79 **ext/rtree**. The **ext/misc** subdirectory contains a number of
80 smaller, single-file extensions, such as a REGEXP operator.
82 The **tool/** subdirectory contains various scripts and programs used
83 for building generated source code files or for testing or for generating
84 accessory programs such as "sqlite3_analyzer(.exe)".
86 ### Generated Source Code Files
88 Several of the C-language source files used by SQLite are generated from
89 other sources rather than being typed in manually by a programmer. This
90 section will summarize those automatically-generated files. To create all
91 of the automatically-generated files, simply run "make target_source".
92 The "target_source" make target will create a subdirectory "tsrc/" and
93 fill it with all the source files needed to build SQLite, both
94 manually-edited files and automatically-generated files.
96 The SQLite interface is defined by the **sqlite3.h** header file, which is
97 generated from src/sqlite.h.in, ./manifest.uuid, and ./VERSION. The
98 Tcl script at tool/mksqlite3h.tcl does the conversion. The manifest.uuid
99 file contains the SHA1 hash of the particular check-in and is used to generate
100 the SQLITE_SOURCE_ID macro. The VERSION file contains the current SQLite
101 version number. The sqlite3.h header is really just a copy of src/sqlite.h.in
102 with the source-id and version number inserted at just the right spots.
103 Note that comment text in the sqlite3.h file is used to generate much of
104 the SQLite API documentation. The Tcl scripts used to generate that
105 documentation are in a separate source repository.
107 The SQL language parser is **parse.c** which is generate from a grammar in
108 the src/parse.y file. The conversion of "parse.y" into "parse.c" is done
109 by the [lemon](./doc/lemon.html) LALR(1) parser generator. The source code
110 for lemon is at tool/lemon.c. Lemon uses a
111 template for generating its parser. A generic template is in tool/lempar.c,
112 but SQLite uses a slightly modified template found in src/lempar.c.
114 Lemon also generates the **parse.h** header file, at the same time it
115 generates parse.c. But the parse.h header file is
116 modified further (to add additional symbols) using the ./addopcodes.awk
119 The **opcodes.h** header file contains macros that define the numbers
120 corresponding to opcodes in the "VDBE" virtual machine. The opcodes.h
121 file is generated by the scanning the src/vdbe.c source file. The
122 AWK script at ./mkopcodeh.awk does this scan and generates opcodes.h.
123 A second AWK script, ./mkopcodec.awk, then scans opcodes.h to generate
124 the **opcodes.c** source file, which contains a reverse mapping from
125 opcode-number to opcode-name that is used for EXPLAIN output.
127 The **keywordhash.h** header file contains the definition of a hash table
128 that maps SQL language keywords (ex: "CREATE", "SELECT", "INDEX", etc.) into
129 the numeric codes used by the parse.c parser. The keywordhash.h file is
130 generated by a C-language program at tool mkkeywordhash.c.
134 All of the individual C source code and header files (both manually-edited
135 and automatically-generated) can be combined into a single big source file
136 **sqlite3.c** called "the amalgamation". The amalgamation is the recommended
137 way of using SQLite in a larger application. Combining all individual
138 source code files into a single big source code file allows the C compiler
139 to perform more cross-procedure analysis and generate better code. SQLite
140 runs about 5% faster when compiled from the amalgamation versus when compiled
141 from individual source files.
143 The amalgamation is generated from the tool/mksqlite3c.tcl Tcl script.
144 First, all of the individual source files must be gathered into the tsrc/
145 subdirectory (using the equivalent of "make target_source") then the
146 tool/mksqlite3c.tcl script is run to copy them all together in just the
147 right order while resolving internal "#include" references.
149 The amalgamation source file is more than 100K lines long. Some symbolic
150 debuggers (most notably MSVC) are unable to deal with files longer than 64K
151 lines. To work around this, a separate Tcl script, tool/split-sqlite3c.tcl,
152 can be run on the amalgamation to break it up into a single small C file
153 called **sqlite3-all.c** that does #include on about five other files
154 named **sqlite3-1.c**, **sqlite3-2.c**, ..., **sqlite3-5.c**. In this way,
155 all of the source code is contained within a single translation unit so
156 that the compiler can do extra cross-procedure optimization, but no
157 individual source file exceeds 32K lines in length.
159 ## How It All Fits Together
161 SQLite is modular in design.
162 See the [architectural description](http://www.sqlite.org/arch.html)
163 for details. Other documents that are useful in
164 (helping to understand how SQLite works include the
165 [file format](http://www.sqlite.org/fileformat2.html) description,
166 the [virtual machine](http://www.sqlite.org/vdbe.html) that runs
167 prepared statements, the description of
168 [how transactions work](http://www.sqlite.org/atomiccommit.html), and
169 the [overview of the query planner](http://www.sqlite.org/optoverview.html).
171 Unfortunately, years of effort have gone into optimizating SQLite, both
172 for small size and high performance. And optimizations tend to result in
173 complex code. So there is a lot of complexity in the SQLite implementation.
177 * **sqlite3.h** - This file defines the public interface to the SQLite
178 library. Readers will need to be familiar with this interface before
179 trying to understand how the library works internally.
181 * **sqliteInt.h** - this header file defines many of the data objects
182 used internally by SQLite.
184 * **parse.y** - This file describes the LALR(1) grammer that SQLite uses
185 to parse SQL statements, and the actions that are taken at each stop
186 in the parsing process.
188 * **vdbe.c** - This file implements the virtual machine that runs
189 prepared statements. There are various helper files whose names
190 begin with "vdbe". The VDBE has access to the vdbeInt.h header file
191 which defines internal data objects. The rest of SQLite interacts
192 with the VDBE through an interface defined by vdbe.h.
194 * **where.c** - This file analyzes the WHERE clause and generates
195 virtual machine code to run queries efficiently. This file is
196 sometimes called the "query optimizer". It has its own private
197 header file, whereInt.h, that defines data objects used internally.
199 * **btree.c** - This file contains the implementation of the B-Tree
200 storage engine used by SQLite.
202 * **pager.c** - This file contains the "pager" implementation, the
203 module that implements transactions.
205 * **os_unix.c** and **os_win.c** - These two files implement the interface
206 between SQLite and the underlying operating system using the run-time
207 pluggable VFS interface.
212 The main SQLite webpage is [http://www.sqlite.org/](http://www.sqlite.org/)
213 with geographically distributed backup servers at
214 [http://www2.sqlite.org/](http://www2.sqlite.org) and
215 [http://www3.sqlite.org/](http://www3.sqlite.org).