Compare commits

...

2 Commits

3 changed files with 29 additions and 26 deletions

View File

@ -19,7 +19,10 @@ 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(
parent_id,
parent_uuid,
id, id,
uuid, uuid,
parent_id,
parent_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,16 +56,14 @@ FROM (
notes.deleted_date, notes.deleted_date,
','||notes.id||',' -- Cycle monitor ','||notes.id||',' -- Cycle monitor
FROM notes FROM notes
INNER JOIN pages ON pages.note_id = notes.id WHERE notes.id = ? AND notes.notetype = "root"
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,
@ -83,5 +81,6 @@ 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,27 +58,28 @@ 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 = match select_page_by_title(&mut tx, title).await { let (page, notes) = match select_page_by_title(&mut tx, title).await {
Ok(page) => page, Ok(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 new_page = { let 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 new_page_id = insert_one_new_page(&mut tx, &new_page).await?; let _ = 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?
}; };
match new_page { let note_id = page.note_id;
Ok(page) => page, (page, select_note_collection_from_root(&mut tx, note_id).await?)
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, vec![])); return Ok((page, notes));
} }
} }
@ -129,12 +130,12 @@ where
.await?) .await?)
} }
async fn get_note_collection_for_root<'a, E>(executor: E, root: i64) -> SqlResult<Vec<RawNote>> async fn select_note_collection_from_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_for_root = include_str!("sql/select_note_collection_from_root.sql"); let select_note_collection_from_root_sql = include_str!("sql/select_note_collection_from_root.sql");
Ok(sqlx::query_as(&select_note_collection_for_root) Ok(sqlx::query_as(&select_note_collection_from_root_sql)
.bind(&root) .bind(&root)
.fetch_all(executor) .fetch_all(executor)
.await?) .await?)