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
use crate::{
    ast::{self, WithName},
    types::{DefaultAttribute, FieldWithArgs, OperatorClassStore, ScalarField, ScalarType, SortOrder},
    walkers::*,
    OperatorClass, ParserDatabase, ScalarFieldId, ScalarFieldType,
};
use diagnostics::Span;
use either::Either;

/// A scalar field, as part of a model.
pub type ScalarFieldWalker<'db> = Walker<'db, ScalarFieldId>;

impl<'db> ScalarFieldWalker<'db> {
    /// The ID of the field node in the AST.
    pub fn field_id(self) -> ast::FieldId {
        self.attributes().field_id
    }

    /// The field node in the AST.
    pub fn ast_field(self) -> &'db ast::Field {
        let ScalarField { model_id, field_id, .. } = self.attributes();
        &self.db.ast[*model_id][*field_id]
    }

    /// Is this field unique? This method will return true if:
    ///
    /// - The field has an `@id` or `@unique` attribute.
    /// - There is an `@@id` or `@@unique` on the model that contains __only__ this field.
    pub fn is_unique(self) -> bool {
        let model = self.model();

        if let Some(true) = model
            .primary_key()
            .map(|pk| pk.contains_exactly_fields_by_id(&[self.id]))
        {
            return true;
        }

        self.model().indexes().any(|idx| {
            let mut fields = idx.fields();
            idx.is_unique() && fields.len() == 1 && fields.next().map(|f| f.field_id()) == Some(self.field_id())
        })
    }

    /// The name of the field.
    pub fn name(self) -> &'db str {
        self.ast_field().name()
    }

    /// The `@default()` AST attribute on the field, if any.
    pub fn default_attribute(self) -> Option<&'db ast::Attribute> {
        self.attributes()
            .default
            .as_ref()
            .map(|d| d.default_attribute)
            .map(|id| &self.db.ast[id])
    }

    /// The final database name of the field. See crate docs for explanations on database names.
    pub fn database_name(self) -> &'db str {
        self.attributes()
            .mapped_name
            .map(|id| &self.db[id])
            .unwrap_or_else(|| self.name())
    }

    /// Does the field have an `@default(autoincrement())` attribute?
    pub fn is_autoincrement(self) -> bool {
        self.default_value().map(|dv| dv.is_autoincrement()).unwrap_or(false)
    }

    /// Does the field define a primary key by its own.
    pub fn is_single_pk(self) -> bool {
        self.model().field_is_single_pk(self.field_id())
    }

    /// Is the field part of a compound primary key.
    pub fn is_part_of_a_compound_pk(self) -> bool {
        self.model().field_is_part_of_a_compound_pk(self.field_id())
    }

    /// Is there an `@ignore` attribute on the field?
    pub fn is_ignored(self) -> bool {
        self.attributes().is_ignored
    }

    /// Is the field optional / nullable?
    pub fn is_optional(self) -> bool {
        self.ast_field().arity.is_optional()
    }

    /// Is the field a list
    pub fn is_list(self) -> bool {
        self.ast_field().arity.is_list()
    }

    /// Is there an `@updatedAt` attribute on the field?
    pub fn is_updated_at(self) -> bool {
        self.attributes().is_updated_at
    }

    fn attributes(self) -> &'db ScalarField {
        &self.db.types[self.id]
    }

    /// Is this field's type an enum? If yes, walk the enum.
    pub fn field_type_as_enum(self) -> Option<EnumWalker<'db>> {
        self.scalar_field_type().as_enum().map(|id| self.db.walk(id))
    }

    /// The name in the `@map(<name>)` attribute.
    pub fn mapped_name(self) -> Option<&'db str> {
        self.attributes().mapped_name.map(|id| &self.db[id])
    }

    /// The model that contains the field.
    pub fn model(self) -> ModelWalker<'db> {
        self.walk(self.attributes().model_id)
    }

    /// (attribute scope, native type name, arguments, span)
    ///
    /// For example: `@db.Text` would translate to ("db", "Text", &[], <the span>)
    pub fn raw_native_type(self) -> Option<(&'db str, &'db str, &'db [String], Span)> {
        let db = self.db;
        self.attributes()
            .native_type
            .as_ref()
            .map(move |(datasource_name, name, args, span)| (&db[*datasource_name], &db[*name], args.as_slice(), *span))
    }

    /// Is the type of the field `Unsupported("...")`?
    pub fn is_unsupported(self) -> bool {
        matches!(self.ast_field().field_type, ast::FieldType::Unsupported(_, _))
    }

    /// The `@default()` attribute of the field, if any.
    pub fn default_value(self) -> Option<DefaultValueWalker<'db>> {
        let ScalarField { default, .. } = self.attributes();
        default.as_ref().map(|default| DefaultValueWalker {
            field_id: self.id,
            db: self.db,
            default,
        })
    }

    /// The type of the field.
    pub fn scalar_field_type(self) -> ScalarFieldType {
        self.attributes().r#type
    }

    /// The type of the field in case it is a scalar type (not an enum, not a composite type).
    pub fn scalar_type(self) -> Option<ScalarType> {
        match self.attributes().r#type {
            ScalarFieldType::BuiltInScalar(scalar) => Some(scalar),
            _ => None,
        }
    }
}

/// An `@default()` attribute on a field.
#[derive(Clone, Copy)]
pub struct DefaultValueWalker<'db> {
    pub(super) field_id: ScalarFieldId,
    pub(super) db: &'db ParserDatabase,
    pub(super) default: &'db DefaultAttribute,
}

impl<'db> DefaultValueWalker<'db> {
    /// The AST node of the attribute.
    pub fn ast_attribute(self) -> &'db ast::Attribute {
        &self.db.ast[self.default.default_attribute]
    }

    /// The value expression in the `@default` attribute.
    ///
    /// ```ignore
    /// score Int @default(0)
    ///                    ^
    /// ```
    pub fn value(self) -> &'db ast::Expression {
        &self.ast_attribute().arguments.arguments[self.default.argument_idx].value
    }

    /// Is this an `@default(autoincrement())`?
    pub fn is_autoincrement(self) -> bool {
        matches!(self.value(), ast::Expression::Function(name, _, _) if name == "autoincrement")
    }

    /// Is this an `@default(cuid())`?
    pub fn is_cuid(self) -> bool {
        matches!(self.value(), ast::Expression::Function(name, _, _) if name == "cuid")
    }

    /// Is this an `@default(nanoid())`?
    pub fn is_nanoid(self) -> bool {
        matches!(self.value(), ast::Expression::Function(name, _, _) if name == "nanoid")
    }

    /// Is this an `@default(dbgenerated())`?
    pub fn is_dbgenerated(self) -> bool {
        matches!(self.value(), ast::Expression::Function(name, _, _) if name == "dbgenerated")
    }

    /// Is this an `@default(auto())`?
    pub fn is_auto(self) -> bool {
        matches!(self.value(), ast::Expression::Function(name, _, _) if name == "auto")
    }

    /// Is this an `@default(now())`?
    pub fn is_now(self) -> bool {
        matches!(self.value(), ast::Expression::Function(name, _, _) if name == "now")
    }

    /// Is this an `@default(sequence())`?
    pub fn is_sequence(self) -> bool {
        matches!(self.value(), ast::Expression::Function(name, _, _) if name == "sequence")
    }

    /// Is this an `@default(uuid())`?
    pub fn is_uuid(self) -> bool {
        matches!(self.value(), ast::Expression::Function(name, _, _) if name == "uuid")
    }

    /// The mapped name of the default value. Not applicable to all connectors. See crate docs for
    /// details on mapped names.
    ///
    /// ```ignore
    /// name String @default("george", map: "name_default_to_george")
    ///                                     ^^^^^^^^^^^^^^^^^^^^^^^^
    /// ```
    pub fn mapped_name(self) -> Option<&'db str> {
        self.default.mapped_name.map(|id| &self.db[id])
    }

    /// The field carrying the default attribute.
    ///
    /// ```ignore
    /// name String @default("george")
    /// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    /// ```
    pub fn field(self) -> ScalarFieldWalker<'db> {
        self.db.walk(self.field_id)
    }
}

/// An operator class defines the operators allowed in an index. Mostly
/// a PostgreSQL thing.
#[derive(Copy, Clone)]
pub struct OperatorClassWalker<'db> {
    pub(crate) class: &'db OperatorClassStore,
    pub(crate) db: &'db ParserDatabase,
}

impl<'db> OperatorClassWalker<'db> {
    /// Gets the operator class of the indexed field.
    ///
    /// ```ignore
    /// @@index(name(ops: InetOps))
    /// //                ^ Either::Left(InetOps)
    /// @@index(name(ops: raw("tsvector_ops")))
    /// //                ^ Either::Right("tsvector_ops")
    pub fn get(self) -> Either<OperatorClass, &'db str> {
        match self.class.inner {
            Either::Left(class) => Either::Left(class),
            Either::Right(id) => Either::Right(&self.db[id]),
        }
    }
}

/// A scalar field as referenced in a key specification (id, index or unique).
#[derive(Copy, Clone)]
pub struct ScalarFieldAttributeWalker<'db> {
    pub(crate) fields: &'db [FieldWithArgs],
    pub(crate) db: &'db ParserDatabase,
    pub(crate) field_arg_id: usize,
}

impl<'db> ScalarFieldAttributeWalker<'db> {
    fn args(self) -> &'db FieldWithArgs {
        &self.fields[self.field_arg_id]
    }

    /// The length argument on the field.
    ///
    /// ```ignore
    /// @@index(name(length: 10))
    ///                      ^^
    /// ```
    pub fn length(self) -> Option<u32> {
        self.args().length
    }

    /// A custom operator class to control the operators catched by the index.
    ///
    /// ```ignore
    /// @@index([name(ops: InetOps)], type: Gist)
    ///                    ^^^^^^^
    /// ```
    pub fn operator_class(self) -> Option<OperatorClassWalker<'db>> {
        self.args()
            .operator_class
            .as_ref()
            .map(|class| OperatorClassWalker { class, db: self.db })
    }

    /// The underlying field.
    ///
    /// ```ignore
    /// // either this
    /// model Test {
    ///   id          Int @id
    ///   name        String
    ///   ^^^^^^^^^^^^^^^^^^
    ///   kind        Int
    ///
    ///   @@index([name])
    /// }
    ///
    /// // or this
    /// type A {
    ///   field String
    ///   ^^^^^^^^^^^^
    /// }
    ///
    /// model Test {
    ///   id Int @id
    ///   a  A
    ///
    ///   @@index([a.field])
    /// }
    /// ```
    pub fn as_index_field(self) -> IndexFieldWalker<'db> {
        match self.args().path.field_in_index() {
            Either::Left(id) => IndexFieldWalker::new(self.db.walk(id)),
            Either::Right(ctid) => IndexFieldWalker::new(self.db.walk(ctid)),
        }
    }

    /// Gives the full path from the current model to the field included in the index.
    /// For example, if the field is through two composite types:
    ///
    /// ```ignore
    /// type A {
    ///   field Int
    /// }
    ///
    /// type B {
    ///   a A
    /// }
    ///
    /// model C {
    ///   id Int @id
    ///   b  B
    ///
    ///   @@index([b.a.field])
    /// }
    /// ```
    ///
    /// The method would return a vector from model to the final field:
    ///
    /// ```ignore
    /// vec![("b", None), ("a", Some("B")), ("field", Some("A"))];
    /// ```
    ///
    /// The first part of the tuple is the name of the field, the second part is
    /// the name of the composite type.
    ///
    /// This method prefers the prisma side naming, and should not be used when
    /// writing to the database.
    pub fn as_path_to_indexed_field(self) -> Vec<(&'db str, Option<&'db str>)> {
        let path = &self.args().path;
        let root_name = self.db.walk(path.root()).name();
        let mut result = vec![(root_name, None)];

        for (ctid, field_id) in path.path() {
            let ct = &self.db.ast[*ctid];
            let field = ct[*field_id].name();

            result.push((field, Some(ct.name())));
        }

        result
    }

    /// Similar to the method [`as_path_to_indexed_field`], but prefers the
    /// mapped names and is to be used when defining indices in the database.
    ///
    /// [`as_path_to_indexed_field`]: struct.ScalarFieldAttributeWalker
    pub fn as_mapped_path_to_indexed_field(self) -> Vec<(&'db str, Option<&'db str>)> {
        let path = &self.args().path;
        let root = {
            let mapped = &self.db.types[path.root()].mapped_name;

            mapped
                .and_then(|id| self.db.interner.get(id))
                .unwrap_or_else(|| self.db.walk(path.root()).name())
        };

        let mut result = vec![(root, None)];

        for (ctid, field_id) in path.path() {
            let ct = &self.db.ast[*ctid];

            let field = &self.db.types.composite_type_fields[&(*ctid, *field_id)]
                .mapped_name
                .and_then(|id| self.db.interner.get(id))
                .unwrap_or_else(|| ct[*field_id].name());

            result.push((field, Some(ct.name())));
        }

        result
    }

    /// The sort order (asc or desc) on the field.
    ///
    /// ```ignore
    /// @@index(name(sort: Desc))
    ///                    ^^^^
    /// ```
    pub fn sort_order(self) -> Option<SortOrder> {
        self.args().sort_order
    }
}