Skip to main content

reddb_server/application/
schema.rs

1use crate::application::ports::RuntimeSchemaPort;
2use crate::runtime::RuntimeQueryResult;
3use crate::RedDBResult;
4
5#[derive(Debug, Clone)]
6pub struct CreateTableColumnInput {
7    pub name: String,
8    pub data_type: String,
9    pub not_null: bool,
10    pub default: Option<String>,
11    pub compress: Option<u8>,
12    pub unique: bool,
13    pub primary_key: bool,
14    pub enum_variants: Vec<String>,
15    pub array_element: Option<String>,
16    pub decimal_precision: Option<u8>,
17}
18
19/// How the application-level DTO expresses a `PARTITION BY` clause.
20/// Mirrors `storage::query::PartitionKind` but stays decoupled from the
21/// SQL-parser crate so drivers can call this port without pulling the
22/// AST module into their dependency graph.
23#[derive(Debug, Clone, Copy, PartialEq, Eq)]
24pub enum CreateTablePartitionKind {
25    Range,
26    List,
27    Hash,
28}
29
30#[derive(Debug, Clone, PartialEq, Eq)]
31pub struct CreateTablePartitionSpec {
32    pub kind: CreateTablePartitionKind,
33    pub column: String,
34}
35
36#[derive(Debug, Clone)]
37pub struct CreateTableInput {
38    pub name: String,
39    pub columns: Vec<CreateTableColumnInput>,
40    pub if_not_exists: bool,
41    pub default_ttl_ms: Option<u64>,
42    pub context_index_fields: Vec<String>,
43    pub timestamps: bool,
44    /// Optional `PARTITION BY RANGE|LIST|HASH (column)` declaration.
45    /// Reaches parity with the SQL parser path — callers no longer
46    /// need to route partitioned DDL through raw SQL.
47    pub partition_by: Option<CreateTablePartitionSpec>,
48    /// Optional `TENANT BY (column)` declaration (auto-install RLS).
49    pub tenant_by: Option<String>,
50    /// Reject UPDATE / DELETE at parse time when `true`.
51    /// Corresponds to `CREATE TABLE ... APPEND ONLY`.
52    pub append_only: bool,
53}
54
55#[derive(Debug, Clone)]
56pub struct DropTableInput {
57    pub name: String,
58    pub if_exists: bool,
59}
60
61#[derive(Debug, Clone)]
62pub struct CreateTimeSeriesInput {
63    pub name: String,
64    pub retention_ms: Option<u64>,
65    pub chunk_size: Option<usize>,
66    pub downsample_policies: Vec<String>,
67    pub if_not_exists: bool,
68}
69
70#[derive(Debug, Clone)]
71pub struct DropTimeSeriesInput {
72    pub name: String,
73    pub if_exists: bool,
74}
75
76pub struct SchemaUseCases<'a, P: ?Sized> {
77    runtime: &'a P,
78}
79
80impl<'a, P: RuntimeSchemaPort + ?Sized> SchemaUseCases<'a, P> {
81    pub fn new(runtime: &'a P) -> Self {
82        Self { runtime }
83    }
84
85    pub fn create_table(&self, input: CreateTableInput) -> RedDBResult<RuntimeQueryResult> {
86        self.runtime.create_table(input)
87    }
88
89    pub fn drop_table(&self, input: DropTableInput) -> RedDBResult<RuntimeQueryResult> {
90        self.runtime.drop_table(input)
91    }
92
93    pub fn create_timeseries(
94        &self,
95        input: CreateTimeSeriesInput,
96    ) -> RedDBResult<RuntimeQueryResult> {
97        self.runtime.create_timeseries(input)
98    }
99
100    pub fn drop_timeseries(&self, input: DropTimeSeriesInput) -> RedDBResult<RuntimeQueryResult> {
101        self.runtime.drop_timeseries(input)
102    }
103}