File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 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
Original file line number Diff line number Diff line change 11compile_commands.json
22build
3- .cache
3+ .cache
4+ * .db
Original file line number Diff line number Diff line change @@ -6,6 +6,7 @@ set(CMAKE_C_STANDARD_REQUIRED 99)
66
77set (CMAKE_AUTOMOC ON )
88find_package (Qt6 REQUIRED COMPONENTS Widgets Gui Core )
9+ find_package (SQLite3 REQUIRED )
910
1011set (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
Original file line number Diff line number Diff line change @@ -8,7 +8,7 @@ nproc=$(nproc)
88install_dependency () {
99 sudo apt-get update
1010 sudo apt-get install -y git cmake \
11- clang
11+ clang sqlite3
1212}
1313
1414function check() {
Original file line number Diff line number Diff line change @@ -12,5 +12,6 @@ panic = "abort"
1212[dependencies ]
1313dirs = " 6.0.0"
1414serde = { version = " 1.0.219" , features = [" derive" ] }
15+ sqlite = " 0.37.0"
1516toml = " 0.9.5"
1617webbrowser = " 1.0.5"
Original file line number Diff line number Diff 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
910pub mod internal {
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff 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
You can’t perform that action at this time.
0 commit comments