pub enum Expr {
Show 30 variants
Column {
table: Option<String>,
name: String,
},
Literal(Value),
Placeholder(usize),
Binary {
left: Box<Expr>,
op: BinaryOp,
right: Box<Expr>,
},
Unary {
op: UnaryOp,
expr: Box<Expr>,
},
Function {
name: String,
args: Vec<Expr>,
},
Case {
when_clauses: Vec<(Expr, Expr)>,
else_clause: Option<Box<Expr>>,
},
In {
expr: Box<Expr>,
values: Vec<Expr>,
negated: bool,
},
Between {
expr: Box<Expr>,
low: Box<Expr>,
high: Box<Expr>,
negated: bool,
},
IsNull {
expr: Box<Expr>,
negated: bool,
},
IsDistinctFrom {
left: Box<Expr>,
right: Box<Expr>,
negated: bool,
},
Cast {
expr: Box<Expr>,
type_name: String,
},
Like {
expr: Box<Expr>,
pattern: String,
negated: bool,
case_insensitive: bool,
},
Subquery(String),
Exists {
subquery: String,
params: Vec<Value>,
negated: bool,
},
Raw(String),
Paren(Box<Expr>),
CountStar,
Window {
function: Box<Expr>,
partition_by: Vec<Expr>,
order_by: Vec<OrderBy>,
frame: Option<WindowFrame>,
},
JsonExtract {
expr: Box<Expr>,
path: JsonPath,
},
JsonExtractText {
expr: Box<Expr>,
path: JsonPath,
},
JsonExtractPath {
expr: Box<Expr>,
path: Vec<String>,
},
JsonExtractPathText {
expr: Box<Expr>,
path: Vec<String>,
},
JsonContains {
expr: Box<Expr>,
other: Box<Expr>,
},
JsonContainedBy {
expr: Box<Expr>,
other: Box<Expr>,
},
JsonHasKey {
expr: Box<Expr>,
key: String,
},
JsonHasAnyKey {
expr: Box<Expr>,
keys: Vec<String>,
},
JsonHasAllKeys {
expr: Box<Expr>,
keys: Vec<String>,
},
JsonArrayLength {
expr: Box<Expr>,
},
JsonTypeof {
expr: Box<Expr>,
},
}Expand description
A SQL expression that can be used in WHERE, HAVING, etc.
Variants§
Column
Column reference with optional table qualifier
Literal(Value)
Literal value
Placeholder(usize)
Explicit placeholder for bound parameters
Binary
Binary operation (e.g., a = b, a > b)
Unary
Unary operation (e.g., NOT a, -a)
Function
Function call (e.g., COUNT(*), UPPER(name))
Case
CASE WHEN … THEN … ELSE … END
Fields
In
IN expression
Between
BETWEEN expression
IsNull
IS NULL / IS NOT NULL
IsDistinctFrom
IS DISTINCT FROM / IS NOT DISTINCT FROM (NULL-safe comparison)
Cast
CAST(expr AS type)
Like
LIKE / NOT LIKE pattern
Subquery(String)
Subquery (stores the SQL string)
Exists
EXISTS (subquery) / NOT EXISTS (subquery)
Used for subquery existence checks in WHERE clauses.
Fields
Raw(String)
Raw SQL fragment (escape hatch)
Paren(Box<Expr>)
Parenthesized expression
CountStar
Special aggregate: COUNT(*)
Window
Window function with OVER clause
Fields
frame: Option<WindowFrame>Frame specification (ROWS or RANGE)
JsonExtract
JSON path extraction (returns JSON value)
- PostgreSQL:
expr -> 'path'orexpr -> path_expr - MySQL:
JSON_EXTRACT(expr, '$.path') - SQLite:
json_extract(expr, '$.path')
Fields
JsonExtractText
JSON path extraction as text (returns text/string)
- PostgreSQL:
expr ->> 'path' - MySQL:
JSON_UNQUOTE(JSON_EXTRACT(expr, '$.path')) - SQLite:
json_extract(expr, '$.path')(SQLite returns text by default)
JsonExtractPath
JSON path extraction with nested path (returns JSON)
- PostgreSQL:
expr #> '{path, to, value}' - MySQL/SQLite:
JSON_EXTRACT(expr, '$.path.to.value')
JsonExtractPathText
JSON path extraction with nested path as text
- PostgreSQL:
expr #>> '{path, to, value}' - MySQL:
JSON_UNQUOTE(JSON_EXTRACT(expr, '$.path.to.value')) - SQLite:
json_extract(expr, '$.path.to.value')
JsonContains
JSON containment check (left contains right)
- PostgreSQL:
expr @> other(JSONB only) - MySQL:
JSON_CONTAINS(expr, other) - SQLite: Not directly supported (requires json_each workaround)
JsonContainedBy
JSON contained-by check (left is contained by right)
- PostgreSQL:
expr <@ other(JSONB only) - MySQL:
JSON_CONTAINS(other, expr) - SQLite: Not directly supported
JsonHasKey
JSON key existence check
- PostgreSQL:
expr ? 'key'(JSONB only) - MySQL:
JSON_CONTAINS_PATH(expr, 'one', '$.key') - SQLite:
json_type(expr, '$.key') IS NOT NULL
JsonHasAnyKey
JSON any key existence (has any of the keys)
- PostgreSQL:
expr ?| array['key1', 'key2'](JSONB only) - MySQL:
JSON_CONTAINS_PATH(expr, 'one', '$.key1', '$.key2') - SQLite: Requires OR of json_type checks
JsonHasAllKeys
JSON all keys existence (has all of the keys)
- PostgreSQL:
expr ?& array['key1', 'key2'](JSONB only) - MySQL:
JSON_CONTAINS_PATH(expr, 'all', '$.key1', '$.key2') - SQLite: Requires AND of json_type checks
JsonArrayLength
JSON array length
- PostgreSQL:
jsonb_array_length(expr) - MySQL:
JSON_LENGTH(expr) - SQLite:
json_array_length(expr)
JsonTypeof
JSON typeof (returns the type of the JSON value)
- PostgreSQL:
jsonb_typeof(expr) - MySQL:
JSON_TYPE(expr) - SQLite:
json_type(expr)
Implementations§
Source§impl Expr
impl Expr
Sourcepub fn qualified(table: impl Into<String>, column: impl Into<String>) -> Self
pub fn qualified(table: impl Into<String>, column: impl Into<String>) -> Self
Create a qualified column reference (table.column).
Sourcepub fn placeholder(index: usize) -> Self
pub fn placeholder(index: usize) -> Self
Create a placeholder for bound parameters.
Sourcepub fn is_not_null(self) -> Self
pub fn is_not_null(self) -> Self
IS NOT NULL
Sourcepub fn is_distinct_from(self, other: impl Into<Expr>) -> Self
pub fn is_distinct_from(self, other: impl Into<Expr>) -> Self
IS DISTINCT FROM (NULL-safe comparison: returns TRUE/FALSE, never NULL)
Unlike !=, this returns TRUE when comparing NULL to a non-NULL value,
and FALSE when comparing NULL to NULL.
Sourcepub fn is_not_distinct_from(self, other: impl Into<Expr>) -> Self
pub fn is_not_distinct_from(self, other: impl Into<Expr>) -> Self
IS NOT DISTINCT FROM (NULL-safe equality: returns TRUE/FALSE, never NULL)
Unlike =, this returns TRUE when comparing NULL to NULL,
and FALSE when comparing NULL to a non-NULL value.
Sourcepub fn ilike(self, pattern: impl Into<String>) -> Self
pub fn ilike(self, pattern: impl Into<String>) -> Self
ILIKE (case-insensitive) pattern match (PostgreSQL)
Sourcepub fn not_ilike(self, pattern: impl Into<String>) -> Self
pub fn not_ilike(self, pattern: impl Into<String>) -> Self
NOT ILIKE pattern match (PostgreSQL)
Sourcepub fn starts_with(self, pattern: impl AsRef<str>) -> Self
pub fn starts_with(self, pattern: impl AsRef<str>) -> Self
Sourcepub fn icontains(self, pattern: impl AsRef<str>) -> Self
pub fn icontains(self, pattern: impl AsRef<str>) -> Self
Case-insensitive contains (ILIKE ‘%pattern%’ or LOWER fallback).
Sourcepub fn istarts_with(self, pattern: impl AsRef<str>) -> Self
pub fn istarts_with(self, pattern: impl AsRef<str>) -> Self
Case-insensitive starts_with (ILIKE ‘pattern%’ or LOWER fallback).
Sourcepub fn iends_with(self, pattern: impl AsRef<str>) -> Self
pub fn iends_with(self, pattern: impl AsRef<str>) -> Self
Case-insensitive ends_with (ILIKE ‘%pattern’ or LOWER fallback).
Sourcepub fn not_in_list(self, values: Vec<impl Into<Expr>>) -> Self
pub fn not_in_list(self, values: Vec<impl Into<Expr>>) -> Self
NOT IN list of values
Sourcepub fn not_between(self, low: impl Into<Expr>, high: impl Into<Expr>) -> Self
pub fn not_between(self, low: impl Into<Expr>, high: impl Into<Expr>) -> Self
NOT BETWEEN low AND high
Sourcepub fn array_contains(self, other: impl Into<Expr>) -> Self
pub fn array_contains(self, other: impl Into<Expr>) -> Self
Array contains (@>). Tests if this array contains all elements of other.
Sourcepub fn array_contained_by(self, other: impl Into<Expr>) -> Self
pub fn array_contained_by(self, other: impl Into<Expr>) -> Self
Array contained by (<@). Tests if this array is contained by other.
Sourcepub fn array_overlap(self, other: impl Into<Expr>) -> Self
pub fn array_overlap(self, other: impl Into<Expr>) -> Self
Array overlap (&&). Tests if this array has any elements in common with other.
Sourcepub fn array_any_eq(self, value: impl Into<Expr>) -> Self
pub fn array_any_eq(self, value: impl Into<Expr>) -> Self
ANY(array) = value. Tests if any element of the array equals the value.
Generates: value = ANY(array_column)
Sourcepub fn case() -> CaseBuilder
pub fn case() -> CaseBuilder
Sourcepub fn count_star() -> Self
pub fn count_star() -> Self
COUNT(*) aggregate function.
Sourcepub fn function(name: impl Into<String>, args: Vec<Expr>) -> Self
pub fn function(name: impl Into<String>, args: Vec<Expr>) -> Self
Create a generic function call.
Sourcepub fn row_number() -> Self
pub fn row_number() -> Self
ROW_NUMBER() window function. Returns the sequential number of a row within a partition.
Sourcepub fn dense_rank() -> Self
pub fn dense_rank() -> Self
DENSE_RANK() window function. Returns the rank of the current row without gaps.
Sourcepub fn percent_rank() -> Self
pub fn percent_rank() -> Self
PERCENT_RANK() window function. Returns the relative rank of the current row.
Sourcepub fn cume_dist() -> Self
pub fn cume_dist() -> Self
CUME_DIST() window function. Returns the cumulative distribution of a value.
Sourcepub fn ntile(n: i64) -> Self
pub fn ntile(n: i64) -> Self
NTILE(n) window function. Divides rows into n groups and returns the group number.
Sourcepub fn lag(self) -> Self
pub fn lag(self) -> Self
LAG(expr) window function with default offset of 1. Returns the value of expr from the row that precedes the current row.
Sourcepub fn lag_offset(self, offset: i64) -> Self
pub fn lag_offset(self, offset: i64) -> Self
LAG(expr, offset) window function. Returns the value of expr from the row at the given offset before current row.
Sourcepub fn lag_with_default(self, offset: i64, default: impl Into<Expr>) -> Self
pub fn lag_with_default(self, offset: i64, default: impl Into<Expr>) -> Self
LAG(expr, offset, default) window function. Returns the value of expr or default if the offset row doesn’t exist.
Sourcepub fn lead(self) -> Self
pub fn lead(self) -> Self
LEAD(expr) window function with default offset of 1. Returns the value of expr from the row that follows the current row.
Sourcepub fn lead_offset(self, offset: i64) -> Self
pub fn lead_offset(self, offset: i64) -> Self
LEAD(expr, offset) window function. Returns the value of expr from the row at the given offset after current row.
Sourcepub fn lead_with_default(self, offset: i64, default: impl Into<Expr>) -> Self
pub fn lead_with_default(self, offset: i64, default: impl Into<Expr>) -> Self
LEAD(expr, offset, default) window function. Returns the value of expr or default if the offset row doesn’t exist.
Sourcepub fn first_value(self) -> Self
pub fn first_value(self) -> Self
FIRST_VALUE(expr) window function. Returns the first value within the window frame.
Sourcepub fn last_value(self) -> Self
pub fn last_value(self) -> Self
LAST_VALUE(expr) window function. Returns the last value within the window frame.
Sourcepub fn nth_value(self, n: i64) -> Self
pub fn nth_value(self, n: i64) -> Self
NTH_VALUE(expr, n) window function. Returns the nth value within the window frame.
Sourcepub fn over(self) -> WindowBuilder
pub fn over(self) -> WindowBuilder
Start building a window function with OVER clause.
§Example
// ROW_NUMBER() OVER (PARTITION BY department ORDER BY salary DESC)
Expr::row_number()
.over()
.partition_by(Expr::col("department"))
.order_by(Expr::col("salary").desc())
.build()
// SUM(amount) OVER (PARTITION BY customer_id)
Expr::col("amount").sum()
.over()
.partition_by(Expr::col("customer_id"))
.build()Sourcepub fn ifnull(expr1: impl Into<Expr>, expr2: impl Into<Expr>) -> Self
pub fn ifnull(expr1: impl Into<Expr>, expr2: impl Into<Expr>) -> Self
IFNULL/NVL function (dialect-specific): returns expr2 if expr1 is NULL.
This generates IFNULL for SQLite/MySQL or COALESCE for PostgreSQL.
Sourcepub fn substr(
self,
start: impl Into<Expr>,
length: Option<impl Into<Expr>>,
) -> Self
pub fn substr( self, start: impl Into<Expr>, length: Option<impl Into<Expr>>, ) -> Self
SUBSTR/SUBSTRING function: extracts a substring.
§Arguments
start- 1-based start positionlength- Optional length of substring
Sourcepub fn replace(self, from: impl Into<Expr>, to: impl Into<Expr>) -> Self
pub fn replace(self, from: impl Into<Expr>, to: impl Into<Expr>) -> Self
REPLACE function: replaces occurrences of a substring.
Sourcepub fn round(self, decimals: impl Into<Expr>) -> Self
pub fn round(self, decimals: impl Into<Expr>) -> Self
ROUND function: rounds to specified decimal places.
Sourcepub fn exists(subquery_sql: impl Into<String>, params: Vec<Value>) -> Self
pub fn exists(subquery_sql: impl Into<String>, params: Vec<Value>) -> Self
Create an EXISTS subquery expression.
§Arguments
subquery_sql- The SELECT subquery SQL (without outer parentheses)params- Parameters for the subquery
§Example
// EXISTS (SELECT 1 FROM orders WHERE orders.customer_id = customers.id)
Expr::exists(
"SELECT 1 FROM orders WHERE orders.customer_id = customers.id",
vec![]
)Sourcepub fn not_exists(subquery_sql: impl Into<String>, params: Vec<Value>) -> Self
pub fn not_exists(subquery_sql: impl Into<String>, params: Vec<Value>) -> Self
Create a NOT EXISTS subquery expression.
§Arguments
subquery_sql- The SELECT subquery SQL (without outer parentheses)params- Parameters for the subquery
§Example
// NOT EXISTS (SELECT 1 FROM orders WHERE orders.customer_id = customers.id)
Expr::not_exists(
"SELECT 1 FROM orders WHERE orders.customer_id = customers.id",
vec![]
)Sourcepub fn json_get_index(self, index: i64) -> Self
pub fn json_get_index(self, index: i64) -> Self
Sourcepub fn json_get_text(self, key: impl Into<String>) -> Self
pub fn json_get_text(self, key: impl Into<String>) -> Self
Sourcepub fn json_get_text_index(self, index: i64) -> Self
pub fn json_get_text_index(self, index: i64) -> Self
Sourcepub fn json_path(self, path: &[&str]) -> Self
pub fn json_path(self, path: &[&str]) -> Self
Extract a nested JSON value by path (returns JSON).
Generates dialect-specific SQL:
- PostgreSQL:
expr #> '{path, to, value}' - MySQL:
JSON_EXTRACT(expr, '$.path.to.value') - SQLite:
json_extract(expr, '$.path.to.value')
§Example
Expr::col("data").json_path(&["address", "city"])
// PostgreSQL: "data" #> '{address, city}'Sourcepub fn json_path_text(self, path: &[&str]) -> Self
pub fn json_path_text(self, path: &[&str]) -> Self
Extract a nested JSON value by path as text.
Generates dialect-specific SQL:
- PostgreSQL:
expr #>> '{path, to, value}' - MySQL:
JSON_UNQUOTE(JSON_EXTRACT(expr, '$.path.to.value')) - SQLite:
json_extract(expr, '$.path.to.value')
§Example
Expr::col("data").json_path_text(&["address", "city"])
// PostgreSQL: "data" #>> '{address, city}'Sourcepub fn json_contains(self, other: impl Into<Expr>) -> Self
pub fn json_contains(self, other: impl Into<Expr>) -> Self
Sourcepub fn json_contained_by(self, other: impl Into<Expr>) -> Self
pub fn json_contained_by(self, other: impl Into<Expr>) -> Self
Sourcepub fn json_has_key(self, key: impl Into<String>) -> Self
pub fn json_has_key(self, key: impl Into<String>) -> Self
Sourcepub fn json_has_any_key(self, keys: &[&str]) -> Self
pub fn json_has_any_key(self, keys: &[&str]) -> Self
Check if JSON object has any of the specified keys.
Generates dialect-specific SQL:
- PostgreSQL:
expr ?| array['key1', 'key2'](JSONB only) - MySQL:
JSON_CONTAINS_PATH(expr, 'one', '$.key1', '$.key2')
§Example
Expr::col("data").json_has_any_key(&["email", "phone"])
// PostgreSQL: "data" ?| array['email', 'phone']Sourcepub fn json_has_all_keys(self, keys: &[&str]) -> Self
pub fn json_has_all_keys(self, keys: &[&str]) -> Self
Check if JSON object has all of the specified keys.
Generates dialect-specific SQL:
- PostgreSQL:
expr ?& array['key1', 'key2'](JSONB only) - MySQL:
JSON_CONTAINS_PATH(expr, 'all', '$.key1', '$.key2')
§Example
Expr::col("data").json_has_all_keys(&["email", "phone"])
// PostgreSQL: "data" ?& array['email', 'phone']