欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

SQLite源码剖析(1)

程序员文章站 2022-03-02 16:27:43
...

声明:本SQLite源码剖析系列为刘兴(http://deepfuture.iteye.com/)原创,未经笔者授权,任何人和机构不能转载

什么是 sqlite ?

sqlite 是一款轻量级的、基于文件的嵌入式数据库,2000年就已经诞生,经过7年多的发展,直到今天已经成为最流行的嵌入式数据库,包括google在内的公司在其桌面软件中亦使用 sqlite 存储用户数据。由此可以看出,已经没有任何理由去怀疑sqlite的稳定性了。


sqlite的优势?

1. 免配置,和access一样,只要把数据库文件通过ftp上传到服务器上就可以使用,不需要服务器的额外支持

2. 备份方便,因为只是一个文件,只要复制一份该文件,就能备份整个数据库

3. 虽然是轻量级数据库,但他支持最大 2tb 的单个库文件。

4. 快,无与伦比的快。经过实际测试,在几百万记录的情况下,sqlite的插入和查询速度和 mysql 不分上下,快于 sql server,10倍于 access (但这并不意味着它可以替代 sql server )

 

SQLite源码下载 

    SQLITE主页上下载源码,选择右边出现以下字样的进行下载


SQLite源码剖析(1)
            
    
    博客分类: 数据库 SQLiteSQLSQL ServerOS嵌入式 
 

 

 

源码分析

我们先分析主程序sqlite3.c

/******************************************************************************

** This file is an amalgamation of many separate C source files from SQLite

** version 3.6.23.1.  By combining all the individual C code files into this

** single large file, the entire code can be compiled as a one translation

** unit.  This allows many compilers to do optimizations that would not be

** possible if the files were compiled separately.  Performance improvements

** of 5% are more are commonly seen when SQLite is compiled as a single

** translation unit.

**

** This file is all you need to compile SQLite.  To use SQLite in other

** programs, you need this file and the "sqlite3.h" header file that defines

** the programming interface to the SQLite library.  (If you do not have

** the "sqlite3.h" header file at hand, you will find a copy embedded within

** the text of this file.  Search for "Begin file sqlite3.h" to find the start

** of the embedded sqlite3.h header file.) Additional code files may be needed

** if you want a wrapper to interface SQLite with your choice of programming

** language. The code for the "sqlite3" command-line shell is also in a

** separate file. This file contains only code for the core SQLite library.

*/

//主程序定义SQLITE核心:define SQLITE_CORE 1

//主程序定义SQLITE_API

//主程序序版本为合并后的源代码:define SQLITE_AMALGAMATION 1

#define SQLITE_CORE 1

#define SQLITE_AMALGAMATION 1

#ifndef SQLITE_PRIVATE

# define SQLITE_PRIVATE static

#endif

#ifndef SQLITE_API

# define SQLITE_API

#endif

/************** Begin file sqliteInt.h ***************************************/

/*

** 2001 September 15

**

** The author disclaims copyright to this source code.  In place of

** a legal notice, here is a blessing:

**

**    May you do good and not evil.

**    May you find forgiveness for yourself and forgive others.

**    May you share freely, never taking more than you give.

**

*************************************************************************

** Internal interface definitions for SQLite.

**

*/

//SQLITE内部接口定义

#ifndef _SQLITEINT_H_

#define _SQLITEINT_H_

//如果基础操作系统支持,可用#define开启大于2G的单个文件支持。

/*

** These #defines should enable >2GB file support on POSIX if the

** underlying operating system supports it.  If the OS lacks

** large file support, or if the OS is windows, these should be no-ops.

**_LARGEFILE_SOURCE宏必须在任何#include前使用

** Ticket #2739:  The _LARGEFILE_SOURCE macro must appear before any

** system #includes.  Hence, this block of code must be the very first

** code in all source files.

**在编译命令行使用-DSQLITE_DISABLE_LFS可禁止大文件支持,

** Large file support can be disabled using the -DSQLITE_DISABLE_LFS switch

** on the compiler command line.  This is necessary if you are compiling

** on a recent machine (ex: Red Hat 7.2) but you want your code to work

** on an older machine (ex: Red Hat 6.0).  If you compile on Red Hat 7.2

** without this option, LFS is enable.  But LFS does not exist in the kernel

** in Red Hat 6.0, so the code won't work.  Hence, for maximum binary

** portability you should omit LFS.

**

** Similar is true for Mac OS X.  LFS is only supported on Mac OS X 9 and later.

*/

//如果没禁止大文件支持,则定义相关常量

#ifndef SQLITE_DISABLE_LFS

# define _LARGE_FILE       1

# ifndef _FILE_OFFSET_BITS

#   define _FILE_OFFSET_BITS 64

# endif

# define _LARGEFILE_SOURCE 1

#endif

 

/*

** Include the configuration header output by 'configure' if we're using the

** autoconf-based build

*/

//如果使用autoconf-based构建,则include "config.h"

#ifdef _HAVE_SQLITE_CONFIG_H

#include "config.h"

#endif

 

/************** Include sqliteLimit.h in the middle of sqliteInt.h ***********/

/************** Begin file sqliteLimit.h *************************************/

/*

** 2007 May 7

**

** The author disclaims copyright to this source code.  In place of

** a legal notice, here is a blessing:

**

**    May you do good and not evil.

**    May you find forgiveness for yourself and forgive others.

**    May you share freely, never taking more than you give.

**

*************************************************************************

**

** This file defines various limits of what SQLite can process.

*/

 

/*

** The maximum length of a TEXT or BLOB in bytes.   This also

** limits the size of a row in a table or index.

**

** The hard limit is the ability of a 32-bit signed integer

** to count the size: 2^31-1 or 2147483647.

*/

//最大32位有符号整数

#ifndef SQLITE_MAX_LENGTH

# define SQLITE_MAX_LENGTH 1000000000

#endif

 

/*

** This is the maximum number of

**以下这些项的最大值:

**表中的列、索引中的列、视图的列、updateset从句的数量、select

**返回的字段数、GROUP BY ORDER BY的字段数、INSERTvalues从句

**    * Columns in a table

**    * Columns in an index

**    * Columns in a view

**    * Terms in the SET clause of an UPDATE statement

**    * Terms in the result set of a SELECT statement

**    * Terms in the GROUP BY or ORDER BY clauses of a SELECT statement.

**    * Terms in the VALUES clause of an INSERT statement

**

** The hard upper limit here is 32676.  Most database people will

** tell you that in a well-normalized database, you usually should

** not have more than a dozen or so columns in any table.  And if

** that is the case, there is no point in having more than a few

** dozen values in any of the other situations described above.

*/

/*

**以下这些项的最大值:

**表中的列、索引中的列、视图的列、updateset从句的数量、select

**返回的字段数、GROUP BY ORDER BY的字段数、INSERTvalues从句

*/

#ifndef SQLITE_MAX_COLUMN

# define SQLITE_MAX_COLUMN 2000

#endif

 

/*

** The maximum length of a single SQL statement in bytes.

**

** It used to be the case that setting this value to zero would

** turn the limit off.  That is no longer true.  It is not possible

** to turn this limit off.

*/

//SQL语句的最大长度

#ifndef SQLITE_MAX_SQL_LENGTH

# define SQLITE_MAX_SQL_LENGTH 1000000000

#endif

 

/*

** The maximum depth of an expression tree. This is limited to

** some extent by SQLITE_MAX_SQL_LENGTH. But sometime you might

** want to place more severe limits on the complexity of an

** expression.

**

** A value of 0 used to mean that the limit was not enforced.

** But that is no longer true.  The limit is now strictly enforced

** at all times.

*/

//解释树的最大深度

#ifndef SQLITE_MAX_EXPR_DEPTH

# define SQLITE_MAX_EXPR_DEPTH 1000

#endif

 

/*

** The maximum number of terms in a compound SELECT statement.

** The code generator for compound SELECT statements does one

** level of recursion for each term.  A stack overflow can result

** if the number of terms is too large.  In practice, most SQL

** never has more than 3 or 4 terms.  Use a value of 0 to disable

** any limit on the number of terms in a compount SELECT.

*/

//复合SQL语句的最大项数

#ifndef SQLITE_MAX_COMPOUND_SELECT

# define SQLITE_MAX_COMPOUND_SELECT 500

#endif

  • SQLite源码剖析(1)
            
    
    博客分类: 数据库 SQLiteSQLSQL ServerOS嵌入式 
  • 大小: 6 KB