mirror of
https://github.com/patarapolw/cjdic2.git
synced 2026-11-08 17:35:18 +00:00
test that sqlite actually runs
This commit is contained in:
1 parent
d0a12a57ae
commit
2d4e686d02
6 files changed
+252
-7
No files matched your search
@@ -0,0 +1,29 @@
|
||||
use serde::Serialize;
|
||||
|
||||
use crate::state;
|
||||
|
||||
#[derive(Serialize)]
|
||||
pub struct SearchRow {
|
||||
a: i32,
|
||||
b: i32,
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
pub fn get_db(state: tauri::State<state::AppState>) -> Result<Vec<SearchRow>, String> {
|
||||
let conn = state.conn.lock().map_err(|e| e.to_string())?;
|
||||
let mut stmt = conn.prepare("SELECT a, b FROM test").map_err(|e| e.to_string())?;
|
||||
|
||||
let rows = stmt.query_map([], |r| {
|
||||
Ok(SearchRow {
|
||||
a: r.get(0)?,
|
||||
b: r.get(1)?,
|
||||
})
|
||||
}).map_err(|e| e.to_string())?;
|
||||
|
||||
let mut out = Vec::new();
|
||||
for r in rows {
|
||||
out.push(r.map_err(|e| e.to_string())?);
|
||||
}
|
||||
|
||||
Ok(out)
|
||||
}
|
||||
+61
-3
@@ -1,3 +1,10 @@
|
||||
use tauri::Manager;
|
||||
use rusqlite::Connection;
|
||||
use std::sync::Mutex;
|
||||
|
||||
mod db;
|
||||
mod state;
|
||||
|
||||
// Learn more about Tauri commands at https://tauri.app/develop/calling-rust/
|
||||
#[tauri::command]
|
||||
fn greet(name: &str) -> String {
|
||||
@@ -8,7 +15,58 @@ fn greet(name: &str) -> String {
|
||||
pub fn run() {
|
||||
tauri::Builder::default()
|
||||
.plugin(tauri_plugin_opener::init())
|
||||
.invoke_handler(tauri::generate_handler![greet])
|
||||
.run(tauri::generate_context!())
|
||||
.expect("error while running tauri application");
|
||||
.setup(|app| {
|
||||
let cfg_dir = app.path().app_config_dir();
|
||||
let app_dir = cfg_dir
|
||||
.unwrap_or_else(|_| std::env::current_dir().unwrap_or_else(|_| std::path::PathBuf::from(".")));
|
||||
|
||||
std::fs::create_dir_all(&app_dir)?;
|
||||
|
||||
let db_path = app_dir.join("cjdic.db");
|
||||
let is_new = !db_path.exists();
|
||||
println!("{:?}", db_path);
|
||||
|
||||
let conn = Connection::open(&db_path)?;
|
||||
|
||||
if is_new {
|
||||
conn.execute_batch("CREATE TABLE test (a, b);")?;
|
||||
let mut stmt = conn.prepare("INSERT INTO test VALUES (?1, ?2)")?;
|
||||
stmt.insert([1, 2])?;
|
||||
stmt.insert([3, 4])?;
|
||||
}
|
||||
|
||||
app.manage(state::AppState{
|
||||
conn: Mutex::new(conn),
|
||||
});
|
||||
Ok(())
|
||||
})
|
||||
.invoke_handler(tauri::generate_handler![greet, db::get_db])
|
||||
.build(tauri::generate_context!())
|
||||
.expect("error while running tauri application")
|
||||
.run(|_app_handle, event| match event {
|
||||
tauri::RunEvent::ExitRequested { .. } => {
|
||||
// api.prevent_exit();
|
||||
// let handle = app_handle.clone();
|
||||
|
||||
// if let Some(state) = app_handle.try_state::<state::AppState>() {
|
||||
// if let Ok(conn) = state.conn.lock() {
|
||||
// if let Err(e) = (|| -> Result<(), rusqlite::Error> {
|
||||
// conn.execute_batch("PRAGMA optimize;")?;
|
||||
// conn.execute("PRAGMA wal_checkpoint(FULL);", [])?;
|
||||
|
||||
// let (_busy, _log, _checkpointed): (i32, i32, i32) =
|
||||
// conn.query_row("PRAGMA wal_checkpoint(FULL);", [], |row| {
|
||||
// Ok((row.get(0)?, row.get(1)?, row.get(2)?))
|
||||
// })?;
|
||||
// Ok(())
|
||||
// })() {
|
||||
// eprintln!("Shutdown SQL failed: {e}");
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
|
||||
// handle.exit(0);
|
||||
}
|
||||
_ => {}
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
use rusqlite::Connection;
|
||||
use std::sync::Mutex;
|
||||
|
||||
pub struct AppState {
|
||||
pub conn: Mutex<Connection>
|
||||
}
|
||||
Reference in new issue
Block a user