interplane.Get Omega
Read in your language

sqlite

[Ported from github.com/sqlite/sqlite] Fix bugs in and improve the interface to the ".check" command of the CLI.

Brought by drakestapleton

branch master Branches Tags History

sqlite / README.md

429 lines · 21275 bytes Raw Blame History Rendered
1<h1 align="center">SQLite Source Repository</h1>
2
3This repository contains the complete source code for the
4[SQLite database engine](https://sqlite.org/) going back
5to 2000-05-29. The tree includes many tests and some
6documentation, though additional tests and most documentation
7are managed separately.
8
9See the [on-line documentation](https://sqlite.org/) for more information
10about what SQLite is and how it works from a user's perspective. This
11README file is about the source code that goes into building SQLite,
12not about how SQLite is used.
13
14## Version Control
15
16SQLite sources are managed using
17[Fossil](https://fossil-scm.org/), a distributed version control system
18that was specifically designed and written to support SQLite development.
19The [Fossil repository](https://sqlite.org/src/timeline) contains the urtext.
20
21If you are reading this on GitHub or some other Git repository or service,
22then you are looking at a mirror. The names of check-ins and
23other artifacts in a Git mirror are different from the official
24names for those objects. The official names for check-ins are
25found in a footer on the check-in comment for authorized mirrors.
26The official check-in name can also be seen in the `manifest.uuid` file
27in the root of the tree. Always use the official name, not the
28Git-name, when communicating about an SQLite check-in.
29
30If you pulled your SQLite source code from a secondary source and want to
31verify 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
36The preferred way to ask questions or make comments about SQLite is to
37visit the [SQLite Forum](https://sqlite.org/forum) at
38<https://sqlite.org/forum/>. Anonymous postings are permitted.
39Do <u>not</u> send bug reports to the main SQLite Forum.
40To report bugs, post a message on the [SQLite bugs
41forum](https://sqlite.org/bugs) at <https://sqlite.org/bugs>.
42
43If you think you have found a bug that has security implications, report
44it to the SQLite bugs forum anyhow. If you really don't want to do that,
45you can instead send a private email to drh at sqlite dot org. However,
46unsolicited messages sent to that email address are liable to be
47copy/pasted onto the SQLite bugs forum anyhow.
48
49## Public Domain
50
51The SQLite source code is in the public domain. See
52<https://sqlite.org/copyright.html> for details.
53
54Because SQLite is in the public domain, we do not normally accept pull
55requests, because if we did take a pull request, the changes in that
56pull request might carry a copyright and the SQLite source code would
57then no longer be fully in the public domain.
58
59## Obtaining The SQLite Source Code
60
61Source 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
74To access sources directly using [Fossil](https://fossil-scm.org/home),
75first install Fossil version 2.0 or later.
76Source tarballs and precompiled binaries for Fossil are available at
77<https://fossil-scm.org/home/uv/download.html>. Fossil is
78a stand-alone program. To install, simply download or build the single
79executable file and put that file someplace on your $PATH or %PATH%.
80Then run commands like this:
81
82 mkdir -p ~/sqlite
83 cd ~/sqlite
84 fossil open https://sqlite.org/src
85
86The initial "fossil open" command will take two or three minutes. Afterwards,
87you can do fast, bandwidth-efficient updates to whatever versions of SQLite you
88like. 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
95Or type "fossil ui" to get a web-based user interface.
96
97## Compiling for Unix-like systems
98
99First create a directory in which to place
100the build products. It is recommended, but not required, that the
101build directory be separate from the source directory. Cd into the
102build directory and then from the build directory run the configure
103script found at the root of the source tree. Then run "make".
104See the [compile-for-unix.md](doc/compile-for-unix.md) document for
105more detail.
106
107For 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
122See the makefile for additional targets. For debugging builds, the
123core developers typically run "configure" with options like this:
124
125 ../sqlite/configure --all --debug CFLAGS='-O0 -g'
126
127For release builds, the core developers usually do:
128
129 ../sqlite/configure --all
130
131Core deliverables (sqlite3.c, sqlite3) can be built without a TCL, but
132many makefile targets require a "tclsh" TCL interpreter version 8.6
133or later. The "tclextension-install" target and the test targets that follow
134all require TCL development libraries too. ("apt install tcl-dev"). It is
135helpful, but is not required, to install the SQLite TCL extension (the
136"tclextension-install" target) prior to running tests. The "releasetest"
137target has additional requirements, such as "valgrind".
138
139On "make" command-lines, one can add "OPTIONS=..." to specify additional
140compile-time options over and above those set by ./configure. For example,
141to compile with the SQLITE_OMIT_DEPRECATED compile-time option, one could say:
142
143 ./configure --all
144 make OPTIONS=-DSQLITE_OMIT_DEPRECATED sqlite3
145
146The configure script uses [autosetup](https://msteveb.github.io/autosetup/).
147If the configure script does not work out for you, there is a generic
148makefile named "Makefile.linux-gcc" in the top directory of the source tree
149that you can copy and edit to suit your needs. Comments on the generic
150makefile show what changes are needed.
151
152## Compiling for Windows Using MSVC
153
154On Windows, everything can be compiled with MSVC.
155You will also need a working installation of TCL if you want to run tests,
156though TCL is not required if you just want to build SQLite itself.
157See the [compile-for-windows.md](doc/compile-for-windows.md) document for
158additional information about how to install MSVC and TCL and configure your
159build environment.
160
161If you want to run tests, you need to let SQLite know the location of your
162TCL library, using a command like this:
163
164 set TCLDIR=c:\Tcl
165
166SQLite itself does not contain any TCL code, but it does use TCL to run
167tests. You may need to install TCL development libraries in order to
168successfully complete some makefile targets. It is helpful, but is not
169required, to install the SQLite TCL extension (the "tclextension-install"
170target) prior to running tests.
171
172The source tree contains a "make.bat" file that allows the same "make"
173commands 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
175file:
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
185There are many other makefile targets. See comments in Makefile.msc for
186details.
187
188As with the unix Makefile, the OPTIONS=... argument can be passed on the nmake
189command-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
234Several of the C-language source files used by SQLite are generated from
235other sources rather than being typed in manually by a programmer. This
236section will summarize those automatically-generated files. To create all
237of the automatically-generated files, simply run "make target&#95;source".
238The "target&#95;source" make target will create a subdirectory "tsrc/" and
239fill it with all the source files needed to build SQLite, both
240manually-edited files and automatically-generated files.
241
242The SQLite interface is defined by the **sqlite3.h** header file, which is
243generated from src/sqlite.h.in, ./manifest.uuid, and ./VERSION. The
244[Tcl script](https://www.tcl.tk) at tool/mksqlite3h.tcl does the conversion.
245The manifest.uuid file contains the SHA3 hash of the particular check-in
246and is used to generate the SQLITE\_SOURCE\_ID macro. The VERSION file
247contains the current SQLite version number. The sqlite3.h header is really
248just a copy of src/sqlite.h.in with the source-id and version number inserted
249at just the right spots. Note that comment text in the sqlite3.h file is
250used to generate much of the SQLite API documentation. The Tcl scripts
251used to generate that documentation are in a separate source repository.
252
253The SQL language parser is **parse.c** which is generated from a grammar in
254the src/parse.y file. The conversion of "parse.y" into "parse.c" is done
255by the [lemon](./doc/lemon.html) LALR(1) parser generator. The source code
256for lemon is at tool/lemon.c. Lemon uses the tool/lempar.c file as a
257template for generating its parser.
258Lemon also generates the **parse.h** header file, at the same time it
259generates parse.c.
260
261The **opcodes.h** header file contains macros that define the numbers
262corresponding to opcodes in the "VDBE" virtual machine. The opcodes.h
263file is generated by scanning the src/vdbe.c source file. The
264Tcl script at ./mkopcodeh.tcl does this scan and generates opcodes.h.
265A second Tcl script, ./mkopcodec.tcl, then scans opcodes.h to generate
266the **opcodes.c** source file, which contains a reverse mapping from
267opcode-number to opcode-name that is used for EXPLAIN output.
268
269The **keywordhash.h** header file contains the definition of a hash table
270that maps SQL language keywords (ex: "CREATE", "SELECT", "INDEX", etc.) into
271the numeric codes used by the parse.c parser. The keywordhash.h file is
272generated by a C-language program at tool mkkeywordhash.c.
273
274The **pragma.h** header file contains various definitions used to parse
275and implement the PRAGMA statements. The header is generated by a
276script **tool/mkpragmatab.tcl**. If you want to add a new PRAGMA, edit
277the **tool/mkpragmatab.tcl** file to insert the information needed by the
278parser for your new PRAGMA, then run the script to regenerate the
279**pragma.h** header file.
280
281### The Amalgamation
282
283All of the individual C source code and header files (both manually-edited
284and automatically-generated) can be combined into a single big source file
285**sqlite3.c** called "the amalgamation". The amalgamation is the recommended
286way of using SQLite in a larger application. Combining all individual
287source code files into a single big source code file allows the C compiler
288to perform more cross-procedure analysis and generate better code. SQLite
289runs about 5% faster when compiled from the amalgamation versus when compiled
290from individual source files.
291
292The amalgamation is generated from the tool/mksqlite3c.tcl Tcl script.
293First, all of the individual source files must be gathered into the tsrc/
294subdirectory (using the equivalent of "make target_source") then the
295tool/mksqlite3c.tcl script is run to copy them all together in just the
296right order while resolving internal "#include" references.
297
298The amalgamation source file is more than 200K lines long. Some symbolic
299debuggers (most notably MSVC) are unable to deal with files longer than 64K
300lines. To work around this, a separate Tcl script, tool/split-sqlite3c.tcl,
301can be run on the amalgamation to break it up into a single small C file
302called **sqlite3-all.c** that does #include on about seven other files
303named **sqlite3-1.c**, **sqlite3-2.c**, ..., **sqlite3-7.c**. In this way,
304all of the source code is contained within a single translation unit so
305that the compiler can do extra cross-procedure optimization, but no
306individual source file exceeds 32K lines in length.
307
308## How It All Fits Together
309
310SQLite is modular in design.
311See the [architectural description](https://sqlite.org/arch.html)
312for details. Other documents that are useful in
313helping to understand how SQLite works include the
314[file format](https://sqlite.org/fileformat2.html) description,
315the [virtual machine](https://sqlite.org/opcode.html) that runs
316prepared statements, the description of
317[how transactions work](https://sqlite.org/atomiccommit.html), and
318the [overview of the query planner](https://sqlite.org/optoverview.html).
319
320Decades of effort have gone into optimizing SQLite, both
321for small size and high performance. And optimizations tend to result in
322complex code. So there is a lot of complexity in the current SQLite
323implementation. 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
392There are many other source files. Each has a succinct header comment that
393describes its purpose and role within the larger system.
394
395<a name="vauth"></a>
396## Verifying Code Authenticity
397
398The `manifest` file at the root directory of the source tree
399contains either a SHA3-256 hash or a SHA1 hash
400for every source file in the repository.
401The name of the version of the entire source tree is just the
402SHA3-256 hash of the `manifest` file itself, possibly with the
403last line of that file omitted if the last line begins with
404"`# Remove this line`".
405The `manifest.uuid` file should contain the SHA3-256 hash of the
406`manifest` file. If all of the above hash comparisons are correct, then
407you can be confident that your source tree is authentic and unadulterated.
408Details 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
411The process of checking source code authenticity is automated by the
412makefile:
413
414> make verify-source
415
416Using the makefile to verify source integrity is good for detecting
417accidental changes to the source tree, but malicious changes could be
418hidden by also modifying the makefiles.
419
420## Contacts
421
422The main SQLite website is [https://sqlite.org/](https://sqlite.org/)
423with geographically distributed backups at
424[https://www2.sqlite.org/](https://www2.sqlite.org) and
425[https://www3.sqlite.org/](https://www3.sqlite.org).
426
427Contact the SQLite developers through the
428[SQLite Forum](https://sqlite.org/forum/). In an emergency, you
429can send private email to the lead developer at drh at sqlite dot org.