#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;
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);
+}