Skip to content

Commit ec358bb

Browse files
committed
keep column declaration ordering
1 parent 31c4295 commit ec358bb

2 files changed

Lines changed: 27 additions & 41 deletions

File tree

src/algos/convertor.rs

Lines changed: 7 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ enum ColumnConstraint {
2121

2222
fn statement_mermaid(
2323
statement: Statement,
24-
table_column_type: &mut HashMap<String, HashMap<String, DataType>>,
24+
table_column_type: &mut HashMap<String, Vec<(String, DataType)>>,
2525
table_colum_constraints: &mut HashMap<String, HashMap<String, HashSet<ColumnConstraint>>>,
2626
column_foreign_key_column: &mut Vec<(TableColumn, TableColumn, String)>,
2727
) -> String {
@@ -47,22 +47,11 @@ fn statement_mermaid(
4747
like,
4848
} => {
4949
let mut column_constraints: HashMap<String, HashSet<ColumnConstraint>> = HashMap::new();
50-
let mut column_type: HashMap<String, DataType> = HashMap::new();
50+
let mut column_type: Vec<(String, DataType)> = Vec::new();
5151
mermaid.push_str(&format!("\t{} {{\n", name.to_string().replace('"', "")));
5252

5353
for column in columns {
54-
column_type.insert(column.name.to_string(), column.data_type);
55-
// mermaid.push_str(&format!(
56-
// "\t\t{} {}\n",
57-
// column
58-
// .data_type
59-
// .to_string()
60-
// .replace(' ', "_")
61-
// .replace('(', "")
62-
// .replace(')', "")
63-
// .replace(',', "_"),
64-
// column.name.to_string().replace('"', ""),
65-
// ));
54+
column_type.push((column.name.to_string(), column.data_type));
6655
for ColumnOptionDef {
6756
name: option_name,
6857
option,
@@ -219,7 +208,7 @@ pub fn sql_s_mermaid(sql: &str) -> String {
219208
let mut table_column_constraints: HashMap<String, HashMap<String, HashSet<ColumnConstraint>>> =
220209
HashMap::new();
221210
let mut column_foreign_key_column: Vec<(TableColumn, TableColumn, String)> = Vec::new();
222-
let mut table_column_type: HashMap<String, HashMap<String, DataType>> = HashMap::new();
211+
let mut table_column_type: HashMap<String, Vec<(String, DataType)>> = HashMap::new();
223212

224213
let mut mermaid: String = "erDiagram\n".to_string();
225214

@@ -271,10 +260,10 @@ pub fn sql_s_mermaid(sql: &str) -> String {
271260
// draw links
272261
for ((l_table, l_column), (r_table, r_column), foreign_key) in column_foreign_key_column {
273262
mermaid.push_str(&format!(
274-
"\t{} }}o--{}{} {} : \"{}\"\n",
263+
"\t{} {}{}--{}{} {} : \"{}\"\n",
275264
l_table,
276-
// one_or_many_relation(Side::Left, &l_table, &l_column, &table_column_constraints,),
277-
// zero_or_one_relation(&l_table, &l_column, &table_column_constraints,),
265+
one_or_many_relation(Side::Left, &l_table, &l_column, &table_column_constraints,),
266+
"o".to_string(), //zero_or_one_relation(&l_table, &l_column, &table_column_constraints,),
278267
zero_or_one_relation(&r_table, &r_column, &table_column_constraints,),
279268
one_or_many_relation(Side::Right, &r_table, &r_column, &table_column_constraints,),
280269
r_table,

src/routes/home.rs

Lines changed: 20 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -15,30 +15,27 @@ enum Tabs {
1515
Code,
1616
}
1717

18-
const INIT_SQL: &'static str = r#"CREATE TABLE CUSTOMERS(
19-
ID INT NOT NULL,
20-
NAME VARCHAR (20) NOT NULL,
21-
AGE INT NOT NULL,
22-
ADDRESS INT NOT NULL,
23-
SALARY DECIMAL (18, 2),
24-
PRIMARY KEY (ID),
25-
CONSTRAINT FK_Address FOREIGN KEY (ADDRESS)
26-
REFERENCES ADDRESS(ID)
27-
);
28-
CREATE TABLE ORDERS (
29-
ID INT NOT NULL,
30-
DATE DATETIME,
31-
CUSTOMER_ID INT references CUSTOMERS(ID),
32-
AMOUNT double,
33-
PRIMARY KEY (ID)
34-
);
18+
const INIT_SQL: &'static str = r#"CREATE TABLE Student(
19+
StudentID INT PRIMARY KEY,
20+
Name VARCHAR(100)
21+
);
22+
CREATE TABLE Class(
23+
ClassID INT PRIMARY KEY,
24+
Course VARCHAR(100),
25+
);
26+
27+
CREATE TABLE StudentClassRelation(
28+
StudentID INT NOT NULL,
29+
ClassID INT NOT NULL,FOREIGN KEY (StudentID) REFERENCES Student(StudentID),
30+
FOREIGN KEY (ClassID) REFERENCES Class(ClassID),
31+
UNIQUE (StudentID, ClassID)
32+
);
3533
36-
CREATE TABLE ADDRESS (
37-
ID INT NOT NULL,
38-
COUNTRY VARCHAR (20),
39-
CITY VARCHAR (20),
40-
PRIMARY KEY (ID)
41-
)
34+
CREATE TABLE StudentDetails(
35+
StudentID INT UNIQUE NOT NULL,
36+
Email VARCHAR(100),
37+
CONSTRAINT private_info FOREIGN KEY (StudentID) REFERENCES Student(StudentID)
38+
);
4239
"#;
4340

4441
#[function_component(Home)]

0 commit comments

Comments
 (0)