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
|
@ -297,9 +297,16 @@ impl AutoUpdater {
|
||||||
) -> Task<Result<()>> {
|
) -> Task<Result<()>> {
|
||||||
cx.background().spawn(async move {
|
cx.background().spawn(async move {
|
||||||
if should_show {
|
if should_show {
|
||||||
KEY_VALUE_STORE.write_kvp(SHOULD_SHOW_UPDATE_NOTIFICATION_KEY, "")?;
|
KEY_VALUE_STORE
|
||||||
|
.write_kvp(
|
||||||
|
SHOULD_SHOW_UPDATE_NOTIFICATION_KEY.to_string(),
|
||||||
|
"".to_string(),
|
||||||
|
)
|
||||||
|
.await?;
|
||||||
} else {
|
} else {
|
||||||
KEY_VALUE_STORE.delete_kvp(SHOULD_SHOW_UPDATE_NOTIFICATION_KEY)?;
|
KEY_VALUE_STORE
|
||||||
|
.delete_kvp(SHOULD_SHOW_UPDATE_NOTIFICATION_KEY.to_string())
|
||||||
|
.await?;
|
||||||
}
|
}
|
||||||
Ok(())
|
Ok(())
|
||||||
})
|
})
|
||||||
|
|
|
@ -157,7 +157,9 @@ impl Telemetry {
|
||||||
device_id
|
device_id
|
||||||
} else {
|
} else {
|
||||||
let device_id = Uuid::new_v4().to_string();
|
let device_id = Uuid::new_v4().to_string();
|
||||||
KEY_VALUE_STORE.write_kvp("device_id", &device_id)?;
|
KEY_VALUE_STORE
|
||||||
|
.write_kvp("device_id".to_string(), device_id.clone())
|
||||||
|
.await?;
|
||||||
device_id
|
device_id
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -75,45 +75,59 @@ macro_rules! connection {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[macro_export]
|
#[macro_export]
|
||||||
macro_rules! sql_method {
|
macro_rules! query {
|
||||||
($id:ident() -> Result<()>: $sql:expr) => {
|
($vis:vis fn $id:ident() -> Result<()> { $sql:expr }) => {
|
||||||
pub fn $id(&self) -> $crate::anyhow::Result<()> {
|
$vis fn $id(&self) -> $crate::anyhow::Result<()> {
|
||||||
use $crate::anyhow::Context;
|
use $crate::anyhow::Context;
|
||||||
|
|
||||||
self.exec($sql)?().context(::std::format!(
|
self.exec($sql)?().context(::std::format!(
|
||||||
"Error in {}, exec failed to execute or parse for: {}",
|
"Error in {}, exec failed to execute or parse for: {}",
|
||||||
::std::stringify!($id),
|
::std::stringify!($id),
|
||||||
::std::stringify!($sql),
|
$sql,
|
||||||
))
|
))
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
(async $id:ident() -> Result<()>: $sql:expr) => {
|
($vis:vis async fn $id:ident() -> Result<()> { $sql:expr }) => {
|
||||||
pub async fn $id(&self) -> $crate::anyhow::Result<()> {
|
$vis async fn $id(&self) -> $crate::anyhow::Result<()> {
|
||||||
use $crate::anyhow::Context;
|
use $crate::anyhow::Context;
|
||||||
|
|
||||||
self.write(|connection| {
|
self.write(|connection| {
|
||||||
connection.exec($sql)?().context(::std::format!(
|
connection.exec($sql)?().context(::std::format!(
|
||||||
"Error in {}, exec failed to execute or parse for: {}",
|
"Error in {}, exec failed to execute or parse for: {}",
|
||||||
::std::stringify!($id),
|
::std::stringify!($id),
|
||||||
::std::stringify!($sql),
|
$sql,
|
||||||
))
|
))
|
||||||
}).await
|
}).await
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
($id:ident($($arg:ident: $arg_type:ty),+) -> Result<()>: $sql:expr) => {
|
($vis:vis fn $id:ident($($arg:ident: $arg_type:ty),+) -> Result<()> { $sql:expr }) => {
|
||||||
pub fn $id(&self, $($arg: $arg_type),+) -> $crate::anyhow::Result<()> {
|
$vis fn $id(&self, $($arg: $arg_type),+) -> $crate::anyhow::Result<()> {
|
||||||
use $crate::anyhow::Context;
|
use $crate::anyhow::Context;
|
||||||
|
|
||||||
self.exec_bound::<($($arg_type),+)>($sql)?(($($arg),+))
|
self.exec_bound::<($($arg_type),+)>($sql)?(($($arg),+))
|
||||||
.context(::std::format!(
|
.context(::std::format!(
|
||||||
"Error in {}, exec_bound failed to execute or parse for: {}",
|
"Error in {}, exec_bound failed to execute or parse for: {}",
|
||||||
::std::stringify!($id),
|
::std::stringify!($id),
|
||||||
::std::stringify!($sql),
|
$sql,
|
||||||
))
|
))
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
(async $id:ident($($arg:ident: $arg_type:ty),+) -> Result<()>: $sql:expr) => {
|
($vis:vis async fn $id:ident($arg:ident: $arg_type:ty) -> Result<()> { $sql:expr }) => {
|
||||||
pub async fn $id(&self, $($arg: $arg_type),+) -> $crate::anyhow::Result<()> {
|
$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;
|
use $crate::anyhow::Context;
|
||||||
|
|
||||||
self.write(move |connection| {
|
self.write(move |connection| {
|
||||||
|
@ -121,24 +135,24 @@ macro_rules! sql_method {
|
||||||
.context(::std::format!(
|
.context(::std::format!(
|
||||||
"Error in {}, exec_bound failed to execute or parse for: {}",
|
"Error in {}, exec_bound failed to execute or parse for: {}",
|
||||||
::std::stringify!($id),
|
::std::stringify!($id),
|
||||||
::std::stringify!($sql),
|
$sql,
|
||||||
))
|
))
|
||||||
}).await
|
}).await
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
($id:ident() -> Result<Vec<$return_type:ty>>: $sql:expr) => {
|
($vis:vis fn $id:ident() -> Result<Vec<$return_type:ty>> { $sql:expr }) => {
|
||||||
pub fn $id(&self) -> $crate::anyhow::Result<Vec<$return_type>> {
|
$vis fn $id(&self) -> $crate::anyhow::Result<Vec<$return_type>> {
|
||||||
use $crate::anyhow::Context;
|
use $crate::anyhow::Context;
|
||||||
|
|
||||||
self.select::<$return_type>($sql)?(())
|
self.select::<$return_type>($sql)?(())
|
||||||
.context(::std::format!(
|
.context(::std::format!(
|
||||||
"Error in {}, select_row failed to execute or parse for: {}",
|
"Error in {}, select_row failed to execute or parse for: {}",
|
||||||
::std::stringify!($id),
|
::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>> {
|
pub async fn $id(&self) -> $crate::anyhow::Result<Vec<$return_type>> {
|
||||||
use $crate::anyhow::Context;
|
use $crate::anyhow::Context;
|
||||||
|
|
||||||
|
@ -147,25 +161,25 @@ macro_rules! sql_method {
|
||||||
.context(::std::format!(
|
.context(::std::format!(
|
||||||
"Error in {}, select_row failed to execute or parse for: {}",
|
"Error in {}, select_row failed to execute or parse for: {}",
|
||||||
::std::stringify!($id),
|
::std::stringify!($id),
|
||||||
::std::stringify!($sql),
|
$sql,
|
||||||
))
|
))
|
||||||
}).await
|
}).await
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
($id:ident($($arg:ident: $arg_type:ty),+) -> Result<Vec<$return_type:ty>>: $sql:expr) => {
|
($vis:vis fn $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 fn $id(&self, $($arg: $arg_type),+) -> $crate::anyhow::Result<Vec<$return_type>> {
|
||||||
use $crate::anyhow::Context;
|
use $crate::anyhow::Context;
|
||||||
|
|
||||||
self.select_bound::<($($arg_type),+), $return_type>($sql)?(($($arg),+))
|
self.select_bound::<($($arg_type),+), $return_type>($sql)?(($($arg),+))
|
||||||
.context(::std::format!(
|
.context(::std::format!(
|
||||||
"Error in {}, exec_bound failed to execute or parse for: {}",
|
"Error in {}, exec_bound failed to execute or parse for: {}",
|
||||||
::std::stringify!($id),
|
::std::stringify!($id),
|
||||||
::std::stringify!($sql),
|
$sql,
|
||||||
))
|
))
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
(async $id:ident($($arg:ident: $arg_type:ty),+) -> Result<Vec<$return_type:ty>>: $sql:expr) => {
|
($vis:vis async fn $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 async fn $id(&self, $($arg: $arg_type),+) -> $crate::anyhow::Result<Vec<$return_type>> {
|
||||||
use $crate::anyhow::Context;
|
use $crate::anyhow::Context;
|
||||||
|
|
||||||
self.write(|connection| {
|
self.write(|connection| {
|
||||||
|
@ -173,25 +187,25 @@ macro_rules! sql_method {
|
||||||
.context(::std::format!(
|
.context(::std::format!(
|
||||||
"Error in {}, exec_bound failed to execute or parse for: {}",
|
"Error in {}, exec_bound failed to execute or parse for: {}",
|
||||||
::std::stringify!($id),
|
::std::stringify!($id),
|
||||||
::std::stringify!($sql),
|
$sql,
|
||||||
))
|
))
|
||||||
}).await
|
}).await
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
($id:ident() -> Result<Option<$return_type:ty>>: $sql:expr) => {
|
($vis:vis fn $id:ident() -> Result<Option<$return_type:ty>> { $sql:expr }) => {
|
||||||
pub fn $id(&self) -> $crate::anyhow::Result<Option<$return_type>> {
|
$vis fn $id(&self) -> $crate::anyhow::Result<Option<$return_type>> {
|
||||||
use $crate::anyhow::Context;
|
use $crate::anyhow::Context;
|
||||||
|
|
||||||
self.select_row::<$return_type>($sql)?()
|
self.select_row::<$return_type>($sql)?()
|
||||||
.context(::std::format!(
|
.context(::std::format!(
|
||||||
"Error in {}, select_row failed to execute or parse for: {}",
|
"Error in {}, select_row failed to execute or parse for: {}",
|
||||||
::std::stringify!($id),
|
::std::stringify!($id),
|
||||||
::std::stringify!($sql),
|
$sql,
|
||||||
))
|
))
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
(async $id:ident() -> Result<Option<$return_type:ty>>: $sql:expr) => {
|
($vis:vis async fn $id:ident() -> Result<Option<$return_type:ty>> { $sql:expr }) => {
|
||||||
pub async fn $id(&self) -> $crate::anyhow::Result<Option<$return_type>> {
|
$vis async fn $id(&self) -> $crate::anyhow::Result<Option<$return_type>> {
|
||||||
use $crate::anyhow::Context;
|
use $crate::anyhow::Context;
|
||||||
|
|
||||||
self.write(|connection| {
|
self.write(|connection| {
|
||||||
|
@ -199,57 +213,70 @@ macro_rules! sql_method {
|
||||||
.context(::std::format!(
|
.context(::std::format!(
|
||||||
"Error in {}, select_row failed to execute or parse for: {}",
|
"Error in {}, select_row failed to execute or parse for: {}",
|
||||||
::std::stringify!($id),
|
::std::stringify!($id),
|
||||||
::std::stringify!($sql),
|
$sql,
|
||||||
))
|
))
|
||||||
}).await
|
}).await
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
($id:ident($($arg:ident: $arg_type:ty),+) -> Result<Option<$return_type:ty>>: $sql:expr) => {
|
($vis:vis fn $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 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;
|
use $crate::anyhow::Context;
|
||||||
|
|
||||||
self.select_row_bound::<($($arg_type),+), $return_type>($sql)?(($($arg),+))
|
self.select_row_bound::<($($arg_type),+), $return_type>($sql)?(($($arg),+))
|
||||||
.context(::std::format!(
|
.context(::std::format!(
|
||||||
"Error in {}, select_row_bound failed to execute or parse for: {}",
|
"Error in {}, select_row_bound failed to execute or parse for: {}",
|
||||||
::std::stringify!($id),
|
::std::stringify!($id),
|
||||||
::std::stringify!($sql),
|
$sql,
|
||||||
))
|
))
|
||||||
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
(async $id:ident($($arg:ident: $arg_type:ty),+) -> Result<Option<$return_type:ty>>: $sql:expr) => {
|
($vis:vis async fn $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 async fn $id(&self, $($arg: $arg_type),+) -> $crate::anyhow::Result<Option<$return_type>> {
|
||||||
use $crate::anyhow::Context;
|
use $crate::anyhow::Context;
|
||||||
|
|
||||||
self.write(|connection| {
|
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!(
|
.context(::std::format!(
|
||||||
"Error in {}, select_row_bound failed to execute or parse for: {}",
|
"Error in {}, select_row_bound failed to execute or parse for: {}",
|
||||||
::std::stringify!($id),
|
::std::stringify!($id),
|
||||||
::std::stringify!($sql),
|
$sql,
|
||||||
))
|
))
|
||||||
}).await
|
}).await
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
($id:ident() -> Result<$return_type:ty>: $sql:expr) => {
|
($vis:vis fn $id:ident() -> Result<$return_type:ty> { $sql:expr }) => {
|
||||||
pub fn $id(&self) -> $crate::anyhow::Result<$return_type> {
|
$vis fn $id(&self) -> $crate::anyhow::Result<$return_type> {
|
||||||
use $crate::anyhow::Context;
|
use $crate::anyhow::Context;
|
||||||
|
|
||||||
self.select_row::<$return_type>($sql)?()
|
self.select_row::<$return_type>(indoc! { $sql })?()
|
||||||
.context(::std::format!(
|
.context(::std::format!(
|
||||||
"Error in {}, select_row_bound failed to execute or parse for: {}",
|
"Error in {}, select_row_bound failed to execute or parse for: {}",
|
||||||
::std::stringify!($id),
|
::std::stringify!($id),
|
||||||
::std::stringify!($sql),
|
$sql,
|
||||||
))?
|
))?
|
||||||
.context(::std::format!(
|
.context(::std::format!(
|
||||||
"Error in {}, select_row_bound expected single row result but found none for: {}",
|
"Error in {}, select_row_bound expected single row result but found none for: {}",
|
||||||
::std::stringify!($id),
|
::std::stringify!($id),
|
||||||
::std::stringify!($sql),
|
$sql,
|
||||||
))
|
))
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
(async $id:ident() -> Result<$return_type:ty>: $sql:expr) => {
|
($vis:vis async fn $id:ident() -> Result<$return_type:ty> { $sql:expr }) => {
|
||||||
pub async fn $id(&self) -> $crate::anyhow::Result<$return_type> {
|
$vis async fn $id(&self) -> $crate::anyhow::Result<$return_type> {
|
||||||
use $crate::anyhow::Context;
|
use $crate::anyhow::Context;
|
||||||
|
|
||||||
self.write(|connection| {
|
self.write(|connection| {
|
||||||
|
@ -257,35 +284,52 @@ macro_rules! sql_method {
|
||||||
.context(::std::format!(
|
.context(::std::format!(
|
||||||
"Error in {}, select_row_bound failed to execute or parse for: {}",
|
"Error in {}, select_row_bound failed to execute or parse for: {}",
|
||||||
::std::stringify!($id),
|
::std::stringify!($id),
|
||||||
::std::stringify!($sql),
|
$sql,
|
||||||
))?
|
))?
|
||||||
.context(::std::format!(
|
.context(::std::format!(
|
||||||
"Error in {}, select_row_bound expected single row result but found none for: {}",
|
"Error in {}, select_row_bound expected single row result but found none for: {}",
|
||||||
::std::stringify!($id),
|
::std::stringify!($id),
|
||||||
::std::stringify!($sql),
|
$sql,
|
||||||
))
|
))
|
||||||
}).await
|
}).await
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
($id:ident($($arg:ident: $arg_type:ty),+) -> Result<$return_type:ty>: $sql:expr) => {
|
($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> {
|
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;
|
use $crate::anyhow::Context;
|
||||||
|
|
||||||
self.select_row_bound::<($($arg_type),+), $return_type>($sql)?(($($arg),+))
|
self.select_row_bound::<($($arg_type),+), $return_type>($sql)?(($($arg),+))
|
||||||
.context(::std::format!(
|
.context(::std::format!(
|
||||||
"Error in {}, select_row_bound failed to execute or parse for: {}",
|
"Error in {}, select_row_bound failed to execute or parse for: {}",
|
||||||
::std::stringify!($id),
|
::std::stringify!($id),
|
||||||
::std::stringify!($sql),
|
$sql,
|
||||||
))?
|
))?
|
||||||
.context(::std::format!(
|
.context(::std::format!(
|
||||||
"Error in {}, select_row_bound expected single row result but found none for: {}",
|
"Error in {}, select_row_bound expected single row result but found none for: {}",
|
||||||
::std::stringify!($id),
|
::std::stringify!($id),
|
||||||
::std::stringify!($sql),
|
$sql,
|
||||||
))
|
))
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
(async $id:ident($($arg:ident: $arg_type:ty),+) -> Result<$return_type:ty>: $sql:expr) => {
|
($vis:vis fn 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 async fn $id(&self, $($arg: $arg_type),+) -> $crate::anyhow::Result<$return_type> {
|
||||||
use $crate::anyhow::Context;
|
use $crate::anyhow::Context;
|
||||||
|
|
||||||
self.write(|connection| {
|
self.write(|connection| {
|
||||||
|
@ -293,12 +337,12 @@ macro_rules! sql_method {
|
||||||
.context(::std::format!(
|
.context(::std::format!(
|
||||||
"Error in {}, select_row_bound failed to execute or parse for: {}",
|
"Error in {}, select_row_bound failed to execute or parse for: {}",
|
||||||
::std::stringify!($id),
|
::std::stringify!($id),
|
||||||
::std::stringify!($sql),
|
$sql,
|
||||||
))?
|
))?
|
||||||
.context(::std::format!(
|
.context(::std::format!(
|
||||||
"Error in {}, select_row_bound expected single row result but found none for: {}",
|
"Error in {}, select_row_bound expected single row result but found none for: {}",
|
||||||
::std::stringify!($id),
|
::std::stringify!($id),
|
||||||
::std::stringify!($sql),
|
$sql,
|
||||||
))
|
))
|
||||||
}).await
|
}).await
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,16 +1,26 @@
|
||||||
use anyhow::Result;
|
|
||||||
use indoc::indoc;
|
use indoc::indoc;
|
||||||
|
|
||||||
use sqlez::{domain::Domain, thread_safe_connection::ThreadSafeConnection};
|
use sqlez::{domain::Domain, thread_safe_connection::ThreadSafeConnection};
|
||||||
use std::ops::Deref;
|
|
||||||
|
|
||||||
lazy_static::lazy_static! {
|
use crate::{open_file_db, open_memory_db, query};
|
||||||
pub static ref KEY_VALUE_STORE: KeyValueStore =
|
|
||||||
KeyValueStore(crate::open_file_db());
|
pub struct KeyValueStore(ThreadSafeConnection<KeyValueStore>);
|
||||||
|
|
||||||
|
impl std::ops::Deref for KeyValueStore {
|
||||||
|
type Target = ThreadSafeConnection<KeyValueStore>;
|
||||||
|
|
||||||
|
fn deref(&self) -> &Self::Target {
|
||||||
|
&self.0
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone)]
|
lazy_static::lazy_static! {
|
||||||
pub struct KeyValueStore(ThreadSafeConnection<KeyValueStore>);
|
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 {
|
impl Domain for KeyValueStore {
|
||||||
fn name() -> &'static str {
|
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 {
|
impl KeyValueStore {
|
||||||
pub fn read_kvp(&self, key: &str) -> Result<Option<String>> {
|
query! {
|
||||||
self.select_row_bound("SELECT value FROM kv_store WHERE key = (?)")?(key)
|
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<()> {
|
query! {
|
||||||
self.exec_bound("INSERT OR REPLACE INTO kv_store(key, value) VALUES ((?), (?))")?((
|
pub async fn write_kvp(key: String, value: String) -> Result<()> {
|
||||||
key, value,
|
"INSERT OR REPLACE INTO kv_store(key, value) VALUES ((?), (?))"
|
||||||
))?;
|
}
|
||||||
|
|
||||||
Ok(())
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn delete_kvp(&self, key: &str) -> Result<()> {
|
query! {
|
||||||
self.exec_bound("DELETE FROM kv_store WHERE key = (?)")?(key)
|
pub async fn delete_kvp(key: String) -> Result<()> {
|
||||||
|
"DELETE FROM kv_store WHERE key = (?)"
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use anyhow::Result;
|
|
||||||
|
|
||||||
use crate::kvp::KeyValueStore;
|
use crate::kvp::KeyValueStore;
|
||||||
|
|
||||||
#[test]
|
#[gpui::test]
|
||||||
fn test_kvp() -> Result<()> {
|
async fn test_kvp() {
|
||||||
let db = KeyValueStore(crate::open_memory_db("test_kvp"));
|
let db = KeyValueStore(crate::open_memory_db("test_kvp"));
|
||||||
|
|
||||||
assert_eq!(db.read_kvp("key-1").unwrap(), None);
|
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()));
|
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()));
|
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()));
|
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);
|
assert_eq!(db.read_kvp("key-1").unwrap(), None);
|
||||||
|
|
||||||
Ok(())
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
use std::path::PathBuf;
|
use std::path::PathBuf;
|
||||||
|
|
||||||
use db::{connection, sql_method};
|
use db::{connection, query};
|
||||||
use indoc::indoc;
|
use indoc::indoc;
|
||||||
use sqlez::domain::Domain;
|
use sqlez::domain::Domain;
|
||||||
use workspace::{ItemId, Workspace, WorkspaceId};
|
use workspace::{ItemId, Workspace, WorkspaceId};
|
||||||
|
@ -31,17 +31,21 @@ impl Domain for Editor {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl EditorDb {
|
impl EditorDb {
|
||||||
sql_method! {
|
query! {
|
||||||
get_path(item_id: ItemId, workspace_id: WorkspaceId) -> Result<PathBuf>:
|
pub fn get_path(item_id: ItemId, workspace_id: WorkspaceId) -> Result<PathBuf> {
|
||||||
indoc! {"
|
indoc!{"
|
||||||
SELECT path FROM editors
|
SELECT path FROM editors
|
||||||
WHERE item_id = ? AND workspace_id = ?"}
|
WHERE item_id = ? AND workspace_id = ?
|
||||||
|
"}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
sql_method! {
|
query! {
|
||||||
async save_path(item_id: ItemId, workspace_id: WorkspaceId, path: PathBuf) -> Result<()>:
|
pub async fn save_path(item_id: ItemId, workspace_id: WorkspaceId, path: PathBuf) -> Result<()> {
|
||||||
indoc! {"
|
indoc!{"
|
||||||
INSERT OR REPLACE INTO editors(item_id, workspace_id, path)
|
INSERT OR REPLACE INTO editors(item_id, workspace_id, path)
|
||||||
VALUES (?, ?, ?)"}
|
VALUES (?, ?, ?)
|
||||||
|
"}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
use std::path::PathBuf;
|
use std::path::PathBuf;
|
||||||
|
|
||||||
use db::{connection, indoc, sql_method, sqlez::domain::Domain};
|
use db::{connection, indoc, query, sqlez::domain::Domain};
|
||||||
|
|
||||||
use workspace::{ItemId, Workspace, WorkspaceId};
|
use workspace::{ItemId, Workspace, WorkspaceId};
|
||||||
|
|
||||||
|
@ -28,36 +28,40 @@ impl Domain for Terminal {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl TerminalDb {
|
impl TerminalDb {
|
||||||
sql_method! {
|
query! {
|
||||||
async update_workspace_id(
|
pub async fn update_workspace_id(
|
||||||
new_id: WorkspaceId,
|
new_id: WorkspaceId,
|
||||||
old_id: WorkspaceId,
|
old_id: WorkspaceId,
|
||||||
item_id: ItemId
|
item_id: ItemId
|
||||||
) -> Result<()>:
|
) -> Result<()> {
|
||||||
indoc! {"
|
indoc!{"
|
||||||
UPDATE terminals
|
UPDATE terminals
|
||||||
SET workspace_id = ?
|
SET workspace_id = ?
|
||||||
WHERE workspace_id = ? AND item_id = ?
|
WHERE workspace_id = ? AND item_id = ?
|
||||||
"}
|
"}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
sql_method! {
|
query! {
|
||||||
async save_working_directory(
|
pub async fn save_working_directory(
|
||||||
item_id: ItemId,
|
item_id: ItemId,
|
||||||
workspace_id: WorkspaceId,
|
workspace_id: WorkspaceId,
|
||||||
working_directory: PathBuf) -> Result<()>:
|
working_directory: PathBuf
|
||||||
indoc!{"
|
) -> Result<()> {
|
||||||
INSERT OR REPLACE INTO terminals(item_id, workspace_id, working_directory)
|
indoc!{"
|
||||||
VALUES (?1, ?2, ?3)
|
INSERT OR REPLACE INTO terminals(item_id, workspace_id, working_directory)
|
||||||
"}
|
VALUES (?1, ?2, ?3)
|
||||||
|
"}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
sql_method! {
|
query! {
|
||||||
get_working_directory(item_id: ItemId, workspace_id: WorkspaceId) -> Result<Option<PathBuf>>:
|
pub fn get_working_directory(item_id: ItemId, workspace_id: WorkspaceId) -> Result<Option<PathBuf>> {
|
||||||
indoc!{"
|
indoc!{"
|
||||||
SELECT working_directory
|
SELECT working_directory
|
||||||
FROM terminals
|
FROM terminals
|
||||||
WHERE item_id = ? AND workspace_id = ?
|
WHERE item_id = ? AND workspace_id = ?
|
||||||
"}
|
"}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,7 +5,7 @@ pub mod model;
|
||||||
use std::path::Path;
|
use std::path::Path;
|
||||||
|
|
||||||
use anyhow::{anyhow, bail, Context, Result};
|
use anyhow::{anyhow, bail, Context, Result};
|
||||||
use db::{connection, sql_method, sqlez::connection::Connection};
|
use db::{connection, query, sqlez::connection::Connection};
|
||||||
use gpui::Axis;
|
use gpui::Axis;
|
||||||
use indoc::indoc;
|
use indoc::indoc;
|
||||||
|
|
||||||
|
@ -201,9 +201,10 @@ impl WorkspaceDb {
|
||||||
.await;
|
.await;
|
||||||
}
|
}
|
||||||
|
|
||||||
sql_method! {
|
query! {
|
||||||
async next_id() -> Result<WorkspaceId>:
|
pub async fn next_id() -> Result<WorkspaceId> {
|
||||||
"INSERT INTO workspaces DEFAULT VALUES RETURNING workspace_id"
|
"INSERT INTO workspaces DEFAULT VALUES RETURNING workspace_id"
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns the previous workspace ids sorted by last modified along with their opened worktree roots
|
/// Returns the previous workspace ids sorted by last modified along with their opened worktree roots
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue