use crate::error::Result;
use crate::query::result::{ExecuteResult, QueryResult};
use crate::transaction::quote_identifier;
use crate::Transaction;
#[non_exhaustive]
pub struct Savepoint<'t, 'c> {
pub(crate) transaction: &'t mut Transaction<'c>,
pub(crate) name: String,
pub(crate) released: bool,
}
impl<'t, 'c> Savepoint<'t, 'c> {
#[must_use = "savepoint release errors should be checked"]
pub async fn release(mut self) -> Result<()> {
let sql = format!("RELEASE SAVEPOINT {}", quote_identifier(&self.name));
self.transaction.conn.execute(&sql).await?;
self.released = true;
self.transaction.savepoint_depth -= 1;
Ok(())
}
#[must_use = "savepoint rollback errors should be checked"]
pub async fn rollback(mut self) -> Result<()> {
let sql = format!("ROLLBACK TO SAVEPOINT {}", quote_identifier(&self.name));
self.transaction.conn.execute(&sql).await?;
self.released = true;
self.transaction.savepoint_depth -= 1;
Ok(())
}
#[must_use = "query errors should be checked"]
pub async fn query(&mut self, sql: &str) -> Result<QueryResult> {
self.transaction.query(sql).await
}
#[must_use = "execute errors should be checked"]
pub async fn execute(&mut self, sql: &str) -> Result<ExecuteResult> {
self.transaction.execute(sql).await
}
#[must_use = "query errors should be checked"]
pub async fn query_params(
&mut self,
sql: &str,
params: &[&dyn crate::types::ToSql],
) -> Result<QueryResult> {
self.transaction.query_params(sql, params).await
}
#[must_use = "execute errors should be checked"]
pub async fn execute_params(
&mut self,
sql: &str,
params: &[&dyn crate::types::ToSql],
) -> Result<ExecuteResult> {
self.transaction.execute_params(sql, params).await
}
#[must_use = "prepare errors should be checked"]
pub async fn prepare(&mut self, sql: &str) -> Result<crate::query::PreparedStatement> {
self.transaction.prepare(sql).await
}
}
impl<'t, 'c> Drop for Savepoint<'t, 'c> {
fn drop(&mut self) {
if self.released || std::thread::panicking() {
return;
}
self.transaction.savepoint_depth -= 1;
}
}