"insert into notebooks(resource_id, position) " \
"(?, (select coalesce(max(position), 0)+1 from notebooks)) returning notebook_id;"
+#define SQL_NOTEBOOK_SWAP_POS \
+ "update notebooks set position = case notebook_id " \
+ "when ?1 then (select position from notebooks where notebook_id = ?2) " \
+ "when ?2 then (select position from notebooks where notebook_id = ?1) " \
+ "end " \
+ "where notebook_id in (?1, ?2);"
+
static DBUConnection *connection;
static UiThreadpool *queue;
job->error = 0;
job->id1 = res->resource_id;
job->id2 = new_parent_id;
- ui_threadpool_job(queue, obj, (ui_threadfunc)qthr_delete_empty_collection, job, (ui_callback)uithr_execjob_finished, job);
+ ui_threadpool_job(queue, obj, (ui_threadfunc)qthr_delete_collection_and_move_children, job, (ui_callback)uithr_execjob_finished, job);
+}
+
+static void qthr_store_notebook_swap_position(ExecJob *job) {
+ DBUQuery *q0 = connection->createQuery(connection, NULL);
+ dbuQuerySetSQL(q0, SQL_RESOURCE_UPDATE_PARENT);
+ dbuQuerySetParamInt64(q0, 1, job->id1);
+ dbuQuerySetParamInt64(q0, 2, job->id2);
+ if(dbuQueryExec(q0)) {
+ job->error = 1;
+ }
+ dbuQueryFree(q0);
+}
+
+void note_store_notebook_swap_position_async(
+ UiObject *obj,
+ Notebook *nb1,
+ Notebook *nb2,
+ execresult_func resultcb,
+ void *userdata)
+{
+ ExecJob *job = malloc(sizeof(ExecJob));
+ job->resultcb = resultcb;
+ job->userdata = userdata;
+ job->error = 0;
+ job->id1 = nb1->notebook_id;
+ job->id2 = nb2->notebook_id;
+ ui_threadpool_job(queue, obj, (ui_threadfunc)qthr_store_notebook_swap_position, job, (ui_callback)uithr_execjob_finished, job);
}