Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,5 +57,12 @@ pub fn parse() -> ArgMatches {
.action(ArgAction::SetTrue)
.default_value("false"),
)
.arg(
Arg::new("direction")
.long("direction")
.value_parser(["tb", "bt", "lr", "rl"])
.help("Optionally set the direction of the generated diagram")
.action(ArgAction::Set),
)
.get_matches()
}
10 changes: 10 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ pub struct GeneratorConfigOptions {
pub draw_legend: bool,
pub inline_puml_lib: bool,
pub conceptual_diagram: bool,
pub direction: Option<Direction>,
}

pub trait ViewGenerator {
Expand Down Expand Up @@ -54,3 +55,12 @@ pub fn get_generator(generator_type: GeneratorType) -> Result<Box<dyn ViewGenera
GeneratorType::Mermaid => Ok(Box::new(MermaidGenerator::new()?)),
}
}

#[derive(Clone, Debug, Display, EnumString, Eq, PartialEq, PartialOrd, Ord)]
#[strum(serialize_all = "lowercase")]
pub enum Direction {
TB,
BT,
LR,
RL,
}
7 changes: 6 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::str::FromStr;

use clap::ArgMatches;
use sqlant::{get_generator, lookup_parser, GeneratorConfigOptions, GeneratorType};
use sqlant::{get_generator, lookup_parser, Direction, GeneratorConfigOptions, GeneratorType};

fn get_arg(args: &ArgMatches, arg_name: &str) -> String {
args.get_one::<String>(arg_name).unwrap().to_string()
Expand All @@ -22,6 +22,10 @@ async fn main() {
let generator_type =
GeneratorType::from_str(&output_arg).expect("Generator type {output_arg} isn't supported");
let rndr = get_generator(generator_type).unwrap();
let direction_arg = args.get_one::<String>("direction");
let direction = direction_arg.map(|dir| {
Direction::from_str(dir).unwrap_or_else(|_| panic!("Direction {dir} isn't supported"))
});
let result = rndr
.generate(
erd,
Expand All @@ -31,6 +35,7 @@ async fn main() {
draw_legend: args.get_flag("legend"),
inline_puml_lib: args.get_flag("inline-puml-lib"),
conceptual_diagram: args.get_flag("conceptual"),
direction,
},
)
.unwrap();
Expand Down
32 changes: 26 additions & 6 deletions src/mermaid_generator.rs
Original file line number Diff line number Diff line change
@@ -1,23 +1,22 @@
use std::sync::Arc;

use super::sql_entities::{SqlERData, Table, TableColumn};
use crate::{GeneratorConfigOptions, ViewGenerator};
use crate::{Direction, GeneratorConfigOptions, ViewGenerator};
use serde::Serialize;
use tinytemplate::{format_unescaped, TinyTemplate};
use tinytemplate::{TinyTemplate, format_unescaped};

static MERMAID_TEMPLATE: &str = r#"erDiagram
{{ if direction }}direction {direction}{{ endif }}
{{ for ent in entities}}{ent}{{ endfor }}
{{ for en in enums}}{en}{{ endfor }}
{{ for fk in foreign_keys}}{fk}{{ endfor }}
"#;

static ENTITY_TEMPLATE: &str = "{name} \\{\n{pks}{fks}{others}}\n";

static COLUMN_TEMPLATE: &str =
" {col.datatype} {col.name}{{ if is_pk_or_fk }} {{ endif }}{{ if is_pk }}PK,{{ endif }}{{ if is_fk }}FK{{ endif }}";
static COLUMN_TEMPLATE: &str = " {col.datatype} {col.name}{{ if is_pk_or_fk }} {{ endif }}{{ if is_pk }}PK,{{ endif }}{{ if is_fk }}FK{{ endif }}";

static REL_TEMPLATE: &str =
"{source_table_name} {{ if is_zero_one_to_one }}|o--||{{else}}}o--||{{ endif }} {target_table_name}: \"\"\n";
static REL_TEMPLATE: &str = "{source_table_name} {{ if is_zero_one_to_one }}|o--||{{else}}}o--||{{ endif }} {target_table_name}: \"\"\n";

const ENUM_TEMPLATE: &str = "\"{name} (ENUM)\" \\{\n{{ for v in values}} {v} _\n{{ endfor }}}";

Expand All @@ -38,8 +37,28 @@ struct SColumn<'a> {
is_nn: bool,
}

#[derive(Serialize)]
enum SDirection {
TB,
BT,
LR,
RL,
}

impl From<&Direction> for SDirection {
fn from(value: &Direction) -> Self {
match value {
Direction::TB => Self::TB,
Direction::BT => Self::BT,
Direction::LR => Self::LR,
Direction::RL => Self::RL,
}
}
}

#[derive(Serialize)]
struct SMermaid {
direction: Option<SDirection>,
entities: Vec<String>,
enums: Vec<String>,
foreign_keys: Vec<String>,
Expand Down Expand Up @@ -184,6 +203,7 @@ impl ViewGenerator for MermaidGenerator<'_> {
Ok(self.str_templates.render(
"mermaid",
&SMermaid {
direction: opts.direction.as_ref().map(Into::into),
entities,
enums,
foreign_keys,
Expand Down
35 changes: 31 additions & 4 deletions src/plantuml_generator.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
use std::sync::Arc;

use super::sql_entities::{SqlERData, Table, TableColumn};
use crate::{sql_entities::View, GeneratorConfigOptions, ViewGenerator};
use crate::{Direction, GeneratorConfigOptions, ViewGenerator, sql_entities::View};
use serde::Serialize;
use tinytemplate::{format_unescaped, TinyTemplate};
use tinytemplate::{TinyTemplate, format_unescaped};

pub struct PlantUmlDefaultGenerator<'a> {
str_templates: TinyTemplate<'a>,
}

static PUML_TEMPLATE: &str = "@startuml\n\n\
{{ if direction }}{ direction }\n{{ endif }}\
hide circle\n\
hide empty members\n\
skinparam linetype ortho\n\n\
Expand All @@ -27,8 +28,7 @@ static VIEW_TEMPLATE: &str =

static COLUMN_TEMPLATE: &str = " column({col.name}, \"{col.datatype}\"{{ if is_pk }}, $pk=true{{ endif }}{{ if is_fk }}, $fk=true{{ endif }}{{if is_nn}}, $nn=true{{ endif }})\n";

static REL_TEMPLATE: &str =
"{source_table_name} {{ if is_zero_one_to_one }}|o--||{{else}}}o--||{{ endif }} {target_table_name}\n";
static REL_TEMPLATE: &str = "{source_table_name} {{ if is_zero_one_to_one }}|o--||{{else}}}o--||{{ endif }} {target_table_name}\n";

static ENUM_TEMPLATE: &str =
"enum({name}, \"{{ for v in values}}{{if @last}}{v}{{else}}{v}, {{ endif }}{{ endfor }}\")\n";
Expand Down Expand Up @@ -69,6 +69,25 @@ struct SEntity {
#[derive(Serialize)]
struct SLegend(String);

#[derive(Serialize)]
struct SDirection(String);

impl TryFrom<&Direction> for SDirection {
type Error = crate::SqlantError;
fn try_from(value: &Direction) -> Result<Self, crate::SqlantError> {
match value {
Direction::TB => Ok(Self("top to bottom direction".into())),
Direction::BT => Err(crate::SqlantError::Generator(
"bt (bottom-to-top) direction is not available in plantuml.".into(),
)),
Direction::LR => Ok(Self("left to right direction".into())),
Direction::RL => Err(crate::SqlantError::Generator(
"rl (right-to-left) direction is not available in plantuml.".into(),
)),
}
}
}

#[derive(Serialize)]
struct SPuml {
puml_lib: String,
Expand All @@ -78,6 +97,7 @@ struct SPuml {
foreign_keys: Vec<String>,
enums: Vec<String>,
legend: Option<SLegend>,
direction: Option<SDirection>,
}

#[derive(Serialize)]
Expand Down Expand Up @@ -304,6 +324,12 @@ impl ViewGenerator for PlantUmlDefaultGenerator<'_> {
PUML_LIB_INCLUDE.into()
};

let direction = if let Some(direction) = &opts.direction {
Some(SDirection::try_from(direction)?)
} else {
None
};

Ok(self.str_templates.render(
"puml",
&SPuml {
Expand All @@ -313,6 +339,7 @@ impl ViewGenerator for PlantUmlDefaultGenerator<'_> {
enums,
legend,
views,
direction,
},
)?)
}
Expand Down