use super::{FileInventory, Gap, LockHelperNames, Occurrence, PARSE_GAP, ROUTINE_GAP};
use crate::statements::SqlStatementFileFacts;
use no_mistakes_postgres_sql::SqlFunctionCallFact;
pub fn record_sql_statement(
row: &mut FileInventory,
statement: &SqlStatementFileFacts,
helpers: &LockHelperNames,
) {
row.native_scan_failed |= statement.parse_failed && statement.no_complete_statements;
row.parse_failure_lines
.extend(statement.parse_failure_sites().map(|(site, line)| {
if let crate::statements::SqlFactSite::ParseFailure(index) = site {
statement.parse_failures[index].error_line.unwrap_or(line)
} else {
line
}
}));
record_parse_failures(statement, &mut row.gaps);
if !statement.parse_failed {
row.sites.extend(sql_sites(helpers, statement));
}
}
fn record_parse_failures(statement: &SqlStatementFileFacts, gaps: &mut Vec<Gap>) {
gaps.extend(
statement
.parse_failure_sites()
.map(|(_, line)| (line, PARSE_GAP)),
);
gaps.extend(
statement
.lock_inventory_unanalyzed
.iter()
.map(|&line| (line, ROUTINE_GAP)),
);
}
fn sql_sites<'a>(
helpers: &'a LockHelperNames,
statement: &'a SqlStatementFileFacts,
) -> impl Iterator<Item = Occurrence> + 'a {
statement
.lock_inventory
.iter()
.map(|lock| Occurrence {
line: lock.line,
column: lock.column,
category: lock.category.clone(),
fingerprint: lock.fingerprint.clone(),
})
.chain(
statement
.function_calls
.iter()
.filter_map(move |call| sql_helper(call, helpers)),
)
}
pub fn sql_helper(call: &SqlFunctionCallFact, helpers: &LockHelperNames) -> Option<Occurrence> {
let terminal = call.name_parts.last()?;
let name = call.name_parts.join(".");
helpers.matches_sql(&name, terminal).then(|| Occurrence {
line: call.line,
column: call.column,
category: "helper".into(),
fingerprint: format!("sql:{:?}", call.name_parts),
})
}