nodedb_types/columnar/
column_def.rs1use std::fmt;
7
8use serde::{Deserialize, Serialize};
9
10use super::column_type::ColumnType;
11
12#[derive(
17 Debug,
18 Clone,
19 PartialEq,
20 Eq,
21 Hash,
22 Serialize,
23 Deserialize,
24 zerompk::ToMessagePack,
25 zerompk::FromMessagePack,
26)]
27#[msgpack(c_enum)]
28#[repr(u8)]
29pub enum ColumnModifier {
30 TimeKey = 0,
33 SpatialIndex = 1,
36}
37
38#[non_exhaustive]
43#[derive(
44 Debug,
45 Clone,
46 PartialEq,
47 Eq,
48 Serialize,
49 Deserialize,
50 zerompk::ToMessagePack,
51 zerompk::FromMessagePack,
52)]
53pub struct ColumnDef {
54 pub name: String,
55 pub column_type: ColumnType,
56 pub nullable: bool,
57 pub default: Option<String>,
58 pub primary_key: bool,
59 #[serde(default, skip_serializing_if = "Vec::is_empty")]
61 pub modifiers: Vec<ColumnModifier>,
62 #[serde(default, skip_serializing_if = "Option::is_none")]
65 pub generated_expr: Option<String>,
66 #[serde(default, skip_serializing_if = "Vec::is_empty")]
68 pub generated_deps: Vec<String>,
69 #[serde(default = "default_added_at_version")]
74 pub added_at_version: u32,
75}
76
77fn default_added_at_version() -> u32 {
78 1
79}
80
81impl ColumnDef {
82 pub fn required(name: impl Into<String>, column_type: ColumnType) -> Self {
83 Self {
84 name: name.into(),
85 column_type,
86 nullable: false,
87 default: None,
88 primary_key: false,
89 modifiers: Vec::new(),
90 generated_expr: None,
91 generated_deps: Vec::new(),
92 added_at_version: 1,
93 }
94 }
95
96 pub fn nullable(name: impl Into<String>, column_type: ColumnType) -> Self {
97 Self {
98 name: name.into(),
99 column_type,
100 nullable: true,
101 default: None,
102 primary_key: false,
103 modifiers: Vec::new(),
104 generated_expr: None,
105 generated_deps: Vec::new(),
106 added_at_version: 1,
107 }
108 }
109
110 pub fn with_primary_key(mut self) -> Self {
111 self.primary_key = true;
112 self.nullable = false;
113 self
114 }
115
116 pub fn is_time_key(&self) -> bool {
118 self.modifiers.contains(&ColumnModifier::TimeKey)
119 }
120
121 pub fn is_spatial_index(&self) -> bool {
123 self.modifiers.contains(&ColumnModifier::SpatialIndex)
124 }
125
126 pub fn with_default(mut self, expr: impl Into<String>) -> Self {
127 self.default = Some(expr.into());
128 self
129 }
130}
131
132impl fmt::Display for ColumnDef {
133 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
134 write!(f, "{} {}", self.name, self.column_type)?;
135 if !self.nullable {
136 write!(f, " NOT NULL")?;
137 }
138 if self.primary_key {
139 write!(f, " PRIMARY KEY")?;
140 }
141 if let Some(ref d) = self.default {
142 write!(f, " DEFAULT {d}")?;
143 }
144 Ok(())
145 }
146}