aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/libraries/sqlite/unix/sqlite-3.5.1/www/arch.tcl
diff options
context:
space:
mode:
Diffstat (limited to 'libraries/sqlite/unix/sqlite-3.5.1/www/arch.tcl')
-rw-r--r--libraries/sqlite/unix/sqlite-3.5.1/www/arch.tcl221
1 files changed, 221 insertions, 0 deletions
diff --git a/libraries/sqlite/unix/sqlite-3.5.1/www/arch.tcl b/libraries/sqlite/unix/sqlite-3.5.1/www/arch.tcl
new file mode 100644
index 0000000..d6c8166
--- /dev/null
+++ b/libraries/sqlite/unix/sqlite-3.5.1/www/arch.tcl
@@ -0,0 +1,221 @@
1#
2# Run this Tcl script to generate the sqlite.html file.
3#
4set rcsid {$Id: arch.tcl,v 1.16 2004/10/10 17:24:54 drh Exp $}
5source common.tcl
6header {Architecture of SQLite}
7puts {
8<h2>The Architecture Of SQLite</h2>
9
10<h3>Introduction</h3>
11
12<table align="right" border="1" cellpadding="15" cellspacing="1">
13<tr><th>Block Diagram Of SQLite</th></tr>
14<tr><td><img src="arch2.gif"></td></tr>
15</table>
16<p>This document describes the architecture of the SQLite library.
17The information here is useful to those who want to understand or
18modify the inner workings of SQLite.
19</p>
20
21<p>
22A block diagram showing the main components of SQLite
23and how they interrelate is shown at the right. The text that
24follows will provide a quick overview of each of these components.
25</p>
26
27
28<p>
29This document describes SQLite version 3.0. Version 2.8 and
30earlier are similar but the details differ.
31</p>
32
33<h3>Interface</h3>
34
35<p>Much of the public interface to the SQLite library is implemented by
36functions found in the <b>main.c</b>, <b>legacy.c</b>, and
37<b>vdbeapi.c</b> source files
38though some routines are
39scattered about in other files where they can have access to data
40structures with file scope. The
41<b>sqlite3_get_table()</b> routine is implemented in <b>table.c</b>.
42<b>sqlite3_mprintf()</b> is found in <b>printf.c</b>.
43<b>sqlite3_complete()</b> is in <b>tokenize.c</b>.
44The Tcl interface is implemented by <b>tclsqlite.c</b>. More
45information on the C interface to SQLite is
46<a href="capi3ref.html">available separately</a>.<p>
47
48<p>To avoid name collisions with other software, all external
49symbols in the SQLite library begin with the prefix <b>sqlite3</b>.
50Those symbols that are intended for external use (in other words,
51those symbols which form the API for SQLite) begin
52with <b>sqlite3_</b>.</p>
53
54<h3>Tokenizer</h3>
55
56<p>When a string containing SQL statements is to be executed, the
57interface passes that string to the tokenizer. The job of the tokenizer
58is to break the original string up into tokens and pass those tokens
59one by one to the parser. The tokenizer is hand-coded in C in
60the file <b>tokenize.c</b>.
61
62<p>Note that in this design, the tokenizer calls the parser. People
63who are familiar with YACC and BISON may be used to doing things the
64other way around -- having the parser call the tokenizer. The author
65of SQLite
66has done it both ways and finds things generally work out nicer for
67the tokenizer to call the parser. YACC has it backwards.</p>
68
69<h3>Parser</h3>
70
71<p>The parser is the piece that assigns meaning to tokens based on
72their context. The parser for SQLite is generated using the
73<a href="http://www.hwaci.com/sw/lemon/">Lemon</a> LALR(1) parser
74generator. Lemon does the same job as YACC/BISON, but it uses
75a different input syntax which is less error-prone.
76Lemon also generates a parser which is reentrant and thread-safe.
77And lemon defines the concept of a non-terminal destructor so
78that it does not leak memory when syntax errors are encountered.
79The source file that drives Lemon is found in <b>parse.y</b>.</p>
80
81<p>Because
82lemon is a program not normally found on development machines, the
83complete source code to lemon (just one C file) is included in the
84SQLite distribution in the "tool" subdirectory. Documentation on
85lemon is found in the "doc" subdirectory of the distribution.
86</p>
87
88<h3>Code Generator</h3>
89
90<p>After the parser assembles tokens into complete SQL statements,
91it calls the code generator to produce virtual machine code that
92will do the work that the SQL statements request. There are many
93files in the code generator:
94<b>attach.c</b>,
95<b>auth.c</b>,
96<b>build.c</b>,
97<b>delete.c</b>,
98<b>expr.c</b>,
99<b>insert.c</b>,
100<b>pragma.c</b>,
101<b>select.c</b>,
102<b>trigger.c</b>,
103<b>update.c</b>,
104<b>vacuum.c</b>
105and <b>where.c</b>.
106In these files is where most of the serious magic happens.
107<b>expr.c</b> handles code generation for expressions.
108<b>where.c</b> handles code generation for WHERE clauses on
109SELECT, UPDATE and DELETE statements. The files <b>attach.c</b>,
110<b>delete.c</b>, <b>insert.c</b>, <b>select.c</b>, <b>trigger.c</b>
111<b>update.c</b>, and <b>vacuum.c</b> handle the code generation
112for SQL statements with the same names. (Each of these files calls routines
113in <b>expr.c</b> and <b>where.c</b> as necessary.) All other
114SQL statements are coded out of <b>build.c</b>.
115The <b>auth.c</b> file implements the functionality of
116<b>sqlite3_set_authorizer()</b>.</p>
117
118<h3>Virtual Machine</h3>
119
120<p>The program generated by the code generator is executed by
121the virtual machine. Additional information about the virtual
122machine is <a href="opcode.html">available separately</a>.
123To summarize, the virtual machine implements an abstract computing
124engine specifically designed to manipulate database files. The
125machine has a stack which is used for intermediate storage.
126Each instruction contains an opcode and
127up to three additional operands.</p>
128
129<p>The virtual machine itself is entirely contained in a single
130source file <b>vdbe.c</b>. The virtual machine also has
131its own header files: <b>vdbe.h</b> that defines an interface
132between the virtual machine and the rest of the SQLite library and
133<b>vdbeInt.h</b> which defines structure private the virtual machine.
134The <b>vdbeaux.c</b> file contains utilities used by the virtual
135machine and interface modules used by the rest of the library to
136construct VM programs. The <b>vdbeapi.c</b> file contains external
137interfaces to the virtual machine such as the
138<b>sqlite3_bind_...</b> family of functions. Individual values
139(strings, integer, floating point numbers, and BLOBs) are stored
140in an internal object named "Mem" which is implemented by
141<b>vdbemem.c</b>.</p>
142
143<p>
144SQLite implements SQL functions using callbacks to C-language routines.
145Even the built-in SQL functions are implemented this way. Most of
146the built-in SQL functions (ex: <b>coalesce()</b>, <b>count()</b>,
147<b>substr()</b>, and so forth) can be found in <b>func.c</b>.
148Date and time conversion functions are found in <b>date.c</b>.
149</p>
150
151<h3>B-Tree</h3>
152
153<p>An SQLite database is maintained on disk using a B-tree implementation
154found in the <b>btree.c</b> source file. A separate B-tree is used for
155each table and index in the database. All B-trees are stored in the
156same disk file. Details of the file format are recorded in a large
157comment at the beginning of <b>btree.c</b>.</p>
158
159<p>The interface to the B-tree subsystem is defined by the header file
160<b>btree.h</b>.
161</p>
162
163<h3>Page Cache</h3>
164
165<p>The B-tree module requests information from the disk in fixed-size
166chunks. The default chunk size is 1024 bytes but can vary between 512
167and 65536 bytes.
168The page cache is responsible for reading, writing, and
169caching these chunks.
170The page cache also provides the rollback and atomic commit abstraction
171and takes care of locking of the database file. The
172B-tree driver requests particular pages from the page cache and notifies
173the page cache when it wants to modify pages or commit or rollback
174changes and the page cache handles all the messy details of making sure
175the requests are handled quickly, safely, and efficiently.</p>
176
177<p>The code to implement the page cache is contained in the single C
178source file <b>pager.c</b>. The interface to the page cache subsystem
179is defined by the header file <b>pager.h</b>.
180</p>
181
182<h3>OS Interface</h3>
183
184<p>
185In order to provide portability between POSIX and Win32 operating systems,
186SQLite uses an abstraction layer to interface with the operating system.
187The interface to the OS abstraction layer is defined in
188<b>os.h</b>. Each supported operating system has its own implementation:
189<b>os_unix.c</b> for Unix, <b>os_win.c</b> for windows, and so forth.
190Each of these operating-specific implements typically has its own
191header file: <b>os_unix.h</b>, <b>os_win.h</b>, etc.
192</p>
193
194<h3>Utilities</h3>
195
196<p>
197Memory allocation and caseless string comparison routines are located
198in <b>util.c</b>.
199Symbol tables used by the parser are maintained by hash tables found
200in <b>hash.c</b>. The <b>utf.c</b> source file contains Unicode
201conversion subroutines.
202SQLite has its own private implementation of <b>printf()</b> (with
203some extensions) in <b>printf.c</b> and its own random number generator
204in <b>random.c</b>.
205</p>
206
207<h3>Test Code</h3>
208
209<p>
210If you count regression test scripts,
211more than half the total code base of SQLite is devoted to testing.
212There are many <b>assert()</b> statements in the main code files.
213In additional, the source files <b>test1.c</b> through <b>test5.c</b>
214together with <b>md5.c</b> implement extensions used for testing
215purposes only. The <b>os_test.c</b> backend interface is used to
216simulate power failures to verify the crash-recovery mechanism in
217the pager.
218</p>
219
220}
221footer $rcsid