Compare commits

..

No commits in common. "1b36183edbd50e9bc2dab28e28ea7a731f1381ec" and "fd4f39b5b84809e01db830a8828b27366c0ee25e" have entirely different histories.

3 changed files with 26 additions and 29 deletions

View File

@ -19,10 +19,7 @@ pub struct RawPage {
pub struct RawNote { pub struct RawNote {
pub id: i64, pub id: i64,
pub uuid: String, pub uuid: String,
pub parent_id: i64,
pub parent_uuid: String,
pub content: String, pub content: String,
pub position: i64,
pub notetype: String, pub notetype: String,
pub creation_date: DateTime<Utc>, pub creation_date: DateTime<Utc>,
pub updated_date: DateTime<Utc>, pub updated_date: DateTime<Utc>,

View File

@ -13,10 +13,10 @@
-- *in nesting order* to the client. -- *in nesting order* to the client.
SELECT SELECT
id,
uuid,
parent_id, parent_id,
parent_uuid, parent_uuid,
note_id,
note_uuid,
content, content,
position, position,
notetype, notetype,
@ -28,10 +28,10 @@ SELECT
FROM ( FROM (
WITH RECURSIVE notetree( WITH RECURSIVE notetree(
id,
uuid,
parent_id, parent_id,
parent_uuid, parent_uuid,
id,
uuid,
content, content,
position, position,
notetype, notetype,
@ -42,11 +42,11 @@ FROM (
cycle) AS cycle) AS
-- ROOT expression -- ROOT expression
(SELECT SELECT
notes.id,
notes.uuid,
notes.id, notes.id,
notes.uuid, notes.uuid,
notes.id AS parent_id,
notes.uuid AS parent_uuid,
notes.content, notes.content,
0, -- Root notes are always in position 0 0, -- Root notes are always in position 0
notes.notetype, notes.notetype,
@ -56,14 +56,16 @@ FROM (
notes.deleted_date, notes.deleted_date,
','||notes.id||',' -- Cycle monitor ','||notes.id||',' -- Cycle monitor
FROM notes FROM notes
WHERE notes.id = ? AND notes.notetype = "root" INNER JOIN pages ON pages.note_id = notes.id
WHERE
pages.slug = ? AND notes.notetype = "root"
-- RECURSIVE expression -- RECURSIVE expression
UNION SELECT UNION SELECT
notetree.id,
notetree.uuid,
notes.id, notes.id,
notes.uuid, notes.uuid,
notetree.id AS parent_id,
notetree.uuid AS parent_uuid,
notes.content, notes.content,
note_relationships.position, note_relationships.position,
notes.notetype, notes.notetype,
@ -81,6 +83,5 @@ FROM (
-- be; we're supposed to prevent those. But you never know. -- be; we're supposed to prevent those. But you never know.
WHERE WHERE
notetree.cycle NOT LIKE '%,'||notes.id||',%' notetree.cycle NOT LIKE '%,'||notes.id||',%'
ORDER BY note_relationships.position) ORDER BY note_relationships.position);
SELECT * from notetree);

View File

@ -58,28 +58,27 @@ impl NoteStore {
/// that title is generated automatically. /// that title is generated automatically.
pub async fn get_page_by_title(&self, title: &str) -> NoteResult<(RawPage, Vec<RawNote>)> { pub async fn get_page_by_title(&self, title: &str) -> NoteResult<(RawPage, Vec<RawNote>)> {
let mut tx = self.0.begin().await?; let mut tx = self.0.begin().await?;
let (page, notes) = match select_page_by_title(&mut tx, title).await { let page = match select_page_by_title(&mut tx, title).await {
Ok(page) => { Ok(page) => page,
let note_id = page.note_id;
(page, select_note_collection_from_root(&mut tx, note_id).await?)
}
Err(sqlx::Error::RowNotFound) => { Err(sqlx::Error::RowNotFound) => {
let page = { let new_page = {
let new_root_note = create_unique_root_note(); 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_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_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 = create_new_page_for(&title, &new_page_slug, new_root_note_id);
let _ = insert_one_new_page(&mut tx, &new_page).await?; let new_page_id = insert_one_new_page(&mut tx, &new_page).await?;
select_page_by_title(&mut tx, &title).await? select_page_by_title(&mut tx, title).await
}; };
let note_id = page.note_id; match new_page {
(page, select_note_collection_from_root(&mut tx, note_id).await?) Ok(page) => page,
Err(e) => return Err(NoteStoreError::DBError(e)),
}
}, },
Err(e) => return Err(NoteStoreError::DBError(e)), Err(e) => return Err(NoteStoreError::DBError(e)),
}; };
// Todo: Replace vec with the results of the CTE // Todo: Replace vec with the results of the CTE
tx.commit().await?; tx.commit().await?;
return Ok((page, notes)); return Ok((page, vec![]));
} }
} }
@ -130,12 +129,12 @@ where
.await?) .await?)
} }
async fn select_note_collection_from_root<'a, E>(executor: E, root: i64) -> SqlResult<Vec<RawNote>> async fn get_note_collection_for_root<'a, E>(executor: E, root: i64) -> SqlResult<Vec<RawNote>>
where where
E: Executor<'a, Database = Sqlite>, E: Executor<'a, Database = Sqlite>,
{ {
let select_note_collection_from_root_sql = include_str!("sql/select_note_collection_from_root.sql"); let select_note_collection_for_root = include_str!("sql/select_note_collection_from_root.sql");
Ok(sqlx::query_as(&select_note_collection_from_root_sql) Ok(sqlx::query_as(&select_note_collection_for_root)
.bind(&root) .bind(&root)
.fetch_all(executor) .fetch_all(executor)
.await?) .await?)