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