pub struct Expr(/* private fields */);Expand description
Expression builder for creating SQL expressions.
Expr provides static methods to create expressions and instance methods
to chain operations on them.
§Example
use reinhardt_query::Expr;
// Simple column reference
let col = Expr::col("name");
// Value expression
let val = Expr::val(42);
// Build complex expressions
let expr = Expr::col("age").gte(18).and(Expr::col("active").eq(true));Implementations§
Source§impl Expr
impl Expr
Sourcepub fn col<C>(col: C) -> Selfwhere
C: IntoColumnRef,
pub fn col<C>(col: C) -> Selfwhere
C: IntoColumnRef,
Create an expression from a column reference.
§Example
use reinhardt_query::expr::Expr;
let expr = Expr::col("name");Sourcepub fn tbl<T, C>(table: T, col: C) -> Self
pub fn tbl<T, C>(table: T, col: C) -> Self
Create a table-qualified column expression.
§Example
use reinhardt_query::expr::Expr;
let expr = Expr::tbl("users", "name");Sourcepub fn val<V>(val: V) -> Selfwhere
V: IntoValue,
pub fn val<V>(val: V) -> Selfwhere
V: IntoValue,
Create a value expression.
§Example
use reinhardt_query::expr::Expr;
let expr = Expr::val(42);
let expr2 = Expr::val("hello");Sourcepub fn cust<S>(sql: S) -> Self
pub fn cust<S>(sql: S) -> Self
Create a custom SQL expression.
§Security Warning
DO NOT pass user input directly to this method. This method embeds the SQL string directly into the query without parameterization, which can lead to SQL injection vulnerabilities.
Use Expr::cust_with_values() for dynamic values instead.
§Examples
use reinhardt_query::expr::Expr;
// ✅ SAFE: Static SQL expression
let expr = Expr::cust("NOW()");
// ❌ UNSAFE: User input
// let expr = Expr::cust(&user_input);
// ✅ SAFE: Parameterized custom expression
// let expr = Expr::cust_with_values("? + ?", [user_input, other_value]);Sourcepub fn cust_with_values<S, I, V>(sql: S, values: I) -> Self
pub fn cust_with_values<S, I, V>(sql: S, values: I) -> Self
Create a custom SQL expression with value placeholders.
Use ? as placeholders for the values.
§Example
use reinhardt_query::expr::Expr;
let expr = Expr::cust_with_values("? + ?", [1, 2]);Sourcepub fn tuple<I>(exprs: I) -> Selfwhere
I: IntoIterator<Item = Self>,
pub fn tuple<I>(exprs: I) -> Selfwhere
I: IntoIterator<Item = Self>,
Create a tuple expression.
§Example
use reinhardt_query::expr::Expr;
let expr = Expr::tuple([Expr::val(1), Expr::val(2), Expr::val(3)]);Sourcepub fn asterisk() -> Self
pub fn asterisk() -> Self
Create an asterisk expression (*).
§Example
use reinhardt_query::expr::Expr;
let expr = Expr::asterisk();Sourcepub fn subquery(select: SelectStatement) -> Self
pub fn subquery(select: SelectStatement) -> Self
Sourcepub fn exists(select: SelectStatement) -> Self
pub fn exists(select: SelectStatement) -> Self
Sourcepub fn not_exists(select: SelectStatement) -> Self
pub fn not_exists(select: SelectStatement) -> Self
Sourcepub fn in_subquery(self, select: SelectStatement) -> Self
pub fn in_subquery(self, select: SelectStatement) -> Self
Sourcepub fn not_in_subquery(self, select: SelectStatement) -> Self
pub fn not_in_subquery(self, select: SelectStatement) -> Self
Sourcepub fn case() -> CaseExprBuilder
pub fn case() -> CaseExprBuilder
Create a CASE expression.
§Example
use reinhardt_query::expr::{Expr, ExprTrait};
let case = Expr::case()
.when(Expr::col("status").eq("active"), "Active")
.when(Expr::col("status").eq("pending"), "Pending")
.else_result("Unknown");Sourcepub fn value<V>(val: V) -> Selfwhere
V: IntoValue,
pub fn value<V>(val: V) -> Selfwhere
V: IntoValue,
Create a value expression (alias for Expr::val).
Sourcepub fn constant_true() -> Self
pub fn constant_true() -> Self
Create a TRUE constant expression.
Sourcepub fn constant_false() -> Self
pub fn constant_false() -> Self
Create a FALSE constant expression.
Sourcepub fn current_timestamp() -> Self
pub fn current_timestamp() -> Self
Create a CURRENT_TIMESTAMP expression.
Sourcepub fn current_date() -> Self
pub fn current_date() -> Self
Create a CURRENT_DATE expression.
Sourcepub fn current_time() -> Self
pub fn current_time() -> Self
Create a CURRENT_TIME expression.
Sourcepub fn over(self, window: WindowStatement) -> SimpleExpr
pub fn over(self, window: WindowStatement) -> SimpleExpr
Apply a window specification to this expression.
This creates a window function call with an inline window specification.
§Examples
use reinhardt_query::prelude::*;
use reinhardt_query::types::window::WindowStatement;
let window = WindowStatement {
partition_by: vec![Expr::col("department_id").into_simple_expr()],
order_by: vec![],
frame: None,
};
let expr = Expr::row_number().over(window);Sourcepub fn over_named<T: IntoIden>(self, name: T) -> SimpleExpr
pub fn over_named<T: IntoIden>(self, name: T) -> SimpleExpr
Sourcepub fn row_number() -> Self
pub fn row_number() -> Self
Create a ROW_NUMBER() window function.
Returns a sequential number for each row within a partition.
§Examples
use reinhardt_query::prelude::*;
use reinhardt_query::types::window::WindowStatement;
let window = WindowStatement {
partition_by: vec![],
order_by: vec![],
frame: None,
};
let expr = Expr::row_number().over(window);Sourcepub fn rank() -> Self
pub fn rank() -> Self
Create a RANK() window function.
Returns the rank of each row within a partition, with gaps in ranking for tied values.
§Examples
use reinhardt_query::prelude::*;
use reinhardt_query::types::window::WindowStatement;
let window = WindowStatement {
partition_by: vec![],
order_by: vec![],
frame: None,
};
let expr = Expr::rank().over(window);Sourcepub fn dense_rank() -> Self
pub fn dense_rank() -> Self
Create a DENSE_RANK() window function.
Returns the rank of each row within a partition, without gaps in ranking for tied values.
§Examples
use reinhardt_query::prelude::*;
use reinhardt_query::types::window::WindowStatement;
let window = WindowStatement {
partition_by: vec![],
order_by: vec![],
frame: None,
};
let expr = Expr::dense_rank().over(window);Sourcepub fn ntile(buckets: i64) -> Self
pub fn ntile(buckets: i64) -> Self
Create an NTILE(n) window function.
Divides the rows in a partition into buckets number of groups.
§Examples
use reinhardt_query::prelude::*;
use reinhardt_query::types::window::WindowStatement;
let window = WindowStatement {
partition_by: vec![],
order_by: vec![],
frame: None,
};
let expr = Expr::ntile(4).over(window); // Divide into quartilesSourcepub fn lead(
expr: SimpleExpr,
offset: Option<i64>,
default: Option<Value>,
) -> Self
pub fn lead( expr: SimpleExpr, offset: Option<i64>, default: Option<Value>, ) -> Self
Create a LEAD() window function.
Returns the value of the expression evaluated at the row that is offset
rows after the current row within the partition.
§Arguments
expr- The expression to evaluateoffset- Number of rows after the current row (default is 1 ifNone)default- Default value if the lead row doesn’t exist (default is NULL ifNone)
§Examples
use reinhardt_query::prelude::*;
use reinhardt_query::types::window::WindowStatement;
let window = WindowStatement {
partition_by: vec![],
order_by: vec![],
frame: None,
};
// Get next salary value
let expr = Expr::lead(Expr::col("salary").into_simple_expr(), Some(1), None).over(window);Sourcepub fn lag(
expr: SimpleExpr,
offset: Option<i64>,
default: Option<Value>,
) -> Self
pub fn lag( expr: SimpleExpr, offset: Option<i64>, default: Option<Value>, ) -> Self
Create a LAG() window function.
Returns the value of the expression evaluated at the row that is offset
rows before the current row within the partition.
§Arguments
expr- The expression to evaluateoffset- Number of rows before the current row (default is 1 ifNone)default- Default value if the lag row doesn’t exist (default is NULL ifNone)
§Examples
use reinhardt_query::prelude::*;
use reinhardt_query::types::window::WindowStatement;
let window = WindowStatement {
partition_by: vec![],
order_by: vec![],
frame: None,
};
// Get previous salary value
let expr = Expr::lag(Expr::col("salary").into_simple_expr(), Some(1), None).over(window);Sourcepub fn first_value(expr: SimpleExpr) -> Self
pub fn first_value(expr: SimpleExpr) -> Self
Create a FIRST_VALUE() window function.
Returns the first value in a window frame.
§Examples
use reinhardt_query::prelude::*;
use reinhardt_query::types::window::WindowStatement;
let window = WindowStatement {
partition_by: vec![],
order_by: vec![],
frame: None,
};
let expr = Expr::first_value(Expr::col("salary").into_simple_expr()).over(window);Sourcepub fn last_value(expr: SimpleExpr) -> Self
pub fn last_value(expr: SimpleExpr) -> Self
Create a LAST_VALUE() window function.
Returns the last value in a window frame.
§Examples
use reinhardt_query::prelude::*;
use reinhardt_query::types::window::WindowStatement;
let window = WindowStatement {
partition_by: vec![],
order_by: vec![],
frame: None,
};
let expr = Expr::last_value(Expr::col("salary").into_simple_expr()).over(window);Sourcepub fn nth_value(expr: SimpleExpr, n: i64) -> Self
pub fn nth_value(expr: SimpleExpr, n: i64) -> Self
Create an NTH_VALUE() window function.
Returns the value of the expression at the nth row of the window frame.
§Arguments
expr- The expression to evaluaten- The row number (1-based) within the frame
§Examples
use reinhardt_query::prelude::*;
use reinhardt_query::types::window::WindowStatement;
let window = WindowStatement {
partition_by: vec![],
order_by: vec![],
frame: None,
};
// Get the 3rd salary value in the frame
let expr = Expr::nth_value(Expr::col("salary").into_simple_expr(), 3).over(window);Sourcepub fn into_simple_expr(self) -> SimpleExpr
pub fn into_simple_expr(self) -> SimpleExpr
Convert this Expr into a SimpleExpr.
Sourcepub fn as_simple_expr(&self) -> &SimpleExpr
pub fn as_simple_expr(&self) -> &SimpleExpr
Get a reference to the underlying SimpleExpr.
Sourcepub fn binary<R>(self, op: BinOper, right: R) -> SimpleExprwhere
R: Into<SimpleExpr>,
pub fn binary<R>(self, op: BinOper, right: R) -> SimpleExprwhere
R: Into<SimpleExpr>,
Sourcepub fn equals<C>(self, col: C) -> SimpleExprwhere
C: IntoColumnRef,
pub fn equals<C>(self, col: C) -> SimpleExprwhere
C: IntoColumnRef,
Sourcepub fn expr_as<T: IntoIden>(self, alias: T) -> SimpleExpr
pub fn expr_as<T: IntoIden>(self, alias: T) -> SimpleExpr
Create an aliased expression (AS).
§Example
use reinhardt_query::expr::Expr;
let expr = Expr::col("name").expr_as("alias_name");Trait Implementations§
Source§impl ExprTrait for Expr
impl ExprTrait for Expr
Source§fn into_simple_expr(self) -> SimpleExpr
fn into_simple_expr(self) -> SimpleExpr
Source§fn eq<V>(self, v: V) -> SimpleExprwhere
V: Into<SimpleExpr>,
fn eq<V>(self, v: V) -> SimpleExprwhere
V: Into<SimpleExpr>,
=). Read moreSource§fn ne<V>(self, v: V) -> SimpleExprwhere
V: Into<SimpleExpr>,
fn ne<V>(self, v: V) -> SimpleExprwhere
V: Into<SimpleExpr>,
<>).Source§fn lt<V>(self, v: V) -> SimpleExprwhere
V: Into<SimpleExpr>,
fn lt<V>(self, v: V) -> SimpleExprwhere
V: Into<SimpleExpr>,
<).Source§fn lte<V>(self, v: V) -> SimpleExprwhere
V: Into<SimpleExpr>,
fn lte<V>(self, v: V) -> SimpleExprwhere
V: Into<SimpleExpr>,
<=).Source§fn gt<V>(self, v: V) -> SimpleExprwhere
V: Into<SimpleExpr>,
fn gt<V>(self, v: V) -> SimpleExprwhere
V: Into<SimpleExpr>,
>).Source§fn gte<V>(self, v: V) -> SimpleExprwhere
V: Into<SimpleExpr>,
fn gte<V>(self, v: V) -> SimpleExprwhere
V: Into<SimpleExpr>,
>=).Source§fn is_null(self) -> SimpleExpr
fn is_null(self) -> SimpleExpr
Source§fn is_not_null(self) -> SimpleExpr
fn is_not_null(self) -> SimpleExpr
Source§fn between<A, B>(self, a: A, b: B) -> SimpleExpr
fn between<A, B>(self, a: A, b: B) -> SimpleExpr
Source§fn not_between<A, B>(self, a: A, b: B) -> SimpleExpr
fn not_between<A, B>(self, a: A, b: B) -> SimpleExpr
Source§fn is_in<I, V>(self, values: I) -> SimpleExpr
fn is_in<I, V>(self, values: I) -> SimpleExpr
Source§fn is_not_in<I, V>(self, values: I) -> SimpleExpr
fn is_not_in<I, V>(self, values: I) -> SimpleExpr
Source§fn like<V>(self, pattern: V) -> SimpleExprwhere
V: Into<SimpleExpr>,
fn like<V>(self, pattern: V) -> SimpleExprwhere
V: Into<SimpleExpr>,
Source§fn not_like<V>(self, pattern: V) -> SimpleExprwhere
V: Into<SimpleExpr>,
fn not_like<V>(self, pattern: V) -> SimpleExprwhere
V: Into<SimpleExpr>,
Source§fn ilike<V>(self, pattern: V) -> SimpleExprwhere
V: Into<SimpleExpr>,
fn ilike<V>(self, pattern: V) -> SimpleExprwhere
V: Into<SimpleExpr>,
Source§fn not_ilike<V>(self, pattern: V) -> SimpleExprwhere
V: Into<SimpleExpr>,
fn not_ilike<V>(self, pattern: V) -> SimpleExprwhere
V: Into<SimpleExpr>,
Source§fn starts_with<S>(self, prefix: S) -> SimpleExpr
fn starts_with<S>(self, prefix: S) -> SimpleExpr
Source§fn ends_with<S>(self, suffix: S) -> SimpleExpr
fn ends_with<S>(self, suffix: S) -> SimpleExpr
Source§fn contains<S>(self, substring: S) -> SimpleExpr
fn contains<S>(self, substring: S) -> SimpleExpr
Source§fn and<E>(self, other: E) -> SimpleExprwhere
E: Into<SimpleExpr>,
fn and<E>(self, other: E) -> SimpleExprwhere
E: Into<SimpleExpr>,
Source§fn or<E>(self, other: E) -> SimpleExprwhere
E: Into<SimpleExpr>,
fn or<E>(self, other: E) -> SimpleExprwhere
E: Into<SimpleExpr>,
Source§fn not(self) -> SimpleExpr
fn not(self) -> SimpleExpr
Source§fn add<V>(self, v: V) -> SimpleExprwhere
V: Into<SimpleExpr>,
fn add<V>(self, v: V) -> SimpleExprwhere
V: Into<SimpleExpr>,
+).Source§fn sub<V>(self, v: V) -> SimpleExprwhere
V: Into<SimpleExpr>,
fn sub<V>(self, v: V) -> SimpleExprwhere
V: Into<SimpleExpr>,
-).Source§fn mul<V>(self, v: V) -> SimpleExprwhere
V: Into<SimpleExpr>,
fn mul<V>(self, v: V) -> SimpleExprwhere
V: Into<SimpleExpr>,
*).Source§fn div<V>(self, v: V) -> SimpleExprwhere
V: Into<SimpleExpr>,
fn div<V>(self, v: V) -> SimpleExprwhere
V: Into<SimpleExpr>,
/).Source§fn modulo<V>(self, v: V) -> SimpleExprwhere
V: Into<SimpleExpr>,
fn modulo<V>(self, v: V) -> SimpleExprwhere
V: Into<SimpleExpr>,
%).Source§fn bit_and<V>(self, v: V) -> SimpleExprwhere
V: Into<SimpleExpr>,
fn bit_and<V>(self, v: V) -> SimpleExprwhere
V: Into<SimpleExpr>,
&).Source§fn bit_or<V>(self, v: V) -> SimpleExprwhere
V: Into<SimpleExpr>,
fn bit_or<V>(self, v: V) -> SimpleExprwhere
V: Into<SimpleExpr>,
|).Source§fn left_shift<V>(self, v: V) -> SimpleExprwhere
V: Into<SimpleExpr>,
fn left_shift<V>(self, v: V) -> SimpleExprwhere
V: Into<SimpleExpr>,
<<).Source§fn right_shift<V>(self, v: V) -> SimpleExprwhere
V: Into<SimpleExpr>,
fn right_shift<V>(self, v: V) -> SimpleExprwhere
V: Into<SimpleExpr>,
>>).