Translate expr into a BETWEEN statement if possible, otherwise returns the expr unchanged.
(expr: rq::Expr, ctx: &mut Context)
| 412 | |
| 413 | /// Translate expr into a BETWEEN statement if possible, otherwise returns the expr unchanged. |
| 414 | fn try_into_between(expr: rq::Expr, ctx: &mut Context) -> Result<Option<sql_ast::Expr>> { |
| 415 | match expr.kind { |
| 416 | rq::ExprKind::Operator { name, args } if name == "std.and" => { |
| 417 | let [a, b]: [_; 2] = args.try_into().unwrap(); |
| 418 | |
| 419 | match (a.kind, b.kind) { |
| 420 | ( |
| 421 | rq::ExprKind::Operator { |
| 422 | name: a_name, |
| 423 | args: a_args, |
| 424 | }, |
| 425 | rq::ExprKind::Operator { |
| 426 | name: b_name, |
| 427 | args: b_args, |
| 428 | }, |
| 429 | ) if a_name == "std.gte" && b_name == "std.lte" => { |
| 430 | let [a_l, a_r]: [_; 2] = a_args.try_into().unwrap(); |
| 431 | let [b_l, b_r]: [_; 2] = b_args.try_into().unwrap(); |
| 432 | |
| 433 | // We need for the values on each arm to be the same; e.g. x |
| 434 | // > 3 and x < 5 |
| 435 | if a_l == b_l { |
| 436 | return Ok(Some(sql_ast::Expr::Between { |
| 437 | expr: Box::new( |
| 438 | translate_operand(a_l, true, 0, Associativity::Both, ctx)? |
| 439 | .into_ast(), |
| 440 | ), |
| 441 | negated: false, |
| 442 | low: Box::new( |
| 443 | translate_operand(a_r, true, 0, Associativity::Both, ctx)? |
| 444 | .into_ast(), |
| 445 | ), |
| 446 | high: Box::new( |
| 447 | translate_operand(b_r, true, 0, Associativity::Both, ctx)? |
| 448 | .into_ast(), |
| 449 | ), |
| 450 | })); |
| 451 | } |
| 452 | } |
| 453 | _ => (), |
| 454 | } |
| 455 | } |
| 456 | _ => (), |
| 457 | } |
| 458 | Ok(None) |
| 459 | } |
| 460 | |
| 461 | fn operator_from_name(name: &str) -> Option<BinaryOperator> { |
| 462 | use BinaryOperator::*; |
no test coverage detected