use pgwire::api::results::{Response, Tag};
use pgwire::error::{ErrorInfo, PgWireError, PgWireResult};
use crate::control::security::identity::AuthenticatedIdentity;
use crate::control::server::shared::session::{TransactionState, lifecycle};
use super::super::core::NodeDbPgHandler;
use super::commit::PgwireTxnDp;
impl NodeDbPgHandler {
pub(in crate::control::server::pgwire::handler) fn handle_begin(
&self,
addr: &std::net::SocketAddr,
) -> PgWireResult<Vec<Response>> {
match lifecycle::run_begin(&self.sessions, addr, &self.state) {
Ok(()) => Ok(vec![Response::Execution(Tag::new("BEGIN"))]),
Err(e) => {
let message = match &e {
crate::Error::BadRequest { detail } => detail.clone(),
other => other.to_string(),
};
Err(PgWireError::UserError(Box::new(ErrorInfo::new(
"ERROR".to_owned(),
"25P02".to_owned(),
message,
))))
}
}
}
pub(in crate::control::server::pgwire::handler) async fn handle_rollback(
&self,
identity: &AuthenticatedIdentity,
addr: &std::net::SocketAddr,
) -> PgWireResult<Vec<Response>> {
let dp = PgwireTxnDp { handler: self };
lifecycle::run_rollback(&self.sessions, addr, identity, &self.state, &dp).await;
Ok(vec![Response::Execution(Tag::new("ROLLBACK"))])
}
pub(in crate::control::server::pgwire) async fn reclaim_open_txn(
&self,
addr: &std::net::SocketAddr,
) {
if self.sessions.transaction_state(addr) == TransactionState::Idle {
return;
}
let Some(identity) = self.sessions.identity(addr) else {
return;
};
let dp = PgwireTxnDp { handler: self };
lifecycle::run_rollback(&self.sessions, addr, &identity, &self.state, &dp).await;
}
}