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
#![deny(unsafe_code, rust_2018_idioms, missing_docs)]
#![allow(clippy::derive_partial_eq_without_eq)]
//! See the docs on [ParserDatabase](./struct.ParserDatabase.html).
//!
//! ## Scope
//!
//! The ParserDatabase is tasked with gathering information about the schema. It is _connector
//! agnostic_: it gathers information and performs generic validations, leaving connector-specific
//! validations to later phases in datamodel core.
//!
//! ## Terminology
//!
//! Names:
//!
//! - _name_: the item name in the schema for datasources, generators, models, model fields,
//! composite types, composite type fields, enums and enum variants. The `name:` argument for
//! unique constraints, primary keys and relations.
//! - _mapped name_: the name inside an `@map()` or `@@map()` attribute of a model, field, enum or
//! enum value. This is used to determine what the name of the Prisma schema item is in the
//! database.
//! - _database name_: the name in the database, once both the name of the item and the mapped
//! name have been taken into account. The logic is always the same: if a mapped name is defined,
//! then the database name is the mapped name, otherwise it is the name of the item.
//! - _constraint name_: indexes, primary keys and defaults can have a constraint name. It can be
//! defined with a `map:` argument or be a default, generated name if the `map:` argument is not
//! provided. These usually require a datamodel connector to be defined.
pub mod walkers;
mod attributes;
mod coerce_expression;
mod context;
mod interner;
mod names;
mod relations;
mod types;
pub use coerce_expression::{coerce, coerce_array, coerce_opt};
pub use names::is_reserved_type_name;
pub use relations::{ManyToManyRelationId, ReferentialAction, RelationId};
pub use schema_ast::{ast, SourceFile};
pub use types::{
IndexAlgorithm, IndexFieldPath, IndexType, OperatorClass, RelationFieldId, ScalarFieldId, ScalarFieldType,
ScalarType, SortOrder,
};
use self::{context::Context, interner::StringId, relations::Relations, types::Types};
use diagnostics::{DatamodelError, Diagnostics};
use names::Names;
/// ParserDatabase is a container for a Schema AST, together with information
/// gathered during schema validation. Each validation step enriches the
/// database with information that can be used to work with the schema, without
/// changing the AST. Instantiating with `ParserDatabase::new()` will perform a
/// number of validations and make sure the schema makes sense, but it cannot
/// fail. In case the schema is invalid, diagnostics will be created and the
/// resolved information will be incomplete.
///
/// Validations are carried out in the following order:
///
/// - The AST is walked a first time to resolve names: to each relevant
/// identifier, we attach an ID that can be used to reference the
/// corresponding item (model, enum, field, ...)
/// - The AST is walked a second time to resolve types. For each field and each
/// type alias, we look at the type identifier and resolve what it refers to.
/// - The AST is walked a third time to validate attributes on models and
/// fields.
/// - Global validations are then performed on the mostly validated schema.
/// Currently only index name collisions.
pub struct ParserDatabase {
ast: ast::SchemaAst,
file: schema_ast::SourceFile,
interner: interner::StringInterner,
names: Names,
types: Types,
relations: Relations,
}
impl ParserDatabase {
/// See the docs on [ParserDatabase](/struct.ParserDatabase.html).
pub fn new(file: schema_ast::SourceFile, diagnostics: &mut Diagnostics) -> Self {
let ast = schema_ast::parse_schema(file.as_str(), diagnostics);
let mut interner = Default::default();
let mut names = Default::default();
let mut types = Default::default();
let mut relations = Default::default();
let mut ctx = Context::new(&ast, &mut interner, &mut names, &mut types, &mut relations, diagnostics);
// First pass: resolve names.
names::resolve_names(&mut ctx);
// Return early on name resolution errors.
if ctx.diagnostics.has_errors() {
attributes::create_default_attributes(&mut ctx);
return ParserDatabase {
ast,
file,
interner,
names,
types,
relations,
};
}
// Second pass: resolve top-level items and field types.
types::resolve_types(&mut ctx);
// Return early on type resolution errors.
if ctx.diagnostics.has_errors() {
attributes::create_default_attributes(&mut ctx);
return ParserDatabase {
ast,
file,
interner,
names,
types,
relations,
};
}
// Third pass: validate model and field attributes. All these
// validations should be _order independent_ and only rely on
// information from previous steps, not from other attributes.
attributes::resolve_attributes(&mut ctx);
// Fourth step: relation inference
relations::infer_relations(&mut ctx);
ParserDatabase {
ast,
file,
interner,
names,
types,
relations,
}
}
/// The parsed AST.
pub fn ast(&self) -> &ast::SchemaAst {
&self.ast
}
/// The total number of enums in the schema. This is O(1).
pub fn enums_count(&self) -> usize {
self.types.enum_attributes.len()
}
/// The total number of models in the schema. This is O(1).
pub fn models_count(&self) -> usize {
self.types.model_attributes.len()
}
/// The source file contents.
pub fn source(&self) -> &str {
self.file.as_str()
}
}
impl std::fmt::Debug for ParserDatabase {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str("ParserDatabase { ... }")
}
}
impl std::ops::Index<StringId> for ParserDatabase {
type Output = str;
fn index(&self, index: StringId) -> &Self::Output {
self.interner.get(index).unwrap()
}
}