Blame README.md

Packit Service e31a56

SQLite Source Repository

Packit Service e31a56
Packit Service e31a56
This repository contains the complete source code for the 
Packit Service e31a56
[SQLite database engine](https://sqlite.org/).  Some test scripts 
Packit Service e31a56
are also included.  However, many other test scripts
Packit Service e31a56
and most of the documentation are managed separately.
Packit Service e31a56
Packit Service e31a56
SQLite [does not use Git](https://sqlite.org/whynotgit.html).
Packit Service e31a56
If you are reading this on GitHub, then you are looking at an
Packit Service e31a56
unofficial mirror. See <https://sqlite.org/src> for the official
Packit Service e31a56
repository.
Packit Service e31a56
Packit Service e31a56
## Obtaining The Code
Packit Service e31a56
Packit Service e31a56
SQLite sources are managed using the
Packit Service e31a56
[Fossil](https://www.fossil-scm.org/), a distributed version control system
Packit Service e31a56
that was specifically designed to support SQLite development.
Packit Service e31a56
If you do not want to use Fossil, you can download tarballs or ZIP
Packit Service e31a56
archives or [SQLite archives](https://sqlite.org/cli.html#sqlar) as follows:
Packit Service e31a56
Packit Service e31a56
  *  Lastest trunk check-in as
Packit Service e31a56
     [Tarball](https://www.sqlite.org/src/tarball/sqlite.tar.gz),
Packit Service e31a56
     [ZIP-archive](https://www.sqlite.org/src/zip/sqlite.zip), or
Packit Service e31a56
     [SQLite-archive](https://www.sqlite.org/src/sqlar/sqlite.sqlar).
Packit Service e31a56
Packit Service e31a56
  *  Latest release as
Packit Service e31a56
     [Tarball](https://www.sqlite.org/src/tarball/sqlite.tar.gz?r=release),
Packit Service e31a56
     [ZIP-archive](https://www.sqlite.org/src/zip/sqlite.zip?r=release), or
Packit Service e31a56
     [SQLite-archive](https://www.sqlite.org/src/sqlar/sqlite.sqlar?r=release).
Packit Service e31a56
Packit Service e31a56
  *  For other check-ins, substitute an appropriate branch name or
Packit Service e31a56
     tag or hash prefix for "release" in the URLs of the previous
Packit Service e31a56
     bullet.  Or browse the [timeline](https://www.sqlite.org/src/timeline)
Packit Service e31a56
     to locate the check-in desired, click on its information page link,
Packit Service e31a56
     then click on the "Tarball" or "ZIP Archive" links on the information
Packit Service e31a56
     page.
Packit Service e31a56
Packit Service e31a56
If you do want to use Fossil to check out the source tree, 
Packit Service e31a56
first install Fossil version 2.0 or later.
Packit Service e31a56
(Source tarballs and precompiled binaries available
Packit Service e31a56
[here](https://www.fossil-scm.org/fossil/uv/download.html).  Fossil is
Packit Service e31a56
a stand-alone program.  To install, simply download or build the single 
Packit Service e31a56
executable file and put that file someplace on your $PATH.)
Packit Service e31a56
Then run commands like this:
Packit Service e31a56
Packit Service e31a56
        mkdir ~/sqlite
Packit Service e31a56
        cd ~/sqlite
Packit Service e31a56
        fossil clone https://www.sqlite.org/src sqlite.fossil
Packit Service e31a56
        fossil open sqlite.fossil
Packit Service e31a56
    
Packit Service e31a56
After setting up a repository using the steps above, you can always
Packit Service e31a56
update to the lastest version using:
Packit Service e31a56
Packit Service e31a56
        fossil update trunk   ;# latest trunk check-in
Packit Service e31a56
        fossil update release ;# latest official release
Packit Service e31a56
Packit Service e31a56
Or type "fossil ui" to get a web-based user interface.
Packit Service e31a56
Packit Service e31a56
## Compiling
Packit Service e31a56
Packit Service e31a56
First create a directory in which to place
Packit Service e31a56
the build products.  It is recommended, but not required, that the
Packit Service e31a56
build directory be separate from the source directory.  Cd into the
Packit Service e31a56
build directory and then from the build directory run the configure
Packit Service e31a56
script found at the root of the source tree.  Then run "make".
Packit Service e31a56
Packit Service e31a56
For example:
Packit Service e31a56
Packit Service e31a56
        tar xzf sqlite.tar.gz    ;#  Unpack the source tree into "sqlite"
Packit Service e31a56
        mkdir bld                ;#  Build will occur in a sibling directory
Packit Service e31a56
        cd bld                   ;#  Change to the build directory
Packit Service e31a56
        ../sqlite/configure      ;#  Run the configure script
Packit Service e31a56
        make                     ;#  Run the makefile.
Packit Service e31a56
        make sqlite3.c           ;#  Build the "amalgamation" source file
Packit Service e31a56
        make test                ;#  Run some tests (requires Tcl)
Packit Service e31a56
Packit Service e31a56
See the makefile for additional targets.
Packit Service e31a56
Packit Service e31a56
The configure script uses autoconf 2.61 and libtool.  If the configure
Packit Service e31a56
script does not work out for you, there is a generic makefile named
Packit Service e31a56
"Makefile.linux-gcc" in the top directory of the source tree that you
Packit Service e31a56
can copy and edit to suit your needs.  Comments on the generic makefile
Packit Service e31a56
show what changes are needed.
Packit Service e31a56
Packit Service e31a56
## Using MSVC
Packit Service e31a56
Packit Service e31a56
On Windows, all applicable build products can be compiled with MSVC.
Packit Service e31a56
First open the command prompt window associated with the desired compiler
Packit Service e31a56
version (e.g. "Developer Command Prompt for VS2013").  Next, use NMAKE
Packit Service e31a56
with the provided "Makefile.msc" to build one of the supported targets.
Packit Service e31a56
Packit Service e31a56
For example:
Packit Service e31a56
Packit Service e31a56
        mkdir bld
Packit Service e31a56
        cd bld
Packit Service e31a56
        nmake /f Makefile.msc TOP=..\sqlite
Packit Service e31a56
        nmake /f Makefile.msc sqlite3.c TOP=..\sqlite
Packit Service e31a56
        nmake /f Makefile.msc sqlite3.dll TOP=..\sqlite
Packit Service e31a56
        nmake /f Makefile.msc sqlite3.exe TOP=..\sqlite
Packit Service e31a56
        nmake /f Makefile.msc test TOP=..\sqlite
Packit Service e31a56
Packit Service e31a56
There are several build options that can be set via the NMAKE command
Packit Service e31a56
line.  For example, to build for WinRT, simply add "FOR_WINRT=1" argument
Packit Service e31a56
to the "sqlite3.dll" command line above.  When debugging into the SQLite
Packit Service e31a56
code, adding the "DEBUG=1" argument to one of the above command lines is
Packit Service e31a56
recommended.
Packit Service e31a56
Packit Service e31a56
SQLite does not require [Tcl](http://www.tcl.tk/) to run, but a Tcl installation
Packit Service e31a56
is required by the makefiles (including those for MSVC).  SQLite contains
Packit Service e31a56
a lot of generated code and Tcl is used to do much of that code generation.
Packit Service e31a56
Packit Service e31a56
## Source Code Tour
Packit Service e31a56
Packit Service e31a56
Most of the core source files are in the **src/** subdirectory.  The
Packit Service e31a56
**src/** folder also contains files used to build the "testfixture" test
Packit Service e31a56
harness. The names of the source files used by "testfixture" all begin
Packit Service e31a56
with "test".
Packit Service e31a56
The **src/** also contains the "shell.c" file
Packit Service e31a56
which is the main program for the "sqlite3.exe"
Packit Service e31a56
[command-line shell](https://sqlite.org/cli.html) and
Packit Service e31a56
the "tclsqlite.c" file which implements the
Packit Service e31a56
[Tcl bindings](https://sqlite.org/tclsqlite.html) for SQLite.
Packit Service e31a56
(Historical note:  SQLite began as a Tcl
Packit Service e31a56
extension and only later escaped to the wild as an independent library.)
Packit Service e31a56
Packit Service e31a56
Test scripts and programs are found in the **test/** subdirectory.
Packit Service e31a56
Addtional test code is found in other source repositories.
Packit Service e31a56
See [How SQLite Is Tested](http://www.sqlite.org/testing.html) for
Packit Service e31a56
additional information.
Packit Service e31a56
Packit Service e31a56
The **ext/** subdirectory contains code for extensions.  The
Packit Service e31a56
Full-text search engine is in **ext/fts3**.  The R-Tree engine is in
Packit Service e31a56
**ext/rtree**.  The **ext/misc** subdirectory contains a number of
Packit Service e31a56
smaller, single-file extensions, such as a REGEXP operator.
Packit Service e31a56
Packit Service e31a56
The **tool/** subdirectory contains various scripts and programs used
Packit Service e31a56
for building generated source code files or for testing or for generating
Packit Service e31a56
accessory programs such as "sqlite3_analyzer(.exe)".
Packit Service e31a56
Packit Service e31a56
### Generated Source Code Files
Packit Service e31a56
Packit Service e31a56
Several of the C-language source files used by SQLite are generated from
Packit Service e31a56
other sources rather than being typed in manually by a programmer.  This
Packit Service e31a56
section will summarize those automatically-generated files.  To create all
Packit Service e31a56
of the automatically-generated files, simply run "make target_source".
Packit Service e31a56
The "target_source" make target will create a subdirectory "tsrc/" and
Packit Service e31a56
fill it with all the source files needed to build SQLite, both
Packit Service e31a56
manually-edited files and automatically-generated files.
Packit Service e31a56
Packit Service e31a56
The SQLite interface is defined by the **sqlite3.h** header file, which is
Packit Service e31a56
generated from src/sqlite.h.in, ./manifest.uuid, and ./VERSION.  The
Packit Service e31a56
[Tcl script](http://www.tcl.tk) at tool/mksqlite3h.tcl does the conversion.
Packit Service e31a56
The manifest.uuid file contains the SHA3 hash of the particular check-in
Packit Service e31a56
and is used to generate the SQLITE\_SOURCE\_ID macro.  The VERSION file
Packit Service e31a56
contains the current SQLite version number.  The sqlite3.h header is really
Packit Service e31a56
just a copy of src/sqlite.h.in with the source-id and version number inserted
Packit Service e31a56
at just the right spots. Note that comment text in the sqlite3.h file is
Packit Service e31a56
used to generate much of the SQLite API documentation.  The Tcl scripts
Packit Service e31a56
used to generate that documentation are in a separate source repository.
Packit Service e31a56
Packit Service e31a56
The SQL language parser is **parse.c** which is generate from a grammar in
Packit Service e31a56
the src/parse.y file.  The conversion of "parse.y" into "parse.c" is done
Packit Service e31a56
by the [lemon](./doc/lemon.html) LALR(1) parser generator.  The source code
Packit Service e31a56
for lemon is at tool/lemon.c.  Lemon uses the tool/lempar.c file as a
Packit Service e31a56
template for generating its parser.
Packit Service e31a56
Packit Service e31a56
Lemon also generates the **parse.h** header file, at the same time it
Packit Service e31a56
generates parse.c. But the parse.h header file is
Packit Service e31a56
modified further (to add additional symbols) using the ./addopcodes.tcl
Packit Service e31a56
Tcl script.
Packit Service e31a56
Packit Service e31a56
The **opcodes.h** header file contains macros that define the numbers
Packit Service e31a56
corresponding to opcodes in the "VDBE" virtual machine.  The opcodes.h
Packit Service e31a56
file is generated by the scanning the src/vdbe.c source file.  The
Packit Service e31a56
Tcl script at ./mkopcodeh.tcl does this scan and generates opcodes.h.
Packit Service e31a56
A second Tcl script, ./mkopcodec.tcl, then scans opcodes.h to generate
Packit Service e31a56
the **opcodes.c** source file, which contains a reverse mapping from
Packit Service e31a56
opcode-number to opcode-name that is used for EXPLAIN output.
Packit Service e31a56
Packit Service e31a56
The **keywordhash.h** header file contains the definition of a hash table
Packit Service e31a56
that maps SQL language keywords (ex: "CREATE", "SELECT", "INDEX", etc.) into
Packit Service e31a56
the numeric codes used by the parse.c parser.  The keywordhash.h file is
Packit Service e31a56
generated by a C-language program at tool mkkeywordhash.c.
Packit Service e31a56
Packit Service e31a56
The **pragma.h** header file contains various definitions used to parse
Packit Service e31a56
and implement the PRAGMA statements.  The header is generated by a
Packit Service e31a56
script **tool/mkpragmatab.tcl**. If you want to add a new PRAGMA, edit
Packit Service e31a56
the **tool/mkpragmatab.tcl** file to insert the information needed by the
Packit Service e31a56
parser for your new PRAGMA, then run the script to regenerate the
Packit Service e31a56
**pragma.h** header file.
Packit Service e31a56
Packit Service e31a56
### The Amalgamation
Packit Service e31a56
Packit Service e31a56
All of the individual C source code and header files (both manually-edited
Packit Service e31a56
and automatically-generated) can be combined into a single big source file
Packit Service e31a56
**sqlite3.c** called "the amalgamation".  The amalgamation is the recommended
Packit Service e31a56
way of using SQLite in a larger application.  Combining all individual
Packit Service e31a56
source code files into a single big source code file allows the C compiler
Packit Service e31a56
to perform more cross-procedure analysis and generate better code.  SQLite
Packit Service e31a56
runs about 5% faster when compiled from the amalgamation versus when compiled
Packit Service e31a56
from individual source files.
Packit Service e31a56
Packit Service e31a56
The amalgamation is generated from the tool/mksqlite3c.tcl Tcl script.
Packit Service e31a56
First, all of the individual source files must be gathered into the tsrc/
Packit Service e31a56
subdirectory (using the equivalent of "make target_source") then the
Packit Service e31a56
tool/mksqlite3c.tcl script is run to copy them all together in just the
Packit Service e31a56
right order while resolving internal "#include" references.
Packit Service e31a56
Packit Service e31a56
The amalgamation source file is more than 200K lines long.  Some symbolic
Packit Service e31a56
debuggers (most notably MSVC) are unable to deal with files longer than 64K
Packit Service e31a56
lines.  To work around this, a separate Tcl script, tool/split-sqlite3c.tcl,
Packit Service e31a56
can be run on the amalgamation to break it up into a single small C file
Packit Service e31a56
called **sqlite3-all.c** that does #include on about seven other files
Packit Service e31a56
named **sqlite3-1.c**, **sqlite3-2.c**, ..., **sqlite3-7.c**.  In this way,
Packit Service e31a56
all of the source code is contained within a single translation unit so
Packit Service e31a56
that the compiler can do extra cross-procedure optimization, but no
Packit Service e31a56
individual source file exceeds 32K lines in length.
Packit Service e31a56
Packit Service e31a56
## How It All Fits Together
Packit Service e31a56
Packit Service e31a56
SQLite is modular in design.
Packit Service e31a56
See the [architectural description](http://www.sqlite.org/arch.html)
Packit Service e31a56
for details. Other documents that are useful in
Packit Service e31a56
(helping to understand how SQLite works include the
Packit Service e31a56
[file format](http://www.sqlite.org/fileformat2.html) description,
Packit Service e31a56
the [virtual machine](http://www.sqlite.org/opcode.html) that runs
Packit Service e31a56
prepared statements, the description of
Packit Service e31a56
[how transactions work](http://www.sqlite.org/atomiccommit.html), and
Packit Service e31a56
the [overview of the query planner](http://www.sqlite.org/optoverview.html).
Packit Service e31a56
Packit Service e31a56
Years of effort have gone into optimizating SQLite, both
Packit Service e31a56
for small size and high performance.  And optimizations tend to result in
Packit Service e31a56
complex code.  So there is a lot of complexity in the current SQLite
Packit Service e31a56
implementation.  It will not be the easiest library in the world to hack.
Packit Service e31a56
Packit Service e31a56
Key files:
Packit Service e31a56
Packit Service e31a56
  *  **sqlite.h.in** - This file defines the public interface to the SQLite
Packit Service e31a56
     library.  Readers will need to be familiar with this interface before
Packit Service e31a56
     trying to understand how the library works internally.
Packit Service e31a56
Packit Service e31a56
  *  **sqliteInt.h** - this header file defines many of the data objects
Packit Service e31a56
     used internally by SQLite.  In addition to "sqliteInt.h", some
Packit Service e31a56
     subsystems have their own header files.
Packit Service e31a56
Packit Service e31a56
  *  **parse.y** - This file describes the LALR(1) grammar that SQLite uses
Packit Service e31a56
     to parse SQL statements, and the actions that are taken at each step
Packit Service e31a56
     in the parsing process.
Packit Service e31a56
Packit Service e31a56
  *  **vdbe.c** - This file implements the virtual machine that runs
Packit Service e31a56
     prepared statements.  There are various helper files whose names
Packit Service e31a56
     begin with "vdbe".  The VDBE has access to the vdbeInt.h header file
Packit Service e31a56
     which defines internal data objects.  The rest of SQLite interacts
Packit Service e31a56
     with the VDBE through an interface defined by vdbe.h.
Packit Service e31a56
Packit Service e31a56
  *  **where.c** - This file (together with its helper files named
Packit Service e31a56
     by "where*.c") analyzes the WHERE clause and generates
Packit Service e31a56
     virtual machine code to run queries efficiently.  This file is
Packit Service e31a56
     sometimes called the "query optimizer".  It has its own private
Packit Service e31a56
     header file, whereInt.h, that defines data objects used internally.
Packit Service e31a56
Packit Service e31a56
  *  **btree.c** - This file contains the implementation of the B-Tree
Packit Service e31a56
     storage engine used by SQLite.  The interface to the rest of the system
Packit Service e31a56
     is defined by "btree.h".  The "btreeInt.h" header defines objects
Packit Service e31a56
     used internally by btree.c and not published to the rest of the system.
Packit Service e31a56
Packit Service e31a56
  *  **pager.c** - This file contains the "pager" implementation, the
Packit Service e31a56
     module that implements transactions.  The "pager.h" header file
Packit Service e31a56
     defines the interface between pager.c and the rest of the system.
Packit Service e31a56
Packit Service e31a56
  *  **os_unix.c** and **os_win.c** - These two files implement the interface
Packit Service e31a56
     between SQLite and the underlying operating system using the run-time
Packit Service e31a56
     pluggable VFS interface.
Packit Service e31a56
Packit Service e31a56
  *  **shell.c.in** - This file is not part of the core SQLite library.  This
Packit Service e31a56
     is the file that, when linked against sqlite3.a, generates the
Packit Service e31a56
     "sqlite3.exe" command-line shell.  The "shell.c.in" file is transformed
Packit Service e31a56
     into "shell.c" as part of the build process.
Packit Service e31a56
Packit Service e31a56
  *  **tclsqlite.c** - This file implements the Tcl bindings for SQLite.  It
Packit Service e31a56
     is not part of the core SQLite library.  But as most of the tests in this
Packit Service e31a56
     repository are written in Tcl, the Tcl language bindings are important.
Packit Service e31a56
Packit Service e31a56
  *  **test*.c** - Files in the src/ folder that begin with "test" go into
Packit Service e31a56
     building the "testfixture.exe" program.  The testfixture.exe program is
Packit Service e31a56
     an enhanced Tcl shell.  The testfixture.exe program runs scripts in the
Packit Service e31a56
     test/ folder to validate the core SQLite code.  The testfixture program
Packit Service e31a56
     (and some other test programs too) is build and run when you type
Packit Service e31a56
     "make test".
Packit Service e31a56
Packit Service e31a56
  *  **ext/misc/json1.c** - This file implements the various JSON functions
Packit Service e31a56
     that are build into SQLite.
Packit Service e31a56
Packit Service e31a56
There are many other source files.  Each has a succinct header comment that
Packit Service e31a56
describes its purpose and role within the larger system.
Packit Service e31a56
Packit Service e31a56
Packit Service e31a56
## Contacts
Packit Service e31a56
Packit Service e31a56
The main SQLite webpage is [http://www.sqlite.org/](http://www.sqlite.org/)
Packit Service e31a56
with geographically distributed backups at
Packit Service e31a56
[http://www2.sqlite.org/](http://www2.sqlite.org) and
Packit Service e31a56
[http://www3.sqlite.org/](http://www3.sqlite.org).