"(?, (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) " \
+ "with vars(pos1, pos2) as ( " \
+ " select " \
+ " (select position from notebooks where notebook_id = ?1), " \
+ " (select position from notebooks where notebook_id = ?2) " \
+ ") " \
+ "update notebooks " \
+ "set position = case " \
+ " when notebook_id = ?1 THEN (SELECT pos2 FROM vars) " \
+ " when notebook_id = ?2 THEN (SELECT pos1 FROM vars) " \
+ " else position " \
"end " \
"where notebook_id in (?1, ?2);"
free(job);
}
-static void qthr_delete_empty_collection(ExecJob *job) {
+static int 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;
+ return 0;
}
if(nchildren < 0) {
job->error = 2; // db error
- return;
+ return 0;
}
job->error = 3;
}
dbuQueryFree(q);
+ return 0;
}
void note_store_delete_empty_collection_async(
ui_threadpool_job(queue, obj, (ui_threadfunc)qthr_delete_empty_collection, job, (ui_callback)uithr_execjob_finished, job);
}
-static void qthr_delete_collection_and_move_children(ExecJob *job) {
+static int qthr_delete_collection_and_move_children(ExecJob *job) {
// move all sub-resources to new parent
DBUQuery *q0 = connection->createQuery(connection, NULL);
dbuQuerySetSQL(q0, SQL_RESOURCE_UPDATE_PARENT);
job->error = 4;
}
dbuQueryFree(q1);
+ return 0;
}
void note_store_delete_collection_and_move_children_async(
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) {
+static int qthr_store_notebook_swap_position(ExecJob *job) {
DBUQuery *q0 = connection->createQuery(connection, NULL);
- dbuQuerySetSQL(q0, SQL_RESOURCE_UPDATE_PARENT);
+ dbuQuerySetSQL(q0, SQL_NOTEBOOK_SWAP_POS);
dbuQuerySetParamInt64(q0, 1, job->id1);
dbuQuerySetParamInt64(q0, 2, job->id2);
if(dbuQueryExec(q0)) {
job->error = 1;
}
dbuQueryFree(q0);
+ return 0;
}
void note_store_notebook_swap_position_async(