Skip to content

Architecture of SQLite

Introduction

A nearby diagram shows the main components of SQLite and how they interoperate. The text below explains the roles of the various components.

NOTE: 下面这种图是源自于The SQLite OS Interface or "VFS",它以层次化的结构展示了sqlite的architecture。

Overview

SQLite works by compiling SQL text into bytecode, then running that bytecode using a virtual machine.

interface function
sqlite3_prepare_v2() and related interfaces compiler for converting SQL text into bytecode 对应的是上图中的sql compiler
sqlite3_stmt object container for a single bytecode program using to implement a single SQL statement
sqlite3_step() interface passes a bytecode program into the virtual machine, and runs the program until it either completes, or forms a row of result to be returned, or hits a fatal error, or is interrupted

Interface

Tokenizer

When a string containing SQL statements is to be evaluated it is first sent to the tokenizer. The tokenizer breaks the SQL text into tokens and hands those tokens one by one to the parser. The tokenizer is hand-coded in the file tokenize.c.

Note that in this design, the tokenizer calls the parser. People who are familiar with YACC and BISON may be accustomed to doing things the other way around — having the parser call the tokenizer. Having the tokenizer call the parser is better, though, because it can be made threadsafe and it runs faster.

NOTE: 上面这段话的意思是,sqlite的tokenizer是自己实现的,并没有使用Lex (software)

Parser

The parser assigns meaning to tokens based on their context. The parser for SQLite is generated using the Lemon parser generator.

Code Generator

NOTE: 这个步骤是最最复杂的,它需要考虑database的底层实现,比如table是如何实现的;然后转换为对应的code

After the parser assembles tokens into a parse tree, the code generator runs to analyze the parser tree and generate bytecode that performs the work of the SQL statement.

Bytecode Engine

The bytecode program created by the code generator is run by a virtual machine.

SQLite implements SQL functions using callbacks to C-language routines. Even the built-in SQL functions are implemented this way.

B-Tree

NOTE: 关于B-Tree,参见工程discrete。

An SQLite database is maintained on disk using a B-tree implementation found in the btree.c source file.

OS Interface

In order to provide portability between across operating systems, SQLite uses abstract object called the VFS.

NOTE: VFS是virtual file system,显然它是一种abstraction

Utilities