use super::LiteralObjects;
use crate::embedded::{EmbeddedSqlCall, EmbeddedSqlKind, EmbeddedSqlVariant};
struct Outcome {
sql: String,
poisoned: bool,
}
pub(super) fn apply_call(objects: &LiteralObjects, call: &mut EmbeddedSqlCall) {
call.bound_value_origins
.retain(|(_, _, identity)| !objects.stable_members.poisoned(*identity));
let mut poisoned = apply_optional(
objects,
&mut call.sql_text,
&mut call.sql_source_positions,
&mut call.recovered_placeholder_positions,
);
for variant in &mut call.variants {
poisoned |= apply_variant(objects, variant);
}
if poisoned {
call.kind = EmbeddedSqlKind::Dynamic;
}
sync_closed_singleton(call);
}
fn sync_closed_singleton(call: &mut EmbeddedSqlCall) {
let [variant] = call.variants.as_slice() else {
return;
};
if call.kind != EmbeddedSqlKind::Dynamic
|| !variant.closed_domain
|| variant.variants_truncated
|| variant.sql_text.contains('\0')
|| crate::embedded::helpers::sentinel::contains(&variant.sql_text)
{
return;
}
call.sql_text = Some(variant.sql_text.clone());
call.sql_source_positions = variant.sql_source_positions.clone();
call.sql_source_offsets = variant.sql_source_offsets.clone();
call.recovered_placeholder_positions = variant.recovered_placeholder_positions.clone();
}
pub(super) fn apply_variant(objects: &LiteralObjects, variant: &mut EmbeddedSqlVariant) -> bool {
let outcome = rewrite(objects, &variant.sql_text);
if outcome.sql != variant.sql_text {
variant.sql_text = outcome.sql;
variant.sql_source_offsets.clear();
variant.sql_source_positions.clear();
variant.recovered_placeholder_positions.clear();
}
variant.variants_truncated |= outcome.poisoned;
outcome.poisoned
}
pub(super) fn apply_optional(
objects: &LiteralObjects,
sql: &mut Option<String>,
source_positions: &mut Vec<crate::embedded::EmbeddedSqlSourcePosition>,
placeholders: &mut Vec<(u32, u32)>,
) -> bool {
let Some(text) = sql.clone() else {
return false;
};
let outcome = rewrite(objects, &text);
if outcome.sql != text {
let length_changed = outcome.sql.len() != text.len();
*sql = Some(outcome.sql);
if outcome.poisoned {
source_positions.clear();
}
if outcome.poisoned || length_changed {
placeholders.clear();
}
}
outcome.poisoned
}
fn rewrite(objects: &LiteralObjects, text: &str) -> Outcome {
let literals = objects.literals.borrow();
if literals.is_empty() || !text.contains('\0') {
return Outcome {
sql: text.to_string(),
poisoned: false,
};
}
let mut out = String::new();
let mut rest = text;
let mut poisoned = false;
while let Some(start) = rest.find('\0') {
out.push_str(&rest[..start]);
let after = &rest[start + 1..];
let closed = after
.find('\0')
.map(|end| (end, &after[..end]))
.filter(|(_, id)| id.bytes().all(|byte| byte.is_ascii_digit()))
.and_then(|(end, id)| id.parse::<usize>().ok().map(|index| (end, index)))
.and_then(|(end, index)| literals.get(index).map(|splice| (end, splice)));
if let Some((end, splice)) = closed {
if objects.poisoned.contains(&splice.name) {
out.push_str(&splice.fallback);
poisoned = true;
} else {
out.push_str(&splice.literal);
}
rest = &after[end + 1..];
continue;
}
out.push('\0');
rest = after;
}
out.push_str(rest);
Outcome { sql: out, poisoned }
}
#[cfg(all(test, any(nm_shard_all, nm_shard_2)))]
mod tests;