toql_core/table_mapper/
field_options.rs

1use crate::{field_handler::FieldHandler, role_expr::RoleExpr, sql_arg::SqlArg};
2use std::{collections::HashMap, sync::Arc};
3
4#[derive(Debug, Clone)]
5/// Options for a mapped field.
6pub struct FieldOptions {
7    pub(crate) preselect: bool, // Always select this field, regardless of query fields
8    pub(crate) skip_mut: bool,  // Select field on mut select
9    pub(crate) skip_wildcard: bool, // Skip field for wildcard selection
10    pub(crate) load_role_expr: Option<RoleExpr>, // Only for use by these roles
11    pub(crate) update_role_expr: Option<RoleExpr>, // Only for use by these roles
12    pub(crate) aux_params: HashMap<String, SqlArg>, // Auxiliary params
13    pub(crate) on_aux_params: Vec<String>, // Identity params for on clauses
14    pub(crate) key: bool,       // Field is part of key
15    pub(crate) field_handler: Option<Arc<dyn FieldHandler + Send + Sync>>, // Optional join handler
16}
17
18impl FieldOptions {
19    /// Create new mapper options
20    pub fn new() -> Self {
21        FieldOptions {
22            preselect: false,
23            skip_mut: false,
24            skip_wildcard: false,
25            load_role_expr: None,
26            update_role_expr: None,
27            aux_params: HashMap::new(),
28            on_aux_params: Vec::new(),
29            key: false,
30            field_handler: None,
31        }
32    }
33
34    /// Field is selected, regardless of the query.
35    pub fn preselect(mut self, preselect: bool) -> Self {
36        self.preselect = preselect;
37        self
38    }
39
40    /// Use custom handler to build field.
41    pub fn handler<H>(mut self, handler: H) -> Self
42    where
43        H: 'static + FieldHandler + Send + Sync,
44    {
45        self.field_handler = Some(Arc::new(handler));
46        self
47    }
48
49    /// Field is part of key.
50    /// It cannot be update and is always preselected
51    pub fn key(mut self, key: bool) -> Self {
52        self.key = key;
53        self
54    }
55
56    /// Field is used for the mut select query.
57    pub fn skip_mut(mut self, skip: bool) -> Self {
58        self.skip_mut = skip;
59        self
60    }
61    /// Field is ignored by the wildcard.
62    pub fn skip_wildcard(mut self, skip_wildcard: bool) -> Self {
63        self.skip_wildcard = skip_wildcard;
64        self
65    }
66    /// The field can only be selected and filtered by queries that have
67    /// these roles.
68    /// Example: The email address is only visible to users with
69    /// the _admin_ role.
70    pub fn restrict_load(mut self, role_expr: RoleExpr) -> Self {
71        self.load_role_expr = Some(role_expr);
72        self
73    }
74    /// The field can only be selected and filtered by queries that have
75    /// these roles.
76    /// Example: The email address is only visible to users with
77    /// the _admin_ role.
78    pub fn restrict_update(mut self, role_expr: RoleExpr) -> Self {
79        self.update_role_expr = Some(role_expr);
80        self
81    }
82
83    /// Additional build param. This is used by the query builder together with
84    /// its build params. Build params can be used in SQL expressions (`SELECT <param_name>` )
85    /// and field handlers.
86    pub fn aux_param<S, T>(mut self, name: S, value: T) -> Self
87    where
88        S: Into<String>,
89        T: Into<SqlArg>,
90    {
91        self.aux_params.insert(name.into(), value.into());
92        self
93    }
94}
95
96impl Default for FieldOptions {
97    fn default() -> Self {
98        Self::new()
99    }
100}