Skip to content

Commit

Permalink
feat: support unspecified database type
Browse files Browse the repository at this point in the history
  • Loading branch information
apskhem committed Jan 3, 2025
1 parent 7d3b0db commit 54e3c82
Show file tree
Hide file tree
Showing 10 changed files with 28 additions and 28 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
# DBML parser for Rust
# A DBML parser for Rust

[![crate](https://img.shields.io/crates/v/dbml-rs.svg)](https://crates.io/crates/dbml-rs)
![MSRV](https://img.shields.io/badge/rustc-1.61+-ab6000.svg)
![MIT or Apache 2.0 licensed](https://img.shields.io/crates/l/dbml-rs.svg)
![unsafe forbidden](https://img.shields.io/badge/unsafe-forbidden-success.svg)

DBML parser implemented in Rust programming language.
A DBML parser implemented in Rust programming language.

## How to use it?

Expand Down
10 changes: 6 additions & 4 deletions src/analyzer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,15 @@ mod indexer;
use helper::*;
use indexer::*;

#[derive(Debug, Clone, Default)]
/// Represents a struct of indexing information after analyzing.
#[derive(Debug, Clone)]
pub struct AnalyzedIndexer {
pub indexed_refs: Vec<IndexedRef>,
pub indexer: Indexer,
}

/// Represents a reference to a table, indicating relationships between tables.
#[derive(Debug, Clone)]
pub struct TableRef {
/// References that this table points to, such as foreign keys in its fields.
pub ref_to: Vec<IndexedRef>,
Expand Down Expand Up @@ -67,17 +69,17 @@ pub struct TableRef {
/// ```
pub fn analyze(schema_block: &SchemaBlock) -> AnalyzerResult<AnalyzedIndexer> {
let input = schema_block.input;
let project = schema_block.project();
let projects = schema_block.projects();
let tables = schema_block.tables();
let table_groups = schema_block.table_groups();
let refs = schema_block.refs();
let enums = schema_block.enums();

// check project block
if project.len() > 1 {
if projects.len() > 1 {
throw_err(Err::DuplicateProjectSetting, &schema_block.span_range, input)?;
}
match project.first() {
match projects.first() {
Some(project_block) => {
check_prop_duplicate_keys(&project_block.properties, input)?;
}
Expand Down
2 changes: 2 additions & 0 deletions src/ast/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//! Collections of AST Nodes.
pub(crate) type SpanRange = Range<usize>;

mod enums;
Expand Down
15 changes: 4 additions & 11 deletions src/ast/project.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,10 @@ use core::str::FromStr;
use super::*;

/// Represents different types of databases.
#[derive(Debug, Clone, Default)]
#[derive(Debug, Clone)]
pub enum DatabaseType {
#[default]
Undef,
PostgreSQL,
Oracle,
MySQL,
MongoDB,
MSSQL,
SQLite,
MariaDB,
Unknown(String)
}

impl FromStr for DatabaseType {
Expand All @@ -24,7 +17,7 @@ impl FromStr for DatabaseType {
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
"PostgreSQL" => Ok(Self::PostgreSQL),
_ => Err(format!("'{}' database is not supported", s)),
_ => Ok(Self::Unknown(String::from(s))),
}
}
}
Expand All @@ -39,7 +32,7 @@ pub struct ProjectBlock {
/// An identifier of the project block.
pub ident: Ident,
/// The database type associated with the project block.
pub database_type: DatabaseType,
pub database_type: Option<DatabaseType>,
/// The note block associated with the project block.
pub note: Option<NoteBlock>,
}
2 changes: 1 addition & 1 deletion src/ast/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ pub enum TopLevelBlock {
}

impl<'a> SchemaBlock<'a> {
pub fn project(&self) -> Vec<&ProjectBlock> {
pub fn projects(&self) -> Vec<&ProjectBlock> {
self
.blocks
.iter()
Expand Down
9 changes: 3 additions & 6 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,8 @@ pub(crate) mod utils;
#[cfg(feature = "utils")]
pub mod utils;

pub use analyzer::*;
pub use parser::{
parse as parse_dbml_unchecked,
Rule,
};
use analyzer::*;
pub use parser::parse as parse_dbml_unchecked;

/// Default database schema if not specified in a DBML file.
pub const DEFAULT_SCHEMA: &str = "public";
Expand Down Expand Up @@ -59,7 +56,7 @@ pub const DEFAULT_SCHEMA: &str = "public";
/// }
/// }
/// ```
pub fn parse_dbml(input: &str) -> Result<SchemaBlock, ParseError<Rule>> {
pub fn parse_dbml(input: &str) -> Result<SchemaBlock, ParseError<parser::Rule>> {
let ast = parse_dbml_unchecked(input)?;

analyze(&ast).map(|_| ast)
Expand Down
2 changes: 1 addition & 1 deletion src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ fn parse_project_decl(pair: Pair<Rule>) -> ParserResult<ProjectBlock> {
"database_type" => {
if let Value::String(db_name) = prop.value.value.clone() {
acc.database_type = match DatabaseType::from_str(&db_name) {
Ok(val) => val,
Ok(val) => Some(val),
Err(msg) => throw_msg(msg, p2)?,
}
}
Expand Down
4 changes: 3 additions & 1 deletion tests/out/project.in.ron
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@ SchemaBlock {
raw: "ecommerce",
to_string: "ecommerce",
},
database_type: PostgreSQL,
database_type: Some(
PostgreSQL,
),
note: Some(
NoteBlock {
span_range: 22..116,
Expand Down
4 changes: 3 additions & 1 deletion tests/out/sample_1.ron
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@ SchemaBlock {
raw: "project_name",
to_string: "project_name",
},
database_type: PostgreSQL,
database_type: Some(
PostgreSQL,
),
note: Some(
NoteBlock {
span_range: 55..89,
Expand Down
4 changes: 3 additions & 1 deletion tests/out/sea_orm_codegen.ron
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@ SchemaBlock {
raw: "sea_orm_codegen",
to_string: "sea_orm_codegen",
},
database_type: PostgreSQL,
database_type: Some(
PostgreSQL,
),
note: None,
},
),
Expand Down

0 comments on commit 54e3c82

Please sign in to comment.