modified database scheme a bit. continued work on saving diffs to database

This commit is contained in:
Iaphetes 2025-03-19 07:14:36 +01:00
parent 852ab87b0b
commit 75cd9752a4
51 changed files with 61 additions and 18 deletions

View file

@ -22,7 +22,6 @@ pub enum DBError {
DatabaseConnectionError(String),
DatabaseQueryError(String),
}
async fn populate_file_db(db: &SqlitePool) {}
fn get_database_path(app_handle: &tauri::AppHandle) -> Result<String, DBError> {
if let Some(basepath) = get_basepath(app_handle.clone()) {
@ -74,9 +73,11 @@ pub async fn initialize_database(app_handle: tauri::AppHandle) -> Result<(), DBE
"CREATE TABLE IF NOT EXISTS file_diffs
(
id INTEGER NOT NULL,
client_id INTEGER NOT NULL,
client_id TEXT NOT NULL,
diff_text TEXT NOT NULL,
working_diff BOOLEAN,
FOREIGN KEY (id)
REFERENCES file_diffs (parent_diff_id)
FOREIGN KEY (id)
REFERENCES files (file_id)
PRIMARY KEY (id, client_id)
@ -90,16 +91,6 @@ pub async fn initialize_database(app_handle: tauri::AppHandle) -> Result<(), DBE
Ok(())
}
#[tauri::command]
async fn store_diff(
app_handle: &tauri::AppHandle,
file_path: String,
file_name: String,
diff: String,
) -> Result<(), DBError> {
Ok(())
}
async fn get_file_id(
app_handle: &tauri::AppHandle,
relative_file_path: &str,
@ -134,18 +125,13 @@ pub async fn add_file(
.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!(
let id = sqlx::query(&format!(
"
INSERT INTO
files (relative_path, file_name)
@ -158,3 +144,60 @@ pub async fn add_file(
db.close().await;
Ok(())
}
async fn get_last_file_diff(
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 (parent_diff_id, _, _, _, _, _): (i32, String, String, bool, i32, i32) =
sqlx::query_as(&format!(
"
SELECT *
FROM file_diffs
WHERE relative_path IS '{relative_file_path}'
AND file_name IS '{file_name}'
AND working_diff is 1
"
))
.fetch_one(&db)
.await
.map_err(|e| DBError::DatabaseQueryError(e.to_string()))?;
db.close().await;
Ok(parent_diff_id)
}
#[tauri::command]
async fn store_diff(
app_handle: &tauri::AppHandle,
relative_file_path: &str,
file_name: &str,
diff: String,
) -> Result<(), DBError> {
let file_id = get_file_id(app_handle, relative_file_path, file_name)
.await
.map_err(|e| {
DBError::DatabaseQueryError("Got file diff for non existant file".to_string())
});
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
file_diffs (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(())
}