#![allow(deprecated)]
use chrono::{DateTime, NaiveDate, NaiveDateTime, NaiveTime, Utc};
use serde_json::Value as JsonValue;
use crate::DbError;
#[derive(Debug, Clone, Copy)]
pub enum ComparisonOperator {
Eq,
Ne,
Gt,
Lt,
Gte,
Lte,
}
impl ComparisonOperator {
fn parse(op: &str) -> Result<Self, DbError> {
match op {
"=" => Ok(Self::Eq),
"!=" => Ok(Self::Ne),
">" => Ok(Self::Gt),
"<" => Ok(Self::Lt),
">=" => Ok(Self::Gte),
"<=" => Ok(Self::Lte),
_ => Err(DbError::UnsupportedOperator(op.to_string())),
}
}
fn as_sql(self) -> &'static str {
match self {
Self::Eq => "=",
Self::Ne => "!=",
Self::Gt => ">",
Self::Lt => "<",
Self::Gte => ">=",
Self::Lte => "<=",
}
}
}
#[derive(Debug, Clone)]
pub struct Subquery {
table: String,
field: String,
conditions: Vec<Condition>,
}
impl Subquery {
pub fn new(table: &str, field: &str) -> Result<Self, DbError> {
super::identifier::quote_identifier(table)?;
super::identifier::quote_qualified(field)?;
Ok(Self {
table: table.to_string(),
field: field.to_string(),
conditions: Vec::new(),
})
}
pub fn where_value<V>(mut self, field: &str, op: &str, value: V) -> Result<Self, DbError>
where
V: Into<SqlValue>,
{
super::identifier::quote_qualified(field)?;
let value = value.into();
let condition = match op {
"=" => Condition::Eq(field.to_string(), value),
"!=" => Condition::Ne(field.to_string(), value),
">" => Condition::Gt(field.to_string(), value),
"<" => Condition::Lt(field.to_string(), value),
">=" => Condition::Gte(field.to_string(), value),
"<=" => Condition::Lte(field.to_string(), value),
_ => return Err(DbError::UnsupportedOperator(op.to_string())),
};
self.conditions.push(condition);
Ok(self)
}
pub fn where_column(mut self, left: &str, op: &str, right: &str) -> Result<Self, DbError> {
super::identifier::quote_qualified(left)?;
super::identifier::quote_qualified(right)?;
self.conditions.push(Condition::ColumnComparison(
left.to_string(),
ComparisonOperator::parse(op)?,
right.to_string(),
));
Ok(self)
}
}
#[derive(Debug, Clone)]
#[non_exhaustive]
pub enum SqlValue {
Null,
Bool(bool),
Int(i64),
Float(f64),
String(String),
Bytes(Vec<u8>),
Json(JsonValue),
DateTime(NaiveDateTime),
Timestamp(i64),
Decimal(bigdecimal::BigDecimal),
Uuid(uuid::Uuid),
DateTimeTz(DateTime<Utc>),
Date(NaiveDate),
Time(NaiveTime),
Interval(i64),
Array(Vec<SqlValue>),
TypedArray {
element_type: &'static str,
values: Vec<SqlValue>,
},
Inet(String),
Cidr(String),
MacAddr(String),
MacAddr8(String),
Range(String),
Multirange(String),
}
#[derive(Debug, Clone)]
#[non_exhaustive]
pub enum Condition {
Eq(String, SqlValue),
Ne(String, SqlValue),
Gt(String, SqlValue),
Lt(String, SqlValue),
Gte(String, SqlValue),
Lte(String, SqlValue),
In(String, Vec<SqlValue>),
NotIn(String, Vec<SqlValue>),
Between(String, SqlValue, SqlValue),
Like(String, String),
ILike(String, String),
NotILike(String, String),
RegexMatch(String, String),
RegexIMatch(String, String),
NotRegexMatch(String, String),
NotRegexIMatch(String, String),
IsDistinctFrom(String, SqlValue),
IsNotDistinctFrom(String, SqlValue),
IsNull(String),
IsNotNull(String),
ColumnComparison(String, ComparisonOperator, String),
Exists(Box<Subquery>),
NotExists(Box<Subquery>),
InSubquery(String, Box<Subquery>),
FieldExpr(String, ComparisonOperator, crate::SqlExpr),
JsonContains(String, SqlValue),
JsonContainedBy(String, SqlValue),
JsonHasKey(String, String),
Any(String, SqlValue),
All(String, SqlValue),
RangeContains(String, SqlValue),
RangeOverlaps(String, SqlValue),
JsonPathExists(String, String),
JsonPathMatch(String, String, SqlValue),
FullText(String, String),
FullTextQuery(String, String),
Not(Box<Condition>),
And(Vec<Condition>),
Or(Vec<Condition>),
}
impl From<i32> for SqlValue {
fn from(v: i32) -> Self {
SqlValue::Int(v as i64)
}
}
impl From<i64> for SqlValue {
fn from(v: i64) -> Self {
SqlValue::Int(v)
}
}
impl From<u64> for SqlValue {
fn from(v: u64) -> Self {
if v > i64::MAX as u64 {
SqlValue::String(v.to_string())
} else {
SqlValue::Int(v as i64)
}
}
}
impl From<f64> for SqlValue {
fn from(v: f64) -> Self {
SqlValue::Float(v)
}
}
impl From<f32> for SqlValue {
fn from(v: f32) -> Self {
SqlValue::Float(v as f64)
}
}
impl From<String> for SqlValue {
fn from(v: String) -> Self {
SqlValue::String(v)
}
}
impl From<&str> for SqlValue {
fn from(v: &str) -> Self {
SqlValue::String(v.to_string())
}
}
impl From<bool> for SqlValue {
fn from(v: bool) -> Self {
SqlValue::Bool(v)
}
}
impl From<Vec<u8>> for SqlValue {
fn from(v: Vec<u8>) -> Self {
SqlValue::Bytes(v)
}
}
impl From<Vec<i64>> for SqlValue {
fn from(v: Vec<i64>) -> Self {
SqlValue::Array(v.into_iter().map(SqlValue::Int).collect())
}
}
impl From<Vec<String>> for SqlValue {
fn from(v: Vec<String>) -> Self {
SqlValue::Array(v.into_iter().map(SqlValue::String).collect())
}
}
impl From<Vec<f64>> for SqlValue {
fn from(v: Vec<f64>) -> Self {
SqlValue::Array(v.into_iter().map(SqlValue::Float).collect())
}
}
impl From<Vec<bool>> for SqlValue {
fn from(v: Vec<bool>) -> Self {
SqlValue::Array(v.into_iter().map(SqlValue::Bool).collect())
}
}
impl From<JsonValue> for SqlValue {
fn from(v: JsonValue) -> Self {
SqlValue::Json(v)
}
}
impl From<NaiveDateTime> for SqlValue {
fn from(v: NaiveDateTime) -> Self {
SqlValue::DateTime(v)
}
}
impl From<bigdecimal::BigDecimal> for SqlValue {
fn from(v: bigdecimal::BigDecimal) -> Self {
SqlValue::Decimal(v)
}
}
impl From<uuid::Uuid> for SqlValue {
fn from(v: uuid::Uuid) -> Self {
SqlValue::Uuid(v)
}
}
impl From<DateTime<Utc>> for SqlValue {
fn from(v: DateTime<Utc>) -> Self {
SqlValue::DateTimeTz(v)
}
}
impl From<NaiveDate> for SqlValue {
fn from(v: NaiveDate) -> Self {
SqlValue::Date(v)
}
}
impl From<NaiveTime> for SqlValue {
fn from(v: NaiveTime) -> Self {
SqlValue::Time(v)
}
}
impl From<chrono::Duration> for SqlValue {
fn from(v: chrono::Duration) -> Self {
SqlValue::Interval(v.num_microseconds().unwrap_or_default())
}
}
impl SqlValue {
pub fn interval_micros(micros: i64) -> Self {
SqlValue::Interval(micros)
}
}
impl<T> From<Option<T>> for SqlValue
where
T: Into<SqlValue>,
{
fn from(v: Option<T>) -> Self {
match v {
Some(val) => val.into(),
None => SqlValue::Null,
}
}
}
#[deprecated(
since = "0.1.3",
note = "使用 condition_to_sql_owned_checked 获取结构化错误"
)]
pub fn condition_to_sql(condition: &Condition, params: &mut Vec<SqlValue>) -> String {
condition_to_sql_owned(condition.clone(), params)
}
const FAIL_CLOSED_CONDITION: &str = "/* invalid condition */ 1 = 0";
#[deprecated(
since = "0.1.3",
note = "使用 condition_to_sql_owned_checked 获取结构化错误"
)]
pub fn condition_to_sql_owned(condition: Condition, params: &mut Vec<SqlValue>) -> String {
condition_to_sql_owned_checked(condition, params)
.unwrap_or_else(|_| FAIL_CLOSED_CONDITION.to_string())
}
pub fn condition_to_sql_owned_checked(
condition: Condition,
params: &mut Vec<SqlValue>,
) -> Result<String, DbError> {
let rendered = render_condition_checked(condition, params.len())?;
params.extend(rendered.params);
Ok(rendered.sql)
}
pub(crate) fn render_condition_checked(
condition: Condition,
parameter_offset: usize,
) -> Result<crate::sql_types::RenderedCondition<SqlValue>, DbError> {
crate::dialect::render_condition(
crate::dialect::POSTGRES,
into_node(condition),
parameter_offset,
)
}
fn into_node(condition: Condition) -> crate::dialect::CondNode<SqlValue> {
use crate::dialect::CondNode as Node;
match condition {
Condition::Eq(field, value) => Node::Eq(field, value),
Condition::Ne(field, value) => Node::Ne(field, value),
Condition::Gt(field, value) => Node::Gt(field, value),
Condition::Lt(field, value) => Node::Lt(field, value),
Condition::Gte(field, value) => Node::Gte(field, value),
Condition::Lte(field, value) => Node::Lte(field, value),
Condition::In(field, values) => Node::In(field, values),
Condition::NotIn(field, values) => Node::NotIn(field, values),
Condition::Between(field, start, end) => Node::Between(field, start, end),
Condition::Like(field, pattern) => Node::Like(field, SqlValue::String(pattern)),
Condition::ILike(field, pattern) => {
Node::PatternMatch(field, "ILIKE", SqlValue::String(pattern))
}
Condition::NotILike(field, pattern) => {
Node::PatternMatch(field, "NOT ILIKE", SqlValue::String(pattern))
}
Condition::RegexMatch(field, pattern) => {
Node::PatternMatch(field, "~", SqlValue::String(pattern))
}
Condition::RegexIMatch(field, pattern) => {
Node::PatternMatch(field, "~*", SqlValue::String(pattern))
}
Condition::NotRegexMatch(field, pattern) => {
Node::PatternMatch(field, "!~", SqlValue::String(pattern))
}
Condition::NotRegexIMatch(field, pattern) => {
Node::PatternMatch(field, "!~*", SqlValue::String(pattern))
}
Condition::IsDistinctFrom(field, value) => Node::DistinctFrom(field, true, value),
Condition::IsNotDistinctFrom(field, value) => Node::DistinctFrom(field, false, value),
Condition::IsNull(field) => Node::IsNull(field),
Condition::IsNotNull(field) => Node::IsNotNull(field),
Condition::ColumnComparison(left, op, right) => {
Node::ColumnComparison(left, op.as_sql(), right)
}
Condition::Exists(subquery) => Node::Exists(Box::new(into_subquery_node(*subquery))),
Condition::NotExists(subquery) => Node::NotExists(Box::new(into_subquery_node(*subquery))),
Condition::InSubquery(field, subquery) => {
Node::InSubquery(field, Box::new(into_subquery_node(*subquery)))
}
Condition::FieldExpr(field, operator, expression) => {
Node::ExprComparison(field, operator.as_sql(), expression)
}
Condition::JsonContains(field, value) => Node::JsonContains(field, value),
Condition::JsonContainedBy(field, value) => Node::JsonContainedBy(field, value),
Condition::JsonHasKey(field, key) => Node::JsonHasKey(field, SqlValue::String(key)),
Condition::Any(field, value) => Node::Any(field, value),
Condition::All(field, value) => Node::All(field, value),
Condition::RangeContains(field, value) => Node::RangeContains(field, value),
Condition::RangeOverlaps(field, value) => Node::RangeOverlaps(field, value),
Condition::JsonPathExists(field, path) => {
Node::JsonPathExists(field, SqlValue::String(path))
}
Condition::JsonPathMatch(field, path, vars) => {
Node::JsonPathMatch(field, SqlValue::String(path), vars)
}
Condition::FullText(field, query) => Node::FullText(field, SqlValue::String(query)),
Condition::FullTextQuery(field, query) => {
Node::FullTextQuery(field, SqlValue::String(query))
}
Condition::Not(condition) => Node::Not(Box::new(into_node(*condition))),
Condition::And(conditions) => Node::And(conditions.into_iter().map(into_node).collect()),
Condition::Or(conditions) => Node::Or(conditions.into_iter().map(into_node).collect()),
}
}
fn into_subquery_node(subquery: Subquery) -> crate::dialect::SubqueryNode<SqlValue> {
crate::dialect::SubqueryNode {
table: subquery.table,
field: subquery.field,
conditions: subquery.conditions.into_iter().map(into_node).collect(),
}
}
#[cfg(test)]
#[allow(deprecated)]
#[allow(clippy::unwrap_used, clippy::expect_used)]
mod tests {
use super::*;
#[test]
fn test_checked_rejects_empty_in_condition() {
let mut params = Vec::new();
let result =
condition_to_sql_owned_checked(Condition::In("id".to_string(), vec![]), &mut params);
assert!(matches!(result, Err(crate::DbError::InvalidArgument(_))));
assert!(params.is_empty());
}
#[test]
fn test_checked_rejects_empty_boolean_condition() {
let mut params = Vec::new();
let result = condition_to_sql_owned_checked(Condition::And(vec![]), &mut params);
assert!(matches!(result, Err(crate::DbError::InvalidArgument(_))));
assert!(params.is_empty());
}
#[test]
fn test_checked_failure_does_not_partially_mutate_caller_params() {
let mut params = vec![SqlValue::Int(99)];
let condition = Condition::And(vec![
Condition::Eq("users.id".to_string(), SqlValue::Int(1)),
Condition::Eq("users.id --".to_string(), SqlValue::Int(2)),
]);
assert!(condition_to_sql_owned_checked(condition, &mut params).is_err());
assert_eq!(params.len(), 1);
assert!(matches!(params[0], SqlValue::Int(99)));
}
#[test]
fn test_checked_placeholder_order_starts_after_existing_params() {
let mut params = vec![SqlValue::Int(99)];
let condition = Condition::And(vec![
Condition::Eq("users.id".to_string(), SqlValue::Int(1)),
Condition::Between(
"users.score".to_string(),
SqlValue::Int(10),
SqlValue::Int(20),
),
]);
let sql = match condition_to_sql_owned_checked(condition, &mut params) {
Ok(sql) => sql,
Err(error) => panic!("合法条件不应渲染失败: {error}"),
};
assert_eq!(
sql,
"(\"users\".\"id\" = $2 AND \"users\".\"score\" BETWEEN $3 AND $4)"
);
assert_eq!(params.len(), 4);
assert!(matches!(params[0], SqlValue::Int(99)));
assert!(matches!(params[1], SqlValue::Int(1)));
assert!(matches!(params[2], SqlValue::Int(10)));
assert!(matches!(params[3], SqlValue::Int(20)));
}
#[test]
fn test_legacy_renderer_fails_closed_without_raw_payload_or_params() {
let mut params = Vec::new();
let sql = condition_to_sql_owned(
Condition::Eq("id; DROP TABLE users".to_string(), SqlValue::Int(1)),
&mut params,
);
assert_eq!(sql, FAIL_CLOSED_CONDITION);
assert!(!sql.contains("DROP TABLE"));
assert!(params.is_empty());
}
#[test]
fn test_condition_eq_uses_dollar_placeholder() {
let mut params = Vec::new();
let cond = Condition::Eq("name".to_string(), SqlValue::String("test".to_string()));
let sql = condition_to_sql(&cond, &mut params);
assert_eq!(sql, "\"name\" = $1");
assert_eq!(params.len(), 1);
}
#[test]
fn test_condition_in_numbered_sequentially() {
let mut params = Vec::new();
let cond = Condition::In(
"id".to_string(),
vec![SqlValue::Int(1), SqlValue::Int(2), SqlValue::Int(3)],
);
let sql = condition_to_sql(&cond, &mut params);
assert_eq!(sql, "\"id\" IN ($1, $2, $3)");
assert_eq!(params.len(), 3);
}
#[test]
fn test_condition_in_empty() {
let mut params = Vec::new();
let cond = Condition::In("id".to_string(), vec![]);
let sql = condition_to_sql(&cond, &mut params);
assert_eq!(sql, FAIL_CLOSED_CONDITION);
assert_eq!(params.len(), 0);
}
#[test]
fn test_condition_between() {
let mut params = Vec::new();
let cond = Condition::Between("age".to_string(), SqlValue::Int(18), SqlValue::Int(65));
let sql = condition_to_sql(&cond, &mut params);
assert_eq!(sql, "\"age\" BETWEEN $1 AND $2");
assert_eq!(params.len(), 2);
}
#[test]
fn test_placeholder_continues_from_existing_params() {
let mut params = vec![SqlValue::Int(100), SqlValue::String("x".to_string())];
let cond = Condition::Eq("id".to_string(), SqlValue::Int(1));
let sql = condition_to_sql(&cond, &mut params);
assert_eq!(sql, "\"id\" = $3");
assert_eq!(params.len(), 3);
}
#[test]
fn test_condition_and_priority_and_numbering() {
let mut params = Vec::new();
let cond = Condition::And(vec![
Condition::Or(vec![
Condition::Eq("name".to_string(), SqlValue::String("test".to_string())),
Condition::Eq("name".to_string(), SqlValue::String("demo".to_string())),
]),
Condition::Gt("age".to_string(), SqlValue::Int(18)),
]);
let sql = condition_to_sql(&cond, &mut params);
assert_eq!(sql, "((\"name\" = $1 OR \"name\" = $2) AND \"age\" > $3)");
assert_eq!(params.len(), 3);
}
#[test]
fn test_is_null_and_not_null_no_params() {
let mut params = Vec::new();
let cond = Condition::And(vec![
Condition::IsNull("a".to_string()),
Condition::IsNotNull("b".to_string()),
]);
let sql = condition_to_sql(&cond, &mut params);
assert_eq!(sql, "(\"a\" IS NULL AND \"b\" IS NOT NULL)");
assert_eq!(params.len(), 0);
}
#[test]
fn test_owned_equals_borrowed() {
let cond = Condition::And(vec![
Condition::Eq("a".to_string(), SqlValue::Int(1)),
Condition::Like("b".to_string(), "%x%".to_string()),
]);
let mut p1 = vec![];
let mut p2 = vec![];
let s1 = condition_to_sql_owned(cond.clone(), &mut p1);
let s2 = condition_to_sql(&cond, &mut p2);
assert_eq!(s1, s2);
assert_eq!(p1.len(), p2.len());
assert_eq!(format!("{:?}", p1), format!("{:?}", p2));
}
#[test]
fn test_not_renders_and_composes_with_and_or() {
let mut params = Vec::new();
let condition = Condition::And(vec![
Condition::Not(Box::new(Condition::Eq(
"status".to_string(),
SqlValue::String("deleted".to_string()),
))),
Condition::Or(vec![
Condition::Gt("age".to_string(), SqlValue::Int(18)),
Condition::Not(Box::new(Condition::IsNull("email".to_string()))),
]),
]);
let sql = condition_to_sql_owned_checked(condition, &mut params).unwrap();
assert_eq!(
sql,
"(NOT (\"status\" = $1) AND (\"age\" > $2 OR NOT (\"email\" IS NULL)))"
);
assert_eq!(params.len(), 2);
}
#[test]
fn test_field_expr_placeholder_continues_after_existing_params() {
let mut params = vec![SqlValue::Int(99)];
let condition = Condition::FieldExpr(
"updated_at".to_string(),
ComparisonOperator::Lt,
crate::SqlExpr::unix_timestamp_add(30),
);
let sql = condition_to_sql_owned_checked(condition, &mut params).unwrap();
assert_eq!(sql, "\"updated_at\" < EXTRACT(EPOCH FROM now()) + $2");
assert_eq!(params.len(), 2);
assert!(matches!(params[1], SqlValue::Int(30)));
}
#[test]
fn test_field_expr_without_param_does_not_consume_numbering() {
let mut params = Vec::new();
let condition = Condition::FieldExpr(
"updated_at".to_string(),
ComparisonOperator::Gte,
crate::SqlExpr::now(),
);
let sql = condition_to_sql_owned_checked(condition, &mut params).unwrap();
assert_eq!(sql, "\"updated_at\" >= now()");
assert!(params.is_empty());
}
#[test]
fn test_new_sql_value_from_conversions() {
use chrono::TimeZone;
let decimal = bigdecimal::BigDecimal::from(42);
assert!(matches!(
SqlValue::from(decimal),
SqlValue::Decimal(value) if value.to_string() == "42"
));
let uuid = uuid::Uuid::parse_str("00000000-0000-0000-0000-000000000001").unwrap();
assert!(matches!(SqlValue::from(uuid), SqlValue::Uuid(_)));
let date = chrono::NaiveDate::from_ymd_opt(2026, 11, 8).unwrap();
assert!(matches!(SqlValue::from(date), SqlValue::Date(_)));
let time = chrono::NaiveTime::from_hms_opt(12, 30, 0).unwrap();
assert!(matches!(SqlValue::from(time), SqlValue::Time(_)));
let tz = chrono::Utc.with_ymd_and_hms(2026, 11, 8, 12, 0, 0).unwrap();
assert!(matches!(SqlValue::from(tz), SqlValue::DateTimeTz(_)));
assert!(matches!(
SqlValue::from(chrono::Duration::seconds(2)),
SqlValue::Interval(2_000_000)
));
assert!(matches!(
SqlValue::interval_micros(-5),
SqlValue::Interval(-5)
));
}
#[test]
fn test_array_from_vec_conversions_preserve_element_types() {
assert!(matches!(
SqlValue::from(vec![1_i64, 2]),
SqlValue::Array(values)
if matches!(values.as_slice(), [SqlValue::Int(1), SqlValue::Int(2)])
));
assert!(matches!(
SqlValue::from(vec!["a".to_string(), "b".to_string()]),
SqlValue::Array(values)
if matches!(&values[0], SqlValue::String(value) if value == "a")
&& matches!(&values[1], SqlValue::String(value) if value == "b")
));
assert!(matches!(
SqlValue::from(vec![1.5_f64, 2.5]),
SqlValue::Array(values)
if matches!(&values[0], SqlValue::Float(value) if *value == 1.5)
&& matches!(&values[1], SqlValue::Float(value) if *value == 2.5)
));
assert!(matches!(
SqlValue::from(vec![true, false]),
SqlValue::Array(values)
if matches!(values.as_slice(), [SqlValue::Bool(true), SqlValue::Bool(false)])
));
assert!(matches!(
SqlValue::from(Vec::<i64>::new()),
SqlValue::Array(values) if values.is_empty()
));
}
#[test]
fn test_array_value_renders_as_one_param() {
let mut params = Vec::new();
let condition = Condition::Eq(
"tags".to_string(),
SqlValue::from(vec!["a".to_string(), "b".to_string()]),
);
let sql = condition_to_sql_owned_checked(condition, &mut params).unwrap();
assert_eq!(sql, "\"tags\" = $1");
assert_eq!(params.len(), 1, "数组必须作为单个绑定参数渲染");
assert!(matches!(¶ms[0], SqlValue::Array(values) if values.len() == 2));
}
#[test]
fn test_any_all_render_array_binding_and_numbering() {
let mut params = vec![SqlValue::Int(99)];
let condition = Condition::Any("tags".to_string(), SqlValue::from(vec!["a".to_string()]));
let sql = condition_to_sql_owned_checked(condition, &mut params).unwrap();
assert_eq!(sql, "\"tags\" = ANY($2)");
assert_eq!(params.len(), 2);
let mut params = Vec::new();
let condition = Condition::All("nums".to_string(), SqlValue::from(vec![1_i64, 2]));
let sql = condition_to_sql_owned_checked(condition, &mut params).unwrap();
assert_eq!(sql, "\"nums\" = ALL($1)");
assert_eq!(params.len(), 1);
assert!(matches!(¶ms[0], SqlValue::Array(values) if values.len() == 2));
}
#[test]
fn test_any_rejects_non_array_value_fail_closed() {
let mut params = Vec::new();
let error = condition_to_sql_owned_checked(
Condition::Any("tags".to_string(), SqlValue::String("a".to_string())),
&mut params,
)
.expect_err("ANY 只接受 SqlValue::Array");
assert!(matches!(error, DbError::InvalidArgument(_)));
assert!(params.is_empty(), "失败时不得污染调用方参数");
let mut params = Vec::new();
let error = condition_to_sql_owned_checked(
Condition::All("tags".to_string(), SqlValue::Null),
&mut params,
)
.expect_err("ALL 只接受 SqlValue::Array");
assert!(matches!(error, DbError::InvalidArgument(_)));
assert!(params.is_empty());
}
#[test]
fn test_json_predicates_render_operators_and_bind_values() {
let payload = SqlValue::Json(serde_json::json!({"role": "admin"}));
let mut params = Vec::new();
let sql = condition_to_sql_owned_checked(
Condition::JsonContains("data".to_string(), payload),
&mut params,
)
.unwrap();
assert_eq!(sql, "(\"data\")::jsonb @> $1");
assert_eq!(params.len(), 1);
assert!(matches!(¶ms[0], SqlValue::Json(_)));
let mut params = Vec::new();
let sql = condition_to_sql_owned_checked(
Condition::JsonContainedBy(
"data".to_string(),
SqlValue::Json(serde_json::json!({"role": "admin"})),
),
&mut params,
)
.unwrap();
assert_eq!(sql, "(\"data\")::jsonb <@ $1");
assert_eq!(params.len(), 1);
let mut params = Vec::new();
let sql = condition_to_sql_owned_checked(
Condition::JsonHasKey("data".to_string(), "weird'key".to_string()),
&mut params,
)
.unwrap();
assert_eq!(
sql, "(\"data\")::jsonb ? $1",
"键名必须参数化,不得拼接进 SQL 文本"
);
assert!(!sql.contains("weird'key"));
assert!(matches!(¶ms[0], SqlValue::String(key) if key == "weird'key"));
}
#[test]
fn test_json_predicate_null_renders_sql_null_without_param() {
let mut params = Vec::new();
let sql = condition_to_sql_owned_checked(
Condition::JsonContains("data".to_string(), SqlValue::Null),
&mut params,
)
.unwrap();
assert_eq!(sql, "NULL");
assert!(params.is_empty());
}
#[test]
fn test_json_predicate_rejects_non_json_value() {
let mut params = Vec::new();
let error = condition_to_sql_owned_checked(
Condition::JsonContainedBy("data".to_string(), SqlValue::Int(1)),
&mut params,
)
.expect_err("JSON 谓词只接受 SqlValue::Json");
assert!(matches!(error, DbError::InvalidArgument(_)));
assert!(params.is_empty());
}
#[test]
fn test_json_predicates_compose_and_number_sequentially() {
let mut params = Vec::new();
let condition = Condition::And(vec![
Condition::JsonContains(
"data".to_string(),
SqlValue::Json(serde_json::json!({"role": "admin"})),
),
Condition::JsonHasKey("data".to_string(), "active".to_string()),
]);
let sql = condition_to_sql_owned_checked(condition, &mut params).unwrap();
assert_eq!(sql, "((\"data\")::jsonb @> $1 AND (\"data\")::jsonb ? $2)");
assert_eq!(params.len(), 2);
assert!(matches!(¶ms[1], SqlValue::String(key) if key == "active"));
}
#[test]
fn test_range_predicates_render_operators_and_explicit_casts() {
let mut params = Vec::new();
let sql = condition_to_sql_owned_checked(
Condition::RangeContains("span".to_string(), SqlValue::Range("[1,10)".to_string())),
&mut params,
)
.unwrap();
assert_eq!(sql, "\"span\" @> $1::int4range");
assert_eq!(params.len(), 1);
assert!(matches!(¶ms[0], SqlValue::Range(text) if text == "[1,10)"));
let mut params = vec![SqlValue::Int(99)];
let sql = condition_to_sql_owned_checked(
Condition::RangeOverlaps(
"spans".to_string(),
SqlValue::Multirange("{[1,10),[20,30)}".to_string()),
),
&mut params,
)
.unwrap();
assert_eq!(sql, "\"spans\" && $2::int4multirange");
assert_eq!(params.len(), 2, "范围值必须作为单个绑定参数");
}
#[test]
fn test_range_predicates_reject_non_range_values_and_null_fails_closed() {
let mut params = Vec::new();
let error = condition_to_sql_owned_checked(
Condition::RangeContains("span".to_string(), SqlValue::String("[1,10)".to_string())),
&mut params,
)
.expect_err("范围谓词只接受 Range/Multirange");
assert!(matches!(error, DbError::InvalidArgument(_)));
assert!(params.is_empty());
let mut params = Vec::new();
let sql = condition_to_sql_owned_checked(
Condition::RangeOverlaps("span".to_string(), SqlValue::Null),
&mut params,
)
.unwrap();
assert_eq!(sql, "NULL");
assert!(params.is_empty());
}
#[test]
fn test_json_path_predicates_bind_path_and_vars() {
let mut params = Vec::new();
let sql = condition_to_sql_owned_checked(
Condition::JsonPathExists("data".to_string(), "$.role".to_string()),
&mut params,
)
.unwrap();
assert_eq!(sql, "jsonb_path_exists((\"data\")::jsonb, $1::jsonpath)");
assert!(!sql.contains("$.role"), "路径必须参数化,不得拼进 SQL");
assert!(matches!(¶ms[0], SqlValue::String(path) if path == "$.role"));
let mut params = Vec::new();
let sql = condition_to_sql_owned_checked(
Condition::JsonPathMatch(
"data".to_string(),
"$.age > $min".to_string(),
SqlValue::Json(serde_json::json!({"min": 18})),
),
&mut params,
)
.unwrap();
assert_eq!(
sql,
"jsonb_path_match((\"data\")::jsonb, $1::jsonpath, $2::jsonb)"
);
assert_eq!(params.len(), 2);
assert!(!sql.contains("min"));
}
#[test]
fn test_full_text_predicates_use_simple_config_and_bind_query() {
let mut params = Vec::new();
let sql = condition_to_sql_owned_checked(
Condition::FullText("body".to_string(), "hello world".to_string()),
&mut params,
)
.unwrap();
assert_eq!(
sql,
"to_tsvector('simple', \"body\") @@ plainto_tsquery('simple', $1)"
);
assert!(!sql.contains("hello world"));
assert!(matches!(¶ms[0], SqlValue::String(query) if query == "hello world"));
let mut params = Vec::new();
let sql = condition_to_sql_owned_checked(
Condition::FullTextQuery("body".to_string(), "hello & world".to_string()),
&mut params,
)
.unwrap();
assert_eq!(
sql,
"to_tsvector('simple', \"body\") @@ to_tsquery('simple', $1)"
);
assert_eq!(params.len(), 1);
}
#[test]
fn test_phase_c_conditions_number_sequentially_inside_and() {
let mut params = Vec::new();
let condition = Condition::And(vec![
Condition::FullText("body".to_string(), "rust".to_string()),
Condition::JsonPathExists("meta".to_string(), "$.ok".to_string()),
Condition::RangeContains("span".to_string(), SqlValue::Range("[1,5)".to_string())),
]);
let sql = condition_to_sql_owned_checked(condition, &mut params).unwrap();
assert_eq!(params.len(), 3);
assert!(sql.contains("$1"));
assert!(sql.contains("$2::jsonpath"));
assert!(sql.contains("$3::int4range"));
}
}