use selene_core::DbString;
use crate::ast::{expr::ValueExpr, span::SourceSpan, types::GqlType};
#[derive(Clone, Debug, PartialEq, serde::Deserialize, serde::Serialize)]
#[non_exhaustive]
pub enum DdlStatement {
CreateGraph {
name: DbString,
or_replace: bool,
if_not_exists: bool,
span: SourceSpan,
},
DropGraph {
name: DbString,
if_exists: bool,
span: SourceSpan,
},
CreateNodeType {
label: DbString,
key_label_set: Option<KeyLabelSet>,
or_replace: bool,
if_not_exists: bool,
extends: Option<DbString>,
properties: Vec<TypePropertyDef>,
validation_mode: Option<ValidationMode>,
span: SourceSpan,
},
CreateEdgeType {
label: DbString,
key_label_set: Option<KeyLabelSet>,
or_replace: bool,
if_not_exists: bool,
extends: Option<DbString>,
endpoints: Option<EdgeEndpointSpec>,
properties: Vec<TypePropertyDef>,
validation_mode: Option<ValidationMode>,
span: SourceSpan,
},
DropNodeType {
label: DbString,
if_exists: bool,
behavior: DropBehavior,
span: SourceSpan,
},
DropEdgeType {
label: DbString,
if_exists: bool,
behavior: DropBehavior,
span: SourceSpan,
},
TruncateNodeType {
label: DbString,
span: SourceSpan,
},
TruncateEdgeType {
label: DbString,
span: SourceSpan,
},
CreateIndex {
name: DbString,
label: DbString,
properties: Vec<DbString>,
if_not_exists: bool,
span: SourceSpan,
},
DropIndex {
name: DbString,
if_exists: bool,
span: SourceSpan,
},
ShowNodeTypes(SourceSpan),
ShowEdgeTypes(SourceSpan),
ShowIndexes(SourceSpan),
ShowProcedures(SourceSpan),
}
impl DdlStatement {
#[must_use]
pub const fn span(&self) -> SourceSpan {
match self {
Self::CreateGraph { span, .. }
| Self::DropGraph { span, .. }
| Self::CreateNodeType { span, .. }
| Self::CreateEdgeType { span, .. }
| Self::DropNodeType { span, .. }
| Self::DropEdgeType { span, .. }
| Self::TruncateNodeType { span, .. }
| Self::TruncateEdgeType { span, .. }
| Self::CreateIndex { span, .. }
| Self::DropIndex { span, .. }
| Self::ShowNodeTypes(span)
| Self::ShowEdgeTypes(span)
| Self::ShowIndexes(span)
| Self::ShowProcedures(span) => *span,
}
}
}
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq, serde::Deserialize, serde::Serialize)]
pub enum DropBehavior {
Restrict,
Cascade,
}
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq, serde::Deserialize, serde::Serialize)]
pub enum ValidationMode {
Strict,
Warn,
}
#[derive(Clone, Debug, Eq, PartialEq, serde::Deserialize, serde::Serialize)]
pub struct KeyLabelSet {
pub labels: Vec<DbString>,
pub implied_labels: Vec<DbString>,
pub span: SourceSpan,
}
#[derive(Clone, Debug, Eq, PartialEq, serde::Deserialize, serde::Serialize)]
pub struct EdgeEndpointSpec {
pub from_labels: Vec<DbString>,
pub to_labels: Vec<DbString>,
pub span: SourceSpan,
}
#[derive(Clone, Debug, PartialEq, serde::Deserialize, serde::Serialize)]
pub struct TypePropertyDef {
pub name: DbString,
pub gql_type: GqlType,
pub constraints: Vec<TypePropertyConstraint>,
pub span: SourceSpan,
}
#[derive(Clone, Debug, PartialEq, serde::Deserialize, serde::Serialize)]
#[non_exhaustive]
pub enum TypePropertyConstraint {
NotNull(SourceSpan),
Default(ValueExpr, SourceSpan),
Immutable(SourceSpan),
Unique(SourceSpan),
Indexed {
name: Option<DbString>,
span: SourceSpan,
},
}
impl TypePropertyConstraint {
#[must_use]
pub const fn span(&self) -> SourceSpan {
match self {
Self::NotNull(span)
| Self::Default(_, span)
| Self::Immutable(span)
| Self::Unique(span) => *span,
Self::Indexed { span, .. } => *span,
}
}
}