use crate::ast::expression::HamelinExpression;
use crate::ast::pipeline::HamelinPipeline;
use crate::translation::sql_query_helpers::add_filter_condition;
use crate::translation::PendingQuery;
use hamelin_lib::antlr::hamelinparser::{WhereCommandContext, WhereCommandContextAttrs};
use hamelin_lib::err::{TranslationError, TranslationErrors};
use hamelin_lib::types::BOOLEAN;
pub fn translate(
ctx: &WhereCommandContext<'static>,
pipeline: &HamelinPipeline,
previous: &PendingQuery,
) -> Result<PendingQuery, TranslationErrors> {
let expr_tree = TranslationErrors::expect(ctx, ctx.expression())?;
let expr_ctx = pipeline
.context
.default_expression_translation_context(&previous.env);
let new_where = HamelinExpression::new(expr_tree.clone(), expr_ctx);
let translation = new_where.translate()?;
if translation.typ != BOOLEAN {
return Err(
TranslationError::msg(ctx, "WHERE clause must be a boolean expression")
.with_context(
expr_tree.as_ref(),
&format!("found {}", translation.typ.to_string()),
)
.single(),
);
}
let new_query = add_filter_condition(&previous.query, translation.sql, &previous.env);
Ok(PendingQuery::new(new_query, previous.env.clone()))
}