update sqlx version; use sqlite pool as state; automatic code format

This commit is contained in:
2025-10-26 22:35:04 +01:00
parent 094ec0aa09
commit 9b45042277
6 changed files with 332 additions and 207 deletions

View File

@@ -1,52 +1,80 @@
// Learn more about Tauri commands at https://tauri.app/develop/calling-rust/
use sqlx::sqlite::{self, SqlitePool};
use sqlx::{migrate::MigrateDatabase, sqlite::SqlitePoolOptions, Pool, Sqlite};
use tauri::{App, Manager};
#[tauri::command]
fn greet(name: &str) -> String {
format!("Hello, {}! You've been greeted from Rust!", name)
}
// #[tauri::command]
// fn greet(name: &str) -> String {
// format!("Hello, {}! You've been greeted from Rust!", name)
// }
// Beispiel: SQLite-Version abfragen
#[tauri::command]
async fn db_version(app_handle: tauri::AppHandle) -> Result<String, String> {
use tauri::Manager;
let base_dir = app_handle.path().app_data_dir().map_err(|e| format!("Konnte AppData-Verzeichnis nicht bestimmen: {}", e))?;
let db_path = base_dir.join("paths.sqlite");
println!("{:?}", db_path.to_str());
if let Some(parent) = db_path.parent() {
std::fs::create_dir_all(parent).map_err(|e| format!("Konnte Datenbankverzeichnis nicht erstellen: {}", e))?;
}
let db_opts = sqlite::SqliteConnectOptions::new()
.filename(&db_path)
.create_if_missing(true);
let pool = SqlitePool::connect_with(db_opts)
.await
.map_err(|e| e.to_string())?;
sqlx::migrate!("./migrations").run(&pool).await.map_err(|e| e.to_string())?;
async fn db_version(state: tauri::State<'_, AppState>) -> Result<String, String> {
let pool = &state.db;
let row: (String,) = sqlx::query_as("SELECT sqlite_version()")
.fetch_one(&pool)
.fetch_one(pool)
.await
.map_err(|e| e.to_string())?;
Ok(row.0)
}
async fn setup_db(app: &App) -> Db {
let mut path = app.path().app_data_dir().expect("failed to get data_dir");
match std::fs::create_dir_all(path.clone()) {
Ok(_) => {}
Err(err) => {
panic!("error creating directory {}", err);
}
};
path.push("paths.sqlite");
Sqlite::create_database(
format!(
"sqlite:{}",
path.to_str().expect("path should be something")
)
.as_str(),
)
.await
.expect("failed to create database");
let db = SqlitePoolOptions::new()
.connect(path.to_str().unwrap())
.await
.unwrap();
sqlx::migrate!("./migrations").run(&db).await.unwrap();
db
}
type Db = Pool<Sqlite>;
struct AppState {
db: Db,
}
#[cfg_attr(mobile, tauri::mobile_entry_point)]
pub fn run() {
tauri::Builder::default()
.plugin(tauri_plugin_opener::init())
.invoke_handler(tauri::generate_handler![greet, db_version])
.invoke_handler(tauri::generate_handler![
db_version
])
.setup(|app| {
tauri::async_runtime::block_on(async move {
let db = setup_db(&app).await;
app.manage(AppState { db });
});
Ok(())
})
.run(tauri::generate_context!())
.expect("error while running tauri application");
}
.expect("error building the app");}
// Hinweis: Für produktive Nutzung sollte der Pool als State eingebunden werden,
// damit er nicht bei jedem Aufruf neu erstellt wird. Siehe Tauri-Doku:
// https://tauri.app/v1/guides/features/state/