use crate::{
row_locks::retry_cache::RowLockRetryCache,
statement::{
context::{StatementEffects, StatementExecutionInputs, StatementRuntime},
transactions::StatementTransactions,
},
};
use std::sync::Arc;
use uqa_sql::{
ast::TransactionStmt,
plan::{AggregateClassifier, ExecutablePlanOptimizer, UnifiedPlan},
SQLError, SQLParam, Statement,
};
pub fn statement_deadline<S: Clone + 'static>(
context: &BatchExecutionContext<'_, S>,
) -> Option<uqa_core::CancellationDeadline> {
let timeout = context.statements.statement_timeout()?;
Some(
context
.runtime
.cancellation
.deadline(timeout, uqa_core::CancellationReason::StatementTimeout),
)
}
pub struct CachedStatement {
pub statement: Arc<Statement>,
pub logical_plan: Arc<UnifiedPlan>,
pub optimized_plan: Option<Arc<UnifiedPlan>>,
pub parser: uqa_sql::parser::ParserMetadata,
}
impl CachedStatement {
pub(crate) fn optimized_plan_for(&self, params: &[SQLParam]) -> Option<&Arc<UnifiedPlan>> {
if params.is_empty() {
self.optimized_plan.as_ref()
} else {
None
}
}
}
pub trait StatementCache {
fn cached_sql_analysis(
&self,
_sql: &str,
) -> Option<Arc<uqa_sql::binding::statements::AnalyzedStatement>> {
None
}
fn cache_sql_analysis(
&self,
_sql: &str,
_analysis: Option<Arc<uqa_sql::binding::statements::AnalyzedStatement>>,
) {
}
fn cached_sql_statement(&self, sql: &str) -> Option<CachedStatement>;
fn cache_sql_statement(
&self,
sql: String,
statement: Arc<Statement>,
logical_plan: Arc<UnifiedPlan>,
parser: uqa_sql::parser::ParserMetadata,
);
fn cache_optimized_sql_plan(&self, sql: &str, optimized_plan: Arc<UnifiedPlan>);
}
pub trait BatchTransactions: StatementTransactions {
fn begin_simple_query_transaction(&self) -> Result<(), SQLError>;
fn promote_simple_query_transaction(&self) -> Result<(), SQLError>;
fn run_transaction_statement(&self, statement: TransactionStmt) -> Result<(), SQLError>;
fn with_active_transaction(
&self,
operation: &mut dyn FnMut() -> Result<(), SQLError>,
) -> Result<(), SQLError>;
fn prepare_explicit_statement_snapshot(&self, sets_snapshot: bool) -> Result<(), SQLError>;
fn prepare_explicit_transaction_writer(&self) -> Result<bool, SQLError>;
fn begin_implicit_statement_transaction(&self, read_only: bool) -> Result<(), SQLError>;
}
pub(in crate::statement) fn with_active_transaction<T>(
transactions: &dyn BatchTransactions,
operation: impl FnOnce() -> Result<T, SQLError>,
) -> Result<T, SQLError> {
let mut operation = Some(operation);
let mut result = None;
transactions.with_active_transaction(&mut || {
let operation = operation.take().ok_or_else(|| {
SQLError::Internal("transaction owner invoked its operation more than once".into())
})?;
result = Some(operation()?);
Ok(())
})?;
result.ok_or_else(|| {
SQLError::Internal("transaction owner did not invoke its admitted operation".into())
})
}
pub trait RowLockStatementGuard {}
pub trait BatchRowLocks {
fn begin_row_lock_statement(&self) -> Box<dyn RowLockStatementGuard + '_>;
fn statement_row_lock_cache(&self) -> Result<Arc<RowLockRetryCache>, SQLError>;
}
pub struct BatchExecutionContext<'a, S: Clone + 'static> {
pub runtime: StatementRuntime<'a>,
pub persistent_backend: bool,
pub statements: &'a dyn StatementExecutionInputs<S>,
pub cache: &'a dyn StatementCache,
pub aggregates: &'a dyn AggregateClassifier,
pub effects: &'a dyn StatementEffects,
pub planning: &'a dyn ExecutablePlanOptimizer,
pub transactions: &'a dyn BatchTransactions,
pub row_locks: &'a dyn BatchRowLocks,
}