From: Olaf Wintermann Date: Mon, 19 Jan 2026 19:26:22 +0000 (+0100) Subject: add note_store_delete_collection_async X-Git-Url: https://uap-core.de/gitweb/?a=commitdiff_plain;h=12e1c63440b95739e99d6bde31748bda772a3271;p=note.git add note_store_delete_collection_async --- diff --git a/application/store.c b/application/store.c index 382ffd4..c275a93 100644 --- a/application/store.c +++ b/application/store.c @@ -82,6 +82,8 @@ #define SQL_RESOURCE_DELETE "delete from resources where resource_id = ? ;" +#define SQL_RESOURCE_DELETE_CHILDREN "delete from resource where parent_id = ? ;" + #define SQL_RESOURCE_UPDATE_PARENT "update resources set parent_id = ? where parent_id = ? ;" #define SQL_RESOURCE_COUNT_CHILDREN "select count(*) from resources where parent_id = ? ;" @@ -1029,6 +1031,7 @@ typedef struct ExecJob { void *userdata; int64_t id1; int64_t id2; + int flag; int error; } ExecJob; @@ -1083,6 +1086,7 @@ void note_store_update_resource_async(UiObject *obj, Resource *res, execresult_f job->resource = res; job->resultcb = resultcb; job->userdata = userdata; + job->flag = 0; job->error = 0; //qthr_update_resource(job); //uithr_update_resource_finished(NULL, job); @@ -1200,26 +1204,42 @@ static void uithr_execjob_finished(UiEvent *event, ExecJob *job) { free(job); } -static int qthr_delete_empty_collection(ExecJob *job) { - // only delete the resource when there are no children - int64_t nchildren = resource_get_children(job->id1); - if(nchildren > 0) { - job->error = 1; - return 0; +#define NOTESTORE_COLLECTION_DELETE_ALL 1 + +static int qthr_delete_collection(ExecJob *job) { + if(job->flag == NOTESTORE_COLLECTION_DELETE_ALL) { + DBUResult *result = dbuSqlExecParamInt64(connection, NULL, SQL_RESOURCE_DELETE_CHILDREN, job->id1); + if(!result) { + job->error = 3; + } + int ok = dbuResultIsOk(result); + dbuResultFree(result); + if(!ok) { + job->error = 4; + } + } else { + // only delete the resource when there are no children + int64_t nchildren = resource_get_children(job->id1); + if(nchildren > 0) { + job->error = 1; + return 0; + } + if(nchildren < 0) { + job->error = 2; // db error + return 0; + } } - if(nchildren < 0) { - job->error = 2; // db error - return 0; - } - - DBUQuery *q = connection->createQuery(connection, NULL); - dbuQuerySetSQL(q, SQL_RESOURCE_DELETE); - dbuQuerySetParamInt64(q, 1, job->id1); - if(dbuQueryExec(q)) { - job->error = 3; + DBUResult *result = dbuSqlExecParamInt64(connection, NULL, SQL_RESOURCE_DELETE, job->id1); + if(!result) { + job->error = 5; } - dbuQueryFree(q); + int ok = dbuResultIsOk(result); + dbuResultFree(result); + if(!ok) { + job->error = 6; + } + return 0; } @@ -1236,7 +1256,25 @@ void note_store_delete_empty_collection_async( job->error = 0; job->id1 = res->resource_id; job->id2 = 0; - ui_threadpool_job(queue, obj, (ui_threadfunc)qthr_delete_empty_collection, job, (ui_callback)uithr_execjob_finished, job); + job->flag = 0; + ui_threadpool_job(queue, obj, (ui_threadfunc)qthr_delete_collection, job, (ui_callback)uithr_execjob_finished, job); +} + +void note_store_delete_collection_async( + UiObject *obj, + Resource *res, + execresult_func resultcb, + void *userdata) +{ + ExecJob *job = malloc(sizeof(ExecJob)); + job->resource = res; + job->resultcb = resultcb; + job->userdata = userdata; + job->error = 0; + job->id1 = res->resource_id; + job->id2 = 0; + job->flag = NOTESTORE_COLLECTION_DELETE_ALL; + ui_threadpool_job(queue, obj, (ui_threadfunc)qthr_delete_collection, job, (ui_callback)uithr_execjob_finished, job); } static int qthr_delete_collection_and_move_children(ExecJob *job) { @@ -1275,6 +1313,7 @@ void note_store_delete_collection_and_move_children_async( job->error = 0; job->id1 = res->resource_id; job->id2 = new_parent_id; + job->flag = 0; ui_threadpool_job(queue, obj, (ui_threadfunc)qthr_delete_collection_and_move_children, job, (ui_callback)uithr_execjob_finished, job); } @@ -1302,6 +1341,7 @@ void note_store_notebook_swap_position_async( job->resultcb = resultcb; job->userdata = userdata; job->error = 0; + job->flag = 0; job->id1 = nb1->notebook_id; job->id2 = nb2->notebook_id; ui_threadpool_job(queue, obj, (ui_threadfunc)qthr_store_notebook_swap_position, job, (ui_callback)uithr_execjob_finished, job); diff --git a/application/store.h b/application/store.h index 99afa5d..4e00bd7 100644 --- a/application/store.h +++ b/application/store.h @@ -154,6 +154,12 @@ void note_store_delete_empty_collection_async( execresult_func resultcb, void *userdata); +void note_store_delete_collection_async( + UiObject *obj, + Resource *res, + execresult_func resultcb, + void *userdata); + void note_store_delete_collection_and_move_children_async( UiObject *obj, Resource *res,