From e429eaf93c459b8a98a02768eca2a834ebc2bfb8 Mon Sep 17 00:00:00 2001 From: "Elf M. Sternberg" Date: Fri, 9 Oct 2020 13:06:09 -0700 Subject: [PATCH] TEST FAILING; the CTE isn't solid. Needs revision and testing. --- server/nm-store/src/row_structs.rs | 3 +++ .../sql/select_note_collection_from_root.sql | 4 +-- server/nm-store/src/store.rs | 27 ++++++++++--------- 3 files changed, 18 insertions(+), 16 deletions(-) diff --git a/server/nm-store/src/row_structs.rs b/server/nm-store/src/row_structs.rs index cab6ca9..d4eb467 100644 --- a/server/nm-store/src/row_structs.rs +++ b/server/nm-store/src/row_structs.rs @@ -19,7 +19,10 @@ pub struct RawPage { pub struct RawNote { pub id: i64, pub uuid: String, + pub parent_id: i64, + pub parent_uuid: String, pub content: String, + pub position: i64, pub notetype: String, pub creation_date: DateTime, pub updated_date: DateTime, diff --git a/server/nm-store/src/sql/select_note_collection_from_root.sql b/server/nm-store/src/sql/select_note_collection_from_root.sql index 40ef0ba..9625e64 100644 --- a/server/nm-store/src/sql/select_note_collection_from_root.sql +++ b/server/nm-store/src/sql/select_note_collection_from_root.sql @@ -56,9 +56,7 @@ FROM ( notes.deleted_date, ','||notes.id||',' -- Cycle monitor FROM notes - INNER JOIN pages ON pages.note_id = notes.id - WHERE - pages.slug = ? AND notes.notetype = "root" + WHERE notes.id = ? AND notes.notetype = "root" -- RECURSIVE expression UNION SELECT diff --git a/server/nm-store/src/store.rs b/server/nm-store/src/store.rs index 52a4794..96c7c6b 100644 --- a/server/nm-store/src/store.rs +++ b/server/nm-store/src/store.rs @@ -58,27 +58,28 @@ impl NoteStore { /// that title is generated automatically. pub async fn get_page_by_title(&self, title: &str) -> NoteResult<(RawPage, Vec)> { let mut tx = self.0.begin().await?; - let page = match select_page_by_title(&mut tx, title).await { - Ok(page) => page, + let (page, notes) = match select_page_by_title(&mut tx, title).await { + Ok(page) => { + let note_id = page.note_id; + (page, select_note_collection_from_root(&mut tx, note_id).await?) + } Err(sqlx::Error::RowNotFound) => { - let new_page = { + let page = { let new_root_note = create_unique_root_note(); let new_root_note_id = insert_one_new_note(&mut tx, &new_root_note).await?; let new_page_slug = generate_slug(&mut tx, title).await?; let new_page = create_new_page_for(&title, &new_page_slug, new_root_note_id); - let new_page_id = insert_one_new_page(&mut tx, &new_page).await?; - select_page_by_title(&mut tx, title).await + let _ = insert_one_new_page(&mut tx, &new_page).await?; + select_page_by_title(&mut tx, &title).await? }; - match new_page { - Ok(page) => page, - Err(e) => return Err(NoteStoreError::DBError(e)), - } + let note_id = page.note_id; + (page, select_note_collection_from_root(&mut tx, note_id).await?) }, Err(e) => return Err(NoteStoreError::DBError(e)), }; // Todo: Replace vec with the results of the CTE tx.commit().await?; - return Ok((page, vec![])); + return Ok((page, notes)); } } @@ -129,12 +130,12 @@ where .await?) } -async fn get_note_collection_for_root<'a, E>(executor: E, root: i64) -> SqlResult> +async fn select_note_collection_from_root<'a, E>(executor: E, root: i64) -> SqlResult> where E: Executor<'a, Database = Sqlite>, { - let select_note_collection_for_root = include_str!("sql/select_note_collection_from_root.sql"); - Ok(sqlx::query_as(&select_note_collection_for_root) + let select_note_collection_from_root_sql = include_str!("sql/select_note_collection_from_root.sql"); + Ok(sqlx::query_as(&select_note_collection_from_root_sql) .bind(&root) .fetch_all(executor) .await?)