Skip to content

Commit

Permalink
docs: ast
Browse files Browse the repository at this point in the history
  • Loading branch information
apskhem committed Mar 14, 2024
1 parent 11c8820 commit bfbd1c1
Show file tree
Hide file tree
Showing 12 changed files with 86 additions and 62 deletions.
14 changes: 14 additions & 0 deletions src/analyzer/indexer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use alloc::string::{

use super::*;

/// Represents tables and enums within a given schema name.
#[derive(Debug, PartialEq, Clone, Default)]
pub struct IndexedSchemaBlock {
/// Indexed table names and associated columns
Expand All @@ -17,6 +18,8 @@ pub struct IndexedSchemaBlock {
enum_map: BTreeMap<String, BTreeSet<String>>,
}

/// Represents various types of items for indexing identifiers
/// including aliases, schema groupings, and table groups.
#[derive(Debug, PartialEq, Clone, Default)]
pub struct Indexer {
/// Indexed table groups map.
Expand Down Expand Up @@ -522,3 +525,14 @@ impl From<RefBlock> for IndexedRef {
}
}
}

/// Represents indexed meta data during parsing.
#[derive(Debug, PartialEq, Clone, Default)]
pub struct TableIndexer {
/// A list of primary column names (composition applicable).
pub pk_list: Vec<String>,
/// A list of column names with unique constraint (composition applicable).
pub unique_list: Vec<BTreeSet<String>>,
/// A list of indexed column names (composition applicable).
pub indexed_list: Vec<(Vec<String>, Option<IndexesType>)>,
}
32 changes: 6 additions & 26 deletions src/analyzer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ pub fn analyze(schema_block: &SchemaBlock) -> AnalyzerResult<AnalyzedIndexer> {
}
}

// validate table type
// validate table column types
let tables = tables
.into_iter()
.map(|table| {
Expand All @@ -242,11 +242,10 @@ pub fn analyze(schema_block: &SchemaBlock) -> AnalyzerResult<AnalyzedIndexer> {
.map(|col| {
let type_name = col.r#type.type_name;

if type_name == ColumnTypeName::Undef {
unreachable!("undef field type must not appear");
}

let type_name = match type_name {
ColumnTypeName::Undef => {
unreachable!("undef field type must not appear");
}
ColumnTypeName::Raw(raw_type) => {
match ColumnTypeName::from_str(&raw_type) {
Ok(type_name) => {
Expand Down Expand Up @@ -332,16 +331,7 @@ pub fn analyze(schema_block: &SchemaBlock) -> AnalyzerResult<AnalyzedIndexer> {
Value::String(val) => {
let err = Err::InvalidDefaultValue { raw_value: val.clone(), raw_type: col.r#type.raw.clone() };

if !matches!(
type_name,
ColumnTypeName::Bit
| ColumnTypeName::Varbit
| ColumnTypeName::Char
| ColumnTypeName::VarChar
| ColumnTypeName::Enum(_)
) {
throw_err(err.clone(), &span_range, input)?;
}
// TODO: validate which type can be strings

// validate fixed and variable length data type
match type_name {
Expand All @@ -361,17 +351,7 @@ pub fn analyze(schema_block: &SchemaBlock) -> AnalyzerResult<AnalyzedIndexer> {
Value::Integer(val) => {
let err = Err::DataTypeExceeded { raw_type: col.r#type.raw.clone() };

if !matches!(
type_name,
ColumnTypeName::SmallSerial
| ColumnTypeName::Serial
| ColumnTypeName::BigSerial
| ColumnTypeName::SmallInt
| ColumnTypeName::Integer
| ColumnTypeName::BigInt
) {
throw_err(Err::InvalidDefaultValue { raw_value: val.to_string(), raw_type: col.r#type.raw.clone() }, &span_range, input)?;
}
// TODO: validate which type can be numbers

match type_name {
ColumnTypeName::SmallInt
Expand Down
10 changes: 9 additions & 1 deletion src/ast/enums.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,18 @@ use alloc::vec::Vec;

use super::*;

/// Represents a top-level block of enum.
#[derive(Debug, Clone, Default)]
pub struct EnumBlock {
/// The range of the span in the source text.
pub span_range: SpanRange,
/// The identifier of the enum block including schema (optional) and name.
pub ident: EnumIdent,
/// The list of variants of the enums.
pub values: Vec<EnumValue>,
}

/// Represents an enum value or variant.
#[derive(Debug, Clone, Default)]
pub struct EnumValue {
/// The range of the span in the source text.
Expand All @@ -19,6 +23,7 @@ pub struct EnumValue {
pub settings: Option<EnumValueSettings>,
}

/// Represents settings of an enum value or variant.
#[derive(Debug, Clone, Default)]
pub struct EnumValueSettings {
/// The range of the span in the source text.
Expand All @@ -29,10 +34,13 @@ pub struct EnumValueSettings {
pub note: Option<String>,
}

/// Represents an enum identifier including schema (optional) and name.
#[derive(Debug, Clone, Default)]
pub struct EnumIdent {
/// The range of the span in the source text.
pub span_range: SpanRange,
pub name: Ident,
/// The schema of the enum.
pub schema: Option<Ident>,
/// The name of the enum.
pub name: Ident,
}
13 changes: 13 additions & 0 deletions src/ast/indexes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ use core::str::FromStr;

use super::*;

/// Represents an indexes block inside a table block.
/// Indexes allow users to quickly locate and access the data. Users can define single or multi-column indexes.
#[derive(Debug, Clone, Default)]
pub struct IndexesBlock {
/// The range of the span in the source text.
Expand All @@ -12,6 +14,7 @@ pub struct IndexesBlock {
pub defs: Vec<IndexesDef>,
}

/// Represents an indexes definition or each item in an indexes block.
#[derive(Debug, Clone, Default)]
pub struct IndexesDef {
/// The range of the span in the source text.
Expand All @@ -22,10 +25,12 @@ pub struct IndexesDef {
pub settings: Option<IndexesSettings>,
}

/// Represents settings of an indexes definition.
#[derive(Debug, Clone, Default)]
pub struct IndexesSettings {
/// The range of the span in the source text.
pub span_range: SpanRange,
/// A vector of key and optional value pairs representing attributes of the indexes definition.
pub attributes: Vec<Attribute>,
/// A Type of index (btree, gin, gist, hash depending on DB).
pub r#type: Option<IndexesType>,
Expand All @@ -39,17 +44,25 @@ pub struct IndexesSettings {
pub name: Option<String>,
}

/// Represents the type of column for indexing.
#[derive(Debug, Clone)]
pub enum IndexesColumnType {
/// Represents a column name with the given identifier.
String(Ident),
/// Represents an expression with the given literal expression.
Expr(Literal),
}

/// Represents different types of indexes that can be used.
#[derive(Debug, PartialEq, Eq, Clone)]
pub enum IndexesType {
/// Represents a B-tree index.
BTree,
/// Represents a GIN (Generalized Inverted Index) index.
Gin,
/// Represents a GiST (Generalized Search Tree) index.
Gist,
/// Represents a hash index.
Hash,
}

Expand Down
16 changes: 0 additions & 16 deletions src/ast/meta.rs

This file was deleted.

2 changes: 0 additions & 2 deletions src/ast/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ pub(crate) type SpanRange = Range<usize>;

mod enums;
mod indexes;
mod meta;
mod project;
mod refs;
mod schema;
Expand All @@ -14,7 +13,6 @@ use core::ops::Range;

pub use enums::*;
pub use indexes::*;
pub use meta::*;
pub use project::*;
pub use refs::*;
pub use schema::*;
Expand Down
7 changes: 7 additions & 0 deletions src/ast/project.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use core::str::FromStr;

use super::*;

/// Represents different types of databases.
#[derive(Debug, Clone, Default)]
pub enum DatabaseType {
#[default]
Expand All @@ -28,11 +29,17 @@ impl FromStr for DatabaseType {
}
}

/// Represents a project block for grouping various tables.
#[derive(Debug, Clone, Default)]
pub struct ProjectBlock {
/// Range of the span in the source text.
pub span_range: SpanRange,
/// Properties associated with the project block.
pub properties: Vec<Property>,
/// An identifier of the project block.
pub ident: Ident,
/// The database type associated with the project block.
pub database_type: DatabaseType,
/// The note block associated with the project block.
pub note: Option<NoteBlock>,
}
1 change: 1 addition & 0 deletions src/ast/refs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ impl ToString for ReferentialAction {
pub struct RefSettings {
/// The range of the span in the source text.
pub span_range: SpanRange,
/// A vector of key and optional value pairs representing attributes of the ref.
pub attributes: Vec<Attribute>,
pub on_delete: Option<ReferentialAction>,
pub on_update: Option<ReferentialAction>,
Expand Down
2 changes: 1 addition & 1 deletion src/ast/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use alloc::vec::Vec;

use super::*;

/// An entire structure of a parsed DBML file.
/// Represents the entire structure of a parsed DBML file.
#[derive(Debug, Clone, Default)]
pub struct SchemaBlock<'a> {
/// The span range of the entire parsed DBML structure in the source text.
Expand Down
18 changes: 9 additions & 9 deletions src/ast/table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,33 +4,33 @@ use core::str::FromStr;

use super::*;

/// A struct representing a block of table.
/// Represents a block of table.
#[derive(Debug, Clone, Default)]
pub struct TableBlock {
/// The range of the span in the source text.
pub span_range: SpanRange,
/// Columns or fields of the table.
pub cols: Vec<TableColumn>,
/// Identifier for the table.
/// The identifier for the table.
pub ident: TableIdent,
/// The note for the table.
/// The note block associated with the table block.
pub note: Option<NoteBlock>,
/// The indexes block.
pub indexes: Option<IndexesBlock>,
/// The settings for the table.
pub settings: Option<TableSettings>,
}

/// A struct representing settings of the table.
/// Represents settings of the table.
#[derive(Debug, Clone, Default)]
pub struct TableSettings {
/// The range of the span in the source text.
pub span_range: SpanRange,
/// A vector of key-value pairs representing properties of the table.
/// A vector of key and optional value pairs representing attributes of the table.
pub attributes: Vec<Attribute>,
}

/// A struct representing a single column or field of the table.
/// Represents a single column or field of the table.
#[derive(Debug, Clone, Default)]
pub struct TableColumn {
/// The range of the span in the source text.
Expand All @@ -43,7 +43,7 @@ pub struct TableColumn {
pub settings: Option<ColumnSettings>,
}

/// A struct representing details of the table column.
/// Represents details of the table column.
#[derive(Debug, Clone, Default)]
pub struct ColumnType {
/// The range of the span in the source text.
Expand Down Expand Up @@ -164,7 +164,7 @@ impl FromStr for ColumnTypeName {
}
}

/// A struct representing settings of a column.
/// Represents settings of a column.
#[derive(Debug, Clone, Default)]
pub struct ColumnSettings {
/// The range of the span in the source text.
Expand All @@ -187,7 +187,7 @@ pub struct ColumnSettings {
pub refs: Vec<RefInline>,
}

/// A struct representing a table identifier.
/// Represents a table identifier.
#[derive(Debug, Clone, Default)]
pub struct TableIdent {
/// The range of the span in the source text.
Expand Down
12 changes: 6 additions & 6 deletions src/ast/table_group.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,24 @@ use alloc::vec::Vec;

use super::*;

/// A table group allowing to group the related or associated tables together.
/// Represents a table group allowing to group the related or associated tables together.
#[derive(Debug, Clone, Default)]
pub struct TableGroupBlock {
/// The range of the span in the source text.
pub span_range: SpanRange,
/// A name of a table group
/// The name of a table group
pub ident: Ident,
/// A list of tables inside a group.
/// The list of table identifiers inside the group.
pub items: Vec<TableGroupItem>,
}

/// An associated table inside a table group.
/// Represents an associated table identifier listed inside a table group.
#[derive(Debug, Clone, Default)]
pub struct TableGroupItem {
/// The range of the span in the source text.
pub span_range: SpanRange,
/// A Table schema.
/// The table schema.
pub schema: Option<Ident>,
/// A Table name or alias.
/// The table name or alias.
pub ident_alias: Ident,
}
Loading

0 comments on commit bfbd1c1

Please sign in to comment.