Manage schema_name attribute, add prelude export, update example
This commit is contained in:
parent
a4b4f4925f
commit
c273cf0a39
@ -1,37 +1,13 @@
|
|||||||
use sea_orm::entity::prelude::*;
|
use sea_orm::entity::prelude::*;
|
||||||
|
|
||||||
#[derive(Copy, Clone, Default, Debug, DeriveEntity)]
|
#[derive(Clone, Debug, PartialEq, DeriveModel, DeriveActiveModel, EntityModel)]
|
||||||
pub struct Entity;
|
#[sea_orm(table_name = "cake")]
|
||||||
|
|
||||||
impl EntityName for Entity {
|
|
||||||
fn table_name(&self) -> &str {
|
|
||||||
"cake"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Clone, Debug, PartialEq, DeriveModel, DeriveActiveModel)]
|
|
||||||
pub struct Model {
|
pub struct Model {
|
||||||
|
#[sea_orm(primary_key)]
|
||||||
pub id: i32,
|
pub id: i32,
|
||||||
pub name: String,
|
pub name: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Copy, Clone, Debug, EnumIter, DeriveColumn)]
|
|
||||||
pub enum Column {
|
|
||||||
Id,
|
|
||||||
Name,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Copy, Clone, Debug, EnumIter, DerivePrimaryKey)]
|
|
||||||
pub enum PrimaryKey {
|
|
||||||
Id,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl PrimaryKeyTrait for PrimaryKey {
|
|
||||||
fn auto_increment() -> bool {
|
|
||||||
true
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Copy, Clone, Debug, EnumIter)]
|
#[derive(Copy, Clone, Debug, EnumIter)]
|
||||||
pub enum Relation {
|
pub enum Relation {
|
||||||
Fruit,
|
Fruit,
|
||||||
|
@ -16,7 +16,7 @@ path = "src/lib.rs"
|
|||||||
proc-macro = true
|
proc-macro = true
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
syn = { version = "^1", default-features = false, features = [ "full", "derive", "clone-impls", "parsing", "proc-macro", "printing" ] }
|
syn = { version = "^1", default-features = false, features = [ "full", "derive", "clone-impls", "parsing", "proc-macro", "printing", "extra-traits" ] }
|
||||||
quote = "^1"
|
quote = "^1"
|
||||||
heck = "^0.3"
|
heck = "^0.3"
|
||||||
proc-macro2 = "^1"
|
proc-macro2 = "^1"
|
||||||
|
@ -6,33 +6,44 @@ use syn::{Attribute, Data, Fields, Lit, Meta, parse::Error, punctuated::Punctuat
|
|||||||
use convert_case::{Case, Casing};
|
use convert_case::{Case, Casing};
|
||||||
|
|
||||||
pub fn expand_derive_entity_model(data: Data, attrs: Vec<Attribute>) -> syn::Result<TokenStream> {
|
pub fn expand_derive_entity_model(data: Data, attrs: Vec<Attribute>) -> syn::Result<TokenStream> {
|
||||||
// if #[sea_orm(table_name = "foo")] specified, create Entity struct
|
// if #[sea_orm(table_name = "foo", schema_name = "bar")] specified, create Entity struct
|
||||||
let table_name = attrs.iter().filter_map(|attr| {
|
let mut table_name = None;
|
||||||
if attr.path.get_ident()? != "sea_orm" {
|
let mut schema_name = quote! { None };
|
||||||
return None;
|
attrs.iter().for_each(|attr| {
|
||||||
|
if attr.path.get_ident().map(|i| i == "sea_orm") != Some(true) {
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
let list = attr.parse_args_with(Punctuated::<Meta, Comma>::parse_terminated).ok()?;
|
if let Ok(list) = attr.parse_args_with(Punctuated::<Meta, Comma>::parse_terminated) {
|
||||||
for meta in list.iter() {
|
for meta in list.iter() {
|
||||||
if let Meta::NameValue(nv) = meta {
|
if let Meta::NameValue(nv) = meta {
|
||||||
if nv.path.get_ident()? == "table_name" {
|
if let Some(ident) = nv.path.get_ident() {
|
||||||
let table_name = &nv.lit;
|
if ident == "table_name" {
|
||||||
return Some(quote! {
|
table_name = Some(nv.lit.clone());
|
||||||
|
}
|
||||||
|
else if ident == "schema_name" {
|
||||||
|
let name = &nv.lit;
|
||||||
|
schema_name = quote! { Some(#name) };
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
let entity_def = table_name.map(|table_name| quote! {
|
||||||
#[derive(Copy, Clone, Default, Debug, sea_orm::prelude::DeriveEntity)]
|
#[derive(Copy, Clone, Default, Debug, sea_orm::prelude::DeriveEntity)]
|
||||||
pub struct Entity;
|
pub struct Entity;
|
||||||
|
|
||||||
impl sea_orm::prelude::EntityName for Entity {
|
impl sea_orm::prelude::EntityName for Entity {
|
||||||
|
fn schema_name(&self) -> &str {
|
||||||
|
#schema_name
|
||||||
|
}
|
||||||
|
|
||||||
fn table_name(&self) -> &str {
|
fn table_name(&self) -> &str {
|
||||||
#table_name
|
#table_name
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
}).unwrap_or_default();
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
None
|
|
||||||
}).next().unwrap_or_default();
|
|
||||||
|
|
||||||
// generate Column enum and it's ColumnTrait impl
|
// generate Column enum and it's ColumnTrait impl
|
||||||
let mut columns_enum: Punctuated<_, Comma> = Punctuated::new();
|
let mut columns_enum: Punctuated<_, Comma> = Punctuated::new();
|
||||||
@ -208,7 +219,7 @@ impl sea_orm::prelude::ColumnTrait for Column {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#table_name
|
#entity_def
|
||||||
|
|
||||||
#primary_key
|
#primary_key
|
||||||
})
|
})
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
pub use crate::{
|
pub use crate::{
|
||||||
error::*, ActiveModelBehavior, ActiveModelTrait, ColumnDef, ColumnTrait, ColumnType,
|
error::*, ActiveModelBehavior, ActiveModelTrait, ColumnDef, ColumnTrait, ColumnType,
|
||||||
DeriveActiveModel, DeriveActiveModelBehavior, DeriveColumn, DeriveCustomColumn, DeriveEntity,
|
DeriveActiveModel, DeriveActiveModelBehavior, DeriveColumn, DeriveCustomColumn, DeriveEntity,
|
||||||
DeriveModel, DerivePrimaryKey, EntityName, EntityTrait, EnumIter, Iden, IdenStatic, ModelTrait,
|
DeriveModel, DerivePrimaryKey, EntityModel, EntityName, EntityTrait, EnumIter, Iden, IdenStatic, ModelTrait,
|
||||||
PrimaryKeyToColumn, PrimaryKeyTrait, QueryFilter, QueryResult, Related, RelationDef,
|
PrimaryKeyToColumn, PrimaryKeyTrait, QueryFilter, QueryResult, Related, RelationDef,
|
||||||
RelationTrait, Select, Value,
|
RelationTrait, Select, Value,
|
||||||
};
|
};
|
||||||
|
@ -219,7 +219,7 @@ pub use query::*;
|
|||||||
|
|
||||||
pub use sea_orm_macros::{
|
pub use sea_orm_macros::{
|
||||||
DeriveActiveModel, DeriveActiveModelBehavior, DeriveColumn, DeriveCustomColumn, DeriveEntity,
|
DeriveActiveModel, DeriveActiveModelBehavior, DeriveColumn, DeriveCustomColumn, DeriveEntity,
|
||||||
DeriveModel, DerivePrimaryKey, FromQueryResult,
|
DeriveModel, DerivePrimaryKey, FromQueryResult, EntityModel,
|
||||||
};
|
};
|
||||||
|
|
||||||
pub use sea_query;
|
pub use sea_query;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user