use super::super::SqlByteOrigins;
use super::{EmbeddedSqlCall, EmbeddedSqlKind, EmbeddedSqlSourcePosition};
use std::borrow::Cow;
pub const MAX_EMBEDDED_SQL_VARIANTS: usize = 16;
pub const MAX_FROZEN_OBJECT_SQL_VARIANTS: usize = 64;
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct EmbeddedSqlVariant {
pub sql_text: String,
pub sql_source_offsets: SqlByteOrigins,
#[doc(hidden)]
pub invocations: super::super::site_identity::Invocations,
pub line: u32,
pub sql_source_positions: Vec<EmbeddedSqlSourcePosition>,
pub recovered_placeholder_positions: Vec<(u32, u32)>,
#[doc(hidden)]
pub append_sites: Vec<u32>,
#[doc(hidden)]
pub first_append_offset: Option<usize>,
#[doc(hidden)]
pub variants_truncated: bool,
#[doc(hidden)]
pub closed_domain: bool,
#[doc(hidden)]
pub type_annotation: bool,
}
impl EmbeddedSqlVariant {
#[doc(hidden)]
pub fn push_unique(values: &mut Vec<Self>, value: Self) {
let index = values
.iter()
.position(|other| {
other.sql_text == value.sql_text
&& other.sql_source_offsets == value.sql_source_offsets
&& other.line == value.line
&& other.sql_source_positions == value.sql_source_positions
&& other.recovered_placeholder_positions
== value.recovered_placeholder_positions
})
.unwrap_or_else(|| {
values.push(value.clone());
values.len() - 1
});
values[index].first_append_offset = values[index]
.first_append_offset
.zip(value.first_append_offset)
.map(|(left, right)| left.max(right));
values[index].variants_truncated |= value.variants_truncated;
values[index].closed_domain &= value.closed_domain;
values[index].type_annotation |= value.type_annotation;
for site in value.append_sites {
if !values[index].append_sites.contains(&site) {
values[index].append_sites.push(site);
}
}
}
}
impl EmbeddedSqlCall {
#[doc(hidden)]
pub fn mirrors_single_variant(&self) -> bool {
let [variant] = self.variants.as_slice() else {
return false;
};
self.kind != EmbeddedSqlKind::Dynamic
&& !variant.variants_truncated
&& self.sql_text.as_deref() == Some(variant.sql_text.as_str())
&& self.recovered_placeholder_positions == variant.recovered_placeholder_positions
}
#[doc(hidden)]
pub fn source_offset_at_sql_offset(&self, offset: usize) -> Option<usize> {
self.variants
.first()
.map_or_else(
|| {
self.sql_source_offsets
.get(offset)
.filter(|offset| *offset != 0)
},
|variant| variant.sql_source_offsets.get(offset),
)
.map(|offset| offset as usize)
}
#[doc(hidden)]
pub fn source_offset_at_sql_position(
&self,
sql: &str,
line: usize,
column: usize,
) -> Option<usize> {
let mut current = (1, 1);
for (offset, character) in sql.char_indices() {
if current == (line, column) {
return self.source_offset_at_sql_offset(offset);
}
if character == '\n' {
current = (current.0 + 1, 1);
} else {
current.1 += 1;
}
}
None
}
#[doc(hidden)]
pub fn statement_calls(&self) -> impl Iterator<Item = Cow<'_, Self>> + Clone {
let mirrored = self.mirrors_single_variant();
std::iter::once(Cow::Borrowed(self))
.take(usize::from(self.variants.is_empty() || mirrored))
.chain(
self.variants
.iter()
.filter(move |_| !mirrored)
.map(|variant| {
Cow::Owned(Self {
lock_scope_analysis: self.lock_scope_analysis,
track_share_lock_writes: self.track_share_lock_writes,
track_redundant_prelocks: self.track_redundant_prelocks,
next_discarded_query: self.next_discarded_query,
stable_prelock_parameters: self.stable_prelock_parameters.clone(),
helper_definition: self.helper_definition,
lock_helper: self.lock_helper.clone(),
local_lock_helper_offset: self.local_lock_helper_offset,
transaction_id: self.transaction_id,
caller_transaction_id: self.caller_transaction_id,
transaction_line: self.transaction_line,
transaction_isolation: self.transaction_isolation,
executor_transaction_known: self.executor_transaction_known,
execution_unconditional: self.execution_unconditional,
execution_awaited: self.execution_awaited,
execution_loop: self.execution_loop,
execution_scope: self.execution_scope,
execution_range: self.execution_range,
execution_try: self.execution_try,
bound_parameters: self.bound_parameters.clone(),
bound_value_origins: self.bound_value_origins.clone(),
bound_value_sql: self.bound_value_sql.clone(),
line: self.line,
callee: self.callee.clone(),
sql_text: Some(variant.sql_text.clone()),
kind: if variant.variants_truncated {
EmbeddedSqlKind::Dynamic
} else {
EmbeddedSqlKind::Composed
},
declaration_line: Some(variant.line),
sql_source_positions: variant.sql_source_positions.clone(),
sql_source_offsets: SqlByteOrigins::default(),
recovered_placeholder_positions: variant
.recovered_placeholder_positions
.clone(),
variants: vec![variant.clone()],
})
}),
)
}
#[doc(hidden)]
pub fn variants_truncated(&self) -> bool {
self.variants
.iter()
.any(|variant| variant.variants_truncated)
}
#[doc(hidden)]
pub fn is_unanalyzable(&self) -> bool {
self.kind == EmbeddedSqlKind::Dynamic
&& (self.variants.is_empty() || self.variants_truncated())
}
}