-
Notifications
You must be signed in to change notification settings - Fork 216
/
field.rs
507 lines (422 loc) Β· 13.9 KB
/
field.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
use super::*;
use crate::native_type_instance::NativeTypeInstance;
use crate::scalars::ScalarType;
use crate::traits::{Ignorable, WithDatabaseName, WithName};
use crate::{
default_value::{DefaultValue, ValueGenerator},
relation_info::ReferentialAction,
};
use std::hash::Hash;
/// Arity of a Field in a Model.
#[derive(Debug, PartialEq, Copy, Clone, Eq, Hash)]
pub enum FieldArity {
Required,
Optional,
List,
}
impl FieldArity {
pub fn is_list(&self) -> bool {
self == &Self::List
}
pub fn is_required(&self) -> bool {
self == &Self::Required
}
pub fn is_optional(&self) -> bool {
self == &Self::Optional
}
}
// TODO: when progressing with the native types implementation we should consider merging the variants `NativeType` and `Base`
//Agreed
/// Datamodel field type.
#[derive(Debug, PartialEq, Clone)]
pub enum FieldType {
/// This is an enum field, with an enum of the given name.
Enum(String),
/// This is a relation field.
Relation(RelationInfo),
/// native field type.
NativeType(ScalarType, NativeTypeInstance),
/// This is a field with an unsupported datatype. The content is the db's description of the type, it should enable migrate to create the type.
Unsupported(String),
/// The option is Some(x) if the scalar type is based upon a type alias.
Base(ScalarType, Option<String>),
}
impl FieldType {
pub fn as_base(&self) -> Option<&ScalarType> {
match self {
FieldType::Base(scalar_type, _) => Some(scalar_type),
_ => None,
}
}
pub fn as_native_type(&self) -> Option<(&ScalarType, &NativeTypeInstance)> {
match self {
FieldType::NativeType(a, b) => Some((a, b)),
_ => None,
}
}
pub fn is_compatible_with(&self, other: &FieldType) -> bool {
match (self, other) {
(Self::Base(a, _), Self::Base(b, _)) => a == b, // the name of the type alias is not important for the comparison
(a, b) => a == b,
}
}
pub fn is_datetime(&self) -> bool {
self.scalar_type().map(|st| st.is_datetime()).unwrap_or(false)
}
pub fn is_string(&self) -> bool {
self.scalar_type().map(|st| st.is_string()).unwrap_or(false)
}
pub fn is_enum(&self, name: &str) -> bool {
matches!(self, Self::Enum(this) if this == name)
}
pub fn scalar_type(&self) -> Option<ScalarType> {
match self {
FieldType::NativeType(st, _) => Some(*st),
FieldType::Base(st, _) => Some(*st),
_ => None,
}
}
pub fn native_type(&self) -> Option<&NativeTypeInstance> {
match self {
FieldType::NativeType(_, nt) => Some(nt),
_ => None,
}
}
}
/// Represents a Field in a Model.
#[derive(Debug, PartialEq, Clone)]
pub enum Field {
ScalarField(ScalarField),
RelationField(RelationField),
}
impl Field {
pub fn as_relation_field(&self) -> Option<&RelationField> {
match self {
Field::RelationField(sf) => Some(sf),
_ => None,
}
}
pub fn as_scalar_field(&self) -> Option<&ScalarField> {
match self {
Field::ScalarField(sf) => Some(sf),
_ => None,
}
}
pub fn is_relation(&self) -> bool {
matches!(self, Field::RelationField(_))
}
pub fn is_scalar_field(&self) -> bool {
matches!(self, Field::ScalarField(_))
}
pub fn name(&self) -> &str {
match &self {
Field::ScalarField(sf) => &sf.name,
Field::RelationField(rf) => &rf.name,
}
}
pub fn documentation(&self) -> Option<&str> {
match self {
Field::ScalarField(sf) => sf.documentation.as_deref(),
Field::RelationField(rf) => rf.documentation.as_deref(),
}
}
pub fn set_documentation(&mut self, documentation: Option<String>) {
match self {
Field::ScalarField(sf) => sf.documentation = documentation,
Field::RelationField(rf) => rf.documentation = documentation,
}
}
pub fn is_commented_out(&self) -> bool {
match self {
Field::ScalarField(sf) => sf.is_commented_out,
Field::RelationField(rf) => rf.is_commented_out,
}
}
pub fn arity(&self) -> &FieldArity {
match &self {
Field::ScalarField(sf) => &sf.arity,
Field::RelationField(rf) => &rf.arity,
}
}
pub fn field_type(&self) -> FieldType {
match &self {
Field::ScalarField(sf) => sf.field_type.clone(),
Field::RelationField(rf) => FieldType::Relation(rf.relation_info.clone()),
}
}
pub fn default_value(&self) -> Option<&DefaultValue> {
match &self {
Field::ScalarField(sf) => sf.default_value.as_ref(),
Field::RelationField(_) => None,
}
}
pub fn is_updated_at(&self) -> bool {
match &self {
Field::ScalarField(sf) => sf.is_updated_at,
Field::RelationField(_) => false,
}
}
pub fn is_unique(&self) -> bool {
match &self {
Field::ScalarField(sf) => sf.is_unique,
Field::RelationField(_) => false,
}
}
pub fn is_id(&self) -> bool {
match &self {
Field::ScalarField(sf) => sf.is_id,
Field::RelationField(_) => false,
}
}
pub fn is_generated(&self) -> bool {
match &self {
Field::ScalarField(sf) => sf.is_generated,
Field::RelationField(rf) => rf.is_generated,
}
}
}
impl WithName for Field {
fn name(&self) -> &String {
match self {
Field::ScalarField(sf) => sf.name(),
Field::RelationField(rf) => rf.name(),
}
}
fn set_name(&mut self, name: &str) {
match self {
Field::ScalarField(sf) => sf.set_name(name),
Field::RelationField(rf) => rf.set_name(name),
}
}
}
impl WithDatabaseName for Field {
fn database_name(&self) -> Option<&str> {
match self {
Field::ScalarField(sf) => sf.database_name.as_deref(),
Field::RelationField(_) => None,
}
}
fn set_database_name(&mut self, database_name: Option<String>) {
match self {
Field::ScalarField(sf) => sf.set_database_name(database_name),
Field::RelationField(_) => (),
}
}
}
/// Represents a relation field in a model.
#[derive(Debug, Clone)]
pub struct RelationField {
/// Name of the field.
pub name: String,
/// The field's type.
pub relation_info: RelationInfo,
/// The field's arity.
pub arity: FieldArity,
/// Comments associated with this field.
pub documentation: Option<String>,
/// signals that this field was internally generated (only back relation fields as of now)
pub is_generated: bool,
/// Indicates if this field has to be commented out.
pub is_commented_out: bool,
/// Indicates if this field has to be ignored by the Client.
pub is_ignored: bool,
/// Is `ON DELETE/UPDATE RESTRICT` allowed.
pub supports_restrict_action: Option<bool>,
}
impl PartialEq for RelationField {
//ignores the relation name for reintrospection
fn eq(&self, other: &Self) -> bool {
let this_matches = self.name == other.name
&& self.arity == other.arity
&& self.documentation == other.documentation
&& self.is_generated == other.is_generated
&& self.is_commented_out == other.is_commented_out
&& self.is_ignored == other.is_ignored
&& self.relation_info == other.relation_info;
let this_on_delete = self.relation_info.on_delete.or_else(|| self.default_on_delete_action());
let other_on_delete = other
.relation_info
.on_delete
.or_else(|| other.default_on_delete_action());
let on_delete_matches = this_on_delete == other_on_delete;
let this_on_update = self
.relation_info
.on_update
.unwrap_or_else(|| self.default_on_update_action());
let other_on_update = other
.relation_info
.on_update
.unwrap_or_else(|| other.default_on_update_action());
let on_update_matches = this_on_update == other_on_update;
this_matches && on_delete_matches && on_update_matches
}
}
impl RelationField {
/// Creates a new field with the given name and type.
pub fn new(name: &str, arity: FieldArity, relation_info: RelationInfo) -> Self {
RelationField {
name: String::from(name),
arity,
relation_info,
documentation: None,
is_generated: false,
is_commented_out: false,
is_ignored: false,
supports_restrict_action: None,
}
}
/// The default `onDelete` can be `Restrict`.
pub fn supports_restrict_action(&mut self, value: bool) {
self.supports_restrict_action = Some(value);
}
/// Creates a new field with the given name and type, marked as generated and optional.
pub fn new_generated(name: &str, info: RelationInfo, required: bool) -> Self {
let arity = if required {
FieldArity::Required
} else {
FieldArity::Optional
};
let mut field = Self::new(name, arity, info);
field.is_generated = true;
field
}
pub fn points_to_model(&self, name: &str) -> bool {
self.relation_info.to == name
}
pub fn is_required(&self) -> bool {
self.arity.is_required()
}
pub fn is_list(&self) -> bool {
self.arity.is_list()
}
pub fn is_singular(&self) -> bool {
!self.is_list()
}
pub fn is_optional(&self) -> bool {
self.arity.is_optional()
}
pub fn default_on_delete_action(&self) -> Option<ReferentialAction> {
self.supports_restrict_action.map(|restrict_ok| match self.arity {
FieldArity::Required if restrict_ok => ReferentialAction::Restrict,
FieldArity::Required => ReferentialAction::NoAction,
_ => ReferentialAction::SetNull,
})
}
pub fn default_on_update_action(&self) -> ReferentialAction {
match self.arity {
FieldArity::Required => ReferentialAction::Cascade,
_ => ReferentialAction::SetNull,
}
}
}
/// Represents a scalar field in a model.
#[derive(Debug, PartialEq, Clone)]
pub struct ScalarField {
/// Name of the field.
pub name: String,
/// The field's type.
pub field_type: FieldType,
/// The field's arity.
pub arity: FieldArity,
/// The database internal name.
pub database_name: Option<String>,
/// The default value.
pub default_value: Option<DefaultValue>,
/// Indicates if the field is unique.
pub is_unique: bool,
/// true if this field marked with @id.
pub is_id: bool,
/// Comments associated with this field.
pub documentation: Option<String>,
/// signals that this field was internally generated (only back relation fields as of now)
pub is_generated: bool,
/// If set, signals that this field is updated_at and will be updated to now()
/// automatically.
pub is_updated_at: bool,
/// Indicates if this field has to be commented out.
pub is_commented_out: bool,
/// Indicates if this field is ignored by the Client.
pub is_ignored: bool,
}
impl ScalarField {
/// Creates a new field with the given name and type.
pub fn new(name: &str, arity: FieldArity, field_type: FieldType) -> ScalarField {
ScalarField {
name: String::from(name),
arity,
field_type,
database_name: None,
default_value: None,
is_unique: false,
is_id: false,
documentation: None,
is_generated: false,
is_updated_at: false,
is_commented_out: false,
is_ignored: false,
}
}
/// Creates a new field with the given name and type, marked as generated and optional.
pub fn new_generated(name: &str, field_type: FieldType) -> ScalarField {
let mut field = Self::new(name, FieldArity::Optional, field_type);
field.is_generated = true;
field
}
//todo use withdatabasename::final_database_name instead
pub fn db_name(&self) -> &str {
self.database_name.as_ref().unwrap_or(&self.name)
}
pub fn is_required(&self) -> bool {
self.arity.is_required()
}
pub fn is_list(&self) -> bool {
self.arity.is_list()
}
pub fn is_singular(&self) -> bool {
!self.is_list()
}
pub fn is_optional(&self) -> bool {
self.arity.is_optional()
}
pub fn is_auto_increment(&self) -> bool {
matches!(&self.default_value, Some(DefaultValue::Expression(expr)) if expr == &ValueGenerator::new_autoincrement())
}
}
impl WithName for ScalarField {
fn name(&self) -> &String {
&self.name
}
fn set_name(&mut self, name: &str) {
self.name = String::from(name)
}
}
impl WithName for RelationField {
fn name(&self) -> &String {
&self.name
}
fn set_name(&mut self, name: &str) {
self.name = String::from(name)
}
}
impl WithDatabaseName for ScalarField {
fn database_name(&self) -> Option<&str> {
self.database_name.as_deref()
}
fn set_database_name(&mut self, database_name: Option<String>) {
self.database_name = database_name;
}
}
impl Ignorable for Field {
fn is_ignored(&self) -> bool {
match self {
Field::RelationField(rf) => rf.is_ignored,
Field::ScalarField(sf) => sf.is_ignored,
}
}
fn ignore(&mut self) {
match self {
Field::RelationField(rf) => rf.is_ignored = true,
Field::ScalarField(sf) => sf.is_ignored = true,
}
}
}