mirror of
https://github.com/DragonOS-Community/DragonOS.git
synced 2025-06-18 16:26:31 +00:00
移植sqlite3,并修复一些bug (#323)
* bugfix: 程序加载器映射内存时,计算要映射的大小不正确的问题。 * 修正brk系统调用不符合规范的地方 * bugfix: 修正fat文件系统未能正确的扩展文件大小的bug * 增加fcntl系统调用 * 移植sqlite3
This commit is contained in:
2
user/apps/test_sqlite3/.gitignore
vendored
Normal file
2
user/apps/test_sqlite3/.gitignore
vendored
Normal file
@ -0,0 +1,2 @@
|
||||
sqlite*.zip
|
||||
sqlite-*/
|
44
user/apps/test_sqlite3/Makefile
Normal file
44
user/apps/test_sqlite3/Makefile
Normal file
@ -0,0 +1,44 @@
|
||||
CC=$(DragonOS_GCC)/x86_64-elf-gcc
|
||||
LD=ld
|
||||
OBJCOPY=objcopy
|
||||
SQLITE_FILENAME=sqlite-amalgamation-3420000
|
||||
SQLITE3_DIR=$(shell pwd)/$(SQLITE_FILENAME)
|
||||
|
||||
RELIBC_OPT=$(DADK_BUILD_CACHE_DIR_RELIBC_0_1_0)
|
||||
CFLAGS=-I $(RELIBC_OPT)/include -I $(SQLITE3_DIR) -D__dragonos__ -DSQLITE_THREADSAFE=0 -DSQLITE_OMIT_FLOATING_POINT -DSQLITE_OMIT_LOAD_EXTENSION -DSQLITE_DEBUG
|
||||
|
||||
tmp_output_dir=$(ROOT_PATH)/bin/tmp/user
|
||||
output_dir=$(DADK_BUILD_CACHE_DIR_TEST_SQLITE3_3_42_0)
|
||||
|
||||
LIBC_OBJS:=$(shell find $(RELIBC_OPT)/lib -name "*.o" | sort )
|
||||
LIBC_OBJS+=$(RELIBC_OPT)/lib/libc.a
|
||||
|
||||
.PHONY: all clean download_sqlite3 __download_sqlite3
|
||||
|
||||
|
||||
all: main.o sqlite3.o
|
||||
mkdir -p $(tmp_output_dir)
|
||||
|
||||
$(LD) -b elf64-x86-64 -z muldefs -o $(tmp_output_dir)/test_sqlite3 $(shell find . -name "*.o") $(LIBC_OBJS)
|
||||
|
||||
$(OBJCOPY) -I elf64-x86-64 -R ".eh_frame" -R ".comment" -O elf64-x86-64 $(tmp_output_dir)/test_sqlite3 $(output_dir)/test_sqlite3.elf
|
||||
mv $(output_dir)/test_sqlite3.elf $(output_dir)/test_sqlite3
|
||||
|
||||
main.o: main.c
|
||||
$(CC) $(CFLAGS) -c main.c -o main.o
|
||||
|
||||
sqlite3.o: $(SQLITE3_DIR)/sqlite3.c
|
||||
$(CC) $(CFLAGS) -c $(SQLITE3_DIR)/sqlite3.c -o sqlite3.o
|
||||
|
||||
__download_sqlite3:
|
||||
@echo "Download sqlite3 from https://mirrors.ringotek.cn/pub/third_party/sqlite/$(SQLITE_FILENAME).zip"
|
||||
@wget https://mirrors.ringotek.cn/pub/third_party/sqlite/$(SQLITE_FILENAME).zip || (@echo "Download sqlite3 failed" && rm $(SQLITE_FILENAME).zip && exit 1)
|
||||
@unzip -o $(SQLITE_FILENAME).zip || (@echo "Unzip sqlite3 failed" && exit 1)
|
||||
@rm $(SQLITE_FILENAME).zip || (@echo "Remove $(SQLITE_FILENAME).zip failed" && exit 1)
|
||||
|
||||
download_sqlite3:
|
||||
# 如果文件夹不存在,则下载,否则不下载
|
||||
@test -d $(SQLITE3_DIR) || $(MAKE) __download_sqlite3
|
||||
|
||||
clean:
|
||||
rm -f *.o
|
99
user/apps/test_sqlite3/main.c
Normal file
99
user/apps/test_sqlite3/main.c
Normal file
@ -0,0 +1,99 @@
|
||||
// This is a test program for sqlite3.
|
||||
// We take it from rcore-os/arceos, thanks to @rcore-os community.
|
||||
#include <sqlite3.h>
|
||||
#include <stddef.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
int callback(void *NotUsed, int argc, char **argv, char **azColName)
|
||||
{
|
||||
NotUsed = NULL;
|
||||
|
||||
for (int i = 0; i < argc; ++i) {
|
||||
printf("%s = %s\n", azColName[i], (argv[i] ? argv[i] : "NULL"));
|
||||
}
|
||||
|
||||
printf("\n");
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void exec(sqlite3 *db, char *sql)
|
||||
{
|
||||
printf("sqlite exec:\n %s\n", sql);
|
||||
char *errmsg = NULL;
|
||||
int rc = sqlite3_exec(db, sql, NULL, NULL, &errmsg);
|
||||
if (rc != SQLITE_OK) {
|
||||
printf("sqlite exec error: %s\n", errmsg);
|
||||
}
|
||||
}
|
||||
|
||||
void query(sqlite3 *db, char *sql)
|
||||
{
|
||||
printf("sqlite query:\n %s\n", sql);
|
||||
char *errmsg = NULL;
|
||||
int rc = sqlite3_exec(db, sql, callback, NULL, &errmsg);
|
||||
|
||||
if (rc != SQLITE_OK) {
|
||||
printf("%s\n", errmsg);
|
||||
}
|
||||
}
|
||||
|
||||
void query_test(sqlite3 *db, const char *args)
|
||||
{
|
||||
puts("======== init user table ========");
|
||||
exec(db, "create table user("
|
||||
"id INTEGER PRIMARY KEY AUTOINCREMENT,"
|
||||
"username TEXT,"
|
||||
"password TEXT"
|
||||
")");
|
||||
|
||||
puts("======== insert user 1, 2, 3 into user table ========");
|
||||
|
||||
char cmd[256] = {0};
|
||||
sprintf(cmd,
|
||||
"insert into user (username, password) VALUES ('%s_1', 'password1'), ('%s_2', "
|
||||
"'password2'), ('%s_3', 'password3')",
|
||||
args, args, args);
|
||||
exec(db, cmd);
|
||||
|
||||
puts("======== select all ========");
|
||||
query(db, "select * from user");
|
||||
|
||||
puts("======== select id = 2 ========");
|
||||
query(db, "select * from user where id = 2");
|
||||
}
|
||||
|
||||
void memory()
|
||||
{
|
||||
sqlite3 *db;
|
||||
printf("sqlite open memory\n");
|
||||
int ret = sqlite3_open(":memory:", &db);
|
||||
printf("sqlite open memory status %d \n", ret);
|
||||
|
||||
query_test(db, "memory");
|
||||
}
|
||||
|
||||
void file()
|
||||
{
|
||||
sqlite3 *db;
|
||||
int ret = sqlite3_open("file.sqlite", &db);
|
||||
printf("sqlite open /file.sqlite status %d \n", ret);
|
||||
|
||||
if (ret != 0) {
|
||||
printf("sqlite open error");
|
||||
return;
|
||||
}
|
||||
|
||||
query_test(db, "file");
|
||||
sqlite3_close(db);
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
printf("sqlite version: %s\n", sqlite3_libversion());
|
||||
|
||||
memory();
|
||||
file();
|
||||
return 0;
|
||||
}
|
Reference in New Issue
Block a user