rename sql_method to query and adjust the syntax to more closely match function definitions

This commit is contained in:
Kay Simmons 2022-11-24 00:02:07 -08:00 committed by Mikayla Maki
parent 1cc3e4820a
commit 359b8aaf47
7 changed files with 192 additions and 124 deletions

View file

@ -75,45 +75,59 @@ macro_rules! connection {
}
#[macro_export]
macro_rules! sql_method {
($id:ident() -> Result<()>: $sql:expr) => {
pub fn $id(&self) -> $crate::anyhow::Result<()> {
macro_rules! query {
($vis:vis fn $id:ident() -> Result<()> { $sql:expr }) => {
$vis fn $id(&self) -> $crate::anyhow::Result<()> {
use $crate::anyhow::Context;
self.exec($sql)?().context(::std::format!(
"Error in {}, exec failed to execute or parse for: {}",
::std::stringify!($id),
::std::stringify!($sql),
$sql,
))
}
};
(async $id:ident() -> Result<()>: $sql:expr) => {
pub async fn $id(&self) -> $crate::anyhow::Result<()> {
($vis:vis async fn $id:ident() -> Result<()> { $sql:expr }) => {
$vis async fn $id(&self) -> $crate::anyhow::Result<()> {
use $crate::anyhow::Context;
self.write(|connection| {
connection.exec($sql)?().context(::std::format!(
"Error in {}, exec failed to execute or parse for: {}",
::std::stringify!($id),
::std::stringify!($sql),
$sql,
))
}).await
}
};
($id:ident($($arg:ident: $arg_type:ty),+) -> Result<()>: $sql:expr) => {
pub fn $id(&self, $($arg: $arg_type),+) -> $crate::anyhow::Result<()> {
($vis:vis fn $id:ident($($arg:ident: $arg_type:ty),+) -> Result<()> { $sql:expr }) => {
$vis fn $id(&self, $($arg: $arg_type),+) -> $crate::anyhow::Result<()> {
use $crate::anyhow::Context;
self.exec_bound::<($($arg_type),+)>($sql)?(($($arg),+))
.context(::std::format!(
"Error in {}, exec_bound failed to execute or parse for: {}",
::std::stringify!($id),
::std::stringify!($sql),
$sql,
))
}
};
(async $id:ident($($arg:ident: $arg_type:ty),+) -> Result<()>: $sql:expr) => {
pub async fn $id(&self, $($arg: $arg_type),+) -> $crate::anyhow::Result<()> {
($vis:vis async fn $id:ident($arg:ident: $arg_type:ty) -> Result<()> { $sql:expr }) => {
$vis async fn $id(&self, $arg: $arg_type) -> $crate::anyhow::Result<()> {
use $crate::anyhow::Context;
self.write(move |connection| {
connection.exec_bound::<$arg_type>($sql)?($arg)
.context(::std::format!(
"Error in {}, exec_bound failed to execute or parse for: {}",
::std::stringify!($id),
$sql,
))
}).await
}
};
($vis:vis async fn $id:ident($($arg:ident: $arg_type:ty),+) -> Result<()> { $sql:expr }) => {
$vis async fn $id(&self, $($arg: $arg_type),+) -> $crate::anyhow::Result<()> {
use $crate::anyhow::Context;
self.write(move |connection| {
@ -121,24 +135,24 @@ macro_rules! sql_method {
.context(::std::format!(
"Error in {}, exec_bound failed to execute or parse for: {}",
::std::stringify!($id),
::std::stringify!($sql),
$sql,
))
}).await
}
};
($id:ident() -> Result<Vec<$return_type:ty>>: $sql:expr) => {
pub fn $id(&self) -> $crate::anyhow::Result<Vec<$return_type>> {
($vis:vis fn $id:ident() -> Result<Vec<$return_type:ty>> { $sql:expr }) => {
$vis fn $id(&self) -> $crate::anyhow::Result<Vec<$return_type>> {
use $crate::anyhow::Context;
self.select::<$return_type>($sql)?(())
.context(::std::format!(
"Error in {}, select_row failed to execute or parse for: {}",
::std::stringify!($id),
::std::stringify!($sql),
$sql,
))
}
};
(async $id:ident() -> Result<Vec<$return_type:ty>>: $sql:expr) => {
($vis:vis async fn $id:ident() -> Result<Vec<$return_type:ty>> { $sql:expr }) => {
pub async fn $id(&self) -> $crate::anyhow::Result<Vec<$return_type>> {
use $crate::anyhow::Context;
@ -147,25 +161,25 @@ macro_rules! sql_method {
.context(::std::format!(
"Error in {}, select_row failed to execute or parse for: {}",
::std::stringify!($id),
::std::stringify!($sql),
$sql,
))
}).await
}
};
($id:ident($($arg:ident: $arg_type:ty),+) -> Result<Vec<$return_type:ty>>: $sql:expr) => {
pub fn $id(&self, $($arg: $arg_type),+) -> $crate::anyhow::Result<Vec<$return_type>> {
($vis:vis fn $id:ident($($arg:ident: $arg_type:ty),+) -> Result<Vec<$return_type:ty>> { $sql:expr }) => {
$vis fn $id(&self, $($arg: $arg_type),+) -> $crate::anyhow::Result<Vec<$return_type>> {
use $crate::anyhow::Context;
self.select_bound::<($($arg_type),+), $return_type>($sql)?(($($arg),+))
.context(::std::format!(
"Error in {}, exec_bound failed to execute or parse for: {}",
::std::stringify!($id),
::std::stringify!($sql),
$sql,
))
}
};
(async $id:ident($($arg:ident: $arg_type:ty),+) -> Result<Vec<$return_type:ty>>: $sql:expr) => {
pub async fn $id(&self, $($arg: $arg_type),+) -> $crate::anyhow::Result<Vec<$return_type>> {
($vis:vis async fn $id:ident($($arg:ident: $arg_type:ty),+) -> Result<Vec<$return_type:ty>> { $sql:expr }) => {
$vis async fn $id(&self, $($arg: $arg_type),+) -> $crate::anyhow::Result<Vec<$return_type>> {
use $crate::anyhow::Context;
self.write(|connection| {
@ -173,25 +187,25 @@ macro_rules! sql_method {
.context(::std::format!(
"Error in {}, exec_bound failed to execute or parse for: {}",
::std::stringify!($id),
::std::stringify!($sql),
$sql,
))
}).await
}
};
($id:ident() -> Result<Option<$return_type:ty>>: $sql:expr) => {
pub fn $id(&self) -> $crate::anyhow::Result<Option<$return_type>> {
($vis:vis fn $id:ident() -> Result<Option<$return_type:ty>> { $sql:expr }) => {
$vis fn $id(&self) -> $crate::anyhow::Result<Option<$return_type>> {
use $crate::anyhow::Context;
self.select_row::<$return_type>($sql)?()
.context(::std::format!(
"Error in {}, select_row failed to execute or parse for: {}",
::std::stringify!($id),
::std::stringify!($sql),
$sql,
))
}
};
(async $id:ident() -> Result<Option<$return_type:ty>>: $sql:expr) => {
pub async fn $id(&self) -> $crate::anyhow::Result<Option<$return_type>> {
($vis:vis async fn $id:ident() -> Result<Option<$return_type:ty>> { $sql:expr }) => {
$vis async fn $id(&self) -> $crate::anyhow::Result<Option<$return_type>> {
use $crate::anyhow::Context;
self.write(|connection| {
@ -199,57 +213,70 @@ macro_rules! sql_method {
.context(::std::format!(
"Error in {}, select_row failed to execute or parse for: {}",
::std::stringify!($id),
::std::stringify!($sql),
$sql,
))
}).await
}
};
($id:ident($($arg:ident: $arg_type:ty),+) -> Result<Option<$return_type:ty>>: $sql:expr) => {
pub fn $id(&self, $($arg: $arg_type),+) -> $crate::anyhow::Result<Option<$return_type>> {
($vis:vis fn $id:ident($arg:ident: $arg_type:ty) -> Result<Option<$return_type:ty>> { $sql:expr }) => {
$vis fn $id(&self, $arg: $arg_type) -> $crate::anyhow::Result<Option<$return_type>> {
use $crate::anyhow::Context;
self.select_row_bound::<$arg_type, $return_type>($sql)?($arg)
.context(::std::format!(
"Error in {}, select_row_bound failed to execute or parse for: {}",
::std::stringify!($id),
$sql,
))
}
};
($vis:vis fn $id:ident($($arg:ident: $arg_type:ty),+) -> Result<Option<$return_type:ty>> { $sql:expr }) => {
$vis fn $id(&self, $($arg: $arg_type),+) -> $crate::anyhow::Result<Option<$return_type>> {
use $crate::anyhow::Context;
self.select_row_bound::<($($arg_type),+), $return_type>($sql)?(($($arg),+))
.context(::std::format!(
"Error in {}, select_row_bound failed to execute or parse for: {}",
::std::stringify!($id),
::std::stringify!($sql),
$sql,
))
}
};
(async $id:ident($($arg:ident: $arg_type:ty),+) -> Result<Option<$return_type:ty>>: $sql:expr) => {
pub async fn $id(&self, $($arg: $arg_type),+) -> $crate::anyhow::Result<Option<$return_type>> {
($vis:vis async fn $id:ident($($arg:ident: $arg_type:ty),+) -> Result<Option<$return_type:ty>> { $sql:expr }) => {
$vis async fn $id(&self, $($arg: $arg_type),+) -> $crate::anyhow::Result<Option<$return_type>> {
use $crate::anyhow::Context;
self.write(|connection| {
connection.select_row_bound::<($($arg_type),+), $return_type>($sql)?(($($arg),+))
connection.select_row_bound::<($($arg_type),+), $return_type>(indoc! { $sql })?(($($arg),+))
.context(::std::format!(
"Error in {}, select_row_bound failed to execute or parse for: {}",
::std::stringify!($id),
::std::stringify!($sql),
$sql,
))
}).await
}
};
($id:ident() -> Result<$return_type:ty>: $sql:expr) => {
pub fn $id(&self) -> $crate::anyhow::Result<$return_type> {
($vis:vis fn $id:ident() -> Result<$return_type:ty> { $sql:expr }) => {
$vis fn $id(&self) -> $crate::anyhow::Result<$return_type> {
use $crate::anyhow::Context;
self.select_row::<$return_type>($sql)?()
self.select_row::<$return_type>(indoc! { $sql })?()
.context(::std::format!(
"Error in {}, select_row_bound failed to execute or parse for: {}",
::std::stringify!($id),
::std::stringify!($sql),
$sql,
))?
.context(::std::format!(
"Error in {}, select_row_bound expected single row result but found none for: {}",
::std::stringify!($id),
::std::stringify!($sql),
$sql,
))
}
};
(async $id:ident() -> Result<$return_type:ty>: $sql:expr) => {
pub async fn $id(&self) -> $crate::anyhow::Result<$return_type> {
($vis:vis async fn $id:ident() -> Result<$return_type:ty> { $sql:expr }) => {
$vis async fn $id(&self) -> $crate::anyhow::Result<$return_type> {
use $crate::anyhow::Context;
self.write(|connection| {
@ -257,35 +284,52 @@ macro_rules! sql_method {
.context(::std::format!(
"Error in {}, select_row_bound failed to execute or parse for: {}",
::std::stringify!($id),
::std::stringify!($sql),
$sql,
))?
.context(::std::format!(
"Error in {}, select_row_bound expected single row result but found none for: {}",
::std::stringify!($id),
::std::stringify!($sql),
$sql,
))
}).await
}
};
($id:ident($($arg:ident: $arg_type:ty),+) -> Result<$return_type:ty>: $sql:expr) => {
pub fn $id(&self, $($arg: $arg_type),+) -> $crate::anyhow::Result<$return_type> {
($vis:vis fn $id:ident($arg:ident: $arg_type:ty) -> Result<$return_type:ty> { $sql:expr }) => {
pub fn $id(&self, $arg: $arg_type) -> $crate::anyhow::Result<$return_type> {
use $crate::anyhow::Context;
self.select_row_bound::<$arg_type, $return_type>($sql)?($arg)
.context(::std::format!(
"Error in {}, select_row_bound failed to execute or parse for: {}",
::std::stringify!($id),
$sql,
))?
.context(::std::format!(
"Error in {}, select_row_bound expected single row result but found none for: {}",
::std::stringify!($id),
$sql,
))
}
};
($vis:vis fn $id:ident($($arg:ident: $arg_type:ty),+) -> Result<$return_type:ty> { $sql:expr }) => {
$vis fn $id(&self, $($arg: $arg_type),+) -> $crate::anyhow::Result<$return_type> {
use $crate::anyhow::Context;
self.select_row_bound::<($($arg_type),+), $return_type>($sql)?(($($arg),+))
.context(::std::format!(
"Error in {}, select_row_bound failed to execute or parse for: {}",
::std::stringify!($id),
::std::stringify!($sql),
$sql,
))?
.context(::std::format!(
"Error in {}, select_row_bound expected single row result but found none for: {}",
::std::stringify!($id),
::std::stringify!($sql),
$sql,
))
}
};
(async $id:ident($($arg:ident: $arg_type:ty),+) -> Result<$return_type:ty>: $sql:expr) => {
pub async fn $id(&self, $($arg: $arg_type),+) -> $crate::anyhow::Result<$return_type> {
($vis:vis fn async $id:ident($($arg:ident: $arg_type:ty),+) -> Result<$return_type:ty> { $sql:expr }) => {
$vis async fn $id(&self, $($arg: $arg_type),+) -> $crate::anyhow::Result<$return_type> {
use $crate::anyhow::Context;
self.write(|connection| {
@ -293,12 +337,12 @@ macro_rules! sql_method {
.context(::std::format!(
"Error in {}, select_row_bound failed to execute or parse for: {}",
::std::stringify!($id),
::std::stringify!($sql),
$sql,
))?
.context(::std::format!(
"Error in {}, select_row_bound expected single row result but found none for: {}",
::std::stringify!($id),
::std::stringify!($sql),
$sql,
))
}).await
}