expr

Macro expr 

Source
expr!() { /* proc-macro */ }
Expand description

Parse a Rust expression into a typed SQL expression tree.

The macro accepts a subset of Rust syntax with additional sentinel tokens for SQL semantics:

  • 42, 1.2, "Alpha", true, NULL, [1, 2, 3] literal values
  • #value variable evaluation
  • RadioLog::signal_strength column reference
  • Operator::id == #some_uuid comparison: ==, !=, >, >=. <, <=
  • !Operator::is_certified || RadioLog::signal_strength < -20 logical: &&, ||, !
  • (a + b) * (c - d) math operations: +, -, *, /, %
  • (flags >> 1) & 3 bitwise operations: |, &, <<, >>
  • [1, 2, 3][0] array or map indexing
  • alpha == ? && beta > ? prepared statement parameters
  • col == NULL, col != NULL null check, it becomes IS NULL / IS NOT NULL
  • COUNT(*), SUM(RadioLog::signal_strength) function calls and aggregates
  • 1 as u128 type casting
  • PI identifiers
  • value != "ab%" as LIKE pattern matching, it becomes value NOT LIKE 'ab%', it also supports REGEXP and GLOB (actual supports depends on the driver)
  • -(-PI) + 2 * (5 % (2 + 1)) == 7 && !(4 < 2) combination of the previous

Parentheses obey standard Rust precedence. Empty invocation (expr!()) yields false. Ultimately, the drivers decide if and how these expressions are translated into the specific query language.

Examples:

use tank::expr;
let condition = expr!(User::age > 18 && User::active == true);
let rust_articles = expr!(Post::title == "Rust%" as LIKE);
let first_user = expr!(CAST(User::active as i32) == 1);