adding files to database works

This commit is contained in:
Iaphetes 2025-03-16 19:04:14 +01:00
parent dcfb500a5f
commit 852ab87b0b
4 changed files with 154 additions and 52 deletions

View file

@ -2,18 +2,31 @@ use std::{
fs::File,
io::{Error, Read},
path::Path,
str::FromStr,
};
use crate::get_basepath;
use serde::{Deserialize, Serialize};
use sqlx::{migrate::MigrateDatabase, Sqlite, SqlitePool};
use tauri::{ipc::RuntimeCapability, App, AssetResolver, Manager, Url};
struct DBFileEntry {
id: u64,
relative_file_path: String,
file_name: String,
}
#[derive(Debug, Serialize, Deserialize)]
pub enum DBError {
DatabaseLocationError(String),
DatabaseConnectionError(String),
DatabaseQueryError(String),
}
async fn populate_file_db(db: &SqlitePool) {}
fn get_database_path(app_handle: &tauri::AppHandle) -> Result<String, ()> {
fn get_database_path(app_handle: &tauri::AppHandle) -> Result<String, DBError> {
if let Some(basepath) = get_basepath(app_handle.clone()) {
let db_path = Path::new("Documents")
.join(Path::new("Knowledgebase"))
.join("db.sqlite");
let db_path = Path::new(&basepath).join("db.sqlite");
let resolved_db_path = match app_handle
.path()
@ -21,52 +34,44 @@ fn get_database_path(app_handle: &tauri::AppHandle) -> Result<String, ()> {
{
Ok(resolved_knowledgebase_path) => resolved_knowledgebase_path,
Err(_) => return Err(()),
Err(e) => return Err(DBError::DatabaseLocationError(e.to_string())),
};
let unicode_db_path = &resolved_db_path.to_string_lossy().to_owned();
let unicode_db_path = resolved_db_path.to_string_lossy().as_ref().to_string();
return Ok(unicode_db_path);
}
Err(())
Err(DBError::DatabaseLocationError(
"No knowledgebase defined".to_string(),
))
}
#[tauri::command]
pub async fn initialize_database(app_handle: tauri::AppHandle) {
if let Some(basepath) = get_basepath(app_handle.clone()) {
let db_path = Path::new("Documents")
.join(Path::new("Knowledgebase"))
.join("db.sqlite");
let resolved_db_path = match app_handle
.path()
.resolve(db_path, tauri::path::BaseDirectory::Home)
{
Ok(resolved_knowledgebase_path) => resolved_knowledgebase_path,
Err(_) => return,
};
let unicode_db_path = &resolved_db_path.to_string_lossy().to_owned();
if !Sqlite::database_exists(&unicode_db_path)
.await
.unwrap_or(false)
{
match Sqlite::create_database(&unicode_db_path).await {
Ok(_) => println!("Create db success"),
Err(error) => panic!("error: {}", error),
}
pub async fn initialize_database(app_handle: tauri::AppHandle) -> Result<(), DBError> {
let unicode_db_path = get_database_path(&app_handle)?;
if !Sqlite::database_exists(&unicode_db_path)
.await
.unwrap_or(false)
{
match Sqlite::create_database(&unicode_db_path).await {
Ok(_) => println!("Create db success"),
Err(error) => panic!("error: {}", error),
}
let db = SqlitePool::connect(unicode_db_path).await.unwrap();
let result = sqlx::query(
"CREATE TABLE IF NOT EXISTS files
}
let db = SqlitePool::connect(&unicode_db_path)
.await
.map_err(|e| DBError::DatabaseConnectionError(e.to_string()))?;
let _ = sqlx::query(
"CREATE TABLE IF NOT EXISTS files
(
id INTEGER PRIMARY KEY NOT NULL,
relative_path TEXT NOT NULL,
file_name TEXT NOT NULL
);",
)
.execute(&db)
.await
.unwrap();
)
.execute(&db)
.await
.map_err(|e| DBError::DatabaseQueryError(e.to_string()))?;
let result = sqlx::query(
"CREATE TABLE IF NOT EXISTS file_diffs
let _ = sqlx::query(
"CREATE TABLE IF NOT EXISTS file_diffs
(
id INTEGER NOT NULL,
client_id INTEGER NOT NULL,
@ -77,14 +82,79 @@ pub async fn initialize_database(app_handle: tauri::AppHandle) {
PRIMARY KEY (id, client_id)
)
;",
)
.execute(&db)
.await
.unwrap();
}
)
.execute(&db)
.await
.map_err(|e| DBError::DatabaseQueryError(e.to_string()))?;
db.close().await;
Ok(())
}
#[tauri::command]
async fn store_diff(file_path: String, file_name: String, diff: String) {}
async fn store_diff(
app_handle: &tauri::AppHandle,
file_path: String,
file_name: String,
diff: String,
) -> Result<(), DBError> {
Ok(())
}
async fn add_file(file_path: String, file_name: String) {}
async fn get_file_id(
app_handle: &tauri::AppHandle,
relative_file_path: &str,
file_name: &str,
) -> Result<i32, DBError> {
let unicode_db_path = get_database_path(&app_handle)?;
let db = SqlitePool::connect(&unicode_db_path)
.await
.map_err(|e| DBError::DatabaseConnectionError(e.to_string()))?;
let (id, _, _): (i32, String, String) = sqlx::query_as(&format!(
"
SELECT *
FROM files
WHERE relative_path IS '{relative_file_path}'
AND file_name IS '{file_name}'
"
))
.fetch_one(&db)
.await
.map_err(|e| DBError::DatabaseQueryError(e.to_string()))?;
db.close().await;
Ok(id)
}
pub async fn add_file(
app_handle: &tauri::AppHandle,
relative_file_path: &str,
file_name: &str,
) -> Result<(), DBError> {
if get_file_id(app_handle, relative_file_path, file_name)
.await
.is_ok()
{
return Ok(());
} else {
println!(
"{:?}",
get_file_id(app_handle, relative_file_path, file_name).await
);
}
let unicode_db_path = get_database_path(&app_handle)?;
let db = SqlitePool::connect(&unicode_db_path)
.await
.map_err(|e| DBError::DatabaseConnectionError(e.to_string()))?;
let _ = sqlx::query(&format!(
"
INSERT INTO
files (relative_path, file_name)
VALUES
('{relative_file_path}', '{file_name}');"
))
.execute(&db)
.await
.map_err(|e| DBError::DatabaseQueryError(e.to_string()))?;
db.close().await;
Ok(())
}