2022-10-27 20:58:54 +00:00
|
|
|
pub mod kvp;
|
2022-10-13 23:31:26 +00:00
|
|
|
|
2022-11-21 06:41:10 +00:00
|
|
|
// Re-export
|
|
|
|
pub use anyhow;
|
2022-11-19 23:14:13 +00:00
|
|
|
pub use indoc::indoc;
|
|
|
|
pub use lazy_static;
|
2022-11-30 18:54:01 +00:00
|
|
|
pub use smol;
|
2022-11-19 23:14:13 +00:00
|
|
|
pub use sqlez;
|
2022-11-29 01:42:18 +00:00
|
|
|
pub use sqlez_macros;
|
2022-10-18 18:43:18 +00:00
|
|
|
|
2022-11-21 06:41:10 +00:00
|
|
|
use sqlez::domain::Migrator;
|
2022-11-01 20:15:58 +00:00
|
|
|
use sqlez::thread_safe_connection::ThreadSafeConnection;
|
2022-11-30 18:54:01 +00:00
|
|
|
use sqlez_macros::sql;
|
2022-11-21 06:41:10 +00:00
|
|
|
use std::fs::{create_dir_all, remove_dir_all};
|
|
|
|
use std::path::Path;
|
2022-11-21 21:42:26 +00:00
|
|
|
use std::sync::atomic::{AtomicBool, Ordering};
|
2022-11-18 22:20:52 +00:00
|
|
|
use util::channel::{ReleaseChannel, RELEASE_CHANNEL, RELEASE_CHANNEL_NAME};
|
2022-11-17 00:35:56 +00:00
|
|
|
use util::paths::DB_DIR;
|
2022-11-04 20:22:35 +00:00
|
|
|
|
2022-11-30 18:54:01 +00:00
|
|
|
const CONNECTION_INITIALIZE_QUERY: &'static str = sql!(
|
2022-11-10 23:29:29 +00:00
|
|
|
PRAGMA synchronous=NORMAL;
|
2022-11-17 00:35:56 +00:00
|
|
|
PRAGMA busy_timeout=1;
|
2022-11-10 23:29:29 +00:00
|
|
|
PRAGMA foreign_keys=TRUE;
|
|
|
|
PRAGMA case_sensitive_like=TRUE;
|
2022-11-30 18:54:01 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
const DB_INITIALIZE_QUERY: &'static str = sql!(
|
|
|
|
PRAGMA journal_mode=WAL;
|
|
|
|
);
|
2022-11-10 23:29:29 +00:00
|
|
|
|
2022-11-21 21:42:26 +00:00
|
|
|
lazy_static::lazy_static! {
|
|
|
|
static ref DB_WIPED: AtomicBool = AtomicBool::new(false);
|
|
|
|
}
|
|
|
|
|
2022-11-14 21:18:44 +00:00
|
|
|
/// Open or create a database at the given directory path.
|
2022-12-01 00:19:46 +00:00
|
|
|
pub async fn open_db<M: Migrator>() -> ThreadSafeConnection<M> {
|
2022-11-14 21:18:44 +00:00
|
|
|
// Use 0 for now. Will implement incrementing and clearing of old db files soon TM
|
2022-11-17 00:35:56 +00:00
|
|
|
let current_db_dir = (*DB_DIR).join(Path::new(&format!("0-{}", *RELEASE_CHANNEL_NAME)));
|
|
|
|
|
2022-11-21 21:42:26 +00:00
|
|
|
if *RELEASE_CHANNEL == ReleaseChannel::Dev
|
|
|
|
&& std::env::var("WIPE_DB").is_ok()
|
|
|
|
&& !DB_WIPED.load(Ordering::Acquire)
|
|
|
|
{
|
2022-11-18 22:20:52 +00:00
|
|
|
remove_dir_all(¤t_db_dir).ok();
|
2022-11-21 21:42:26 +00:00
|
|
|
DB_WIPED.store(true, Ordering::Relaxed);
|
2022-11-18 22:20:52 +00:00
|
|
|
}
|
2022-11-17 00:35:56 +00:00
|
|
|
|
|
|
|
create_dir_all(¤t_db_dir).expect("Should be able to create the database directory");
|
2022-11-14 21:18:44 +00:00
|
|
|
let db_path = current_db_dir.join(Path::new("db.sqlite"));
|
|
|
|
|
2022-11-30 18:54:01 +00:00
|
|
|
ThreadSafeConnection::<M>::builder(db_path.to_string_lossy().as_ref(), true)
|
|
|
|
.with_db_initialization_query(DB_INITIALIZE_QUERY)
|
|
|
|
.with_connection_initialize_query(CONNECTION_INITIALIZE_QUERY)
|
|
|
|
.build()
|
|
|
|
.await
|
2022-10-18 22:58:05 +00:00
|
|
|
}
|
2022-10-13 22:43:42 +00:00
|
|
|
|
2022-12-01 00:19:46 +00:00
|
|
|
#[cfg(any(test, feature = "test-support"))]
|
|
|
|
pub async fn open_test_db<M: Migrator>(db_name: &str) -> ThreadSafeConnection<M> {
|
|
|
|
use sqlez::thread_safe_connection::locking_queue;
|
|
|
|
|
2022-11-30 18:54:01 +00:00
|
|
|
ThreadSafeConnection::<M>::builder(db_name, false)
|
|
|
|
.with_db_initialization_query(DB_INITIALIZE_QUERY)
|
|
|
|
.with_connection_initialize_query(CONNECTION_INITIALIZE_QUERY)
|
2022-11-30 20:34:42 +00:00
|
|
|
// Serialize queued writes via a mutex and run them synchronously
|
2022-12-01 00:19:46 +00:00
|
|
|
.with_write_queue_constructor(locking_queue())
|
2022-11-30 18:54:01 +00:00
|
|
|
.build()
|
|
|
|
.await
|
2022-11-14 21:18:44 +00:00
|
|
|
}
|
2022-11-04 20:22:35 +00:00
|
|
|
|
2022-11-17 00:35:56 +00:00
|
|
|
/// Implements a basic DB wrapper for a given domain
|
|
|
|
#[macro_export]
|
|
|
|
macro_rules! connection {
|
|
|
|
($id:ident: $t:ident<$d:ty>) => {
|
2022-12-01 00:19:46 +00:00
|
|
|
pub struct $t($crate::sqlez::thread_safe_connection::ThreadSafeConnection<$d>);
|
2022-11-17 00:35:56 +00:00
|
|
|
|
|
|
|
impl ::std::ops::Deref for $t {
|
2022-12-01 00:19:46 +00:00
|
|
|
type Target = $crate::sqlez::thread_safe_connection::ThreadSafeConnection<$d>;
|
2022-11-17 00:35:56 +00:00
|
|
|
|
|
|
|
fn deref(&self) -> &Self::Target {
|
|
|
|
&self.0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-01 00:19:46 +00:00
|
|
|
#[cfg(any(test, feature = "test-support"))]
|
|
|
|
$crate::lazy_static::lazy_static! {
|
|
|
|
pub static ref $id: $t = $t($crate::smol::block_on($crate::open_test_db(stringify!($id))));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(not(any(test, feature = "test-support")))]
|
|
|
|
$crate::lazy_static::lazy_static! {
|
|
|
|
pub static ref $id: $t = $t($crate::smol::block_on($crate::open_db()));
|
2022-11-17 00:35:56 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
2022-11-21 06:41:10 +00:00
|
|
|
|
|
|
|
#[macro_export]
|
2022-11-24 08:02:07 +00:00
|
|
|
macro_rules! query {
|
2022-11-29 01:42:18 +00:00
|
|
|
($vis:vis fn $id:ident() -> Result<()> { $($sql:tt)+ }) => {
|
2022-11-24 08:02:07 +00:00
|
|
|
$vis fn $id(&self) -> $crate::anyhow::Result<()> {
|
2022-11-21 18:38:16 +00:00
|
|
|
use $crate::anyhow::Context;
|
|
|
|
|
2022-11-29 01:42:18 +00:00
|
|
|
let sql_stmt = $crate::sqlez_macros::sql!($($sql)+);
|
|
|
|
|
|
|
|
self.exec(sql_stmt)?().context(::std::format!(
|
2022-11-21 18:38:16 +00:00
|
|
|
"Error in {}, exec failed to execute or parse for: {}",
|
|
|
|
::std::stringify!($id),
|
2022-11-29 01:42:18 +00:00
|
|
|
sql_stmt,
|
2022-11-21 18:38:16 +00:00
|
|
|
))
|
|
|
|
}
|
2022-11-21 06:41:10 +00:00
|
|
|
};
|
2022-11-29 01:42:18 +00:00
|
|
|
($vis:vis async fn $id:ident() -> Result<()> { $($sql:tt)+ }) => {
|
2022-11-24 08:02:07 +00:00
|
|
|
$vis async fn $id(&self) -> $crate::anyhow::Result<()> {
|
2022-11-23 09:53:58 +00:00
|
|
|
use $crate::anyhow::Context;
|
|
|
|
|
|
|
|
self.write(|connection| {
|
2022-11-29 01:42:18 +00:00
|
|
|
let sql_stmt = $crate::sqlez_macros::sql!($($sql)+);
|
|
|
|
|
|
|
|
connection.exec(sql_stmt)?().context(::std::format!(
|
2022-11-23 09:53:58 +00:00
|
|
|
"Error in {}, exec failed to execute or parse for: {}",
|
|
|
|
::std::stringify!($id),
|
2022-11-29 01:42:18 +00:00
|
|
|
sql_stmt
|
2022-11-23 09:53:58 +00:00
|
|
|
))
|
|
|
|
}).await
|
|
|
|
}
|
|
|
|
};
|
2022-11-29 01:42:18 +00:00
|
|
|
($vis:vis fn $id:ident($($arg:ident: $arg_type:ty),+) -> Result<()> { $($sql:tt)+ }) => {
|
2022-11-24 08:02:07 +00:00
|
|
|
$vis fn $id(&self, $($arg: $arg_type),+) -> $crate::anyhow::Result<()> {
|
2022-11-21 18:38:16 +00:00
|
|
|
use $crate::anyhow::Context;
|
|
|
|
|
2022-11-29 01:42:18 +00:00
|
|
|
let sql_stmt = $crate::sqlez_macros::sql!($($sql)+);
|
|
|
|
|
|
|
|
self.exec_bound::<($($arg_type),+)>(sql_stmt)?(($($arg),+))
|
2022-11-21 18:38:16 +00:00
|
|
|
.context(::std::format!(
|
|
|
|
"Error in {}, exec_bound failed to execute or parse for: {}",
|
|
|
|
::std::stringify!($id),
|
2022-11-29 01:42:18 +00:00
|
|
|
sql_stmt
|
2022-11-21 18:38:16 +00:00
|
|
|
))
|
|
|
|
}
|
2022-11-21 06:41:10 +00:00
|
|
|
};
|
2022-11-29 01:42:18 +00:00
|
|
|
($vis:vis async fn $id:ident($arg:ident: $arg_type:ty) -> Result<()> { $($sql:tt)+ }) => {
|
2022-11-24 08:02:07 +00:00
|
|
|
$vis async fn $id(&self, $arg: $arg_type) -> $crate::anyhow::Result<()> {
|
|
|
|
use $crate::anyhow::Context;
|
|
|
|
|
|
|
|
self.write(move |connection| {
|
2022-11-29 01:42:18 +00:00
|
|
|
let sql_stmt = $crate::sqlez_macros::sql!($($sql)+);
|
|
|
|
|
|
|
|
connection.exec_bound::<$arg_type>(sql_stmt)?($arg)
|
2022-11-24 08:02:07 +00:00
|
|
|
.context(::std::format!(
|
|
|
|
"Error in {}, exec_bound failed to execute or parse for: {}",
|
|
|
|
::std::stringify!($id),
|
2022-11-29 01:42:18 +00:00
|
|
|
sql_stmt
|
2022-11-24 08:02:07 +00:00
|
|
|
))
|
|
|
|
}).await
|
|
|
|
}
|
|
|
|
};
|
2022-11-29 01:42:18 +00:00
|
|
|
($vis:vis async fn $id:ident($($arg:ident: $arg_type:ty),+) -> Result<()> { $($sql:tt)+ }) => {
|
2022-11-24 08:02:07 +00:00
|
|
|
$vis async fn $id(&self, $($arg: $arg_type),+) -> $crate::anyhow::Result<()> {
|
2022-11-23 09:53:58 +00:00
|
|
|
use $crate::anyhow::Context;
|
|
|
|
|
|
|
|
self.write(move |connection| {
|
2022-11-29 01:42:18 +00:00
|
|
|
let sql_stmt = $crate::sqlez_macros::sql!($($sql)+);
|
|
|
|
|
|
|
|
connection.exec_bound::<($($arg_type),+)>(sql_stmt)?(($($arg),+))
|
2022-11-23 09:53:58 +00:00
|
|
|
.context(::std::format!(
|
|
|
|
"Error in {}, exec_bound failed to execute or parse for: {}",
|
|
|
|
::std::stringify!($id),
|
2022-11-29 01:42:18 +00:00
|
|
|
sql_stmt
|
2022-11-23 09:53:58 +00:00
|
|
|
))
|
|
|
|
}).await
|
|
|
|
}
|
|
|
|
};
|
2022-11-29 01:42:18 +00:00
|
|
|
($vis:vis fn $id:ident() -> Result<Vec<$return_type:ty>> { $($sql:tt)+ }) => {
|
2022-11-24 08:02:07 +00:00
|
|
|
$vis fn $id(&self) -> $crate::anyhow::Result<Vec<$return_type>> {
|
2022-11-21 06:41:10 +00:00
|
|
|
use $crate::anyhow::Context;
|
|
|
|
|
2022-11-29 01:42:18 +00:00
|
|
|
let sql_stmt = $crate::sqlez_macros::sql!($($sql)+);
|
|
|
|
|
|
|
|
self.select::<$return_type>(sql_stmt)?(())
|
2022-11-21 06:41:10 +00:00
|
|
|
.context(::std::format!(
|
|
|
|
"Error in {}, select_row failed to execute or parse for: {}",
|
|
|
|
::std::stringify!($id),
|
2022-11-29 01:42:18 +00:00
|
|
|
sql_stmt
|
2022-11-21 06:41:10 +00:00
|
|
|
))
|
|
|
|
}
|
|
|
|
};
|
2022-12-01 01:28:49 +00:00
|
|
|
($vis:vis async fn $id:ident() -> Result<Vec<$return_type:ty>> { $($sql:tt)+ }) => {
|
2022-11-23 09:53:58 +00:00
|
|
|
pub async fn $id(&self) -> $crate::anyhow::Result<Vec<$return_type>> {
|
|
|
|
use $crate::anyhow::Context;
|
|
|
|
|
|
|
|
self.write(|connection| {
|
2022-11-29 01:42:18 +00:00
|
|
|
let sql_stmt = $crate::sqlez_macros::sql!($($sql)+);
|
|
|
|
|
|
|
|
connection.select::<$return_type>(sql_stmt)?(())
|
2022-11-23 09:53:58 +00:00
|
|
|
.context(::std::format!(
|
|
|
|
"Error in {}, select_row failed to execute or parse for: {}",
|
|
|
|
::std::stringify!($id),
|
2022-11-29 01:42:18 +00:00
|
|
|
sql_stmt
|
2022-11-23 09:53:58 +00:00
|
|
|
))
|
|
|
|
}).await
|
|
|
|
}
|
|
|
|
};
|
2022-11-29 01:42:18 +00:00
|
|
|
($vis:vis fn $id:ident($($arg:ident: $arg_type:ty),+) -> Result<Vec<$return_type:ty>> { $($sql:tt)+ }) => {
|
2022-11-24 08:02:07 +00:00
|
|
|
$vis fn $id(&self, $($arg: $arg_type),+) -> $crate::anyhow::Result<Vec<$return_type>> {
|
2022-11-21 06:41:10 +00:00
|
|
|
use $crate::anyhow::Context;
|
|
|
|
|
2022-11-29 01:42:18 +00:00
|
|
|
let sql_stmt = $crate::sqlez_macros::sql!($($sql)+);
|
|
|
|
|
|
|
|
self.select_bound::<($($arg_type),+), $return_type>(sql_stmt)?(($($arg),+))
|
2022-11-21 06:41:10 +00:00
|
|
|
.context(::std::format!(
|
|
|
|
"Error in {}, exec_bound failed to execute or parse for: {}",
|
|
|
|
::std::stringify!($id),
|
2022-11-29 01:42:18 +00:00
|
|
|
sql_stmt
|
2022-11-21 06:41:10 +00:00
|
|
|
))
|
|
|
|
}
|
|
|
|
};
|
2022-11-29 01:42:18 +00:00
|
|
|
($vis:vis async fn $id:ident($($arg:ident: $arg_type:ty),+) -> Result<Vec<$return_type:ty>> { $($sql:tt)+ }) => {
|
2022-11-24 08:02:07 +00:00
|
|
|
$vis async fn $id(&self, $($arg: $arg_type),+) -> $crate::anyhow::Result<Vec<$return_type>> {
|
2022-11-23 09:53:58 +00:00
|
|
|
use $crate::anyhow::Context;
|
|
|
|
|
|
|
|
self.write(|connection| {
|
2022-11-29 01:42:18 +00:00
|
|
|
let sql_stmt = $crate::sqlez_macros::sql!($($sql)+);
|
|
|
|
|
|
|
|
connection.select_bound::<($($arg_type),+), $return_type>(sql_stmt)?(($($arg),+))
|
2022-11-23 09:53:58 +00:00
|
|
|
.context(::std::format!(
|
|
|
|
"Error in {}, exec_bound failed to execute or parse for: {}",
|
|
|
|
::std::stringify!($id),
|
2022-11-29 01:42:18 +00:00
|
|
|
sql_stmt
|
2022-11-23 09:53:58 +00:00
|
|
|
))
|
|
|
|
}).await
|
|
|
|
}
|
|
|
|
};
|
2022-11-29 01:42:18 +00:00
|
|
|
($vis:vis fn $id:ident() -> Result<Option<$return_type:ty>> { $($sql:tt)+ }) => {
|
2022-11-24 08:02:07 +00:00
|
|
|
$vis fn $id(&self) -> $crate::anyhow::Result<Option<$return_type>> {
|
2022-11-21 06:41:10 +00:00
|
|
|
use $crate::anyhow::Context;
|
|
|
|
|
2022-11-29 01:42:18 +00:00
|
|
|
let sql_stmt = $crate::sqlez_macros::sql!($($sql)+);
|
|
|
|
|
|
|
|
self.select_row::<$return_type>(sql_stmt)?()
|
2022-11-21 06:41:10 +00:00
|
|
|
.context(::std::format!(
|
|
|
|
"Error in {}, select_row failed to execute or parse for: {}",
|
|
|
|
::std::stringify!($id),
|
2022-11-29 01:42:18 +00:00
|
|
|
sql_stmt
|
2022-11-21 06:41:10 +00:00
|
|
|
))
|
|
|
|
}
|
|
|
|
};
|
2022-11-29 01:42:18 +00:00
|
|
|
($vis:vis async fn $id:ident() -> Result<Option<$return_type:ty>> { $($sql:tt)+ }) => {
|
2022-11-24 08:02:07 +00:00
|
|
|
$vis async fn $id(&self) -> $crate::anyhow::Result<Option<$return_type>> {
|
2022-11-23 09:53:58 +00:00
|
|
|
use $crate::anyhow::Context;
|
|
|
|
|
|
|
|
self.write(|connection| {
|
2022-11-29 01:42:18 +00:00
|
|
|
let sql_stmt = $crate::sqlez_macros::sql!($($sql)+);
|
|
|
|
|
|
|
|
connection.select_row::<$return_type>(sql_stmt)?()
|
2022-11-23 09:53:58 +00:00
|
|
|
.context(::std::format!(
|
|
|
|
"Error in {}, select_row failed to execute or parse for: {}",
|
|
|
|
::std::stringify!($id),
|
2022-11-29 01:42:18 +00:00
|
|
|
sql_stmt
|
2022-11-23 09:53:58 +00:00
|
|
|
))
|
|
|
|
}).await
|
|
|
|
}
|
|
|
|
};
|
2022-11-29 01:42:18 +00:00
|
|
|
($vis:vis fn $id:ident($arg:ident: $arg_type:ty) -> Result<Option<$return_type:ty>> { $($sql:tt)+ }) => {
|
2022-11-24 08:02:07 +00:00
|
|
|
$vis fn $id(&self, $arg: $arg_type) -> $crate::anyhow::Result<Option<$return_type>> {
|
|
|
|
use $crate::anyhow::Context;
|
|
|
|
|
2022-11-29 01:42:18 +00:00
|
|
|
let sql_stmt = $crate::sqlez_macros::sql!($($sql)+);
|
|
|
|
|
|
|
|
self.select_row_bound::<$arg_type, $return_type>(sql_stmt)?($arg)
|
2022-11-24 08:02:07 +00:00
|
|
|
.context(::std::format!(
|
|
|
|
"Error in {}, select_row_bound failed to execute or parse for: {}",
|
|
|
|
::std::stringify!($id),
|
2022-11-29 01:42:18 +00:00
|
|
|
sql_stmt
|
2022-11-24 08:02:07 +00:00
|
|
|
))
|
|
|
|
|
|
|
|
}
|
|
|
|
};
|
2022-11-29 01:42:18 +00:00
|
|
|
($vis:vis fn $id:ident($($arg:ident: $arg_type:ty),+) -> Result<Option<$return_type:ty>> { $($sql:tt)+ }) => {
|
2022-11-24 08:02:07 +00:00
|
|
|
$vis fn $id(&self, $($arg: $arg_type),+) -> $crate::anyhow::Result<Option<$return_type>> {
|
2022-11-21 06:41:10 +00:00
|
|
|
use $crate::anyhow::Context;
|
|
|
|
|
2022-11-29 01:42:18 +00:00
|
|
|
let sql_stmt = $crate::sqlez_macros::sql!($($sql)+);
|
|
|
|
|
|
|
|
self.select_row_bound::<($($arg_type),+), $return_type>(sql_stmt)?(($($arg),+))
|
2022-11-21 06:41:10 +00:00
|
|
|
.context(::std::format!(
|
|
|
|
"Error in {}, select_row_bound failed to execute or parse for: {}",
|
|
|
|
::std::stringify!($id),
|
2022-11-29 01:42:18 +00:00
|
|
|
sql_stmt
|
2022-11-21 06:41:10 +00:00
|
|
|
))
|
|
|
|
|
|
|
|
}
|
|
|
|
};
|
2022-11-29 01:42:18 +00:00
|
|
|
($vis:vis async fn $id:ident($($arg:ident: $arg_type:ty),+) -> Result<Option<$return_type:ty>> { $($sql:tt)+ }) => {
|
2022-11-24 08:02:07 +00:00
|
|
|
$vis async fn $id(&self, $($arg: $arg_type),+) -> $crate::anyhow::Result<Option<$return_type>> {
|
2022-11-23 09:53:58 +00:00
|
|
|
use $crate::anyhow::Context;
|
|
|
|
|
2022-11-29 01:42:18 +00:00
|
|
|
|
2022-11-23 09:53:58 +00:00
|
|
|
self.write(|connection| {
|
2022-11-29 01:42:18 +00:00
|
|
|
let sql_stmt = $crate::sqlez_macros::sql!($($sql)+);
|
|
|
|
|
2022-11-24 08:02:07 +00:00
|
|
|
connection.select_row_bound::<($($arg_type),+), $return_type>(indoc! { $sql })?(($($arg),+))
|
2022-11-23 09:53:58 +00:00
|
|
|
.context(::std::format!(
|
|
|
|
"Error in {}, select_row_bound failed to execute or parse for: {}",
|
|
|
|
::std::stringify!($id),
|
2022-11-29 01:42:18 +00:00
|
|
|
sql_stmt
|
2022-11-23 09:53:58 +00:00
|
|
|
))
|
|
|
|
}).await
|
|
|
|
}
|
|
|
|
};
|
2022-11-29 01:42:18 +00:00
|
|
|
($vis:vis fn $id:ident() -> Result<$return_type:ty> { $($sql:tt)+ }) => {
|
2022-11-24 08:02:07 +00:00
|
|
|
$vis fn $id(&self) -> $crate::anyhow::Result<$return_type> {
|
2022-11-21 18:52:19 +00:00
|
|
|
use $crate::anyhow::Context;
|
|
|
|
|
2022-11-29 01:42:18 +00:00
|
|
|
let sql_stmt = $crate::sqlez_macros::sql!($($sql)+);
|
|
|
|
|
2022-11-24 08:02:07 +00:00
|
|
|
self.select_row::<$return_type>(indoc! { $sql })?()
|
2022-11-21 18:52:19 +00:00
|
|
|
.context(::std::format!(
|
|
|
|
"Error in {}, select_row_bound failed to execute or parse for: {}",
|
|
|
|
::std::stringify!($id),
|
2022-11-29 01:42:18 +00:00
|
|
|
sql_stmt
|
2022-11-21 18:52:19 +00:00
|
|
|
))?
|
|
|
|
.context(::std::format!(
|
|
|
|
"Error in {}, select_row_bound expected single row result but found none for: {}",
|
|
|
|
::std::stringify!($id),
|
2022-11-29 01:42:18 +00:00
|
|
|
sql_stmt
|
2022-11-21 18:52:19 +00:00
|
|
|
))
|
|
|
|
}
|
|
|
|
};
|
2022-11-29 01:42:18 +00:00
|
|
|
($vis:vis async fn $id:ident() -> Result<$return_type:ty> { $($sql:tt)+ }) => {
|
2022-11-24 08:02:07 +00:00
|
|
|
$vis async fn $id(&self) -> $crate::anyhow::Result<$return_type> {
|
2022-11-23 09:53:58 +00:00
|
|
|
use $crate::anyhow::Context;
|
|
|
|
|
|
|
|
self.write(|connection| {
|
2022-11-29 01:42:18 +00:00
|
|
|
let sql_stmt = $crate::sqlez_macros::sql!($($sql)+);
|
|
|
|
|
|
|
|
connection.select_row::<$return_type>(sql_stmt)?()
|
2022-11-23 09:53:58 +00:00
|
|
|
.context(::std::format!(
|
|
|
|
"Error in {}, select_row_bound failed to execute or parse for: {}",
|
|
|
|
::std::stringify!($id),
|
2022-11-29 01:42:18 +00:00
|
|
|
sql_stmt
|
2022-11-23 09:53:58 +00:00
|
|
|
))?
|
|
|
|
.context(::std::format!(
|
|
|
|
"Error in {}, select_row_bound expected single row result but found none for: {}",
|
|
|
|
::std::stringify!($id),
|
2022-11-29 01:42:18 +00:00
|
|
|
sql_stmt
|
2022-11-23 09:53:58 +00:00
|
|
|
))
|
|
|
|
}).await
|
|
|
|
}
|
|
|
|
};
|
2022-11-29 01:42:18 +00:00
|
|
|
($vis:vis fn $id:ident($arg:ident: $arg_type:ty) -> Result<$return_type:ty> { $($sql:tt)+ }) => {
|
2022-11-24 08:02:07 +00:00
|
|
|
pub fn $id(&self, $arg: $arg_type) -> $crate::anyhow::Result<$return_type> {
|
|
|
|
use $crate::anyhow::Context;
|
|
|
|
|
2022-11-29 01:42:18 +00:00
|
|
|
let sql_stmt = $crate::sqlez_macros::sql!($($sql)+);
|
|
|
|
|
|
|
|
self.select_row_bound::<$arg_type, $return_type>(sql_stmt)?($arg)
|
2022-11-24 08:02:07 +00:00
|
|
|
.context(::std::format!(
|
|
|
|
"Error in {}, select_row_bound failed to execute or parse for: {}",
|
|
|
|
::std::stringify!($id),
|
2022-11-29 01:42:18 +00:00
|
|
|
sql_stmt
|
2022-11-24 08:02:07 +00:00
|
|
|
))?
|
|
|
|
.context(::std::format!(
|
|
|
|
"Error in {}, select_row_bound expected single row result but found none for: {}",
|
|
|
|
::std::stringify!($id),
|
2022-11-29 01:42:18 +00:00
|
|
|
sql_stmt
|
2022-11-24 08:02:07 +00:00
|
|
|
))
|
|
|
|
}
|
|
|
|
};
|
2022-11-29 01:42:18 +00:00
|
|
|
($vis:vis fn $id:ident($($arg:ident: $arg_type:ty),+) -> Result<$return_type:ty> { $($sql:tt)+ }) => {
|
2022-11-24 08:02:07 +00:00
|
|
|
$vis fn $id(&self, $($arg: $arg_type),+) -> $crate::anyhow::Result<$return_type> {
|
2022-11-21 18:52:19 +00:00
|
|
|
use $crate::anyhow::Context;
|
|
|
|
|
2022-11-29 01:42:18 +00:00
|
|
|
let sql_stmt = $crate::sqlez_macros::sql!($($sql)+);
|
|
|
|
|
|
|
|
self.select_row_bound::<($($arg_type),+), $return_type>(sql_stmt)?(($($arg),+))
|
2022-11-21 18:52:19 +00:00
|
|
|
.context(::std::format!(
|
|
|
|
"Error in {}, select_row_bound failed to execute or parse for: {}",
|
|
|
|
::std::stringify!($id),
|
2022-11-29 01:42:18 +00:00
|
|
|
sql_stmt
|
2022-11-21 18:52:19 +00:00
|
|
|
))?
|
|
|
|
.context(::std::format!(
|
|
|
|
"Error in {}, select_row_bound expected single row result but found none for: {}",
|
|
|
|
::std::stringify!($id),
|
2022-11-29 01:42:18 +00:00
|
|
|
sql_stmt
|
2022-11-21 18:52:19 +00:00
|
|
|
))
|
|
|
|
}
|
|
|
|
};
|
2022-11-29 01:42:18 +00:00
|
|
|
($vis:vis fn async $id:ident($($arg:ident: $arg_type:ty),+) -> Result<$return_type:ty> { $($sql:tt)+ }) => {
|
2022-11-24 08:02:07 +00:00
|
|
|
$vis async fn $id(&self, $($arg: $arg_type),+) -> $crate::anyhow::Result<$return_type> {
|
2022-11-23 09:53:58 +00:00
|
|
|
use $crate::anyhow::Context;
|
|
|
|
|
2022-11-29 01:42:18 +00:00
|
|
|
|
2022-11-23 09:53:58 +00:00
|
|
|
self.write(|connection| {
|
2022-11-29 01:42:18 +00:00
|
|
|
let sql_stmt = $crate::sqlez_macros::sql!($($sql)+);
|
|
|
|
|
|
|
|
connection.select_row_bound::<($($arg_type),+), $return_type>(sql_stmt)?(($($arg),+))
|
2022-11-23 09:53:58 +00:00
|
|
|
.context(::std::format!(
|
|
|
|
"Error in {}, select_row_bound failed to execute or parse for: {}",
|
|
|
|
::std::stringify!($id),
|
2022-11-29 01:42:18 +00:00
|
|
|
sql_stmt
|
2022-11-23 09:53:58 +00:00
|
|
|
))?
|
|
|
|
.context(::std::format!(
|
|
|
|
"Error in {}, select_row_bound expected single row result but found none for: {}",
|
|
|
|
::std::stringify!($id),
|
2022-11-29 01:42:18 +00:00
|
|
|
sql_stmt
|
2022-11-23 09:53:58 +00:00
|
|
|
))
|
|
|
|
}).await
|
|
|
|
}
|
|
|
|
};
|
2022-11-21 06:41:10 +00:00
|
|
|
}
|