我找到了一种从源代码编译 sqlite3 的方法,并提供了附加功能extension_functions.c
.
Note:
这次我展示了使用附加功能编译 sqlite 的相当肮脏和简单的方法,因为我没有成功地以正确的方式做到这一点。
但请记住,正如 @ngreen 上面所说,准备一个全新的合并部分来添加自定义功能可能会更好。
这就是sqlite本身的设计方式。
1.下载sqlite源码
https://www.sqlite.org/download.html https://www.sqlite.org/download.html
选择合并之一,最好使用autoconf版本。
例如,这里是3.33.0版本的下载链接。
https://www.sqlite.org/2020/sqlite-autoconf-3330000.tar.gz https://www.sqlite.org/2020/sqlite-autoconf-3330000.tar.gz
curl -O https://www.sqlite.org/2020/sqlite-autoconf-3330000.tar.gz
tar -xzvf sqlite-autoconf-3330000.tar.gz
cd sqlite-autoconf-3330000
2. 下载extension_functions.c
在此网址列出。
https://sqlite.org/contrib https://sqlite.org/contrib
实际网址:
https://sqlite.org/contrib/download/extension-functions.c?get=25 https://sqlite.org/contrib/download/extension-functions.c?get=25
curl -o extension_functions.c https://sqlite.org/contrib/download/extension-functions.c?get=25
3.配置编译
我们可以指定--prefix
确定构建内容的目的地的选项。
./configure --prefix=/usr/local/sqlite/3.33.0
此时可以将其他配置时间选项指定为环境变量。
Check https://www.sqlite.org/draft/compile.html https://www.sqlite.org/draft/compile.html更多细节。
以下是启用 JSON 和 RTree 索引功能的示例。
CPPFLAGS="-DSQLITE_ENABLE_JSON1=1 -DSQLITE_ENABLE_RTREE=1" ./configure --prefix=/usr/local/sqlite/3.33.0
还可以指定 autoconf 选项。
CPPFLAGS="-DSQLITE_ENABLE_JSON1=1 -DSQLITE_ENABLE_RTREE=1" ./configure --prefix=/usr/local/sqlite/3.33.0 --enable-dynamic-extensions
我在官方网站上找不到有关这些选项的任何文档,但在配置脚本本身中找到了一些内容。
Optional Features:
--disable-option-checking ignore unrecognized --enable/--with options
--disable-FEATURE do not include FEATURE (same as --enable-FEATURE=no)
--enable-FEATURE[=ARG] include FEATURE [ARG=yes]
--enable-silent-rules less verbose build output (undo: "make V=1")
--disable-silent-rules verbose build output (undo: "make V=0")
--disable-largefile omit support for large files
--enable-dependency-tracking
do not reject slow dependency extractors
--disable-dependency-tracking
speeds up one-time build
--enable-shared[=PKGS] build shared libraries [default=yes]
--enable-static[=PKGS] build static libraries [default=yes]
--enable-fast-install[=PKGS]
optimize for fast installation [default=yes]
--disable-libtool-lock avoid locking (might break parallel builds)
--enable-editline use BSD libedit
--enable-readline use readline
--enable-threadsafe build a thread-safe library [default=yes]
--enable-dynamic-extensions
support loadable extensions [default=yes]
--enable-fts4 include fts4 support [default=yes]
--enable-fts3 include fts3 support [default=no]
--enable-fts5 include fts5 support [default=yes]
--enable-json1 include json1 support [default=yes]
--enable-rtree include rtree support [default=yes]
--enable-session enable the session extension [default=no]
--enable-debug build with debugging features enabled [default=no]
--enable-static-shell statically link libsqlite3 into shell tool
[default=yes]
仅供参考,这是 Homebrew 中使用的默认安装脚本。也许确定应指定哪个选项会很有用。
def install
ENV.append "CPPFLAGS", "-DSQLITE_ENABLE_COLUMN_METADATA=1"
# Default value of MAX_VARIABLE_NUMBER is 999 which is too low for many
# applications. Set to 250000 (Same value used in Debian and Ubuntu).
ENV.append "CPPFLAGS", "-DSQLITE_MAX_VARIABLE_NUMBER=250000"
ENV.append "CPPFLAGS", "-DSQLITE_ENABLE_RTREE=1"
ENV.append "CPPFLAGS", "-DSQLITE_ENABLE_FTS3=1 -DSQLITE_ENABLE_FTS3_PARENTHESIS=1"
ENV.append "CPPFLAGS", "-DSQLITE_ENABLE_JSON1=1"
args = %W[
--prefix=#{prefix}
--disable-dependency-tracking
--enable-dynamic-extensions
--enable-readline
--disable-editline
--enable-session
]
system "./configure", *args
system "make", "install"
end
4.消除冲突
现在我们必须修改extension_functions.c
以避免在将它们编译在一起之前与 sqlite 的源代码发生冲突。
Open extension_functions.c
并将第 123 ~ 128 行替换为单行SQLITE_EXTENSION_INIT1
.
#ifdef COMPILE_SQLITE_EXTENSIONS_AS_LOADABLE_MODULE
#include "sqlite3ext.h"
SQLITE_EXTENSION_INIT1
#else
#include "sqlite3.h"
#endif
↓
SQLITE_EXTENSION_INIT1
5. 启用扩展功能
我们需要插入一些行shell.c
导入并启用扩展功能。
Open shell.c
, 搜索static void open_db
并插入#include "extension_functions.c"
在上面的行。
#include "extension_functions.c"
static void open_db(ShellState *p, int openFlags){
然后搜索sqlite3_shathree_init(p->db, 0, 0);
并插入sqlite3_extension_init(p->db, 0, 0);
在 init 函数的底部。
#endif
sqlite3_fileio_init(p->db, 0, 0);
sqlite3_shathree_init(p->db, 0, 0);
sqlite3_completion_init(p->db, 0, 0);
sqlite3_uint_init(p->db, 0, 0);
sqlite3_decimal_init(p->db, 0, 0);
sqlite3_ieee_init(p->db, 0, 0);
sqlite3_extension_init(p->db, 0, 0);
6. 编译
最后准备编译包含扩展函数的 sqlite。
make install
这需要一段时间,完成后,将在配置时指定的目的地生成分发文件--prefix
option.
# Now we can use extension_functions without loading it manually.
$ /usr/local/sqlite/3.33.0/bin/sqlite3
sqlite> select cos(10);
-0.839071529076452