]> uap-core.de Git - note.git/commitdiff
add note_store_delete_empty_collection_async function
authorOlaf Wintermann <olaf.wintermann@gmail.com>
Sun, 27 Jul 2025 10:52:27 +0000 (12:52 +0200)
committerOlaf Wintermann <olaf.wintermann@gmail.com>
Sun, 27 Jul 2025 10:52:27 +0000 (12:52 +0200)
application/store.c
application/store.h

index c70071a9019ece6715a870f7ba853195caad0ae4..f19aa574f625b50581e786a6e78765be8ea0c381 100644 (file)
@@ -78,7 +78,9 @@
 
 #define SQL_NOTE_MOVE_TO_TRASH "update resources set parent_id = ? where resource_id = ? ;"
 
-#define SQL_NOTE_DELETE "delete from resources where resource_id = ? ;"
+#define SQL_RESOURCE_DELETE "delete from resources where resource_id = ? ;"
+
+#define SQL_RESOURCE_COUNT_CHILDREN "select count(*) from resources where parent_id = ? ;"
 
 #define SQL_ATTACHMENT_RESOURCE_NEW \
     "insert into resources(parent_id, nodename, lastmodified, creationdate, content) values " \
@@ -391,6 +393,20 @@ NoteStore* note_store_get() {
     return current_store;
 }
 
+static int64_t resource_get_children(int64_t resource_id) {
+    DBUQuery *q = connection->createQuery(connection, NULL);
+    dbuQuerySetSQL(q, SQL_RESOURCE_COUNT_CHILDREN);
+    dbuQuerySetParamInt64(q, 1, resource_id);
+    if(dbuQueryExec(q)) {
+        return -1;
+    }
+    DBUResult *result = dbuQueryGetResult(q);
+    int64_t intResult = -1;
+    dbuResultAsInt64(result, &intResult);
+    dbuQueryFree(q);
+    return intResult;
+}
+
 
 CxList* note_store_get_notes(const CxAllocator *a, int64_t parent_collection_id) {
     DBUQuery *q = connection->createQuery(connection, NULL);
@@ -628,7 +644,7 @@ static int qthr_delete_note(DeleteNoteJob *job) {
         dbuQuerySetParamInt64(q, 1, current_store->trash->resource_id);
         dbuQuerySetParamInt64(q, 2, job->note_id);
     } else {
-        dbuQuerySetSQL(q, SQL_NOTE_DELETE);
+        dbuQuerySetSQL(q, SQL_RESOURCE_DELETE);
         dbuQuerySetParamInt64(q, 1, job->note_id);
     }
     if(dbuQueryExec(q)) {
@@ -846,3 +862,54 @@ void note_store_new_notebook_async(UiObject *obj, Resource *notebook, createresu
     job->error = 0;
     ui_threadpool_job(queue, obj, (ui_threadfunc)qthr_new_notebook, job, (ui_callback)uithr_new_notebook_finished, job);
 }
+
+
+typedef struct ExecJob {
+    execresult_func resultcb;
+    void *userdata;
+    int64_t id1;
+    int64_t id2;
+    int error;
+} ExecJob;
+
+static void uithr_execjob_finished(UiEvent *event, ExecJob *job) {
+    job->resultcb(event, job->error, job->userdata);
+    free(job);
+}
+
+static void 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;
+    }
+    if(nchildren < 0) {
+        job->error = 2; // db error
+        return;
+    } 
+    
+    
+    DBUQuery *q = connection->createQuery(connection, NULL);
+    dbuQuerySetSQL(q, SQL_RESOURCE_DELETE);
+    dbuQuerySetParamInt64(q, 1, job->id1);
+    if(dbuQueryExec(q)) {
+        job->error = 3;
+    }
+    dbuQueryFree(q);
+}
+
+void note_store_delete_empty_collection_async(
+        UiObject *obj,
+        Resource *res,
+        execresult_func resultcb,
+        void *userdata)
+{
+    ExecJob *job = malloc(sizeof(ExecJob));
+    job->resultcb = resultcb;
+    job->userdata = userdata;
+    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);
+}
index 1c1ba6d3d035137b95ffab7fe8b34c3f1c74fab1..a319dca82945fdf49ae25d09b97733c14a1c899b 100644 (file)
@@ -100,6 +100,26 @@ void note_store_load_note_attachments_async(UiObject *obj, Note *note, execresul
 
 void note_store_new_notebook_async(UiObject *obj, Resource *notebook, createresult_func resultcb, void *userdata);
 
+void note_store_delete_empty_collection_async(
+        UiObject *obj,
+        Resource *res,
+        execresult_func resultcb,
+        void *userdata);
+
+void note_store_delete_collection_and_move_children_async(
+        UiObject *obj,
+        Resource *res,
+        int64_t new_parent_id,
+        execresult_func resultcb,
+        void *userdata);
+
+void note_store_notebook_swap_position(
+        UiObject *obj,
+        Notebook *nb1,
+        Notebook *nb2,
+        execresult_func resultcb,
+        void *userdata);
+
 #ifdef __cplusplus
 }
 #endif