Skip to content

Commit

Permalink
Optimize by capability
Browse files Browse the repository at this point in the history
  • Loading branch information
Sergey Tatarintsev committed Feb 15, 2024
1 parent 1eb96ff commit 5394ce7
Show file tree
Hide file tree
Showing 27 changed files with 110 additions and 191 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions psl/psl-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,4 @@ hex = "0.4"
# For the connector API.
lsp-types = "0.91.1"
url.workspace = true
cfg-if = "1.0.0"
21 changes: 21 additions & 0 deletions psl/psl-core/src/builtin_connectors/has_capability.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
use crate::datamodel_connector::{Connector, ConnectorCapability};

cfg_if::cfg_if! {
if #[cfg(all(feature="mysql", not(any(feature = "postgresql", feature="sqlite", feature = "cockroachdb", feature="mssql", feature="mongodb"))))] {
pub fn has_capability(_: &dyn Connector, capability: ConnectorCapability) -> bool {
super::mysql_datamodel_connector::CAPABILITIES.contains(capability)
}
} else if #[cfg(all(feature="sqlite", not(any(feature = "postgresql", feature="mysql", feature = "cockroachdb", feature="mssql", feature="mongodb"))))] {
pub fn has_capability(_: &dyn Connector, capability: ConnectorCapability) -> bool {
super::sqlite_datamodel_connector::CAPABILITIES.contains(capability)
}
} else if #[cfg(all(feature="postgresql", not(any(feature = "sqlite", feature="mysql", feature = "cockroachdb", feature="mssql", feature="mongodb"))))] {
pub fn has_capability(_: &dyn Connector, capability: ConnectorCapability) -> bool {
super::postgres_datamodel_connector::CAPABILITIES.contains(capability)
}
} else {
pub fn has_capability(connector: &dyn Connector, capability: ConnectorCapability) -> bool {
connector.capabilities().contains(capability)
}
}
}
2 changes: 2 additions & 0 deletions psl/psl-core/src/builtin_connectors/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ pub use mysql_datamodel_connector::MySqlType;
#[cfg(feature = "postgresql")]
pub use postgres_datamodel_connector::{PostgresDatasourceProperties, PostgresType};

mod has_capability;
#[cfg(feature = "mongodb")]
mod mongodb;
#[cfg(feature = "mssql")]
Expand All @@ -25,6 +26,7 @@ mod postgres_datamodel_connector;
#[cfg(feature = "sqlite")]
mod sqlite_datamodel_connector;
mod utils;
pub use has_capability::has_capability;

use crate::{datamodel_connector::Connector, ConnectorRegistry};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ const TEXT_TYPE_NAME: &str = "Text";
const MEDIUM_TEXT_TYPE_NAME: &str = "MediumText";
const LONG_TEXT_TYPE_NAME: &str = "LongText";

const CAPABILITIES: ConnectorCapabilities = enumflags2::make_bitflags!(ConnectorCapability::{
pub const CAPABILITIES: ConnectorCapabilities = enumflags2::make_bitflags!(ConnectorCapability::{
Enums |
EnumArrayPush |
Json |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ const CONSTRAINT_SCOPES: &[ConstraintScope] = &[
ConstraintScope::ModelPrimaryKeyKeyIndexForeignKey,
];

const CAPABILITIES: ConnectorCapabilities = enumflags2::make_bitflags!(ConnectorCapability::{
pub const CAPABILITIES: ConnectorCapabilities = enumflags2::make_bitflags!(ConnectorCapability::{
AdvancedJsonNullability |
AnyId |
AutoIncrement |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use enumflags2::BitFlags;

const NATIVE_TYPE_CONSTRUCTORS: &[NativeTypeConstructor] = &[];
const CONSTRAINT_SCOPES: &[ConstraintScope] = &[ConstraintScope::GlobalKeyIndex];
const CAPABILITIES: ConnectorCapabilities = enumflags2::make_bitflags!(ConnectorCapability::{
pub const CAPABILITIES: ConnectorCapabilities = enumflags2::make_bitflags!(ConnectorCapability::{
AnyId |
AutoIncrement |
CompoundIds |
Expand Down
66 changes: 0 additions & 66 deletions psl/psl-core/src/datamodel_connector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,6 @@ pub trait Connector: Send + Sync {
/// 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;
Expand Down Expand Up @@ -105,22 +100,6 @@ pub trait Connector: Send + Sync {
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),
Expand Down Expand Up @@ -222,46 +201,6 @@ pub trait Connector: Send + Sync {
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()
}
Expand All @@ -270,11 +209,6 @@ pub trait Connector: Send + Sync {
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
Expand Down
11 changes: 8 additions & 3 deletions psl/psl-core/src/datamodel_connector/walker_ext_traits.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
use crate::datamodel_connector::{
constraint_names::ConstraintNames, Connector, NativeTypeInstance, ReferentialAction, RelationMode,
use crate::{
builtin_connectors::has_capability,
datamodel_connector::{
constraint_names::ConstraintNames, Connector, NativeTypeInstance, ReferentialAction, RelationMode,
},
};
use parser_database::{
ast::{self, WithSpan},
walkers::*,
};
use std::borrow::Cow;

use super::ConnectorCapability;

pub trait IndexWalkerExt<'db> {
fn constraint_name(self, connector: &dyn Connector) -> Cow<'db, str>;
}
Expand Down Expand Up @@ -60,7 +65,7 @@ pub trait PrimaryKeyWalkerExt<'db> {

impl<'db> PrimaryKeyWalkerExt<'db> for PrimaryKeyWalker<'db> {
fn constraint_name(self, connector: &dyn Connector) -> Option<Cow<'db, str>> {
if !connector.supports_named_primary_keys() {
if !has_capability(connector, ConnectorCapability::NamedPrimaryKeys) {
return None;
}

Expand Down
7 changes: 6 additions & 1 deletion psl/psl-core/src/validate/validation_pipeline/context.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::{
datamodel_connector::{Connector, RelationMode},
builtin_connectors::has_capability,
datamodel_connector::{Connector, ConnectorCapability, RelationMode},
Datasource, PreviewFeature,
};
use diagnostics::{DatamodelError, DatamodelWarning, Diagnostics};
Expand Down Expand Up @@ -29,4 +30,8 @@ impl Context<'_> {
pub(super) fn push_warning(&mut self, warning: DatamodelWarning) {
self.diagnostics.push_warning(warning);
}

pub(super) fn has_capability(&self, capability: ConnectorCapability) -> bool {
has_capability(self.connector, capability)
}
}
4 changes: 3 additions & 1 deletion psl/psl-core/src/validate/validation_pipeline/validations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ mod relation_fields;
mod relations;
mod views;

use crate::datamodel_connector::ConnectorCapability;

use super::context::Context;
use names::Names;
use parser_database::walkers::RefinedRelationWalker;
Expand Down Expand Up @@ -134,7 +136,7 @@ pub(super) fn validate(ctx: &mut Context<'_>) {
}
}

if ctx.connector.supports_enums() {
if ctx.has_capability(ConnectorCapability::Enums) {
enums::database_name_clashes(ctx);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::{
datamodel_connector::ConnectorCapability,
diagnostics::DatamodelError,
parser_database::{ast::WithSpan, walkers::ModelWalker},
validate::validation_pipeline::context::Context,
Expand All @@ -12,7 +13,7 @@ pub(super) fn validate_auto_increment(model: ModelWalker<'_>, ctx: &mut Context<
}

// First check if the provider supports autoincrement at all. If yes, proceed with the detailed checks.
if !ctx.connector.supports_auto_increment() {
if !ctx.has_capability(ConnectorCapability::AutoIncrement) {
for field in autoincrement_fields() {
let msg = "The `autoincrement()` default value is used with a datasource that does not support it.";

Expand All @@ -27,7 +28,7 @@ pub(super) fn validate_auto_increment(model: ModelWalker<'_>, ctx: &mut Context<
return;
}

if !ctx.connector.supports_multiple_auto_increment() && autoincrement_fields().count() > 1 {
if !ctx.has_capability(ConnectorCapability::AutoIncrementMultipleAllowed) && autoincrement_fields().count() > 1 {
let msg = "The `autoincrement()` default value is used multiple times on this model even though the underlying datasource only supports one instance per table.";

ctx.push_error(DatamodelError::new_attribute_validation_error(
Expand All @@ -41,7 +42,9 @@ pub(super) fn validate_auto_increment(model: ModelWalker<'_>, ctx: &mut Context<
for field in autoincrement_fields() {
let field_is_indexed = || model.field_is_indexed_for_autoincrement(field.field_id());

if !ctx.connector.supports_non_id_auto_increment() && !model.field_is_single_pk(field.field_id()) {
if !ctx.has_capability(ConnectorCapability::AutoIncrementAllowedOnNonId)
&& !model.field_is_single_pk(field.field_id())
{
let msg = "The `autoincrement()` default value is used on a non-id field even though the datasource does not support this.";

ctx.push_error(DatamodelError::new_attribute_validation_error(
Expand All @@ -51,7 +54,7 @@ pub(super) fn validate_auto_increment(model: ModelWalker<'_>, ctx: &mut Context<
))
}

if !ctx.connector.supports_non_indexed_auto_increment() && !field_is_indexed() {
if !ctx.has_capability(ConnectorCapability::AutoIncrementNonIndexedAllowed) && !field_is_indexed() {
let msg = "The `autoincrement()` default value is used on a non-indexed field even though the datasource does not support this.";

ctx.push_error(DatamodelError::new_attribute_validation_error(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use super::default_value;
use crate::validate::validation_pipeline::context::Context;
use crate::{datamodel_connector::ConnectorCapability, validate::validation_pipeline::context::Context};
use diagnostics::DatamodelError;
use parser_database::{
ast::{self, WithSpan},
Expand Down Expand Up @@ -79,7 +79,7 @@ pub(super) fn detect_composite_cycles(ctx: &mut Context<'_>) {

/// Does the connector support composite types.
pub(crate) fn composite_types_support(composite_type: CompositeTypeWalker<'_>, ctx: &mut Context<'_>) {
if ctx.connector.supports_composite_types() {
if ctx.has_capability(ConnectorCapability::CompositeTypes) {
return;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,7 @@ pub(super) fn schemas_property_with_no_connector_support(datasource: &Datasource
return;
}

if ctx
.connector
.has_capability(crate::datamodel_connector::ConnectorCapability::MultiSchema)
{
if ctx.has_capability(crate::datamodel_connector::ConnectorCapability::MultiSchema) {
return;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use schema_ast::ast::{self, Expression};

/// Function `auto()` works for now only with MongoDB.
pub(super) fn validate_auto_param(default_value: Option<&ast::Expression>, ctx: &mut Context<'_>) {
if ctx.connector.has_capability(ConnectorCapability::DefaultValueAuto) {
if ctx.has_capability(ConnectorCapability::DefaultValueAuto) {
return;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ pub(super) fn schema_is_defined_in_the_datasource(r#enum: EnumWalker<'_>, ctx: &
return;
}

if !ctx.connector.has_capability(ConnectorCapability::MultiSchema) {
if !ctx.has_capability(ConnectorCapability::MultiSchema) {
return;
}

Expand Down Expand Up @@ -52,7 +52,7 @@ pub(super) fn schema_attribute_supported_in_connector(r#enum: EnumWalker<'_>, ct
return;
}

if ctx.connector.has_capability(ConnectorCapability::MultiSchema) {
if ctx.has_capability(ConnectorCapability::MultiSchema) {
return;
}

Expand All @@ -72,7 +72,7 @@ pub(super) fn schema_attribute_missing(r#enum: EnumWalker<'_>, ctx: &mut Context
return;
}

if !ctx.connector.has_capability(ConnectorCapability::MultiSchema) {
if !ctx.has_capability(ConnectorCapability::MultiSchema) {
return;
}

Expand Down Expand Up @@ -113,7 +113,7 @@ pub(super) fn multischema_feature_flag_needed(r#enum: EnumWalker<'_>, ctx: &mut
}

pub(crate) fn connector_supports_enums(r#enum: EnumWalker<'_>, ctx: &mut Context<'_>) {
if ctx.connector.supports_enums() {
if ctx.has_capability(ConnectorCapability::Enums) {
return;
}

Expand Down
Loading

0 comments on commit 5394ce7

Please sign in to comment.