[sea-orm-cli] generate entity with relation variant order by name of reference table (#1229)

This commit is contained in:
Billy Chan 2022-11-24 12:15:31 +08:00 committed by GitHub
parent 80c81eaffe
commit a7c8970800
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 15 additions and 9 deletions

View File

@ -3,17 +3,17 @@ use crate::{
PrimaryKey, Relation, RelationType,
};
use sea_query::{ColumnSpec, TableCreateStatement};
use std::collections::HashMap;
use std::collections::BTreeMap;
#[derive(Clone, Debug)]
pub struct EntityTransformer;
impl EntityTransformer {
pub fn transform(table_create_stmts: Vec<TableCreateStatement>) -> Result<EntityWriter, Error> {
let mut enums: HashMap<String, ActiveEnum> = HashMap::new();
let mut inverse_relations: HashMap<String, Vec<Relation>> = HashMap::new();
let mut conjunct_relations: HashMap<String, Vec<ConjunctRelation>> = HashMap::new();
let mut entities = HashMap::new();
let mut enums: BTreeMap<String, ActiveEnum> = BTreeMap::new();
let mut inverse_relations: BTreeMap<String, Vec<Relation>> = BTreeMap::new();
let mut conjunct_relations: BTreeMap<String, Vec<ConjunctRelation>> = BTreeMap::new();
let mut entities = BTreeMap::new();
for table_create in table_create_stmts.into_iter() {
let table_name = match table_create.get_table_name() {
Some(table_ref) => match table_ref {
@ -71,7 +71,7 @@ impl EntityTransformer {
col
})
.collect();
let mut ref_table_counts: HashMap<String, usize> = HashMap::new();
let mut ref_table_counts: BTreeMap<String, usize> = BTreeMap::new();
let relations: Vec<Relation> = table_create
.get_foreign_key_create_stmts()
.iter()
@ -202,7 +202,13 @@ impl EntityTransformer {
}
}
Ok(EntityWriter {
entities: entities.into_iter().map(|(_, v)| v).collect(),
entities: entities
.into_iter()
.map(|(_, mut v)| {
v.relations.sort_by(|a, b| a.ref_table.cmp(&b.ref_table));
v
})
.collect(),
enums,
})
}

View File

@ -2,14 +2,14 @@ use crate::{util::escape_rust_keyword, ActiveEnum, Entity};
use heck::CamelCase;
use proc_macro2::TokenStream;
use quote::{format_ident, quote};
use std::{collections::HashMap, str::FromStr};
use std::{collections::BTreeMap, str::FromStr};
use syn::{punctuated::Punctuated, token::Comma};
use tracing::info;
#[derive(Clone, Debug)]
pub struct EntityWriter {
pub(crate) entities: Vec<Entity>,
pub(crate) enums: HashMap<String, ActiveEnum>,
pub(crate) enums: BTreeMap<String, ActiveEnum>,
}
pub struct WriterOutput {