From 0f366dd48c0962b574461c1ef3e0351b7156f9de Mon Sep 17 00:00:00 2001 From: Olaf Wintermann Date: Thu, 31 Jul 2025 20:37:39 +0200 Subject: [PATCH] add note_store_save_repository_async --- application/store.c | 86 +++++++++++++++++++++++++++++++++++++++++++++ application/store.h | 2 ++ 2 files changed, 88 insertions(+) diff --git a/application/store.c b/application/store.c index 4694991..eb43cb7 100644 --- a/application/store.c +++ b/application/store.c @@ -122,6 +122,20 @@ "end " \ "where notebook_id in (?1, ?2);" +#define SQL_REPOSITORY_NEW \ + "insert into repositories(name, url, encryption, default_key, authmethod, local_path) " \ + "values (?, ?, ?, ?, ?, ?) repository_id;" + +#define SQL_REPOSITORY_SAVE \ + "update repositories set " \ + " name = ? , " \ + " url = ? , " \ + " encryption = ? , " \ + " default_key = ? , " \ + " authmethod = ? , " \ + " local_path = ? " \ + "where repository_id = ?;" + static DBUConnection *connection; static UiThreadpool *queue; @@ -1009,3 +1023,75 @@ void note_store_notebook_swap_position_async( job->id2 = nb2->notebook_id; ui_threadpool_job(queue, obj, (ui_threadfunc)qthr_store_notebook_swap_position, job, (ui_callback)uithr_execjob_finished, job); } + + + +typedef struct SaveRepositoryJob { + Repository *repo; + execresult_func resultcb; + void *userdata; + int error; +} SaveRepositoryJob; + +static void uithr_save_repository_job_finished(UiEvent *event, SaveRepositoryJob *job) { + job->resultcb(event, job->error, job->userdata); + free(job); +} + +static int qthr_store_save_repository(SaveRepositoryJob *job) { + DBUQuery *q = connection->createQuery(connection, NULL); + Repository *repo = job->repo; + bool update_id = FALSE; + if(job->repo->repository_id != 0) { + // update + dbuQuerySetSQL(q, SQL_REPOSITORY_SAVE); + dbuQuerySetParamInt64(q, 7, repo->repository_id); + } else { + // new repository + dbuQuerySetSQL(q, SQL_REPOSITORY_NEW); + update_id = TRUE; + } + + dbuQuerySetParamString(q, 1, cx_str(repo->name)); + if(repo->url) { + dbuQuerySetParamString(q, 2, cx_str(repo->url)); + } else { + dbuQuerySetParamNull(q, 2); + } + dbuQuerySetParamInt(q, 4, repo->encryption); + if(repo->url) { + dbuQuerySetParamString(q, 4, cx_str(repo->default_key)); + } else { + dbuQuerySetParamNull(q, 4); + } + dbuQuerySetParamInt(q, 5, repo->authmethod); + if(repo->local_path) { + dbuQuerySetParamString(q, 6, cx_str(repo->local_path)); + } else { + dbuQuerySetParamNull(q, 6); + } + + if(dbuQueryExec(q)) { + job->error = 1; + } else if(update_id) { + DBUResult *result = dbuQueryGetResult(q); + if(result) { + if(dbuResultAsInt64(result, &repo->repository_id)) { + job->error = 3; + } + } else { + job->error = 2; + } + } + dbuQueryFree(q); + return 0; +} + +void note_store_save_repository_async(UiObject *obj, Repository *repository, execresult_func resultcb, void *userdata) { + SaveRepositoryJob *job = malloc(sizeof(SaveRepositoryJob)); + job->repo = repository; + job->resultcb = resultcb; + job->userdata = userdata; + job->error = 0; + ui_threadpool_job(queue, obj, (ui_threadfunc)qthr_store_save_repository, job, (ui_callback)uithr_save_repository_job_finished, job); +} diff --git a/application/store.h b/application/store.h index 42e44c6..466a502 100644 --- a/application/store.h +++ b/application/store.h @@ -121,6 +121,8 @@ void note_store_notebook_swap_position_async( execresult_func resultcb, void *userdata); +void note_store_save_repository_async(UiObject *obj, Repository *repository, execresult_func resultcb, void *userdata); + #ifdef __cplusplus } #endif -- 2.47.3