Skip to main content

sz_orm_query/
lib.rs

1//! SZ-ORM Query Builder
2//!
3//! 查询构建、方言、类型化查询、关联查询等。
4
5use std::fmt;
6use std::ops::Deref;
7
8pub mod data_permission;
9pub mod dynamic_filter;
10pub mod dynamic_sql;
11pub mod entity_graph;
12pub mod find_with_related;
13pub mod hydration_plugin;
14pub mod join_dsl;
15pub mod json_query;
16pub mod lambda;
17pub mod query;
18pub mod queryable;
19pub mod quick_query;
20pub mod typed;
21pub mod typed_ast;
22
23pub use data_permission::*;
24pub use dynamic_filter::*;
25pub use dynamic_sql::*;
26pub use entity_graph::*;
27pub use find_with_related::*;
28pub use hydration_plugin::*;
29pub use join_dsl::*;
30pub use json_query::*;
31pub use lambda::*;
32pub use query::*;
33pub use queryable::*;
34pub use quick_query::*;
35pub use typed::*;
36pub use typed_ast::*;
37
38// ============================================================================
39// ValidationError — 查询校验错误(封装 sz-orm-sql-validator 内部类型)
40// ============================================================================
41
42/// SQL 校验错误集合。
43///
44/// `QueryBuilder::validate*` 系列方法返回此类型,而非直接暴露
45/// `sz_orm_sql_validator::SqlValidationError`,避免下游代码对内部 crate 产生依赖。
46///
47/// 通过 [`Deref`] 可像 `Vec<SqlValidationError>` 一样遍历:
48///
49/// ```ignore
50/// match builder.validate() {
51///     Ok(()) => { /* SQL 合法 */ }
52///     Err(errors) => {
53///         for err in errors.iter() {
54///             eprintln!("validation error: {}", err);
55///         }
56///     }
57/// }
58/// ```
59#[derive(Debug, Clone, PartialEq, Default)]
60pub struct ValidationError(Vec<sz_orm_sql_validator::SqlValidationError>);
61
62impl Deref for ValidationError {
63    type Target = Vec<sz_orm_sql_validator::SqlValidationError>;
64    fn deref(&self) -> &Self::Target {
65        &self.0
66    }
67}
68
69impl From<sz_orm_sql_validator::SqlValidationError> for ValidationError {
70    fn from(e: sz_orm_sql_validator::SqlValidationError) -> Self {
71        Self(vec![e])
72    }
73}
74
75impl From<Vec<sz_orm_sql_validator::SqlValidationError>> for ValidationError {
76    fn from(v: Vec<sz_orm_sql_validator::SqlValidationError>) -> Self {
77        Self(v)
78    }
79}
80
81impl fmt::Display for ValidationError {
82    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
83        if self.0.len() == 1 {
84            write!(f, "{}", self.0[0])
85        } else {
86            write!(f, "{} validation errors: ", self.0.len())?;
87            for (i, e) in self.0.iter().enumerate() {
88                if i > 0 {
89                    write!(f, "; ")?;
90                }
91                write!(f, "{}", e)?;
92            }
93            Ok(())
94        }
95    }
96}
97
98impl std::error::Error for ValidationError {}