use crate::errors::NoteStoreError; use crate::row_structs::{JustSlugs, NewNote, NewPage, RawNote, RawPage}; use lazy_static::lazy_static; use regex::Regex; use slug::slugify; use sqlx; use sqlx::{ sqlite::{Sqlite, SqlitePool}, Executor, }; use std::sync::Arc; /// A handle to our Sqlite database. #[derive(Clone, Debug)] pub struct NoteStore(Arc); type NoteResult = core::result::Result; type SqlResult = sqlx::Result; impl NoteStore { pub async fn new(url: &str) -> NoteResult { let pool = SqlitePool::connect(url).await?; Ok(NoteStore(Arc::new(pool))) } // Erase all the data in the database and restore it // to its original empty form. Do not use unless you // really, really want that to happen. pub async fn reset_database(&self) -> NoteResult<()> { reset_database(&*self.0).await.map_err(NoteStoreError::DBError) } /// Fetch page by slug /// /// 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 /// 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_page_by_slug(&self, slug: &str) -> NoteResult<(RawPage, Vec)> { // let select_note_collection_for_root = include_str!("sql/select_note_collection_for_root.sql"); 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) // .bind(page.note_id) // .fetch(&tx) // .await?; tx.commit().await?; Ok((page, vec![])) } pub async fn get_page_by_title(&self, title: &str) -> NoteResult<(RawPage, Vec)> { let mut tx = self.0.begin().await?; let page = match select_page_by_title(&mut tx, title).await { Ok(page) => page, Err(sqlx::Error::RowNotFound) => match create_page_for_title(&mut tx, title).await { Ok(page) => page, Err(e) => return Err(NoteStoreError::DBError(e)) }, Err(e) => return Err(NoteStoreError::DBError(e)), }; // Todo: Replace vec with the results of the CTE return Ok((page, vec![])); } } // ___ _ _ // | _ \_ _(_)_ ____ _| |_ ___ // | _/ '_| \ V / _` | _/ -_) // |_| |_| |_|\_/\__,_|\__\___| // // I'm putting a lot of faith in Rust's ability to inline stuff. I'm // sure this is okay. But really, this lets the API be clean and // coherent and easily readable, and hides away the gnarliness of some // of the SQL queries. async fn select_page_by_slug<'e, E>(executor: E, slug: &str) -> SqlResult where E: 'e + Executor<'e, Database = Sqlite>, { let select_one_page_by_slug_sql = concat!( "SELECT id, title, slug, note_id, creation_date, updated_date, ", "lastview_date, deleted_date FROM pages WHERE slug=?;" ); Ok(sqlx::query_as(&select_one_page_by_slug_sql) .bind(&slug) .fetch_one(executor) .await?) } async fn select_page_by_title<'e, E>(executor: E, title: &str) -> SqlResult where E: 'e + Executor<'e, Database = Sqlite>, { let select_one_page_by_title_sql = concat!( "SELECT id, title, slug, note_id, creation_date, updated_date, ", "lastview_date, deleted_date FROM pages WHERE title=?;" ); Ok(sqlx::query_as(&select_one_page_by_title_sql) .bind(&title) .fetch_one(executor) .await?) } async fn reset_database<'e, E>(executor: E) -> SqlResult<()> where E: 'e + Executor<'e, Database = Sqlite>, { let initialize_sql = include_str!("sql/initialize_database.sql"); sqlx::query(initialize_sql).execute(executor).await.map(|_| ()) } async fn get_note_collection_for_root<'e, E>(executor: E, root: i64) -> SqlResult> where E: 'e + Executor<'e, Database = Sqlite>, { let select_note_collection_for_root = include_str!("sql/select_note_collection_from_root.sql"); Ok(sqlx::query_as(&select_note_collection_for_root) .bind(&root) .fetch_all(executor) .await?) } async fn insert_one_new_note<'e, E>(executor: E, note: &NewNote) -> SqlResult where E: 'e + Executor<'e, Database = Sqlite>, { let insert_one_note_sql = concat!( "INSERT INTO notes ( ", " uuid, ", " content, ", " notetype, ", " creation_date, ", " updated_date, ", " lastview_date) ", "VALUES (?, ?, ?, ?, ?, ?);" ); Ok(sqlx::query(insert_one_note_sql) .bind(¬e.uuid) .bind(¬e.content) .bind(¬e.notetype) .bind(¬e.creation_date) .bind(¬e.updated_date) .bind(¬e.lastview_date) .execute(executor) .await? .last_insert_rowid()) } fn find_maximal_slug(slugs: &Vec) -> Option { lazy_static! { static ref RE_CAP_NUM: Regex = Regex::new(r"-(\d+)$").unwrap(); } if slugs.len() == 0 { return None; } let mut slug_counters: Vec = slugs .iter() .filter_map(|slug| RE_CAP_NUM.captures(&slug.slug)) .map(|cap| cap.get(1).unwrap().as_str().parse::().unwrap()) .collect(); slug_counters.sort_unstable(); slug_counters.pop() } // Given an initial string and an existing collection of slugs, // generate a new slug that does not conflict with the current // collection. async fn generate_slug<'e, E>(executor: E, title: &str) -> SqlResult where E: 'e + Executor<'e, Database = Sqlite>, { lazy_static! { static ref RE_STRIP_NUM: Regex = Regex::new(r"-\d+$").unwrap(); } let initial_slug = slugify(title); let sample_slug = RE_STRIP_NUM.replace_all(&initial_slug, ""); let slug_finder_sql = "SELECT slug FROM pages WHERE slug LIKE '?%';"; let similar_slugs: Vec = sqlx::query_as(&slug_finder_sql) .bind(&*sample_slug) .fetch_all(executor) .await?; let maximal_slug = find_maximal_slug(&similar_slugs); match maximal_slug { None => Ok(initial_slug), Some(max_slug) => Ok(format!("{}-{}", initial_slug, max_slug + 1)), } } async fn insert_one_new_page<'e, E>(executor: E, page: &NewPage) -> SqlResult where E: 'e + Executor<'e, Database = Sqlite>, { let insert_one_page_sql = concat!( "INSERT INTO pages ( ", " slug, ", " title, ", " note_id, ", " creation_date, ", " updated_date, ", " lastview_date) ", "VALUES (?, ?, ?, ?, ?, ?);" ); Ok(sqlx::query(insert_one_page_sql) .bind(&page.slug) .bind(&page.title) .bind(&page.note_id) .bind(&page.creation_date) .bind(&page.updated_date) .bind(&page.lastview_date) .execute(executor) .await? .last_insert_rowid()) } async fn create_page_for_title<'e, E>(_executor: E, _title: &str) -> SqlResult where E: 'e + Executor<'e, Database = Sqlite>, { todo!() }