pub enum Expr {
Show 31 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,
},
ExistsQuery {
subquery: Box<SelectQuery>,
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
ExistsQuery
EXISTS (subquery) / NOT EXISTS (subquery) built from a query builder.
Used to defer SQL generation until a specific dialect is known.
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>) -> Expr
pub fn qualified(table: impl Into<String>, column: impl Into<String>) -> Expr
Create a qualified column reference (table.column).
Sourcepub fn placeholder(index: usize) -> Expr
pub fn placeholder(index: usize) -> Expr
Create a placeholder for bound parameters.
Sourcepub fn is_not_null(self) -> Expr
pub fn is_not_null(self) -> Expr
IS NOT NULL
Sourcepub fn is_distinct_from(self, other: impl Into<Expr>) -> Expr
pub fn is_distinct_from(self, other: impl Into<Expr>) -> Expr
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>) -> Expr
pub fn is_not_distinct_from(self, other: impl Into<Expr>) -> Expr
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>) -> Expr
pub fn ilike(self, pattern: impl Into<String>) -> Expr
ILIKE (case-insensitive) pattern match (PostgreSQL)
Sourcepub fn not_ilike(self, pattern: impl Into<String>) -> Expr
pub fn not_ilike(self, pattern: impl Into<String>) -> Expr
NOT ILIKE pattern match (PostgreSQL)
Sourcepub fn starts_with(self, pattern: impl AsRef<str>) -> Expr
pub fn starts_with(self, pattern: impl AsRef<str>) -> Expr
Sourcepub fn icontains(self, pattern: impl AsRef<str>) -> Expr
pub fn icontains(self, pattern: impl AsRef<str>) -> Expr
Case-insensitive contains (ILIKE ‘%pattern%’ or LOWER fallback).
Sourcepub fn istarts_with(self, pattern: impl AsRef<str>) -> Expr
pub fn istarts_with(self, pattern: impl AsRef<str>) -> Expr
Case-insensitive starts_with (ILIKE ‘pattern%’ or LOWER fallback).
Sourcepub fn iends_with(self, pattern: impl AsRef<str>) -> Expr
pub fn iends_with(self, pattern: impl AsRef<str>) -> Expr
Case-insensitive ends_with (ILIKE ‘%pattern’ or LOWER fallback).
Sourcepub fn not_between(self, low: impl Into<Expr>, high: impl Into<Expr>) -> Expr
pub fn not_between(self, low: impl Into<Expr>, high: impl Into<Expr>) -> Expr
NOT BETWEEN low AND high
Sourcepub fn array_contains(self, other: impl Into<Expr>) -> Expr
pub fn array_contains(self, other: impl Into<Expr>) -> Expr
Array contains (@>). Tests if this array contains all elements of other.
Sourcepub fn array_contained_by(self, other: impl Into<Expr>) -> Expr
pub fn array_contained_by(self, other: impl Into<Expr>) -> Expr
Array contained by (<@). Tests if this array is contained by other.
Sourcepub fn array_overlap(self, other: impl Into<Expr>) -> Expr
pub fn array_overlap(self, other: impl Into<Expr>) -> Expr
Array overlap (&&). Tests if this array has any elements in common with other.
Sourcepub fn array_any_eq(self, value: impl Into<Expr>) -> Expr
pub fn array_any_eq(self, value: impl Into<Expr>) -> Expr
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() -> Expr
pub fn count_star() -> Expr
COUNT(*) aggregate function.
Sourcepub fn function(name: impl Into<String>, args: Vec<Expr>) -> Expr
pub fn function(name: impl Into<String>, args: Vec<Expr>) -> Expr
Create a generic function call.
Sourcepub fn row_number() -> Expr
pub fn row_number() -> Expr
ROW_NUMBER() window function. Returns the sequential number of a row within a partition.
Sourcepub fn dense_rank() -> Expr
pub fn dense_rank() -> Expr
DENSE_RANK() window function. Returns the rank of the current row without gaps.
Sourcepub fn percent_rank() -> Expr
pub fn percent_rank() -> Expr
PERCENT_RANK() window function. Returns the relative rank of the current row.
Sourcepub fn cume_dist() -> Expr
pub fn cume_dist() -> Expr
CUME_DIST() window function. Returns the cumulative distribution of a value.
Sourcepub fn ntile(n: i64) -> Expr
pub fn ntile(n: i64) -> Expr
NTILE(n) window function. Divides rows into n groups and returns the group number.
Sourcepub fn lag(self) -> Expr
pub fn lag(self) -> Expr
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) -> Expr
pub fn lag_offset(self, offset: i64) -> Expr
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>) -> Expr
pub fn lag_with_default(self, offset: i64, default: impl Into<Expr>) -> Expr
LAG(expr, offset, default) window function. Returns the value of expr or default if the offset row doesn’t exist.
Sourcepub fn lead(self) -> Expr
pub fn lead(self) -> Expr
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) -> Expr
pub fn lead_offset(self, offset: i64) -> Expr
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>) -> Expr
pub fn lead_with_default(self, offset: i64, default: impl Into<Expr>) -> Expr
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) -> Expr
pub fn first_value(self) -> Expr
FIRST_VALUE(expr) window function. Returns the first value within the window frame.
Sourcepub fn last_value(self) -> Expr
pub fn last_value(self) -> Expr
LAST_VALUE(expr) window function. Returns the last value within the window frame.
Sourcepub fn nth_value(self, n: i64) -> Expr
pub fn nth_value(self, n: i64) -> Expr
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>) -> Expr
pub fn ifnull(expr1: impl Into<Expr>, expr2: impl Into<Expr>) -> Expr
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>>,
) -> Expr
pub fn substr( self, start: impl Into<Expr>, length: Option<impl Into<Expr>>, ) -> Expr
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>) -> Expr
pub fn replace(self, from: impl Into<Expr>, to: impl Into<Expr>) -> Expr
REPLACE function: replaces occurrences of a substring.
Sourcepub fn round(self, decimals: impl Into<Expr>) -> Expr
pub fn round(self, decimals: impl Into<Expr>) -> Expr
ROUND function: rounds to specified decimal places.
Sourcepub fn exists(subquery_sql: impl Into<String>, params: Vec<Value>) -> Expr
pub fn exists(subquery_sql: impl Into<String>, params: Vec<Value>) -> Expr
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>) -> Expr
pub fn not_exists(subquery_sql: impl Into<String>, params: Vec<Value>) -> Expr
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 exists_query(subquery: SelectQuery) -> Expr
pub fn exists_query(subquery: SelectQuery) -> Expr
Create an EXISTS subquery expression from a query builder.
Sourcepub fn not_exists_query(subquery: SelectQuery) -> Expr
pub fn not_exists_query(subquery: SelectQuery) -> Expr
Create a NOT EXISTS subquery expression from a query builder.
Sourcepub fn json_get_index(self, index: i64) -> Expr
pub fn json_get_index(self, index: i64) -> Expr
Sourcepub fn json_get_text(self, key: impl Into<String>) -> Expr
pub fn json_get_text(self, key: impl Into<String>) -> Expr
Sourcepub fn json_get_text_index(self, index: i64) -> Expr
pub fn json_get_text_index(self, index: i64) -> Expr
Sourcepub fn json_path(self, path: &[&str]) -> Expr
pub fn json_path(self, path: &[&str]) -> Expr
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]) -> Expr
pub fn json_path_text(self, path: &[&str]) -> Expr
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>) -> Expr
pub fn json_contains(self, other: impl Into<Expr>) -> Expr
Sourcepub fn json_contained_by(self, other: impl Into<Expr>) -> Expr
pub fn json_contained_by(self, other: impl Into<Expr>) -> Expr
Sourcepub fn json_has_key(self, key: impl Into<String>) -> Expr
pub fn json_has_key(self, key: impl Into<String>) -> Expr
Sourcepub fn json_has_any_key(self, keys: &[&str]) -> Expr
pub fn json_has_any_key(self, keys: &[&str]) -> Expr
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]) -> Expr
pub fn json_has_all_keys(self, keys: &[&str]) -> Expr
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']