Skip to main content

ormdantic_schema/
column.rs

1#[derive(Debug, Clone, PartialEq, Eq)]
2pub struct ColumnDef {
3    name: String,
4    kind: FieldKind,
5    nullable: bool,
6    primary_key: bool,
7    default: Option<ColumnDefault>,
8    server_default: Option<String>,
9    identity: Option<IdentityDef>,
10    computed: Option<ComputedDef>,
11    autoincrement: bool,
12    collation: Option<String>,
13    comment: Option<String>,
14    max_length: Option<u32>,
15    precision: Option<u32>,
16    scale: Option<u32>,
17    sqlite_on_conflict_primary_key: Option<String>,
18    sqlite_on_conflict_not_null: Option<String>,
19    sqlite_on_conflict_unique: Option<String>,
20}
21
22impl ColumnDef {
23    pub fn new(name: impl Into<String>, kind: FieldKind) -> Self {
24        Self {
25            name: name.into(),
26            kind,
27            nullable: false,
28            primary_key: false,
29            default: None,
30            server_default: None,
31            identity: None,
32            computed: None,
33            autoincrement: false,
34            collation: None,
35            comment: None,
36            max_length: None,
37            precision: None,
38            scale: None,
39            sqlite_on_conflict_primary_key: None,
40            sqlite_on_conflict_not_null: None,
41            sqlite_on_conflict_unique: None,
42        }
43    }
44
45    pub fn nullable(mut self, nullable: bool) -> Self {
46        self.nullable = nullable;
47        self
48    }
49
50    pub fn primary_key(mut self, primary_key: bool) -> Self {
51        self.primary_key = primary_key;
52        self
53    }
54
55    pub fn default(mut self, default: ColumnDefault) -> Self {
56        self.default = Some(default);
57        self
58    }
59
60    pub fn with_server_default(mut self, server_default: impl Into<String>) -> Self {
61        self.server_default = Some(server_default.into());
62        self
63    }
64
65    pub fn with_identity(mut self, identity: IdentityDef) -> Self {
66        self.identity = Some(identity);
67        self
68    }
69
70    pub fn with_computed(mut self, computed: ComputedDef) -> Self {
71        self.computed = Some(computed);
72        self
73    }
74
75    pub fn autoincrement(mut self, autoincrement: bool) -> Self {
76        self.autoincrement = autoincrement;
77        self
78    }
79
80    pub fn with_collation(mut self, collation: impl Into<String>) -> Self {
81        self.collation = Some(collation.into());
82        self
83    }
84
85    pub fn with_comment(mut self, comment: impl Into<String>) -> Self {
86        self.comment = Some(comment.into());
87        self
88    }
89
90    pub fn with_max_length(mut self, max_length: u32) -> Self {
91        self.max_length = Some(max_length);
92        self
93    }
94
95    pub fn numeric(mut self, precision: u32, scale: u32) -> Self {
96        self.precision = Some(precision);
97        self.scale = Some(scale);
98        self
99    }
100
101    pub fn with_sqlite_on_conflict_primary_key(mut self, policy: impl Into<String>) -> Self {
102        self.sqlite_on_conflict_primary_key = Some(policy.into());
103        self
104    }
105
106    pub fn with_sqlite_on_conflict_primary_key_option(mut self, policy: Option<String>) -> Self {
107        self.sqlite_on_conflict_primary_key = policy;
108        self
109    }
110
111    pub fn with_sqlite_on_conflict_not_null(mut self, policy: impl Into<String>) -> Self {
112        self.sqlite_on_conflict_not_null = Some(policy.into());
113        self
114    }
115
116    pub fn with_sqlite_on_conflict_not_null_option(mut self, policy: Option<String>) -> Self {
117        self.sqlite_on_conflict_not_null = policy;
118        self
119    }
120
121    pub fn with_sqlite_on_conflict_unique(mut self, policy: impl Into<String>) -> Self {
122        self.sqlite_on_conflict_unique = Some(policy.into());
123        self
124    }
125
126    pub fn with_sqlite_on_conflict_unique_option(mut self, policy: Option<String>) -> Self {
127        self.sqlite_on_conflict_unique = policy;
128        self
129    }
130
131    pub fn name(&self) -> &str {
132        &self.name
133    }
134
135    pub fn kind(&self) -> &FieldKind {
136        &self.kind
137    }
138
139    pub fn is_nullable(&self) -> bool {
140        self.nullable
141    }
142
143    pub fn is_primary_key(&self) -> bool {
144        self.primary_key
145    }
146
147    pub fn default_value(&self) -> Option<&ColumnDefault> {
148        self.default.as_ref()
149    }
150
151    pub fn server_default(&self) -> Option<&str> {
152        self.server_default.as_deref()
153    }
154
155    pub fn identity(&self) -> Option<&IdentityDef> {
156        self.identity.as_ref()
157    }
158
159    pub fn computed(&self) -> Option<&ComputedDef> {
160        self.computed.as_ref()
161    }
162
163    pub fn is_autoincrement(&self) -> bool {
164        self.autoincrement
165    }
166
167    pub fn collation(&self) -> Option<&str> {
168        self.collation.as_deref()
169    }
170
171    pub fn comment(&self) -> Option<&str> {
172        self.comment.as_deref()
173    }
174
175    pub fn definition_eq_ignoring_comment(&self, other: &Self) -> bool {
176        self.name == other.name
177            && self.kind == other.kind
178            && self.nullable == other.nullable
179            && self.primary_key == other.primary_key
180            && self.default == other.default
181            && self.server_default == other.server_default
182            && self.identity == other.identity
183            && self.computed == other.computed
184            && self.autoincrement == other.autoincrement
185            && self.collation == other.collation
186            && self.max_length == other.max_length
187            && self.precision == other.precision
188            && self.scale == other.scale
189            && self.sqlite_on_conflict_primary_key == other.sqlite_on_conflict_primary_key
190            && self.sqlite_on_conflict_not_null == other.sqlite_on_conflict_not_null
191            && self.sqlite_on_conflict_unique == other.sqlite_on_conflict_unique
192    }
193
194    pub fn precision(&self) -> Option<u32> {
195        self.precision
196    }
197
198    pub fn max_length(&self) -> Option<u32> {
199        self.max_length
200    }
201
202    pub fn scale(&self) -> Option<u32> {
203        self.scale
204    }
205
206    pub fn sqlite_on_conflict_primary_key(&self) -> Option<&str> {
207        self.sqlite_on_conflict_primary_key.as_deref()
208    }
209
210    pub fn sqlite_on_conflict_not_null(&self) -> Option<&str> {
211        self.sqlite_on_conflict_not_null.as_deref()
212    }
213
214    pub fn sqlite_on_conflict_unique(&self) -> Option<&str> {
215        self.sqlite_on_conflict_unique.as_deref()
216    }
217}
218
219#[derive(Debug, Clone, PartialEq, Eq)]
220pub enum ColumnDefault {
221    Literal(String),
222    Expression(String),
223}
224
225#[derive(Debug, Clone, PartialEq, Eq)]
226pub struct IdentityDef {
227    start: Option<i64>,
228    increment: Option<i64>,
229    min_value: Option<i64>,
230    max_value: Option<i64>,
231    no_min_value: bool,
232    no_max_value: bool,
233    cache: Option<i64>,
234    always: bool,
235    cycle: bool,
236    order: bool,
237    on_null: bool,
238}
239
240impl IdentityDef {
241    pub fn new() -> Self {
242        Self {
243            start: None,
244            increment: None,
245            min_value: None,
246            max_value: None,
247            no_min_value: false,
248            no_max_value: false,
249            cache: None,
250            always: false,
251            cycle: false,
252            order: false,
253            on_null: false,
254        }
255    }
256
257    pub fn start(mut self, start: i64) -> Self {
258        self.start = Some(start);
259        self
260    }
261
262    pub fn increment(mut self, increment: i64) -> Self {
263        self.increment = Some(increment);
264        self
265    }
266
267    pub fn min_value(mut self, min_value: i64) -> Self {
268        self.min_value = Some(min_value);
269        self
270    }
271
272    pub fn max_value(mut self, max_value: i64) -> Self {
273        self.max_value = Some(max_value);
274        self
275    }
276
277    pub fn no_min_value(mut self, no_min_value: bool) -> Self {
278        self.no_min_value = no_min_value;
279        self
280    }
281
282    pub fn no_max_value(mut self, no_max_value: bool) -> Self {
283        self.no_max_value = no_max_value;
284        self
285    }
286
287    pub fn cache(mut self, cache: i64) -> Self {
288        self.cache = Some(cache);
289        self
290    }
291
292    pub fn always(mut self, always: bool) -> Self {
293        self.always = always;
294        self
295    }
296
297    pub fn cycle(mut self, cycle: bool) -> Self {
298        self.cycle = cycle;
299        self
300    }
301
302    pub fn order(mut self, order: bool) -> Self {
303        self.order = order;
304        self
305    }
306
307    pub fn on_null(mut self, on_null: bool) -> Self {
308        self.on_null = on_null;
309        self
310    }
311
312    pub fn start_value(&self) -> Option<i64> {
313        self.start
314    }
315
316    pub fn increment_value(&self) -> Option<i64> {
317        self.increment
318    }
319
320    pub fn minimum_value(&self) -> Option<i64> {
321        self.min_value
322    }
323
324    pub fn maximum_value(&self) -> Option<i64> {
325        self.max_value
326    }
327
328    pub fn is_no_min_value(&self) -> bool {
329        self.no_min_value
330    }
331
332    pub fn is_no_max_value(&self) -> bool {
333        self.no_max_value
334    }
335
336    pub fn cache_value(&self) -> Option<i64> {
337        self.cache
338    }
339
340    pub fn is_always(&self) -> bool {
341        self.always
342    }
343
344    pub fn is_cycle(&self) -> bool {
345        self.cycle
346    }
347
348    pub fn is_ordered(&self) -> bool {
349        self.order
350    }
351
352    pub fn is_on_null(&self) -> bool {
353        self.on_null
354    }
355}
356
357impl Default for IdentityDef {
358    fn default() -> Self {
359        Self::new()
360    }
361}
362
363#[derive(Debug, Clone, PartialEq, Eq)]
364pub struct ComputedDef {
365    expression: String,
366    persisted: bool,
367}
368
369impl ComputedDef {
370    pub fn new(expression: impl Into<String>) -> Self {
371        Self {
372            expression: expression.into(),
373            persisted: false,
374        }
375    }
376
377    pub fn persisted(mut self, persisted: bool) -> Self {
378        self.persisted = persisted;
379        self
380    }
381
382    pub fn expression(&self) -> &str {
383        &self.expression
384    }
385
386    pub fn is_persisted(&self) -> bool {
387        self.persisted
388    }
389}
390
391#[derive(Debug, Clone, PartialEq, Eq)]
392pub enum FieldKind {
393    String,
394    Integer,
395    Float,
396    Boolean,
397    Uuid,
398    Date,
399    DateTime,
400    Json,
401    ModelJson,
402    Enum {
403        name: Option<String>,
404        schema: Option<String>,
405    },
406    Decimal,
407    Binary,
408    ForeignKey {
409        target_table: String,
410    },
411    Unknown,
412}