Finished implementing the workspace stuff
This commit is contained in:
parent
395070cb92
commit
777f05eb76
10 changed files with 263 additions and 278 deletions
|
@ -3,10 +3,36 @@ use anyhow::Result;
|
|||
use crate::connection::Connection;
|
||||
|
||||
impl Connection {
|
||||
// Run a set of commands within the context of a `SAVEPOINT name`. If the callback
|
||||
// returns Err(_), the savepoint will be rolled back. Otherwise, the save
|
||||
// point is released.
|
||||
pub fn with_savepoint<R, F>(&mut self, name: impl AsRef<str>, f: F) -> Result<R>
|
||||
where
|
||||
F: FnOnce(&mut Connection) -> Result<R>,
|
||||
{
|
||||
let name = name.as_ref().to_owned();
|
||||
self.exec(format!("SAVEPOINT {}", &name))?;
|
||||
let result = f(self);
|
||||
match result {
|
||||
Ok(_) => {
|
||||
self.exec(format!("RELEASE {}", name))?;
|
||||
}
|
||||
Err(_) => {
|
||||
self.exec(format!("ROLLBACK TO {}", name))?;
|
||||
self.exec(format!("RELEASE {}", name))?;
|
||||
}
|
||||
}
|
||||
result
|
||||
}
|
||||
|
||||
// Run a set of commands within the context of a `SAVEPOINT name`. If the callback
|
||||
// returns Ok(None) or Err(_), the savepoint will be rolled back. Otherwise, the save
|
||||
// point is released.
|
||||
pub fn with_savepoint<F, R>(&mut self, name: impl AsRef<str>, f: F) -> Result<Option<R>>
|
||||
pub fn with_savepoint_rollback<R, F>(
|
||||
&mut self,
|
||||
name: impl AsRef<str>,
|
||||
f: F,
|
||||
) -> Result<Option<R>>
|
||||
where
|
||||
F: FnOnce(&mut Connection) -> Result<Option<R>>,
|
||||
{
|
||||
|
@ -50,15 +76,15 @@ mod tests {
|
|||
connection.with_savepoint("first", |save1| {
|
||||
save1
|
||||
.prepare("INSERT INTO text(text, idx) VALUES (?, ?)")?
|
||||
.bound((save1_text, 1))?
|
||||
.run()?;
|
||||
.bind((save1_text, 1))?
|
||||
.exec()?;
|
||||
|
||||
assert!(save1
|
||||
.with_savepoint("second", |save2| -> Result<Option<()>, anyhow::Error> {
|
||||
save2
|
||||
.prepare("INSERT INTO text(text, idx) VALUES (?, ?)")?
|
||||
.bound((save2_text, 2))?
|
||||
.run()?;
|
||||
.bind((save2_text, 2))?
|
||||
.exec()?;
|
||||
|
||||
assert_eq!(
|
||||
save2
|
||||
|
@ -79,11 +105,34 @@ mod tests {
|
|||
vec![save1_text],
|
||||
);
|
||||
|
||||
save1.with_savepoint("second", |save2| {
|
||||
save1.with_savepoint_rollback::<(), _>("second", |save2| {
|
||||
save2
|
||||
.prepare("INSERT INTO text(text, idx) VALUES (?, ?)")?
|
||||
.bound((save2_text, 2))?
|
||||
.run()?;
|
||||
.bind((save2_text, 2))?
|
||||
.exec()?;
|
||||
|
||||
assert_eq!(
|
||||
save2
|
||||
.prepare("SELECT text FROM text ORDER BY text.idx ASC")?
|
||||
.rows::<String>()?,
|
||||
vec![save1_text, save2_text],
|
||||
);
|
||||
|
||||
Ok(None)
|
||||
})?;
|
||||
|
||||
assert_eq!(
|
||||
save1
|
||||
.prepare("SELECT text FROM text ORDER BY text.idx ASC")?
|
||||
.rows::<String>()?,
|
||||
vec![save1_text],
|
||||
);
|
||||
|
||||
save1.with_savepoint_rollback("second", |save2| {
|
||||
save2
|
||||
.prepare("INSERT INTO text(text, idx) VALUES (?, ?)")?
|
||||
.bind((save2_text, 2))?
|
||||
.exec()?;
|
||||
|
||||
assert_eq!(
|
||||
save2
|
||||
|
@ -102,9 +151,16 @@ mod tests {
|
|||
vec![save1_text, save2_text],
|
||||
);
|
||||
|
||||
Ok(Some(()))
|
||||
Ok(())
|
||||
})?;
|
||||
|
||||
assert_eq!(
|
||||
connection
|
||||
.prepare("SELECT text FROM text ORDER BY text.idx ASC")?
|
||||
.rows::<String>()?,
|
||||
vec![save1_text, save2_text],
|
||||
);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue