Skip to content

Commit 17b10fd

Browse files
Merge pull request #52 from Reim-developer/dev
Implement `SQLite` & FFI Test
2 parents b66fd7e + d8d1a94 commit 17b10fd

10 files changed

Lines changed: 135 additions & 4 deletions

File tree

.github/workflows/linux_dev.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ jobs:
3232
sudo apt-get update
3333
sudo apt-get install -y \
3434
git cmake make ccache \
35-
clang
35+
clang sqlite3
3636
3737
- name: Install Qt
3838
uses: jurplel/install-qt-action@v4

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
compile_commands.json
22
build
3-
.cache
3+
.cache
4+
*.db

CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ set(CMAKE_C_STANDARD_REQUIRED 99)
66

77
set(CMAKE_AUTOMOC ON)
88
find_package(Qt6 REQUIRED COMPONENTS Widgets Gui Core)
9+
find_package(SQLite3 REQUIRED)
910

1011
set(RUST_BACKEND_DIR ${CMAKE_CURRENT_SOURCE_DIR}/src/back_end)
1112

@@ -35,6 +36,7 @@ target_link_libraries(
3536
PRIVATE Qt6::Widgets
3637
PRIVATE Qt6::Core
3738
PRIVATE Qt6::Gui
39+
PRIVATE SQLite::SQLite3
3840
PRIVATE ${RUST_BACKEND_LIB}
3941
)
4042

scripts/linux_deploy.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ nproc=$(nproc)
88
install_dependency() {
99
sudo apt-get update
1010
sudo apt-get install -y git cmake \
11-
clang
11+
clang sqlite3
1212
}
1313

1414
function check() {

src/back_end/Cargo.lock

Lines changed: 50 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/back_end/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,6 @@ panic = "abort"
1212
[dependencies]
1313
dirs = "6.0.0"
1414
serde = { version = "1.0.219", features = ["derive"] }
15+
sqlite = "0.37.0"
1516
toml = "0.9.5"
1617
webbrowser = "1.0.5"

src/back_end/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ pub mod utils {
44
pub mod browser;
55
pub mod fs_utils;
66
pub mod memory;
7+
pub mod sqlite;
78
}
89

910
pub mod internal {

src/back_end/src/utils/sqlite.rs

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
use sqlite::Connection;
2+
use std::ffi::{CStr, c_char};
3+
4+
#[repr(C)]
5+
pub enum InitDatabaseStatus {
6+
Ok,
7+
CreateDatabaseFailed,
8+
CStrConvertFailed,
9+
ExecuteSqlFailed,
10+
}
11+
12+
const INIT_QUERY: &str = r"--sql
13+
CREATE TABLE IF NOT EXISTS clipboard_cache (
14+
id INTEGER PRIMARY KEY AUTOINCREMENT,
15+
content_hash TEXT NOT NULL UNIQUE,
16+
time DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
17+
content TEXT NOT NULL,
18+
type TEXT NOT NULL,
19+
is_pinned BOOLEAN NOT NULL DEFAULT FALSE
20+
);
21+
";
22+
23+
#[unsafe(no_mangle)]
24+
/// # Safety
25+
/// Careful with unsafe context & raw pointers.
26+
pub unsafe extern "C" fn raw_init_clipboard_cache(
27+
file_path: *const c_char,
28+
) -> InitDatabaseStatus {
29+
unsafe {
30+
use InitDatabaseStatus as Status;
31+
32+
let Ok(file_path_cstr) = CStr::from_ptr(file_path).to_str() else {
33+
return Status::CStrConvertFailed;
34+
};
35+
36+
let Ok(sql) = Connection::open(file_path_cstr) else {
37+
return Status::CreateDatabaseFailed;
38+
};
39+
40+
let is_err = sql.execute(INIT_QUERY).is_err();
41+
42+
if is_err {
43+
return Status::ExecuteSqlFailed;
44+
}
45+
46+
Status::Ok
47+
}
48+
}

tests-ffi/sqlite.cxx

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
2+
#include <cassert>
3+
#include <cstdint>
4+
using std::uint8_t;
5+
6+
extern "C" enum class InitDatabaseStatus : uint8_t {
7+
OK,
8+
CREATE_DATABASE_FAILED,
9+
C_STR_CONVERT_FAILED,
10+
EXECUTE_SQL_FAILED
11+
};
12+
13+
extern "C" InitDatabaseStatus raw_init_clipboard_cache(const char* path);
14+
15+
int main() {
16+
using Status = InitDatabaseStatus;
17+
18+
const char* test_path = "test_init.db";
19+
auto status = raw_init_clipboard_cache(test_path);
20+
21+
assert(status != Status::CREATE_DATABASE_FAILED);
22+
assert(status != Status::C_STR_CONVERT_FAILED);
23+
assert(status != Status::EXECUTE_SQL_FAILED);
24+
assert(status == Status::OK);
25+
26+
return 0;
27+
}

tests-ffi/tests.sh

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ function run_ffi_test() {
3232
local clang_cxx="clang++"
3333
local static_lib_path="../src/back_end/target/debug/libback_end.a"
3434
local build_dir="build"
35+
local sqlite_lib="sqlite3"
3536
check "$clang_cxx"
3637

3738
echo -e "\e[0;32m[+] Build backend:\e[0;37m"
@@ -46,7 +47,7 @@ function run_ffi_test() {
4647
build_output=$(basename "$file" ".cxx")
4748
file_name=$(basename "$file")
4849

49-
$clang_cxx "$file" "$static_lib_path" -o "$build_dir/$build_output"
50+
$clang_cxx "$file" -l"$sqlite_lib" "$static_lib_path" -o "$build_dir/$build_output"
5051
echo
5152
echo -e -n "\e[0;32m[+] Test for $file_name:\e[0;37m"
5253

0 commit comments

Comments
 (0)