Compare commits
4 Commits
cf6f906fa4
...
013ca18c62
Author | SHA1 | Date |
---|---|---|
Elf M. Sternberg | 013ca18c62 | |
Elf M. Sternberg | 8a83d802d3 | |
Elf M. Sternberg | 5672f2e235 | |
Elf M. Sternberg | 6bd14057ab |
|
@ -1,4 +1,5 @@
|
|||
[ ] Add RelationshipKind to Notes passed out
|
||||
[ ] Add KastenKind to Backreferences passed out
|
||||
[ ] Provide the array of notes references (the 'cycle' manager) to make
|
||||
[ ] Provide the array of note references (the 'cycle' manager) to make
|
||||
mapping from Vec->Tree easier.
|
||||
|
||||
|
|
|
@ -0,0 +1 @@
|
|||
Content analysis
|
|
@ -5,7 +5,7 @@ mod structs;
|
|||
|
||||
pub use crate::errors::NoteStoreError;
|
||||
pub use crate::store::NoteStore;
|
||||
pub use crate::structs::{Note, NoteKind, NoteRelationship, KastenRelationship};
|
||||
pub use crate::structs::{Note, NoteKind, NoteRelationship, PageRelationship};
|
||||
|
||||
|
||||
#[cfg(test)]
|
||||
|
@ -29,8 +29,8 @@ mod tests {
|
|||
#[tokio::test(threaded_scheduler)]
|
||||
async fn fetching_unfound_page_by_slug_works() {
|
||||
let storagepool = fresh_inmemory_database().await;
|
||||
let foundkasten = storagepool.get_kasten_by_slug("nonexistent-kasten").await;
|
||||
assert!(foundkasten.is_err());
|
||||
let foundpage = storagepool.get_page_by_slug("nonexistent-page").await;
|
||||
assert!(foundpage.is_err());
|
||||
}
|
||||
|
||||
// Request for the page by title. If the page exists, return it.
|
||||
|
@ -42,7 +42,7 @@ mod tests {
|
|||
let title = "Nonexistent Page";
|
||||
let now = chrono::Utc::now();
|
||||
let storagepool = fresh_inmemory_database().await;
|
||||
let newpageresult = storagepool.get_kasten_by_title(&title).await;
|
||||
let newpageresult = storagepool.get_page_by_title(&title).await;
|
||||
|
||||
assert!(newpageresult.is_ok(), "{:?}", newpageresult);
|
||||
let (newpages, _) = newpageresult.unwrap();
|
||||
|
@ -52,7 +52,7 @@ mod tests {
|
|||
|
||||
assert_eq!(newpage.content, title, "{:?}", newpage.content);
|
||||
assert_eq!(newpage.id, "nonexistent-page");
|
||||
assert_eq!(newpage.kind, NoteKind::Kasten);
|
||||
assert_eq!(newpage.kind, NoteKind::Page);
|
||||
assert!((newpage.creation_date - now).num_minutes() < 1);
|
||||
assert!((newpage.updated_date - now).num_minutes() < 1);
|
||||
assert!((newpage.lastview_date - now).num_minutes() < 1);
|
||||
|
@ -70,7 +70,7 @@ mod tests {
|
|||
async fn can_nest_notes() {
|
||||
let title = "Nonexistent Page";
|
||||
let storagepool = fresh_inmemory_database().await;
|
||||
let newpageresult = storagepool.get_kasten_by_title(&title).await;
|
||||
let newpageresult = storagepool.get_page_by_title(&title).await;
|
||||
|
||||
assert!(newpageresult.is_ok(), "{:?}", newpageresult);
|
||||
let (newpages, _) = newpageresult.unwrap();
|
||||
|
@ -100,7 +100,7 @@ mod tests {
|
|||
assert!(note4_id.is_ok(), "{:?}", note4_id);
|
||||
let _note4_id = note4_id.unwrap();
|
||||
|
||||
let newpageresult = storagepool.get_kasten_by_title(&title).await;
|
||||
let newpageresult = storagepool.get_page_by_title(&title).await;
|
||||
assert!(newpageresult.is_ok(), "{:?}", newpageresult);
|
||||
let (newpages, _) = newpageresult.unwrap();
|
||||
|
||||
|
|
|
@ -4,6 +4,7 @@ use regex::Regex;
|
|||
use slug::slugify;
|
||||
use sqlx::{sqlite::Sqlite, Acquire, Done, Executor, Transaction};
|
||||
use std::collections::HashSet;
|
||||
use std::cmp;
|
||||
|
||||
type SqlResult<T> = sqlx::Result<T>;
|
||||
|
||||
|
@ -19,7 +20,7 @@ type SqlResult<T> = sqlx::Result<T>;
|
|||
// of the SQL queries.
|
||||
|
||||
lazy_static! {
|
||||
static ref SELECT_KASTEN_BY_TITLE_SQL: String = str::replace(
|
||||
static ref SELECT_PAGE_BY_TITLE_SQL: String = str::replace(
|
||||
include_str!("sql/select_notes_by_parameter.sql"),
|
||||
"QUERYPARAMETER",
|
||||
"notes.content"
|
||||
|
@ -27,16 +28,17 @@ lazy_static! {
|
|||
}
|
||||
|
||||
lazy_static! {
|
||||
static ref SELECT_KASTEN_BY_ID_SQL: String = str::replace(
|
||||
static ref SELECT_PAGE_BY_ID_SQL: String = str::replace(
|
||||
include_str!("sql/select_notes_by_parameter.sql"),
|
||||
"QUERYPARAMETER",
|
||||
"notes.id"
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
lazy_static! {
|
||||
static ref SELECT_NOTES_BACKREFENCING_KASTEN_SQL: &'static str =
|
||||
include_str!("sql/select_notes_backreferencing_kasten.sql");
|
||||
static ref SELECT_NOTES_BACKREFERENCING_PAGE_SQL: &'static str =
|
||||
include_str!("sql/select_notes_backreferencing_page.sql");
|
||||
}
|
||||
|
||||
// ___ _
|
||||
|
@ -62,56 +64,58 @@ where
|
|||
// |_|\___|\__\__|_||_| |_|\_\__,_/__/\__\___|_||_|
|
||||
//
|
||||
|
||||
// Select the requested kasten via its id. This is fairly rare;
|
||||
// kastens should usually be picked up via their title, but if you're
|
||||
// navigating to an instance, this is how you specify the kasten in a
|
||||
// The next three functions are essentially the same, although the internal
|
||||
// SQL operations are quite different between the first two and the last.
|
||||
async fn select_object_by_query<'a, E>(executor: E, query: &str, field: &str) -> SqlResult<Vec<Note>>
|
||||
where
|
||||
E: Executor<'a, Database = Sqlite>,
|
||||
{
|
||||
let r: Vec<RowNote> = sqlx::query_as(query)
|
||||
.bind(field)
|
||||
.fetch_all(executor)
|
||||
.await?;
|
||||
Ok(r.into_iter().map(|z| Note::from(z)).collect())
|
||||
}
|
||||
|
||||
|
||||
// Select the requested page via its id. This is fairly rare;
|
||||
// pages should usually be picked up via their title, but if you're
|
||||
// navigating to an instance, this is how you specify the page in a
|
||||
// URL. The return value is an array of Note objects; it is the
|
||||
// responsibility of client code to restructure these into a tree-like
|
||||
// object.
|
||||
//
|
||||
// Recommended: Clients should update the URL whenever changing
|
||||
// kasten.
|
||||
pub(crate) async fn select_kasten_by_slug<'a, E>(executor: E, slug: &NoteId) -> SqlResult<Vec<Note>>
|
||||
// page.
|
||||
pub(crate) async fn select_page_by_slug<'a, E>(executor: E, slug: &str) -> SqlResult<Vec<Note>>
|
||||
where
|
||||
E: Executor<'a, Database = Sqlite>,
|
||||
{
|
||||
let r: Vec<RowNote> = sqlx::query_as(&SELECT_KASTEN_BY_ID_SQL)
|
||||
.bind(&**slug)
|
||||
.fetch_all(executor)
|
||||
.await?;
|
||||
Ok(r.into_iter().map(|z| Note::from(z)).collect())
|
||||
select_object_by_query(executor, &SELECT_PAGE_BY_ID_SQL, &slug).await
|
||||
}
|
||||
|
||||
// Fetch the kasten by title. The return value is an array of Note
|
||||
// Fetch the page by title. The return value is an array of Note
|
||||
// objects; it is the responsibility of client code to restructure
|
||||
// these into a tree-like object.
|
||||
pub(crate) async fn select_kasten_by_title<'a, E>(executor: E, title: &str) -> SqlResult<Vec<Note>>
|
||||
pub(crate) async fn select_page_by_title<'a, E>(executor: E, title: &str) -> SqlResult<Vec<Note>>
|
||||
where
|
||||
E: Executor<'a, Database = Sqlite>,
|
||||
{
|
||||
let r: Vec<RowNote> = sqlx::query_as(&SELECT_KASTEN_BY_TITLE_SQL)
|
||||
.bind(&title)
|
||||
.fetch_all(executor)
|
||||
.await?;
|
||||
Ok(r.into_iter().map(|z| Note::from(z)).collect())
|
||||
select_object_by_query(executor, &SELECT_PAGE_BY_TITLE_SQL, &title).await
|
||||
}
|
||||
|
||||
// Fetch all backreferences to a kasten. The return value is an array
|
||||
// of arrays, and inside each array is a list from a root kasten to
|
||||
// the note that references the give kasten. Clients may choose how
|
||||
// Fetch all backreferences to a page. The return value is an array
|
||||
// of arrays, and inside each array is a list from a root page to
|
||||
// the note that references the give page. Clients may choose how
|
||||
// they want to display that collection.
|
||||
pub(crate) async fn select_backreferences_for_kasten<'a, E>(
|
||||
pub(crate) async fn select_backreferences_for_page<'a, E>(
|
||||
executor: E,
|
||||
kasten_id: &NoteId,
|
||||
page_id: &str,
|
||||
) -> SqlResult<Vec<Note>>
|
||||
where
|
||||
E: Executor<'a, Database = Sqlite>,
|
||||
{
|
||||
let r: Vec<RowNote> = sqlx::query_as(&SELECT_NOTES_BACKREFENCING_KASTEN_SQL)
|
||||
.bind(&**kasten_id)
|
||||
.fetch_all(executor)
|
||||
.await?;
|
||||
Ok(r.into_iter().map(|z| Note::from(z)).collect())
|
||||
select_object_by_query(executor, &SELECT_NOTES_BACKREFERENCING_PAGE_SQL, &page_id).await
|
||||
}
|
||||
|
||||
// ___ _ ___ _ _ _
|
||||
|
@ -121,26 +125,55 @@ where
|
|||
//
|
||||
|
||||
// Inserts a single note into the notes table. That is all.
|
||||
pub(crate) async fn insert_note<'a, E>(executor: E, zettle: &NewNote) -> SqlResult<String>
|
||||
pub(crate) async fn insert_note<'a, E>(executor: E, note: &NewNote) -> SqlResult<String>
|
||||
where
|
||||
E: Executor<'a, Database = Sqlite>,
|
||||
{
|
||||
let insert_one_page_sql = concat!(
|
||||
let insert_one_note_sql = concat!(
|
||||
"INSERT INTO notes (id, content, kind, ",
|
||||
" creation_date, updated_date, lastview_date) ",
|
||||
"VALUES (?, ?, ?, ?, ?, ?);"
|
||||
);
|
||||
|
||||
let _ = sqlx::query(insert_one_page_sql)
|
||||
.bind(&zettle.id)
|
||||
.bind(&zettle.content)
|
||||
.bind(zettle.kind.to_string())
|
||||
.bind(&zettle.creation_date)
|
||||
.bind(&zettle.updated_date)
|
||||
.bind(&zettle.lastview_date)
|
||||
let _ = sqlx::query(insert_one_note_sql)
|
||||
.bind(¬e.id)
|
||||
.bind(¬e.content)
|
||||
.bind(note.kind.to_string())
|
||||
.bind(¬e.creation_date)
|
||||
.bind(¬e.updated_date)
|
||||
.bind(¬e.lastview_date)
|
||||
.execute(executor)
|
||||
.await?;
|
||||
Ok(zettle.id.clone())
|
||||
Ok(note.id.clone())
|
||||
}
|
||||
|
||||
// Inserts a single note into the notes table. That is all.
|
||||
pub(crate) async fn bulk_insert_notes<'a, E>(executor: E, notes: &[NewNote]) -> SqlResult<()>
|
||||
where
|
||||
E: Executor<'a, Database = Sqlite>,
|
||||
{
|
||||
if notes.is_empty() {
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
let insert_pattern = "VALUES (?, ?, ?, ?, ?, ?)".to_string();
|
||||
let insert_bulk_notes_sql =
|
||||
"INSERT INTO notes (id, content, kind, creation_date, updated_date, lastview_date) ".to_string()
|
||||
+ &[insert_pattern.as_str()]
|
||||
.repeat(notes.len())
|
||||
.join(", ") + &";".to_string();
|
||||
|
||||
let mut request = sqlx::query(&insert_bulk_notes_sql);
|
||||
for note in notes {
|
||||
request = request
|
||||
.bind(¬e.id)
|
||||
.bind(¬e.content)
|
||||
.bind(note.kind.to_string())
|
||||
.bind(¬e.creation_date)
|
||||
.bind(¬e.updated_date)
|
||||
.bind(¬e.lastview_date);
|
||||
}
|
||||
request.execute(executor).await.map(|_| ())
|
||||
}
|
||||
|
||||
// ___ _ _ _ _ __ _
|
||||
|
@ -181,7 +214,7 @@ where
|
|||
static ref RE_STRIP_NUM: Regex = Regex::new(r"-\d+$").unwrap();
|
||||
static ref SLUG_FINDER_SQL: String = format!(
|
||||
"SELECT id FROM notes WHERE kind = '{}' AND id LIKE '?%';",
|
||||
NoteKind::Kasten.to_string()
|
||||
NoteKind::Page.to_string()
|
||||
);
|
||||
}
|
||||
|
||||
|
@ -198,13 +231,13 @@ where
|
|||
})
|
||||
}
|
||||
|
||||
// A helper function: given a title and a slug, create a KastenType
|
||||
// A helper function: given a title and a slug, create a PageType
|
||||
// note.
|
||||
pub(crate) fn create_zettlekasten(title: &str, slug: &str) -> NewNote {
|
||||
pub(crate) fn create_page(title: &str, slug: &str) -> NewNote {
|
||||
NewNoteBuilder::default()
|
||||
.id(slug.to_string())
|
||||
.content(title.to_string())
|
||||
.kind(NoteKind::Kasten)
|
||||
.kind(NoteKind::Page)
|
||||
.build()
|
||||
.unwrap()
|
||||
}
|
||||
|
@ -245,8 +278,8 @@ where
|
|||
|
||||
pub(crate) async fn select_note_to_note_relationship<'a, E>(
|
||||
executor: E,
|
||||
parent_id: &ParentId,
|
||||
note_id: &NoteId,
|
||||
parent_id: &str,
|
||||
note_id: &str,
|
||||
) -> SqlResult<NoteRelationship>
|
||||
where
|
||||
E: Executor<'a, Database = Sqlite>,
|
||||
|
@ -258,8 +291,8 @@ where
|
|||
"LIMIT 1"
|
||||
);
|
||||
let s: NoteRelationshipRow = sqlx::query_as(get_note_to_note_relationship_sql)
|
||||
.bind(&**parent_id)
|
||||
.bind(&**note_id)
|
||||
.bind(parent_id)
|
||||
.bind(note_id)
|
||||
.fetch_one(executor)
|
||||
.await?;
|
||||
Ok(NoteRelationship::from(s))
|
||||
|
@ -273,8 +306,8 @@ where
|
|||
|
||||
pub(crate) async fn insert_note_to_note_relationship<'a, E>(
|
||||
executor: E,
|
||||
parent_id: &ParentId,
|
||||
note_id: &NoteId,
|
||||
parent_id: &str,
|
||||
note_id: &str,
|
||||
location: i64,
|
||||
kind: &RelationshipKind,
|
||||
) -> SqlResult<()>
|
||||
|
@ -287,10 +320,10 @@ where
|
|||
);
|
||||
|
||||
let _ = sqlx::query(insert_note_to_note_relationship_sql)
|
||||
.bind(&**parent_id)
|
||||
.bind(&**note_id)
|
||||
.bind(parent_id)
|
||||
.bind(note_id)
|
||||
.bind(&location)
|
||||
.bind(&kind.to_string())
|
||||
.bind(kind.to_string())
|
||||
.execute(executor)
|
||||
.await?;
|
||||
Ok(())
|
||||
|
@ -298,7 +331,7 @@ where
|
|||
|
||||
pub(crate) async fn make_room_for_new_note<'a, E>(
|
||||
executor: E,
|
||||
parent_id: &ParentId,
|
||||
parent_id: &str,
|
||||
location: i64,
|
||||
) -> SqlResult<()>
|
||||
where
|
||||
|
@ -312,15 +345,30 @@ where
|
|||
|
||||
let _ = sqlx::query(make_room_for_new_note_sql)
|
||||
.bind(&location)
|
||||
.bind(&**parent_id)
|
||||
.bind(parent_id)
|
||||
.execute(executor)
|
||||
.await?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub(crate) async fn determine_max_child_location_for_note<'a, E>(
|
||||
executor: E,
|
||||
note_id: &str,
|
||||
comp_loc: Option<i64>,
|
||||
) -> SqlResult<i64>
|
||||
where
|
||||
E: Executor<'a, Database = Sqlite>,
|
||||
{
|
||||
let row_count = assert_max_child_location_for_note(executor, note_id).await? + 1;
|
||||
Ok(match comp_loc {
|
||||
Some(location) => cmp::min(row_count, location),
|
||||
None => row_count
|
||||
})
|
||||
}
|
||||
|
||||
pub(crate) async fn assert_max_child_location_for_note<'a, E>(
|
||||
executor: E,
|
||||
note_id: &ParentId,
|
||||
note_id: &str,
|
||||
) -> SqlResult<i64>
|
||||
where
|
||||
E: Executor<'a, Database = Sqlite>,
|
||||
|
@ -329,7 +377,7 @@ where
|
|||
"SELECT MAX(location) AS count FROM note_relationships WHERE parent_id = ?;";
|
||||
|
||||
let count: RowCount = sqlx::query_as(assert_max_child_location_for_note_sql)
|
||||
.bind(&**note_id)
|
||||
.bind(note_id)
|
||||
.fetch_one(executor)
|
||||
.await?;
|
||||
|
||||
|
@ -342,10 +390,10 @@ where
|
|||
// |_|\_\___/\__\___| \__\___/ |_|\_\__,_/__/\__\___|_||_| |_|_\___|_\__,_|\__|_\___/_||_/__/_||_|_| .__/__/
|
||||
// |_|
|
||||
|
||||
pub(crate) async fn insert_bulk_note_to_kasten_relationships<'a, E>(
|
||||
pub(crate) async fn insert_bulk_note_to_page_relationships<'a, E>(
|
||||
executor: E,
|
||||
note_id: &NoteId,
|
||||
references: &[NoteId],
|
||||
note_id: &str,
|
||||
references: &[String],
|
||||
) -> SqlResult<()>
|
||||
where
|
||||
E: Executor<'a, Database = Sqlite>,
|
||||
|
@ -354,32 +402,32 @@ where
|
|||
return Ok(());
|
||||
}
|
||||
|
||||
let insert_pattern = format!("(?, ?, '{}')", KastenRelationshipKind::Kasten.to_string());
|
||||
let insert_pattern = format!("(?, ?, '{}')", PageRelationshipKind::Page.to_string());
|
||||
let insert_note_page_references_sql =
|
||||
"INSERT INTO note_kasten_relationships (note_id, kasten_id, kind) VALUES ".to_string()
|
||||
"INSERT INTO note_page_relationships (note_id, page_id, kind) VALUES ".to_string()
|
||||
+ &[insert_pattern.as_str()]
|
||||
.repeat(references.len())
|
||||
.join(", ") + &";".to_string();
|
||||
|
||||
let mut request = sqlx::query(&insert_note_page_references_sql);
|
||||
for reference in references {
|
||||
request = request.bind(&**note_id).bind(&**reference);
|
||||
request = request.bind(note_id).bind(reference);
|
||||
}
|
||||
|
||||
request.execute(executor).await.map(|_| ())
|
||||
}
|
||||
|
||||
pub(crate) async fn delete_bulk_note_to_kasten_relationships<'a, E>(
|
||||
pub(crate) async fn delete_bulk_note_to_page_relationships<'a, E>(
|
||||
executor: E,
|
||||
note_id: &NoteId,
|
||||
note_id: &str,
|
||||
) -> SqlResult<()>
|
||||
where
|
||||
E: Executor<'a, Database = Sqlite>,
|
||||
{
|
||||
let delete_note_to_kasten_relationship_sql =
|
||||
"DELETE FROM note_kasten_relationships WHERE and note_id = ?;";
|
||||
let _ = sqlx::query(delete_note_to_kasten_relationship_sql)
|
||||
.bind(&**note_id)
|
||||
let delete_note_to_page_relationship_sql =
|
||||
"DELETE FROM note_page_relationships WHERE and note_id = ?;";
|
||||
let _ = sqlx::query(delete_note_to_page_relationship_sql)
|
||||
.bind(note_id)
|
||||
.execute(executor)
|
||||
.await?;
|
||||
Ok(())
|
||||
|
@ -405,7 +453,7 @@ pub(crate) fn diff_references(
|
|||
// Returns all the (Id, title) pairs found in the database out of a
|
||||
// list of titles. Used by insert_note and update_note_content to
|
||||
// find the ids of all the references in a given document.
|
||||
pub(crate) async fn find_all_kasten_from_list_of_references<'a, E>(
|
||||
pub(crate) async fn find_all_page_from_list_of_references<'a, E>(
|
||||
executor: E,
|
||||
references: &[String],
|
||||
) -> SqlResult<Vec<PageTitle>>
|
||||
|
@ -419,7 +467,7 @@ where
|
|||
lazy_static! {
|
||||
static ref SELECT_ALL_REFERENCES_FOR_SQL_BASE: String = format!(
|
||||
"SELECT id, content FROM notes WHERE kind = '{}' AND content IN (",
|
||||
NoteKind::Kasten.to_string()
|
||||
NoteKind::Page.to_string()
|
||||
);
|
||||
}
|
||||
|
||||
|
@ -442,8 +490,8 @@ where
|
|||
|
||||
pub(crate) async fn delete_note_to_note_relationship<'a, E>(
|
||||
executor: E,
|
||||
parent_id: &ParentId,
|
||||
note_id: &NoteId,
|
||||
parent_id: &str,
|
||||
note_id: &str,
|
||||
) -> SqlResult<()>
|
||||
where
|
||||
E: Executor<'a, Database = Sqlite>,
|
||||
|
@ -454,8 +502,8 @@ where
|
|||
);
|
||||
|
||||
let count = sqlx::query(delete_note_to_note_relationship_sql)
|
||||
.bind(&**parent_id)
|
||||
.bind(&**note_id)
|
||||
.bind(parent_id)
|
||||
.bind(note_id)
|
||||
.execute(executor)
|
||||
.await?
|
||||
.rows_affected();
|
||||
|
@ -466,36 +514,36 @@ where
|
|||
}
|
||||
}
|
||||
|
||||
pub(crate) async fn delete_note_to_kasten_relationships<'a, E>(
|
||||
pub(crate) async fn delete_note_to_page_relationships<'a, E>(
|
||||
executor: E,
|
||||
note_id: &NoteId,
|
||||
note_id: &str,
|
||||
) -> SqlResult<()>
|
||||
where
|
||||
E: Executor<'a, Database = Sqlite>,
|
||||
{
|
||||
lazy_static! {
|
||||
static ref DELETE_NOTE_TO_KASTEN_RELATIONSHIPS_SQL: String = format!(
|
||||
static ref DELETE_NOTE_TO_PAGE_RELATIONSHIPS_SQL: String = format!(
|
||||
"DELETE FROM note_relationships WHERE kind in ('{}', '{}') AND parent_id = ?;",
|
||||
KastenRelationshipKind::Kasten.to_string(),
|
||||
KastenRelationshipKind::Unacked.to_string()
|
||||
PageRelationshipKind::Page.to_string(),
|
||||
PageRelationshipKind::Unacked.to_string()
|
||||
);
|
||||
}
|
||||
|
||||
let _ = sqlx::query(&DELETE_NOTE_TO_KASTEN_RELATIONSHIPS_SQL)
|
||||
.bind(&**note_id)
|
||||
let _ = sqlx::query(&DELETE_NOTE_TO_PAGE_RELATIONSHIPS_SQL)
|
||||
.bind(note_id)
|
||||
.execute(executor)
|
||||
.await?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub(crate) async fn delete_note<'a, E>(executor: E, note_id: &NoteId) -> SqlResult<()>
|
||||
pub(crate) async fn delete_note<'a, E>(executor: E, note_id: &str) -> SqlResult<()>
|
||||
where
|
||||
E: Executor<'a, Database = Sqlite>,
|
||||
{
|
||||
let delete_note_sql = "DELETE FROM notes WHERE note_id = ?";
|
||||
|
||||
let count = sqlx::query(delete_note_sql)
|
||||
.bind(&**note_id)
|
||||
.bind(note_id)
|
||||
.execute(executor)
|
||||
.await?
|
||||
.rows_affected();
|
||||
|
@ -511,7 +559,7 @@ where
|
|||
// sequential.
|
||||
pub(crate) async fn close_hole_for_deleted_note<'a, E>(
|
||||
executor: E,
|
||||
parent_id: &ParentId,
|
||||
parent_id: &str,
|
||||
location: i64,
|
||||
) -> SqlResult<()>
|
||||
where
|
||||
|
@ -525,12 +573,39 @@ where
|
|||
|
||||
let _ = sqlx::query(close_hole_for_deleted_note_sql)
|
||||
.bind(&location)
|
||||
.bind(&**parent_id)
|
||||
.bind(parent_id)
|
||||
.execute(executor)
|
||||
.await?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
|
||||
// Given a list of references found in the content, generate the
|
||||
// references that do not previously exist, returning all found
|
||||
// references. NOTE: The function signature for this is for a
|
||||
// transaction, and uses a nested transaction.
|
||||
pub(crate) async fn validate_or_generate_all_found_references(
|
||||
txi: &mut Transaction<'_, Sqlite>,
|
||||
references: &[String]
|
||||
) -> SqlResult<Vec<String>> {
|
||||
let mut tx = txi.begin().await?;
|
||||
|
||||
let found_references =
|
||||
find_all_page_from_list_of_references(&mut tx, &references).await?;
|
||||
let new_references = diff_references(&references, &found_references);
|
||||
let mut new_page: Vec<NewNote> = vec![];
|
||||
for one_reference in new_references.iter() {
|
||||
let slug = generate_slug(&mut tx, one_reference).await?;
|
||||
new_page.push(create_page(&one_reference, &slug));
|
||||
}
|
||||
let _ = bulk_insert_notes(&mut tx, &new_page).await?;
|
||||
|
||||
let mut all_reference_ids: Vec<String> = found_references.iter().map(|r| r.id.clone()).collect();
|
||||
all_reference_ids.append(&mut new_page.iter().map(|r| r.id.clone()).collect());
|
||||
tx.commit().await?;
|
||||
Ok(all_reference_ids)
|
||||
}
|
||||
|
||||
// __ __ _
|
||||
// | \/ (_)___ __
|
||||
// | |\/| | (_-</ _|
|
||||
|
@ -539,27 +614,15 @@ where
|
|||
|
||||
// The dreaded miscellaneous!
|
||||
|
||||
pub(crate) async fn count_existing_note_relationships(
|
||||
tx: &mut Transaction<'_, Sqlite>,
|
||||
note_id: &NoteId,
|
||||
) -> SqlResult<i64> {
|
||||
let mut txi = tx.begin().await?;
|
||||
pub(crate) async fn count_existing_note_relationships<'a, E>(executor: E, note_id: &str) -> SqlResult<i64>
|
||||
where
|
||||
E: Executor<'a, Database = Sqlite>,
|
||||
{
|
||||
let count_existing_note_relationships_sql =
|
||||
"SELECT COUNT(*) as count FROM note_relationships WHERE note_id = ?;";
|
||||
let _: RowCount = sqlx::query_as(&count_existing_note_relationships_sql)
|
||||
.bind(&**note_id)
|
||||
.fetch_one(&mut txi)
|
||||
let count: RowCount = sqlx::query_as(&count_existing_note_relationships_sql)
|
||||
.bind(note_id)
|
||||
.fetch_one(executor)
|
||||
.await?;
|
||||
|
||||
let count: RowCount = {
|
||||
let count_existing_note_relationships_sql =
|
||||
"SELECT COUNT(*) as count FROM note_relationships WHERE note_id = ?;";
|
||||
|
||||
sqlx::query_as(&count_existing_note_relationships_sql)
|
||||
.bind(&**note_id)
|
||||
.fetch_one(&mut txi)
|
||||
.await?
|
||||
};
|
||||
txi.commit().await?;
|
||||
Ok(count.count)
|
||||
}
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
DROP TABLE IF EXISTS notes;
|
||||
DROP TABLE IF EXISTS note_relationships;
|
||||
DROP TABLE IF EXISTS note_kasten_relationships;
|
||||
DROP TABLE IF EXISTS note_page_relationships;
|
||||
DROP TABLE IF EXISTS favorites;
|
||||
|
||||
CREATE TABLE notes (
|
||||
|
@ -21,9 +21,9 @@ CREATE TABLE favorites (
|
|||
FOREIGN KEY (id) REFERENCES notes (id) ON DELETE CASCADE
|
||||
);
|
||||
|
||||
-- This table represents the forest of data relating a kasten to its
|
||||
-- This table represents the forest of data relating a page to its
|
||||
-- collections of notes. The root is itself "a note," but the content
|
||||
-- of that note will always be just the title of the kasten.
|
||||
-- of that note will always be just the title of the page.
|
||||
--
|
||||
CREATE TABLE note_relationships (
|
||||
note_id TEXT NOT NULL,
|
||||
|
@ -37,22 +37,22 @@ CREATE TABLE note_relationships (
|
|||
CHECK (note_id <> parent_id)
|
||||
);
|
||||
|
||||
-- This table represents the graph of data relating notes to kastens.
|
||||
-- This table represents the graph of data relating notes to pages.
|
||||
--
|
||||
CREATE TABLE note_kasten_relationships (
|
||||
CREATE TABLE note_page_relationships (
|
||||
note_id TEXT NOT NULL,
|
||||
kasten_id TEXT NOT NULL,
|
||||
page_id TEXT NOT NULL,
|
||||
kind TEXT NOT NULL,
|
||||
-- If either note disappears, we want all the edges to disappear as well.
|
||||
FOREIGN KEY (note_id) REFERENCES notes (id) ON DELETE CASCADE,
|
||||
FOREIGN KEY (kasten_id) REFERENCES notes (id) ON DELETE CASCADE,
|
||||
UNIQUE (note_id, kasten_id),
|
||||
CHECK (note_id <> kasten_id)
|
||||
FOREIGN KEY (page_id) REFERENCES notes (id) ON DELETE CASCADE,
|
||||
UNIQUE (note_id, page_id),
|
||||
CHECK (note_id <> page_id)
|
||||
);
|
||||
|
||||
-- A fabulous constraint. This index prevents us from saying that
|
||||
-- if a note points to a kasten, the kasten may not point to a
|
||||
-- note. Now, it's absolutely required that a kasten_id point to
|
||||
-- a KastenType note; the content should be a title only.
|
||||
CREATE UNIQUE INDEX note_kasten_unique_idx
|
||||
ON note_kasten_relationships (MIN(note_id, kasten_id), MAX(note_id, kasten_id));
|
||||
-- if a note points to a page, the page may not point to a
|
||||
-- note. Now, it's absolutely required that a page_id point to
|
||||
-- a PageType note; the content should be a title only.
|
||||
CREATE UNIQUE INDEX note_page_unique_idx
|
||||
ON note_page_relationships (MIN(note_id, page_id), MAX(note_id, page_id));
|
||||
|
|
|
@ -45,8 +45,8 @@ FROM (
|
|||
ON note_parents.id = note_relationships.parent_id
|
||||
WHERE notes.id
|
||||
IN (SELECT note_id
|
||||
FROM note_kasten_relationships
|
||||
WHERE kasten_id = ?) -- IMPORTANT: THIS IS THE PARAMETER
|
||||
FROM note_page_relationships
|
||||
WHERE page_id = ?) -- IMPORTANT: THIS IS THE PARAMETER
|
||||
|
||||
UNION
|
||||
SELECT DISTINCT
|
|
@ -10,7 +10,7 @@
|
|||
//! sense in the future to separate the decomposition of the note
|
||||
//! content into a higher layer.
|
||||
//!
|
||||
//! Notesmachine storage notes consist of two items: Note and Kasten.
|
||||
//! Notesmachine storage notes consist of two items: Note and Page.
|
||||
//! This distinction is somewhat arbitrary, as structurally these two
|
||||
//! items are stored in the same table.
|
||||
//!
|
||||
|
@ -56,8 +56,6 @@ use crate::reference_parser::build_references;
|
|||
use crate::store::private::*;
|
||||
use crate::structs::*;
|
||||
use sqlx::sqlite::SqlitePool;
|
||||
use std::cmp;
|
||||
// use std::collections::HashMap;
|
||||
use std::sync::Arc;
|
||||
|
||||
/// A handle to our Sqlite database.
|
||||
|
@ -67,7 +65,7 @@ pub struct NoteStore(Arc<SqlitePool>);
|
|||
pub type NoteResult<T> = core::result::Result<T, NoteStoreError>;
|
||||
|
||||
// After wrestling for a while with the fact that 'box' is a reserved
|
||||
// word in Rust, I decided to just go with Note (note) and Kasten
|
||||
// word in Rust, I decided to just go with Note (note) and Page
|
||||
// (box).
|
||||
|
||||
impl NoteStore {
|
||||
|
@ -94,38 +92,34 @@ impl NoteStore {
|
|||
/// the slug, the slug is insufficient to generate a new page, so
|
||||
/// this use case says that in the event of a failure to find the
|
||||
/// requested page, return a basic NotFound.
|
||||
pub async fn get_kasten_by_slug(&self, slug: &str) -> NoteResult<(Vec<Note>, Vec<Note>)> {
|
||||
let kasten = select_kasten_by_slug(&*self.0, &NoteId(slug.to_string())).await?;
|
||||
if kasten.is_empty() {
|
||||
pub async fn get_page_by_slug(&self, slug: &str) -> NoteResult<(Vec<Note>, Vec<Note>)> {
|
||||
let page = select_page_by_slug(&*self.0, slug).await?;
|
||||
if page.is_empty() {
|
||||
return Err(NoteStoreError::NotFound);
|
||||
}
|
||||
|
||||
let note_id = NoteId(kasten[0].id.clone());
|
||||
Ok((
|
||||
kasten,
|
||||
select_backreferences_for_kasten(&*self.0, ¬e_id).await?,
|
||||
))
|
||||
let note_id = &page[0].id;
|
||||
let backreferences = select_backreferences_for_page(&*self.0, ¬e_id).await?;
|
||||
Ok((page, backreferences))
|
||||
}
|
||||
|
||||
/// Fetch page by title
|
||||
|
||||
///
|
||||
/// The most common use case: the user is navigating by requesting
|
||||
/// a page. The page either exists or it doesn't. If it
|
||||
/// doesn't, we go out and make it. Since we know it doesn't exist,
|
||||
/// we also know no backreferences to it exist, so in that case you
|
||||
/// get back two empty vecs.
|
||||
pub async fn get_kasten_by_title(&self, title: &str) -> NoteResult<(Vec<Note>, Vec<Note>)> {
|
||||
pub async fn get_page_by_title(&self, title: &str) -> NoteResult<(Vec<Note>, Vec<Note>)> {
|
||||
if title.len() == 0 {
|
||||
return Err(NoteStoreError::NotFound);
|
||||
}
|
||||
|
||||
let kasten = select_kasten_by_title(&*self.0, title).await?;
|
||||
if kasten.len() > 0 {
|
||||
let note_id = NoteId(kasten[0].id.clone());
|
||||
return Ok((
|
||||
kasten,
|
||||
select_backreferences_for_kasten(&*self.0, ¬e_id).await?,
|
||||
));
|
||||
let page = select_page_by_title(&*self.0, title).await?;
|
||||
if page.len() > 0 {
|
||||
let note_id = &page[0].id;
|
||||
let backreferences = select_backreferences_for_page(&*self.0, ¬e_id).await?;
|
||||
return Ok((page, backreferences));
|
||||
}
|
||||
|
||||
// Sanity check!
|
||||
|
@ -138,11 +132,11 @@ impl NoteStore {
|
|||
|
||||
let mut tx = self.0.begin().await?;
|
||||
let slug = generate_slug(&mut tx, title).await?;
|
||||
let zettlekasten = create_zettlekasten(&title, &slug);
|
||||
let _ = insert_note(&mut tx, &zettlekasten).await?;
|
||||
let page = create_page(&title, &slug);
|
||||
let _ = insert_note(&mut tx, &page).await?;
|
||||
tx.commit().await?;
|
||||
|
||||
Ok((vec![Note::from(zettlekasten)], vec![]))
|
||||
Ok((vec![Note::from(page)], vec![]))
|
||||
}
|
||||
|
||||
pub async fn add_note(
|
||||
|
@ -151,14 +145,8 @@ impl NoteStore {
|
|||
parent_id: &str,
|
||||
location: Option<i64>,
|
||||
) -> NoteResult<String> {
|
||||
let new_id = self
|
||||
.insert_note(
|
||||
note,
|
||||
&ParentId(parent_id.to_string()),
|
||||
location,
|
||||
RelationshipKind::Direct,
|
||||
)
|
||||
.await?;
|
||||
let kind = RelationshipKind::Direct;
|
||||
let new_id = self.insert_note(note, parent_id, location, kind).await?;
|
||||
Ok(new_id)
|
||||
}
|
||||
|
||||
|
@ -172,19 +160,13 @@ impl NoteStore {
|
|||
) -> NoteResult<()> {
|
||||
let mut tx = self.0.begin().await?;
|
||||
|
||||
let old_parent_id = ParentId(old_parent_id.to_string());
|
||||
let new_parent_id = ParentId(new_parent_id.to_string());
|
||||
let note_id = NoteId(note_id.to_string());
|
||||
|
||||
let old_note = select_note_to_note_relationship(&mut tx, &old_parent_id, ¬e_id).await?;
|
||||
let old_note_location = old_note.location;
|
||||
let old_note_kind = old_note.kind;
|
||||
|
||||
let _ = delete_note_to_note_relationship(&mut tx, &old_parent_id, ¬e_id).await?;
|
||||
let _ = close_hole_for_deleted_note(&mut tx, &old_parent_id, old_note_location).await?;
|
||||
let parent_max_location =
|
||||
assert_max_child_location_for_note(&mut tx, &new_parent_id).await?;
|
||||
let new_location = cmp::min(parent_max_location + 1, new_location);
|
||||
let new_location = determine_max_child_location_for_note(&mut tx, &new_parent_id, Some(new_location)).await?;
|
||||
let _ = make_room_for_new_note(&mut tx, &new_parent_id, new_location).await?;
|
||||
let _ = insert_note_to_note_relationship(
|
||||
&mut tx,
|
||||
|
@ -206,25 +188,9 @@ impl NoteStore {
|
|||
|
||||
let mut tx = self.0.begin().await?;
|
||||
let _ = update_note_content(&mut tx, ¬e_id, &content).await?;
|
||||
let _ = delete_bulk_note_to_kasten_relationships(&mut tx, ¬e_id).await?;
|
||||
let found_references =
|
||||
find_all_kasten_from_list_of_references(&mut tx, &references).await?;
|
||||
let new_references = diff_references(&references, &found_references);
|
||||
let mut known_reference_ids: Vec<NoteId> = Vec::new();
|
||||
for one_reference in new_references.iter() {
|
||||
let slug = generate_slug(&mut tx, one_reference).await?;
|
||||
let zettlekasten = create_zettlekasten(&one_reference, &slug);
|
||||
let _ = insert_note(&mut tx, &zettlekasten).await?;
|
||||
known_reference_ids.push(NoteId(slug));
|
||||
}
|
||||
|
||||
known_reference_ids.append(
|
||||
&mut found_references
|
||||
.iter()
|
||||
.map(|r| NoteId(r.id.clone()))
|
||||
.collect(),
|
||||
);
|
||||
let _ = insert_bulk_note_to_kasten_relationships(&mut tx, ¬e_id, &known_reference_ids)
|
||||
let _ = delete_bulk_note_to_page_relationships(&mut tx, ¬e_id).await?;
|
||||
let known_reference_ids = validate_or_generate_all_found_references(&mut tx, &references).await?;
|
||||
let _ = insert_bulk_note_to_page_relationships(&mut tx, ¬e_id, &known_reference_ids)
|
||||
.await?;
|
||||
tx.commit().await?;
|
||||
Ok(())
|
||||
|
@ -243,7 +209,7 @@ impl NoteStore {
|
|||
// The big one: if zero parents report having an interest in this note, then it,
|
||||
// *and any sub-relationships*, go away.
|
||||
if count_existing_note_relationships(&mut tx, ¬e_id).await? == 0 {
|
||||
let _ = delete_note_to_kasten_relationships(&mut tx, ¬e_id).await?;
|
||||
let _ = delete_note_to_page_relationships(&mut tx, ¬e_id).await?;
|
||||
let _ = delete_note(&mut tx, ¬e_id).await?;
|
||||
}
|
||||
tx.commit().await?;
|
||||
|
@ -259,7 +225,7 @@ impl NoteStore {
|
|||
async fn insert_note(
|
||||
&self,
|
||||
note: &NewNote,
|
||||
parent_id: &ParentId,
|
||||
parent_id: &str,
|
||||
location: Option<i64>,
|
||||
kind: RelationshipKind,
|
||||
) -> NoteResult<String> {
|
||||
|
@ -292,38 +258,13 @@ impl NoteStore {
|
|||
let references = build_references(¬e.content);
|
||||
|
||||
let mut tx = self.0.begin().await?;
|
||||
let location = {
|
||||
let max_child = assert_max_child_location_for_note(&mut tx, parent_id).await? + 1;
|
||||
if let Some(location) = location {
|
||||
cmp::min(max_child, location)
|
||||
} else {
|
||||
max_child
|
||||
}
|
||||
};
|
||||
|
||||
let location = determine_max_child_location_for_note(&mut tx, parent_id, location).await?;
|
||||
let note_id = NoteId(note.id.clone());
|
||||
insert_note(&mut tx, ¬e).await?;
|
||||
make_room_for_new_note(&mut tx, &parent_id, location).await?;
|
||||
insert_note_to_note_relationship(&mut tx, &parent_id, ¬e_id, location, &kind).await?;
|
||||
|
||||
let found_references =
|
||||
find_all_kasten_from_list_of_references(&mut tx, &references).await?;
|
||||
let new_references = diff_references(&references, &found_references);
|
||||
let mut known_reference_ids: Vec<NoteId> = Vec::new();
|
||||
for one_reference in new_references.iter() {
|
||||
let slug = generate_slug(&mut tx, one_reference).await?;
|
||||
let zettlekasten = create_zettlekasten(&one_reference, &slug);
|
||||
let _ = insert_note(&mut tx, &zettlekasten).await?;
|
||||
known_reference_ids.push(NoteId(slug));
|
||||
}
|
||||
|
||||
known_reference_ids.append(
|
||||
&mut found_references
|
||||
.iter()
|
||||
.map(|r| NoteId(r.id.clone()))
|
||||
.collect(),
|
||||
);
|
||||
let _ = insert_bulk_note_to_kasten_relationships(&mut tx, ¬e_id, &known_reference_ids)
|
||||
let known_reference_ids = validate_or_generate_all_found_references(&mut tx, &references).await?;
|
||||
let _ = insert_bulk_note_to_page_relationships(&mut tx, ¬e_id, &known_reference_ids)
|
||||
.await?;
|
||||
tx.commit().await?;
|
||||
Ok(note_id.to_string())
|
||||
|
|
|
@ -4,8 +4,8 @@ use friendly_id;
|
|||
use shrinkwraprs::Shrinkwrap;
|
||||
use sqlx::{self, FromRow};
|
||||
|
||||
// Kasten is German for "Box," and is used both because this is
|
||||
// supposed to be a Zettlekasten, and because "Box" is a heavily
|
||||
// Page is German for "Box," and is used both because this is
|
||||
// supposed to be a Page, and because "Box" is a heavily
|
||||
// reserved word in Rust. So, for that matter, are "crate" and
|
||||
// "cargo," "cell," and so forth. If I'd wanted to go the Full
|
||||
// Noguchi, I guess I could have used "envelope."
|
||||
|
@ -60,7 +60,7 @@ pub(crate) struct ParentId(pub String);
|
|||
|
||||
build_conversion_enums!(
|
||||
NoteKind,
|
||||
"box" => Kasten,
|
||||
"box" => Page,
|
||||
"note" => Note,
|
||||
"resource" => Resource,
|
||||
);
|
||||
|
@ -78,8 +78,8 @@ build_conversion_enums!(
|
|||
);
|
||||
|
||||
build_conversion_enums!(
|
||||
KastenRelationshipKind,
|
||||
"kasten" => Kasten,
|
||||
PageRelationshipKind,
|
||||
"page" => Page,
|
||||
"unacked" => Unacked,
|
||||
"cancelled" => Cancelled,
|
||||
);
|
||||
|
@ -136,7 +136,7 @@ impl From<RowNote> for Note {
|
|||
/// A new Note object as it's inserted into the system. It has no
|
||||
/// parent or location information; those are data relative to the
|
||||
/// parent, and must be provided by the client. In the case of a
|
||||
/// Kasten, no location or parent is necessary.
|
||||
/// Page, no location or parent is necessary.
|
||||
#[derive(Clone, Debug, Builder)]
|
||||
pub struct NewNote {
|
||||
#[builder(default = r#"friendly_id::create()"#)]
|
||||
|
@ -155,8 +155,8 @@ pub struct NewNote {
|
|||
}
|
||||
|
||||
impl From<NewNote> for Note {
|
||||
/// Only used for building new kastens, so the decision- making is
|
||||
/// limited to kasten-level things, like pointing to self and
|
||||
/// Only used for building new pages, so the decision- making is
|
||||
/// limited to page-level things, like pointing to self and
|
||||
/// having a location of zero.
|
||||
fn from(note: NewNote) -> Self {
|
||||
Self {
|
||||
|
@ -217,25 +217,25 @@ impl From<NoteRelationshipRow> for NoteRelationship {
|
|||
}
|
||||
|
||||
#[derive(Clone, Debug, FromRow)]
|
||||
pub(crate) struct KastenRelationshipRow {
|
||||
pub(crate) struct PageRelationshipRow {
|
||||
pub note_id: String,
|
||||
pub kasten_id: String,
|
||||
pub page_id: String,
|
||||
pub kind: String,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct KastenRelationship {
|
||||
pub struct PageRelationship {
|
||||
pub note_id: String,
|
||||
pub kasten_id: String,
|
||||
pub kind: KastenRelationshipKind,
|
||||
pub page_id: String,
|
||||
pub kind: PageRelationshipKind,
|
||||
}
|
||||
|
||||
impl From<KastenRelationshipRow> for KastenRelationship {
|
||||
fn from(rel: KastenRelationshipRow) -> Self {
|
||||
impl From<PageRelationshipRow> for PageRelationship {
|
||||
fn from(rel: PageRelationshipRow) -> Self {
|
||||
Self {
|
||||
kasten_id: rel.kasten_id,
|
||||
page_id: rel.page_id,
|
||||
note_id: rel.note_id,
|
||||
kind: KastenRelationshipKind::from(rel.kind),
|
||||
kind: PageRelationshipKind::from(rel.kind),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue