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#valuevariable evaluationRadioLog::signal_strengthcolumn referenceOperator::id == #some_uuidcomparison:==,!=,>,>=.<,<=!Operator::is_certified || RadioLog::signal_strength < -20logical:&&,||,!(a + b) * (c - d)math operations:+,-,*,/,%(flags >> 1) & 3bitwise operations:|,&,<<,>>[1, 2, 3][0]array or map indexingalpha == ? && beta > ?prepared statement parameterscol == NULL,col != NULLnull check, it becomesIS NULL/IS NOT NULLCOUNT(*),SUM(RadioLog::signal_strength)function calls and aggregates1 as u128type castingPIidentifiersvalue != "ab%" as LIKEpattern matching, it becomesvalue NOT LIKE 'ab%', it also supportsREGEXPandGLOB(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);