TEST FAILING; the CTE isn't solid. Needs revision and testing.

This commit is contained in:
Elf M. Sternberg 2020-10-09 13:06:09 -07:00
parent fd4f39b5b8
commit e429eaf93c
3 changed files with 18 additions and 16 deletions

View File

@ -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<Utc>,
pub updated_date: DateTime<Utc>,

View File

@ -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

View File

@ -58,27 +58,28 @@ impl NoteStore {
/// that title is generated automatically.
pub async fn get_page_by_title(&self, title: &str) -> NoteResult<(RawPage, Vec<RawNote>)> {
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<Vec<RawNote>>
async fn select_note_collection_from_root<'a, E>(executor: E, root: i64) -> SqlResult<Vec<RawNote>>
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?)