derive_builder pattern is applied successfully.

This is mostly an exercise to understand the derive_builder pattern.
It required a few tips to get it working, but in the end, it's
actually what I want.

I also learned a lot about how the Executor pattern, the Results<> object,
error mapping, and futures interact in this code.  This is going to be
incredibly useful long-term, as long as I still keep this project "live"
in my head.
This commit is contained in:
Elf M. Sternberg 2020-10-08 12:18:08 -07:00
parent e0c463f9fc
commit 1b8e1c067d
6 changed files with 226 additions and 145 deletions

View File

@ -14,6 +14,9 @@ readme = "./README.org"
friendly_id = "0.3.0" friendly_id = "0.3.0"
thiserror = "1.0.20" thiserror = "1.0.20"
derive_builder = "0.9.0" derive_builder = "0.9.0"
lazy_static = "1.4.0"
regex = "1.3.9"
slug = "0.1.4"
tokio = { version = "0.2.22", features = ["rt-threaded", "blocking"] } tokio = { version = "0.2.22", features = ["rt-threaded", "blocking"] }
serde = { version = "1.0.116", features = ["derive"] } serde = { version = "1.0.116", features = ["derive"] }
serde_json = "1.0.56" serde_json = "1.0.56"

View File

@ -13,6 +13,6 @@ pub enum NoteStoreError {
NotFound, NotFound,
/// All other errors from the database. /// All other errors from the database.
#[error(transparent)] #[error("Sqlx")]
DBError(#[from] sqlx::Error), DBError(#[from] sqlx::Error),
} }

View File

@ -1,4 +1,3 @@
use chrono::{DateTime, Utc};
mod errors; mod errors;
mod row_structs; mod row_structs;
mod store; mod store;
@ -9,6 +8,7 @@ pub use crate::store::NoteStore;
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use chrono;
use super::*; use super::*;
use tokio; use tokio;
@ -38,12 +38,12 @@ mod tests {
#[tokio::test(threaded_scheduler)] #[tokio::test(threaded_scheduler)]
async fn fetching_unfound_page_by_title_works() { async fn fetching_unfound_page_by_title_works() {
let title = "Nonexistent Page"; let title = "Nonexistent Page";
let now = chrono::Utc::now(); let _now = chrono::Utc::now();
let storagepool = fresh_inmemory_database().await; let storagepool = fresh_inmemory_database().await;
let newpageresult = storagepool.get_page_by_title(&title).await; let newpageresult = storagepool.get_page_by_title(&title).await;
assert!(newpageresult.is_ok(), "{:?}", newpage); assert!(newpageresult.is_ok(), "{:?}", newpageresult);
let (newpage, newnotes) = newpageresult.unwrap(); let (newpage, _newnotes) = newpageresult.unwrap();
assert_eq!(newpage.title, title, "{:?}", newpage.title); assert_eq!(newpage.title, title, "{:?}", newpage.title);
assert_eq!(newpage.slug, "nonexistent-page"); assert_eq!(newpage.slug, "nonexistent-page");

View File

@ -1,5 +1,5 @@
use chrono::{DateTime, Utc}; use chrono::{DateTime, Utc};
use derive_builder; use derive_builder::Builder;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use sqlx::{self, FromRow}; use sqlx::{self, FromRow};
@ -32,12 +32,13 @@ pub struct NewPage {
pub slug: String, pub slug: String,
pub title: String, pub title: String,
pub note_id: i64, pub note_id: i64,
#[builder(default = "chrono::Utc::now()")] #[builder(default = r#"chrono::Utc::now()"#)]
pub creation_date: DateTime<Utc>, pub creation_date: DateTime<Utc>,
#[builder(default = "chrono::Utc::now()")] #[builder(default = r#"chrono::Utc::now()"#)]
pub updated_date: DateTime<Utc>, pub updated_date: DateTime<Utc>,
#[builder(default = "chrono::Utc::now()")] #[builder(default = r#"chrono::Utc::now()"#)]
pub lastview_date: DateTime<Utc>, pub lastview_date: DateTime<Utc>,
#[builder(default = r#"None"#)]
pub deleted_date: Option<DateTime<Utc>>, pub deleted_date: Option<DateTime<Utc>>,
} }
@ -45,28 +46,43 @@ pub struct NewPage {
pub struct NewNote { pub struct NewNote {
pub uuid: String, pub uuid: String,
pub content: String, pub content: String,
#[builder(default = "note")] #[builder(default = r#""note".to_string()"#)]
pub notetype: String, pub notetype: String,
#[builder(default = "chrono::Utc::now()")] #[builder(default = r#"chrono::Utc::now()"#)]
pub creation_date: DateTime<Utc>, pub creation_date: DateTime<Utc>,
#[builder(default = "chrono::Utc::now()")] #[builder(default = r#"chrono::Utc::now()"#)]
pub updated_date: DateTime<Utc>, pub updated_date: DateTime<Utc>,
#[builder(default = "chrono::Utc::now()")] #[builder(default = r#"chrono::Utc::now()"#)]
pub lastview_date: DateTime<Utc>, pub lastview_date: DateTime<Utc>,
#[builder(default = r#"None"#)]
pub deleted_date: Option<DateTime<Utc>>, pub deleted_date: Option<DateTime<Utc>>,
} }
#[derive(Clone, Serialize, Deserialize, Debug, FromRow)]
pub(crate) struct JustSlugs {
pub slug: String,
}
#[derive(Clone, Serialize, Deserialize, Debug, FromRow)]
pub struct JustTitles {
title: String,
}
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::*; use super::*;
#[test] #[test]
fn can_build_new_page() { fn can_build_new_note() {
let now = chrono::Utc::now(); let now = chrono::Utc::now();
let newnote = NewNoteBuilder::default(); let newnote = NewNoteBuilder::default()
assert!((newnote.creation_date - now).num_minutes() < 1.0); .uuid("foo".to_string())
assert!((newnote.updated_date - now).num_minutes() < 1.0); .content("bar".to_string())
assert!((newnote.lastview_date - now).num_minutes() < 1.0); .build()
.unwrap();
assert!((newnote.creation_date - now).num_minutes() < 1);
assert!((newnote.updated_date - now).num_minutes() < 1);
assert!((newnote.lastview_date - now).num_minutes() < 1);
assert!(newnote.deleted_date.is_none()); assert!(newnote.deleted_date.is_none());
} }
} }

View File

@ -1,38 +1,87 @@
SELECT parent_uuid, uuid, content, notetype, nature, position FROM ( -- This is undoubtedly one of the more complex bits of code I've
-- written recently, and I do wish there had been macros because
-- there's a lot of hand-written, copy-pasted code here around the
-- basic content of a note; it would have been nice to be able to DRY
-- that out.
WITH RECURSIVE children( -- This expression creates a table, 'notetree', that contains all of
-- the notes nested under a page. Each entry in the table includes
-- the note's parent's internal and external ids so that applications
-- can build an actual tree out of a vec of these things.
-- TODO: Extensive testing to validate that the nodes are delivered
-- *in nesting order* to the client.
SELECT
parent_id, parent_id,
parent_uuid, id, parent_uuid,
note_id,
note_uuid,
content,
position,
notetype,
creation_date,
updated_date,
lastview_date,
deleted_date
FROM (
WITH RECURSIVE notetree(
parent_id,
parent_uuid,
id,
uuid, uuid,
content, content,
position,
notetype, notetype,
creation_date, creation_date,
updated_date, updated_date,
lastview_date, lastview_date,
deleted_date, deleted_date,
cycle cycle) AS
) AS (
-- ROOT expression
SELECT SELECT
notes.id, notes.id,
notes.uuid, notes.uuid,
notes.id, notes.id,
notes.uuid, notes.uuid,
notes.content, notes.content,
notes.notetype, 'page', 0, ','||notes.id||',' 0, -- Root notes are always in position 0
FROM notes INNER JOIN pages notes.notetype,
ON pages.note_id = notes.id notes.creation_date,
WHERE pages.id = ? notes.updated_date,
AND notes.notetype="page" notes.lastview_date,
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"
UNION -- RECURSIVE expression
SELECT note_relationships.parent_id, notes.id, UNION SELECT
notes.content, notes.notetype, note_relationships.nature, notetree.id,
notetree.uuid,
notes.id,
notes.uuid,
notes.content,
note_relationships.position, note_relationships.position,
children.cycle||notes.id||',' notes.notetype,
notes.creation_date,
notes.updated_date,
notes.lastview_date,
notes.deleted_date,
notetree.cycle||notes.id||','
FROM notes FROM notes
INNER JOIN note_relationships ON notes.id = note_relationships.note_id INNER JOIN note_relationships ON notes.id = note_relationships.note_id
INNER JOIN children ON note_relationships.parent_id = children.id -- For a given ID in the level of notetree in *this* recursion,
WHERE children.cycle NOT LIKE '%,'||notes.id||',%' -- we want each note's branches one level down.
ORDER BY note_relationships.position) INNER JOIN notetree ON note_relationships.parent_id = notetree.id
SELECT * from children);</code> -- And we want to make sure there are no cycles. There shouldn't
-- be; we're supposed to prevent those. But you never know.
WHERE
notetree.cycle NOT LIKE '%,'||notes.id||',%'
ORDER BY note_relationships.position);

View File

@ -1,11 +1,12 @@
use crate::errors::NoteStoreError; use crate::errors::NoteStoreError;
use crate::row_structs::{RawNote, RawPage}; use crate::row_structs::{JustSlugs, NewNote, NewPage, RawNote, RawPage};
use chrono; use lazy_static::lazy_static;
use friendly_id; use regex::Regex;
use slug::slugify;
use sqlx; use sqlx;
use sqlx::{ use sqlx::{
sqlite::{Sqlite, SqlitePool}, sqlite::{Sqlite, SqlitePool},
Done, Executor, Executor,
}; };
use std::sync::Arc; use std::sync::Arc;
@ -26,7 +27,7 @@ impl NoteStore {
// 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_databate(&*self.0).await reset_database(&*self.0).await.map_err(NoteStoreError::DBError)
} }
/// Fetch page by slug /// Fetch page by slug
@ -39,6 +40,7 @@ impl NoteStore {
pub async fn get_page_by_slug(&self, slug: &str) -> NoteResult<(RawPage, Vec<RawNote>)> { pub async fn get_page_by_slug(&self, slug: &str) -> NoteResult<(RawPage, Vec<RawNote>)> {
// let select_note_collection_for_root = include_str!("sql/select_note_collection_for_root.sql"); // let select_note_collection_for_root = include_str!("sql/select_note_collection_for_root.sql");
let mut tx = self.0.begin().await?; let mut tx = self.0.begin().await?;
let page = select_page_by_slug(&mut tx, slug).await?;
// let notes = sqlx::query_as(select_note_collection_for_root) // let notes = sqlx::query_as(select_note_collection_for_root)
// .bind(page.note_id) // .bind(page.note_id)
// .fetch(&tx) // .fetch(&tx)
@ -49,18 +51,16 @@ impl NoteStore {
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) { let page = match select_page_by_title(&mut tx, title).await {
Ok(page) => page, Ok(page) => page,
Err(sqlx::Error::NotFound) => { Err(sqlx::Error::RowNotFound) => match create_page_for_title(&mut tx, title).await {
match create_page_for_title(&mut tx, title) {
Ok(page) => page, Ok(page) => page,
Err(e) => return Err(e) Err(e) => return Err(NoteStoreError::DBError(e))
}
}, },
Err(e) => return Err(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
return Ok((page, vec![])) return Ok((page, vec![]));
} }
} }
@ -79,14 +79,14 @@ async fn select_page_by_slug<'e, E>(executor: E, slug: &str) -> SqlResult<RawPag
where where
E: 'e + Executor<'e, Database = Sqlite>, E: 'e + Executor<'e, Database = Sqlite>,
{ {
let select_one_page_by_title_sql = concat!( let select_one_page_by_slug_sql = concat!(
"SELECT id, title, slug, note_id, creation_date, updated_date, ", "SELECT id, title, slug, note_id, creation_date, updated_date, ",
"lastview_date, deleted_date FROM pages WHERE slug=?;" "lastview_date, deleted_date FROM pages WHERE slug=?;"
); );
sqlx::query_as(select_one_page_by_slug_sql) Ok(sqlx::query_as(&select_one_page_by_slug_sql)
.bind(&slug) .bind(&slug)
.fetch_one(&mut executor) .fetch_one(executor)
.await? .await?)
} }
async fn select_page_by_title<'e, E>(executor: E, title: &str) -> SqlResult<RawPage> async fn select_page_by_title<'e, E>(executor: E, title: &str) -> SqlResult<RawPage>
@ -97,10 +97,10 @@ where
"SELECT id, title, slug, note_id, creation_date, updated_date, ", "SELECT id, title, slug, note_id, creation_date, updated_date, ",
"lastview_date, deleted_date FROM pages WHERE title=?;" "lastview_date, deleted_date FROM pages WHERE title=?;"
); );
sqlx::query_as(select_one_page_by_title_sql) Ok(sqlx::query_as(&select_one_page_by_title_sql)
.bind(&title) .bind(&title)
.fetch_one(&mut executor) .fetch_one(executor)
.await? .await?)
} }
async fn reset_database<'e, E>(executor: E) -> SqlResult<()> async fn reset_database<'e, E>(executor: E) -> SqlResult<()>
@ -108,20 +108,22 @@ where
E: 'e + Executor<'e, Database = Sqlite>, E: 'e + Executor<'e, 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(&*self.0).await? sqlx::query(initialize_sql).execute(executor).await.map(|_| ())
} }
async fn get_note_collection_for_root<'e, E>(executor: E, root: i64) -> SqlResult<Vec<RawNotes>> async fn get_note_collection_for_root<'e, E>(executor: E, root: i64) -> SqlResult<Vec<RawNote>>
where where
E: 'e + Executor<'e, Database = Sqlite>, E: 'e + Executor<'e, Database = Sqlite>,
{ {
let select_note_collection_for_root = include_str!("sql/select_note_collection_for_root.sql"); let select_note_collection_for_root = include_str!("sql/select_note_collection_from_root.sql");
sqlx::query_as(select_note_collection_for_root) Ok(sqlx::query_as(&select_note_collection_for_root)
.fetch(&*self.0) .bind(&root)
.await? .fetch_all(executor)
.await?)
} }
async fn insert_one_new_note<'e, E>(executor: E, note: &NewNote) -> SqlResult<i64> where async fn insert_one_new_note<'e, E>(executor: E, note: &NewNote) -> SqlResult<i64>
where
E: 'e + Executor<'e, Database = Sqlite>, E: 'e + Executor<'e, Database = Sqlite>,
{ {
let insert_one_note_sql = concat!( let insert_one_note_sql = concat!(
@ -132,20 +134,39 @@ async fn insert_one_new_note<'e, E>(executor: E, note: &NewNote) -> SqlResult<i6
" creation_date, ", " creation_date, ",
" updated_date, ", " updated_date, ",
" lastview_date) ", " lastview_date) ",
"VALUES (?, ?, ?, ?, ?, ?);"); "VALUES (?, ?, ?, ?, ?, ?);"
);
Ok(sqlx::query(insert_one_note_sql) Ok(sqlx::query(insert_one_note_sql)
.bind(&note.uuid) .bind(&note.uuid)
.bind(&note.content) .bind(&note.content)
.bind(&note.note_type) .bind(&note.notetype)
.bind(&note.creation_date) .bind(&note.creation_date)
.bind(&note.updated_date) .bind(&note.updated_date)
.bind(&note.lastview_date) .bind(&note.lastview_date)
.execute(&mut tx) .execute(executor)
.await? .await?
.last_insert_rowid()) .last_insert_rowid())
} }
fn find_maximal_slug(slugs: &Vec<JustSlugs>) -> Option<u32> {
lazy_static! {
static ref RE_CAP_NUM: Regex = Regex::new(r"-(\d+)$").unwrap();
}
if slugs.len() == 0 {
return None;
}
let mut slug_counters: Vec<u32> = slugs
.iter()
.filter_map(|slug| RE_CAP_NUM.captures(&slug.slug))
.map(|cap| cap.get(1).unwrap().as_str().parse::<u32>().unwrap())
.collect();
slug_counters.sort_unstable();
slug_counters.pop()
}
// Given an initial string and an existing collection of slugs, // Given an initial string and an existing collection of slugs,
// generate a new slug that does not conflict with the current // generate a new slug that does not conflict with the current
// collection. // collection.
@ -156,27 +177,18 @@ where
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();
} }
lazy_static! {
static ref RE_CAP_NUM: Regex = Regex::new(r"-(\d+)$").unwrap();
}
let initial_slug = slugify::slugify(title); let initial_slug = slugify(title);
let sample_slug = RE_STRIP_NUM.replace_all(slug, ""); let sample_slug = RE_STRIP_NUM.replace_all(&initial_slug, "");
let similar_slugs: Vec<JustSlugs> = sqlx::query("SELECT slug FROM pages WHERE slug LIKE '?%';") let slug_finder_sql = "SELECT slug FROM pages WHERE slug LIKE '?%';";
.bind(&sample_slug) let similar_slugs: Vec<JustSlugs> = sqlx::query_as(&slug_finder_sql)
.execute(executor) .bind(&*sample_slug)
.fetch_all(executor)
.await?; .await?;
let slug_counters = similar_slugs let maximal_slug = find_maximal_slug(&similar_slugs);
.iter() match maximal_slug {
.map(|slug| RE_CAPNUM.captures(slug.slug)) None => Ok(initial_slug),
.filter_map(|cap| cap.get(1).unwrap().parse::<u32>().unwrap()) Some(max_slug) => Ok(format!("{}-{}", initial_slug, max_slug + 1)),
.collect();
match slug_counters.len() {
0 => Ok(initial_slug),
_ => {
slug_counters.sort_unstable();
return Ok(format!("{}-{}", initial_slug, slug_counters.pop() + 1))
}
} }
} }
@ -192,7 +204,8 @@ where
" creation_date, ", " creation_date, ",
" updated_date, ", " updated_date, ",
" lastview_date) ", " lastview_date) ",
"VALUES (?, ?, ?, ?, ?, ?);"); "VALUES (?, ?, ?, ?, ?, ?);"
);
Ok(sqlx::query(insert_one_page_sql) Ok(sqlx::query(insert_one_page_sql)
.bind(&page.slug) .bind(&page.slug)
@ -201,13 +214,13 @@ where
.bind(&page.creation_date) .bind(&page.creation_date)
.bind(&page.updated_date) .bind(&page.updated_date)
.bind(&page.lastview_date) .bind(&page.lastview_date)
.execute(&mut tx) .execute(executor)
.await? .await?
.last_insert_rowid()) .last_insert_rowid())
} }
async fn create_page_for_title<'e, E>(_executor: E, _title: &str) -> SqlResult<RawPage>
async fn create_page_for_title<'e, E>(executor: E, title: &str) -> SqlResult<RawPage> where where
E: 'e + Executor<'e, Database = Sqlite>, E: 'e + Executor<'e, Database = Sqlite>,
{ {
todo!() todo!()