notesmachine/server/nm-store/src/store_private.rs

56 lines
1.4 KiB
Rust

use crate::structs::*;
use lazy_static::lazy_static;
use regex::Regex;
use slug::slugify;
use sqlx::{
sqlite::{Sqlite, SqliteRow},
Done, Executor, Row,
};
use std::collections::HashSet;
type SqlResult<T> = sqlx::Result<T>;
// ___ _ _
// | _ \_ _(_)_ ____ _| |_ ___
// | _/ '_| \ 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.
lazy_static! {
static ref select_kasten_by_title_sql: String = str::replace(
include_str!("sql/select_kasten_by_parameter.sql"),
"QUERYPARAMETER",
"zetteln.title");
}
lazy_static! {
static ref select_kasten_by_id_sql: String = str::replace(
include_str!("sql/select_kasten_by_parameter.sql"),
"QUERYPARAMETER",
"zetteln.id");
}
pub(crate) async fn reset_database<'a, E>(executor: E) -> SqlResult<()>
where
E: Executor<'a, Database = Sqlite>,
{
let initialize_sql = include_str!("sql/initialize_database.sql");
sqlx::query(initialize_sql).execute(executor).await.map(|_| ())
}
pub(crate) async fn select_kasten_by_slug<'a, E>(executor: E, slug: &str) -> SqlResult<Vec<RawZettle>>
where
E: Executor<'a, Database = Sqlite>,
{
Ok(sqlx::query_as(&select_kasten_by_id_sql)
.bind(&slug)
.fetch_all(executor)
.await?)
}