It has the secret for nested transactions.
This commit is contained in:
parent
ec81083aa9
commit
cf6f906fa4
|
@ -2,7 +2,7 @@ use crate::structs::*;
|
||||||
use lazy_static::lazy_static;
|
use lazy_static::lazy_static;
|
||||||
use regex::Regex;
|
use regex::Regex;
|
||||||
use slug::slugify;
|
use slug::slugify;
|
||||||
use sqlx::{sqlite::Sqlite, Done, Executor};
|
use sqlx::{sqlite::Sqlite, Acquire, Done, Executor, Transaction};
|
||||||
use std::collections::HashSet;
|
use std::collections::HashSet;
|
||||||
|
|
||||||
type SqlResult<T> = sqlx::Result<T>;
|
type SqlResult<T> = sqlx::Result<T>;
|
||||||
|
@ -19,24 +19,24 @@ type SqlResult<T> = sqlx::Result<T>;
|
||||||
// of the SQL queries.
|
// of the SQL queries.
|
||||||
|
|
||||||
lazy_static! {
|
lazy_static! {
|
||||||
static ref SELECT_KASTEN_BY_TITLE_SQL: String = str::replace(
|
static ref SELECT_KASTEN_BY_TITLE_SQL: String = str::replace(
|
||||||
include_str!("sql/select_notes_by_parameter.sql"),
|
include_str!("sql/select_notes_by_parameter.sql"),
|
||||||
"QUERYPARAMETER",
|
"QUERYPARAMETER",
|
||||||
"notes.content"
|
"notes.content"
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
lazy_static! {
|
lazy_static! {
|
||||||
static ref SELECT_KASTEN_BY_ID_SQL: String = str::replace(
|
static ref SELECT_KASTEN_BY_ID_SQL: String = str::replace(
|
||||||
include_str!("sql/select_notes_by_parameter.sql"),
|
include_str!("sql/select_notes_by_parameter.sql"),
|
||||||
"QUERYPARAMETER",
|
"QUERYPARAMETER",
|
||||||
"notes.id"
|
"notes.id"
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
lazy_static! {
|
lazy_static! {
|
||||||
static ref SELECT_NOTES_BACKREFENCING_KASTEN_SQL: &'static str =
|
static ref SELECT_NOTES_BACKREFENCING_KASTEN_SQL: &'static str =
|
||||||
include_str!("sql/select_notes_backreferencing_kasten.sql");
|
include_str!("sql/select_notes_backreferencing_kasten.sql");
|
||||||
}
|
}
|
||||||
|
|
||||||
// ___ _
|
// ___ _
|
||||||
|
@ -47,10 +47,13 @@ lazy_static! {
|
||||||
|
|
||||||
pub(crate) async fn reset_database<'a, E>(executor: E) -> SqlResult<()>
|
pub(crate) async fn reset_database<'a, E>(executor: E) -> SqlResult<()>
|
||||||
where
|
where
|
||||||
E: Executor<'a, Database = Sqlite>,
|
E: Executor<'a, Database = Sqlite>,
|
||||||
{
|
{
|
||||||
let initialize_sql = include_str!("sql/initialize_database.sql");
|
let initialize_sql = include_str!("sql/initialize_database.sql");
|
||||||
sqlx::query(initialize_sql).execute(executor).await.map(|_| ())
|
sqlx::query(initialize_sql)
|
||||||
|
.execute(executor)
|
||||||
|
.await
|
||||||
|
.map(|_| ())
|
||||||
}
|
}
|
||||||
|
|
||||||
// ___ _ _ _ __ _
|
// ___ _ _ _ __ _
|
||||||
|
@ -70,13 +73,13 @@ where
|
||||||
// kasten.
|
// kasten.
|
||||||
pub(crate) async fn select_kasten_by_slug<'a, E>(executor: E, slug: &NoteId) -> SqlResult<Vec<Note>>
|
pub(crate) async fn select_kasten_by_slug<'a, E>(executor: E, slug: &NoteId) -> SqlResult<Vec<Note>>
|
||||||
where
|
where
|
||||||
E: Executor<'a, Database = Sqlite>,
|
E: Executor<'a, Database = Sqlite>,
|
||||||
{
|
{
|
||||||
let r: Vec<RowNote> = sqlx::query_as(&SELECT_KASTEN_BY_ID_SQL)
|
let r: Vec<RowNote> = sqlx::query_as(&SELECT_KASTEN_BY_ID_SQL)
|
||||||
.bind(&**slug)
|
.bind(&**slug)
|
||||||
.fetch_all(executor)
|
.fetch_all(executor)
|
||||||
.await?;
|
.await?;
|
||||||
Ok(r.into_iter().map(|z| Note::from(z)).collect())
|
Ok(r.into_iter().map(|z| Note::from(z)).collect())
|
||||||
}
|
}
|
||||||
|
|
||||||
// Fetch the kasten by title. The return value is an array of Note
|
// Fetch the kasten by title. The return value is an array of Note
|
||||||
|
@ -84,28 +87,31 @@ where
|
||||||
// these into a tree-like object.
|
// 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_kasten_by_title<'a, E>(executor: E, title: &str) -> SqlResult<Vec<Note>>
|
||||||
where
|
where
|
||||||
E: Executor<'a, Database = Sqlite>,
|
E: Executor<'a, Database = Sqlite>,
|
||||||
{
|
{
|
||||||
let r: Vec<RowNote> = sqlx::query_as(&SELECT_KASTEN_BY_TITLE_SQL)
|
let r: Vec<RowNote> = sqlx::query_as(&SELECT_KASTEN_BY_TITLE_SQL)
|
||||||
.bind(&title)
|
.bind(&title)
|
||||||
.fetch_all(executor)
|
.fetch_all(executor)
|
||||||
.await?;
|
.await?;
|
||||||
Ok(r.into_iter().map(|z| Note::from(z)).collect())
|
Ok(r.into_iter().map(|z| Note::from(z)).collect())
|
||||||
}
|
}
|
||||||
|
|
||||||
// Fetch all backreferences to a kasten. The return value is an array
|
// 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
|
// 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
|
// the note that references the give kasten. Clients may choose how
|
||||||
// they want to display that collection.
|
// they want to display that collection.
|
||||||
pub(crate) async fn select_backreferences_for_kasten<'a, E>(executor: E, kasten_id: &NoteId) -> SqlResult<Vec<Note>>
|
pub(crate) async fn select_backreferences_for_kasten<'a, E>(
|
||||||
|
executor: E,
|
||||||
|
kasten_id: &NoteId,
|
||||||
|
) -> SqlResult<Vec<Note>>
|
||||||
where
|
where
|
||||||
E: Executor<'a, Database = Sqlite>,
|
E: Executor<'a, Database = Sqlite>,
|
||||||
{
|
{
|
||||||
let r: Vec<RowNote> = sqlx::query_as(&SELECT_NOTES_BACKREFENCING_KASTEN_SQL)
|
let r: Vec<RowNote> = sqlx::query_as(&SELECT_NOTES_BACKREFENCING_KASTEN_SQL)
|
||||||
.bind(&**kasten_id)
|
.bind(&**kasten_id)
|
||||||
.fetch_all(executor)
|
.fetch_all(executor)
|
||||||
.await?;
|
.await?;
|
||||||
Ok(r.into_iter().map(|z| Note::from(z)).collect())
|
Ok(r.into_iter().map(|z| Note::from(z)).collect())
|
||||||
}
|
}
|
||||||
|
|
||||||
// ___ _ ___ _ _ _
|
// ___ _ ___ _ _ _
|
||||||
|
@ -117,24 +123,24 @@ where
|
||||||
// Inserts a single note into the notes table. That is all.
|
// 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, zettle: &NewNote) -> SqlResult<String>
|
||||||
where
|
where
|
||||||
E: Executor<'a, Database = Sqlite>,
|
E: Executor<'a, Database = Sqlite>,
|
||||||
{
|
{
|
||||||
let insert_one_page_sql = concat!(
|
let insert_one_page_sql = concat!(
|
||||||
"INSERT INTO notes (id, content, kind, ",
|
"INSERT INTO notes (id, content, kind, ",
|
||||||
" creation_date, updated_date, lastview_date) ",
|
" creation_date, updated_date, lastview_date) ",
|
||||||
"VALUES (?, ?, ?, ?, ?, ?);"
|
"VALUES (?, ?, ?, ?, ?, ?);"
|
||||||
);
|
);
|
||||||
|
|
||||||
let _ = sqlx::query(insert_one_page_sql)
|
let _ = sqlx::query(insert_one_page_sql)
|
||||||
.bind(&zettle.id)
|
.bind(&zettle.id)
|
||||||
.bind(&zettle.content)
|
.bind(&zettle.content)
|
||||||
.bind(zettle.kind.to_string())
|
.bind(zettle.kind.to_string())
|
||||||
.bind(&zettle.creation_date)
|
.bind(&zettle.creation_date)
|
||||||
.bind(&zettle.updated_date)
|
.bind(&zettle.updated_date)
|
||||||
.bind(&zettle.lastview_date)
|
.bind(&zettle.lastview_date)
|
||||||
.execute(executor)
|
.execute(executor)
|
||||||
.await?;
|
.await?;
|
||||||
Ok(zettle.id.clone())
|
Ok(zettle.id.clone())
|
||||||
}
|
}
|
||||||
|
|
||||||
// ___ _ _ _ _ __ _
|
// ___ _ _ _ _ __ _
|
||||||
|
@ -146,21 +152,21 @@ where
|
||||||
// Given a possible slug, find the slug with the highest
|
// Given a possible slug, find the slug with the highest
|
||||||
// uniquification number, and return that number, if any.
|
// uniquification number, and return that number, if any.
|
||||||
pub(crate) fn find_maximal_slug_number(slugs: &[JustId]) -> Option<u32> {
|
pub(crate) fn find_maximal_slug_number(slugs: &[JustId]) -> Option<u32> {
|
||||||
lazy_static! {
|
lazy_static! {
|
||||||
static ref RE_CAP_NUM: Regex = Regex::new(r"-(\d+)$").unwrap();
|
static ref RE_CAP_NUM: Regex = Regex::new(r"-(\d+)$").unwrap();
|
||||||
}
|
}
|
||||||
|
|
||||||
if slugs.is_empty() {
|
if slugs.is_empty() {
|
||||||
return None;
|
return None;
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut slug_counters: Vec<u32> = slugs
|
let mut slug_counters: Vec<u32> = slugs
|
||||||
.iter()
|
.iter()
|
||||||
.filter_map(|slug| RE_CAP_NUM.captures(&slug.id))
|
.filter_map(|slug| RE_CAP_NUM.captures(&slug.id))
|
||||||
.map(|cap| cap.get(1).unwrap().as_str().parse::<u32>().unwrap())
|
.map(|cap| cap.get(1).unwrap().as_str().parse::<u32>().unwrap())
|
||||||
.collect();
|
.collect();
|
||||||
slug_counters.sort_unstable();
|
slug_counters.sort_unstable();
|
||||||
slug_counters.pop()
|
slug_counters.pop()
|
||||||
}
|
}
|
||||||
|
|
||||||
// Given an initial string and an existing collection of slugs,
|
// Given an initial string and an existing collection of slugs,
|
||||||
|
@ -169,38 +175,38 @@ pub(crate) fn find_maximal_slug_number(slugs: &[JustId]) -> Option<u32> {
|
||||||
// isn't all that.
|
// isn't all that.
|
||||||
pub(crate) async fn generate_slug<'a, E>(executor: E, title: &str) -> SqlResult<String>
|
pub(crate) async fn generate_slug<'a, E>(executor: E, title: &str) -> SqlResult<String>
|
||||||
where
|
where
|
||||||
E: Executor<'a, Database = Sqlite>,
|
E: Executor<'a, Database = Sqlite>,
|
||||||
{
|
{
|
||||||
lazy_static! {
|
lazy_static! {
|
||||||
static ref RE_STRIP_NUM: Regex = Regex::new(r"-\d+$").unwrap();
|
static ref RE_STRIP_NUM: Regex = Regex::new(r"-\d+$").unwrap();
|
||||||
static ref SLUG_FINDER_SQL: String = format!(
|
static ref SLUG_FINDER_SQL: String = format!(
|
||||||
"SELECT id FROM notes WHERE kind = '{}' AND id LIKE '?%';",
|
"SELECT id FROM notes WHERE kind = '{}' AND id LIKE '?%';",
|
||||||
NoteKind::Kasten.to_string()
|
NoteKind::Kasten.to_string()
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
let initial_slug = slugify(title);
|
let initial_slug = slugify(title);
|
||||||
let sample_slug = RE_STRIP_NUM.replace_all(&initial_slug, "");
|
let sample_slug = RE_STRIP_NUM.replace_all(&initial_slug, "");
|
||||||
let similar_slugs: Vec<JustId> = sqlx::query_as(&SLUG_FINDER_SQL)
|
let similar_slugs: Vec<JustId> = sqlx::query_as(&SLUG_FINDER_SQL)
|
||||||
.bind(&*sample_slug)
|
.bind(&*sample_slug)
|
||||||
.fetch_all(executor)
|
.fetch_all(executor)
|
||||||
.await?;
|
.await?;
|
||||||
let maximal_slug_number = find_maximal_slug_number(&similar_slugs);
|
let maximal_slug_number = find_maximal_slug_number(&similar_slugs);
|
||||||
Ok(match maximal_slug_number {
|
Ok(match maximal_slug_number {
|
||||||
None => initial_slug,
|
None => initial_slug,
|
||||||
Some(slug_number) => format!("{}-{}", initial_slug, slug_number + 1),
|
Some(slug_number) => format!("{}-{}", initial_slug, slug_number + 1),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
// A helper function: given a title and a slug, create a KastenType
|
// A helper function: given a title and a slug, create a KastenType
|
||||||
// note.
|
// note.
|
||||||
pub(crate) fn create_zettlekasten(title: &str, slug: &str) -> NewNote {
|
pub(crate) fn create_zettlekasten(title: &str, slug: &str) -> NewNote {
|
||||||
NewNoteBuilder::default()
|
NewNoteBuilder::default()
|
||||||
.id(slug.to_string())
|
.id(slug.to_string())
|
||||||
.content(title.to_string())
|
.content(title.to_string())
|
||||||
.kind(NoteKind::Kasten)
|
.kind(NoteKind::Kasten)
|
||||||
.build()
|
.build()
|
||||||
.unwrap()
|
.unwrap()
|
||||||
}
|
}
|
||||||
|
|
||||||
// _ _ _ _ ___ _ _ _
|
// _ _ _ _ ___ _ _ _
|
||||||
|
@ -209,22 +215,26 @@ pub(crate) fn create_zettlekasten(title: &str, slug: &str) -> NewNote {
|
||||||
// \___/| .__/\__,_\__,_|\__\___| \___/|_||_\___| |_|\_\___/\__\___|
|
// \___/| .__/\__,_\__,_|\__\___| \___/|_||_\___| |_|\_\___/\__\___|
|
||||||
// |_|
|
// |_|
|
||||||
|
|
||||||
pub(crate) async fn update_note_content<'a, E>(executor: E, note_id: &NoteId, content: &str) -> SqlResult<()>
|
pub(crate) async fn update_note_content<'a, E>(
|
||||||
|
executor: E,
|
||||||
|
note_id: &NoteId,
|
||||||
|
content: &str,
|
||||||
|
) -> SqlResult<()>
|
||||||
where
|
where
|
||||||
E: Executor<'a, Database = Sqlite>,
|
E: Executor<'a, Database = Sqlite>,
|
||||||
{
|
{
|
||||||
let update_note_content_sql = "UPDATE notes SET content = ? WHERE note_id = ?";
|
let update_note_content_sql = "UPDATE notes SET content = ? WHERE note_id = ?";
|
||||||
let count = sqlx::query(update_note_content_sql)
|
let count = sqlx::query(update_note_content_sql)
|
||||||
.bind(content)
|
.bind(content)
|
||||||
.bind(&**note_id)
|
.bind(&**note_id)
|
||||||
.execute(executor)
|
.execute(executor)
|
||||||
.await?
|
.await?
|
||||||
.rows_affected();
|
.rows_affected();
|
||||||
|
|
||||||
match count {
|
match count {
|
||||||
1 => Ok(()),
|
1 => Ok(()),
|
||||||
_ => Err(sqlx::Error::RowNotFound),
|
_ => Err(sqlx::Error::RowNotFound),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// ___ _ _ ___ _ _ _ ___ _ _ _ _ _
|
// ___ _ _ ___ _ _ _ ___ _ _ _ _ _
|
||||||
|
@ -234,25 +244,25 @@ where
|
||||||
// |_|
|
// |_|
|
||||||
|
|
||||||
pub(crate) async fn select_note_to_note_relationship<'a, E>(
|
pub(crate) async fn select_note_to_note_relationship<'a, E>(
|
||||||
executor: E,
|
executor: E,
|
||||||
parent_id: &ParentId,
|
parent_id: &ParentId,
|
||||||
note_id: &NoteId,
|
note_id: &NoteId,
|
||||||
) -> SqlResult<NoteRelationship>
|
) -> SqlResult<NoteRelationship>
|
||||||
where
|
where
|
||||||
E: Executor<'a, Database = Sqlite>,
|
E: Executor<'a, Database = Sqlite>,
|
||||||
{
|
{
|
||||||
let get_note_to_note_relationship_sql = concat!(
|
let get_note_to_note_relationship_sql = concat!(
|
||||||
"SELECT parent_id, note_id, location, kind ",
|
"SELECT parent_id, note_id, location, kind ",
|
||||||
"FROM note_relationships ",
|
"FROM note_relationships ",
|
||||||
"WHERE parent_id = ? and note_id = ? ",
|
"WHERE parent_id = ? and note_id = ? ",
|
||||||
"LIMIT 1"
|
"LIMIT 1"
|
||||||
);
|
);
|
||||||
let s: NoteRelationshipRow = sqlx::query_as(get_note_to_note_relationship_sql)
|
let s: NoteRelationshipRow = sqlx::query_as(get_note_to_note_relationship_sql)
|
||||||
.bind(&**parent_id)
|
.bind(&**parent_id)
|
||||||
.bind(&**note_id)
|
.bind(&**note_id)
|
||||||
.fetch_one(executor)
|
.fetch_one(executor)
|
||||||
.await?;
|
.await?;
|
||||||
Ok(NoteRelationship::from(s))
|
Ok(NoteRelationship::from(s))
|
||||||
}
|
}
|
||||||
|
|
||||||
// _ _ _ _ _ _ _ ___ _ _ _ _ _
|
// _ _ _ _ _ _ _ ___ _ _ _ _ _
|
||||||
|
@ -262,61 +272,68 @@ where
|
||||||
// |_|
|
// |_|
|
||||||
|
|
||||||
pub(crate) async fn insert_note_to_note_relationship<'a, E>(
|
pub(crate) async fn insert_note_to_note_relationship<'a, E>(
|
||||||
executor: E,
|
executor: E,
|
||||||
parent_id: &ParentId,
|
parent_id: &ParentId,
|
||||||
note_id: &NoteId,
|
note_id: &NoteId,
|
||||||
location: i64,
|
location: i64,
|
||||||
kind: &RelationshipKind,
|
kind: &RelationshipKind,
|
||||||
) -> SqlResult<()>
|
) -> SqlResult<()>
|
||||||
where
|
where
|
||||||
E: Executor<'a, Database = Sqlite>,
|
E: Executor<'a, Database = Sqlite>,
|
||||||
{
|
{
|
||||||
let insert_note_to_note_relationship_sql = concat!(
|
let insert_note_to_note_relationship_sql = concat!(
|
||||||
"INSERT INTO note_relationships (parent_id, note_id, location, kind) ",
|
"INSERT INTO note_relationships (parent_id, note_id, location, kind) ",
|
||||||
"values (?, ?, ?, ?)"
|
"values (?, ?, ?, ?)"
|
||||||
);
|
);
|
||||||
|
|
||||||
let _ = sqlx::query(insert_note_to_note_relationship_sql)
|
let _ = sqlx::query(insert_note_to_note_relationship_sql)
|
||||||
.bind(&**parent_id)
|
.bind(&**parent_id)
|
||||||
.bind(&**note_id)
|
.bind(&**note_id)
|
||||||
.bind(&location)
|
.bind(&location)
|
||||||
.bind(&kind.to_string())
|
.bind(&kind.to_string())
|
||||||
.execute(executor)
|
.execute(executor)
|
||||||
.await?;
|
.await?;
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) async fn make_room_for_new_note<'a, E>(executor: E, parent_id: &ParentId, location: i64) -> SqlResult<()>
|
pub(crate) async fn make_room_for_new_note<'a, E>(
|
||||||
|
executor: E,
|
||||||
|
parent_id: &ParentId,
|
||||||
|
location: i64,
|
||||||
|
) -> SqlResult<()>
|
||||||
where
|
where
|
||||||
E: Executor<'a, Database = Sqlite>,
|
E: Executor<'a, Database = Sqlite>,
|
||||||
{
|
{
|
||||||
let make_room_for_new_note_sql = concat!(
|
let make_room_for_new_note_sql = concat!(
|
||||||
"UPDATE note_relationships ",
|
"UPDATE note_relationships ",
|
||||||
"SET location = location + 1 ",
|
"SET location = location + 1 ",
|
||||||
"WHERE location >= ? and parent_id = ?;"
|
"WHERE location >= ? and parent_id = ?;"
|
||||||
);
|
);
|
||||||
|
|
||||||
let _ = sqlx::query(make_room_for_new_note_sql)
|
let _ = sqlx::query(make_room_for_new_note_sql)
|
||||||
.bind(&location)
|
.bind(&location)
|
||||||
.bind(&**parent_id)
|
.bind(&**parent_id)
|
||||||
.execute(executor)
|
.execute(executor)
|
||||||
.await?;
|
.await?;
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) async fn assert_max_child_location_for_note<'a, E>(executor: E, note_id: &ParentId) -> SqlResult<i64>
|
pub(crate) async fn assert_max_child_location_for_note<'a, E>(
|
||||||
|
executor: E,
|
||||||
|
note_id: &ParentId,
|
||||||
|
) -> SqlResult<i64>
|
||||||
where
|
where
|
||||||
E: Executor<'a, Database = Sqlite>,
|
E: Executor<'a, Database = Sqlite>,
|
||||||
{
|
{
|
||||||
let assert_max_child_location_for_note_sql =
|
let assert_max_child_location_for_note_sql =
|
||||||
"SELECT MAX(location) AS count FROM note_relationships WHERE parent_id = ?;";
|
"SELECT MAX(location) AS count FROM note_relationships WHERE parent_id = ?;";
|
||||||
|
|
||||||
let count: RowCount = sqlx::query_as(assert_max_child_location_for_note_sql)
|
let count: RowCount = sqlx::query_as(assert_max_child_location_for_note_sql)
|
||||||
.bind(&**note_id)
|
.bind(&**note_id)
|
||||||
.fetch_one(executor)
|
.fetch_one(executor)
|
||||||
.await?;
|
.await?;
|
||||||
|
|
||||||
Ok(count.count)
|
Ok(count.count)
|
||||||
}
|
}
|
||||||
|
|
||||||
// _ _ _ _ _ __ _ ___ _ _ _ _ _
|
// _ _ _ _ _ __ _ ___ _ _ _ _ _
|
||||||
|
@ -326,49 +343,57 @@ where
|
||||||
// |_|
|
// |_|
|
||||||
|
|
||||||
pub(crate) async fn insert_bulk_note_to_kasten_relationships<'a, E>(
|
pub(crate) async fn insert_bulk_note_to_kasten_relationships<'a, E>(
|
||||||
executor: E,
|
executor: E,
|
||||||
note_id: &NoteId,
|
note_id: &NoteId,
|
||||||
references: &[NoteId],
|
references: &[NoteId],
|
||||||
) -> SqlResult<()>
|
) -> SqlResult<()>
|
||||||
where
|
where
|
||||||
E: Executor<'a, Database = Sqlite>,
|
E: Executor<'a, Database = Sqlite>,
|
||||||
{
|
{
|
||||||
if references.is_empty() {
|
if references.is_empty() {
|
||||||
return Ok(());
|
return Ok(());
|
||||||
}
|
}
|
||||||
|
|
||||||
let insert_pattern = format!("(?, ?, '{}')", KastenRelationshipKind::Kasten.to_string());
|
let insert_pattern = format!("(?, ?, '{}')", KastenRelationshipKind::Kasten.to_string());
|
||||||
let insert_note_page_references_sql = "INSERT INTO note_kasten_relationships (note_id, kasten_id, kind) VALUES "
|
let insert_note_page_references_sql =
|
||||||
.to_string()
|
"INSERT INTO note_kasten_relationships (note_id, kasten_id, kind) VALUES ".to_string()
|
||||||
+ &[insert_pattern.as_str()].repeat(references.len()).join(", ")
|
+ &[insert_pattern.as_str()]
|
||||||
+ &";".to_string();
|
.repeat(references.len())
|
||||||
|
.join(", ") + &";".to_string();
|
||||||
|
|
||||||
let mut request = sqlx::query(&insert_note_page_references_sql);
|
let mut request = sqlx::query(&insert_note_page_references_sql);
|
||||||
for reference in references {
|
for reference in references {
|
||||||
request = request.bind(&**note_id).bind(&**reference);
|
request = request.bind(&**note_id).bind(&**reference);
|
||||||
}
|
}
|
||||||
|
|
||||||
request.execute(executor).await.map(|_| ())
|
request.execute(executor).await.map(|_| ())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) async fn delete_bulk_note_to_kasten_relationships<'a, E>(executor: E, note_id: &NoteId) -> SqlResult<()>
|
pub(crate) async fn delete_bulk_note_to_kasten_relationships<'a, E>(
|
||||||
|
executor: E,
|
||||||
|
note_id: &NoteId,
|
||||||
|
) -> SqlResult<()>
|
||||||
where
|
where
|
||||||
E: Executor<'a, Database = Sqlite>,
|
E: Executor<'a, Database = Sqlite>,
|
||||||
{
|
{
|
||||||
let delete_note_to_kasten_relationship_sql = "DELETE FROM note_kasten_relationships WHERE and note_id = ?;";
|
let delete_note_to_kasten_relationship_sql =
|
||||||
let _ = sqlx::query(delete_note_to_kasten_relationship_sql)
|
"DELETE FROM note_kasten_relationships WHERE and note_id = ?;";
|
||||||
.bind(&**note_id)
|
let _ = sqlx::query(delete_note_to_kasten_relationship_sql)
|
||||||
.execute(executor)
|
.bind(&**note_id)
|
||||||
.await?;
|
.execute(executor)
|
||||||
Ok(())
|
.await?;
|
||||||
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
// Given the references supplied, and the references found in the datastore,
|
// Given the references supplied, and the references found in the datastore,
|
||||||
// return a list of the references not found in the datastore.
|
// return a list of the references not found in the datastore.
|
||||||
pub(crate) fn diff_references(references: &[String], found_references: &[PageTitle]) -> Vec<String> {
|
pub(crate) fn diff_references(
|
||||||
let all: HashSet<String> = references.iter().cloned().collect();
|
references: &[String],
|
||||||
let found: HashSet<String> = found_references.iter().map(|r| r.content.clone()).collect();
|
found_references: &[PageTitle],
|
||||||
all.difference(&found).cloned().collect()
|
) -> Vec<String> {
|
||||||
|
let all: HashSet<String> = references.iter().cloned().collect();
|
||||||
|
let found: HashSet<String> = found_references.iter().map(|r| r.content.clone()).collect();
|
||||||
|
all.difference(&found).cloned().collect()
|
||||||
}
|
}
|
||||||
|
|
||||||
// ___ _ _ _ _ __ _ ___ _ _ _ _ _
|
// ___ _ _ _ _ __ _ ___ _ _ _ _ _
|
||||||
|
@ -381,31 +406,32 @@ pub(crate) fn diff_references(references: &[String], found_references: &[PageTit
|
||||||
// list of titles. Used by insert_note and update_note_content to
|
// list of titles. Used by insert_note and update_note_content to
|
||||||
// find the ids of all the references in a given document.
|
// 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_kasten_from_list_of_references<'a, E>(
|
||||||
executor: E,
|
executor: E,
|
||||||
references: &[String],
|
references: &[String],
|
||||||
) -> SqlResult<Vec<PageTitle>>
|
) -> SqlResult<Vec<PageTitle>>
|
||||||
where
|
where
|
||||||
E: Executor<'a, Database = Sqlite>,
|
E: Executor<'a, Database = Sqlite>,
|
||||||
{
|
{
|
||||||
if references.is_empty() {
|
if references.is_empty() {
|
||||||
return Ok(vec![]);
|
return Ok(vec![]);
|
||||||
}
|
}
|
||||||
|
|
||||||
lazy_static! {
|
lazy_static! {
|
||||||
static ref SELECT_ALL_REFERENCES_FOR_SQL_BASE: String = format!(
|
static ref SELECT_ALL_REFERENCES_FOR_SQL_BASE: String = format!(
|
||||||
"SELECT id, content FROM notes WHERE kind = '{}' AND content IN (",
|
"SELECT id, content FROM notes WHERE kind = '{}' AND content IN (",
|
||||||
NoteKind::Kasten.to_string()
|
NoteKind::Kasten.to_string()
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
let find_all_references_for_sql =
|
let find_all_references_for_sql = SELECT_ALL_REFERENCES_FOR_SQL_BASE.to_string()
|
||||||
SELECT_ALL_REFERENCES_FOR_SQL_BASE.to_string() + &["?"].repeat(references.len()).join(",") + &");".to_string();
|
+ &["?"].repeat(references.len()).join(",")
|
||||||
|
+ &");".to_string();
|
||||||
|
|
||||||
let mut request = sqlx::query_as(&find_all_references_for_sql);
|
let mut request = sqlx::query_as(&find_all_references_for_sql);
|
||||||
for id in references.iter() {
|
for id in references.iter() {
|
||||||
request = request.bind(id);
|
request = request.bind(id);
|
||||||
}
|
}
|
||||||
request.fetch_all(executor).await
|
request.fetch_all(executor).await
|
||||||
}
|
}
|
||||||
|
|
||||||
// ___ _ _
|
// ___ _ _
|
||||||
|
@ -415,112 +441,125 @@ where
|
||||||
//
|
//
|
||||||
|
|
||||||
pub(crate) async fn delete_note_to_note_relationship<'a, E>(
|
pub(crate) async fn delete_note_to_note_relationship<'a, E>(
|
||||||
executor: E,
|
executor: E,
|
||||||
parent_id: &ParentId,
|
parent_id: &ParentId,
|
||||||
note_id: &NoteId,
|
note_id: &NoteId,
|
||||||
) -> SqlResult<()>
|
) -> SqlResult<()>
|
||||||
where
|
where
|
||||||
E: Executor<'a, Database = Sqlite>,
|
E: Executor<'a, Database = Sqlite>,
|
||||||
{
|
{
|
||||||
let delete_note_to_note_relationship_sql = concat!(
|
let delete_note_to_note_relationship_sql = concat!(
|
||||||
"DELETE FROM note_relationships ",
|
"DELETE FROM note_relationships ",
|
||||||
"WHERE parent_id = ? and note_id = ? "
|
"WHERE parent_id = ? and note_id = ? "
|
||||||
);
|
);
|
||||||
|
|
||||||
let count = sqlx::query(delete_note_to_note_relationship_sql)
|
let count = sqlx::query(delete_note_to_note_relationship_sql)
|
||||||
.bind(&**parent_id)
|
.bind(&**parent_id)
|
||||||
.bind(&**note_id)
|
.bind(&**note_id)
|
||||||
.execute(executor)
|
.execute(executor)
|
||||||
.await?
|
.await?
|
||||||
.rows_affected();
|
.rows_affected();
|
||||||
|
|
||||||
match count {
|
match count {
|
||||||
1 => Ok(()),
|
1 => Ok(()),
|
||||||
_ => Err(sqlx::Error::RowNotFound),
|
_ => Err(sqlx::Error::RowNotFound),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) async fn delete_note_to_kasten_relationships<'a, E>(executor: E, note_id: &NoteId) -> SqlResult<()>
|
pub(crate) async fn delete_note_to_kasten_relationships<'a, E>(
|
||||||
|
executor: E,
|
||||||
|
note_id: &NoteId,
|
||||||
|
) -> SqlResult<()>
|
||||||
where
|
where
|
||||||
E: Executor<'a, Database = Sqlite>,
|
E: Executor<'a, Database = Sqlite>,
|
||||||
{
|
{
|
||||||
lazy_static! {
|
lazy_static! {
|
||||||
static ref DELETE_NOTE_TO_KASTEN_RELATIONSHIPS_SQL: String = format!(
|
static ref DELETE_NOTE_TO_KASTEN_RELATIONSHIPS_SQL: String = format!(
|
||||||
"DELETE FROM note_relationships WHERE kind in ('{}', '{}') AND parent_id = ?;",
|
"DELETE FROM note_relationships WHERE kind in ('{}', '{}') AND parent_id = ?;",
|
||||||
KastenRelationshipKind::Kasten.to_string(),
|
KastenRelationshipKind::Kasten.to_string(),
|
||||||
KastenRelationshipKind::Unacked.to_string()
|
KastenRelationshipKind::Unacked.to_string()
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
let _ = sqlx::query(&DELETE_NOTE_TO_KASTEN_RELATIONSHIPS_SQL)
|
let _ = sqlx::query(&DELETE_NOTE_TO_KASTEN_RELATIONSHIPS_SQL)
|
||||||
.bind(&**note_id)
|
.bind(&**note_id)
|
||||||
.execute(executor)
|
.execute(executor)
|
||||||
.await?;
|
.await?;
|
||||||
Ok(())
|
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: &NoteId) -> SqlResult<()>
|
||||||
where
|
where
|
||||||
E: Executor<'a, Database = Sqlite>,
|
E: Executor<'a, Database = Sqlite>,
|
||||||
{
|
{
|
||||||
let delete_note_sql = "DELETE FROM notes WHERE note_id = ?";
|
let delete_note_sql = "DELETE FROM notes WHERE note_id = ?";
|
||||||
|
|
||||||
let count = sqlx::query(delete_note_sql)
|
let count = sqlx::query(delete_note_sql)
|
||||||
.bind(&**note_id)
|
.bind(&**note_id)
|
||||||
.execute(executor)
|
.execute(executor)
|
||||||
.await?
|
.await?
|
||||||
.rows_affected();
|
.rows_affected();
|
||||||
|
|
||||||
match count {
|
match count {
|
||||||
1 => Ok(()),
|
1 => Ok(()),
|
||||||
_ => Err(sqlx::Error::RowNotFound),
|
_ => Err(sqlx::Error::RowNotFound),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// After removing a note, recalculate the position of all notes under
|
// After removing a note, recalculate the position of all notes under
|
||||||
// the parent note, such that there order is now completely
|
// the parent note, such that there order is now completely
|
||||||
// sequential.
|
// sequential.
|
||||||
pub(crate) async fn close_hole_for_deleted_note<'a, E>(
|
pub(crate) async fn close_hole_for_deleted_note<'a, E>(
|
||||||
executor: E,
|
executor: E,
|
||||||
parent_id: &ParentId,
|
parent_id: &ParentId,
|
||||||
location: i64,
|
location: i64,
|
||||||
) -> SqlResult<()>
|
) -> SqlResult<()>
|
||||||
where
|
where
|
||||||
E: Executor<'a, Database = Sqlite>,
|
E: Executor<'a, Database = Sqlite>,
|
||||||
{
|
{
|
||||||
let close_hole_for_deleted_note_sql = concat!(
|
let close_hole_for_deleted_note_sql = concat!(
|
||||||
"UPDATE note_relationships ",
|
"UPDATE note_relationships ",
|
||||||
"SET location = location - 1 ",
|
"SET location = location - 1 ",
|
||||||
"WHERE location > ? and parent_id = ?;"
|
"WHERE location > ? and parent_id = ?;"
|
||||||
);
|
);
|
||||||
|
|
||||||
let _ = sqlx::query(close_hole_for_deleted_note_sql)
|
let _ = sqlx::query(close_hole_for_deleted_note_sql)
|
||||||
.bind(&location)
|
.bind(&location)
|
||||||
.bind(&**parent_id)
|
.bind(&**parent_id)
|
||||||
.execute(executor)
|
.execute(executor)
|
||||||
.await?;
|
.await?;
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
// __ __ _
|
// __ __ _
|
||||||
// | \/ (_)___ __
|
// | \/ (_)___ __
|
||||||
// | |\/| | (_-</ _|
|
// | |\/| | (_-</ _|
|
||||||
// |_| |_|_/__/\__|
|
// |_| |_|_/__/\__|
|
||||||
//
|
//
|
||||||
|
|
||||||
// The dreaded miscellaneous!
|
// The dreaded miscellaneous!
|
||||||
|
|
||||||
pub(crate) async fn count_existing_note_relationships<'a, E>(executor: E, note_id: &NoteId) -> SqlResult<i64>
|
pub(crate) async fn count_existing_note_relationships(
|
||||||
where
|
tx: &mut Transaction<'_, Sqlite>,
|
||||||
E: Executor<'a, Database = Sqlite>,
|
note_id: &NoteId,
|
||||||
{
|
) -> SqlResult<i64> {
|
||||||
let count_existing_note_relationships_sql =
|
let mut txi = tx.begin().await?;
|
||||||
"SELECT COUNT(*) as count FROM note_relationships WHERE note_id = ?;";
|
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)
|
||||||
|
.await?;
|
||||||
|
|
||||||
let count: RowCount = sqlx::query_as(&count_existing_note_relationships_sql)
|
let count: RowCount = {
|
||||||
.bind(&**note_id)
|
let count_existing_note_relationships_sql =
|
||||||
.fetch_one(executor)
|
"SELECT COUNT(*) as count FROM note_relationships WHERE note_id = ?;";
|
||||||
.await?;
|
|
||||||
|
|
||||||
Ok(count.count)
|
sqlx::query_as(&count_existing_note_relationships_sql)
|
||||||
|
.bind(&**note_id)
|
||||||
|
.fetch_one(&mut txi)
|
||||||
|
.await?
|
||||||
|
};
|
||||||
|
txi.commit().await?;
|
||||||
|
Ok(count.count)
|
||||||
}
|
}
|
||||||
|
|
|
@ -71,169 +71,198 @@ pub type NoteResult<T> = core::result::Result<T, NoteStoreError>;
|
||||||
// (box).
|
// (box).
|
||||||
|
|
||||||
impl NoteStore {
|
impl NoteStore {
|
||||||
/// Initializes a new instance of the note store. Note that the
|
/// Initializes a new instance of the note store. Note that the
|
||||||
/// note store holds an Arc internally; this code is (I think)
|
/// note store holds an Arc internally; this code is (I think)
|
||||||
/// safe to Send.
|
/// safe to Send.
|
||||||
pub async fn new(url: &str) -> NoteResult<Self> {
|
pub async fn new(url: &str) -> NoteResult<Self> {
|
||||||
let pool = SqlitePool::connect(url).await?;
|
let pool = SqlitePool::connect(url).await?;
|
||||||
Ok(NoteStore(Arc::new(pool)))
|
Ok(NoteStore(Arc::new(pool)))
|
||||||
}
|
}
|
||||||
/// Erase all the data in the database and restore it
|
/// Erase all the data in the database and restore it
|
||||||
/// to its original empty form. Do not use unless you
|
/// to its original empty form. Do not use unless you
|
||||||
/// really, really want that to happen.
|
/// really, really want that to happen.
|
||||||
pub async fn reset_database(&self) -> NoteResult<()> {
|
pub async fn reset_database(&self) -> NoteResult<()> {
|
||||||
reset_database(&*self.0).await.map_err(NoteStoreError::DBError)
|
reset_database(&*self.0)
|
||||||
}
|
.await
|
||||||
|
.map_err(NoteStoreError::DBError)
|
||||||
|
}
|
||||||
|
|
||||||
/// Fetch page by slug
|
/// Fetch page by slug
|
||||||
///
|
///
|
||||||
/// Supports the use case of the user navigating to a known place
|
/// Supports the use case of the user navigating to a known place
|
||||||
/// via a bookmark or other URL. Since the title isn't clear from
|
/// via a bookmark or other URL. Since the title isn't clear from
|
||||||
/// the slug, the slug is insufficient to generate a new page, so
|
/// 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
|
/// this use case says that in the event of a failure to find the
|
||||||
/// requested page, return a basic NotFound.
|
/// requested page, return a basic NotFound.
|
||||||
pub async fn get_kasten_by_slug(&self, slug: &str) -> NoteResult<(Vec<Note>, Vec<Note>)> {
|
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?;
|
let kasten = select_kasten_by_slug(&*self.0, &NoteId(slug.to_string())).await?;
|
||||||
if kasten.is_empty() {
|
if kasten.is_empty() {
|
||||||
return Err(NoteStoreError::NotFound)
|
return Err(NoteStoreError::NotFound);
|
||||||
}
|
}
|
||||||
|
|
||||||
let note_id = NoteId(kasten[0].id.clone());
|
let note_id = NoteId(kasten[0].id.clone());
|
||||||
Ok((kasten, select_backreferences_for_kasten(&*self.0, ¬e_id).await?))
|
Ok((
|
||||||
}
|
kasten,
|
||||||
|
select_backreferences_for_kasten(&*self.0, ¬e_id).await?,
|
||||||
|
))
|
||||||
|
}
|
||||||
|
|
||||||
/// Fetch page by title
|
/// Fetch page by title
|
||||||
|
|
||||||
/// The most common use case: the user is navigating by requesting
|
/// The most common use case: the user is navigating by requesting
|
||||||
/// a page. The page either exists or it doesn't. If it
|
/// 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,
|
/// 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
|
/// we also know no backreferences to it exist, so in that case you
|
||||||
/// get back two empty vecs.
|
/// get back two empty vecs.
|
||||||
pub async fn get_kasten_by_title(&self, title: &str) -> NoteResult<(Vec<Note>, Vec<Note>)> {
|
pub async fn get_kasten_by_title(&self, title: &str) -> NoteResult<(Vec<Note>, Vec<Note>)> {
|
||||||
if title.len() == 0 {
|
if title.len() == 0 {
|
||||||
return Err(NoteStoreError::NotFound);
|
return Err(NoteStoreError::NotFound);
|
||||||
}
|
}
|
||||||
|
|
||||||
let kasten = select_kasten_by_title(&*self.0, title).await?;
|
let kasten = select_kasten_by_title(&*self.0, title).await?;
|
||||||
if kasten.len() > 0 {
|
if kasten.len() > 0 {
|
||||||
let note_id = NoteId(kasten[0].id.clone());
|
let note_id = NoteId(kasten[0].id.clone());
|
||||||
return Ok((kasten, select_backreferences_for_kasten(&*self.0, ¬e_id).await?));
|
return Ok((
|
||||||
}
|
kasten,
|
||||||
|
select_backreferences_for_kasten(&*self.0, ¬e_id).await?,
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
||||||
// Sanity check!
|
// Sanity check!
|
||||||
let references = build_references(&title);
|
let references = build_references(&title);
|
||||||
if references.len() > 0 {
|
if references.len() > 0 {
|
||||||
return Err(NoteStoreError::InvalidNoteStructure(
|
return Err(NoteStoreError::InvalidNoteStructure(
|
||||||
"Titles may not contain nested references.".to_string(),
|
"Titles may not contain nested references.".to_string(),
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut tx = self.0.begin().await?;
|
let mut tx = self.0.begin().await?;
|
||||||
let slug = generate_slug(&mut tx, title).await?;
|
let slug = generate_slug(&mut tx, title).await?;
|
||||||
let zettlekasten = create_zettlekasten(&title, &slug);
|
let zettlekasten = create_zettlekasten(&title, &slug);
|
||||||
let _ = insert_note(&mut tx, &zettlekasten).await?;
|
let _ = insert_note(&mut tx, &zettlekasten).await?;
|
||||||
tx.commit().await?;
|
tx.commit().await?;
|
||||||
|
|
||||||
Ok((vec![Note::from(zettlekasten)], vec![]))
|
Ok((vec![Note::from(zettlekasten)], vec![]))
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn add_note(&self, note: &NewNote, parent_id: &str, location: Option<i64>) -> NoteResult<String> {
|
pub async fn add_note(
|
||||||
let new_id = self.insert_note(
|
&self,
|
||||||
note,
|
note: &NewNote,
|
||||||
&ParentId(parent_id.to_string()),
|
parent_id: &str,
|
||||||
location,
|
location: Option<i64>,
|
||||||
RelationshipKind::Direct).await?;
|
) -> NoteResult<String> {
|
||||||
|
let new_id = self
|
||||||
|
.insert_note(
|
||||||
|
note,
|
||||||
|
&ParentId(parent_id.to_string()),
|
||||||
|
location,
|
||||||
|
RelationshipKind::Direct,
|
||||||
|
)
|
||||||
|
.await?;
|
||||||
Ok(new_id)
|
Ok(new_id)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Move a note from one location to another.
|
/// Move a note from one location to another.
|
||||||
pub async fn move_note(
|
pub async fn move_note(
|
||||||
&self,
|
&self,
|
||||||
note_id: &str,
|
note_id: &str,
|
||||||
old_parent_id: &str,
|
old_parent_id: &str,
|
||||||
new_parent_id: &str,
|
new_parent_id: &str,
|
||||||
new_location: i64,
|
new_location: i64,
|
||||||
) -> NoteResult<()> {
|
) -> NoteResult<()> {
|
||||||
let mut tx = self.0.begin().await?;
|
let mut tx = self.0.begin().await?;
|
||||||
|
|
||||||
let old_parent_id = ParentId(old_parent_id.to_string());
|
let old_parent_id = ParentId(old_parent_id.to_string());
|
||||||
let new_parent_id = ParentId(new_parent_id.to_string());
|
let new_parent_id = ParentId(new_parent_id.to_string());
|
||||||
let note_id = NoteId(note_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 = select_note_to_note_relationship(&mut tx, &old_parent_id, ¬e_id).await?;
|
||||||
let old_note_location = old_note.location;
|
let old_note_location = old_note.location;
|
||||||
let old_note_kind = old_note.kind;
|
let old_note_kind = old_note.kind;
|
||||||
|
|
||||||
let _ = delete_note_to_note_relationship(&mut tx, &old_parent_id, ¬e_id).await?;
|
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 _ = 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 parent_max_location =
|
||||||
let new_location = cmp::min(parent_max_location + 1, new_location);
|
assert_max_child_location_for_note(&mut tx, &new_parent_id).await?;
|
||||||
let _ = make_room_for_new_note(&mut tx, &new_parent_id, new_location).await?;
|
let new_location = cmp::min(parent_max_location + 1, new_location);
|
||||||
let _ =
|
let _ = make_room_for_new_note(&mut tx, &new_parent_id, new_location).await?;
|
||||||
insert_note_to_note_relationship(&mut tx, &new_parent_id, ¬e_id, new_location, &old_note_kind).await?;
|
let _ = insert_note_to_note_relationship(
|
||||||
tx.commit().await?;
|
&mut tx,
|
||||||
Ok(())
|
&new_parent_id,
|
||||||
}
|
¬e_id,
|
||||||
|
new_location,
|
||||||
|
&old_note_kind,
|
||||||
|
)
|
||||||
|
.await?;
|
||||||
|
tx.commit().await?;
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
/// Updates a note's content. Completely rebuilds the note's
|
/// Updates a note's content. Completely rebuilds the note's
|
||||||
/// outgoing edge reference list every time.
|
/// outgoing edge reference list every time.
|
||||||
pub async fn update_note_content(&self, note_id: &str, content: &str) -> NoteResult<()> {
|
pub async fn update_note_content(&self, note_id: &str, content: &str) -> NoteResult<()> {
|
||||||
let references = build_references(&content);
|
let references = build_references(&content);
|
||||||
let note_id = NoteId(note_id.to_string());
|
let note_id = NoteId(note_id.to_string());
|
||||||
|
|
||||||
let mut tx = self.0.begin().await?;
|
let mut tx = self.0.begin().await?;
|
||||||
let _ = update_note_content(&mut tx, ¬e_id, &content).await?;
|
let _ = update_note_content(&mut tx, ¬e_id, &content).await?;
|
||||||
let _ = delete_bulk_note_to_kasten_relationships(&mut tx, ¬e_id).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 found_references =
|
||||||
let new_references = diff_references(&references, &found_references);
|
find_all_kasten_from_list_of_references(&mut tx, &references).await?;
|
||||||
let mut known_reference_ids: Vec<NoteId> = Vec::new();
|
let new_references = diff_references(&references, &found_references);
|
||||||
for one_reference in new_references.iter() {
|
let mut known_reference_ids: Vec<NoteId> = Vec::new();
|
||||||
let slug = generate_slug(&mut tx, one_reference).await?;
|
for one_reference in new_references.iter() {
|
||||||
let zettlekasten = create_zettlekasten(&one_reference, &slug);
|
let slug = generate_slug(&mut tx, one_reference).await?;
|
||||||
let _ = insert_note(&mut tx, &zettlekasten).await?;
|
let zettlekasten = create_zettlekasten(&one_reference, &slug);
|
||||||
known_reference_ids.push(NoteId(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());
|
known_reference_ids.append(
|
||||||
let _ = insert_bulk_note_to_kasten_relationships(&mut tx, ¬e_id, &known_reference_ids).await?;
|
&mut found_references
|
||||||
tx.commit().await?;
|
.iter()
|
||||||
Ok(())
|
.map(|r| NoteId(r.id.clone()))
|
||||||
}
|
.collect(),
|
||||||
|
);
|
||||||
|
let _ = insert_bulk_note_to_kasten_relationships(&mut tx, ¬e_id, &known_reference_ids)
|
||||||
|
.await?;
|
||||||
|
tx.commit().await?;
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
/// Deletes a note. If the note's relationship drops to zero, all
|
/// Deletes a note. If the note's relationship drops to zero, all
|
||||||
/// references from that note to pages are also deleted.
|
/// references from that note to pages are also deleted.
|
||||||
pub async fn delete_note(&self, note_id: &str, note_parent_id: &str) -> NoteResult<()> {
|
pub async fn delete_note(&self, note_id: &str, note_parent_id: &str) -> NoteResult<()> {
|
||||||
let mut tx = self.0.begin().await?;
|
let mut tx = self.0.begin().await?;
|
||||||
let note_id = NoteId(note_id.to_string());
|
let note_id = NoteId(note_id.to_string());
|
||||||
let parent_id = ParentId(note_parent_id.to_string());
|
let parent_id = ParentId(note_parent_id.to_string());
|
||||||
|
|
||||||
if *parent_id != *note_id {
|
if *parent_id != *note_id {
|
||||||
let _ = delete_note_to_note_relationship(&mut tx, &parent_id, ¬e_id);
|
let _ = delete_note_to_note_relationship(&mut tx, &parent_id, ¬e_id);
|
||||||
}
|
}
|
||||||
// The big one: if zero parents report having an interest in this note, then it,
|
// The big one: if zero parents report having an interest in this note, then it,
|
||||||
// *and any sub-relationships*, go away.
|
// *and any sub-relationships*, go away.
|
||||||
if count_existing_note_relationships(&mut tx, ¬e_id).await? == 0 {
|
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_kasten_relationships(&mut tx, ¬e_id).await?;
|
||||||
let _ = delete_note(&mut tx, ¬e_id).await?;
|
let _ = delete_note(&mut tx, ¬e_id).await?;
|
||||||
}
|
}
|
||||||
tx.commit().await?;
|
tx.commit().await?;
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// The Private stuff
|
// The Private stuff
|
||||||
|
|
||||||
impl NoteStore {
|
impl NoteStore {
|
||||||
// Pretty much the most dangerous function in our system. Has to
|
// Pretty much the most dangerous function in our system. Has to
|
||||||
// have ALL the error checking.
|
// have ALL the error checking.
|
||||||
async fn insert_note(
|
async fn insert_note(
|
||||||
&self,
|
&self,
|
||||||
note: &NewNote,
|
note: &NewNote,
|
||||||
parent_id: &ParentId,
|
parent_id: &ParentId,
|
||||||
location: Option<i64>,
|
location: Option<i64>,
|
||||||
kind: RelationshipKind,
|
kind: RelationshipKind,
|
||||||
) -> NoteResult<String> {
|
) -> NoteResult<String> {
|
||||||
if let Some(location) = location {
|
if let Some(location) = location {
|
||||||
if location < 0 {
|
if location < 0 {
|
||||||
return Err(NoteStoreError::InvalidNoteStructure(
|
return Err(NoteStoreError::InvalidNoteStructure(
|
||||||
|
@ -243,53 +272,60 @@ impl NoteStore {
|
||||||
}
|
}
|
||||||
|
|
||||||
if parent_id.is_empty() {
|
if parent_id.is_empty() {
|
||||||
return Err(NoteStoreError::InvalidNoteStructure(
|
return Err(NoteStoreError::InvalidNoteStructure(
|
||||||
"Add note: A parent note ID is required.".to_string(),
|
"Add note: A parent note ID is required.".to_string(),
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
|
|
||||||
if note.id.is_empty() {
|
if note.id.is_empty() {
|
||||||
return Err(NoteStoreError::InvalidNoteStructure(
|
return Err(NoteStoreError::InvalidNoteStructure(
|
||||||
"Add note: Your note should have an id already".to_string(),
|
"Add note: Your note should have an id already".to_string(),
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
|
|
||||||
if note.content.is_empty() {
|
if note.content.is_empty() {
|
||||||
return Err(NoteStoreError::InvalidNoteStructure(
|
return Err(NoteStoreError::InvalidNoteStructure(
|
||||||
"Add note: Empty notes are not supported.".to_string(),
|
"Add note: Empty notes are not supported.".to_string(),
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
|
|
||||||
let references = build_references(¬e.content);
|
let references = build_references(¬e.content);
|
||||||
|
|
||||||
let mut tx = self.0.begin().await?;
|
let mut tx = self.0.begin().await?;
|
||||||
let location = {
|
let location = {
|
||||||
let max_child = assert_max_child_location_for_note(&mut tx, parent_id).await? + 1;
|
let max_child = assert_max_child_location_for_note(&mut tx, parent_id).await? + 1;
|
||||||
if let Some(location) = location {
|
if let Some(location) = location {
|
||||||
cmp::min(max_child, location)
|
cmp::min(max_child, location)
|
||||||
} else {
|
} else {
|
||||||
max_child
|
max_child
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
let note_id = NoteId(note.id.clone());
|
let note_id = NoteId(note.id.clone());
|
||||||
insert_note(&mut tx, ¬e).await?;
|
insert_note(&mut tx, ¬e).await?;
|
||||||
make_room_for_new_note(&mut tx, &parent_id, location).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?;
|
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 found_references =
|
||||||
let new_references = diff_references(&references, &found_references);
|
find_all_kasten_from_list_of_references(&mut tx, &references).await?;
|
||||||
let mut known_reference_ids: Vec<NoteId> = Vec::new();
|
let new_references = diff_references(&references, &found_references);
|
||||||
for one_reference in new_references.iter() {
|
let mut known_reference_ids: Vec<NoteId> = Vec::new();
|
||||||
let slug = generate_slug(&mut tx, one_reference).await?;
|
for one_reference in new_references.iter() {
|
||||||
let zettlekasten = create_zettlekasten(&one_reference, &slug);
|
let slug = generate_slug(&mut tx, one_reference).await?;
|
||||||
let _ = insert_note(&mut tx, &zettlekasten).await?;
|
let zettlekasten = create_zettlekasten(&one_reference, &slug);
|
||||||
known_reference_ids.push(NoteId(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());
|
known_reference_ids.append(
|
||||||
let _ = insert_bulk_note_to_kasten_relationships(&mut tx, ¬e_id, &known_reference_ids).await?;
|
&mut found_references
|
||||||
tx.commit().await?;
|
.iter()
|
||||||
Ok(note_id.to_string())
|
.map(|r| NoteId(r.id.clone()))
|
||||||
}
|
.collect(),
|
||||||
|
);
|
||||||
|
let _ = insert_bulk_note_to_kasten_relationships(&mut tx, ¬e_id, &known_reference_ids)
|
||||||
|
.await?;
|
||||||
|
tx.commit().await?;
|
||||||
|
Ok(note_id.to_string())
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue