Merge pull request #176 from penberg/sqlite3-improvements

This commit is contained in:
Pekka Enberg
2024-07-17 14:01:35 +03:00
committed by GitHub
8 changed files with 74 additions and 11 deletions

27
Cargo.lock generated
View File

@@ -526,6 +526,16 @@ version = "0.1.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c34f04666d835ff5d62e058c3995147c06f42fe86ff053337632bca83e42702d"
[[package]]
name = "env_filter"
version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a009aa4810eb158359dda09d0c87378e4bbb89b5a801f016885a4707ba24f7ea"
dependencies = [
"log",
"regex",
]
[[package]]
name = "env_logger"
version = "0.10.2"
@@ -539,6 +549,19 @@ dependencies = [
"termcolor",
]
[[package]]
name = "env_logger"
version = "0.11.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "38b35839ba51819680ba087cd351788c9a3c476841207e0b8cee0b04722343b9"
dependencies = [
"anstream",
"anstyle",
"env_filter",
"humantime",
"log",
]
[[package]]
name = "equivalent"
version = "1.0.1"
@@ -952,7 +975,7 @@ dependencies = [
"clap 4.5.8",
"cli-table",
"dirs",
"env_logger",
"env_logger 0.10.2",
"limbo_core",
"rustyline",
]
@@ -1006,7 +1029,9 @@ name = "limbo_sqlite3"
version = "0.0.0"
dependencies = [
"cbindgen",
"env_logger 0.11.3",
"limbo_core",
"log",
]
[[package]]

View File

@@ -16,4 +16,6 @@ doc = false
cbindgen = "0.24.0"
[dependencies]
env_logger = "0.11.3"
limbo_core = { path = "../core" }
log = "0.4.22"

View File

@@ -1,6 +1,7 @@
#![allow(clippy::missing_safety_doc)]
#![allow(non_camel_case_types)]
use log::trace;
use std::cell::RefCell;
use std::ffi;
use std::rc::Rc;
@@ -36,14 +37,19 @@ impl<'a> sqlite3_stmt<'a> {
}
}
static INIT_DONE: std::sync::Once = std::sync::Once::new();
#[no_mangle]
pub unsafe extern "C" fn sqlite3_initialize() -> ffi::c_int {
todo!();
INIT_DONE.call_once(|| {
env_logger::init();
});
SQLITE_OK
}
#[no_mangle]
pub unsafe extern "C" fn sqlite3_shutdown() -> ffi::c_int {
todo!();
SQLITE_OK
}
#[no_mangle]
@@ -51,6 +57,11 @@ pub unsafe extern "C" fn sqlite3_open(
filename: *const ffi::c_char,
db_out: *mut *mut sqlite3,
) -> ffi::c_int {
trace!("sqlite3_open");
let rc = sqlite3_initialize();
if rc != SQLITE_OK {
return rc;
}
if filename.is_null() {
return SQLITE_MISUSE;
}
@@ -83,11 +94,13 @@ pub unsafe extern "C" fn sqlite3_open_v2(
_flags: ffi::c_int,
_z_vfs: *const ffi::c_char,
) -> ffi::c_int {
trace!("sqlite3_open_v2");
sqlite3_open(filename, db_out)
}
#[no_mangle]
pub unsafe extern "C" fn sqlite3_close(db: *mut sqlite3) -> ffi::c_int {
trace!("sqlite3_close");
if db.is_null() {
return SQLITE_OK;
}
@@ -97,6 +110,7 @@ pub unsafe extern "C" fn sqlite3_close(db: *mut sqlite3) -> ffi::c_int {
#[no_mangle]
pub unsafe extern "C" fn sqlite3_close_v2(db: *mut sqlite3) -> ffi::c_int {
trace!("sqlite3_close_v2");
sqlite3_close(db)
}
@@ -759,15 +773,17 @@ pub unsafe extern "C" fn sqlite3_complete(_sql: *const std::ffi::c_char) -> ffi:
#[no_mangle]
pub unsafe extern "C" fn sqlite3_threadsafe() -> ffi::c_int {
todo!();
1
}
#[no_mangle]
pub unsafe extern "C" fn sqlite3_libversion() -> *const std::ffi::c_char {
todo!();
ffi::CStr::from_bytes_with_nul(b"3.42.0\0")
.unwrap()
.as_ptr()
}
#[no_mangle]
pub unsafe extern "C" fn sqlite3_libversion_number() -> ffi::c_int {
todo!();
3042000
}

View File

@@ -15,6 +15,7 @@ CFLAGS = -g -Wall -std=c17 -MMD -MP
LIBS ?= -lsqlite3
OBJS += main.o
OBJS += test-aux.o
OBJS += test-close.o
OBJS += test-open.o
OBJS += test-prepare.o

View File

@@ -1,3 +1,5 @@
extern void test_libversion();
extern void test_libversion_number();
extern void test_open_misuse();
extern void test_open_not_found();
extern void test_open_existing();
@@ -6,6 +8,8 @@ extern void test_prepare_misuse();
int main(int argc, char *argv[])
{
test_libversion();
test_libversion_number();
test_open_misuse();
test_open_not_found();
test_open_existing();

11
sqlite3/tests/test-aux.c Normal file
View File

@@ -0,0 +1,11 @@
#include "check.h"
#include <sqlite3.h>
void test_libversion(void) {
sqlite3_libversion();
}
void test_libversion_number(void) {
sqlite3_libversion_number();
}

View File

@@ -7,9 +7,10 @@
void test_open_misuse(void)
{
CHECK_EQUAL(SQLITE_MISUSE, sqlite3_open(NULL, NULL));
// TODO: SIGSEGV with sqlite3
// CHECK_EQUAL(SQLITE_MISUSE, sqlite3_open(NULL, NULL));
CHECK_EQUAL(SQLITE_MISUSE, sqlite3_open("local.db", NULL));
// CHECK_EQUAL(SQLITE_MISUSE, sqlite3_open("local.db", NULL));
}
void test_open_not_found(void)

View File

@@ -12,13 +12,16 @@ void test_prepare_misuse(void)
CHECK_EQUAL(SQLITE_OK, sqlite3_open("../../testing/testing.db", &db));
// Database handle is NULL.
CHECK_EQUAL(SQLITE_MISUSE, sqlite3_prepare_v2(NULL, "SELECT 1", -1, NULL, NULL));
// TODO: SIGSEGV with sqlite3
// CHECK_EQUAL(SQLITE_MISUSE, sqlite3_prepare_v2(NULL, "SELECT 1", -1, NULL, NULL));
// Output statement is NULL.
CHECK_EQUAL(SQLITE_MISUSE, sqlite3_prepare_v2(db, "SELECT 1", -1, NULL, NULL));
// TODO: SIGSEGV with sqlite3
// CHECK_EQUAL(SQLITE_MISUSE, sqlite3_prepare_v2(db, "SELECT 1", -1, NULL, NULL));
// SQL string length is too short, truncating the statement.
CHECK_EQUAL(SQLITE_MISUSE, sqlite3_prepare_v2(db, "SELECT 1", 7, NULL, NULL));
// TODO: SIGSEGV with sqlite3
// CHECK_EQUAL(SQLITE_MISUSE, sqlite3_prepare_v2(db, "SELECT 1", 7, NULL, NULL));
CHECK_EQUAL(SQLITE_OK, sqlite3_close(db));
}