rquery_orm/
mapping.rs

1use crate::query::{PlaceholderStyle, SqlParam};
2
3pub struct ColumnMeta {
4    pub name: &'static str,
5    pub required: bool,
6    pub allow_null: bool,
7    pub max_length: Option<usize>,
8    pub min_length: Option<usize>,
9    pub allow_empty: bool,
10    pub regex: Option<&'static str>,
11    pub error_max_length: Option<&'static str>,
12    pub error_min_length: Option<&'static str>,
13    pub error_required: Option<&'static str>,
14    pub error_allow_null: Option<&'static str>,
15    pub error_allow_empty: Option<&'static str>,
16    pub error_regex: Option<&'static str>,
17    pub ignore: bool,
18    pub ignore_in_update: bool,
19    pub ignore_in_insert: bool,
20    pub ignore_in_delete: bool,
21}
22
23pub struct KeyMeta {
24    pub column: &'static str,
25    pub is_identity: bool,
26    pub ignore_in_update: bool,
27    pub ignore_in_insert: bool,
28}
29
30pub struct RelationMeta {
31    pub name: &'static str,
32    pub foreign_key: &'static str,
33    pub table: &'static str,
34    pub table_number: Option<u32>,
35    pub ignore_in_update: bool,
36    pub ignore_in_insert: bool,
37}
38
39pub struct TableMeta {
40    pub name: &'static str,
41    pub schema: Option<&'static str>,
42    pub columns: &'static [ColumnMeta],
43    pub keys: &'static [KeyMeta],
44    pub relations: &'static [RelationMeta],
45}
46
47pub trait Entity {
48    fn table() -> &'static TableMeta;
49}
50
51pub trait FromRowNamed: Sized {
52    fn from_row_ms(row: &tiberius::Row) -> anyhow::Result<Self>;
53    fn from_row_pg(row: &tokio_postgres::Row) -> anyhow::Result<Self>;
54}
55
56// Like FromRowNamed, but expects column names to be prefixed with
57// a short identifier, e.g., "t_ColumnName" or "u_ColumnName".
58pub trait FromRowWithPrefix: Sized {
59    fn from_row_ms_with(row: &tiberius::Row, prefix: &str) -> anyhow::Result<Self>;
60    fn from_row_pg_with(row: &tokio_postgres::Row, prefix: &str) -> anyhow::Result<Self>;
61}
62
63pub trait Validatable {
64    fn validate(&self) -> Result<(), Vec<String>>;
65}
66
67pub trait Persistable {
68    fn build_insert(&self, style: PlaceholderStyle) -> (String, Vec<SqlParam>, bool);
69    fn build_update(&self, style: PlaceholderStyle) -> (String, Vec<SqlParam>);
70    fn build_delete(&self, style: PlaceholderStyle) -> (String, Vec<SqlParam>);
71    fn build_delete_by_key(key: SqlParam, style: PlaceholderStyle) -> (String, Vec<SqlParam>);
72}
73
74pub trait KeyAsInt {
75    fn key(&self) -> i32;
76}
77
78pub trait KeyAsGuid {
79    fn key(&self) -> uuid::Uuid;
80}
81
82pub trait KeyAsString {
83    fn key(&self) -> String;
84}