From: Olaf Wintermann Date: Fri, 11 Apr 2025 19:28:41 +0000 (+0200) Subject: add note_store_load_note_attachments_async (unfinished, waiting for ucx mempool trans... X-Git-Url: https://uap-core.de/gitweb/?a=commitdiff_plain;h=03be984281de74a76c5cbda01666f757b24970e7;p=note.git add note_store_load_note_attachments_async (unfinished, waiting for ucx mempool transfer feature) --- diff --git a/application/store.c b/application/store.c index 99a5659..1af0fc7 100644 --- a/application/store.c +++ b/application/store.c @@ -37,6 +37,7 @@ #include #include #include +#include #include "types.h" #include "store_sqlite.h" @@ -73,6 +74,11 @@ #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); +} diff --git a/application/store.h b/application/store.h index 951e9b2..131bcc3 100644 --- a/application/store.h +++ b/application/store.h @@ -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 diff --git a/application/types.h b/application/types.h index 5214794..50ed50e 100644 --- a/application/types.h +++ b/application/types.h @@ -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();