use super::*;
type ParameterCache = SyncMutex<BTreeMap<String, Arc<Vec<PgTypeInfo>>>>;
pub(super) struct TransactionParameterCache {
initial: SyncMutex<Option<Arc<ParameterCache>>>,
local: ParameterCache,
}
impl TransactionParameterCache {
pub(super) fn new(pool: &Arc<ParameterCache>) -> Self {
Self {
initial: SyncMutex::new(Some(Arc::clone(pool))),
local: SyncMutex::new(BTreeMap::new()),
}
}
pub(super) fn begin_statement(&self) -> Option<Arc<ParameterCache>> {
self.initial.lock().take()
}
pub(super) async fn describe(
&self,
initial: Option<Arc<ParameterCache>>,
conn: &mut sqlx_postgres::PgConnection,
sql: &str,
builtin: &str,
) -> Result<Arc<Vec<PgTypeInfo>>, VmError> {
let cache = initial.as_deref().unwrap_or(&self.local);
let oids = described_param_oids(cache, conn, sql, builtin, true).await?;
if initial.is_some() {
self.local.lock().insert(sql.to_owned(), Arc::clone(&oids));
}
Ok(oids)
}
}