From eeec2fbe857d8047f4b7ec74ce7200b78e88d4a8 Mon Sep 17 00:00:00 2001 From: Olaf Wintermann Date: Sun, 27 Jul 2025 18:31:48 +0200 Subject: [PATCH] fix notebook swap sql --- application/store.c | 28 +++++++++++++++++++--------- 1 file changed, 19 insertions(+), 9 deletions(-) diff --git a/application/store.c b/application/store.c index 18a09a0..a0e74e7 100644 --- a/application/store.c +++ b/application/store.c @@ -106,9 +106,16 @@ "(?, (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( -- 2.47.3