]> uap-core.de Git - note.git/commitdiff
fix notebook swap sql
authorOlaf Wintermann <olaf.wintermann@gmail.com>
Sun, 27 Jul 2025 16:31:48 +0000 (18:31 +0200)
committerOlaf Wintermann <olaf.wintermann@gmail.com>
Sun, 27 Jul 2025 16:31:48 +0000 (18:31 +0200)
application/store.c

index 18a09a026fa156f47269d24bd4ced97534ca3d15..a0e74e7984ba6d73039617126fc25916c5399e65 100644 (file)
     "(?, (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);"
 
@@ -886,16 +893,16 @@ static void uithr_execjob_finished(UiEvent *event, ExecJob *job) {
     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;
     } 
     
     
@@ -906,6 +913,7 @@ static void qthr_delete_empty_collection(ExecJob *job) {
         job->error = 3;
     }
     dbuQueryFree(q);
+    return 0;
 }
 
 void note_store_delete_empty_collection_async(
@@ -923,7 +931,7 @@ 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);
@@ -942,6 +950,7 @@ static void qthr_delete_collection_and_move_children(ExecJob *job) {
         job->error = 4;
     }
     dbuQueryFree(q1);
+    return 0;
 }
 
 void note_store_delete_collection_and_move_children_async(
@@ -960,15 +969,16 @@ 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(