#[non_exhaustive]pub enum SqlExpr {
UnixTimestamp,
UnixTimestampAdd(i64),
Now,
Coalesce {
field: FieldRef,
fallback: SqlExprParam,
},
Greatest(Vec<ExprOperand>),
Least(Vec<ExprOperand>),
NullIf {
field: FieldRef,
value: SqlExprParam,
},
Strpos {
field: FieldRef,
needle: String,
},
Lpad {
field: FieldRef,
length: i64,
fill: String,
},
Rpad {
field: FieldRef,
length: i64,
fill: String,
},
ToChar {
field: FieldRef,
format: String,
},
}Expand description
受控服务端 SQL 标量表达式。
只能由白名单构造函数创建:渲染进 SQL 文本的是固定函数名与已校验列引用, 动态部分(偏移秒数、格式串、填充串、字面量)一律走绑定参数,调用方无法 注入任意 SQL。
Variants (Non-exhaustive)§
This enum is marked as non-exhaustive
UnixTimestamp
服务端当前 Unix 时间戳(秒)。
UnixTimestampAdd(i64)
服务端当前 Unix 时间戳加偏移秒数;偏移量以绑定参数传递。
Now
服务端当前时间:PostgreSQL now(),MySQL CURRENT_TIMESTAMP。
Coalesce
COALESCE(列, 字面量);字面量按 SqlExprParam 的类型绑定。
Greatest(Vec<ExprOperand>)
GREATEST(操作数...),至少两个操作数。
NULL 语义与方言相关:PostgreSQL 忽略 NULL 操作数(全 NULL 才返回 NULL), MySQL 只要有一个 NULL 操作数就返回 NULL。
Least(Vec<ExprOperand>)
LEAST(操作数...),至少两个操作数。
NULL 语义与方言相关:PostgreSQL 忽略 NULL 操作数(全 NULL 才返回 NULL), MySQL 只要有一个 NULL 操作数就返回 NULL。
NullIf
NULLIF(列, 字面量);字面量按 SqlExprParam 的类型绑定。
Strpos
strpos(列, 字面量)(MySQL 渲染为等价的 LOCATE(字面量, 列))。
Lpad
lpad(列, 长度, 填充串)。
Rpad
rpad(列, 长度, 填充串)。
ToChar
to_char(列, 格式串)(仅 PostgreSQL;MySQL 渲染返回
DbError::UnsupportedOperator,
因为 PG 格式串与 MySQL DATE_FORMAT 记号不等价)。
Implementations§
Source§impl SqlExpr
impl SqlExpr
Sourcepub fn unix_timestamp() -> Self
pub fn unix_timestamp() -> Self
服务端当前 Unix 时间戳(秒):MySQL UNIX_TIMESTAMP(),
PostgreSQL EXTRACT(EPOCH FROM now())。
Sourcepub fn unix_timestamp_add(seconds: i64) -> Self
pub fn unix_timestamp_add(seconds: i64) -> Self
服务端当前 Unix 时间戳加 seconds 秒;seconds 以绑定参数传递,可正可负。
Sourcepub fn coalesce(field: &FieldRef, fallback: impl Into<SqlExprParam>) -> Self
pub fn coalesce(field: &FieldRef, fallback: impl Into<SqlExprParam>) -> Self
COALESCE(field, fallback);fallback 以绑定参数传递,其绑定类型由
SqlExprParam 的 From 推断(&str / i64 / f64 / bool),可用
SqlExprParam::text 强制文本。
Sourcepub fn greatest(operands: Vec<ExprOperand>) -> Self
pub fn greatest(operands: Vec<ExprOperand>) -> Self
GREATEST(operands...);调用方必须提供至少两个操作数。
NULL 语义与方言相关:PostgreSQL 忽略 NULL,MySQL 任一 NULL 即返回 NULL。
Sourcepub fn least(operands: Vec<ExprOperand>) -> Self
pub fn least(operands: Vec<ExprOperand>) -> Self
LEAST(operands...);调用方必须提供至少两个操作数。
NULL 语义与方言相关:PostgreSQL 忽略 NULL,MySQL 任一 NULL 即返回 NULL。
Sourcepub fn nullif(field: &FieldRef, value: impl Into<SqlExprParam>) -> Self
pub fn nullif(field: &FieldRef, value: impl Into<SqlExprParam>) -> Self
NULLIF(field, value);value 以绑定参数传递,绑定类型规则同
SqlExpr::coalesce。
Sourcepub fn strpos(field: &FieldRef, needle: impl Into<String>) -> Self
pub fn strpos(field: &FieldRef, needle: impl Into<String>) -> Self
strpos(field, needle);needle 以绑定参数传递。
Sourcepub fn lpad(field: &FieldRef, length: i64, fill: impl Into<String>) -> Self
pub fn lpad(field: &FieldRef, length: i64, fill: impl Into<String>) -> Self
lpad(field, length, fill);长度与填充串以绑定参数传递。
Trait Implementations§
impl Eq for SqlExpr
impl StructuralPartialEq for SqlExpr
Auto Trait Implementations§
impl Forget for SqlExpr
impl Freeze for SqlExpr
impl Move for SqlExpr
impl RefUnwindSafe for SqlExpr
impl Send for SqlExpr
impl Sync for SqlExpr
impl Unpin for SqlExpr
impl UnsafeUnpin for SqlExpr
impl UnwindSafe for SqlExpr
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key and return true if they are equal.Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
Source§fn in_current_span(self) -> Instrumented<Self> ⓘ
fn in_current_span(self) -> Instrumented<Self> ⓘ
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more