rename sql_method to query and adjust the syntax to more closely match function definitions
This commit is contained in:
parent
1cc3e4820a
commit
359b8aaf47
7 changed files with 192 additions and 124 deletions
|
@ -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
|
||||
}
|
||||
|
|
|
@ -1,16 +1,26 @@
|
|||
use anyhow::Result;
|
||||
use indoc::indoc;
|
||||
|
||||
use sqlez::{domain::Domain, thread_safe_connection::ThreadSafeConnection};
|
||||
use std::ops::Deref;
|
||||
|
||||
lazy_static::lazy_static! {
|
||||
pub static ref KEY_VALUE_STORE: KeyValueStore =
|
||||
KeyValueStore(crate::open_file_db());
|
||||
use crate::{open_file_db, open_memory_db, query};
|
||||
|
||||
pub struct KeyValueStore(ThreadSafeConnection<KeyValueStore>);
|
||||
|
||||
impl std::ops::Deref for KeyValueStore {
|
||||
type Target = ThreadSafeConnection<KeyValueStore>;
|
||||
|
||||
fn deref(&self) -> &Self::Target {
|
||||
&self.0
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct KeyValueStore(ThreadSafeConnection<KeyValueStore>);
|
||||
lazy_static::lazy_static! {
|
||||
pub static ref KEY_VALUE_STORE: KeyValueStore = KeyValueStore(if cfg!(any(test, feature = "test-support")) {
|
||||
open_memory_db(stringify!($id))
|
||||
} else {
|
||||
open_file_db()
|
||||
});
|
||||
}
|
||||
|
||||
impl Domain for KeyValueStore {
|
||||
fn name() -> &'static str {
|
||||
|
@ -27,56 +37,52 @@ impl Domain for KeyValueStore {
|
|||
}
|
||||
}
|
||||
|
||||
impl Deref for KeyValueStore {
|
||||
type Target = ThreadSafeConnection<KeyValueStore>;
|
||||
|
||||
fn deref(&self) -> &Self::Target {
|
||||
&self.0
|
||||
}
|
||||
}
|
||||
|
||||
impl KeyValueStore {
|
||||
pub fn read_kvp(&self, key: &str) -> Result<Option<String>> {
|
||||
self.select_row_bound("SELECT value FROM kv_store WHERE key = (?)")?(key)
|
||||
query! {
|
||||
pub fn read_kvp(key: &str) -> Result<Option<String>> {
|
||||
"SELECT value FROM kv_store WHERE key = (?)"
|
||||
}
|
||||
}
|
||||
|
||||
pub fn write_kvp(&self, key: &str, value: &str) -> Result<()> {
|
||||
self.exec_bound("INSERT OR REPLACE INTO kv_store(key, value) VALUES ((?), (?))")?((
|
||||
key, value,
|
||||
))?;
|
||||
|
||||
Ok(())
|
||||
query! {
|
||||
pub async fn write_kvp(key: String, value: String) -> Result<()> {
|
||||
"INSERT OR REPLACE INTO kv_store(key, value) VALUES ((?), (?))"
|
||||
}
|
||||
}
|
||||
|
||||
pub fn delete_kvp(&self, key: &str) -> Result<()> {
|
||||
self.exec_bound("DELETE FROM kv_store WHERE key = (?)")?(key)
|
||||
query! {
|
||||
pub async fn delete_kvp(key: String) -> Result<()> {
|
||||
"DELETE FROM kv_store WHERE key = (?)"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use anyhow::Result;
|
||||
|
||||
use crate::kvp::KeyValueStore;
|
||||
|
||||
#[test]
|
||||
fn test_kvp() -> Result<()> {
|
||||
#[gpui::test]
|
||||
async fn test_kvp() {
|
||||
let db = KeyValueStore(crate::open_memory_db("test_kvp"));
|
||||
|
||||
assert_eq!(db.read_kvp("key-1").unwrap(), None);
|
||||
|
||||
db.write_kvp("key-1", "one").unwrap();
|
||||
db.write_kvp("key-1".to_string(), "one".to_string())
|
||||
.await
|
||||
.unwrap();
|
||||
assert_eq!(db.read_kvp("key-1").unwrap(), Some("one".to_string()));
|
||||
|
||||
db.write_kvp("key-1", "one-2").unwrap();
|
||||
db.write_kvp("key-1".to_string(), "one-2".to_string())
|
||||
.await
|
||||
.unwrap();
|
||||
assert_eq!(db.read_kvp("key-1").unwrap(), Some("one-2".to_string()));
|
||||
|
||||
db.write_kvp("key-2", "two").unwrap();
|
||||
db.write_kvp("key-2".to_string(), "two".to_string())
|
||||
.await
|
||||
.unwrap();
|
||||
assert_eq!(db.read_kvp("key-2").unwrap(), Some("two".to_string()));
|
||||
|
||||
db.delete_kvp("key-1").unwrap();
|
||||
db.delete_kvp("key-1".to_string()).await.unwrap();
|
||||
assert_eq!(db.read_kvp("key-1").unwrap(), None);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue