Skip to main content

vlorql_core/schema/
dialect.rs

1//! Controlled SQL dialect capabilities.
2
3use super::types::{IdentifierQuoting, JoinType, SqlDialect};
4use derive_builder::Builder;
5use schemars::JsonSchema;
6use serde::{Deserialize, Serialize};
7
8/// The SQL capabilities allowed for one query-planning context.
9///
10/// Each profile captures the dialect family, identifier quoting
11/// convention, the set of allowed / denied features, and a few
12/// quantitative limits that the validator and SQL compiler consult.
13/// Use [`DialectProfile::builder`] to construct a customized profile
14/// from the defaults.
15///
16/// # Examples
17///
18/// ```
19/// use vlorql_core::schema::{DialectProfile, SqlDialect, JoinType};
20///
21/// let profile = DialectProfile::builder()
22///     .dialect(SqlDialect::Sqlite)
23///     .max_joins(5)
24///     .build()
25///     .expect("valid profile");
26/// assert_eq!(profile.dialect, SqlDialect::Sqlite);
27/// assert_eq!(profile.max_joins, Some(5));
28/// ```
29#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema, Builder)]
30#[builder(setter(into), default)]
31#[serde(default, deny_unknown_fields)]
32pub struct DialectProfile {
33    /// The SQL dialect this profile emits.
34    pub dialect: SqlDialect,
35    /// How identifiers should be quoted in the generated SQL.
36    pub quote_style: IdentifierQuoting,
37    /// Whether `WITH <cte> ... SELECT ...` is allowed.
38    pub supports_cte: bool,
39    /// Whether window functions are allowed.
40    pub supports_window_functions: bool,
41    /// Whether JSON operators (`->`, `->>`, `@@`, …) are allowed.
42    pub supports_json_operations: bool,
43    /// Maximum number of joins permitted in a single plan. `None`
44    /// means no limit.
45    pub max_joins: Option<usize>,
46    /// The join types the validator allows. An empty list means
47    /// "all join types allowed".
48    pub allowed_join_types: Vec<JoinType>,
49    /// The function names the validator allows. An empty list
50    /// means "any function not explicitly denied".
51    pub allowed_functions: Vec<String>,
52    /// The function names the validator always rejects.
53    pub denied_functions: Vec<String>,
54    /// Maximum number of `GROUP BY` expressions. `None` means no
55    /// limit.
56    pub max_group_by_columns: Option<usize>,
57    /// Whether `DISTINCT` is permitted inside function calls.
58    pub allow_distinct: bool,
59    /// Whether `SELECT DISTINCT` (whole-query dedup) is allowed.
60    pub allow_select_distinct: bool,
61    /// Whether `OFFSET n` is allowed.
62    pub supports_offset: bool,
63    /// Whether `FETCH FIRST n ROWS ONLY` is allowed.
64    pub supports_fetch: bool,
65}
66
67impl Default for DialectProfile {
68    fn default() -> Self {
69        Self {
70            dialect: SqlDialect::Postgres,
71            quote_style: IdentifierQuoting::DoubleQuote,
72            supports_cte: true,
73            supports_window_functions: true,
74            supports_json_operations: true,
75            max_joins: None,
76            allowed_join_types: vec![
77                JoinType::Inner,
78                JoinType::Left,
79                JoinType::Right,
80                JoinType::Full,
81                JoinType::Cross,
82            ],
83            allowed_functions: Vec::new(),
84            denied_functions: Vec::new(),
85            max_group_by_columns: None,
86            allow_distinct: true,
87            allow_select_distinct: true,
88            supports_offset: true,
89            supports_fetch: true,
90        }
91    }
92}
93
94impl DialectProfile {
95    /// Returns a builder initialized with the profile defaults.
96    pub fn builder() -> DialectProfileBuilder {
97        DialectProfileBuilder::default()
98    }
99}