fixed diff db
This commit is contained in:
parent
3951f91901
commit
b38d0cc149
2 changed files with 23 additions and 25 deletions
|
|
@ -6,8 +6,9 @@ use std::{
|
||||||
};
|
};
|
||||||
|
|
||||||
use crate::{config::_get_client_id, get_basepath};
|
use crate::{config::_get_client_id, get_basepath};
|
||||||
|
use futures::FutureExt;
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
use sqlx::{migrate::MigrateDatabase, Sqlite, SqlitePool};
|
use sqlx::{migrate::MigrateDatabase, sqlite::SqliteConnectOptions, Sqlite, SqlitePool};
|
||||||
use tauri::{ipc::RuntimeCapability, App, AssetResolver, Manager, Url};
|
use tauri::{ipc::RuntimeCapability, App, AssetResolver, Manager, Url};
|
||||||
|
|
||||||
struct DBFileEntry {
|
struct DBFileEntry {
|
||||||
|
|
@ -76,17 +77,18 @@ pub async fn initialize_database(app_handle: tauri::AppHandle) -> Result<(), DBE
|
||||||
client_id TEXT NOT NULL,
|
client_id TEXT NOT NULL,
|
||||||
diff_text TEXT NOT NULL,
|
diff_text TEXT NOT NULL,
|
||||||
current_diff BOOLEAN,
|
current_diff BOOLEAN,
|
||||||
FOREIGN KEY (id)
|
parent_diff_id INTEGER,
|
||||||
REFERENCES file_diffs (parent_diff_id)
|
file_id INTEGER,
|
||||||
FOREIGN KEY (id)
|
FOREIGN KEY (parent_diff_id) REFERENCES file_diffs(id),
|
||||||
REFERENCES files (file_id)
|
FOREIGN KEY (file_id) REFERENCES files(id),
|
||||||
PRIMARY KEY (id, client_id)
|
PRIMARY KEY (id, client_id)
|
||||||
)
|
)
|
||||||
;",
|
;",
|
||||||
)
|
)
|
||||||
.execute(&db)
|
.execute(&db)
|
||||||
.await
|
.await
|
||||||
.map_err(|e| DBError::DatabaseQueryError(e.to_string()))?;
|
.unwrap();
|
||||||
|
// .map_err(|e| DBError::DatabaseQueryError(e.to_string()))?;
|
||||||
db.close().await;
|
db.close().await;
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
@ -195,6 +197,7 @@ pub async fn store_diff(
|
||||||
// FOREIGN KEY (id)
|
// FOREIGN KEY (id)
|
||||||
// REFERENCES files (file_id)
|
// REFERENCES files (file_id)
|
||||||
// PRIMARY KEY (id, client_id)
|
// PRIMARY KEY (id, client_id)
|
||||||
|
let file_id = get_file_id(app_handle, relative_file_path, file_name).await?;
|
||||||
let _ = sqlx::query(&format!(
|
let _ = sqlx::query(&format!(
|
||||||
"
|
"
|
||||||
UPDATE file_diffs
|
UPDATE file_diffs
|
||||||
|
|
@ -210,23 +213,9 @@ pub async fn store_diff(
|
||||||
let _ = sqlx::query(&format!(
|
let _ = sqlx::query(&format!(
|
||||||
"
|
"
|
||||||
INSERT INTO
|
INSERT INTO
|
||||||
file_diffs (client_id, diff_text, current_diff, file_diffs, file_id)
|
file_diffs (client_id, diff_text, current_diff, file_id)
|
||||||
VALUES
|
VALUES
|
||||||
('{client_id}', '{diff}', 1, (
|
('{client_id}', '{diff}', 1, );"
|
||||||
SELECT id
|
|
||||||
FROM file_diffs
|
|
||||||
WHERE relative_path IS '{relative_file_path}'
|
|
||||||
AND file_name IS '{file_name}'
|
|
||||||
AND current_diff is 1
|
|
||||||
LIMIT 1
|
|
||||||
),
|
|
||||||
(
|
|
||||||
SELECT id
|
|
||||||
FROM files
|
|
||||||
WHERE relative_path IS '{relative_file_path}'
|
|
||||||
AND file_name IS '{file_name}'
|
|
||||||
LIMIT 1
|
|
||||||
));"
|
|
||||||
))
|
))
|
||||||
.execute(&db)
|
.execute(&db)
|
||||||
.await
|
.await
|
||||||
|
|
|
||||||
|
|
@ -4,8 +4,8 @@ use crate::{
|
||||||
get_basepath,
|
get_basepath,
|
||||||
};
|
};
|
||||||
use diff_match_patch_rs::{Compat, DiffMatchPatch, Error, PatchInput};
|
use diff_match_patch_rs::{Compat, DiffMatchPatch, Error, PatchInput};
|
||||||
use std::{fs::read_to_string, path::PathBuf};
|
|
||||||
use std::path::Path;
|
use std::path::Path;
|
||||||
|
use std::{fs::read_to_string, path::PathBuf};
|
||||||
use tauri::{ipc::RuntimeCapability, App, AssetResolver, Manager, Url};
|
use tauri::{ipc::RuntimeCapability, App, AssetResolver, Manager, Url};
|
||||||
|
|
||||||
fn compare_content(old_content: &str, new_content: &str) -> Result<String, Error> {
|
fn compare_content(old_content: &str, new_content: &str) -> Result<String, Error> {
|
||||||
|
|
@ -22,10 +22,19 @@ fn compare_content(old_content: &str, new_content: &str) -> Result<String, Error
|
||||||
}
|
}
|
||||||
|
|
||||||
#[tauri::command]
|
#[tauri::command]
|
||||||
pub async fn save_file(app_handle: tauri::AppHandle, content: String) -> Result<(), String> {
|
pub async fn save_file(app_handle: tauri::AppHandle, content: String) -> Result<(), String> {
|
||||||
if let Some(path) = get_open_file_path(app_handle.clone()) {
|
if let Some(path) = get_open_file_path(app_handle.clone()) {
|
||||||
if let Ok(file_content) = load_file(app_handle.clone(), &path).await {
|
if let Ok(file_content) = load_file(app_handle.clone(), &path).await {
|
||||||
store_diff(&app_handle, &path.clone(), &PathBuf::from(path).file_name().unwrap().to_string_lossy(), compare_content(&file_content, &content).map_err(|e| format!("{e:?}"))?);
|
println!(
|
||||||
|
"{:?}",
|
||||||
|
store_diff(
|
||||||
|
&app_handle,
|
||||||
|
&path.clone(),
|
||||||
|
&PathBuf::from(path).file_name().unwrap().to_string_lossy(),
|
||||||
|
compare_content(&file_content, &content).map_err(|e| format!("{e:?}"))?,
|
||||||
|
)
|
||||||
|
.await
|
||||||
|
);
|
||||||
println!("{:?}", compare_content(&file_content, &content));
|
println!("{:?}", compare_content(&file_content, &content));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue