(expr: rq::Expr, ctx: &mut Context)
| 22 | use crate::{Error, Result, Span, WithErrorInfo}; |
| 23 | |
| 24 | pub(super) fn translate_expr(expr: rq::Expr, ctx: &mut Context) -> Result<ExprOrSource> { |
| 25 | Ok(match expr.kind { |
| 26 | rq::ExprKind::ColumnRef(cid) => translate_cid(cid, ctx)?, |
| 27 | |
| 28 | // Fairly hacky — convert everything to a string, then concat it, |
| 29 | // then convert to sql_ast::Expr. We can't use the `Item::sql_ast::Expr` code above |
| 30 | // since we don't want to intersperse with spaces. |
| 31 | rq::ExprKind::SString(s_string_items) => { |
| 32 | let text = translate_sstring(s_string_items, ctx)?; |
| 33 | |
| 34 | ExprOrSource::Source(SourceExpr { |
| 35 | text, |
| 36 | binding_strength: 100, |
| 37 | window_frame: false, |
| 38 | }) |
| 39 | } |
| 40 | rq::ExprKind::Param(id) => ExprOrSource::Source(SourceExpr { |
| 41 | text: format!("${id}"), |
| 42 | binding_strength: 100, |
| 43 | window_frame: false, |
| 44 | }), |
| 45 | rq::ExprKind::Literal(l) => translate_literal(l, ctx)?.into(), |
| 46 | rq::ExprKind::Case(mut cases) => { |
| 47 | let default = cases |
| 48 | .last() |
| 49 | .filter(|last| { |
| 50 | matches!( |
| 51 | last.condition.kind, |
| 52 | rq::ExprKind::Literal(Literal::Boolean(true)) |
| 53 | ) |
| 54 | }) |
| 55 | .map(|def| translate_expr(def.value.clone(), ctx)) |
| 56 | .transpose()? |
| 57 | .map(|x| x.into_ast()); |
| 58 | |
| 59 | if default.is_some() { |
| 60 | cases.pop(); |
| 61 | } |
| 62 | |
| 63 | let else_result = default |
| 64 | .or(Some(sql_ast::Expr::Value(Value::Null.into()))) |
| 65 | .map(Box::new); |
| 66 | |
| 67 | let conditions = cases |
| 68 | .into_iter() |
| 69 | .map(|case| -> Result<_> { |
| 70 | let condition = translate_expr(case.condition, ctx)?.into_ast(); |
| 71 | let result = translate_expr(case.value, ctx)?.into_ast(); |
| 72 | Ok(sql_ast::CaseWhen { condition, result }) |
| 73 | }) |
| 74 | .try_collect()?; |
| 75 | |
| 76 | sql_ast::Expr::Case { |
| 77 | case_token: sqlparser::ast::helpers::attached_token::AttachedToken::empty(), |
| 78 | end_token: sqlparser::ast::helpers::attached_token::AttachedToken::empty(), |
| 79 | operand: None, |
| 80 | conditions, |
| 81 | else_result, |
no test coverage detected