]> uap-core.de Git - note.git/commitdiff
add note_store_load_note_attachments_async (unfinished, waiting for ucx mempool trans...
authorOlaf Wintermann <olaf.wintermann@gmail.com>
Fri, 11 Apr 2025 19:28:41 +0000 (21:28 +0200)
committerOlaf Wintermann <olaf.wintermann@gmail.com>
Fri, 11 Apr 2025 19:28:41 +0000 (21:28 +0200)
application/store.c
application/store.h
application/types.h

index 99a5659514b8e74c5501f08deaa32554f3371b0d..1af0fc7edd304da13612d8536fe31e7d5c5d8a7c 100644 (file)
@@ -37,6 +37,7 @@
 #include <cx/printf.h>
 #include <cx/array_list.h>
 #include <cx/hash_map.h>
+#include <cx/mempool.h>
 
 #include "types.h"
 #include "store_sqlite.h"
 #define SQL_ATTACHMENT_RESOURCE_NEW "insert into resources(parent_id, name, lastmodified, creationdate, bin_content) values ((select parent_id from resources where resource_id = ?), ?, datetime(), datetime(), ?) returning resource_id;"
 #define SQL_ATTACHMENT_NEW "insert into attachments(attachment_resource_id, parent_resource_id, type) values (?, ?, ?) returning attachment_id;"
 
+#define SQL_ATTACHMENTS_GET "select attachment_id, attachment_resource_id, parent_resource_id, a.type, "\
+    "r.name, r.bin_content from attachments a "\
+    "inner join resource r on a.attachment_resource_id = r.resource_id " \
+    "where parent_resource_id = ? order by attachment_id;"
+
 static DBUConnection *connection;
 
 static UiThreadpool *queue;
@@ -684,3 +690,54 @@ void note_store_save_attachment_async(UiObject *obj, Attachment *attachment, exe
     job->userdata = userdata;
     ui_threadpool_job(queue, obj, (ui_threadfunc)qthr_save_attachment, job, (ui_callback)uithr_save_attachment_finished, job);
 }
+
+
+typedef struct LoadAttachmentsJob {
+    Resource *note;
+    CxMempool *temp_mp;
+    CxList *result;
+    execresult_func resultcb;
+    void *userdata;
+    int error;
+} LoadAttachmentsJob;
+
+
+static int qthr_load_attachments(LoadAttachmentsJob *job) {
+    //CxMempool *mp = cxMempoolCreateSimple(32);
+    //job->temp_mp = mp;
+    // TODO: use separate mempool and transfer it to the resource mempool
+    
+    DBUQuery *q = connection->createQuery(connection, NULL);
+    dbuQuerySetSQL(q, SQL_ATTACHMENTS_GET);
+    dbuQuerySetParamInt64(q, 1, job->note->resource_id);
+    DBUObjectBuilder *builder = dbuObjectBuilder(attachments_class, q, job->note->model->note_allocator); // use mp mempool allocator
+    job->result =  dbuObjectBuilderGetList(builder);
+    if(!job->result) {
+        job->error = 1;
+    }
+    dbuObjectBuilderDestroy(builder);
+    
+    return 0;
+}
+
+static void uithr_load_attachments_finished(UiEvent *event, LoadAttachmentsJob *job) {
+    if(job->result) {
+        job->note->attachments = job->result; // TODO: transfer temp_mp first
+    }
+    
+    if(job->resultcb) {
+        job->resultcb(event, job->error, job->userdata);
+    }
+    free(job);
+}
+
+void note_store_load_note_attachments_async(UiObject *obj, Resource *note, execresult_func resultcb, void *userdata) {
+    LoadAttachmentsJob *job = malloc(sizeof(LoadAttachmentsJob));
+    job->note = note;
+    job->temp_mp = NULL;
+    job->result = NULL;
+    job->error = 0;
+    job->resultcb = resultcb;
+    job->userdata = userdata;
+    ui_threadpool_job(queue, obj, (ui_threadfunc)qthr_load_attachments, job, (ui_callback)uithr_load_attachments_finished, job);
+}
index 951e9b26a580f9ea204391447b32baa6160aba0d..131bcc3495dc5112315648211a6a7abc30d9993e 100644 (file)
@@ -94,6 +94,8 @@ void note_store_delete_async(UiObject *obj, Resource *note, UiBool move_to_trash
 
 void note_store_save_attachment_async(UiObject *obj, Attachment *attachment, execresult_func resultcb, void *userdata);
 
+void note_store_load_note_attachments_async(UiObject *obj, Resource *note, execresult_func resultcb, void *userdata);
+
 #ifdef __cplusplus
 }
 #endif
index 5214794e6d70aab267f31079d253d7b81589575a..50ed50eed04cf6b53554b0bbcf8224f4f70ceebd 100644 (file)
@@ -141,6 +141,7 @@ extern DBUClass *usersettings_class;
 extern DBUClass *repository_class;
 extern DBUClass *collection_class;
 extern DBUClass *notes_class;
+extern DBUClass *attachments_class;
 
 void register_types();