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
//! The interface implemented by connectors for Prisma schema validation and interpretation.

/// Connector capabilities
pub mod capabilities;
/// Constraint name defaults.
pub mod constraint_names;
/// Extensions for parser database walkers with context from the connector.
pub mod walker_ext_traits;

/// Connector completions
pub mod completions;

mod empty_connector;
mod filters;
mod native_types;
mod relation_mode;

pub use self::{
    capabilities::{ConnectorCapabilities, ConnectorCapability},
    completions::format_completion_docs,
    empty_connector::EmptyDatamodelConnector,
    filters::*,
    native_types::{NativeTypeArguments, NativeTypeConstructor, NativeTypeInstance},
    relation_mode::RelationMode,
};

use crate::{configuration::DatasourceConnectorData, Configuration, Datasource, PreviewFeature};
use chrono::{DateTime, FixedOffset};
use diagnostics::{DatamodelError, Diagnostics, NativeTypeErrorFactory, Span};
use enumflags2::BitFlags;
use lsp_types::CompletionList;
use parser_database::{
    ast::{self, SchemaPosition},
    walkers, IndexAlgorithm, ParserDatabase, ReferentialAction, ScalarType,
};
use std::{borrow::Cow, collections::HashMap, str::FromStr};

pub const EXTENSIONS_KEY: &str = "extensions";

/// The datamodel connector API.
pub trait Connector: Send + Sync {
    /// The name of the provider, for string comparisons determining which connector we are on.
    fn provider_name(&self) -> &'static str;

    /// Must return true whenever the passed in provider name is a match.
    fn is_provider(&self, name: &str) -> bool {
        name == self.provider_name()
    }

    /// The database flavour, divergences in database backends capabilities might consider
    /// us to use a different flavour, like in the case of CockroachDB. However other databases
    /// are less divergent as to consider sharing a flavour with others, like Planetscale and MySQL
    /// or Neon and Postgres, which respectively have the Mysql and Postgres flavours.
    fn flavour(&self) -> Flavour;

    /// The name of the connector. Can be used in error messages.
    fn name(&self) -> &str;

    /// The static list of capabilities for the connector.
    fn capabilities(&self) -> ConnectorCapabilities;

    /// Does the connector have this capability?
    fn has_capability(&self, capability: ConnectorCapability) -> bool {
        self.capabilities().contains(capability)
    }

    /// The maximum length of constraint names in bytes. Connectors without a
    /// limit should return usize::MAX.
    fn max_identifier_length(&self) -> usize;

    // Relation mode

    /// The relation modes that can be set through the relationMode datasource
    /// argument.
    fn allowed_relation_mode_settings(&self) -> BitFlags<RelationMode> {
        use RelationMode::*;

        ForeignKeys | Prisma
    }

    /// The default relation mode to assume for this connector.
    fn default_relation_mode(&self) -> RelationMode {
        RelationMode::ForeignKeys
    }

    /// The referential actions supported by the connector.
    fn referential_actions(&self) -> BitFlags<ReferentialAction>;

    /// The referential actions supported when using relationMode = "prisma" by the connector.
    /// There are in fact scenarios in which the set of emulated referential actions supported may change
    /// depending on the connector. For example, Postgres' NoAction mode behaves similarly to Restrict
    /// (raising an error if any referencing rows still exist when the constraint is checked), but with
    /// a subtle twist we decided not to emulate: NO ACTION allows the check to be deferred until later
    /// in the transaction, whereas RESTRICT does not.
    fn emulated_referential_actions(&self) -> BitFlags<ReferentialAction> {
        RelationMode::allowed_emulated_referential_actions_default()
    }

    /// Most SQL databases reject table definitions with a SET NULL referential action referencing a non-nullable field,
    /// but that's not true for all of them.
    /// This was introduced because Postgres accepts data definition language statements with the SET NULL
    /// referential action referencing non-nullable fields, although this would lead to a runtime error once
    /// the action is actually triggered.
    fn allows_set_null_referential_action_on_non_nullable_fields(&self, _relation_mode: RelationMode) -> bool {
        false
    }

    fn supports_composite_types(&self) -> bool {
        self.has_capability(ConnectorCapability::CompositeTypes)
    }

    fn supports_named_primary_keys(&self) -> bool {
        self.has_capability(ConnectorCapability::NamedPrimaryKeys)
    }

    fn supports_named_foreign_keys(&self) -> bool {
        self.has_capability(ConnectorCapability::NamedForeignKeys)
    }

    fn supports_named_default_values(&self) -> bool {
        self.has_capability(ConnectorCapability::NamedDefaultValues)
    }

    fn supports_referential_action(&self, relation_mode: &RelationMode, action: ReferentialAction) -> bool {
        match relation_mode {
            RelationMode::ForeignKeys => self.referential_actions().contains(action),
            RelationMode::Prisma => self.emulated_referential_actions().contains(action),
        }
    }

    /// This is used by the query engine schema builder.
    ///
    /// For a given scalar type + native type combination, this method should return the name to be
    /// given to the filter input objects for the type. The significance of that name is that the
    /// resulting input objects will be cached by name, so for a given filter input object name,
    /// the filters should always be identical.
    fn scalar_filter_name(&self, scalar_type_name: String, _native_type_name: Option<&str>) -> Cow<'_, str> {
        Cow::Owned(scalar_type_name)
    }

    /// This is used by the query engine schema builder. It is only called for filters of String
    /// fields and aggregates.
    ///
    /// For a given filter input object type name returned by `scalar_filter_name`, it should
    /// return the string operations to be made available in the Client API.
    ///
    /// Implementations of this method _must_ always associate the same filters to the same input
    /// object type name. This is because the filter types are cached by name, so if different
    /// calls to the method return different filters, only the first return value will be used.
    fn string_filters(&self, input_object_name: &str) -> BitFlags<StringFilter> {
        match input_object_name {
            "String" => BitFlags::all(), // all the filters are available by default
            _ => panic!("Unexpected scalar input object name for string filters: `{input_object_name}`"),
        }
    }

    /// Validate that the arguments passed to a native type attribute are valid.
    fn validate_native_type_arguments(
        &self,
        _native_type: &NativeTypeInstance,
        _scalar_type: &ScalarType,
        _span: Span,
        _: &mut Diagnostics,
    ) {
    }

    fn validate_enum(&self, _enum: walkers::EnumWalker<'_>, _: &mut Diagnostics) {}
    fn validate_model(&self, _model: walkers::ModelWalker<'_>, _: RelationMode, _: &mut Diagnostics) {}
    fn validate_relation_field(&self, _field: walkers::RelationFieldWalker<'_>, _: &mut Diagnostics) {}
    fn validate_datasource(&self, _: BitFlags<PreviewFeature>, _: &Datasource, _: &mut Diagnostics) {}

    fn validate_scalar_field_unknown_default_functions(
        &self,
        db: &parser_database::ParserDatabase,
        diagnostics: &mut Diagnostics,
    ) {
        for d in db.walk_scalar_field_defaults_with_unknown_function() {
            let (func_name, _, span) = d.value().as_function().unwrap();
            diagnostics.push_error(DatamodelError::new_default_unknown_function(func_name, span));
        }
    }

    /// The scopes in which a constraint name should be validated. If empty, doesn't check for name
    /// clashes in the validation phase.
    fn constraint_violation_scopes(&self) -> &'static [ConstraintScope] {
        &[]
    }

    /// Returns all available native type constructors available through this connector.
    /// Powers the auto completion of the VSCode plugin.
    fn available_native_type_constructors(&self) -> &'static [NativeTypeConstructor];

    /// Returns the default scalar type for the given native type
    fn scalar_type_for_native_type(&self, native_type: &NativeTypeInstance) -> ScalarType;

    /// On each connector, each built-in Prisma scalar type (`Boolean`,
    /// `String`, `Float`, etc.) has a corresponding native type.
    fn default_native_type_for_scalar_type(&self, scalar_type: &ScalarType) -> Option<NativeTypeInstance>;

    /// Same mapping as `default_native_type_for_scalar_type()`, but in the opposite direction.
    fn native_type_is_default_for_scalar_type(
        &self,
        native_type: &NativeTypeInstance,
        scalar_type: &ScalarType,
    ) -> bool;

    /// Debug/error representation of a native type.
    fn native_type_to_parts(&self, native_type: &NativeTypeInstance) -> (&'static str, Vec<String>);

    fn find_native_type_constructor(&self, name: &str) -> Option<&NativeTypeConstructor> {
        self.available_native_type_constructors()
            .iter()
            .find(|constructor| constructor.name == name)
    }

    /// This function is used during Schema parsing to calculate the concrete native type.
    fn parse_native_type(
        &self,
        name: &str,
        args: &[String],
        span: Span,
        diagnostics: &mut Diagnostics,
    ) -> Option<NativeTypeInstance>;

    fn supports_scalar_lists(&self) -> bool {
        self.has_capability(ConnectorCapability::ScalarLists)
    }

    fn supports_enums(&self) -> bool {
        self.has_capability(ConnectorCapability::Enums)
    }

    fn supports_json(&self) -> bool {
        self.has_capability(ConnectorCapability::Json)
    }

    fn supports_json_lists(&self) -> bool {
        self.has_capability(ConnectorCapability::JsonLists)
    }

    fn supports_auto_increment(&self) -> bool {
        self.has_capability(ConnectorCapability::AutoIncrement)
    }

    fn supports_non_id_auto_increment(&self) -> bool {
        self.has_capability(ConnectorCapability::AutoIncrementAllowedOnNonId)
    }

    fn supports_multiple_auto_increment(&self) -> bool {
        self.has_capability(ConnectorCapability::AutoIncrementMultipleAllowed)
    }

    fn supports_non_indexed_auto_increment(&self) -> bool {
        self.has_capability(ConnectorCapability::AutoIncrementNonIndexedAllowed)
    }

    fn supports_compound_ids(&self) -> bool {
        self.has_capability(ConnectorCapability::CompoundIds)
    }

    fn supports_decimal(&self) -> bool {
        self.has_capability(ConnectorCapability::DecimalType)
    }

    fn supported_index_types(&self) -> BitFlags<IndexAlgorithm> {
        IndexAlgorithm::BTree.into()
    }

    fn supports_index_type(&self, algo: IndexAlgorithm) -> bool {
        self.supported_index_types().contains(algo)
    }

    fn allows_relation_fields_in_arbitrary_order(&self) -> bool {
        self.has_capability(ConnectorCapability::RelationFieldsInArbitraryOrder)
    }

    /// If true, the schema validator function checks whether the referencing fields in a `@relation` attribute
    /// are included in an index.
    fn should_suggest_missing_referencing_fields_indexes(&self) -> bool {
        true
    }

    fn native_type_to_string(&self, instance: &NativeTypeInstance) -> String {
        let (name, args) = self.native_type_to_parts(instance);
        let args = if args.is_empty() {
            String::new()
        } else {
            format!("({})", args.join(","))
        };
        format!("{name}{args}")
    }

    fn native_instance_error(&self, instance: &NativeTypeInstance) -> NativeTypeErrorFactory {
        NativeTypeErrorFactory::new(self.native_type_to_string(instance), self.name().to_owned())
    }

    fn validate_url(&self, url: &str) -> Result<(), String>;

    fn datamodel_completions(
        &self,
        _db: &ParserDatabase,
        _position: SchemaPosition<'_>,
        _completions: &mut CompletionList,
    ) {
    }

    fn datasource_completions(&self, _config: &Configuration, _completion_list: &mut CompletionList) {}

    fn parse_datasource_properties(
        &self,
        _args: &mut HashMap<&str, (Span, &ast::Expression)>,
        _diagnostics: &mut Diagnostics,
    ) -> DatasourceConnectorData {
        Default::default()
    }

    fn parse_json_datetime(
        &self,
        _str: &str,
        _nt: Option<NativeTypeInstance>,
    ) -> chrono::ParseResult<DateTime<FixedOffset>> {
        unreachable!("This method is only implemented on connectors with lateral join support.")
    }

    fn parse_json_bytes(
        &self,
        _str: &str,
        _nt: Option<NativeTypeInstance>,
    ) -> prisma_value::PrismaValueResult<Vec<u8>> {
        unreachable!("This method is only implemented on connectors with lateral join support.")
    }
}

#[derive(Copy, Clone, Debug, PartialEq)]
pub enum Flavour {
    Cockroach,
    Mongo,
    Sqlserver,
    Mysql,
    Postgres,
    Sqlite,
}

impl FromStr for Flavour {
    type Err = String;
    fn from_str(s: &str) -> Result<Self, Self::Err> {
        match s.to_lowercase().as_str() {
            "mysql" => Ok(Self::Mysql),
            "postgres" => Ok(Self::Postgres),
            "cockroachdb" => Ok(Self::Cockroach),
            "mssql" => Ok(Self::Sqlserver),
            "sqlite" => Ok(Self::Sqlite),
            _ => Err(format!("Unknown flavour: {}", s)),
        }
    }
}

#[derive(Debug, Clone, Copy, Eq, PartialEq, Ord, PartialOrd)]
pub enum ConstraintType {
    PrimaryKey,
    ForeignKey,
    KeyOrIdx,
    Default,
}

/// A scope where a constraint name must be unique.
#[derive(Debug, Eq, PartialEq, Ord, PartialOrd, Hash, Clone, Copy)]
pub enum ConstraintScope {
    /// Globally indices and unique constraints
    GlobalKeyIndex,
    /// Globally foreign keys
    GlobalForeignKey,
    /// Globally primary keys, indices and unique constraints
    GlobalPrimaryKeyKeyIndex,
    /// Globally primary keys, foreign keys and default constraints
    GlobalPrimaryKeyForeignKeyDefault,
    /// Per model indices and unique constraints
    ModelKeyIndex,
    /// Per model primary keys, indices and unique constraints
    ModelPrimaryKeyKeyIndex,
    /// Per model primary keys, foreign keys, indices and unique constraints
    ModelPrimaryKeyKeyIndexForeignKey,
}

impl ConstraintScope {
    /// A beefed-up display for errors.
    pub fn description(self, model_name: &str) -> Cow<'static, str> {
        match self {
            ConstraintScope::GlobalKeyIndex => Cow::from("global for indexes and unique constraints"),
            ConstraintScope::GlobalForeignKey => Cow::from("global for foreign keys"),
            ConstraintScope::GlobalPrimaryKeyKeyIndex => {
                Cow::from("global for primary key, indexes and unique constraints")
            }
            ConstraintScope::GlobalPrimaryKeyForeignKeyDefault => {
                Cow::from("global for primary keys, foreign keys and default constraints")
            }
            ConstraintScope::ModelKeyIndex => {
                Cow::from(format!("on model `{model_name}` for indexes and unique constraints"))
            }
            ConstraintScope::ModelPrimaryKeyKeyIndex => Cow::from(format!(
                "on model `{model_name}` for primary key, indexes and unique constraints"
            )),
            ConstraintScope::ModelPrimaryKeyKeyIndexForeignKey => Cow::from(format!(
                "on model `{model_name}` for primary key, indexes, unique constraints and foreign keys"
            )),
        }
    }
}