Skip to main content

sql_expr

Function sql_expr 

Source
pub fn sql_expr(sql: &str) -> Expr
Expand description

Parse a raw SQL expression fragment into an Expr.

Internally wraps the string in SELECT <sql>, parses it with the full SQL parser, and extracts the first expression from the SELECT list. This is useful for embedding complex SQL fragments (window functions, subquery predicates, etc.) that would be cumbersome to build purely through the builder API.

§Examples

use polyglot_sql::builder::sql_expr;

let expr = sql_expr("COALESCE(a, b, 0)");
assert_eq!(expr.to_sql(), "COALESCE(a, b, 0)");

let cond = sql_expr("age > 18 AND status = 'active'");

§Panics

Panics if the SQL fragment cannot be parsed, or if the parser fails to extract a valid expression from the result. Invalid SQL will cause a panic with a message prefixed by "sql_expr:".