Skip to content

Commit

Permalink
Escape Rust keywords used in table names
Browse files Browse the repository at this point in the history
  • Loading branch information
andy128k committed Sep 18, 2022
1 parent 4e51b88 commit 750235a
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 6 deletions.
8 changes: 5 additions & 3 deletions sea-orm-codegen/src/entity/base_entity.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
use crate::{Column, ConjunctRelation, DateTimeCrate, PrimaryKey, Relation};
use crate::{
util::escape_rust_keyword, Column, ConjunctRelation, DateTimeCrate, PrimaryKey, Relation,
};
use heck::{CamelCase, SnakeCase};
use proc_macro2::{Ident, TokenStream};
use quote::format_ident;
Expand All @@ -22,11 +24,11 @@ impl Entity {
}

pub fn get_table_name_snake_case_ident(&self) -> Ident {
format_ident!("{}", self.get_table_name_snake_case())
format_ident!("{}", escape_rust_keyword(self.get_table_name_snake_case()))
}

pub fn get_table_name_camel_case_ident(&self) -> Ident {
format_ident!("{}", self.get_table_name_camel_case())
format_ident!("{}", escape_rust_keyword(self.get_table_name_camel_case()))
}

pub fn get_column_names_snake_case(&self) -> Vec<Ident> {
Expand Down
7 changes: 6 additions & 1 deletion sea-orm-codegen/src/entity/relation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ use proc_macro2::{Ident, TokenStream};
use quote::{format_ident, quote};
use sea_query::{ForeignKeyAction, TableForeignKey};

use crate::util::escape_rust_keyword;

#[derive(Clone, Debug)]
pub enum RelationType {
HasOne,
Expand Down Expand Up @@ -40,7 +42,10 @@ impl Relation {
if self.self_referencing {
None
} else {
Some(format_ident!("{}", self.ref_table.to_snake_case()))
Some(format_ident!(
"{}",
escape_rust_keyword(self.ref_table.to_snake_case())
))
}
}

Expand Down
7 changes: 5 additions & 2 deletions sea-orm-codegen/src/entity/writer.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{ActiveEnum, Entity};
use crate::{util::escape_rust_keyword, ActiveEnum, Entity};
use heck::CamelCase;
use proc_macro2::TokenStream;
use quote::{format_ident, quote};
Expand Down Expand Up @@ -543,7 +543,10 @@ impl EntityWriter {
}

pub fn gen_mod(entity: &Entity) -> TokenStream {
let table_name_snake_case_ident = entity.get_table_name_snake_case_ident();
let table_name_snake_case_ident = format_ident!(
"{}",
escape_rust_keyword(entity.get_table_name_snake_case_ident())
);
quote! {
pub mod #table_name_snake_case_ident;
}
Expand Down

0 comments on commit 750235a

Please sign in to comment.