use super::ScopeVisitor;
use crate::embedded::{EmbeddedSqlCall, EmbeddedSqlKind};
use crate::fx::FxHashMap;
use no_mistakes_ts_facts::syntax_helpers::{byte_offset_to_line, unwrap_ts_wrappers};
use oxc_ast::ast::{CallExpression, Expression};
mod applications;
mod collect;
mod escapes;
mod invocation;
mod option_query;
mod parameters;
mod query;
mod specialize;
mod summary;
pub(super) use collect::collect;
pub use summary::{ExecutorAccess, Invocation, Parameter, Summary};
#[derive(Clone)]
pub(super) struct Helper {
calls: Vec<summary::Query>,
}
pub(super) type LockHelpers = FxHashMap<String, Helper>;
impl<'a> ScopeVisitor<'a> {
pub(super) fn proven_lock_helper(&self, call: &CallExpression<'_>) -> Option<&Helper> {
if !self.executor_options.track_transaction_helpers {
return None;
}
let Expression::Identifier(name) = unwrap_ts_wrappers(&call.callee) else {
return None;
};
let helper = self.lock_helpers.get(name.name.as_str())?;
if !self.awaited_lock_helper_calls.contains(&call.span.start)
|| helper.calls.iter().any(|query| {
if query.option_property.is_some() {
return false;
}
call.arguments
.get(query.executor)
.and_then(|argument| argument.as_expression())
.and_then(summary::access)
!= Some(query.access)
})
|| self.lookup(name.name.as_str()).is_some()
|| call.optional
|| call
.arguments
.iter()
.any(|argument| argument.as_expression().is_none())
{
return None;
}
Some(helper)
}
pub(super) fn record_lock_helper_call(&mut self, call: &CallExpression<'a>) {
let Expression::Identifier(name) = unwrap_ts_wrappers(&call.callee) else {
return;
};
let Some(helper) = self.proven_lock_helper(call).cloned() else {
self.record_unknown_lock_helper(call, name.name.as_str());
return;
};
for query in helper.calls {
if specialize::enabled(call, &query) == Some(false) {
self.lock_helper_applications.entry(query.span).or_default();
continue;
}
let mut fact = EmbeddedSqlCall::default();
specialize::annotate(self, call, &query, &mut fact);
if specialize::enabled(call, &query).is_none() {
fact.transaction_id = None;
fact.executor_transaction_known = false;
}
fact.sql_text = Some(query.sql.clone());
fact.kind = EmbeddedSqlKind::Inline;
fact.lock_scope_analysis = true;
fact.track_share_lock_writes = self.executor_options.track_share_lock_writes;
super::transaction::annotate_execution_location(
self,
call,
&mut fact,
super::transaction::execution_scope(self),
);
fact.line = byte_offset_to_line(self.source, call.span.start as usize);
fact.callee = name.name.to_string();
fact.execution_unconditional = self.helper_execution_unconditional();
fact.execution_awaited = true;
fact.execution_loop = self.execution_loop;
fact.lock_helper = Some(Box::new((name.name.to_string(), query.line)));
fact.local_lock_helper_offset = Some(query.span.0);
fact.bound_parameters = query
.parameters
.iter()
.map(|parameter| match parameter {
Parameter::Argument(index) => call
.arguments
.get(*index)
.and_then(|argument| argument.as_expression())
.and_then(|value| super::transaction::identity(self, value)),
Parameter::Literal(value) => Some(value.clone()),
Parameter::Unknown => None,
})
.collect();
self.lock_helper_applications
.entry(query.span)
.or_default()
.push(self.calls.len());
self.calls.push(fact);
self.call_spans.push((call.span.start, call.span.end));
if self.track_order {
self.confirmed_order.push(self.next_seq);
}
self.next_seq += 1;
}
}
}
impl ScopeVisitor<'_> {
pub(super) fn lock_helper_summaries(&mut self) -> FxHashMap<String, Summary> {
#[cfg(feature = "test-instrumentation")]
{
self.lock_summary_passes += 1;
}
summary::summaries(&self.lock_helpers, self.source)
}
}